Search in sources :

Example 6 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class UpdateExecutablePreparedStatement method generateSql.

@Override
protected String generateSql(List<ColumnConfig> cols) {
    StringBuilder sql = new StringBuilder("UPDATE ").append(database.escapeTableName(getCatalogName(), getSchemaName(), getTableName()));
    StringBuilder params = new StringBuilder(" SET ");
    for (ColumnConfig column : getColumns()) {
        params.append(database.escapeColumnName(getCatalogName(), getSchemaName(), getTableName(), column.getName()));
        params.append(" = ");
        params.append("?, ");
        cols.add(column);
    }
    params.deleteCharAt(params.lastIndexOf(" "));
    params.deleteCharAt(params.lastIndexOf(","));
    sql.append(params);
    if (getWhereClause() != null) {
        sql.append(" WHERE ").append(replacePredicatePlaceholders(database, getWhereClause(), getWhereColumnNames(), getWhereParameters()));
    }
    return sql.toString();
}
Also used : ColumnConfig(liquibase.change.ColumnConfig)

Example 7 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class SelectFromDatabaseChangeLogGenerator method generateSql.

@Override
public Sql[] generateSql(SelectFromDatabaseChangeLogStatement statement, final Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<ColumnConfig> columnsToSelect = Arrays.asList(statement.getColumnsToSelect());
    String sql = "SELECT " + (database instanceof MSSQLDatabase && statement.getLimit() != null ? "TOP " + statement.getLimit() + " " : "") + StringUtils.join(columnsToSelect, ",", new StringUtils.StringUtilsFormatter<ColumnConfig>() {

        @Override
        public String toString(ColumnConfig column) {
            if (column.getComputed() != null && column.getComputed()) {
                return column.getName();
            } else {
                return database.escapeColumnName(null, null, null, column.getName());
            }
        }
    }).toUpperCase() + " FROM " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName());
    SelectFromDatabaseChangeLogStatement.WhereClause whereClause = statement.getWhereClause();
    if (whereClause != null) {
        if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByTag) {
            sql += " WHERE " + database.escapeColumnName(null, null, null, "TAG") + "='" + ((SelectFromDatabaseChangeLogStatement.ByTag) whereClause).getTagName() + "'";
        } else if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByNotNullCheckSum) {
            sql += " WHERE " + database.escapeColumnName(null, null, null, "MD5SUM") + " IS NOT NULL";
        } else {
            throw new UnexpectedLiquibaseException("Unknown where clause type: " + whereClause.getClass().getName());
        }
    }
    if (statement.getOrderByColumns() != null && statement.getOrderByColumns().length > 0) {
        sql += " ORDER BY ";
        Iterator<String> orderBy = Arrays.asList(statement.getOrderByColumns()).iterator();
        while (orderBy.hasNext()) {
            String orderColumn = orderBy.next();
            String[] orderColumnData = orderColumn.split(" ");
            sql += database.escapeColumnName(null, null, null, orderColumnData[0]);
            if (orderColumnData.length == 2) {
                sql += " ";
                sql += orderColumnData[1].toUpperCase();
            }
            if (orderBy.hasNext()) {
                sql += ", ";
            }
        }
    }
    if (statement.getLimit() != null) {
        if (database instanceof OracleDatabase) {
            if (whereClause == null) {
                sql += " WHERE ROWNUM=" + statement.getLimit();
            } else {
                sql += " AND ROWNUM=" + statement.getLimit();
            }
        } else if (database instanceof MySQLDatabase || database instanceof PostgresDatabase) {
            sql += " LIMIT " + statement.getLimit();
        } else if (database instanceof DB2Database) {
            sql += " FETCH FIRST " + statement.getLimit() + " ROWS ONLY";
        }
    }
    return new Sql[] { new UnparsedSql(sql) };
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) UnparsedSql(liquibase.sql.UnparsedSql) SelectFromDatabaseChangeLogStatement(liquibase.statement.core.SelectFromDatabaseChangeLogStatement) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql) StringUtils(liquibase.util.StringUtils) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 8 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class ValidatingVisitorPreConditionsTest method setUp.

/**
     * Create a DatabaseChangelog, one changeset, and a create sequence change
     */
@Before
public void setUp() {
    changeLog = new DatabaseChangeLog();
    changeSet1 = new ChangeSet("1", "testAuthor", false, false, "path/changelog", null, null, null);
    changeLog.addChangeSet(changeSet1);
    CreateTableChange change1 = new CreateTableChange();
    change1.setTableName("valid_test");
    change1.addColumn(new ColumnConfig().setName("id").setType("int"));
    changeSet1.addChange(change1);
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) CreateTableChange(liquibase.change.core.CreateTableChange) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) Before(org.junit.Before)

Example 9 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class ValidatingVisitorTest method visit_successful.

@Test
public void visit_successful() throws Exception {
    CreateTableChange change1 = new CreateTableChange();
    change1.setTableName("table1");
    ColumnConfig column1 = new ColumnConfig();
    change1.addColumn(column1);
    column1.setName("col1");
    column1.setType("int");
    CreateTableChange change2 = new CreateTableChange();
    change2.setTableName("table2");
    ColumnConfig column2 = new ColumnConfig();
    change2.addColumn(column2);
    column2.setName("col2");
    column2.setType("int");
    changeSet1.addChange(change1);
    changeSet2.addChange(change2);
    ValidatingVisitor handler = new ValidatingVisitor(new ArrayList<RanChangeSet>());
    handler.visit(changeSet1, new DatabaseChangeLog(), new MockDatabase(), null);
    handler.visit(changeSet2, new DatabaseChangeLog(), new MockDatabase(), null);
    assertTrue(handler.validationPassed());
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) CreateTableChange(liquibase.change.core.CreateTableChange) MockDatabase(liquibase.sdk.database.MockDatabase) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) RanChangeSet(liquibase.changelog.RanChangeSet) Test(org.junit.Test)

Example 10 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class CreateTableGeneratorTest method testWithColumnWithDefaultValue.

@Test
public void testWithColumnWithDefaultValue() {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database instanceof OracleDatabase) {
            CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
            statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("java.sql.Types.TIMESTAMP", database), new ColumnConfig().setDefaultValue("null").getDefaultValueObject());
            if (shouldBeImplementation(database)) {
                assertEquals("CREATE TABLE CATALOG_NAME.TABLE_NAME (COLUMN1_NAME TIMESTAMP DEFAULT null)", this.generatorUnderTest.generateSql(statement, database, null)[0].toSql());
            }
        }
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) ColumnConfig(liquibase.change.ColumnConfig) 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) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Aggregations

ColumnConfig (liquibase.change.ColumnConfig)28 Test (org.junit.Test)14 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 AddColumnConfig (liquibase.change.AddColumnConfig)5 CreateTableChange (liquibase.change.core.CreateTableChange)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)5 DatabaseException (liquibase.exception.DatabaseException)5 Sql (liquibase.sql.Sql)5 PostgresDatabase (liquibase.database.core.PostgresDatabase)4 SqlStatement (liquibase.statement.SqlStatement)4 ArrayList (java.util.ArrayList)3 ConstraintsConfig (liquibase.change.ConstraintsConfig)3 Database (liquibase.database.Database)3 DB2Database (liquibase.database.core.DB2Database)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 UnparsedSql (liquibase.sql.UnparsedSql)3 AddUniqueConstraintStatement (liquibase.statement.core.AddUniqueConstraintStatement)3 Column (liquibase.structure.core.Column)3 IOException (java.io.IOException)2