Search in sources :

Example 1 with SqlParameter

use of io.vertigo.database.sql.statement.SqlParameter 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 SqlParameter

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

the class SqlDataBaseManagerImpl method executeBatch.

/**
 * {@inheritDoc}
 */
@Override
public OptionalInt executeBatch(final SqlStatement sqlStatement, final SqlConnection connection) throws SQLException {
    Assertion.checkNotNull(sqlStatement);
    Assertion.checkNotNull(connection);
    // ---
    try (final PreparedStatement statement = sqlStatementDriver.createStatement(sqlStatement.getSqlQuery(), connection)) {
        for (final List<SqlParameter> parameters : sqlStatement.getSqlParametersForBatch()) {
            sqlStatementDriver.setParameters(statement, parameters, connection);
            statement.addBatch();
        }
        return traceWithReturn(sqlStatement.getSqlQuery(), tracer -> doExecuteBatch(statement, tracer));
    } catch (final WrappedSqlException e) {
        throw e.getSqlException();
    }
}
Also used : SqlParameter(io.vertigo.database.sql.statement.SqlParameter) PreparedStatement(java.sql.PreparedStatement)

Example 3 with SqlParameter

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

the class SqlStatementDriver method setParameters.

// ------------------ Set values on statement ------------------------------//
void setParameters(final PreparedStatement statement, final List<SqlParameter> parameters, final SqlConnection connection) throws SQLException {
    // -----
    for (int index = 0; index < parameters.size(); index++) {
        final SqlParameter parameter = parameters.get(index);
        final Class javaDataType = parameter.getDataType();
        final Class sqlDataType = sqlMapper.getSqlType(javaDataType);
        connection.getDataBase().getSqlMapping().setValueOnStatement(statement, index + 1, sqlDataType, sqlMapper.toSql(javaDataType, parameter.getValue()));
    }
}
Also used : SqlParameter(io.vertigo.database.sql.statement.SqlParameter)

Aggregations

SqlParameter (io.vertigo.database.sql.statement.SqlParameter)3 SqlConnection (io.vertigo.database.sql.connection.SqlConnection)1 SqlConnectionProvider (io.vertigo.database.sql.connection.SqlConnectionProvider)1 Movie (io.vertigo.database.sql.data.Movie)1 SqlStatementBuilder (io.vertigo.database.sql.statement.SqlStatementBuilder)1 DataStream (io.vertigo.lang.DataStream)1 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 OptionalInt (java.util.OptionalInt)1 Test (org.junit.Test)1