未验证 提交 bd2b9f06 编写于 作者: H haojun Liao 提交者: GitHub

Merge pull request #651 from xieyinglin/master

print the java.library.path variable for debug
...@@ -19,102 +19,104 @@ import java.sql.SQLWarning; ...@@ -19,102 +19,104 @@ import java.sql.SQLWarning;
import java.util.List; import java.util.List;
public class TSDBJNIConnector { public class TSDBJNIConnector {
static volatile Boolean isInitialized = false; static volatile Boolean isInitialized = false;
static { static {
System.loadLibrary("taos"); System.loadLibrary("taos");
} System.out.println("java.library.path:" + System.getProperty("java.library.path"));
}
/**
* Connection pointer used in C /**
*/ * Connection pointer used in C
private long taos = TSDBConstants.JNI_NULL_POINTER; */
private long taos = TSDBConstants.JNI_NULL_POINTER;
/**
* Result set pointer for the current connection /**
*/ * Result set pointer for the current connection
private long taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER; */
private long taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER;
/**
* result set status in current connection /**
*/ * result set status in current connection
private boolean isResultsetClosed = true; */
private int affectedRows = -1; private boolean isResultsetClosed = true;
private int affectedRows = -1;
/**
* Whether the connection is closed /**
*/ * Whether the connection is closed
public boolean isClosed() { */
return this.taos == TSDBConstants.JNI_NULL_POINTER; public boolean isClosed() {
} return this.taos == TSDBConstants.JNI_NULL_POINTER;
}
/**
* Returns the status of last result set in current connection /**
* @return * Returns the status of last result set in current connection
*/ *
public boolean isResultsetClosed() { * @return
return this.isResultsetClosed; */
} public boolean isResultsetClosed() {
return this.isResultsetClosed;
/** }
* Initialize static variables in JNI to optimize performance
*/ /**
public static void init(String configDir, String locale, String charset, String timezone) throws SQLWarning { * Initialize static variables in JNI to optimize performance
synchronized(isInitialized) { */
if (!isInitialized) { public static void init(String configDir, String locale, String charset, String timezone) throws SQLWarning {
initImp(configDir); synchronized (isInitialized) {
if (setOptions(0, locale) < 0) { if (!isInitialized) {
throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set locale: " + locale + ". System default will be used.")); initImp(configDir);
} if (setOptions(0, locale) < 0) {
throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set locale: " + locale + ". System default will be used."));
}
if (setOptions(1, charset) < 0) { if (setOptions(1, charset) < 0) {
throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set charset: " + charset + ". System default will be used.")); throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set charset: " + charset + ". System default will be used."));
} }
if (setOptions(2, timezone) < 0) { if (setOptions(2, timezone) < 0) {
throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set timezone: " + timezone + ". System default will be used.")); throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set timezone: " + timezone + ". System default will be used."));
} }
isInitialized = true; isInitialized = true;
TaosGlobalConfig.setCharset(getTsCharset()); TaosGlobalConfig.setCharset(getTsCharset());
} }
} }
} }
public static native void initImp(String configDir); public static native void initImp(String configDir);
public static native int setOptions(int optionIndex, String optionValue); public static native int setOptions(int optionIndex, String optionValue);
public static native String getTsCharset(); public static native String getTsCharset();
/** /**
* Get connection pointer * Get connection pointer
* *
* @throws SQLException * @throws SQLException
*/ */
public boolean connect(String host, int port, String dbName, String user, String password) throws SQLException { public boolean connect(String host, int port, String dbName, String user, String password) throws SQLException {
if (this.taos != TSDBConstants.JNI_NULL_POINTER) { if (this.taos != TSDBConstants.JNI_NULL_POINTER) {
this.closeConnectionImp(this.taos); this.closeConnectionImp(this.taos);
this.taos = TSDBConstants.JNI_NULL_POINTER; this.taos = TSDBConstants.JNI_NULL_POINTER;
} }
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()), "", this.getErrCode());
} }
return true; return true;
} }
private native long connectImp(String host, int port, String dbName, String user, String password); private native long connectImp(String host, int port, String dbName, String user, String password);
/** /**
* Execute DML/DDL operation * Execute DML/DDL operation
* *
* @throws SQLException * @throws SQLException
*/ */
public int executeQuery(String sql) throws SQLException { public int executeQuery(String sql) throws SQLException {
if (!this.isResultsetClosed) { if (!this.isResultsetClosed) {
freeResultSet(taosResultSetPointer); freeResultSet(taosResultSetPointer);
} }
int code; int code;
try { try {
code = this.executeQueryImp(sql.getBytes(TaosGlobalConfig.getCharset()), this.taos); code = this.executeQueryImp(sql.getBytes(TaosGlobalConfig.getCharset()), this.taos);
...@@ -122,7 +124,7 @@ public class TSDBJNIConnector { ...@@ -122,7 +124,7 @@ public class TSDBJNIConnector {
e.printStackTrace(); e.printStackTrace();
throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding")); throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding"));
} }
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) {
...@@ -140,157 +142,159 @@ public class TSDBJNIConnector { ...@@ -140,157 +142,159 @@ public class TSDBJNIConnector {
isResultsetClosed = false; isResultsetClosed = false;
} }
return code; return code;
} }
private native int executeQueryImp(byte[] sqlBytes, long connection); private native int executeQueryImp(byte[] sqlBytes, long connection);
/** /**
* Get recent error code by connection * Get recent error code by connection
*/ */
public int getErrCode() { public int getErrCode() {
return Math.abs(this.getErrCodeImp(this.taos)); return Math.abs(this.getErrCodeImp(this.taos));
} }
private native int getErrCodeImp(long connection); private native int getErrCodeImp(long connection);
/** /**
* Get recent error message by connection * Get recent error message by connection
*/ */
public String getErrMsg() { public String getErrMsg() {
return this.getErrMsgImp(this.taos); return this.getErrMsgImp(this.taos);
} }
private native String getErrMsgImp(long connection); private native String getErrMsgImp(long connection);
/** /**
* Get resultset pointer * Get resultset pointer
* Each connection should have a single open result set at a time * Each connection should have a single open result set at a time
*/ */
public long getResultSet() { public long getResultSet() {
return taosResultSetPointer; return taosResultSetPointer;
} }
private native long getResultSetImp(long connection); private native long getResultSetImp(long connection);
/** /**
* Free resultset operation from C to release resultset pointer by JNI * Free resultset operation from C to release resultset pointer by JNI
*/ */
public int freeResultSet(long result) { public int freeResultSet(long result) {
int res = TSDBConstants.JNI_SUCCESS; int res = TSDBConstants.JNI_SUCCESS;
if (result != taosResultSetPointer && taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) { if (result != taosResultSetPointer && taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) {
throw new RuntimeException("Invalid result set pointer"); throw new RuntimeException("Invalid result set pointer");
} else if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER){ } else if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) {
res = this.freeResultSetImp(this.taos, result); res = this.freeResultSetImp(this.taos, result);
isResultsetClosed = true; // reset resultSetPointer to 0 after freeResultSetImp() return isResultsetClosed = true; // reset resultSetPointer to 0 after freeResultSetImp() return
taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER; taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER;
} else { } else {
isResultsetClosed = true; isResultsetClosed = true;
} }
return res; return res;
} }
/** /**
* Close the open result set which is associated to the current connection. If the result set is already * Close the open result set which is associated to the current connection. If the result set is already
* closed, return 0 for success. * closed, return 0 for success.
*
* @return * @return
*/ */
public int freeResultSet() { public int freeResultSet() {
int resCode = TSDBConstants.JNI_SUCCESS; int resCode = TSDBConstants.JNI_SUCCESS;
if (!isResultsetClosed) { if (!isResultsetClosed) {
resCode = this.freeResultSetImp(this.taos, this.taosResultSetPointer); resCode = this.freeResultSetImp(this.taos, this.taosResultSetPointer);
taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER; taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER;
isResultsetClosed = true; isResultsetClosed = true;
} }
return resCode; return resCode;
} }
private native int freeResultSetImp(long connection, long result); private native int freeResultSetImp(long connection, long result);
/** /**
* Get affected rows count * Get affected rows count
*/ */
public int getAffectedRows() { public int getAffectedRows() {
int affectedRows = this.affectedRows; int affectedRows = this.affectedRows;
if (affectedRows < 0) { if (affectedRows < 0) {
affectedRows = this.getAffectedRowsImp(this.taos); affectedRows = this.getAffectedRowsImp(this.taos);
} }
return affectedRows; return affectedRows;
} }
private native int getAffectedRowsImp(long connection); private native int getAffectedRowsImp(long connection);
/** /**
* Get schema metadata * Get schema metadata
*/ */
public int getSchemaMetaData(long resultSet, List<ColumnMetaData> columnMetaData) { public int getSchemaMetaData(long resultSet, List<ColumnMetaData> columnMetaData) {
return this.getSchemaMetaDataImp(this.taos, resultSet, columnMetaData); return this.getSchemaMetaDataImp(this.taos, resultSet, columnMetaData);
} }
private native int getSchemaMetaDataImp(long connection, long resultSet, List<ColumnMetaData> columnMetaData); private native int getSchemaMetaDataImp(long connection, long resultSet, List<ColumnMetaData> columnMetaData);
/** /**
* Get one row data * Get one row data
*/ */
public int fetchRow(long resultSet, TSDBResultSetRowData rowData) { public int fetchRow(long resultSet, TSDBResultSetRowData rowData) {
return this.fetchRowImp(this.taos, resultSet, rowData); return this.fetchRowImp(this.taos, resultSet, rowData);
} }
private native int fetchRowImp(long connection, long resultSet, TSDBResultSetRowData rowData); private native int fetchRowImp(long connection, long resultSet, TSDBResultSetRowData rowData);
/** /**
* Execute close operation from C to release connection pointer by JNI * Execute close operation from C to release connection pointer by JNI
* *
* @throws SQLException * @throws SQLException
*/ */
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());
} else if (code == 0){ } else if (code == 0) {
this.taos = TSDBConstants.JNI_NULL_POINTER; this.taos = TSDBConstants.JNI_NULL_POINTER;
} else { } else {
throw new SQLException("Undefined error code returned by TDengine when closing a connection"); throw new SQLException("Undefined error code returned by TDengine when closing a connection");
} }
} }
private native int closeConnectionImp(long connection); private native int closeConnectionImp(long connection);
/** /**
* Subscribe to a table in TSDB * Subscribe to a table in TSDB
*/ */
public long subscribe(String host, String user, String password, String database, String table, long time, int period){ public long subscribe(String host, String user, String password, String database, String table, long time, int period) {
return subscribeImp(host, user, password, database, table, time, period); return subscribeImp(host, user, password, database, table, time, period);
} }
private native long subscribeImp(String host, String user, String password, String database, String table, long time, int period); private native long subscribeImp(String host, String user, String password, String database, String table, long time, int period);
/** /**
* Consume a subscribed table * Consume a subscribed table
*/ */
public TSDBResultSetRowData consume(long subscription) { public TSDBResultSetRowData consume(long subscription) {
return this.consumeImp(subscription); return this.consumeImp(subscription);
} }
private native TSDBResultSetRowData consumeImp(long subscription); private native TSDBResultSetRowData consumeImp(long subscription);
/** /**
* Unsubscribe a table * Unsubscribe a table
* @param subscription *
*/ * @param subscription
public void unsubscribe(long subscription) { */
unsubscribeImp(subscription); public void unsubscribe(long subscription) {
} unsubscribeImp(subscription);
}
private native void unsubscribeImp(long subscription);
private native void unsubscribeImp(long subscription);
/**
* Validate if a <I>create table</I> sql statement is correct without actually creating that table /**
*/ * Validate if a <I>create table</I> sql statement is correct without actually creating that table
public boolean validateCreateTableSql(String sql) { */
long connection = taos; public boolean validateCreateTableSql(String sql) {
int res = validateCreateTableSqlImp(connection, sql.getBytes()); long connection = taos;
int res = validateCreateTableSqlImp(connection, sql.getBytes());
return res != 0 ? false : true; return res != 0 ? false : true;
} }
private native int validateCreateTableSqlImp(long connection, byte[] sqlBytes); private native int validateCreateTableSqlImp(long connection, byte[] sqlBytes);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册