Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Dec 26, 2012
1 parent a860ef2 commit bcd5aa5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 215 deletions.
Expand Up @@ -35,6 +35,7 @@
* Represents a column in the database model.
*/
public class Column implements Cloneable, Serializable {

/** Unique ID for serialization purposes. */
private static final long serialVersionUID = -6226348998874210093L;

Expand Down
@@ -1,6 +1,8 @@
package org.jumpmind.db.platform;


import java.util.List;

import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.Table;

Expand All @@ -10,4 +12,8 @@ public interface IDdlReader {

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

public List<String> getCatalogs();

public List<String> getSchemas(String catalog);

}
@@ -1,5 +1,6 @@
package org.jumpmind.db.platform.sqlite;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.NotImplementedException;
Expand Down Expand Up @@ -96,6 +97,15 @@ public Table readTable(String catalog, String schema, String tableName) {

return table;
}

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


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

static class ColumnMapper extends AbstractSqlRowMapper<Column> {
public Column mapRow(Row row) {
Expand Down Expand Up @@ -165,5 +175,7 @@ public IndexColumn mapRow(Row row) {
return column;
}
}



}
8 changes: 8 additions & 0 deletions symmetric-db/src/main/java/org/jumpmind/db/sql/Row.java
Expand Up @@ -192,4 +192,12 @@ protected void checkForColumn(String columnName) {
final private java.util.Date getDate(String value, String[] pattern) {
return FormatUtils.parseDate(value, pattern);
}

public Object[] toArray(String[] keys) {
Object[] values = new Object[keys.length];
for (int i = 0; i < keys.length; i++) {
values[i] = get(keys[i]);
}
return values;
}
}
Expand Up @@ -421,11 +421,11 @@ protected List<MetaDataColumnDescriptor> getColumnsForIndex() {
public Database getDatabase(Connection connection) throws SQLException {
return readTables(null, null, null);
}

protected String getResultSetSchemaName() {
return "TABLE_SCHEM";
}

protected String getResultSetCatalogName() {
return "TABLE_CAT";
}
Expand Down Expand Up @@ -624,19 +624,19 @@ protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaDat
return null;
}
}

table = new Table();
table.setName(tableName);
table.setType(type);

String catalog = (String) values.get(getResultSetCatalogName());
table.setCatalog(catalog);
metaData.setCatalog(catalog);

String schema = (String) values.get(getResultSetSchemaName());
table.setSchema(schema);
metaData.setSchemaPattern(schema);

table.setDescription((String) values.get("REMARKS"));

table.addColumns(readColumns(metaData, tableName));
Expand All @@ -655,7 +655,7 @@ protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaDat
}
return table;
}

protected String[] getUnsupportedTableTypes() {
return new String[0];
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ protected void readIndex(DatabaseMetaDataWrapper metaData, Map<String, Object> v

String columnName = (String) values.get("COLUMN_NAME");
if (columnName.startsWith("\"") && columnName.endsWith("\"")) {
columnName = columnName.substring(1, columnName.length()-1);
columnName = columnName.substring(1, columnName.length() - 1);
}
indexColumn.setName(columnName);
if (values.containsKey("ORDINAL_POSITION")) {
Expand Down Expand Up @@ -1110,7 +1110,7 @@ protected void determineAutoIncrementFromResultSetMetaData(Connection conn, Tabl
*
* @param columnsToCheck The columns to check (e.g. the primary key columns)
*/
public void determineAutoIncrementFromResultSetMetaData(Connection conn, Table table,
protected void determineAutoIncrementFromResultSetMetaData(Connection conn, Table table,
final Column columnsToCheck[], String catalogSeparator) throws SQLException {
StringBuilder query = new StringBuilder();
try {
Expand Down Expand Up @@ -1149,7 +1149,8 @@ public void determineAutoIncrementFromResultSetMetaData(Connection conn, Table t

for (int idx = 0; idx < columnsToCheck.length; idx++) {
if (log.isDebugEnabled()) {
log.debug(columnsToCheck[idx] + " is auto increment? " + rsMetaData.isAutoIncrement(idx + 1));
log.debug(columnsToCheck[idx] + " is auto increment? "
+ rsMetaData.isAutoIncrement(idx + 1));
}
if (rsMetaData.isAutoIncrement(idx + 1)) {
columnsToCheck[idx].setAutoIncrement(true);
Expand Down Expand Up @@ -1220,70 +1221,50 @@ protected String unescape(String text, String unescaped, String escaped) {
return result;
}

/*
* Tries to find the schema to which the given table belongs.
*
* @param connection The database connection
*
* @param schemaPattern The schema pattern to limit the schemas to search in
*
* @param table The table to search for
*
* @return The schema name or <code>null</code> if the schema of the table
* could not be found
*
* @deprecated Will be removed once full schema support is in place
*/
public String determineSchemaOf(Connection connection, String schemaPattern, Table table)
throws SQLException {
ResultSet tableData = null;
ResultSet columnData = null;

try {
DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();

metaData.setMetaData(connection.getMetaData());
metaData.setCatalog(getDefaultCatalogPattern());
metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern()
: schemaPattern);
metaData.setTableTypes(getDefaultTableTypes());

String tablePattern = table.getName();

if (getPlatform().getDdlBuilder().isDelimitedIdentifierModeOn()) {
tablePattern = tablePattern.toUpperCase();
public List<String> getCatalogs() {
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
return sqlTemplate.execute(new IConnectionCallback<List<String>>() {
public List<String> execute(Connection connection) throws SQLException {
ArrayList<String> catalogs = new ArrayList<String>();
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = null;
try {
rs = meta.getCatalogs();
while (rs.next()) {
catalogs.add(rs.getString(1));
}
return catalogs;
} finally {
JdbcSqlTemplate.close(rs);
}
}
});
}

tableData = metaData.getTables(tablePattern);

boolean found = false;
String schema = null;

while (!found && tableData.next()) {
Map<String, Object> values = readMetaData(tableData, getColumnsForTable());
String tableName = (String) values.get(getResultSetCatalogName());

if ((tableName != null) && (tableName.length() > 0)) {
schema = (String) values.get(getResultSetSchemaName());
columnData = metaData.getColumns(tableName, getDefaultColumnPattern());
found = true;

while (found && columnData.next()) {
values = readMetaData(columnData, getColumnsForColumn());

if (table.findColumn((String) values.get("COLUMN_NAME"), getPlatform()
.getDdlBuilder().isDelimitedIdentifierModeOn()) == null) {
found = false;
public List<String> getSchemas(final String catalog) {
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
return sqlTemplate.execute(new IConnectionCallback<List<String>>() {
public List<String> execute(Connection connection) throws SQLException {
ArrayList<String> schemas = new ArrayList<String>();
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = null;
try {
rs = meta.getSchemas();
while (rs.next()) {
String schema = rs.getString(1);
String schemaCatalog = rs.getString(2);
if (StringUtils.isBlank(catalog) && !schemas.contains(schema)) {
schemas.add(schema);
} else if (StringUtils.isNotBlank(schemaCatalog) && schemaCatalog.equals(catalog)) {
schemas.add(schema);
}
}
columnData.close();
columnData = null;
return schemas;
} finally {
JdbcSqlTemplate.close(rs);
}
}
return found ? schema : null;
} finally {
close(columnData);
close(tableData);
}
});
}

}
Expand Up @@ -355,79 +355,6 @@ protected boolean isInternalForeignKeyIndex(Connection connection,
}
}

@Override
public String determineSchemaOf(Connection connection, String schemaPattern, Table table)
throws SQLException {
ResultSet tableData = null;
ResultSet columnData = null;

try {
DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();

metaData.setMetaData(connection.getMetaData());
metaData.setCatalog(getDefaultCatalogPattern());
metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern()
: schemaPattern);
metaData.setTableTypes(getDefaultTableTypes());

String tablePattern = table.getName();

if (getPlatform().getDdlBuilder().isDelimitedIdentifierModeOn()) {
tablePattern = tablePattern.toUpperCase();
}

tableData = metaData.getTables(tablePattern);

boolean found = false;
String schema = null;

while (!found && tableData.next()) {
Map<String,Object> values = readMetaData(tableData, getColumnsForTable());
String tableName = (String) values.get("TABLE_NAME");

if ((tableName != null) && (tableName.length() > 0)) {
schema = (String) values.get("TABLE_SCHEM");
found = true;

if (getPlatform().getDdlBuilder().isDelimitedIdentifierModeOn()) {
// Jaybird has a problem when delimited identifiers are
// used as
// it is not able to find the columns for the table
// So we have to filter manually below
columnData = metaData.getColumns(getDefaultTablePattern(),
getDefaultColumnPattern());
} else {
columnData = metaData.getColumns(tableName, getDefaultColumnPattern());
}

while (found && columnData.next()) {
values = readMetaData(columnData, getColumnsForColumn());

if (getPlatform().getDdlBuilder().isDelimitedIdentifierModeOn()
&& !tableName.equals(values.get("TABLE_NAME"))) {
continue;
}

if (table.findColumn((String) values.get("COLUMN_NAME"), getPlatform().getDdlBuilder()
.isDelimitedIdentifierModeOn()) == null) {
found = false;
}
}
columnData.close();
columnData = null;
}
}
return found ? schema : null;
} finally {
if (columnData != null) {
columnData.close();
}
if (tableData != null) {
tableData.close();
}
}
}

@Override
protected String getTableNamePattern(String tableName) {
/*
Expand Down

0 comments on commit bcd5aa5

Please sign in to comment.