use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.
the class TableBuilderTest method embeddedNotInsertableFromBuilder.
@Test
void embeddedNotInsertableFromBuilder() {
Database database = Database.newBuilder().defaultSchema("DEFAULT_SCHEMA").table(EmbeddedColumnsNoAnnotations.class, t -> t.embedded(EmbeddableNoAnnotations.class, EmbeddedColumnsNoAnnotations::gross, c -> c.insertable(false)).embedded(EmbeddableNoAnnotations.class, EmbeddedColumnsNoAnnotations::net)).build();
String insertSql = database.table(EmbeddedColumnsNoAnnotations.class).columns().flatMap(Column::insertColumnSql).collect(joining(", "));
assertThat(database.table(EmbeddedColumnsNoAnnotations.class).column(EmbeddedColumnsNoAnnotations::gross).insertable(), is(false));
assertThat(database.table(EmbeddedColumnsNoAnnotations.class).column(EmbeddedColumnsNoAnnotations::net).insertable(), is(true));
assertThat(insertSql, is("NET_AMOUNT, NET_UNIT"));
}
use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.
the class TableBuilderTest method embeddedNotInsertableFromAnnotationOverride.
@Test
void embeddedNotInsertableFromAnnotationOverride() {
Database database = Database.newBuilder().defaultSchema("DEFAULT_SCHEMA").build();
String insertSql = database.table(EmbeddedColumnsInsertOverride.class).columns().flatMap(Column::insertColumnSql).collect(joining(", "));
assertThat(database.table(EmbeddedColumnsInsertOverride.class).column(EmbeddedColumnsInsertOverride::gross).insertable(), is(true));
assertThat(database.table(EmbeddedColumnsInsertOverride.class).column(EmbeddedColumnsInsertOverride::net).insertable(), is(true));
assertThat(insertSql, is("GROSS_AMOUNT, GROSS_UNIT, NET_AMOUNT"));
}
use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.
the class SiestaExample method typeSafeIdExample.
@Test
void typeSafeIdExample() {
Database database = Database.newBuilder().defaultSqlExecutor(JdbcSqlExecutor.of(dataSource)).table(Widget.class, t -> t.column(Widget::description, "WIDGET_DESC")).adapter(WidgetId.class, DbTypeId.BIGINT, WidgetId::id, WidgetId::new).adapter(ManufacturerId.class, DbTypeId.BIGINT, ManufacturerId::id, ManufacturerId::new).build();
WidgetId widgetId = new WidgetId(newId());
ManufacturerId manufacturerId = new ManufacturerId(newId());
WidgetRowWithTypeSafeId widget = WidgetRowWithTypeSafeId.newBuilder().widgetId(widgetId).manufacturerId(manufacturerId).name("Safety Gadget").build();
database.insert(widget);
WidgetRowWithTypeSafeId result = database.from(WidgetRowWithTypeSafeId.class).where(WidgetRowWithTypeSafeId::widgetId).isEqualTo(widgetId).or(WidgetRowWithTypeSafeId::manufacturerId).isEqualTo(literal(manufacturerId)).single();
MatcherAssert.assertThat(result.name(), is("Safety Gadget"));
}
use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.
the class SiestaExample method insertSomeGizmosAndReadThemBack.
@Test
void insertSomeGizmosAndReadThemBack() {
Database database = Database.newBuilder().defaultSchema("SIESTA").defaultSqlExecutor(JdbcSqlExecutor.of(dataSource)).build();
ZonedDateTime start = ZonedDateTime.now(ZoneOffset.UTC);
database.insert(new Manufacturer(2004L, "Gizmos Inc"), new Manufacturer(2005L, "Acme Inc"));
database.insert(new Widget(1003L, "Gizmo", 2004L, Optional.empty()), new Widget(1004L, "Gizmo", 2005L, Optional.of("Acme Gizmo")), new Widget(1005L, "Gizmo", 2005L, Optional.of("Acme Gizmo Mk II")));
ZonedDateTime end = ZonedDateTime.now(ZoneOffset.UTC);
List<Tuple3<String, String, ZonedDateTime>> makersOfGizmos = database.from(Widget.class, "w").join(Manufacturer.class, "m").on(Manufacturer::manufacturerId).isEqualTo(Widget::manufacturerId).select(Manufacturer::name).comma(Widget::description).comma(Manufacturer::insertionTs).where(Widget::name).isEqualTo("Gizmo").orderBy(Widget::widgetId).list();
assertThat(makersOfGizmos, hasSize(3));
assertThat(makersOfGizmos.get(0).item1(), is("Gizmos Inc"));
assertThat(makersOfGizmos.get(1).item1(), is("Acme Inc"));
assertThat(makersOfGizmos.get(2).item1(), is("Acme Inc"));
assertThat(makersOfGizmos.get(0).item3().isBefore(start), is(false));
assertThat(makersOfGizmos.get(0).item3().isAfter(end), is(false));
}
use of com.cadenzauk.siesta.Database 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"));
}
Aggregations