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());
}
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());
}
Aggregations