Skip to content

Commit

Permalink
Added api to look up table names in a catalog/schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Dec 27, 2012
1 parent 215fd38 commit 47ea75d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;

Expand All @@ -28,10 +29,10 @@

/**
* Represents a database foreign key.
*
* @version $Revision: 504014 $
*/
public class ForeignKey implements Cloneable {
public class ForeignKey implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;

/** The name of the foreign key, may be <code>null</code>. */
private String name;
Expand Down
Expand Up @@ -12,8 +12,10 @@ public interface IDdlReader {

public Table readTable(String catalog, String schema, String tableName);

public List<String> getCatalogs();
public List<String> getCatalogNames();

public List<String> getSchemas(String catalog);
public List<String> getSchemaNames(String catalog);

public List<String> getTableNames(String catalog, String schema, String[] tableTypes);

}
Expand Up @@ -29,11 +29,15 @@ public class SqliteDdlReader implements IDdlReader {
public SqliteDdlReader(IDatabasePlatform platform) {
this.platform = platform;
}

public Database readTables(String catalog, String schema, String[] tableTypes) {
List<String> tableNames = platform.getSqlTemplate()
public List<String> getTableNames(String catalog, String schema, String[] tableTypes) {
return platform.getSqlTemplate()
.query("select tbl_name from sqlite_master where type='table'",
SqlConstants.STRING_MAPPER);
}

public Database readTables(String catalog, String schema, String[] tableTypes) {
List<String> tableNames = getTableNames(catalog, schema, tableTypes);
Database database = new Database();
for (String tableName : tableNames) {
Table table = readTable(catalog, schema, tableName);
Expand Down Expand Up @@ -98,12 +102,12 @@ public Table readTable(String catalog, String schema, String tableName) {
return table;
}

public List<String> getCatalogs() {
public List<String> getCatalogNames() {
return new ArrayList<String>(0);
}


public List<String> getSchemas(String catalog) {
public List<String> getSchemaNames(String catalog) {
return new ArrayList<String>(0);
}

Expand Down
Expand Up @@ -1221,7 +1221,7 @@ protected String unescape(String text, String unescaped, String escaped) {
return result;
}

public List<String> getCatalogs() {
public List<String> getCatalogNames() {
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
return sqlTemplate.execute(new IConnectionCallback<List<String>>() {
public List<String> execute(Connection connection) throws SQLException {
Expand All @@ -1241,7 +1241,7 @@ public List<String> execute(Connection connection) throws SQLException {
});
}

public List<String> getSchemas(final String catalog) {
public List<String> getSchemaNames(final String catalog) {
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
return sqlTemplate.execute(new IConnectionCallback<List<String>>() {
public List<String> execute(Connection connection) throws SQLException {
Expand All @@ -1251,11 +1251,16 @@ public List<String> execute(Connection connection) throws SQLException {
try {
rs = meta.getSchemas();
while (rs.next()) {
int columnCount = rs.getMetaData().getColumnCount();
String schema = rs.getString(1);
String schemaCatalog = rs.getString(2);
String schemaCatalog = null;
if (columnCount > 1) {
schemaCatalog = rs.getString(2);
}
if (StringUtils.isBlank(catalog) && !schemas.contains(schema)) {
schemas.add(schema);
} else if (StringUtils.isNotBlank(schemaCatalog) && schemaCatalog.equals(catalog)) {
} else if (StringUtils.isNotBlank(schemaCatalog)
&& schemaCatalog.equals(catalog)) {
schemas.add(schema);
}
}
Expand All @@ -1267,4 +1272,26 @@ public List<String> execute(Connection connection) throws SQLException {
});
}

public List<String> getTableNames(final String catalog, final String schema,
final String[] tableTypes) {
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
return sqlTemplate.execute(new IConnectionCallback<List<String>>() {
public List<String> execute(Connection connection) throws SQLException {
ArrayList<String> list = new ArrayList<String>();
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = null;
try {
rs = meta.getTables(catalog, schema, null, tableTypes);
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
list.add(tableName);
}
return list;
} finally {
JdbcSqlTemplate.close(rs);
}
}
});
}

}
Expand Up @@ -54,9 +54,9 @@ public static BasicDataSource create(TypedProperties properties,
}
dataSource.setPassword(password);
dataSource.setInitialSize(properties.getInt(
BasicDataSourcePropertyConstants.DB_POOL_INITIAL_SIZE, 5));
BasicDataSourcePropertyConstants.DB_POOL_INITIAL_SIZE, 2));
dataSource.setMaxActive(properties.getInt(
BasicDataSourcePropertyConstants.DB_POOL_MAX_ACTIVE, 20));
BasicDataSourcePropertyConstants.DB_POOL_MAX_ACTIVE, 10));
dataSource.setMaxWait(properties.getInt(BasicDataSourcePropertyConstants.DB_POOL_MAX_WAIT,
5000));
dataSource.setMinEvictableIdleTimeMillis(properties.getInt(
Expand Down

0 comments on commit 47ea75d

Please sign in to comment.