提交 a8133047 编写于 作者: Z zyyang

add TSDBPreparedStatement test cases

上级 d7dfde2d
...@@ -22,6 +22,7 @@ public class TSDBError { ...@@ -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_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_UPDATE, "not a valid sql for executeUpdate: (?)");
TSDBErrorMap.put(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE, "not a valid sql for execute: (?)"); 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"); TSDBErrorMap.put(TSDBErrorNumbers.ERROR_UNKNOWN, "unknown error");
......
...@@ -16,6 +16,7 @@ public class TSDBErrorNumbers { ...@@ -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_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_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_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 public static final int ERROR_UNKNOWN = 0x2350; //unknown error
...@@ -45,6 +46,7 @@ public class TSDBErrorNumbers { ...@@ -45,6 +46,7 @@ public class TSDBErrorNumbers {
errorNumbers.add(ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE); errorNumbers.add(ERROR_DATABASE_NOT_SPECIFIED_OR_AVAILABLE);
errorNumbers.add(ERROR_INVALID_FOR_EXECUTE_UPDATE); errorNumbers.add(ERROR_INVALID_FOR_EXECUTE_UPDATE);
errorNumbers.add(ERROR_INVALID_FOR_EXECUTE); errorNumbers.add(ERROR_INVALID_FOR_EXECUTE);
errorNumbers.add(ERROR_PARAMETER_INDEX_OUT_BOUNDARY);
/*****************************************************/ /*****************************************************/
errorNumbers.add(ERROR_SUBSCRIBE_FAILED); errorNumbers.add(ERROR_SUBSCRIBE_FAILED);
......
...@@ -32,7 +32,7 @@ import java.util.regex.Pattern; ...@@ -32,7 +32,7 @@ import java.util.regex.Pattern;
public class TSDBPreparedStatement extends TSDBStatement implements PreparedStatement { public class TSDBPreparedStatement extends TSDBStatement implements PreparedStatement {
protected String rawSql; protected String rawSql;
protected String sql; protected String sql;
protected ArrayList<Object> parameters = new ArrayList<Object>(); protected ArrayList<Object> parameters = new ArrayList<>();
//start with insert or import and is case-insensitive //start with insert or import and is case-insensitive
private static Pattern savePattern = Pattern.compile("(?i)^\\s*(insert|import)"); private static Pattern savePattern = Pattern.compile("(?i)^\\s*(insert|import)");
...@@ -53,7 +53,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -53,7 +53,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
this.isSaved = isSavedSql(this.rawSql); this.isSaved = isSavedSql(this.rawSql);
if (this.isSaved) { if (this.isSaved) {
try { try {
this.savedPreparedStatement = new SavedPreparedStatement(this.rawSql, this); this.savedPreparedStatement = new SavedPreparedStatement(this.rawSql, this);
} catch (SQLException e) { } catch (SQLException e) {
...@@ -90,10 +89,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -90,10 +89,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
this.parameters = parameters; 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 * 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 * 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 ...@@ -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 @Override
public void setNull(int parameterIndex, int sqlType) throws SQLException { 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"); setObject(parameterIndex, "NULL");
} }
@Override @Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException { public void setBoolean(int parameterIndex, boolean x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setByte(int parameterIndex, byte x) throws SQLException { 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 @Override
public void setShort(int parameterIndex, short x) throws SQLException { public void setShort(int parameterIndex, short x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setInt(int parameterIndex, int x) throws SQLException { public void setInt(int parameterIndex, int x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setLong(int parameterIndex, long x) throws SQLException { public void setLong(int parameterIndex, long x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setFloat(int parameterIndex, float x) throws SQLException { public void setFloat(int parameterIndex, float x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setDouble(int parameterIndex, double x) throws SQLException { public void setDouble(int parameterIndex, double x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { 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 @Override
public void setString(int parameterIndex, String x) throws SQLException { public void setString(int parameterIndex, String x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setBytes(int parameterIndex, byte[] x) throws SQLException { 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 @Override
public void setDate(int parameterIndex, Date x) throws SQLException { 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 @Override
public void setTime(int parameterIndex, Time x) throws SQLException { 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 @Override
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
setObject(parameterIndex, x); setObject(parameterIndex, x);
} }
@Override @Override
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { 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 @Override
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { 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 @Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { 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 @Override
public void clearParameters() throws SQLException { public void clearParameters() throws SQLException {
if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
parameters.clear(); parameters.clear();
} }
@Override @Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { 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 @Override
...@@ -311,9 +398,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -311,9 +398,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
if (isSaved) { if (isSaved) {
this.savedPreparedStatement.addBatch(); this.savedPreparedStatement.addBatch();
} else { } else {
if (this.batchedArgs == null) { if (this.batchedArgs == null) {
batchedArgs = new ArrayList<String>(); batchedArgs = new ArrayList<>();
} }
super.addBatch(getNativeSql()); super.addBatch(getNativeSql());
} }
...@@ -321,156 +407,225 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -321,156 +407,225 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override @Override
public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { 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 @Override
public void setRef(int parameterIndex, Ref x) throws SQLException { 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 @Override
public void setBlob(int parameterIndex, Blob x) throws SQLException { 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 @Override
public void setClob(int parameterIndex, Clob x) throws SQLException { 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 @Override
public void setArray(int parameterIndex, Array x) throws SQLException { 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 @Override
public ResultSetMetaData getMetaData() throws SQLException { 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 @Override
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { 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 @Override
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { 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 @Override
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { 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 @Override
public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { 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 @Override
public void setURL(int parameterIndex, URL x) throws SQLException { 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 @Override
public ParameterMetaData getParameterMetaData() throws SQLException { 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 @Override
public void setRowId(int parameterIndex, RowId x) throws SQLException { 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 @Override
public void setNString(int parameterIndex, String value) throws SQLException { 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 @Override
public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { 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 @Override
public void setNClob(int parameterIndex, NClob value) throws SQLException { 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 @Override
public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { 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 @Override
public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { 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 @Override
public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { 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 @Override
public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { 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 @Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { 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 @Override
public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { 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 @Override
public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { 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 @Override
public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException { 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 @Override
public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { 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 @Override
public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { 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 @Override
public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { 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 @Override
public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { 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 @Override
public void setClob(int parameterIndex, Reader reader) throws SQLException { 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 @Override
public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { 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 @Override
public void setNClob(int parameterIndex, Reader reader) throws SQLException { 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);
} }
} }
package com.taosdata.jdbc; package com.taosdata.jdbc;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.sql.Connection; import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TSDBPreparedStatementTest { public class TSDBPreparedStatementTest {
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private static Connection conn; private static Connection conn;
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 @Test
public void executeQuery() { 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 @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 @Test
public void setNull() { public void setNull() throws SQLException {
pstmt_insert.setNull(2, Types.FLOAT);
} }
@Test @Test
public void setBoolean() { public void setBoolean() throws SQLException {
pstmt_insert.setBoolean(2, true);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setByte() { public void setByte() throws SQLException {
pstmt_insert.setByte(1, (byte) 0x001);
} }
@Test @Test
...@@ -54,120 +75,150 @@ public class TSDBPreparedStatementTest { ...@@ -54,120 +75,150 @@ public class TSDBPreparedStatementTest {
public void setDouble() { public void setDouble() {
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setBigDecimal() { public void setBigDecimal() throws SQLException {
pstmt_insert.setBigDecimal(1, null);
} }
@Test @Test
public void setString() { public void setString() {
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setBytes() { public void setBytes() throws SQLException {
pstmt_insert.setBytes(1, new byte[]{});
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setDate() { public void setDate() throws SQLException {
pstmt_insert.setDate(1, new Date(System.currentTimeMillis()));
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setTime() { public void setTime() throws SQLException {
pstmt_insert.setTime(1, new Time(System.currentTimeMillis()));
} }
@Test @Test
public void setTimestamp() { public void setTimestamp() {
//TODO
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setAsciiStream() { public void setAsciiStream() throws SQLException {
pstmt_insert.setAsciiStream(1, null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setUnicodeStream() { public void setBinaryStream() throws SQLException {
} pstmt_insert.setBinaryStream(1, null);
@Test
public void setBinaryStream() {
} }
@Test @Test
public void clearParameters() { public void clearParameters() {
//TODO
} }
@Test @Test
public void setObject() { public void setObject() throws SQLException {
pstmt_insert.setObject(1, System.currentTimeMillis());
//TODO
} }
@Test @Test
public void execute() { public void execute() {
//TODO
} }
@Test @Test
public void addBatch() { public void addBatch() {
//TODO:
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setCharacterStream() { public void setCharacterStream() throws SQLException {
pstmt_insert.setCharacterStream(1, null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setRef() { public void setRef() throws SQLException {
pstmt_insert.setRef(1, null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setBlob() { public void setBlob() throws SQLException {
pstmt_insert.setBlob(1, (Blob) null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setClob() { public void setClob() throws SQLException {
pstmt_insert.setClob(1, (Clob) null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setArray() { public void setArray() throws SQLException {
pstmt_insert.setArray(1, null);
} }
@Test @Test
public void getMetaData() { public void getMetaData() throws SQLException {
ResultSetMetaData metaData = pstmt_insert.getMetaData();
Assert.assertNotNull(metaData);
//TODO
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setURL() { public void setURL() throws SQLException {
pstmt_insert.setURL(1, null);
} }
@Test @Test
public void getParameterMetaData() { public void getParameterMetaData() throws SQLException {
ParameterMetaData parameterMetaData = pstmt_insert.getParameterMetaData();
Assert.assertNotNull(parameterMetaData);
//TODO:
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setRowId() { public void setRowId() throws SQLException {
pstmt_insert.setRowId(1, null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setNString() { public void setNString() throws SQLException {
pstmt_insert.setNString(1, null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setNCharacterStream() { public void setNCharacterStream() throws SQLException {
pstmt_insert.setNCharacterStream(1, null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setNClob() { public void setNClob() throws SQLException {
pstmt_insert.setNClob(1, (NClob) null);
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void setSQLXML() { public void setSQLXML() throws SQLException {
pstmt_insert.setSQLXML(1, null);
} }
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
try { 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"); 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) { } catch (ClassNotFoundException | SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -176,6 +227,7 @@ public class TSDBPreparedStatementTest { ...@@ -176,6 +227,7 @@ public class TSDBPreparedStatementTest {
@AfterClass @AfterClass
public static void afterClass() { public static void afterClass() {
try { try {
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册