|
9 | 9 | import java.util.Collections; |
10 | 10 | import java.util.Comparator; |
11 | 11 | import java.util.HashSet; |
12 | | -import java.util.LinkedHashMap; |
13 | 12 | import java.util.List; |
14 | 13 | import java.util.Map; |
15 | 14 | import java.util.Objects; |
|
24 | 23 |
|
25 | 24 | import com.google.common.base.Strings; |
26 | 25 |
|
| 26 | +import tech.ydb.core.StatusCode; |
27 | 27 | import tech.ydb.jdbc.YdbConnection; |
28 | 28 | import tech.ydb.jdbc.YdbConst; |
29 | 29 | import tech.ydb.jdbc.YdbDatabaseMetaData; |
30 | 30 | import tech.ydb.jdbc.YdbDriverInfo; |
31 | 31 | import tech.ydb.jdbc.YdbTypes; |
32 | 32 | import tech.ydb.jdbc.common.YdbFunctions; |
33 | 33 | import tech.ydb.jdbc.context.YdbExecutor; |
| 34 | +import tech.ydb.jdbc.exception.YdbExecutionStatusException; |
34 | 35 | import tech.ydb.proto.scheme.SchemeOperationProtos; |
35 | 36 | import tech.ydb.scheme.SchemeClient; |
36 | 37 | import tech.ydb.scheme.description.ListDirectoryResult; |
@@ -764,7 +765,9 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa |
764 | 765 | } |
765 | 766 |
|
766 | 767 | Predicate<String> columnFilter = equalsFilter(columnNamePattern); |
767 | | - return fromTables(tableNamePattern, |
| 768 | + Collection<String> tables = listTables(tableNamePattern); |
| 769 | + |
| 770 | + return fromTables(tables, |
768 | 771 | (tableName, tableDesc, rows) -> { |
769 | 772 | short index = 0; |
770 | 773 | for (TableColumn column : tableDesc.getColumns()) { |
@@ -849,7 +852,7 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl |
849 | 852 | } |
850 | 853 |
|
851 | 854 | // Only primary keys could be used as row identifiers |
852 | | - return fromTables(table, |
| 855 | + return fromTables(Collections.singletonList(table), |
853 | 856 | (tableName, tableDesc, rows) -> { |
854 | 857 | Map<String, TableColumn> columnMap = tableDesc.getColumns().stream() |
855 | 858 | .collect(Collectors.toMap(TableColumn::getName, Function.identity())); |
@@ -899,7 +902,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr |
899 | 902 | return fromEmptyResultSet(); |
900 | 903 | } |
901 | 904 |
|
902 | | - return fromTables(table, |
| 905 | + return fromTables(Collections.singletonList(table), |
903 | 906 | (tableName, tableDesc, rows) -> { |
904 | 907 | short index = 0; |
905 | 908 | for (String key : tableDesc.getPrimaryKeys()) { |
@@ -1039,7 +1042,7 @@ public ResultSet getIndexInfo(String catalog, String schema, String table, boole |
1039 | 1042 | return fromEmptyResultSet(); |
1040 | 1043 | } |
1041 | 1044 |
|
1042 | | - return fromTables(table, |
| 1045 | + return fromTables(Collections.singletonList(table), |
1043 | 1046 | (tableName, tableDesc, rows) -> { |
1044 | 1047 | for (TableIndex tableIndex : tableDesc.getIndexes()) { |
1045 | 1048 | short index = 0; |
@@ -1281,21 +1284,6 @@ public boolean isWrapperFor(Class<?> iface) { |
1281 | 1284 | return iface.isAssignableFrom(getClass()); |
1282 | 1285 | } |
1283 | 1286 |
|
1284 | | - private Map<String, TableDescription> collectTableDescriptions(Session session, Collection<String> tables) throws SQLException { |
1285 | | - DescribeTableSettings settings = connection.withDefaultTimeout(new DescribeTableSettings()); |
1286 | | - String databaseWithSuffix = withSuffix(connection.getCtx().getDatabase()); |
1287 | | - |
1288 | | - Map<String, TableDescription> target = new LinkedHashMap<>(tables.size()); |
1289 | | - |
1290 | | - for (String table: tables) { |
1291 | | - target.put(table, executor.call("Get table description for " + table, |
1292 | | - () -> session.describeTable(databaseWithSuffix + table, settings) |
1293 | | - )); |
1294 | | - } |
1295 | | - |
1296 | | - return target; |
1297 | | - } |
1298 | | - |
1299 | 1287 | private Collection<String> listTables(String tableNamePattern) throws SQLException { |
1300 | 1288 | Predicate<String> filter = equalsFilter(tableNamePattern); |
1301 | 1289 |
|
@@ -1338,21 +1326,35 @@ private List<String> tables(String databasePrefix, String path, Predicate<String |
1338 | 1326 | return tables; |
1339 | 1327 | } |
1340 | 1328 |
|
1341 | | - private ResultSet fromTables(String tableNamePattern, |
| 1329 | + private ResultSet fromTables(Collection<String> tables, |
1342 | 1330 | TableCollector tableCollector, |
1343 | 1331 | Comparator<Map<String, Object>> comparator) throws SQLException { |
1344 | | - Collection<String> tables = listTables(tableNamePattern); |
1345 | 1332 | if (tables.isEmpty()) { |
1346 | 1333 | return fromEmptyResultSet(); |
1347 | 1334 | } |
1348 | 1335 |
|
| 1336 | + DescribeTableSettings settings = connection.withDefaultTimeout(new DescribeTableSettings()); |
| 1337 | + String databaseWithSuffix = withSuffix(connection.getCtx().getDatabase()); |
| 1338 | + |
1349 | 1339 | try (Session session = executor.createSession(connection.getCtx())) { |
1350 | | - Map<String, TableDescription> tableMap = collectTableDescriptions(session, tables); |
| 1340 | + List<Map<String, Object>> rows = new ArrayList<>(tables.size() * 16); |
1351 | 1341 |
|
1352 | | - List<Map<String, Object>> rows = new ArrayList<>(tableMap.size() * 16); |
1353 | | - for (Map.Entry<String, TableDescription> entry : tableMap.entrySet()) { |
1354 | | - tableCollector.collect(entry.getKey(), entry.getValue(), rows); |
| 1342 | + for (String table: tables) { |
| 1343 | + try { |
| 1344 | + TableDescription description = executor.call("Describe table " + table, |
| 1345 | + () -> session.describeTable(databaseWithSuffix + table, settings) |
| 1346 | + ); |
| 1347 | + tableCollector.collect(table, description, rows); |
| 1348 | + } catch (YdbExecutionStatusException ex) { |
| 1349 | + if (ex.getStatusCode() != StatusCode.SCHEME_ERROR) { // ignore scheme errors like path not found |
| 1350 | + throw ex; |
| 1351 | + } |
| 1352 | + LOGGER.log(Level.WARNING, "Cannot describe table {0} -> {1}", |
| 1353 | + new Object[]{ table, ex.getMessage() } |
| 1354 | + ); |
| 1355 | + } |
1355 | 1356 | } |
| 1357 | + |
1356 | 1358 | rows.sort(comparator); |
1357 | 1359 | return fromRows(rows); |
1358 | 1360 | } |
|
0 commit comments