Search in sources :

Example 1 with GenerationMode

use of io.vertigo.database.sql.vendor.SqlDialect.GenerationMode in project vertigo by KleeGroup.

the class TaskEngineInsertWithGeneratedKeys method doExecute.

/**
 * {@inheritDoc}
 */
@Override
public OptionalInt doExecute(final SqlStatement sqlStatement, final SqlConnection connection) throws SQLException {
    Assertion.checkNotNull(sqlStatement);
    Assertion.checkNotNull(connection);
    // --
    final GenerationMode generationMode = connection.getDataBase().getSqlDialect().getGenerationMode();
    // gestion de generatedKey
    final Entity entity = getValue("DTO");
    final DtDefinition dtDefinition = DtObjectUtil.findDtDefinition(entity);
    final DtField idField = dtDefinition.getIdField().get();
    final Tuples.Tuple2<Integer, ?> result = getDataBaseManager().executeUpdateWithGeneratedKey(sqlStatement, generationMode, idField.getName(), idField.getDomain().getJavaClass(), connection);
    final Object id = result.getVal2();
    idField.getDataAccessor().setValue(entity, id);
    // ---
    return /*sqlRowcount*/
    OptionalInt.of(result.getVal1());
}
Also used : Entity(io.vertigo.dynamo.domain.model.Entity) GenerationMode(io.vertigo.database.sql.vendor.SqlDialect.GenerationMode) Tuples(io.vertigo.lang.Tuples) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) DtField(io.vertigo.dynamo.domain.metamodel.DtField)

Example 2 with GenerationMode

use of io.vertigo.database.sql.vendor.SqlDialect.GenerationMode in project vertigo by KleeGroup.

the class AbstractSqlDataBaseManagerTest method testInsert.

@Test
public final void testInsert() throws Exception {
    final String insertWithgeneratedKey = obtainMainConnection().getDataBase().getSqlDialect().createInsertQuery("ID", Arrays.asList("TITLE"), "seq_", "movie");
    final GenerationMode generationMode = obtainMainConnection().getDataBase().getSqlDialect().getGenerationMode();
    // We check that we have the right expected mode
    Assert.assertEquals(getExpectedGenerationMode(), generationMode);
    // ---
    final SqlConnection connection = obtainMainConnection();
    long generatedKey;
    try {
        final Movie movie = new Movie();
        movie.setTitle("frankenstein");
        generatedKey = dataBaseManager.executeUpdateWithGeneratedKey(SqlStatement.builder(insertWithgeneratedKey).bind("DTO", Movie.class, movie).build(), generationMode, "ID", Long.class, connection).getVal2();
        connection.commit();
    } finally {
        connection.release();
    }
    final List<Integer> result = executeQuery(Integer.class, "select count(*) from movie", null);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(1, result.get(0).intValue());
    final List<Integer> keys = executeQuery(Integer.class, "select id from movie", null);
    Assert.assertEquals(1, keys.size());
    Assert.assertEquals(generatedKey, keys.get(0).intValue());
}
Also used : Movie(io.vertigo.database.sql.data.Movie) GenerationMode(io.vertigo.database.sql.vendor.SqlDialect.GenerationMode) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Test(org.junit.Test)

Aggregations

GenerationMode (io.vertigo.database.sql.vendor.SqlDialect.GenerationMode)2 SqlConnection (io.vertigo.database.sql.connection.SqlConnection)1 Movie (io.vertigo.database.sql.data.Movie)1 DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)1 DtField (io.vertigo.dynamo.domain.metamodel.DtField)1 Entity (io.vertigo.dynamo.domain.model.Entity)1 Tuples (io.vertigo.lang.Tuples)1 Test (org.junit.Test)1