Search in sources :

Example 16 with SqlConnection

use of io.vertigo.database.sql.connection.SqlConnection in project vertigo by KleeGroup.

the class AbstractSqlDataBaseManagerTest method createDatas.

private void createDatas() throws Exception {
    final SqlConnection connection = obtainMainConnection();
    try {
        insert(connection, Movies.createMovie(1, Movies.TITLE_MOVIE_1, // mail
        null, null, null, null, new Date(1941 - 1900, 5 - 1, 1, 16, 30), LocalDate.of(1941, 5, 1), LocalDateTime.of(1941, 5, 1, 16, 30).toInstant(ZoneOffset.UTC)));
        // -----
        insert(connection, Movies.createMovie(2, Movies.TITLE_MOVIE_2, // mail
        null, null, null, null, new Date(1958 - 1900, 5 - 1, 9, 16, 30), LocalDate.of(1958, 5, 9), LocalDateTime.of(1958, 5, 9, 16, 30).toInstant(ZoneOffset.UTC)));
        // -----
        // On passe par une requête bindée
        insert(connection, Movies.createMovie(3, Movies.TITLE_MOVIE_3, // Mail
        null, null, null, null, null, null, null));
        connection.commit();
    } finally {
        connection.release();
    }
}
Also used : SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 17 with SqlConnection

use of io.vertigo.database.sql.connection.SqlConnection 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)

Example 18 with SqlConnection

use of io.vertigo.database.sql.connection.SqlConnection in project vertigo by KleeGroup.

the class NameSpace2JavaTest method execSqlScript.

private void execSqlScript(final String sqlScript, final App app) {
    final ResourceManager resourceManager = app.getComponentSpace().resolve(ResourceManager.class);
    final SqlDataBaseManager sqlDataBaseManager = app.getComponentSpace().resolve(SqlDataBaseManager.class);
    final SqlConnection connection = sqlDataBaseManager.getConnectionProvider(SqlDataBaseManager.MAIN_CONNECTION_PROVIDER_NAME).obtainConnection();
    DataBaseScriptUtil.execSqlScript(connection, sqlScript, resourceManager, sqlDataBaseManager);
}
Also used : SqlDataBaseManager(io.vertigo.database.sql.SqlDataBaseManager) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) ResourceManager(io.vertigo.core.resource.ResourceManager)

Aggregations

SqlConnection (io.vertigo.database.sql.connection.SqlConnection)18 Test (org.junit.Test)7 VTransactionWritable (io.vertigo.commons.transaction.VTransactionWritable)5 Movie (io.vertigo.database.sql.data.Movie)5 DefinitionSpace (io.vertigo.core.definition.DefinitionSpace)2 SqlDataBaseManager (io.vertigo.database.sql.SqlDataBaseManager)2 SqlConnectionProvider (io.vertigo.database.sql.connection.SqlConnectionProvider)2 SqlStatementBuilder (io.vertigo.database.sql.statement.SqlStatementBuilder)2 SearchIndexDefinition (io.vertigo.dynamo.search.metamodel.SearchIndexDefinition)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 OptionalInt (java.util.OptionalInt)2 HealthChecked (io.vertigo.commons.analytics.health.HealthChecked)1 HealthMeasureBuilder (io.vertigo.commons.analytics.health.HealthMeasureBuilder)1 VTransaction (io.vertigo.commons.transaction.VTransaction)1 ResourceManager (io.vertigo.core.resource.ResourceManager)1 SqlParameter (io.vertigo.database.sql.statement.SqlParameter)1 SqlStatement (io.vertigo.database.sql.statement.SqlStatement)1