Search in sources :

Example 1 with CompositeAutoCloseable

use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.

the class JdbcSqlExecutor method stream.

@Override
public <T> Stream<T> stream(String sql, Object[] args, RowMapper<T> rowMapper) {
    CompositeAutoCloseable closeable = new CompositeAutoCloseable();
    Connection connection = closeable.add(connect());
    return stream(connection, sql, args, rowMapper, closeable);
}
Also used : CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) Connection(java.sql.Connection)

Example 2 with CompositeAutoCloseable

use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.

the class ExceptionIntegrationTest method lockTimeoutOnDelete.

@Test
void lockTimeoutOnDelete() {
    assumeTrue(dialect.supportsLockTimeout(), "Database does not support lock timeouts.");
    Database database = TestDatabase.testDatabase(dataSource);
    SalesAreaRow salesArea = SalesAreaRow.newBuilder().salesAreaId(newId()).salesAreaName("NZ").build();
    database.insert(salesArea);
    try (CompositeAutoCloseable closer = new CompositeAutoCloseable()) {
        Transaction transaction1 = closer.add(database.beginTransaction());
        database.update(SalesAreaRow.class).set(SalesAreaRow::salesCount).to(10L).where(SalesAreaRow::salesAreaId).isEqualTo(salesArea.salesAreaId()).execute(transaction1);
        Transaction transaction2 = closer.add(database.beginTransaction());
        closer.add(database.withLockTimeout(transaction2, 0, TimeUnit.MILLISECONDS));
        calling(() -> database.delete(SalesAreaRow.class).where(SalesAreaRow::salesAreaId).isEqualTo(salesArea.salesAreaId()).execute(transaction2)).shouldThrow(RuntimeSqlException.class).with(subclass(LockingException.class)).withMessage(startsWith("Locking failure while executing 'delete "));
        transaction2.rollback();
    }
}
Also used : CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) IncorrectSalesAreaRow(com.cadenzauk.siesta.model.IncorrectSalesAreaRow) SalesAreaRow(com.cadenzauk.siesta.model.SalesAreaRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with CompositeAutoCloseable

use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.

the class ExceptionIntegrationTest method lockTimeoutOnUpdate.

@Test
void lockTimeoutOnUpdate() {
    assumeTrue(dialect.supportsLockTimeout(), "Database does not support lock timeouts.");
    Database database = TestDatabase.testDatabase(dataSource);
    SalesAreaRow salesArea = SalesAreaRow.newBuilder().salesAreaId(newId()).salesAreaName("NZ").build();
    database.insert(salesArea);
    try (CompositeAutoCloseable closer = new CompositeAutoCloseable()) {
        Transaction transaction1 = closer.add(database.beginTransaction());
        database.update(SalesAreaRow.class).set(SalesAreaRow::salesCount).to(10L).where(SalesAreaRow::salesAreaId).isEqualTo(salesArea.salesAreaId()).execute(transaction1);
        Transaction transaction2 = closer.add(database.beginTransaction());
        closer.add(database.withLockTimeout(transaction2, 0, TimeUnit.MILLISECONDS));
        calling(() -> database.update(SalesAreaRow.class).set(SalesAreaRow::salesCount).to(1L).where(SalesAreaRow::salesAreaId).isEqualTo(salesArea.salesAreaId()).execute(transaction2)).shouldThrow(RuntimeSqlException.class).with(subclass(LockingException.class)).withMessage(startsWith("Locking failure while executing 'update "));
        transaction2.rollback();
    }
}
Also used : CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) IncorrectSalesAreaRow(com.cadenzauk.siesta.model.IncorrectSalesAreaRow) SalesAreaRow(com.cadenzauk.siesta.model.SalesAreaRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with CompositeAutoCloseable

use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.

the class SiestaExample method selectIntoObject.

@Test
void selectIntoObject() {
    Database database = Database.newBuilder().defaultSchema("SIESTA").defaultSqlExecutor(JdbcSqlExecutor.of(dataSource)).build();
    database.insert(new Manufacturer(2006L, "Spacely Space Sprockets, Inc"), new Manufacturer(2007L, "Cogswell's Cogs"), new Manufacturer(2008L, "Orbit City Gears"));
    database.insert(new Widget(1006L, "Cog", 2006L, Optional.of("Spacely Sprocket")), new Widget(1007L, "Cog", 2007L, Optional.of("Cogswell Cog")), new Widget(1008L, "Cog", 2007L, Optional.of("Cogswell Sprocket")));
    List<Tuple2<String, Integer>> partCountsBySupplier = database.from(Manufacturer.class, "m").leftJoin(Widget.class, "w").on(Widget::manufacturerId).isEqualTo(Manufacturer::manufacturerId).select(Manufacturer::name).comma(countDistinct(Widget::widgetId)).where(Manufacturer::manufacturerId).isIn(2006L, 2007L, 2008L).groupBy(Manufacturer::manufacturerId).orderBy(Manufacturer::manufacturerId).list();
    List<ManufacturerSummary> manufacturerSummaries = database.from(Manufacturer.class, "m").leftJoin(Widget.class, "w").on(Widget::manufacturerId).isEqualTo(Manufacturer::manufacturerId).selectInto(ManufacturerSummary.class).with(Manufacturer::name).as(ManufacturerSummary::name).with(countDistinct(Widget::widgetId)).as(ManufacturerSummary::numberOfPartsSupplied).where(Manufacturer::manufacturerId).isIn(2006L, 2007L, 2008L).groupBy(Manufacturer::manufacturerId).orderBy(Manufacturer::manufacturerId).list();
    List<ManufacturerSummary> nonSuppliers = database.from(Manufacturer.class, "m").leftJoin(Widget.class, "w").on(Widget::manufacturerId).isEqualTo(Manufacturer::manufacturerId).selectInto(ManufacturerSummary.class).with(Manufacturer::name).as(ManufacturerSummary::name).with(countDistinct(Widget::widgetId)).as(ManufacturerSummary::numberOfPartsSupplied).where(Manufacturer::manufacturerId).isIn(2006L, 2007L, 2008L).groupBy(Manufacturer::manufacturerId).having(countDistinct(Widget::widgetId)).isEqualTo(0).orderBy(Manufacturer::manufacturerId).list();
    try (CompositeAutoCloseable autoCloseable = new CompositeAutoCloseable()) {
        database.from(Manufacturer.class).select(Manufacturer::name).where(Manufacturer::manufacturerId).isEqualTo(1L).stream(autoCloseable).forEach(System.out::println);
    }
    assertThat(partCountsBySupplier, hasSize(3));
    assertThat(partCountsBySupplier.get(0).item1(), is("Spacely Space Sprockets, Inc"));
    assertThat(partCountsBySupplier.get(1).item1(), is("Cogswell's Cogs"));
    assertThat(partCountsBySupplier.get(2).item1(), is("Orbit City Gears"));
    assertThat(partCountsBySupplier.get(0).item2(), is(1));
    assertThat(partCountsBySupplier.get(1).item2(), is(2));
    assertThat(partCountsBySupplier.get(2).item2(), is(0));
    assertThat(manufacturerSummaries, hasSize(3));
    assertThat(manufacturerSummaries.get(0).name(), is("Spacely Space Sprockets, Inc"));
    assertThat(manufacturerSummaries.get(1).name(), is("Cogswell's Cogs"));
    assertThat(manufacturerSummaries.get(2).name(), is("Orbit City Gears"));
    assertThat(manufacturerSummaries.get(0).numberOfPartsSupplied(), is(1));
    assertThat(manufacturerSummaries.get(1).numberOfPartsSupplied(), is(2));
    assertThat(manufacturerSummaries.get(2).numberOfPartsSupplied(), is(0));
    assertThat(nonSuppliers, hasSize(1));
    assertThat(nonSuppliers.get(0).name(), is("Orbit City Gears"));
}
Also used : CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) Tuple2(com.cadenzauk.core.tuple.Tuple2) Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test) IntegrationTest(com.cadenzauk.siesta.IntegrationTest)

Example 5 with CompositeAutoCloseable

use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.

the class SelectTest method stream.

@Test
void stream() {
    Database database = Database.newBuilder().defaultSchema("SIESTA").build();
    when(transaction.stream(any(), any(), Mockito.<RowMapper<Row1>>any())).thenReturn(stream);
    Stream<Row1> result;
    try (CompositeAutoCloseable autoCloseable = new CompositeAutoCloseable()) {
        result = database.from(Row1.class, "w").stream(transaction, autoCloseable);
    }
    assertThat(result, sameInstance(stream));
    verify(stream).close();
}
Also used : CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MockitoTest(com.cadenzauk.core.MockitoTest)

Aggregations

CompositeAutoCloseable (com.cadenzauk.core.lang.CompositeAutoCloseable)12 Test (org.junit.jupiter.api.Test)11 MockitoTest (com.cadenzauk.core.MockitoTest)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 IncorrectSalesAreaRow (com.cadenzauk.siesta.model.IncorrectSalesAreaRow)3 SalesAreaRow (com.cadenzauk.siesta.model.SalesAreaRow)3 SQLException (java.sql.SQLException)2 Stream (java.util.stream.Stream)2 RuntimeSqlException (com.cadenzauk.core.sql.RuntimeSqlException)1 Tuple2 (com.cadenzauk.core.tuple.Tuple2)1 Database (com.cadenzauk.siesta.Database)1 IntegrationTest (com.cadenzauk.siesta.IntegrationTest)1 LockTestRow (com.cadenzauk.siesta.model.LockTestRow)1 Connection (java.sql.Connection)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1