Search in sources :

Example 6 with SqlConnection

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

the class AbstractSqlDataBaseManagerTest method testConnection.

@Test
public void testConnection() throws Exception {
    final SqlConnection connection = obtainMainConnection();
    try {
        Assert.assertNotNull(connection);
        connection.commit();
    } finally {
        connection.release();
    }
}
Also used : SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Test(org.junit.Test)

Example 7 with SqlConnection

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

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

the class AbstractSqlDataBaseManagerTest method testTwoDataSource.

// On teste un preparestatement mappé sur un type statique (Class famille)
@Test
public void testTwoDataSource() throws Exception {
    createDatas();
    setupSecondary();
    // on crée des données dans 'secondary'
    final SqlConnection connection = dataBaseManager.getConnectionProvider("secondary").obtainConnection();
    try {
        execpreparedStatement(connection, "insert into movie values (1, 'Star wars', null, null, null, null, null, null, null, null)");
        execpreparedStatement(connection, "insert into movie values (2, 'Will Hunting', null, null, null, null, null, null, null, null)");
        execpreparedStatement(connection, "insert into movie values (3, 'Usual Suspects', null, null, null, null, null, null, null, null)");
        // -----
        // 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();
    }
    // ----
    final List<Integer> result2 = executeQuery(Integer.class, "select count(*) from movie", dataBaseManager.getConnectionProvider("secondary"), 1);
    Assert.assertEquals(1, result2.size());
    Assert.assertEquals(4, result2.get(0).intValue());
    final List<Movie> resultMovie1 = executeQuery(Movie.class, "select * from movie where id=1", dataBaseManager.getConnectionProvider("secondary"), 1);
    Assert.assertEquals(1, resultMovie1.size());
    Assert.assertEquals("Star wars", resultMovie1.get(0).getTitle());
    final List<Integer> result1 = executeQuery(Integer.class, "select count(*) from movie", dataBaseManager.getConnectionProvider(SqlDataBaseManager.MAIN_CONNECTION_PROVIDER_NAME), 1);
    Assert.assertEquals(1, result1.size());
    Assert.assertEquals(3, result1.get(0).intValue());
}
Also used : Movie(io.vertigo.database.sql.data.Movie) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Test(org.junit.Test)

Example 9 with SqlConnection

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

the class AbstractSqlDataBaseManagerTest method testPrimitiveParam.

@Test
public void testPrimitiveParam() throws Exception {
    // On crée les données
    createDatas();
    // ----
    final SqlConnection connection = obtainMainConnection();
    final List<Movie> movies;
    try {
        movies = dataBaseManager.executeQuery(SqlStatement.builder("select * from movie where movie.title = #title# ").bind("title", String.class, Movies.TITLE_MOVIE_1).build(), Movie.class, 1, connection);
    } finally {
        connection.release();
    }
    Assert.assertEquals(1, movies.size());
    Assert.assertEquals(Movies.TITLE_MOVIE_1, movies.get(0).getTitle());
}
Also used : Movie(io.vertigo.database.sql.data.Movie) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Test(org.junit.Test)

Example 10 with SqlConnection

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

the class HibernateConnectionProviderTest method ping.

@Test
public void ping() throws SQLException {
    final SqlConnectionProvider sqlConnectionProvider = sqlDataBaseManager.getConnectionProvider(SqlDataBaseManager.MAIN_CONNECTION_PROVIDER_NAME);
    final String testQuery = sqlConnectionProvider.getDataBase().getSqlDialect().getTestQuery();
    try (VTransactionWritable transaction = transactionManager.createCurrentTransaction()) {
        final SqlConnection connection = sqlConnectionProvider.obtainConnection();
        try {
            sqlDataBaseManager.executeQuery(SqlStatement.builder(testQuery).build(), Integer.class, 1, connection);
        } finally {
            connection.release();
        }
    }
}
Also used : SqlConnectionProvider(io.vertigo.database.sql.connection.SqlConnectionProvider) VTransactionWritable(io.vertigo.commons.transaction.VTransactionWritable) SqlConnection(io.vertigo.database.sql.connection.SqlConnection) Test(org.junit.Test)

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