use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class CreateViewChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
List<SqlStatement> statements = new ArrayList<SqlStatement>();
boolean replaceIfExists = false;
if (getReplaceIfExists() != null && getReplaceIfExists()) {
replaceIfExists = true;
}
boolean fullDefinition = false;
if (this.fullDefinition != null) {
fullDefinition = this.fullDefinition;
}
if (!supportsReplaceIfExistsOption(database) && replaceIfExists) {
statements.add(new DropViewStatement(getCatalogName(), getSchemaName(), getViewName()));
statements.add(createViewStatement(getCatalogName(), getSchemaName(), getViewName(), getSelectQuery(), false).setFullDefinition(fullDefinition));
} else {
statements.add(createViewStatement(getCatalogName(), getSchemaName(), getViewName(), getSelectQuery(), replaceIfExists).setFullDefinition(fullDefinition));
}
return statements.toArray(new SqlStatement[statements.size()]);
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AbstractSQLChange method generateStatements.
/**
* Generates one or more SqlStatements depending on how the SQL should be parsed.
* If split statements is set to true then the SQL is split and each command is made into a separate SqlStatement.
* <p></p>
* If stripping comments is true then any comments are removed before the splitting is executed.
* The set SQL is passed through the {@link java.sql.Connection#nativeSQL} method if a connection is available.
*/
@Override
public SqlStatement[] generateStatements(Database database) {
List<SqlStatement> returnStatements = new ArrayList<SqlStatement>();
String sql = StringUtils.trimToNull(getSql());
if (sql == null) {
return new SqlStatement[0];
}
String processedSQL = normalizeLineEndings(sql);
for (String statement : StringUtils.processMutliLineSQL(processedSQL, isStripComments(), isSplitStatements(), getEndDelimiter())) {
if (database instanceof MSSQLDatabase) {
statement = statement.replaceAll("\\n", "\r\n");
}
String escapedStatement = statement;
try {
if (database.getConnection() != null) {
escapedStatement = database.getConnection().nativeSQL(statement);
}
} catch (DatabaseException e) {
escapedStatement = statement;
}
returnStatements.add(new RawSqlStatement(escapedStatement, getEndDelimiter()));
}
return returnStatements.toArray(new SqlStatement[returnStatements.size()]);
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AddColumnGeneratorSQLite method generateSql.
@Override
public Sql[] generateSql(final AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
// SQLite does not support this ALTER TABLE operation until now.
// For more information see: http://www.sqlite.org/omitted.html.
// This is a small work around...
List<Sql> sql = new ArrayList<Sql>();
// define alter table logic
SQLiteDatabase.AlterTableVisitor rename_alter_visitor = new SQLiteDatabase.AlterTableVisitor() {
public ColumnConfig[] getColumnsToAdd() {
return new ColumnConfig[] { new ColumnConfig().setName(statement.getColumnName()).setType(statement.getColumnType()).setAutoIncrement(statement.isAutoIncrement()) };
}
public boolean copyThisColumn(ColumnConfig column) {
return !column.getName().equals(statement.getColumnName());
}
public boolean createThisColumn(ColumnConfig column) {
return true;
}
public boolean createThisIndex(Index index) {
return true;
}
};
try {
// alter table
List<SqlStatement> alterTableStatements = SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
sql.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(alterTableStatements.toArray(new SqlStatement[alterTableStatements.size()]), database)));
} catch (DatabaseException e) {
System.err.println(e);
e.printStackTrace();
}
return sql.toArray(new Sql[sql.size()]);
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class SqlGeneratorFactory method generateSql.
public Sql[] generateSql(SqlStatement[] statements, Database database) {
List<Sql> returnList = new ArrayList<Sql>();
SqlGeneratorFactory factory = SqlGeneratorFactory.getInstance();
for (SqlStatement statement : statements) {
Sql[] sqlArray = factory.generateSql(statement, database);
if (sqlArray != null && sqlArray.length > 0) {
List<Sql> sqlList = Arrays.asList(sqlArray);
returnList.addAll(sqlList);
}
}
return returnList.toArray(new Sql[returnList.size()]);
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class MarkChangeSetRanGenerator method generateSql.
@Override
public Sql[] generateSql(MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String dateValue = database.getCurrentDateTimeFunction();
ChangeSet changeSet = statement.getChangeSet();
SqlStatement runStatement;
try {
if (statement.getExecType().equals(ChangeSet.ExecType.FAILED) || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
//don't mark
return new Sql[0];
}
String tag = null;
for (Change change : changeSet.getChanges()) {
if (change instanceof TagDatabaseChange) {
TagDatabaseChange tagChange = (TagDatabaseChange) change;
tag = tagChange.getTag();
}
}
if (statement.getExecType().ranBefore) {
runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addNewColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addNewColumnValue("EXECTYPE", statement.getExecType().value).addNewColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId()).setWhereClause(database.escapeObjectName("ID", Column.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", Column.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", Column.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
if (tag != null) {
((UpdateStatement) runStatement).addNewColumnValue("TAG", tag);
}
} else {
runStatement = new InsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addColumnValue("ID", changeSet.getId()).addColumnValue("AUTHOR", changeSet.getAuthor()).addColumnValue("FILENAME", changeSet.getFilePath()).addColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addColumnValue("DESCRIPTION", limitSize(changeSet.getDescription())).addColumnValue("COMMENTS", limitSize(StringUtils.trimToEmpty(changeSet.getComments()))).addColumnValue("EXECTYPE", statement.getExecType().value).addColumnValue("CONTEXTS", changeSet.getContexts() == null || changeSet.getContexts().isEmpty() ? null : changeSet.getContexts().toString()).addColumnValue("LABELS", changeSet.getLabels() == null || changeSet.getLabels().isEmpty() ? null : changeSet.getLabels().toString()).addColumnValue("LIQUIBASE", LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP")).addColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId());
if (tag != null) {
((InsertStatement) runStatement).addColumnValue("TAG", tag);
}
}
} catch (LiquibaseException e) {
throw new UnexpectedLiquibaseException(e);
}
return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
}
Aggregations