[#1000] Add support for MySQL INSERT INTO .. SET .. syntax in MERGE statement's WHEN NOT MATCHED THEN INSERT clause
This commit is contained in:
parent
b88ce315f5
commit
22b52d23df
@ -4434,14 +4434,17 @@ public abstract class jOOQAbstractTest<
|
||||
Field<String> f = val("Dan").as("f");
|
||||
Field<String> l = val("Brown").as("l");
|
||||
|
||||
// [#1000] Add a check for the alternative INSERT .. SET .. syntax
|
||||
MergeFinalStep<A> 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"),
|
||||
|
||||
62
jOOQ/src/main/java/org/jooq/MergeNotMatchedSetMoreStep.java
Normal file
62
jOOQ/src/main/java/org/jooq/MergeNotMatchedSetMoreStep.java
Normal file
@ -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.
|
||||
* <p>
|
||||
* Example: <code><pre>
|
||||
* 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();
|
||||
* </pre></code>
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface MergeNotMatchedSetMoreStep<R extends Record>
|
||||
extends
|
||||
MergeNotMatchedSetStep<R>,
|
||||
MergeNotMatchedWhereStep<R> {
|
||||
|
||||
}
|
||||
82
jOOQ/src/main/java/org/jooq/MergeNotMatchedSetStep.java
Normal file
82
jOOQ/src/main/java/org/jooq/MergeNotMatchedSetStep.java
Normal file
@ -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.
|
||||
* <p>
|
||||
* Example: <code><pre>
|
||||
* 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();
|
||||
* </pre></code>
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface MergeNotMatchedSetStep<R extends Record> {
|
||||
|
||||
/**
|
||||
* Set values for <code>INSERT</code> in the <code>MERGE</code> statement's
|
||||
* <code>WHEN NOT MATCHED</code> clause
|
||||
*/
|
||||
<T> MergeNotMatchedSetMoreStep<R> set(Field<T> field, T value);
|
||||
|
||||
/**
|
||||
* Set values for <code>INSERT</code> in the <code>MERGE</code> statement's
|
||||
* <code>WHEN NOT MATCHED</INSERT> clause
|
||||
*/
|
||||
<T> MergeNotMatchedSetMoreStep<R> set(Field<T> field, Field<T> value);
|
||||
|
||||
/**
|
||||
* Set multiple values for <code>INSERT</code> in the <code>MERGE</code>
|
||||
* statement's <code>WHEN NOT MATCHED</code> clause.
|
||||
* <p>
|
||||
* Please assure that key/value pairs have matching <code><T></code>
|
||||
* types. Values can either be of type <code><T></code> or
|
||||
* <code>Field<T></code>
|
||||
*/
|
||||
MergeNotMatchedSetMoreStep<R> set(Map<? extends Field<?>, ?> map);
|
||||
}
|
||||
@ -58,6 +58,17 @@ import java.util.Collection;
|
||||
*/
|
||||
public interface MergeNotMatchedStep<R extends Record> extends MergeFinalStep<R> {
|
||||
|
||||
/**
|
||||
* Add the <code>WHEN NOT MATCHED THEN INSERT</code> clause to the
|
||||
* <code>MERGE</code> statement.
|
||||
* <p>
|
||||
* Unlike the {@link #whenNotMatchedThenInsert(Field...)} and
|
||||
* {@link #whenNotMatchedThenInsert(Collection)} methods, this will give
|
||||
* access to a MySQL-like API allowing for
|
||||
* <code>INSERT SET a = x, b = y</code> syntax.
|
||||
*/
|
||||
MergeNotMatchedSetStep<R> whenNotMatchedThenInsert();
|
||||
|
||||
/**
|
||||
* Add the <code>WHEN NOT MATCHED THEN INSERT</code> clause to the
|
||||
* <code>MERGE</code> statement
|
||||
|
||||
@ -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<R>,
|
||||
MergeMatchedSetMoreStep<R>,
|
||||
MergeMatchedDeleteStep<R>,
|
||||
MergeNotMatchedValuesStep<R>,
|
||||
MergeNotMatchedWhereStep<R> {
|
||||
MergeNotMatchedSetMoreStep<R>,
|
||||
MergeNotMatchedValuesStep<R> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
@ -219,16 +220,39 @@ implements
|
||||
|
||||
@Override
|
||||
public final <T> MergeImpl<R> set(Field<T> field, Field<T> 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<R> set(Map<? extends Field<?>, ?> 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<R> whenNotMatchedThenInsert() {
|
||||
return whenNotMatchedThenInsert(Collections.<Field<?>>emptyList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final MergeImpl<R> whenNotMatchedThenInsert(Field<?>... fields) {
|
||||
return whenNotMatchedThenInsert(Arrays.asList(fields));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user