From b3f7dbb4a669e1e1d875d7014cb47c61a61b31ac Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 4 Jan 2012 21:13:16 +0000 Subject: [PATCH] [#1010] The MERGE INTO .. WHEN NOT MATCHED THEN INSERT .. syntax may cause type-safety issues in some databases. VALUES should be converted before binding --- .../src/org/jooq/test/jOOQAbstractTest.java | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index dffd1e62ba..c60ddf8857 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -4492,6 +4492,7 @@ public abstract class jOOQAbstractTest< reset = false; // Always do an update of everything + // -------------------------------- create().mergeInto(TAuthor()) .using(create().selectOne()) .on("1 = 1") @@ -4507,6 +4508,7 @@ public abstract class jOOQAbstractTest< .fetch(TAuthor_FIRST_NAME())); // Always do an update of the first author + // -------------------------------- create().mergeInto(TAuthor()) .using(create().selectOne()) .on(TAuthor_ID().equal(1)) @@ -4517,14 +4519,15 @@ public abstract class jOOQAbstractTest< .execute(); assertEquals(Arrays.asList("John", "Alfred"), - create().selectFrom(TAuthor()) - .orderBy(TAuthor_ID()) - .fetch(TAuthor_FIRST_NAME())); + create().selectFrom(TAuthor()) + .orderBy(TAuthor_ID()) + .fetch(TAuthor_FIRST_NAME())); 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)) @@ -4536,18 +4539,53 @@ public abstract class jOOQAbstractTest< .set(TAuthor_FIRST_NAME(), f) .set(TAuthor_LAST_NAME(), l); + // Execute an insert q.execute(); assertEquals(Arrays.asList("John", "Alfred", "Dan"), create().selectFrom(TAuthor()) .orderBy(TAuthor_ID()) .fetch(TAuthor_FIRST_NAME())); + // Execute an update q.execute(); assertEquals(Arrays.asList("John", "Alfred", "James"), create().selectFrom(TAuthor()) .orderBy(TAuthor_ID()) .fetch(TAuthor_FIRST_NAME())); + f = val("Herman").as("f"); + l = val("Hesse").as("l"); + + // Check if INSERT-only MERGE works + // -------------------------------- + q = + create().mergeInto(TAuthor()) + .using(create().select(f, l)) + .on(TAuthor_LAST_NAME().equal(l)) + .whenNotMatchedThenInsert( + TAuthor_ID(), + TAuthor_FIRST_NAME(), + TAuthor_LAST_NAME(), + TAuthor_DATE_OF_BIRTH()) + + // [#1010] Be sure that this type-unsafe clause can deal with + // any convertable type + .values("4", f, l, 0L); + + // Execute an insert + q.execute(); + assertEquals(Arrays.asList("John", "Alfred", "James", "Herman"), + create().selectFrom(TAuthor()) + .orderBy(TAuthor_ID()) + .fetch(TAuthor_FIRST_NAME())); + + // Execute nothing + q.execute(); + assertEquals(Arrays.asList("John", "Alfred", "James", "Herman"), + create().selectFrom(TAuthor()) + .orderBy(TAuthor_ID()) + .fetch(TAuthor_FIRST_NAME())); + // TODO: Add more sophisticated MERGE statement tests // Especially for SQL Server and Sybase, some bugs could be expected }