use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class MockSqlGenerator method generateSql.
@Override
public Sql[] generateSql(SqlStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
List<Sql> sql = new ArrayList<Sql>();
for (String returnSql : this.returnSql) {
sql.add(new UnparsedSql(returnSql));
}
sql.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
return sql.toArray(new Sql[sql.size()]);
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class AddDefaultValueGeneratorInformix method generateSql.
@Override
public Sql[] generateSql(AddDefaultValueStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
Object defaultValue = statement.getDefaultValue();
StringBuffer sql = new StringBuffer("ALTER TABLE ");
sql.append(database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()));
sql.append(" MODIFY (");
sql.append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()));
sql.append(" ");
sql.append(DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database));
sql.append(" DEFAULT ");
sql.append(DataTypeFactory.getInstance().fromObject(defaultValue, database).objectToSql(defaultValue, database));
sql.append(")");
UnparsedSql unparsedSql = new UnparsedSql(sql.toString(), getAffectedColumn(statement));
return new Sql[] { unparsedSql };
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class CopyRowsGenerator method generateSql.
@Override
public Sql[] generateSql(CopyRowsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
StringBuffer sql = new StringBuffer();
if (statement.getCopyColumns().size() == 0) {
return new Sql[] {};
}
if (database instanceof SQLiteDatabase) {
sql.append("INSERT INTO `").append(statement.getTargetTable()).append("` (");
for (int i = 0; i < statement.getCopyColumns().size(); i++) {
ColumnConfig column = statement.getCopyColumns().get(i);
if (i > 0) {
sql.append(",");
}
sql.append("`").append(column.getName()).append("`");
}
sql.append(") SELECT ");
for (int i = 0; i < statement.getCopyColumns().size(); i++) {
ColumnConfig column = statement.getCopyColumns().get(i);
if (i > 0) {
sql.append(",");
}
sql.append("`").append(column.getName()).append("`");
}
sql.append(" FROM `").append(statement.getSourceTable()).append("`");
}
return new Sql[] { new UnparsedSql(sql.toString(), getAffectedTable(statement)) };
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class CreateProcedureGenerator method generateSql.
@Override
public Sql[] generateSql(CreateProcedureStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
List<Sql> sql = new ArrayList<Sql>();
String schemaName = statement.getSchemaName();
if (schemaName == null && LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getAlwaysOverrideStoredLogicSchema()) {
schemaName = database.getDefaultSchemaName();
}
String procedureText = addSchemaToText(statement.getProcedureText(), schemaName, "PROCEDURE", database);
if (statement.getReplaceIfExists() != null && statement.getReplaceIfExists()) {
String fullyQualifiedName = database.escapeObjectName(statement.getProcedureName(), StoredProcedure.class);
if (schemaName != null) {
fullyQualifiedName = database.escapeObjectName(schemaName, Schema.class) + "." + fullyQualifiedName;
}
sql.add(new UnparsedSql("if object_id('" + fullyQualifiedName + "', 'p') is null exec ('create procedure " + fullyQualifiedName + " as select 1 a')"));
StringClauses parsedSql = SqlParser.parse(procedureText, true, true);
StringClauses.ClauseIterator clauseIterator = parsedSql.getClauseIterator();
Object next = "START";
while (next != null && !(next.toString().equalsIgnoreCase("create") || next.toString().equalsIgnoreCase("alter")) && clauseIterator.hasNext()) {
next = clauseIterator.nextNonWhitespace();
}
clauseIterator.replace("ALTER");
procedureText = parsedSql.toString();
}
procedureText = removeTrailingDelimiter(procedureText, statement.getEndDelimiter());
if (database instanceof MSSQLDatabase && procedureText.toLowerCase().contains("merge") && !procedureText.endsWith(";")) {
//mssql "AS MERGE" procedures need a trailing ; (regardless of the end delimiter)
StringClauses parsed = SqlParser.parse(procedureText);
StringClauses.ClauseIterator clauseIterator = parsed.getClauseIterator();
boolean reallyMerge = false;
while (clauseIterator.hasNext()) {
Object clause = clauseIterator.nextNonWhitespace();
if (((String) clause).equalsIgnoreCase("merge")) {
reallyMerge = true;
}
}
if (reallyMerge) {
procedureText = procedureText + ";";
}
}
sql.add(new UnparsedSql(procedureText, statement.getEndDelimiter()));
surroundWithSchemaSets(sql, statement.getSchemaName(), database);
return sql.toArray(new Sql[sql.size()]);
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class AddColumnGenerator method generateSingleColumn.
protected Sql[] generateSingleColumn(AddColumnStatement statement, Database database) {
String alterTable = generateSingleColumBaseSQL(statement, database);
alterTable += generateSingleColumnSQL(statement, database);
List<Sql> returnSql = new ArrayList<Sql>();
returnSql.add(new UnparsedSql(alterTable, getAffectedColumn(statement)));
addUniqueConstrantStatements(statement, database, returnSql);
addForeignKeyStatements(statement, database, returnSql);
return returnSql.toArray(new Sql[returnSql.size()]);
}
Aggregations