Skip to content

Commit

Permalink
[#1] Added an example of mapping a MULTISET into a nested Map
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed May 9, 2022
1 parent 05ab45f commit 1d6353a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/test/java/org/jooq/demo/java/Demo01Querying.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
import org.jooq.demo.java.db.tables.records.ActorRecord;
import org.jooq.demo.java.db.Tables;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.junit.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.jooq.Records.intoMap;
import static org.jooq.Records.mapping;
import static org.jooq.demo.java.db.Tables.*;
import static org.jooq.impl.DSL.*;
import static org.jooq.impl.SQLDataType.DATE;
import static org.jooq.impl.SQLDataType.LOCALDATE;

public class Demo01Querying extends AbstractDemo {

Expand Down Expand Up @@ -310,4 +317,31 @@ record Film(String title, List<Actor> actors, List<Category> categories) {}

// Try modifying the records and see what needs to be done to get the query to compile again
}

@Test
public void nestingToManyRelationshipsAsMaps() {
title("Arbitrary nested data structures are possible");

record Film(String title, Map<LocalDate, BigDecimal> revenue) {}
List<Film> result =
ctx.select(
FILM.TITLE,
multiset(
select(PAYMENT.PAYMENT_DATE.cast(LOCALDATE), sum(PAYMENT.AMOUNT))
.from(PAYMENT)
.groupBy(PAYMENT.PAYMENT_DATE.cast(LOCALDATE))
.orderBy(PAYMENT.PAYMENT_DATE.cast(LOCALDATE))
).convertFrom(r -> r.collect(intoMap()))
)
.from(FILM)
.orderBy(FILM.TITLE)
.fetch(mapping(Film::new));

for (Film film : result) {
println("");
println("Film %s with revenue: ".formatted(film.title));

film.revenue.forEach((d, r) -> println(" %s: %s".formatted(d, r)));
}
}
}
44 changes: 39 additions & 5 deletions src/test/kotlin/org/jooq/demo/kotlin/Demo01Querying.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package org.jooq.demo.kotlin

import org.jooq.*
import org.jooq.Records.intoMap
import org.jooq.Records.mapping
import org.jooq.demo.AbstractDemo
import org.jooq.demo.java.db.Tables
import org.jooq.demo.kotlin.db.tables.references.*
import org.jooq.impl.DSL
import org.jooq.impl.DSL.*
import org.jooq.impl.SQLDataType
import org.jooq.impl.SQLDataType.LOCALDATE
import org.junit.Test
import java.math.BigDecimal
import java.time.LocalDate
import java.util.function.BiConsumer

class Demo01Querying : AbstractDemo() {

Expand Down Expand Up @@ -215,7 +222,7 @@ class Demo01Querying : AbstractDemo() {
.from(CUSTOMER)
.orderBy(1, 2)
.limit(5)
.fetch(Records.mapping(::Customer))
.fetch(mapping(::Customer))
r.forEach { t: Customer? -> println(t) }
}

Expand Down Expand Up @@ -288,22 +295,49 @@ class Demo01Querying : AbstractDemo() {
).mapping(::Name))
.from(FILM_ACTOR)
.where(FILM_ACTOR.FILM_ID.eq(FILM.FILM_ID))
).convertFrom { r -> r.map(Records.mapping(::Actor)) },
).convertFrom { r -> r.map(mapping(::Actor)) },
multiset(
select(FILM_CATEGORY.category().NAME)
.from(FILM_CATEGORY)
.where(FILM_CATEGORY.FILM_ID.eq(FILM.FILM_ID))
).convertFrom { r -> r.map(Records.mapping(::Category)) }
).convertFrom { r -> r.map(mapping(::Category)) }
)
.from(FILM)
.orderBy(FILM.TITLE)
.limit(5)
.fetch(Records.mapping(::Film))
.fetch(mapping(::Film))

for (film in result) {
println("Film ${film.title} with categories ${film.categories} and actors ${film.actors}")
}

// Try modifying the records and see what needs to be done to get the query to compile again
}

@Test
fun nestingToManyRelationshipsAsMaps() {
title("Arbitrary nested data structures are possible")
data class Film(val title: String?, val revenue: Map<LocalDate, BigDecimal>)

val result: List<Film> = ctx
.select(
FILM.TITLE,
multiset(
select(PAYMENT.PAYMENT_DATE.cast(LOCALDATE), sum(PAYMENT.AMOUNT))
.from(PAYMENT)
.groupBy(PAYMENT.PAYMENT_DATE.cast(LOCALDATE))
.orderBy(PAYMENT.PAYMENT_DATE.cast(LOCALDATE))
).convertFrom { r -> r.collect(intoMap()) }
)
.from(FILM)
.orderBy(FILM.TITLE)
.fetch(mapping(::Film))

for (film in result) {
println("")
println("Film ${film.title} with revenue: ")
film.revenue.forEach { (d, r) -> println(" $d: $r") }
}
}

}

0 comments on commit 1d6353a

Please sign in to comment.