提交 123d4327 编写于 作者: S Shuaiqiang Chang

fix: jdbc resultset

上级 a7e7b8e2
...@@ -71,7 +71,7 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp ...@@ -71,7 +71,7 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp
* Signature: (J)I * Signature: (J)I
*/ */
JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrCodeImp JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrCodeImp
(JNIEnv *, jobject, jlong); (JNIEnv *, jobject, jlong, jlong);
/* /*
* Class: com_taosdata_jdbc_TSDBJNIConnector * Class: com_taosdata_jdbc_TSDBJNIConnector
......
...@@ -305,14 +305,21 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp( ...@@ -305,14 +305,21 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp(
return (jlong)pSql; 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; TAOS *tscon = (TAOS *)con;
if (tscon == NULL) { if (tscon == NULL) {
jniError("jobj:%p, connection is closed", jobj); 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) { 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 ...@@ -464,7 +471,7 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEn
TAOS_ROW row = taos_fetch_row(result); TAOS_ROW row = taos_fetch_row(result);
if (row == NULL) { if (row == NULL) {
int tserrno = taos_errno(tscon); int tserrno = taos_errno(result);
if (tserrno == 0) { if (tserrno == 0) {
jniTrace("jobj:%p, conn:%p, resultset:%p, fields size is %d, fetch row to the end", jobj, tscon, res, num_fields); 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; return JNI_FETCH_END;
......
...@@ -18,8 +18,8 @@ import java.io.InputStream; ...@@ -18,8 +18,8 @@ import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.sql.*;
import java.sql.Date; import java.sql.Date;
import java.sql.*;
import java.util.*; import java.util.*;
/* /*
...@@ -102,41 +102,49 @@ public class DatabaseMetaDataResultSet implements ResultSet { ...@@ -102,41 +102,49 @@ public class DatabaseMetaDataResultSet implements ResultSet {
@Override @Override
public byte getByte(int columnIndex) throws SQLException { public byte getByte(int columnIndex) throws SQLException {
columnIndex--;
return (byte) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); return (byte) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType());
} }
@Override @Override
public short getShort(int columnIndex) throws SQLException { public short getShort(int columnIndex) throws SQLException {
columnIndex--;
return (short) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); return (short) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType());
} }
@Override @Override
public int getInt(int columnIndex) throws SQLException { public int getInt(int columnIndex) throws SQLException {
columnIndex--;
return rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); return rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType());
} }
@Override @Override
public long getLong(int columnIndex) throws SQLException { public long getLong(int columnIndex) throws SQLException {
columnIndex--;
return rowCursor.getLong(columnIndex, columnMetaDataList.get(columnIndex).getColType()); return rowCursor.getLong(columnIndex, columnMetaDataList.get(columnIndex).getColType());
} }
@Override @Override
public float getFloat(int columnIndex) throws SQLException { public float getFloat(int columnIndex) throws SQLException {
columnIndex--;
return rowCursor.getFloat(columnIndex, columnMetaDataList.get(columnIndex).getColType()); return rowCursor.getFloat(columnIndex, columnMetaDataList.get(columnIndex).getColType());
} }
@Override @Override
public double getDouble(int columnIndex) throws SQLException { public double getDouble(int columnIndex) throws SQLException {
columnIndex--;
return rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType()); return rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType());
} }
@Override @Override
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
columnIndex--;
return new BigDecimal(rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType())); return new BigDecimal(rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType()));
} }
@Override @Override
public byte[] getBytes(int columnIndex) throws SQLException { public byte[] getBytes(int columnIndex) throws SQLException {
columnIndex--;
return (rowCursor.getString(columnIndex, columnMetaDataList.get(columnIndex).getColType())).getBytes(); return (rowCursor.getString(columnIndex, columnMetaDataList.get(columnIndex).getColType())).getBytes();
} }
......
...@@ -99,7 +99,7 @@ public class TSDBJNIConnector { ...@@ -99,7 +99,7 @@ public class TSDBJNIConnector {
this.taos = this.connectImp(host, port, dbName, user, password); this.taos = this.connectImp(host, port, dbName, user, password);
if (this.taos == TSDBConstants.JNI_NULL_POINTER) { 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; return true;
...@@ -117,52 +117,57 @@ public class TSDBJNIConnector { ...@@ -117,52 +117,57 @@ public class TSDBJNIConnector {
freeResultSet(taosResultSetPointer); freeResultSet(taosResultSetPointer);
} }
int code; long pSql = 0l;
try { try {
code = this.executeQueryImp(sql.getBytes(TaosGlobalConfig.getCharset()), this.taos); pSql = this.executeQueryImp(sql.getBytes(TaosGlobalConfig.getCharset()), this.taos);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
this.freeResultSet(pSql);
throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding")); throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding"));
} }
int code = this.getErrCode(pSql);
affectedRows = code; affectedRows = code;
if (code < 0) { if (code < 0) {
affectedRows = -1; affectedRows = -1;
if (code == TSDBConstants.JNI_TDENGINE_ERROR) { 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 { } 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 // 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, // 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 should be a non-zero value.
taosResultSetPointer = this.getResultSetImp(this.taos); taosResultSetPointer = this.getResultSetImp(this.taos, pSql);
if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) { if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) {
isResultsetClosed = false; isResultsetClosed = false;
} }
return code; return code;
} }
private native int executeQueryImp(byte[] sqlBytes, long connection); private native long executeQueryImp(byte[] sqlBytes, long connection);
/** /**
* Get recent error code by connection * Get recent error code by connection
*/ */
public int getErrCode() { public int getErrCode(Long pSql) {
return Math.abs(this.getErrCodeImp(this.taos)); 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 * Get recent error message by connection
*/ */
public String getErrMsg() { public String getErrMsg(Long pSql) {
return this.getErrMsgImp(this.taos); return this.getErrMsgImp(this.taos, pSql);
} }
private native String getErrMsgImp(long connection); private native String getErrMsgImp(long connection, Long pSql);
/** /**
* Get resultset pointer * Get resultset pointer
...@@ -172,7 +177,7 @@ public class TSDBJNIConnector { ...@@ -172,7 +177,7 @@ public class TSDBJNIConnector {
return taosResultSetPointer; 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 * Free resultset operation from C to release resultset pointer by JNI
...@@ -212,15 +217,15 @@ public class TSDBJNIConnector { ...@@ -212,15 +217,15 @@ public class TSDBJNIConnector {
/** /**
* Get affected rows count * Get affected rows count
*/ */
public int getAffectedRows() { public int getAffectedRows(Long pSql) {
int affectedRows = this.affectedRows; int affectedRows = this.affectedRows;
if (affectedRows < 0) { if (affectedRows < 0) {
affectedRows = this.getAffectedRowsImp(this.taos); affectedRows = this.getAffectedRowsImp(this.taos, pSql);
} }
return affectedRows; return affectedRows;
} }
private native int getAffectedRowsImp(long connection); private native int getAffectedRowsImp(long connection, Long pSql);
/** /**
* Get schema metadata * Get schema metadata
...@@ -248,7 +253,7 @@ public class TSDBJNIConnector { ...@@ -248,7 +253,7 @@ public class TSDBJNIConnector {
public void closeConnection() throws SQLException { public void closeConnection() throws SQLException {
int code = this.closeConnectionImp(this.taos); int code = this.closeConnectionImp(this.taos);
if (code < 0) { if (code < 0) {
throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode()); throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode(null));
} else if (code == 0) { } else if (code == 0) {
this.taos = TSDBConstants.JNI_NULL_POINTER; this.taos = TSDBConstants.JNI_NULL_POINTER;
} else { } else {
......
...@@ -27,6 +27,8 @@ public class TSDBStatement implements Statement { ...@@ -27,6 +27,8 @@ public class TSDBStatement implements Statement {
/** Timeout for a query */ /** Timeout for a query */
protected int queryTimeout = 0; protected int queryTimeout = 0;
private Long pSql = 0l;
/** /**
* Status of current statement * Status of current statement
*/ */
...@@ -66,21 +68,23 @@ public class TSDBStatement implements Statement { ...@@ -66,21 +68,23 @@ public class TSDBStatement implements Statement {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); 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(); long resultSetPointer = this.connecter.getResultSet();
if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) { if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) {
this.connecter.freeResultSet(res);
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
} else if (resultSetPointer != TSDBConstants.JNI_NULL_POINTER) { } else if (resultSetPointer != TSDBConstants.JNI_NULL_POINTER) {
this.connecter.freeResultSet(); this.connecter.freeResultSet();
throw new SQLException("The executed SQL is not a DML or a DDL"); throw new SQLException("The executed SQL is not a DML or a DDL");
} else { } else {
return res; int num = this.connecter.getAffectedRows(res);
return num;
} }
} }
public String getErrorMsg() { public String getErrorMsg(long pSql) {
return this.connecter.getErrMsg(); return this.connecter.getErrMsg(pSql);
} }
public void close() throws SQLException { public void close() throws SQLException {
...@@ -170,7 +174,7 @@ public class TSDBStatement implements Statement { ...@@ -170,7 +174,7 @@ public class TSDBStatement implements Statement {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); 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 { public boolean getMoreResults() throws SQLException {
......
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()
...@@ -461,8 +461,7 @@ int main(int argc, char *argv[]) { ...@@ -461,8 +461,7 @@ int main(int argc, char *argv[]) {
taos_init(); taos_init();
TAOS *taos = taos_connect(ip_addr, user, pass, NULL, port); TAOS *taos = taos_connect(ip_addr, user, pass, NULL, port);
if (taos == NULL) { if (taos == NULL) {
fprintf(stderr, "Failed to connect to TDengine, reason:%s\n", taos_errstr(taos)); fprintf(stderr, "Failed to connect to TDengine, reason:%s\n", taos_errstr(NULL));
taos_close(taos);
return 1; return 1;
} }
char command[BUFFER_SIZE] = "\0"; char command[BUFFER_SIZE] = "\0";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册