提交 e8934d75 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use UNION in getTables() only if required

上级 77401651
...@@ -144,34 +144,25 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -144,34 +144,25 @@ public class JdbcDatabaseMetaData extends TraceObject implements
} }
checkClosed(); checkClosed();
int typesLength = types != null ? types.length : 0; int typesLength = types != null ? types.length : 0;
boolean includeSynonyms = types == null || Arrays.asList(types).contains("SYNONYM");
String tableSelect = "SELECT " // (1024 - 16) is enough for the most cases
+ "TABLE_CATALOG TABLE_CAT, " StringBuilder select = new StringBuilder(1008);
+ "TABLE_SCHEMA TABLE_SCHEM, " if (includeSynonyms) {
select.append("SELECT "
+ "TABLE_CAT, "
+ "TABLE_SCHEM, "
+ "TABLE_NAME, " + "TABLE_NAME, "
+ "TABLE_TYPE, " + "TABLE_TYPE, "
+ "REMARKS, " + "REMARKS, "
+ "TYPE_NAME TYPE_CAT, " + "TYPE_CAT, "
+ "TYPE_NAME TYPE_SCHEM, " + "TYPE_SCHEM, "
+ "TYPE_NAME, " + "TYPE_NAME, "
+ "TYPE_NAME SELF_REFERENCING_COL_NAME, " + "SELF_REFERENCING_COL_NAME, "
+ "TYPE_NAME REF_GENERATION, " + "REF_GENERATION, "
+ "SQL " + "SQL "
+ "FROM INFORMATION_SCHEMA.TABLES " + "FROM ("
+ "WHERE TABLE_CATALOG LIKE ?1 ESCAPE ?4 " + "SELECT "
+ "AND TABLE_SCHEMA LIKE ?2 ESCAPE ?4 "
+ "AND TABLE_NAME LIKE ?3 ESCAPE ?4";
if (typesLength > 0) {
StatementBuilder buff = new StatementBuilder(tableSelect).append(" AND TABLE_TYPE IN(");
for (int i = 0; i < typesLength; i++) {
buff.appendExceptFirst(", ");
buff.append('?').append(i + 5);
}
tableSelect = buff.append(')').toString();
}
boolean includeSynonyms = types == null || Arrays.asList(types).contains("SYNONYM");
String synonymSelect = "SELECT "
+ "SYNONYM_CATALOG TABLE_CAT, " + "SYNONYM_CATALOG TABLE_CAT, "
+ "SYNONYM_SCHEMA TABLE_SCHEM, " + "SYNONYM_SCHEMA TABLE_SCHEM, "
+ "SYNONYM_NAME as TABLE_NAME, " + "SYNONYM_NAME as TABLE_NAME, "
...@@ -187,22 +178,39 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -187,22 +178,39 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+ "WHERE SYNONYM_CATALOG LIKE ?1 ESCAPE ?4 " + "WHERE SYNONYM_CATALOG LIKE ?1 ESCAPE ?4 "
+ "AND SYNONYM_SCHEMA LIKE ?2 ESCAPE ?4 " + "AND SYNONYM_SCHEMA LIKE ?2 ESCAPE ?4 "
+ "AND SYNONYM_NAME LIKE ?3 ESCAPE ?4 " + "AND SYNONYM_NAME LIKE ?3 ESCAPE ?4 "
+ "AND (" + includeSynonyms + ") "; + "UNION ");
}
PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT " select.append("SELECT "
+ "TABLE_CAT, " + "TABLE_CATALOG TABLE_CAT, "
+ "TABLE_SCHEM, " + "TABLE_SCHEMA TABLE_SCHEM, "
+ "TABLE_NAME, " + "TABLE_NAME, "
+ "TABLE_TYPE, " + "TABLE_TYPE, "
+ "REMARKS, " + "REMARKS, "
+ "TYPE_CAT, " + "TYPE_NAME TYPE_CAT, "
+ "TYPE_SCHEM, " + "TYPE_NAME TYPE_SCHEM, "
+ "TYPE_NAME, " + "TYPE_NAME, "
+ "SELF_REFERENCING_COL_NAME, " + "TYPE_NAME SELF_REFERENCING_COL_NAME, "
+ "REF_GENERATION, " + "TYPE_NAME REF_GENERATION, "
+ "SQL " + "SQL "
+ "FROM (" + synonymSelect + " UNION " + tableSelect + ") " + "FROM INFORMATION_SCHEMA.TABLES "
+ "ORDER BY TABLE_TYPE, TABLE_SCHEM, TABLE_NAME"); + "WHERE TABLE_CATALOG LIKE ?1 ESCAPE ?4 "
+ "AND TABLE_SCHEMA LIKE ?2 ESCAPE ?4 "
+ "AND TABLE_NAME LIKE ?3 ESCAPE ?4");
if (typesLength > 0) {
select.append(" AND TABLE_TYPE IN(");
for (int i = 0; i < typesLength; i++) {
if (i > 0) {
select.append(", ");
}
select.append('?').append(i + 5);
}
select.append(')');
}
if (includeSynonyms) {
select.append(')');
}
PreparedStatement prep = conn.prepareAutoCloseStatement(
select.append(" ORDER BY TABLE_TYPE, TABLE_SCHEM, TABLE_NAME").toString());
prep.setString(1, getCatalogPattern(catalogPattern)); prep.setString(1, getCatalogPattern(catalogPattern));
prep.setString(2, getSchemaPattern(schemaPattern)); prep.setString(2, getSchemaPattern(schemaPattern));
prep.setString(3, getPattern(tableNamePattern)); prep.setString(3, getPattern(tableNamePattern));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论