提交 179a4f55 编写于 作者: Z zyyang

[TD-3142]<fix>: fix JDBC-RESTful back ResultSet with null Type

上级 1734b1d1
...@@ -308,7 +308,7 @@ public class DatabaseMetaDataResultSet implements ResultSet { ...@@ -308,7 +308,7 @@ public class DatabaseMetaDataResultSet implements ResultSet {
return colMetaData.getColIndex() + 1; return colMetaData.getColIndex() + 1;
} }
} }
throw new SQLException(TSDBConstants.INVALID_VARIABLES); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
} }
@Override @Override
......
...@@ -14,16 +14,13 @@ ...@@ -14,16 +14,13 @@
*****************************************************************************/ *****************************************************************************/
package com.taosdata.jdbc; package com.taosdata.jdbc;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public abstract class TSDBConstants { public abstract class TSDBConstants {
public static final String STATEMENT_CLOSED = "statement is closed";
public static final String UNSUPPORTED_METHOD_EXCEPTION_MSG = "this operation is NOT supported currently!";
public static final String INVALID_VARIABLES = "invalid variables";
public static final String RESULT_SET_IS_CLOSED = "resultSet is closed";
public static final String DEFAULT_PORT = "6200"; public static final String DEFAULT_PORT = "6200";
public static Map<Integer, String> DATATYPE_MAP = null; public static Map<Integer, String> DATATYPE_MAP = null;
...@@ -77,8 +74,65 @@ public abstract class TSDBConstants { ...@@ -77,8 +74,65 @@ public abstract class TSDBConstants {
return WrapErrMsg("unkown error!"); return WrapErrMsg("unkown error!");
} }
public static int taosType2JdbcType(int taosType) throws SQLException {
switch (taosType) {
case TSDBConstants.TSDB_DATA_TYPE_NULL:
return Types.NULL;
case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return Types.BOOLEAN;
case TSDBConstants.TSDB_DATA_TYPE_TINYINT:
return Types.TINYINT;
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:
return Types.SMALLINT;
case TSDBConstants.TSDB_DATA_TYPE_INT:
return Types.INTEGER;
case TSDBConstants.TSDB_DATA_TYPE_BIGINT:
return Types.BIGINT;
case TSDBConstants.TSDB_DATA_TYPE_FLOAT:
return Types.FLOAT;
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
return Types.DOUBLE;
case TSDBConstants.TSDB_DATA_TYPE_BINARY:
return Types.BINARY;
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
return Types.TIMESTAMP;
case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
return Types.NCHAR;
}
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE);
}
public static int jdbcType2TaosType(int jdbcType) throws SQLException {
switch (jdbcType){
case Types.NULL:
return TSDBConstants.TSDB_DATA_TYPE_NULL;
case Types.BOOLEAN:
return TSDBConstants.TSDB_DATA_TYPE_BOOL;
case Types.TINYINT:
return TSDBConstants.TSDB_DATA_TYPE_TINYINT;
case Types.SMALLINT:
return TSDBConstants.TSDB_DATA_TYPE_SMALLINT;
case Types.INTEGER:
return TSDBConstants.TSDB_DATA_TYPE_INT;
case Types.BIGINT:
return TSDBConstants.TSDB_DATA_TYPE_BIGINT;
case Types.FLOAT:
return TSDBConstants.TSDB_DATA_TYPE_FLOAT;
case Types.DOUBLE:
return TSDBConstants.TSDB_DATA_TYPE_DOUBLE;
case Types.BINARY:
return TSDBConstants.TSDB_DATA_TYPE_BINARY;
case Types.TIMESTAMP:
return TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP;
case Types.NCHAR:
return TSDBConstants.TSDB_DATA_TYPE_NCHAR;
}
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE);
}
static { static {
DATATYPE_MAP = new HashMap<>(); DATATYPE_MAP = new HashMap<>();
DATATYPE_MAP.put(0, "NULL");
DATATYPE_MAP.put(1, "BOOL"); DATATYPE_MAP.put(1, "BOOL");
DATATYPE_MAP.put(2, "TINYINT"); DATATYPE_MAP.put(2, "TINYINT");
DATATYPE_MAP.put(3, "SMALLINT"); DATATYPE_MAP.put(3, "SMALLINT");
...@@ -90,4 +144,8 @@ public abstract class TSDBConstants { ...@@ -90,4 +144,8 @@ public abstract class TSDBConstants {
DATATYPE_MAP.put(9, "TIMESTAMP"); DATATYPE_MAP.put(9, "TIMESTAMP");
DATATYPE_MAP.put(10, "NCHAR"); DATATYPE_MAP.put(10, "NCHAR");
} }
public static String jdbcType2TaosTypeName(int type) throws SQLException {
return DATATYPE_MAP.get(jdbcType2TaosType(type));
}
} }
...@@ -18,6 +18,7 @@ public class TSDBErrorNumbers { ...@@ -18,6 +18,7 @@ public class TSDBErrorNumbers {
public static final int ERROR_INVALID_FOR_EXECUTE = 0x230c; //not a valid sql for execute: (SQL) public static final int ERROR_INVALID_FOR_EXECUTE = 0x230c; //not a valid sql for execute: (SQL)
public static final int ERROR_PARAMETER_INDEX_OUT_RANGE = 0x230d; // parameter index out of range public static final int ERROR_PARAMETER_INDEX_OUT_RANGE = 0x230d; // parameter index out of range
public static final int ERROR_SQLCLIENT_EXCEPTION_ON_CONNECTION_CLOSED = 0x230e; // connection already closed public static final int ERROR_SQLCLIENT_EXCEPTION_ON_CONNECTION_CLOSED = 0x230e; // connection already closed
public static final int ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE = 0x230f; //unknown sql type in tdengine
public static final int ERROR_UNKNOWN = 0x2350; //unknown error public static final int ERROR_UNKNOWN = 0x2350; //unknown error
...@@ -49,6 +50,7 @@ public class TSDBErrorNumbers { ...@@ -49,6 +50,7 @@ public class TSDBErrorNumbers {
errorNumbers.add(ERROR_INVALID_FOR_EXECUTE); errorNumbers.add(ERROR_INVALID_FOR_EXECUTE);
errorNumbers.add(ERROR_PARAMETER_INDEX_OUT_RANGE); errorNumbers.add(ERROR_PARAMETER_INDEX_OUT_RANGE);
errorNumbers.add(ERROR_SQLCLIENT_EXCEPTION_ON_CONNECTION_CLOSED); errorNumbers.add(ERROR_SQLCLIENT_EXCEPTION_ON_CONNECTION_CLOSED);
errorNumbers.add(ERROR_UNKNOWN_SQL_TYPE_IN_TDENGINE);
/*****************************************************/ /*****************************************************/
errorNumbers.add(ERROR_SUBSCRIBE_FAILED); errorNumbers.add(ERROR_SUBSCRIBE_FAILED);
......
...@@ -20,7 +20,7 @@ import java.sql.Timestamp; ...@@ -20,7 +20,7 @@ import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
import java.util.List; import java.util.List;
public class TSDBResultSetMetaData implements ResultSetMetaData { public class TSDBResultSetMetaData extends WrapperImpl implements ResultSetMetaData {
List<ColumnMetaData> colMetaDataList = null; List<ColumnMetaData> colMetaDataList = null;
...@@ -28,14 +28,6 @@ public class TSDBResultSetMetaData implements ResultSetMetaData { ...@@ -28,14 +28,6 @@ public class TSDBResultSetMetaData implements ResultSetMetaData {
this.colMetaDataList = metaDataList; this.colMetaDataList = metaDataList;
} }
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG);
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG);
}
public int getColumnCount() throws SQLException { public int getColumnCount() throws SQLException {
return colMetaDataList.size(); return colMetaDataList.size();
} }
...@@ -94,7 +86,7 @@ public class TSDBResultSetMetaData implements ResultSetMetaData { ...@@ -94,7 +86,7 @@ public class TSDBResultSetMetaData implements ResultSetMetaData {
} }
public String getSchemaName(int column) throws SQLException { public String getSchemaName(int column) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
public int getPrecision(int column) throws SQLException { public int getPrecision(int column) throws SQLException {
...@@ -125,18 +117,18 @@ public class TSDBResultSetMetaData implements ResultSetMetaData { ...@@ -125,18 +117,18 @@ public class TSDBResultSetMetaData implements ResultSetMetaData {
} }
public String getTableName(int column) throws SQLException { public String getTableName(int column) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
public String getCatalogName(int column) throws SQLException { public String getCatalogName(int column) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
public int getColumnType(int column) throws SQLException { public int getColumnType(int column) throws SQLException {
ColumnMetaData meta = this.colMetaDataList.get(column - 1); ColumnMetaData meta = this.colMetaDataList.get(column - 1);
switch (meta.getColType()) { switch (meta.getColType()) {
case TSDBConstants.TSDB_DATA_TYPE_BOOL: case TSDBConstants.TSDB_DATA_TYPE_BOOL:
return java.sql.Types.BIT; return Types.BOOLEAN;
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: case TSDBConstants.TSDB_DATA_TYPE_TINYINT:
return java.sql.Types.TINYINT; return java.sql.Types.TINYINT;
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:
...@@ -150,13 +142,13 @@ public class TSDBResultSetMetaData implements ResultSetMetaData { ...@@ -150,13 +142,13 @@ public class TSDBResultSetMetaData implements ResultSetMetaData {
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: case TSDBConstants.TSDB_DATA_TYPE_DOUBLE:
return java.sql.Types.DOUBLE; return java.sql.Types.DOUBLE;
case TSDBConstants.TSDB_DATA_TYPE_BINARY: case TSDBConstants.TSDB_DATA_TYPE_BINARY:
return java.sql.Types.CHAR; return Types.BINARY;
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
return java.sql.Types.BIGINT; return java.sql.Types.TIMESTAMP;
case TSDBConstants.TSDB_DATA_TYPE_NCHAR: case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
return java.sql.Types.CHAR; return Types.NCHAR;
} }
throw new SQLException(TSDBConstants.INVALID_VARIABLES); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
} }
public String getColumnTypeName(int column) throws SQLException { public String getColumnTypeName(int column) throws SQLException {
...@@ -173,7 +165,7 @@ public class TSDBResultSetMetaData implements ResultSetMetaData { ...@@ -173,7 +165,7 @@ public class TSDBResultSetMetaData implements ResultSetMetaData {
} }
public boolean isDefinitelyWritable(int column) throws SQLException { public boolean isDefinitelyWritable(int column) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
public String getColumnClassName(int column) throws SQLException { public String getColumnClassName(int column) throws SQLException {
......
...@@ -1153,11 +1153,11 @@ public class TSDBResultSetWrapper implements ResultSet { ...@@ -1153,11 +1153,11 @@ public class TSDBResultSetWrapper implements ResultSet {
} }
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException { public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException { public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
throw new SQLException(TSDBConstants.UNSUPPORTED_METHOD_EXCEPTION_MSG); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
@Override @Override
......
...@@ -18,10 +18,10 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { ...@@ -18,10 +18,10 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet {
private final String database; private final String database;
private final Statement statement; private final Statement statement;
// data // data
private ArrayList<ArrayList<Object>> resultSet = new ArrayList<>(); private ArrayList<ArrayList<Object>> resultSet;
// meta // meta
private ArrayList<String> columnNames = new ArrayList<>(); private ArrayList<String> columnNames;
private ArrayList<Field> columns = new ArrayList<>(); private ArrayList<Field> columns;
private RestfulResultSetMetaData metaData; private RestfulResultSetMetaData metaData;
/** /**
...@@ -29,11 +29,36 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { ...@@ -29,11 +29,36 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet {
* *
* @param resultJson: 包含data信息的结果集,有sql返回的结果集 * @param resultJson: 包含data信息的结果集,有sql返回的结果集
***/ ***/
public RestfulResultSet(String database, Statement statement, JSONObject resultJson) { public RestfulResultSet(String database, Statement statement, JSONObject resultJson) throws SQLException {
this.database = database; this.database = database;
this.statement = statement; this.statement = statement;
// column metadata
JSONArray columnMeta = resultJson.getJSONArray("column_meta");
columnNames = new ArrayList<>();
columns = new ArrayList<>();
for (int colIndex = 0; colIndex < columnMeta.size(); colIndex++) {
JSONArray col = columnMeta.getJSONArray(colIndex);
String col_name = col.getString(0);
int col_type = TSDBConstants.taosType2JdbcType(col.getInteger(1));
int col_length = col.getInteger(2);
columnNames.add(col_name);
columns.add(new Field(col_name, col_type, col_length, ""));
}
this.metaData = new RestfulResultSetMetaData(this.database, columns, this);
// row data // row data
JSONArray data = resultJson.getJSONArray("data"); JSONArray data = resultJson.getJSONArray("data");
resultSet = new ArrayList<>();
for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) {
ArrayList row = new ArrayList();
JSONArray jsonRow = data.getJSONArray(rowIndex);
for (int colIndex = 0; colIndex < jsonRow.size(); colIndex++) {
row.add(parseColumnData(jsonRow, colIndex, columns.get(colIndex).type));
}
resultSet.add(row);
}
/*
int columnIndex = 0; int columnIndex = 0;
for (; columnIndex < data.size(); columnIndex++) { for (; columnIndex < data.size(); columnIndex++) {
ArrayList oneRow = new ArrayList<>(); ArrayList oneRow = new ArrayList<>();
...@@ -52,50 +77,77 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { ...@@ -52,50 +77,77 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet {
columns.add(new Field(name, "", 0, "")); columns.add(new Field(name, "", 0, ""));
} }
this.metaData = new RestfulResultSetMetaData(this.database, columns, this); this.metaData = new RestfulResultSetMetaData(this.database, columns, this);
} */
}
/**
* 由多个resultSet的JSON构造结果集 private Object parseColumnData(JSONArray row, int colIndex, int sqlType) {
* switch (sqlType) {
* @param resultJson: 包含data信息的结果集,有sql返回的结果集 case Types.NULL:
* @param fieldJson: 包含多个(最多2个)meta信息的结果集,有describe xxx return null;
**/ case Types.BOOLEAN:
public RestfulResultSet(String database, Statement statement, JSONObject resultJson, List<JSONObject> fieldJson) { return row.getBoolean(colIndex);
this(database, statement, resultJson); case Types.TINYINT:
ArrayList<Field> newColumns = new ArrayList<>(); case Types.SMALLINT:
return row.getShort(colIndex);
for (Field column : columns) { case Types.INTEGER:
Field field = findField(column.name, fieldJson); return row.getInteger(colIndex);
if (field != null) { case Types.BIGINT:
newColumns.add(field); return row.getBigInteger(colIndex);
} else { case Types.FLOAT:
newColumns.add(column); return row.getFloat(colIndex);
} case Types.DOUBLE:
} return row.getDouble(colIndex);
this.columns = newColumns; case Types.TIMESTAMP:
this.metaData = new RestfulResultSetMetaData(this.database, this.columns, this); return row.getTimestamp(colIndex);
} case Types.BINARY:
case Types.NCHAR:
public Field findField(String columnName, List<JSONObject> fieldJsonList) { default:
for (JSONObject fieldJSON : fieldJsonList) { return row.getString(colIndex);
JSONArray fieldDataJson = fieldJSON.getJSONArray("data");
for (int i = 0; i < fieldDataJson.size(); i++) {
JSONArray field = fieldDataJson.getJSONArray(i);
if (columnName.equalsIgnoreCase(field.getString(0))) {
return new Field(field.getString(0), field.getString(1), field.getInteger(2), field.getString(3));
}
}
} }
return null;
} }
// /**
// * 由多个resultSet的JSON构造结果集
// *
// * @param resultJson: 包含data信息的结果集,有sql返回的结果集
// * @param fieldJson: 包含多个(最多2个)meta信息的结果集,有describe xxx
// **/
// public RestfulResultSet(String database, Statement statement, JSONObject resultJson, List<JSONObject> fieldJson) throws SQLException {
// this(database, statement, resultJson);
// ArrayList<Field> newColumns = new ArrayList<>();
//
// for (Field column : columns) {
// Field field = findField(column.name, fieldJson);
// if (field != null) {
// newColumns.add(field);
// } else {
// newColumns.add(column);
// }
// }
// this.columns = newColumns;
// this.metaData = new RestfulResultSetMetaData(this.database, this.columns, this);
// }
// public Field findField(String columnName, List<JSONObject> fieldJsonList) {
// for (JSONObject fieldJSON : fieldJsonList) {
// JSONArray fieldDataJson = fieldJSON.getJSONArray("data");
// for (int i = 0; i < fieldDataJson.size(); i++) {
// JSONArray field = fieldDataJson.getJSONArray(i);
// if (columnName.equalsIgnoreCase(field.getString(0))) {
// return new Field(field.getString(0), field.getString(1), field.getInteger(2), field.getString(3));
// }
// }
// }
// return null;
// }
public class Field { public class Field {
String name; String name;
String type; int type;
int length; int length;
String note; String note;
public Field(String name, String type, int length, String note) { public Field(String name, int type, int length, String note) {
this.name = name; this.name = name;
this.type = type; this.type = type;
this.length = length; this.length = length;
......
...@@ -5,6 +5,7 @@ import com.taosdata.jdbc.TSDBConstants; ...@@ -5,6 +5,7 @@ import com.taosdata.jdbc.TSDBConstants;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
public class RestfulResultSetMetaData implements ResultSetMetaData { public class RestfulResultSetMetaData implements ResultSetMetaData {
...@@ -53,14 +54,14 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { ...@@ -53,14 +54,14 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
@Override @Override
public boolean isSigned(int column) throws SQLException { public boolean isSigned(int column) throws SQLException {
String type = this.fields.get(column - 1).type.toUpperCase(); int type = this.fields.get(column - 1).type;
switch (type) { switch (type) {
case "TINYINT": case Types.TINYINT:
case "SMALLINT": case Types.SMALLINT:
case "INT": case Types.INTEGER:
case "BIGINT": case Types.BIGINT:
case "FLOAT": case Types.FLOAT:
case "DOUBLE": case Types.DOUBLE:
return true; return true;
default: default:
return false; return false;
...@@ -89,14 +90,14 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { ...@@ -89,14 +90,14 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
@Override @Override
public int getPrecision(int column) throws SQLException { public int getPrecision(int column) throws SQLException {
String type = this.fields.get(column - 1).type.toUpperCase(); int type = this.fields.get(column - 1).type;
switch (type) { switch (type) {
case "FLOAT": case Types.FLOAT:
return 5; return 5;
case "DOUBLE": case Types.DOUBLE:
return 9; return 9;
case "BINARY": case Types.BINARY:
case "NCHAR": case Types.NCHAR:
return this.fields.get(column - 1).length; return this.fields.get(column - 1).length;
default: default:
return 0; return 0;
...@@ -105,11 +106,11 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { ...@@ -105,11 +106,11 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
@Override @Override
public int getScale(int column) throws SQLException { public int getScale(int column) throws SQLException {
String type = this.fields.get(column - 1).type.toUpperCase(); int type = this.fields.get(column - 1).type;
switch (type) { switch (type) {
case "FLOAT": case Types.FLOAT:
return 5; return 5;
case "DOUBLE": case Types.DOUBLE:
return 9; return 9;
default: default:
return 0; return 0;
...@@ -128,36 +129,13 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { ...@@ -128,36 +129,13 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
@Override @Override
public int getColumnType(int column) throws SQLException { public int getColumnType(int column) throws SQLException {
String type = this.fields.get(column - 1).type.toUpperCase(); return this.fields.get(column - 1).type;
switch (type) {
case "BOOL":
return java.sql.Types.BOOLEAN;
case "TINYINT":
return java.sql.Types.TINYINT;
case "SMALLINT":
return java.sql.Types.SMALLINT;
case "INT":
return java.sql.Types.INTEGER;
case "BIGINT":
return java.sql.Types.BIGINT;
case "FLOAT":
return java.sql.Types.FLOAT;
case "DOUBLE":
return java.sql.Types.DOUBLE;
case "BINARY":
return java.sql.Types.BINARY;
case "TIMESTAMP":
return java.sql.Types.TIMESTAMP;
case "NCHAR":
return java.sql.Types.NCHAR;
}
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
} }
@Override @Override
public String getColumnTypeName(int column) throws SQLException { public String getColumnTypeName(int column) throws SQLException {
String type = fields.get(column - 1).type; int type = fields.get(column - 1).type;
return type.toUpperCase(); return TSDBConstants.jdbcType2TaosTypeName(type);
} }
@Override @Override
...@@ -177,26 +155,26 @@ public class RestfulResultSetMetaData implements ResultSetMetaData { ...@@ -177,26 +155,26 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
@Override @Override
public String getColumnClassName(int column) throws SQLException { public String getColumnClassName(int column) throws SQLException {
String type = this.fields.get(column - 1).type; int type = this.fields.get(column - 1).type;
String columnClassName = ""; String columnClassName = "";
switch (type) { switch (type) {
case "BOOL": case Types.BOOLEAN:
return Boolean.class.getName(); return Boolean.class.getName();
case "TINYINT": case Types.TINYINT:
case "SMALLINT": case Types.SMALLINT:
return Short.class.getName(); return Short.class.getName();
case "INT": case Types.INTEGER:
return Integer.class.getName(); return Integer.class.getName();
case "BIGINT": case Types.BIGINT:
return Long.class.getName(); return Long.class.getName();
case "FLOAT": case Types.FLOAT:
return Float.class.getName(); return Float.class.getName();
case "DOUBLE": case Types.DOUBLE:
return Double.class.getName(); return Double.class.getName();
case "TIMESTAMP": case Types.TIMESTAMP:
return Timestamp.class.getName(); return Timestamp.class.getName();
case "BINARY": case Types.BINARY:
case "NCHAR": case Types.NCHAR:
return String.class.getName(); return String.class.getName();
} }
return columnClassName; return columnClassName;
......
...@@ -151,22 +151,21 @@ public class RestfulStatement extends AbstractStatement { ...@@ -151,22 +151,21 @@ public class RestfulStatement extends AbstractStatement {
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + resultJson.getString("desc") + "\n" + "error code: " + resultJson.getString("code"))); throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + resultJson.getString("desc") + "\n" + "error code: " + resultJson.getString("code")));
} }
// parse table name from sql // parse table name from sql
String[] tableIdentifiers = parseTableIdentifier(sql); // String[] tableIdentifiers = parseTableIdentifier(sql);
if (tableIdentifiers != null) { // if (tableIdentifiers != null) {
List<JSONObject> fieldJsonList = new ArrayList<>(); // List<JSONObject> fieldJsonList = new ArrayList<>();
for (String tableIdentifier : tableIdentifiers) { // for (String tableIdentifier : tableIdentifiers) {
// field meta // String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier);
String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier); // JSONObject fieldJson = JSON.parseObject(fields);
JSONObject fieldJson = JSON.parseObject(fields); // if (fieldJson.getString("status").equals("error")) {
if (fieldJson.getString("status").equals("error")) { // throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code")));
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code"))); // }
} // fieldJsonList.add(fieldJson);
fieldJsonList.add(fieldJson); // }
} // this.resultSet = new RestfulResultSet(database, this, resultJson, fieldJsonList);
this.resultSet = new RestfulResultSet(database, this, resultJson, fieldJsonList); // } else {
} else { this.resultSet = new RestfulResultSet(database, this, resultJson);
this.resultSet = new RestfulResultSet(database, this, resultJson); // }
}
this.affectedRows = 0; this.affectedRows = 0;
return resultSet; return resultSet;
} }
...@@ -201,7 +200,7 @@ public class RestfulStatement extends AbstractStatement { ...@@ -201,7 +200,7 @@ public class RestfulStatement extends AbstractStatement {
@Override @Override
public ResultSet getResultSet() throws SQLException { public ResultSet getResultSet() throws SQLException {
if (isClosed()) if (isClosed())
throw new SQLException(TSDBConstants.STATEMENT_CLOSED); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED);
return resultSet; return resultSet;
} }
......
...@@ -11,8 +11,8 @@ import java.sql.*; ...@@ -11,8 +11,8 @@ import java.sql.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SQLTest { public class SQLTest {
private static final String host = "127.0.0.1"; // private static final String host = "127.0.0.1";
// private static final String host = "master"; private static final String host = "master";
private static Connection connection; private static Connection connection;
@Test @Test
...@@ -323,6 +323,18 @@ public class SQLTest { ...@@ -323,6 +323,18 @@ public class SQLTest {
SQLExecutor.executeQuery(connection, sql); SQLExecutor.executeQuery(connection, sql);
} }
@Test
public void testCase052() {
String sql = "select server_status()";
SQLExecutor.executeQuery(connection, sql);
}
@Test
public void testCase053() {
String sql = "select avg(cpu_taosd), avg(cpu_system), max(cpu_cores), avg(mem_taosd), avg(mem_system), max(mem_total), avg(disk_used), max(disk_total), avg(band_speed), avg(io_read), avg(io_write), sum(req_http), sum(req_select), sum(req_insert) from log.dn1 where ts> now - 60m and ts<= now interval(1m) fill(value, 0)";
SQLExecutor.executeQuery(connection, sql);
}
@BeforeClass @BeforeClass
public static void before() throws ClassNotFoundException, SQLException { public static void before() throws ClassNotFoundException, SQLException {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver"); Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
......
...@@ -3,17 +3,15 @@ package com.taosdata.taosdemo; ...@@ -3,17 +3,15 @@ package com.taosdata.taosdemo;
import com.taosdata.taosdemo.components.DataSourceFactory; import com.taosdata.taosdemo.components.DataSourceFactory;
import com.taosdata.taosdemo.components.JdbcTaosdemoConfig; import com.taosdata.taosdemo.components.JdbcTaosdemoConfig;
import com.taosdata.taosdemo.domain.SuperTableMeta; import com.taosdata.taosdemo.domain.SuperTableMeta;
import com.taosdata.taosdemo.service.*; import com.taosdata.taosdemo.service.DatabaseService;
import com.taosdata.taosdemo.service.SqlExecuteTask;
import com.taosdata.taosdemo.service.SubTableService;
import com.taosdata.taosdemo.service.SuperTableService;
import com.taosdata.taosdemo.service.data.SuperTableMetaGenerator; import com.taosdata.taosdemo.service.data.SuperTableMetaGenerator;
import com.taosdata.taosdemo.utils.Printer;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.IOException; import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays; import java.util.Arrays;
...@@ -35,7 +33,7 @@ public class TaosDemoApplication { ...@@ -35,7 +33,7 @@ public class TaosDemoApplication {
// 初始化 // 初始化
final DataSource dataSource = DataSourceFactory.getInstance(config.host, config.port, config.user, config.password); final DataSource dataSource = DataSourceFactory.getInstance(config.host, config.port, config.user, config.password);
if (config.executeSql != null && !config.executeSql.isEmpty() && !config.executeSql.replaceAll("\\s", "").isEmpty()) { if (config.executeSql != null && !config.executeSql.isEmpty() && !config.executeSql.replaceAll("\\s", "").isEmpty()) {
Thread task = new Thread(new SqlExecuteor(dataSource, config.executeSql)); Thread task = new Thread(new SqlExecuteTask(dataSource, config.executeSql));
task.start(); task.start();
try { try {
task.join(); task.join();
......
...@@ -8,11 +8,11 @@ import java.sql.ResultSet; ...@@ -8,11 +8,11 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
public class SqlExecuteor implements Runnable { public class SqlExecuteTask implements Runnable {
private final DataSource dataSource; private final DataSource dataSource;
private final String sql; private final String sql;
public SqlExecuteor(DataSource dataSource, String sql) { public SqlExecuteTask(DataSource dataSource, String sql) {
this.dataSource = dataSource; this.dataSource = dataSource;
this.sql = sql; this.sql = sql;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册