Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java/jOOQ: Add code generation add-on for Gradle, with reflection from the database #10

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions by-language/java-jooq/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.gradle/
.idea
*.iws
*.iml
*.ipr
build/
out/
target/
1 change: 1 addition & 0 deletions by-language/java-jooq/.java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17
87 changes: 87 additions & 0 deletions by-language/java-jooq/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
.. highlight:: sh

############################################################
Java jOOQ demo application for CrateDB using PostgreSQL JDBC
############################################################

*****
About
*****

A demo application using `CrateDB`_ with `jOOQ`_ and the `PostgreSQL
JDBC driver`_.

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


Introduction
============

The idea of jOOQ is to generate typesafe code based on the SQL schema.
Then, accessing a database table using the jOOQ DSL API looks like this:

.. code-block:: java

// Fetch records, with filtering and sorting.
Result<Record> result = db.select()
.from(AUTHOR)
.where(AUTHOR.NAME.like("Ja%"))
.orderBy(AUTHOR.NAME)
.fetch();

In some kind, jOOQ is similar to `LINQ`_, `but better <Insight into Language
Integrated Querying_>`_.


Details
=======

The code example will demonstrate a few of the `different use cases for jOOQ`_.
That is, `Dynamic SQL`_, the `jOOQ DSL API`_, and how to use `jOOQ as a SQL
builder without code generation`_.


Caveats
=======

- Most of the jOOQ examples use uppercase letters for the database, table, and
field names. CrateDB currently only handles lowercase letters.


*****
Usage
*****

1. Make sure `Java 17`_ is installed.
2. Run CrateDB::

docker run -it --rm --publish=4200:4200 --publish=5432:5432 \
crate:latest -Cdiscovery.type=single-node

3. Invoke demo application::

./gradlew run

3. Invoke software tests::

./gradlew test

4. Generate the jOOQ sources from the main jOOQ configuration, see ``jooq.gradle``::

./gradlew generateJooq


.. _CrateDB: https://github.com/crate/crate
.. _Different use cases for jOOQ: https://www.jooq.org/doc/latest/manual/getting-started/use-cases/
.. _Dynamic SQL: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql/
.. _Gradle: https://gradle.org/
.. _Insight into Language Integrated Querying: https://blog.jooq.org/jooq-tuesdays-ming-yee-iu-gives-insight-into-language-integrated-querying/
.. _Java 17: https://adoptium.net/temurin/releases/?version=17
.. _jOOQ: https://github.com/jOOQ/jOOQ
.. _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/
.. _jOOQ's code generator: https://www.jooq.org/doc/latest/manual/code-generation/
.. _jOOQ DSL API: https://www.jooq.org/doc/latest/manual/sql-building/dsl-api/
.. _LINQ: https://en.wikipedia.org/wiki/Language_Integrated_Query
.. _PostgreSQL JDBC Driver: https://github.com/pgjdbc/pgjdbc
64 changes: 64 additions & 0 deletions by-language/java-jooq/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* A demo application using CrateDB with jOOQ and the PostgreSQL JDBC driver.
*/

buildscript {
repositories {
mavenCentral()
}
}

plugins {
id 'application'
id 'com.adarshr.test-logger' version '3.2.0'
id 'idea'
id 'java'
}

repositories {
mavenCentral()
mavenLocal()
}

dependencies {
implementation 'org.jooq:jooq:3.17.7'
implementation 'org.postgresql:postgresql:42.5.1'
implementation 'org.slf4j:slf4j-api:2.0.6'
implementation 'org.slf4j:slf4j-simple:2.0.6'
testImplementation 'junit:junit:4.13.2'
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

jar {
archiveBaseName = 'cratedb-demo-java-jooq'
archiveVersion = '0.0.1-SNAPSHOT'
}

application {
mainClass = 'io.crate.demo.jooq.Application'
}

sourceSets {
main {
java.srcDirs += [
"src/generated/java",
"src/main/java",
]
}
}

test {
dependsOn 'cleanTest'
}

// Activate jOOQ code generation add-on.
apply from: 'jooq.gradle'

idea.module.inheritOutputDirs = true
processResources.destinationDir = compileJava.destinationDir
compileJava.dependsOn processResources
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading