Skip to content

Commit d8e4146

Browse files
committed
Add support for Boolean, Double, and Float arrays
[closes #394] Signed-off-by: Mark Paluch <[email protected]>
1 parent 3a113e2 commit d8e4146

File tree

6 files changed

+253
-3
lines changed

6 files changed

+253
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,14 @@ Support for the following single-dimensional arrays (read and write):
377377

378378
| PostgreSQL Type | Supported Data Type |
379379
|:-----------------------------------------------|:-------------------------------------|
380-
|[`text[]`][psql-text-ref] |[`String[]`][java-string-ref] |
381-
|[`integer[] or int[]`][psql-integer-ref] |[`Integer[]`][java-integer-ref], [`Long[]`][java-long-ref], [`Short[]`][java-short-ref]|
380+
|[`bool[]`][psql-boolean-ref] |[`Boolean[]`][java-boolean-ref] |
381+
|[`double precision`][psql-floating-point-ref] |[`Double`][java-double-ref] |
382+
|[`integer[] or int[]`][psql-integer-ref] |[`Integer[]`][java-integer-ref] |
382383
|[`numeric[]`][psql-bignumeric-ref] |[`BigDecimal[]`][java-bigdecimal-ref] |
383-
|[`uuid[]`][psql-uuid-ref] |[`UUID[]`][java-uuid-ref]|
384+
|[`real`][psql-real-ref] |[`Float[]`][java-float-ref] |
385+
|[`serial[]`][psql-serial-ref] |[`Long[]`][java-long-ref] |
386+
|[`text[]`][psql-text-ref] |[`String[]`][java-string-ref] |
387+
|[`uuid[]`][psql-uuid-ref] |[`UUID[]`][java-uuid-ref] |
384388

385389
[psql-bigint-ref]: https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-INT
386390
[psql-bit-ref]: https://www.postgresql.org/docs/current/datatype-numeric.html
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.postgresql.codec;
18+
19+
import io.netty.buffer.ByteBuf;
20+
import io.netty.buffer.ByteBufAllocator;
21+
import io.r2dbc.postgresql.client.Parameter;
22+
import io.r2dbc.postgresql.message.Format;
23+
import io.r2dbc.postgresql.type.PostgresqlObjectId;
24+
import io.r2dbc.postgresql.util.Assert;
25+
26+
import java.util.function.Supplier;
27+
28+
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
29+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.BOOL_ARRAY;
30+
31+
/**
32+
* @since 0.8.8
33+
*/
34+
final class BooleanArrayCodec extends AbstractArrayCodec<Boolean> {
35+
36+
BooleanArrayCodec(ByteBufAllocator byteBufAllocator) {
37+
super(byteBufAllocator, Boolean.class);
38+
}
39+
40+
@Override
41+
public Parameter encodeNull() {
42+
return createNull(BOOL_ARRAY, FORMAT_TEXT);
43+
}
44+
45+
@Override
46+
Boolean doDecodeBinary(ByteBuf byteBuffer) {
47+
return byteBuffer.readBoolean();
48+
}
49+
50+
@Override
51+
Boolean doDecodeText(String text) {
52+
53+
return "1".equals(text)
54+
|| "true".equalsIgnoreCase(text)
55+
|| "t".equalsIgnoreCase(text)
56+
|| "yes".equalsIgnoreCase(text)
57+
|| "y".equalsIgnoreCase(text)
58+
|| "on".equalsIgnoreCase(text);
59+
}
60+
61+
@Override
62+
boolean doCanDecode(PostgresqlObjectId type, Format format) {
63+
Assert.requireNonNull(type, "type must not be null");
64+
65+
return BOOL_ARRAY == type;
66+
}
67+
68+
@Override
69+
Parameter encodeArray(Supplier<ByteBuf> encodedSupplier) {
70+
return create(BOOL_ARRAY, FORMAT_TEXT, encodedSupplier);
71+
}
72+
73+
@Override
74+
String doEncodeText(Boolean value) {
75+
Assert.requireNonNull(value, "value must not be null");
76+
77+
return value ? "TRUE" : "FALSE";
78+
}
79+
80+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,14 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBu
101101
new BlobCodec(byteBufAllocator),
102102
new ClobCodec(byteBufAllocator),
103103

104+
new BooleanArrayCodec(byteBufAllocator),
104105
new BigDecimalArrayCodec(byteBufAllocator),
105106
new ShortArrayCodec(byteBufAllocator),
106107
new StringArrayCodec(byteBufAllocator),
107108
new IntegerArrayCodec(byteBufAllocator),
108109
new LongArrayCodec(byteBufAllocator),
110+
new FloatArrayCodec(byteBufAllocator),
111+
new DoubleArrayCodec(byteBufAllocator),
109112
new UuidArrayCodec(byteBufAllocator),
110113

111114
//Geometry
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.postgresql.codec;
18+
19+
import io.netty.buffer.ByteBuf;
20+
import io.netty.buffer.ByteBufAllocator;
21+
import io.r2dbc.postgresql.client.Parameter;
22+
import io.r2dbc.postgresql.message.Format;
23+
import io.r2dbc.postgresql.type.PostgresqlObjectId;
24+
import io.r2dbc.postgresql.util.Assert;
25+
26+
import java.util.function.Supplier;
27+
28+
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
29+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.FLOAT8_ARRAY;
30+
31+
/**
32+
* @since 0.8.8
33+
*/
34+
final class DoubleArrayCodec extends AbstractArrayCodec<Double> {
35+
36+
DoubleArrayCodec(ByteBufAllocator byteBufAllocator) {
37+
super(byteBufAllocator, Double.class);
38+
}
39+
40+
@Override
41+
public Parameter encodeNull() {
42+
return createNull(FLOAT8_ARRAY, FORMAT_TEXT);
43+
}
44+
45+
@Override
46+
Double doDecodeBinary(ByteBuf byteBuffer) {
47+
return byteBuffer.readDouble();
48+
}
49+
50+
@Override
51+
Double doDecodeText(String text) {
52+
return Double.parseDouble(text);
53+
}
54+
55+
@Override
56+
boolean doCanDecode(PostgresqlObjectId type, Format format) {
57+
Assert.requireNonNull(type, "type must not be null");
58+
59+
return FLOAT8_ARRAY == type;
60+
}
61+
62+
@Override
63+
Parameter encodeArray(Supplier<ByteBuf> encodedSupplier) {
64+
return create(FLOAT8_ARRAY, FORMAT_TEXT, encodedSupplier);
65+
}
66+
67+
@Override
68+
String doEncodeText(Double value) {
69+
Assert.requireNonNull(value, "value must not be null");
70+
71+
return value.toString();
72+
}
73+
74+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.postgresql.codec;
18+
19+
import io.netty.buffer.ByteBuf;
20+
import io.netty.buffer.ByteBufAllocator;
21+
import io.r2dbc.postgresql.client.Parameter;
22+
import io.r2dbc.postgresql.message.Format;
23+
import io.r2dbc.postgresql.type.PostgresqlObjectId;
24+
import io.r2dbc.postgresql.util.Assert;
25+
26+
import java.util.function.Supplier;
27+
28+
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
29+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.FLOAT4_ARRAY;
30+
31+
/**
32+
* @since 0.8.8
33+
*/
34+
final class FloatArrayCodec extends AbstractArrayCodec<Float> {
35+
36+
FloatArrayCodec(ByteBufAllocator byteBufAllocator) {
37+
super(byteBufAllocator, Float.class);
38+
}
39+
40+
@Override
41+
public Parameter encodeNull() {
42+
return createNull(FLOAT4_ARRAY, FORMAT_TEXT);
43+
}
44+
45+
@Override
46+
Float doDecodeBinary(ByteBuf byteBuffer) {
47+
return byteBuffer.readFloat();
48+
}
49+
50+
@Override
51+
Float doDecodeText(String text) {
52+
return Float.parseFloat(text);
53+
}
54+
55+
@Override
56+
boolean doCanDecode(PostgresqlObjectId type, Format format) {
57+
Assert.requireNonNull(type, "type must not be null");
58+
59+
return FLOAT4_ARRAY == type;
60+
}
61+
62+
@Override
63+
Parameter encodeArray(Supplier<ByteBuf> encodedSupplier) {
64+
return create(FLOAT4_ARRAY, FORMAT_TEXT, encodedSupplier);
65+
}
66+
67+
@Override
68+
String doEncodeText(Float value) {
69+
Assert.requireNonNull(value, "value must not be null");
70+
71+
return value.toString();
72+
}
73+
74+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ void booleanPrimitive() {
126126
testCodec(Boolean.class, true, "BOOL");
127127
}
128128

129+
@Test
130+
void booleanArray() {
131+
testCodec(Boolean[].class, new Boolean[]{true, false}, "BOOL[]");
132+
}
133+
129134
@Test
130135
void binary() {
131136
testCodec(ByteBuffer.class, ByteBuffer.wrap(new byte[]{1, 2, 3, 4}), "BYTEA");
@@ -223,6 +228,11 @@ void doublePrimitive() {
223228
testCodec(Double.class, 100.1, "FLOAT8");
224229
}
225230

231+
@Test
232+
void doubleArray() {
233+
testCodec(Double[].class, new Double[]{100.0, 200.1}, "FLOAT8[]");
234+
}
235+
226236
@Test
227237
void simpleMappedEnum() {
228238
testCodec(MyEnum.class, MyEnum.HELLO, "my_enum");
@@ -238,6 +248,11 @@ void floatPrimitive() {
238248
testCodec(Float.class, 100.1f, "FLOAT8");
239249
}
240250

251+
@Test
252+
void floatArray() {
253+
testCodec(Float[].class, new Float[]{100.0f, 200.1f}, "FLOAT4[]");
254+
}
255+
241256
@Test
242257
void hstore() {
243258
SERVER.getJdbcOperations().execute("CREATE EXTENSION IF NOT EXISTS hstore");

0 commit comments

Comments
 (0)