From 123d432772f03b908f9877464036a6e60e0bbafc Mon Sep 17 00:00:00 2001 From: Shuaiqiang Chang Date: Thu, 4 Jun 2020 13:56:24 +0800 Subject: [PATCH] fix: jdbc resultset --- .../jni/com_taosdata_jdbc_TSDBJNIConnector.h | 2 +- src/client/src/TSDBJNIConnector.c | 15 +++++-- .../jdbc/DatabaseMetaDataResultSet.java | 10 ++++- .../com/taosdata/jdbc/TSDBJNIConnector.java | 41 +++++++++++-------- .../java/com/taosdata/jdbc/TSDBStatement.java | 14 ++++--- src/connector/python/linux/python3/test.py | 21 ++++++++++ src/kit/taosdemo/taosdemo.c | 3 +- 7 files changed, 75 insertions(+), 31 deletions(-) create mode 100644 src/connector/python/linux/python3/test.py diff --git a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h index b37301cdad..3782d1693d 100644 --- a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h +++ b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h @@ -71,7 +71,7 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp * Signature: (J)I */ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrCodeImp - (JNIEnv *, jobject, jlong); + (JNIEnv *, jobject, jlong, jlong); /* * Class: com_taosdata_jdbc_TSDBJNIConnector diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index 7012307771..2a5f7f441e 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -305,14 +305,21 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp( return (jlong)pSql; } -JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrCodeImp(JNIEnv *env, jobject jobj, jlong con) { +JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrCodeImp(JNIEnv *env, jobject jobj, jlong con, jlong tres) { TAOS *tscon = (TAOS *)con; if (tscon == NULL) { jniError("jobj:%p, connection is closed", jobj); - return (jint)-TSDB_CODE_INVALID_CONNECTION; + return (jint)TSDB_CODE_INVALID_CONNECTION; } - return (jint)-taos_errno(tscon); + if ((void *)tres == NULL) { + jniError("jobj:%p, conn:%p, resultset is null", jobj, tscon); + return JNI_RESULT_SET_NULL; + } + + TAOS_RES *pSql = (TAOS_RES *)tres; + + return (jint)taos_errno(pSql); } JNIEXPORT jstring JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrMsgImp(JNIEnv *env, jobject jobj, jlong tres) { @@ -464,7 +471,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEn TAOS_ROW row = taos_fetch_row(result); if (row == NULL) { - int tserrno = taos_errno(tscon); + int tserrno = taos_errno(result); if (tserrno == 0) { jniTrace("jobj:%p, conn:%p, resultset:%p, fields size is %d, fetch row to the end", jobj, tscon, res, num_fields); return JNI_FETCH_END; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java index 027d2197a3..cbd3523b34 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java @@ -18,8 +18,8 @@ import java.io.InputStream; import java.io.Reader; import java.math.BigDecimal; import java.net.URL; -import java.sql.*; import java.sql.Date; +import java.sql.*; import java.util.*; /* @@ -102,41 +102,49 @@ public class DatabaseMetaDataResultSet implements ResultSet { @Override public byte getByte(int columnIndex) throws SQLException { + columnIndex--; return (byte) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); } @Override public short getShort(int columnIndex) throws SQLException { + columnIndex--; return (short) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); } @Override public int getInt(int columnIndex) throws SQLException { + columnIndex--; return rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); } @Override public long getLong(int columnIndex) throws SQLException { + columnIndex--; return rowCursor.getLong(columnIndex, columnMetaDataList.get(columnIndex).getColType()); } @Override public float getFloat(int columnIndex) throws SQLException { + columnIndex--; return rowCursor.getFloat(columnIndex, columnMetaDataList.get(columnIndex).getColType()); } @Override public double getDouble(int columnIndex) throws SQLException { + columnIndex--; return rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType()); } @Override public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { + columnIndex--; return new BigDecimal(rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType())); } @Override public byte[] getBytes(int columnIndex) throws SQLException { + columnIndex--; return (rowCursor.getString(columnIndex, columnMetaDataList.get(columnIndex).getColType())).getBytes(); } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java index 3adb601822..6fa9e7bc48 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java @@ -99,7 +99,7 @@ public class TSDBJNIConnector { this.taos = this.connectImp(host, port, dbName, user, password); if (this.taos == TSDBConstants.JNI_NULL_POINTER) { - throw new SQLException(TSDBConstants.WrapErrMsg(this.getErrMsg()), "", this.getErrCode()); + throw new SQLException(TSDBConstants.WrapErrMsg(this.getErrMsg(null)), "", this.getErrCode(null)); } return true; @@ -117,52 +117,57 @@ public class TSDBJNIConnector { freeResultSet(taosResultSetPointer); } - int code; + long pSql = 0l; try { - code = this.executeQueryImp(sql.getBytes(TaosGlobalConfig.getCharset()), this.taos); + pSql = this.executeQueryImp(sql.getBytes(TaosGlobalConfig.getCharset()), this.taos); } catch (Exception e) { e.printStackTrace(); + this.freeResultSet(pSql); throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding")); } + int code = this.getErrCode(pSql); + affectedRows = code; if (code < 0) { affectedRows = -1; if (code == TSDBConstants.JNI_TDENGINE_ERROR) { - throw new SQLException(TSDBConstants.WrapErrMsg(this.getErrMsg()), "", this.getErrCode()); + this.freeResultSet(pSql); + throw new SQLException(TSDBConstants.WrapErrMsg(this.getErrMsg(pSql)), "", this.getErrCode(pSql)); } else { - throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode()); + this.freeResultSet(pSql); + throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode(pSql)); } } // Try retrieving result set for the executed SQL using the current connection pointer. If the executed // SQL is a DML/DDL which doesn't return a result set, then taosResultSetPointer should be 0L. Otherwise, // taosResultSetPointer should be a non-zero value. - taosResultSetPointer = this.getResultSetImp(this.taos); + taosResultSetPointer = this.getResultSetImp(this.taos, pSql); if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) { isResultsetClosed = false; } return code; } - private native int executeQueryImp(byte[] sqlBytes, long connection); + private native long executeQueryImp(byte[] sqlBytes, long connection); /** * Get recent error code by connection */ - public int getErrCode() { - return Math.abs(this.getErrCodeImp(this.taos)); + public int getErrCode(Long pSql) { + return Math.abs(this.getErrCodeImp(this.taos, pSql)); } - private native int getErrCodeImp(long connection); + private native int getErrCodeImp(long connection, Long pSql); /** * Get recent error message by connection */ - public String getErrMsg() { - return this.getErrMsgImp(this.taos); + public String getErrMsg(Long pSql) { + return this.getErrMsgImp(this.taos, pSql); } - private native String getErrMsgImp(long connection); + private native String getErrMsgImp(long connection, Long pSql); /** * Get resultset pointer @@ -172,7 +177,7 @@ public class TSDBJNIConnector { return taosResultSetPointer; } - private native long getResultSetImp(long connection); + private native long getResultSetImp(long connection, long pSql); /** * Free resultset operation from C to release resultset pointer by JNI @@ -212,15 +217,15 @@ public class TSDBJNIConnector { /** * Get affected rows count */ - public int getAffectedRows() { + public int getAffectedRows(Long pSql) { int affectedRows = this.affectedRows; if (affectedRows < 0) { - affectedRows = this.getAffectedRowsImp(this.taos); + affectedRows = this.getAffectedRowsImp(this.taos, pSql); } return affectedRows; } - private native int getAffectedRowsImp(long connection); + private native int getAffectedRowsImp(long connection, Long pSql); /** * Get schema metadata @@ -248,7 +253,7 @@ public class TSDBJNIConnector { public void closeConnection() throws SQLException { int code = this.closeConnectionImp(this.taos); if (code < 0) { - throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode()); + throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode(null)); } else if (code == 0) { this.taos = TSDBConstants.JNI_NULL_POINTER; } else { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java index 455b73bb92..dbe1e41e6b 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java @@ -27,6 +27,8 @@ public class TSDBStatement implements Statement { /** Timeout for a query */ protected int queryTimeout = 0; + private Long pSql = 0l; + /** * Status of current statement */ @@ -66,21 +68,23 @@ public class TSDBStatement implements Statement { if (isClosed) { throw new SQLException("Invalid method call on a closed statement."); } - int res = this.connecter.executeQuery(sql); + long res = this.connecter.executeQuery(sql); long resultSetPointer = this.connecter.getResultSet(); if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) { + this.connecter.freeResultSet(res); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); } else if (resultSetPointer != TSDBConstants.JNI_NULL_POINTER) { this.connecter.freeResultSet(); throw new SQLException("The executed SQL is not a DML or a DDL"); } else { - return res; + int num = this.connecter.getAffectedRows(res); + return num; } } - public String getErrorMsg() { - return this.connecter.getErrMsg(); + public String getErrorMsg(long pSql) { + return this.connecter.getErrMsg(pSql); } public void close() throws SQLException { @@ -170,7 +174,7 @@ public class TSDBStatement implements Statement { if (isClosed) { throw new SQLException("Invalid method call on a closed statement."); } - return this.connecter.getAffectedRows(); + return this.connecter.getAffectedRows(this.pSql); } public boolean getMoreResults() throws SQLException { diff --git a/src/connector/python/linux/python3/test.py b/src/connector/python/linux/python3/test.py new file mode 100644 index 0000000000..b11b2c76a0 --- /dev/null +++ b/src/connector/python/linux/python3/test.py @@ -0,0 +1,21 @@ +from taos.cinterface import CTaosInterface +from taos.error import * +from taos.subscription import TDengineSubscription +from taos.connection import TDengineConnection + + +if __name__ == '__main__': + conn = TDengineConnection( + host="127.0.0.1", user="root", password="taosdata", database="test") + + # Generate a cursor object to run SQL commands + sub = conn.subscribe(False, "test", "select * from log0601;", 1000) + + for i in range(100): + print(i) + data = sub.consume() + for d in data: + print(d) + + sub.close() + conn.close() diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 1cdff80faa..81426b683a 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -461,8 +461,7 @@ int main(int argc, char *argv[]) { taos_init(); TAOS *taos = taos_connect(ip_addr, user, pass, NULL, port); if (taos == NULL) { - fprintf(stderr, "Failed to connect to TDengine, reason:%s\n", taos_errstr(taos)); - taos_close(taos); + fprintf(stderr, "Failed to connect to TDengine, reason:%s\n", taos_errstr(NULL)); return 1; } char command[BUFFER_SIZE] = "\0"; -- GitLab