blob: 9f8409b1471a05335806b8dbe678d773b828c1cc [file] [log] [blame]
dan029ead62011-10-27 15:19:58 +00001# 2001 September 15
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# Test that if sqlite3_prepare_v2() is used to prepare a query, the
12# error-message associated with an sqlite3_step() error is available
13# immediately. Whereas if sqlite3_prepare() is used, it is not available
14# until sqlite3_finalize() or sqlite3_reset() has been called.
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20set testprefix errmsg
21
22# Test organization:
23#
24# errmsg-1.* User-defined SQL function errors
25# errmsg-2.* Errors generated by the VDBE (constraint failures etc.)
26# errmsg-3.* SQLITE_SCHEMA and statement recompilation errors.
27#
28
29proc error_messages_worker {prepare sql schema} {
30 set ret [list]
31
32 set stmt [$prepare db $sql -1 dummy]
33 execsql $schema
34 lappend ret [sqlite3_step $stmt]
35 lappend ret [sqlite3_errmsg db]
36 lappend ret [sqlite3_finalize $stmt]
37 lappend ret [sqlite3_errmsg db]
38
39 set ret
40}
41
42proc error_messages_v2 {sql {schema {}}} {
43 error_messages_worker sqlite3_prepare_v2 $sql $schema
44}
45
46proc error_messages {sql {schema {}}} {
47 error_messages_worker sqlite3_prepare $sql $schema
48}
49
50proc sql_error {msg} { error $msg }
51db func sql_error sql_error
52
53#-------------------------------------------------------------------------
54# Test error messages returned by user-defined SQL functions.
55#
56do_test 1.1 {
57 error_messages "SELECT sql_error('custom message')"
58} [list {*}{
59 SQLITE_ERROR {SQL logic error or missing database}
60 SQLITE_ERROR {custom message}
61}]
62do_test 1.2 {
63 error_messages_v2 "SELECT sql_error('custom message')"
64} [list {*}{
65 SQLITE_ERROR {custom message}
66 SQLITE_ERROR {custom message}
67}]
68
69#-------------------------------------------------------------------------
70# Test error messages generated directly by VDBE code (e.g. constraint
71# failures).
72#
73do_execsql_test 2.1 {
74 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
75 INSERT INTO t1 VALUES('abc', 'def');
76}
77do_test 2.2 {
78 error_messages "INSERT INTO t1 VALUES('ghi', 'def')"
79} [list {*}{
80 SQLITE_ERROR {SQL logic error or missing database}
81 SQLITE_CONSTRAINT {column b is not unique}
82}]
83do_test 2.3 {
84 error_messages_v2 "INSERT INTO t1 VALUES('ghi', 'def')"
85} [list {*}{
86 SQLITE_CONSTRAINT {column b is not unique}
87 SQLITE_CONSTRAINT {column b is not unique}
88}]
89
90#-------------------------------------------------------------------------
91# Test SQLITE_SCHEMA errors. And, for _v2(), test that if the schema
92# change invalidates the SQL statement itself the error message is returned
93# correctly.
94#
95do_execsql_test 3.1.1 {
96 CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
97 INSERT INTO t2 VALUES('abc', 'def');
98}
99do_test 3.1.2 {
100 error_messages "SELECT a FROM t2" "DROP TABLE t2"
101} [list {*}{
102 SQLITE_ERROR {SQL logic error or missing database}
103 SQLITE_SCHEMA {database schema has changed}
104}]
105do_execsql_test 3.2.1 {
106 CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
107 INSERT INTO t2 VALUES('abc', 'def');
108}
109do_test 3.2.2 {
110 error_messages_v2 "SELECT a FROM t2" "DROP TABLE t2"
111} [list {*}{
112 SQLITE_ERROR {no such table: t2}
113 SQLITE_ERROR {no such table: t2}
114}]
115
116finish_test