xref: /illumos-gate/usr/src/lib/libsqlite/test/capi2.test (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1#
2# 2003 January 29
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.  The
13# focus of this script testing the callback-free C/C++ API.
14#
15# $Id: capi2.test,v 1.10 2003/08/05 13:13:38 drh Exp $
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Check basic functionality
22#
23do_test capi2-1.1 {
24  db close
25  set DB [sqlite db test.db]
26  execsql {CREATE TABLE t1(a,b,c)}
27  set VM [sqlite_compile $DB {SELECT name, rowid FROM sqlite_master} TAIL]
28  set TAIL
29} {}
30do_test capi2-1.2 {
31  sqlite_step $VM N VALUES COLNAMES
32} {SQLITE_ROW}
33do_test capi2-1.3 {
34  set N
35} {2}
36do_test capi2-1.4 {
37  set VALUES
38} {t1 1}
39do_test capi2-1.5 {
40  set COLNAMES
41} {name rowid text INTEGER}
42do_test capi2-1.6 {
43  set N x
44  set VALUES y
45  set COLNAMES z
46  sqlite_step $VM N VALUES COLNAMES
47} {SQLITE_DONE}
48do_test capi2-1.7 {
49  list $N $VALUES $COLNAMES
50} {2 {} {name rowid text INTEGER}}
51do_test capi2-1.8 {
52  set N x
53  set VALUES y
54  set COLNAMES z
55  sqlite_step $VM N VALUES COLNAMES
56} {SQLITE_MISUSE}
57do_test capi2-1.9 {
58  list $N $VALUES $COLNAMES
59} {0 {} {}}
60do_test capi2-1.10 {
61  sqlite_finalize $VM
62} {}
63
64# Check to make sure that the "tail" of a multi-statement SQL script
65# is returned by sqlite_compile.
66#
67do_test capi2-2.1 {
68  set SQL {
69    SELECT name, rowid FROM sqlite_master;
70    SELECT name, rowid FROM sqlite_temp_master;
71    -- A comment at the end
72  }
73  set VM [sqlite_compile $DB $SQL SQL]
74  set SQL
75} {
76    SELECT name, rowid FROM sqlite_temp_master;
77    -- A comment at the end
78  }
79do_test capi2-2.2 {
80  set r [sqlite_step $VM n val colname]
81  lappend r $n $val $colname
82} {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}}
83do_test capi2-2.3 {
84  set r [sqlite_step $VM n val colname]
85  lappend r $n $val $colname
86} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
87do_test capi2-2.4 {
88  sqlite_finalize $VM
89} {}
90do_test capi2-2.5 {
91  set VM [sqlite_compile $DB $SQL SQL]
92  set SQL
93} {
94    -- A comment at the end
95  }
96do_test capi2-2.6 {
97  set r [sqlite_step $VM n val colname]
98  lappend r $n $val $colname
99} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
100do_test capi2-2.7 {
101  sqlite_finalize $VM
102} {}
103do_test capi2-2.8 {
104  set VM [sqlite_compile $DB $SQL SQL]
105  list $SQL $VM
106} {{} {}}
107
108# Check the error handling.
109#
110do_test capi2-3.1 {
111  set rc [catch {
112      sqlite_compile $DB {select bogus from sqlite_master} TAIL
113  } msg]
114  lappend rc $msg $TAIL
115} {1 {(1) no such column: bogus} {}}
116do_test capi2-3.2 {
117  set rc [catch {
118      sqlite_compile $DB {select bogus from } TAIL
119  } msg]
120  lappend rc $msg $TAIL
121} {1 {(1) near " ": syntax error} {}}
122do_test capi2-3.3 {
123  set rc [catch {
124      sqlite_compile $DB {;;;;select bogus from sqlite_master} TAIL
125  } msg]
126  lappend rc $msg $TAIL
127} {1 {(1) no such column: bogus} {}}
128do_test capi2-3.4 {
129  set rc [catch {
130      sqlite_compile $DB {select bogus from sqlite_master;x;} TAIL
131  } msg]
132  lappend rc $msg $TAIL
133} {1 {(1) no such column: bogus} {x;}}
134do_test capi2-3.5 {
135  set rc [catch {
136      sqlite_compile $DB {select bogus from sqlite_master;;;x;} TAIL
137  } msg]
138  lappend rc $msg $TAIL
139} {1 {(1) no such column: bogus} {;;x;}}
140do_test capi2-3.6 {
141  set rc [catch {
142      sqlite_compile $DB {select 5/0} TAIL
143  } VM]
144  lappend rc $TAIL
145} {0 {}}
146do_test capi2-3.7 {
147  set N {}
148  set VALUE {}
149  set COLNAME {}
150  list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
151} {SQLITE_ROW 1 {{}} {5/0 NUMERIC}}
152do_test capi2-3.8 {
153  sqlite_finalize $VM
154} {}
155do_test capi2-3.9 {
156  execsql {CREATE UNIQUE INDEX i1 ON t1(a)}
157  set VM [sqlite_compile $DB {INSERT INTO t1 VALUES(1,2,3)} TAIL]
158  set TAIL
159} {}
160do_test capi2-3.9b {db changes} {0}
161do_test capi2-3.10 {
162  set N {}
163  set VALUE {}
164  set COLNAME {}
165  list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
166} {SQLITE_DONE 0 {} {}}
167do_test capi2-3.10b {db changes} {1}
168do_test capi2-3.11 {
169  sqlite_finalize $VM
170} {}
171do_test capi2-3.11b {db changes} {1}
172do_test capi2-3.12 {
173  list [catch {sqlite_finalize $VM} msg] [set msg]
174} {1 {(21) library routine called out of sequence}}
175do_test capi2-3.13 {
176  set VM [sqlite_compile $DB {INSERT INTO t1 VALUES(1,3,4)} TAIL]
177  list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
178} {SQLITE_ERROR 0 {} {}}
179do_test capi2-3.13b {db changes} {0}
180do_test capi2-3.14 {
181  list [catch {sqlite_finalize $VM} msg] [set msg]
182} {1 {(19) column a is not unique}}
183do_test capi2-3.15 {
184  set VM [sqlite_compile $DB {CREATE TABLE t2(a NOT NULL, b)} TAIL]
185  set TAIL
186} {}
187do_test capi2-3.16 {
188  list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
189} {SQLITE_DONE 0 {} {}}
190do_test capi2-3.17 {
191  list [catch {sqlite_finalize $VM} msg] [set msg]
192} {0 {}}
193do_test capi2-3.18 {
194  set VM [sqlite_compile $DB {INSERT INTO t2 VALUES(NULL,2)} TAIL]
195  list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
196} {SQLITE_ERROR 0 {} {}}
197do_test capi2-3.19 {
198  list [catch {sqlite_finalize $VM} msg] [set msg]
199} {1 {(19) t2.a may not be NULL}}
200
201# Two or more virtual machines exists at the same time.
202#
203do_test capi2-4.1 {
204  set VM1 [sqlite_compile $DB {INSERT INTO t2 VALUES(1,2)} TAIL]
205  set TAIL
206} {}
207do_test capi2-4.2 {
208  set VM2 [sqlite_compile $DB {INSERT INTO t2 VALUES(2,3)} TAIL]
209  set TAIL
210} {}
211do_test capi2-4.3 {
212  set VM3 [sqlite_compile $DB {INSERT INTO t2 VALUES(3,4)} TAIL]
213  set TAIL
214} {}
215do_test capi2-4.4 {
216  list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
217} {SQLITE_DONE 0 {} {}}
218do_test capi2-4.5 {
219  execsql {SELECT * FROM t2 ORDER BY a}
220} {2 3}
221do_test capi2-4.6 {
222  list [catch {sqlite_finalize $VM2} msg] [set msg]
223} {0 {}}
224do_test capi2-4.7 {
225  list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
226} {SQLITE_DONE 0 {} {}}
227do_test capi2-4.8 {
228  execsql {SELECT * FROM t2 ORDER BY a}
229} {2 3 3 4}
230do_test capi2-4.9 {
231  list [catch {sqlite_finalize $VM3} msg] [set msg]
232} {0 {}}
233do_test capi2-4.10 {
234  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
235} {SQLITE_DONE 0 {} {}}
236do_test capi2-4.11 {
237  execsql {SELECT * FROM t2 ORDER BY a}
238} {1 2 2 3 3 4}
239do_test capi2-4.12 {
240  list [catch {sqlite_finalize $VM1} msg] [set msg]
241} {0 {}}
242
243# Interleaved SELECTs
244#
245do_test capi2-5.1 {
246  set VM1 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
247  set VM2 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
248  set VM3 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
249  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
250} {SQLITE_ROW 2 {2 3} {a b {} {}}}
251do_test capi2-5.2 {
252  list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
253} {SQLITE_ROW 2 {2 3} {a b {} {}}}
254do_test capi2-5.3 {
255  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
256} {SQLITE_ROW 2 {3 4} {a b {} {}}}
257do_test capi2-5.4 {
258  list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
259} {SQLITE_ROW 2 {2 3} {a b {} {}}}
260do_test capi2-5.5 {
261  list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
262} {SQLITE_ROW 2 {3 4} {a b {} {}}}
263do_test capi2-5.6 {
264  list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
265} {SQLITE_ROW 2 {1 2} {a b {} {}}}
266do_test capi2-5.7 {
267  list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
268} {SQLITE_DONE 2 {} {a b {} {}}}
269do_test capi2-5.8 {
270  list [catch {sqlite_finalize $VM3} msg] [set msg]
271} {0 {}}
272do_test capi2-5.9 {
273  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
274} {SQLITE_ROW 2 {1 2} {a b {} {}}}
275do_test capi2-5.10 {
276  list [catch {sqlite_finalize $VM1} msg] [set msg]
277} {0 {}}
278do_test capi2-5.11 {
279  list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
280} {SQLITE_ROW 2 {3 4} {a b {} {}}}
281do_test capi2-5.12 {
282  list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
283} {SQLITE_ROW 2 {1 2} {a b {} {}}}
284do_test capi2-5.11 {
285  list [catch {sqlite_finalize $VM2} msg] [set msg]
286} {0 {}}
287
288# Check for proper SQLITE_BUSY returns.
289#
290do_test capi2-6.1 {
291  execsql {
292    BEGIN;
293    CREATE TABLE t3(x counter);
294    INSERT INTO t3 VALUES(1);
295    INSERT INTO t3 VALUES(2);
296    INSERT INTO t3 SELECT x+2 FROM t3;
297    INSERT INTO t3 SELECT x+4 FROM t3;
298    INSERT INTO t3 SELECT x+8 FROM t3;
299    COMMIT;
300  }
301  set VM1 [sqlite_compile $DB {SELECT * FROM t3} TAIL]
302  sqlite db2 test.db
303  execsql {BEGIN} db2
304} {}
305do_test capi2-6.2 {
306  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
307} {SQLITE_BUSY 0 {} {}}
308do_test capi2-6.3 {
309  execsql {COMMIT} db2
310} {}
311do_test capi2-6.4 {
312  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
313} {SQLITE_ROW 1 1 {x counter}}
314do_test capi2-6.5 {
315  catchsql {BEGIN} db2
316} {1 {database is locked}}
317do_test capi2-6.6 {
318  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
319} {SQLITE_ROW 1 2 {x counter}}
320do_test capi2-6.7 {
321  execsql {SELECT * FROM t2} db2
322} {2 3 3 4 1 2}
323do_test capi2-6.8 {
324  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
325} {SQLITE_ROW 1 3 {x counter}}
326do_test capi2-6.9 {
327  execsql {SELECT * FROM t2}
328} {2 3 3 4 1 2}
329do_test capi2-6.10 {
330  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
331} {SQLITE_ROW 1 4 {x counter}}
332do_test capi2-6.11 {
333  execsql {BEGIN}
334} {}
335do_test capi2-6.12 {
336  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
337} {SQLITE_ROW 1 5 {x counter}}
338# execsql {pragma vdbe_trace=on}
339do_test capi2-6.13 {
340  catchsql {UPDATE t3 SET x=x+1}
341} {1 {database table is locked}}
342do_test capi2-6.14 {
343  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
344} {SQLITE_ROW 1 6 {x counter}}
345# puts [list [catch {sqlite_finalize $VM1} msg] [set msg]]; exit
346do_test capi2-6.15 {
347  execsql {SELECT * FROM t1}
348} {1 2 3}
349do_test capi2-6.16 {
350  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
351} {SQLITE_ROW 1 7 {x counter}}
352do_test capi2-6.17 {
353  catchsql {UPDATE t1 SET b=b+1}
354} {0 {}}
355do_test capi2-6.18 {
356  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
357} {SQLITE_ROW 1 8 {x counter}}
358do_test capi2-6.19 {
359  execsql {SELECT * FROM t1}
360} {1 3 3}
361do_test capi2-6.20 {
362  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
363} {SQLITE_ROW 1 9 {x counter}}
364do_test capi2-6.21 {
365  execsql {ROLLBACK; SELECT * FROM t1}
366} {1 2 3}
367do_test capi2-6.22 {
368  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
369} {SQLITE_ROW 1 10 {x counter}}
370do_test capi2-6.23 {
371  execsql {BEGIN TRANSACTION ON CONFLICT ROLLBACK;}
372} {}
373do_test capi2-6.24 {
374  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
375} {SQLITE_ROW 1 11 {x counter}}
376do_test capi2-6.25 {
377  execsql {
378    INSERT INTO t1 VALUES(2,3,4);
379    SELECT * FROM t1;
380  }
381} {1 2 3 2 3 4}
382do_test capi2-6.26 {
383  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
384} {SQLITE_ROW 1 12 {x counter}}
385do_test capi2-6.27 {
386  catchsql {
387    INSERT INTO t1 VALUES(2,4,5);
388    SELECT * FROM t1;
389  }
390} {1 {column a is not unique}}
391do_test capi2-6.28 {
392  list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
393} {SQLITE_ROW 1 13 {x counter}}
394do_test capi2-6.99 {
395  list [catch {sqlite_finalize $VM1} msg] [set msg]
396} {0 {}}
397catchsql {ROLLBACK}
398
399do_test capi2-7.1 {
400  stepsql $DB {
401    SELECT * FROM t1
402  }
403} {0 1 2 3}
404do_test capi2-7.2 {
405  stepsql $DB {
406    PRAGMA count_changes=on
407  }
408} {0}
409do_test capi2-7.3 {
410  stepsql $DB {
411    UPDATE t1 SET a=a+10;
412  }
413} {0 1}
414do_test capi2-7.4 {
415  stepsql $DB {
416    INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
417  }
418} {0 1}
419do_test capi2-7.4b {db changes} {1}
420do_test capi2-7.5 {
421  stepsql $DB {
422    UPDATE t1 SET a=a+10;
423  }
424} {0 2}
425do_test capi2-7.5b {db changes} {2}
426do_test capi2-7.6 {
427  stepsql $DB {
428    SELECT * FROM t1;
429  }
430} {0 21 2 3 22 3 4}
431do_test capi2-7.7 {
432  stepsql $DB {
433    INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
434  }
435} {0 2}
436do_test capi2-7.8 {
437  db changes
438} {2}
439do_test capi2-7.9 {
440  stepsql $DB {
441    SELECT * FROM t1;
442  }
443} {0 21 2 3 22 3 4 23 4 5 24 5 6}
444do_test capi2-7.10 {
445  stepsql $DB {
446    UPDATE t1 SET a=a-20;
447    SELECT * FROM t1;
448  }
449} {0 4 1 2 3 2 3 4 3 4 5 4 5 6}
450do_test capi2-7.11 {
451  db changes
452} {0}
453do_test capi2-7.12 {
454  set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
455  lindex $x 0
456} {0}
457
458# Ticket #261 - make sure we can finalize before the end of a query.
459#
460do_test capi2-8.1 {
461  set VM1 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
462  sqlite_finalize $VM1
463} {}
464
465# Tickets #384 and #385 - make sure the TAIL argument to sqlite_compile
466# and all of the return pointers in sqlite_step can be null.
467#
468do_test capi2-9.1 {
469  set VM1 [sqlite_compile $DB {SELECT * FROM t2}]
470  sqlite_step $VM1
471  sqlite_finalize $VM1
472} {}
473
474db2 close
475
476finish_test
477