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

fix: jdbc resultset

上级 a7e7b8e2
......@@ -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
......
......@@ -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;
......
......@@ -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();
}
......
......@@ -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 {
......
......@@ -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 {
......
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[]) {
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";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册