Skip to content

Commit 7f794f4

Browse files
committed
Add bigint <=> BigDecimal codec
1 parent b9948d7 commit 7f794f4

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java'
33
}
44

5-
version '1.3'
5+
version '1.3.1'
66

77
sourceCompatibility = 1.8
88

driver/src/main/java/com/dbschema/CassandraJdbcDriver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.datastax.driver.core.exceptions.AuthenticationException;
99
import com.datastax.driver.core.exceptions.InvalidQueryException;
1010
import com.datastax.driver.core.exceptions.NoHostAvailableException;
11+
import com.dbschema.codec.jbiginteger.LongCodec;
1112
import com.dbschema.codec.jbytes.BlobCodec;
1213
import com.dbschema.codec.jlong.*;
1314
import com.dbschema.codec.jsqldate.DateCodec;
@@ -89,6 +90,7 @@ private void registerCodecs(Cluster cluster) {
8990
myCodecRegistry.register(com.dbschema.codec.jdouble.DecimalCodec.INSTANCE);
9091
myCodecRegistry.register(DateCodec.INSTANCE);
9192
myCodecRegistry.register(TimeCodec.INSTANCE);
93+
myCodecRegistry.register(LongCodec.INSTANCE);
9294
myCodecRegistry.register(com.dbschema.codec.juuid.StringCodec.INSTANCE);
9395
myCodecRegistry.register(com.dbschema.codec.jduration.StringCodec.INSTANCE);
9496
myCodecRegistry.register(com.dbschema.codec.jtimeuuid.StringCodec.INSTANCE);
@@ -110,7 +112,7 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
110112
}
111113

112114
String getVersion() {
113-
return "1.3";
115+
return "1.3.1";
114116
}
115117

116118
@Override
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.dbschema.codec.jbiginteger;
2+
3+
import com.datastax.driver.core.DataType;
4+
import com.datastax.driver.core.ProtocolVersion;
5+
import com.datastax.driver.core.TypeCodec;
6+
import com.datastax.driver.core.exceptions.InvalidTypeException;
7+
8+
import java.math.BigDecimal;
9+
import java.nio.ByteBuffer;
10+
11+
/**
12+
* @author Liudmila Kornilova
13+
**/
14+
public class LongCodec extends TypeCodec<BigDecimal> {
15+
public static final LongCodec INSTANCE = new LongCodec();
16+
17+
private LongCodec() {
18+
super(DataType.bigint(), BigDecimal.class);
19+
}
20+
21+
@Override
22+
public ByteBuffer serialize(BigDecimal value, ProtocolVersion protocolVersion) throws InvalidTypeException {
23+
if (value == null) return null;
24+
ByteBuffer bb = ByteBuffer.allocate(8);
25+
try {
26+
long l = value.longValueExact();
27+
bb.putLong(l);
28+
} catch (ArithmeticException e) {
29+
throw new InvalidTypeException("BigInteger value " + value + " does not fit into cql type long", e);
30+
}
31+
bb.flip();
32+
return bb;
33+
}
34+
35+
@Override
36+
public BigDecimal deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
37+
return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes);
38+
}
39+
40+
private BigDecimal deserializeNoBoxing(ByteBuffer bytes) {
41+
if (bytes.remaining() != 8) {
42+
throw new InvalidTypeException("Invalid value, expecting 8 bytes but got " + bytes.remaining());
43+
}
44+
return new BigDecimal(Long.toString(bytes.getLong()));
45+
}
46+
47+
@Override
48+
public BigDecimal parse(String value) throws InvalidTypeException {
49+
try {
50+
return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")
51+
? null
52+
: new BigDecimal(value);
53+
} catch (NumberFormatException e) {
54+
throw new InvalidTypeException(
55+
String.format("Cannot parse 64-bits long value from \"%s\"", value));
56+
}
57+
}
58+
59+
@Override
60+
public String format(BigDecimal value) throws InvalidTypeException {
61+
if (value == null) return "NULL";
62+
return Long.toString(value.longValue());
63+
}
64+
}

0 commit comments

Comments
 (0)