未验证 提交 2865a5be 编写于 作者: H huolibo 提交者: GitHub

[TD-14219]<fix>: change getTables for tableNamePattern (#10926)

上级 4a4d1431
package com.taosdata.jdbc; package com.taosdata.jdbc;
import com.taosdata.jdbc.utils.StringUtils;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public abstract class AbstractDatabaseMetaData extends WrapperImpl implements DatabaseMetaData { public abstract class AbstractDatabaseMetaData extends WrapperImpl implements DatabaseMetaData {
...@@ -550,9 +553,9 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -550,9 +553,9 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
} }
protected ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types, Connection connection) throws SQLException { protected ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types, Connection connection) throws SQLException {
if (catalog == null || catalog.isEmpty()) if (StringUtils.isEmpty(catalog))
return null; catalog = this.getConnection().getCatalog();
if (!isAvailableCatalog(connection, catalog)) if (!StringUtils.isEmpty(catalog) && !isAvailableCatalog(connection, catalog))
return new EmptyResultSet(); return new EmptyResultSet();
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
...@@ -561,8 +564,27 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -561,8 +564,27 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
// set row data // set row data
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(); List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
stmt.execute("use " + catalog); List<String> dbs = new ArrayList<>();
try (ResultSet tables = stmt.executeQuery("show tables")) { if (!StringUtils.isEmpty(catalog)) {
dbs.add(catalog);
} else {
ResultSet dbRs = stmt.executeQuery("show databases");
while (dbRs.next()) {
dbs.add(dbRs.getString("name"));
}
}
if (dbs.isEmpty()) {
return new EmptyResultSet();
}
for (String db : dbs) {
stmt.execute("use " + db);
String sql = "show tables ";
String Ssql = "show stables ";
if (!StringUtils.isEmpty(tableNamePattern)) {
sql = sql + "like '" + tableNamePattern + "'";
Ssql = Ssql + "like '" + tableNamePattern + "'";
}
ResultSet tables = stmt.executeQuery(sql);
while (tables.next()) { while (tables.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); TSDBResultSetRowData rowData = new TSDBResultSetRowData(10);
rowData.setStringValue(1, catalog); //TABLE_CAT rowData.setStringValue(1, catalog); //TABLE_CAT
...@@ -572,8 +594,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da ...@@ -572,8 +594,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da
rowData.setStringValue(5, ""); //REMARKS rowData.setStringValue(5, ""); //REMARKS
rowDataList.add(rowData); rowDataList.add(rowData);
} }
} ResultSet stables = stmt.executeQuery(Ssql);
try (ResultSet stables = stmt.executeQuery("show stables")) {
while (stables.next()) { while (stables.next()) {
TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); TSDBResultSetRowData rowData = new TSDBResultSetRowData(10);
rowData.setStringValue(1, catalog); //TABLE_CAT rowData.setStringValue(1, catalog); //TABLE_CAT
......
package com.taosdata.jdbc.utils;
public class StringUtils {
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册