summaryrefslogtreecommitdiffstats
path: root/test/upsert4.test
blob: c4bcc0329d727b966bb24308972a0db135664e76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# 2018-04-17
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# Test cases for UPSERT

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix upsert4

foreach {tn sql} {
  1 { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c UNIQUE) }
  2 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE) }
  3 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE) WITHOUT ROWID}
} {
  reset_db
  execsql $sql

  do_execsql_test 1.$tn.0 {
    INSERT INTO t1 VALUES(1, NULL, 'one');
    INSERT INTO t1 VALUES(2, NULL, 'two');
    INSERT INTO t1 VALUES(3, NULL, 'three');
  }
  
  do_execsql_test 1.$tn.1 {
    INSERT INTO t1 VALUES(1, NULL, 'xyz') ON CONFLICT DO NOTHING;
    SELECT * FROM t1;
  } {
    1 {} one 2 {} two 3 {} three
  }
  
  do_execsql_test 1.$tn.2 {
    INSERT INTO t1 VALUES(4, NULL, 'two') ON CONFLICT DO NOTHING;
    SELECT * FROM t1;
  } {
    1 {} one 2 {} two 3 {} three
  }
  
  do_execsql_test 1.$tn.3 {
    INSERT INTO t1 VALUES(4, NULL, 'two') ON CONFLICT (c) DO UPDATE SET b = 1;
    SELECT * FROM t1;
  } {
    1 {} one 2 1 two 3 {} three
  }
  
  do_execsql_test 1.$tn.4 {
    INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a) DO UPDATE SET b=2;
    SELECT * FROM t1;
  } {1 {} one 2 2 two 3 {} three}

  do_catchsql_test 1.$tn.5 {
    INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a) 
      DO UPDATE SET c = 'one';
  } {1 {UNIQUE constraint failed: t1.c}}

  do_execsql_test 1.$tn.6 {
    SELECT * FROM t1;
  } {1 {} one 2 2 two 3 {} three}

  do_execsql_test 1.$tn.7 {
    INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a) 
      DO UPDATE SET (b, c) = (SELECT 'x', 'y');
    SELECT * FROM t1;
  } {1 {} one 2 x y 3 {} three}

  do_execsql_test 1.$tn.8 {
    INSERT INTO t1 VALUES(1, NULL, NULL) ON CONFLICT (a) 
      DO UPDATE SET (c, a) = ('four', 4);
    SELECT * FROM t1 ORDER BY 1;
  } {2 x y 3 {} three 4 {} four}
}

#-------------------------------------------------------------------------
# Test target analysis.
#
set rtbl(0) {0 {}}
set rtbl(1) {/1 .*failed.*/}
set rtbl(2) {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}

foreach {tn sql} {
  1 { 
      CREATE TABLE xyz(a INTEGER PRIMARY KEY, b, c, d);
      CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase);
  }

  2 { 
      CREATE TABLE xyz(a INT PRIMARY KEY, b, c, d);
      CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase);
  }

  3 { 
      CREATE TABLE xyz(a INT PRIMARY KEY, b, c, d) WITHOUT ROWID;
      CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase);
  }
} {
  reset_db
  execsql $sql
  do_execsql_test 2.$tn.1 {
    INSERT INTO xyz VALUES(10, 1, 1, 'one');
  }


  foreach {tn2 oc res} {
    1 "ON CONFLICT (b COLLATE nocase, c, d) DO NOTHING"   0
    2 "ON CONFLICT (b, c, d) DO NOTHING"                  0
    3 "ON CONFLICT (b, c COLLATE nocase, d) DO NOTHING"   2
    4 "ON CONFLICT (a) DO NOTHING"                        1
    5 "ON CONFLICT DO NOTHING"                            0
    6 "ON CONFLICT (b, c, d) WHERE a!=0 DO NOTHING"       0
    7 "ON CONFLICT (d, c, c) WHERE a!=0 DO NOTHING"       2
    8 "ON CONFLICT (b COLLATE nocase, c COLLATE nocase, d) DO NOTHING"   2
    9 "ON CONFLICT (b, c, d) WHERE b==45 DO NOTHING"      0
  } {

    do_catchsql_test 2.$tn.2.$tn2 "
      INSERT INTO xyz VALUES(11, 1, 1, 'one') $oc
    " $rtbl($res)
  }

  do_execsql_test 2.$tn.3 {
    SELECT * FROM xyz;
  } {10 1 1 one}
}

foreach {tn sql} {
  1 {
    CREATE TABLE abc(a INTEGER PRIMARY KEY, x, y);
    CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase);
  }
  2 {
    CREATE TABLE abc(a INT PRIMARY KEY, x, y);
    CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase);
  }
  3 { 
    CREATE TABLE abc(a INT PRIMARY KEY, x, y) WITHOUT ROWID;
    CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase);
  }
} {
  reset_db
  execsql $sql
  do_execsql_test 3.$tn.1 {
    INSERT INTO abc VALUES(1, 'one', 'two');
  }

  foreach {tn2 oc res} {
    1 "ON CONFLICT DO NOTHING"                             0
    2 "ON CONFLICT ('x' || x) DO NOTHING"                  0
    3 "ON CONFLICT (('x' || x) COLLATE nocase) DO NOTHING" 0
    4 "ON CONFLICT (('x' || x) COLLATE binary) DO NOTHING" 2
    5 "ON CONFLICT (x || 'x') DO NOTHING"                  2
    6 "ON CONFLICT ((('x' || x))) DO NOTHING"              0
  } {
    do_catchsql_test 3.$tn.2.$tn2 "
      INSERT INTO abc VALUES(2, 'one', NULL) $oc;
    " $rtbl($res)
  }

  do_execsql_test 3.$tn.3 {
    SELECT * FROM abc
  } {1 one two}
}

foreach {tn sql} {
  1 {
    CREATE TABLE abc(a INTEGER PRIMARY KEY, x, y);
    CREATE UNIQUE INDEX abc1 ON abc(x) WHERE y>0;
    CREATE UNIQUE INDEX abc2 ON abc(y) WHERE x='xyz' COLLATE nocase;
  }
} {
  reset_db
  execsql $sql
  do_execsql_test 4.$tn.1 {
    INSERT INTO abc VALUES(1, 'one', 1);
    INSERT INTO abc VALUES(2, 'two', 2);
    INSERT INTO abc VALUES(3, 'xyz', 3);
    INSERT INTO abc VALUES(4, 'XYZ', 4);
  }

  foreach {tn2 oc res} {
    1 "ON CONFLICT DO NOTHING"                                 0
    2 "ON CONFLICT(x) WHERE y>0 DO NOTHING"                    0
    3 "ON CONFLICT(x) DO NOTHING"                              2
    4 "ON CONFLICT(x) WHERE y>=0 DO NOTHING"                   2
    5 "ON CONFLICT(y) WHERE x='xyz' COLLATE nocase DO NOTHING" 1
  } {
    do_catchsql_test 4.$tn.2.$tn2 "
      INSERT INTO abc VALUES(5, 'one', 10) $oc
    " $rtbl($res)
  }

  do_execsql_test 4.$tn.3 {
    SELECT * FROM abc
  } {1 one 1 2 two 2 3 xyz 3 4 XYZ 4}

  foreach {tn2 oc res} {
    1 "ON CONFLICT DO NOTHING"                                 0
    2 "ON CONFLICT(y) WHERE x='xyz' COLLATE nocase DO NOTHING" 0
    3 "ON CONFLICT(y) WHERE x='xyz' COLLATE binary DO NOTHING" 2
    4 "ON CONFLICT(x) WHERE y>0 DO NOTHING"                    1
  } {
    do_catchsql_test 4.$tn.2.$tn2 "
      INSERT INTO abc VALUES(5, 'xYz', 3) $oc
    " $rtbl($res)
  }
}

do_catchsql_test 5.0 {
  CREATE TABLE w1(a INT PRIMARY KEY, x, y);
  CREATE UNIQUE INDEX w1expr ON w1(('x' || x));
  INSERT INTO w1 VALUES(2, 'one', NULL)
    ON CONFLICT (('x' || x) COLLATE nocase) DO NOTHING;
} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}}

#-------------------------------------------------------------------------
# Test that ON CONFLICT constraint processing occurs before any REPLACE
# constraint processing.
#
foreach {tn sql} {
  1 {
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c);
  }
  2 {
    CREATE TABLE t1(a INT PRIMARY KEY, b UNIQUE, c);
  }
  3 {
    CREATE TABLE t1(a INT PRIMARY KEY, b UNIQUE, c) WITHOUT ROWID;
  }
} {
  reset_db
  execsql $sql
  do_execsql_test 6.1.$tn {
    INSERT INTO t1 VALUES(1, 1, 'one');
    INSERT INTO t1 VALUES(2, 2, 'two');
    INSERT OR REPLACE INTO t1 VALUES(1, 2, 'two') ON CONFLICT(b) DO NOTHING;
    PRAGMA integrity_check;
  } {ok}
}

foreach {tn sql} {
  1 {
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c UNIQUE);
  }
} {
  reset_db
  execsql $sql

  do_execsql_test 6.2.$tn.1 {
    INSERT INTO t1 VALUES(1, 1, 1);
    INSERT INTO t1 VALUES(2, 2, 2);
  }

  do_execsql_test 6.2.$tn.2 {
    INSERT OR REPLACE INTO t1 VALUES(3, 1, 1) ON CONFLICT(b) DO NOTHING;
    SELECT * FROM t1;
    PRAGMA integrity_check;
  } {1 1 1 2 2 2 ok}

  do_execsql_test 6.2.$tn.3 {
    INSERT OR REPLACE INTO t1 VALUES(3, 2, 2) ON CONFLICT(c) DO NOTHING;
    SELECT * FROM t1;
    PRAGMA integrity_check;
  } {1 1 1 2 2 2 ok}

  do_execsql_test 6.2.$tn.2 {
    INSERT OR REPLACE INTO t1 VALUES(3, 1, 1) ON CONFLICT(b) 
      DO UPDATE SET b=b||'x';
    SELECT * FROM t1;
    PRAGMA integrity_check;
  } {1 1x 1 2 2 2 ok}

  do_execsql_test 6.2.$tn.2 {
    INSERT OR REPLACE INTO t1 VALUES(3, 2, 2) ON CONFLICT(c) 
      DO UPDATE SET c=c||'x';
    SELECT * FROM t1;
    PRAGMA integrity_check;
  } {1 1x 1 2 2 2x ok}
}

#-------------------------------------------------------------------------
# Test references to "excluded". And using an alias in an INSERT 
# statement.
#
foreach {tn sql} {
  1 {
    CREATE TABLE t1(w, x, y, z, PRIMARY KEY(x, y));
    CREATE UNIQUE INDEX zz ON t1(z);
  }
  2 {
    CREATE TABLE t1(w, x, y, z, PRIMARY KEY(x, y)) WITHOUT ROWID;
    CREATE UNIQUE INDEX zz ON t1(z);
  }
} {
  reset_db
  execsql $sql
  do_execsql_test 7.$tn.0 {
    INSERT INTO t1 VALUES('a', 1, 1, 1);
    INSERT INTO t1 VALUES('b', 2, 2, 2);
  }

  do_execsql_test 7.$tn.1 {
    INSERT INTO t1 VALUES('c', 3, 3, 1) ON CONFLICT(z) 
      DO UPDATE SET w = excluded.w;
    SELECT * FROM t1;
  } {c 1 1 1 b 2 2 2}

  do_execsql_test 7.$tn.2 {
    INSERT INTO t1 VALUES('c', 2, 2, 3) ON CONFLICT(y, x) 
      DO UPDATE SET w = w||w;
    SELECT * FROM t1;
  } {c 1 1 1 bb 2 2 2}

  do_execsql_test 7.$tn.3 {
    INSERT INTO t1 VALUES('c', 2, 2, 3) ON CONFLICT(y, x) 
      DO UPDATE SET w = w||t1.w;
    SELECT * FROM t1;
  } {c 1 1 1 bbbb 2 2 2}

  do_execsql_test 7.$tn.4 {
    INSERT INTO t1 AS tbl VALUES('c', 2, 2, 3) ON CONFLICT(y, x) 
      DO UPDATE SET w = w||tbl.w;
    SELECT * FROM t1;
  } {c 1 1 1 bbbbbbbb 2 2 2}
}

foreach {tn sql} {
  1 {
    CREATE TABLE excluded(w, x INTEGER, 'a b', z, PRIMARY KEY(x, 'a b'));
    CREATE UNIQUE INDEX zz ON excluded(z);
    CREATE INDEX zz2 ON excluded(z);
  }
  2 {
    CREATE TABLE excluded(w, x, 'a b', z, PRIMARY KEY(x, 'a b')) WITHOUT ROWID;
    CREATE UNIQUE INDEX zz ON excluded(z);
    CREATE INDEX zz2 ON excluded(z);
  }
} {
  reset_db
  execsql $sql
  do_execsql_test 8.$tn.0 {
    INSERT INTO excluded VALUES('a', 1, 1, 1);
    INSERT INTO excluded VALUES('b', 2, 2, 2);
  }

  # Note: An error in Postgres: "table reference "excluded" is ambiguous".
  #
  do_execsql_test 8.$tn.1 {
    INSERT INTO excluded VALUES('hello', 1, 1, NULL) ON CONFLICT(x, "a b")
      DO UPDATE SET w=excluded.w;
    SELECT * FROM excluded;
  } {a 1 1 1 b 2 2 2}

  do_execsql_test 8.$tn.2 {
    INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) ON CONFLICT(x, [a b])
      DO UPDATE SET w=excluded.w;
    SELECT * FROM excluded;
  } {hello 1 1 1 b 2 2 2}

  do_execsql_test 8.$tn.3 {
    INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) ON CONFLICT(x, [a b])
      DO UPDATE SET w=w||w WHERE excluded.w!='hello';
    SELECT * FROM excluded;
  } {hello 1 1 1 b 2 2 2}

  do_execsql_test 8.$tn.4 {
    INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) ON CONFLICT(x, [a b])
      DO UPDATE SET w=w||w WHERE excluded.x=1;
    SELECT * FROM excluded;
  } {hellohello 1 1 1 b 2 2 2}

  do_catchsql_test 8.$tn.5 {
    INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) 
      ON CONFLICT(x, [a b]) WHERE y=1
      DO UPDATE SET w=w||w WHERE excluded.x=1;
  } {1 {no such column: y}}
}

#--------------------------------------------------------------------------
#
do_execsql_test 9.0 {
  CREATE TABLE v(x INTEGER);
  CREATE TABLE hist(x INTEGER PRIMARY KEY, cnt INTEGER);
  CREATE TRIGGER vt AFTER INSERT ON v BEGIN
    INSERT INTO hist VALUES(new.x, 1) ON CONFLICT(x) DO
      UPDATE SET cnt=cnt+1;
  END;
}

do_execsql_test 9.1 {
  INSERT INTO v VALUES(1), (4), (1), (5), (5), (8), (9), (1);
  SELECT * FROM hist;
} {
  1 3
  4 1
  5 2
  8 1
  9 1
}


finish_test