-
Notifications
You must be signed in to change notification settings - Fork 3
/
Example.java
46 lines (39 loc) · 1.43 KB
/
Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//DEPS org.jooq:jooq:${jooq.version:RELEASE}
//DEPS com.h2database:h2:${h2.version:RELEASE}
//SOURCES com/example/db/**
import static org.jooq.impl.DSL.*;
import static com.example.db.Tables.*;
import org.jooq.*;
import java.io.*;
import java.io.File;
import java.util.*;
import com.example.db.tables.records.*;
public class Example {
public static void main(String[] args) throws Exception {
try (CloseableDSLContext ctx = using("jdbc:h2:mem:jooq-jbang-example")) {
execute(ctx, "db.sql");
execute(ctx, "data.sql");
var query =
ctx.select(
BOOK_TO_BOOK_STORE.book().author().FIRST_NAME,
BOOK_TO_BOOK_STORE.book().author().LAST_NAME,
BOOK_TO_BOOK_STORE.book().TITLE,
countDistinct(BOOK_TO_BOOK_STORE.BOOK_ID)
.over(partitionBy(BOOK_TO_BOOK_STORE.book().AUTHOR_ID)).as("books written by author"),
countDistinct(BOOK_TO_BOOK_STORE.NAME)
.over(partitionBy(BOOK_TO_BOOK_STORE.book().AUTHOR_ID)).as("bookstores listing author"),
BOOK_TO_BOOK_STORE.book().language().CD,
BOOK_TO_BOOK_STORE.bookStore().NAME)
.from(BOOK_TO_BOOK_STORE)
.orderBy(1, 2, 3);
System.out.println("Query:");
System.out.println(query);
System.out.println();
System.out.println("Result:");
System.out.println(query.fetch());
}
}
private static void execute(DSLContext ctx, String file) throws Exception {
ctx.parser().parse(Source.of(new File(file)).readString()).executeBatch();
}
}