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