xref: /sqlite-3.40.0/test/capi3.test (revision 9bccde3d)
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}
928do_test capi3-11.11 {
929  sqlite3_step $STMT
930} {SQLITE_DONE}
931ifcapable api_armor {
932  do_test capi3-11.12armor {
933    sqlite3_step $STMT
934    sqlite3_step $STMT
935  } {SQLITE_MISUSE}
936} else {
937  do_test capi3-11.12 {
938    sqlite3_step $STMT
939    sqlite3_step $STMT
940  } {SQLITE_ROW}
941}
942do_test capi3-11.13 {
943  sqlite3_finalize $STMT
944} {SQLITE_OK}
945do_test capi3-11.14 {
946  execsql {
947    SELECT a FROM t2;
948  }
949} {1 2}
950do_test capi3-11.14.1 {
951  sqlite3_get_autocommit $DB
952} 1
953do_test capi3-11.15 {
954  catchsql {
955    ROLLBACK;
956  }
957} {1 {cannot rollback - no transaction is active}}
958do_test capi3-11.15.1 {
959  sqlite3_get_autocommit $DB
960} 1
961do_test capi3-11.16 {
962  execsql {
963    SELECT a FROM t2;
964  }
965} {1 2}
966
967# Sanity check on the definition of 'outstanding VM'. This means any VM
968# that has had sqlite3_step() called more recently than sqlite3_finalize() or
969# sqlite3_reset(). So a VM that has just been prepared or reset does not
970# count as an active VM.
971do_test capi3-11.17 {
972  execsql {
973    BEGIN;
974  }
975} {}
976do_test capi3-11.18 {
977  set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL]
978  catchsql {
979    COMMIT;
980  }
981} {0 {}}
982do_test capi3-11.19 {
983  sqlite3_step $STMT
984} {SQLITE_ROW}
985do_test capi3-11.20 {
986  catchsql {
987    BEGIN;
988    COMMIT;
989  }
990} {0 {}}
991do_test capi3-11.20 {
992  sqlite3_reset $STMT
993  catchsql {
994    COMMIT;
995  }
996} {1 {cannot commit - no transaction is active}}
997do_test capi3-11.21 {
998  sqlite3_finalize $STMT
999} {SQLITE_OK}
1000
1001# The following tests - capi3-12.* - check that its Ok to start a
1002# transaction while other VMs are active, and that its Ok to execute
1003# atomic updates in the same situation
1004#
1005do_test capi3-12.1 {
1006  set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL]
1007  sqlite3_step $STMT
1008} {SQLITE_ROW}
1009do_test capi3-12.2 {
1010  catchsql {
1011    INSERT INTO t1 VALUES(3, NULL);
1012  }
1013} {0 {}}
1014do_test capi3-12.3 {
1015  catchsql {
1016    INSERT INTO t2 VALUES(4);
1017  }
1018} {0 {}}
1019do_test capi3-12.4 {
1020  catchsql {
1021    BEGIN;
1022    INSERT INTO t1 VALUES(4, NULL);
1023  }
1024} {0 {}}
1025do_test capi3-12.5 {
1026  sqlite3_step $STMT
1027} {SQLITE_ROW}
1028do_test capi3-12.5.1 {
1029  sqlite3_step $STMT
1030} {SQLITE_ROW}
1031do_test capi3-12.6 {
1032  sqlite3_step $STMT
1033} {SQLITE_DONE}
1034do_test capi3-12.7 {
1035  sqlite3_finalize $STMT
1036} {SQLITE_OK}
1037do_test capi3-12.8 {
1038  execsql {
1039    COMMIT;
1040    SELECT a FROM t1;
1041  }
1042} {1 2 3 4}
1043
1044# Test cases capi3-13.* test the sqlite3_clear_bindings() and
1045# sqlite3_sleep APIs.
1046#
1047if {[llength [info commands sqlite3_clear_bindings]]>0} {
1048  do_test capi3-13.1 {
1049    execsql {
1050      DELETE FROM t1;
1051    }
1052    set STMT [sqlite3_prepare $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
1053    sqlite3_step $STMT
1054  } {SQLITE_DONE}
1055  do_test capi3-13.2 {
1056    sqlite3_reset $STMT
1057    sqlite3_bind_text $STMT 1 hello 5
1058    sqlite3_bind_text $STMT 2 world 5
1059    sqlite3_step $STMT
1060  } {SQLITE_DONE}
1061  do_test capi3-13.3 {
1062    sqlite3_reset $STMT
1063    sqlite3_clear_bindings $STMT
1064    sqlite3_step $STMT
1065  } {SQLITE_DONE}
1066  do_test capi3-13-4 {
1067    sqlite3_finalize $STMT
1068    execsql {
1069      SELECT * FROM t1;
1070    }
1071  } {{} {} hello world {} {}}
1072}
1073if {[llength [info commands sqlite3_sleep]]>0} {
1074  do_test capi3-13-5 {
1075    set ms [sqlite3_sleep 80]
1076    expr {$ms==80 || $ms==1000}
1077  } {1}
1078}
1079
1080# Ticket #1219:  Make sure binding APIs can handle a NULL pointer.
1081#
1082if {[clang_sanitize_address]==0} {
1083  do_test capi3-14.1-misuse {
1084    set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
1085      lappend rc $msg
1086  } {1 SQLITE_MISUSE}
1087}
1088
1089# Ticket #1650:  Honor the nBytes parameter to sqlite3_prepare.
1090#
1091do_test capi3-15.1 {
1092  set sql {SELECT * FROM t2}
1093  set nbytes [string length $sql]
1094  append sql { WHERE a==1}
1095  set STMT [sqlite3_prepare $DB $sql $nbytes TAIL]
1096  sqlite3_step $STMT
1097  sqlite3_column_int $STMT 0
1098} {1}
1099do_test capi3-15.2 {
1100  sqlite3_step $STMT
1101  sqlite3_column_int $STMT 0
1102} {2}
1103do_test capi3-15.3 {
1104  sqlite3_finalize $STMT
1105} {SQLITE_OK}
1106do_test capi3-15.4 {
1107  #        123456789 1234567
1108  set sql {SELECT 1234567890}
1109  set STMT [sqlite3_prepare $DB $sql 8 TAIL]
1110  sqlite3_step $STMT
1111  set v1 [sqlite3_column_int $STMT 0]
1112  sqlite3_finalize $STMT
1113  set v1
1114} {1}
1115do_test capi3-15.5 {
1116  #        123456789 1234567
1117  set sql {SELECT 1234567890}
1118  set STMT [sqlite3_prepare $DB $sql 9 TAIL]
1119  sqlite3_step $STMT
1120  set v1 [sqlite3_column_int $STMT 0]
1121  sqlite3_finalize $STMT
1122  set v1
1123} {12}
1124do_test capi3-15.6 {
1125  #        123456789 1234567
1126  set sql {SELECT 1234567890}
1127  set STMT [sqlite3_prepare $DB $sql 12 TAIL]
1128  sqlite3_step $STMT
1129  set v1 [sqlite3_column_int $STMT 0]
1130  sqlite3_finalize $STMT
1131  set v1
1132} {12345}
1133do_test capi3-15.7 {
1134  #        123456789 1234567
1135  set sql {SELECT 12.34567890}
1136  set STMT [sqlite3_prepare $DB $sql 12 TAIL]
1137  sqlite3_step $STMT
1138  set v1 [sqlite3_column_double $STMT 0]
1139  sqlite3_finalize $STMT
1140  set v1
1141} {12.34}
1142do_test capi3-15.8 {
1143  #        123456789 1234567
1144  set sql {SELECT 12.34567890}
1145  set STMT [sqlite3_prepare $DB $sql 14 TAIL]
1146  sqlite3_step $STMT
1147  set v1 [sqlite3_column_double $STMT 0]
1148  sqlite3_finalize $STMT
1149  set v1
1150} {12.3456}
1151
1152# Make sure code is always generated even if an IF EXISTS or
1153# IF NOT EXISTS clause is present that the table does not or
1154# does exists.  That way we will always have a prepared statement
1155# to expire when the schema changes.
1156#
1157do_test capi3-16.1 {
1158  set sql {DROP TABLE IF EXISTS t3}
1159  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
1160  sqlite3_finalize $STMT
1161  expr {$STMT!=""}
1162} {1}
1163do_test capi3-16.2 {
1164  set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}
1165  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
1166  sqlite3_finalize $STMT
1167  expr {$STMT!=""}
1168} {1}
1169
1170# But still we do not generate code if there is no SQL
1171#
1172do_test capi3-16.3 {
1173  set STMT [sqlite3_prepare $DB {} -1 TAIL]
1174  sqlite3_finalize $STMT
1175  expr {$STMT==""}
1176} {1}
1177do_test capi3-16.4 {
1178  set STMT [sqlite3_prepare $DB {;} -1 TAIL]
1179  sqlite3_finalize $STMT
1180  expr {$STMT==""}
1181} {1}
1182
1183# Ticket #2426:  Misuse of sqlite3_column_* by calling it after
1184# a sqlite3_reset should be harmless.
1185#
1186do_test capi3-17.1 {
1187  set STMT [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
1188  sqlite3_step $STMT
1189  sqlite3_column_int $STMT 0
1190} {1}
1191do_test capi3-17.2 {
1192  sqlite3_reset $STMT
1193  sqlite3_column_int $STMT 0
1194} {0}
1195do_test capi3-17.3 {
1196  sqlite3_finalize $STMT
1197} {SQLITE_OK}
1198
1199# Verify that sqlite3_step() fails with an SQLITE_SCHEMA error
1200# when the statement is prepared with sqlite3_prepare() (not
1201# sqlite3_prepare_v2()) and the schema has changed.
1202#
1203do_test capi3-18.1 {
1204  set STMT [sqlite3_prepare db {SELECT * FROM t2} -1 TAIL]
1205  sqlite3 db2 test.db
1206  db2 eval {CREATE TABLE t3(x)}
1207  db2 close
1208  sqlite3_step $STMT
1209} {SQLITE_ERROR}
1210do_test capi3-18.2 {
1211  sqlite3_reset $STMT
1212  sqlite3_errcode db
1213} {SQLITE_SCHEMA}
1214do_test capi3-18.3 {
1215  sqlite3_errmsg db
1216} {database schema has changed}
1217# The error persist on retry when sqlite3_prepare() has been used.
1218do_test capi3-18.4 {
1219  sqlite3_step $STMT
1220} {SQLITE_ERROR}
1221do_test capi3-18.5 {
1222  sqlite3_reset $STMT
1223  sqlite3_errcode db
1224} {SQLITE_SCHEMA}
1225do_test capi3-18.6 {
1226  sqlite3_errmsg db
1227} {database schema has changed}
1228sqlite3_finalize $STMT
1229
1230# Ticket #3134.  Prepare a statement with an nBytes parameter of 0.
1231# Make sure this works correctly and does not reference memory out of
1232# range.
1233#
1234do_test capi3-19.1 {
1235  sqlite3_prepare_tkt3134 db
1236} {}
1237
1238# Test that calling sqlite3_column_blob() on a TEXT value does not change
1239# the return type of subsequent calls to sqlite3_column_type().
1240#
1241do_execsql_test 20.1 {
1242  CREATE TABLE t4(x);
1243  INSERT INTO t4 VALUES('abcdefghij');
1244}
1245do_test 20.2 {
1246  set stmt [sqlite3_prepare db "SELECT * FROM t4" -1 dummy]
1247  sqlite3_step $stmt
1248} {SQLITE_ROW}
1249do_test 20.3 { sqlite3_column_type $stmt 0 } {TEXT}
1250do_test 20.4 { sqlite3_column_blob $stmt 0 } {abcdefghij}
1251do_test 20.5 { sqlite3_column_type $stmt 0 } {TEXT}
1252do_test 20.6 { sqlite3_finalize $stmt } SQLITE_OK
1253
1254
1255# Tests of the interface when no VFS is registered.
1256#
1257if {![info exists tester_do_binarylog]} {
1258  db close
1259  vfs_unregister_all
1260  do_test capi3-20.1 {
1261    sqlite3_sleep 100
1262  } {0}
1263  vfs_reregister_all
1264}
1265
1266finish_test
1267