SQLite4
Artifact Content
Not logged in

Artifact 9984aace5d12ae9db493a0ae073c2a96103bce6b:


# 2001 September 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# Test that if sqlite4_prepare_v2() is used to prepare a query, the
# error-message associated with an sqlite4_step() error is available
# immediately. Whereas if sqlite4_prepare() is used, it is not available
# until sqlite4_finalize() or sqlite4_reset() has been called.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

set testprefix errmsg

# Test organization:
#
#   errmsg-1.*         User-defined SQL function errors
#   errmsg-2.*         Errors generated by the VDBE (constraint failures etc.)
#   errmsg-3.*         SQLITE4_SCHEMA and statement recompilation errors.
#

proc error_messages_worker {prepare sql schema} {
  set ret [list]

  set stmt [$prepare db $sql -1 dummy]
  execsql $schema
  lappend ret [sqlite4_step $stmt]
  lappend ret [sqlite4_errmsg db]
  lappend ret [sqlite4_finalize $stmt]
  lappend ret [sqlite4_errmsg db]

  set ret
}

proc error_messages_v2 {sql {schema {}}} {
  error_messages_worker sqlite4_prepare_v2 $sql $schema
}

proc error_messages {sql {schema {}}} {
  error_messages_worker sqlite4_prepare $sql $schema
}

proc sql_error {msg} { error $msg }
db func sql_error sql_error

#-------------------------------------------------------------------------
# Test error messages returned by user-defined SQL functions.
#
do_test 1.1 {
  error_messages "SELECT sql_error('custom message')"
} [list {*}{
    SQLITE4_ERROR {SQL logic error or missing database} 
    SQLITE4_ERROR {custom message}
}]
do_test 1.2 {
  error_messages_v2 "SELECT sql_error('custom message')"
} [list {*}{
    SQLITE4_ERROR {custom message}
    SQLITE4_ERROR {custom message}
}]

#-------------------------------------------------------------------------
# Test error messages generated directly by VDBE code (e.g. constraint
# failures).
#
do_execsql_test 2.1 {
  CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
  INSERT INTO t1 VALUES('abc', 'def');
}
do_test 2.2 {
  error_messages "INSERT INTO t1 VALUES('ghi', 'def')"
} [list {*}{
    SQLITE4_ERROR      {SQL logic error or missing database} 
    SQLITE4_CONSTRAINT {column b is not unique}
}]
do_test 2.3 {
  error_messages_v2 "INSERT INTO t1 VALUES('ghi', 'def')"
} [list {*}{
    SQLITE4_CONSTRAINT {column b is not unique}
    SQLITE4_CONSTRAINT {column b is not unique}
}]

#-------------------------------------------------------------------------
# Test SQLITE4_SCHEMA errors. And, for _v2(), test that if the schema
# change invalidates the SQL statement itself the error message is returned
# correctly.
#
do_execsql_test 3.1.1 {
  CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
  INSERT INTO t2 VALUES('abc', 'def');
}
do_test 3.1.2 {
  error_messages "SELECT a FROM t2" "DROP TABLE t2"
} [list {*}{
    SQLITE4_ERROR {SQL logic error or missing database} 
    SQLITE4_SCHEMA {database schema has changed}
}]
do_execsql_test 3.2.1 {
  CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
  INSERT INTO t2 VALUES('abc', 'def');
}
do_test 3.2.2 {
  error_messages_v2 "SELECT a FROM t2" "DROP TABLE t2"
} [list {*}{
    SQLITE4_ERROR {no such table: t2} 
    SQLITE4_ERROR {no such table: t2}
}]

finish_test