Skip to content

Commit

Permalink
Add bigint <=> BigDecimal codec
Browse files Browse the repository at this point in the history
  • Loading branch information
kornilova203 committed May 31, 2019
1 parent b9948d7 commit 7f794f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'java'
}

version '1.3'
version '1.3.1'

sourceCompatibility = 1.8

Expand Down
4 changes: 3 additions & 1 deletion driver/src/main/java/com/dbschema/CassandraJdbcDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.datastax.driver.core.exceptions.AuthenticationException;
import com.datastax.driver.core.exceptions.InvalidQueryException;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import com.dbschema.codec.jbiginteger.LongCodec;
import com.dbschema.codec.jbytes.BlobCodec;
import com.dbschema.codec.jlong.*;
import com.dbschema.codec.jsqldate.DateCodec;
Expand Down Expand Up @@ -89,6 +90,7 @@ private void registerCodecs(Cluster cluster) {
myCodecRegistry.register(com.dbschema.codec.jdouble.DecimalCodec.INSTANCE);
myCodecRegistry.register(DateCodec.INSTANCE);
myCodecRegistry.register(TimeCodec.INSTANCE);
myCodecRegistry.register(LongCodec.INSTANCE);
myCodecRegistry.register(com.dbschema.codec.juuid.StringCodec.INSTANCE);
myCodecRegistry.register(com.dbschema.codec.jduration.StringCodec.INSTANCE);
myCodecRegistry.register(com.dbschema.codec.jtimeuuid.StringCodec.INSTANCE);
Expand All @@ -110,7 +112,7 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
}

String getVersion() {
return "1.3";
return "1.3.1";
}

@Override
Expand Down
64 changes: 64 additions & 0 deletions driver/src/main/java/com/dbschema/codec/jbiginteger/LongCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.dbschema.codec.jbiginteger;

import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.TypeCodec;
import com.datastax.driver.core.exceptions.InvalidTypeException;

import java.math.BigDecimal;
import java.nio.ByteBuffer;

/**
* @author Liudmila Kornilova
**/
public class LongCodec extends TypeCodec<BigDecimal> {
public static final LongCodec INSTANCE = new LongCodec();

private LongCodec() {
super(DataType.bigint(), BigDecimal.class);
}

@Override
public ByteBuffer serialize(BigDecimal value, ProtocolVersion protocolVersion) throws InvalidTypeException {
if (value == null) return null;
ByteBuffer bb = ByteBuffer.allocate(8);
try {
long l = value.longValueExact();
bb.putLong(l);
} catch (ArithmeticException e) {
throw new InvalidTypeException("BigInteger value " + value + " does not fit into cql type long", e);
}
bb.flip();
return bb;
}

@Override
public BigDecimal deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes);
}

private BigDecimal deserializeNoBoxing(ByteBuffer bytes) {
if (bytes.remaining() != 8) {
throw new InvalidTypeException("Invalid value, expecting 8 bytes but got " + bytes.remaining());
}
return new BigDecimal(Long.toString(bytes.getLong()));
}

@Override
public BigDecimal parse(String value) throws InvalidTypeException {
try {
return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")
? null
: new BigDecimal(value);
} catch (NumberFormatException e) {
throw new InvalidTypeException(
String.format("Cannot parse 64-bits long value from \"%s\"", value));
}
}

@Override
public String format(BigDecimal value) throws InvalidTypeException {
if (value == null) return "NULL";
return Long.toString(value.longValue());
}
}

0 comments on commit 7f794f4

Please sign in to comment.