Search in sources :

Example 1 with DefaultOutput

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

Example 2 with DefaultOutput

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

Example 3 with DefaultOutput

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);
    }
}
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 4 with DefaultOutput

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();
}
Also used : DefaultOutput(io.requery.sql.gen.DefaultOutput)

Example 5 with DefaultOutput

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

Aggregations

DefaultOutput (io.requery.sql.gen.DefaultOutput)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 BaseScalar (io.requery.query.BaseScalar)2 PersistenceException (io.requery.PersistenceException)1 Type (io.requery.meta.Type)1 MutableTuple (io.requery.query.MutableTuple)1 BatchUpdateException (java.sql.BatchUpdateException)1 ResultSet (java.sql.ResultSet)1