diff --git a/by-language/java-jooq/README.rst b/by-language/java-jooq/README.rst index 628cd431..dbaa8d14 100644 --- a/by-language/java-jooq/README.rst +++ b/by-language/java-jooq/README.rst @@ -4,6 +4,7 @@ Java jOOQ demo application for CrateDB using PostgreSQL JDBC ############################################################ + ***** About ***** @@ -12,9 +13,8 @@ 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. - +testing rig for eventually growing a full-fledged CrateDB dialect. +Contributions are welcome. Introduction ============ @@ -35,19 +35,48 @@ In some kind, jOOQ is similar to `LINQ`_, `but better `_. +******* Details -======= +******* + +Overview +======== -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`_. +The code examples will demonstrate a few of the `different use cases for jOOQ`_. +That is, how to use the `jOOQ DSL API`_ based on code generated with `jOOQ's +code generator`_ to take your database schema and reverse-engineer it into a +set of Java classes, as well how to use the `Dynamic SQL API`_ by using `jOOQ +as a SQL builder without code generation`_. +Schema management +================= + +In many cases, the schema is defined in the form of SQL scripts, which can be +used with a `database schema migration`_ framework like `Flyway`_, +`Liquibase`_, `Bytebase`_, etc. + +The `DDLDatabase - Code generation from SQL files`_ feature can be used to +effectively reflect the database schema from SQL DDL files, without needing +a database instance at all. The code provided within the ``src/generated`` +directory has been generated like this. Caveats ======= +- `jOOQ's code generator`_ currently does not work with directly connecting to + a real CrateDB database instance and reflecting the schema from there. + Because SQL DDL statements are usually maintained in form of multiple + incremental migration scripts anyway, this is considered to be not of a too + big concern, see above. With corresponding improvements to CrateDB, this + can be made work in the future, see `issue #10 - with reflection from the + database`_. + - Most of the jOOQ examples use uppercase letters for the database, table, and - field names. CrateDB currently only handles lowercase letters. + field names. Within this setup, we have only been able to make it work using + lowercase letters. + +- We have not been able to make multiple SQL DDL statements work within a + single SQL bootstrap file at ``src/main/resources/bootstrap.sql``. ***** @@ -73,15 +102,21 @@ Usage ./gradlew generateJooq +.. _Bytebase: https://github.com/bytebase/bytebase .. _CrateDB: https://github.com/crate/crate +.. _database schema migration: https://en.wikipedia.org/wiki/Schema_migration +.. _DDLDatabase - Code generation from SQL files: https://www.jooq.org/doc/latest/manual/code-generation/codegen-ddl/ .. _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/ +.. _Dynamic SQL API: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql/ +.. _Flyway: https://github.com/flyway/flyway .. _Gradle: https://gradle.org/ .. _Insight into Language Integrated Querying: https://blog.jooq.org/jooq-tuesdays-ming-yee-iu-gives-insight-into-language-integrated-querying/ +.. _issue #10 - with reflection from the database: https://github.com/crate/cratedb-examples/pull/10 .. _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 +.. _Liquibase: https://github.com/liquibase/liquibase .. _PostgreSQL JDBC Driver: https://github.com/pgjdbc/pgjdbc diff --git a/by-language/java-jooq/build.gradle b/by-language/java-jooq/build.gradle index 871a3b55..940ae3a8 100644 --- a/by-language/java-jooq/build.gradle +++ b/by-language/java-jooq/build.gradle @@ -22,6 +22,7 @@ repositories { dependencies { implementation 'org.jooq:jooq:3.17.7' + implementation 'org.jooq:jooq-meta: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' diff --git a/by-language/java-jooq/jooq.gradle b/by-language/java-jooq/jooq.gradle index 6a0caf1b..39c463b8 100644 --- a/by-language/java-jooq/jooq.gradle +++ b/by-language/java-jooq/jooq.gradle @@ -36,14 +36,11 @@ apply plugin: nu.studer.gradle.jooq.JooqPlugin dependencies { jooqGenerator 'org.postgresql:postgresql:42.5.1' + jooqGenerator 'org.jooq:jooq-meta-extensions:3.17.7' } jooq { - // Defaults (can be omitted). - // version = '3.17.6' - // edition = nu.studer.gradle.jooq.JooqEdition.OSS - configurations { // Name of the jOOQ configuration. main { @@ -53,27 +50,61 @@ jooq { generationTool { logging = org.jooq.meta.jaxb.Logging.WARN - jdbc { - driver = 'org.postgresql.Driver' - url = 'jdbc:postgresql://localhost:5432/testdrive' - user = 'crate' - password = '' - properties { - property { - key = 'PAGE_SIZE' - value = 2048 - } - } - } generator { name = 'org.jooq.codegen.DefaultGenerator' + strategy.name = 'org.jooq.codegen.DefaultGeneratorStrategy' database { - name = 'org.jooq.meta.postgres.PostgresDatabase' - inputSchema = 'testdrive' + name = 'org.jooq.meta.extensions.ddl.DDLDatabase' + properties { + + // Specify the location of your SQL script. + // You may use ant-style file matching, e.g. /path/**/to/*.sql + // + // Where: + // - ** matches any directory subtree + // - * matches any number of characters in a directory / file name + // - ? matches a single character in a directory / file name + property { + key = 'scripts' + value = 'src/main/resources/*.sql' + } + + // The sort order of the scripts within a directory, where: + // + // - semantic: sorts versions, e.g. v-3.10.0 is after v-3.9.0 (default) + // - alphanumeric: sorts strings, e.g. v-3.10.0 is before v-3.9.0 + // - flyway: sorts files the same way as flyway does + // - none: doesn't sort directory contents after fetching them from the directory + property { + key = 'sort' + value = 'semantic' + } + + // The default schema for unqualified objects: + // + // - public: all unqualified objects are located in the PUBLIC (upper case) schema + // - none: all unqualified objects are located in the default schema (default) + // + // This configuration can be overridden with the schema mapping feature + property { + key = 'unqualifiedSchema' + value = 'none' + } + + // The default name case for unquoted objects: + // + // - as_is: unquoted object names are kept unquoted + // - upper: unquoted object names are turned into upper case (most databases) + // - lower: unquoted object names are turned into lower case (e.g. PostgreSQL) + property { + key = 'defaultNameCase' + value = 'lower' + } + } } generate { deprecated = false - records = false + records = true immutablePojos = false fluentSetters = false } @@ -81,7 +112,6 @@ jooq { packageName = 'io.crate.demo.jooq.model' directory = 'src/generated/java' } - strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy" } } } diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultCatalog.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultCatalog.java index d1cef101..d0a975ed 100644 --- a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultCatalog.java +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultCatalog.java @@ -7,8 +7,6 @@ import java.util.Arrays; import java.util.List; -import javax.annotation.processing.Generated; - import org.jooq.Constants; import org.jooq.Schema; import org.jooq.impl.CatalogImpl; @@ -17,13 +15,6 @@ /** * This class is generated by jOOQ. */ -@Generated( - value = { - "https://www.jooq.org", - "jOOQ version:3.17.7" - }, - comments = "This class is generated by jOOQ." -) @SuppressWarnings({ "all", "unchecked", "rawtypes" }) public class DefaultCatalog extends CatalogImpl { @@ -35,9 +26,9 @@ public class DefaultCatalog extends CatalogImpl { public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog(); /** - * The schema testdrive. + * The schema DEFAULT_SCHEMA. */ - public final DemoDatabase DEMO_DATABASE = DemoDatabase.DEMO_DATABASE; + public final DefaultSchema DEFAULT_SCHEMA = DefaultSchema.DEFAULT_SCHEMA; /** * No further instances allowed @@ -48,8 +39,8 @@ private DefaultCatalog() { @Override public final List getSchemas() { - return Arrays.asList( - DemoDatabase.DEMO_DATABASE + return Arrays.asList( + DefaultSchema.DEFAULT_SCHEMA ); } diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DemoDatabase.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultSchema.java similarity index 58% rename from by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DemoDatabase.java rename to by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultSchema.java index 6b5e93da..8795c9ad 100644 --- a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DemoDatabase.java +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/DefaultSchema.java @@ -5,12 +5,11 @@ import io.crate.demo.jooq.model.tables.Author; +import io.crate.demo.jooq.model.tables.Book; import java.util.Arrays; import java.util.List; -import javax.annotation.processing.Generated; - import org.jooq.Catalog; import org.jooq.Table; import org.jooq.impl.SchemaImpl; @@ -19,33 +18,31 @@ /** * This class is generated by jOOQ. */ -@Generated( - value = { - "https://www.jooq.org", - "jOOQ version:3.17.7" - }, - comments = "This class is generated by jOOQ." -) @SuppressWarnings({ "all", "unchecked", "rawtypes" }) -public class DemoDatabase extends SchemaImpl { +public class DefaultSchema extends SchemaImpl { private static final long serialVersionUID = 1L; /** - * The reference instance of testdrive + * The reference instance of DEFAULT_SCHEMA */ - public static final DemoDatabase DEMO_DATABASE = new DemoDatabase(); + public static final DefaultSchema DEFAULT_SCHEMA = new DefaultSchema(); /** - * The table testdrive.author. + * The table author. */ public final Author AUTHOR = Author.AUTHOR; + /** + * The table book. + */ + public final Book BOOK = Book.BOOK; + /** * No further instances allowed */ - private DemoDatabase() { - super("testdrive", null); + private DefaultSchema() { + super("", null); } @@ -57,7 +54,8 @@ public Catalog getCatalog() { @Override public final List> getTables() { return Arrays.asList( - Author.AUTHOR + Author.AUTHOR, + Book.BOOK ); } } diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Keys.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Keys.java index 79ffd13f..88fc2db2 100644 --- a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Keys.java +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Keys.java @@ -5,9 +5,9 @@ import io.crate.demo.jooq.model.tables.Author; +import io.crate.demo.jooq.model.tables.Book; import io.crate.demo.jooq.model.tables.records.AuthorRecord; - -import javax.annotation.processing.Generated; +import io.crate.demo.jooq.model.tables.records.BookRecord; import org.jooq.TableField; import org.jooq.UniqueKey; @@ -16,21 +16,16 @@ /** - * A class modelling foreign key relationships and constraints of tables of - * the testdb schema. + * A class modelling foreign key relationships and constraints of tables in the + * default schema. */ -@Generated( - value = { - "https://www.jooq.org", - "jOOQ version:3.17.7" - }, - comments = "This class is generated by jOOQ" -) -@SuppressWarnings({ "all", "unchecked", "rawtypes" })public class Keys { +@SuppressWarnings({ "all", "unchecked", "rawtypes" }) +public class Keys { // ------------------------------------------------------------------------- // UNIQUE and PRIMARY KEY definitions // ------------------------------------------------------------------------- - public static final UniqueKey PK_AUTHOR = Internal.createUniqueKey(Author.AUTHOR, DSL.name("PK_AUTHOR"), new TableField[] { Author.AUTHOR.ID }, true); + public static final UniqueKey CONSTRAINT_A = Internal.createUniqueKey(Author.AUTHOR, DSL.name("CONSTRAINT_A"), new TableField[] { Author.AUTHOR.ID }, true); + public static final UniqueKey CONSTRAINT_2 = Internal.createUniqueKey(Book.BOOK, DSL.name("CONSTRAINT_2"), new TableField[] { Book.BOOK.ID }, true); } diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Tables.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Tables.java index b7657f06..ee234bcb 100644 --- a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Tables.java +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/Tables.java @@ -5,24 +5,22 @@ import io.crate.demo.jooq.model.tables.Author; +import io.crate.demo.jooq.model.tables.Book; -import javax.annotation.processing.Generated; /** - * Convenience access to all tables in testdb + * Convenience access to all tables in the default schema. */ -@Generated( - value = { - "https://www.jooq.org", - "jOOQ version:3.17.7" - }, - comments = "This class is generated by jOOQ" -) @SuppressWarnings({ "all", "unchecked", "rawtypes" }) public class Tables { /** - * The table testdrive.author. + * The table author. */ public static final Author AUTHOR = Author.AUTHOR; + + /** + * The table book. + */ + public static final Book BOOK = Book.BOOK; } diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/Author.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/Author.java index edcec673..2224b60c 100644 --- a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/Author.java +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/Author.java @@ -4,7 +4,7 @@ package io.crate.demo.jooq.model.tables; -import io.crate.demo.jooq.model.DemoDatabase; +import io.crate.demo.jooq.model.DefaultSchema; import io.crate.demo.jooq.model.Keys; import io.crate.demo.jooq.model.tables.records.AuthorRecord; @@ -13,7 +13,6 @@ import org.jooq.Field; import org.jooq.ForeignKey; import org.jooq.Function2; -import org.jooq.Identity; import org.jooq.Name; import org.jooq.Record; import org.jooq.Records; @@ -28,25 +27,17 @@ import org.jooq.impl.SQLDataType; import org.jooq.impl.TableImpl; -import javax.annotation.processing.Generated; /** * This class is generated by jOOQ. */ -@Generated( - value = { - "https://www.jooq.org", - "jOOQ version:3.17.7" - }, - comments = "This class is generated by jOOQ." -) @SuppressWarnings({ "all", "unchecked", "rawtypes" }) public class Author extends TableImpl { private static final long serialVersionUID = 1L; /** - * The reference instance of testdrive.author + * The reference instance of author */ public static final Author AUTHOR = new Author(); @@ -58,9 +49,15 @@ public Class getRecordType() { return AuthorRecord.class; } - public final TableField ID = createField(DSL.name("id"), SQLDataType.INTEGER.nullable(false).identity(true), this, "The author's identifier"); + /** + * The column author.id. + */ + public final TableField ID = createField(DSL.name("id"), SQLDataType.INTEGER.nullable(false), this, ""); - public final TableField NAME = createField(DSL.name("name"), SQLDataType.VARCHAR.nullable(true), this, "The author's name"); + /** + * The column author.name. + */ + public final TableField NAME = createField(DSL.name("name"), SQLDataType.VARCHAR(255), this, ""); private Author(Name alias, Table aliased) { this(alias, aliased, null); @@ -71,21 +68,21 @@ private Author(Name alias, Table aliased, Field[] parameters) { } /** - * Create an aliased testdrive.author table reference + * Create an aliased author table reference */ public Author(String alias) { this(DSL.name(alias), AUTHOR); } /** - * Create an aliased testdrive.author table reference + * Create an aliased author table reference */ public Author(Name alias) { this(alias, AUTHOR); } /** - * Create an aliased testdrive.author table reference + * Create a author table reference */ public Author() { this(DSL.name("author"), null); @@ -97,17 +94,12 @@ public Author(Table child, ForeignKey key @Override public Schema getSchema() { - return aliased() ? null : DemoDatabase.DEMO_DATABASE; - } - - @Override - public Identity getIdentity() { - return (Identity) super.getIdentity(); + return aliased() ? null : DefaultSchema.DEFAULT_SCHEMA; } @Override public UniqueKey getPrimaryKey() { - return Keys.PK_AUTHOR; + return Keys.CONSTRAINT_A; } @Override @@ -154,7 +146,7 @@ public Author rename(Table name) { // ------------------------------------------------------------------------- @Override - public Row2 fieldsRow() { + public Row2 fieldsRow() { return (Row2) super.fieldsRow(); } diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/Book.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/Book.java new file mode 100644 index 00000000..fc231016 --- /dev/null +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/Book.java @@ -0,0 +1,167 @@ +/* + * This file is generated by jOOQ. + */ +package io.crate.demo.jooq.model.tables; + + +import io.crate.demo.jooq.model.DefaultSchema; +import io.crate.demo.jooq.model.Keys; +import io.crate.demo.jooq.model.tables.records.BookRecord; + +import java.util.function.Function; + +import org.jooq.Field; +import org.jooq.ForeignKey; +import org.jooq.Function2; +import org.jooq.Name; +import org.jooq.Record; +import org.jooq.Records; +import org.jooq.Row2; +import org.jooq.Schema; +import org.jooq.SelectField; +import org.jooq.Table; +import org.jooq.TableField; +import org.jooq.TableOptions; +import org.jooq.UniqueKey; +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 { + + private static final long serialVersionUID = 1L; + + /** + * The reference instance of book + */ + public static final Book BOOK = new Book(); + + /** + * The class holding records for this type + */ + @Override + public Class getRecordType() { + return BookRecord.class; + } + + /** + * The column book.id. + */ + public final TableField ID = createField(DSL.name("id"), SQLDataType.INTEGER.nullable(false), this, ""); + + /** + * The column book.title. + */ + public final TableField TITLE = createField(DSL.name("title"), SQLDataType.VARCHAR(255), this, ""); + + private Book(Name alias, Table aliased) { + this(alias, aliased, null); + } + + private Book(Name alias, Table aliased, Field[] parameters) { + super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table()); + } + + /** + * Create an aliased book table reference + */ + public Book(String alias) { + this(DSL.name(alias), BOOK); + } + + /** + * Create an aliased book table reference + */ + public Book(Name alias) { + this(alias, BOOK); + } + + /** + * Create a book table reference + */ + public Book() { + this(DSL.name("book"), null); + } + + public Book(Table child, ForeignKey key) { + super(child, key, BOOK); + } + + @Override + public Schema getSchema() { + return aliased() ? null : DefaultSchema.DEFAULT_SCHEMA; + } + + @Override + public UniqueKey getPrimaryKey() { + return Keys.CONSTRAINT_2; + } + + @Override + public Book as(String alias) { + return new Book(DSL.name(alias), this); + } + + @Override + public Book as(Name alias) { + return new Book(alias, this); + } + + @Override + public Book as(Table alias) { + return new Book(alias.getQualifiedName(), 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); + } + + /** + * Rename this table + */ + @Override + public Book rename(Table name) { + return new Book(name.getQualifiedName(), null); + } + + // ------------------------------------------------------------------------- + // Row2 type methods + // ------------------------------------------------------------------------- + + @Override + public Row2 fieldsRow() { + return (Row2) super.fieldsRow(); + } + + /** + * Convenience mapping calling {@link SelectField#convertFrom(Function)}. + */ + public SelectField mapping(Function2 from) { + return convertFrom(Records.mapping(from)); + } + + /** + * Convenience mapping calling {@link SelectField#convertFrom(Class, + * Function)}. + */ + public SelectField mapping(Class toType, Function2 from) { + return convertFrom(toType, Records.mapping(from)); + } +} diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/records/AuthorRecord.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/records/AuthorRecord.java index fd2be9c7..0c165808 100644 --- a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/records/AuthorRecord.java +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/records/AuthorRecord.java @@ -1,10 +1,11 @@ +/* + * This file is generated by jOOQ. + */ package io.crate.demo.jooq.model.tables.records; import io.crate.demo.jooq.model.tables.Author; -import javax.annotation.processing.Generated; - import org.jooq.Field; import org.jooq.Record1; import org.jooq.Record2; @@ -15,30 +16,35 @@ /** * This class is generated by jOOQ. */ -@Generated( - value = { - "https://www.jooq.org", - "jOOQ version:3.17.7" - }, - comments = "This class is generated by jOOQ." -) @SuppressWarnings({ "all", "unchecked", "rawtypes" }) public class AuthorRecord extends UpdatableRecordImpl implements Record2 { private static final long serialVersionUID = 1L; + /** + * Setter for author.id. + */ public void setId(Integer value) { set(0, value); } + /** + * Getter for author.id. + */ public Integer getId() { return (Integer) get(0); } + /** + * Setter for author.name. + */ public void setName(String value) { set(1, value); } + /** + * Getter for author.name. + */ public String getName() { return (String) get(1); } diff --git a/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/records/BookRecord.java b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/records/BookRecord.java new file mode 100644 index 00000000..53ef60b6 --- /dev/null +++ b/by-language/java-jooq/src/generated/java/io/crate/demo/jooq/model/tables/records/BookRecord.java @@ -0,0 +1,144 @@ +/* + * This file is generated by jOOQ. + */ +package io.crate.demo.jooq.model.tables.records; + + +import io.crate.demo.jooq.model.tables.Book; + +import org.jooq.Field; +import org.jooq.Record1; +import org.jooq.Record2; +import org.jooq.Row2; +import org.jooq.impl.UpdatableRecordImpl; + + +/** + * This class is generated by jOOQ. + */ +@SuppressWarnings({ "all", "unchecked", "rawtypes" }) +public class BookRecord extends UpdatableRecordImpl implements Record2 { + + private static final long serialVersionUID = 1L; + + /** + * Setter for book.id. + */ + public void setId(Integer value) { + set(0, value); + } + + /** + * Getter for book.id. + */ + public Integer getId() { + return (Integer) get(0); + } + + /** + * Setter for book.title. + */ + public void setTitle(String value) { + set(1, value); + } + + /** + * Getter for book.title. + */ + public String getTitle() { + return (String) get(1); + } + + // ------------------------------------------------------------------------- + // Primary key information + // ------------------------------------------------------------------------- + + @Override + public Record1 key() { + return (Record1) super.key(); + } + + // ------------------------------------------------------------------------- + // Record2 type implementation + // ------------------------------------------------------------------------- + + @Override + public Row2 fieldsRow() { + return (Row2) super.fieldsRow(); + } + + @Override + public Row2 valuesRow() { + return (Row2) super.valuesRow(); + } + + @Override + public Field field1() { + return Book.BOOK.ID; + } + + @Override + public Field field2() { + return Book.BOOK.TITLE; + } + + @Override + public Integer component1() { + return getId(); + } + + @Override + public String component2() { + return getTitle(); + } + + @Override + public Integer value1() { + return getId(); + } + + @Override + public String value2() { + return getTitle(); + } + + @Override + public BookRecord value1(Integer value) { + setId(value); + return this; + } + + @Override + public BookRecord value2(String value) { + setTitle(value); + return this; + } + + @Override + public BookRecord values(Integer value1, String value2) { + value1(value1); + value2(value2); + return this; + } + + // ------------------------------------------------------------------------- + // Constructors + // ------------------------------------------------------------------------- + + /** + * Create a detached BookRecord + */ + public BookRecord() { + super(Book.BOOK); + } + + /** + * Create a detached, initialised BookRecord + */ + public BookRecord(Integer id, String title) { + super(Book.BOOK); + + setId(id); + setTitle(title); + } +} diff --git a/by-language/java-jooq/src/main/java/io/crate/demo/jooq/Application.java b/by-language/java-jooq/src/main/java/io/crate/demo/jooq/Application.java index 0c3338fc..cc05311e 100644 --- a/by-language/java-jooq/src/main/java/io/crate/demo/jooq/Application.java +++ b/by-language/java-jooq/src/main/java/io/crate/demo/jooq/Application.java @@ -1,7 +1,7 @@ package io.crate.demo.jooq; -import org.jooq.*; import org.jooq.Record; +import org.jooq.*; import org.jooq.conf.Settings; import org.jooq.impl.DSL; @@ -12,8 +12,7 @@ import java.util.Properties; import io.crate.demo.jooq.model.tables.records.AuthorRecord; - -import static io.crate.demo.jooq.model.Tables.*; +import static io.crate.demo.jooq.model.Tables.AUTHOR; import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.table; @@ -91,7 +90,7 @@ public void exampleWithGeneratedCode() throws IOException, SQLException { DSLContext db = getDSLContext(); // Create table. - String bootstrap_sql = Tools.readTextFile("bootstrap.sql"); + String bootstrap_sql = Tools.readTextFile("author.sql"); db.query(bootstrap_sql).execute(); // Truncate table. @@ -148,12 +147,12 @@ public void exampleWithDynamicSchema() throws IOException, SQLException { DSLContext db = getDSLContext(); - Table BOOK = table("\"testdrive\".\"book\""); + Table BOOK = table("book"); Field BOOK_ID = field("id"); Field BOOK_TITLE = field("title"); // Create table. - String bootstrap_sql = Tools.readTextFile("bootstrap.sql"); + String bootstrap_sql = Tools.readTextFile("book.sql"); db.query(bootstrap_sql).execute(); // Truncate table. diff --git a/by-language/java-jooq/src/main/resources/application.properties b/by-language/java-jooq/src/main/resources/application.properties index 63a01274..82732147 100644 --- a/by-language/java-jooq/src/main/resources/application.properties +++ b/by-language/java-jooq/src/main/resources/application.properties @@ -1,3 +1,3 @@ -application.datasource.url=jdbc:postgresql://localhost:5432/ +application.datasource.url=jdbc:postgresql://localhost:5432/testdrive application.datasource.username=crate application.datasource.password= diff --git a/by-language/java-jooq/src/main/resources/author.sql b/by-language/java-jooq/src/main/resources/author.sql new file mode 100644 index 00000000..6147296d --- /dev/null +++ b/by-language/java-jooq/src/main/resources/author.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS "author" ( + id INTEGER PRIMARY KEY, + name VARCHAR(255) +); diff --git a/by-language/java-jooq/src/main/resources/book.sql b/by-language/java-jooq/src/main/resources/book.sql new file mode 100644 index 00000000..50c03f1b --- /dev/null +++ b/by-language/java-jooq/src/main/resources/book.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS "book" ( + id INTEGER PRIMARY KEY, + title VARCHAR(255) +); diff --git a/by-language/java-jooq/src/main/resources/bootstrap.sql b/by-language/java-jooq/src/main/resources/bootstrap.sql deleted file mode 100644 index 5ca396f6..00000000 --- a/by-language/java-jooq/src/main/resources/bootstrap.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE IF NOT EXISTS "testdrive"."author" ( - id INTEGER PRIMARY KEY, - name VARCHAR(255) -); - -CREATE TABLE IF NOT EXISTS "testdrive"."book" ( - id INTEGER PRIMARY KEY, - title VARCHAR(255) -); diff --git a/by-language/java-jooq/src/test/java/io/crate/demo/jooq/ApplicationTest.java b/by-language/java-jooq/src/test/java/io/crate/demo/jooq/ApplicationTest.java index 07fa02dc..51b18dbd 100644 --- a/by-language/java-jooq/src/test/java/io/crate/demo/jooq/ApplicationTest.java +++ b/by-language/java-jooq/src/test/java/io/crate/demo/jooq/ApplicationTest.java @@ -40,7 +40,7 @@ public void testExampleWithDynamicSchema() throws SQLException, IOException { // Check number of records. DSLContext db = app.getDSLContext(); - int count = db.fetchCount(DSL.selectFrom("testdrive.book")); + int count = db.fetchCount(DSL.selectFrom("book")); Assert.assertEquals(count, 2); }