提交 16530a22 编写于 作者: Z zyyang

change

上级 4509e2c3
...@@ -616,7 +616,6 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { ...@@ -616,7 +616,6 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
columnMetaDataList.add(col10); columnMetaDataList.add(col10);
resultSet.setColumnMetaDataList(columnMetaDataList); resultSet.setColumnMetaDataList(columnMetaDataList);
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
ResultSet tables = stmt.executeQuery("show tables"); ResultSet tables = stmt.executeQuery("show tables");
while (tables.next()) { while (tables.next()) {
...@@ -655,8 +654,10 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { ...@@ -655,8 +654,10 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
} }
public ResultSet getTableTypes() throws SQLException { public ResultSet getTableTypes() throws SQLException {
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); if (conn == null || conn.isClosed())
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
// set up ColumnMetaDataList // set up ColumnMetaDataList
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(1); List<ColumnMetaData> columnMetaDataList = new ArrayList<>(1);
ColumnMetaData colMetaData = new ColumnMetaData(); ColumnMetaData colMetaData = new ColumnMetaData();
...@@ -681,91 +682,113 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { ...@@ -681,91 +682,113 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
} }
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
// add by zyyang-taosdata if (conn == null || conn.isClosed())
Statement stmt; throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
if (null != conn && !conn.isClosed()) {
stmt = conn.createStatement(); try (Statement stmt = conn.createStatement()) {
if (catalog == null || catalog.isEmpty()) if (catalog == null || catalog.isEmpty())
return null; return null;
stmt.executeUpdate("use " + catalog); 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();
// set up ColumnMetaDataList // set up ColumnMetaDataList
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(24); List<ColumnMetaData> columnMetaDataList = new ArrayList<>(24);
// TABLE_CAT
ColumnMetaData col1 = new ColumnMetaData();
col1.setColIndex(1);
col1.setColName("TABLE_CAT");
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col1);
// TABLE_SCHEM
ColumnMetaData col2 = new ColumnMetaData();
col2.setColIndex(2);
col2.setColName("TABLE_SCHEM");
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col2);
// TABLE_NAME
ColumnMetaData col3 = new ColumnMetaData();
col3.setColIndex(3);
col3.setColName("TABLE_NAME");
col3.setColSize(193);
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col3);
// COLUMN_NAME
ColumnMetaData col4 = new ColumnMetaData();
col4.setColIndex(4);
col4.setColName("COLUMN_NAME");
col4.setColSize(65);
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col4);
// DATA_TYPE
ColumnMetaData col5 = new ColumnMetaData();
col5.setColIndex(5);
col5.setColName("DATA_TYPE");
col5.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(col5);
// TYPE_NAME
ColumnMetaData col6 = new ColumnMetaData();
col6.setColIndex(6);
col6.setColName("TYPE_NAME");
col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
columnMetaDataList.add(col6);
// COLUMN_SIZE
ColumnMetaData col7 = new ColumnMetaData();
col7.setColIndex(7);
col7.setColName("COLUMN_SIZE");
col7.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(col7);
// BUFFER_LENGTH ,not used
columnMetaDataList.add(null); columnMetaDataList.add(null);
columnMetaDataList.add(null); // DECIMAL_DIGITS
// add TABLE_NAME ColumnMetaData col9 = new ColumnMetaData();
ColumnMetaData colMetaData = new ColumnMetaData(); col9.setColIndex(9);
colMetaData.setColIndex(3); col9.setColName("DECIMAL_DIGITS");
colMetaData.setColName("TABLE_NAME"); col9.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
colMetaData.setColSize(193); columnMetaDataList.add(col9);
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
columnMetaDataList.add(colMetaData);
// add COLUMN_NAME
colMetaData = new ColumnMetaData();
colMetaData.setColIndex(4);
colMetaData.setColName("COLUMN_NAME");
colMetaData.setColSize(65);
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
columnMetaDataList.add(colMetaData);
// add DATA_TYPE
colMetaData = new ColumnMetaData();
colMetaData.setColIndex(5);
colMetaData.setColName("DATA_TYPE");
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(colMetaData);
// add TYPE_NAME
colMetaData = new ColumnMetaData();
colMetaData.setColIndex(6);
colMetaData.setColName("TYPE_NAME");
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
columnMetaDataList.add(colMetaData);
// add COLUMN_SIZE
colMetaData = new ColumnMetaData();
colMetaData.setColIndex(7);
colMetaData.setColName("COLUMN_SIZE");
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(colMetaData);
// add BUFFER_LENGTH ,not used
columnMetaDataList.add(null);
// add DECIMAL_DIGITS
colMetaData = new ColumnMetaData();
colMetaData.setColIndex(9);
colMetaData.setColName("DECIMAL_DIGITS");
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(colMetaData);
// add NUM_PREC_RADIX // add NUM_PREC_RADIX
colMetaData = new ColumnMetaData(); ColumnMetaData col10 = new ColumnMetaData();
colMetaData.setColIndex(10); col10.setColIndex(10);
colMetaData.setColName("NUM_PREC_RADIX"); col10.setColName("NUM_PREC_RADIX");
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col10.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(colMetaData); columnMetaDataList.add(col10);
// add NULLABLE // NULLABLE
colMetaData = new ColumnMetaData(); ColumnMetaData col11 = new ColumnMetaData();
colMetaData.setColIndex(11); col11.setColIndex(11);
colMetaData.setColName("NULLABLE"); col11.setColName("NULLABLE");
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); col11.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
columnMetaDataList.add(colMetaData); columnMetaDataList.add(col11);
resultSet.setColumnMetaDataList(columnMetaDataList); resultSet.setColumnMetaDataList(columnMetaDataList);
// set up rowDataList // set up rowDataList
ResultSet resultSet0 = stmt.executeQuery("describe " + tableNamePattern); ResultSet rs = stmt.executeQuery("describe " + dbname + "." + tableNamePattern);
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
int index = 0; int index = 0;
while (resultSet0.next()) { while (rs.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(24); TSDBResultSetRowData rowData = new TSDBResultSetRowData(24);
// set TABLE_CAT
rowData.setString(0, dbname);
// set TABLE_NAME // set TABLE_NAME
rowData.setString(2, tableNamePattern); rowData.setString(2, tableNamePattern);
// set COLUMN_NAME // set COLUMN_NAME
rowData.setString(3, resultSet0.getString(1)); rowData.setString(3, rs.getString("Field"));
// set DATA_TYPE // set DATA_TYPE
String typeName = resultSet0.getString(2); String typeName = rs.getString("Type");
rowData.setInt(4, getDataType(typeName)); rowData.setInt(4, getDataType(typeName));
// set TYPE_NAME // set TYPE_NAME
rowData.setString(5, typeName); rowData.setString(5, typeName);
// set COLUMN_SIZE // set COLUMN_SIZE
int length = resultSet0.getInt(3); int length = rs.getInt("Length");
rowData.setInt(6, getColumnSize(typeName, length)); rowData.setInt(6, getColumnSize(typeName, length));
// set DECIMAL_DIGITS // set DECIMAL_DIGITS
rowData.setInt(8, getDecimalDigits(typeName)); rowData.setInt(8, getDecimalDigits(typeName));
...@@ -773,15 +796,18 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { ...@@ -773,15 +796,18 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
rowData.setInt(9, 10); rowData.setInt(9, 10);
// set NULLABLE // set NULLABLE
rowData.setInt(10, getNullable(index, typeName)); rowData.setInt(10, getNullable(index, typeName));
// set REMARKS
rowData.setString(11, rs.getString("Note"));
rowDataList.add(rowData); rowDataList.add(rowData);
index++; index++;
} }
resultSet.setRowDataList(rowDataList); resultSet.setRowDataList(rowDataList);
return resultSet; return resultSet;
} else {
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); } catch (SQLException e) {
e.printStackTrace();
} }
return null;
} }
private int getNullable(int index, String typeName) { private int getNullable(int index, String typeName) {
......
...@@ -7,8 +7,8 @@ public class TestTSDBDatabaseMetaData { ...@@ -7,8 +7,8 @@ public class TestTSDBDatabaseMetaData {
public static void main(String[] args) throws SQLException { public static void main(String[] args) throws SQLException {
Connection connection = null; Connection connection = null;
DatabaseMetaData dbMetaData = null; DatabaseMetaData dbMetaData;
ResultSet resSet = null; ResultSet resSet;
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties(); Properties properties = new Properties();
......
...@@ -667,7 +667,14 @@ public class TSDBDatabaseMetaDataTest { ...@@ -667,7 +667,14 @@ public class TSDBDatabaseMetaDataTest {
@Test @Test
public void getColumns() throws SQLException { public void getColumns() throws SQLException {
Assert.assertNotNull(metaData.getColumns("", "", "", "")); ResultSet columns = metaData.getColumns("log", "", "dn", "");
ResultSetMetaData meta = columns.getMetaData();
while (columns.next()) {
for (int i = 1; i <= meta.getColumnCount(); i++) {
System.out.print(meta.getColumnLabel(i) + ": " + columns.getString(i));
}
System.out.println();
}
} }
@Test @Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册