use of io.requery.query.MutableTuple 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.query.MutableTuple in project requery by requery.
the class RawTupleQuery method get.
@Override
public Result<Tuple> get() {
PreparedStatement statement = null;
try {
Connection connection = configuration.getConnection();
statement = prepare(sql, connection);
mapParameters(statement, boundParameters);
switch(queryType) {
case SELECT:
default:
return new TupleResult(statement);
case INSERT:
case UPDATE:
case UPSERT:
case DELETE:
case TRUNCATE:
case MERGE:
// DML, only the row count is returned
StatementListener listener = configuration.getStatementListener();
listener.beforeExecuteUpdate(statement, sql, boundParameters);
int count = statement.executeUpdate();
listener.afterExecuteUpdate(statement, count);
MutableTuple tuple = new MutableTuple(1);
tuple.set(0, NamedExpression.ofInteger("count"), count);
try {
statement.close();
} finally {
try {
connection.close();
} catch (Exception ignored) {
}
}
return new SingleResult<Tuple>(tuple);
}
} catch (Exception e) {
throw StatementExecutionException.closing(statement, e, sql);
}
}
use of io.requery.query.MutableTuple in project requery by requery.
the class TupleResultReader method read.
@Override
public Tuple read(ResultSet results, Set<? extends Expression<?>> selection) throws SQLException {
MutableTuple tuple = new MutableTuple(selection.size());
int index = 1;
Mapping mapping = configuration.getMapping();
for (Expression<?> expression : selection) {
Object value = mapping.read(expression, results, index);
tuple.set(index - 1, expression, value);
index++;
}
return tuple;
}
Aggregations