Search in sources :

Example 1 with SybaseASADatabase

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)) };
    }
}
Also used : SybaseASADatabase(liquibase.database.core.SybaseASADatabase) DB2Database(liquibase.database.core.DB2Database) InformixDatabase(liquibase.database.core.InformixDatabase) UnparsedSql(liquibase.sql.UnparsedSql) AddColumnConfig(liquibase.change.AddColumnConfig) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 2 with SybaseASADatabase

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());
        }
    }
}
Also used : SybaseASADatabase(liquibase.database.core.SybaseASADatabase) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) DB2Database(liquibase.database.core.DB2Database) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 3 with SybaseASADatabase

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());
        }
    }
}
Also used : SybaseASADatabase(liquibase.database.core.SybaseASADatabase) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) DB2Database(liquibase.database.core.DB2Database) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 4 with SybaseASADatabase

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;
}
Also used : SybaseASADatabase(liquibase.database.core.SybaseASADatabase) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) SybaseDatabase(liquibase.database.core.SybaseDatabase)

Example 5 with SybaseASADatabase

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);
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SybaseDatabase(liquibase.database.core.SybaseDatabase) CreateProcedureStatement(liquibase.statement.core.CreateProcedureStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) IOException(java.io.IOException) DatabaseException(liquibase.exception.DatabaseException) ExecutablePreparedStatement(liquibase.statement.ExecutablePreparedStatement)

Aggregations

SybaseASADatabase (liquibase.database.core.SybaseASADatabase)6 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 SybaseDatabase (liquibase.database.core.SybaseDatabase)5 DB2Database (liquibase.database.core.DB2Database)4 OracleDatabase (liquibase.database.core.OracleDatabase)4 Sql (liquibase.sql.Sql)4 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)4 Database (liquibase.database.Database)3 DerbyDatabase (liquibase.database.core.DerbyDatabase)3 H2Database (liquibase.database.core.H2Database)3 HsqlDatabase (liquibase.database.core.HsqlDatabase)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)3 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)3 CreateTableStatement (liquibase.statement.core.CreateTableStatement)3 Test (org.junit.Test)3 IOException (java.io.IOException)1 AddColumnConfig (liquibase.change.AddColumnConfig)1 InformixDatabase (liquibase.database.core.InformixDatabase)1