[jOOQ/jOOQ#12206] Add an R2DBC example project to our examples

This commit is contained in:
Lukas Eder 2021-07-15 16:25:13 +02:00
parent 934c0bed14
commit a253577055
16 changed files with 1071 additions and 0 deletions

View File

@ -0,0 +1,2 @@
/target
/target

View File

@ -0,0 +1,19 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Other licenses:
-----------------------------------------------------------------------------
Commercial licenses for this work are available. These replace the above
ASL 2.0 and offer limited warranties, support, maintenance, and commercial
database integrations.
For more information, please visit: http://www.jooq.org/licenses

View File

@ -0,0 +1,32 @@
Thanks for downloading jOOQ.
Please visit http://www.jooq.org for more information.
### Description
This example uses
1. [Flyway](https://flywaydb.org/) for database installation
2. [Spark Java](http://sparkjava.com/) as a web server
3. [chart.js](http://www.chartjs.org/) as an HTML5 charting library
4. [jOOQ](http://www.jooq.org) for reporting
The whole thing then looks like this:
![animation](http://i.imgur.com/W8u29Zn.gif)
### Installation
To install and run this example, simply check it out and follow these steps
1. Create a "Sakila" database on your PostgreSQL instance
2. Edit src/main/resources/config.properties and configure your database instance
2. Run the following commands
```
$ pwd
/path/to/checkout/dir
$ cd jOOQ-examples/jOOQ-spark-chart-example
...
$ mvn clean install
...
```

View File

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jooq</groupId>
<artifactId>jooq-examples</artifactId>
<version>3.16.0-SNAPSHOT</version>
</parent>
<artifactId>jooq-r2dbc-example</artifactId>
<properties>
<log4j.version>2.11.2</log4j.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>11</release>
<!-- IntelliJ needs these https://youtrack.jetbrains.com/issue/IDEA-195472 -->
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>generate-postgres</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generator>
<database>
<name>org.jooq.meta.extensions.ddl.DDLDatabase</name>
<properties>
<property>
<key>scripts</key>
<value>${basedir}/src/main/resources/db.sql</value>
</property>
</properties>
<inputSchema>R2DBC_EXAMPLE</inputSchema>
</database>
<target>
<packageName>org.jooq.example.r2dbc.db</packageName>
<directory>src/main/java</directory>
</target>
</generator>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta-extensions</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>0.8.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,45 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db;
import java.util.Arrays;
import java.util.List;
import org.jooq.Schema;
import org.jooq.impl.CatalogImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class DefaultCatalog extends CatalogImpl {
private static final long serialVersionUID = 1L;
/**
* The reference instance of <code>DEFAULT_CATALOG</code>
*/
public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog();
/**
* The schema <code>R2DBC_EXAMPLE</code>.
*/
public final R2dbcExample R2DBC_EXAMPLE = R2dbcExample.R2DBC_EXAMPLE;
/**
* No further instances allowed
*/
private DefaultCatalog() {
super("");
}
@Override
public final List<Schema> getSchemas() {
return Arrays.asList(
R2dbcExample.R2DBC_EXAMPLE
);
}
}

View File

@ -0,0 +1,37 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db;
import org.jooq.ForeignKey;
import org.jooq.TableField;
import org.jooq.UniqueKey;
import org.jooq.example.r2dbc.db.tables.Author;
import org.jooq.example.r2dbc.db.tables.Book;
import org.jooq.example.r2dbc.db.tables.records.AuthorRecord;
import org.jooq.example.r2dbc.db.tables.records.BookRecord;
import org.jooq.impl.DSL;
import org.jooq.impl.Internal;
/**
* A class modelling foreign key relationships and constraints of tables in
* R2DBC_EXAMPLE.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Keys {
// -------------------------------------------------------------------------
// UNIQUE and PRIMARY KEY definitions
// -------------------------------------------------------------------------
public static final UniqueKey<AuthorRecord> PK_AUTHOR = Internal.createUniqueKey(Author.AUTHOR, DSL.name("PK_AUTHOR"), new TableField[] { Author.AUTHOR.ID }, true);
public static final UniqueKey<BookRecord> PK_BOOK = Internal.createUniqueKey(Book.BOOK, DSL.name("PK_BOOK"), new TableField[] { Book.BOOK.ID }, true);
// -------------------------------------------------------------------------
// FOREIGN KEY definitions
// -------------------------------------------------------------------------
public static final ForeignKey<BookRecord, AuthorRecord> FK_BOOK_AUTHOR = Internal.createForeignKey(Book.BOOK, DSL.name("FK_BOOK_AUTHOR"), new TableField[] { Book.BOOK.ID }, Keys.PK_AUTHOR, new TableField[] { Author.AUTHOR.ID }, true);
}

View File

@ -0,0 +1,60 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db;
import java.util.Arrays;
import java.util.List;
import org.jooq.Catalog;
import org.jooq.Table;
import org.jooq.example.r2dbc.db.tables.Author;
import org.jooq.example.r2dbc.db.tables.Book;
import org.jooq.impl.SchemaImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class R2dbcExample extends SchemaImpl {
private static final long serialVersionUID = 1L;
/**
* The reference instance of <code>R2DBC_EXAMPLE</code>
*/
public static final R2dbcExample R2DBC_EXAMPLE = new R2dbcExample();
/**
* The table <code>R2DBC_EXAMPLE.AUTHOR</code>.
*/
public final Author AUTHOR = Author.AUTHOR;
/**
* The table <code>R2DBC_EXAMPLE.BOOK</code>.
*/
public final Book BOOK = Book.BOOK;
/**
* No further instances allowed
*/
private R2dbcExample() {
super("R2DBC_EXAMPLE", null);
}
@Override
public Catalog getCatalog() {
return DefaultCatalog.DEFAULT_CATALOG;
}
@Override
public final List<Table<?>> getTables() {
return Arrays.asList(
Author.AUTHOR,
Book.BOOK
);
}
}

View File

@ -0,0 +1,26 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db;
import org.jooq.example.r2dbc.db.tables.Author;
import org.jooq.example.r2dbc.db.tables.Book;
/**
* Convenience access to all tables in R2DBC_EXAMPLE.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Tables {
/**
* The table <code>R2DBC_EXAMPLE.AUTHOR</code>.
*/
public static final Author AUTHOR = Author.AUTHOR;
/**
* The table <code>R2DBC_EXAMPLE.BOOK</code>.
*/
public static final Book BOOK = Book.BOOK;
}

View File

@ -0,0 +1,144 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db.tables;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Identity;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Row3;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.example.r2dbc.db.Keys;
import org.jooq.example.r2dbc.db.R2dbcExample;
import org.jooq.example.r2dbc.db.tables.records.AuthorRecord;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Author extends TableImpl<AuthorRecord> {
private static final long serialVersionUID = 1L;
/**
* The reference instance of <code>R2DBC_EXAMPLE.AUTHOR</code>
*/
public static final Author AUTHOR = new Author();
/**
* The class holding records for this type
*/
@Override
public Class<AuthorRecord> getRecordType() {
return AuthorRecord.class;
}
/**
* The column <code>R2DBC_EXAMPLE.AUTHOR.ID</code>.
*/
public final TableField<AuthorRecord, Integer> ID = createField(DSL.name("ID"), SQLDataType.INTEGER.nullable(false).identity(true), this, "");
/**
* The column <code>R2DBC_EXAMPLE.AUTHOR.FIRST_NAME</code>.
*/
public final TableField<AuthorRecord, String> FIRST_NAME = createField(DSL.name("FIRST_NAME"), SQLDataType.VARCHAR(100).nullable(false), this, "");
/**
* The column <code>R2DBC_EXAMPLE.AUTHOR.LAST_NAME</code>.
*/
public final TableField<AuthorRecord, String> LAST_NAME = createField(DSL.name("LAST_NAME"), SQLDataType.VARCHAR(100).nullable(false), this, "");
private Author(Name alias, Table<AuthorRecord> aliased) {
this(alias, aliased, null);
}
private Author(Name alias, Table<AuthorRecord> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
}
/**
* Create an aliased <code>R2DBC_EXAMPLE.AUTHOR</code> table reference
*/
public Author(String alias) {
this(DSL.name(alias), AUTHOR);
}
/**
* Create an aliased <code>R2DBC_EXAMPLE.AUTHOR</code> table reference
*/
public Author(Name alias) {
this(alias, AUTHOR);
}
/**
* Create a <code>R2DBC_EXAMPLE.AUTHOR</code> table reference
*/
public Author() {
this(DSL.name("AUTHOR"), null);
}
public <O extends Record> Author(Table<O> child, ForeignKey<O, AuthorRecord> key) {
super(child, key, AUTHOR);
}
@Override
public Schema getSchema() {
return aliased() ? null : R2dbcExample.R2DBC_EXAMPLE;
}
@Override
public Identity<AuthorRecord, Integer> getIdentity() {
return (Identity<AuthorRecord, Integer>) super.getIdentity();
}
@Override
public UniqueKey<AuthorRecord> getPrimaryKey() {
return Keys.PK_AUTHOR;
}
@Override
public Author as(String alias) {
return new Author(DSL.name(alias), this);
}
@Override
public Author as(Name alias) {
return new Author(alias, this);
}
/**
* Rename this table
*/
@Override
public Author rename(String name) {
return new Author(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public Author rename(Name name) {
return new Author(name, null);
}
// -------------------------------------------------------------------------
// Row3 type methods
// -------------------------------------------------------------------------
@Override
public Row3<Integer, String, String> fieldsRow() {
return (Row3) super.fieldsRow();
}
}

View File

@ -0,0 +1,161 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db.tables;
import java.util.Arrays;
import java.util.List;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Identity;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Row3;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.example.r2dbc.db.Keys;
import org.jooq.example.r2dbc.db.R2dbcExample;
import org.jooq.example.r2dbc.db.tables.records.BookRecord;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Book extends TableImpl<BookRecord> {
private static final long serialVersionUID = 1L;
/**
* The reference instance of <code>R2DBC_EXAMPLE.BOOK</code>
*/
public static final Book BOOK = new Book();
/**
* The class holding records for this type
*/
@Override
public Class<BookRecord> getRecordType() {
return BookRecord.class;
}
/**
* The column <code>R2DBC_EXAMPLE.BOOK.ID</code>.
*/
public final TableField<BookRecord, Integer> ID = createField(DSL.name("ID"), SQLDataType.INTEGER.nullable(false).identity(true), this, "");
/**
* The column <code>R2DBC_EXAMPLE.BOOK.AUTHOR_ID</code>.
*/
public final TableField<BookRecord, Integer> AUTHOR_ID = createField(DSL.name("AUTHOR_ID"), SQLDataType.INTEGER.nullable(false), this, "");
/**
* The column <code>R2DBC_EXAMPLE.BOOK.TITLE</code>.
*/
public final TableField<BookRecord, String> TITLE = createField(DSL.name("TITLE"), SQLDataType.VARCHAR(100).nullable(false), this, "");
private Book(Name alias, Table<BookRecord> aliased) {
this(alias, aliased, null);
}
private Book(Name alias, Table<BookRecord> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
}
/**
* Create an aliased <code>R2DBC_EXAMPLE.BOOK</code> table reference
*/
public Book(String alias) {
this(DSL.name(alias), BOOK);
}
/**
* Create an aliased <code>R2DBC_EXAMPLE.BOOK</code> table reference
*/
public Book(Name alias) {
this(alias, BOOK);
}
/**
* Create a <code>R2DBC_EXAMPLE.BOOK</code> table reference
*/
public Book() {
this(DSL.name("BOOK"), null);
}
public <O extends Record> Book(Table<O> child, ForeignKey<O, BookRecord> key) {
super(child, key, BOOK);
}
@Override
public Schema getSchema() {
return aliased() ? null : R2dbcExample.R2DBC_EXAMPLE;
}
@Override
public Identity<BookRecord, Integer> getIdentity() {
return (Identity<BookRecord, Integer>) super.getIdentity();
}
@Override
public UniqueKey<BookRecord> getPrimaryKey() {
return Keys.PK_BOOK;
}
@Override
public List<ForeignKey<BookRecord, ?>> getReferences() {
return Arrays.asList(Keys.FK_BOOK_AUTHOR);
}
private transient Author _author;
public Author author() {
if (_author == null)
_author = new Author(this, Keys.FK_BOOK_AUTHOR);
return _author;
}
@Override
public Book as(String alias) {
return new Book(DSL.name(alias), this);
}
@Override
public Book as(Name alias) {
return new Book(alias, this);
}
/**
* Rename this table
*/
@Override
public Book rename(String name) {
return new Book(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public Book rename(Name name) {
return new Book(name, null);
}
// -------------------------------------------------------------------------
// Row3 type methods
// -------------------------------------------------------------------------
@Override
public Row3<Integer, Integer, String> fieldsRow() {
return (Row3) super.fieldsRow();
}
}

View File

@ -0,0 +1,180 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db.tables.records;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record3;
import org.jooq.Row3;
import org.jooq.example.r2dbc.db.tables.Author;
import org.jooq.impl.UpdatableRecordImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class AuthorRecord extends UpdatableRecordImpl<AuthorRecord> implements Record3<Integer, String, String> {
private static final long serialVersionUID = 1L;
/**
* Setter for <code>R2DBC_EXAMPLE.AUTHOR.ID</code>.
*/
public void setId(Integer value) {
set(0, value);
}
/**
* Getter for <code>R2DBC_EXAMPLE.AUTHOR.ID</code>.
*/
public Integer getId() {
return (Integer) get(0);
}
/**
* Setter for <code>R2DBC_EXAMPLE.AUTHOR.FIRST_NAME</code>.
*/
public void setFirstName(String value) {
set(1, value);
}
/**
* Getter for <code>R2DBC_EXAMPLE.AUTHOR.FIRST_NAME</code>.
*/
public String getFirstName() {
return (String) get(1);
}
/**
* Setter for <code>R2DBC_EXAMPLE.AUTHOR.LAST_NAME</code>.
*/
public void setLastName(String value) {
set(2, value);
}
/**
* Getter for <code>R2DBC_EXAMPLE.AUTHOR.LAST_NAME</code>.
*/
public String getLastName() {
return (String) get(2);
}
// -------------------------------------------------------------------------
// Primary key information
// -------------------------------------------------------------------------
@Override
public Record1<Integer> key() {
return (Record1) super.key();
}
// -------------------------------------------------------------------------
// Record3 type implementation
// -------------------------------------------------------------------------
@Override
public Row3<Integer, String, String> fieldsRow() {
return (Row3) super.fieldsRow();
}
@Override
public Row3<Integer, String, String> valuesRow() {
return (Row3) super.valuesRow();
}
@Override
public Field<Integer> field1() {
return Author.AUTHOR.ID;
}
@Override
public Field<String> field2() {
return Author.AUTHOR.FIRST_NAME;
}
@Override
public Field<String> field3() {
return Author.AUTHOR.LAST_NAME;
}
@Override
public Integer component1() {
return getId();
}
@Override
public String component2() {
return getFirstName();
}
@Override
public String component3() {
return getLastName();
}
@Override
public Integer value1() {
return getId();
}
@Override
public String value2() {
return getFirstName();
}
@Override
public String value3() {
return getLastName();
}
@Override
public AuthorRecord value1(Integer value) {
setId(value);
return this;
}
@Override
public AuthorRecord value2(String value) {
setFirstName(value);
return this;
}
@Override
public AuthorRecord value3(String value) {
setLastName(value);
return this;
}
@Override
public AuthorRecord values(Integer value1, String value2, String value3) {
value1(value1);
value2(value2);
value3(value3);
return this;
}
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/**
* Create a detached AuthorRecord
*/
public AuthorRecord() {
super(Author.AUTHOR);
}
/**
* Create a detached, initialised AuthorRecord
*/
public AuthorRecord(Integer id, String firstName, String lastName) {
super(Author.AUTHOR);
setId(id);
setFirstName(firstName);
setLastName(lastName);
}
}

View File

@ -0,0 +1,180 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.example.r2dbc.db.tables.records;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record3;
import org.jooq.Row3;
import org.jooq.example.r2dbc.db.tables.Book;
import org.jooq.impl.UpdatableRecordImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class BookRecord extends UpdatableRecordImpl<BookRecord> implements Record3<Integer, Integer, String> {
private static final long serialVersionUID = 1L;
/**
* Setter for <code>R2DBC_EXAMPLE.BOOK.ID</code>.
*/
public void setId(Integer value) {
set(0, value);
}
/**
* Getter for <code>R2DBC_EXAMPLE.BOOK.ID</code>.
*/
public Integer getId() {
return (Integer) get(0);
}
/**
* Setter for <code>R2DBC_EXAMPLE.BOOK.AUTHOR_ID</code>.
*/
public void setAuthorId(Integer value) {
set(1, value);
}
/**
* Getter for <code>R2DBC_EXAMPLE.BOOK.AUTHOR_ID</code>.
*/
public Integer getAuthorId() {
return (Integer) get(1);
}
/**
* Setter for <code>R2DBC_EXAMPLE.BOOK.TITLE</code>.
*/
public void setTitle(String value) {
set(2, value);
}
/**
* Getter for <code>R2DBC_EXAMPLE.BOOK.TITLE</code>.
*/
public String getTitle() {
return (String) get(2);
}
// -------------------------------------------------------------------------
// Primary key information
// -------------------------------------------------------------------------
@Override
public Record1<Integer> key() {
return (Record1) super.key();
}
// -------------------------------------------------------------------------
// Record3 type implementation
// -------------------------------------------------------------------------
@Override
public Row3<Integer, Integer, String> fieldsRow() {
return (Row3) super.fieldsRow();
}
@Override
public Row3<Integer, Integer, String> valuesRow() {
return (Row3) super.valuesRow();
}
@Override
public Field<Integer> field1() {
return Book.BOOK.ID;
}
@Override
public Field<Integer> field2() {
return Book.BOOK.AUTHOR_ID;
}
@Override
public Field<String> field3() {
return Book.BOOK.TITLE;
}
@Override
public Integer component1() {
return getId();
}
@Override
public Integer component2() {
return getAuthorId();
}
@Override
public String component3() {
return getTitle();
}
@Override
public Integer value1() {
return getId();
}
@Override
public Integer value2() {
return getAuthorId();
}
@Override
public String value3() {
return getTitle();
}
@Override
public BookRecord value1(Integer value) {
setId(value);
return this;
}
@Override
public BookRecord value2(Integer value) {
setAuthorId(value);
return this;
}
@Override
public BookRecord value3(String value) {
setTitle(value);
return this;
}
@Override
public BookRecord values(Integer value1, Integer value2, String value3) {
value1(value1);
value2(value2);
value3(value3);
return this;
}
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/**
* Create a detached BookRecord
*/
public BookRecord() {
super(Book.BOOK);
}
/**
* Create a detached, initialised BookRecord
*/
public BookRecord(Integer id, Integer authorId, String title) {
super(Book.BOOK);
setId(id);
setAuthorId(authorId);
setTitle(title);
}
}

View File

@ -0,0 +1,19 @@
DROP SCHEMA IF EXISTS r2dbc_example CASCADE;
CREATE SCHEMA r2dbc_example;
CREATE TABLE r2dbc_example.author (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
CONSTRAINT pk_author PRIMARY KEY (id)
);
CREATE TABLE r2dbc_example.book (
id INT NOT NULL AUTO_INCREMENT,
author_id INT NOT NULL,
title VARCHAR(100) NOT NULL,
CONSTRAINT pk_book PRIMARY KEY (id),
CONSTRAINT fk_book_author FOREIGN KEY (id) REFERENCES r2dbc_example.author
);

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ABSOLUTE} %5p [%-50c{4}] - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,50 @@
package org.jooq.example.r2dbc.test;
import static org.jooq.example.r2dbc.db.Tables.AUTHOR;
import static org.jooq.example.r2dbc.db.Tables.BOOK;
import org.jooq.DSLContext;
import org.jooq.RowCountQuery;
import org.jooq.Source;
import org.jooq.impl.DSL;
import org.junit.Before;
import org.junit.Test;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import reactor.core.publisher.Flux;
public class R2dbcTest {
DSLContext ctx;
@Before
public void setup() {
ConnectionFactory connectionFactory = ConnectionFactories.get(
ConnectionFactoryOptions
.parse("r2dbc:h2:file://localhost/~/jooq-r2dbc-example")
.mutate()
.option(ConnectionFactoryOptions.USER, "sa")
.option(ConnectionFactoryOptions.PASSWORD, "")
.build()
);
ctx = DSL.using(connectionFactory);
Flux.fromIterable(ctx.parser().parse(Source.of(R2dbcTest.class.getResourceAsStream("/db.sql")).readString()))
.ofType(RowCountQuery.class)
.flatMap(q -> q)
.collectList()
.block();
}
@Test
public void test() {
Flux.from(ctx.insertInto(AUTHOR).columns(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME).values("John", "Doe").returningResult(AUTHOR.ID))
.flatMap(id -> ctx.insertInto(BOOK).columns(BOOK.AUTHOR_ID, BOOK.TITLE).values(id.value1(), "Fancy Book"))
.thenMany(ctx.select(BOOK.author().FIRST_NAME, BOOK.author().LAST_NAME, BOOK.TITLE).from(BOOK))
.doOnNext(System.out::println)
.subscribe();
}
}

View File

@ -81,6 +81,7 @@
<module>jOOQ-spring-boot-example</module>
<module>jOOQ-jpa-example-entities</module>
<module>jOOQ-jpa-example</module>
<module>jOOQ-r2dbc-example</module>
<module>jOOQ-testcontainers-example</module>
</modules>
</project>