Hotfix/td 3689 (#5754)
* [TD-3689]<fix>: fix setNull exception in JDBC * change * change
This commit is contained in:
parent
60e6729d7a
commit
f1f6a57245
|
@ -33,17 +33,9 @@ import java.util.regex.Pattern;
|
|||
public class TSDBPreparedStatement extends TSDBStatement implements PreparedStatement {
|
||||
|
||||
private String rawSql;
|
||||
private String sql;
|
||||
// private ArrayList<Object> parameters = new ArrayList<>();
|
||||
private Object[] parameters;
|
||||
private boolean isPrepared;
|
||||
|
||||
//start with insert or import and is case-insensitive
|
||||
private static Pattern savePattern = Pattern.compile("(?i)^\\s*(insert|import)");
|
||||
// is insert or import
|
||||
private boolean isSaved;
|
||||
|
||||
// private SavedPreparedStatement savedPreparedStatement;
|
||||
private volatile TSDBParameterMetaData parameterMetaData;
|
||||
|
||||
TSDBPreparedStatement(TSDBConnection connection, TSDBJNIConnector connecter, String sql) {
|
||||
|
@ -65,35 +57,11 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
private void init(String sql) {
|
||||
this.rawSql = sql;
|
||||
preprocessSql();
|
||||
// this.isSaved = isSavedSql(this.rawSql);
|
||||
// if (this.isSaved) {
|
||||
// try {
|
||||
// this.savedPreparedStatement = new SavedPreparedStatement(this.rawSql, this);
|
||||
// } catch (SQLException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* if the precompiled sql is insert or import
|
||||
*
|
||||
* @param sql
|
||||
* @return
|
||||
*/
|
||||
private boolean isSavedSql(String sql) {
|
||||
Matcher matcher = savePattern.matcher(sql);
|
||||
return matcher.find();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] executeBatch() throws SQLException {
|
||||
// if (isSaved) {
|
||||
// return this.savedPreparedStatement.executeBatch();
|
||||
// } else {
|
||||
return super.executeBatch();
|
||||
// }
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -157,23 +125,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
*
|
||||
* @return a string of the native sql statement for TSDB
|
||||
*/
|
||||
// private String getNativeSql(String rawSql) {
|
||||
// for (int i = 0; i < parameters.length; i++) {
|
||||
// Object para = parameters[i];
|
||||
// if (para != null) {
|
||||
// String paraStr = para.toString();
|
||||
// if (para instanceof Timestamp || para instanceof String) {
|
||||
// paraStr = "'" + paraStr + "'";
|
||||
// }
|
||||
// this.sql = this.sql.replaceFirst("[?]", paraStr);
|
||||
// } else {
|
||||
// this.sql = this.sql.replaceFirst("[?]", "NULL");
|
||||
// }
|
||||
// }
|
||||
// parameters = new Object[parameters.length];
|
||||
// return sql;
|
||||
// }
|
||||
|
||||
private String getNativeSql(String rawSql) throws SQLException {
|
||||
String sql = rawSql;
|
||||
for (int i = 0; i < parameters.length; ++i) {
|
||||
|
@ -201,108 +152,58 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
|
||||
@Override
|
||||
public ResultSet executeQuery() throws SQLException {
|
||||
// if (isSaved) {
|
||||
// this.savedPreparedStatement.executeBatchInternal();
|
||||
// return null;
|
||||
// } else {
|
||||
|
||||
if (!isPrepared)
|
||||
return executeQuery(this.rawSql);
|
||||
|
||||
final String sql = getNativeSql(this.rawSql);
|
||||
return executeQuery(sql);
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public int executeUpdate() throws SQLException {
|
||||
// if (isSaved) {
|
||||
// return this.savedPreparedStatement.executeBatchInternal();
|
||||
// } else {
|
||||
if (!isPrepared)
|
||||
return executeUpdate(this.rawSql);
|
||||
String sql = getNativeSql(this.rawSql);
|
||||
return executeUpdate(sql);
|
||||
// }
|
||||
}
|
||||
|
||||
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;
|
||||
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");
|
||||
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 {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
setObject(parameterIndex,x);
|
||||
setObject(parameterIndex, x);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
|
@ -315,17 +216,12 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
|
||||
@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 {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
|
||||
setObject(parameterIndex,x);
|
||||
setObject(parameterIndex, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -344,8 +240,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
|
||||
@Override
|
||||
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
setObject(parameterIndex, x);
|
||||
}
|
||||
|
||||
|
@ -360,7 +254,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
|
||||
}
|
||||
|
||||
|
@ -375,8 +268,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
public void clearParameters() throws SQLException {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
|
||||
// parameters.clear();
|
||||
parameters = new Object[parameters.length];
|
||||
}
|
||||
|
||||
|
@ -384,43 +275,29 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
|
||||
setObject(parameterIndex,x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setObject(int parameterIndex, Object x) throws SQLException {
|
||||
// if (isSaved) {
|
||||
// this.savedPreparedStatement.setParam(parameterIndex, x);
|
||||
// } else {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
if (parameterIndex < 1 && parameterIndex >= parameters.length)
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE);
|
||||
|
||||
parameters[parameterIndex - 1] = x;
|
||||
// parameters.add(x);
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() throws SQLException {
|
||||
// if (isSaved) {
|
||||
// int result = this.savedPreparedStatement.executeBatchInternal();
|
||||
// return result > 0;
|
||||
// } else {
|
||||
if (!isPrepared)
|
||||
return execute(this.rawSql);
|
||||
|
||||
final String sql = getNativeSql(this.rawSql);
|
||||
|
||||
return execute(sql);
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBatch() throws SQLException {
|
||||
// if (isSaved) {
|
||||
// this.savedPreparedStatement.addBatch();
|
||||
// } else {
|
||||
if (this.batchedArgs == null) {
|
||||
batchedArgs = new ArrayList<>();
|
||||
}
|
||||
|
@ -431,7 +308,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
String sql = this.getConnection().nativeSQL(this.rawSql);
|
||||
addBatch(sql);
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -475,7 +351,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
|
|||
public ResultSetMetaData getMetaData() throws SQLException {
|
||||
if (isClosed())
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
|
||||
// return this.getResultSet().getMetaData();
|
||||
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,51 @@ public class TSDBPreparedStatementTest {
|
|||
pstmt_insert.setNull(2, Types.INTEGER);
|
||||
int result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(3, Types.BIGINT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(4, Types.FLOAT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(5, Types.DOUBLE);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(6, Types.SMALLINT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(7, Types.TINYINT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(8, Types.BOOLEAN);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(9, Types.BINARY);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(10, Types.NCHAR);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(10, Types.OTHER);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -129,7 +174,7 @@ public class TSDBPreparedStatementTest {
|
|||
Assert.assertFalse(pstmt_insert.execute());
|
||||
}
|
||||
|
||||
class Person implements Serializable {
|
||||
class Person {
|
||||
String name;
|
||||
int age;
|
||||
boolean sex;
|
||||
|
|
|
@ -160,6 +160,7 @@ public class TSDBResultSetTest {
|
|||
@Test
|
||||
public void getTime() throws SQLException {
|
||||
Time f1 = rs.getTime("f1");
|
||||
Assert.assertNotNull(f1);
|
||||
Assert.assertEquals("00:00:00", f1.toString());
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ public class DriverAutoloadTest {
|
|||
final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
|
||||
Connection conn = DriverManager.getConnection(url, properties);
|
||||
Assert.assertNotNull(conn);
|
||||
conn.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -27,6 +28,7 @@ public class DriverAutoloadTest {
|
|||
final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
|
||||
Connection conn = DriverManager.getConnection(url, properties);
|
||||
Assert.assertNotNull(conn);
|
||||
conn.close();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -50,6 +50,51 @@ public class RestfulPreparedStatementTest {
|
|||
pstmt_insert.setNull(2, Types.INTEGER);
|
||||
int result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(3, Types.BIGINT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(4, Types.FLOAT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(5, Types.DOUBLE);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(6, Types.SMALLINT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(7, Types.TINYINT);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(8, Types.BOOLEAN);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(9, Types.BINARY);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(10, Types.NCHAR);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
|
||||
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
|
||||
pstmt_insert.setNull(10, Types.OTHER);
|
||||
result = pstmt_insert.executeUpdate();
|
||||
Assert.assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -129,7 +174,7 @@ public class RestfulPreparedStatementTest {
|
|||
Assert.assertFalse(pstmt_insert.execute());
|
||||
}
|
||||
|
||||
class Person implements Serializable {
|
||||
private class Person {
|
||||
String name;
|
||||
int age;
|
||||
boolean sex;
|
||||
|
|
|
@ -160,6 +160,7 @@ public class RestfulResultSetTest {
|
|||
@Test
|
||||
public void getTime() throws SQLException {
|
||||
Time f1 = rs.getTime("f1");
|
||||
Assert.assertNotNull(f1);
|
||||
Assert.assertEquals("00:00:00", f1.toString());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue