Search in sources :

Example 6 with ManufacturerRow

use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.

the class DatabaseIntegrationTest method selectFromDatabaseTwoTables.

@Test
void selectFromDatabaseTwoTables() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturerId = newId();
    ManufacturerRow aManufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Makers")).build();
    WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Dodacky").description(Optional.of("Thingamibob")).build();
    database.insert(aManufacturer);
    database.insert(aWidget);
    Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
    Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
    Optional<WidgetRow> theSame = database.from(w).join(m).on(m, ManufacturerRow::manufacturerId).isEqualTo(w, WidgetRow::manufacturerId).where(w, WidgetRow::name).isEqualTo("Dodacky").and(w, WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional().map(Tuple2::item1);
    assertThat(theSame, is(Optional.of(aWidget)));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) Tuple2(com.cadenzauk.core.tuple.Tuple2) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with ManufacturerRow

use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.

the class DatabaseIntegrationTestH2 method selectSubquery.

@Test
void selectSubquery() {
    Database database = testDatabase(dataSource);
    long manufacturerId = newId();
    ManufacturerRow manufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Doc Brown")).build();
    WidgetRow[] widgets = IntStream.range(1, 4).mapToObj(i -> WidgetRow.newBuilder().widgetId(newId()).name("Flux capacitor " + i).manufacturerId(manufacturerId).build()).toArray(WidgetRow[]::new);
    database.insert(manufacturer);
    database.insert(widgets);
    Tuple2<Long, Integer> result1 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count()).where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId)).where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
    Tuple2<Long, Integer> result2 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count()).where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId), "widget_count").where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
    Tuple2<Long, Integer> result3 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count(), "wcount").where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId), "wcount").where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
    assertThat(result1.item1(), is(manufacturerId));
    assertThat(result1.item2(), is(3));
    assertThat(result2.item1(), is(manufacturerId));
    assertThat(result2.item2(), is(3));
    assertThat(result3.item1(), is(manufacturerId));
    assertThat(result3.item2(), is(3));
}
Also used : Tuple2(com.cadenzauk.core.tuple.Tuple2) IntStream(java.util.stream.IntStream) RandomValues(com.cadenzauk.core.RandomValues) Tuple5(com.cadenzauk.core.tuple.Tuple5) Aggregates.count(com.cadenzauk.siesta.grammar.expression.Aggregates.count) DatabaseIntegrationTest(com.cadenzauk.siesta.DatabaseIntegrationTest) PartRow(com.cadenzauk.siesta.model.PartRow) TypedExpression.column(com.cadenzauk.siesta.grammar.expression.TypedExpression.column) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Optional(java.util.Optional) MoneyAmount(com.cadenzauk.siesta.model.MoneyAmount) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Database(com.cadenzauk.siesta.Database) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) DatabaseIntegrationTest(com.cadenzauk.siesta.DatabaseIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 8 with ManufacturerRow

use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.

the class SelectExpressionTest method between.

@Test
void between() {
    Database database = testDatabase(new AnsiDialect());
    Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
    String sql = database.from(m).where(ManufacturerRow::name).isBetween("Fred").and("Barney").or(ManufacturerRow::checked).isNull().and(ManufacturerRow::manufacturerId).isGreaterThan(3L).sql();
    assertThat(sql, is("select m.MANUFACTURER_ID as m_MANUFACTURER_ID, m.NAME as m_NAME, m.CHECKED as m_CHECKED " + "from SIESTA.MANUFACTURER m " + "where m.NAME between ? and ? " + "or m.CHECKED is null and m.MANUFACTURER_ID > ?"));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test)

Example 9 with ManufacturerRow

use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.

the class SelectExpressionTest method andAndOrsInWhere.

@Test
void andAndOrsInWhere() {
    Database database = testDatabase(new AnsiDialect());
    Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
    String sql = database.from(m).where(ManufacturerRow::name).isEqualTo("Fred").or(ManufacturerRow::checked).isNull().and(ManufacturerRow::manufacturerId).isGreaterThan(3L).sql();
    assertThat(sql, is("select m.MANUFACTURER_ID as m_MANUFACTURER_ID, m.NAME as m_NAME, m.CHECKED as m_CHECKED " + "from SIESTA.MANUFACTURER m " + "where m.NAME = ? or m.CHECKED is null and m.MANUFACTURER_ID > ?"));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test)

Example 10 with ManufacturerRow

use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.

the class SelectExpressionTest method andExpression.

@Test
void andExpression() {
    Database database = testDatabase(new AnsiDialect());
    Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
    String sql = database.from(m).where(ManufacturerRow::checked).isLessThan(LocalDate.now()).and(ManufacturerRow::name).isEqualTo("Fred").and(column(ManufacturerRow::name).isEqualTo("Bob").or(ManufacturerRow::checked).isNull()).and(ManufacturerRow::checked).isGreaterThan(LocalDate.now()).sql();
    assertThat(sql, is("select m.MANUFACTURER_ID as m_MANUFACTURER_ID, m.NAME as m_NAME, m.CHECKED as m_CHECKED " + "from SIESTA.MANUFACTURER m " + "where m.CHECKED < cast(? as date) and m.NAME = ? and (m.NAME = ? or m.CHECKED is null) and m.CHECKED > cast(? as date)"));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test)

Aggregations

ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)15 Test (org.junit.jupiter.api.Test)15 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)13 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)8 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Tuple2 (com.cadenzauk.core.tuple.Tuple2)5 MockitoTest (com.cadenzauk.core.MockitoTest)2 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)2 WidgetViewRow (com.cadenzauk.siesta.model.WidgetViewRow)2 RandomValues (com.cadenzauk.core.RandomValues)1 Tuple5 (com.cadenzauk.core.tuple.Tuple5)1 Database (com.cadenzauk.siesta.Database)1 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)1 InvalidJoinException (com.cadenzauk.siesta.grammar.InvalidJoinException)1 Aggregates.count (com.cadenzauk.siesta.grammar.expression.Aggregates.count)1 TypedExpression.column (com.cadenzauk.siesta.grammar.expression.TypedExpression.column)1 MoneyAmount (com.cadenzauk.siesta.model.MoneyAmount)1 PartRow (com.cadenzauk.siesta.model.PartRow)1 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)1