Skip to content

Commit

Permalink
CORE-749: modified table, view, and column exists preconditions to be…
Browse files Browse the repository at this point in the history
… case-insensitive

git-svn-id: http://liquibase.jira.com/svn/CORE/branches/1_9@1787 e6edf6fb-f266-4316-afb4-e53d95876a76
  • Loading branch information
donsmith committed Oct 19, 2010
1 parent ffecf8a commit 02fff9c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 31 deletions.
41 changes: 30 additions & 11 deletions core/src/java/liquibase/preconditions/ColumnExistsPrecondition.java
Expand Up @@ -4,6 +4,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;
import java.util.HashMap;

import liquibase.DatabaseChangeLog;
import liquibase.database.Database;
Expand Down Expand Up @@ -45,24 +46,42 @@ public void setColumnName(String columnName) {
}

public void check(Database database, DatabaseChangeLog changeLog) throws PreconditionFailedException, PreconditionErrorException {
HashMap tableColumnNames = new HashMap(3);
tableColumnNames.put(getTableName(), getColumnName());
tableColumnNames.put(getTableName().toLowerCase(), getColumnName().toLowerCase());
tableColumnNames.put(getTableName().toUpperCase(), getColumnName().toUpperCase());

for (Object tableName : tableColumnNames.keySet()) {
String columnName = (String) tableColumnNames.get(tableName);
try {
if (columnExists(database, database.convertRequestedSchemaToCatalog(getSchemaName()), (String) tableName, columnName)) {
return;
}
} catch (JDBCException e) {
throw new PreconditionErrorException(e, changeLog, this);
} catch (SQLException e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}

// If we got this far, the table doesn't exist, so throw PreconditionFailedException
throw new PreconditionFailedException("View "+database.escapeStringForDatabase(getColumnName())+" does not exist", changeLog, this);
}

private boolean columnExists(final Database database, final String schemaName, final String tableName, final String columnName)
throws SQLException {
// Use DatabaseMetaData to query db's data dictionary
DatabaseConnection conn = database.getConnection();
ResultSet columns = null;
try {
DatabaseMetaData dbm = conn.getMetaData();
columns = dbm.getColumns(
database.convertRequestedSchemaToCatalog(getSchemaName()),
database.convertRequestedSchemaToSchema(getSchemaName()),
getTableName(),
getColumnName()
schemaName,
schemaName,
tableName,
columnName
);
if (!columns.next()) {
throw new PreconditionFailedException("Column "+database.escapeColumnName(getSchemaName(), getTableName(), getColumnName())+" does not exist", changeLog, this);
}
} catch (JDBCException je) {
throw new PreconditionErrorException(je, changeLog, this);
} catch (SQLException se) {
throw new PreconditionErrorException(se, changeLog, this);
return columns.next();
} finally {
if (columns != null) {
try {
Expand Down
36 changes: 26 additions & 10 deletions core/src/java/liquibase/preconditions/TableExistsPrecondition.java
Expand Up @@ -37,24 +37,40 @@ public void setTableName(String tableName) {
}

public void check(Database database, DatabaseChangeLog changeLog) throws PreconditionFailedException, PreconditionErrorException {
String[] tableNames = new String[3];
tableNames[0] = getTableName();
tableNames[1] = getTableName().toLowerCase();
tableNames[2] = getTableName().toUpperCase();

for (String tableName : tableNames) {
try {
if (tableExists(database, database.convertRequestedSchemaToCatalog(getSchemaName()), tableName)) {
return;
}
} catch (JDBCException e) {
throw new PreconditionErrorException(e, changeLog, this);
} catch (SQLException e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}

// If we got this far, the table doesn't exist, so throw PreconditionFailedException
throw new PreconditionFailedException("Table "+database.escapeTableName(getSchemaName(), getTableName())+" does not exist", changeLog, this);
}

private boolean tableExists(final Database database, final String schemaName, final String tableName) throws SQLException {
// Use DatabaseMetaData to query db's data dictionary
DatabaseConnection conn = database.getConnection();
ResultSet tables = null;
try {
DatabaseMetaData dbm = conn.getMetaData();
tables = dbm.getTables(
database.convertRequestedSchemaToCatalog(getSchemaName()),
database.convertRequestedSchemaToSchema(getSchemaName()),
getTableName(),
schemaName,
schemaName,
tableName,
new String[]{"TABLE"}
);
if (!tables.next()) {
throw new PreconditionFailedException("Table "+database.escapeTableName(getSchemaName(), getTableName())+" does not exist", changeLog, this);
}
} catch (JDBCException je) {
throw new PreconditionErrorException(je, changeLog, this);
} catch (SQLException se) {
throw new PreconditionErrorException(se, changeLog, this);
return tables.next();
} finally {
if (tables != null) {
try {
Expand Down
36 changes: 26 additions & 10 deletions core/src/java/liquibase/preconditions/ViewExistsPrecondition.java
Expand Up @@ -37,24 +37,40 @@ public void setViewName(String viewName) {
}

public void check(Database database, DatabaseChangeLog changeLog) throws PreconditionFailedException, PreconditionErrorException {
String[] viewNames = new String[3];
viewNames[0] = getViewName();
viewNames[1] = getViewName().toLowerCase();
viewNames[2] = getViewName().toUpperCase();

for (String viewName : viewNames) {
try {
if (viewExists(database, database.convertRequestedSchemaToCatalog(getSchemaName()), viewName)) {
return;
}
} catch (JDBCException e) {
throw new PreconditionErrorException(e, changeLog, this);
} catch (SQLException e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}

// If we got this far, the table doesn't exist, so throw PreconditionFailedException
throw new PreconditionFailedException("View "+database.escapeStringForDatabase(getViewName())+" does not exist", changeLog, this);
}

private boolean viewExists(final Database database, final String schemaName, final String viewName) throws SQLException {
// Use DatabaseMetaData to query db's data dictionary
DatabaseConnection conn = database.getConnection();
ResultSet views = null;
try {
DatabaseMetaData dbm = conn.getMetaData();
views = dbm.getTables(
database.convertRequestedSchemaToCatalog(getSchemaName()),
database.convertRequestedSchemaToSchema(getSchemaName()),
getViewName(),
schemaName,
schemaName,
viewName,
new String[]{"VIEW"}
);
if (!views.next()) {
throw new PreconditionFailedException("View "+database.escapeStringForDatabase(getViewName())+" does not exist", changeLog, this);
}
} catch (JDBCException je) {
throw new PreconditionErrorException(je, changeLog, this);
} catch (SQLException se) {
throw new PreconditionErrorException(se, changeLog, this);
return views.next();
} finally {
if (views != null) {
try {
Expand Down

0 comments on commit 02fff9c

Please sign in to comment.