From 501caf4d020991e612fc98c79d65ca3854a2c415 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 26 May 2015 16:24:27 +0200 Subject: [PATCH] [#4307] Add an example using the Spark Framework --- jOOQ-examples/jOOQ-spark-example/.gitignore | 2 + jOOQ-examples/jOOQ-spark-example/LICENSE.txt | 22 ++ jOOQ-examples/jOOQ-spark-example/README.md | 16 ++ jOOQ-examples/jOOQ-spark-example/pom.xml | 224 ++++++++++++++++++ .../org/jooq/example/spark/SparkCRUD.java | 181 ++++++++++++++ .../src/main/resources/META-INF/LICENSE.txt | 22 ++ .../src/main/resources/META-INF/README.txt | 2 + .../src/main/resources/config.properties | 11 + .../src/main/resources/db-h2.sql | 26 ++ .../src/main/resources/log4j.xml | 36 +++ 10 files changed, 542 insertions(+) create mode 100644 jOOQ-examples/jOOQ-spark-example/.gitignore create mode 100644 jOOQ-examples/jOOQ-spark-example/LICENSE.txt create mode 100644 jOOQ-examples/jOOQ-spark-example/README.md create mode 100644 jOOQ-examples/jOOQ-spark-example/pom.xml create mode 100644 jOOQ-examples/jOOQ-spark-example/src/main/java/org/jooq/example/spark/SparkCRUD.java create mode 100644 jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/LICENSE.txt create mode 100644 jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/README.txt create mode 100644 jOOQ-examples/jOOQ-spark-example/src/main/resources/config.properties create mode 100644 jOOQ-examples/jOOQ-spark-example/src/main/resources/db-h2.sql create mode 100644 jOOQ-examples/jOOQ-spark-example/src/main/resources/log4j.xml diff --git a/jOOQ-examples/jOOQ-spark-example/.gitignore b/jOOQ-examples/jOOQ-spark-example/.gitignore new file mode 100644 index 0000000000..9df5d49712 --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/.gitignore @@ -0,0 +1,2 @@ +/target +/*.iml \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/LICENSE.txt b/jOOQ-examples/jOOQ-spark-example/LICENSE.txt new file mode 100644 index 0000000000..d253ed643b --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) +All rights reserved. + +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 \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/README.md b/jOOQ-examples/jOOQ-spark-example/README.md new file mode 100644 index 0000000000..4405ddd201 --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/README.md @@ -0,0 +1,16 @@ +Thanks for downloading jOOQ. +Please visit http://www.jooq.org for more information. + +To install and run this example, please check out the complete jOOQ repository first, and use Maven to install the latest SNAPSHOT version of jOOQ: + +``` +$ pwd +/path/to/checkout/dir +$ ls +jOOQ jOOQ-meta jOOQ-codegen ... +$ mvn clean install +... +$ cd jOOQ-examples/jOOQ-javafx-example +... +$ mvn clean install +``` \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/pom.xml b/jOOQ-examples/jOOQ-spark-example/pom.xml new file mode 100644 index 0000000000..404a604cf5 --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/pom.xml @@ -0,0 +1,224 @@ + + + 4.0.0 + + org.jooq + jooq-spark-example + 1.0 + jOOQ Spark Example + + + + Apache License, Version 2.0 + http://www.jooq.org/inc/LICENSE.txt + repo + + + + + UTF-8 + 3.2.6.RELEASE + 3.6.1 + 1.4.181 + + + + + + + org.jooq + jooq + ${org.jooq.version} + + + com.h2database + h2 + ${org.h2.version} + + + + + com.sparkjava + spark-core + 2.2 + + + + + log4j + log4j + 1.2.16 + + + org.slf4j + slf4j-log4j12 + 1.7.5 + + + + + junit + junit + 4.11 + jar + test + + + + + + + + + true + src/main/resources + + **/*.xml + **/*.properties + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + true + 1024m + 256m + UTF-8 + 1.8 + 1.8 + true + lines,vars,source + + + -Xlint:varargs + + + + + + + org.codehaus.mojo + properties-maven-plugin + 1.0-alpha-2 + + + initialize + + read-project-properties + + + + src/main/resources/config.properties + + + + + + + + + org.codehaus.mojo + sql-maven-plugin + 1.5 + + + ${maven.test.skip} + always + + + + + create-database-postgres + generate-sources + + execute + + + ${db.driver} + ${db.url} + ${db.username} + ${db.password} + + true + + src/main/resources/db-h2.sql + + + + + + + + com.h2database + h2 + ${org.h2.version} + + + + + + + org.jooq + jooq-codegen-maven + ${org.jooq.version} + + + + generate-h2 + generate-sources + + generate + + + + ${db.driver} + ${db.url} + ${db.username} + ${db.password} + + + + PUBLIC + + + org.jooq.example.db.h2 + target/generated-sources/jooq-h2 + + + + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + run-example + install + + java + + + org.jooq.example.spark.SparkCRUD + + + + + + + \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/src/main/java/org/jooq/example/spark/SparkCRUD.java b/jOOQ-examples/jOOQ-spark-example/src/main/java/org/jooq/example/spark/SparkCRUD.java new file mode 100644 index 0000000000..b07f5defcb --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/src/main/java/org/jooq/example/spark/SparkCRUD.java @@ -0,0 +1,181 @@ +/** + * Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * 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. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.example.spark; + +import static org.jooq.example.db.h2.Tables.AUTHOR; +import static org.jooq.example.db.h2.Tables.BOOK; +import static spark.Spark.delete; +import static spark.Spark.get; +import static spark.Spark.post; +import static spark.Spark.put; + +import java.util.Properties; +import java.util.stream.Collectors; + +import org.jooq.DSLContext; +import org.jooq.Record2; +import org.jooq.example.db.h2.tables.records.AuthorRecord; +import org.jooq.example.db.h2.tables.records.BookRecord; +import org.jooq.impl.DSL; + +import spark.Request; + +/** + * A simple CRUD example showing howto create, get, update and delete book resources. + */ +public class SparkCRUD { + + public static void main(String[] args) throws Exception { + final Properties properties = new Properties(); + properties.load(SparkCRUD.class.getResourceAsStream("/config.properties")); + Class.forName(properties.getProperty("db.driver")); + final DSLContext ctx = DSL.using( + properties.getProperty("db.url"), + properties.getProperty("db.username"), + properties.getProperty("db.password") + ); + + // Creates a new book resource, will return the ID to the created resource + // author and title are sent as query parameters e.g. /books?author=Foo&title=Bar + post("/books", (request, response) -> { + AuthorRecord author = upsertAuthor(ctx, request); + + BookRecord book = ctx.newRecord(BOOK); + book.setAuthorId(author.getId()); + book.setTitle(request.queryParams("title")); + book.store(); + + response.status(201); // 201 Created + return book.getId(); + }); + + // Gets the book resource for the provided id + get("/books/:id", (request, response) -> { + Record2 book = ctx + .select(BOOK.TITLE, AUTHOR.NAME) + .from(BOOK) + .join(AUTHOR).on(BOOK.AUTHOR_ID.eq(AUTHOR.ID)) + .where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))) + .fetchOne(); + + if (book != null) { + return "Title: " + book.value1() + ", Author: " + book.value2(); + } + else { + response.status(404); // 404 Not found + return "Book not found"; + } + }); + + // Updates the book resource for the provided id with new information + // author and title are sent as query parameters e.g. /books/?author=Foo&title=Bar + put("/books/:id", (request, response) -> { + BookRecord book = ctx + .selectFrom(BOOK) + .where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))) + .fetchOne(); + + if (book != null) { + AuthorRecord author = upsertAuthor(ctx, request); + + String newAuthor = request.queryParams("author"); + String newTitle = request.queryParams("title"); + + if (newAuthor != null) { + book.setAuthorId(author.getId()); + } + if (newTitle != null) { + book.setTitle(newTitle); + } + + book.update(); + return "Book with id '" + book.getId() + "' updated"; + } + else { + response.status(404); // 404 Not found + return "Book not found"; + } + }); + + // Deletes the book resource for the provided id + delete("/books/:id", (request, response) -> { + BookRecord book = ctx + .deleteFrom(BOOK) + .where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))) + .returning() + .fetchOne(); + + if (book != null) { + return "Book with id '" + book.getId() + "' deleted"; + } + else { + response.status(404); // 404 Not found + return "Book not found"; + } + }); + + // Gets all available book resources (id's) + get("/books", (request, response) -> { + return ctx + .select(BOOK.ID) + .from(BOOK) + .fetch(BOOK.ID) + .stream() + .map(Object::toString) + .collect(Collectors.joining(" ")); + }); + } + + private static AuthorRecord upsertAuthor(final DSLContext ctx, Request request) { + AuthorRecord author = ctx + .selectFrom(AUTHOR) + .where(AUTHOR.NAME.eq(request.queryParams("author"))) + .fetchOne(); + + if (author == null) + author = ctx.newRecord(AUTHOR); + + author.setName(request.queryParams("author")); + author.store(); + + return author; + } +} \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/LICENSE.txt b/jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/LICENSE.txt new file mode 100644 index 0000000000..d253ed643b --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) +All rights reserved. + +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 \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/README.txt b/jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/README.txt new file mode 100644 index 0000000000..0ba2c43dd5 --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/src/main/resources/META-INF/README.txt @@ -0,0 +1,2 @@ +Thanks for downloading jOOQ. +Please visit http://www.jooq.org for more information. diff --git a/jOOQ-examples/jOOQ-spark-example/src/main/resources/config.properties b/jOOQ-examples/jOOQ-spark-example/src/main/resources/config.properties new file mode 100644 index 0000000000..a4627ea8a7 --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/src/main/resources/config.properties @@ -0,0 +1,11 @@ +#Database Configuration +db.driver=org.h2.Driver +db.url=jdbc:h2:~/jooq-spark-example +db.username=sa +db.password= + +#jOOQ Configuration +jooq.sql.dialect=H2 + +#DB Schema +db.schema.script=db-h2.sql \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/src/main/resources/db-h2.sql b/jOOQ-examples/jOOQ-spark-example/src/main/resources/db-h2.sql new file mode 100644 index 0000000000..bfe252bb76 --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/src/main/resources/db-h2.sql @@ -0,0 +1,26 @@ +DROP TABLE IF EXISTS book; +DROP TABLE IF EXISTS author; + +CREATE TABLE author ( + id INT NOT NULL AUTO_INCREMENT, + name VARCHAR(50), + + CONSTRAINT pk_t_author PRIMARY KEY (ID) +); + +CREATE TABLE book ( + id INT NOT NULL AUTO_INCREMENT, + author_id INT NOT NULL, + title VARCHAR(400) NOT NULL, + + CONSTRAINT pk_t_book PRIMARY KEY (id), + CONSTRAINT fk_t_book_author_id FOREIGN KEY (author_id) REFERENCES author(id) ON DELETE CASCADE, +); + +INSERT INTO author VALUES (DEFAULT, 'George Orwell'); +INSERT INTO author VALUES (DEFAULT, 'Paulo Coelho'); + +INSERT INTO book VALUES (DEFAULT, 1, '1984'); +INSERT INTO book VALUES (DEFAULT, 1, 'Animal Farm'); +INSERT INTO book VALUES (DEFAULT, 2, 'O Alquimista'); +INSERT INTO book VALUES (DEFAULT, 2, 'Brida'); \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spark-example/src/main/resources/log4j.xml b/jOOQ-examples/jOOQ-spark-example/src/main/resources/log4j.xml new file mode 100644 index 0000000000..8c478c982b --- /dev/null +++ b/jOOQ-examples/jOOQ-spark-example/src/main/resources/log4j.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +