Skip to content

Commit 79ed3e9

Browse files
committed
Add new codecs for Integer type
1 parent 5736dda commit 79ed3e9

File tree

10 files changed

+501
-2
lines changed

10 files changed

+501
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1313
`ResultSetMetaData` for the classes `CassandraResultSet` and `CassandraMetadataResultSet`.
1414
- Implement the methods `getURL(int|String)` in the classes `CassandraResultSet` and `CassandraMetadataResultSet`. The
1515
URL values are handled as `String` values.
16+
- Add codecs for conversions between `Integer` and CQL types `varint`, `smallint` and `tinyint`. It also fixes the issue
17+
[#33](https://github.com/adejanovski/cassandra-jdbc-wrapper/issues/33) of the [original project].
1618
### Changed
1719
- Improve documentation and code quality (refactoring, removing dead code, adding tests, ...).
1820
- Improve the implementation of the metadata precision/size for the columns.

src/main/java/com/ing/data/cassandra/jdbc/JdbcByte.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
public class JdbcByte extends AbstractJdbcType<Byte> {
2424

25-
// The maximal size of a 1-bit signed integer is 4 (length of '-128').
25+
// The maximal size of a 8-bit signed integer is 4 (length of '-128').
2626
private static final int DEFAULT_TINYINT_PRECISION = 4;
2727

2828
/**

src/main/java/com/ing/data/cassandra/jdbc/JdbcShort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
public class JdbcShort extends AbstractJdbcType<Short> {
2424

25-
// The maximal size of a 2-bit signed integer is 6 (length of '-32768').
25+
// The maximal size of a 16-bit signed integer is 6 (length of '-32768').
2626
private static final int DEFAULT_SMALLINT_PRECISION = 6;
2727

2828
/**

src/main/java/com/ing/data/cassandra/jdbc/SessionHolder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
import com.ing.data.cassandra.jdbc.codec.FloatToDoubleCodec;
3636
import com.ing.data.cassandra.jdbc.codec.IntToLongCodec;
3737
import com.ing.data.cassandra.jdbc.codec.LongToIntCodec;
38+
import com.ing.data.cassandra.jdbc.codec.SmallintToIntCodec;
3839
import com.ing.data.cassandra.jdbc.codec.TimestampToLongCodec;
40+
import com.ing.data.cassandra.jdbc.codec.TinyintToIntCodec;
41+
import com.ing.data.cassandra.jdbc.codec.VarintToIntCodec;
3942
import org.apache.commons.lang3.StringUtils;
4043
import org.slf4j.Logger;
4144
import org.slf4j.LoggerFactory;
@@ -250,6 +253,9 @@ private Session createSession(final Properties properties) throws SQLException {
250253
codecs.add(new BigintToBigDecimalCodec());
251254
codecs.add(new DecimalToDoubleCodec());
252255
codecs.add(new FloatToDoubleCodec());
256+
codecs.add(new VarintToIntCodec());
257+
codecs.add(new SmallintToIntCodec());
258+
codecs.add(new TinyintToIntCodec());
253259
builder.addTypeCodecs(codecs.toArray(new TypeCodec[]{}));
254260

255261
builder.withKeyspace(keyspace);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.ing.data.cassandra.jdbc.codec;
16+
17+
import com.datastax.oss.driver.api.core.ProtocolVersion;
18+
import com.datastax.oss.driver.api.core.type.DataType;
19+
import com.datastax.oss.driver.api.core.type.DataTypes;
20+
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
21+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
22+
import edu.umd.cs.findbugs.annotations.NonNull;
23+
import org.apache.cassandra.utils.ByteBufferUtil;
24+
25+
import java.nio.ByteBuffer;
26+
27+
/**
28+
* Manages the two-way conversion between the CQL type {@link DataTypes#SMALLINT} and the Java type {@link Integer}.
29+
*/
30+
public class SmallintToIntCodec extends AbstractCodec<Integer> implements TypeCodec<Integer> {
31+
32+
public SmallintToIntCodec() {
33+
}
34+
35+
@NonNull
36+
@Override
37+
public GenericType<Integer> getJavaType() {
38+
return GenericType.INTEGER;
39+
}
40+
41+
@NonNull
42+
@Override
43+
public DataType getCqlType() {
44+
return DataTypes.SMALLINT;
45+
}
46+
47+
@Override
48+
public ByteBuffer encode(final Integer value, @NonNull final ProtocolVersion protocolVersion) {
49+
if (value == null) {
50+
return null;
51+
}
52+
return ByteBufferUtil.bytes(value);
53+
}
54+
55+
@Override
56+
public Integer decode(final ByteBuffer bytes, @NonNull final ProtocolVersion protocolVersion) {
57+
if (bytes == null) {
58+
return null;
59+
}
60+
// always duplicate the ByteBuffer instance before consuming it!
61+
final short value = ByteBufferUtil.toShort(bytes.duplicate());
62+
return (int) value;
63+
}
64+
65+
@Override
66+
Integer parseNonNull(@NonNull final String value) {
67+
return Integer.valueOf(value);
68+
}
69+
70+
@Override
71+
String formatNonNull(@NonNull final Integer value) {
72+
return String.valueOf(value);
73+
}
74+
75+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.ing.data.cassandra.jdbc.codec;
16+
17+
import com.datastax.oss.driver.api.core.ProtocolVersion;
18+
import com.datastax.oss.driver.api.core.type.DataType;
19+
import com.datastax.oss.driver.api.core.type.DataTypes;
20+
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
21+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
22+
import edu.umd.cs.findbugs.annotations.NonNull;
23+
import org.apache.cassandra.utils.ByteBufferUtil;
24+
25+
import java.nio.ByteBuffer;
26+
27+
/**
28+
* Manages the two-way conversion between the CQL type {@link DataTypes#TINYINT} and the Java type {@link Integer}.
29+
*/
30+
public class TinyintToIntCodec extends AbstractCodec<Integer> implements TypeCodec<Integer> {
31+
32+
public TinyintToIntCodec() {
33+
}
34+
35+
@NonNull
36+
@Override
37+
public GenericType<Integer> getJavaType() {
38+
return GenericType.INTEGER;
39+
}
40+
41+
@NonNull
42+
@Override
43+
public DataType getCqlType() {
44+
return DataTypes.TINYINT;
45+
}
46+
47+
@Override
48+
public ByteBuffer encode(final Integer value, @NonNull final ProtocolVersion protocolVersion) {
49+
if (value == null) {
50+
return null;
51+
}
52+
return ByteBufferUtil.bytes(value);
53+
}
54+
55+
@Override
56+
public Integer decode(final ByteBuffer bytes, @NonNull final ProtocolVersion protocolVersion) {
57+
if (bytes == null) {
58+
return null;
59+
}
60+
// always duplicate the ByteBuffer instance before consuming it!
61+
final byte value = bytes.duplicate().get();
62+
return (int) value;
63+
}
64+
65+
@Override
66+
Integer parseNonNull(@NonNull final String value) {
67+
return Integer.valueOf(value);
68+
}
69+
70+
@Override
71+
String formatNonNull(@NonNull final Integer value) {
72+
return String.valueOf(value);
73+
}
74+
75+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.ing.data.cassandra.jdbc.codec;
16+
17+
import com.datastax.oss.driver.api.core.ProtocolVersion;
18+
import com.datastax.oss.driver.api.core.type.DataType;
19+
import com.datastax.oss.driver.api.core.type.DataTypes;
20+
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
21+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
22+
import edu.umd.cs.findbugs.annotations.NonNull;
23+
import org.apache.cassandra.utils.ByteBufferUtil;
24+
25+
import java.nio.ByteBuffer;
26+
27+
/**
28+
* Manages the two-way conversion between the CQL type {@link DataTypes#VARINT} and the Java type {@link Integer}.
29+
*/
30+
public class VarintToIntCodec extends AbstractCodec<Integer> implements TypeCodec<Integer> {
31+
32+
public VarintToIntCodec() {
33+
}
34+
35+
@NonNull
36+
@Override
37+
public GenericType<Integer> getJavaType() {
38+
return GenericType.INTEGER;
39+
}
40+
41+
@NonNull
42+
@Override
43+
public DataType getCqlType() {
44+
return DataTypes.VARINT;
45+
}
46+
47+
@Override
48+
public ByteBuffer encode(final Integer value, @NonNull final ProtocolVersion protocolVersion) {
49+
if (value == null) {
50+
return null;
51+
}
52+
return ByteBufferUtil.bytes(value);
53+
}
54+
55+
@Override
56+
public Integer decode(final ByteBuffer bytes, @NonNull final ProtocolVersion protocolVersion) {
57+
if (bytes == null) {
58+
return null;
59+
}
60+
// always duplicate the ByteBuffer instance before consuming it!
61+
return ByteBufferUtil.toInt(bytes.duplicate());
62+
}
63+
64+
@Override
65+
Integer parseNonNull(@NonNull final String value) {
66+
return Integer.valueOf(value);
67+
}
68+
69+
@Override
70+
String formatNonNull(@NonNull final Integer value) {
71+
return String.valueOf(value);
72+
}
73+
74+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.ing.data.cassandra.jdbc.codec;
15+
16+
import com.datastax.oss.driver.api.core.ProtocolVersion;
17+
import com.datastax.oss.driver.api.core.type.DataTypes;
18+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
19+
import org.apache.commons.lang3.StringUtils;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.nio.ByteBuffer;
23+
24+
import static com.ing.data.cassandra.jdbc.Utils.NULL_KEYWORD;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
import static org.junit.jupiter.api.Assertions.assertNull;
28+
29+
public class SmallintToIntCodecTest {
30+
31+
private final SmallintToIntCodec sut = new SmallintToIntCodec();
32+
33+
@Test
34+
void givenCodec_whenGetJavaType_returnInt() {
35+
assertEquals(GenericType.INTEGER, sut.getJavaType());
36+
}
37+
38+
@Test
39+
void givenCodec_whenGetCqlType_returnVarint() {
40+
assertEquals(DataTypes.SMALLINT, sut.getCqlType());
41+
}
42+
43+
@Test
44+
void givenNullValue_whenEncode_returnNull() {
45+
assertNull(sut.encode(null, ProtocolVersion.DEFAULT));
46+
}
47+
48+
@Test
49+
void givenValue_whenEncode_returnByteBuffer() {
50+
ByteBuffer bytes = sut.encode(12345, ProtocolVersion.DEFAULT);
51+
assertNotNull(bytes);
52+
assertEquals(12345, bytes.getInt());
53+
}
54+
55+
@Test
56+
void givenNullValue_whenDecode_returnNull() {
57+
assertNull(sut.decode(null, ProtocolVersion.DEFAULT));
58+
}
59+
60+
@Test
61+
void givenValue_whenDecode_returnInt() {
62+
ByteBuffer bytes = ByteBuffer.allocate(2).putShort((short) 12345);
63+
bytes.position(0);
64+
assertEquals(12345, sut.decode(bytes, ProtocolVersion.DEFAULT));
65+
}
66+
67+
@Test
68+
void givenNullOrEmptyValue_whenParse_returnNull() {
69+
assertNull(sut.parse(null));
70+
assertNull(sut.parse(NULL_KEYWORD));
71+
assertNull(sut.parse(StringUtils.EMPTY));
72+
assertNull(sut.parse(StringUtils.SPACE));
73+
}
74+
75+
@Test
76+
void givenNonNullValue_whenParse_returnExpectedValue() {
77+
assertEquals(12345, sut.parse("12345"));
78+
}
79+
80+
@Test
81+
void givenNullValue_whenFormat_returnNull() {
82+
assertEquals(NULL_KEYWORD, sut.format(null));
83+
}
84+
85+
@Test
86+
void givenNonNullValue_whenFormat_returnExpectedValue() {
87+
assertEquals("12345", sut.format(12345));
88+
}
89+
}

0 commit comments

Comments
 (0)