Search in sources :

Example 11 with Database

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"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Embedded(javax.persistence.Embedded) ZonedDateTime(java.time.ZonedDateTime) Collectors.joining(java.util.stream.Collectors.joining) Test(org.junit.jupiter.api.Test) AttributeOverride(javax.persistence.AttributeOverride) BigDecimal(java.math.BigDecimal) Alias(com.cadenzauk.siesta.Alias) EmbeddedId(javax.persistence.EmbeddedId) Optional(java.util.Optional) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Database(com.cadenzauk.siesta.Database) Id(javax.persistence.Id) Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test)

Example 12 with Database

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"));
}
Also used : Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test)

Example 13 with Database

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"));
}
Also used : Tuple2(com.cadenzauk.core.tuple.Tuple2) Tuple3(com.cadenzauk.core.tuple.Tuple3) WidgetRowWithTypeSafeId(com.cadenzauk.siesta.model.WidgetRowWithTypeSafeId) ManufacturerId(com.cadenzauk.siesta.model.ManufacturerId) ZonedDateTime(java.time.ZonedDateTime) TypedExpression.literal(com.cadenzauk.siesta.grammar.expression.TypedExpression.literal) DateFunctions.currentDate(com.cadenzauk.siesta.grammar.expression.DateFunctions.currentDate) CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) Test(org.junit.jupiter.api.Test) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) IntegrationTest(com.cadenzauk.siesta.IntegrationTest) LocalDate(java.time.LocalDate) Optional(java.util.Optional) Matchers.hasSize(org.hamcrest.Matchers.hasSize) WidgetId(com.cadenzauk.siesta.model.WidgetId) Matchers.is(org.hamcrest.Matchers.is) JdbcSqlExecutor(com.cadenzauk.siesta.jdbc.JdbcSqlExecutor) ZoneOffset(java.time.ZoneOffset) DateFunctions.currentTimestamp(com.cadenzauk.siesta.grammar.expression.DateFunctions.currentTimestamp) DbTypeId(com.cadenzauk.siesta.type.DbTypeId) Aggregates.countDistinct(com.cadenzauk.siesta.grammar.expression.Aggregates.countDistinct) Database(com.cadenzauk.siesta.Database) ManufacturerId(com.cadenzauk.siesta.model.ManufacturerId) Database(com.cadenzauk.siesta.Database) WidgetId(com.cadenzauk.siesta.model.WidgetId) WidgetRowWithTypeSafeId(com.cadenzauk.siesta.model.WidgetRowWithTypeSafeId) Test(org.junit.jupiter.api.Test) IntegrationTest(com.cadenzauk.siesta.IntegrationTest)

Example 14 with Database

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));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Tuple3(com.cadenzauk.core.tuple.Tuple3) Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test) IntegrationTest(com.cadenzauk.siesta.IntegrationTest)

Example 15 with Database

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

Aggregations

Database (com.cadenzauk.siesta.Database)57 Test (org.junit.jupiter.api.Test)41 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)21 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 ZonedDateTime (java.time.ZonedDateTime)17 Optional (java.util.Optional)17 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)16 Alias (com.cadenzauk.siesta.Alias)15 BigDecimal (java.math.BigDecimal)14 Collectors.joining (java.util.stream.Collectors.joining)14 AttributeOverride (javax.persistence.AttributeOverride)14 Embedded (javax.persistence.Embedded)14 EmbeddedId (javax.persistence.EmbeddedId)14 Id (javax.persistence.Id)14 CoreMatchers.is (org.hamcrest.CoreMatchers.is)14 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)13 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)7 MockitoTest (com.cadenzauk.core.MockitoTest)5