From 22b52d23df42ea33c04096682e97df6186f5d9a7 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sun, 18 Dec 2011 18:09:37 +0000 Subject: [PATCH] [#1000] Add support for MySQL INSERT INTO .. SET .. syntax in MERGE statement's WHEN NOT MATCHED THEN INSERT clause --- .../src/org/jooq/test/jOOQAbstractTest.java | 7 +- .../org/jooq/MergeNotMatchedSetMoreStep.java | 62 ++++++++++++++ .../java/org/jooq/MergeNotMatchedSetStep.java | 82 +++++++++++++++++++ .../java/org/jooq/MergeNotMatchedStep.java | 11 +++ .../main/java/org/jooq/impl/MergeImpl.java | 34 ++++++-- 5 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/MergeNotMatchedSetMoreStep.java create mode 100644 jOOQ/src/main/java/org/jooq/MergeNotMatchedSetStep.java diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index 4c06c75bbf..35431c4b36 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -4434,14 +4434,17 @@ public abstract class jOOQAbstractTest< Field f = val("Dan").as("f"); Field l = val("Brown").as("l"); + // [#1000] Add a check for the alternative INSERT .. SET .. syntax MergeFinalStep q = create().mergeInto(TAuthor()) .using(create().select(f, l)) .on(TAuthor_LAST_NAME().equal(l)) .whenMatchedThenUpdate() .set(TAuthor_FIRST_NAME(), "James") - .whenNotMatchedThenInsert(TAuthor_ID(), TAuthor_FIRST_NAME(), TAuthor_LAST_NAME()) - .values(3, f, l); + .whenNotMatchedThenInsert() + .set(TAuthor_ID(), 3) + .set(TAuthor_FIRST_NAME(), f) + .set(TAuthor_LAST_NAME(), l); q.execute(); assertEquals(Arrays.asList("John", "Alfred", "Dan"), diff --git a/jOOQ/src/main/java/org/jooq/MergeNotMatchedSetMoreStep.java b/jOOQ/src/main/java/org/jooq/MergeNotMatchedSetMoreStep.java new file mode 100644 index 0000000000..8f2be38904 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/MergeNotMatchedSetMoreStep.java @@ -0,0 +1,62 @@ +/** + * 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; + +/** + * This type is used for the {@link Merge}'s DSL API. + *

+ * Example:

+ * Factory create = new Factory();
+ *
+ * create.mergeInto(table)
+ *       .using(select)
+ *       .on(condition)
+ *       .whenMatchedThenUpdate()
+ *       .set(field1, value1)
+ *       .set(field2, value2)
+ *       .whenNotMatchedThenInsert(field1, field2)
+ *       .values(value1, value2)
+ *       .execute();
+ * 
+ * + * @author Lukas Eder + */ +public interface MergeNotMatchedSetMoreStep + extends + MergeNotMatchedSetStep, + MergeNotMatchedWhereStep { + +} diff --git a/jOOQ/src/main/java/org/jooq/MergeNotMatchedSetStep.java b/jOOQ/src/main/java/org/jooq/MergeNotMatchedSetStep.java new file mode 100644 index 0000000000..92be0df398 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/MergeNotMatchedSetStep.java @@ -0,0 +1,82 @@ +/** + * 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.Map; + +/** + * This type is used for the {@link Merge}'s DSL API. + *

+ * Example:

+ * Factory create = new Factory();
+ *
+ * create.mergeInto(table)
+ *       .using(select)
+ *       .on(condition)
+ *       .whenMatchedThenUpdate()
+ *       .set(field1, value1)
+ *       .set(field2, value2)
+ *       .whenNotMatchedThenInsert(field1, field2)
+ *       .values(value1, value2)
+ *       .execute();
+ * 
+ * + * @author Lukas Eder + */ +public interface MergeNotMatchedSetStep { + + /** + * Set values for INSERT in the MERGE statement's + * WHEN NOT MATCHED clause + */ + MergeNotMatchedSetMoreStep set(Field field, T value); + + /** + * Set values for INSERT in the MERGE statement's + * WHEN NOT MATCHED clause + */ + MergeNotMatchedSetMoreStep set(Field field, Field value); + + /** + * Set multiple values for INSERT in the MERGE + * statement's WHEN NOT MATCHED clause. + *

+ * Please assure that key/value pairs have matching <T> + * types. Values can either be of type <T> or + * Field<T> + */ + MergeNotMatchedSetMoreStep set(Map, ?> map); +} diff --git a/jOOQ/src/main/java/org/jooq/MergeNotMatchedStep.java b/jOOQ/src/main/java/org/jooq/MergeNotMatchedStep.java index 83d3250dd6..3d93669b03 100644 --- a/jOOQ/src/main/java/org/jooq/MergeNotMatchedStep.java +++ b/jOOQ/src/main/java/org/jooq/MergeNotMatchedStep.java @@ -58,6 +58,17 @@ import java.util.Collection; */ public interface MergeNotMatchedStep extends MergeFinalStep { + /** + * Add the WHEN NOT MATCHED THEN INSERT clause to the + * MERGE statement. + *

+ * Unlike the {@link #whenNotMatchedThenInsert(Field...)} and + * {@link #whenNotMatchedThenInsert(Collection)} methods, this will give + * access to a MySQL-like API allowing for + * INSERT SET a = x, b = y syntax. + */ + MergeNotMatchedSetStep whenNotMatchedThenInsert(); + /** * Add the WHEN NOT MATCHED THEN INSERT clause to the * MERGE statement diff --git a/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java b/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java index 4588f5556e..b3629724e2 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java @@ -44,6 +44,7 @@ import static org.jooq.impl.Factory.vals; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -54,8 +55,8 @@ import org.jooq.Configuration; import org.jooq.Field; import org.jooq.MergeMatchedDeleteStep; import org.jooq.MergeMatchedSetMoreStep; +import org.jooq.MergeNotMatchedSetMoreStep; import org.jooq.MergeNotMatchedValuesStep; -import org.jooq.MergeNotMatchedWhereStep; import org.jooq.MergeOnConditionStep; import org.jooq.MergeOnStep; import org.jooq.MergeUsingStep; @@ -79,8 +80,8 @@ implements MergeOnConditionStep, MergeMatchedSetMoreStep, MergeMatchedDeleteStep, - MergeNotMatchedValuesStep, - MergeNotMatchedWhereStep { + MergeNotMatchedSetMoreStep, + MergeNotMatchedValuesStep { /** * Generated UID @@ -219,16 +220,39 @@ implements @Override public final MergeImpl set(Field field, Field value) { - matchedUpdate.put(field, nullSafe(value)); + if (matchedClause) { + matchedUpdate.put(field, nullSafe(value)); + } + else if (notMatchedClause) { + notMatchedInsert.put(field, nullSafe(value)); + } + else { + throw new IllegalStateException("Cannot call where() on the current state of the MERGE statement"); + } + return this; } @Override public final MergeImpl set(Map, ?> map) { - matchedUpdate.set(map); + if (matchedClause) { + matchedUpdate.set(map); + } + else if (notMatchedClause) { + notMatchedInsert.set(map); + } + else { + throw new IllegalStateException("Cannot call where() on the current state of the MERGE statement"); + } + return this; } + @Override + public final MergeImpl whenNotMatchedThenInsert() { + return whenNotMatchedThenInsert(Collections.>emptyList()); + } + @Override public final MergeImpl whenNotMatchedThenInsert(Field... fields) { return whenNotMatchedThenInsert(Arrays.asList(fields));