use of liquibase.util.StringClauses in project liquibase by liquibase.
the class CreateViewGenerator method generateSql.
@Override
public Sql[] generateSql(CreateViewStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
if (database instanceof InformixDatabase) {
return new CreateViewGeneratorInformix().generateSql(statement, database, sqlGeneratorChain);
}
List<Sql> sql = new ArrayList<Sql>();
StringClauses viewDefinition = SqlParser.parse(statement.getSelectQuery(), true, true);
if (!statement.isFullDefinition()) {
viewDefinition.prepend(" ").prepend("AS").prepend(" ").prepend(database.escapeViewName(statement.getCatalogName(), statement.getSchemaName(), statement.getViewName())).prepend(" ").prepend("VIEW").prepend(" ").prepend("CREATE");
}
if (statement.isReplaceIfExists()) {
if (database instanceof FirebirdDatabase) {
viewDefinition.replaceIfExists("CREATE", "RECREATE");
} else if (database instanceof SybaseASADatabase && statement.getSelectQuery().toLowerCase().startsWith("create view")) {
// Sybase ASA saves view definitions with header.
} else if (database instanceof MSSQLDatabase) {
//from http://stackoverflow.com/questions/163246/sql-server-equivalent-to-oracles-create-or-replace-view
CatalogAndSchema schema = new CatalogAndSchema(statement.getCatalogName(), statement.getSchemaName()).customize(database);
sql.add(new UnparsedSql("IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[" + schema.getSchemaName() + "].[" + statement.getViewName() + "]'))\n" + " EXEC sp_executesql N'CREATE VIEW [" + schema.getSchemaName() + "].[" + statement.getViewName() + "] AS SELECT ''This is a code stub which will be replaced by an Alter Statement'' as [code_stub]'"));
viewDefinition.replaceIfExists("CREATE", "ALTER");
} else if (database instanceof PostgresDatabase) {
sql.add(new UnparsedSql("DROP VIEW IF EXISTS " + database.escapeViewName(statement.getCatalogName(), statement.getSchemaName(), statement.getViewName())));
} else {
if (!viewDefinition.contains("replace")) {
viewDefinition.replace("CREATE", "CREATE OR REPLACE");
}
}
}
sql.add(new UnparsedSql(viewDefinition.toString(), getAffectedView(statement)));
return sql.toArray(new Sql[sql.size()]);
}
use of liquibase.util.StringClauses in project liquibase by liquibase.
the class CreateProcedureGenerator method addSchemaToText.
/**
* Convenience method for other classes similar to this that want to be able to modify the procedure text to add the schema
*/
public static String addSchemaToText(String procedureText, String schemaName, String keywordBeforeName, Database database) {
if (schemaName == null) {
return procedureText;
}
if ((StringUtils.trimToNull(schemaName) != null) && LiquibaseConfiguration.getInstance().getProperty(ChangeLogParserCofiguration.class, ChangeLogParserCofiguration.USE_PROCEDURE_SCHEMA).getValue(Boolean.class)) {
StringClauses parsedSql = SqlParser.parse(procedureText, true, true);
StringClauses.ClauseIterator clauseIterator = parsedSql.getClauseIterator();
Object next = "START";
while (next != null && !next.toString().equalsIgnoreCase(keywordBeforeName) && clauseIterator.hasNext()) {
if (!keywordBeforeName.equalsIgnoreCase("PACKAGE") && ((String) next).equalsIgnoreCase("PACKAGE")) {
return procedureText;
}
next = clauseIterator.nextNonWhitespace();
}
if (next != null && clauseIterator.hasNext()) {
Object procNameClause = clauseIterator.nextNonWhitespace();
if (procNameClause instanceof String) {
String[] nameParts = ((String) procNameClause).split("\\.");
String finalName;
if (nameParts.length == 1) {
finalName = database.escapeObjectName(schemaName, Schema.class) + "." + nameParts[0];
} else if (nameParts.length == 2) {
finalName = database.escapeObjectName(schemaName, Schema.class) + "." + nameParts[1];
} else if (nameParts.length == 3) {
finalName = nameParts[0] + "." + database.escapeObjectName(schemaName, Schema.class) + "." + nameParts[2];
} else {
//just go with what was there
finalName = (String) procNameClause;
}
clauseIterator.replace(finalName);
}
procedureText = parsedSql.toString();
}
}
return procedureText;
}
use of liquibase.util.StringClauses 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()]);
}
Aggregations