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,31 +245,15 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -246,31 +245,15 @@ 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 { traceOperation("SESSION_SET_AUTOCOMMIT", autoCommit ? 1 : 0);
traceOperation("SESSION_SET_AUTOCOMMIT", autoCommit ? 1 : 0); transfer.writeInt(SessionRemote.SESSION_SET_AUTOCOMMIT).
transfer.writeInt(SessionRemote.SESSION_SET_AUTOCOMMIT). writeBoolean(autoCommit);
writeBoolean(autoCommit); done(transfer);
done(transfer); } catch (IOException e) {
} catch (IOException e) { 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,19 +143,44 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -143,19 +143,44 @@ 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
StringBuilder select = new StringBuilder(1008);
if (includeSynonyms) {
select.append("SELECT "
+ "TABLE_CAT, "
+ "TABLE_SCHEM, "
+ "TABLE_NAME, "
+ "TABLE_TYPE, "
+ "REMARKS, "
+ "TYPE_CAT, "
+ "TYPE_SCHEM, "
+ "TYPE_NAME, "
+ "SELF_REFERENCING_COL_NAME, "
+ "REF_GENERATION, "
+ "SQL "
+ "FROM ("
+ "SELECT "
+ "SYNONYM_CATALOG TABLE_CAT, "
+ "SYNONYM_SCHEMA TABLE_SCHEM, "
+ "SYNONYM_NAME as TABLE_NAME, "
+ "TYPE_NAME AS TABLE_TYPE, "
+ "REMARKS, "
+ "TYPE_NAME TYPE_CAT, "
+ "TYPE_NAME TYPE_SCHEM, "
+ "TYPE_NAME AS TYPE_NAME, "
+ "TYPE_NAME SELF_REFERENCING_COL_NAME, "
+ "TYPE_NAME REF_GENERATION, "
+ "NULL AS SQL "
+ "FROM INFORMATION_SCHEMA.SYNONYMS "
+ "WHERE SYNONYM_CATALOG LIKE ?1 ESCAPE ?4 "
+ "AND SYNONYM_SCHEMA LIKE ?2 ESCAPE ?4 "
+ "AND SYNONYM_NAME LIKE ?3 ESCAPE ?4 "
+ "UNION ");
}
select.append("SELECT "
+ "TABLE_CATALOG TABLE_CAT, " + "TABLE_CATALOG TABLE_CAT, "
+ "TABLE_SCHEMA TABLE_SCHEM, " + "TABLE_SCHEMA TABLE_SCHEM, "
+ "TABLE_NAME, " + "TABLE_NAME, "
...@@ -168,58 +193,30 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -168,58 +193,30 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+ "TYPE_NAME REF_GENERATION, " + "TYPE_NAME REF_GENERATION, "
+ "SQL " + "SQL "
+ "FROM INFORMATION_SCHEMA.TABLES " + "FROM INFORMATION_SCHEMA.TABLES "
+ "WHERE TABLE_CATALOG LIKE ? ESCAPE ? " + "WHERE TABLE_CATALOG LIKE ?1 ESCAPE ?4 "
+ "AND TABLE_SCHEMA LIKE ? ESCAPE ? " + "AND TABLE_SCHEMA LIKE ?2 ESCAPE ?4 "
+ "AND TABLE_NAME LIKE ? ESCAPE ? " + "AND TABLE_NAME LIKE ?3 ESCAPE ?4");
+ "AND (" + tableType + ") "; if (typesLength > 0) {
select.append(" AND TABLE_TYPE IN(");
boolean includeSynonyms = types == null || Arrays.asList(types).contains("SYNONYM"); for (int i = 0; i < typesLength; i++) {
String synonymSelect = "SELECT " if (i > 0) {
+ "SYNONYM_CATALOG TABLE_CAT, " select.append(", ");
+ "SYNONYM_SCHEMA TABLE_SCHEM, " }
+ "SYNONYM_NAME as TABLE_NAME, " select.append('?').append(i + 5);
+ "TYPE_NAME AS TABLE_TYPE, " }
+ "REMARKS, " select.append(')');
+ "TYPE_NAME TYPE_CAT, " }
+ "TYPE_NAME TYPE_SCHEM, " if (includeSynonyms) {
+ "TYPE_NAME AS TYPE_NAME, " select.append(')');
+ "TYPE_NAME SELF_REFERENCING_COL_NAME, " }
+ "TYPE_NAME REF_GENERATION, " PreparedStatement prep = conn.prepareAutoCloseStatement(
+ "NULL AS SQL " select.append(" ORDER BY TABLE_TYPE, TABLE_SCHEM, TABLE_NAME").toString());
+ "FROM INFORMATION_SCHEMA.SYNONYMS "
+ "WHERE SYNONYM_CATALOG LIKE ? ESCAPE ? "
+ "AND SYNONYM_SCHEMA LIKE ? ESCAPE ? "
+ "AND SYNONYM_NAME LIKE ? ESCAPE ? "
+ "AND (" + includeSynonyms + ") ";
PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT "
+ "TABLE_CAT, "
+ "TABLE_SCHEM, "
+ "TABLE_NAME, "
+ "TABLE_TYPE, "
+ "REMARKS, "
+ "TYPE_CAT, "
+ "TYPE_SCHEM, "
+ "TYPE_NAME, "
+ "SELF_REFERENCING_COL_NAME, "
+ "REF_GENERATION, "
+ "SQL "
+ "FROM (" + synonymSelect + " UNION " + tableSelect + ") "
+ "ORDER BY TABLE_TYPE, TABLE_SCHEM, TABLE_NAME");
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论