interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / capi3.test

1278 lines · 32392 bytes Raw Blame History
1# 2003 January 29
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this script testing the callback-free C/C++ API.
13#
14# $Id: capi3.test,v 1.70 2009/01/09 02:49:32 drh Exp $
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19set ::testprefix capi3
20
21# Do not use a codec for tests in this file, as the database file is
22# manipulated directly using tcl scripts (using the [hexio_write] command).
23#
24do_not_use_codec
25
26# Return the UTF-16 representation of the supplied UTF-8 string $str.
27# If $nt is true, append two 0x00 bytes as a nul terminator.
28proc utf16 {str {nt 1}} {
29 set r [encoding convertto unicode $str]
30 if {$nt} {
31 append r "\x00\x00"
32 }
33 return $r
34}
35
36# Return the UTF-8 representation of the supplied UTF-16 string $str.
37proc utf8 {str} {
38 # If $str ends in two 0x00 0x00 bytes, knock these off before
39 # converting to UTF-8 using TCL.
40 binary scan $str \c* vals
41 if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
42 set str [binary format \c* [lrange $vals 0 end-2]]
43 }
44
45 set r [encoding convertfrom unicode $str]
46 return $r
47}
48
49# These tests complement those in capi2.test. They are organized
50# as follows:
51#
52# capi3-1.*: Test sqlite3_prepare
53# capi3-2.*: Test sqlite3_prepare16
54# capi3-3.*: Test sqlite3_open
55# capi3-4.*: Test sqlite3_open16
56# capi3-5.*: Test the various sqlite3_result_* APIs
57# capi3-6.*: Test that sqlite3_close fails if there are outstanding VMs.
58#
59
60set DB [sqlite3_connection_pointer db]
61
62do_test capi3-1.0 {
63 sqlite3_get_autocommit $DB
64} 1
65do_test capi3-1.1 {
66 set STMT [sqlite3_prepare $DB {SELECT name FROM sqlite_master} -1 TAIL]
67 sqlite3_finalize $STMT
68 set TAIL
69} {}
70do_test capi3-1.2.1 {
71 sqlite3_errcode $DB
72} {SQLITE_OK}
73do_test capi3-1.2.2 {
74 sqlite3_extended_errcode $DB
75} {SQLITE_OK}
76do_test capi3-1.3 {
77 sqlite3_errmsg $DB
78} {not an error}
79do_test capi3-1.4 {
80 set sql {SELECT name FROM sqlite_master;SELECT 10}
81 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
82 sqlite3_finalize $STMT
83 set TAIL
84} {SELECT 10}
85do_test capi3-1.5 {
86 set sql {SELECT name FROM sqlite_master;SELECT 10}
87 set STMT [sqlite3_prepare $DB $sql [string length $sql] TAIL]
88 sqlite3_finalize $STMT
89 set TAIL
90} {SELECT 10}
91do_test capi3-1.6 {
92 set sql {SELECT name FROM sqlite_master;SELECT 10}
93 set STMT [sqlite3_prepare $DB $sql [expr [string length $sql]+1] TAIL]
94 sqlite3_finalize $STMT
95 set TAIL
96} {SELECT 10}
97
98do_test capi3-1.7 {
99 set sql {SELECT namex FROM sqlite_master}
100 catch {
101 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
102 }
103} {1}
104do_test capi3-1.8.1 {
105 sqlite3_errcode $DB
106} {SQLITE_ERROR}
107do_test capi3-1.8.2 {
108 sqlite3_extended_errcode $DB
109} {SQLITE_ERROR}
110do_test capi3-1.9 {
111 sqlite3_errmsg $DB
112} {no such column: namex}
113
114ifcapable {utf16} {
115 do_test capi3-2.1 {
116 set sql16 [utf16 {SELECT name FROM sqlite_master}]
117 set STMT [sqlite3_prepare16 $DB $sql16 -1 ::TAIL]
118 sqlite3_finalize $STMT
119 utf8 $::TAIL
120 } {}
121 do_test capi3-2.2 {
122 set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}]
123 set STMT [sqlite3_prepare16 $DB $sql -1 TAIL]
124 sqlite3_finalize $STMT
125 utf8 $TAIL
126 } {SELECT 10}
127 do_test capi3-2.3 {
128 set sql [utf16 {SELECT namex FROM sqlite_master}]
129 catch {
130 set STMT [sqlite3_prepare16 $DB $sql -1]
131 }
132 } {1}
133 do_test capi3-2.4.1 {
134 sqlite3_errcode $DB
135 } {SQLITE_ERROR}
136 do_test capi3-2.4.2 {
137 sqlite3_extended_errcode $DB
138 } {SQLITE_ERROR}
139 do_test capi3-2.5 {
140 sqlite3_errmsg $DB
141 } {no such column: namex}
142
143 ifcapable schema_pragmas {
144 do_test capi3-2.6 {
145 execsql {CREATE TABLE tablename(x)}
146 set sql16 [utf16 {PRAGMA table_info("TableName"); --excess text}]
147 set STMT [sqlite3_prepare16 $DB $sql16 -1]
148 sqlite3_step $STMT
149 } SQLITE_ROW
150 do_test capi3-2.7 {
151 sqlite3_step $STMT
152 } SQLITE_DONE
153 do_test capi3-2.8 {
154 sqlite3_finalize $STMT
155 } SQLITE_OK
156 }
157
158} ;# endif utf16
159
160# rename sqlite3_open sqlite3_open_old
161# proc sqlite3_open {fname options} {sqlite3_open_new $fname $options}
162
163do_test capi3-3.1 {
164 set db2 [sqlite3_open test.db {}]
165 sqlite3_errcode $db2
166} {SQLITE_OK}
167# FIX ME: Should test the db handle works.
168do_test capi3-3.2 {
169 sqlite3_close $db2
170} {SQLITE_OK}
171do_test capi3-3.3 {
172 catch {
173 set db2 [sqlite3_open /bogus/path/test.db {}]
174 }
175 set ::capi3_errno [sqlite3_system_errno $db2]
176 list [sqlite3_extended_errcode $db2] [expr {$::capi3_errno!=0}]
177} {SQLITE_CANTOPEN 1}
178do_test capi3-3.4 {
179 sqlite3_errmsg $db2
180} {unable to open database file}
181do_test capi3-3.5 {
182 list [sqlite3_system_errno $db2] [sqlite3_close $db2]
183} [list $::capi3_errno SQLITE_OK]
184if {[clang_sanitize_address]==0 && 0} {
185 do_test capi3-3.6.1-misuse {
186 sqlite3_close $db2
187 } {SQLITE_MISUSE}
188 do_test capi3-3.6.2-misuse {
189 sqlite3_errmsg $db2
190 } {bad parameter or other API misuse}
191 ifcapable {utf16} {
192 do_test capi3-3.6.3-misuse {
193 utf8 [sqlite3_errmsg16 $db2]
194 } {bad parameter or other API misuse}
195 }
196}
197
198do_test capi3-3.7 {
199 set db2 [sqlite3_open]
200 sqlite3_errcode $db2
201} {SQLITE_OK}
202do_test capi3-3.8 {
203 sqlite3_close $db2
204} {SQLITE_OK}
205
206# rename sqlite3_open ""
207# rename sqlite3_open_old sqlite3_open
208
209ifcapable {utf16} {
210do_test capi3-4.1 {
211 set db2 [sqlite3_open16 [utf16 test.db] {}]
212 sqlite3_errcode $db2
213} {SQLITE_OK}
214# FIX ME: Should test the db handle works.
215do_test capi3-4.2 {
216 sqlite3_close $db2
217} {SQLITE_OK}
218do_test capi3-4.3 {
219 catch {
220 set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]
221 }
222 sqlite3_errcode $db2
223} {SQLITE_CANTOPEN}
224do_test capi3-4.4 {
225 utf8 [sqlite3_errmsg16 $db2]
226} {unable to open database file}
227do_test capi3-4.5 {
228 sqlite3_close $db2
229} {SQLITE_OK}
230} ;# utf16
231
232# This proc is used to test the following API calls:
233#
234# sqlite3_column_count
235# sqlite3_column_name
236# sqlite3_column_name16
237# sqlite3_column_decltype
238# sqlite3_column_decltype16
239#
240# $STMT is a compiled SQL statement. $test is a prefix
241# to use for test names within this proc. $names is a list
242# of the column names that should be returned by $STMT.
243# $decltypes is a list of column declaration types for $STMT.
244#
245# Example:
246#
247# set STMT [sqlite3_prepare "SELECT 1, 2, 2;" -1 DUMMY]
248# check_header test1.1 {1 2 3} {"" "" ""}
249#
250proc check_header {STMT test names decltypes} {
251
252 # Use the return value of sqlite3_column_count() to build
253 # a list of column indexes. i.e. If sqlite3_column_count
254 # is 3, build the list {0 1 2}.
255 set ::idxlist [list]
256 set ::numcols [sqlite3_column_count $STMT]
257 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
258
259 # Column names in UTF-8
260 do_test $test.1 {
261 set cnamelist [list]
262 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
263 set cnamelist
264 } $names
265
266 # Column names in UTF-16
267 ifcapable {utf16} {
268 do_test $test.2 {
269 set cnamelist [list]
270 foreach i $idxlist {
271 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
272 }
273 set cnamelist
274 } $names
275 }
276
277 # Column names in UTF-8
278 do_test $test.3 {
279 set cnamelist [list]
280 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
281 set cnamelist
282 } $names
283
284 # Column names in UTF-16
285 ifcapable {utf16} {
286 do_test $test.4 {
287 set cnamelist [list]
288 foreach i $idxlist {
289 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
290 }
291 set cnamelist
292 } $names
293 }
294
295 # Column names in UTF-8
296 do_test $test.5 {
297 set cnamelist [list]
298 foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]}
299 set cnamelist
300 } $decltypes
301
302 # Column declaration types in UTF-16
303 ifcapable {utf16} {
304 do_test $test.6 {
305 set cnamelist [list]
306 foreach i $idxlist {
307 lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]]
308 }
309 set cnamelist
310 } $decltypes
311 }
312
313
314 # Test some out of range conditions:
315 ifcapable {utf16} {
316 do_test $test.7 {
317 list \
318 [sqlite3_column_name $STMT -1] \
319 [sqlite3_column_name16 $STMT -1] \
320 [sqlite3_column_decltype $STMT -1] \
321 [sqlite3_column_decltype16 $STMT -1] \
322 [sqlite3_column_name $STMT $numcols] \
323 [sqlite3_column_name16 $STMT $numcols] \
324 [sqlite3_column_decltype $STMT $numcols] \
325 [sqlite3_column_decltype16 $STMT $numcols]
326 } {{} {} {} {} {} {} {} {}}
327 }
328}
329
330# This proc is used to test the following API calls:
331#
332# sqlite3_column_origin_name
333# sqlite3_column_origin_name16
334# sqlite3_column_table_name
335# sqlite3_column_table_name16
336# sqlite3_column_database_name
337# sqlite3_column_database_name16
338#
339# $STMT is a compiled SQL statement. $test is a prefix
340# to use for test names within this proc. $names is a list
341# of the column names that should be returned by $STMT.
342# $decltypes is a list of column declaration types for $STMT.
343#
344# Example:
345#
346# set STMT [sqlite3_prepare "SELECT 1, 2, 2;" -1 DUMMY]
347# check_header test1.1 {1 2 3} {"" "" ""}
348#
349proc check_origin_header {STMT test dbs tables cols} {
350 # If sqlite3_column_origin_name() and friends are not compiled into
351 # this build, this proc is a no-op.
352 ifcapable columnmetadata {
353 # Use the return value of sqlite3_column_count() to build
354 # a list of column indexes. i.e. If sqlite3_column_count
355 # is 3, build the list {0 1 2}.
356 set ::idxlist [list]
357 set ::numcols [sqlite3_column_count $STMT]
358 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
359
360 # Database names in UTF-8
361 do_test $test.8 {
362 set cnamelist [list]
363 foreach i $idxlist {
364 lappend cnamelist [sqlite3_column_database_name $STMT $i]
365 }
366 set cnamelist
367 } $dbs
368
369 # Database names in UTF-16
370 ifcapable {utf16} {
371 do_test $test.9 {
372 set cnamelist [list]
373 foreach i $idxlist {
374 lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]]
375 }
376 set cnamelist
377 } $dbs
378 }
379
380 # Table names in UTF-8
381 do_test $test.10 {
382 set cnamelist [list]
383 foreach i $idxlist {
384 lappend cnamelist [sqlite3_column_table_name $STMT $i]
385 }
386 set cnamelist
387 } $tables
388
389 # Table names in UTF-16
390 ifcapable {utf16} {
391 do_test $test.11 {
392 set cnamelist [list]
393 foreach i $idxlist {
394 lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]]
395 }
396 set cnamelist
397 } $tables
398 }
399
400 # Origin names in UTF-8
401 do_test $test.12 {
402 set cnamelist [list]
403 foreach i $idxlist {
404 lappend cnamelist [sqlite3_column_origin_name $STMT $i]
405 }
406 set cnamelist
407 } $cols
408
409 # Origin declaration types in UTF-16
410 ifcapable {utf16} {
411 do_test $test.13 {
412 set cnamelist [list]
413 foreach i $idxlist {
414 lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]]
415 }
416 set cnamelist
417 } $cols
418 }
419 }
420}
421
422# This proc is used to test the following APIs:
423#
424# sqlite3_data_count
425# sqlite3_column_type
426# sqlite3_column_int
427# sqlite3_column_text
428# sqlite3_column_text16
429# sqlite3_column_double
430#
431# $STMT is a compiled SQL statement for which the previous call
432# to sqlite3_step returned SQLITE_ROW. $test is a prefix to use
433# for test names within this proc. $types is a list of the
434# manifest types for the current row. $ints, $doubles and $strings
435# are lists of the integer, real and string representations of
436# the values in the current row.
437#
438# Example:
439#
440# set STMT [sqlite3_prepare "SELECT 'hello', 1.1, NULL" -1 DUMMY]
441# sqlite3_step $STMT
442# check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}}
443#
444proc check_data {STMT test types ints doubles strings} {
445
446 # Use the return value of sqlite3_column_count() to build
447 # a list of column indexes. i.e. If sqlite3_column_count
448 # is 3, build the list {0 1 2}.
449 set ::idxlist [list]
450 set numcols [sqlite3_data_count $STMT]
451 for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i}
452
453# types
454do_test $test.1 {
455 set types [list]
456 foreach i $idxlist {
457 set x [sqlite3_column_type $STMT $i]
458 # EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
459 # fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
460 # point number string BLOB NULL
461 if {[lsearch {INTEGER FLOAT TEXT BLOB NULL} $x]<0} {
462 set types ERROR
463 break
464 } else {
465 lappend types $x
466 }
467 }
468 set types
469} $types
470
471
472# Integers
473do_test $test.2 {
474 set ints [list]
475 foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]}
476 set ints
477} $ints
478
479# bytes
480set lens [list]
481foreach i $::idxlist {
482 lappend lens [string length [lindex $strings $i]]
483}
484do_test $test.3 {
485 set bytes [list]
486 set lens [list]
487 foreach i $idxlist {
488 lappend bytes [sqlite3_column_bytes $STMT $i]
489 }
490 set bytes
491} $lens
492
493# bytes16
494ifcapable {utf16} {
495 set lens [list]
496 foreach i $::idxlist {
497 lappend lens [expr 2 * [string length [lindex $strings $i]]]
498 }
499 do_test $test.4 {
500 set bytes [list]
501 set lens [list]
502 foreach i $idxlist {
503 lappend bytes [sqlite3_column_bytes16 $STMT $i]
504 }
505 set bytes
506 } $lens
507}
508
509# Blob
510do_test $test.5 {
511 set utf8 [list]
512 foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]}
513 set utf8
514} $strings
515
516# UTF-8
517do_test $test.6 {
518 set utf8 [list]
519 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
520 set utf8
521} $strings
522
523# Floats
524do_test $test.7 {
525 set utf8 [list]
526 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
527 set utf8
528} $doubles
529
530# UTF-16
531ifcapable {utf16} {
532 do_test $test.8 {
533 set utf8 [list]
534 foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]}
535 set utf8
536 } $strings
537}
538
539# Integers
540do_test $test.9 {
541 set ints [list]
542 foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]}
543 set ints
544} $ints
545
546# Floats
547do_test $test.10 {
548 set utf8 [list]
549 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
550 set utf8
551} $doubles
552
553# UTF-8
554do_test $test.11 {
555 set utf8 [list]
556 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
557 set utf8
558} $strings
559
560# Types
561do_test $test.12 {
562 set types [list]
563 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
564 set types
565} $types
566
567# Test that an out of range request returns the equivalent of NULL
568do_test $test.13 {
569 sqlite3_column_int $STMT -1
570} {0}
571do_test $test.13 {
572 sqlite3_column_text $STMT -1
573} {}
574
575}
576
577ifcapable !floatingpoint {
578 finish_test
579 return
580}
581
582do_test capi3-5.0 {
583 execsql {
584 CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));
585 INSERT INTO t1 VALUES(1, 2, 3);
586 INSERT INTO t1 VALUES('one', 'two', NULL);
587 INSERT INTO t1 VALUES(1.2, 1.3, 1.4);
588 }
589 set sql "SELECT * FROM t1"
590 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
591 sqlite3_column_count $STMT
592} 3
593
594check_header $STMT capi3-5.1 {a b c} {VARINT BLOB VARCHAR(16)}
595check_origin_header $STMT capi3-5.1 {main main main} {t1 t1 t1} {a b c}
596do_test capi3-5.2 {
597 sqlite3_step $STMT
598} SQLITE_ROW
599
600check_header $STMT capi3-5.3 {a b c} {VARINT BLOB VARCHAR(16)}
601check_origin_header $STMT capi3-5.3 {main main main} {t1 t1 t1} {a b c}
602check_data $STMT capi3-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}
603
604do_test capi3-5.5 {
605 sqlite3_step $STMT
606} SQLITE_ROW
607
608check_header $STMT capi3-5.6 {a b c} {VARINT BLOB VARCHAR(16)}
609check_origin_header $STMT capi3-5.6 {main main main} {t1 t1 t1} {a b c}
610check_data $STMT capi3-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}
611
612do_test capi3-5.8 {
613 sqlite3_step $STMT
614} SQLITE_ROW
615
616check_header $STMT capi3-5.9 {a b c} {VARINT BLOB VARCHAR(16)}
617check_origin_header $STMT capi3-5.9 {main main main} {t1 t1 t1} {a b c}
618check_data $STMT capi3-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}
619
620do_test capi3-5.11 {
621 sqlite3_step $STMT
622} SQLITE_DONE
623
624do_test capi3-5.12 {
625 sqlite3_finalize $STMT
626} SQLITE_OK
627
628do_test capi3-5.20 {
629 set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"
630 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
631 sqlite3_column_count $STMT
632} 3
633
634check_header $STMT capi3-5.21 {a sum(b) max(c)} {VARINT {} {}}
635check_origin_header $STMT capi3-5.22 {main {} {}} {t1 {} {}} {a {} {}}
636do_test capi3-5.23 {
637 sqlite3_finalize $STMT
638} SQLITE_OK
639
640do_test capi3-5.30 {
641 set sql "SELECT a AS x, sum(b) AS y, max(c) AS z FROM t1 AS m GROUP BY x"
642 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
643 sqlite3_column_count $STMT
644} 3
645
646check_header $STMT capi3-5.31 {x y z} {VARINT {} {}}
647check_origin_header $STMT capi3-5.32 {main {} {}} {t1 {} {}} {a {} {}}
648do_test capi3-5.33 {
649 sqlite3_finalize $STMT
650} SQLITE_OK
651
652# 2018-01-09: If a column is the last token if a string, the column name
653# was not being set correctly, due to changes in check-in
654# https://sqlite.org/src/info/0fdf97efe5df7455
655#
656# This problem was detected by the community during beta-testing.
657#
658do_test capi3-5.34 {
659 set STMT [sqlite3_prepare $DB {SELECT :a, :b} -1 TAIL]
660 sqlite3_column_count $STMT
661} 2
662check_header $STMT capi-5.35 {:a :b} {{} {}}
663sqlite3_finalize $STMT
664
665set ::ENC [execsql {pragma encoding}]
666db close
667
668do_test capi3-6.0 {
669 sqlite3 db test.db
670 set DB [sqlite3_connection_pointer db]
671 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy }
672 set sql {SELECT a FROM t1 order by rowid}
673 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
674 expr 0
675} {0}
676do_test capi3-6.1 {
677 db cache flush
678 sqlite3_close $DB
679} {SQLITE_BUSY}
680
681# 6.2 and 6.3 used to return SQLITE_ERROR and SQLITE_SCHEMA, respectively.
682# But since attempting to close a connection no longer resets the internal
683# schema and expires all statements, this is no longer the case.
684do_test capi3-6.2 {
685 sqlite3_step $STMT
686} {SQLITE_ROW}
687#check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1}
688do_test capi3-6.3 {
689 sqlite3_finalize $STMT
690} {SQLITE_OK}
691
692if {0 && [clang_sanitize_address]==0} {
693 # This use-after-free occasionally causes segfaults during ordinary
694 # builds. Let's just disable it completely.
695 do_test capi3-6.4-misuse {
696 db cache flush
697 sqlite3_close $DB
698 } {SQLITE_OK}
699}
700db close
701
702# This procedure sets the value of the file-format in file 'test.db'
703# to $newval. Also, the schema cookie is incremented.
704#
705proc set_file_format {newval} {
706 hexio_write test.db 44 [hexio_render_int32 $newval]
707 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
708 incr schemacookie
709 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
710 return {}
711}
712
713# This procedure returns the value of the file-format in file 'test.db'.
714#
715proc get_file_format {{fname test.db}} {
716 return [hexio_get_int [hexio_read $fname 44 4]]
717}
718
719if {![sqlite3 -has-codec]} {
720 # Test what happens when the library encounters a newer file format.
721 do_test capi3-7.1 {
722 set_file_format 5
723 } {}
724 do_test capi3-7.2 {
725 catch { sqlite3 db test.db }
726 catchsql {
727 SELECT * FROM sqlite_master;
728 }
729 } {1 {unsupported file format}}
730 db close
731}
732
733if {![sqlite3 -has-codec]} {
734 # Now test that the library correctly handles bogus entries in the
735 # sqlite_master table (schema corruption).
736 do_test capi3-8.1 {
737 forcedelete test.db test.db-journal
738 sqlite3 db test.db
739 execsql {
740 CREATE TABLE t1(a);
741 }
742 db close
743 } {}
744 do_test capi3-8.2 {
745 sqlite3 db test.db
746 sqlite3_db_config db DEFENSIVE 0
747 execsql {
748 PRAGMA writable_schema=ON;
749 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);
750 }
751 db close
752 } {}
753 do_test capi3-8.3 {
754 catch { sqlite3 db test.db }
755 catchsql {
756 SELECT * FROM sqlite_master;
757 }
758 } {1 {malformed database schema (?)}}
759 do_test capi3-8.4 {
760 # Build a 5-field row record. The first field is a string 'table', and
761 # subsequent fields are all NULL.
762 db close
763 forcedelete test.db test.db-journal
764 sqlite3 db test.db
765 sqlite3_db_config db DEFENSIVE 0
766 execsql {
767 CREATE TABLE t1(a);
768 PRAGMA writable_schema=ON;
769 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);
770 }
771 db close
772 } {};
773 do_test capi3-8.5 {
774 catch { sqlite3 db test.db }
775 catchsql {
776 SELECT * FROM sqlite_master;
777 }
778 } {1 {malformed database schema (?)}}
779 db close
780}
781forcedelete test.db
782forcedelete test.db-journal
783
784
785# Test the english language string equivalents for sqlite error codes
786set code2english [list \
787SQLITE_OK {not an error} \
788SQLITE_ERROR {SQL logic error} \
789SQLITE_PERM {access permission denied} \
790SQLITE_ABORT {query aborted} \
791SQLITE_BUSY {database is locked} \
792SQLITE_LOCKED {database table is locked} \
793SQLITE_NOMEM {out of memory} \
794SQLITE_READONLY {attempt to write a readonly database} \
795SQLITE_INTERRUPT {interrupted} \
796SQLITE_IOERR {disk I/O error} \
797SQLITE_CORRUPT {database disk image is malformed} \
798SQLITE_FULL {database or disk is full} \
799SQLITE_CANTOPEN {unable to open database file} \
800SQLITE_SCHEMA {database schema has changed} \
801SQLITE_CONSTRAINT {constraint failed} \
802SQLITE_MISMATCH {datatype mismatch} \
803SQLITE_MISUSE {bad parameter or other API misuse} \
804SQLITE_AUTH {authorization denied} \
805SQLITE_RANGE {column index out of range} \
806SQLITE_NOTADB {file is not a database} \
807unknownerror {unknown error} \
808]
809
810set test_number 1
811foreach {code english} $code2english {
812 do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english
813 incr test_number
814}
815
816# Test the error message when a "real" out of memory occurs.
817if { [permutation] != "nofaultsim" } {
818 do_test capi3-10-1 {
819 sqlite3 db test.db
820 set DB [sqlite3_connection_pointer db]
821 sqlite3_memdebug_fail 1
822 catchsql {
823 select * from sqlite_master;
824 }
825 } {1 {out of memory}}
826 do_test capi3-10-2 {
827 sqlite3_errmsg $::DB
828 } {out of memory}
829 ifcapable {utf16} {
830 do_test capi3-10-3 {
831 utf8 [sqlite3_errmsg16 $::DB]
832 } {out of memory}
833 }
834 db close
835 sqlite3_memdebug_fail -1
836 do_test capi3-10-4 {
837 sqlite3 db test.db
838 set DB [sqlite3_connection_pointer db]
839 sqlite3_memdebug_fail 1
840 catchsql {
841 select * from sqlite_master where rowid>5;
842 }
843 } {1 {out of memory}}
844 do_test capi3-10-5 {
845 sqlite3_errmsg $::DB
846 } {out of memory}
847 ifcapable {utf16} {
848 do_test capi3-10-6 {
849 utf8 [sqlite3_errmsg16 $::DB]
850 } {out of memory}
851 }
852 db close
853 sqlite3_memdebug_fail -1
854}
855
856# The following tests - capi3-11.* - test that a COMMIT or ROLLBACK
857# statement issued while there are still outstanding VMs that are part of
858# the transaction fails.
859sqlite3 db test.db
860set DB [sqlite3_connection_pointer db]
861sqlite_register_test_function $DB func
862do_test capi3-11.1 {
863 execsql {
864 BEGIN;
865 CREATE TABLE t1(a, b);
866 INSERT INTO t1 VALUES(1, 'int');
867 INSERT INTO t1 VALUES(2, 'notatype');
868 }
869} {}
870do_test capi3-11.1.1 {
871 sqlite3_get_autocommit $DB
872} 0
873do_test capi3-11.2 {
874 set STMT [sqlite3_prepare $DB "SELECT func(b, a) FROM t1" -1 TAIL]
875 sqlite3_step $STMT
876} {SQLITE_ROW}
877
878# As of 3.6.5 a COMMIT is OK during while a query is still running -
879# as long as it is a read-only query and not an incremental BLOB write.
880#
881do_test capi3-11.3.1 {
882 catchsql {
883 COMMIT;
884 }
885} {0 {}}
886do_test capi3-11.3.2 {
887 sqlite3_extended_errcode $DB
888} {SQLITE_OK}
889do_test capi3-11.3.3 {
890 sqlite3_get_autocommit $DB
891} 1
892do_test capi3-11.3.4 {
893 db eval {PRAGMA lock_status}
894} {main shared temp closed}
895
896do_test capi3-11.4 {
897 sqlite3_step $STMT
898} {SQLITE_ERROR}
899do_test capi3-11.5 {
900 sqlite3_finalize $STMT
901} {SQLITE_ERROR}
902do_test capi3-11.6 {
903 catchsql {
904 SELECT * FROM t1;
905 }
906} {0 {1 int 2 notatype}}
907do_test capi3-11.7 {
908 sqlite3_get_autocommit $DB
909} 1
910do_test capi3-11.8 {
911 execsql {
912 CREATE TABLE t2(a);
913 INSERT INTO t2 VALUES(1);
914 INSERT INTO t2 VALUES(2);
915 BEGIN;
916 INSERT INTO t2 VALUES(3);
917 }
918} {}
919do_test capi3-11.8.1 {
920 sqlite3_get_autocommit $DB
921} 0
922do_test capi3-11.9 {
923 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL]
924 sqlite3_step $STMT
925} {SQLITE_ROW}
926do_test capi3-11.9.1 {
927 sqlite3_get_autocommit $DB
928} 0
929do_test capi3-11.9.2 {
930 catchsql {
931 ROLLBACK;
932 }
933} {0 {}}
934do_test capi3-11.9.3 {
935 sqlite3_get_autocommit $DB
936} 1
937do_test capi3-11.10 {
938 sqlite3_step $STMT
939} {SQLITE_ROW}
940do_test capi3-11.11 {
941 sqlite3_step $STMT
942} {SQLITE_DONE}
943ifcapable !autoreset {
944 do_test capi3-11.12armor {
945 sqlite3_step $STMT
946 sqlite3_step $STMT
947 } {SQLITE_MISUSE}
948} else {
949 do_test capi3-11.12 {
950 sqlite3_step $STMT
951 sqlite3_step $STMT
952 } {SQLITE_ROW}
953}
954do_test capi3-11.13 {
955 sqlite3_finalize $STMT
956} {SQLITE_OK}
957do_test capi3-11.14 {
958 execsql {
959 SELECT a FROM t2;
960 }
961} {1 2}
962do_test capi3-11.14.1 {
963 sqlite3_get_autocommit $DB
964} 1
965do_test capi3-11.15 {
966 catchsql {
967 ROLLBACK;
968 }
969} {1 {cannot rollback - no transaction is active}}
970do_test capi3-11.15.1 {
971 sqlite3_get_autocommit $DB
972} 1
973do_test capi3-11.16 {
974 execsql {
975 SELECT a FROM t2;
976 }
977} {1 2}
978
979# Sanity check on the definition of 'outstanding VM'. This means any VM
980# that has had sqlite3_step() called more recently than sqlite3_finalize() or
981# sqlite3_reset(). So a VM that has just been prepared or reset does not
982# count as an active VM.
983do_test capi3-11.17 {
984 execsql {
985 BEGIN;
986 }
987} {}
988do_test capi3-11.18 {
989 set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL]
990 catchsql {
991 COMMIT;
992 }
993} {0 {}}
994do_test capi3-11.19 {
995 sqlite3_step $STMT
996} {SQLITE_ROW}
997do_test capi3-11.20 {
998 catchsql {
999 BEGIN;
1000 COMMIT;
1001 }
1002} {0 {}}
1003do_test capi3-11.20 {
1004 sqlite3_reset $STMT
1005 catchsql {
1006 COMMIT;
1007 }
1008} {1 {cannot commit - no transaction is active}}
1009do_test capi3-11.21 {
1010 sqlite3_finalize $STMT
1011} {SQLITE_OK}
1012
1013# The following tests - capi3-12.* - check that its Ok to start a
1014# transaction while other VMs are active, and that its Ok to execute
1015# atomic updates in the same situation
1016#
1017do_test capi3-12.1 {
1018 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL]
1019 sqlite3_step $STMT
1020} {SQLITE_ROW}
1021do_test capi3-12.2 {
1022 catchsql {
1023 INSERT INTO t1 VALUES(3, NULL);
1024 }
1025} {0 {}}
1026do_test capi3-12.3 {
1027 catchsql {
1028 INSERT INTO t2 VALUES(4);
1029 }
1030} {0 {}}
1031do_test capi3-12.4 {
1032 catchsql {
1033 BEGIN;
1034 INSERT INTO t1 VALUES(4, NULL);
1035 }
1036} {0 {}}
1037do_test capi3-12.5 {
1038 sqlite3_step $STMT
1039} {SQLITE_ROW}
1040do_test capi3-12.5.1 {
1041 sqlite3_step $STMT
1042} {SQLITE_ROW}
1043do_test capi3-12.6 {
1044 sqlite3_step $STMT
1045} {SQLITE_DONE}
1046do_test capi3-12.7 {
1047 sqlite3_finalize $STMT
1048} {SQLITE_OK}
1049do_test capi3-12.8 {
1050 execsql {
1051 COMMIT;
1052 SELECT a FROM t1;
1053 }
1054} {1 2 3 4}
1055
1056# Test cases capi3-13.* test the sqlite3_clear_bindings() and
1057# sqlite3_sleep APIs.
1058#
1059if {[llength [info commands sqlite3_clear_bindings]]>0} {
1060 do_test capi3-13.1 {
1061 execsql {
1062 DELETE FROM t1;
1063 }
1064 set STMT [sqlite3_prepare $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
1065 sqlite3_step $STMT
1066 } {SQLITE_DONE}
1067 do_test capi3-13.2 {
1068 sqlite3_reset $STMT
1069 sqlite3_bind_text $STMT 1 hello 5
1070 sqlite3_bind_text $STMT 2 world 5
1071 sqlite3_step $STMT
1072 } {SQLITE_DONE}
1073 do_test capi3-13.3 {
1074 sqlite3_reset $STMT
1075 sqlite3_clear_bindings $STMT
1076 sqlite3_step $STMT
1077 } {SQLITE_DONE}
1078 do_test capi3-13-4 {
1079 sqlite3_finalize $STMT
1080 execsql {
1081 SELECT * FROM t1;
1082 }
1083 } {{} {} hello world {} {}}
1084}
1085if {[llength [info commands sqlite3_sleep]]>0} {
1086 do_test capi3-13-5 {
1087 set ms [sqlite3_sleep 80]
1088 expr {$ms==80 || $ms==1000}
1089 } {1}
1090}
1091
1092# Ticket #1219: Make sure binding APIs can handle a NULL pointer.
1093#
1094if {[clang_sanitize_address]==0} {
1095 do_test capi3-14.1-misuse {
1096 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
1097 lappend rc $msg
1098 } {1 SQLITE_MISUSE}
1099}
1100
1101# Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.
1102#
1103do_test capi3-15.1 {
1104 set sql {SELECT * FROM t2}
1105 set nbytes [string length $sql]
1106 append sql { WHERE a==1}
1107 set STMT [sqlite3_prepare $DB $sql $nbytes TAIL]
1108 sqlite3_step $STMT
1109 sqlite3_column_int $STMT 0
1110} {1}
1111do_test capi3-15.2 {
1112 sqlite3_step $STMT
1113 sqlite3_column_int $STMT 0
1114} {2}
1115do_test capi3-15.3 {
1116 sqlite3_finalize $STMT
1117} {SQLITE_OK}
1118do_test capi3-15.4 {
1119 # 123456789 1234567
1120 set sql {SELECT 1234567890}
1121 set STMT [sqlite3_prepare $DB $sql 8 TAIL]
1122 sqlite3_step $STMT
1123 set v1 [sqlite3_column_int $STMT 0]
1124 sqlite3_finalize $STMT
1125 set v1
1126} {1}
1127do_test capi3-15.5 {
1128 # 123456789 1234567
1129 set sql {SELECT 1234567890}
1130 set STMT [sqlite3_prepare $DB $sql 9 TAIL]
1131 sqlite3_step $STMT
1132 set v1 [sqlite3_column_int $STMT 0]
1133 sqlite3_finalize $STMT
1134 set v1
1135} {12}
1136do_test capi3-15.6 {
1137 # 123456789 1234567
1138 set sql {SELECT 1234567890}
1139 set STMT [sqlite3_prepare $DB $sql 12 TAIL]
1140 sqlite3_step $STMT
1141 set v1 [sqlite3_column_int $STMT 0]
1142 sqlite3_finalize $STMT
1143 set v1
1144} {12345}
1145do_test capi3-15.7 {
1146 # 123456789 1234567
1147 set sql {SELECT 12.34567890}
1148 set STMT [sqlite3_prepare $DB $sql 12 TAIL]
1149 sqlite3_step $STMT
1150 set v1 [sqlite3_column_double $STMT 0]
1151 sqlite3_finalize $STMT
1152 set v1
1153} {12.34}
1154do_test capi3-15.8 {
1155 # 123456789 1234567
1156 set sql {SELECT 12.34567890}
1157 set STMT [sqlite3_prepare $DB $sql 14 TAIL]
1158 sqlite3_step $STMT
1159 set v1 [sqlite3_column_double $STMT 0]
1160 sqlite3_finalize $STMT
1161 set v1
1162} {12.3456}
1163
1164# Make sure code is always generated even if an IF EXISTS or
1165# IF NOT EXISTS clause is present that the table does not or
1166# does exists. That way we will always have a prepared statement
1167# to expire when the schema changes.
1168#
1169do_test capi3-16.1 {
1170 set sql {DROP TABLE IF EXISTS t3}
1171 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
1172 sqlite3_finalize $STMT
1173 expr {$STMT!=""}
1174} {1}
1175do_test capi3-16.2 {
1176 set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}
1177 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
1178 sqlite3_finalize $STMT
1179 expr {$STMT!=""}
1180} {1}
1181
1182# But still we do not generate code if there is no SQL
1183#
1184do_test capi3-16.3 {
1185 set STMT [sqlite3_prepare $DB {} -1 TAIL]
1186 sqlite3_finalize $STMT
1187 expr {$STMT==""}
1188} {1}
1189do_test capi3-16.4 {
1190 set STMT [sqlite3_prepare $DB {;} -1 TAIL]
1191 sqlite3_finalize $STMT
1192 expr {$STMT==""}
1193} {1}
1194
1195# Ticket #2426: Misuse of sqlite3_column_* by calling it after
1196# a sqlite3_reset should be harmless.
1197#
1198do_test capi3-17.1 {
1199 set STMT [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
1200 sqlite3_step $STMT
1201 sqlite3_column_int $STMT 0
1202} {1}
1203do_test capi3-17.2 {
1204 sqlite3_reset $STMT
1205 sqlite3_column_int $STMT 0
1206} {0}
1207do_test capi3-17.3 {
1208 sqlite3_finalize $STMT
1209} {SQLITE_OK}
1210
1211# Verify that sqlite3_step() fails with an SQLITE_SCHEMA error
1212# when the statement is prepared with sqlite3_prepare() (not
1213# sqlite3_prepare_v2()) and the schema has changed.
1214#
1215do_test capi3-18.1 {
1216 set STMT [sqlite3_prepare db {SELECT * FROM t2} -1 TAIL]
1217 sqlite3 db2 test.db
1218 db2 eval {CREATE TABLE t3(x)}
1219 db2 close
1220 sqlite3_step $STMT
1221} {SQLITE_ERROR}
1222do_test capi3-18.2 {
1223 sqlite3_reset $STMT
1224 sqlite3_errcode db
1225} {SQLITE_SCHEMA}
1226do_test capi3-18.3 {
1227 sqlite3_errmsg db
1228} {database schema has changed}
1229# The error persist on retry when sqlite3_prepare() has been used.
1230do_test capi3-18.4 {
1231 sqlite3_step $STMT
1232} {SQLITE_ERROR}
1233do_test capi3-18.5 {
1234 sqlite3_reset $STMT
1235 sqlite3_errcode db
1236} {SQLITE_SCHEMA}
1237do_test capi3-18.6 {
1238 sqlite3_errmsg db
1239} {database schema has changed}
1240sqlite3_finalize $STMT
1241
1242# Ticket #3134. Prepare a statement with an nBytes parameter of 0.
1243# Make sure this works correctly and does not reference memory out of
1244# range.
1245#
1246do_test capi3-19.1 {
1247 sqlite3_prepare_tkt3134 db
1248} {}
1249
1250# Test that calling sqlite3_column_blob() on a TEXT value does not change
1251# the return type of subsequent calls to sqlite3_column_type().
1252#
1253do_execsql_test 20.1 {
1254 CREATE TABLE t4(x);
1255 INSERT INTO t4 VALUES('abcdefghij');
1256}
1257do_test 20.2 {
1258 set stmt [sqlite3_prepare db "SELECT * FROM t4" -1 dummy]
1259 sqlite3_step $stmt
1260} {SQLITE_ROW}
1261do_test 20.3 { sqlite3_column_type $stmt 0 } {TEXT}
1262do_test 20.4 { sqlite3_column_blob $stmt 0 } {abcdefghij}
1263do_test 20.5 { sqlite3_column_type $stmt 0 } {TEXT}
1264do_test 20.6 { sqlite3_finalize $stmt } SQLITE_OK
1265
1266
1267# Tests of the interface when no VFS is registered.
1268#
1269if {![info exists tester_do_binarylog]} {
1270 db close
1271 vfs_unregister_all
1272 do_test capi3-20.1 {
1273 sqlite3_sleep 100
1274 } {0}
1275 vfs_reregister_all
1276}
1277
1278finish_test