未验证 提交 352cde98 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #3480 from taosdata/hotfix/td-1350

Hotfix/td 1350
...@@ -93,14 +93,13 @@ ...@@ -93,14 +93,13 @@
<version>3.6.1</version> <version>3.6.1</version>
<configuration> <configuration>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<source>11</source> <source>8</source>
<target>11</target> <target>8</target>
<debug>true</debug> <debug>true</debug>
<showDeprecation>true</showDeprecation> <showDeprecation>true</showDeprecation>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
......
...@@ -57,9 +57,9 @@ public class TSDBConnection implements Connection { ...@@ -57,9 +57,9 @@ public class TSDBConnection implements Connection {
File cfgDir = loadConfigDir(info.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR)); File cfgDir = loadConfigDir(info.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR));
File cfgFile = cfgDir.listFiles((dir, name) -> "taos.cfg".equalsIgnoreCase(name))[0]; File cfgFile = cfgDir.listFiles((dir, name) -> "taos.cfg".equalsIgnoreCase(name))[0];
List<String> endpoints = loadConfigEndpoints(cfgFile); List<String> endpoints = loadConfigEndpoints(cfgFile);
if (!endpoints.isEmpty()){ if (!endpoints.isEmpty()) {
info.setProperty(TSDBDriver.PROPERTY_KEY_HOST,endpoints.get(0).split(":")[0]); info.setProperty(TSDBDriver.PROPERTY_KEY_HOST, endpoints.get(0).split(":")[0]);
info.setProperty(TSDBDriver.PROPERTY_KEY_PORT,endpoints.get(0).split(":")[1]); info.setProperty(TSDBDriver.PROPERTY_KEY_PORT, endpoints.get(0).split(":")[1]);
} }
//load taos.cfg end //load taos.cfg end
...@@ -69,15 +69,15 @@ public class TSDBConnection implements Connection { ...@@ -69,15 +69,15 @@ public class TSDBConnection implements Connection {
info.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD)); info.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD));
} }
private List<String> loadConfigEndpoints(File cfgFile){ private List<String> loadConfigEndpoints(File cfgFile) {
List<String> endpoints = new ArrayList<>(); List<String> endpoints = new ArrayList<>();
try(BufferedReader reader = new BufferedReader(new FileReader(cfgFile))) { try (BufferedReader reader = new BufferedReader(new FileReader(cfgFile))) {
String line = null; String line = null;
while ((line = reader.readLine())!=null){ while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("firstEp") || line.trim().startsWith("secondEp")){ if (line.trim().startsWith("firstEp") || line.trim().startsWith("secondEp")) {
endpoints.add(line.substring(line.indexOf('p')+1).trim()); endpoints.add(line.substring(line.indexOf('p') + 1).trim());
} }
if (endpoints.size()>1) if (endpoints.size() > 1)
break; break;
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
...@@ -91,7 +91,7 @@ public class TSDBConnection implements Connection { ...@@ -91,7 +91,7 @@ public class TSDBConnection implements Connection {
/** /**
* @param cfgDirPath * @param cfgDirPath
* @return return the config dir * @return return the config dir
* **/ **/
private File loadConfigDir(String cfgDirPath) { private File loadConfigDir(String cfgDirPath) {
if (cfgDirPath == null) if (cfgDirPath == null)
return loadDefaultConfigDir(); return loadDefaultConfigDir();
...@@ -103,8 +103,8 @@ public class TSDBConnection implements Connection { ...@@ -103,8 +103,8 @@ public class TSDBConnection implements Connection {
/** /**
* @return search the default config dir, if the config dir is not exist will return null * @return search the default config dir, if the config dir is not exist will return null
* */ */
private File loadDefaultConfigDir(){ private File loadDefaultConfigDir() {
File cfgDir; File cfgDir;
File cfgDir_linux = new File("/etc/taos"); File cfgDir_linux = new File("/etc/taos");
cfgDir = cfgDir_linux.exists() ? cfgDir_linux : null; cfgDir = cfgDir_linux.exists() ? cfgDir_linux : null;
...@@ -132,7 +132,9 @@ public class TSDBConnection implements Connection { ...@@ -132,7 +132,9 @@ public class TSDBConnection implements Connection {
public Statement createStatement() throws SQLException { public Statement createStatement() throws SQLException {
if (!this.connector.isClosed()) { if (!this.connector.isClosed()) {
return new TSDBStatement(this.connector); TSDBStatement statement = new TSDBStatement(this, this.connector);
statement.setConnection(this);
return statement;
} else { } else {
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
} }
...@@ -153,7 +155,7 @@ public class TSDBConnection implements Connection { ...@@ -153,7 +155,7 @@ public class TSDBConnection implements Connection {
public PreparedStatement prepareStatement(String sql) throws SQLException { public PreparedStatement prepareStatement(String sql) throws SQLException {
if (!this.connector.isClosed()) { if (!this.connector.isClosed()) {
return new TSDBPreparedStatement(this.connector, sql); return new TSDBPreparedStatement(this, this.connector, sql);
} else { } else {
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
} }
......
...@@ -42,8 +42,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -42,8 +42,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
private SavedPreparedStatement savedPreparedStatement; private SavedPreparedStatement savedPreparedStatement;
TSDBPreparedStatement(TSDBJNIConnector connecter, String sql) { TSDBPreparedStatement(TSDBConnection connection, TSDBJNIConnector connecter, String sql) {
super(connecter); super(connection, connecter);
init(sql); init(sql);
} }
......
...@@ -19,153 +19,164 @@ import java.util.ArrayList; ...@@ -19,153 +19,164 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TSDBStatement implements Statement { public class TSDBStatement implements Statement {
private TSDBJNIConnector connecter = null; private TSDBJNIConnector connecter = null;
/** To store batched commands */ /**
protected List<String> batchedArgs; * To store batched commands
*/
protected List<String> batchedArgs;
/** Timeout for a query */ /**
protected int queryTimeout = 0; * Timeout for a query
*/
protected int queryTimeout = 0;
private Long pSql = 0l; private Long pSql = 0l;
/** /**
* Status of current statement * Status of current statement
*/ */
private boolean isClosed = true; private boolean isClosed = true;
private int affectedRows = 0; private int affectedRows = 0;
TSDBStatement(TSDBJNIConnector connecter) { private TSDBConnection connection;
this.connecter = connecter;
this.isClosed = false;
}
public <T> T unwrap(Class<T> iface) throws SQLException { public void setConnection(TSDBConnection connection) {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); this.connection = connection;
} }
public boolean isWrapperFor(Class<?> iface) throws SQLException { TSDBStatement(TSDBConnection connection, TSDBJNIConnector connecter) {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); this.connection = connection;
} this.connecter = connecter;
this.isClosed = false;
}
public ResultSet executeQuery(String sql) throws SQLException { public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
}
public ResultSet executeQuery(String sql) throws SQLException {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); throw new SQLException("Invalid method call on a closed statement.");
} }
// TODO make sure it is not a update query // TODO make sure it is not a update query
pSql = this.connecter.executeQuery(sql); pSql = 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(pSql); this.connecter.freeResultSet(pSql);
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
} }
// create/insert/update/delete/alter // create/insert/update/delete/alter
if (resultSetPointer == TSDBConstants.JNI_NULL_POINTER) { if (resultSetPointer == TSDBConstants.JNI_NULL_POINTER) {
this.connecter.freeResultSet(pSql); this.connecter.freeResultSet(pSql);
return null; return null;
} }
if (!this.connecter.isUpdateQuery(pSql)) { if (!this.connecter.isUpdateQuery(pSql)) {
return new TSDBResultSet(this.connecter, resultSetPointer); return new TSDBResultSet(this.connecter, resultSetPointer);
} else { } else {
this.connecter.freeResultSet(pSql); this.connecter.freeResultSet(pSql);
return null; return null;
} }
} }
public int executeUpdate(String sql) throws SQLException { public int executeUpdate(String sql) throws SQLException {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); throw new SQLException("Invalid method call on a closed statement.");
} }
// TODO check if current query is update query // TODO check if current query is update query
pSql = this.connecter.executeQuery(sql); pSql = 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(pSql); this.connecter.freeResultSet(pSql);
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
} }
this.affectedRows = this.connecter.getAffectedRows(pSql); this.affectedRows = this.connecter.getAffectedRows(pSql);
this.connecter.freeResultSet(pSql); this.connecter.freeResultSet(pSql);
return this.affectedRows; return this.affectedRows;
} }
public String getErrorMsg(long pSql) { public String getErrorMsg(long pSql) {
return this.connecter.getErrMsg(pSql); return this.connecter.getErrMsg(pSql);
} }
public void close() throws SQLException { public void close() throws SQLException {
if (!isClosed) { if (!isClosed) {
if (!this.connecter.isResultsetClosed()) { if (!this.connecter.isResultsetClosed()) {
this.connecter.freeResultSet(); this.connecter.freeResultSet();
} }
isClosed = true; isClosed = true;
} }
} }
public int getMaxFieldSize() throws SQLException { public int getMaxFieldSize() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public void setMaxFieldSize(int max) throws SQLException { public void setMaxFieldSize(int max) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public int getMaxRows() throws SQLException { public int getMaxRows() throws SQLException {
// always set maxRows to zero, meaning unlimitted rows in a resultSet // always set maxRows to zero, meaning unlimitted rows in a resultSet
return 0; return 0;
} }
public void setMaxRows(int max) throws SQLException { public void setMaxRows(int max) throws SQLException {
// always set maxRows to zero, meaning unlimitted rows in a resultSet // always set maxRows to zero, meaning unlimitted rows in a resultSet
} }
public void setEscapeProcessing(boolean enable) throws SQLException { public void setEscapeProcessing(boolean enable) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public int getQueryTimeout() throws SQLException { public int getQueryTimeout() throws SQLException {
return queryTimeout; return queryTimeout;
} }
public void setQueryTimeout(int seconds) throws SQLException { public void setQueryTimeout(int seconds) throws SQLException {
this.queryTimeout = seconds; this.queryTimeout = seconds;
} }
public void cancel() throws SQLException { public void cancel() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public SQLWarning getWarnings() throws SQLException { public SQLWarning getWarnings() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public void clearWarnings() throws SQLException { public void clearWarnings() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public void setCursorName(String name) throws SQLException { public void setCursorName(String name) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public boolean execute(String sql) throws SQLException { public boolean execute(String sql) throws SQLException {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); throw new SQLException("Invalid method call on a closed statement.");
} }
boolean res = true; boolean res = true;
pSql = this.connecter.executeQuery(sql); pSql = 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(pSql); this.connecter.freeResultSet(pSql);
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) {
// no result set is retrieved // no result set is retrieved
...@@ -173,145 +184,147 @@ public class TSDBStatement implements Statement { ...@@ -173,145 +184,147 @@ public class TSDBStatement implements Statement {
res = false; res = false;
} }
return res; return res;
} }
public ResultSet getResultSet() throws SQLException { public ResultSet getResultSet() throws SQLException {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); throw new SQLException("Invalid method call on a closed statement.");
} }
long resultSetPointer = connecter.getResultSet(); long resultSetPointer = connecter.getResultSet();
TSDBResultSet resSet = null; TSDBResultSet resSet = null;
if (resultSetPointer != TSDBConstants.JNI_NULL_POINTER) { if (resultSetPointer != TSDBConstants.JNI_NULL_POINTER) {
resSet = new TSDBResultSet(connecter, resultSetPointer); resSet = new TSDBResultSet(connecter, resultSetPointer);
} }
return resSet; return resSet;
} }
public int getUpdateCount() throws SQLException { public int getUpdateCount() throws SQLException {
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.affectedRows; return this.affectedRows;
} }
public boolean getMoreResults() throws SQLException { public boolean getMoreResults() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public void setFetchDirection(int direction) throws SQLException { public void setFetchDirection(int direction) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public int getFetchDirection() throws SQLException { public int getFetchDirection() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
/* /*
* used by spark * used by spark
*/ */
public void setFetchSize(int rows) throws SQLException { public void setFetchSize(int rows) throws SQLException {
} }
/* /*
* used by spark * used by spark
*/ */
public int getFetchSize() throws SQLException { public int getFetchSize() throws SQLException {
return 4096; return 4096;
} }
public int getResultSetConcurrency() throws SQLException { public int getResultSetConcurrency() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public int getResultSetType() throws SQLException { public int getResultSetType() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} }
public void addBatch(String sql) throws SQLException { public void addBatch(String sql) throws SQLException {
if (batchedArgs == null) { if (batchedArgs == null) {
batchedArgs = new ArrayList<String>(); batchedArgs = new ArrayList<String>();
} }
batchedArgs.add(sql); batchedArgs.add(sql);
} }
public void clearBatch() throws SQLException { public void clearBatch() throws SQLException {
batchedArgs.clear(); batchedArgs.clear();
} }
public int[] executeBatch() throws SQLException { public int[] executeBatch() throws SQLException {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); throw new SQLException("Invalid method call on a closed statement.");
} }
if (batchedArgs == null) { if (batchedArgs == null) {
throw new SQLException(TSDBConstants.WrapErrMsg("Batch is empty!")); throw new SQLException(TSDBConstants.WrapErrMsg("Batch is empty!"));
} else { } else {
int[] res = new int[batchedArgs.size()]; int[] res = new int[batchedArgs.size()];
for (int i = 0; i < batchedArgs.size(); i++) { for (int i = 0; i < batchedArgs.size(); i++) {
res[i] = executeUpdate(batchedArgs.get(i)); res[i] = executeUpdate(batchedArgs.get(i));
} }
return res; return res;
} }
} }
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG); if (this.connecter != null)
} return this.connection;
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public boolean getMoreResults(int current) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public boolean getMoreResults(int current) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public ResultSet getGeneratedKeys() throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public ResultSet getGeneratedKeys() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public int executeUpdate(String sql, String[] columnNames) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public int executeUpdate(String sql, String[] columnNames) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public boolean execute(String sql, int[] columnIndexes) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public boolean execute(String sql, int[] columnIndexes) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public boolean execute(String sql, String[] columnNames) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public boolean execute(String sql, String[] columnNames) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public int getResultSetHoldability() throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public int getResultSetHoldability() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public boolean isClosed() throws SQLException { }
return isClosed;
} public boolean isClosed() throws SQLException {
return isClosed;
public void setPoolable(boolean poolable) throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public void setPoolable(boolean poolable) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public boolean isPoolable() throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public boolean isPoolable() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public void closeOnCompletion() throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public void closeOnCompletion() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
public boolean isCloseOnCompletion() throws SQLException { }
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
} public boolean isCloseOnCompletion() throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
}
} }
...@@ -23,6 +23,7 @@ import java.sql.SQLException; ...@@ -23,6 +23,7 @@ import java.sql.SQLException;
public class SqlSyntaxValidator { public class SqlSyntaxValidator {
private TSDBConnection tsdbConnection; private TSDBConnection tsdbConnection;
public SqlSyntaxValidator(Connection connection) { public SqlSyntaxValidator(Connection connection) {
this.tsdbConnection = (TSDBConnection) connection; this.tsdbConnection = (TSDBConnection) connection;
} }
......
...@@ -8,8 +8,7 @@ import org.junit.BeforeClass; ...@@ -8,8 +8,7 @@ import org.junit.BeforeClass;
public class BaseTest { public class BaseTest {
private static boolean testCluster = false; private static boolean testCluster = false;
private static TDNodes nodes = new TDNodes(); private static TDNodes nodes = new TDNodes();
@BeforeClass @BeforeClass
public static void setupEnv() { public static void setupEnv() {
...@@ -19,11 +18,9 @@ public class BaseTest { ...@@ -19,11 +18,9 @@ public class BaseTest {
nodes.getTDNode(1).setRunning(1); nodes.getTDNode(1).setRunning(1);
nodes.stop(1); nodes.stop(1);
} }
nodes.setTestCluster(testCluster); nodes.setTestCluster(testCluster);
nodes.deploy(1); nodes.deploy(1);
nodes.start(1); nodes.start(1);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -7,13 +7,11 @@ import org.junit.Test; ...@@ -7,13 +7,11 @@ import org.junit.Test;
import java.sql.*; import java.sql.*;
import java.util.Properties; import java.util.Properties;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ExecutorService;
import static org.junit.Assert.assertEquals;
import java.util.Properties;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.*; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals;
public class BatchInsertTest extends BaseTest { public class BatchInsertTest extends BaseTest {
......
...@@ -34,7 +34,6 @@ public class SelectTest extends BaseTest { ...@@ -34,7 +34,6 @@ public class SelectTest extends BaseTest {
statement.executeUpdate("drop database if exists " + dbName); statement.executeUpdate("drop database if exists " + dbName);
statement.executeUpdate("create database if not exists " + dbName); statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)"); statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)");
} }
@Test @Test
...@@ -66,6 +65,5 @@ public class SelectTest extends BaseTest { ...@@ -66,6 +65,5 @@ public class SelectTest extends BaseTest {
statement.close(); statement.close();
connection.close(); connection.close();
Thread.sleep(10); Thread.sleep(10);
} }
} }
package com.taosdata.jdbc.cases;
import com.taosdata.jdbc.lib.TSDBCommon;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
public class BatchInsertTest {
static String host = "localhost";
static String dbName = "test";
static String stbName = "meters";
static int numOfTables = 30;
final static int numOfRecordsPerTable = 1000;
static long ts = 1496732686000l;
final static String tablePrefix = "t";
private Connection connection;
@Before
public void before() {
try {
connection = TSDBCommon.getConn(host);
TSDBCommon.createDatabase(connection, dbName);
TSDBCommon.createStable(connection, stbName);
TSDBCommon.createTables(connection, numOfTables, stbName, tablePrefix);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testBatchInsert(){
ExecutorService executorService = Executors.newFixedThreadPool(numOfTables);
for (int i = 0; i < numOfTables; i++) {
final int index = i;
executorService.execute(new Runnable() {
@Override
public void run() {
try {
long startTime = System.currentTimeMillis();
Statement statement = connection.createStatement(); // get statement
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO " + tablePrefix + index + " VALUES");
Random rand = new Random();
for (int j = 1; j <= numOfRecordsPerTable; j++) {
sb.append("(" + (ts + j) + ", ");
sb.append(rand.nextInt(100) + ", ");
sb.append(rand.nextInt(100) + ", ");
sb.append(rand.nextInt(100) + ")");
}
statement.addBatch(sb.toString());
statement.executeBatch();
long endTime = System.currentTimeMillis();
System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds");
connection.commit();
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
executorService.shutdown();
try {
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from meters");
int num = 0;
while (rs.next()) {
num++;
}
assertEquals(num, numOfTables * numOfRecordsPerTable);
rs.close();
}catch (Exception e){
e.printStackTrace();
}
}
@After
public void after() {
try {
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.taosdata.jdbc.lib;
import com.taosdata.jdbc.TSDBDriver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class TSDBCommon {
public static Connection getConn(String host) throws SQLException, ClassNotFoundException {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
return DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
}
public static void createDatabase(Connection connection, String dbName) throws SQLException {
Statement statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("use " + dbName);
statement.close();
}
public static void createStable(Connection connection, String stbName) throws SQLException {
Statement statement = connection.createStatement();
String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))";
statement.executeUpdate(createTableSql);
statement.close();
}
public static void createTables(Connection connection, int numOfTables, String stbName,String tablePrefix) throws SQLException {
Statement statement = connection.createStatement();
for(int i = 0; i < numOfTables; i++) {
String loc = i % 2 == 0 ? "beijing" : "shanghai";
String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')";
statement.executeUpdate(createSubTalbesSql);
}
statement.close();
}
}
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.taosdata.jdbc</groupId> <groupId>com.taosdata.jdbc</groupId>
<artifactId>SpringJdbcTemplate</artifactId> <artifactId>SpringJdbcTemplate</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<name>SpringJdbcTemplate</name> <name>SpringJdbcTemplate</name>
<url>http://www.taosdata.com</url> <url>http://www.taosdata.com</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version> <version>5.2.8.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId> <artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version> <version>5.1.9.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.springframework</groupId>
<artifactId>junit</artifactId> <artifactId>spring-test</artifactId>
<version>4.11</version> <version>5.1.9.RELEASE</version>
<scope>test</scope> </dependency>
</dependency>
<dependency> <dependency>
<groupId>com.taosdata.jdbc</groupId> <groupId>junit</groupId>
<artifactId>taos-jdbcdriver</artifactId> <artifactId>junit</artifactId>
<version>2.0.2</version> <version>4.13</version>
</dependency> <scope>test</scope>
</dependency>
</dependencies> <dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.4</version>
</dependency>
<build> </dependencies>
<plugins>
<plugin> <build>
<artifactId>maven-compiler-plugin</artifactId> <plugins>
<version>3.8.0</version> <plugin>
<configuration> <artifactId>maven-compiler-plugin</artifactId>
<source>1.8</source> <version>3.8.0</version>
<target>1.8</target> <configuration>
</configuration> <source>1.8</source>
</plugin> <target>1.8</target>
<plugin> </configuration>
<groupId>org.apache.maven.plugins</groupId> </plugin>
<artifactId>maven-assembly-plugin</artifactId> <plugin>
<version>3.1.0</version> <groupId>org.apache.maven.plugins</groupId>
<configuration> <artifactId>maven-assembly-plugin</artifactId>
<archive> <version>3.1.0</version>
<manifest> <configuration>
<mainClass>com.taosdata.jdbc.App</mainClass> <archive>
</manifest> <manifest>
</archive> <mainClass>com.taosdata.jdbc.example.jdbcTemplate.App</mainClass>
<descriptorRefs> </manifest>
<descriptorRef>jar-with-dependencies</descriptorRef> </archive>
</descriptorRefs> <descriptorRefs>
</configuration> <descriptorRef>jar-with-dependencies</descriptorRef>
<executions> </descriptorRefs>
<execution> </configuration>
<id>make-assembly</id> <executions>
<phase>package</phase> <execution>
<goals> <id>make-assembly</id>
<goal>single</goal> <phase>package</phase>
</goals> <goals>
</execution> <goal>single</goal>
</executions> </goals>
</plugin> </execution>
</plugins> </executions>
</build> </plugin>
</plugins>
</build>
</project> </project>
package com.taosdata.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
public class App {
public static void main( String[] args ) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
// create database
jdbcTemplate.execute("create database if not exists db ");
// create table
jdbcTemplate.execute("create table if not exists db.tb (ts timestamp, temperature int, humidity float)");
String insertSql = "insert into db.tb values(now, 23, 10.3) (now + 1s, 20, 9.3)";
// insert rows
int affectedRows = jdbcTemplate.update(insertSql);
System.out.println("insert success " + affectedRows + " rows.");
// query for list
List<Map<String, Object>> resultList = jdbcTemplate.queryForList("select * from db.tb");
if(!CollectionUtils.isEmpty(resultList)){
for (Map<String, Object> row : resultList){
System.out.printf("%s, %d, %s\n", row.get("ts"), row.get("temperature"), row.get("humidity"));
}
}
}
}
package com.taosdata.jdbc.example.jdbcTemplate;
import com.taosdata.jdbc.example.jdbcTemplate.dao.ExecuteAsStatement;
import com.taosdata.jdbc.example.jdbcTemplate.dao.WeatherDao;
import com.taosdata.jdbc.example.jdbcTemplate.domain.Weather;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class App {
private static Random random = new Random(System.currentTimeMillis());
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ExecuteAsStatement executor = ctx.getBean(ExecuteAsStatement.class);
// drop database
executor.doExecute("drop database if exists test");
// create database
executor.doExecute("create database if not exists test");
//use database
executor.doExecute("use test");
// create table
executor.doExecute("create table if not exists test.weather (ts timestamp, temperature int, humidity float)");
WeatherDao weatherDao = ctx.getBean(WeatherDao.class);
Weather weather = new Weather(new Timestamp(new Date().getTime()), random.nextFloat() * 50.0f, random.nextInt(100));
// insert rows
int affectedRows = weatherDao.add(weather);
System.out.println("insert success " + affectedRows + " rows.");
// query for list
int limit = 10, offset = 0;
List<Weather> weatherList = weatherDao.queryForList(limit, offset);
for (Weather w : weatherList) {
System.out.println(w);
}
}
}
package com.taosdata.jdbc.example.jdbcTemplate.dao;
public interface ExecuteAsStatement{
void doExecute(String sql);
}
package com.taosdata.jdbc.example.jdbcTemplate.dao;
import com.taosdata.jdbc.example.jdbcTemplate.domain.Weather;
import java.util.List;
public interface WeatherDao {
int add(Weather weather);
int[] batchInsert(List<Weather> weatherList);
List<Weather> queryForList(int limit, int offset);
int count();
}
package com.taosdata.jdbc.example.jdbcTemplate.dao.impl;
import com.taosdata.jdbc.example.jdbcTemplate.dao.ExecuteAsStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class ExecuteAsStatementImpl implements ExecuteAsStatement {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void doExecute(String sql) {
jdbcTemplate.execute(sql);
}
}
package com.taosdata.jdbc.example.jdbcTemplate.dao.impl;
import com.taosdata.jdbc.example.jdbcTemplate.dao.WeatherDao;
import com.taosdata.jdbc.example.jdbcTemplate.domain.Weather;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class WeatherDaoImpl implements WeatherDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int add(Weather weather) {
return jdbcTemplate.update(
"insert into test.weather(ts, temperature, humidity) VALUES(?,?,?)",
weather.getTs(), weather.getTemperature(), weather.getHumidity()
);
}
@Override
public int[] batchInsert(List<Weather> weatherList) {
return jdbcTemplate.batchUpdate("insert into test.weather(ts, temperature, humidity) values( ?, ?, ?)", new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setTimestamp(1, weatherList.get(i).getTs());
ps.setFloat(2, weatherList.get(i).getTemperature());
ps.setInt(3, weatherList.get(i).getHumidity());
}
@Override
public int getBatchSize() {
return weatherList.size();
}
});
}
@Override
public List<Weather> queryForList(int limit, int offset) {
return jdbcTemplate.query("select * from test.weather limit ? offset ?", (rs, rowNum) -> {
Timestamp ts = rs.getTimestamp("ts");
float temperature = rs.getFloat("temperature");
int humidity = rs.getInt("humidity");
return new Weather(ts, temperature, humidity);
}, limit, offset);
}
@Override
public int count() {
return jdbcTemplate.queryForObject("select count(*) from test.weather", Integer.class);
}
}
package com.taosdata.jdbc.example.jdbcTemplate.domain;
import java.sql.Timestamp;
public class Weather {
private Timestamp ts;
private float temperature;
private int humidity;
public Weather() {
}
public Weather(Timestamp ts, float temperature, int humidity) {
this.ts = ts;
this.temperature = temperature;
this.humidity = humidity;
}
@Override
public String toString() {
return "Weather{" +
"ts=" + ts +
", temperature=" + temperature +
", humidity=" + humidity +
'}';
}
public Timestamp getTs() {
return ts;
}
public void setTs(Timestamp ts) {
this.ts = ts;
}
public float getTemperature() {
return temperature;
}
public void setTemperature(float temperature) {
this.temperature = temperature;
}
public int getHumidity() {
return humidity;
}
public void setHumidity(int humidity) {
this.humidity = humidity;
}
}
...@@ -5,20 +5,21 @@ ...@@ -5,20 +5,21 @@
xsi:schemaLocation=" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
"
default-autowire="byName"> default-autowire="byName">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.taosdata.jdbc.TSDBDriver"></property> <property name="driverClassName" value="com.taosdata.jdbc.TSDBDriver"></property>
<property name="url" value="jdbc:TAOS://127.0.0.1:6030/log"></property> <property name="url" value="jdbc:TAOS://192.168.236.137:6030/"></property>
<property name="username" value="root"></property> <property name="username" value="root"></property>
<property name="password" value="taosdata"></property> <property name="password" value="taosdata"></property>
</bean> </bean>
<bean id = "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" > <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref = "dataSource" ></property> <property name="dataSource" ref="dataSource"></property>
</bean> </bean>
<context:component-scan base-package="com.taosdata.jdbc.example.jdbcTemplate"/>
</beans> </beans>
...@@ -7,14 +7,12 @@ import org.junit.Test; ...@@ -7,14 +7,12 @@ import org.junit.Test;
/** /**
* Unit test for simple App. * Unit test for simple App.
*/ */
public class AppTest public class AppTest {
{
/** /**
* Rigorous Test :-) * Rigorous Test :-)
*/ */
@Test @Test
public void shouldAnswerWithTrue() public void shouldAnswerWithTrue() {
{ assertTrue(true);
assertTrue( true );
} }
} }
package com.taosdata.jdbc.example.jdbcTemplate;
import com.taosdata.jdbc.example.jdbcTemplate.dao.ExecuteAsStatement;
import com.taosdata.jdbc.example.jdbcTemplate.dao.WeatherDao;
import com.taosdata.jdbc.example.jdbcTemplate.domain.Weather;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
public class BatcherInsertTest {
@Autowired
private WeatherDao weatherDao;
@Autowired
private ExecuteAsStatement executor;
private static final int numOfRecordsPerTable = 1000;
private static long ts = 1496732686000l;
private static Random random = new Random(System.currentTimeMillis());
@Before
public void before() {
// drop database
executor.doExecute("drop database if exists test");
// create database
executor.doExecute("create database if not exists test");
//use database
executor.doExecute("use test");
// create table
executor.doExecute("create table if not exists test.weather (ts timestamp, temperature int, humidity float)");
}
@Test
public void batchInsert() {
List<Weather> weatherList = new ArrayList<>();
for (int i = 0; i < numOfRecordsPerTable; i++) {
ts += 1000;
Weather weather = new Weather(new Timestamp(ts), random.nextFloat() * 50.0f, random.nextInt(100));
weatherList.add(weather);
}
long start = System.currentTimeMillis();
weatherDao.batchInsert(weatherList);
long end = System.currentTimeMillis();
System.out.println("batch insert(" + numOfRecordsPerTable + " rows) time cost ==========> " + (end - start) + " ms");
int count = weatherDao.count();
assertEquals(count, numOfRecordsPerTable);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册