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