Unverified 提交 f622267f authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #991 from katzyn/misc

Generate shorter queries in JdbcDatabaseMetaData.getTables() and remove some dead code
...@@ -73,7 +73,6 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -73,7 +73,6 @@ public class SessionRemote extends SessionWithState implements DataHandler {
private ArrayList<Transfer> transferList = New.arrayList(); private ArrayList<Transfer> transferList = New.arrayList();
private int nextId; private int nextId;
private boolean autoCommit = true; private boolean autoCommit = true;
private CommandInterface autoCommitFalse, autoCommitTrue;
private ConnectionInfo connectionInfo; private ConnectionInfo connectionInfo;
private String databaseName; private String databaseName;
private String cipher; private String cipher;
...@@ -246,7 +245,6 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -246,7 +245,6 @@ public class SessionRemote extends SessionWithState implements DataHandler {
} }
private synchronized void setAutoCommitSend(boolean autoCommit) { private synchronized void setAutoCommitSend(boolean autoCommit) {
if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_8) {
for (int i = 0, count = 0; i < transferList.size(); i++) { for (int i = 0, count = 0; i < transferList.size(); i++) {
Transfer transfer = transferList.get(i); Transfer transfer = transferList.get(i);
try { try {
...@@ -258,21 +256,6 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -258,21 +256,6 @@ public class SessionRemote extends SessionWithState implements DataHandler {
removeServer(e, i--, ++count); removeServer(e, i--, ++count);
} }
} }
} else {
if (autoCommit) {
if (autoCommitTrue == null) {
autoCommitTrue = prepareCommand(
"SET AUTOCOMMIT TRUE", Integer.MAX_VALUE);
}
autoCommitTrue.executeUpdate(false);
} else {
if (autoCommitFalse == null) {
autoCommitFalse = prepareCommand(
"SET AUTOCOMMIT FALSE", Integer.MAX_VALUE);
}
autoCommitFalse.executeUpdate(false);
}
}
} }
/** /**
......
...@@ -143,38 +143,26 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -143,38 +143,26 @@ public class JdbcDatabaseMetaData extends TraceObject implements
", " + quoteArray(types) + ");"); ", " + quoteArray(types) + ");");
} }
checkClosed(); checkClosed();
String tableType; int typesLength = types != null ? types.length : 0;
if (types != null && types.length > 0) { boolean includeSynonyms = types == null || Arrays.asList(types).contains("SYNONYM");
StatementBuilder buff = new StatementBuilder("TABLE_TYPE IN(");
for (String ignored : types) {
buff.appendExceptFirst(", ");
buff.append('?');
}
tableType = buff.append(')').toString();
} else {
tableType = "TRUE";
}
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 ? ESCAPE ? " + "SELECT "
+ "AND TABLE_SCHEMA LIKE ? ESCAPE ? "
+ "AND TABLE_NAME LIKE ? ESCAPE ? "
+ "AND (" + tableType + ") ";
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,39 +175,48 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -187,39 +175,48 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+ "TYPE_NAME REF_GENERATION, " + "TYPE_NAME REF_GENERATION, "
+ "NULL AS SQL " + "NULL AS SQL "
+ "FROM INFORMATION_SCHEMA.SYNONYMS " + "FROM INFORMATION_SCHEMA.SYNONYMS "
+ "WHERE SYNONYM_CATALOG LIKE ? ESCAPE ? " + "WHERE SYNONYM_CATALOG LIKE ?1 ESCAPE ?4 "
+ "AND SYNONYM_SCHEMA LIKE ? ESCAPE ? " + "AND SYNONYM_SCHEMA LIKE ?2 ESCAPE ?4 "
+ "AND SYNONYM_NAME LIKE ? ESCAPE ? " + "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, "\\"); prep.setString(2, getSchemaPattern(schemaPattern));
prep.setString(3, getSchemaPattern(schemaPattern)); prep.setString(3, getPattern(tableNamePattern));
prep.setString(4, "\\"); prep.setString(4, "\\");
prep.setString(5, getPattern(tableNamePattern)); for (int i = 0; i < typesLength; i++) {
prep.setString(6, "\\"); prep.setString(5 + i, types[i]);
prep.setString(7, getCatalogPattern(catalogPattern));
prep.setString(8, "\\");
prep.setString(9, getSchemaPattern(schemaPattern));
prep.setString(10, "\\");
prep.setString(11, getPattern(tableNamePattern));
prep.setString(12, "\\");
for (int i = 0; types != null && i < types.length; i++) {
prep.setString(13 + i, types[i]);
} }
return prep.executeQuery(); return prep.executeQuery();
} catch (Exception e) { } catch (Exception e) {
......
...@@ -1250,7 +1250,6 @@ public class JdbcPreparedStatement extends JdbcStatement implements ...@@ -1250,7 +1250,6 @@ public class JdbcPreparedStatement extends JdbcStatement implements
@Override @Override
public int[] executeBatch() throws SQLException { public int[] executeBatch() throws SQLException {
try { try {
int id = getNextId(TraceObject.PREPARED_STATEMENT);
debugCodeCall("executeBatch"); debugCodeCall("executeBatch");
if (batchParameters == null) { if (batchParameters == null) {
// TODO batch: check what other database do if no parameters are // TODO batch: check what other database do if no parameters are
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论