/ Check-in [5e592c42]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add tests for error conditions surrounding the creation/connection of virtual tables. (CVS 3235)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:5e592c422b86deb5802c6536e91661717ee9bbe1
User & Date: danielk1977 2006-06-14 06:31:28
Context
2006-06-14
06:58
Change the pModule parameter of the xCreate and xConnect methods to a void*. (CVS 3236) check-in: 3ffa51b5 user: danielk1977 tags: trunk
06:31
Add tests for error conditions surrounding the creation/connection of virtual tables. (CVS 3235) check-in: 5e592c42 user: danielk1977 tags: trunk
2006-06-13
23:51
The echo module test is now running. Added the tclvar module test but have not yet done anything with it. (CVS 3234) check-in: 29199eee user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/test8.c.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
31
32
33
34
35
36
37
38
39
40


41
42
43
44
45
46
47
48
49
50
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211













212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233





234
235
236
237
238
239
240
241
...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing the virtual table interfaces.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library.
**
** $Id: test8.c,v 1.11 2006/06/13 23:51:35 drh Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include "os.h"
#include <stdlib.h>
#include <string.h>

................................................................................
** the same number of entries as there are columns in the underlying
** real table.
*/
struct echo_vtab {
  sqlite3_vtab base;
  Tcl_Interp *interp;
  sqlite3 *db;
  char *zTableName;       /* Name of the real table */
  char *zStmt;                 /* "SELECT rowid, * FROM <real-table-name> " */



  int *aIndex;
  int nCol;
  char **aCol;
};

/* An echo cursor object */
struct echo_cursor {
  sqlite3_vtab_cursor base;
  sqlite3_stmt *pStmt;
  int errcode;                 /* Error code */
................................................................................
#ifndef SQLITE_OMIT_VIRTUALTABLE
      sqlite3_declare_vtab(db, zCreateTable);
#endif
    } else {
      rc = SQLITE_ERROR;
    }
    sqlite3_finalize(pStmt);
    pVtab->zStmt = sqlite3MPrintf("SELECT rowid, * FROM %s ", argv[1]);
    if( rc==SQLITE_OK ){
      rc = getIndexArray(db, argv[1], &pVtab->aIndex);
    }
    if( rc==SQLITE_OK ){
      rc = getColumnNames(db, argv[1], &pVtab->aCol, &pVtab->nCol);
    }
  }

  return rc;
}














static int echoConstructor(
  sqlite3 *db,
  const sqlite3_module *pModule,
  int argc, char **argv,
  sqlite3_vtab **ppVtab
){
  int i;
  echo_vtab *pVtab;

  pVtab = sqliteMalloc( sizeof(*pVtab) );

  *ppVtab = &pVtab->base;
  pVtab->base.pModule = pModule;
  pVtab->interp = pModule->pAux;
  pVtab->db = db;
  pVtab->zTableName = sqlite3MPrintf("%s", argv[1]);
  for(i=0; i<argc; i++){
    appendToEchoModule(pVtab->interp, argv[i]);
  }

  echoDeclareVtab(pVtab, db, argc, argv);





  return 0;
}

/* Methods for the echo module */
static int echoCreate(
  sqlite3 *db,
  const sqlite3_module *pModule,
  int argc, char **argv,
................................................................................
  int argc, char **argv,
  sqlite3_vtab **ppVtab
){
  appendToEchoModule((Tcl_Interp *)(pModule->pAux), "xConnect");
  return echoConstructor(db, pModule, argc, argv, ppVtab);
}

static int echoDestructor(sqlite3_vtab *pVtab){
  int ii;
  echo_vtab *p = (echo_vtab*)pVtab;
  sqliteFree(p->zStmt);
  sqliteFree(p->aIndex);
  for(ii=0; ii<p->nCol; ii++){
    sqliteFree(p->aCol[ii]);
  }
  sqliteFree(p->aCol);
  sqliteFree(p->zTableName);
  sqliteFree(p);
  return 0;
}

static int echoDisconnect(sqlite3_vtab *pVtab){
  appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
  return echoDestructor(pVtab);
}
static int echoDestroy(sqlite3_vtab *pVtab){
  appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
  return echoDestructor(pVtab);







|







 







<
<

>
>
|
|
<







 







<










>
>
>
>
>
>
>
>
>
>
>
>
>











<
<








|
>
>
>
>
>
|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
31
32
33
34
35
36
37


38
39
40
41
42

43
44
45
46
47
48
49
...
193
194
195
196
197
198
199

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
...
264
265
266
267
268
269
270














271
272
273
274
275
276
277
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing the virtual table interfaces.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library.
**
** $Id: test8.c,v 1.12 2006/06/14 06:31:28 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include "os.h"
#include <stdlib.h>
#include <string.h>

................................................................................
** the same number of entries as there are columns in the underlying
** real table.
*/
struct echo_vtab {
  sqlite3_vtab base;
  Tcl_Interp *interp;
  sqlite3 *db;



  char *zTableName;       /* Name of the real table */
  int nCol;               /* Number of columns in the real table */
  int *aIndex;            /* Array of size nCol. True if column has an index */
  char **aCol;            /* Array of size nCol. Column names */

};

/* An echo cursor object */
struct echo_cursor {
  sqlite3_vtab_cursor base;
  sqlite3_stmt *pStmt;
  int errcode;                 /* Error code */
................................................................................
#ifndef SQLITE_OMIT_VIRTUALTABLE
      sqlite3_declare_vtab(db, zCreateTable);
#endif
    } else {
      rc = SQLITE_ERROR;
    }
    sqlite3_finalize(pStmt);

    if( rc==SQLITE_OK ){
      rc = getIndexArray(db, argv[1], &pVtab->aIndex);
    }
    if( rc==SQLITE_OK ){
      rc = getColumnNames(db, argv[1], &pVtab->aCol, &pVtab->nCol);
    }
  }

  return rc;
}

static int echoDestructor(sqlite3_vtab *pVtab){
  int ii;
  echo_vtab *p = (echo_vtab*)pVtab;
  sqliteFree(p->aIndex);
  for(ii=0; ii<p->nCol; ii++){
    sqliteFree(p->aCol[ii]);
  }
  sqliteFree(p->aCol);
  sqliteFree(p->zTableName);
  sqliteFree(p);
  return 0;
}

static int echoConstructor(
  sqlite3 *db,
  const sqlite3_module *pModule,
  int argc, char **argv,
  sqlite3_vtab **ppVtab
){
  int i;
  echo_vtab *pVtab;

  pVtab = sqliteMalloc( sizeof(*pVtab) );


  pVtab->base.pModule = pModule;
  pVtab->interp = pModule->pAux;
  pVtab->db = db;
  pVtab->zTableName = sqlite3MPrintf("%s", argv[1]);
  for(i=0; i<argc; i++){
    appendToEchoModule(pVtab->interp, argv[i]);
  }

  if( echoDeclareVtab(pVtab, db, argc, argv) ){
    echoDestructor((sqlite3_vtab *)pVtab);
    return SQLITE_ERROR;
  }

  *ppVtab = &pVtab->base;
  return SQLITE_OK;
}

/* Methods for the echo module */
static int echoCreate(
  sqlite3 *db,
  const sqlite3_module *pModule,
  int argc, char **argv,
................................................................................
  int argc, char **argv,
  sqlite3_vtab **ppVtab
){
  appendToEchoModule((Tcl_Interp *)(pModule->pAux), "xConnect");
  return echoConstructor(db, pModule, argc, argv, ppVtab);
}















static int echoDisconnect(sqlite3_vtab *pVtab){
  appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
  return echoDestructor(pVtab);
}
static int echoDestroy(sqlite3_vtab *pVtab){
  appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
  return echoDestructor(pVtab);

Changes to src/vtab.c.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
...
345
346
347
348
349
350
351
352
353
354

355
356

357

358
359
360
361
362
363
364
365
366
367






368
369
370
371
372
373
374
**    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.
**
*************************************************************************
** This file contains code used to help implement virtual tables.
**
** $Id: vtab.c,v 1.8 2006/06/13 23:51:35 drh Exp $
*/
#ifndef SQLITE_OMIT_VIRTUALTABLE
#include "sqliteInt.h"

/*
** External API function used to create a new virtual-table module.
*/
................................................................................
  */
  if( !db->init.busy ){
    char *zStmt;
    char *zWhere;
    int iDb;
    Vdbe *v;
    if( pTab->pModule==0 ){
      sqlite3ErrorMsg(pParse, "unknown module: %s", zModule);
    }

    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
    if( pEnd ){
      pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
    }
    zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
................................................................................
    return SQLITE_OK;
  }

  pModule = pTab->pModule;
  zModule = pTab->azModuleArg[0];
  if( !pModule || !pModule->xConnect ){
    const char *zModule = pTab->azModuleArg[0];
    sqlite3ErrorMsg(pParse, "unknown module: %s", zModule);
    rc = SQLITE_ERROR;
  } else {
    char **azArg = pTab->azModuleArg;
    int nArg = pTab->nModuleArg;
    sqlite3 *db = pParse->db;
    assert( !db->pVTab );
    db->pVTab = pTab;
................................................................................
  zModule = pTab->azModuleArg[0];

  /* If the module has been registered and includes a Create method, 
  ** invoke it now. If the module has not been registered, return an 
  ** error. Otherwise, do nothing.
  */
  if( !pModule ){
    *pzErr = sqlite3MPrintf("unknown module: %s", zModule);
    rc = SQLITE_ERROR;
  }else if( pModule->xCreate ){

    char **azArg = pTab->azModuleArg;
    int nArg = pTab->nModuleArg;

    assert( !db->pVTab );

    db->pVTab = pTab;
    rc = sqlite3SafetyOff(db);
    assert( rc==SQLITE_OK );
    rc = pModule->xCreate(db, pModule, nArg, azArg, &pTab->pVtab);
    db->pVTab = 0;
    if( rc ){
      *pzErr = sqlite3MPrintf("module create failed: %s", zModule);
      sqlite3SafetyOn(db);
    } else {
      rc = sqlite3SafetyOn(db);






    }
  }

  return rc;
}

/*







|







 







|







 







|







 







|

|
>


>

>




|
|
|
|
|
|
>
>
>
>
>
>







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
...
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
**    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.
**
*************************************************************************
** This file contains code used to help implement virtual tables.
**
** $Id: vtab.c,v 1.9 2006/06/14 06:31:28 danielk1977 Exp $
*/
#ifndef SQLITE_OMIT_VIRTUALTABLE
#include "sqliteInt.h"

/*
** External API function used to create a new virtual-table module.
*/
................................................................................
  */
  if( !db->init.busy ){
    char *zStmt;
    char *zWhere;
    int iDb;
    Vdbe *v;
    if( pTab->pModule==0 ){
      sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
    }

    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
    if( pEnd ){
      pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
    }
    zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
................................................................................
    return SQLITE_OK;
  }

  pModule = pTab->pModule;
  zModule = pTab->azModuleArg[0];
  if( !pModule || !pModule->xConnect ){
    const char *zModule = pTab->azModuleArg[0];
    sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
    rc = SQLITE_ERROR;
  } else {
    char **azArg = pTab->azModuleArg;
    int nArg = pTab->nModuleArg;
    sqlite3 *db = pParse->db;
    assert( !db->pVTab );
    db->pVTab = pTab;
................................................................................
  zModule = pTab->azModuleArg[0];

  /* If the module has been registered and includes a Create method, 
  ** invoke it now. If the module has not been registered, return an 
  ** error. Otherwise, do nothing.
  */
  if( !pModule ){
    *pzErr = sqlite3MPrintf("no such module: %s", zModule);
    rc = SQLITE_ERROR;
  }else{
    int rc2;
    char **azArg = pTab->azModuleArg;
    int nArg = pTab->nModuleArg;

    assert( !db->pVTab );
    assert( pModule->xCreate );
    db->pVTab = pTab;
    rc = sqlite3SafetyOff(db);
    assert( rc==SQLITE_OK );
    rc = pModule->xCreate(db, pModule, nArg, azArg, &pTab->pVtab);
    rc2 = sqlite3SafetyOn(db);

    if( SQLITE_OK!=rc ){
      *pzErr = sqlite3MPrintf("vtable constructor failed: %s", zTab);
    } else if( db->pVTab ){
      const char *zFormat = "vtable constructor did not declare schema: %s";
      *pzErr = sqlite3MPrintf(zFormat, zTab);
      rc = SQLITE_ERROR;
    } 
    db->pVTab = 0;
    if( rc==SQLITE_OK ){
      rc = rc2;
    }
  }

  return rc;
}

/*

Changes to test/vtab1.test.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

























24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


39


40



41
42
43
44
45
46
47
48
49







50





51
52
53














54














55
56
57
58
59
60
61
..
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

84
85
86
87
88
89
90
91
92

93

94

95
96












97





98
99
100
101
102
103



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

144
145
146
147
148
149
150
151
152
153

154
155


156
157
158
159




160
161
162
163
164
165
166
167
168
...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185















186
187

188
189
190
191
192
193
194
195
196

197
198
199
200
201
202
203
204
205

206
207
208
209
210
211
212
213
214

215
216

#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is creating and dropping virtual tables.
#
# $Id: vtab1.test,v 1.11 2006/06/13 23:51:35 drh Exp $

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

ifcapable !vtab {
  finish_test
  return
}


























# We cannot create a virtual table if the module has not
# been registered.
#
do_test vtab1-1.1 {
  catchsql {
    CREATE VIRTUAL TABLE t1 USING echo;
  }
} {1 {unknown module: echo}}
do_test vtab1-1.2 {
  execsql {
    SELECT name FROM sqlite_master ORDER BY 1
  }
} {}

# After we register the echo module, we are allowed to create


# virtual tables using that module.


#



do_test vtab1-1.3 {
  register_echo_module [sqlite3_connection_pointer db]
  catchsql {
    CREATE VIRTUAL TABLE t1 USING echo;
  }
} {0 {}}
do_test vtab1-1.4 {
  set echo_module
} {xCreate echo}







do_test vtab1-1.5 {





  execsql {
    SELECT name, sql FROM sqlite_master ORDER BY 1
  }














} {t1 {CREATE VIRTUAL TABLE t1 USING echo}}















# If a single argument is passed to the echo module during table
# creation, it is assumed to be the name of a table in the same
# database. The echo module attempts to set the schema of the
# new virtual table to be the same as the existing database table.
#
do_test vtab1-2.1 {
................................................................................
} [list         \
  0 a {} 0 {} 0 \
  1 b {} 0 {} 0 \
  2 c {} 0 {} 0 \
]
do_test vtab1-2.2 {
  execsql {
    CREATE VIRTUAL TABLE t2 USING echo(template);
  }
  execsql { PRAGMA table_info(t2); }
} [list         \
  0 a {} 0 {} 0 \
  1 b {} 0 {} 0 \
  2 c {} 0 {} 0 \
]

# Test that the database can be unloaded. This should invoke 
# the xDisconnect() callback each of the two virtual tables - t1 and t2.

do_test vtab1-2.3 {
breakpoint
  set echo_module [list]
  db close
  set echo_module
} [list xDisconnect xDisconnect]

  set echo_module [list]
# Re-open the database. Check that the schema of the virtual

# table is still correct.

do_test vtab1-2.4 {

  sqlite3 db test.db
  db cache size 0












  register_echo_module [sqlite3_connection_pointer db]





  execsql { PRAGMA table_info(t2); }
} [list         \
  0 a {} 0 {} 0 \
  1 b {} 0 {} 0 \
  2 c {} 0 {} 0 \
]




# Drop the table t2. This should cause the xDestroy (but not xDisconnect)
# method to be invoked.
do_test vtab1-2.5 {
  set echo_module [list]
  execsql { 
    DROP TABLE t2;
  }
  set echo_module
} [list xDestroy]

do_test vtab1-2.6 {
  execsql { 
    PRAGMA table_info(t2); 
  }
} {}
do_test vtab1-2.7 {
  execsql {
    SELECT sql FROM sqlite_master;
  }
} [list {CREATE VIRTUAL TABLE t1 USING echo} \
        {CREATE TABLE template(a, b, c)}     \
]

do_test vtab1-2.8 {
  set echo_module [list]
  execsql { 
    DROP TABLE t1;
    DROP TABLE template;
  }
  set echo_module
} [list]
do_test vtab1-2.9 {
  execsql {
    SELECT sql FROM sqlite_master;
  }
} [list]

#----------------------------------------------------------------------
# Test case vtab1-3 tests simple linear scans (no filter conditions) of 
# virtual table modules.

do_test vtab1-3.1 {
  set echo_module ""
  execsql {
    CREATE TABLE treal(a INTEGER, b INTEGER, c); 
    CREATE INDEX treal_idx ON treal(b);
    CREATE VIRTUAL TABLE t1 USING echo(treal);
  }
  set echo_module
} [list xCreate echo treal]
do_test vtab1-3.2 {

  # Test that a SELECT on t2 doesn't crash. No rows are returned
  # because the underlying real table, is currently empty.


  execsql {
    SELECT a, b, c FROM t1;
  }
} {}




do_test vtab1-3.3 {
  # Put some data into the table treal. Then try a select on t1.
  execsql {
    INSERT INTO treal VALUES(1, 2, 3);
    INSERT INTO treal VALUES(4, 5, 6);
    SELECT * FROM t1;
  }
} {1 2 3 4 5 6}
do_test vtab1-3.4 {
................................................................................
  }
} {1 4}
do_test vtab1-3.5 {
  execsql {
    SELECT rowid FROM t1;
  }
} {1 2}

do_test vtab1-3.6 {
  set echo_module ""
  execsql {
    SELECT * FROM t1;
  }
} {1 2 3 4 5 6}
do_test vtab1-3.7 {















  set echo_module
} {xBestIndex {SELECT rowid, * FROM 'treal'} xFilter {SELECT rowid, * FROM 'treal'}}

do_test vtab1-3.8 {
  set echo_module ""
  execsql {
    SELECT * FROM t1 WHERE b = 5;
  }
} {4 5 6}
do_test vtab1-3.9 {
  set echo_module
} {xBestIndex {SELECT rowid, * FROM 'treal' WHERE b = ?} xFilter {SELECT rowid, * FROM 'treal' WHERE b = ?} 5}

do_test vtab1-3.10 {
  set echo_module ""
  execsql {
    SELECT * FROM t1 WHERE b >= 5 AND b <= 10;
  }
} {4 5 6}
do_test vtab1-3.11 {
  set echo_module
} {xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} xFilter {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 5 10}

do_test vtab1-3.12 {
  set echo_module ""
  execsql {
    SELECT * FROM t1 WHERE b BETWEEN 2 AND 10;
  }
} {1 2 3 4 5 6}
do_test vtab1-3.13 {
  set echo_module
} {xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} xFilter {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 2 10}


finish_test








|









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<





|






|
>
>
|
>
>

>
>
>

<



|

|
|
>
>
>
>
>
>
>

>
>
>
>
>

|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|

|






|
|
>

<



|

<
|
>
|
>

>


>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
|





>
>
>

|
|

|

|


|
<


|






<
|
|
>

<

<

<
<
<
<
<





|
|
>









<
>
|
|
>
>




>
>
>
>

<







 







<







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
>
|





|

|
>








|
>








|
>


>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

156
157
158
159
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

206
207
208
209
210
211
212
213
214

215
216
217
218

219

220





221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245
246
247
248
249
250
251

252
253
254
255
256
257
258
...
261
262
263
264
265
266
267

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is creating and dropping virtual tables.
#
# $Id: vtab1.test,v 1.12 2006/06/14 06:31:28 danielk1977 Exp $

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

ifcapable !vtab {
  finish_test
  return
}

#----------------------------------------------------------------------
# Organization of tests in this file:
#
# vtab1-1.*: Error conditions and other issues surrounding creation/connection
#            of a virtual module.
# vtab1-2.*: Test sqlite3_declare_vtab() and the xConnect/xDisconnect methods.
# vtab1-3.*: Table scans and WHERE clauses.
# vtab1-4.*: Table scans and ORDER BY clauses.
# vtab1-5.*: Test queries that include joins. This brings the
#            sqlite3_index_info.estimatedCost variable into play.
#
# This file uses the "echo" module (see src/test8.c). Refer to comments
# in that file for the special behaviour of the Tcl $echo_module variable.
#
# TODO: 
#
# * How to test the sqlite3_index_constraint_usage.omit field? Or
#   sqlite3_index_info.orderByConsumed?
#


#----------------------------------------------------------------------
# Test cases vtab1.1.*
#

# We cannot create a virtual table if the module has not been registered.

#
do_test vtab1-1.1 {
  catchsql {
    CREATE VIRTUAL TABLE t1 USING echo;
  }
} {1 {no such module: echo}}
do_test vtab1-1.2 {
  execsql {
    SELECT name FROM sqlite_master ORDER BY 1
  }
} {}

# Register the module
register_echo_module [sqlite3_connection_pointer db]

# Once a module has been registered, virtual tables using that module
# may be created. However if a module xCreate() fails to call
# sqlite3_declare_vtab() an error will be raised and the table not created.
#
# The "echo" module does not invoke sqlite3_declare_vtab() if it is
# passed zero arguments.
#
do_test vtab1-1.3 {

  catchsql {
    CREATE VIRTUAL TABLE t1 USING echo;
  }
} {1 {vtable constructor did not declare schema: t1}}
do_test vtab1-1.4 {
  execsql {
    SELECT name FROM sqlite_master ORDER BY 1
  }
} {}

# The "echo" module xCreate method returns an error and does not create
# the virtual table if it is passed an argument that does not correspond
# to an existing real table in the same database.
#
do_test vtab1-1.5 {
  catchsql {
    CREATE VIRTUAL TABLE t1 USING echo(no_such_table);
  }
} {1 {vtable constructor failed: t1}}
do_test vtab1-1.6 {
  execsql {
    SELECT name FROM sqlite_master ORDER BY 1
  }
} {}

# Test to make sure nothing goes wrong and no memory is leaked if we 
# select an illegal table-name (i.e a reserved name or the name of a
# table that already exists).
#
do_test vtab1-1.7 {
  catchsql {
    CREATE VIRTUAL TABLE sqlite_master USING echo;
  }
} {1 {object name reserved for internal use: sqlite_master}}
do_test vtab1-1.8 {
  catchsql {
    CREATE TABLE treal(a, b, c);
    CREATE VIRTUAL TABLE treal USING echo(treal);
  }
} {1 {table treal already exists}}
do_test vtab1-1.9 {
  execsql {
    DROP TABLE treal;
    SELECT name FROM sqlite_master ORDER BY 1
  }
} {}

#----------------------------------------------------------------------
# Test cases vtab1.2.*
#
# At this point, the database is completely empty. The echo module
# has already been registered.

# If a single argument is passed to the echo module during table
# creation, it is assumed to be the name of a table in the same
# database. The echo module attempts to set the schema of the
# new virtual table to be the same as the existing database table.
#
do_test vtab1-2.1 {
................................................................................
} [list         \
  0 a {} 0 {} 0 \
  1 b {} 0 {} 0 \
  2 c {} 0 {} 0 \
]
do_test vtab1-2.2 {
  execsql {
    CREATE VIRTUAL TABLE t1 USING echo(template);
  }
  execsql { PRAGMA table_info(t1); }
} [list         \
  0 a {} 0 {} 0 \
  1 b {} 0 {} 0 \
  2 c {} 0 {} 0 \
]

# Test that the database can be unloaded. This should invoke the xDisconnect()
# callback for the successfully create virtual table (t1).
#
do_test vtab1-2.3 {

  set echo_module [list]
  db close
  set echo_module
} [list xDisconnect]


# Re-open the database. This should not cause any virtual methods to 
# be called. The invocation of xConnect() is delayed until the virtual
# table schema is first required by the compiler.
#
do_test vtab1-2.4 {
  set echo_module [list]
  sqlite3 db test.db
  db cache size 0
  set echo_module
} {}

# Try to query the virtual table schema. This should fail, as the
# echo module has not been registered with this database connection.
#
do_test vtab1.2.6 {
breakpoint
  catchsql { PRAGMA table_info(t1); }
} {1 {no such module: echo}}

# Register the module
register_echo_module [sqlite3_connection_pointer db]

# Try to query the virtual table schema again. This time it should
# invoke the xConnect method and succeed.
#
do_test vtab1.2.7 {
  execsql { PRAGMA table_info(t1); }
} [list         \
  0 a {} 0 {} 0 \
  1 b {} 0 {} 0 \
  2 c {} 0 {} 0 \
]
do_test vtab1.2.8 {
  set echo_module
} {xConnect echo template}

# Drop table t1. This should cause the xDestroy (but not xDisconnect) method 
# to be invoked.
do_test vtab1-2.5 {
  set echo_module ""
  execsql { 
    DROP TABLE t1;
  }
  set echo_module
} {xDestroy}

do_test vtab1-2.6 {
  execsql { 
    PRAGMA table_info(t1); 
  }
} {}
do_test vtab1-2.7 {
  execsql {
    SELECT sql FROM sqlite_master;
  }

} [list {CREATE TABLE template(a, b, c)}]

# Clean up other test artifacts:
do_test vtab1-2.8 {

  execsql { 

    DROP TABLE template;





    SELECT sql FROM sqlite_master;
  }
} [list]

#----------------------------------------------------------------------
# Test case vtab1-3 test table scans and the echo module's 
# xBestIndex/xFilter handling of WHERE conditions.

do_test vtab1-3.1 {
  set echo_module ""
  execsql {
    CREATE TABLE treal(a INTEGER, b INTEGER, c); 
    CREATE INDEX treal_idx ON treal(b);
    CREATE VIRTUAL TABLE t1 USING echo(treal);
  }
  set echo_module
} [list xCreate echo treal]


# Test that a SELECT on t1 doesn't crash. No rows are returned
# because the underlying real table is currently empty.
#
do_test vtab1-3.2 {
  execsql {
    SELECT a, b, c FROM t1;
  }
} {}

# Put some data into the table treal. Then try a few simple SELECT 
# statements on t1.
#
do_test vtab1-3.3 {

  execsql {
    INSERT INTO treal VALUES(1, 2, 3);
    INSERT INTO treal VALUES(4, 5, 6);
    SELECT * FROM t1;
  }
} {1 2 3 4 5 6}
do_test vtab1-3.4 {
................................................................................
  }
} {1 4}
do_test vtab1-3.5 {
  execsql {
    SELECT rowid FROM t1;
  }
} {1 2}

do_test vtab1-3.6 {
  set echo_module ""
  execsql {
    SELECT * FROM t1;
  }
} {1 2 3 4 5 6}
do_test vtab1-3.7 {
  execsql {
    SELECT rowid, * FROM t1;
  }
} {1 1 2 3 2 4 5 6}

# Execute some SELECT statements with WHERE clauses on the t1 table.
# Then check the echo_module variable (written to by the module methods
# in test8.c) to make sure the xBestIndex() and xFilter() methods were
# called correctly.
#
do_test vtab1-3.8 {
  set echo_module ""
  execsql {
    SELECT * FROM t1;
  }
  set echo_module
} [list xBestIndex {SELECT rowid, * FROM 'treal'} \
        xFilter    {SELECT rowid, * FROM 'treal'} ]
do_test vtab1-3.9 {
  set echo_module ""
  execsql {
    SELECT * FROM t1 WHERE b = 5;
  }
} {4 5 6}
do_test vtab1-3.10 {
  set echo_module
} [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b = ?}   \
        xFilter    {SELECT rowid, * FROM 'treal' WHERE b = ?} 5 ]
do_test vtab1-3.10 {
  set echo_module ""
  execsql {
    SELECT * FROM t1 WHERE b >= 5 AND b <= 10;
  }
} {4 5 6}
do_test vtab1-3.11 {
  set echo_module
} [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?}      \
        xFilter    {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 5 10 ]
do_test vtab1-3.12 {
  set echo_module ""
  execsql {
    SELECT * FROM t1 WHERE b BETWEEN 2 AND 10;
  }
} {1 2 3 4 5 6}
do_test vtab1-3.13 {
  set echo_module
} [list xBestIndex {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?}      \
        xFilter    {SELECT rowid, * FROM 'treal' WHERE b >= ? AND b <= ?} 2 10 ]

finish_test