From 0e987bb9e36cb8bcb95fe965a59eb06c49e0eeac Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 21 Jan 2021 17:00:22 +0800 Subject: [PATCH] change --- src/connector/jdbc/src/main/java/App3.java | 60 --- .../src/main/java/TdEngineSuperDataGen.java | 67 --- .../jdbc/AbstractDatabaseMetaData.java | 390 ++++++++++++++++ .../com/taosdata/jdbc/ColumnMetaData.java | 72 +-- .../taosdata/jdbc/TSDBDatabaseMetaData.java | 419 +----------------- .../jdbc/rs/RestfulDatabaseMetaData.java | 384 +--------------- 6 files changed, 442 insertions(+), 950 deletions(-) delete mode 100755 src/connector/jdbc/src/main/java/App3.java delete mode 100755 src/connector/jdbc/src/main/java/TdEngineSuperDataGen.java diff --git a/src/connector/jdbc/src/main/java/App3.java b/src/connector/jdbc/src/main/java/App3.java deleted file mode 100755 index 5274b76d88..0000000000 --- a/src/connector/jdbc/src/main/java/App3.java +++ /dev/null @@ -1,60 +0,0 @@ -import org.apache.commons.dbcp2.BasicDataSource; - -import java.sql.*; - -public class App3 { - - public static void main(String[] args) throws SQLException, ClassNotFoundException { - - // 添加数据源1-TDengine,并且作为schema命名为“public” - String url = "jdbc:TAOS://127.0.0.1:6030/hdb"; - Class.forName("com.taosdata.jdbc.TSDBDriver"); - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setUrl(url); - dataSource.setUsername("root"); - dataSource.setPassword("taosdata"); - - // 执行SQL语句,sql语句中表的引用必须用前面设置的schema名称,例如“testdata”就是schema“public“下的表,”datedim“是schema”test“下的表。 - String sql = "select cast(s.updates as date ),t.id,t.cmt from public.testdata t , test.datedim s " - + "where cast(t.uptime as date )=s.updates and t.uptime<'2011-12-01 00:00:00' and t.id>100 limit 100 offset 10 "; - long startTime = System.currentTimeMillis(); - Connection conn = dataSource.getConnection(); - Statement stmt = conn.createStatement(); -// ResultSet rs = stmt.executeQuery(sql); - long rowCount = 0; - String[] types = { "TABLE" }; - ResultSet rs = conn.getMetaData().getTables("hdb", null, null, types); - while (rs.next()) { -// if (rowCount < 5) { - for (int j = 0; j < rs.getMetaData().getColumnCount(); j++) { - System.out.print(rs.getMetaData().getColumnName(j + 1) + ": " + rs.getObject(j) + ", "); - } - System.out.println(); -// } - rowCount++; - } - rs.close(); - System.out.println("\nGET COLUMN:"); - rs = conn.getMetaData().getColumns("hdb", null, "sdata", null); - while (rs.next()) { -// if (rowCount < 5) { - ResultSetMetaData meta = rs.getMetaData(); - for (int j = 0; j < rs.getMetaData().getColumnCount(); j++) { - if (j != 7) { - System.out.print(rs.getMetaData().getColumnName(j + 1) + ": " + rs.getObject(j) + ", "); - System.out.println(); - } - } - System.out.println(); -// } - rowCount++; - } - long endTime = System.currentTimeMillis(); - System.out.println("execute time:" + (endTime - startTime) + "ms, resultset rows " + rowCount + ", " - + rowCount * 1000 / (endTime - startTime) + " rows/sec"); - rs.close(); - stmt.close(); - conn.close(); - } - -} diff --git a/src/connector/jdbc/src/main/java/TdEngineSuperDataGen.java b/src/connector/jdbc/src/main/java/TdEngineSuperDataGen.java deleted file mode 100755 index 8732262187..0000000000 --- a/src/connector/jdbc/src/main/java/TdEngineSuperDataGen.java +++ /dev/null @@ -1,67 +0,0 @@ -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Random; - -import org.apache.commons.codec.digest.DigestUtils; - -public class TdEngineSuperDataGen { - - public static void main(String[] args) throws ClassNotFoundException, SQLException { - Class.forName("com.taosdata.jdbc.TSDBDriver"); - String url = "jdbc:TAOS://127.0.0.1:6030/test?user=root&password=taosdata"; - Connection conn = DriverManager.getConnection(url); - Statement stmt = conn.createStatement(); - // create database - stmt.executeUpdate("create database if not exists hdb"); - // use database - stmt.executeUpdate("use hdb"); - stmt.executeUpdate("drop table if exists sdata"); - // create table - stmt.executeUpdate( - "create table if not exists sdata (uptime timestamp, id int, x int , y int ,cmt binary(100)) tags(location nchar(100),tname nchar(100))"); - - ZoneId zoneId = ZoneId.systemDefault(); - Map table = new HashMap<>(); - table.put("dt001", "beijing"); - table.put("dt002", "shanghai"); - table.put("dt003", "chongqing"); - table.put("dt004", "xian"); - for (Entry kv : table.entrySet()) { - LocalDateTime d = LocalDateTime.now().minusMonths(2); - long rowCount = LocalDateTime.now().atZone(zoneId).toEpochSecond() - d.atZone(zoneId).toEpochSecond(); - Random r = new Random(); - StringBuilder sb = null; - long startTime = System.currentTimeMillis(); - try { - for (long i = 0; i < rowCount; i++) { - sb = new StringBuilder("insert into " + kv.getKey() + " using sdata tags(" + kv.getValue() + "," + kv.getKey() + ") values('"); - d = d.plusSeconds(1); - sb.append(d.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.MS"))); - sb.append("'," + i + "," + r.nextInt(100) + "," + r.nextInt(100) + ",'"); - sb.append(DigestUtils.md5Hex(d.toString())); - sb.append("')"); - System.out.println("SQL >>> " + sb.toString()); - stmt.executeUpdate(sb.toString()); - } - } catch (SQLException e) { - System.out.println(d); - System.out.println(sb.toString()); - e.printStackTrace(); - } - long endTime = System.currentTimeMillis(); - System.out.println("generate data execute time:" + (endTime - startTime) + "ms, resultset rows " + rowCount - + ", " + rowCount * 1000 / (endTime - startTime) + " rows/sec"); - } - stmt.close(); - conn.close(); - } - -} diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java index ac5f0a747e..35709fea36 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java @@ -495,6 +495,102 @@ public abstract class AbstractDatabaseMetaData implements DatabaseMetaData, Wrap 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 { + 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 columnMetaDataList = new ArrayList<>(); + ColumnMetaData col1 = new ColumnMetaData(); + col1.setColIndex(1); + col1.setColName("TABLE_CAT"); + col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col1); + ColumnMetaData col2 = new ColumnMetaData(); + col2.setColIndex(2); + col2.setColName("TABLE_SCHEM"); + col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col2); + ColumnMetaData col3 = new ColumnMetaData(); + col3.setColIndex(3); + col3.setColName("TABLE_NAME"); + col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + 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(); + col6.setColIndex(6); + col6.setColName("TYPE_CAT"); + col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col6); + ColumnMetaData col7 = new ColumnMetaData(); + col7.setColIndex(7); + col7.setColName("TYPE_SCHEM"); + col7.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col7); + ColumnMetaData col8 = new ColumnMetaData(); + col8.setColIndex(8); + col8.setColName("TYPE_NAME"); + col8.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col8); + ColumnMetaData col9 = new ColumnMetaData(); + col9.setColIndex(9); + col9.setColName("SELF_REFERENCING_COL_NAME"); + col9.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col9); + ColumnMetaData col10 = new ColumnMetaData(); + col10.setColIndex(10); + col10.setColName("REF_GENERATION"); + col10.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col10); + resultSet.setColumnMetaDataList(columnMetaDataList); + + List rowDataList = new ArrayList<>(); + ResultSet tables = stmt.executeQuery("show tables"); + while (tables.next()) { + TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); + rowData.setString(0, dbname); + rowData.setString(2, tables.getString("table_name")); + rowData.setString(3, "TABLE"); + rowData.setString(4, ""); + rowDataList.add(rowData); + } + + ResultSet stables = stmt.executeQuery("show stables"); + while (stables.next()) { + TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); + rowData.setString(0, dbname); + rowData.setString(2, stables.getString("name")); + rowData.setString(3, "TABLE"); + rowData.setString(4, "STABLE"); + rowDataList.add(rowData); + } + resultSet.setRowDataList(rowDataList); + return resultSet; + } + } + public ResultSet getSchemas() throws SQLException { return getEmptyResultSet(); } @@ -803,4 +899,298 @@ public abstract class AbstractDatabaseMetaData implements DatabaseMetaData, Wrap public boolean isWrapperFor(Class iface) throws SQLException { return iface.isInstance(this); } + + protected ResultSet getCatalogs(Connection conn) throws SQLException { + try (Statement stmt = conn.createStatement()) { + DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); + // set up ColumnMetaDataList + List columnMetaDataList = new ArrayList<>(); + // 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); + + List rowDataList = new ArrayList<>(); + ResultSet rs = stmt.executeQuery("show databases"); + while (rs.next()) { + TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); + rowData.setString(0, rs.getString("name")); + rowDataList.add(rowData); + } + resultSet.setRowDataList(rowDataList); + return resultSet; + } + } + + protected ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern, Connection conn) { + try (Statement stmt = conn.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(); + // set up ColumnMetaDataList + + List 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); + // DECIMAL_DIGITS + ColumnMetaData col9 = new ColumnMetaData(); + col9.setColIndex(9); + col9.setColName("DECIMAL_DIGITS"); + col9.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); + columnMetaDataList.add(col9); + // add NUM_PREC_RADIX + ColumnMetaData col10 = new ColumnMetaData(); + col10.setColIndex(10); + col10.setColName("NUM_PREC_RADIX"); + col10.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); + columnMetaDataList.add(col10); + // NULLABLE + ColumnMetaData col11 = new ColumnMetaData(); + col11.setColIndex(11); + col11.setColName("NULLABLE"); + col11.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); + columnMetaDataList.add(col11); + // REMARKS + ColumnMetaData col12 = new ColumnMetaData(); + col12.setColIndex(12); + col12.setColName("REMARKS"); + col12.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); + columnMetaDataList.add(col12); + + resultSet.setColumnMetaDataList(columnMetaDataList); + // set up rowDataList + ResultSet rs = stmt.executeQuery("describe " + dbname + "." + tableNamePattern); + List 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) { + e.printStackTrace(); + } + return null; + } + + protected ResultSet getPrimaryKeys(String catalog, String schema, String table, Connection conn) throws SQLException { + try (Statement stmt = conn.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(); + // set up ColumnMetaDataList + List columnMetaDataList = new ArrayList<>(); + // 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 + List rowDataList = new ArrayList<>(); + ResultSet rs = stmt.executeQuery("describe " + dbname + "." + table); + rs.next(); + TSDBResultSetRowData rowData = new TSDBResultSetRowData(6); + rowData.setString(0, null); + rowData.setString(1, null); + rowData.setString(2, table); + String pkName = rs.getString(1); + rowData.setString(3, pkName); + rowData.setInt(4, 1); + rowData.setString(5, pkName); + rowDataList.add(rowData); + resultSet.setRowDataList(rowDataList); + return resultSet; + } + } + + protected ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern, Connection conn) throws SQLException { + try (Statement stmt = conn.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(); + // set up ColumnMetaDataList + List columnMetaDataList = new ArrayList<>(); + // 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); + // 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 + "'"); + List rowDataList = new ArrayList<>(); + while (rs.next()) { + TSDBResultSetRowData rowData = new TSDBResultSetRowData(4); + rowData.setString(2, rs.getString(1)); + rowData.setString(3, rs.getString(4)); + rowDataList.add(rowData); + } + resultSet.setRowDataList(rowDataList); + return resultSet; + } + } } \ No newline at end of file diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java index 633fdcd5ab..fe16aa6535 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/ColumnMetaData.java @@ -16,40 +16,40 @@ package com.taosdata.jdbc; public class ColumnMetaData { - private int colType = 0; - private String colName = null; - private int colSize = -1; - private int colIndex = 0; - - public int getColSize() { - return colSize; - } - - public void setColSize(int colSize) { - this.colSize = colSize; - } - - public int getColType() { - return colType; - } - - public void setColType(int colType) { - this.colType = colType; - } - - public String getColName() { - return colName; - } - - public void setColName(String colName) { - this.colName = colName; - } - - public int getColIndex() { - return colIndex; - } - - public void setColIndex(int colIndex) { - this.colIndex = colIndex; - } + private int colType = 0; + private String colName = null; + private int colSize = -1; + private int colIndex = 0; + + public int getColSize() { + return colSize; + } + + public void setColSize(int colSize) { + this.colSize = colSize; + } + + public int getColType() { + return colType; + } + + public void setColType(int colType) { + this.colType = colType; + } + + public String getColName() { + return colName; + } + + public void setColName(String colName) { + this.colName = colName; + } + + public int getColIndex() { + return colIndex; + } + + public void setColIndex(int colIndex) { + this.colIndex = colIndex; + } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java index 7124154682..d1f1e77b1c 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java @@ -14,9 +14,9 @@ *****************************************************************************/ package com.taosdata.jdbc; -import java.sql.*; -import java.util.ArrayList; -import java.util.List; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; public class TSDBDatabaseMetaData extends AbstractDatabaseMetaData { @@ -59,435 +59,38 @@ public class TSDBDatabaseMetaData extends AbstractDatabaseMetaData { if (conn == null || conn.isClosed()) { throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); } - - try (Statement stmt = conn.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 columnMetaDataList = new ArrayList<>(); - ColumnMetaData col1 = new ColumnMetaData(); - col1.setColIndex(1); - col1.setColName("TABLE_CAT"); - col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col1); - ColumnMetaData col2 = new ColumnMetaData(); - col2.setColIndex(2); - col2.setColName("TABLE_SCHEM"); - col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col2); - ColumnMetaData col3 = new ColumnMetaData(); - col3.setColIndex(3); - col3.setColName("TABLE_NAME"); - col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - 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(); - col6.setColIndex(6); - col6.setColName("TYPE_CAT"); - col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col6); - ColumnMetaData col7 = new ColumnMetaData(); - col7.setColIndex(7); - col7.setColName("TYPE_SCHEM"); - col7.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col7); - ColumnMetaData col8 = new ColumnMetaData(); - col8.setColIndex(8); - col8.setColName("TYPE_NAME"); - col8.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col8); - ColumnMetaData col9 = new ColumnMetaData(); - col9.setColIndex(9); - col9.setColName("SELF_REFERENCING_COL_NAME"); - col9.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col9); - ColumnMetaData col10 = new ColumnMetaData(); - col10.setColIndex(10); - col10.setColName("REF_GENERATION"); - col10.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col10); - resultSet.setColumnMetaDataList(columnMetaDataList); - - List rowDataList = new ArrayList<>(); - ResultSet tables = stmt.executeQuery("show tables"); - while (tables.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); - rowData.setString(0, dbname); - rowData.setString(2, tables.getString("table_name")); - rowData.setString(3, "TABLE"); - rowData.setString(4, ""); - rowDataList.add(rowData); - } - - ResultSet stables = stmt.executeQuery("show stables"); - while (stables.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); - rowData.setString(0, dbname); - rowData.setString(2, stables.getString("name")); - rowData.setString(3, "TABLE"); - rowData.setString(4, "STABLE"); - rowDataList.add(rowData); - } - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getTables(catalog, schemaPattern, tableNamePattern, types, conn); } + public ResultSet getCatalogs() throws SQLException { if (conn == null || conn.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - try (Statement stmt = conn.createStatement()) { - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); - // set up ColumnMetaDataList - List 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); - resultSet.setColumnMetaDataList(columnMetaDataList); - - List rowDataList = new ArrayList<>(); - ResultSet rs = stmt.executeQuery("show databases"); - while (rs.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); - rowData.setString(0, rs.getString("name")); - rowDataList.add(rowData); - } - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getCatalogs(conn); } public ResultSet getTableTypes() throws SQLException { if (conn == null || conn.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); - // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - 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 - List rowDataList = new ArrayList<>(); - TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); - rowData.setString(0, "TABLE"); - rowDataList.add(rowData); - rowData = new TSDBResultSetRowData(1); - rowData.setString(0, "STABLE"); - rowDataList.add(rowData); - resultSet.setRowDataList(rowDataList); - - return resultSet; + return super.getTableTypes(); } public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { if (conn == null || conn.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - try (Statement stmt = conn.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(); - // set up ColumnMetaDataList - List 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); - // DECIMAL_DIGITS - ColumnMetaData col9 = new ColumnMetaData(); - col9.setColIndex(9); - col9.setColName("DECIMAL_DIGITS"); - col9.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col9); - // add NUM_PREC_RADIX - ColumnMetaData col10 = new ColumnMetaData(); - col10.setColIndex(10); - col10.setColName("NUM_PREC_RADIX"); - col10.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col10); - // NULLABLE - ColumnMetaData col11 = new ColumnMetaData(); - col11.setColIndex(11); - col11.setColName("NULLABLE"); - col11.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col11); - // REMARKS - ColumnMetaData col12 = new ColumnMetaData(); - col12.setColIndex(12); - col12.setColName("REMARKS"); - col12.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col12); - resultSet.setColumnMetaDataList(columnMetaDataList); - - - // set up rowDataList - ResultSet rs = stmt.executeQuery("describe " + dbname + "." + tableNamePattern); - List 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) { - e.printStackTrace(); - } - return null; + return super.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern, conn); } public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { if (conn == null || conn.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - try (Statement stmt = conn.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(); - // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - // 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 - List rowDataList = new ArrayList<>(); - ResultSet rs = stmt.executeQuery("describe " + dbname + "." + table); - rs.next(); - TSDBResultSetRowData rowData = new TSDBResultSetRowData(6); - rowData.setString(0, null); - rowData.setString(1, null); - rowData.setString(2, table); - String pkName = rs.getString(1); - rowData.setString(3, pkName); - rowData.setInt(4, 1); - rowData.setString(5, pkName); - rowDataList.add(rowData); - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getPrimaryKeys(catalog, schema, table, conn); } - - public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws - SQLException { + public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { if (conn == null || conn.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - try (Statement stmt = conn.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(); - // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - // 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); - // 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 + "'"); - List rowDataList = new ArrayList<>(); - while (rs.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(4); - rowData.setString(2, rs.getString(1)); - rowData.setString(3, rs.getString(4)); - rowDataList.add(rowData); - } - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getSuperTables(catalog, schemaPattern, tableNamePattern, conn); } } \ No newline at end of file diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaData.java index f9d66978c2..71189a02d3 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaData.java @@ -38,409 +38,35 @@ public class RestfulDatabaseMetaData extends AbstractDatabaseMetaData { if (connection == null || connection.isClosed()) { throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); } - - 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 columnMetaDataList = new ArrayList<>(); - ColumnMetaData col1 = new ColumnMetaData(); - col1.setColIndex(1); - col1.setColName("TABLE_CAT"); - col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col1); - ColumnMetaData col2 = new ColumnMetaData(); - col2.setColIndex(2); - col2.setColName("TABLE_SCHEM"); - col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col2); - ColumnMetaData col3 = new ColumnMetaData(); - col3.setColIndex(3); - col3.setColName("TABLE_NAME"); - col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - 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(); - col6.setColIndex(6); - col6.setColName("TYPE_CAT"); - col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col6); - ColumnMetaData col7 = new ColumnMetaData(); - col7.setColIndex(7); - col7.setColName("TYPE_SCHEM"); - col7.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col7); - ColumnMetaData col8 = new ColumnMetaData(); - col8.setColIndex(8); - col8.setColName("TYPE_NAME"); - col8.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col8); - ColumnMetaData col9 = new ColumnMetaData(); - col9.setColIndex(9); - col9.setColName("SELF_REFERENCING_COL_NAME"); - col9.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col9); - ColumnMetaData col10 = new ColumnMetaData(); - col10.setColIndex(10); - col10.setColName("REF_GENERATION"); - col10.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col10); - resultSet.setColumnMetaDataList(columnMetaDataList); - - List rowDataList = new ArrayList<>(); - ResultSet tables = stmt.executeQuery("show tables"); - while (tables.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); - rowData.setString(0, dbname); - rowData.setString(2, tables.getString("table_name")); - rowData.setString(3, "TABLE"); - rowData.setString(4, ""); - rowDataList.add(rowData); - } - - ResultSet stables = stmt.executeQuery("show stables"); - while (stables.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); - rowData.setString(0, dbname); - rowData.setString(2, stables.getString("name")); - rowData.setString(3, "TABLE"); - rowData.setString(4, "STABLE"); - rowDataList.add(rowData); - } - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getTables(catalog, schemaPattern, tableNamePattern, types, connection); } @Override public ResultSet getCatalogs() throws SQLException { if (connection == null || connection.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - try (Statement stmt = connection.createStatement()) { - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); - // set up ColumnMetaDataList - List 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); - resultSet.setColumnMetaDataList(columnMetaDataList); - - List rowDataList = new ArrayList<>(); - ResultSet rs = stmt.executeQuery("show databases"); - while (rs.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); - rowData.setString(0, rs.getString("name")); - rowDataList.add(rowData); - } - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getCatalogs(connection); } @Override public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { if (connection == null || connection.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - 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(); - // set up ColumnMetaDataList - List 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); - // DECIMAL_DIGITS - ColumnMetaData col9 = new ColumnMetaData(); - col9.setColIndex(9); - col9.setColName("DECIMAL_DIGITS"); - col9.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col9); - // add NUM_PREC_RADIX - ColumnMetaData col10 = new ColumnMetaData(); - col10.setColIndex(10); - col10.setColName("NUM_PREC_RADIX"); - col10.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col10); - // NULLABLE - ColumnMetaData col11 = new ColumnMetaData(); - col11.setColIndex(11); - col11.setColName("NULLABLE"); - col11.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col11); - // REMARKS - ColumnMetaData col12 = new ColumnMetaData(); - col12.setColIndex(12); - col12.setColName("REMARKS"); - col12.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col12); - resultSet.setColumnMetaDataList(columnMetaDataList); - - - // set up rowDataList - ResultSet rs = stmt.executeQuery("describe " + dbname + "." + tableNamePattern); - List 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) { - e.printStackTrace(); - } - return null; + return super.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern, connection); } @Override public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { if (connection == null || connection.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - 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(); - // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - // 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 - List rowDataList = new ArrayList<>(); - ResultSet rs = stmt.executeQuery("describe " + dbname + "." + table); - rs.next(); - TSDBResultSetRowData rowData = new TSDBResultSetRowData(6); - rowData.setString(0, null); - rowData.setString(1, null); - rowData.setString(2, table); - String pkName = rs.getString(1); - rowData.setString(3, pkName); - rowData.setInt(4, 1); - rowData.setString(5, pkName); - rowDataList.add(rowData); - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getPrimaryKeys(catalog, schema, table, connection); } @Override public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { if (connection == null || connection.isClosed()) throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); - - 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(); - // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - // 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); - // 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 + "'"); - List rowDataList = new ArrayList<>(); - while (rs.next()) { - TSDBResultSetRowData rowData = new TSDBResultSetRowData(4); - rowData.setString(2, rs.getString(1)); - rowData.setString(3, rs.getString(4)); - rowDataList.add(rowData); - } - resultSet.setRowDataList(rowDataList); - return resultSet; - } + return super.getSuperTables(catalog, schemaPattern, tableNamePattern, connection); } } -- GitLab