use of io.requery.sql.gen.DefaultOutput in project requery by requery.
the class BatchUpdateOperation method evaluate.
@Override
public int[] evaluate(QueryElement<int[]> query) {
int[] result = batchInStatement ? null : new int[length];
try (Connection connection = configuration.getConnection()) {
DefaultOutput generator = new DefaultOutput(configuration, query);
String sql = generator.toSql();
StatementListener listener = configuration.getStatementListener();
try (PreparedStatement statement = prepare(sql, connection)) {
for (int i = 0; i < length; i++) {
E element = elements[i];
parameterBinder.bindParameters(statement, element, null);
if (batchInStatement) {
statement.addBatch();
} else {
listener.beforeExecuteBatchUpdate(statement, sql);
result[i] = statement.executeUpdate();
listener.afterExecuteBatchUpdate(statement, result);
readGeneratedKeys(i, statement);
}
}
if (batchInStatement) {
listener.beforeExecuteBatchUpdate(statement, sql);
result = statement.executeBatch();
listener.afterExecuteBatchUpdate(statement, result);
readGeneratedKeys(0, statement);
}
}
} catch (BatchUpdateException e) {
result = e.getUpdateCounts();
if (result == null) {
throw new PersistenceException(e);
}
} catch (SQLException e) {
throw new PersistenceException(e);
}
return result;
}
use of io.requery.sql.gen.DefaultOutput in project requery by requery.
the class EntityUpdateOperation method evaluate.
@Override
public Scalar<Integer> evaluate(final QueryElement<Scalar<Integer>> query) {
return new BaseScalar<Integer>(configuration.getWriteExecutor()) {
@Override
public Integer evaluate() {
// doesn't use the query params, just maps to the parameterBinder callback
QueryBuilder qb = new QueryBuilder(configuration.getQueryBuilderOptions());
DefaultOutput output = new DefaultOutput(configuration, query, qb, null, false);
String sql = output.toSql();
int result;
try (Connection connection = configuration.getConnection()) {
StatementListener listener = configuration.getStatementListener();
try (PreparedStatement statement = prepare(sql, connection)) {
bindParameters(statement);
listener.beforeExecuteUpdate(statement, sql, null);
result = statement.executeUpdate();
listener.afterExecuteUpdate(statement, result);
readGeneratedKeys(0, statement);
}
} catch (SQLException e) {
throw new StatementExecutionException(e, sql);
}
return result;
}
};
}
use of io.requery.sql.gen.DefaultOutput in project requery by requery.
the class InsertReturningOperation method evaluate.
@Override
public Result<Tuple> evaluate(final QueryElement<Result<Tuple>> query) {
DefaultOutput generator = new DefaultOutput(configuration, query);
String sql = generator.toSql();
BoundParameters parameters = generator.parameters();
int count;
PreparedStatement statement = null;
try {
Connection connection = configuration.getConnection();
StatementListener listener = configuration.getStatementListener();
if (query.insertType() == InsertType.SELECT) {
statement = connection.prepareStatement(sql, Statement.NO_GENERATED_KEYS);
} else {
statement = prepare(sql, connection);
}
mapParameters(statement, parameters);
listener.beforeExecuteUpdate(statement, sql, parameters);
count = statement.executeUpdate();
listener.afterExecuteUpdate(statement, count);
if (selection == null || selection.isEmpty() || query.insertType() == InsertType.SELECT) {
connection.close();
MutableTuple tuple = new MutableTuple(1);
tuple.set(0, NamedExpression.ofInteger("count"), count);
return new SingleResult<Tuple>(tuple);
} else {
ResultSet results = statement.getGeneratedKeys();
return new GeneratedKeyResult(configuration, selection, connection, results, count);
}
} catch (Exception e) {
throw StatementExecutionException.closing(statement, e, sql);
}
}
use of io.requery.sql.gen.DefaultOutput in project requery by requery.
the class SelectResult method createQuery.
private BoundParameters createQuery(int skip, int take) {
// query can't already have been limited and skip/take must be non-defaults
if (limit == null && take > 0 && take != Integer.MAX_VALUE) {
query.limit(take).offset(skip);
}
DefaultOutput generator = new DefaultOutput(configuration, query);
sql = generator.toSql();
return generator.parameters();
}
use of io.requery.sql.gen.DefaultOutput in project requery by requery.
the class UpdateOperation method evaluate.
@Override
public Scalar<Integer> evaluate(final QueryElement<Scalar<Integer>> query) {
return new BaseScalar<Integer>(configuration.getWriteExecutor()) {
@Override
public Integer evaluate() {
DefaultOutput output = new DefaultOutput(configuration, query);
String sql = output.toSql();
int result;
TransactionProvider transactionProvider = configuration.getTransactionProvider();
Set<Type<?>> types = query.entityTypes();
try (TransactionScope scope = new TransactionScope(transactionProvider, types);
Connection connection = configuration.getConnection()) {
StatementListener listener = configuration.getStatementListener();
try (PreparedStatement statement = prepare(sql, connection)) {
BoundParameters parameters = output.parameters();
mapParameters(statement, parameters);
listener.beforeExecuteUpdate(statement, sql, parameters);
result = statement.executeUpdate();
listener.afterExecuteUpdate(statement, result);
readGeneratedKeys(0, statement);
}
scope.commit();
} catch (SQLException e) {
throw new StatementExecutionException(e, sql);
}
return result;
}
};
}
Aggregations