Skip to content

Commit

Permalink
Merge pull request #214 from taosdata/feat/TS-5888
Browse files Browse the repository at this point in the history
add unsigned data to jdbc WebSocket Connection
  • Loading branch information
zitsen authored Jan 22, 2025
2 parents 5ab2196 + fdbeeed commit 0aae4e4
Show file tree
Hide file tree
Showing 24 changed files with 652 additions and 104 deletions.
2 changes: 1 addition & 1 deletion deploy-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.5.2</version>
<version>3.5.3</version>
<packaging>jar</packaging>

<name>JDBCDriver</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.5.2</version>
<version>3.5.3</version>

<packaging>jar</packaging>
<name>JDBCDriver</name>
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/taosdata/jdbc/TSDBConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public abstract class TSDBConstants {
public static final int TSDB_DATA_TYPE_NCHAR = 10;
/**
* 系统增加新的无符号数据类型,分别是:
* unsigned tinyint, 数值范围:0-254, NULL 为255
* unsigned smallint,数值范围: 0-65534, NULL 为65535
* unsigned int,数值范围:0-4294967294,NULL 为4294967295u
* unsigned bigint,数值范围:0-18446744073709551614u,NULL 为18446744073709551615u
* unsigned tinyint, 数值范围:0-255
* unsigned smallint,数值范围: 0-65535
* unsigned int,数值范围:0-4294967295
* unsigned bigint,数值范围:0-18446744073709551615u
* example:
* create table tb(ts timestamp, a tinyint unsigned, b smallint unsigned, c int unsigned, d bigint unsigned);
*/
Expand All @@ -64,10 +64,15 @@ public abstract class TSDBConstants {

// precision for data types, this is used for metadata
public static final int BOOLEAN_PRECISION = 1;
public static final int TINYINT_PRECISION = 4;
public static final int SMALLINT_PRECISION = 6;
public static final int INT_PRECISION = 11;
public static final int BIGINT_PRECISION = 20;
public static final int TINYINT_PRECISION = 3;
public static final int UNSIGNED_TINYINT_PRECISION = 3;
public static final int SMALLINT_PRECISION = 5;
public static final int UNSIGNED_SMALLINT_PRECISION = 5;

public static final int INT_PRECISION = 10;
public static final int UNSIGNED_INT_PRECISION = 10;
public static final int BIGINT_PRECISION = 19;
public static final int UNSIGNED_BIGINT_PRECISION = 20;
public static final int FLOAT_PRECISION = 12;
public static final int DOUBLE_PRECISION = 22;
public static final int TIMESTAMP_MS_PRECISION = 23;
Expand All @@ -80,6 +85,12 @@ public abstract class TSDBConstants {

public static final boolean DEFAULT_BATCH_ERROR_IGNORE = false;


public static final short MAX_UNSIGNED_BYTE = 255;
public static final int MAX_UNSIGNED_SHORT = 65535;
public static final long MAX_UNSIGNED_INT = 4294967295L;
public static final String MAX_UNSIGNED_LONG = "18446744073709551615";

public static String jdbcType2TaosTypeName(int jdbcType) throws SQLException {
switch (jdbcType) {
case Types.BOOLEAN:
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -464,6 +465,11 @@ public void setTagLong(int index, long value) {
this.tagValueLength += Long.BYTES;
}

@Override
public void setTagBigInteger(int index, BigInteger value) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}

@Override
public void setTagTimestamp(int index, long value) {
ensureTagCapacity(index);
Expand Down Expand Up @@ -564,6 +570,11 @@ public void setLong(int columnIndex, List<Long> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BIGINT, Long.BYTES);
}

@Override
public void setBigInteger(int columnIndex, List<BigInteger> list) throws SQLException{
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}

@Override
public void setDouble(int columnIndex, List<Double> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_DOUBLE, Double.BYTES);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/taosdata/jdbc/TaosPrepareStatement.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.taosdata.jdbc;

import java.math.BigInteger;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
Expand All @@ -21,6 +22,8 @@ public interface TaosPrepareStatement extends PreparedStatement {

void setTagLong(int index, long value);

void setTagBigInteger(int index, BigInteger value) throws SQLException;

void setTagFloat(int index, float value);

void setTagDouble(int index, double value);
Expand All @@ -43,6 +46,7 @@ public interface TaosPrepareStatement extends PreparedStatement {
void setTimestamp(int columnIndex, List<Long> list) throws SQLException;

void setLong(int columnIndex, List<Long> list) throws SQLException;
void setBigInteger(int columnIndex, List<BigInteger> list) throws SQLException;

void setDouble(int columnIndex, List<Double> list) throws SQLException;

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/taosdata/jdbc/common/DataLengthCfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ public class DataLengthCfg {
4,//TSDB_DATA_TYPE_FLOAT
8,//TSDB_DATA_TYPE_DOUBLE
null,
8//TSDB_DATA_TYPE_TIMESTAMP
};
8, //TSDB_DATA_TYPE_TIMESTAMP
null,
1, //TSDB_DATA_TYPE_UTINYINT
2, //TSDB_DATA_TYPE_USMALLINT
4, //TSDB_DATA_TYPE_UINT
8 //TSDB_DATA_TYPE_UBIGINT
};

public static Integer getDataLength(int dataType){
if (dataType < dataLenArr.length){
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/taosdata/jdbc/common/SerializeBlock.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.taosdata.jdbc.common;

import com.taosdata.jdbc.TSDBConstants;
import com.taosdata.jdbc.TSDBError;
import com.taosdata.jdbc.TSDBErrorNumbers;
import com.taosdata.jdbc.utils.DateTimeUtils;
import com.taosdata.jdbc.utils.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.SQLException;
import java.time.Instant;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -175,6 +177,23 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_UTINYINT: {
SerializeInt(buf, offset, objectList.size());
offset += Integer.BYTES;

for (Object o: objectList){
if (o == null) {
buf[offset++] = 0;
} else {
short v = (Short) o;
if (v < 0 || v > MAX_UNSIGNED_BYTE){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "utinyint value is out of range");
}
buf[offset++] = (byte) (v & 0xFF);
}
}
break;
}
case TSDB_DATA_TYPE_SMALLINT: {
SerializeInt(buf, offset, objectList.size() * Short.BYTES);
offset += Integer.BYTES;
Expand All @@ -190,6 +209,25 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_USMALLINT: {
SerializeInt(buf, offset, objectList.size() * Short.BYTES);
offset += Integer.BYTES;

for (Object o: objectList){
if (o != null) {
int v = (Integer) o;
if (v < 0 || v > MAX_UNSIGNED_SHORT){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "usmallint value is out of range");
}
SerializeShort(buf, offset, (short) (v & 0xFFFF));
} else {
SerializeShort(buf, offset, (short)0);
}

offset += Short.BYTES;
}
break;
}
case TSDB_DATA_TYPE_INT: {
SerializeInt(buf, offset, objectList.size() * Integer.BYTES);
offset += Integer.BYTES;
Expand All @@ -204,6 +242,24 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_UINT: {
SerializeInt(buf, offset, objectList.size() * Integer.BYTES);
offset += Integer.BYTES;

for (Object o: objectList){
if (o != null) {
long v = (Long) o;
if (v < 0 || v > MAX_UNSIGNED_INT){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "uint value is out of range");
}
SerializeInt(buf, offset, (int) (v & 0xFFFFFFFFL));
} else {
SerializeInt(buf, offset, 0);
}
offset += Integer.BYTES;
}
break;
}
case TSDB_DATA_TYPE_BIGINT: {
SerializeInt(buf, offset, objectList.size() * Long.BYTES);
offset += Integer.BYTES;
Expand All @@ -218,6 +274,24 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_UBIGINT: {
SerializeInt(buf, offset, objectList.size() * Long.BYTES);
offset += Integer.BYTES;

for (Object o: objectList){
if (o != null) {
BigInteger v = (BigInteger) o;
if (v.compareTo(BigInteger.ZERO) < 0 || v.compareTo(new BigInteger(MAX_UNSIGNED_LONG)) > 0){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "ubigint value is out of range");
}
SerializeLong(buf, offset, v.longValue());
} else {
SerializeLong(buf, offset, 0L);
}
offset += Long.BYTES;
}
break;
}
case TSDB_DATA_TYPE_FLOAT: {
SerializeInt(buf, offset, objectList.size() * Integer.BYTES);
offset += Integer.BYTES;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/taosdata/jdbc/enums/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
public enum DataType {
NULL("NULL", Types.NULL, TSDB_DATA_TYPE_NULL, 0),
BOOL("BOOL", Types.BOOLEAN, TSDB_DATA_TYPE_BOOL, BOOLEAN_PRECISION),

TINYINT("TINYINT", Types.TINYINT, TSDB_DATA_TYPE_TINYINT, TINYINT_PRECISION),
UTINYINT("TINYINT UNSIGNED", Types.TINYINT, TSDB_DATA_TYPE_UTINYINT, TINYINT_PRECISION),
USMALLINT("SMALLINT UNSIGNED", Types.SMALLINT, TSDB_DATA_TYPE_USMALLINT, SMALLINT_PRECISION),
UTINYINT("TINYINT UNSIGNED", Types.SMALLINT, TSDB_DATA_TYPE_UTINYINT, UNSIGNED_TINYINT_PRECISION),
USMALLINT("SMALLINT UNSIGNED", Types.INTEGER, TSDB_DATA_TYPE_USMALLINT, UNSIGNED_SMALLINT_PRECISION),
SMALLINT("SMALLINT", Types.SMALLINT, TSDB_DATA_TYPE_SMALLINT, SMALLINT_PRECISION),
UINT("INT UNSIGNED", Types.INTEGER, TSDB_DATA_TYPE_UINT, INT_PRECISION),
UINT("INT UNSIGNED", Types.BIGINT, TSDB_DATA_TYPE_UINT, UNSIGNED_INT_PRECISION),
INT("INT", Types.INTEGER, TSDB_DATA_TYPE_INT, INT_PRECISION),
UBIGINT("BIGINT UNSIGNED", Types.BIGINT, TSDB_DATA_TYPE_UBIGINT, BIGINT_PRECISION),
UBIGINT("BIGINT UNSIGNED", Types.OTHER, TSDB_DATA_TYPE_UBIGINT, UNSIGNED_BIGINT_PRECISION),
BIGINT("BIGINT", Types.BIGINT, TSDB_DATA_TYPE_BIGINT, BIGINT_PRECISION),
FLOAT("FLOAT", Types.FLOAT, TSDB_DATA_TYPE_FLOAT, FLOAT_PRECISION),
DOUBLE("DOUBLE", Types.DOUBLE, TSDB_DATA_TYPE_DOUBLE, DOUBLE_PRECISION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
Expand Down Expand Up @@ -136,7 +137,11 @@ public V deserialize(ResultSet data, String topic, String dbName) throws Deseria
} else if (param.clazz.isAssignableFrom(BigDecimal.class)){
BigDecimal bigDecimal = data.getBigDecimal(param.name);
param.method.invoke(t, data.wasNull() ? null : bigDecimal);
} else if (param.clazz.isAssignableFrom(BigInteger.class)){
BigInteger bigInteger = (BigInteger) data.getObject(param.name);
param.method.invoke(t, data.wasNull() ? null : bigInteger);
}

} catch (IllegalAccessException | InvocationTargetException e) {
throw new SQLException(this.getClass().getSimpleName() + ": " + param.name
+ " through method:" + param.method.getName() +" get Data error: ", e);
Expand Down
Loading

0 comments on commit 0aae4e4

Please sign in to comment.