3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is creating and dropping virtual tables.
14 # $Id: vtab1.test,v 1.57 2008/08/01 17:51:47 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
20 ifcapable !vtab||!schema_pragmas {
25 #----------------------------------------------------------------------
26 # Organization of tests in this file:
28 # vtab1-1.*: Error conditions and other issues surrounding creation/connection
29 # of a virtual module.
30 # vtab1-2.*: Test sqlite3_declare_vtab() and the xConnect/xDisconnect methods.
31 # vtab1-3.*: Table scans and WHERE clauses.
32 # vtab1-4.*: Table scans and ORDER BY clauses.
33 # vtab1-5.*: Test queries that include joins. This brings the
34 # sqlite3_index_info.estimatedCost variable into play.
35 # vtab1-6.*: Test UPDATE/INSERT/DELETE on vtables.
36 # vtab1-7.*: Test sqlite3_last_insert_rowid().
38 # This file uses the "echo" module (see src/test8.c). Refer to comments
39 # in that file for the special behaviour of the Tcl $echo_module variable.
42 # * How to test the sqlite3_index_constraint_usage.omit field?
45 # vtab1-14.*: Test 'IN' constraints - i.e. "SELECT * FROM t1 WHERE id IN(...)"
47 # vtab1-18.*: Check that the LIKE optimization is not applied when the lhs
48 # is a virtual table column.
52 #----------------------------------------------------------------------
53 # Test cases vtab1.1.*
56 # We cannot create a virtual table if the module has not been registered.
60 CREATE VIRTUAL TABLE t1 USING echo;
63 CREATE VIRTUAL TABLE t1 USING echo;
65 } {1 {no such module: echo}}
68 CREATE VIRTUAL TABLE IF NOT EXISTS t1 USING echo;
70 } {1 {no such module: echo}}
73 SELECT name FROM sqlite_master ORDER BY 1
78 register_echo_module [sqlite3_connection_pointer db]
80 # Once a module has been registered, virtual tables using that module
81 # may be created. However if a module xCreate() fails to call
82 # sqlite3_declare_vtab() an error will be raised and the table not created.
84 # The "echo" module does not invoke sqlite3_declare_vtab() if it is
85 # passed zero arguments.
89 CREATE VIRTUAL TABLE t1 USING echo;
91 } {1 {vtable constructor did not declare schema: t1}}
94 CREATE VIRTUAL TABLE IF NOT EXISTS t1 USING echo;
96 } {1 {vtable constructor did not declare schema: t1}}
99 SELECT name FROM sqlite_master ORDER BY 1
103 # The "echo" module xCreate method returns an error and does not create
104 # the virtual table if it is passed an argument that does not correspond
105 # to an existing real table in the same database.
107 do_test vtab1-1.5.1 {
109 CREATE VIRTUAL TABLE t1 USING echo(no_such_table);
111 } {1 {vtable constructor failed: t1}}
112 do_test vtab1-1.5.2 {
114 CREATE VIRTUAL TABLE IF NOT EXISTS t1 USING echo(no_such_table);
116 } {1 {vtable constructor failed: t1}}
119 SELECT name FROM sqlite_master ORDER BY 1
123 # Ticket #2156. Using the sqlite3_prepare_v2() API, make sure that
124 # a CREATE VIRTUAL TABLE statement can be used multiple times.
126 do_test vtab1-1.2152.1 {
127 set DB [sqlite3_connection_pointer db]
128 set sql {CREATE VIRTUAL TABLE t2152a USING echo(t2152b)}
129 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
132 do_test vtab-1.2152.2 {
136 do_test vtab-1.2152.3 {
138 db eval {CREATE TABLE t2152b(x,y)}
141 do_test vtab-1.2152.4 {
142 sqlite3_finalize $STMT
143 db eval {DROP TABLE t2152a; DROP TABLE t2152b}
146 # Test to make sure nothing goes wrong and no memory is leaked if we
147 # select an illegal table-name (i.e a reserved name or the name of a
148 # table that already exists).
150 do_test vtab1-1.7.1 {
152 CREATE VIRTUAL TABLE sqlite_master USING echo;
154 } {1 {object name reserved for internal use: sqlite_master}}
155 do_test vtab1-1.7.2 {
157 CREATE VIRTUAL TABLE IF NOT EXISTS sqlite_master USING echo;
159 } {1 {object name reserved for internal use: sqlite_master}}
160 do_test vtab1-1.8.1 {
162 CREATE TABLE treal(a, b, c);
163 CREATE VIRTUAL TABLE treal USING echo(treal);
165 } {1 {table treal already exists}}
166 do_test vtab1-1.8.2 {
168 CREATE VIRTUAL TABLE IF NOT EXISTS treal USING echo(treal);
174 SELECT name FROM sqlite_master ORDER BY 1
180 CREATE TABLE treal(a, b, c);
181 CREATE VIRTUAL TABLE techo USING echo(treal);
188 } {1 {no such module: echo}}
191 INSERT INTO techo VALUES(1, 2, 3);
193 } {1 {no such module: echo}}
196 UPDATE techo SET a = 10;
198 } {1 {no such module: echo}}
203 } {1 {no such module: echo}}
206 PRAGMA table_info(techo)
208 } {1 {no such module: echo}}
213 } {1 {no such module: echo}}
215 register_echo_module [sqlite3_connection_pointer db]
216 register_echo_module [sqlite3_connection_pointer db]
218 # Test an error message returned from a v-table constructor.
223 CREATE TABLE logmsg(log);
226 CREATE VIRTUAL TABLE techo USING echo(treal, logmsg);
228 } {1 {table 'logmsg' already exists}}
234 SELECT sql FROM sqlite_master;
238 #----------------------------------------------------------------------
239 # Test cases vtab1.2.*
241 # At this point, the database is completely empty. The echo module
242 # has already been registered.
244 # If a single argument is passed to the echo module during table
245 # creation, it is assumed to be the name of a table in the same
246 # database. The echo module attempts to set the schema of the
247 # new virtual table to be the same as the existing database table.
251 CREATE TABLE template(a, b, c);
253 execsql { PRAGMA table_info(template); }
261 CREATE VIRTUAL TABLE t1 USING echo(template);
263 execsql { PRAGMA table_info(t1); }
270 # Test that the database can be unloaded. This should invoke the xDisconnect()
271 # callback for the successfully create virtual table (t1).
274 set echo_module [list]
279 # Re-open the database. This should not cause any virtual methods to
280 # be called. The invocation of xConnect() is delayed until the virtual
281 # table schema is first required by the compiler.
284 set echo_module [list]
290 # Try to query the virtual table schema. This should fail, as the
291 # echo module has not been registered with this database connection.
294 catchsql { PRAGMA table_info(t1); }
295 } {1 {no such module: echo}}
297 # Register the module
298 register_echo_module [sqlite3_connection_pointer db]
300 # Try to query the virtual table schema again. This time it should
301 # invoke the xConnect method and succeed.
304 execsql { PRAGMA table_info(t1); }
312 } {xConnect echo main t1 template}
314 # Drop table t1. This should cause the xDestroy (but not xDisconnect) method
326 PRAGMA table_info(t1);
331 SELECT sql FROM sqlite_master;
333 } [list {CREATE TABLE template(a, b, c)}]
334 # Clean up other test artifacts:
338 SELECT sql FROM sqlite_master;
342 #----------------------------------------------------------------------
343 # Test case vtab1-3 test table scans and the echo module's
344 # xBestIndex/xFilter handling of WHERE conditions.
349 CREATE TABLE treal(a INTEGER, b INTEGER, c);
350 CREATE INDEX treal_idx ON treal(b);
351 CREATE VIRTUAL TABLE t1 USING echo(treal);
354 } [list xCreate echo main t1 treal \
356 xCommit echo(treal) \
359 # Test that a SELECT on t1 doesn't crash. No rows are returned
360 # because the underlying real table is currently empty.
364 SELECT a, b, c FROM t1;
368 # Put some data into the table treal. Then try a few simple SELECT
373 INSERT INTO treal VALUES(1, 2, 3);
374 INSERT INTO treal VALUES(4, 5, 6);
385 SELECT rowid FROM t1;
396 SELECT rowid, * FROM t1;
401 SELECT a AS d, b AS e, c AS f FROM t1;
405 # Execute some SELECT statements with WHERE clauses on the t1 table.
406 # Then check the echo_module variable (written to by the module methods
407 # in test8.c) to make sure the xBestIndex() and xFilter() methods were
416 } [list xBestIndex {SELECT rowid, * FROM 'treal'} \
417 xFilter {SELECT rowid, * FROM 'treal'} ]
421 SELECT * FROM t1 WHERE b = 5;
426 } [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b = ?} \
427 xFilter {SELECT rowid, * FROM 'treal' WHERE b = ?} 5 ]
431 SELECT * FROM t1 WHERE b >= 5 AND b <= 10;
436 } [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} \
437 xFilter {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 5 10 ]
441 SELECT * FROM t1 WHERE b BETWEEN 2 AND 10;
446 } [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} \
447 xFilter {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 2 10 ]
449 # Add a function for the MATCH operator. Everything always matches!
450 #proc test_match {lhs rhs} {
451 # lappend ::echo_module MATCH $lhs $rhs
454 #db function match test_match
460 SELECT * FROM t1 WHERE a MATCH 'string';
462 } {1 {unable to use function MATCH in the requested context}}
465 } [list xBestIndex {SELECT rowid, * FROM 'treal'} \
466 xFilter {SELECT rowid, * FROM 'treal'}]
468 # The echo module uses a subquery internally to implement the MATCH operator.
472 SELECT * FROM t1 WHERE b MATCH 'string';
478 {SELECT rowid, * FROM 'treal' WHERE b LIKE (SELECT '%'||?||'%')} \
480 {SELECT rowid, * FROM 'treal' WHERE b LIKE (SELECT '%'||?||'%')} \
482 }; #ifcapable subquery
484 #----------------------------------------------------------------------
485 # Test case vtab1-3 test table scans and the echo module's
486 # xBestIndex/xFilter handling of ORDER BY clauses.
488 # This procedure executes the SQL. Then it checks to see if the OP_Sort
489 # opcode was executed. If an OP_Sort did occur, then "sort" is appended
490 # to the result. If no OP_Sort happened, then "nosort" is appended.
492 # This procedure is used to check to make sure sorting is or is not
493 # occurring as expected.
496 set ::sqlite_sort_count 0
497 set data [execsql $sql]
498 if {$::sqlite_sort_count} {set x sort} {set x nosort}
506 SELECT b FROM t1 ORDER BY b;
511 } [list xBestIndex {SELECT rowid, * FROM 'treal' ORDER BY b ASC} \
512 xFilter {SELECT rowid, * FROM 'treal' ORDER BY b ASC} ]
516 SELECT b FROM t1 ORDER BY b DESC;
521 } [list xBestIndex {SELECT rowid, * FROM 'treal' ORDER BY b DESC} \
522 xFilter {SELECT rowid, * FROM 'treal' ORDER BY b DESC} ]
526 SELECT b FROM t1 ORDER BY b||'';
531 } [list xBestIndex {SELECT rowid, * FROM 'treal'} \
532 xFilter {SELECT rowid, * FROM 'treal'} ]
539 #----------------------------------------------------------------------
540 # Test cases vtab1-5 test SELECT queries that include joins on virtual
545 for {set ii 0} {$ii < [llength $log]} {incr ii} {
546 if {[lindex $log $ii] eq "xFilter"} {
548 lappend out [lindex $log [expr $ii+1]]
556 CREATE TABLE t1(a, b, c);
557 CREATE TABLE t2(d, e, f);
558 INSERT INTO t1 VALUES(1, 'red', 'green');
559 INSERT INTO t1 VALUES(2, 'blue', 'black');
560 INSERT INTO t2 VALUES(1, 'spades', 'clubs');
561 INSERT INTO t2 VALUES(2, 'hearts', 'diamonds');
562 CREATE VIRTUAL TABLE et1 USING echo(t1);
563 CREATE VIRTUAL TABLE et2 USING echo(t2);
570 SELECT * FROM et1, et2;
573 1 red green 1 spades clubs \
574 1 red green 2 hearts diamonds \
575 2 blue black 1 spades clubs \
576 2 blue black 2 hearts diamonds \
581 xFilter {SELECT rowid, * FROM 't1'} \
582 xFilter {SELECT rowid, * FROM 't2'} \
583 xFilter {SELECT rowid, * FROM 't2'} \
588 SELECT * FROM et1, et2 WHERE et2.d = 2;
591 1 red green 2 hearts diamonds \
592 2 blue black 2 hearts diamonds \
597 xFilter {SELECT rowid, * FROM 't1'} \
598 xFilter {SELECT rowid, * FROM 't2'} \
599 xFilter {SELECT rowid, * FROM 't2'} \
603 CREATE INDEX i1 ON t2(d);
608 register_echo_module [sqlite3_connection_pointer db]
612 SELECT * FROM et1, et2 WHERE et2.d = 2;
615 1 red green 2 hearts diamonds \
616 2 blue black 2 hearts diamonds \
619 filter $::echo_module
621 xFilter {SELECT rowid, * FROM 't1'} \
622 xFilter {SELECT rowid, * FROM 't2' WHERE d = ?} \
623 xFilter {SELECT rowid, * FROM 't2' WHERE d = ?} \
633 #----------------------------------------------------------------------
634 # Test cases vtab1-6 test INSERT, UPDATE and DELETE operations
637 execsql { SELECT sql FROM sqlite_master }
641 CREATE TABLE treal(a PRIMARY KEY, b, c);
642 CREATE VIRTUAL TABLE techo USING echo(treal);
643 SELECT name FROM sqlite_master WHERE type = 'table';
646 do_test vtab1-6-3.1.1 {
648 PRAGMA count_changes=ON;
649 INSERT INTO techo VALUES(1, 2, 3);
652 do_test vtab1-6-3.1.2 {
655 do_test vtab1-6-3.2 {
660 do_test vtab1-6-4.1 {
662 UPDATE techo SET a = 5;
666 do_test vtab1-6-4.2 {
671 do_test vtab1-6-4.3 {
673 UPDATE techo SET a=6 WHERE a<0;
677 do_test vtab1-6-4.4 {
683 do_test vtab1-6-5.1 {
685 UPDATE techo set a = a||b||c;
689 do_test vtab1-6-5.2 {
695 do_test vtab1-6-6.1 {
697 UPDATE techo set rowid = 10;
701 do_test vtab1-6-6.2 {
703 SELECT rowid FROM techo;
707 do_test vtab1-6-7.1.1 {
709 INSERT INTO techo VALUES(11,12,13);
712 do_test vtab1-6-7.1.2 {
715 do_test vtab1-6-7.2 {
717 SELECT * FROM techo ORDER BY a;
720 do_test vtab1-6-7.3 {
722 UPDATE techo SET b=b+1000
726 do_test vtab1-6-7.4 {
728 SELECT * FROM techo ORDER BY a;
730 } {11 1012 13 523 1002 3}
733 do_test vtab1-6-8.1 {
735 DELETE FROM techo WHERE a=5;
739 do_test vtab1-6-8.2 {
741 SELECT * FROM techo ORDER BY a;
743 } {11 1012 13 523 1002 3}
744 do_test vtab1-6-8.3 {
750 do_test vtab1-6-8.4 {
752 SELECT * FROM techo ORDER BY a;
755 execsql {PRAGMA count_changes=OFF}
758 forcedelete test2.db-journal
761 CREATE TABLE techo(a PRIMARY KEY, b, c);
763 proc check_echo_table {tn} {
764 set ::data1 [execsql {SELECT rowid, * FROM techo}]
765 set ::data2 [execsql {SELECT rowid, * FROM techo} db2]
767 string equal $::data1 $::data2
772 {INSERT INTO techo VALUES('abc', 'def', 'ghi')} \
773 {INSERT INTO techo SELECT a||'.'||rowid, b, c FROM techo} \
774 {INSERT INTO techo SELECT a||'x'||rowid, b, c FROM techo} \
775 {INSERT INTO techo SELECT a||'y'||rowid, b, c FROM techo} \
776 {DELETE FROM techo WHERE (oid % 3) = 0} \
777 {UPDATE techo set rowid = 100 WHERE rowid = 1} \
778 {INSERT INTO techo(a, b) VALUES('hello', 'world')} \
779 {DELETE FROM techo} \
783 check_echo_table vtab1-6.8.[incr tn]
790 #----------------------------------------------------------------------
791 # Test cases vtab1-7 tests that the value returned by
792 # sqlite3_last_insert_rowid() is set correctly when rows are inserted
793 # into virtual tables.
796 CREATE TABLE real_abc(a PRIMARY KEY, b, c);
797 CREATE VIRTUAL TABLE echo_abc USING echo(real_abc);
802 INSERT INTO echo_abc VALUES(1, 2, 3);
803 SELECT last_insert_rowid();
808 INSERT INTO echo_abc(rowid) VALUES(31427);
809 SELECT last_insert_rowid();
814 INSERT INTO echo_abc SELECT a||'.v2', b, c FROM echo_abc;
815 SELECT last_insert_rowid();
820 SELECT rowid, a, b, c FROM echo_abc
828 # Now test that DELETE and UPDATE operations do not modify the value.
831 UPDATE echo_abc SET c = 5 WHERE b = 2;
832 SELECT last_insert_rowid();
837 UPDATE echo_abc SET rowid = 5 WHERE rowid = 1;
838 SELECT last_insert_rowid();
843 DELETE FROM echo_abc WHERE b = 2;
844 SELECT last_insert_rowid();
849 SELECT rowid, a, b, c FROM echo_abc
851 } [list 31427 {} {} {} \
856 DELETE FROM echo_abc WHERE b = 2;
857 SELECT last_insert_rowid();
862 SELECT rowid, a, b, c FROM real_abc
864 } [list 31427 {} {} {} \
869 DELETE FROM echo_abc;
870 SELECT last_insert_rowid();
875 SELECT rowid, a, b, c FROM real_abc
883 ATTACH 'test2.db' AS aux;
884 CREATE VIRTUAL TABLE aux.e2 USING echo(real_abc);
887 } [list xCreate echo aux e2 real_abc \
888 xSync echo(real_abc) \
889 xCommit echo(real_abc) \
907 CREATE TABLE r(a, b, c);
908 CREATE VIRTUAL TABLE e USING echo(r, e_log);
909 SELECT name FROM sqlite_master;
915 SELECT name FROM sqlite_master;
922 CREATE VIRTUAL TABLE e USING echo(r, e_log, virtual 1 2 3 varchar(32));
926 xCreate echo main e r e_log {virtual 1 2 3 varchar(32)} \
934 CREATE VIRTUAL TABLE e2 USING echo(del);
938 register_echo_module [sqlite3_connection_pointer db]
945 } {1 {vtable constructor failed: e2}}
948 set ptr [sqlite3_connection_pointer db]
949 sqlite3_declare_vtab $ptr {CREATE TABLE abc(a, b, c)}
952 } {1 {library routine called out of sequence}}
954 set ::echo_module_begin_fail r
956 INSERT INTO e VALUES(1, 2, 3);
958 } {1 {SQL logic error or missing database}}
961 EXPLAIN SELECT * FROM e WHERE rowid = 2;
962 EXPLAIN QUERY PLAN SELECT * FROM e WHERE rowid = 2 ORDER BY rowid;
969 SELECT * FROM e WHERE rowid||'' MATCH 'pattern';
973 xBestIndex {SELECT rowid, * FROM 'r'} \
974 xFilter {SELECT rowid, * FROM 'r'} \
976 proc match_func {args} {return ""}
979 db function match match_func
981 SELECT * FROM e WHERE match('pattern', rowid, 'pattern2');
985 xBestIndex {SELECT rowid, * FROM 'r'} \
986 xFilter {SELECT rowid, * FROM 'r'} \
990 # Testing the xFindFunction interface
992 catch {rename ::echo_glob_overload {}}
995 INSERT INTO r(a,b,c) VALUES(1,'?',99);
996 INSERT INTO r(a,b,c) VALUES(2,3,99);
997 SELECT a GLOB b FROM e
1000 proc ::echo_glob_overload {a b} {
1003 do_test vtab1.11-2 {
1005 SELECT a like 'b' FROM e
1008 do_test vtab1.11-3 {
1010 SELECT a glob '2' FROM e
1013 do_test vtab1.11-4 {
1015 SELECT glob('2',a) FROM e
1018 do_test vtab1.11-5 {
1020 SELECT glob(a,'2') FROM e
1024 #----------------------------------------------------------------------
1025 # Test the outcome if a constraint is encountered half-way through
1026 # a multi-row INSERT that is inside a transaction
1028 do_test vtab1.12-1 {
1030 CREATE TABLE b(a, b, c);
1031 CREATE TABLE c(a UNIQUE, b, c);
1032 INSERT INTO b VALUES(1, 'A', 'B');
1033 INSERT INTO b VALUES(2, 'C', 'D');
1034 INSERT INTO b VALUES(3, 'E', 'F');
1035 INSERT INTO c VALUES(3, 'G', 'H');
1036 CREATE VIRTUAL TABLE echo_c USING echo(c);
1040 # First test outside of a transaction.
1041 do_test vtab1.12-2 {
1042 catchsql { INSERT INTO echo_c SELECT * FROM b; }
1043 } {1 {echo-vtab-error: UNIQUE constraint failed: c.a}}
1044 do_test vtab1.12-2.1 {
1046 } {echo-vtab-error: UNIQUE constraint failed: c.a}
1047 do_test vtab1.12-3 {
1048 execsql { SELECT * FROM c }
1051 # Now the real test - wrapped in a transaction.
1052 do_test vtab1.12-4 {
1054 catchsql { INSERT INTO echo_c SELECT * FROM b; }
1055 } {1 {echo-vtab-error: UNIQUE constraint failed: c.a}}
1056 do_test vtab1.12-5 {
1057 execsql { SELECT * FROM c }
1059 do_test vtab1.12-6 {
1061 execsql { SELECT * FROM c }
1064 # At one point (ticket #2759), a WHERE clause of the form "<column> IS NULL"
1065 # on a virtual table was causing an assert() to fail in the compiler.
1067 # "IS NULL" clauses should not be passed through to the virtual table
1068 # implementation. They are handled by SQLite after the vtab returns its
1071 do_test vtab1.13-1 {
1073 SELECT * FROM echo_c WHERE a IS NULL
1076 do_test vtab1.13-2 {
1078 INSERT INTO c VALUES(NULL, 15, 16);
1079 SELECT * FROM echo_c WHERE a IS NULL
1082 do_test vtab1.13-3 {
1084 INSERT INTO c VALUES(15, NULL, 16);
1085 SELECT * FROM echo_c WHERE b IS NULL
1088 do_test vtab1.13-3 {
1090 SELECT * FROM echo_c WHERE b IS NULL AND a = 15;
1095 do_test vtab1-14.001 {
1096 execsql {SELECT rowid, * FROM echo_c WHERE +rowid IN (1,2,3)}
1097 } {1 3 G H 2 {} 15 16 3 15 {} 16}
1098 do_test vtab1-14.002 {
1099 execsql {SELECT rowid, * FROM echo_c WHERE rowid IN (1,2,3)}
1100 } {1 3 G H 2 {} 15 16 3 15 {} 16}
1101 do_test vtab1-14.003 {
1102 execsql {SELECT rowid, * FROM echo_c WHERE +rowid IN (0,1,5,2,'a',3,NULL)}
1103 } {1 3 G H 2 {} 15 16 3 15 {} 16}
1104 do_test vtab1-14.004 {
1105 execsql {SELECT rowid, * FROM echo_c WHERE rowid IN (0,1,5,'a',2,3,NULL)}
1106 } {1 3 G H 2 {} 15 16 3 15 {} 16}
1107 do_test vtab1-14.005 {
1108 execsql {SELECT rowid, * FROM echo_c WHERE rowid NOT IN (0,1,5,'a',2,3)}
1110 do_test vtab1-14.006 {
1111 execsql {SELECT rowid, * FROM echo_c WHERE rowid NOT IN (0,5,'a',2,3)}
1113 do_test vtab1-14.007 {
1114 execsql {SELECT rowid, * FROM echo_c WHERE +rowid NOT IN (0,5,'a',2,3,NULL)}
1116 do_test vtab1-14.008 {
1117 execsql {SELECT rowid, * FROM echo_c WHERE rowid NOT IN (0,5,'a',2,3,NULL)}
1119 do_test vtab1-14.011 {
1120 execsql {SELECT * FROM echo_c WHERE +a IN (1,3,8,'x',NULL,15,24)}
1122 do_test vtab1-14.012 {
1123 execsql {SELECT * FROM echo_c WHERE a IN (1,3,8,'x',NULL,15,24)}
1125 do_test vtab1-14.013 {
1126 execsql {SELECT * FROM echo_c WHERE a NOT IN (1,8,'x',15,24)}
1128 do_test vtab1-14.014 {
1129 execsql {SELECT * FROM echo_c WHERE a NOT IN (1,8,'x',NULL,15,24)}
1131 do_test vtab1-14.015 {
1132 execsql {SELECT * FROM echo_c WHERE +a NOT IN (1,8,'x',NULL,15,24)}
1137 #do_test vtab1-14.1 {
1138 # execsql { DELETE FROM c }
1139 # set echo_module ""
1140 # execsql { SELECT * FROM echo_c WHERE rowid IN (1, 2, 3) }
1142 #} {/.*xBestIndex {SELECT rowid, . FROM 'c' WHERE rowid = .} xFilter {SELECT rowid, . FROM 'c'} 1/}
1144 do_test vtab1-14.2 {
1146 execsql { SELECT * FROM echo_c WHERE rowid = 1 }
1148 } [list xBestIndex {SELECT rowid, * FROM 'c' WHERE rowid = ?} xFilter {SELECT rowid, * FROM 'c' WHERE rowid = ?} 1]
1150 do_test vtab1-14.3 {
1152 execsql { SELECT * FROM echo_c WHERE a = 1 }
1154 } [list xBestIndex {SELECT rowid, * FROM 'c' WHERE a = ?} xFilter {SELECT rowid, * FROM 'c' WHERE a = ?} 1]
1156 #do_test vtab1-14.4 {
1157 # set echo_module ""
1158 # execsql { SELECT * FROM echo_c WHERE a IN (1, 2) }
1160 #} {/xBestIndex {SELECT rowid, . FROM 'c' WHERE a = .} xFilter {SELECT rowid, . FROM 'c' WHERE a = .} 1/}
1162 do_test vtab1-15.1 {
1164 CREATE TABLE t1(a, b, c);
1165 CREATE VIRTUAL TABLE echo_t1 USING echo(t1);
1168 do_test vtab1-15.2 {
1170 INSERT INTO echo_t1(rowid) VALUES(45);
1171 SELECT rowid, * FROM echo_t1;
1174 do_test vtab1-15.3 {
1176 INSERT INTO echo_t1(rowid) VALUES(NULL);
1177 SELECT rowid, * FROM echo_t1;
1179 } {45 {} {} {} 46 {} {} {}}
1180 do_test vtab1-15.4 {
1182 INSERT INTO echo_t1(rowid) VALUES('new rowid');
1184 } {1 {datatype mismatch}}
1186 # The following tests - vtab1-16.* - are designed to test that setting
1187 # sqlite3_vtab.zErrMsg variable can be used by the vtab interface to
1188 # return an error message to the user.
1190 do_test vtab1-16.1 {
1192 CREATE TABLE t2(a PRIMARY KEY, b, c);
1193 INSERT INTO t2 VALUES(1, 2, 3);
1194 INSERT INTO t2 VALUES(4, 5, 6);
1195 CREATE VIRTUAL TABLE echo_t2 USING echo(t2);
1200 foreach method [list \
1208 do_test vtab1-16.$tn {
1209 set echo_module_fail($method,t2) "the $method method has failed"
1210 catchsql { SELECT rowid, * FROM echo_t2 WHERE a >= 1 }
1211 } "1 {echo-vtab-error: the $method method has failed}"
1212 unset echo_module_fail($method,t2)
1216 foreach method [list \
1221 do_test vtab1-16.$tn {
1222 set echo_module_fail($method,t2) "the $method method has failed"
1223 catchsql { INSERT INTO echo_t2 VALUES(7, 8, 9) }
1224 } "1 {echo-vtab-error: the $method method has failed}"
1225 unset echo_module_fail($method,t2)
1229 ifcapable altertable {
1230 do_test vtab1-16.$tn {
1231 set echo_module_fail(xRename,t2) "the xRename method has failed"
1232 catchsql { ALTER TABLE echo_t2 RENAME TO another_name }
1233 } "1 {echo-vtab-error: the xRename method has failed}"
1234 unset echo_module_fail(xRename,t2)
1238 # The following test case exposes an instance in sqlite3_declare_vtab()
1239 # an error message was set using a call similar to sqlite3_mprintf(zErr),
1240 # where zErr is an arbitrary string. This is no good if the string contains
1241 # characters that can be mistaken for printf() formatting directives.
1243 do_test vtab1-17.1 {
1245 PRAGMA writable_schema = 1;
1246 INSERT INTO sqlite_master VALUES(
1247 'table', 't3', 't3', 0, 'INSERT INTO "%s%s" VALUES(1)'
1250 catchsql { CREATE VIRTUAL TABLE t4 USING echo(t3); }
1251 } {1 {vtable constructor failed: t4}}
1253 # This test verifies that ticket 48f29963 is fixed.
1255 do_test vtab1-17.1 {
1257 CREATE TABLE t5(a, b);
1258 CREATE VIRTUAL TABLE e5 USING echo_v2(t5);
1260 INSERT INTO e5 VALUES(1, 2);
1268 do_test vtab1-17.2 {
1269 execsql { DELETE FROM sqlite_master WHERE sql LIKE 'insert%' }
1272 #-------------------------------------------------------------------------
1273 # The following tests - vtab1-18.* - test that the optimization of LIKE
1274 # constraints in where.c plays well with virtual tables.
1276 # 18.1.*: Case-insensitive LIKE.
1277 # 18.2.*: Case-sensitive LIKE.
1279 unset -nocomplain echo_module_begin_fail
1281 do_execsql_test 18.1.0 {
1282 CREATE TABLE t6(a, b TEXT);
1283 CREATE INDEX i6 ON t6(b, a);
1284 INSERT INTO t6 VALUES(1, 'Peter');
1285 INSERT INTO t6 VALUES(2, 'Andrew');
1286 INSERT INTO t6 VALUES(3, 'James');
1287 INSERT INTO t6 VALUES(4, 'John');
1288 INSERT INTO t6 VALUES(5, 'Phillip');
1289 INSERT INTO t6 VALUES(6, 'Bartholomew');
1290 CREATE VIRTUAL TABLE e6 USING echo(t6);
1293 foreach {tn sql res filter} {
1294 1.1 "SELECT a FROM e6 WHERE b>'James'" {4 1 5}
1295 {xFilter {SELECT rowid, * FROM 't6' WHERE b > ?} James}
1297 1.2 "SELECT a FROM e6 WHERE b>='J' AND b<'K'" {3 4}
1298 {xFilter {SELECT rowid, * FROM 't6' WHERE b >= ? AND b < ?} J K}
1300 1.3 "SELECT a FROM e6 WHERE b LIKE 'J%'" {3 4}
1301 {xFilter {SELECT rowid, * FROM 't6'}}
1303 1.4 "SELECT a FROM e6 WHERE b LIKE 'j%'" {3 4}
1304 {xFilter {SELECT rowid, * FROM 't6'}}
1307 do_execsql_test 18.$tn.1 $sql $res
1308 do_test 18.$tn.2 { lrange $::echo_module 2 end } $filter
1311 do_execsql_test 18.2.0 { PRAGMA case_sensitive_like = ON }
1312 foreach {tn sql res filter} {
1313 2.1 "SELECT a FROM e6 WHERE b LIKE 'J%'" {3 4}
1314 {xFilter {SELECT rowid, * FROM 't6'}}
1316 2.2 "SELECT a FROM e6 WHERE b LIKE 'j%'" {}
1317 {xFilter {SELECT rowid, * FROM 't6'}}
1320 do_execsql_test 18.$tn.1 $sql $res
1321 do_test 18.$tn.2 { lrange $::echo_module 2 end } $filter
1323 do_execsql_test 18.2.x { PRAGMA case_sensitive_like = OFF }
1325 #-------------------------------------------------------------------------
1326 # Test that an existing module may not be overridden.
1330 register_echo_module [sqlite3_connection_pointer db2]
1333 register_echo_module [sqlite3_connection_pointer db2]
1339 #-------------------------------------------------------------------------
1340 # Test that the bug fixed by [b0c1ba655d69] really is fixed.
1342 do_execsql_test 20.1 {
1343 CREATE TABLE t7 (a, b);
1344 CREATE TABLE t8 (c, d);
1345 CREATE INDEX i2 ON t7(a);
1346 CREATE INDEX i3 ON t7(b);
1347 CREATE INDEX i4 ON t8(c);
1348 CREATE INDEX i5 ON t8(d);
1350 CREATE VIRTUAL TABLE t7v USING echo(t7);
1351 CREATE VIRTUAL TABLE t8v USING echo(t8);
1355 for {set i 0} {$i < 1000} {incr i} {
1356 db eval {INSERT INTO t7 VALUES($i, $i)}
1357 db eval {INSERT INTO t8 VALUES($i, $i)}
1361 do_execsql_test 20.3 {
1363 SELECT a, b FROM t7 WHERE a=11 OR b=12
1365 SELECT c, d FROM t8 WHERE c=5 OR d=6
1368 } {5 5 6 6 11 11 12 12}
1370 do_execsql_test 20.4 {
1372 SELECT a, b FROM t7v WHERE a=11 OR b=12
1374 SELECT c, d FROM t8v WHERE c=5 OR d=6
1377 } {5 5 6 6 11 11 12 12}
1379 #-------------------------------------------------------------------------
1381 do_execsql_test 21.1 {
1382 CREATE TABLE t9(a,b,c);
1383 CREATE VIRTUAL TABLE t9v USING echo(t9);
1385 INSERT INTO t9 VALUES(1,2,3);
1386 INSERT INTO t9 VALUES(3,2,1);
1387 INSERT INTO t9 VALUES(2,2,2);
1390 do_execsql_test 21.2 {
1391 SELECT * FROM t9v WHERE a<b;
1394 do_execsql_test 21.3 {
1395 SELECT * FROM t9v WHERE a=b;