Skip to content

Commit

Permalink
Merge pull request #205 from taosdata/feat/TD-32222
Browse files Browse the repository at this point in the history
add stmt2 support
  • Loading branch information
zitsen authored Dec 25, 2024
2 parents 4ba2926 + 919cdaa commit 95dac34
Show file tree
Hide file tree
Showing 44 changed files with 1,914 additions and 907 deletions.
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Maven 项目中,在 pom.xml 中添加以下依赖:
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.3.0</version>
<version>3.5.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Add following dependency in the `pom.xml` file of your Maven project:
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.0.*</version>
<version>3.5.0</version>
</dependency>
```

Expand Down
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.4.1</version>
<version>3.5.0</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.4.1-SNAPSHOT</version>
<version>3.5.0</version>

<packaging>jar</packaging>
<name>JDBCDriver</name>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/taosdata/jdbc/AbstractDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected Connection getWSConnection(String url, ConnectionParam param, Properti

transport.setTextMessageHandler(message -> {
try {
log.trace("received message: {}", message);
JsonNode jsonObject = JsonUtil.getObjectReader().readTree(message);
Action action = Action.of(jsonObject.get("action").asText());
ObjectReader actionReader = JsonUtil.getObjectReader(action.getResponseClazz());
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/taosdata/jdbc/AbstractStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,5 +367,4 @@ public boolean isCloseOnCompletion() throws SQLException {
public Long getInstanceId(){
return this.instanceId;
}

}
74 changes: 52 additions & 22 deletions src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,27 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.PriorityQueue;
import java.sql.Date;
import java.util.*;

/*
* TDengine only supports a subset of the standard SQL, thus this implementation of the
* standard JDBC API contains more or less some adjustments customized for certain
* compatibility needs.
*/
public class TSDBPreparedStatement extends TSDBStatement implements PreparedStatement {
public class TSDBPreparedStatement extends TSDBStatement implements TaosPrepareStatement {
// for jdbc preparedStatement interface
private String rawSql;
private Object[] parameters = new Object[0];
// for parameter binding
private long nativeStmtHandle;
private String tableName;
private ArrayList<TableTagInfo> tableTags;
private List<TableTagInfo> tableTags;
private int tagValueLength;
private PriorityQueue<ColumnInfo> queue = new PriorityQueue<>();



TSDBPreparedStatement(TSDBConnection connection, String sql, Long instanceId) throws SQLException {
super(connection, instanceId);
this.rawSql = sql;
Expand Down Expand Up @@ -370,7 +369,7 @@ public void setNClob(int parameterIndex, Reader reader) throws SQLException {
// parameter binding
private static class ColumnInfo implements Comparable<ColumnInfo> {
@SuppressWarnings("rawtypes")
private ArrayList data;
private List data;
private int type;
private int bytes;
private boolean typeIsSet;
Expand Down Expand Up @@ -416,6 +415,7 @@ public static TableTagInfo createNullTag(int type) {
}
}

@Override
public void setTableName(String name) throws SQLException {

if (this.nativeStmtHandle == 0) {
Expand All @@ -437,65 +437,81 @@ private void ensureTagCapacity(int index) {
}
}

@Override
public void setTagNull(int index, int type) {
ensureTagCapacity(index);
this.tableTags.set(index, TableTagInfo.createNullTag(type));
}

@Override
public void setTagBoolean(int index, boolean value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BOOL));
this.tagValueLength += Byte.BYTES;
}

@Override
public void setTagInt(int index, int value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_INT));
this.tagValueLength += Integer.BYTES;
}

@Override
public void setTagByte(int index, byte value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_TINYINT));
this.tagValueLength += Byte.BYTES;
}

@Override
public void setTagShort(int index, short value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_SMALLINT));
this.tagValueLength += Short.BYTES;
}

@Override
public void setTagLong(int index, long value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BIGINT));
this.tagValueLength += Long.BYTES;
}

@Override
public void setTagTimestamp(int index, long value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP));
this.tagValueLength += Long.BYTES;
}

@Override
public void setTagTimestamp(int index, Timestamp value) {
throw new RuntimeException("not supported");
}

@Override
public void setTagFloat(int index, float value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_FLOAT));
this.tagValueLength += Float.BYTES;
}

@Override
public void setTagDouble(int index, double value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_DOUBLE));
this.tagValueLength += Double.BYTES;
}

@Override
public void setTagString(int index, String value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BINARY));
this.tagValueLength += value.getBytes().length;
}

@Override
public void setTagNString(int index, String value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_NCHAR));
Expand All @@ -508,6 +524,7 @@ public void setTagNString(int index, String value) {
}
}

@Override
public void setTagJson(int index, String value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_JSON));
Expand All @@ -520,75 +537,88 @@ public void setTagJson(int index, String value) {
}
}

@Override
public void setTagVarbinary(int index, byte[] value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_VARBINARY));
this.tagValueLength += value.length;
}
@Override
public void setTagGeometry(int index, byte[] value) {
ensureTagCapacity(index);
this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_GEOMETRY));
this.tagValueLength += value.length;
}

public <T> void setValueImpl(int columnIndex, ArrayList<T> list, int type, int bytes) throws SQLException {
public <T> void setValueImpl(int columnIndex, List<T> list, int type, int bytes) throws SQLException {
ColumnInfo p = new ColumnInfo();
p.setType(type);
p.bytes = bytes;
p.data = (ArrayList<?>) list.clone();
p.data = list;
p.index = columnIndex;
queue.add(p);
}

public void setInt(int columnIndex, ArrayList<Integer> list) throws SQLException {
@Override
public void setInt(int columnIndex, List<Integer> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_INT, Integer.BYTES);
}

public void setFloat(int columnIndex, ArrayList<Float> list) throws SQLException {
@Override
public void setFloat(int columnIndex, List<Float> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_FLOAT, Float.BYTES);
}

public void setTimestamp(int columnIndex, ArrayList<Long> list) throws SQLException {
@Override
public void setTimestamp(int columnIndex, List<Long> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP, Long.BYTES);
}

public void setLong(int columnIndex, ArrayList<Long> list) throws SQLException {
@Override
public void setLong(int columnIndex, List<Long> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BIGINT, Long.BYTES);
}

public void setDouble(int columnIndex, ArrayList<Double> list) throws SQLException {
@Override
public void setDouble(int columnIndex, List<Double> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_DOUBLE, Double.BYTES);
}

public void setBoolean(int columnIndex, ArrayList<Boolean> list) throws SQLException {
@Override
public void setBoolean(int columnIndex, List<Boolean> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BOOL, Byte.BYTES);
}

public void setByte(int columnIndex, ArrayList<Byte> list) throws SQLException {
public void setByte(int columnIndex, List<Byte> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_TINYINT, Byte.BYTES);
}

public void setShort(int columnIndex, ArrayList<Short> list) throws SQLException {
@Override
public void setShort(int columnIndex, List<Short> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_SMALLINT, Short.BYTES);
}

public void setString(int columnIndex, ArrayList<String> list, int size) throws SQLException {
@Override
public void setString(int columnIndex, List<String> list, int size) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BINARY, size);
}

public void setVarbinary(int columnIndex, ArrayList<byte[]> list, int size) throws SQLException {
@Override
public void setVarbinary(int columnIndex, List<byte[]> list, int size) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_VARBINARY, size);
}

public void setGeometry(int columnIndex, ArrayList<byte[]> list, int size) throws SQLException {
@Override
public void setGeometry(int columnIndex, List<byte[]> list, int size) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_GEOMETRY, size);
}

// note: expand the required space for each NChar character
public void setNString(int columnIndex, ArrayList<String> list, int size) throws SQLException {
@Override
public void setNString(int columnIndex, List<String> list, int size) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_NCHAR, size * Integer.BYTES);
}

@Override
public void columnDataAddBatch() throws SQLException {
// pass the data block to native code
if (rawSql == null) {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/taosdata/jdbc/TaosPrepareStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.taosdata.jdbc;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

public interface TaosPrepareStatement extends PreparedStatement {

void setTableName(String name) throws SQLException;

void setTagNull(int index, int type) throws SQLException;

void setTagBoolean(int index, boolean value);

void setTagByte(int index, byte value);

void setTagShort(int index, short value);

void setTagInt(int index, int value);

void setTagLong(int index, long value);

void setTagFloat(int index, float value);

void setTagDouble(int index, double value);

void setTagTimestamp(int index, long value);

void setTagTimestamp(int index, Timestamp value);

void setTagString(int index, String value);

void setTagVarbinary(int index, byte[] value);
void setTagGeometry(int index, byte[] value);

void setTagNString(int index, String value);

void setTagJson(int index, String value);
void setInt(int columnIndex, List<Integer> list) throws SQLException;
void setFloat(int columnIndex, List<Float> list) throws SQLException;

void setTimestamp(int columnIndex, List<Long> list) throws SQLException;

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

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

void setBoolean(int columnIndex, List<Boolean> list) throws SQLException;

void setByte(int columnIndex, List<Byte> list) throws SQLException;

void setShort(int columnIndex, List<Short> list) throws SQLException;

void setString(int columnIndex, List<String> list, int size) throws SQLException;

void setVarbinary(int columnIndex, List<byte[]> list, int size) throws SQLException;
void setGeometry(int columnIndex, List<byte[]> list, int size) throws SQLException;
// note: expand the required space for each NChar character
void setNString(int columnIndex, List<String> list, int size) throws SQLException;

void columnDataAddBatch() throws SQLException;

void columnDataExecuteBatch() throws SQLException;
}
Loading

0 comments on commit 95dac34

Please sign in to comment.