Skip to content

Commit 538e160

Browse files
committed
Java/jOOQ: Use code generation based on SQL DDL
This time, the code within the `src/generated` folder has truly been generated using jOOQ 3.17.7. - The code generator used for that is `DDLDatabase` [1]. - The relevant upstream documentation is [2]. [1] org.jooq.meta.extensions.ddl.DDLDatabase [2] https://www.jooq.org/doc/latest/manual/code-generation/codegen-ddl/
1 parent 6c2b9a5 commit 538e160

File tree

15 files changed

+478
-122
lines changed

15 files changed

+478
-122
lines changed

by-language/java-jooq/README.rst

+44-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Java jOOQ demo application for CrateDB using PostgreSQL JDBC
55
############################################################
66

7+
78
*****
89
About
910
*****
@@ -12,9 +13,8 @@ A demo application using `CrateDB`_ with `jOOQ`_ and the `PostgreSQL
1213
JDBC driver`_.
1314

1415
It is intended as a basic example to demonstrate what currently works, and as a
15-
testing rig for eventually growing a full-fledged CrateDB dialect, or at least
16-
making the code generator work. Contributions are welcome.
17-
16+
testing rig for eventually growing a full-fledged CrateDB dialect.
17+
Contributions are welcome.
1818

1919
Introduction
2020
============
@@ -35,19 +35,48 @@ In some kind, jOOQ is similar to `LINQ`_, `but better <Insight into Language
3535
Integrated Querying_>`_.
3636

3737

38+
*******
3839
Details
39-
=======
40+
*******
41+
42+
Overview
43+
========
4044

41-
The code example will demonstrate a few of the `different use cases for jOOQ`_.
42-
That is, `Dynamic SQL`_, the `jOOQ DSL API`_, and how to use `jOOQ as a SQL
43-
builder without code generation`_.
45+
The code examples will demonstrate a few of the `different use cases for jOOQ`_.
46+
That is, how to use the `jOOQ DSL API`_ based on code generated with `jOOQ's
47+
code generator`_ to take your database schema and reverse-engineer it into a
48+
set of Java classes, as well how to use the `Dynamic SQL API`_ by using `jOOQ
49+
as a SQL builder without code generation`_.
4450

51+
Schema management
52+
=================
53+
54+
In many cases, the schema is defined in the form of SQL scripts, which can be
55+
used with a `database schema migration`_ framework like `Flyway`_,
56+
`Liquibase`_, `Bytebase`_, etc.
57+
58+
The `DDLDatabase - Code generation from SQL files`_ feature can be used to
59+
effectively reflect the database schema from SQL DDL files, without needing
60+
a database instance at all. The code provided within the ``src/generated``
61+
directory has been generated like this.
4562

4663
Caveats
4764
=======
4865

66+
- `jOOQ's code generator`_ currently does not work with directly connecting to
67+
a real CrateDB database instance and reflecting the schema from there.
68+
Because SQL DDL statements are usually maintained in form of multiple
69+
incremental migration scripts anyway, this is considered to be not of a too
70+
big concern, see above. With corresponding improvements to CrateDB, this
71+
can be made work in the future, see `issue #10 - with reflection from the
72+
database`_.
73+
4974
- Most of the jOOQ examples use uppercase letters for the database, table, and
50-
field names. CrateDB currently only handles lowercase letters.
75+
field names. Within this setup, we have only been able to make it work using
76+
lowercase letters.
77+
78+
- We have not been able to make multiple SQL DDL statements work within a
79+
single SQL bootstrap file at ``src/main/resources/bootstrap.sql``.
5180

5281

5382
*****
@@ -73,15 +102,21 @@ Usage
73102
./gradlew generateJooq
74103

75104

105+
.. _Bytebase: https://github.com/bytebase/bytebase
76106
.. _CrateDB: https://github.com/crate/crate
107+
.. _database schema migration: https://en.wikipedia.org/wiki/Schema_migration
108+
.. _DDLDatabase - Code generation from SQL files: https://www.jooq.org/doc/latest/manual/code-generation/codegen-ddl/
77109
.. _Different use cases for jOOQ: https://www.jooq.org/doc/latest/manual/getting-started/use-cases/
78-
.. _Dynamic SQL: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql/
110+
.. _Dynamic SQL API: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql/
111+
.. _Flyway: https://github.com/flyway/flyway
79112
.. _Gradle: https://gradle.org/
80113
.. _Insight into Language Integrated Querying: https://blog.jooq.org/jooq-tuesdays-ming-yee-iu-gives-insight-into-language-integrated-querying/
114+
.. _issue #10 - with reflection from the database: https://github.com/crate/cratedb-examples/pull/10
81115
.. _Java 17: https://adoptium.net/temurin/releases/?version=17
82116
.. _jOOQ: https://github.com/jOOQ/jOOQ
83117
.. _jOOQ as a SQL builder without code generation: https://www.jooq.org/doc/latest/manual/getting-started/use-cases/jooq-as-a-sql-builder-without-codegeneration/
84118
.. _jOOQ's code generator: https://www.jooq.org/doc/latest/manual/code-generation/
85119
.. _jOOQ DSL API: https://www.jooq.org/doc/latest/manual/sql-building/dsl-api/
86120
.. _LINQ: https://en.wikipedia.org/wiki/Language_Integrated_Query
121+
.. _Liquibase: https://github.com/liquibase/liquibase
87122
.. _PostgreSQL JDBC Driver: https://github.com/pgjdbc/pgjdbc

by-language/java-jooq/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ repositories {
2222

2323
dependencies {
2424
implementation 'org.jooq:jooq:3.17.7'
25+
implementation 'org.jooq:jooq-meta:3.17.7'
2526
implementation 'org.postgresql:postgresql:42.5.1'
2627
implementation 'org.slf4j:slf4j-api:2.0.6'
2728
implementation 'org.slf4j:slf4j-simple:2.0.6'

by-language/java-jooq/jooq.gradle

+50-20
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ apply plugin: nu.studer.gradle.jooq.JooqPlugin
3636

3737
dependencies {
3838
jooqGenerator 'org.postgresql:postgresql:42.5.1'
39+
jooqGenerator 'org.jooq:jooq-meta-extensions:3.17.7'
3940
}
4041

4142
jooq {
4243

43-
// Defaults (can be omitted).
44-
// version = '3.17.6'
45-
// edition = nu.studer.gradle.jooq.JooqEdition.OSS
46-
4744
configurations {
4845
// Name of the jOOQ configuration.
4946
main {
@@ -53,35 +50,68 @@ jooq {
5350

5451
generationTool {
5552
logging = org.jooq.meta.jaxb.Logging.WARN
56-
jdbc {
57-
driver = 'org.postgresql.Driver'
58-
url = 'jdbc:postgresql://localhost:5432/testdrive'
59-
user = 'crate'
60-
password = ''
61-
properties {
62-
property {
63-
key = 'PAGE_SIZE'
64-
value = 2048
65-
}
66-
}
67-
}
6853
generator {
6954
name = 'org.jooq.codegen.DefaultGenerator'
55+
strategy.name = 'org.jooq.codegen.DefaultGeneratorStrategy'
7056
database {
71-
name = 'org.jooq.meta.postgres.PostgresDatabase'
72-
inputSchema = 'testdrive'
57+
name = 'org.jooq.meta.extensions.ddl.DDLDatabase'
58+
properties {
59+
60+
// Specify the location of your SQL script.
61+
// You may use ant-style file matching, e.g. /path/**/to/*.sql
62+
//
63+
// Where:
64+
// - ** matches any directory subtree
65+
// - * matches any number of characters in a directory / file name
66+
// - ? matches a single character in a directory / file name
67+
property {
68+
key = 'scripts'
69+
value = 'src/main/resources/bootstrap.sql'
70+
}
71+
72+
// The sort order of the scripts within a directory, where:
73+
//
74+
// - semantic: sorts versions, e.g. v-3.10.0 is after v-3.9.0 (default)
75+
// - alphanumeric: sorts strings, e.g. v-3.10.0 is before v-3.9.0
76+
// - flyway: sorts files the same way as flyway does
77+
// - none: doesn't sort directory contents after fetching them from the directory
78+
property {
79+
key = 'sort'
80+
value = 'semantic'
81+
}
82+
83+
// The default schema for unqualified objects:
84+
//
85+
// - public: all unqualified objects are located in the PUBLIC (upper case) schema
86+
// - none: all unqualified objects are located in the default schema (default)
87+
//
88+
// This configuration can be overridden with the schema mapping feature
89+
property {
90+
key = 'unqualifiedSchema'
91+
value = 'none'
92+
}
93+
94+
// The default name case for unquoted objects:
95+
//
96+
// - as_is: unquoted object names are kept unquoted
97+
// - upper: unquoted object names are turned into upper case (most databases)
98+
// - lower: unquoted object names are turned into lower case (e.g. PostgreSQL)
99+
property {
100+
key = 'defaultNameCase'
101+
value = 'lower'
102+
}
103+
}
73104
}
74105
generate {
75106
deprecated = false
76-
records = false
107+
records = true
77108
immutablePojos = false
78109
fluentSetters = false
79110
}
80111
target {
81112
packageName = 'io.crate.demo.jooq.model'
82113
directory = 'src/generated/java'
83114
}
84-
strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy"
85115
}
86116
}
87117
}

by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultCatalog.java

+4-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DemoDatabase.java renamed to by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultSchema.java

+14-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Keys.java

+8-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Tables.java

+8-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)