os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/vtab1.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2006 June 10
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is creating and dropping virtual tables.
sl@0
    13
#
sl@0
    14
# $Id: vtab1.test,v 1.57 2008/08/01 17:51:47 danielk1977 Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
ifcapable !vtab||!schema_pragmas {
sl@0
    20
  finish_test
sl@0
    21
  return
sl@0
    22
}
sl@0
    23
sl@0
    24
#----------------------------------------------------------------------
sl@0
    25
# Organization of tests in this file:
sl@0
    26
#
sl@0
    27
# vtab1-1.*: Error conditions and other issues surrounding creation/connection
sl@0
    28
#            of a virtual module.
sl@0
    29
# vtab1-2.*: Test sqlite3_declare_vtab() and the xConnect/xDisconnect methods.
sl@0
    30
# vtab1-3.*: Table scans and WHERE clauses.
sl@0
    31
# vtab1-4.*: Table scans and ORDER BY clauses.
sl@0
    32
# vtab1-5.*: Test queries that include joins. This brings the
sl@0
    33
#            sqlite3_index_info.estimatedCost variable into play.
sl@0
    34
# vtab1-6.*: Test UPDATE/INSERT/DELETE on vtables.
sl@0
    35
# vtab1-7.*: Test sqlite3_last_insert_rowid().
sl@0
    36
#
sl@0
    37
# This file uses the "echo" module (see src/test8.c). Refer to comments
sl@0
    38
# in that file for the special behaviour of the Tcl $echo_module variable.
sl@0
    39
#
sl@0
    40
# TODO: 
sl@0
    41
#   * How to test the sqlite3_index_constraint_usage.omit field?
sl@0
    42
#   * vtab1-5.*
sl@0
    43
#
sl@0
    44
# vtab1-14.*: Test 'IN' constraints - i.e. "SELECT * FROM t1 WHERE id IN(...)"
sl@0
    45
#
sl@0
    46
sl@0
    47
sl@0
    48
#----------------------------------------------------------------------
sl@0
    49
# Test cases vtab1.1.*
sl@0
    50
#
sl@0
    51
sl@0
    52
# We cannot create a virtual table if the module has not been registered.
sl@0
    53
#
sl@0
    54
do_test vtab1-1.1 {
sl@0
    55
  explain {
sl@0
    56
    CREATE VIRTUAL TABLE t1 USING echo;
sl@0
    57
  }
sl@0
    58
  catchsql {
sl@0
    59
    CREATE VIRTUAL TABLE t1 USING echo;
sl@0
    60
  }
sl@0
    61
} {1 {no such module: echo}}
sl@0
    62
do_test vtab1-1.2 {
sl@0
    63
  execsql {
sl@0
    64
    SELECT name FROM sqlite_master ORDER BY 1
sl@0
    65
  }
sl@0
    66
} {}
sl@0
    67
sl@0
    68
# Register the module
sl@0
    69
register_echo_module [sqlite3_connection_pointer db]
sl@0
    70
sl@0
    71
# Once a module has been registered, virtual tables using that module
sl@0
    72
# may be created. However if a module xCreate() fails to call
sl@0
    73
# sqlite3_declare_vtab() an error will be raised and the table not created.
sl@0
    74
#
sl@0
    75
# The "echo" module does not invoke sqlite3_declare_vtab() if it is
sl@0
    76
# passed zero arguments.
sl@0
    77
#
sl@0
    78
do_test vtab1-1.3 {
sl@0
    79
  catchsql {
sl@0
    80
    CREATE VIRTUAL TABLE t1 USING echo;
sl@0
    81
  }
sl@0
    82
} {1 {vtable constructor did not declare schema: t1}}
sl@0
    83
do_test vtab1-1.4 {
sl@0
    84
  execsql {
sl@0
    85
    SELECT name FROM sqlite_master ORDER BY 1
sl@0
    86
  }
sl@0
    87
} {}
sl@0
    88
sl@0
    89
# The "echo" module xCreate method returns an error and does not create
sl@0
    90
# the virtual table if it is passed an argument that does not correspond
sl@0
    91
# to an existing real table in the same database.
sl@0
    92
#
sl@0
    93
do_test vtab1-1.5 {
sl@0
    94
  catchsql {
sl@0
    95
    CREATE VIRTUAL TABLE t1 USING echo(no_such_table);
sl@0
    96
  }
sl@0
    97
} {1 {vtable constructor failed: t1}}
sl@0
    98
do_test vtab1-1.6 {
sl@0
    99
  execsql {
sl@0
   100
    SELECT name FROM sqlite_master ORDER BY 1
sl@0
   101
  }
sl@0
   102
} {}
sl@0
   103
sl@0
   104
# Ticket #2156.  Using the sqlite3_prepare_v2() API, make sure that
sl@0
   105
# a CREATE VIRTUAL TABLE statement can be used multiple times.
sl@0
   106
#
sl@0
   107
do_test vtab1-1.2152.1 {
sl@0
   108
  set DB [sqlite3_connection_pointer db]
sl@0
   109
  set sql {CREATE VIRTUAL TABLE t2152a USING echo(t2152b)}
sl@0
   110
  set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
sl@0
   111
  sqlite3_step $STMT
sl@0
   112
} SQLITE_ERROR
sl@0
   113
do_test vtab-1.2152.2 {
sl@0
   114
  sqlite3_reset $STMT
sl@0
   115
  sqlite3_step $STMT
sl@0
   116
} SQLITE_ERROR
sl@0
   117
do_test vtab-1.2152.3 {
sl@0
   118
  sqlite3_reset $STMT
sl@0
   119
  db eval {CREATE TABLE t2152b(x,y)}
sl@0
   120
  sqlite3_step $STMT
sl@0
   121
} SQLITE_DONE
sl@0
   122
do_test vtab-1.2152.4 {
sl@0
   123
  sqlite3_finalize $STMT
sl@0
   124
  db eval {DROP TABLE t2152a; DROP TABLE t2152b}
sl@0
   125
} {}
sl@0
   126
sl@0
   127
# Test to make sure nothing goes wrong and no memory is leaked if we 
sl@0
   128
# select an illegal table-name (i.e a reserved name or the name of a
sl@0
   129
# table that already exists).
sl@0
   130
#
sl@0
   131
do_test vtab1-1.7 {
sl@0
   132
  catchsql {
sl@0
   133
    CREATE VIRTUAL TABLE sqlite_master USING echo;
sl@0
   134
  }
sl@0
   135
} {1 {object name reserved for internal use: sqlite_master}}
sl@0
   136
do_test vtab1-1.8 {
sl@0
   137
  catchsql {
sl@0
   138
    CREATE TABLE treal(a, b, c);
sl@0
   139
    CREATE VIRTUAL TABLE treal USING echo(treal);
sl@0
   140
  }
sl@0
   141
} {1 {table treal already exists}}
sl@0
   142
do_test vtab1-1.9 {
sl@0
   143
  execsql {
sl@0
   144
    DROP TABLE treal;
sl@0
   145
    SELECT name FROM sqlite_master ORDER BY 1
sl@0
   146
  }
sl@0
   147
} {}
sl@0
   148
sl@0
   149
do_test vtab1-1.10 {
sl@0
   150
  execsql {
sl@0
   151
    CREATE TABLE treal(a, b, c);
sl@0
   152
    CREATE VIRTUAL TABLE techo USING echo(treal);
sl@0
   153
  }
sl@0
   154
  db close
sl@0
   155
  sqlite3 db test.db
sl@0
   156
  catchsql {
sl@0
   157
    SELECT * FROM techo;
sl@0
   158
  }
sl@0
   159
} {1 {no such module: echo}}
sl@0
   160
do_test vtab1-1.11 {
sl@0
   161
  catchsql {
sl@0
   162
    INSERT INTO techo VALUES(1, 2, 3);
sl@0
   163
  }
sl@0
   164
} {1 {no such module: echo}}
sl@0
   165
do_test vtab1-1.12 {
sl@0
   166
  catchsql {
sl@0
   167
    UPDATE techo SET a = 10;
sl@0
   168
  }
sl@0
   169
} {1 {no such module: echo}}
sl@0
   170
do_test vtab1-1.13 {
sl@0
   171
  catchsql {
sl@0
   172
    DELETE FROM techo;
sl@0
   173
  }
sl@0
   174
} {1 {no such module: echo}}
sl@0
   175
do_test vtab1-1.14 {
sl@0
   176
  catchsql {
sl@0
   177
    PRAGMA table_info(techo)
sl@0
   178
  }
sl@0
   179
} {1 {no such module: echo}}
sl@0
   180
do_test vtab1-1.15 {
sl@0
   181
  catchsql {
sl@0
   182
    DROP TABLE techo;
sl@0
   183
  }
sl@0
   184
} {1 {no such module: echo}}
sl@0
   185
sl@0
   186
register_echo_module [sqlite3_connection_pointer db]
sl@0
   187
register_echo_module [sqlite3_connection_pointer db]
sl@0
   188
sl@0
   189
# Test an error message returned from a v-table constructor.
sl@0
   190
#
sl@0
   191
do_test vtab1-1.16 {
sl@0
   192
  execsql {
sl@0
   193
    DROP TABLE techo;
sl@0
   194
    CREATE TABLE logmsg(log);
sl@0
   195
  }
sl@0
   196
  catchsql {
sl@0
   197
    CREATE VIRTUAL TABLE techo USING echo(treal, logmsg);
sl@0
   198
  }
sl@0
   199
} {1 {table 'logmsg' already exists}}
sl@0
   200
sl@0
   201
do_test vtab1-1.17 {
sl@0
   202
  execsql {
sl@0
   203
    DROP TABLE treal;
sl@0
   204
    DROP TABLE logmsg;
sl@0
   205
    SELECT sql FROM sqlite_master;
sl@0
   206
  }
sl@0
   207
} {}
sl@0
   208
sl@0
   209
#----------------------------------------------------------------------
sl@0
   210
# Test cases vtab1.2.*
sl@0
   211
#
sl@0
   212
# At this point, the database is completely empty. The echo module
sl@0
   213
# has already been registered.
sl@0
   214
sl@0
   215
# If a single argument is passed to the echo module during table
sl@0
   216
# creation, it is assumed to be the name of a table in the same
sl@0
   217
# database. The echo module attempts to set the schema of the
sl@0
   218
# new virtual table to be the same as the existing database table.
sl@0
   219
#
sl@0
   220
do_test vtab1-2.1 {
sl@0
   221
  execsql {
sl@0
   222
    CREATE TABLE template(a, b, c);
sl@0
   223
  }
sl@0
   224
  execsql { PRAGMA table_info(template); }
sl@0
   225
} [list         \
sl@0
   226
  0 a {} 0 {} 0 \
sl@0
   227
  1 b {} 0 {} 0 \
sl@0
   228
  2 c {} 0 {} 0 \
sl@0
   229
]
sl@0
   230
do_test vtab1-2.2 {
sl@0
   231
  execsql {
sl@0
   232
    CREATE VIRTUAL TABLE t1 USING echo(template);
sl@0
   233
  }
sl@0
   234
  execsql { PRAGMA table_info(t1); }
sl@0
   235
} [list         \
sl@0
   236
  0 a {} 0 {} 0 \
sl@0
   237
  1 b {} 0 {} 0 \
sl@0
   238
  2 c {} 0 {} 0 \
sl@0
   239
]
sl@0
   240
sl@0
   241
# Test that the database can be unloaded. This should invoke the xDisconnect()
sl@0
   242
# callback for the successfully create virtual table (t1).
sl@0
   243
#
sl@0
   244
do_test vtab1-2.3 {
sl@0
   245
  set echo_module [list]
sl@0
   246
  db close
sl@0
   247
  set echo_module
sl@0
   248
} [list xDisconnect]
sl@0
   249
sl@0
   250
# Re-open the database. This should not cause any virtual methods to 
sl@0
   251
# be called. The invocation of xConnect() is delayed until the virtual
sl@0
   252
# table schema is first required by the compiler.
sl@0
   253
#
sl@0
   254
do_test vtab1-2.4 {
sl@0
   255
  set echo_module [list]
sl@0
   256
  sqlite3 db test.db
sl@0
   257
  db cache size 0
sl@0
   258
  set echo_module
sl@0
   259
} {}
sl@0
   260
sl@0
   261
# Try to query the virtual table schema. This should fail, as the
sl@0
   262
# echo module has not been registered with this database connection.
sl@0
   263
#
sl@0
   264
do_test vtab1.2.6 {
sl@0
   265
  catchsql { PRAGMA table_info(t1); }
sl@0
   266
} {1 {no such module: echo}}
sl@0
   267
sl@0
   268
# Register the module
sl@0
   269
register_echo_module [sqlite3_connection_pointer db]
sl@0
   270
sl@0
   271
# Try to query the virtual table schema again. This time it should
sl@0
   272
# invoke the xConnect method and succeed.
sl@0
   273
#
sl@0
   274
do_test vtab1.2.7 {
sl@0
   275
  execsql { PRAGMA table_info(t1); }
sl@0
   276
} [list         \
sl@0
   277
  0 a {} 0 {} 0 \
sl@0
   278
  1 b {} 0 {} 0 \
sl@0
   279
  2 c {} 0 {} 0 \
sl@0
   280
]
sl@0
   281
do_test vtab1.2.8 {
sl@0
   282
  set echo_module
sl@0
   283
} {xConnect echo main t1 template}
sl@0
   284
sl@0
   285
# Drop table t1. This should cause the xDestroy (but not xDisconnect) method 
sl@0
   286
# to be invoked.
sl@0
   287
do_test vtab1-2.5 {
sl@0
   288
  set echo_module ""
sl@0
   289
  execsql {
sl@0
   290
    DROP TABLE t1;
sl@0
   291
  }
sl@0
   292
  set echo_module
sl@0
   293
} {xDestroy}
sl@0
   294
sl@0
   295
do_test vtab1-2.6 {
sl@0
   296
  execsql { 
sl@0
   297
    PRAGMA table_info(t1); 
sl@0
   298
  }
sl@0
   299
} {}
sl@0
   300
do_test vtab1-2.7 {
sl@0
   301
  execsql {
sl@0
   302
    SELECT sql FROM sqlite_master;
sl@0
   303
  }
sl@0
   304
} [list {CREATE TABLE template(a, b, c)}]
sl@0
   305
# Clean up other test artifacts:
sl@0
   306
do_test vtab1-2.8 {
sl@0
   307
  execsql { 
sl@0
   308
    DROP TABLE template;
sl@0
   309
    SELECT sql FROM sqlite_master;
sl@0
   310
  }
sl@0
   311
} [list]
sl@0
   312
sl@0
   313
#----------------------------------------------------------------------
sl@0
   314
# Test case vtab1-3 test table scans and the echo module's 
sl@0
   315
# xBestIndex/xFilter handling of WHERE conditions.
sl@0
   316
sl@0
   317
do_test vtab1-3.1 {
sl@0
   318
  set echo_module ""
sl@0
   319
  execsql {
sl@0
   320
    CREATE TABLE treal(a INTEGER, b INTEGER, c); 
sl@0
   321
    CREATE INDEX treal_idx ON treal(b);
sl@0
   322
    CREATE VIRTUAL TABLE t1 USING echo(treal);
sl@0
   323
  }
sl@0
   324
  set echo_module
sl@0
   325
} [list xCreate echo main t1 treal   \
sl@0
   326
        xSync   echo(treal)  \
sl@0
   327
        xCommit echo(treal)  \
sl@0
   328
]
sl@0
   329
sl@0
   330
# Test that a SELECT on t1 doesn't crash. No rows are returned
sl@0
   331
# because the underlying real table is currently empty.
sl@0
   332
#
sl@0
   333
do_test vtab1-3.2 {
sl@0
   334
  execsql {
sl@0
   335
    SELECT a, b, c FROM t1;
sl@0
   336
  }
sl@0
   337
} {}
sl@0
   338
sl@0
   339
# Put some data into the table treal. Then try a few simple SELECT 
sl@0
   340
# statements on t1.
sl@0
   341
#
sl@0
   342
do_test vtab1-3.3 {
sl@0
   343
  execsql {
sl@0
   344
    INSERT INTO treal VALUES(1, 2, 3);
sl@0
   345
    INSERT INTO treal VALUES(4, 5, 6);
sl@0
   346
    SELECT * FROM t1;
sl@0
   347
  }
sl@0
   348
} {1 2 3 4 5 6}
sl@0
   349
do_test vtab1-3.4 {
sl@0
   350
  execsql {
sl@0
   351
    SELECT a FROM t1;
sl@0
   352
  }
sl@0
   353
} {1 4}
sl@0
   354
do_test vtab1-3.5 {
sl@0
   355
  execsql {
sl@0
   356
    SELECT rowid FROM t1;
sl@0
   357
  }
sl@0
   358
} {1 2}
sl@0
   359
do_test vtab1-3.6 {
sl@0
   360
  set echo_module ""
sl@0
   361
  execsql {
sl@0
   362
    SELECT * FROM t1;
sl@0
   363
  }
sl@0
   364
} {1 2 3 4 5 6}
sl@0
   365
do_test vtab1-3.7 {
sl@0
   366
  execsql {
sl@0
   367
    SELECT rowid, * FROM t1;
sl@0
   368
  }
sl@0
   369
} {1 1 2 3 2 4 5 6}
sl@0
   370
do_test vtab1-3.8 {
sl@0
   371
  execsql {
sl@0
   372
    SELECT a AS d, b AS e, c AS f FROM t1;
sl@0
   373
  }
sl@0
   374
} {1 2 3 4 5 6}
sl@0
   375
sl@0
   376
# Execute some SELECT statements with WHERE clauses on the t1 table.
sl@0
   377
# Then check the echo_module variable (written to by the module methods
sl@0
   378
# in test8.c) to make sure the xBestIndex() and xFilter() methods were
sl@0
   379
# called correctly.
sl@0
   380
#
sl@0
   381
do_test vtab1-3.8 {
sl@0
   382
  set echo_module ""
sl@0
   383
  execsql {
sl@0
   384
    SELECT * FROM t1;
sl@0
   385
  }
sl@0
   386
  set echo_module
sl@0
   387
} [list xBestIndex {SELECT rowid, * FROM 'treal'} \
sl@0
   388
        xFilter    {SELECT rowid, * FROM 'treal'} ]
sl@0
   389
do_test vtab1-3.9 {
sl@0
   390
  set echo_module ""
sl@0
   391
  execsql {
sl@0
   392
    SELECT * FROM t1 WHERE b = 5;
sl@0
   393
  }
sl@0
   394
} {4 5 6}
sl@0
   395
do_test vtab1-3.10 {
sl@0
   396
  set echo_module
sl@0
   397
} [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b = ?}   \
sl@0
   398
        xFilter    {SELECT rowid, * FROM 'treal' WHERE b = ?} 5 ]
sl@0
   399
do_test vtab1-3.10 {
sl@0
   400
  set echo_module ""
sl@0
   401
  execsql {
sl@0
   402
    SELECT * FROM t1 WHERE b >= 5 AND b <= 10;
sl@0
   403
  }
sl@0
   404
} {4 5 6}
sl@0
   405
do_test vtab1-3.11 {
sl@0
   406
  set echo_module
sl@0
   407
} [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?}      \
sl@0
   408
        xFilter    {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 5 10 ]
sl@0
   409
do_test vtab1-3.12 {
sl@0
   410
  set echo_module ""
sl@0
   411
  execsql {
sl@0
   412
    SELECT * FROM t1 WHERE b BETWEEN 2 AND 10;
sl@0
   413
  }
sl@0
   414
} {1 2 3 4 5 6}
sl@0
   415
do_test vtab1-3.13 {
sl@0
   416
  set echo_module
sl@0
   417
} [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?}      \
sl@0
   418
        xFilter    {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 2 10 ]
sl@0
   419
sl@0
   420
# Add a function for the MATCH operator. Everything always matches!
sl@0
   421
#proc test_match {lhs rhs} {
sl@0
   422
#  lappend ::echo_module MATCH $lhs $rhs
sl@0
   423
#  return 1
sl@0
   424
#}
sl@0
   425
#db function match test_match
sl@0
   426
sl@0
   427
set echo_module ""
sl@0
   428
do_test vtab1-3.12 {
sl@0
   429
  set echo_module ""
sl@0
   430
  catchsql {
sl@0
   431
    SELECT * FROM t1 WHERE a MATCH 'string';
sl@0
   432
  }
sl@0
   433
} {1 {unable to use function MATCH in the requested context}}
sl@0
   434
do_test vtab1-3.13 {
sl@0
   435
  set echo_module
sl@0
   436
} [list xBestIndex {SELECT rowid, * FROM 'treal'} \
sl@0
   437
        xFilter    {SELECT rowid, * FROM 'treal'}]
sl@0
   438
ifcapable subquery {
sl@0
   439
# The echo module uses a subquery internally to implement the MATCH operator.
sl@0
   440
do_test vtab1-3.14 {
sl@0
   441
  set echo_module ""
sl@0
   442
  execsql {
sl@0
   443
    SELECT * FROM t1 WHERE b MATCH 'string';
sl@0
   444
  }
sl@0
   445
} {}
sl@0
   446
do_test vtab1-3.15 {
sl@0
   447
  set echo_module
sl@0
   448
} [list xBestIndex \
sl@0
   449
        {SELECT rowid, * FROM 'treal' WHERE b LIKE (SELECT '%'||?||'%')}  \
sl@0
   450
        xFilter \
sl@0
   451
        {SELECT rowid, * FROM 'treal' WHERE b LIKE (SELECT '%'||?||'%')} \
sl@0
   452
        string ]
sl@0
   453
}; #ifcapable subquery
sl@0
   454
sl@0
   455
#----------------------------------------------------------------------
sl@0
   456
# Test case vtab1-3 test table scans and the echo module's 
sl@0
   457
# xBestIndex/xFilter handling of ORDER BY clauses.
sl@0
   458
sl@0
   459
# This procedure executes the SQL.  Then it checks to see if the OP_Sort
sl@0
   460
# opcode was executed.  If an OP_Sort did occur, then "sort" is appended
sl@0
   461
# to the result.  If no OP_Sort happened, then "nosort" is appended.
sl@0
   462
#
sl@0
   463
# This procedure is used to check to make sure sorting is or is not
sl@0
   464
# occurring as expected.
sl@0
   465
#
sl@0
   466
proc cksort {sql} {
sl@0
   467
  set ::sqlite_sort_count 0
sl@0
   468
  set data [execsql $sql]
sl@0
   469
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
sl@0
   470
  lappend data $x
sl@0
   471
  return $data
sl@0
   472
}
sl@0
   473
sl@0
   474
do_test vtab1-4.1 {
sl@0
   475
  set echo_module ""
sl@0
   476
  cksort {
sl@0
   477
    SELECT b FROM t1 ORDER BY b;
sl@0
   478
  }
sl@0
   479
} {2 5 nosort}
sl@0
   480
do_test vtab1-4.2 {
sl@0
   481
  set echo_module
sl@0
   482
} [list xBestIndex {SELECT rowid, * FROM 'treal' ORDER BY b ASC} \
sl@0
   483
        xFilter    {SELECT rowid, * FROM 'treal' ORDER BY b ASC} ]
sl@0
   484
do_test vtab1-4.3 {
sl@0
   485
  set echo_module ""
sl@0
   486
  cksort {
sl@0
   487
    SELECT b FROM t1 ORDER BY b DESC;
sl@0
   488
  }
sl@0
   489
} {5 2 nosort}
sl@0
   490
do_test vtab1-4.4 {
sl@0
   491
  set echo_module
sl@0
   492
} [list xBestIndex {SELECT rowid, * FROM 'treal' ORDER BY b DESC} \
sl@0
   493
        xFilter    {SELECT rowid, * FROM 'treal' ORDER BY b DESC} ]
sl@0
   494
do_test vtab1-4.3 {
sl@0
   495
  set echo_module ""
sl@0
   496
  cksort {
sl@0
   497
    SELECT b FROM t1 ORDER BY b||'';
sl@0
   498
  }
sl@0
   499
} {2 5 sort}
sl@0
   500
do_test vtab1-4.4 {
sl@0
   501
  set echo_module
sl@0
   502
} [list xBestIndex {SELECT rowid, * FROM 'treal'} \
sl@0
   503
        xFilter    {SELECT rowid, * FROM 'treal'} ]
sl@0
   504
sl@0
   505
execsql {
sl@0
   506
  DROP TABLE t1;
sl@0
   507
  DROP TABLE treal;
sl@0
   508
}
sl@0
   509
sl@0
   510
#----------------------------------------------------------------------
sl@0
   511
# Test cases vtab1-5 test SELECT queries that include joins on virtual 
sl@0
   512
# tables.
sl@0
   513
sl@0
   514
proc filter {log} {
sl@0
   515
  set out [list]
sl@0
   516
  for {set ii 0} {$ii < [llength $log]} {incr ii} {
sl@0
   517
    if {[lindex $log $ii] eq "xFilter"} {
sl@0
   518
      lappend out xFilter
sl@0
   519
      lappend out [lindex $log [expr $ii+1]]
sl@0
   520
    }
sl@0
   521
  }
sl@0
   522
  return $out
sl@0
   523
}
sl@0
   524
sl@0
   525
do_test vtab1-5-1 {
sl@0
   526
  execsql { 
sl@0
   527
    CREATE TABLE t1(a, b, c);
sl@0
   528
    CREATE TABLE t2(d, e, f);
sl@0
   529
    INSERT INTO t1 VALUES(1, 'red', 'green');
sl@0
   530
    INSERT INTO t1 VALUES(2, 'blue', 'black');
sl@0
   531
    INSERT INTO t2 VALUES(1, 'spades', 'clubs');
sl@0
   532
    INSERT INTO t2 VALUES(2, 'hearts', 'diamonds');
sl@0
   533
    CREATE VIRTUAL TABLE et1 USING echo(t1);
sl@0
   534
    CREATE VIRTUAL TABLE et2 USING echo(t2);
sl@0
   535
  }
sl@0
   536
} {}
sl@0
   537
sl@0
   538
do_test vtab1-5-2 {
sl@0
   539
  set echo_module ""
sl@0
   540
  execsql {
sl@0
   541
    SELECT * FROM et1, et2;
sl@0
   542
  }
sl@0
   543
} [list \
sl@0
   544
  1 red green 1 spades clubs     \
sl@0
   545
  1 red green 2 hearts diamonds  \
sl@0
   546
  2 blue black 1 spades clubs    \
sl@0
   547
  2 blue black 2 hearts diamonds \
sl@0
   548
]
sl@0
   549
do_test vtab1-5-3 {
sl@0
   550
  filter $echo_module
sl@0
   551
} [list \
sl@0
   552
  xFilter {SELECT rowid, * FROM 't1'} \
sl@0
   553
  xFilter {SELECT rowid, * FROM 't2'} \
sl@0
   554
  xFilter {SELECT rowid, * FROM 't2'} \
sl@0
   555
]
sl@0
   556
do_test vtab1-5-4 {
sl@0
   557
  set echo_module ""
sl@0
   558
  execsql {
sl@0
   559
    SELECT * FROM et1, et2 WHERE et2.d = 2;
sl@0
   560
  }
sl@0
   561
} [list \
sl@0
   562
  1 red green 2 hearts diamonds  \
sl@0
   563
  2 blue black 2 hearts diamonds \
sl@0
   564
]
sl@0
   565
do_test vtab1-5-5 {
sl@0
   566
  filter $echo_module
sl@0
   567
} [list \
sl@0
   568
  xFilter {SELECT rowid, * FROM 't1'} \
sl@0
   569
  xFilter {SELECT rowid, * FROM 't2'} \
sl@0
   570
  xFilter {SELECT rowid, * FROM 't2'} \
sl@0
   571
]
sl@0
   572
do_test vtab1-5-6 {
sl@0
   573
  execsql {
sl@0
   574
    CREATE INDEX i1 ON t2(d);
sl@0
   575
  }
sl@0
   576
sl@0
   577
  db close
sl@0
   578
  sqlite3 db test.db
sl@0
   579
  register_echo_module [sqlite3_connection_pointer db]
sl@0
   580
sl@0
   581
  set ::echo_module ""
sl@0
   582
  execsql {
sl@0
   583
    SELECT * FROM et1, et2 WHERE et2.d = 2;
sl@0
   584
  }
sl@0
   585
} [list \
sl@0
   586
  1 red green 2 hearts diamonds  \
sl@0
   587
  2 blue black 2 hearts diamonds \
sl@0
   588
]
sl@0
   589
do_test vtab1-5-7 {
sl@0
   590
  filter $::echo_module
sl@0
   591
} [list \
sl@0
   592
  xFilter {SELECT rowid, * FROM 't2' WHERE d = ?} \
sl@0
   593
  xFilter {SELECT rowid, * FROM 't1'}             \
sl@0
   594
]
sl@0
   595
sl@0
   596
execsql {
sl@0
   597
  DROP TABLE t1;
sl@0
   598
  DROP TABLE t2;
sl@0
   599
  DROP TABLE et1;
sl@0
   600
  DROP TABLE et2;
sl@0
   601
}
sl@0
   602
sl@0
   603
#----------------------------------------------------------------------
sl@0
   604
# Test cases vtab1-6 test INSERT, UPDATE and DELETE operations 
sl@0
   605
# on virtual tables.
sl@0
   606
do_test vtab1-6-1 {
sl@0
   607
  execsql { SELECT sql FROM sqlite_master }
sl@0
   608
} {}
sl@0
   609
do_test vtab1-6-2 {
sl@0
   610
  execsql {
sl@0
   611
    CREATE TABLE treal(a PRIMARY KEY, b, c);
sl@0
   612
    CREATE VIRTUAL TABLE techo USING echo(treal);
sl@0
   613
    SELECT name FROM sqlite_master WHERE type = 'table';
sl@0
   614
  }
sl@0
   615
} {treal techo}
sl@0
   616
do_test vtab1-6-3.1.1 {
sl@0
   617
  execsql {
sl@0
   618
    PRAGMA count_changes=ON;
sl@0
   619
    INSERT INTO techo VALUES(1, 2, 3);
sl@0
   620
  }
sl@0
   621
} {1}
sl@0
   622
do_test vtab1-6-3.1.2 {
sl@0
   623
  db changes
sl@0
   624
} {1}
sl@0
   625
do_test vtab1-6-3.2 {
sl@0
   626
  execsql {
sl@0
   627
    SELECT * FROM techo;
sl@0
   628
  }
sl@0
   629
} {1 2 3}
sl@0
   630
do_test vtab1-6-4.1 {
sl@0
   631
  execsql {
sl@0
   632
    UPDATE techo SET a = 5;
sl@0
   633
  }
sl@0
   634
  db changes
sl@0
   635
} {1}
sl@0
   636
do_test vtab1-6-4.2 {
sl@0
   637
  execsql {
sl@0
   638
    SELECT * FROM techo;
sl@0
   639
  }
sl@0
   640
} {5 2 3}
sl@0
   641
do_test vtab1-6-4.3 {
sl@0
   642
  execsql {
sl@0
   643
    UPDATE techo SET a=6 WHERE a<0;
sl@0
   644
  }
sl@0
   645
  db changes
sl@0
   646
} {0}
sl@0
   647
do_test vtab1-6-4.4 {
sl@0
   648
  execsql {
sl@0
   649
    SELECT * FROM techo;
sl@0
   650
  }
sl@0
   651
} {5 2 3}
sl@0
   652
sl@0
   653
do_test vtab1-6-5.1 {
sl@0
   654
 execsql {
sl@0
   655
   UPDATE techo set a = a||b||c;
sl@0
   656
 }
sl@0
   657
 db changes
sl@0
   658
} {1}
sl@0
   659
do_test vtab1-6-5.2 {
sl@0
   660
 execsql {
sl@0
   661
   SELECT * FROM techo;
sl@0
   662
 }
sl@0
   663
} {523 2 3}
sl@0
   664
sl@0
   665
do_test vtab1-6-6.1 {
sl@0
   666
  execsql {
sl@0
   667
    UPDATE techo set rowid = 10;
sl@0
   668
  }
sl@0
   669
  db changes
sl@0
   670
} {1}
sl@0
   671
do_test vtab1-6-6.2 {
sl@0
   672
  execsql {
sl@0
   673
    SELECT rowid FROM techo;
sl@0
   674
  }
sl@0
   675
} {10}
sl@0
   676
sl@0
   677
do_test vtab1-6-7.1.1 {
sl@0
   678
  execsql {
sl@0
   679
    INSERT INTO techo VALUES(11,12,13);
sl@0
   680
  }
sl@0
   681
} {1}
sl@0
   682
do_test vtab1-6-7.1.2 {
sl@0
   683
  db changes
sl@0
   684
} {1}
sl@0
   685
do_test vtab1-6-7.2 {
sl@0
   686
  execsql {
sl@0
   687
    SELECT * FROM techo ORDER BY a;
sl@0
   688
  }
sl@0
   689
} {11 12 13 523 2 3}
sl@0
   690
do_test vtab1-6-7.3 {
sl@0
   691
  execsql {
sl@0
   692
    UPDATE techo SET b=b+1000
sl@0
   693
  }
sl@0
   694
  db changes
sl@0
   695
} {2}
sl@0
   696
do_test vtab1-6-7.4 {
sl@0
   697
  execsql {
sl@0
   698
    SELECT * FROM techo ORDER BY a;
sl@0
   699
  }
sl@0
   700
} {11 1012 13 523 1002 3}
sl@0
   701
sl@0
   702
sl@0
   703
do_test vtab1-6-8.1 {
sl@0
   704
  execsql {
sl@0
   705
    DELETE FROM techo WHERE a=5;
sl@0
   706
  }
sl@0
   707
  db changes
sl@0
   708
} {0}
sl@0
   709
do_test vtab1-6-8.2 {
sl@0
   710
  execsql {
sl@0
   711
    SELECT * FROM techo ORDER BY a;
sl@0
   712
  }
sl@0
   713
} {11 1012 13 523 1002 3}
sl@0
   714
do_test vtab1-6-8.3 {
sl@0
   715
  execsql {
sl@0
   716
    DELETE FROM techo;
sl@0
   717
  }
sl@0
   718
  db changes
sl@0
   719
} {2}
sl@0
   720
do_test vtab1-6-8.4 {
sl@0
   721
  execsql {
sl@0
   722
    SELECT * FROM techo ORDER BY a;
sl@0
   723
  }
sl@0
   724
} {}
sl@0
   725
execsql {PRAGMA count_changes=OFF}
sl@0
   726
sl@0
   727
file delete -force test2.db
sl@0
   728
file delete -force test2.db-journal
sl@0
   729
sqlite3 db2 test2.db
sl@0
   730
execsql {
sl@0
   731
  CREATE TABLE techo(a PRIMARY KEY, b, c);
sl@0
   732
} db2
sl@0
   733
proc check_echo_table {tn} {
sl@0
   734
  set ::data1 [execsql {SELECT rowid, * FROM techo}]
sl@0
   735
  set ::data2 [execsql {SELECT rowid, * FROM techo} db2]
sl@0
   736
  do_test $tn {
sl@0
   737
    string equal $::data1 $::data2
sl@0
   738
  } 1
sl@0
   739
}
sl@0
   740
set tn 0
sl@0
   741
foreach stmt [list \
sl@0
   742
  {INSERT INTO techo VALUES('abc', 'def', 'ghi')}                        \
sl@0
   743
  {INSERT INTO techo SELECT a||'.'||rowid, b, c FROM techo}              \
sl@0
   744
  {INSERT INTO techo SELECT a||'x'||rowid, b, c FROM techo}              \
sl@0
   745
  {INSERT INTO techo SELECT a||'y'||rowid, b, c FROM techo}              \
sl@0
   746
  {DELETE FROM techo WHERE (oid % 3) = 0}                                \
sl@0
   747
  {UPDATE techo set rowid = 100 WHERE rowid = 1}                         \
sl@0
   748
  {INSERT INTO techo(a, b) VALUES('hello', 'world')}                     \
sl@0
   749
  {DELETE FROM techo}                                                    \
sl@0
   750
] {
sl@0
   751
  execsql $stmt
sl@0
   752
  execsql $stmt db2
sl@0
   753
  check_echo_table vtab1-6.8.[incr tn]
sl@0
   754
}
sl@0
   755
sl@0
   756
db2 close
sl@0
   757
sl@0
   758
sl@0
   759
sl@0
   760
#----------------------------------------------------------------------
sl@0
   761
# Test cases vtab1-7 tests that the value returned by 
sl@0
   762
# sqlite3_last_insert_rowid() is set correctly when rows are inserted
sl@0
   763
# into virtual tables.
sl@0
   764
do_test vtab1.7-1 {
sl@0
   765
  execsql {
sl@0
   766
    CREATE TABLE real_abc(a PRIMARY KEY, b, c);
sl@0
   767
    CREATE VIRTUAL TABLE echo_abc USING echo(real_abc);
sl@0
   768
  }
sl@0
   769
} {}
sl@0
   770
do_test vtab1.7-2 {
sl@0
   771
  execsql {
sl@0
   772
    INSERT INTO echo_abc VALUES(1, 2, 3);
sl@0
   773
    SELECT last_insert_rowid();
sl@0
   774
  }
sl@0
   775
} {1}
sl@0
   776
do_test vtab1.7-3 {
sl@0
   777
  execsql {
sl@0
   778
    INSERT INTO echo_abc(rowid) VALUES(31427);
sl@0
   779
    SELECT last_insert_rowid();
sl@0
   780
  }
sl@0
   781
} {31427}
sl@0
   782
do_test vtab1.7-4 {
sl@0
   783
  execsql {
sl@0
   784
    INSERT INTO echo_abc SELECT a||'.v2', b, c FROM echo_abc;
sl@0
   785
    SELECT last_insert_rowid();
sl@0
   786
  }
sl@0
   787
} {31429}
sl@0
   788
do_test vtab1.7-5 {
sl@0
   789
  execsql {
sl@0
   790
    SELECT rowid, a, b, c FROM echo_abc
sl@0
   791
  }
sl@0
   792
} [list 1     1    2  3  \
sl@0
   793
        31427 {}   {} {} \
sl@0
   794
        31428 1.v2 2  3  \
sl@0
   795
        31429 {}  {} {}  \
sl@0
   796
]
sl@0
   797
sl@0
   798
# Now test that DELETE and UPDATE operations do not modify the value.
sl@0
   799
do_test vtab1.7-6 {
sl@0
   800
  execsql {
sl@0
   801
    UPDATE echo_abc SET c = 5 WHERE b = 2;
sl@0
   802
    SELECT last_insert_rowid();
sl@0
   803
  }
sl@0
   804
} {31429}
sl@0
   805
do_test vtab1.7-7 {
sl@0
   806
  execsql {
sl@0
   807
    UPDATE echo_abc SET rowid = 5 WHERE rowid = 1;
sl@0
   808
    SELECT last_insert_rowid();
sl@0
   809
  }
sl@0
   810
} {31429}
sl@0
   811
do_test vtab1.7-8 {
sl@0
   812
  execsql {
sl@0
   813
    DELETE FROM echo_abc WHERE b = 2;
sl@0
   814
    SELECT last_insert_rowid();
sl@0
   815
  }
sl@0
   816
} {31429}
sl@0
   817
do_test vtab1.7-9 {
sl@0
   818
  execsql {
sl@0
   819
    SELECT rowid, a, b, c FROM echo_abc
sl@0
   820
  }
sl@0
   821
} [list 31427 {} {} {} \
sl@0
   822
        31429 {} {} {} \
sl@0
   823
]
sl@0
   824
do_test vtab1.7-10 {
sl@0
   825
  execsql {
sl@0
   826
    DELETE FROM echo_abc WHERE b = 2;
sl@0
   827
    SELECT last_insert_rowid();
sl@0
   828
  }
sl@0
   829
} {31429}
sl@0
   830
do_test vtab1.7-11 {
sl@0
   831
  execsql {
sl@0
   832
    SELECT rowid, a, b, c FROM real_abc
sl@0
   833
  }
sl@0
   834
} [list 31427 {} {} {} \
sl@0
   835
        31429 {} {} {} \
sl@0
   836
]
sl@0
   837
do_test vtab1.7-12 {
sl@0
   838
  execsql {
sl@0
   839
    DELETE FROM echo_abc;
sl@0
   840
    SELECT last_insert_rowid();
sl@0
   841
  }
sl@0
   842
} {31429}
sl@0
   843
do_test vtab1.7-13 {
sl@0
   844
  execsql {
sl@0
   845
    SELECT rowid, a, b, c FROM real_abc
sl@0
   846
  }
sl@0
   847
} {}
sl@0
   848
sl@0
   849
ifcapable attach {
sl@0
   850
  do_test vtab1.8-1 {
sl@0
   851
    set echo_module ""
sl@0
   852
    execsql {
sl@0
   853
      ATTACH 'test2.db' AS aux;
sl@0
   854
      CREATE VIRTUAL TABLE aux.e2 USING echo(real_abc);
sl@0
   855
    }
sl@0
   856
    set echo_module
sl@0
   857
  } [list xCreate echo aux e2 real_abc   \
sl@0
   858
          xSync   echo(real_abc)         \
sl@0
   859
          xCommit echo(real_abc)         \
sl@0
   860
  ]
sl@0
   861
}
sl@0
   862
do_test vtab1.8-2 {
sl@0
   863
  catchsql {
sl@0
   864
    DROP TABLE aux.e2;
sl@0
   865
  }
sl@0
   866
  execsql {
sl@0
   867
    DROP TABLE treal;
sl@0
   868
    DROP TABLE techo;
sl@0
   869
    DROP TABLE echo_abc;
sl@0
   870
    DROP TABLE real_abc;
sl@0
   871
  }
sl@0
   872
} {}
sl@0
   873
sl@0
   874
do_test vtab1.9-1 {
sl@0
   875
  set echo_module ""
sl@0
   876
  execsql {
sl@0
   877
    CREATE TABLE r(a, b, c);
sl@0
   878
    CREATE VIRTUAL TABLE e USING echo(r, e_log);
sl@0
   879
    SELECT name FROM sqlite_master;
sl@0
   880
  }
sl@0
   881
} {r e e_log}
sl@0
   882
do_test vtab1.9-2 {
sl@0
   883
  execsql {
sl@0
   884
    DROP TABLE e;
sl@0
   885
    SELECT name FROM sqlite_master;
sl@0
   886
  }
sl@0
   887
} {r}
sl@0
   888
sl@0
   889
do_test vtab1.9-3 {
sl@0
   890
  set echo_module ""
sl@0
   891
  execsql {
sl@0
   892
    CREATE VIRTUAL TABLE e USING echo(r, e_log, virtual 1 2 3 varchar(32));
sl@0
   893
  }
sl@0
   894
  set echo_module
sl@0
   895
} [list                                                        \
sl@0
   896
  xCreate echo main e r e_log {virtual 1 2 3 varchar(32)}      \
sl@0
   897
  xSync echo(r)                                                \
sl@0
   898
  xCommit echo(r)                                              \
sl@0
   899
]
sl@0
   900
sl@0
   901
do_test vtab1.10-1 {
sl@0
   902
  execsql {
sl@0
   903
    CREATE TABLE del(d);
sl@0
   904
    CREATE VIRTUAL TABLE e2 USING echo(del);
sl@0
   905
  }
sl@0
   906
  db close
sl@0
   907
  sqlite3 db test.db
sl@0
   908
  register_echo_module [sqlite3_connection_pointer db]
sl@0
   909
  execsql {
sl@0
   910
    DROP TABLE del;
sl@0
   911
  }
sl@0
   912
  catchsql {
sl@0
   913
    SELECT * FROM e2;
sl@0
   914
  }
sl@0
   915
} {1 {vtable constructor failed: e2}}
sl@0
   916
do_test vtab1.10-2 {
sl@0
   917
  set rc [catch {
sl@0
   918
    set ptr [sqlite3_connection_pointer db]
sl@0
   919
    sqlite3_declare_vtab $ptr {CREATE TABLE abc(a, b, c)}
sl@0
   920
  } msg]
sl@0
   921
  list $rc $msg
sl@0
   922
} {1 {library routine called out of sequence}}
sl@0
   923
do_test vtab1.10-3 {
sl@0
   924
  set ::echo_module_begin_fail r
sl@0
   925
  catchsql {
sl@0
   926
    INSERT INTO e VALUES(1, 2, 3);
sl@0
   927
  }
sl@0
   928
} {1 {SQL logic error or missing database}}
sl@0
   929
do_test vtab1.10-4 {
sl@0
   930
  catch {execsql {
sl@0
   931
    EXPLAIN SELECT * FROM e WHERE rowid = 2;
sl@0
   932
    EXPLAIN QUERY PLAN SELECT * FROM e WHERE rowid = 2 ORDER BY rowid;
sl@0
   933
  }}
sl@0
   934
} {0}
sl@0
   935
sl@0
   936
do_test vtab1.10-5 {
sl@0
   937
  set echo_module ""
sl@0
   938
  execsql {
sl@0
   939
    SELECT * FROM e WHERE rowid||'' MATCH 'pattern';
sl@0
   940
  }
sl@0
   941
  set echo_module
sl@0
   942
} [list \
sl@0
   943
  xBestIndex {SELECT rowid, * FROM 'r'} \
sl@0
   944
  xFilter {SELECT rowid, * FROM 'r'}    \
sl@0
   945
]
sl@0
   946
proc match_func {args} {return ""}
sl@0
   947
do_test vtab1.10-6 {
sl@0
   948
  set echo_module ""
sl@0
   949
  db function match match_func
sl@0
   950
  execsql {
sl@0
   951
    SELECT * FROM e WHERE match('pattern', rowid, 'pattern2');
sl@0
   952
  }
sl@0
   953
  set echo_module
sl@0
   954
} [list \
sl@0
   955
  xBestIndex {SELECT rowid, * FROM 'r'} \
sl@0
   956
  xFilter {SELECT rowid, * FROM 'r'}    \
sl@0
   957
]
sl@0
   958
sl@0
   959
sl@0
   960
# Testing the xFindFunction interface
sl@0
   961
#
sl@0
   962
catch {rename ::echo_glob_overload {}}
sl@0
   963
do_test vtab1.11-1 {
sl@0
   964
  execsql {
sl@0
   965
    INSERT INTO r(a,b,c) VALUES(1,'?',99);
sl@0
   966
    INSERT INTO r(a,b,c) VALUES(2,3,99);
sl@0
   967
    SELECT a GLOB b FROM e
sl@0
   968
  }
sl@0
   969
} {1 0}
sl@0
   970
proc ::echo_glob_overload {a b} {
sl@0
   971
 return [list $b $a]
sl@0
   972
}
sl@0
   973
do_test vtab1.11-2 {
sl@0
   974
  execsql {
sl@0
   975
    SELECT a like 'b' FROM e
sl@0
   976
  }
sl@0
   977
} {0 0}
sl@0
   978
do_test vtab1.11-3 {
sl@0
   979
  execsql {
sl@0
   980
    SELECT a glob '2' FROM e
sl@0
   981
  }
sl@0
   982
} {{1 2} {2 2}}
sl@0
   983
do_test vtab1.11-4 {
sl@0
   984
  execsql {
sl@0
   985
    SELECT  glob('2',a) FROM e
sl@0
   986
  }
sl@0
   987
} {0 1}
sl@0
   988
do_test vtab1.11-5 {
sl@0
   989
  execsql {
sl@0
   990
    SELECT  glob(a,'2') FROM e
sl@0
   991
  }
sl@0
   992
} {{2 1} {2 2}}
sl@0
   993
 
sl@0
   994
#----------------------------------------------------------------------
sl@0
   995
# Test the outcome if a constraint is encountered half-way through
sl@0
   996
# a multi-row INSERT that is inside a transaction
sl@0
   997
#
sl@0
   998
do_test vtab1.12-1 {
sl@0
   999
  execsql {
sl@0
  1000
    CREATE TABLE b(a, b, c);
sl@0
  1001
    CREATE TABLE c(a UNIQUE, b, c);
sl@0
  1002
    INSERT INTO b VALUES(1, 'A', 'B');
sl@0
  1003
    INSERT INTO b VALUES(2, 'C', 'D');
sl@0
  1004
    INSERT INTO b VALUES(3, 'E', 'F');
sl@0
  1005
    INSERT INTO c VALUES(3, 'G', 'H');
sl@0
  1006
    CREATE VIRTUAL TABLE echo_c USING echo(c);
sl@0
  1007
  }
sl@0
  1008
} {}
sl@0
  1009
sl@0
  1010
# First test outside of a transaction.
sl@0
  1011
do_test vtab1.12-2 {
sl@0
  1012
  catchsql { INSERT INTO echo_c SELECT * FROM b; }
sl@0
  1013
} {1 {echo-vtab-error: column a is not unique}}
sl@0
  1014
do_test vtab1.12-2.1 {
sl@0
  1015
  sqlite3_errmsg db
sl@0
  1016
} {echo-vtab-error: column a is not unique}
sl@0
  1017
do_test vtab1.12-3 {
sl@0
  1018
  execsql { SELECT * FROM c }
sl@0
  1019
} {3 G H}
sl@0
  1020
sl@0
  1021
# Now the real test - wrapped in a transaction.
sl@0
  1022
do_test vtab1.12-4 {
sl@0
  1023
  execsql  {BEGIN}
sl@0
  1024
  catchsql { INSERT INTO echo_c SELECT * FROM b; }
sl@0
  1025
} {1 {echo-vtab-error: column a is not unique}}
sl@0
  1026
do_test vtab1.12-5 {
sl@0
  1027
  execsql { SELECT * FROM c }
sl@0
  1028
} {3 G H}
sl@0
  1029
do_test vtab1.12-6 {
sl@0
  1030
  execsql { COMMIT }
sl@0
  1031
  execsql { SELECT * FROM c }
sl@0
  1032
} {3 G H}
sl@0
  1033
sl@0
  1034
# At one point (ticket #2759), a WHERE clause of the form "<column> IS NULL"
sl@0
  1035
# on a virtual table was causing an assert() to fail in the compiler.
sl@0
  1036
#
sl@0
  1037
# "IS NULL" clauses should not be passed through to the virtual table
sl@0
  1038
# implementation. They are handled by SQLite after the vtab returns its
sl@0
  1039
# data.
sl@0
  1040
#
sl@0
  1041
do_test vtab1.13-1 {
sl@0
  1042
  execsql { 
sl@0
  1043
    SELECT * FROM echo_c WHERE a IS NULL 
sl@0
  1044
  }
sl@0
  1045
} {}
sl@0
  1046
do_test vtab1.13-2 {
sl@0
  1047
  execsql { 
sl@0
  1048
    INSERT INTO c VALUES(NULL, 15, 16);
sl@0
  1049
    SELECT * FROM echo_c WHERE a IS NULL 
sl@0
  1050
  }
sl@0
  1051
} {{} 15 16}
sl@0
  1052
do_test vtab1.13-3 {
sl@0
  1053
  execsql { 
sl@0
  1054
    INSERT INTO c VALUES(15, NULL, 16);
sl@0
  1055
    SELECT * FROM echo_c WHERE b IS NULL 
sl@0
  1056
  }
sl@0
  1057
} {15 {} 16}
sl@0
  1058
do_test vtab1.13-3 {
sl@0
  1059
  execsql { 
sl@0
  1060
    SELECT * FROM echo_c WHERE b IS NULL AND a = 15;
sl@0
  1061
  }
sl@0
  1062
} {15 {} 16}
sl@0
  1063
sl@0
  1064
sl@0
  1065
do_test vtab1-14.1 {
sl@0
  1066
  execsql { DELETE FROM c }
sl@0
  1067
  set echo_module ""
sl@0
  1068
  execsql { SELECT * FROM echo_c WHERE rowid IN (1, 2, 3) }
sl@0
  1069
  set echo_module
sl@0
  1070
} [list xBestIndex {SELECT rowid, * FROM 'c'} xFilter {SELECT rowid, * FROM 'c'}]
sl@0
  1071
sl@0
  1072
do_test vtab1-14.2 {
sl@0
  1073
  set echo_module ""
sl@0
  1074
  execsql { SELECT * FROM echo_c WHERE rowid = 1 }
sl@0
  1075
  set echo_module
sl@0
  1076
} [list xBestIndex {SELECT rowid, * FROM 'c' WHERE rowid = ?} xFilter {SELECT rowid, * FROM 'c' WHERE rowid = ?} 1]
sl@0
  1077
sl@0
  1078
do_test vtab1-14.3 {
sl@0
  1079
  set echo_module ""
sl@0
  1080
  execsql { SELECT * FROM echo_c WHERE a = 1 }
sl@0
  1081
  set echo_module
sl@0
  1082
} [list xBestIndex {SELECT rowid, * FROM 'c' WHERE a = ?} xFilter {SELECT rowid, * FROM 'c' WHERE a = ?} 1]
sl@0
  1083
sl@0
  1084
do_test vtab1-14.4 {
sl@0
  1085
  set echo_module ""
sl@0
  1086
  execsql { SELECT * FROM echo_c WHERE a IN (1, 2) }
sl@0
  1087
  set echo_module
sl@0
  1088
} [list xBestIndex {SELECT rowid, * FROM 'c'} xFilter {SELECT rowid, * FROM 'c'}]
sl@0
  1089
sl@0
  1090
do_test vtab1-15.1 {
sl@0
  1091
  execsql {
sl@0
  1092
    CREATE TABLE t1(a, b, c);
sl@0
  1093
    CREATE VIRTUAL TABLE echo_t1 USING echo(t1);
sl@0
  1094
  }
sl@0
  1095
} {}
sl@0
  1096
do_test vtab1-15.2 {
sl@0
  1097
  execsql {
sl@0
  1098
    INSERT INTO echo_t1(rowid) VALUES(45);
sl@0
  1099
    SELECT rowid, * FROM echo_t1;
sl@0
  1100
  }
sl@0
  1101
} {45 {} {} {}}
sl@0
  1102
do_test vtab1-15.3 {
sl@0
  1103
  execsql {
sl@0
  1104
    INSERT INTO echo_t1(rowid) VALUES(NULL);
sl@0
  1105
    SELECT rowid, * FROM echo_t1;
sl@0
  1106
  }
sl@0
  1107
} {45 {} {} {} 46 {} {} {}}
sl@0
  1108
do_test vtab1-15.4 {
sl@0
  1109
  catchsql {
sl@0
  1110
    INSERT INTO echo_t1(rowid) VALUES('new rowid');
sl@0
  1111
  }
sl@0
  1112
} {1 {datatype mismatch}}
sl@0
  1113
sl@0
  1114
# The following tests - vtab1-16.* - are designed to test that setting 
sl@0
  1115
# sqlite3_vtab.zErrMsg variable can be used by the vtab interface to 
sl@0
  1116
# return an error message to the user.
sl@0
  1117
# 
sl@0
  1118
do_test vtab1-16.1 {
sl@0
  1119
  execsql {
sl@0
  1120
    CREATE TABLE t2(a PRIMARY KEY, b, c);
sl@0
  1121
    INSERT INTO t2 VALUES(1, 2, 3);
sl@0
  1122
    INSERT INTO t2 VALUES(4, 5, 6);
sl@0
  1123
    CREATE VIRTUAL TABLE echo_t2 USING echo(t2);
sl@0
  1124
  }
sl@0
  1125
} {}
sl@0
  1126
sl@0
  1127
set tn 2
sl@0
  1128
foreach method [list \
sl@0
  1129
    xBestIndex       \
sl@0
  1130
    xOpen            \
sl@0
  1131
    xFilter          \
sl@0
  1132
    xNext            \
sl@0
  1133
    xColumn          \
sl@0
  1134
    xRowid           \
sl@0
  1135
] {
sl@0
  1136
  do_test vtab1-16.$tn {
sl@0
  1137
    set echo_module_fail($method,t2) "the $method method has failed"
sl@0
  1138
    catchsql { SELECT rowid, * FROM echo_t2 WHERE a >= 1 }
sl@0
  1139
  } "1 {echo-vtab-error: the $method method has failed}"
sl@0
  1140
  unset echo_module_fail($method,t2)
sl@0
  1141
  incr tn
sl@0
  1142
}
sl@0
  1143
sl@0
  1144
foreach method [list \
sl@0
  1145
  xUpdate            \
sl@0
  1146
  xBegin             \
sl@0
  1147
  xSync              \
sl@0
  1148
] {
sl@0
  1149
  do_test vtab1-16.$tn {
sl@0
  1150
    set echo_module_fail($method,t2) "the $method method has failed"
sl@0
  1151
    catchsql { INSERT INTO echo_t2 VALUES(7, 8, 9) }
sl@0
  1152
  } "1 {echo-vtab-error: the $method method has failed}"
sl@0
  1153
  unset echo_module_fail($method,t2)
sl@0
  1154
  incr tn
sl@0
  1155
}
sl@0
  1156
sl@0
  1157
do_test vtab1-16.$tn {
sl@0
  1158
  set echo_module_fail(xRename,t2) "the xRename method has failed"
sl@0
  1159
  catchsql { ALTER TABLE echo_t2 RENAME TO another_name }
sl@0
  1160
} "1 {echo-vtab-error: the xRename method has failed}"
sl@0
  1161
unset echo_module_fail(xRename,t2)
sl@0
  1162
incr tn
sl@0
  1163
sl@0
  1164
unset -nocomplain echo_module_begin_fail
sl@0
  1165
finish_test