forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcVsJooq.java
More file actions
executable file
·37 lines (31 loc) · 1.22 KB
/
JdbcVsJooq.java
File metadata and controls
executable file
·37 lines (31 loc) · 1.22 KB
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
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
//DEPS org.jooq:jooq:3.20.11
import org.jooq.*;
import org.jooq.impl.DSL;
import javax.sql.DataSource;
import java.util.*;
/// Proof: jdbc-vs-jooq
/// Source: content/enterprise/jdbc-vs-jooq.yaml
class User {
Long id; String name; String email;
}
// Simulating the generated jOOQ table fields (normally produced by jOOQ codegen)
class USERS {
static final Field<String> DEPARTMENT = DSL.field(DSL.name("department"), String.class);
static final Field<Integer> SALARY = DSL.field(DSL.name("salary"), Integer.class);
static final Field<Long> ID = DSL.field(DSL.name("id"), Long.class);
static final Field<String> NAME = DSL.field(DSL.name("name"), String.class);
static final Field<String> EMAIL = DSL.field(DSL.name("email"), String.class);
static final Table<?> TABLE = DSL.table(DSL.name("users"));
}
List<User> findByDept(DataSource ds, String department, int minSalary) {
DSLContext dsl = DSL.using(ds, SQLDialect.POSTGRES);
return dsl
.select(USERS.ID, USERS.NAME, USERS.EMAIL)
.from(USERS.TABLE)
.where(USERS.DEPARTMENT.eq(department)
.and(USERS.SALARY.gt(minSalary)))
.fetchInto(User.class);
}
void main() {}