Search in sources :

Example 26 with ColumnConfig

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

the class InsertExecutablePreparedStatement method generateSql.

@Override
protected String generateSql(List<ColumnConfig> cols) {
    StringBuilder sql = new StringBuilder("INSERT INTO ");
    StringBuilder params = new StringBuilder("VALUES(");
    sql.append(database.escapeTableName(getCatalogName(), getSchemaName(), getTableName()));
    sql.append("(");
    for (ColumnConfig column : getColumns()) {
        if (database.supportsAutoIncrement() && Boolean.TRUE.equals(column.isAutoIncrement())) {
            continue;
        }
        sql.append(database.escapeColumnName(getCatalogName(), getSchemaName(), getTableName(), column.getName()));
        sql.append(", ");
        params.append("?, ");
        cols.add(column);
    }
    sql.deleteCharAt(sql.lastIndexOf(" "));
    sql.deleteCharAt(sql.lastIndexOf(","));
    params.deleteCharAt(params.lastIndexOf(" "));
    params.deleteCharAt(params.lastIndexOf(","));
    params.append(")");
    sql.append(") ");
    sql.append(params);
    return sql.toString();
}
Also used : ColumnConfig(liquibase.change.ColumnConfig)

Example 27 with ColumnConfig

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

the class InsertOrUpdateGeneratorOracleTest method testInsertSequenceValWithSchema.

@Test
public void testInsertSequenceValWithSchema() {
    OracleDatabase database = new OracleDatabase();
    InsertGenerator generator = new InsertGenerator();
    InsertStatement statement = new InsertStatement("mycatalog", "myschema", "mytable");
    ColumnConfig columnConfig = new ColumnConfig();
    columnConfig.setValueSequenceNext(new SequenceNextValueFunction("myschema.my_seq"));
    columnConfig.setName("col3");
    statement.addColumn(columnConfig);
    Sql[] sql = generator.generateSql(statement, database, null);
    String theSql = sql[0].toSql();
    assertEquals("INSERT INTO mycatalog.mytable (col3) VALUES (\"myschema\".\"my_seq\".nextval)", theSql);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) ColumnConfig(liquibase.change.ColumnConfig) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 28 with ColumnConfig

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

the class InsertOrUpdateGeneratorPostgresTest method testInsertSequenceValWithSchema.

@Test
public void testInsertSequenceValWithSchema() {
    PostgresDatabase postgresDatabase = new PostgresDatabase();
    InsertGenerator generator = new InsertGenerator();
    InsertStatement statement = new InsertStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
    ColumnConfig columnConfig = new ColumnConfig();
    columnConfig.setValueSequenceNext(new SequenceNextValueFunction(SCHEMA_NAME + '.' + SEQUENCE_NAME));
    columnConfig.setName("col3");
    statement.addColumn(columnConfig);
    Sql[] sql = generator.generateSql(statement, postgresDatabase, null);
    String theSql = sql[0].toSql();
    assertEquals(String.format("INSERT INTO %s.%s (col3) VALUES (nextval('%s.%s'))", SCHEMA_NAME, TABLE_NAME, SCHEMA_NAME, SEQUENCE_NAME), theSql);
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) ColumnConfig(liquibase.change.ColumnConfig) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql) 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