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