Search in sources :

Example 26 with InsertStatement

use of liquibase.statement.core.InsertStatement in project liquibase by liquibase.

the class CustomChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) throws CustomChangeException {
    SqlStatement[] st = new SqlStatement[1];
    InsertStatement is = new InsertStatement(null, null, "persons");
    is.addColumnValue("id", new Integer(1));
    is.addColumnValue("firstname", "joan");
    is.addColumnValue("lastname", "pujol");
    st[0] = is;
    return st;
}
Also used : SqlStatement(liquibase.statement.SqlStatement) InsertStatement(liquibase.statement.core.InsertStatement)

Example 27 with InsertStatement

use of liquibase.statement.core.InsertStatement in project liquibase by liquibase.

the class LoadDataChange method generateStatementsFromRows.

protected SqlStatement[] generateStatementsFromRows(Database database, List<LoadDataRowConfig> rows) {
    List<SqlStatement> statements = new ArrayList<>();
    List<ExecutablePreparedStatementBase> preparedStatements = new ArrayList<>();
    for (LoadDataRowConfig row : rows) {
        List<LoadDataColumnConfig> columnsFromCsv = row.getColumns();
        if (row.needsPreparedStatement()) {
            ExecutablePreparedStatementBase stmt = this.createPreparedStatement(database, getCatalogName(), getSchemaName(), getTableName(), columnsFromCsv, getChangeSet(), Scope.getCurrentScope().getResourceAccessor());
            preparedStatements.add(stmt);
        } else {
            InsertStatement insertStatement = this.createStatement(getCatalogName(), getSchemaName(), getTableName());
            for (LoadDataColumnConfig column : columnsFromCsv) {
                String columnName = column.getName();
                Object value = column.getValueObject();
                if (value == null) {
                    value = "NULL";
                }
                insertStatement.addColumnValue(columnName, value);
                if (insertStatement instanceof InsertOrUpdateStatement) {
                    ((InsertOrUpdateStatement) insertStatement).setAllowColumnUpdate(columnName, column.getAllowUpdate() == null || column.getAllowUpdate());
                }
            }
            statements.add(insertStatement);
        }
    }
    if (rows.stream().anyMatch(LoadDataRowConfig::needsPreparedStatement)) {
        // If we have only prepared statements and the database supports batching, let's roll
        if (supportsBatchUpdates(database) && !preparedStatements.isEmpty()) {
            if (database instanceof PostgresDatabase || database instanceof MySQLDatabase) {
                // mysql supports batch updates, but the performance vs. the big insert is worse
                return preparedStatements.toArray(new SqlStatement[0]);
            } else {
                return new SqlStatement[] { new BatchDmlExecutablePreparedStatement(database, getCatalogName(), getSchemaName(), getTableName(), columns, getChangeSet(), Scope.getCurrentScope().getResourceAccessor(), preparedStatements) };
            }
        } else {
            return statements.toArray(new SqlStatement[0]);
        }
    } else {
        if (statements.isEmpty()) {
            // avoid returning unnecessary dummy statement
            return new SqlStatement[0];
        }
        InsertSetStatement statementSet = this.createStatementSet(getCatalogName(), getSchemaName(), getTableName());
        for (SqlStatement stmt : statements) {
            statementSet.addInsertStatement((InsertStatement) stmt);
        }
        if ((database instanceof MSSQLDatabase) || (database instanceof MySQLDatabase) || (database instanceof PostgresDatabase)) {
            List<InsertStatement> innerStatements = statementSet.getStatements();
            if ((innerStatements != null) && (!innerStatements.isEmpty()) && (innerStatements.get(0) instanceof InsertOrUpdateStatement)) {
                // cannot do insert or update in a single statement
                return statementSet.getStatementsArray();
            }
            // should the need arise, on generation.
            return new SqlStatement[] { statementSet };
        } else {
            return statementSet.getStatementsArray();
        }
    }
}
Also used : InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) MySQLDatabase(liquibase.database.core.MySQLDatabase) InsertStatement(liquibase.statement.core.InsertStatement) ExecutablePreparedStatementBase(liquibase.statement.ExecutablePreparedStatementBase) SqlStatement(liquibase.statement.SqlStatement) PostgresDatabase(liquibase.database.core.PostgresDatabase) InsertSetStatement(liquibase.statement.core.InsertSetStatement) BatchDmlExecutablePreparedStatement(liquibase.statement.BatchDmlExecutablePreparedStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Aggregations

InsertStatement (liquibase.statement.core.InsertStatement)27 PreparedStatement (java.sql.PreparedStatement)9 ResultSet (java.sql.ResultSet)9 Sql (liquibase.sql.Sql)8 CustomChangeException (liquibase.exception.CustomChangeException)6 SqlStatement (liquibase.statement.SqlStatement)6 Table (liquibase.structure.core.Table)6 ArrayList (java.util.ArrayList)4 ColumnConfig (liquibase.change.ColumnConfig)4 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)4 Map (java.util.Map)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 DeleteStatement (liquibase.statement.core.DeleteStatement)3 HashMap (java.util.HashMap)2 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)2 UnparsedSql (liquibase.sql.UnparsedSql)2 SequenceCurrentValueFunction (liquibase.statement.SequenceCurrentValueFunction)2 UpdateStatement (liquibase.statement.core.UpdateStatement)2 Column (liquibase.structure.core.Column)2