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