TableViewtemporarySourceTableView=createTemporarySessionView(command.getQueryAlias(),querySQLOutput[0],columnTemplateList,false/*no recursion*/,false/* do not add to session*/);
TableViewtemporarySourceTableView=createTemporarySessionView(command.getQueryAlias(),querySQLOutput[0],columnTemplateList,false/*no recursion*/,false/* do not add to session*/);
privatestaticfinalStringGATHER_ORDERED_RESULTS_SQL="SELECT ID, NAME FROM PARENT ORDER BY ID ASC";
privatestaticfinalStringGATHER_ORDERED_RESULTS_SQL="SELECT ID, NAME FROM PARENT ORDER BY ID ASC";
privatestaticinttriggerTestingUpdateCount=0;
privatestaticinttriggerTestingUpdateCount=0;
...
@@ -34,7 +34,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -34,7 +34,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
@Override
@Override
publicvoidtest()throwsException{
publicvoidtest()throwsException{
// Simple ID,NAME inserts, target table with PK initially empty
// Simple ID,NAME inserts, target table with PK initially empty
testMergeUsing(
testMergeUsing(
"CREATE TABLE PARENT(ID INT, NAME VARCHAR, PRIMARY KEY(ID) );",
"CREATE TABLE PARENT(ID INT, NAME VARCHAR, PRIMARY KEY(ID) );",
...
@@ -117,8 +117,8 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -117,8 +117,8 @@ public class TestMergeUsing extends TestBase implements Trigger {
GATHER_ORDERED_RESULTS_SQL,
GATHER_ORDERED_RESULTS_SQL,
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
3
3
);
);
// Only insert clause, no update or delete clauses
// Only insert clause, no update or delete clauses
testMergeUsing(
testMergeUsing(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,1) );"+
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,1) );"+
...
@@ -146,8 +146,8 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -146,8 +146,8 @@ public class TestMergeUsing extends TestBase implements Trigger {
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,1) WHERE X<0",
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,1) WHERE X<0",
2
2
);
);
// Duplicate source keys but different ROWID update - so no error
// Duplicate source keys but different ROWID update - so no error
// SQL standard says duplicate or repeated updates of same row in same statement should cause errors - but because first row is updated, deleted (on source row 1) then inserted (on source row 2)
// SQL standard says duplicate or repeated updates of same row in same statement should cause errors - but because first row is updated, deleted (on source row 1) then inserted (on source row 2)
// it's considered different - with respect to to ROWID - so no error
// it's considered different - with respect to to ROWID - so no error
// One insert, one update one delete happens (on same row) , target table missing PK, no source or target alias
// One insert, one update one delete happens (on same row) , target table missing PK, no source or target alias
testMergeUsing(
testMergeUsing(
...
@@ -158,7 +158,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -158,7 +158,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
"SELECT 1 AS ID, 'Marcy'||X||X UNION ALL SELECT 1 AS ID, 'Marcy2'",
"SELECT 1 AS ID, 'Marcy'||X||X UNION ALL SELECT 1 AS ID, 'Marcy2'",
2
2
);
);
// Multiple update on same row: SQL standard says duplicate or repeated updates in same statement should cause errors -but because first row is updated, delete then insert it's considered different
// Multiple update on same row: SQL standard says duplicate or repeated updates in same statement should cause errors -but because first row is updated, delete then insert it's considered different
// One insert, one update one delete happens (on same row, which is okay), then another update (which is illegal)target table missing PK, no source or target alias
// One insert, one update one delete happens (on same row, which is okay), then another update (which is illegal)target table missing PK, no source or target alias
testMergeUsingException(
testMergeUsingException(
...
@@ -179,7 +179,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -179,7 +179,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
3,
3,
"Duplicate key updated 3 rows at once, only 1 expected"
"Duplicate key updated 3 rows at once, only 1 expected"
);
);
// Missing target columns in ON expression
// Missing target columns in ON expression
testMergeUsingException(
testMergeUsingException(
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"+
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"+
...
@@ -189,7 +189,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -189,7 +189,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
3,
3,
"No references to target columns found in ON clause"
"No references to target columns found in ON clause"
);
);
// Missing source columns in ON expression
// Missing source columns in ON expression
testMergeUsingException(
testMergeUsingException(
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"+
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"+
...
@@ -199,7 +199,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -199,7 +199,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
3,
3,
"No references to source columns found in ON clause"
"No references to source columns found in ON clause"
);
);
// Insert does not insert correct values with respect to ON condition (inserts ID value above 100, instead)
// Insert does not insert correct values with respect to ON condition (inserts ID value above 100, instead)
testMergeUsingException(
testMergeUsingException(
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(4,4) );"+
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(4,4) );"+
...
@@ -209,7 +209,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -209,7 +209,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(4,4)",
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(4,4)",
1,
1,
"Expected to find key after row inserted, but none found. Insert does not match ON condition."
"Expected to find key after row inserted, but none found. Insert does not match ON condition."
);
);
// One insert, one update one delete happens, target table missing PK, triggers update all NAME fields
// One insert, one update one delete happens, target table missing PK, triggers update all NAME fields
triggerTestingUpdateCount=0;
triggerTestingUpdateCount=0;
testMergeUsing(
testMergeUsing(
...
@@ -219,7 +219,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -219,7 +219,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
GATHER_ORDERED_RESULTS_SQL,
GATHER_ORDERED_RESULTS_SQL,
"SELECT 2 AS ID, 'Marcy22-updated2' AS NAME UNION ALL SELECT X AS ID, 'Marcy'||X||'-inserted'||X AS NAME FROM SYSTEM_RANGE(3,4)",
"SELECT 2 AS ID, 'Marcy22-updated2' AS NAME UNION ALL SELECT X AS ID, 'Marcy'||X||'-inserted'||X AS NAME FROM SYSTEM_RANGE(3,4)",
4
4
);
);
}
}
/**
/**
...
@@ -243,20 +243,20 @@ public class TestMergeUsing extends TestBase implements Trigger {
...
@@ -243,20 +243,20 @@ public class TestMergeUsing extends TestBase implements Trigger {
try{
try{
stat=conn.createStatement();
stat=conn.createStatement();
stat.execute(setupSQL);
stat.execute(setupSQL);
prep=conn.prepareStatement(statementUnderTest);
prep=conn.prepareStatement(statementUnderTest);
rowCountUpdate=prep.executeUpdate();
rowCountUpdate=prep.executeUpdate();
// compare actual results from SQL resultsset with expected results - by diffing (aka set MINUS operation)
// compare actual results from SQL resultset with expected results - by diffing (aka set MINUS operation)
rs=stat.executeQuery("( "+gatherResultsSQL+" ) MINUS ( "+expectedResultsSQL+" )");
rs=stat.executeQuery("( "+gatherResultsSQL+" ) MINUS ( "+expectedResultsSQL+" )");