Search in sources :

Example 1 with SqlStatementBuilder

use of io.vertigo.database.sql.statement.SqlStatementBuilder in project vertigo by KleeGroup.

the class AbstractSqlDataBaseManagerTest method testBatchInserts.

@Test
public void testBatchInserts() throws Exception {
    final SqlConnectionProvider sqlConnectionProvider = dataBaseManager.getConnectionProvider(SqlDataBaseManager.MAIN_CONNECTION_PROVIDER_NAME);
    final String sql = INSERT_INTO_MOVIE_VALUES;
    final List<Movie> movies = Movies.bondMovies();
    // --prepare data
    final List<List<SqlParameter>> batch = new ArrayList<>();
    for (final Movie movie : movies) {
        final List<SqlParameter> sqlParameters = Arrays.asList(SqlParameter.of(Long.class, movie.getId()), SqlParameter.of(String.class, movie.getTitle()), SqlParameter.of(Double.class, movie.getFps()), SqlParameter.of(BigDecimal.class, movie.getIncome()), SqlParameter.of(Boolean.class, movie.getColor()), SqlParameter.of(Date.class, movie.getReleaseDate()), SqlParameter.of(LocalDate.class, movie.getReleaseLocalDate()), SqlParameter.of(Instant.class, movie.getReleaseInstant()), SqlParameter.of(DataStream.class, movie.getIcon()));
        batch.add(sqlParameters);
    }
    final SqlConnection connection = sqlConnectionProvider.obtainConnection();
    final OptionalInt result;
    try {
        final SqlStatementBuilder sqlStatementBuilder = SqlStatement.builder(sql);
        for (final Movie movie : movies) {
            sqlStatementBuilder.bind("movie", Movie.class, movie).nextLine();
        }
        result = dataBaseManager.executeBatch(sqlStatementBuilder.build(), connection);
        connection.commit();
    } finally {
        connection.release();
    }
    // ---
    if (result.isPresent()) {
        Assert.assertEquals(movies.size(), result.getAsInt());
    }
    final List<Integer> countMovie = executeQuery(Integer.class, "select count(*) from movie", 1);
    Assert.assertEquals(movies.size(), countMovie.get(0).intValue());
}
Also used : Movie(io.vertigo.database.sql.data.Movie) SqlParameter(io.vertigo.database.sql.statement.SqlParameter) DataStream(io.vertigo.lang.DataStream) Instant(java.time.Instant) ArrayList(java.util.ArrayList) OptionalInt(java.util.OptionalInt) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(java.time.LocalDate) SqlStatementBuilder(io.vertigo.database.sql.statement.SqlStatementBuilder) SqlConnectionProvider(io.vertigo.database.sql.connection.SqlConnectionProvider) ArrayList(java.util.ArrayList) List(java.util.List) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Test(org.junit.Test)

Example 2 with SqlStatementBuilder

use of io.vertigo.database.sql.statement.SqlStatementBuilder in project vertigo by KleeGroup.

the class TaskEngineProcBatch method setNamedParameters.

@Override
protected void setNamedParameters(final SqlStatementBuilder sqlStatementBuilder) {
    final List<TaskAttribute> potentialBatchAttributes = getTaskDefinition().getInAttributes().stream().filter(// multiple
    inAttribute -> inAttribute.getDomain().isMultiple()).collect(Collectors.toList());
    Assertion.checkState(potentialBatchAttributes.size() == 1, "For batch a single List param is required");
    final TaskAttribute listAttribute = potentialBatchAttributes.get(0);
    final List<TaskAttribute> otherAttributes = getTaskDefinition().getInAttributes().stream().filter(// not multiple
    inAttribute -> !inAttribute.getDomain().isMultiple()).collect(Collectors.toList());
    // ---
    final List<?> list = getValue(listAttribute.getName());
    list.forEach(object -> {
        // we bind the parameter of the batch
        sqlStatementBuilder.bind(listAttribute.getName(), listAttribute.getDomain().getJavaClass(), object);
        // we add all the "constant" parameters
        otherAttributes.forEach(otherAttribute -> sqlStatementBuilder.bind(otherAttribute.getName(), otherAttribute.getDomain().getJavaClass(), getValue(otherAttribute.getName())));
        sqlStatementBuilder.nextLine();
    });
}
Also used : TaskAttribute(io.vertigo.dynamo.task.metamodel.TaskAttribute) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) VTransactionManager(io.vertigo.commons.transaction.VTransactionManager) SqlStatementBuilder(io.vertigo.database.sql.statement.SqlStatementBuilder) Inject(javax.inject.Inject) ScriptManager(io.vertigo.commons.script.ScriptManager) SqlDataBaseManager(io.vertigo.database.sql.SqlDataBaseManager) StoreManager(io.vertigo.dynamo.store.StoreManager) SQLException(java.sql.SQLException) List(java.util.List) SqlStatement(io.vertigo.database.sql.statement.SqlStatement) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Assertion(io.vertigo.lang.Assertion) TaskAttribute(io.vertigo.dynamo.task.metamodel.TaskAttribute)

Example 3 with SqlStatementBuilder

use of io.vertigo.database.sql.statement.SqlStatementBuilder in project vertigo by KleeGroup.

the class AbstractTaskEngineSQL method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute() {
    final SqlConnection connection = obtainConnection();
    final SqlStatementBuilder statementBuilder = SqlStatement.builder(getSqlQuery().trim());
    setNamedParameters(statementBuilder);
    final SqlStatement sqlStatement = statementBuilder.build();
    try {
        // Execute le Statement JDBC.
        final OptionalInt sqlRowcountOpt = doExecute(sqlStatement, connection);
        // On positionne le nombre de lignes affectées.
        sqlRowcountOpt.ifPresent(this::setRowCount);
    } catch (final BatchUpdateException sqle) {
        // Gère les erreurs d'exécution Batch JDBC.
        throw handleSQLException(connection, sqle.getNextException(), sqlStatement.getSqlQuery());
    } catch (final SQLException sqle) {
        // Gère les erreurs d'exécution JDBC.
        throw handleSQLException(connection, sqle, sqlStatement.getSqlQuery());
    }
}
Also used : SqlStatement(io.vertigo.database.sql.statement.SqlStatement) SqlStatementBuilder(io.vertigo.database.sql.statement.SqlStatementBuilder) SQLException(java.sql.SQLException) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) OptionalInt(java.util.OptionalInt) BatchUpdateException(java.sql.BatchUpdateException)

Aggregations

SqlConnection (io.vertigo.database.sql.connection.SqlConnection)3 SqlStatementBuilder (io.vertigo.database.sql.statement.SqlStatementBuilder)3 OptionalInt (java.util.OptionalInt)3 SqlStatement (io.vertigo.database.sql.statement.SqlStatement)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 ScriptManager (io.vertigo.commons.script.ScriptManager)1 VTransactionManager (io.vertigo.commons.transaction.VTransactionManager)1 SqlDataBaseManager (io.vertigo.database.sql.SqlDataBaseManager)1 SqlConnectionProvider (io.vertigo.database.sql.connection.SqlConnectionProvider)1 Movie (io.vertigo.database.sql.data.Movie)1 SqlParameter (io.vertigo.database.sql.statement.SqlParameter)1 StoreManager (io.vertigo.dynamo.store.StoreManager)1 TaskAttribute (io.vertigo.dynamo.task.metamodel.TaskAttribute)1 Assertion (io.vertigo.lang.Assertion)1 DataStream (io.vertigo.lang.DataStream)1 BigDecimal (java.math.BigDecimal)1 BatchUpdateException (java.sql.BatchUpdateException)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1