use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AbstractChange method validate.
/**
* Implementation checks the ChangeParameterMetaData for declared required fields
* and also delegates logic to the {@link liquibase.sqlgenerator.SqlGenerator#validate(liquibase.statement.SqlStatement, liquibase.database.Database, liquibase.sqlgenerator.SqlGeneratorChain)} method on the {@link SqlStatement} objects returned by {@link #generateStatements }.
* If no or null SqlStatements are returned by generateStatements then this method returns no errors.
* If there are no parameters than this method returns no errors
*/
@Override
public ValidationErrors validate(Database database) {
ValidationErrors changeValidationErrors = new ValidationErrors();
for (ChangeParameterMetaData param : ChangeFactory.getInstance().getChangeMetaData(this).getParameters().values()) {
if (param.isRequiredFor(database) && param.getCurrentValue(this) == null) {
changeValidationErrors.addError(param.getParameterName() + " is required for " + ChangeFactory.getInstance().getChangeMetaData(this).getName() + " on " + database.getShortName());
}
}
if (changeValidationErrors.hasErrors()) {
return changeValidationErrors;
}
if (!generateStatementsVolatile(database)) {
String unsupportedWarning = ChangeFactory.getInstance().getChangeMetaData(this).getName() + " is not supported on " + database.getShortName();
boolean sawUnsupportedError = false;
SqlStatement[] statements = generateStatements(database);
if (statements != null) {
for (SqlStatement statement : statements) {
boolean supported = SqlGeneratorFactory.getInstance().supports(statement, database);
if (!supported && !sawUnsupportedError) {
if (!statement.skipOnUnsupported()) {
changeValidationErrors.addError(unsupportedWarning);
sawUnsupportedError = true;
}
} else {
changeValidationErrors.addAll(SqlGeneratorFactory.getInstance().validate(statement, database));
}
}
}
}
return changeValidationErrors;
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AbstractChange method warn.
/**
* Implementation delegates logic to the {@link liquibase.sqlgenerator.SqlGenerator#warn(liquibase.statement.SqlStatement, liquibase.database.Database, liquibase.sqlgenerator.SqlGeneratorChain)} method on the {@link SqlStatement} objects returned by {@link #generateStatements }.
* If a generated statement is not supported for the given database, no warning will be added since that is a validation error.
* If no or null SqlStatements are returned by generateStatements then this method returns no warnings.
*/
@Override
public Warnings warn(Database database) {
Warnings warnings = new Warnings();
if (generateStatementsVolatile(database)) {
return warnings;
}
SqlStatement[] statements = generateStatements(database);
if (statements == null) {
return warnings;
}
for (SqlStatement statement : statements) {
if (SqlGeneratorFactory.getInstance().supports(statement, database)) {
warnings.addAll(SqlGeneratorFactory.getInstance().warn(statement, database));
} else if (statement.skipOnUnsupported()) {
warnings.addWarning(statement.getClass().getName() + " is not supported on " + database.getShortName() + ", but " + ChangeFactory.getInstance().getChangeMetaData(this).getName() + " will still execute");
}
}
return warnings;
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AbstractChange method getAffectedDatabaseObjects.
/**
* Implementation delegates logic to the {@link liquibase.sqlgenerator.SqlGeneratorFactory#getAffectedDatabaseObjects(liquibase.statement.SqlStatement, liquibase.database.Database)} method on the {@link SqlStatement} objects returned by {@link #generateStatements }
* Returns empty set if change is not supported for the passed database
*/
@Override
public Set<DatabaseObject> getAffectedDatabaseObjects(Database database) {
if (this.generateStatementsVolatile(database)) {
return new HashSet<DatabaseObject>();
}
Set<DatabaseObject> affectedObjects = new HashSet<DatabaseObject>();
SqlStatement[] statements = generateStatements(database);
if (statements != null) {
for (SqlStatement statement : statements) {
affectedObjects.addAll(SqlGeneratorFactory.getInstance().getAffectedDatabaseObjects(statement, database));
}
}
return affectedObjects;
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class ChangeParameterMetaData method getStatementErrors.
private ValidationErrors getStatementErrors(Change testChange, Database database) {
ValidationErrors errors = new ValidationErrors();
SqlStatement[] statements = testChange.generateStatements(database);
for (SqlStatement statement : statements) {
errors.addAll(SqlGeneratorFactory.getInstance().validate(statement, database));
}
return errors;
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AddLookupTableChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
List<SqlStatement> statements = new ArrayList<SqlStatement>();
String newTableCatalogName = getNewTableCatalogName();
String newTableSchemaName = getNewTableSchemaName();
String existingTableCatalogName = getExistingTableCatalogName();
String existingTableSchemaName = getExistingTableSchemaName();
SqlStatement[] createTablesSQL = { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " AS SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
if (database instanceof MSSQLDatabase) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
} else if (database instanceof SybaseASADatabase) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
} else if (database instanceof DB2Database) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " AS (SELECT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + ") WITH NO DATA"), new RawSqlStatement("INSERT INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
} else if (database instanceof InformixDatabase) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " ( " + database.escapeObjectName(getNewColumnName(), Column.class) + " " + getNewColumnDataType() + " )"), new RawSqlStatement("INSERT INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " ( " + database.escapeObjectName(getNewColumnName(), Column.class) + " ) SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
}
statements.addAll(Arrays.asList(createTablesSQL));
if (!(database instanceof OracleDatabase)) {
AddNotNullConstraintChange addNotNullChange = new AddNotNullConstraintChange();
addNotNullChange.setSchemaName(newTableSchemaName);
addNotNullChange.setTableName(getNewTableName());
addNotNullChange.setColumnName(getNewColumnName());
addNotNullChange.setColumnDataType(getNewColumnDataType());
statements.addAll(Arrays.asList(addNotNullChange.generateStatements(database)));
}
if (database instanceof DB2Database) {
statements.add(new ReorganizeTableStatement(newTableCatalogName, newTableSchemaName, getNewTableName()));
}
AddPrimaryKeyChange addPKChange = new AddPrimaryKeyChange();
addPKChange.setSchemaName(newTableSchemaName);
addPKChange.setTableName(getNewTableName());
addPKChange.setColumnNames(getNewColumnName());
statements.addAll(Arrays.asList(addPKChange.generateStatements(database)));
if (database instanceof DB2Database) {
statements.add(new ReorganizeTableStatement(newTableCatalogName, newTableSchemaName, getNewTableName()));
}
AddForeignKeyConstraintChange addFKChange = new AddForeignKeyConstraintChange();
addFKChange.setBaseTableSchemaName(existingTableSchemaName);
addFKChange.setBaseTableName(getExistingTableName());
addFKChange.setBaseColumnNames(getExistingColumnName());
addFKChange.setReferencedTableSchemaName(newTableSchemaName);
addFKChange.setReferencedTableName(getNewTableName());
addFKChange.setReferencedColumnNames(getNewColumnName());
addFKChange.setConstraintName(getFinalConstraintName());
statements.addAll(Arrays.asList(addFKChange.generateStatements(database)));
return statements.toArray(new SqlStatement[statements.size()]);
}
Aggregations