use of liquibase.database.core.SybaseASADatabase in project liquibase by liquibase.
the class CreateIndexGeneratorPostgres method generateSql.
@Override
public Sql[] generateSql(CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
// Default filter of index creation:
// creation of all indexes with associations are switched off.
List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT) || associatedWith.contains(Index.MARK_FOREIGN_KEY))) {
return new Sql[0];
}
StringBuilder buffer = new StringBuilder();
buffer.append("CREATE ");
if (statement.isUnique() != null && statement.isUnique()) {
buffer.append("UNIQUE ");
}
buffer.append("INDEX ");
if (statement.getIndexName() != null) {
// for postgres setting the schema name for the index name is invalid
buffer.append(database.escapeObjectName(statement.getIndexName(), Index.class)).append(" ");
}
buffer.append("ON ");
buffer.append(database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName())).append("(");
Iterator<AddColumnConfig> iterator = Arrays.asList(statement.getColumns()).iterator();
while (iterator.hasNext()) {
AddColumnConfig column = iterator.next();
if (column.getComputed() == null) {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName(), false));
} else {
if (column.getComputed()) {
buffer.append(column.getName());
} else {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName()));
}
}
if (iterator.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) {
if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) {
buffer.append(" ON ").append(statement.getTablespace());
} else if (database instanceof DB2Database || database instanceof InformixDatabase) {
buffer.append(" IN ").append(statement.getTablespace());
} else {
buffer.append(" TABLESPACE ").append(statement.getTablespace());
}
}
if (statement.isClustered() != null && statement.isClustered()) {
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)), new UnparsedSql("CLUSTER " + database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName()) + " USING " + database.escapeObjectName(statement.getIndexName(), Index.class)) };
} else {
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)) };
}
}
use of liquibase.database.core.SybaseASADatabase in project liquibase by liquibase.
the class CreateTableGeneratorTest method testAutoIncrementStartWithSybaseASADatabase.
@Test
public void testAutoIncrementStartWithSybaseASADatabase() throws Exception {
for (Database database : TestContext.getInstance().getAllDatabases()) {
if (database instanceof SybaseASADatabase) {
CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new AutoIncrementConstraint(COLUMN_NAME1, BigInteger.valueOf(2), null));
Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
// start with not supported by SybaseASA
assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT DEFAULT AUTOINCREMENT NULL)", generatedSql[0].toSql());
}
}
}
use of liquibase.database.core.SybaseASADatabase in project liquibase by liquibase.
the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementBySybaseASADatabase.
@Test
public void testAutoIncrementStartWithIncrementBySybaseASADatabase() throws Exception {
for (Database database : TestContext.getInstance().getAllDatabases()) {
if (database instanceof SybaseASADatabase) {
CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new AutoIncrementConstraint(COLUMN_NAME1, BigInteger.valueOf(2), BigInteger.TEN));
Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
// start with and increment by not supported by SybaseASA
assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT DEFAULT AUTOINCREMENT NULL)", generatedSql[0].toSql());
}
}
}
use of liquibase.database.core.SybaseASADatabase in project liquibase by liquibase.
the class AddColumnGeneratorDefaultClauseBeforeNotNull method generateSingleColumnSQL.
@Override
protected String generateSingleColumnSQL(AddColumnStatement statement, Database database) {
String alterTable = " ADD " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance().fromDescription(statement.getColumnType() + (statement.isAutoIncrement() ? "{autoIncrement:true}" : ""), database).toDatabaseDataType(database);
alterTable += getDefaultClause(statement, database);
if (primaryKeyBeforeNotNull(database)) {
if (statement.isPrimaryKey()) {
alterTable += " PRIMARY KEY";
}
}
if (statement.isAutoIncrement()) {
AutoIncrementConstraint autoIncrementConstraint = statement.getAutoIncrementConstraint();
alterTable += " " + database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy());
}
if (!statement.isNullable()) {
alterTable += " NOT NULL";
} else if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase) {
alterTable += " NULL";
}
if (!primaryKeyBeforeNotNull(database)) {
if (statement.isPrimaryKey()) {
alterTable += " PRIMARY KEY";
}
}
return alterTable;
}
use of liquibase.database.core.SybaseASADatabase in project liquibase by liquibase.
the class LoggingExecutor method outputStatement.
private void outputStatement(SqlStatement sql, List<SqlVisitor> sqlVisitors) throws DatabaseException {
try {
if (SqlGeneratorFactory.getInstance().generateStatementsVolatile(sql, database)) {
throw new DatabaseException(sql.getClass().getSimpleName() + " requires access to up to date database metadata which is not available in SQL output mode");
}
if (sql instanceof ExecutablePreparedStatement) {
output.write("WARNING: This statement uses a prepared statement which cannot be execute directly by this script. Only works in 'update' mode\n\n");
}
for (String statement : applyVisitors(sql, sqlVisitors)) {
if (statement == null) {
continue;
}
if (database instanceof OracleDatabase) {
//remove trailing /
while (statement.matches("(?s).*[\\s\\r\\n]*/[\\s\\r\\n]*$")) {
//all trailing /'s
statement = statement.replaceFirst("[\\s\\r\\n]*/[\\s\\r\\n]*$", "");
}
}
output.write(statement);
if (database instanceof MSSQLDatabase || database instanceof SybaseDatabase || database instanceof SybaseASADatabase) {
output.write(StreamUtil.getLineSeparator());
output.write("GO");
// } else if (database instanceof OracleDatabase) {
// output.write(StreamUtil.getLineSeparator());
// output.write("/");
} else {
String endDelimiter = ";";
String potentialDelimiter = null;
if (sql instanceof RawSqlStatement) {
potentialDelimiter = ((RawSqlStatement) sql).getEndDelimiter();
} else if (sql instanceof CreateProcedureStatement) {
potentialDelimiter = ((CreateProcedureStatement) sql).getEndDelimiter();
}
if (potentialDelimiter != null) {
//ignore trailing $ as a regexp to determine if it should be output
potentialDelimiter = potentialDelimiter.replaceFirst("\\$$", "");
if (potentialDelimiter.replaceAll("\\n", "\n").replace("\\r", "\r").matches("[;/\r\n\\w@\\-]+")) {
endDelimiter = potentialDelimiter;
}
}
endDelimiter = endDelimiter.replace("\\n", "\n");
endDelimiter = endDelimiter.replace("\\r", "\r");
if (!statement.endsWith(endDelimiter)) {
output.write(endDelimiter);
}
}
output.write(StreamUtil.getLineSeparator());
output.write(StreamUtil.getLineSeparator());
}
} catch (IOException e) {
throw new DatabaseException(e);
}
}
Aggregations