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();
}
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) };
}
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);
}
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());
}
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());
}
}
}
}
Aggregations