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