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