Skip to content

Commit

Permalink
Add missing write methods for *Int128 and *Int256
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Feb 7, 2021
1 parent af57f24 commit ad1fd0d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/main/java/ru/yandex/clickhouse/jdbcbridge/core/ByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,7 @@ public BigInteger readInt128() {
}

public ByteBuffer writeInt128(BigInteger value) {
byte empty = value.signum() == -1 ? (byte) 0xFF : 0x00;
byte[] bytes = value.toByteArray();
for (int i = bytes.length - 1; i >= 0; i--) {
writeByte(bytes[i]);
}

for (int i = 16 - bytes.length; i > 0; i--) {
writeByte(empty);
}

return this;
return writeBigInteger(value, DataType.Int128.getLength());
}

public BigInteger readInt256() {
Expand All @@ -341,17 +331,7 @@ public BigInteger readInt256() {
}

public ByteBuffer writeInt256(BigInteger value) {
byte empty = value.signum() == -1 ? (byte) 0xFF : 0x00;
byte[] bytes = value.toByteArray();
for (int i = bytes.length - 1; i >= 0; i--) {
writeByte(bytes[i]);
}

for (int i = 32 - bytes.length; i > 0; i--) {
writeByte(empty);
}

return this;
return writeBigInteger(value, DataType.Int256.getLength());
}

public BigInteger readUInt64() {
Expand Down Expand Up @@ -411,12 +391,20 @@ public java.util.UUID readUUID() {
}

public ByteBuffer writeBigInteger(BigInteger value) {
return writeBigInteger(value, 16);
}

public ByteBuffer writeBigInteger(BigInteger value, int length) {
byte empty = value.signum() == -1 ? (byte) 0xFF : 0x00;
byte[] bytes = value.toByteArray();
for (int i = bytes.length - 1; i >= 0; i--) {
writeByte(bytes[i]);
}

writeBytes(new byte[16 - bytes.length]);
// FIXME when the given (byte)length is less than bytes.length...
for (int i = length - bytes.length; i > 0; i--) {
writeByte(empty);
}

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package ru.yandex.clickhouse.jdbcbridge.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
Expand Down Expand Up @@ -758,6 +759,12 @@ protected final void write(PreparedStatement stmt, ColumnDefinition[] cols, Quer
case Int64:
stmt.setLong(i, buffer.readInt64());
break;
case Int128:
stmt.setBigDecimal(i, new BigDecimal(buffer.readInt128()));
break;
case Int256:
stmt.setBigDecimal(i, new BigDecimal(buffer.readInt256()));
break;
case UInt8:
stmt.setInt(i, buffer.readUInt8());
break;
Expand All @@ -770,6 +777,12 @@ protected final void write(PreparedStatement stmt, ColumnDefinition[] cols, Quer
case UInt64:
stmt.setString(i, buffer.readUInt64().toString(10));
break;
case UInt128:
stmt.setBigDecimal(i, new BigDecimal(buffer.readUInt128()));
break;
case UInt256:
stmt.setBigDecimal(i, new BigDecimal(buffer.readUInt256()));
break;
case Float32:
stmt.setFloat(i, buffer.readFloat32());
break;
Expand Down

0 comments on commit ad1fd0d

Please sign in to comment.