Search in sources :

Example 1 with MutableTuple

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);
    }
}
Also used : DefaultOutput(io.requery.sql.gen.DefaultOutput) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) MutableTuple(io.requery.query.MutableTuple)

Example 2 with MutableTuple

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);
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) PersistenceException(io.requery.PersistenceException) SQLException(java.sql.SQLException) MutableTuple(io.requery.query.MutableTuple)

Example 3 with MutableTuple

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;
}
Also used : MutableTuple(io.requery.query.MutableTuple)

Aggregations

MutableTuple (io.requery.query.MutableTuple)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 PersistenceException (io.requery.PersistenceException)1 DefaultOutput (io.requery.sql.gen.DefaultOutput)1 ResultSet (java.sql.ResultSet)1