Search in sources :

Example 1 with BaseScalar

use of io.requery.query.BaseScalar 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 2 with BaseScalar

use of io.requery.query.BaseScalar 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

BaseScalar (io.requery.query.BaseScalar)2 DefaultOutput (io.requery.sql.gen.DefaultOutput)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Type (io.requery.meta.Type)1