未验证 提交 72ca4518 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #6424 from taosdata/fix/jdbc

sync jdbc driver from develop to master
...@@ -12,6 +12,9 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -12,6 +12,9 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
private final static int DRIVER_MAJAR_VERSION = 2; private final static int DRIVER_MAJAR_VERSION = 2;
private final static int DRIVER_MINOR_VERSION = 0; private final static int DRIVER_MINOR_VERSION = 0;
private String precision = "ms";
private String database;
public boolean allProceduresAreCallable() throws SQLException { public boolean allProceduresAreCallable() throws SQLException {
return false; return false;
} }
...@@ -493,102 +496,105 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -493,102 +496,105 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
public abstract ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException; public abstract ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException;
protected ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types, Connection connection) throws SQLException { private List<ColumnMetaData> buildGetTablesColumnMetaDataList() {
try (Statement stmt = connection.createStatement()) {
if (catalog == null || catalog.isEmpty())
return null;
ResultSet databases = stmt.executeQuery("show databases");
String dbname = null;
while (databases.next()) {
dbname = databases.getString("name");
if (dbname.equalsIgnoreCase(catalog))
break;
}
databases.close();
if (dbname == null)
return null;
stmt.execute("use " + dbname);
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
ColumnMetaData col1 = new ColumnMetaData(); columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT
col1.setColIndex(1); columnMetaDataList.add(buildTableSchemaMeta(2)); // 2. TABLE_SCHEM
col1.setColName("TABLE_CAT"); columnMetaDataList.add(buildTableNameMeta(3)); // 3. TABLE_NAME
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); columnMetaDataList.add(buildTableTypeMeta(4)); // 4. TABLE_TYPE
columnMetaDataList.add(col1); columnMetaDataList.add(buildRemarksMeta(5)); // 5. remarks
ColumnMetaData col2 = new ColumnMetaData(); columnMetaDataList.add(buildTypeCatMeta(6)); // 6. TYPE_CAT
col2.setColIndex(2); columnMetaDataList.add(buildTypeSchemaMeta(7)); // 7. TYPE_SCHEM
col2.setColName("TABLE_SCHEM"); columnMetaDataList.add(buildTypeNameMeta(8)); // 8. TYPE_NAME
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); columnMetaDataList.add(buildSelfReferencingColName(9)); // 9. SELF_REFERENCING_COL_NAME
columnMetaDataList.add(col2); columnMetaDataList.add(buildRefGenerationMeta(10)); // 10. REF_GENERATION
ColumnMetaData col3 = new ColumnMetaData(); return columnMetaDataList;
col3.setColIndex(3); }
col3.setColName("TABLE_NAME");
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); private ColumnMetaData buildTypeCatMeta(int colIndex) {
columnMetaDataList.add(col3);
ColumnMetaData col4 = new ColumnMetaData();
col4.setColIndex(4);
col4.setColName("TABLE_TYPE");
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col4);
ColumnMetaData col5 = new ColumnMetaData();
col5.setColIndex(5);
col5.setColName("REMARKS");
col5.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col5);
ColumnMetaData col6 = new ColumnMetaData(); ColumnMetaData col6 = new ColumnMetaData();
col6.setColIndex(6); col6.setColIndex(colIndex);
col6.setColName("TYPE_CAT"); col6.setColName("TYPE_CAT");
col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col6.setColType(Types.NCHAR);
columnMetaDataList.add(col6); return col6;
}
private ColumnMetaData buildTypeSchemaMeta(int colIndex) {
ColumnMetaData col7 = new ColumnMetaData(); ColumnMetaData col7 = new ColumnMetaData();
col7.setColIndex(7); col7.setColIndex(colIndex);
col7.setColName("TYPE_SCHEM"); col7.setColName("TYPE_SCHEM");
col7.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col7.setColType(Types.NCHAR);
columnMetaDataList.add(col7); return col7;
}
private ColumnMetaData buildTypeNameMeta(int colIndex) {
ColumnMetaData col8 = new ColumnMetaData(); ColumnMetaData col8 = new ColumnMetaData();
col8.setColIndex(8); col8.setColIndex(colIndex);
col8.setColName("TYPE_NAME"); col8.setColName("TYPE_NAME");
col8.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col8.setColType(Types.NCHAR);
columnMetaDataList.add(col8); return col8;
}
private ColumnMetaData buildSelfReferencingColName(int colIndex) {
ColumnMetaData col9 = new ColumnMetaData(); ColumnMetaData col9 = new ColumnMetaData();
col9.setColIndex(9); col9.setColIndex(colIndex);
col9.setColName("SELF_REFERENCING_COL_NAME"); col9.setColName("SELF_REFERENCING_COL_NAME");
col9.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col9.setColType(Types.NCHAR);
columnMetaDataList.add(col9); return col9;
}
private ColumnMetaData buildRefGenerationMeta(int colIndex) {
ColumnMetaData col10 = new ColumnMetaData(); ColumnMetaData col10 = new ColumnMetaData();
col10.setColIndex(10); col10.setColIndex(colIndex);
col10.setColName("REF_GENERATION"); col10.setColName("REF_GENERATION");
col10.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col10.setColType(Types.NCHAR);
columnMetaDataList.add(col10); return col10;
resultSet.setColumnMetaDataList(columnMetaDataList); }
protected ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types, Connection connection) throws SQLException {
if (catalog == null || catalog.isEmpty())
return null;
if (!isAvailableCatalog(connection, catalog))
return new EmptyResultSet();
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
// set column metadata list
resultSet.setColumnMetaDataList(buildGetTablesColumnMetaDataList());
// set row data
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
try (Statement stmt = connection.createStatement()) {
stmt.execute("use " + catalog);
ResultSet tables = stmt.executeQuery("show tables"); ResultSet tables = stmt.executeQuery("show tables");
while (tables.next()) { while (tables.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); TSDBResultSetRowData rowData = new TSDBResultSetRowData(10);
rowData.setString(0, dbname); //table_cat rowData.setStringValue(1, catalog); //TABLE_CAT
rowData.setString(1, null); //TABLE_SCHEM rowData.setStringValue(2, null); //TABLE_SCHEM
rowData.setString(2, tables.getString("table_name")); //TABLE_NAME rowData.setStringValue(3, tables.getString("table_name")); //TABLE_NAME
rowData.setString(3, "TABLE"); //TABLE_TYPE rowData.setStringValue(4, "TABLE"); //TABLE_TYPE
rowData.setString(4, ""); //REMARKS rowData.setStringValue(5, ""); //REMARKS
rowDataList.add(rowData); rowDataList.add(rowData);
} }
ResultSet stables = stmt.executeQuery("show stables"); ResultSet stables = stmt.executeQuery("show stables");
while (stables.next()) { while (stables.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); TSDBResultSetRowData rowData = new TSDBResultSetRowData(10);
rowData.setString(0, dbname); //TABLE_CAT rowData.setStringValue(1, catalog); //TABLE_CAT
rowData.setString(1, null); //TABLE_SCHEM rowData.setStringValue(2, null); //TABLE_SCHEM
rowData.setString(2, stables.getString("name")); //TABLE_NAME rowData.setStringValue(3, stables.getString("name")); //TABLE_NAME
rowData.setString(3, "TABLE"); //TABLE_TYPE rowData.setStringValue(4, "TABLE"); //TABLE_TYPE
rowData.setString(4, "STABLE"); //REMARKS rowData.setStringValue(5, "STABLE"); //REMARKS
rowDataList.add(rowData); rowDataList.add(rowData);
} }
resultSet.setRowDataList(rowDataList); resultSet.setRowDataList(rowDataList);
}
return resultSet; return resultSet;
} }
private ColumnMetaData buildTableTypeMeta(int colIndex) {
ColumnMetaData col4 = new ColumnMetaData();
col4.setColIndex(colIndex);
col4.setColName("TABLE_TYPE");
col4.setColType(Types.NCHAR);
return col4;
} }
public ResultSet getSchemas() throws SQLException { public ResultSet getSchemas() throws SQLException {
...@@ -597,25 +603,24 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -597,25 +603,24 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
public abstract ResultSet getCatalogs() throws SQLException; public abstract ResultSet getCatalogs() throws SQLException;
private List<ColumnMetaData> buildTableTypesColumnMetadataList() {
List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
columnMetaDataList.add(buildTableTypeMeta(1));
return columnMetaDataList;
}
public ResultSet getTableTypes() throws SQLException { public ResultSet getTableTypes() throws SQLException {
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
// set up ColumnMetaDataList // set up ColumnMetaDataList
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); resultSet.setColumnMetaDataList(buildTableTypesColumnMetadataList());
ColumnMetaData colMetaData = new ColumnMetaData();
colMetaData.setColIndex(0);
colMetaData.setColName("TABLE_TYPE");
colMetaData.setColSize(10);
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(colMetaData);
resultSet.setColumnMetaDataList(columnMetaDataList);
// set up rowDataList // set up rowDataList
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); TSDBResultSetRowData rowData = new TSDBResultSetRowData(1);
rowData.setString(0, "TABLE"); rowData.setStringValue(1, "TABLE");
rowDataList.add(rowData); rowDataList.add(rowData);
rowData = new TSDBResultSetRowData(1); rowData = new TSDBResultSetRowData(1);
rowData.setString(0, "STABLE"); rowData.setStringValue(1, "STABLE");
rowDataList.add(rowData); rowDataList.add(rowData);
resultSet.setRowDataList(rowDataList); resultSet.setRowDataList(rowDataList);
...@@ -625,278 +630,330 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -625,278 +630,330 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
public abstract ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException; public abstract ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException;
protected ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern, Connection conn) { protected ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern, Connection conn) {
try (Statement stmt = conn.createStatement()) {
if (catalog == null || catalog.isEmpty()) if (catalog == null || catalog.isEmpty())
return null; return null;
if (!isAvailableCatalog(conn, catalog))
return new EmptyResultSet();
ResultSet databases = stmt.executeQuery("show databases"); DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
String dbname = null; // set up ColumnMetaDataList
while (databases.next()) { resultSet.setColumnMetaDataList(buildGetColumnsColumnMetaDataList());
dbname = databases.getString("name"); // set up rowDataList
if (dbname.equalsIgnoreCase(catalog)) List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
break; try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("describe " + catalog + "." + tableNamePattern);
int rowIndex = 0;
while (rs.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(24);
// set TABLE_CAT
rowData.setStringValue(1, catalog);
// set TABLE_SCHEM
rowData.setStringValue(2, null);
// set TABLE_NAME
rowData.setStringValue(3, tableNamePattern);
// set COLUMN_NAME
rowData.setStringValue(4, rs.getString("Field"));
// set DATA_TYPE
String typeName = rs.getString("Type");
rowData.setIntValue(5, TSDBConstants.typeName2JdbcType(typeName));
// set TYPE_NAME
rowData.setStringValue(6, typeName);
// set COLUMN_SIZE
int length = rs.getInt("Length");
rowData.setIntValue(7, calculateColumnSize(typeName, precision, length));
// set BUFFER LENGTH
rowData.setStringValue(8, null);
// set DECIMAL_DIGITS
Integer decimalDigits = calculateDecimalDigits(typeName);
if (decimalDigits != null) {
rowData.setIntValue(9, decimalDigits);
} else {
rowData.setStringValue(9, null);
} }
databases.close(); // set NUM_PREC_RADIX
if (dbname == null) rowData.setIntValue(10, 10);
// set NULLABLE
rowData.setIntValue(11, isNullable(rowIndex, typeName));
// set REMARKS
String note = rs.getString("Note");
rowData.setStringValue(12, note.trim().isEmpty() ? null : note);
rowDataList.add(rowData);
rowIndex++;
}
resultSet.setRowDataList(rowDataList);
} catch (SQLException e) {
e.printStackTrace();
}
return resultSet;
}
private int isNullable(int index, String typeName) {
if (index == 0 && "TIMESTAMP".equals(typeName))
return DatabaseMetaData.columnNoNulls;
return DatabaseMetaData.columnNullable;
}
private Integer calculateColumnSize(String typeName, String precisionType, int length) {
switch (typeName) {
case "TIMESTAMP":
return precisionType.equals("ms") ? TSDBConstants.TIMESTAMP_MS_PRECISION : TSDBConstants.TIMESTAMP_US_PRECISION;
case "BOOL":
return TSDBConstants.BOOLEAN_PRECISION;
case "TINYINT":
return TSDBConstants.TINYINT_PRECISION;
case "SMALLINT":
return TSDBConstants.SMALLINT_PRECISION;
case "INT":
return TSDBConstants.INT_PRECISION;
case "BIGINT":
return TSDBConstants.BIGINT_PRECISION;
case "FLOAT":
return TSDBConstants.FLOAT_PRECISION;
case "DOUBLE":
return TSDBConstants.DOUBLE_PRECISION;
case "NCHAR":
case "BINARY":
return length;
default:
return null; return null;
}
}
stmt.execute("use " + dbname); private Integer calculateDecimalDigits(String typeName) {
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); switch (typeName) {
// set up ColumnMetaDataList case "TINYINT":
case "SMALLINT":
case "INT":
case "BIGINT":
return 0;
default:
return null;
}
}
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); private ColumnMetaData buildTableCatalogMeta(int colIndex) {
// TABLE_CAT
ColumnMetaData col1 = new ColumnMetaData(); ColumnMetaData col1 = new ColumnMetaData();
col1.setColIndex(1); col1.setColIndex(colIndex);
col1.setColName("TABLE_CAT"); col1.setColName("TABLE_CAT");
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col1.setColType(Types.NCHAR);
columnMetaDataList.add(col1); return col1;
// TABLE_SCHEM }
private ColumnMetaData buildTableSchemaMeta(int colIndex) {
ColumnMetaData col2 = new ColumnMetaData(); ColumnMetaData col2 = new ColumnMetaData();
col2.setColIndex(2); col2.setColIndex(colIndex);
col2.setColName("TABLE_SCHEM"); col2.setColName("TABLE_SCHEM");
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col2.setColType(Types.NCHAR);
columnMetaDataList.add(col2); return col2;
// TABLE_NAME }
private ColumnMetaData buildTableNameMeta(int colIndex) {
ColumnMetaData col3 = new ColumnMetaData(); ColumnMetaData col3 = new ColumnMetaData();
col3.setColIndex(3); col3.setColIndex(colIndex);
col3.setColName("TABLE_NAME"); col3.setColName("TABLE_NAME");
col3.setColSize(193); col3.setColSize(193);
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col3.setColType(Types.NCHAR);
columnMetaDataList.add(col3); return col3;
// COLUMN_NAME }
private ColumnMetaData buildColumnNameMeta(int colIndex) {
ColumnMetaData col4 = new ColumnMetaData(); ColumnMetaData col4 = new ColumnMetaData();
col4.setColIndex(4); col4.setColIndex(colIndex);
col4.setColName("COLUMN_NAME"); col4.setColName("COLUMN_NAME");
col4.setColSize(65); col4.setColSize(65);
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col4.setColType(Types.NCHAR);
columnMetaDataList.add(col4); return col4;
// DATA_TYPE }
private ColumnMetaData buildDataTypeMeta(int colIndex) {
ColumnMetaData col5 = new ColumnMetaData(); ColumnMetaData col5 = new ColumnMetaData();
col5.setColIndex(5); col5.setColIndex(colIndex);
col5.setColName("DATA_TYPE"); col5.setColName("DATA_TYPE");
col5.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col5.setColType(Types.INTEGER);
columnMetaDataList.add(col5); return col5;
// TYPE_NAME }
ColumnMetaData col6 = new ColumnMetaData();
col6.setColIndex(6); private ColumnMetaData buildColumnSizeMeta() {
col6.setColName("TYPE_NAME");
col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col6);
// COLUMN_SIZE
ColumnMetaData col7 = new ColumnMetaData(); ColumnMetaData col7 = new ColumnMetaData();
col7.setColIndex(7); col7.setColIndex(7);
col7.setColName("COLUMN_SIZE"); col7.setColName("COLUMN_SIZE");
col7.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col7.setColType(Types.INTEGER);
columnMetaDataList.add(col7); return col7;
// BUFFER_LENGTH, not used }
private ColumnMetaData buildBufferLengthMeta() {
ColumnMetaData col8 = new ColumnMetaData(); ColumnMetaData col8 = new ColumnMetaData();
col8.setColIndex(8); col8.setColIndex(8);
col8.setColName("BUFFER_LENGTH"); col8.setColName("BUFFER_LENGTH");
columnMetaDataList.add(col8); return col8;
// DECIMAL_DIGITS }
private ColumnMetaData buildDecimalDigitsMeta() {
ColumnMetaData col9 = new ColumnMetaData(); ColumnMetaData col9 = new ColumnMetaData();
col9.setColIndex(9); col9.setColIndex(9);
col9.setColName("DECIMAL_DIGITS"); col9.setColName("DECIMAL_DIGITS");
col9.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col9.setColType(Types.INTEGER);
columnMetaDataList.add(col9); return col9;
// add NUM_PREC_RADIX }
private ColumnMetaData buildNumPrecRadixMeta() {
ColumnMetaData col10 = new ColumnMetaData(); ColumnMetaData col10 = new ColumnMetaData();
col10.setColIndex(10); col10.setColIndex(10);
col10.setColName("NUM_PREC_RADIX"); col10.setColName("NUM_PREC_RADIX");
col10.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col10.setColType(Types.INTEGER);
columnMetaDataList.add(col10); return col10;
// NULLABLE }
private ColumnMetaData buildNullableMeta() {
ColumnMetaData col11 = new ColumnMetaData(); ColumnMetaData col11 = new ColumnMetaData();
col11.setColIndex(11); col11.setColIndex(11);
col11.setColName("NULLABLE"); col11.setColName("NULLABLE");
col11.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col11.setColType(Types.INTEGER);
columnMetaDataList.add(col11); return col11;
// REMARKS }
private ColumnMetaData buildRemarksMeta(int colIndex) {
ColumnMetaData col12 = new ColumnMetaData(); ColumnMetaData col12 = new ColumnMetaData();
col12.setColIndex(12); col12.setColIndex(colIndex);
col12.setColName("REMARKS"); col12.setColName("REMARKS");
col12.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col12.setColType(Types.NCHAR);
columnMetaDataList.add(col12); return col12;
// COLUMN_DEF }
private ColumnMetaData buildColumnDefMeta() {
ColumnMetaData col13 = new ColumnMetaData(); ColumnMetaData col13 = new ColumnMetaData();
col13.setColIndex(13); col13.setColIndex(13);
col13.setColName("COLUMN_DEF"); col13.setColName("COLUMN_DEF");
col13.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col13.setColType(Types.NCHAR);
columnMetaDataList.add(col13); return col13;
//SQL_DATA_TYPE }
private ColumnMetaData buildSqlDataTypeMeta() {
ColumnMetaData col14 = new ColumnMetaData(); ColumnMetaData col14 = new ColumnMetaData();
col14.setColIndex(14); col14.setColIndex(14);
col14.setColName("SQL_DATA_TYPE"); col14.setColName("SQL_DATA_TYPE");
col14.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col14.setColType(Types.INTEGER);
columnMetaDataList.add(col14); return col14;
//SQL_DATETIME_SUB }
private ColumnMetaData buildSqlDatetimeSubMeta() {
ColumnMetaData col15 = new ColumnMetaData(); ColumnMetaData col15 = new ColumnMetaData();
col15.setColIndex(15); col15.setColIndex(15);
col15.setColName("SQL_DATETIME_SUB"); col15.setColName("SQL_DATETIME_SUB");
col15.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col15.setColType(Types.INTEGER);
columnMetaDataList.add(col15); return col15;
//CHAR_OCTET_LENGTH }
private ColumnMetaData buildCharOctetLengthMeta() {
ColumnMetaData col16 = new ColumnMetaData(); ColumnMetaData col16 = new ColumnMetaData();
col16.setColIndex(16); col16.setColIndex(16);
col16.setColName("CHAR_OCTET_LENGTH"); col16.setColName("CHAR_OCTET_LENGTH");
col16.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col16.setColType(Types.INTEGER);
columnMetaDataList.add(col16); return col16;
//ORDINAL_POSITION }
private ColumnMetaData buildOrdinalPositionMeta() {
ColumnMetaData col17 = new ColumnMetaData(); ColumnMetaData col17 = new ColumnMetaData();
col17.setColIndex(17); col17.setColIndex(17);
col17.setColName("ORDINAL_POSITION"); col17.setColName("ORDINAL_POSITION");
col17.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col17.setColType(Types.INTEGER);
columnMetaDataList.add(col17); return col17;
// IS_NULLABLE }
private ColumnMetaData buildIsNullableMeta() {
ColumnMetaData col18 = new ColumnMetaData(); ColumnMetaData col18 = new ColumnMetaData();
col18.setColIndex(18); col18.setColIndex(18);
col18.setColName("IS_NULLABLE"); col18.setColName("IS_NULLABLE");
col18.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col18.setColType(Types.NCHAR);
columnMetaDataList.add(col18); return col18;
//SCOPE_CATALOG }
private ColumnMetaData buildScopeCatalogMeta() {
ColumnMetaData col19 = new ColumnMetaData(); ColumnMetaData col19 = new ColumnMetaData();
col19.setColIndex(19); col19.setColIndex(19);
col19.setColName("SCOPE_CATALOG"); col19.setColName("SCOPE_CATALOG");
col19.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col19.setColType(Types.NCHAR);
columnMetaDataList.add(col19); return col19;
//SCOPE_SCHEMA }
private ColumnMetaData buildScopeSchemaMeta() {
ColumnMetaData col20 = new ColumnMetaData(); ColumnMetaData col20 = new ColumnMetaData();
col20.setColIndex(20); col20.setColIndex(20);
col20.setColName("SCOPE_SCHEMA"); col20.setColName("SCOPE_SCHEMA");
col20.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col20.setColType(Types.NCHAR);
columnMetaDataList.add(col20); return col20;
//SCOPE_TABLE }
private ColumnMetaData buildScopeTableMeta() {
ColumnMetaData col21 = new ColumnMetaData(); ColumnMetaData col21 = new ColumnMetaData();
col21.setColIndex(21); col21.setColIndex(21);
col21.setColName("SCOPE_TABLE"); col21.setColName("SCOPE_TABLE");
col21.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col21.setColType(Types.NCHAR);
columnMetaDataList.add(col21); return col21;
//SOURCE_DATA_TYPE }
private ColumnMetaData buildSourceDataTypeMeta() {
ColumnMetaData col22 = new ColumnMetaData(); ColumnMetaData col22 = new ColumnMetaData();
col22.setColIndex(22); col22.setColIndex(22);
col22.setColName("SOURCE_DATA_TYPE"); col22.setColName("SOURCE_DATA_TYPE");
col22.setColType(TSDBConstants.TSDB_DATA_TYPE_SMALLINT); col22.setColType(TSDBConstants.TSDB_DATA_TYPE_SMALLINT);
columnMetaDataList.add(col22); return col22;
//IS_AUTOINCREMENT }
private ColumnMetaData buildIsAutoIncrementMeta() {
ColumnMetaData col23 = new ColumnMetaData(); ColumnMetaData col23 = new ColumnMetaData();
col23.setColIndex(23); col23.setColIndex(23);
col23.setColName("IS_AUTOINCREMENT"); col23.setColName("IS_AUTOINCREMENT");
col23.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col23.setColType(Types.NCHAR);
columnMetaDataList.add(col23); return col23;
//IS_GENERATEDCOLUMN }
private ColumnMetaData buildIsGeneratedColumnMeta() {
ColumnMetaData col24 = new ColumnMetaData(); ColumnMetaData col24 = new ColumnMetaData();
col24.setColIndex(24); col24.setColIndex(24);
col24.setColName("IS_GENERATEDCOLUMN"); col24.setColName("IS_GENERATEDCOLUMN");
col24.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); col24.setColType(Types.NCHAR);
columnMetaDataList.add(col24); return col24;
resultSet.setColumnMetaDataList(columnMetaDataList);
// set up rowDataList
ResultSet rs = stmt.executeQuery("describe " + dbname + "." + tableNamePattern);
List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
int index = 0;
while (rs.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(24);
// set TABLE_CAT
rowData.setString(0, dbname);
// set TABLE_NAME
rowData.setString(2, tableNamePattern);
// set COLUMN_NAME
rowData.setString(3, rs.getString("Field"));
// set DATA_TYPE
String typeName = rs.getString("Type");
rowData.setInt(4, getDataType(typeName));
// set TYPE_NAME
rowData.setString(5, typeName);
// set COLUMN_SIZE
int length = rs.getInt("Length");
rowData.setInt(6, getColumnSize(typeName, length));
// set DECIMAL_DIGITS
rowData.setInt(8, getDecimalDigits(typeName));
// set NUM_PREC_RADIX
rowData.setInt(9, 10);
// set NULLABLE
rowData.setInt(10, getNullable(index, typeName));
// set REMARKS
rowData.setString(11, rs.getString("Note"));
rowDataList.add(rowData);
index++;
} }
resultSet.setRowDataList(rowDataList);
return resultSet;
} catch (SQLException e) { private List<ColumnMetaData> buildGetColumnsColumnMetaDataList() {
e.printStackTrace(); List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
} columnMetaDataList.add(buildTableCatalogMeta(1)); //1. TABLE_CAT
return null; columnMetaDataList.add(buildTableSchemaMeta(2)); //2. TABLE_SCHEMA
} columnMetaDataList.add(buildTableNameMeta(3)); //3. TABLE_NAME
columnMetaDataList.add(buildColumnNameMeta(4)); //4. COLUMN_NAME
protected int getNullable(int index, String typeName) { columnMetaDataList.add(buildDataTypeMeta(5)); //5. DATA_TYPE
if (index == 0 && "TIMESTAMP".equals(typeName)) columnMetaDataList.add(buildTypeNameMeta(6)); //6. TYPE_NAME
return DatabaseMetaData.columnNoNulls; columnMetaDataList.add(buildColumnSizeMeta()); //7. COLUMN_SIZE
return DatabaseMetaData.columnNullable; columnMetaDataList.add(buildBufferLengthMeta()); //8. BUFFER_LENGTH, not used
} columnMetaDataList.add(buildDecimalDigitsMeta()); //9. DECIMAL_DIGITS
columnMetaDataList.add(buildNumPrecRadixMeta()); //10. NUM_PREC_RADIX
protected int getColumnSize(String typeName, int length) { columnMetaDataList.add(buildNullableMeta()); //11. NULLABLE
switch (typeName) { columnMetaDataList.add(buildRemarksMeta(12)); //12. REMARKS
case "TIMESTAMP": columnMetaDataList.add(buildColumnDefMeta()); //13. COLUMN_DEF
return 23; columnMetaDataList.add(buildSqlDataTypeMeta()); //14. SQL_DATA_TYPE
default: columnMetaDataList.add(buildSqlDatetimeSubMeta()); //15. SQL_DATETIME_SUB
return 0; columnMetaDataList.add(buildCharOctetLengthMeta()); //16. CHAR_OCTET_LENGTH
} columnMetaDataList.add(buildOrdinalPositionMeta()); //17. ORDINAL_POSITION
} columnMetaDataList.add(buildIsNullableMeta()); //18. IS_NULLABLE
columnMetaDataList.add(buildScopeCatalogMeta()); //19. SCOPE_CATALOG
protected int getDecimalDigits(String typeName) { columnMetaDataList.add(buildScopeSchemaMeta()); //20. SCOPE_SCHEMA
switch (typeName) { columnMetaDataList.add(buildScopeTableMeta()); //21. SCOPE_TABLE
case "FLOAT": columnMetaDataList.add(buildSourceDataTypeMeta()); //22. SOURCE_DATA_TYPE
return 5; columnMetaDataList.add(buildIsAutoIncrementMeta()); //23. IS_AUTOINCREMENT
case "DOUBLE": columnMetaDataList.add(buildIsGeneratedColumnMeta()); //24. IS_GENERATEDCOLUMN
return 9; return columnMetaDataList;
default: }
return 0;
} public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) throws SQLException {
}
protected int getDataType(String typeName) {
switch (typeName) {
case "TIMESTAMP":
return Types.TIMESTAMP;
case "INT":
return Types.INTEGER;
case "BIGINT":
return Types.BIGINT;
case "FLOAT":
return Types.FLOAT;
case "DOUBLE":
return Types.DOUBLE;
case "BINARY":
return Types.BINARY;
case "SMALLINT":
return Types.SMALLINT;
case "TINYINT":
return Types.TINYINT;
case "BOOL":
return Types.BOOLEAN;
case "NCHAR":
return Types.NCHAR;
default:
return Types.NULL;
}
}
public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern)
throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) throws SQLException {
throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
...@@ -914,8 +971,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -914,8 +971,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
return getEmptyResultSet(); return getEmptyResultSet();
} }
public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException {
String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
...@@ -923,8 +979,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -923,8 +979,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
return getEmptyResultSet(); return getEmptyResultSet();
} }
public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException {
throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
...@@ -976,8 +1031,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -976,8 +1031,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
return false; return false;
} }
public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException {
throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
...@@ -1005,8 +1059,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -1005,8 +1059,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
public abstract ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException; public abstract ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException;
public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) throws SQLException {
String attributeNamePattern) throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
...@@ -1069,18 +1122,15 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -1069,18 +1122,15 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
return getEmptyResultSet(); return getEmptyResultSet();
} }
public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) throws SQLException {
throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) throws SQLException {
String columnNamePattern) throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
String columnNamePattern) throws SQLException {
return getEmptyResultSet(); return getEmptyResultSet();
} }
...@@ -1093,164 +1143,142 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -1093,164 +1143,142 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
} }
protected ResultSet getCatalogs(Connection conn) throws SQLException { protected ResultSet getCatalogs(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
// set up ColumnMetaDataList // set up ColumnMetaDataList
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
// TABLE_CAT columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT
ColumnMetaData col1 = new ColumnMetaData();
col1.setColIndex(1);
col1.setColName("TABLE_CAT");
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col1);
resultSet.setColumnMetaDataList(columnMetaDataList); resultSet.setColumnMetaDataList(columnMetaDataList);
try (Statement stmt = conn.createStatement()) {
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
ResultSet rs = stmt.executeQuery("show databases"); ResultSet rs = stmt.executeQuery("show databases");
while (rs.next()) { while (rs.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); TSDBResultSetRowData rowData = new TSDBResultSetRowData(1);
rowData.setString(0, rs.getString("name")); rowData.setStringValue(1, rs.getString("name"));
rowDataList.add(rowData); rowDataList.add(rowData);
} }
resultSet.setRowDataList(rowDataList); resultSet.setRowDataList(rowDataList);
return resultSet;
} }
return resultSet;
} }
protected ResultSet getPrimaryKeys(String catalog, String schema, String table, Connection conn) throws SQLException { protected ResultSet getPrimaryKeys(String catalog, String schema, String table, Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
if (catalog == null || catalog.isEmpty()) if (catalog == null || catalog.isEmpty())
return null; return null;
if (!isAvailableCatalog(conn, catalog))
return new EmptyResultSet();
ResultSet databases = stmt.executeQuery("show databases");
String dbname = null;
while (databases.next()) {
dbname = databases.getString("name");
if (dbname.equalsIgnoreCase(catalog))
break;
}
databases.close();
if (dbname == null)
return null;
stmt.execute("use " + dbname);
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
try (Statement stmt = conn.createStatement()) {
// set up ColumnMetaDataList // set up ColumnMetaDataList
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); resultSet.setColumnMetaDataList(buildGetPrimaryKeysMetadataList());
// TABLE_CAT
ColumnMetaData col1 = new ColumnMetaData();
col1.setColIndex(0);
col1.setColName("TABLE_CAT");
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col1);
// TABLE_SCHEM
ColumnMetaData col2 = new ColumnMetaData();
col2.setColIndex(1);
col2.setColName("TABLE_SCHEM");
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col2);
// TABLE_NAME
ColumnMetaData col3 = new ColumnMetaData();
col3.setColIndex(2);
col3.setColName("TABLE_NAME");
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col3);
// COLUMN_NAME
ColumnMetaData col4 = new ColumnMetaData();
col4.setColIndex(3);
col4.setColName("COLUMN_NAME");
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col4);
// KEY_SEQ
ColumnMetaData col5 = new ColumnMetaData();
col5.setColIndex(4);
col5.setColName("KEY_SEQ");
col5.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(col5);
// PK_NAME
ColumnMetaData col6 = new ColumnMetaData();
col6.setColIndex(5);
col6.setColName("PK_NAME");
col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col6);
resultSet.setColumnMetaDataList(columnMetaDataList);
// set rowData // set rowData
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
ResultSet rs = stmt.executeQuery("describe " + dbname + "." + table); ResultSet rs = stmt.executeQuery("describe " + catalog + "." + table);
rs.next(); rs.next();
TSDBResultSetRowData rowData = new TSDBResultSetRowData(6); TSDBResultSetRowData rowData = new TSDBResultSetRowData(6);
rowData.setString(0, null); rowData.setStringValue(1, catalog);
rowData.setString(1, null); rowData.setStringValue(2, null);
rowData.setString(2, table); rowData.setStringValue(3, table);
String pkName = rs.getString(1); String primaryKey = rs.getString("Field");
rowData.setString(3, pkName); rowData.setStringValue(4, primaryKey);
rowData.setInt(4, 1); rowData.setShortValue(5, (short) 1);
rowData.setString(5, pkName); rowData.setStringValue(6, primaryKey);
rowDataList.add(rowData); rowDataList.add(rowData);
resultSet.setRowDataList(rowDataList); resultSet.setRowDataList(rowDataList);
}
return resultSet; return resultSet;
} }
private List<ColumnMetaData> buildGetPrimaryKeysMetadataList() {
List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT
columnMetaDataList.add(buildTableSchemaMeta(2)); // 2. TABLE_SCHEM
columnMetaDataList.add(buildTableNameMeta(3)); // 3. TABLE_NAME
columnMetaDataList.add(buildColumnNameMeta(4)); // 4. COLUMN_NAME
columnMetaDataList.add(buildKeySeqMeta(5)); // 5. KEY_SEQ
columnMetaDataList.add(buildPrimaryKeyNameMeta(6)); // 6. PK_NAME
return columnMetaDataList;
} }
protected ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern, Connection conn) throws SQLException { private ColumnMetaData buildKeySeqMeta(int colIndex) {
try (Statement stmt = conn.createStatement()) { ColumnMetaData col5 = new ColumnMetaData();
if (catalog == null || catalog.isEmpty()) col5.setColIndex(colIndex);
return null; col5.setColName("KEY_SEQ");
col5.setColType(Types.SMALLINT);
return col5;
}
private ColumnMetaData buildPrimaryKeyNameMeta(int colIndex) {
ColumnMetaData col6 = new ColumnMetaData();
col6.setColIndex(colIndex);
col6.setColName("PK_NAME");
col6.setColType(Types.NCHAR);
return col6;
}
private boolean isAvailableCatalog(Connection connection, String catalog) {
try (Statement stmt = connection.createStatement()) {
ResultSet databases = stmt.executeQuery("show databases"); ResultSet databases = stmt.executeQuery("show databases");
String dbname = null;
while (databases.next()) { while (databases.next()) {
dbname = databases.getString("name"); String dbname = databases.getString("name");
this.database = dbname;
this.precision = databases.getString("precision");
if (dbname.equalsIgnoreCase(catalog)) if (dbname.equalsIgnoreCase(catalog))
break; return true;
} }
databases.close(); databases.close();
if (dbname == null) } catch (SQLException e) {
e.printStackTrace();
}
return false;
}
protected ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern, Connection conn) throws SQLException {
if (catalog == null || catalog.isEmpty())
return null; return null;
stmt.execute("use " + dbname); if (!isAvailableCatalog(conn, catalog)) {
return new EmptyResultSet();
}
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
try (Statement stmt = conn.createStatement()) {
// set up ColumnMetaDataList // set up ColumnMetaDataList
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); resultSet.setColumnMetaDataList(buildGetSuperTablesColumnMetaDataList());
// TABLE_CAT // set result set row data
ColumnMetaData col1 = new ColumnMetaData(); stmt.execute("use " + catalog);
col1.setColIndex(0);
col1.setColName("TABLE_CAT");
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col1);
// TABLE_SCHEM
ColumnMetaData col2 = new ColumnMetaData();
col2.setColIndex(1);
col2.setColName("TABLE_SCHEM");
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col2);
// TABLE_NAME
ColumnMetaData col3 = new ColumnMetaData();
col3.setColIndex(2);
col3.setColName("TABLE_NAME");
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col3);
// SUPERTABLE_NAME
ColumnMetaData col4 = new ColumnMetaData();
col4.setColIndex(3);
col4.setColName("SUPERTABLE_NAME");
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col4);
resultSet.setColumnMetaDataList(columnMetaDataList);
ResultSet rs = stmt.executeQuery("show tables like '" + tableNamePattern + "'"); ResultSet rs = stmt.executeQuery("show tables like '" + tableNamePattern + "'");
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(4); TSDBResultSetRowData rowData = new TSDBResultSetRowData(4);
rowData.setString(2, rs.getString(1)); rowData.setStringValue(1, catalog);
rowData.setString(3, rs.getString(4)); rowData.setStringValue(2, null);
rowData.setStringValue(3, rs.getString("table_name"));
rowData.setStringValue(4, rs.getString("stable_name"));
rowDataList.add(rowData); rowDataList.add(rowData);
} }
resultSet.setRowDataList(rowDataList); resultSet.setRowDataList(rowDataList);
}
return resultSet; return resultSet;
} }
private List<ColumnMetaData> buildGetSuperTablesColumnMetaDataList() {
List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT
columnMetaDataList.add(buildTableSchemaMeta(2)); // 2. TABLE_SCHEM
columnMetaDataList.add(buildTableNameMeta(3)); // 3. TABLE_NAME
columnMetaDataList.add(buildSuperTableNameMeta(4)); // 4. SUPERTABLE_NAME
return columnMetaDataList;
} }
private ColumnMetaData buildSuperTableNameMeta(int colIndex) {
ColumnMetaData col4 = new ColumnMetaData();
col4.setColIndex(colIndex);
col4.setColName("SUPERTABLE_NAME");
col4.setColType(Types.NCHAR);
return col4;
}
} }
\ No newline at end of file
package com.taosdata.jdbc; package com.taosdata.jdbc;
import com.sun.org.apache.xpath.internal.operations.Bool;
import java.sql.ParameterMetaData; import java.sql.ParameterMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
...@@ -49,6 +51,22 @@ public abstract class AbstractParameterMetaData extends WrapperImpl implements P ...@@ -49,6 +51,22 @@ public abstract class AbstractParameterMetaData extends WrapperImpl implements P
if (param < 1 && param >= parameters.length) if (param < 1 && param >= parameters.length)
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE);
if (parameters[param - 1] instanceof Boolean)
return TSDBConstants.BOOLEAN_PRECISION;
if (parameters[param - 1] instanceof Byte)
return TSDBConstants.TINYINT_PRECISION;
if (parameters[param - 1] instanceof Short)
return TSDBConstants.SMALLINT_PRECISION;
if (parameters[param - 1] instanceof Integer)
return TSDBConstants.INT_PRECISION;
if (parameters[param - 1] instanceof Long)
return TSDBConstants.BIGINT_PRECISION;
if (parameters[param - 1] instanceof Timestamp)
return TSDBConstants.TIMESTAMP_MS_PRECISION;
if (parameters[param - 1] instanceof Float)
return TSDBConstants.FLOAT_PRECISION;
if (parameters[param - 1] instanceof Double)
return TSDBConstants.DOUBLE_PRECISION;
if (parameters[param - 1] instanceof String) if (parameters[param - 1] instanceof String)
return ((String) parameters[param - 1]).length(); return ((String) parameters[param - 1]).length();
if (parameters[param - 1] instanceof byte[]) if (parameters[param - 1] instanceof byte[])
...@@ -60,6 +78,11 @@ public abstract class AbstractParameterMetaData extends WrapperImpl implements P ...@@ -60,6 +78,11 @@ public abstract class AbstractParameterMetaData extends WrapperImpl implements P
public int getScale(int param) throws SQLException { public int getScale(int param) throws SQLException {
if (param < 1 && param >= parameters.length) if (param < 1 && param >= parameters.length)
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE);
if (parameters[param - 1] instanceof Float)
return TSDBConstants.FLOAT_SCALE;
if (parameters[param - 1] instanceof Double)
return TSDBConstants.DOUBLE_SCALE;
return 0; return 0;
} }
......
...@@ -66,10 +66,16 @@ public abstract class AbstractResultSet extends WrapperImpl implements ResultSet ...@@ -66,10 +66,16 @@ public abstract class AbstractResultSet extends WrapperImpl implements ResultSet
public abstract byte[] getBytes(int columnIndex) throws SQLException; public abstract byte[] getBytes(int columnIndex) throws SQLException;
@Override @Override
public abstract Date getDate(int columnIndex) throws SQLException; public Date getDate(int columnIndex) throws SQLException {
Timestamp timestamp = getTimestamp(columnIndex);
return timestamp == null ? null : new Date(timestamp.getTime());
}
@Override @Override
public abstract Time getTime(int columnIndex) throws SQLException; public Time getTime(int columnIndex) throws SQLException {
Timestamp timestamp = getTimestamp(columnIndex);
return timestamp == null ? null : new Time(timestamp.getTime());
}
@Override @Override
public abstract Timestamp getTimestamp(int columnIndex) throws SQLException; public abstract Timestamp getTimestamp(int columnIndex) throws SQLException;
......
...@@ -14,76 +14,42 @@ ...@@ -14,76 +14,42 @@
*****************************************************************************/ *****************************************************************************/
package com.taosdata.jdbc; package com.taosdata.jdbc;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL;
import java.sql.Date;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
/* /*
* TDengine only supports a subset of the standard SQL, thus this implemetation of the * TDengine only supports a subset of the standard SQL, thus this implementation of the
* standard JDBC API contains more or less some adjustments customized for certain * standard JDBC API contains more or less some adjustments customized for certain
* compatibility needs. * compatibility needs.
*/ */
public class DatabaseMetaDataResultSet implements ResultSet { public class DatabaseMetaDataResultSet extends AbstractResultSet {
private List<ColumnMetaData> columnMetaDataList; private List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
private List<TSDBResultSetRowData> rowDataList; private List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
private TSDBResultSetRowData rowCursor; private TSDBResultSetRowData rowCursor;
// position of cursor, starts from 0 as beforeFirst, increases as next() is called // position of cursor, starts from 0 as beforeFirst, increases as next() is called
private int cursorRowNumber = 0; private int cursorRowNumber = 0;
public DatabaseMetaDataResultSet() {
rowDataList = new ArrayList<TSDBResultSetRowData>();
columnMetaDataList = new ArrayList<ColumnMetaData>();
}
public List<TSDBResultSetRowData> getRowDataList() {
return rowDataList;
}
public void setRowDataList(List<TSDBResultSetRowData> rowDataList) { public void setRowDataList(List<TSDBResultSetRowData> rowDataList) {
this.rowDataList = rowDataList; this.rowDataList = rowDataList;
} }
public List<ColumnMetaData> getColumnMetaDataList() {
return columnMetaDataList;
}
public void setColumnMetaDataList(List<ColumnMetaData> columnMetaDataList) { public void setColumnMetaDataList(List<ColumnMetaData> columnMetaDataList) {
this.columnMetaDataList = columnMetaDataList; this.columnMetaDataList = columnMetaDataList;
} }
public TSDBResultSetRowData getRowCursor() {
return rowCursor;
}
public void setRowCursor(TSDBResultSetRowData rowCursor) {
this.rowCursor = rowCursor;
}
@Override @Override
public boolean next() throws SQLException { public boolean next() throws SQLException {
// boolean ret = false;
// if (rowDataList.size() > 0) {
// ret = rowDataList.iterator().hasNext();
// if (ret) {
// rowCursor = rowDataList.iterator().next();
// cursorRowNumber++;
// }
// }
// return ret;
/**** add by zyyang 2020-09-29 ****************/
boolean ret = false; boolean ret = false;
if (!rowDataList.isEmpty() && cursorRowNumber < rowDataList.size()) { if (!rowDataList.isEmpty() && cursorRowNumber < rowDataList.size()) {
rowCursor = rowDataList.get(cursorRowNumber++); rowCursor = rowDataList.get(cursorRowNumber++);
ret = true; ret = true;
} }
return ret; return ret;
} }
...@@ -99,189 +65,72 @@ public class DatabaseMetaDataResultSet implements ResultSet { ...@@ -99,189 +65,72 @@ public class DatabaseMetaDataResultSet implements ResultSet {
@Override @Override
public String getString(int columnIndex) throws SQLException { public String getString(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
int colType = columnMetaDataList.get(columnIndex).getColType(); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
return rowCursor.getString(columnIndex, colType); return rowCursor.getString(columnIndex, nativeType);
} }
@Override @Override
public boolean getBoolean(int columnIndex) throws SQLException { public boolean getBoolean(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return rowCursor.getBoolean(columnIndex, columnMetaDataList.get(columnIndex).getColType()); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
return rowCursor.getBoolean(columnIndex, nativeType);
} }
@Override @Override
public byte getByte(int columnIndex) throws SQLException { public byte getByte(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return (byte) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
return (byte) rowCursor.getInt(columnIndex, nativeType);
} }
@Override @Override
public short getShort(int columnIndex) throws SQLException { public short getShort(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return (short) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
return (short) rowCursor.getInt(columnIndex, nativeType);
} }
@Override @Override
public int getInt(int columnIndex) throws SQLException { public int getInt(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
return rowCursor.getInt(columnIndex, nativeType);
} }
@Override @Override
public long getLong(int columnIndex) throws SQLException { public long getLong(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return rowCursor.getLong(columnIndex, columnMetaDataList.get(columnIndex).getColType()); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
return rowCursor.getLong(columnIndex, nativeType);
} }
@Override @Override
public float getFloat(int columnIndex) throws SQLException { public float getFloat(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return rowCursor.getFloat(columnIndex, columnMetaDataList.get(columnIndex).getColType()); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
return rowCursor.getFloat(columnIndex, nativeType);
} }
@Override @Override
public double getDouble(int columnIndex) throws SQLException { public double getDouble(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType()); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
} return rowCursor.getDouble(columnIndex, nativeType);
@Override
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
columnIndex--;
return new BigDecimal(rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType()));
} }
@Override @Override
public byte[] getBytes(int columnIndex) throws SQLException { public byte[] getBytes(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return (rowCursor.getString(columnIndex, columnMetaDataList.get(columnIndex).getColType())).getBytes(); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
} return (rowCursor.getString(columnIndex, nativeType)).getBytes();
@Override
public Date getDate(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public Time getTime(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
@Override @Override
public Timestamp getTimestamp(int columnIndex) throws SQLException { public Timestamp getTimestamp(int columnIndex) throws SQLException {
columnIndex--; int colType = columnMetaDataList.get(columnIndex - 1).getColType();
return rowCursor.getTimestamp(columnIndex); int nativeType = TSDBConstants.jdbcType2TaosType(colType);
} return rowCursor.getTimestamp(columnIndex,nativeType);
@Override
public InputStream getAsciiStream(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public InputStream getBinaryStream(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public String getString(String columnLabel) throws SQLException {
return getString(findColumn(columnLabel));
}
@Override
public boolean getBoolean(String columnLabel) throws SQLException {
return getBoolean(findColumn(columnLabel));
}
@Override
public byte getByte(String columnLabel) throws SQLException {
return getByte(findColumn(columnLabel));
}
@Override
public short getShort(String columnLabel) throws SQLException {
return getShort(findColumn(columnLabel));
}
@Override
public int getInt(String columnLabel) throws SQLException {
return getInt(findColumn(columnLabel));
}
@Override
public long getLong(String columnLabel) throws SQLException {
return getLong(findColumn(columnLabel));
}
@Override
public float getFloat(String columnLabel) throws SQLException {
return getFloat(findColumn(columnLabel));
}
@Override
public double getDouble(String columnLabel) throws SQLException {
return getDouble(findColumn(columnLabel));
}
@Override
public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException {
return getBigDecimal(findColumn(columnLabel));
}
@Override
public byte[] getBytes(String columnLabel) throws SQLException {
return getBytes(findColumn(columnLabel));
}
@Override
public Date getDate(String columnLabel) throws SQLException {
return getDate(findColumn(columnLabel));
}
@Override
public Time getTime(String columnLabel) throws SQLException {
return getTime(findColumn(columnLabel));
}
@Override
public Timestamp getTimestamp(String columnLabel) throws SQLException {
return getTimestamp(findColumn(columnLabel));
}
@Override
public InputStream getAsciiStream(String columnLabel) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public InputStream getUnicodeStream(String columnLabel) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public InputStream getBinaryStream(String columnLabel) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public SQLWarning getWarnings() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public String getCursorName() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
@Override @Override
...@@ -291,12 +140,7 @@ public class DatabaseMetaDataResultSet implements ResultSet { ...@@ -291,12 +140,7 @@ public class DatabaseMetaDataResultSet implements ResultSet {
@Override @Override
public Object getObject(int columnIndex) throws SQLException { public Object getObject(int columnIndex) throws SQLException {
return rowCursor.get(columnIndex); return rowCursor.getObject(columnIndex);
}
@Override
public Object getObject(String columnLabel) throws SQLException {
return rowCursor.get(findColumn(columnLabel));
} }
@Override @Override
...@@ -304,31 +148,19 @@ public class DatabaseMetaDataResultSet implements ResultSet { ...@@ -304,31 +148,19 @@ public class DatabaseMetaDataResultSet implements ResultSet {
Iterator<ColumnMetaData> colMetaDataIt = this.columnMetaDataList.iterator(); Iterator<ColumnMetaData> colMetaDataIt = this.columnMetaDataList.iterator();
while (colMetaDataIt.hasNext()) { while (colMetaDataIt.hasNext()) {
ColumnMetaData colMetaData = colMetaDataIt.next(); ColumnMetaData colMetaData = colMetaDataIt.next();
if (colMetaData.getColName() != null && colMetaData.getColName().equalsIgnoreCase(columnLabel)) { if (colMetaData.getColName() != null && colMetaData.getColName().equals(columnLabel)) {
return colMetaData.getColIndex() + 1; return colMetaData.getColIndex();
} }
} }
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
} }
@Override
public Reader getCharacterStream(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public Reader getCharacterStream(String columnLabel) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override @Override
public BigDecimal getBigDecimal(int columnIndex) throws SQLException { public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
return new BigDecimal(rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType())); int colType = columnMetaDataList.get(columnIndex - 1).getColType();
} int nativeType = TSDBConstants.jdbcType2TaosType(colType);
double value = rowCursor.getDouble(columnIndex, nativeType);
@Override return new BigDecimal(value);
public BigDecimal getBigDecimal(String columnLabel) throws SQLException {
return getBigDecimal(findColumn(columnLabel));
} }
@Override @Override
...@@ -378,7 +210,6 @@ public class DatabaseMetaDataResultSet implements ResultSet { ...@@ -378,7 +210,6 @@ public class DatabaseMetaDataResultSet implements ResultSet {
} else { } else {
return 0; return 0;
} }
} }
@Override @Override
...@@ -397,680 +228,23 @@ public class DatabaseMetaDataResultSet implements ResultSet { ...@@ -397,680 +228,23 @@ public class DatabaseMetaDataResultSet implements ResultSet {
} }
@Override @Override
public void setFetchDirection(int direction) throws SQLException { public Statement getStatement() throws SQLException {
return null;
}
@Override
public int getFetchDirection() throws SQLException {
return ResultSet.FETCH_FORWARD;
}
@Override
public void setFetchSize(int rows) throws SQLException {
}
@Override
public int getFetchSize() throws SQLException {
return 0;
}
@Override
public int getType() throws SQLException {
return ResultSet.TYPE_FORWARD_ONLY;
}
@Override
public int getConcurrency() throws SQLException {
return ResultSet.CONCUR_READ_ONLY;
}
@Override
public boolean rowUpdated() throws SQLException {
return false;
} }
@Override public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
public boolean rowInserted() throws SQLException { //TODO: calendar is not used
return false; return getTimestamp(columnIndex);
} }
@Override @Override
public boolean rowDeleted() throws SQLException { public boolean isClosed() throws SQLException {
return false; return false;
} }
@Override @Override
public void updateNull(int columnIndex) throws SQLException { public String getNString(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); return getString(columnIndex);
}
@Override
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateByte(int columnIndex, byte x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateShort(int columnIndex, short x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateInt(int columnIndex, int x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateLong(int columnIndex, long x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateFloat(int columnIndex, float x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateDouble(int columnIndex, double x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateString(int columnIndex, String x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateDate(int columnIndex, Date x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateTime(int columnIndex, Time x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateObject(int columnIndex, Object x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateNull(String columnLabel) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateBoolean(String columnLabel, boolean x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateByte(String columnLabel, byte x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateShort(String columnLabel, short x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateInt(String columnLabel, int x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateLong(String columnLabel, long x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateFloat(String columnLabel, float x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateDouble(String columnLabel, double x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateBigDecimal(String columnLabel, BigDecimal x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateString(String columnLabel, String x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
@Override
public void updateBytes(String columnLabel, byte[] x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateDate(String columnLabel, Date x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateTime(String columnLabel, Time x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateObject(String columnLabel, Object x, int scaleOrLength) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateObject(String columnLabel, Object x) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void insertRow() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void updateRow() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void deleteRow() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void refreshRow() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void cancelRowUpdates() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void moveToInsertRow() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public void moveToCurrentRow() throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public Statement getStatement() throws SQLException {
return null;
}
@Override
public Object getObject(int columnIndex, Map<String, Class<?>> map) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public Ref getRef(int columnIndex) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public Blob getBlob(int columnIndex) throws SQLException {
return null;
}
@Override
public Clob getClob(int columnIndex) throws SQLException {
return null;
}
@Override
public Array getArray(int columnIndex) throws SQLException {
return null;
}
@Override
public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQLException {
return null;
}
@Override
public Ref getRef(String columnLabel) throws SQLException {
return null;
}
@Override
public Blob getBlob(String columnLabel) throws SQLException {
return null;
}
@Override
public Clob getClob(String columnLabel) throws SQLException {
return null;
}
@Override
public Array getArray(String columnLabel) throws SQLException {
return null;
}
@Override
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
return null;
}
@Override
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
return null;
}
@Override
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
return null;
}
@Override
public Time getTime(String columnLabel, Calendar cal) throws SQLException {
return null;
}
@Override
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
return null;
}
@Override
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
return null;
}
@Override
public URL getURL(int columnIndex) throws SQLException {
return null;
}
@Override
public URL getURL(String columnLabel) throws SQLException {
return null;
}
@Override
public void updateRef(int columnIndex, Ref x) throws SQLException {
}
@Override
public void updateRef(String columnLabel, Ref x) throws SQLException {
}
@Override
public void updateBlob(int columnIndex, Blob x) throws SQLException {
}
@Override
public void updateBlob(String columnLabel, Blob x) throws SQLException {
}
@Override
public void updateClob(int columnIndex, Clob x) throws SQLException {
}
@Override
public void updateClob(String columnLabel, Clob x) throws SQLException {
}
@Override
public void updateArray(int columnIndex, Array x) throws SQLException {
}
@Override
public void updateArray(String columnLabel, Array x) throws SQLException {
}
@Override
public RowId getRowId(int columnIndex) throws SQLException {
return null;
}
@Override
public RowId getRowId(String columnLabel) throws SQLException {
return null;
}
@Override
public void updateRowId(int columnIndex, RowId x) throws SQLException {
}
@Override
public void updateRowId(String columnLabel, RowId x) throws SQLException {
}
@Override
public int getHoldability() throws SQLException {
return 0;
}
@Override
public boolean isClosed() throws SQLException {
return false;
}
@Override
public void updateNString(int columnIndex, String nString) throws SQLException {
}
@Override
public void updateNString(String columnLabel, String nString) throws SQLException {
}
@Override
public void updateNClob(int columnIndex, NClob nClob) throws SQLException {
}
@Override
public void updateNClob(String columnLabel, NClob nClob) throws SQLException {
}
@Override
public NClob getNClob(int columnIndex) throws SQLException {
return null;
}
@Override
public NClob getNClob(String columnLabel) throws SQLException {
return null;
}
@Override
public SQLXML getSQLXML(int columnIndex) throws SQLException {
return null;
}
@Override
public SQLXML getSQLXML(String columnLabel) throws SQLException {
return null;
}
@Override
public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException {
}
@Override
public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException {
}
@Override
public String getNString(int columnIndex) throws SQLException {
return null;
}
@Override
public String getNString(String columnLabel) throws SQLException {
return null;
}
@Override
public Reader getNCharacterStream(int columnIndex) throws SQLException {
return null;
}
@Override
public Reader getNCharacterStream(String columnLabel) throws SQLException {
return null;
}
@Override
public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
}
@Override
public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
}
@Override
public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException {
}
@Override
public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException {
}
@Override
public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
}
@Override
public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException {
}
@Override
public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
}
@Override
public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
}
@Override
public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException {
}
@Override
public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException {
}
@Override
public void updateClob(int columnIndex, Reader reader, long length) throws SQLException {
}
@Override
public void updateClob(String columnLabel, Reader reader, long length) throws SQLException {
}
@Override
public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException {
}
@Override
public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException {
}
@Override
public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException {
}
@Override
public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException {
}
@Override
public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException {
}
@Override
public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException {
}
@Override
public void updateCharacterStream(int columnIndex, Reader x) throws SQLException {
}
@Override
public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException {
}
@Override
public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException {
}
@Override
public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException {
}
@Override
public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException {
}
@Override
public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException {
}
@Override
public void updateClob(int columnIndex, Reader reader) throws SQLException {
}
@Override
public void updateClob(String columnLabel, Reader reader) throws SQLException {
}
@Override
public void updateNClob(int columnIndex, Reader reader) throws SQLException {
}
@Override
public void updateNClob(String columnLabel, Reader reader) throws SQLException {
}
@Override
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
private int getTrueColumnIndex(int columnIndex) throws SQLException {
if (columnIndex < 1) {
throw new SQLException("Column Index out of range, " + columnIndex + " < " + 1);
}
int numOfCols = this.columnMetaDataList.size();
if (columnIndex > numOfCols) {
throw new SQLException("Column Index out of range, " + columnIndex + " > " + numOfCols);
}
return columnIndex - 1;
}
} }
...@@ -41,14 +41,14 @@ public abstract class TSDBConstants { ...@@ -41,14 +41,14 @@ public abstract class TSDBConstants {
public static final int TSDB_DATA_TYPE_BINARY = 8; public static final int TSDB_DATA_TYPE_BINARY = 8;
public static final int TSDB_DATA_TYPE_TIMESTAMP = 9; public static final int TSDB_DATA_TYPE_TIMESTAMP = 9;
public static final int TSDB_DATA_TYPE_NCHAR = 10; public static final int TSDB_DATA_TYPE_NCHAR = 10;
/* /**
系统增加新的无符号数据类型,分别是: * 系统增加新的无符号数据类型,分别是:
unsigned tinyint, 数值范围:0-254, NULL 为255 * unsigned tinyint, 数值范围:0-254, NULL 为255
unsigned smallint,数值范围: 0-65534, NULL 为65535 * unsigned smallint,数值范围: 0-65534, NULL 为65535
unsigned int,数值范围:0-4294967294,NULL 为4294967295u * unsigned int,数值范围:0-4294967294,NULL 为4294967295u
unsigned bigint,数值范围:0-18446744073709551614u,NULL 为18446744073709551615u。 * unsigned bigint,数值范围:0-18446744073709551614u,NULL 为18446744073709551615u。
example: * example:
create table tb(ts timestamp, a tinyint unsigned, b smallint unsigned, c int unsigned, d bigint unsigned); * create table tb(ts timestamp, a tinyint unsigned, b smallint unsigned, c int unsigned, d bigint unsigned);
*/ */
public static final int TSDB_DATA_TYPE_UTINYINT = 11; //unsigned tinyint public static final int TSDB_DATA_TYPE_UTINYINT = 11; //unsigned tinyint
public static final int TSDB_DATA_TYPE_USMALLINT = 12; //unsigned smallint public static final int TSDB_DATA_TYPE_USMALLINT = 12; //unsigned smallint
...@@ -57,6 +57,47 @@ public abstract class TSDBConstants { ...@@ -57,6 +57,47 @@ public abstract class TSDBConstants {
// nchar column max length // nchar column max length
public static final int maxFieldSize = 16 * 1024; public static final int maxFieldSize = 16 * 1024;
// precision for data types
public static final int BOOLEAN_PRECISION = 1;
public static final int TINYINT_PRECISION = 4;
public static final int SMALLINT_PRECISION = 6;
public static final int INT_PRECISION = 11;
public static final int BIGINT_PRECISION = 20;
public static final int FLOAT_PRECISION = 12;
public static final int DOUBLE_PRECISION = 22;
public static final int TIMESTAMP_MS_PRECISION = 23;
public static final int TIMESTAMP_US_PRECISION = 26;
// scale for data types
public static final int FLOAT_SCALE = 31;
public static final int DOUBLE_SCALE = 31;
public static int typeName2JdbcType(String type) {
switch (type.toUpperCase()) {
case "TIMESTAMP":
return Types.TIMESTAMP;
case "INT":
return Types.INTEGER;
case "BIGINT":
return Types.BIGINT;
case "FLOAT":
return Types.FLOAT;
case "DOUBLE":
return Types.DOUBLE;
case "BINARY":
return Types.BINARY;
case "SMALLINT":
return Types.SMALLINT;
case "TINYINT":
return Types.TINYINT;
case "BOOL":
return Types.BOOLEAN;
case "NCHAR":
return Types.NCHAR;
default:
return Types.NULL;
}
}
public static int taosType2JdbcType(int taosType) throws SQLException { public static int taosType2JdbcType(int taosType) throws SQLException {
switch (taosType) { switch (taosType) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
...@@ -88,7 +129,7 @@ public abstract class TSDBConstants { ...@@ -88,7 +129,7 @@ public abstract class TSDBConstants {
} }
public static String taosType2JdbcTypeName(int taosType) throws SQLException { public static String taosType2JdbcTypeName(int taosType) throws SQLException {
switch (taosType){ switch (taosType) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return "BOOL"; return "BOOL";
case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: case TSDBConstants.TSDB_DATA_TYPE_UTINYINT:
...@@ -119,7 +160,7 @@ public abstract class TSDBConstants { ...@@ -119,7 +160,7 @@ public abstract class TSDBConstants {
} }
public static int jdbcType2TaosType(int jdbcType) throws SQLException { public static int jdbcType2TaosType(int jdbcType) throws SQLException {
switch (jdbcType){ switch (jdbcType) {
case Types.BOOLEAN: case Types.BOOLEAN:
return TSDBConstants.TSDB_DATA_TYPE_BOOL; return TSDBConstants.TSDB_DATA_TYPE_BOOL;
case Types.TINYINT: case Types.TINYINT:
...@@ -145,7 +186,7 @@ public abstract class TSDBConstants { ...@@ -145,7 +186,7 @@ public abstract class TSDBConstants {
} }
public static String jdbcType2TaosTypeName(int jdbcType) throws SQLException { public static String jdbcType2TaosTypeName(int jdbcType) throws SQLException {
switch (jdbcType){ switch (jdbcType) {
case Types.BOOLEAN: case Types.BOOLEAN:
return "BOOL"; return "BOOL";
case Types.TINYINT: case Types.TINYINT:
......
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
*/ */
package com.taosdata.jdbc; package com.taosdata.jdbc;
import com.taosdata.jdbc.utils.TaosInfo;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLWarning; import java.sql.SQLWarning;
import java.util.List; import java.util.List;
import com.taosdata.jdbc.utils.TaosInfo;
/** /**
* JNI connector * JNI connector
*/ */
...@@ -194,7 +194,9 @@ public class TSDBJNIConnector { ...@@ -194,7 +194,9 @@ public class TSDBJNIConnector {
* Get schema metadata * Get schema metadata
*/ */
public int getSchemaMetaData(long resultSet, List<ColumnMetaData> columnMetaData) { public int getSchemaMetaData(long resultSet, List<ColumnMetaData> columnMetaData) {
return this.getSchemaMetaDataImp(this.taos, resultSet, columnMetaData); int ret = this.getSchemaMetaDataImp(this.taos, resultSet, columnMetaData);
columnMetaData.stream().forEach(column -> column.setColIndex(column.getColIndex() + 1));
return ret;
} }
private native int getSchemaMetaDataImp(long connection, long resultSet, List<ColumnMetaData> columnMetaData); private native int getSchemaMetaDataImp(long connection, long resultSet, List<ColumnMetaData> columnMetaData);
...@@ -276,14 +278,23 @@ public class TSDBJNIConnector { ...@@ -276,14 +278,23 @@ public class TSDBJNIConnector {
private native int validateCreateTableSqlImp(long connection, byte[] sqlBytes); private native int validateCreateTableSqlImp(long connection, byte[] sqlBytes);
public long prepareStmt(String sql) throws SQLException { public long prepareStmt(String sql) throws SQLException {
Long stmt = prepareStmtImp(sql.getBytes(), this.taos); Long stmt;
if (stmt == TSDBConstants.JNI_TDENGINE_ERROR) { try {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_SQL); stmt = prepareStmtImp(sql.getBytes(), this.taos);
} else if (stmt == TSDBConstants.JNI_CONNECTION_NULL) { } catch (Exception e) {
e.printStackTrace();
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_ENCODING);
}
if (stmt == TSDBConstants.JNI_CONNECTION_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL);
} else if (stmt == TSDBConstants.JNI_SQL_NULL) { }
if (stmt == TSDBConstants.JNI_SQL_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_SQL_NULL); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_SQL_NULL);
} else if (stmt == TSDBConstants.JNI_OUT_OF_MEMORY) { }
if (stmt == TSDBConstants.JNI_OUT_OF_MEMORY) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_OUT_OF_MEMORY); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_OUT_OF_MEMORY);
} }
...@@ -311,7 +322,7 @@ public class TSDBJNIConnector { ...@@ -311,7 +322,7 @@ public class TSDBJNIConnector {
private native int setTableNameTagsImp(long stmt, String name, int numOfTags, byte[] tags, byte[] typeList, byte[] lengthList, byte[] nullList, long conn); private native int setTableNameTagsImp(long stmt, String name, int numOfTags, byte[] tags, byte[] typeList, byte[] lengthList, byte[] nullList, long conn);
public void bindColumnDataArray(long stmt, ByteBuffer colDataList, ByteBuffer lengthList, ByteBuffer isNullList, int type, int bytes, int numOfRows,int columnIndex) throws SQLException { public void bindColumnDataArray(long stmt, ByteBuffer colDataList, ByteBuffer lengthList, ByteBuffer isNullList, int type, int bytes, int numOfRows, int columnIndex) throws SQLException {
int code = bindColDataImp(stmt, colDataList.array(), lengthList.array(), isNullList.array(), type, bytes, numOfRows, columnIndex, this.taos); int code = bindColDataImp(stmt, colDataList.array(), lengthList.array(), isNullList.array(), type, bytes, numOfRows, columnIndex, this.taos);
if (code != TSDBConstants.JNI_SUCCESS) { if (code != TSDBConstants.JNI_SUCCESS) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to bind column data"); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to bind column data");
......
...@@ -205,9 +205,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -205,9 +205,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override @Override
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
if (isClosed()) setObject(parameterIndex, x.doubleValue());
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
@Override @Override
...@@ -222,16 +220,12 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -222,16 +220,12 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override @Override
public void setDate(int parameterIndex, Date x) throws SQLException { public void setDate(int parameterIndex, Date x) throws SQLException {
if (isClosed()) setObject(parameterIndex, new Timestamp(x.getTime()));
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 {
if (isClosed()) setObject(parameterIndex, new Timestamp(x.getTime()));
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
@Override @Override
...@@ -283,7 +277,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -283,7 +277,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
if (parameterIndex < 1 && parameterIndex >= parameters.length) { if (parameterIndex < 1 && parameterIndex >= parameters.length) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE);
} }
parameters[parameterIndex - 1] = x; parameters[parameterIndex - 1] = x;
} }
...@@ -350,9 +343,9 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -350,9 +343,9 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override @Override
public ResultSetMetaData getMetaData() throws SQLException { public ResultSetMetaData getMetaData() throws SQLException {
if (isClosed()) if (this.getResultSet() == null)
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); return null;
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); return getResultSet().getMetaData();
} }
@Override @Override
...@@ -396,10 +389,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -396,10 +389,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
if (isClosed()) if (isClosed())
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
if (parameterMetaData == null) { return new TSDBParameterMetaData(parameters);
this.parameterMetaData = new TSDBParameterMetaData(parameters);
}
return this.parameterMetaData;
} }
@Override @Override
...@@ -411,9 +401,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -411,9 +401,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override @Override
public void setNString(int parameterIndex, String value) throws SQLException { public void setNString(int parameterIndex, String value) throws SQLException {
if (isClosed()) setString(parameterIndex, value);
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
@Override @Override
...@@ -563,12 +551,15 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -563,12 +551,15 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
public boolean isTypeSet() { public boolean isTypeSet() {
return this.typeIsSet; return this.typeIsSet;
} }
}; }
;
private static class TableTagInfo { private static class TableTagInfo {
private boolean isNull; private boolean isNull;
private Object value; private Object value;
private int type; private int type;
public TableTagInfo(Object value, int type) { public TableTagInfo(Object value, int type) {
this.value = value; this.value = value;
this.type = type; this.type = type;
...@@ -579,7 +570,9 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -579,7 +570,9 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
info.isNull = true; info.isNull = true;
return info; return info;
} }
}; }
;
public void setTableName(String name) { public void setTableName(String name) {
this.tableName = name; this.tableName = name;
...@@ -666,8 +659,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -666,8 +659,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
public <T> void setValueImpl(int columnIndex, ArrayList<T> list, int type, int bytes) throws SQLException { public <T> void setValueImpl(int columnIndex, ArrayList<T> list, int type, int bytes) throws SQLException {
if (this.colData.size() == 0) { if (this.colData.size() == 0) {
this.colData.addAll(Collections.nCopies(this.parameters.length - 1 - this.tableTags.size(), null)); this.colData.addAll(Collections.nCopies(this.parameters.length - 1 - this.tableTags.size(), null));
} }
ColumnInfo col = (ColumnInfo) this.colData.get(columnIndex); ColumnInfo col = (ColumnInfo) this.colData.get(columnIndex);
if (col == null) { if (col == null) {
ColumnInfo p = new ColumnInfo(); ColumnInfo p = new ColumnInfo();
...@@ -852,7 +845,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -852,7 +845,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
} }
typeList.put((byte) tag.type); typeList.put((byte) tag.type);
isNullList.putInt(tag.isNull? 1 : 0); isNullList.putInt(tag.isNull ? 1 : 0);
} }
connector.setBindTableNameAndTags(this.nativeStmtHandle, this.tableName, this.tableTags.size(), tagDataList, connector.setBindTableNameAndTags(this.nativeStmtHandle, this.tableName, this.tableTags.size(), tagDataList,
...@@ -888,8 +881,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -888,8 +881,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
case TSDBConstants.TSDB_DATA_TYPE_INT: { case TSDBConstants.TSDB_DATA_TYPE_INT: {
for (int j = 0; j < rows; ++j) { for (int j = 0; j < rows; ++j) {
Integer val = (Integer) col1.data.get(j); Integer val = (Integer) col1.data.get(j);
colDataList.putInt(val == null? Integer.MIN_VALUE:val); colDataList.putInt(val == null ? Integer.MIN_VALUE : val);
isNullList.put((byte) (val == null? 1:0)); isNullList.put((byte) (val == null ? 1 : 0));
} }
break; break;
} }
...@@ -897,8 +890,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -897,8 +890,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { case TSDBConstants.TSDB_DATA_TYPE_TINYINT: {
for (int j = 0; j < rows; ++j) { for (int j = 0; j < rows; ++j) {
Byte val = (Byte) col1.data.get(j); Byte val = (Byte) col1.data.get(j);
colDataList.put(val == null? 0:val); colDataList.put(val == null ? 0 : val);
isNullList.put((byte) (val == null? 1:0)); isNullList.put((byte) (val == null ? 1 : 0));
} }
break; break;
} }
...@@ -909,10 +902,10 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -909,10 +902,10 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
if (val == null) { if (val == null) {
colDataList.put((byte) 0); colDataList.put((byte) 0);
} else { } else {
colDataList.put((byte) (val? 1:0)); colDataList.put((byte) (val ? 1 : 0));
} }
isNullList.put((byte) (val == null? 1:0)); isNullList.put((byte) (val == null ? 1 : 0));
} }
break; break;
} }
...@@ -920,8 +913,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -920,8 +913,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: {
for (int j = 0; j < rows; ++j) { for (int j = 0; j < rows; ++j) {
Short val = (Short) col1.data.get(j); Short val = (Short) col1.data.get(j);
colDataList.putShort(val == null? 0:val); colDataList.putShort(val == null ? 0 : val);
isNullList.put((byte) (val == null? 1:0)); isNullList.put((byte) (val == null ? 1 : 0));
} }
break; break;
} }
...@@ -930,8 +923,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -930,8 +923,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { case TSDBConstants.TSDB_DATA_TYPE_BIGINT: {
for (int j = 0; j < rows; ++j) { for (int j = 0; j < rows; ++j) {
Long val = (Long) col1.data.get(j); Long val = (Long) col1.data.get(j);
colDataList.putLong(val == null? 0:val); colDataList.putLong(val == null ? 0 : val);
isNullList.put((byte) (val == null? 1:0)); isNullList.put((byte) (val == null ? 1 : 0));
} }
break; break;
} }
...@@ -939,8 +932,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -939,8 +932,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { case TSDBConstants.TSDB_DATA_TYPE_FLOAT: {
for (int j = 0; j < rows; ++j) { for (int j = 0; j < rows; ++j) {
Float val = (Float) col1.data.get(j); Float val = (Float) col1.data.get(j);
colDataList.putFloat(val == null? 0:val); colDataList.putFloat(val == null ? 0 : val);
isNullList.put((byte) (val == null? 1:0)); isNullList.put((byte) (val == null ? 1 : 0));
} }
break; break;
} }
...@@ -948,8 +941,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -948,8 +941,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: {
for (int j = 0; j < rows; ++j) { for (int j = 0; j < rows; ++j) {
Double val = (Double) col1.data.get(j); Double val = (Double) col1.data.get(j);
colDataList.putDouble(val == null? 0:val); colDataList.putDouble(val == null ? 0 : val);
isNullList.put((byte) (val == null? 1:0)); isNullList.put((byte) (val == null ? 1 : 0));
} }
break; break;
} }
...@@ -994,7 +987,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat ...@@ -994,7 +987,8 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "not support data types"); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "not support data types");
} }
}; }
;
connector.bindColumnDataArray(this.nativeStmtHandle, colDataList, lengthList, isNullList, col1.type, col1.bytes, rows, i); connector.bindColumnDataArray(this.nativeStmtHandle, colDataList, lengthList, isNullList, col1.type, col1.bytes, rows, i);
} }
......
...@@ -133,9 +133,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -133,9 +133,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return this.blockData.getString(columnIndex - 1); return this.blockData.getString(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
res = this.rowData.getString(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = this.rowData.getString(columnIndex, nativeType);
} }
return res; return res;
} }
...@@ -147,9 +148,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -147,9 +148,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return this.blockData.getBoolean(columnIndex - 1); return this.blockData.getBoolean(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
res = this.rowData.getBoolean(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = this.rowData.getBoolean(columnIndex, nativeType);
} }
return res; return res;
} }
...@@ -161,9 +163,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -161,9 +163,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return (byte) this.blockData.getInt(columnIndex - 1); return (byte) this.blockData.getInt(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
res = (byte) this.rowData.getInt(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = (byte) this.rowData.getInt(columnIndex, nativeType);
} }
return res; return res;
} }
...@@ -175,9 +178,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -175,9 +178,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return (short) this.blockData.getInt(columnIndex - 1); return (short) this.blockData.getInt(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
res = (short) this.rowData.getInt(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = (short) this.rowData.getInt(columnIndex, nativeType);
} }
return res; return res;
} }
...@@ -189,9 +193,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -189,9 +193,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return this.blockData.getInt(columnIndex - 1); return this.blockData.getInt(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
res = this.rowData.getInt(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = this.rowData.getInt(columnIndex, nativeType);
} }
return res; return res;
} }
...@@ -203,13 +209,15 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -203,13 +209,15 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return this.blockData.getLong(columnIndex - 1); return this.blockData.getLong(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
Object value = this.rowData.get(columnIndex - 1); Object value = this.rowData.getObject(columnIndex);
if (value instanceof Timestamp) if (value instanceof Timestamp) {
res = ((Timestamp) value).getTime(); res = ((Timestamp) value).getTime();
else } else {
res = this.rowData.getLong(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = this.rowData.getLong(columnIndex, nativeType);
}
} }
return res; return res;
} }
...@@ -221,9 +229,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -221,9 +229,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return (float) this.blockData.getDouble(columnIndex - 1); return (float) this.blockData.getDouble(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) if (!lastWasNull) {
res = this.rowData.getFloat(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = this.rowData.getFloat(columnIndex, nativeType);
}
return res; return res;
} }
...@@ -235,9 +245,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -235,9 +245,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return this.blockData.getDouble(columnIndex - 1); return this.blockData.getDouble(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
res = this.rowData.getDouble(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = this.rowData.getDouble(columnIndex, nativeType);
} }
return res; return res;
} }
...@@ -245,34 +256,27 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -245,34 +256,27 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
public byte[] getBytes(int columnIndex) throws SQLException { public byte[] getBytes(int columnIndex) throws SQLException {
checkAvailability(columnIndex, this.columnMetaDataList.size()); checkAvailability(columnIndex, this.columnMetaDataList.size());
Object value = this.rowData.get(columnIndex - 1); Object value = this.rowData.getObject(columnIndex);
if (value == null) if (value == null)
return null; return null;
int colType = this.columnMetaDataList.get(columnIndex - 1).getColType(); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
switch (colType) { switch (nativeType) {
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
return Longs.toByteArray((Long) value); return Longs.toByteArray((long) value);
case TSDBConstants.TSDB_DATA_TYPE_INT: case TSDBConstants.TSDB_DATA_TYPE_INT:
return Ints.toByteArray((int) value); return Ints.toByteArray((int) value);
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:
return Shorts.toByteArray((Short) value); return Shorts.toByteArray((short) value);
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: case TSDBConstants.TSDB_DATA_TYPE_TINYINT:
return new byte[]{(byte) value}; return new byte[]{(byte) value};
} case TSDBConstants.TSDB_DATA_TYPE_BINARY:
return (byte[]) value;
case TSDBConstants.TSDB_DATA_TYPE_BOOL:
case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
default:
return value.toString().getBytes(); return value.toString().getBytes();
} }
@Override
public Date getDate(int columnIndex) throws SQLException {
Timestamp timestamp = getTimestamp(columnIndex);
return timestamp == null ? null : new Date(timestamp.getTime());
}
@Override
public Time getTime(int columnIndex) throws SQLException {
Timestamp timestamp = getTimestamp(columnIndex);
return timestamp == null ? null : new Time(timestamp.getTime());
} }
public Timestamp getTimestamp(int columnIndex) throws SQLException { public Timestamp getTimestamp(int columnIndex) throws SQLException {
...@@ -282,9 +286,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -282,9 +286,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return this.blockData.getTimestamp(columnIndex - 1); return this.blockData.getTimestamp(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
res = this.rowData.getTimestamp(columnIndex - 1); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
res = this.rowData.getTimestamp(columnIndex, nativeType);
} }
return res; return res;
} }
...@@ -304,13 +309,9 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -304,13 +309,9 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return this.blockData.get(columnIndex - 1); return this.blockData.get(columnIndex - 1);
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
if (!lastWasNull) { if (!lastWasNull) {
int colType = this.columnMetaDataList.get(columnIndex - 1).getColType(); res = this.rowData.getObject(columnIndex);
if (colType == TSDBConstants.TSDB_DATA_TYPE_BINARY)
res = ((String) this.rowData.get(columnIndex - 1)).getBytes();
else
res = this.rowData.get(columnIndex - 1);
} }
return res; return res;
} }
...@@ -318,7 +319,7 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -318,7 +319,7 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
public int findColumn(String columnLabel) throws SQLException { public int findColumn(String columnLabel) throws SQLException {
for (ColumnMetaData colMetaData : this.columnMetaDataList) { for (ColumnMetaData colMetaData : this.columnMetaDataList) {
if (colMetaData.getColName() != null && colMetaData.getColName().equalsIgnoreCase(columnLabel)) { if (colMetaData.getColName() != null && colMetaData.getColName().equalsIgnoreCase(columnLabel)) {
return colMetaData.getColIndex() + 1; return colMetaData.getColIndex();
} }
} }
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
...@@ -329,25 +330,25 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { ...@@ -329,25 +330,25 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet {
if (this.getBatchFetch()) if (this.getBatchFetch())
return new BigDecimal(this.blockData.getLong(columnIndex - 1)); return new BigDecimal(this.blockData.getLong(columnIndex - 1));
this.lastWasNull = this.rowData.wasNull(columnIndex - 1); this.lastWasNull = this.rowData.wasNull(columnIndex);
BigDecimal res = null; BigDecimal res = null;
if (!lastWasNull) { if (!lastWasNull) {
int colType = this.columnMetaDataList.get(columnIndex - 1).getColType(); int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType();
switch (colType) { switch (nativeType) {
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: case TSDBConstants.TSDB_DATA_TYPE_TINYINT:
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:
case TSDBConstants.TSDB_DATA_TYPE_INT: case TSDBConstants.TSDB_DATA_TYPE_INT:
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
res = new BigDecimal(Long.valueOf(this.rowData.get(columnIndex - 1).toString())); res = new BigDecimal(Long.valueOf(this.rowData.getObject(columnIndex).toString()));
break; break;
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
res = new BigDecimal(Double.valueOf(this.rowData.get(columnIndex - 1).toString())); res = new BigDecimal(Double.valueOf(this.rowData.getObject(columnIndex).toString()));
break; break;
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
return new BigDecimal(((Timestamp) this.rowData.get(columnIndex - 1)).getTime()); return new BigDecimal(((Timestamp) this.rowData.getObject(columnIndex)).getTime());
default: default:
res = new BigDecimal(this.rowData.get(columnIndex - 1).toString()); res = new BigDecimal(this.rowData.getObject(columnIndex).toString());
} }
} }
return res; return res;
......
...@@ -113,6 +113,7 @@ public class TSDBResultSetMetaData extends WrapperImpl implements ResultSetMetaD ...@@ -113,6 +113,7 @@ public class TSDBResultSetMetaData extends WrapperImpl implements ResultSetMetaD
ColumnMetaData columnMetaData = this.colMetaDataList.get(column - 1); ColumnMetaData columnMetaData = this.colMetaDataList.get(column - 1);
switch (columnMetaData.getColType()) { switch (columnMetaData.getColType()) {
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
return 5; return 5;
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
*****************************************************************************/ *****************************************************************************/
package com.taosdata.jdbc; package com.taosdata.jdbc;
import com.taosdata.jdbc.utils.NullType;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
...@@ -22,11 +24,13 @@ import java.util.ArrayList; ...@@ -22,11 +24,13 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
public class TSDBResultSetRowData { public class TSDBResultSetRowData {
private ArrayList<Object> data; private ArrayList<Object> data;
private int colSize = 0; private int colSize;
public TSDBResultSetRowData(int colSize) { public TSDBResultSetRowData(int colSize) {
this.setColSize(colSize); this.colSize = colSize;
this.clear();
} }
public void clear() { public void clear() {
...@@ -41,68 +45,104 @@ public class TSDBResultSetRowData { ...@@ -41,68 +45,104 @@ public class TSDBResultSetRowData {
} }
public boolean wasNull(int col) { public boolean wasNull(int col) {
return data.get(col) == null; return data.get(col - 1) == null;
}
/**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setBooleanValue(int col, boolean value) {
setBoolean(col - 1, value);
} }
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setBoolean(int col, boolean value) { public void setBoolean(int col, boolean value) {
data.set(col, value); data.set(col, value);
} }
public boolean getBoolean(int col, int srcType) throws SQLException { public boolean getBoolean(int col, int nativeType) throws SQLException {
Object obj = data.get(col); Object obj = data.get(col - 1);
switch (srcType) { switch (nativeType) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return (Boolean) obj; return (Boolean) obj;
case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
return ((Float) obj) == 1.0 ? Boolean.TRUE : Boolean.FALSE;
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
return ((Double) obj) == 1.0 ? Boolean.TRUE : Boolean.FALSE;
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: case TSDBConstants.TSDB_DATA_TYPE_TINYINT:
return ((Byte) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; return ((Byte) obj) == 1 ? Boolean.TRUE : Boolean.FALSE;
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:
return ((Short) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; return ((Short) obj) == 1 ? Boolean.TRUE : Boolean.FALSE;
case TSDBConstants.TSDB_DATA_TYPE_INT: case TSDBConstants.TSDB_DATA_TYPE_INT:
return ((Integer) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; return ((Integer) obj) == 1 ? Boolean.TRUE : Boolean.FALSE;
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
return ((Long) obj) == 1L ? Boolean.TRUE : Boolean.FALSE; return ((Long) obj) == 1L ? Boolean.TRUE : Boolean.FALSE;
case TSDBConstants.TSDB_DATA_TYPE_BINARY:
case TSDBConstants.TSDB_DATA_TYPE_NCHAR: {
return obj.toString().contains("1");
}
default: default:
return false; return false;
} }
} }
/**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setByteValue(int colIndex, byte value) {
setByte(colIndex - 1, value);
}
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setByte(int col, byte value) { public void setByte(int col, byte value) {
data.set(col, value); data.set(col, value);
} }
/**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setShortValue(int colIndex, short value) {
setShort(colIndex - 1, value);
}
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setShort(int col, short value) { public void setShort(int col, short value) {
data.set(col, value); data.set(col, value);
} }
/**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setIntValue(int colIndex, int value) {
setInt(colIndex - 1, value);
}
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setInt(int col, int value) { public void setInt(int col, int value) {
data.set(col, value); data.set(col, value);
} }
@SuppressWarnings("deprecation") public int getInt(int col, int nativeType) throws SQLException {
public int getInt(int col, int srcType) throws SQLException { Object obj = data.get(col - 1);
Object obj = data.get(col); if (obj == null)
return NullType.getIntNull();
switch (srcType) { switch (nativeType) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return Boolean.TRUE.equals(obj) ? 1 : 0; return Boolean.TRUE.equals(obj) ? 1 : 0;
case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
return ((Float) obj).intValue();
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
return ((Double) obj).intValue();
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: case TSDBConstants.TSDB_DATA_TYPE_TINYINT:
return (Byte) obj; return (Byte) obj;
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:
return (Short) obj; return (Short) obj;
case TSDBConstants.TSDB_DATA_TYPE_INT: case TSDBConstants.TSDB_DATA_TYPE_INT:
return (Integer) obj; return (Integer) obj;
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
return ((Long) obj).intValue(); return ((Long) obj).intValue();
case TSDBConstants.TSDB_DATA_TYPE_NCHAR: case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
case TSDBConstants.TSDB_DATA_TYPE_BINARY: case TSDBConstants.TSDB_DATA_TYPE_BINARY:
...@@ -131,33 +171,46 @@ public class TSDBResultSetRowData { ...@@ -131,33 +171,46 @@ public class TSDBResultSetRowData {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE);
return Long.valueOf(value).intValue(); return Long.valueOf(value).intValue();
} }
case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
return ((Float) obj).intValue();
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
return ((Double) obj).intValue();
default:
return 0;
}
} }
return 0; /**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setLongValue(int colIndex, long value) {
setLong(colIndex - 1, value);
} }
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setLong(int col, long value) { public void setLong(int col, long value) {
data.set(col, value); data.set(col, value);
} }
public long getLong(int col, int srcType) throws SQLException { public long getLong(int col, int nativeType) throws SQLException {
Object obj = data.get(col); Object obj = data.get(col - 1);
if (obj == null) {
return NullType.getBigIntNull();
}
switch (srcType) { switch (nativeType) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return Boolean.TRUE.equals(obj) ? 1 : 0; return Boolean.TRUE.equals(obj) ? 1 : 0;
case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
return ((Float) obj).longValue();
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
return ((Double) obj).longValue();
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: case TSDBConstants.TSDB_DATA_TYPE_TINYINT:
return (Byte) obj; return (Byte) obj;
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:
return (Short) obj; return (Short) obj;
case TSDBConstants.TSDB_DATA_TYPE_INT: case TSDBConstants.TSDB_DATA_TYPE_INT:
return (Integer) obj; return (Integer) obj;
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
return (Long) obj; return (Long) obj;
case TSDBConstants.TSDB_DATA_TYPE_NCHAR: case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
case TSDBConstants.TSDB_DATA_TYPE_BINARY: case TSDBConstants.TSDB_DATA_TYPE_BINARY:
...@@ -186,19 +239,35 @@ public class TSDBResultSetRowData { ...@@ -186,19 +239,35 @@ public class TSDBResultSetRowData {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE);
return value; return value;
} }
case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
return ((Float) obj).longValue();
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
return ((Double) obj).longValue();
default:
return 0;
}
} }
return 0; /**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setFloatValue(int colIndex, float value) {
setFloat(colIndex - 1, value);
} }
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setFloat(int col, float value) { public void setFloat(int col, float value) {
data.set(col, value); data.set(col, value);
} }
public float getFloat(int col, int srcType) { public float getFloat(int col, int nativeType) {
Object obj = data.get(col); Object obj = data.get(col - 1);
if (obj == null)
return NullType.getFloatNull();
switch (srcType) { switch (nativeType) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return Boolean.TRUE.equals(obj) ? 1 : 0; return Boolean.TRUE.equals(obj) ? 1 : 0;
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
...@@ -214,19 +283,31 @@ public class TSDBResultSetRowData { ...@@ -214,19 +283,31 @@ public class TSDBResultSetRowData {
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
return (Long) obj; return (Long) obj;
default:
return NullType.getFloatNull();
}
} }
return 0; /**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setDoubleValue(int colIndex, double value) {
setDouble(colIndex - 1, value);
} }
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setDouble(int col, double value) { public void setDouble(int col, double value) {
data.set(col, value); data.set(col, value);
} }
public double getDouble(int col, int srcType) { public double getDouble(int col, int nativeType) {
Object obj = data.get(col); Object obj = data.get(col - 1);
if (obj == null)
return NullType.getDoubleNull();
switch (srcType) { switch (nativeType) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return Boolean.TRUE.equals(obj) ? 1 : 0; return Boolean.TRUE.equals(obj) ? 1 : 0;
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
...@@ -242,16 +323,46 @@ public class TSDBResultSetRowData { ...@@ -242,16 +323,46 @@ public class TSDBResultSetRowData {
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
return (Long) obj; return (Long) obj;
default:
return NullType.getDoubleNull();
}
} }
return 0; /**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setStringValue(int colIndex, String value) {
data.set(colIndex - 1, value);
} }
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setString(int col, String value) { public void setString(int col, String value) {
data.set(col, value); // TODO:
// !!!NOTE!!!
// this is very confusing problem which related to JNI-method implementation,
// the JNI method return a String(encoded in UTF) for BINARY value, which means the JNI method will invoke
// this setString(int, String) to handle BINARY value, we need to build a byte[] with default charsetEncoding
data.set(col, value == null ? null : value.getBytes());
} }
/**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setByteArrayValue(int colIndex, byte[] value) {
setByteArray(colIndex - 1, value);
}
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setByteArray(int col, byte[] value) { public void setByteArray(int col, byte[] value) {
// TODO:
// !!!NOTE!!!
// this is very confusing problem which related to JNI-method implementation,
// the JNI method return a byte[] for NCHAR value, which means the JNI method will invoke
// this setByteArr(int, byte[]) to handle NCHAR value, we need to build a String with charsetEncoding by TaosGlobalConfig
try { try {
data.set(col, new String(value, TaosGlobalConfig.getCharset())); data.set(col, new String(value, TaosGlobalConfig.getCharset()));
} catch (Exception e) { } catch (Exception e) {
...@@ -259,47 +370,56 @@ public class TSDBResultSetRowData { ...@@ -259,47 +370,56 @@ public class TSDBResultSetRowData {
} }
} }
/** public String getString(int col, int nativeType) {
* The original type may not be a string type, but will be converted to by calling this method Object obj = data.get(col - 1);
* if (obj == null)
* @param col column index return null;
* @return
*/ switch (nativeType) {
public String getString(int col, int srcType) {
switch (srcType) {
case TSDBConstants.TSDB_DATA_TYPE_BINARY:
case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
return (String) data.get(col);
case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: { case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: {
Byte value = new Byte(String.valueOf(data.get(col))); Byte value = new Byte(String.valueOf(obj));
if (value >= 0) if (value >= 0)
return value.toString(); return value.toString();
return Integer.toString(value & 0xff); return Integer.toString(value & 0xff);
} }
case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: { case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: {
Short value = new Short(String.valueOf(data.get(col))); Short value = new Short(String.valueOf(obj));
if (value >= 0) if (value >= 0)
return value.toString(); return value.toString();
return Integer.toString(value & 0xffff); return Integer.toString(value & 0xffff);
} }
case TSDBConstants.TSDB_DATA_TYPE_UINT: { case TSDBConstants.TSDB_DATA_TYPE_UINT: {
Integer value = new Integer(String.valueOf(data.get(col))); Integer value = new Integer(String.valueOf(obj));
if (value >= 0) if (value >= 0)
return value.toString(); return value.toString();
return Long.toString(value & 0xffffffffl); return Long.toString(value & 0xffffffffl);
} }
case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: {
Long value = new Long(String.valueOf(data.get(col))); Long value = new Long(String.valueOf(obj));
if (value >= 0) if (value >= 0)
return value.toString(); return value.toString();
long lowValue = value & 0x7fffffffffffffffL; long lowValue = value & 0x7fffffffffffffffL;
return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1)).toString(); return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1)).toString();
} }
case TSDBConstants.TSDB_DATA_TYPE_BINARY:
return new String((byte[]) obj);
case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
return (String) obj;
default: default:
return String.valueOf(data.get(col)); return String.valueOf(obj);
}
} }
/**
* $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api
*/
public void setTimestampValue(int colIndex, long value) {
setTimestamp(colIndex - 1, value);
} }
/**
* !!! this method is invoked by JNI method and the index start from 0 in C implementations
*/
public void setTimestamp(int col, long ts) { public void setTimestamp(int col, long ts) {
//TODO: this implementation contains logical error //TODO: this implementation contains logical error
// when precision is us the (long ts) is 16 digital number // when precision is us the (long ts) is 16 digital number
...@@ -316,28 +436,20 @@ public class TSDBResultSetRowData { ...@@ -316,28 +436,20 @@ public class TSDBResultSetRowData {
} }
} }
public Timestamp getTimestamp(int col) { public Timestamp getTimestamp(int col, int nativeType) {
return (Timestamp) data.get(col); Object obj = data.get(col - 1);
} if (obj == null)
return null;
public Object get(int col) { switch (nativeType) {
return data.get(col); case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
} return new Timestamp((Long) obj);
default:
public int getColSize() { return (Timestamp) obj;
return colSize;
} }
private void setColSize(int colSize) {
this.colSize = colSize;
this.clear();
} }
public ArrayList<Object> getData() { public Object getObject(int col) {
return data; return data.get(col - 1);
} }
public void setData(ArrayList<Object> data) {
this.data = (ArrayList<Object>) data.clone();
}
} }
...@@ -32,14 +32,15 @@ public class TSDBStatement extends AbstractStatement { ...@@ -32,14 +32,15 @@ public class TSDBStatement extends AbstractStatement {
} }
public ResultSet executeQuery(String sql) throws SQLException { public ResultSet executeQuery(String sql) throws SQLException {
// check if closed
if (isClosed()) { if (isClosed()) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
} }
//TODO:
//TODO: 如果在executeQuery方法中执行insert语句,那么先执行了SQL,再通过pSql来检查是否为一个insert语句,但这个insert SQL已经执行成功了 // this is an unreasonable implementation, if the paratemer is a insert statement,
// the JNI connector will execute the sql at first and return a pointer: pSql,
// execute query // we use this pSql and invoke the isUpdateQuery(long pSql) method to decide .
// but the insert sql is already executed in database.
//execute query
long pSql = this.connection.getConnector().executeQuery(sql); long pSql = this.connection.getConnector().executeQuery(sql);
// if pSql is create/insert/update/delete/alter SQL // if pSql is create/insert/update/delete/alter SQL
if (this.connection.getConnector().isUpdateQuery(pSql)) { if (this.connection.getConnector().isUpdateQuery(pSql)) {
......
...@@ -95,16 +95,7 @@ public class Utils { ...@@ -95,16 +95,7 @@ public class Utils {
public static String getNativeSql(String rawSql, Object[] parameters) { public static String getNativeSql(String rawSql, Object[] parameters) {
// toLowerCase // toLowerCase
String preparedSql = rawSql.trim().toLowerCase(); String preparedSql = rawSql.trim().toLowerCase();
String[] clause = new String[]{"values\\s*\\(.*?\\)", "tags\\s*\\(.*?\\)", "where\\s*.*"};
String[] clause = new String[0];
if (SqlSyntaxValidator.isInsertSql(preparedSql)) {
// insert or import
clause = new String[]{"values\\s*\\(.*?\\)", "tags\\s*\\(.*?\\)"};
}
if (SqlSyntaxValidator.isSelectSql(preparedSql)) {
// select
clause = new String[]{"where\\s*.*"};
}
Map<Integer, Integer> placeholderPositions = new HashMap<>(); Map<Integer, Integer> placeholderPositions = new HashMap<>();
RangeSet<Integer> clauseRangeSet = TreeRangeSet.create(); RangeSet<Integer> clauseRangeSet = TreeRangeSet.create();
findPlaceholderPosition(preparedSql, placeholderPositions); findPlaceholderPosition(preparedSql, placeholderPositions);
......
...@@ -32,20 +32,34 @@ public class TSDBConnectionTest { ...@@ -32,20 +32,34 @@ public class TSDBConnectionTest {
} }
@Test @Test
public void subscribe() { public void runSubscribe() {
try { try {
// given
TSDBConnection unwrap = conn.unwrap(TSDBConnection.class); TSDBConnection unwrap = conn.unwrap(TSDBConnection.class);
TSDBSubscribe subscribe = unwrap.subscribe("topic1", "select * from log.log", false); TSDBSubscribe subscribe = unwrap.subscribe("topic1", "select * from log.log", false);
// when
TSDBResultSet rs = subscribe.consume(); TSDBResultSet rs = subscribe.consume();
ResultSetMetaData metaData = rs.getMetaData(); ResultSetMetaData metaData = rs.getMetaData();
for (int count = 0; count < 10 && rs.next(); count++) {
for (int i = 1; i <= metaData.getColumnCount(); i++) { // then
String value = rs.getString(i);
System.out.print(metaData.getColumnLabel(i) + ":" + value + "\t");
}
System.out.println();
}
Assert.assertNotNull(rs); Assert.assertNotNull(rs);
Assert.assertEquals(4, metaData.getColumnCount());
Assert.assertEquals("ts", metaData.getColumnLabel(1));
Assert.assertEquals("level", metaData.getColumnLabel(2));
Assert.assertEquals("content", metaData.getColumnLabel(3));
Assert.assertEquals("ipaddr", metaData.getColumnLabel(4));
rs.next();
// row 1
{
Assert.assertNotNull(rs.getTimestamp(1));
Assert.assertNotNull(rs.getTimestamp("ts"));
Assert.assertNotNull(rs.getByte(2));
Assert.assertNotNull(rs.getByte("level"));
Assert.assertNotNull(rs.getString(3));
Assert.assertNotNull(rs.getString("content"));
Assert.assertNotNull(rs.getString(4));
Assert.assertNotNull(rs.getString("ipaddr"));
}
subscribe.close(false); subscribe.close(false);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -7,9 +7,11 @@ import java.util.Properties; ...@@ -7,9 +7,11 @@ import java.util.Properties;
public class TSDBDatabaseMetaDataTest { public class TSDBDatabaseMetaDataTest {
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private static final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
private static Connection connection; private static Connection connection;
private static TSDBDatabaseMetaData metaData; private static TSDBDatabaseMetaData metaData;
@Test @Test
public void unwrap() throws SQLException { public void unwrap() throws SQLException {
TSDBDatabaseMetaData unwrap = metaData.unwrap(TSDBDatabaseMetaData.class); TSDBDatabaseMetaData unwrap = metaData.unwrap(TSDBDatabaseMetaData.class);
...@@ -33,7 +35,7 @@ public class TSDBDatabaseMetaDataTest { ...@@ -33,7 +35,7 @@ public class TSDBDatabaseMetaDataTest {
@Test @Test
public void getURL() throws SQLException { public void getURL() throws SQLException {
Assert.assertEquals("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", metaData.getURL()); Assert.assertEquals(url, metaData.getURL());
} }
@Test @Test
...@@ -627,17 +629,32 @@ public class TSDBDatabaseMetaDataTest { ...@@ -627,17 +629,32 @@ public class TSDBDatabaseMetaDataTest {
@Test @Test
public void getTables() throws SQLException { public void getTables() throws SQLException {
System.out.println("****************************************************"); ResultSet rs = metaData.getTables("log", "", null, null);
ResultSet tables = metaData.getTables("log", "", null, null); ResultSetMetaData meta = rs.getMetaData();
ResultSetMetaData metaData = tables.getMetaData(); Assert.assertNotNull(rs);
while (tables.next()) { rs.next();
System.out.print(metaData.getColumnLabel(1) + ":" + tables.getString(1) + "\t"); {
System.out.print(metaData.getColumnLabel(3) + ":" + tables.getString(3) + "\t"); // TABLE_CAT
System.out.print(metaData.getColumnLabel(4) + ":" + tables.getString(4) + "\t"); Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
System.out.print(metaData.getColumnLabel(5) + ":" + tables.getString(5) + "\n"); Assert.assertEquals("log", rs.getString(1));
Assert.assertEquals("log", rs.getString("TABLE_CAT"));
// TABLE_SCHEM
Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2));
Assert.assertEquals(null, rs.getString(2));
Assert.assertEquals(null, rs.getString("TABLE_SCHEM"));
// TABLE_NAME
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertNotNull(rs.getString(3));
Assert.assertNotNull(rs.getString("TABLE_NAME"));
// TABLE_TYPE
Assert.assertEquals("TABLE_TYPE", meta.getColumnLabel(4));
Assert.assertEquals("TABLE", rs.getString(4));
Assert.assertEquals("TABLE", rs.getString("TABLE_TYPE"));
// REMARKS
Assert.assertEquals("REMARKS", meta.getColumnLabel(5));
Assert.assertEquals("", rs.getString(5));
Assert.assertEquals("", rs.getString("REMARKS"));
} }
System.out.println();
Assert.assertNotNull(tables);
} }
@Test @Test
...@@ -647,46 +664,130 @@ public class TSDBDatabaseMetaDataTest { ...@@ -647,46 +664,130 @@ public class TSDBDatabaseMetaDataTest {
@Test @Test
public void getCatalogs() throws SQLException { public void getCatalogs() throws SQLException {
System.out.println("****************************************************"); ResultSet rs = metaData.getCatalogs();
ResultSetMetaData meta = rs.getMetaData();
ResultSet catalogs = metaData.getCatalogs(); rs.next();
ResultSetMetaData meta = catalogs.getMetaData(); {
while (catalogs.next()) { // TABLE_CAT
for (int i = 1; i <= meta.getColumnCount(); i++) { Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
System.out.print(meta.getColumnLabel(i) + ": " + catalogs.getString(i)); Assert.assertNotNull(rs.getString(1));
} Assert.assertNotNull(rs.getString("TABLE_CAT"));
System.out.println();
} }
} }
@Test @Test
public void getTableTypes() throws SQLException { public void getTableTypes() throws SQLException {
System.out.println("****************************************************");
ResultSet tableTypes = metaData.getTableTypes(); ResultSet tableTypes = metaData.getTableTypes();
while (tableTypes.next()) { tableTypes.next();
System.out.println(tableTypes.getString("TABLE_TYPE")); // tableTypes: table
{
Assert.assertEquals("TABLE", tableTypes.getString(1));
Assert.assertEquals("TABLE", tableTypes.getString("TABLE_TYPE"));
}
tableTypes.next();
// tableTypes: stable
{
Assert.assertEquals("STABLE", tableTypes.getString(1));
Assert.assertEquals("STABLE", tableTypes.getString("TABLE_TYPE"));
} }
Assert.assertNotNull(metaData.getTableTypes());
} }
@Test @Test
public void getColumns() throws SQLException { public void getColumns() throws SQLException {
System.out.println("****************************************************"); // when
ResultSet columns = metaData.getColumns("log", "", "dn", ""); ResultSet columns = metaData.getColumns("log", "", "dn", "");
// then
ResultSetMetaData meta = columns.getMetaData(); ResultSetMetaData meta = columns.getMetaData();
while (columns.next()) { columns.next();
System.out.print(meta.getColumnLabel(1) + ": " + columns.getString(1) + "\t"); // column: 1
System.out.print(meta.getColumnLabel(3) + ": " + columns.getString(3) + "\t"); {
System.out.print(meta.getColumnLabel(4) + ": " + columns.getString(4) + "\t"); // TABLE_CAT
System.out.print(meta.getColumnLabel(5) + ": " + columns.getString(5) + "\t"); Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
System.out.print(meta.getColumnLabel(6) + ": " + columns.getString(6) + "\t"); Assert.assertEquals("log", columns.getString(1));
System.out.print(meta.getColumnLabel(7) + ": " + columns.getString(7) + "\t"); Assert.assertEquals("log", columns.getString("TABLE_CAT"));
System.out.print(meta.getColumnLabel(9) + ": " + columns.getString(9) + "\t"); // TABLE_NAME
System.out.print(meta.getColumnLabel(10) + ": " + columns.getString(10) + "\t"); Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
System.out.print(meta.getColumnLabel(11) + ": " + columns.getString(11) + "\n"); Assert.assertEquals("dn", columns.getString(3));
System.out.print(meta.getColumnLabel(12) + ": " + columns.getString(12) + "\n"); Assert.assertEquals("dn", columns.getString("TABLE_NAME"));
// COLUMN_NAME
Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4));
Assert.assertEquals("ts", columns.getString(4));
Assert.assertEquals("ts", columns.getString("COLUMN_NAME"));
// DATA_TYPE
Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5));
Assert.assertEquals(Types.TIMESTAMP, columns.getInt(5));
Assert.assertEquals(Types.TIMESTAMP, columns.getInt("DATA_TYPE"));
// TYPE_NAME
Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6));
Assert.assertEquals("TIMESTAMP", columns.getString(6));
Assert.assertEquals("TIMESTAMP", columns.getString("TYPE_NAME"));
// COLUMN_SIZE
Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7));
Assert.assertEquals(26, columns.getInt(7));
Assert.assertEquals(26, columns.getInt("COLUMN_SIZE"));
// DECIMAL_DIGITS
Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS"));
Assert.assertEquals(null, columns.getString(9));
Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS"));
// NUM_PREC_RADIX
Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10));
Assert.assertEquals(10, columns.getInt(10));
Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX"));
// NULLABLE
Assert.assertEquals("NULLABLE", meta.getColumnLabel(11));
Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt(11));
Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt("NULLABLE"));
// REMARKS
Assert.assertEquals("REMARKS", meta.getColumnLabel(12));
Assert.assertEquals(null, columns.getString(12));
Assert.assertEquals(null, columns.getString("REMARKS"));
}
columns.next();
// column: 2
{
// TABLE_CAT
Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
Assert.assertEquals("log", columns.getString(1));
Assert.assertEquals("log", columns.getString("TABLE_CAT"));
// TABLE_NAME
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertEquals("dn", columns.getString(3));
Assert.assertEquals("dn", columns.getString("TABLE_NAME"));
// COLUMN_NAME
Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4));
Assert.assertEquals("cpu_taosd", columns.getString(4));
Assert.assertEquals("cpu_taosd", columns.getString("COLUMN_NAME"));
// DATA_TYPE
Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5));
Assert.assertEquals(Types.FLOAT, columns.getInt(5));
Assert.assertEquals(Types.FLOAT, columns.getInt("DATA_TYPE"));
// TYPE_NAME
Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6));
Assert.assertEquals("FLOAT", columns.getString(6));
Assert.assertEquals("FLOAT", columns.getString("TYPE_NAME"));
// COLUMN_SIZE
Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7));
Assert.assertEquals(12, columns.getInt(7));
Assert.assertEquals(12, columns.getInt("COLUMN_SIZE"));
// DECIMAL_DIGITS
Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS"));
Assert.assertEquals(null, columns.getString(9));
Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS"));
// NUM_PREC_RADIX
Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10));
Assert.assertEquals(10, columns.getInt(10));
Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX"));
// NULLABLE
Assert.assertEquals("NULLABLE", meta.getColumnLabel(11));
Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt(11));
Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt("NULLABLE"));
// REMARKS
Assert.assertEquals("REMARKS", meta.getColumnLabel(12));
Assert.assertEquals(null, columns.getString(12));
} }
} }
...@@ -712,17 +813,35 @@ public class TSDBDatabaseMetaDataTest { ...@@ -712,17 +813,35 @@ public class TSDBDatabaseMetaDataTest {
@Test @Test
public void getPrimaryKeys() throws SQLException { public void getPrimaryKeys() throws SQLException {
System.out.println("****************************************************");
ResultSet rs = metaData.getPrimaryKeys("log", "", "dn1"); ResultSet rs = metaData.getPrimaryKeys("log", "", "dn1");
while (rs.next()) { ResultSetMetaData meta = rs.getMetaData();
System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); rs.next();
System.out.println("COLUMN_NAME: " + rs.getString("COLUMN_NAME")); {
System.out.println("KEY_SEQ: " + rs.getString("KEY_SEQ")); // TABLE_CAT
System.out.println("PK_NAME: " + rs.getString("PK_NAME")); Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
Assert.assertEquals("log", rs.getString(1));
Assert.assertEquals("log", rs.getString("TABLE_CAT"));
// TABLE_SCHEM
Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2));
Assert.assertEquals(null, rs.getString(2));
Assert.assertEquals(null, rs.getString("TABLE_SCHEM"));
// TABLE_NAME
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertEquals("dn1", rs.getString(3));
Assert.assertEquals("dn1", rs.getString("TABLE_NAME"));
// COLUMN_NAME
Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4));
Assert.assertEquals("ts", rs.getString(4));
Assert.assertEquals("ts", rs.getString("COLUMN_NAME"));
// KEY_SEQ
Assert.assertEquals("KEY_SEQ", meta.getColumnLabel(5));
Assert.assertEquals(1, rs.getShort(5));
Assert.assertEquals(1, rs.getShort("KEY_SEQ"));
// DATA_TYPE
Assert.assertEquals("PK_NAME", meta.getColumnLabel(6));
Assert.assertEquals("ts", rs.getString(6));
Assert.assertEquals("ts", rs.getString("PK_NAME"));
} }
Assert.assertNotNull(rs);
} }
@Test @Test
...@@ -847,14 +966,27 @@ public class TSDBDatabaseMetaDataTest { ...@@ -847,14 +966,27 @@ public class TSDBDatabaseMetaDataTest {
@Test @Test
public void getSuperTables() throws SQLException { public void getSuperTables() throws SQLException {
System.out.println("****************************************************");
ResultSet rs = metaData.getSuperTables("log", "", "dn1"); ResultSet rs = metaData.getSuperTables("log", "", "dn1");
while (rs.next()) { ResultSetMetaData meta = rs.getMetaData();
System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); rs.next();
System.out.println("SUPERTABLE_NAME: " + rs.getString("SUPERTABLE_NAME")); {
// TABLE_CAT
Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
Assert.assertEquals("log", rs.getString(1));
Assert.assertEquals("log", rs.getString("TABLE_CAT"));
// TABLE_CAT
Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2));
Assert.assertEquals(null, rs.getString(2));
Assert.assertEquals(null, rs.getString("TABLE_SCHEM"));
// TABLE_CAT
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertEquals("dn1", rs.getString(3));
Assert.assertEquals("dn1", rs.getString("TABLE_NAME"));
// TABLE_CAT
Assert.assertEquals("SUPERTABLE_NAME", meta.getColumnLabel(4));
Assert.assertEquals("dn", rs.getString(4));
Assert.assertEquals("dn", rs.getString("SUPERTABLE_NAME"));
} }
Assert.assertNotNull(rs);
} }
@Test @Test
...@@ -951,15 +1083,12 @@ public class TSDBDatabaseMetaDataTest { ...@@ -951,15 +1083,12 @@ public class TSDBDatabaseMetaDataTest {
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", properties); connection = DriverManager.getConnection(url, properties);
metaData = connection.getMetaData().unwrap(TSDBDatabaseMetaData.class); metaData = connection.getMetaData().unwrap(TSDBDatabaseMetaData.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -45,9 +45,9 @@ public class TSDBJNIConnectorTest { ...@@ -45,9 +45,9 @@ public class TSDBJNIConnectorTest {
rowData = new TSDBResultSetRowData(columnSize); rowData = new TSDBResultSetRowData(columnSize);
// iterate resultSet // iterate resultSet
for (int i = 0; next(connector, pSql); i++) { for (int i = 0; next(connector, pSql); i++) {
System.out.println("col[" + i + "] size: " + rowData.getColSize()); // System.out.println("col[" + i + "] size: " + rowData.getColSize());
rowData.getData().stream().forEach(col -> System.out.print(col + "\t")); // rowData.getData().stream().forEach(col -> System.out.print(col + "\t"));
System.out.println(); // System.out.println();
} }
// close resultSet // close resultSet
code = connector.freeResultSet(pSql); code = connector.freeResultSet(pSql);
......
...@@ -54,16 +54,17 @@ public class TSDBParameterMetaDataTest { ...@@ -54,16 +54,17 @@ public class TSDBParameterMetaDataTest {
@Test @Test
public void getPrecision() throws SQLException { public void getPrecision() throws SQLException {
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(1)); //create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64))
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(2)); Assert.assertEquals(TSDBConstants.TIMESTAMP_MS_PRECISION, parameterMetaData_insert.getPrecision(1));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(3)); Assert.assertEquals(TSDBConstants.INT_PRECISION, parameterMetaData_insert.getPrecision(2));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(4)); Assert.assertEquals(TSDBConstants.BIGINT_PRECISION, parameterMetaData_insert.getPrecision(3));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(5)); Assert.assertEquals(TSDBConstants.FLOAT_PRECISION, parameterMetaData_insert.getPrecision(4));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(6)); Assert.assertEquals(TSDBConstants.DOUBLE_PRECISION, parameterMetaData_insert.getPrecision(5));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(7)); Assert.assertEquals(TSDBConstants.SMALLINT_PRECISION, parameterMetaData_insert.getPrecision(6));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(8)); Assert.assertEquals(TSDBConstants.TINYINT_PRECISION, parameterMetaData_insert.getPrecision(7));
Assert.assertEquals(5, parameterMetaData_insert.getPrecision(9)); Assert.assertEquals(TSDBConstants.BOOLEAN_PRECISION, parameterMetaData_insert.getPrecision(8));
Assert.assertEquals(5, parameterMetaData_insert.getPrecision(10)); Assert.assertEquals("hello".getBytes().length, parameterMetaData_insert.getPrecision(9));
Assert.assertEquals("涛思数据".length(), parameterMetaData_insert.getPrecision(10));
} }
@Test @Test
...@@ -71,8 +72,8 @@ public class TSDBParameterMetaDataTest { ...@@ -71,8 +72,8 @@ public class TSDBParameterMetaDataTest {
Assert.assertEquals(0, parameterMetaData_insert.getScale(1)); Assert.assertEquals(0, parameterMetaData_insert.getScale(1));
Assert.assertEquals(0, parameterMetaData_insert.getScale(2)); Assert.assertEquals(0, parameterMetaData_insert.getScale(2));
Assert.assertEquals(0, parameterMetaData_insert.getScale(3)); Assert.assertEquals(0, parameterMetaData_insert.getScale(3));
Assert.assertEquals(0, parameterMetaData_insert.getScale(4)); Assert.assertEquals(31, parameterMetaData_insert.getScale(4));
Assert.assertEquals(0, parameterMetaData_insert.getScale(5)); Assert.assertEquals(31, parameterMetaData_insert.getScale(5));
Assert.assertEquals(0, parameterMetaData_insert.getScale(6)); Assert.assertEquals(0, parameterMetaData_insert.getScale(6));
Assert.assertEquals(0, parameterMetaData_insert.getScale(7)); Assert.assertEquals(0, parameterMetaData_insert.getScale(7));
Assert.assertEquals(0, parameterMetaData_insert.getScale(8)); Assert.assertEquals(0, parameterMetaData_insert.getScale(8));
...@@ -124,10 +125,16 @@ public class TSDBParameterMetaDataTest { ...@@ -124,10 +125,16 @@ public class TSDBParameterMetaDataTest {
@Test @Test
public void getParameterMode() throws SQLException { public void getParameterMode() throws SQLException {
for (int i = 1; i <= parameterMetaData_insert.getParameterCount(); i++) { Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(1));
int parameterMode = parameterMetaData_insert.getParameterMode(i); Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(2));
Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMode); Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(3));
} Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(4));
Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(5));
Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(6));
Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(7));
Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(8));
Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(9));
Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(10));
} }
@Test @Test
...@@ -144,7 +151,6 @@ public class TSDBParameterMetaDataTest { ...@@ -144,7 +151,6 @@ public class TSDBParameterMetaDataTest {
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
try { try {
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()) { try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test_pstmt"); stmt.execute("drop database if exists test_pstmt");
...@@ -164,7 +170,7 @@ public class TSDBParameterMetaDataTest { ...@@ -164,7 +170,7 @@ public class TSDBParameterMetaDataTest {
pstmt_insert.setObject(7, Byte.MAX_VALUE); pstmt_insert.setObject(7, Byte.MAX_VALUE);
pstmt_insert.setObject(8, true); pstmt_insert.setObject(8, true);
pstmt_insert.setObject(9, "hello".getBytes()); pstmt_insert.setObject(9, "hello".getBytes());
pstmt_insert.setObject(10, "Hello"); pstmt_insert.setObject(10, "涛思数据");
parameterMetaData_insert = pstmt_insert.getParameterMetaData(); parameterMetaData_insert = pstmt_insert.getParameterMetaData();
pstmt_select = conn.prepareStatement(sql_select); pstmt_select = conn.prepareStatement(sql_select);
...@@ -173,7 +179,7 @@ public class TSDBParameterMetaDataTest { ...@@ -173,7 +179,7 @@ public class TSDBParameterMetaDataTest {
pstmt_select.setInt(3, 0); pstmt_select.setInt(3, 0);
parameterMetaData_select = pstmt_select.getParameterMetaData(); parameterMetaData_select = pstmt_select.getParameterMetaData();
} catch (ClassNotFoundException | SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
......
package com.taosdata.jdbc; package com.taosdata.jdbc;
import org.junit.AfterClass; import org.junit.*;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.math.BigDecimal;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
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 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 f1 >= ?";
private static final String sql_select = "select * from t1 where ts > ? and ts <= ? and f1 >= ?";
private static PreparedStatement pstmt_select; private PreparedStatement pstmt_insert;
private PreparedStatement pstmt_select;
//create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))
@Test @Test
public void executeQuery() throws SQLException { public void executeQuery() throws SQLException {
long end = System.currentTimeMillis(); // given
long start = end - 1000 * 60 * 60; long ts = System.currentTimeMillis();
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setInt(2, 2);
pstmt_insert.setLong(3, 3l);
pstmt_insert.setFloat(4, 3.14f);
pstmt_insert.setDouble(5, 3.1415);
pstmt_insert.setShort(6, (short) 6);
pstmt_insert.setByte(7, (byte) 7);
pstmt_insert.setBoolean(8, true);
pstmt_insert.setBytes(9, "abc".getBytes());
pstmt_insert.setString(10, "涛思数据");
pstmt_insert.executeUpdate();
long start = ts - 1000 * 60 * 60;
long end = ts + 1000 * 60 * 60;
pstmt_select.setTimestamp(1, new Timestamp(start)); pstmt_select.setTimestamp(1, new Timestamp(start));
pstmt_select.setTimestamp(2, new Timestamp(end)); pstmt_select.setTimestamp(2, new Timestamp(end));
pstmt_select.setInt(3, 0); pstmt_select.setInt(3, 0);
// when
ResultSet rs = pstmt_select.executeQuery(); ResultSet rs = pstmt_select.executeQuery();
ResultSetMetaData meta = rs.getMetaData();
rs.next();
// then
assertMetaData(meta);
{
Assert.assertNotNull(rs); Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(2, rs.getInt(2));
Assert.assertEquals(2, rs.getInt("f1"));
Assert.assertEquals(3l, rs.getLong(3));
Assert.assertEquals(3l, rs.getLong("f2"));
Assert.assertEquals(3.14f, rs.getFloat(4), 0.0);
Assert.assertEquals(3.14f, rs.getFloat("f3"), 0.0);
Assert.assertEquals(3.1415, rs.getDouble(5), 0.0);
Assert.assertEquals(3.1415, rs.getDouble("f4"), 0.0);
Assert.assertEquals((short) 6, rs.getShort(6));
Assert.assertEquals((short) 6, rs.getShort("f5"));
Assert.assertEquals((byte) 7, rs.getByte(7));
Assert.assertEquals((byte) 7, rs.getByte("f6"));
Assert.assertTrue(rs.getBoolean(8));
Assert.assertTrue(rs.getBoolean("f7"));
Assert.assertArrayEquals("abc".getBytes(), rs.getBytes(9));
Assert.assertArrayEquals("abc".getBytes(), rs.getBytes("f8"));
Assert.assertEquals("涛思数据", rs.getString(10));
Assert.assertEquals("涛思数据", rs.getString("f9"));
}
}
private void assertMetaData(ResultSetMetaData meta) throws SQLException {
Assert.assertEquals(10, meta.getColumnCount());
Assert.assertEquals("ts", meta.getColumnLabel(1));
Assert.assertEquals("f1", meta.getColumnLabel(2));
Assert.assertEquals("f2", meta.getColumnLabel(3));
Assert.assertEquals("f3", meta.getColumnLabel(4));
Assert.assertEquals("f4", meta.getColumnLabel(5));
Assert.assertEquals("f5", meta.getColumnLabel(6));
Assert.assertEquals("f6", meta.getColumnLabel(7));
Assert.assertEquals("f7", meta.getColumnLabel(8));
Assert.assertEquals("f8", meta.getColumnLabel(9));
Assert.assertEquals("f9", meta.getColumnLabel(10));
}
@Test
public void setNullForTimestamp() throws SQLException {
// given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(2, Types.INTEGER);
int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData(); ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) { assertMetaData(meta);
for (int i = 1; i <= meta.getColumnCount(); i++) { rs.next();
System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t"); assertAllNullExceptTimestamp(rs, ts);
} }
System.out.println();
} }
private void assertAllNullExceptTimestamp(ResultSet rs, long ts) throws SQLException {
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(0, rs.getInt(2));
Assert.assertEquals(0, rs.getInt("f1"));
Assert.assertEquals(0, rs.getLong(3));
Assert.assertEquals(0, rs.getLong("f2"));
Assert.assertEquals(0, rs.getFloat(4), 0.0);
Assert.assertEquals(0, rs.getFloat("f3"), 0.0);
Assert.assertEquals(0, rs.getDouble(5), 0.0);
Assert.assertEquals(0, rs.getDouble("f4"), 0.0);
Assert.assertEquals(0, rs.getShort(6));
Assert.assertEquals(0, rs.getShort("f5"));
Assert.assertEquals(0, rs.getByte(7));
Assert.assertEquals(0, rs.getByte("f6"));
Assert.assertFalse(rs.getBoolean(8));
Assert.assertFalse(rs.getBoolean("f7"));
Assert.assertNull(rs.getBytes(9));
Assert.assertNull(rs.getBytes("f8"));
Assert.assertNull(rs.getString(10));
Assert.assertNull(rs.getString("f9"));
} }
@Test @Test
public void executeUpdate() throws SQLException { public void setNullForInteger() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
pstmt_insert.setFloat(4, 3.14f); long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(3, Types.BIGINT);
int result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
} }
@Test @Test
public void setNull() throws SQLException { public void setNullForFloat() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
pstmt_insert.setNull(2, Types.INTEGER); long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(4, Types.FLOAT);
int result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, result);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // then
pstmt_insert.setNull(3, Types.BIGINT);
result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
}
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); @Test
pstmt_insert.setNull(4, Types.FLOAT); public void setNullForDouble() throws SQLException {
result = pstmt_insert.executeUpdate(); // given
Assert.assertEquals(1, result); long ts = System.currentTimeMillis();
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(5, Types.DOUBLE); pstmt_insert.setNull(5, Types.DOUBLE);
result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
}
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); @Test
public void setNullForSmallInt() throws SQLException {
// given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(6, Types.SMALLINT); pstmt_insert.setNull(6, Types.SMALLINT);
result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
}
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); @Test
public void setNullForTinyInt() throws SQLException {
// given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(7, Types.TINYINT); pstmt_insert.setNull(7, Types.TINYINT);
result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
}
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); @Test
public void setNullForBoolean() throws SQLException {
// given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(8, Types.BOOLEAN); pstmt_insert.setNull(8, Types.BOOLEAN);
result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
}
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); @Test
public void setNullForBinary() throws SQLException {
// given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(9, Types.BINARY); pstmt_insert.setNull(9, Types.BINARY);
result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
}
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); @Test
public void setNullForNchar() throws SQLException {
// given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setNull(10, Types.NCHAR); pstmt_insert.setNull(10, Types.NCHAR);
result = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, result);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // then
pstmt_insert.setNull(10, Types.OTHER);
result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, result); Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
assertAllNullExceptTimestamp(rs, ts);
}
} }
@Test @Test
public void setBoolean() throws SQLException { public void setBoolean() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setBoolean(8, true); pstmt_insert.setBoolean(8, true);
int ret = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertTrue(rs.getBoolean(8));
Assert.assertTrue(rs.getBoolean("f7"));
}
}
} }
@Test @Test
public void setByte() throws SQLException { public void setByte() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setByte(7, (byte) 0x001); pstmt_insert.setByte(7, (byte) 0x001);
int ret = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals((byte) 0x001, rs.getByte(7));
Assert.assertEquals((byte) 0x001, rs.getByte("f6"));
}
}
} }
@Test @Test
public void setShort() throws SQLException { public void setShort() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setShort(6, (short) 2); pstmt_insert.setShort(6, (short) 2);
int ret = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals((short) 2, rs.getByte(6));
Assert.assertEquals((short) 2, rs.getByte("f5"));
}
}
} }
@Test @Test
public void setInt() throws SQLException { public void setInt() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setInt(2, 10086); pstmt_insert.setInt(2, 10086);
int ret = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals(10086, rs.getInt(2));
Assert.assertEquals(10086, rs.getInt("f1"));
}
}
} }
@Test @Test
public void setLong() throws SQLException { public void setLong() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setLong(3, Long.MAX_VALUE); pstmt_insert.setLong(3, Long.MAX_VALUE);
int ret = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals(Long.MAX_VALUE, rs.getLong(3));
Assert.assertEquals(Long.MAX_VALUE, rs.getLong("f2"));
}
}
} }
@Test @Test
public void setFloat() throws SQLException { public void setFloat() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setFloat(4, 3.14f); pstmt_insert.setFloat(4, 3.14f);
int ret = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals(3.14f, rs.getFloat(4), 0.0f);
Assert.assertEquals(3.14f, rs.getFloat("f3"), 0.0f);
}
}
} }
@Test @Test
public void setDouble() throws SQLException { public void setDouble() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setDouble(5, 3.14444); pstmt_insert.setDouble(5, 3.14444);
int ret = pstmt_insert.executeUpdate(); int result = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
}
@Test(expected = SQLFeatureNotSupportedException.class) // then
public void setBigDecimal() throws SQLException { Assert.assertEquals(1, result);
pstmt_insert.setBigDecimal(1, null); try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals(3.14444, rs.getDouble(5), 0.0);
Assert.assertEquals(3.14444, rs.getDouble("f4"), 0.0);
}
}
} }
@Test @Test
public void setString() throws SQLException { public void setBigDecimal() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
pstmt_insert.setString(10, "aaaa"); long ts = System.currentTimeMillis();
boolean execute = pstmt_insert.execute(); BigDecimal bigDecimal = new BigDecimal(3.14444);
Assert.assertFalse(execute);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // when
pstmt_insert.setString(10, new Person("john", 33, true).toString()); pstmt_insert.setTimestamp(1, new Timestamp(ts));
Assert.assertFalse(pstmt_insert.execute()); pstmt_insert.setBigDecimal(5, bigDecimal);
int result = pstmt_insert.executeUpdate();
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // then
pstmt_insert.setString(10, new Person("john", 33, true).toString().replaceAll("'", "\"")); Assert.assertEquals(1, result);
Assert.assertFalse(pstmt_insert.execute()); try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals(3.14444, rs.getDouble(5), 0.0);
Assert.assertEquals(3.14444, rs.getDouble("f4"), 0.0);
}
}
} }
class Person { @Test
String name; public void setString() throws SQLException {
int age; // given
boolean sex; long ts = System.currentTimeMillis();
String f9 = "{\"name\": \"john\", \"age\": 10, \"address\": \"192.168.1.100\"}";
public Person(String name, int age, boolean sex) { // when
this.name = name; pstmt_insert.setTimestamp(1, new Timestamp(ts));
this.age = age; pstmt_insert.setString(10, f9);
this.sex = sex; int result = pstmt_insert.executeUpdate();
}
@Override // then
public String toString() { Assert.assertEquals(1, result);
return "Person{" + try (Statement stmt = conn.createStatement()) {
"name='" + name + '\'' + ResultSet rs = stmt.executeQuery("select * from t1");
", age=" + age + ResultSetMetaData meta = rs.getMetaData();
", sex=" + sex + assertMetaData(meta);
'}'; rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertEquals(f9, rs.getString(10));
Assert.assertEquals(f9, rs.getString("f9"));
}
} }
} }
@Test @Test
public void setBytes() throws SQLException, IOException { public void setBytes() throws SQLException, IOException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
byte[] f8 = "{\"name\": \"john\", \"age\": 10, \"address\": \"192.168.1.100\"}".getBytes();
// ByteArrayOutputStream baos = new ByteArrayOutputStream(); // when
// ObjectOutputStream oos = new ObjectOutputStream(baos); pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
// oos.writeObject(new Person("john", 33, true)); pstmt_insert.setBytes(9, f8);
// oos.flush(); int result = pstmt_insert.executeUpdate();
// byte[] bytes = baos.toByteArray();
// pstmt_insert.setBytes(9, bytes);
pstmt_insert.setBytes(9, new Person("john", 33, true).toString().getBytes()); // then
int ret = pstmt_insert.executeUpdate(); Assert.assertEquals(1, result);
Assert.assertEquals(1, ret); try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
Assert.assertArrayEquals(f8, rs.getBytes(9));
Assert.assertArrayEquals(f8, rs.getBytes("f8"));
}
}
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test
public void setDate() throws SQLException { public void setDate() throws SQLException {
pstmt_insert.setDate(1, new Date(System.currentTimeMillis())); // given
long ts = new java.util.Date().getTime();
// when
pstmt_insert.setDate(1, new Date(ts));
int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
}
}
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test
public void setTime() throws SQLException { public void setTime() throws SQLException {
pstmt_insert.setTime(1, new Time(System.currentTimeMillis())); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTime(1, new Time(ts));
int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
}
}
} }
@Test @Test
public void setTimestamp() throws SQLException { public void setTimestamp() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); // given
int ret = pstmt_insert.executeUpdate(); long ts = System.currentTimeMillis();
Assert.assertEquals(1, ret);
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
int result = pstmt_insert.executeUpdate();
// then
Assert.assertEquals(1, result);
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from t1");
ResultSetMetaData meta = rs.getMetaData();
assertMetaData(meta);
rs.next();
{
Assert.assertNotNull(rs);
Assert.assertEquals(ts, rs.getTimestamp(1).getTime());
Assert.assertEquals(ts, rs.getTimestamp("ts").getTime());
}
}
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test(expected = SQLFeatureNotSupportedException.class)
...@@ -240,72 +652,6 @@ public class TSDBPreparedStatementTest { ...@@ -240,72 +652,6 @@ public class TSDBPreparedStatementTest {
pstmt_insert.setBinaryStream(1, null); pstmt_insert.setBinaryStream(1, null);
} }
@Test
public void clearParameters() throws SQLException {
pstmt_insert.clearParameters();
}
@Test
public void setObject() throws SQLException {
pstmt_insert.setObject(1, new Timestamp(System.currentTimeMillis()));
int ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(2, 111);
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(3, Long.MAX_VALUE);
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(4, 3.14159265354f);
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(5, Double.MAX_VALUE);
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(6, Short.MAX_VALUE);
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(7, Byte.MAX_VALUE);
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(8, true);
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(9, "hello".getBytes());
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt_insert.setObject(10, "Hello");
ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
}
@Test
public void execute() throws SQLException {
pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
int ret = pstmt_insert.executeUpdate();
Assert.assertEquals(1, ret);
executeQuery();
}
@Test(expected = SQLFeatureNotSupportedException.class) @Test(expected = SQLFeatureNotSupportedException.class)
public void setCharacterStream() throws SQLException { public void setCharacterStream() throws SQLException {
pstmt_insert.setCharacterStream(1, null); pstmt_insert.setCharacterStream(1, null);
...@@ -331,9 +677,17 @@ public class TSDBPreparedStatementTest { ...@@ -331,9 +677,17 @@ public class TSDBPreparedStatementTest {
pstmt_insert.setArray(1, null); pstmt_insert.setArray(1, null);
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test
public void getMetaData() throws SQLException { public void getMetaData() throws SQLException {
pstmt_insert.getMetaData(); // given
long ts = System.currentTimeMillis();
// when
pstmt_insert.setTimestamp(1, new Timestamp(ts));
ResultSetMetaData metaData = pstmt_insert.getMetaData();
// then
Assert.assertNull(metaData);
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test(expected = SQLFeatureNotSupportedException.class)
...@@ -343,9 +697,46 @@ public class TSDBPreparedStatementTest { ...@@ -343,9 +697,46 @@ public class TSDBPreparedStatementTest {
@Test @Test
public void getParameterMetaData() throws SQLException { public void getParameterMetaData() throws SQLException {
// given
long ts = System.currentTimeMillis();
pstmt_insert.setTimestamp(1, new Timestamp(ts));
pstmt_insert.setInt(2, 2);
pstmt_insert.setLong(3, 3l);
pstmt_insert.setFloat(4, 3.14f);
pstmt_insert.setDouble(5, 3.1415);
pstmt_insert.setShort(6, (short) 6);
pstmt_insert.setByte(7, (byte) 7);
pstmt_insert.setBoolean(8, true);
pstmt_insert.setBytes(9, "abc".getBytes());
pstmt_insert.setString(10, "涛思数据");
// when
ParameterMetaData parameterMetaData = pstmt_insert.getParameterMetaData(); ParameterMetaData parameterMetaData = pstmt_insert.getParameterMetaData();
// then
Assert.assertNotNull(parameterMetaData); Assert.assertNotNull(parameterMetaData);
//TODO: modify the test case Assert.assertEquals(10, parameterMetaData.getParameterCount());
Assert.assertEquals(Types.TIMESTAMP, parameterMetaData.getParameterType(1));
Assert.assertEquals(Types.INTEGER, parameterMetaData.getParameterType(2));
Assert.assertEquals(Types.BIGINT, parameterMetaData.getParameterType(3));
Assert.assertEquals(Types.FLOAT, parameterMetaData.getParameterType(4));
Assert.assertEquals(Types.DOUBLE, parameterMetaData.getParameterType(5));
Assert.assertEquals(Types.SMALLINT, parameterMetaData.getParameterType(6));
Assert.assertEquals(Types.TINYINT, parameterMetaData.getParameterType(7));
Assert.assertEquals(Types.BOOLEAN, parameterMetaData.getParameterType(8));
Assert.assertEquals(Types.BINARY, parameterMetaData.getParameterType(9));
Assert.assertEquals(Types.NCHAR, parameterMetaData.getParameterType(10));
Assert.assertEquals("TIMESTAMP", parameterMetaData.getParameterTypeName(1));
Assert.assertEquals("INT", parameterMetaData.getParameterTypeName(2));
Assert.assertEquals("BIGINT", parameterMetaData.getParameterTypeName(3));
Assert.assertEquals("FLOAT", parameterMetaData.getParameterTypeName(4));
Assert.assertEquals("DOUBLE", parameterMetaData.getParameterTypeName(5));
Assert.assertEquals("SMALLINT", parameterMetaData.getParameterTypeName(6));
Assert.assertEquals("TINYINT", parameterMetaData.getParameterTypeName(7));
Assert.assertEquals("BOOL", parameterMetaData.getParameterTypeName(8));
Assert.assertEquals("BINARY", parameterMetaData.getParameterTypeName(9));
Assert.assertEquals("NCHAR", parameterMetaData.getParameterTypeName(10));
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test(expected = SQLFeatureNotSupportedException.class)
...@@ -353,9 +744,9 @@ public class TSDBPreparedStatementTest { ...@@ -353,9 +744,9 @@ public class TSDBPreparedStatementTest {
pstmt_insert.setRowId(1, null); pstmt_insert.setRowId(1, null);
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test
public void setNString() throws SQLException { public void setNString() throws SQLException {
pstmt_insert.setNString(1, null); setString();
} }
@Test(expected = SQLFeatureNotSupportedException.class) @Test(expected = SQLFeatureNotSupportedException.class)
...@@ -373,22 +764,45 @@ public class TSDBPreparedStatementTest { ...@@ -373,22 +764,45 @@ public class TSDBPreparedStatementTest {
pstmt_insert.setSQLXML(1, null); pstmt_insert.setSQLXML(1, null);
} }
@Before
public void before() {
try {
Statement stmt = conn.createStatement();
stmt.execute("drop table if exists weather");
stmt.execute("create table if not exists weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))");
stmt.execute("create table if not exists t1 using weather tags('beijing')");
stmt.close();
pstmt_insert = conn.prepareStatement(sql_insert);
pstmt_select = conn.prepareStatement(sql_select);
} catch (SQLException e) {
e.printStackTrace();
}
}
@After
public void after() {
try {
if (pstmt_insert != null)
pstmt_insert.close();
if (pstmt_select != null)
pstmt_select.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
try { try {
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()) { try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test_pstmt_jni"); stmt.execute("drop database if exists test_pstmt_jni");
stmt.execute("create database if not exists test_pstmt_jni"); stmt.execute("create database if not exists test_pstmt_jni");
stmt.execute("use test_pstmt_jni"); stmt.execute("use test_pstmt_jni");
stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))");
stmt.execute("create table t1 using weather tags('beijing')");
} }
pstmt_insert = conn.prepareStatement(sql_insert); } catch (SQLException e) {
pstmt_select = conn.prepareStatement(sql_select);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
...@@ -396,10 +810,6 @@ public class TSDBPreparedStatementTest { ...@@ -396,10 +810,6 @@ public class TSDBPreparedStatementTest {
@AfterClass @AfterClass
public static void afterClass() { public static void afterClass() {
try { try {
if (pstmt_insert != null)
pstmt_insert.close();
if (pstmt_select != null)
pstmt_select.close();
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
......
...@@ -14,6 +14,7 @@ import java.math.BigDecimal; ...@@ -14,6 +14,7 @@ import java.math.BigDecimal;
import java.sql.*; import java.sql.*;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
public class TSDBResultSetTest { public class TSDBResultSetTest {
...@@ -133,7 +134,7 @@ public class TSDBResultSetTest { ...@@ -133,7 +134,7 @@ public class TSDBResultSetTest {
Assert.assertEquals(3.1415926, Double.valueOf(new String(f5)), 0.000000f); Assert.assertEquals(3.1415926, Double.valueOf(new String(f5)), 0.000000f);
byte[] f6 = rs.getBytes("f6"); byte[] f6 = rs.getBytes("f6");
Assert.assertEquals("abc", new String(f6)); Assert.assertTrue(Arrays.equals("abc".getBytes(), f6));
byte[] f7 = rs.getBytes("f7"); byte[] f7 = rs.getBytes("f7");
Assert.assertEquals((short) 10, Shorts.fromByteArray(f7)); Assert.assertEquals((short) 10, Shorts.fromByteArray(f7));
...@@ -646,7 +647,6 @@ public class TSDBResultSetTest { ...@@ -646,7 +647,6 @@ public class TSDBResultSetTest {
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
try { try {
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");
stmt = conn.createStatement(); stmt = conn.createStatement();
stmt.execute("create database if not exists restful_test"); stmt.execute("create database if not exists restful_test");
...@@ -656,10 +656,9 @@ public class TSDBResultSetTest { ...@@ -656,10 +656,9 @@ public class TSDBResultSetTest {
stmt.execute("insert into restful_test.weather values('2021-01-01 00:00:00.000', 1, 100, 3.1415, 3.1415926, 'abc', 10, 10, true, '涛思数据')"); stmt.execute("insert into restful_test.weather values('2021-01-01 00:00:00.000', 1, 100, 3.1415, 3.1415926, 'abc', 10, 10, true, '涛思数据')");
rs = stmt.executeQuery("select * from restful_test.weather"); rs = stmt.executeQuery("select * from restful_test.weather");
rs.next(); rs.next();
} catch (ClassNotFoundException | SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@AfterClass @AfterClass
......
...@@ -387,15 +387,12 @@ public class TSDBStatementTest { ...@@ -387,15 +387,12 @@ public class TSDBStatementTest {
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", properties); conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", properties);
stmt = conn.createStatement(); stmt = conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -10,6 +10,7 @@ import java.sql.*; ...@@ -10,6 +10,7 @@ import java.sql.*;
import java.util.Properties; import java.util.Properties;
public class RestfulDatabaseMetaDataTest { public class RestfulDatabaseMetaDataTest {
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private static final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"; private static final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
private static Connection connection; private static Connection connection;
...@@ -632,17 +633,32 @@ public class RestfulDatabaseMetaDataTest { ...@@ -632,17 +633,32 @@ public class RestfulDatabaseMetaDataTest {
@Test @Test
public void getTables() throws SQLException { public void getTables() throws SQLException {
System.out.println("****************************************************"); ResultSet rs = metaData.getTables("log", "", null, null);
ResultSet tables = metaData.getTables("log", "", null, null); ResultSetMetaData meta = rs.getMetaData();
ResultSetMetaData metaData = tables.getMetaData(); Assert.assertNotNull(rs);
while (tables.next()) { rs.next();
System.out.print(metaData.getColumnLabel(1) + ":" + tables.getString(1) + "\t"); {
System.out.print(metaData.getColumnLabel(3) + ":" + tables.getString(3) + "\t"); // TABLE_CAT
System.out.print(metaData.getColumnLabel(4) + ":" + tables.getString(4) + "\t"); Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
System.out.print(metaData.getColumnLabel(5) + ":" + tables.getString(5) + "\n"); Assert.assertEquals("log", rs.getString(1));
Assert.assertEquals("log", rs.getString("TABLE_CAT"));
// TABLE_SCHEM
Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2));
Assert.assertEquals(null, rs.getString(2));
Assert.assertEquals(null, rs.getString("TABLE_SCHEM"));
// TABLE_NAME
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertNotNull(rs.getString(3));
Assert.assertNotNull(rs.getString("TABLE_NAME"));
// TABLE_TYPE
Assert.assertEquals("TABLE_TYPE", meta.getColumnLabel(4));
Assert.assertEquals("TABLE", rs.getString(4));
Assert.assertEquals("TABLE", rs.getString("TABLE_TYPE"));
// REMARKS
Assert.assertEquals("REMARKS", meta.getColumnLabel(5));
Assert.assertEquals("", rs.getString(5));
Assert.assertEquals("", rs.getString("REMARKS"));
} }
System.out.println();
Assert.assertNotNull(tables);
} }
@Test @Test
...@@ -652,46 +668,130 @@ public class RestfulDatabaseMetaDataTest { ...@@ -652,46 +668,130 @@ public class RestfulDatabaseMetaDataTest {
@Test @Test
public void getCatalogs() throws SQLException { public void getCatalogs() throws SQLException {
System.out.println("****************************************************"); ResultSet rs = metaData.getCatalogs();
ResultSetMetaData meta = rs.getMetaData();
ResultSet catalogs = metaData.getCatalogs(); rs.next();
ResultSetMetaData meta = catalogs.getMetaData(); {
while (catalogs.next()) { // TABLE_CAT
for (int i = 1; i <= meta.getColumnCount(); i++) { Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
System.out.print(meta.getColumnLabel(i) + ": " + catalogs.getString(i)); Assert.assertNotNull(rs.getString(1));
} Assert.assertNotNull(rs.getString("TABLE_CAT"));
System.out.println();
} }
} }
@Test @Test
public void getTableTypes() throws SQLException { public void getTableTypes() throws SQLException {
System.out.println("****************************************************");
ResultSet tableTypes = metaData.getTableTypes(); ResultSet tableTypes = metaData.getTableTypes();
while (tableTypes.next()) { tableTypes.next();
System.out.println(tableTypes.getString("TABLE_TYPE")); // tableTypes: table
{
Assert.assertEquals("TABLE", tableTypes.getString(1));
Assert.assertEquals("TABLE", tableTypes.getString("TABLE_TYPE"));
}
tableTypes.next();
// tableTypes: stable
{
Assert.assertEquals("STABLE", tableTypes.getString(1));
Assert.assertEquals("STABLE", tableTypes.getString("TABLE_TYPE"));
} }
Assert.assertNotNull(metaData.getTableTypes());
} }
@Test @Test
public void getColumns() throws SQLException { public void getColumns() throws SQLException {
System.out.println("****************************************************"); // when
ResultSet columns = metaData.getColumns("log", "", "dn", ""); ResultSet columns = metaData.getColumns("log", "", "dn", "");
// then
ResultSetMetaData meta = columns.getMetaData(); ResultSetMetaData meta = columns.getMetaData();
while (columns.next()) { columns.next();
System.out.print(meta.getColumnLabel(1) + ": " + columns.getString(1) + "\t"); // column: 1
System.out.print(meta.getColumnLabel(3) + ": " + columns.getString(3) + "\t"); {
System.out.print(meta.getColumnLabel(4) + ": " + columns.getString(4) + "\t"); // TABLE_CAT
System.out.print(meta.getColumnLabel(5) + ": " + columns.getString(5) + "\t"); Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
System.out.print(meta.getColumnLabel(6) + ": " + columns.getString(6) + "\t"); Assert.assertEquals("log", columns.getString(1));
System.out.print(meta.getColumnLabel(7) + ": " + columns.getString(7) + "\t"); Assert.assertEquals("log", columns.getString("TABLE_CAT"));
System.out.print(meta.getColumnLabel(9) + ": " + columns.getString(9) + "\t"); // TABLE_NAME
System.out.print(meta.getColumnLabel(10) + ": " + columns.getString(10) + "\t"); Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
System.out.print(meta.getColumnLabel(11) + ": " + columns.getString(11) + "\n"); Assert.assertEquals("dn", columns.getString(3));
System.out.print(meta.getColumnLabel(12) + ": " + columns.getString(12) + "\n"); Assert.assertEquals("dn", columns.getString("TABLE_NAME"));
// COLUMN_NAME
Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4));
Assert.assertEquals("ts", columns.getString(4));
Assert.assertEquals("ts", columns.getString("COLUMN_NAME"));
// DATA_TYPE
Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5));
Assert.assertEquals(Types.TIMESTAMP, columns.getInt(5));
Assert.assertEquals(Types.TIMESTAMP, columns.getInt("DATA_TYPE"));
// TYPE_NAME
Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6));
Assert.assertEquals("TIMESTAMP", columns.getString(6));
Assert.assertEquals("TIMESTAMP", columns.getString("TYPE_NAME"));
// COLUMN_SIZE
Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7));
Assert.assertEquals(26, columns.getInt(7));
Assert.assertEquals(26, columns.getInt("COLUMN_SIZE"));
// DECIMAL_DIGITS
Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS"));
Assert.assertEquals(null, columns.getString(9));
Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS"));
// NUM_PREC_RADIX
Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10));
Assert.assertEquals(10, columns.getInt(10));
Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX"));
// NULLABLE
Assert.assertEquals("NULLABLE", meta.getColumnLabel(11));
Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt(11));
Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt("NULLABLE"));
// REMARKS
Assert.assertEquals("REMARKS", meta.getColumnLabel(12));
Assert.assertEquals(null, columns.getString(12));
Assert.assertEquals(null, columns.getString("REMARKS"));
}
columns.next();
// column: 2
{
// TABLE_CAT
Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
Assert.assertEquals("log", columns.getString(1));
Assert.assertEquals("log", columns.getString("TABLE_CAT"));
// TABLE_NAME
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertEquals("dn", columns.getString(3));
Assert.assertEquals("dn", columns.getString("TABLE_NAME"));
// COLUMN_NAME
Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4));
Assert.assertEquals("cpu_taosd", columns.getString(4));
Assert.assertEquals("cpu_taosd", columns.getString("COLUMN_NAME"));
// DATA_TYPE
Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5));
Assert.assertEquals(Types.FLOAT, columns.getInt(5));
Assert.assertEquals(Types.FLOAT, columns.getInt("DATA_TYPE"));
// TYPE_NAME
Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6));
Assert.assertEquals("FLOAT", columns.getString(6));
Assert.assertEquals("FLOAT", columns.getString("TYPE_NAME"));
// COLUMN_SIZE
Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7));
Assert.assertEquals(12, columns.getInt(7));
Assert.assertEquals(12, columns.getInt("COLUMN_SIZE"));
// DECIMAL_DIGITS
Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9));
Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS"));
Assert.assertEquals(null, columns.getString(9));
Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS"));
// NUM_PREC_RADIX
Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10));
Assert.assertEquals(10, columns.getInt(10));
Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX"));
// NULLABLE
Assert.assertEquals("NULLABLE", meta.getColumnLabel(11));
Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt(11));
Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt("NULLABLE"));
// REMARKS
Assert.assertEquals("REMARKS", meta.getColumnLabel(12));
Assert.assertEquals(null, columns.getString(12));
} }
} }
...@@ -717,17 +817,35 @@ public class RestfulDatabaseMetaDataTest { ...@@ -717,17 +817,35 @@ public class RestfulDatabaseMetaDataTest {
@Test @Test
public void getPrimaryKeys() throws SQLException { public void getPrimaryKeys() throws SQLException {
System.out.println("****************************************************");
ResultSet rs = metaData.getPrimaryKeys("log", "", "dn1"); ResultSet rs = metaData.getPrimaryKeys("log", "", "dn1");
while (rs.next()) { ResultSetMetaData meta = rs.getMetaData();
System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); rs.next();
System.out.println("COLUMN_NAME: " + rs.getString("COLUMN_NAME")); {
System.out.println("KEY_SEQ: " + rs.getString("KEY_SEQ")); // TABLE_CAT
System.out.println("PK_NAME: " + rs.getString("PK_NAME")); Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
Assert.assertEquals("log", rs.getString(1));
Assert.assertEquals("log", rs.getString("TABLE_CAT"));
// TABLE_SCHEM
Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2));
Assert.assertEquals(null, rs.getString(2));
Assert.assertEquals(null, rs.getString("TABLE_SCHEM"));
// TABLE_NAME
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertEquals("dn1", rs.getString(3));
Assert.assertEquals("dn1", rs.getString("TABLE_NAME"));
// COLUMN_NAME
Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4));
Assert.assertEquals("ts", rs.getString(4));
Assert.assertEquals("ts", rs.getString("COLUMN_NAME"));
// KEY_SEQ
Assert.assertEquals("KEY_SEQ", meta.getColumnLabel(5));
Assert.assertEquals(1, rs.getShort(5));
Assert.assertEquals(1, rs.getShort("KEY_SEQ"));
// DATA_TYPE
Assert.assertEquals("PK_NAME", meta.getColumnLabel(6));
Assert.assertEquals("ts", rs.getString(6));
Assert.assertEquals("ts", rs.getString("PK_NAME"));
} }
Assert.assertNotNull(rs);
} }
@Test @Test
...@@ -852,14 +970,27 @@ public class RestfulDatabaseMetaDataTest { ...@@ -852,14 +970,27 @@ public class RestfulDatabaseMetaDataTest {
@Test @Test
public void getSuperTables() throws SQLException { public void getSuperTables() throws SQLException {
System.out.println("****************************************************");
ResultSet rs = metaData.getSuperTables("log", "", "dn1"); ResultSet rs = metaData.getSuperTables("log", "", "dn1");
while (rs.next()) { ResultSetMetaData meta = rs.getMetaData();
System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); rs.next();
System.out.println("SUPERTABLE_NAME: " + rs.getString("SUPERTABLE_NAME")); {
// TABLE_CAT
Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1));
Assert.assertEquals("log", rs.getString(1));
Assert.assertEquals("log", rs.getString("TABLE_CAT"));
// TABLE_CAT
Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2));
Assert.assertEquals(null, rs.getString(2));
Assert.assertEquals(null, rs.getString("TABLE_SCHEM"));
// TABLE_CAT
Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3));
Assert.assertEquals("dn1", rs.getString(3));
Assert.assertEquals("dn1", rs.getString("TABLE_NAME"));
// TABLE_CAT
Assert.assertEquals("SUPERTABLE_NAME", meta.getColumnLabel(4));
Assert.assertEquals("dn", rs.getString(4));
Assert.assertEquals("dn", rs.getString("SUPERTABLE_NAME"));
} }
Assert.assertNotNull(rs);
} }
@Test @Test
......
...@@ -54,16 +54,17 @@ public class RestfulParameterMetaDataTest { ...@@ -54,16 +54,17 @@ public class RestfulParameterMetaDataTest {
@Test @Test
public void getPrecision() throws SQLException { public void getPrecision() throws SQLException {
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(1)); //create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64))
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(2)); Assert.assertEquals(TSDBConstants.TIMESTAMP_MS_PRECISION, parameterMetaData_insert.getPrecision(1));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(3)); Assert.assertEquals(TSDBConstants.INT_PRECISION, parameterMetaData_insert.getPrecision(2));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(4)); Assert.assertEquals(TSDBConstants.BIGINT_PRECISION, parameterMetaData_insert.getPrecision(3));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(5)); Assert.assertEquals(TSDBConstants.FLOAT_PRECISION, parameterMetaData_insert.getPrecision(4));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(6)); Assert.assertEquals(TSDBConstants.DOUBLE_PRECISION, parameterMetaData_insert.getPrecision(5));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(7)); Assert.assertEquals(TSDBConstants.SMALLINT_PRECISION, parameterMetaData_insert.getPrecision(6));
Assert.assertEquals(0, parameterMetaData_insert.getPrecision(8)); Assert.assertEquals(TSDBConstants.TINYINT_PRECISION, parameterMetaData_insert.getPrecision(7));
Assert.assertEquals(5, parameterMetaData_insert.getPrecision(9)); Assert.assertEquals(TSDBConstants.BOOLEAN_PRECISION, parameterMetaData_insert.getPrecision(8));
Assert.assertEquals(5, parameterMetaData_insert.getPrecision(10)); Assert.assertEquals("hello".getBytes().length, parameterMetaData_insert.getPrecision(9));
Assert.assertEquals("涛思数据".length(), parameterMetaData_insert.getPrecision(10));
} }
@Test @Test
...@@ -71,8 +72,8 @@ public class RestfulParameterMetaDataTest { ...@@ -71,8 +72,8 @@ public class RestfulParameterMetaDataTest {
Assert.assertEquals(0, parameterMetaData_insert.getScale(1)); Assert.assertEquals(0, parameterMetaData_insert.getScale(1));
Assert.assertEquals(0, parameterMetaData_insert.getScale(2)); Assert.assertEquals(0, parameterMetaData_insert.getScale(2));
Assert.assertEquals(0, parameterMetaData_insert.getScale(3)); Assert.assertEquals(0, parameterMetaData_insert.getScale(3));
Assert.assertEquals(0, parameterMetaData_insert.getScale(4)); Assert.assertEquals(31, parameterMetaData_insert.getScale(4));
Assert.assertEquals(0, parameterMetaData_insert.getScale(5)); Assert.assertEquals(31, parameterMetaData_insert.getScale(5));
Assert.assertEquals(0, parameterMetaData_insert.getScale(6)); Assert.assertEquals(0, parameterMetaData_insert.getScale(6));
Assert.assertEquals(0, parameterMetaData_insert.getScale(7)); Assert.assertEquals(0, parameterMetaData_insert.getScale(7));
Assert.assertEquals(0, parameterMetaData_insert.getScale(8)); Assert.assertEquals(0, parameterMetaData_insert.getScale(8));
...@@ -164,7 +165,7 @@ public class RestfulParameterMetaDataTest { ...@@ -164,7 +165,7 @@ public class RestfulParameterMetaDataTest {
pstmt_insert.setObject(7, Byte.MAX_VALUE); pstmt_insert.setObject(7, Byte.MAX_VALUE);
pstmt_insert.setObject(8, true); pstmt_insert.setObject(8, true);
pstmt_insert.setObject(9, "hello".getBytes()); pstmt_insert.setObject(9, "hello".getBytes());
pstmt_insert.setObject(10, "Hello"); pstmt_insert.setObject(10, "涛思数据");
parameterMetaData_insert = pstmt_insert.getParameterMetaData(); parameterMetaData_insert = pstmt_insert.getParameterMetaData();
pstmt_select = conn.prepareStatement(sql_select); pstmt_select = conn.prepareStatement(sql_select);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册