diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java index c4785127fdbfaad3b67c2aa513bc874d7dd3fd9a..28b7bd6a5ff0eef90dee8d97767f71a44ed1552d 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDriver.java @@ -1,74 +1,12 @@ package com.taosdata.jdbc; -import java.io.*; import java.sql.Driver; import java.sql.DriverPropertyInfo; -import java.util.ArrayList; -import java.util.List; import java.util.Properties; import java.util.StringTokenizer; public abstract class AbstractDriver implements Driver { - private static final String TAOS_CFG_FILENAME = "taos.cfg"; - - /** - * @param cfgDirPath - * @return return the config dir - **/ - protected File loadConfigDir(String cfgDirPath) { - if (cfgDirPath == null) - return loadDefaultConfigDir(); - File cfgDir = new File(cfgDirPath); - if (!cfgDir.exists()) - return loadDefaultConfigDir(); - return cfgDir; - } - - /** - * @return search the default config dir, if the config dir is not exist will return null - */ - protected File loadDefaultConfigDir() { - File cfgDir; - File cfgDir_linux = new File("/etc/taos"); - cfgDir = cfgDir_linux.exists() ? cfgDir_linux : null; - File cfgDir_windows = new File("C:\\TDengine\\cfg"); - cfgDir = (cfgDir == null && cfgDir_windows.exists()) ? cfgDir_windows : cfgDir; - return cfgDir; - } - - protected List loadConfigEndpoints(File cfgFile) { - List endpoints = new ArrayList<>(); - try (BufferedReader reader = new BufferedReader(new FileReader(cfgFile))) { - String line = null; - while ((line = reader.readLine()) != null) { - if (line.trim().startsWith("firstEp") || line.trim().startsWith("secondEp")) { - endpoints.add(line.substring(line.indexOf('p') + 1).trim()); - } - if (endpoints.size() > 1) - break; - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return endpoints; - } - - protected void loadTaosConfig(Properties info) { - if ((info.getProperty(TSDBDriver.PROPERTY_KEY_HOST) == null || info.getProperty(TSDBDriver.PROPERTY_KEY_HOST).isEmpty()) && ( - info.getProperty(TSDBDriver.PROPERTY_KEY_PORT) == null || info.getProperty(TSDBDriver.PROPERTY_KEY_PORT).isEmpty())) { - File cfgDir = loadConfigDir(info.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR)); - File cfgFile = cfgDir.listFiles((dir, name) -> TAOS_CFG_FILENAME.equalsIgnoreCase(name))[0]; - List endpoints = loadConfigEndpoints(cfgFile); - if (!endpoints.isEmpty()) { - info.setProperty(TSDBDriver.PROPERTY_KEY_HOST, endpoints.get(0).split(":")[0]); - info.setProperty(TSDBDriver.PROPERTY_KEY_PORT, endpoints.get(0).split(":")[1]); - } - } - } - protected DriverPropertyInfo[] getPropertyInfo(Properties info) { DriverPropertyInfo hostProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_HOST, info.getProperty(TSDBDriver.PROPERTY_KEY_HOST)); hostProp.required = false; @@ -154,6 +92,4 @@ public abstract class AbstractDriver implements Driver { return urlProps; } - - } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java index fe16aa653546f4033bd944f0b6c72aec1863ab8a..14e75f0e09e3403e703658fb503019fefbb6156d 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java @@ -52,4 +52,14 @@ public class ColumnMetaData { public void setColIndex(int colIndex) { this.colIndex = colIndex; } + + @Override + public String toString() { + return "ColumnMetaData{" + + "colType=" + colType + + ", colName='" + colName + '\'' + + ", colSize=" + colSize + + ", colIndex=" + colIndex + + '}'; + } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java index 8d947b9411eb91eded49b3c7b1f12586682346ff..a8653eb1729dffa83fb556e1f7a1772257018699 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java @@ -87,11 +87,10 @@ public class TSDBConnection extends AbstractConnection { } public void close() throws SQLException { - if (isClosed()) { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_CONNECTION_CLOSED); - } - this.isClosed = true; + if (isClosed) + return; this.connector.closeConnection(); + this.isClosed = true; } public boolean isClosed() throws SQLException { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index 2b87b72fef0f2b621536c5a11aba69975aa86434..530ece6bcb6d1e75609ceee0a333dcd1aeb39bde 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -112,8 +112,6 @@ public class TSDBDriver extends AbstractDriver { if ((props = parseURL(url, info)) == null) { return null; } - //load taos.cfg start - loadTaosConfig(info); try { TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), 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 f3c4e025798e8e3fd8db0e5c1a1b25aa6fea3dc7..b27bc63db17e2f7268ee9f1c8ae3c074db15c0e5 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 @@ -1,4 +1,5 @@ -/*************************************************************************** +/** + * ************************************************************************* * Copyright (c) 2019 TAOS Data, Inc. * * This program is free software: you can use, redistribute, and/or modify @@ -11,7 +12,7 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - *****************************************************************************/ + **************************************************************************** */ package com.taosdata.jdbc; import com.taosdata.jdbc.utils.TaosInfo; @@ -20,6 +21,9 @@ import java.sql.SQLException; import java.sql.SQLWarning; import java.util.List; +/** + * JNI connector + * */ public class TSDBJNIConnector { private static volatile Boolean isInitialized = false; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java index 80ff49253016c632b6bf85f4756fe2d702a9bffc..5d68ffad6c35697bdc96d4b1919f23be5aa88e4a 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java @@ -20,18 +20,16 @@ import java.util.ArrayList; import java.util.List; public class TSDBResultSet extends AbstractResultSet implements ResultSet { - private TSDBJNIConnector jniConnector; - + private final TSDBJNIConnector jniConnector; private final TSDBStatement statement; - private long resultSetPointer = 0L; + private final long resultSetPointer; private List columnMetaDataList = new ArrayList<>(); - - private TSDBResultSetRowData rowData; - private TSDBResultSetBlockData blockData; + private final TSDBResultSetRowData rowData; + private final TSDBResultSetBlockData blockData; private boolean batchFetch = false; private boolean lastWasNull = false; - private final int COLUMN_INDEX_START_VALUE = 1; + private boolean isClosed; public void setBatchFetch(boolean batchFetch) { this.batchFetch = batchFetch; @@ -56,13 +54,13 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { int code = this.jniConnector.getSchemaMetaData(this.resultSetPointer, this.columnMetaDataList); if (code == TSDBConstants.JNI_CONNECTION_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } if (code == TSDBConstants.JNI_RESULT_SET_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_RESULT_SET_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); } if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_NUM_OF_FIELDS_0)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_NUM_OF_FIELDS_0); } this.rowData = new TSDBResultSetRowData(this.columnMetaDataList.size()); this.blockData = new TSDBResultSetBlockData(this.columnMetaDataList, this.columnMetaDataList.size()); @@ -78,16 +76,12 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { this.blockData.reset(); if (code == TSDBConstants.JNI_CONNECTION_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } else if (code == TSDBConstants.JNI_RESULT_SET_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_RESULT_SET_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); } else if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_NUM_OF_FIELDS_0)); - } else if (code == TSDBConstants.JNI_FETCH_END) { - return false; - } - - return true; + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_NUM_OF_FIELDS_0); + } else return code != TSDBConstants.JNI_FETCH_END; } else { if (rowData != null) { this.rowData.clear(); @@ -95,11 +89,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { int code = this.jniConnector.fetchRow(this.resultSetPointer, this.rowData); if (code == TSDBConstants.JNI_CONNECTION_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } else if (code == TSDBConstants.JNI_RESULT_SET_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_RESULT_SET_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); } else if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_NUM_OF_FIELDS_0)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_NUM_OF_FIELDS_0); } else if (code == TSDBConstants.JNI_FETCH_END) { return false; } else { @@ -109,14 +103,17 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { } public void close() throws SQLException { + if (isClosed) + return; if (this.jniConnector != null) { int code = this.jniConnector.freeResultSet(this.resultSetPointer); if (code == TSDBConstants.JNI_CONNECTION_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } else if (code == TSDBConstants.JNI_RESULT_SET_NULL) { - throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_RESULT_SET_NULL)); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); } } + isClosed = true; } public boolean wasNull() throws SQLException { @@ -415,8 +412,8 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { } public boolean isClosed() throws SQLException { - //TODO: check if need release resources - boolean isClosed = true; + if (isClosed) + return true; if (jniConnector != null) { isClosed = jniConnector.isResultsetClosed(); } @@ -429,14 +426,12 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { } private int getTrueColumnIndex(int columnIndex) throws SQLException { - if (columnIndex < this.COLUMN_INDEX_START_VALUE) { - throw new SQLException("Column Index out of range, " + columnIndex + " < " + this.COLUMN_INDEX_START_VALUE); - } + if (columnIndex < 1) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "columnIndex(" + columnIndex + "): < 1"); int numOfCols = this.columnMetaDataList.size(); - if (columnIndex > numOfCols) { - throw new SQLException("Column Index out of range, " + columnIndex + " > " + numOfCols); - } + if (columnIndex > numOfCols) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE, "columnIndex: " + columnIndex); return columnIndex - 1; } } 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 996ea4a185d97b2294ac06cacccd08862de2de94..29e849049e8ec0206ca3fe906f86534ecf7fd53a 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 @@ -73,11 +73,11 @@ public class TSDBStatement extends AbstractStatement { } public void close() throws SQLException { - if (!isClosed) { - if (this.resultSet != null) - this.resultSet.close(); - isClosed = true; - } + if (isClosed) + return; + if (this.resultSet != null && !this.resultSet.isClosed()) + this.resultSet.close(); + isClosed = true; } public boolean execute(String sql) throws SQLException { diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java index 5e4f16a73c3c93456ed67450752af094cd96938c..aba95f65b7d618d8f008cad986dd879f5113724c 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java @@ -7,24 +7,26 @@ import java.sql.SQLWarning; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.*; - public class TSDBJNIConnectorTest { - public static void main(String[] args) { + private static TSDBResultSetRowData rowData; + + @Test + public void test() { try { - TSDBJNIConnector.init("/etc/taos/taos.cfg", "en_US.UTF-8", "", ""); + // init + TSDBJNIConnector.init(null, null, null, null); + // connect TSDBJNIConnector connector = new TSDBJNIConnector(); - connector.connect("127.0.0.1", 6030, "test", "root", "taosdata"); - long pSql = connector.executeQuery("show dnodes"); - // if pSql is create/insert/update/delete/alter SQL + connector.connect("127.0.0.1", 6030, null, "root", "taosdata"); + // executeQuery + long pSql = connector.executeQuery("show variables"); if (connector.isUpdateQuery(pSql)) { connector.freeResultSet(pSql); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_WITH_EXECUTEQUERY); } - + // get schema List columnMetaDataList = new ArrayList<>(); - int code = connector.getSchemaMetaData(pSql, columnMetaDataList); if (code == TSDBConstants.JNI_CONNECTION_NULL) { throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); @@ -35,6 +37,29 @@ public class TSDBJNIConnectorTest { if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) { throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_NUM_OF_FIELDS_0)); } + int columnSize = columnMetaDataList.size(); + // print metadata + for (int i = 0; i < columnSize; i++) { + System.out.println(columnMetaDataList.get(i)); + } + rowData = new TSDBResultSetRowData(columnSize); + // iterate resultSet + for (int i = 0; next(connector, pSql); i++) { + System.out.println("col[" + i + "] size: " + rowData.getColSize()); + rowData.getData().stream().forEach(col -> System.out.print(col + "\t")); + System.out.println(); + } + // close resultSet + code = connector.freeResultSet(pSql); + if (code == TSDBConstants.JNI_CONNECTION_NULL) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); + } else if (code == TSDBConstants.JNI_RESULT_SET_NULL) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); + } + // close statement + + // close connection + connector.closeConnection(); } catch (SQLWarning throwables) { throwables.printStackTrace(); @@ -43,88 +68,22 @@ public class TSDBJNIConnectorTest { } } - - @Test - public void isClosed() { - } - - @Test - public void isResultsetClosed() { - } - - @Test - public void init() { - } - - @Test - public void initImp() { - } - - @Test - public void setOptions() { - } - - @Test - public void getTsCharset() { - } - - @Test - public void connect() { - } - - @Test - public void executeQuery() { - } - - @Test - public void getErrCode() { - } - - @Test - public void getErrMsg() { - } - - @Test - public void isUpdateQuery() { - } - - @Test - public void freeResultSet() { - } - - @Test - public void getAffectedRows() { - } - - @Test - public void getSchemaMetaData() { - } - - @Test - public void fetchRow() { - } - - @Test - public void fetchBlock() { - } - - @Test - public void closeConnection() { - } - - @Test - public void subscribe() { - } - - @Test - public void consume() { - } - - @Test - public void unsubscribe() { + private static boolean next(TSDBJNIConnector connector, long pSql) throws SQLException { + if (rowData != null) + rowData.clear(); + + int code = connector.fetchRow(pSql, rowData); + if (code == TSDBConstants.JNI_CONNECTION_NULL) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); + } else if (code == TSDBConstants.JNI_RESULT_SET_NULL) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); + } else if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_NUM_OF_FIELDS_0); + } else if (code == TSDBConstants.JNI_FETCH_END) { + return false; + } else { + return true; + } } - @Test - public void validateCreateTableSql() { - } } \ No newline at end of file