[#1061] Support "DEFAULT" keyword in INSERT and UPDATE statements

This commit is contained in:
Lukas Eder 2014-05-07 11:42:26 +02:00
parent dc2fd7f1ea
commit b27d73c390
3 changed files with 81 additions and 0 deletions

View File

@ -41,6 +41,7 @@
package org.jooq.test._.testcases;
import static java.util.Arrays.asList;
import static java.util.Collections.nCopies;
// ...
// ...
import static org.jooq.SQLDialect.CUBRID;
@ -60,6 +61,7 @@ import static org.jooq.impl.DSL.castNull;
import static org.jooq.impl.DSL.concat;
import static org.jooq.impl.DSL.count;
import static org.jooq.impl.DSL.decode;
import static org.jooq.impl.DSL.defaultValue;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.fieldByName;
import static org.jooq.impl.DSL.inline;
@ -245,6 +247,43 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertEquals(1, create().delete(TTriggers()).execute());
}
public void testInsertDefaultValue() throws Exception {
/* [pro] xx
xx xxxxxxxxxxxxxxxxxxx xx xxxxxxx x
xxxxxxxxxxxxxxxxxxxx xxxxxxxx xxxxxx xxxxxxx
xxxxxxx
x
xx [/pro] */
jOOQAbstractTest.reset = false;
assertEquals(1,
create().insertInto(TBook())
.set(newBook(5))
.set(TBook_LANGUAGE_ID(), defaultValue(int.class))
.execute());
assertEquals(1, (int) create().fetchOne(TBook(), TBook_ID().eq(5)).getValue(TBook_LANGUAGE_ID()));
}
public void testUpdateDefaultValue() throws Exception {
/* [pro] xx
xx xxxxxxxxxxxxxxxxxxx xx xxxxxxx x
xxxxxxxxxxxxxxxxxxxx xxxxxxxx xxxxxx xxxxxxx
xxxxxxx
x
xx [/pro] */
jOOQAbstractTest.reset = false;
assertEquals(4,
create().update(TBook())
.set(TBook_LANGUAGE_ID(), defaultValue(int.class))
.execute());
assertEquals(nCopies(4, 1), create().fetch(TBook()).getValues(TBook_LANGUAGE_ID()));
}
public void testInsertImplicit() throws Exception {
jOOQAbstractTest.reset = false;

View File

@ -1033,6 +1033,16 @@ public abstract class jOOQAbstractTest<
new InsertUpdateTests(this).testInsertDefaultValues();
}
@Test
public void testInsertDefaultValue() throws Exception {
new InsertUpdateTests(this).testInsertDefaultValue();
}
@Test
public void testUpdateDefaultValue() throws Exception {
new InsertUpdateTests(this).testUpdateDefaultValue();
}
@Test
public void testTableMapping() throws Exception {
new SchemaAndMappingTests(this).testTableMapping();

View File

@ -5013,6 +5013,38 @@ public class DSL {
// XXX SQL identifiers
// -------------------------------------------------------------------------
/**
* Create a <code>DEFAULT</code> keyword for use with <code>INSERT</code>,
* <code>UPDATE</code>, or <code>MERGE</code> statements.
* <p>
* While the <code>DEFAULT</code> keyword works with all data types, you may
* still prefer to associate a {@link Field} type with your
* <code>DEFAULT</code> value. In that case, use
* {@link #defaultValue(Class)} or {@link #defaultValue(DataType)} instead.
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
public static Field<Object> defaultValue() {
return defaultValue(Object.class);
}
/**
* Create a <code>DEFAULT</code> keyword for use with <code>INSERT</code>,
* <code>UPDATE</code>, or <code>MERGE</code> statements.
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
public static <T> Field<T> defaultValue(Class<T> type) {
return defaultValue(getDataType(type));
}
/**
* Create a <code>DEFAULT</code> keyword for use with <code>INSERT</code>,
* <code>UPDATE</code>, or <code>MERGE</code> statements.
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
public static <T> Field<T> defaultValue(DataType<T> type) {
return new SQLField<T>(type, keyword("default"));
}
/**
* Create a new SQL identifier using a qualified name.
* <p>