From 243ab8700bff2cdcd083a68229ef2cef6d300cae Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 30 Aug 2011 22:34:35 +0000 Subject: [PATCH] [#813] Add DSL support for INSERT .. RETURNING --- .../src/org/jooq/test/jOOQAbstractTest.java | 21 ++++ .../java/org/jooq/InsertOnDuplicateStep.java | 2 +- jOOQ/src/main/java/org/jooq/InsertQuery.java | 7 +- .../main/java/org/jooq/InsertResultStep.java | 79 +++++++++++++++ .../java/org/jooq/InsertReturningStep.java | 99 +++++++++++++++++++ .../main/java/org/jooq/impl/InsertImpl.java | 28 +++++- 6 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/InsertResultStep.java create mode 100644 jOOQ/src/main/java/org/jooq/InsertReturningStep.java diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index 4c3ba48699..12b43167a4 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -3563,6 +3563,7 @@ public abstract class jOOQAbstractTest< assertEquals(0, create().fetch(T785()).size()); } + @SuppressWarnings("unchecked") @Test public void testInsertReturning() throws Exception { if (TTriggers() == null) { @@ -3617,6 +3618,26 @@ public abstract class jOOQAbstractTest< // TODO [#813] DSL querying // ------------------------ + TableRecord returned = (TableRecord) + create().insertInto(TTriggers(), TTriggers_COUNTER()) + .values(0) + .returning() + .fetchOne(); + assertNotNull(returned); + assertEquals(++ID, (int) returned.getValue(TTriggers_ID())); + assertEquals(2*ID, (int) returned.getValue(TTriggers_COUNTER())); + + returned = (TableRecord) + create().insertInto(TTriggers(), TTriggers_COUNTER()) + .values(0) + .returning(TTriggers_ID()) + .fetchOne(); + assertNotNull(returned); + assertEquals(++ID, (int) returned.getValue(TTriggers_ID())); + assertNull(returned.getValue(TTriggers_COUNTER())); + + returned.refreshUsing(TTriggers_ID()); + assertEquals(2*ID, (int) returned.getValue(TTriggers_COUNTER())); // store() and similar methods T triggered = create().newRecord(TTriggers()); diff --git a/jOOQ/src/main/java/org/jooq/InsertOnDuplicateStep.java b/jOOQ/src/main/java/org/jooq/InsertOnDuplicateStep.java index 9d137efbb0..fcb6de309f 100644 --- a/jOOQ/src/main/java/org/jooq/InsertOnDuplicateStep.java +++ b/jOOQ/src/main/java/org/jooq/InsertOnDuplicateStep.java @@ -54,7 +54,7 @@ import org.jooq.impl.Factory; * * @author Lukas Eder */ -public interface InsertOnDuplicateStep extends InsertFinalStep { +public interface InsertOnDuplicateStep extends InsertFinalStep, InsertReturningStep { /** * Add an ON DUPLICATE KEY UPDATE clause to this insert query. diff --git a/jOOQ/src/main/java/org/jooq/InsertQuery.java b/jOOQ/src/main/java/org/jooq/InsertQuery.java index 23f430d16c..5cc32c3aa5 100644 --- a/jOOQ/src/main/java/org/jooq/InsertQuery.java +++ b/jOOQ/src/main/java/org/jooq/InsertQuery.java @@ -156,9 +156,12 @@ public interface InsertQuery> extends StoreQuery, In * column values as "generated key". If other fields are requested, a second * statement is issued. Client code must assure transactional integrity * between the two statements. + *
  • Sybase and SQLite allow for retrieving IDENTITY values as + * @@identity or last_inserted_rowid() values. + * Those values are fetched in a separate SELECT statement. If + * other fields are requested, a second statement is issued. Client code + * must assure transactional integrity between the two statements.
  • *
  • Ingres support will be added with #808
  • - *
  • Sybase support will be added with #809
  • - *
  • SQLite support will be added with #810
  • * */ R getReturned(); diff --git a/jOOQ/src/main/java/org/jooq/InsertResultStep.java b/jOOQ/src/main/java/org/jooq/InsertResultStep.java new file mode 100644 index 0000000000..fec4095cca --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/InsertResultStep.java @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq; + +import java.sql.SQLException; + +/** + * This type is used for the {@link Insert}'s DSL API. + *

    + * Example:

    + * Factory create = new Factory();
    + *
    + * TableRecord record =
    + * create.insertInto(table, field1, field2)
    + *       .values(value1, value2)
    + *       .returning(field1)
    + *       .fetchOne();
    + * 
    + *

    + * This implemented differently for every dialect: + *

      + *
    • Postgres has native support for INSERT .. RETURNING clauses
    • + *
    • HSQLDB, Oracle, and DB2 JDBC drivers allow for retrieving any table + * column as "generated key" in one statement
    • + *
    • Derby, H2, MySQL, SQL Server only allow for retrieving IDENTITY column + * values as "generated key". If other fields are requested, a second statement + * is issued. Client code must assure transactional integrity between the two + * statements.
    • + *
    • Sybase and SQLite allow for retrieving IDENTITY values as + * @@identity or last_inserted_rowid() values. Those + * values are fetched in a separate SELECT statement. If other + * fields are requested, a second statement is issued. Client code must assure + * transactional integrity between the two statements.
    • + *
    • Ingres support will be added with #808
    • + *
    + * + * @author Lukas Eder + */ +public interface InsertResultStep extends Insert { + + /** + * The record holding returned values as specified by the + * {@link InsertReturningStep} + */ + TableRecord fetchOne() throws SQLException; +} diff --git a/jOOQ/src/main/java/org/jooq/InsertReturningStep.java b/jOOQ/src/main/java/org/jooq/InsertReturningStep.java new file mode 100644 index 0000000000..a50141a5ec --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/InsertReturningStep.java @@ -0,0 +1,99 @@ +/** + * Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq; + +import java.util.Collection; + +/** + * This type is used for the {@link Insert}'s DSL API. + *

    + * Example:

    + * Factory create = new Factory();
    + *
    + * TableRecord record =
    + * create.insertInto(table, field1, field2)
    + *       .values(value1, value2)
    + *       .returning(field1)
    + *       .fetchOne();
    + * 
    + *

    + * This implemented differently for every dialect: + *

      + *
    • Postgres has native support for INSERT .. RETURNING clauses
    • + *
    • HSQLDB, Oracle, and DB2 JDBC drivers allow for retrieving any table + * column as "generated key" in one statement
    • + *
    • Derby, H2, MySQL, SQL Server only allow for retrieving IDENTITY column + * values as "generated key". If other fields are requested, a second statement + * is issued. Client code must assure transactional integrity between the two + * statements.
    • + *
    • Sybase and SQLite allow for retrieving IDENTITY values as + * @@identity or last_inserted_rowid() values. Those + * values are fetched in a separate SELECT statement. If other + * fields are requested, a second statement is issued. Client code must assure + * transactional integrity between the two statements.
    • + *
    • Ingres support will be added with #808
    • + *
    + * + * @author Lukas Eder + */ +public interface InsertReturningStep { + + /** + * Configure the INSERT statement to return all fields in + * R. + * + * @see InsertResultStep + */ + InsertResultStep returning(); + + /** + * Configure the INSERT statement to return a list of fields in + * R. + * + * @param fields Fields to be returned + * @see InsertResultStep + */ + InsertResultStep returning(Field... fields); + + /** + * Configure the INSERT statement to return a list of fields in + * R. + * + * @param fields Fields to be returned + * @see InsertResultStep + */ + InsertResultStep returning(Collection> fields); +} diff --git a/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java b/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java index 0589cd0acd..657157cf46 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InsertImpl.java @@ -49,6 +49,7 @@ import org.jooq.Configuration; import org.jooq.Field; import org.jooq.InsertOnDuplicateSetMoreStep; import org.jooq.InsertQuery; +import org.jooq.InsertResultStep; import org.jooq.InsertSetMoreStep; import org.jooq.InsertSetStep; import org.jooq.InsertValuesStep; @@ -65,7 +66,8 @@ class InsertImpl> extends AbstractQueryPart implements // Cascading interface implementations for Insert behaviour InsertValuesStep, InsertSetMoreStep, - InsertOnDuplicateSetMoreStep { + InsertOnDuplicateSetMoreStep, + InsertResultStep { /** * Generated UID @@ -185,4 +187,28 @@ class InsertImpl> extends AbstractQueryPart implements delegate.newRecord(); return this; } + + @Override + public final InsertResultStep returning() { + delegate.setReturning(); + return this; + } + + @Override + public final InsertResultStep returning(Field... f) { + delegate.setReturning(f); + return this; + } + + @Override + public final InsertResultStep returning(Collection> f) { + delegate.setReturning(f); + return this; + } + + @Override + public final TableRecord fetchOne() throws SQLException { + delegate.execute(); + return delegate.getReturned(); + } }