Skip to content

Commit

Permalink
0006552: SQL Anywhere fails to call DatabaseMetaData.getSchemas()
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Marzullo committed Aug 14, 2024
1 parent dbc2688 commit 381b4d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,6 @@ public List<String> getSchemaNames(final String catalog) {
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplateDirty();
return sqlTemplate.execute(new IConnectionCallback<List<String>>() {
public List<String> execute(Connection connection) throws SQLException {
ArrayList<String> schemas = new ArrayList<String>();
IConnectionHandler connectionHandler = getConnectionHandler(catalog);
if (connectionHandler != null) {
connectionHandler.before(connection);
Expand All @@ -1487,24 +1486,13 @@ public List<String> execute(Connection connection) throws SQLException {
try {
try {
rs = meta.getSchemas();
return processSchemaResultSet(rs, catalog);
} catch (SQLException e) {
rs = meta.getSchemas("", null);
close(rs);
rs = null;
rs = getSchemasHandleException(connection, meta, "", null);
return processSchemaResultSet(rs, catalog);
}
while (rs.next()) {
int columnCount = rs.getMetaData().getColumnCount();
String schema = rs.getString(1);
String schemaCatalog = null;
if (columnCount > 1) {
schemaCatalog = rs.getString(2);
}
if ((StringUtils.isBlank(schemaCatalog) || StringUtils.isBlank(catalog)) && !schemas.contains(schema)) {
schemas.add(schema);
} else if (StringUtils.isNotBlank(schemaCatalog)
&& schemaCatalog.equals(catalog)) {
schemas.add(schema);
}
}
return schemas;
} finally {
if (connectionHandler != null) {
connectionHandler.after(connection);
Expand All @@ -1515,6 +1503,30 @@ public List<String> execute(Connection connection) throws SQLException {
});
}

protected ArrayList<String> processSchemaResultSet(ResultSet rs, String catalog) throws SQLException {
ArrayList<String> schemas = new ArrayList<String>();
while (rs.next()) {
int columnCount = rs.getMetaData().getColumnCount();
String schema = rs.getString(1);
String schemaCatalog = null;
if (columnCount > 1) {
schemaCatalog = rs.getString(2);
}
if ((StringUtils.isBlank(schemaCatalog) || StringUtils.isBlank(catalog)) && !schemas.contains(schema)) {
schemas.add(schema);
} else if (StringUtils.isNotBlank(schemaCatalog)
&& schemaCatalog.equals(catalog)) {
schemas.add(schema);
}
}

return schemas;
}

protected ResultSet getSchemasHandleException(Connection connection, DatabaseMetaData meta, String catalog, String schemaPattern) throws SQLException {
return meta.getSchemas(catalog, schemaPattern);
}

protected IConnectionHandler getConnectionHandler(String catalog) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -83,6 +84,13 @@ public SqlAnywhereDdlReader(IDatabasePlatform platform) {
setDefaultTablePattern("%");
}

@Override
protected ResultSet getSchemasHandleException(Connection connection, DatabaseMetaData meta, String catalog, String schemaPattern) throws SQLException {
Statement stmt = connection.createStatement();
String sql = "select distinct table_owner as TABLE_SCHEM, table_qualifier as TABLE_CATALOG from sp_tables()";
return stmt.executeQuery(sql);
}

@Override
protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaData,
Map<String, Object> values) throws SQLException {
Expand Down

0 comments on commit 381b4d8

Please sign in to comment.