From 293524b4b9f4fe83f16974170ea102c6fb2b8ddf Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 25 Feb 2021 09:39:55 +0800 Subject: [PATCH] change --- .../com/taosdata/jdbc/AbstractStatement.java | 38 +++++++++++++++-- .../java/com/taosdata/jdbc/TSDBStatement.java | 42 ++----------------- .../taosdata/jdbc/rs/RestfulStatement.java | 18 -------- .../jdbc/rs/RestfulStatementTest.java | 4 +- 4 files changed, 40 insertions(+), 62 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java index e4eb03001e..8b6c074d1b 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java @@ -1,9 +1,12 @@ package com.taosdata.jdbc; import java.sql.*; +import java.util.ArrayList; +import java.util.List; public abstract class AbstractStatement extends WrapperImpl implements Statement { + protected List batchedArgs; private int fetchSize; @Override @@ -168,13 +171,42 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement } @Override - public abstract void addBatch(String sql) throws SQLException; + public void addBatch(String sql) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + if (batchedArgs == null) { + batchedArgs = new ArrayList<>(); + } + batchedArgs.add(sql); + } @Override - public abstract void clearBatch() throws SQLException; + public void clearBatch() throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + if (batchedArgs != null) + batchedArgs.clear(); + } @Override - public abstract int[] executeBatch() throws SQLException; + public int[] executeBatch() throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + if (batchedArgs == null || batchedArgs.isEmpty()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_BATCH_IS_EMPTY); + + int[] res = new int[batchedArgs.size()]; + for (int i = 0; i < batchedArgs.size(); i++) { + boolean isSelect = execute(batchedArgs.get(i)); + if (isSelect) { + res[i] = SUCCESS_NO_INFO; + } else { + res[i] = getUpdateCount(); + } + } + return res; + } @Override public abstract Connection getConnection() throws SQLException; 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 c4c1904629..c847b151d4 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 @@ -14,9 +14,9 @@ *****************************************************************************/ package com.taosdata.jdbc; -import java.sql.*; -import java.util.ArrayList; -import java.util.List; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; public class TSDBStatement extends AbstractStatement { @@ -24,7 +24,6 @@ public class TSDBStatement extends AbstractStatement { /** * To store batched commands */ - protected List batchedArgs; /** * Status of current statement */ @@ -119,41 +118,6 @@ public class TSDBStatement extends AbstractStatement { return this.affectedRows; } - public void addBatch(String sql) throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - - if (batchedArgs == null) { - batchedArgs = new ArrayList<>(); - } - batchedArgs.add(sql); - } - - public void clearBatch() throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - if (batchedArgs != null) - batchedArgs.clear(); - } - - public int[] executeBatch() throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - if (batchedArgs == null || batchedArgs.isEmpty()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_BATCH_IS_EMPTY); - - int[] res = new int[batchedArgs.size()]; - for (int i = 0; i < batchedArgs.size(); i++) { - boolean isSelect = execute(batchedArgs.get(i)); - if (isSelect) { - res[i] = SUCCESS_NO_INFO; - } else { - res[i] = getUpdateCount(); - } - } - return res; - } - public Connection getConnection() throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java index a49bdae7d4..f0c24375e6 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java @@ -213,24 +213,6 @@ public class RestfulStatement extends AbstractStatement { return this.affectedRows; } - @Override - public void addBatch(String sql) throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - //TODO: - } - - @Override - public void clearBatch() throws SQLException { - //TODO: - } - - @Override - public int[] executeBatch() throws SQLException { - //TODO: - return new int[0]; - } - @Override public Connection getConnection() throws SQLException { if (isClosed()) diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulStatementTest.java index 84c8421d09..31ec1f57e1 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulStatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulStatementTest.java @@ -11,8 +11,8 @@ import java.util.Properties; import java.util.UUID; public class RestfulStatementTest { - private static final String host = "127.0.0.1"; - // private static final String host = "master"; +// private static final String host = "127.0.0.1"; + private static final String host = "master"; private static Connection conn; private static Statement stmt; -- GitLab