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