From a8133047bc3f599dbeb50db253796ee7f4700b36 Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 24 Feb 2021 15:21:55 +0800 Subject: [PATCH] add TSDBPreparedStatement test cases --- .../java/com/taosdata/jdbc/TSDBError.java | 1 + .../com/taosdata/jdbc/TSDBErrorNumbers.java | 2 + .../taosdata/jdbc/TSDBPreparedStatement.java | 251 ++++++++++++++---- .../jdbc/TSDBPreparedStatementTest.java | 170 ++++++++---- 4 files changed, 317 insertions(+), 107 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java index ce1fcaae5a..db57d542ba 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBError.java @@ -22,6 +22,7 @@ public class TSDBError { TSDBErrorMap.put(TSDBErrorNumbers.ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE, "Database not specified or available"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE_UPDATE, "not a valid sql for executeUpdate: (?)"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE, "not a valid sql for execute: (?)"); + TSDBErrorMap.put(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_BOUNDARY, "parameter index out of range"); /**************************************************/ TSDBErrorMap.put(TSDBErrorNumbers.ERROR_UNKNOWN, "unknown error"); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java index 9a4effb8ee..f35af2353a 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java @@ -16,6 +16,7 @@ public class TSDBErrorNumbers { public static final int ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE = 0x2310; //Database not specified or available public static final int ERROR_INVALID_FOR_EXECUTE_UPDATE = 0x2311; //not a valid sql for executeUpdate: (SQL) public static final int ERROR_INVALID_FOR_EXECUTE = 0x2312; //not a valid sql for execute: (SQL) + public static final int ERROR_PARAMETER_INDEX_OUT_BOUNDARY = 0x2313; // parameter index out of range public static final int ERROR_UNKNOWN = 0x2350; //unknown error @@ -45,6 +46,7 @@ public class TSDBErrorNumbers { errorNumbers.add(ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE); errorNumbers.add(ERROR_INVALID_FOR_EXECUTE_UPDATE); errorNumbers.add(ERROR_INVALID_FOR_EXECUTE); + errorNumbers.add(ERROR_PARAMETER_INDEX_OUT_BOUNDARY); /*****************************************************/ errorNumbers.add(ERROR_SUBSCRIBE_FAILED); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java index decf14434e..b1e7701ce3 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java @@ -32,7 +32,7 @@ import java.util.regex.Pattern; public class TSDBPreparedStatement extends TSDBStatement implements PreparedStatement { protected String rawSql; protected String sql; - protected ArrayList parameters = new ArrayList(); + protected ArrayList parameters = new ArrayList<>(); //start with insert or import and is case-insensitive private static Pattern savePattern = Pattern.compile("(?i)^\\s*(insert|import)"); @@ -53,7 +53,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat this.isSaved = isSavedSql(this.rawSql); if (this.isSaved) { - try { this.savedPreparedStatement = new SavedPreparedStatement(this.rawSql, this); } catch (SQLException e) { @@ -90,10 +89,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat this.parameters = parameters; } - public String getRawSql() { - return rawSql; - } - /* * Some of the SQLs sent by other popular frameworks or tools like Spark, contains syntax that cannot be parsed by * the TDengine client. Thus, some simple parsers/filters are intentionally added in this JDBC implementation in @@ -192,99 +187,191 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat } } + private boolean isSupportedSQLType(int sqlType) { + switch (sqlType) { + case Types.TIMESTAMP: + case Types.INTEGER: + case Types.BIGINT: + case Types.FLOAT: + case Types.DOUBLE: + case Types.SMALLINT: + case Types.TINYINT: + case Types.BOOLEAN: + case Types.BINARY: + case Types.NCHAR: + return true; + case Types.ARRAY: + case Types.BIT: + case Types.BLOB: + case Types.CHAR: + case Types.CLOB: + case Types.DATALINK: + case Types.DATE: + case Types.DECIMAL: + case Types.DISTINCT: + case Types.JAVA_OBJECT: + case Types.LONGNVARCHAR: + case Types.LONGVARBINARY: + case Types.LONGVARCHAR: + case Types.NCLOB: + case Types.NULL: + case Types.NUMERIC: + case Types.NVARCHAR: + case Types.OTHER: + case Types.REAL: + case Types.REF: + case Types.REF_CURSOR: + case Types.ROWID: + case Types.SQLXML: + case Types.STRUCT: + case Types.TIME: + case Types.TIME_WITH_TIMEZONE: + case Types.TIMESTAMP_WITH_TIMEZONE: + case Types.VARBINARY: + case Types.VARCHAR: + default: + return false; + } + } + @Override public void setNull(int parameterIndex, int sqlType) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + if (!isSupportedSQLType(sqlType) || parameterIndex < 0) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); + if (parameterIndex >= parameters.size()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_BOUNDARY); + setObject(parameterIndex, "NULL"); } @Override public void setBoolean(int parameterIndex, boolean x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + setObject(parameterIndex, x); } @Override public void setByte(int parameterIndex, byte x) throws SQLException { - setObject(parameterIndex, x); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setShort(int parameterIndex, short x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); setObject(parameterIndex, x); } @Override public void setInt(int parameterIndex, int x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); setObject(parameterIndex, x); } @Override public void setLong(int parameterIndex, long x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); setObject(parameterIndex, x); } @Override public void setFloat(int parameterIndex, float x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); setObject(parameterIndex, x); } @Override public void setDouble(int parameterIndex, double x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); setObject(parameterIndex, x); } @Override public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { - setObject(parameterIndex, x); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setString(int parameterIndex, String x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); setObject(parameterIndex, x); } @Override public void setBytes(int parameterIndex, byte[] x) throws SQLException { - setObject(parameterIndex, x); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setDate(int parameterIndex, Date x) throws SQLException { - setObject(parameterIndex, x); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setTime(int parameterIndex, Time x) throws SQLException { - setObject(parameterIndex, x); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); setObject(parameterIndex, x); } @Override public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void clearParameters() throws SQLException { + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); parameters.clear(); } @Override public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override @@ -311,9 +398,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat if (isSaved) { this.savedPreparedStatement.addBatch(); } else { - if (this.batchedArgs == null) { - batchedArgs = new ArrayList(); + batchedArgs = new ArrayList<>(); } super.addBatch(getNativeSql()); } @@ -321,156 +407,225 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat @Override public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setRef(int parameterIndex, Ref x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setBlob(int parameterIndex, Blob x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setClob(int parameterIndex, Clob x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setArray(int parameterIndex, Array x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public ResultSetMetaData getMetaData() throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + return this.getResultSet().getMetaData(); } @Override public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + // TODOļ¼š + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setURL(int parameterIndex, URL x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public ParameterMetaData getParameterMetaData() throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + //TODO: + return null; +// throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setRowId(int parameterIndex, RowId x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setNString(int parameterIndex, String value) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setNClob(int parameterIndex, NClob value) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + //TODO: + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setClob(int parameterIndex, Reader reader) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + } @Override public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @Override public void setNClob(int parameterIndex, Reader reader) throws SQLException { - throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); + if (isClosed()) + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java index ebacf6af6b..583e7e639a 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java @@ -1,37 +1,58 @@ package com.taosdata.jdbc; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; +import java.sql.*; public class TSDBPreparedStatementTest { private static final String host = "127.0.0.1"; private static Connection conn; - - @Test - public void executeQuery() { - + private static final String sql_insert = "insert into t1 values(?, ?)"; + private static PreparedStatement pstmt_insert; + private static final String sql_select = "select * from t1 where ts > ? and ts <= ? and temperature >= ?"; + private static PreparedStatement pstmt_select; + + @Test + public void executeQuery() throws SQLException { + pstmt_select.setString(1, "now - 1h"); + pstmt_select.setString(2, "now"); + pstmt_select.setFloat(3, 0); + + ResultSet rs = pstmt_select.executeQuery(); + Assert.assertNotNull(rs); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + for (int i = 1; i <= meta.getColumnCount(); i++) { + System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t"); + } + System.out.println(); + } } @Test - public void executeUpdate() { - + public void executeUpdate() throws SQLException { + pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + pstmt_insert.setFloat(2, 3.14f); + int result = pstmt_insert.executeUpdate(); + Assert.assertEquals(1, result); } @Test - public void setNull() { + public void setNull() throws SQLException { + pstmt_insert.setNull(2, Types.FLOAT); } @Test - public void setBoolean() { + public void setBoolean() throws SQLException { + pstmt_insert.setBoolean(2, true); } - @Test - public void setByte() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setByte() throws SQLException { + pstmt_insert.setByte(1, (byte) 0x001); } @Test @@ -54,120 +75,150 @@ public class TSDBPreparedStatementTest { public void setDouble() { } - @Test - public void setBigDecimal() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setBigDecimal() throws SQLException { + pstmt_insert.setBigDecimal(1, null); } @Test public void setString() { } - @Test - public void setBytes() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setBytes() throws SQLException { + pstmt_insert.setBytes(1, new byte[]{}); } - @Test - public void setDate() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setDate() throws SQLException { + pstmt_insert.setDate(1, new Date(System.currentTimeMillis())); } - @Test - public void setTime() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setTime() throws SQLException { + pstmt_insert.setTime(1, new Time(System.currentTimeMillis())); } @Test public void setTimestamp() { + //TODO } - @Test - public void setAsciiStream() { - } - - @Test - public void setUnicodeStream() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setAsciiStream() throws SQLException { + pstmt_insert.setAsciiStream(1, null); } - @Test - public void setBinaryStream() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setBinaryStream() throws SQLException { + pstmt_insert.setBinaryStream(1, null); } @Test public void clearParameters() { + //TODO } @Test - public void setObject() { - + public void setObject() throws SQLException { + pstmt_insert.setObject(1, System.currentTimeMillis()); + //TODO } @Test public void execute() { + //TODO } @Test public void addBatch() { - + //TODO: } - @Test - public void setCharacterStream() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setCharacterStream() throws SQLException { + pstmt_insert.setCharacterStream(1, null); } - @Test - public void setRef() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setRef() throws SQLException { + pstmt_insert.setRef(1, null); } - @Test - public void setBlob() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setBlob() throws SQLException { + pstmt_insert.setBlob(1, (Blob) null); } - @Test - public void setClob() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setClob() throws SQLException { + pstmt_insert.setClob(1, (Clob) null); } - @Test - public void setArray() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setArray() throws SQLException { + pstmt_insert.setArray(1, null); } @Test - public void getMetaData() { + public void getMetaData() throws SQLException { + ResultSetMetaData metaData = pstmt_insert.getMetaData(); + Assert.assertNotNull(metaData); + //TODO } - @Test - public void setURL() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setURL() throws SQLException { + pstmt_insert.setURL(1, null); } @Test - public void getParameterMetaData() { + public void getParameterMetaData() throws SQLException { + ParameterMetaData parameterMetaData = pstmt_insert.getParameterMetaData(); + Assert.assertNotNull(parameterMetaData); + //TODO: } - @Test - public void setRowId() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setRowId() throws SQLException { + pstmt_insert.setRowId(1, null); } - @Test - public void setNString() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setNString() throws SQLException { + pstmt_insert.setNString(1, null); } - @Test - public void setNCharacterStream() { + @Test(expected = SQLFeatureNotSupportedException.class) + public void setNCharacterStream() throws SQLException { + pstmt_insert.setNCharacterStream(1, null); } - @Test - public void setNClob() { - + @Test(expected = SQLFeatureNotSupportedException.class) + public void setNClob() throws SQLException { + pstmt_insert.setNClob(1, (NClob) null); } - @Test - public void setSQLXML() { - + @Test(expected = SQLFeatureNotSupportedException.class) + public void setSQLXML() throws SQLException { + pstmt_insert.setSQLXML(1, null); } @BeforeClass public static void beforeClass() { try { - Class.forName("com.taosdata.jdbc.rs.RestfulDriver"); + Class.forName("com.taosdata.jdbc.TSDBDriver"); conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata"); + try (Statement stmt = conn.createStatement()) { + stmt.execute("drop database if exists test_pstmt"); + stmt.execute("create database if not exists test_pstmt"); + stmt.execute("use test_pstmt"); + stmt.execute("create table weather(ts timestamp, temperature float) tags(loc nchar(64))"); + stmt.execute("create table t1 using weather tags('beijing')"); + } + pstmt_insert = conn.prepareStatement(sql_insert); + pstmt_select = conn.prepareStatement(sql_select); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } @@ -176,6 +227,7 @@ public class TSDBPreparedStatementTest { @AfterClass public static void afterClass() { try { + if (conn != null) conn.close(); } catch (SQLException e) { -- GitLab