Skip to content

Commit e8364d7

Browse files
Francisco Guerreromp911de
Francisco Guerrero
authored andcommitted
Implement Lseg[] codec support
[pgjdbc#394][pgjdbc#403]
1 parent f07d396 commit e8364d7

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ Support for the following single-dimensional arrays (read and write):
437437
| [`integer`[]][psql-integer-ref] | [**`Integer[]`**][java-integer-ref], [`Boolean[]`][java-boolean-ref], [`Byte[]`][java-byte-ref], [`Short[]`][java-short-ref], [`Long[]`][java-long-ref], [`BigDecimal[]`][java-bigdecimal-ref], [`BigInteger[]`][java-biginteger-ref]|
438438
| [`interval[]`][psql-interval-ref] | **`Interval[]`**|
439439
| [`line[]`][psql-line-ref] | **`Line[]`**|
440+
| [`lseg[]`][psql-lseq-ref] | **`Lseg[]`**|
440441
| [`numeric[]`][psql-bignumeric-ref] | [`BigDecimal[]`][java-bigdecimal-ref], [`Boolean[]`][java-boolean-ref], [`Byte[]`][java-byte-ref], [`Short[]`][java-short-ref], [`Integer[]`][java-integer-ref], [`Long[]`][java-long-ref], [`BigInteger[]`][java-biginteger-ref]|
441442
| [`double precision[]`][psql-floating-point-ref] | [`Double[]`][java-double-ref] |
442443
| [`point[]`][psql-point-ref] | **`Point[]`**|

src/main/java/io/r2dbc/postgresql/codec/Lseg.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public static Lseg of(Point p1, Point p2) {
3434
return new Lseg(p1, p2);
3535
}
3636

37+
/**
38+
* Create a new {@link Lseg} given parameters {@code p1x}, {@code p1y}, {@code p2x} and {@code p2y}.
39+
*
40+
* @param p1x the x axis coordinate of {@link Point point} p1
41+
* @param p1y the y axis coordinate of {@link Point point} p1
42+
* @param p2x the x axis coordinate of {@link Point point} p2
43+
* @param p2y the y axis coordinate of {@link Point point} p2
44+
* @return the new {@link Lseg} object
45+
*/
46+
public static Lseg of(double p1x, double p1y, double p2x, double p2y) {
47+
return new Lseg(Point.of(p1x, p1y), Point.of(p2x, p2y));
48+
}
49+
3750
public Point getP1() {
3851
return this.p1;
3952
}

src/test/java/io/r2dbc/postgresql/AbstractCodecIntegrationTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,21 @@ void lineTwoDimensionalArray() {
609609
testCodec(Line[][].class, new Line[][]{{Line.of(1., 2., 4.), Line.of(-10.42, 3.14, 5.24)}, {null, Line.of(3, 4, 5)}}, "LINE[][]");
610610
}
611611

612+
@Test
613+
void lsegArray() {
614+
testCodec(Lseg[].class, new Lseg[]{Lseg.of(1.11, 2.22, 3.33, 4.44), Lseg.of(6.6, 3.5, 6.6, -2.36)}, "LSEG[]");
615+
}
616+
612617
@Test
613618
void lseg() {
614619
testCodec(Lseg.class, Lseg.of(Point.of(1.11, 2.22), Point.of(3.33, 4.44)), "LSEG");
615620
}
616621

622+
@Test
623+
void lsegTwoDimensionalArray() {
624+
testCodec(Lseg[][].class, new Lseg[][]{{null, Lseg.of(1.11, 2.22, 3.33, 4.44)}, {Lseg.of(6.6, 3.5, 6.6, -2.36), null}}, "LSEG[][]");
625+
}
626+
617627
@Test
618628
void path() {
619629
testCodec(Path.class, Path.closed(Point.of(1.1, 2.2), Point.of(10.10, 10.10), Point.of(1.1, 2.2)), "PATH");
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import io.netty.buffer.ByteBuf;
4+
5+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.LSEG;
6+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.LSEG_ARRAY;
7+
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
8+
9+
/**
10+
* Unit tests for {@link ArrayCodec<Lseg>}.
11+
*/
12+
final class LsegArrayCodecUnitTests extends AbstractArrayCodecUnitTests<Lseg> {
13+
14+
private final ByteBuf SINGLE_DIM_BINARY_ARRAY = TEST
15+
.buffer()
16+
.writeInt(1) // num of dimensions
17+
.writeInt(0) // flag: has nulls
18+
.writeInt(601) // oid
19+
.writeInt(2) // num of elements
20+
.writeInt(1) // ignore Lower Bound
21+
.writeInt(32) // length of element
22+
.writeDouble(1.11) // the x axis coordinate of point p1
23+
.writeDouble(2.22) // the y axis coordinate of point p1
24+
.writeDouble(3.33) // the x axis coordinate of point p2
25+
.writeDouble(4.44) // the y axis coordinate of point p2
26+
.writeInt(32) // length of element
27+
.writeDouble(6.6) // the x axis coordinate of point p1
28+
.writeDouble(3.5) // the y axis coordinate of point p1
29+
.writeDouble(6.6) // the x axis coordinate of point p2
30+
.writeDouble(-2.36); // the y axis coordinate of point p2
31+
32+
private final ByteBuf TWO_DIM_BINARY_ARRAY = TEST
33+
.buffer()
34+
.writeInt(2) // num of dims
35+
.writeInt(1) // flag: has nulls
36+
.writeInt(601) // oid
37+
.writeInt(2) // dim 1 length
38+
.writeInt(1) // dim 1 lower bound
39+
.writeInt(1) // dim 2 length
40+
.writeInt(1) // dim 2 lower bound
41+
.writeInt(32) // length of element
42+
.writeDouble(1.11) // the x axis coordinate of point p1
43+
.writeDouble(2.22) // the y axis coordinate of point p1
44+
.writeDouble(3.33) // the x axis coordinate of point p2
45+
.writeDouble(4.44) // the y axis coordinate of point p2
46+
.writeInt(-1); // length of null element
47+
48+
@Override
49+
ArrayCodec<Lseg> createInstance() {
50+
return new ArrayCodec<>(TEST, LSEG_ARRAY, new LsegCodec(TEST), Lseg.class);
51+
}
52+
53+
@Override
54+
PostgresqlObjectId getPostgresqlObjectId() {
55+
return LSEG;
56+
}
57+
58+
@Override
59+
PostgresqlObjectId getArrayPostgresqlObjectId() {
60+
return LSEG_ARRAY;
61+
}
62+
63+
@Override
64+
ByteBuf getSingleDimensionBinaryArray() {
65+
return SINGLE_DIM_BINARY_ARRAY;
66+
}
67+
68+
@Override
69+
ByteBuf getTwoDimensionBinaryArray() {
70+
return TWO_DIM_BINARY_ARRAY;
71+
}
72+
73+
@Override
74+
Class<? extends Lseg[]> getSingleDimensionArrayType() {
75+
return Lseg[].class;
76+
}
77+
78+
@Override
79+
Class<? extends Lseg[][]> getTwoDimensionArrayType() {
80+
return Lseg[][].class;
81+
}
82+
83+
@Override
84+
Lseg[] getExpectedSingleDimensionArray() {
85+
return new Lseg[]{Lseg.of(1.11, 2.22, 3.33, 4.44), Lseg.of(6.6, 3.5, 6.6, -2.36)};
86+
}
87+
88+
@Override
89+
Lseg[][] getExpectedTwoDimensionArray() {
90+
return new Lseg[][]{{Lseg.of(1.11, 2.22, 3.33, 4.44)}, {null}};
91+
}
92+
93+
@Override
94+
String getSingleDimensionStringInput() {
95+
return "{\"[ ( 1.11, 2.22 ) , ( 3.33, 4.44 ) ]\",\"[ ( 6.6, 3.5 ) , ( 6.6, -2.36 ) ]\"}";
96+
}
97+
98+
@Override
99+
String getTwoDimensionStringInput() {
100+
return "{{\"[ ( 1.11, 2.22 ) , ( 3.33, 4.44 ) ]\"},{NULL}}";
101+
}
102+
103+
}

src/test/java/io/r2dbc/postgresql/codec/LsegCodecUnitTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ void doEncodeNoValue() {
105105
.withMessage("value must not be null");
106106
}
107107

108+
@Test
109+
void decodeText() {
110+
LsegCodec codec = new LsegCodec(TEST);
111+
112+
// Line segments are represented by pairs of points that are the endpoints of the segment.
113+
// Values of type lseg are specified using any of the following syntaxes:
114+
// [ ( x1 , y1 ) , ( x2 , y2 ) ]
115+
// ( ( x1 , y1 ) , ( x2 , y2 ) )
116+
// ( x1 , y1 ) , ( x2 , y2 )
117+
// x1 , y1 , x2 , y2
118+
assertThat(codec.decode(encode(TEST, "[(6.6,3.5), (6.6,-2.36)]"), dataType, FORMAT_TEXT, Lseg.class))
119+
.isEqualTo(Lseg.of(6.6, 3.5, 6.6, -2.36));
120+
121+
assertThat(codec.decode(encode(TEST, "((6.6,3.5), (6.6,-2.36))"), dataType, FORMAT_TEXT, Lseg.class))
122+
.isEqualTo(Lseg.of(6.6, 3.5, 6.6, -2.36));
123+
124+
assertThat(codec.decode(encode(TEST, "(6.6,3.5), (6.6,-2.36)"), dataType, FORMAT_TEXT, Lseg.class))
125+
.isEqualTo(Lseg.of(6.6, 3.5, 6.6, -2.36));
126+
127+
assertThat(codec.decode(encode(TEST, "6.6,3.5,6.6,-2.36"), dataType, FORMAT_TEXT, Lseg.class))
128+
.isEqualTo(Lseg.of(6.6, 3.5, 6.6, -2.36));
129+
}
130+
108131
@Test
109132
void encodeNull() {
110133
ParameterAssert.assertThat(new LsegCodec(TEST).encodeNull())

0 commit comments

Comments
 (0)