4
** The author disclaims copyright to this source code. In place of
5
** a legal notice, here is a blessing:
7
** May you do good and not evil.
8
** May you find forgiveness for yourself and forgive others.
9
** May you share freely, never taking more than you give.
11
*************************************************************************
12
** Code for testing the virtual table interfaces. This code
13
** is not included in the SQLite library. It is used for automated
14
** testing of the SQLite library.
16
** $Id: test_schema.c,v 1.15 2008/07/07 14:50:14 drh Exp $
19
/* The code in this file defines a sqlite3 virtual-table module that
20
** provides a read-only view of the current database schema. There is one
21
** row in the schema table for each column in the database schema.
25
"database," /* Name of database (i.e. main, temp etc.) */ \
26
"tablename," /* Name of table */ \
27
"cid," /* Column number (from left-to-right, 0 upward) */ \
28
"name," /* Column name */ \
29
"type," /* Specified type (i.e. VARCHAR(32)) */ \
30
"not_null," /* Boolean. True if NOT NULL was specified */ \
31
"dflt_value," /* Default value for this column */ \
32
"pk" /* True if this column is part of the primary key */ \
35
/* If SQLITE_TEST is defined this code is preprocessed for use as part
36
** of the sqlite test binary "testfixture". Otherwise it is preprocessed
37
** to be compiled into an sqlite dynamic extension.
40
#include "sqliteInt.h"
43
#include "sqlite3ext.h"
44
SQLITE_EXTENSION_INIT1
51
typedef struct schema_vtab schema_vtab;
52
typedef struct schema_cursor schema_cursor;
54
/* A schema table object */
60
/* A schema table cursor object */
61
struct schema_cursor {
62
sqlite3_vtab_cursor base;
63
sqlite3_stmt *pDbList;
64
sqlite3_stmt *pTableList;
65
sqlite3_stmt *pColumnList;
70
** None of this works unless we have virtual tables.
72
#ifndef SQLITE_OMIT_VIRTUALTABLE
75
** Table destructor for the schema module.
77
static int schemaDestroy(sqlite3_vtab *pVtab){
83
** Table constructor for the schema module.
85
static int schemaCreate(
88
int argc, const char *const*argv,
89
sqlite3_vtab **ppVtab,
92
int rc = SQLITE_NOMEM;
93
schema_vtab *pVtab = sqlite3_malloc(sizeof(schema_vtab));
95
memset(pVtab, 0, sizeof(schema_vtab));
97
#ifndef SQLITE_OMIT_VIRTUALTABLE
98
rc = sqlite3_declare_vtab(db, SCHEMA);
101
*ppVtab = (sqlite3_vtab *)pVtab;
106
** Open a new cursor on the schema table.
108
static int schemaOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
109
int rc = SQLITE_NOMEM;
111
pCur = sqlite3_malloc(sizeof(schema_cursor));
113
memset(pCur, 0, sizeof(schema_cursor));
114
*ppCursor = (sqlite3_vtab_cursor *)pCur;
121
** Close a schema table cursor.
123
static int schemaClose(sqlite3_vtab_cursor *cur){
124
schema_cursor *pCur = (schema_cursor *)cur;
125
sqlite3_finalize(pCur->pDbList);
126
sqlite3_finalize(pCur->pTableList);
127
sqlite3_finalize(pCur->pColumnList);
133
** Retrieve a column of data.
135
static int schemaColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
136
schema_cursor *pCur = (schema_cursor *)cur;
139
sqlite3_result_value(ctx, sqlite3_column_value(pCur->pDbList, 1));
142
sqlite3_result_value(ctx, sqlite3_column_value(pCur->pTableList, 0));
145
sqlite3_result_value(ctx, sqlite3_column_value(pCur->pColumnList, i-2));
152
** Retrieve the current rowid.
154
static int schemaRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
155
schema_cursor *pCur = (schema_cursor *)cur;
156
*pRowid = pCur->rowid;
160
static int finalize(sqlite3_stmt **ppStmt){
161
int rc = sqlite3_finalize(*ppStmt);
166
static int schemaEof(sqlite3_vtab_cursor *cur){
167
schema_cursor *pCur = (schema_cursor *)cur;
168
return (pCur->pDbList ? 0 : 1);
172
** Advance the cursor to the next row.
174
static int schemaNext(sqlite3_vtab_cursor *cur){
176
schema_cursor *pCur = (schema_cursor *)cur;
177
schema_vtab *pVtab = (schema_vtab *)(cur->pVtab);
180
while( !pCur->pColumnList || SQLITE_ROW!=sqlite3_step(pCur->pColumnList) ){
181
if( SQLITE_OK!=(rc = finalize(&pCur->pColumnList)) ) goto next_exit;
183
while( !pCur->pTableList || SQLITE_ROW!=sqlite3_step(pCur->pTableList) ){
184
if( SQLITE_OK!=(rc = finalize(&pCur->pTableList)) ) goto next_exit;
186
assert(pCur->pDbList);
187
while( SQLITE_ROW!=sqlite3_step(pCur->pDbList) ){
188
rc = finalize(&pCur->pDbList);
192
/* Set zSql to the SQL to pull the list of tables from the
193
** sqlite_master (or sqlite_temp_master) table of the database
194
** identfied by the row pointed to by the SQL statement pCur->pDbList
195
** (iterating through a "PRAGMA database_list;" statement).
197
if( sqlite3_column_int(pCur->pDbList, 0)==1 ){
198
zSql = sqlite3_mprintf(
199
"SELECT name FROM sqlite_temp_master WHERE type='table'"
202
sqlite3_stmt *pDbList = pCur->pDbList;
203
zSql = sqlite3_mprintf(
204
"SELECT name FROM %Q.sqlite_master WHERE type='table'",
205
sqlite3_column_text(pDbList, 1)
213
rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pTableList, 0);
215
if( rc!=SQLITE_OK ) goto next_exit;
218
/* Set zSql to the SQL to the table_info pragma for the table currently
219
** identified by the rows pointed to by statements pCur->pDbList and
222
zSql = sqlite3_mprintf("PRAGMA %Q.table_info(%Q)",
223
sqlite3_column_text(pCur->pDbList, 1),
224
sqlite3_column_text(pCur->pTableList, 0)
231
rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pColumnList, 0);
233
if( rc!=SQLITE_OK ) goto next_exit;
238
/* TODO: Handle rc */
243
** Reset a schema table cursor.
245
static int schemaFilter(
246
sqlite3_vtab_cursor *pVtabCursor,
247
int idxNum, const char *idxStr,
248
int argc, sqlite3_value **argv
251
schema_vtab *pVtab = (schema_vtab *)(pVtabCursor->pVtab);
252
schema_cursor *pCur = (schema_cursor *)pVtabCursor;
254
finalize(&pCur->pTableList);
255
finalize(&pCur->pColumnList);
256
finalize(&pCur->pDbList);
257
rc = sqlite3_prepare(pVtab->db,"PRAGMA database_list", -1, &pCur->pDbList, 0);
258
return (rc==SQLITE_OK ? schemaNext(pVtabCursor) : rc);
262
** Analyse the WHERE condition.
264
static int schemaBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
269
** A virtual table module that merely echos method calls into TCL
272
static sqlite3_module schemaModule = {
279
schemaOpen, /* xOpen - open a cursor */
280
schemaClose, /* xClose - close a cursor */
281
schemaFilter, /* xFilter - configure scan constraints */
282
schemaNext, /* xNext - advance a cursor */
283
schemaEof, /* xEof */
284
schemaColumn, /* xColumn - read data */
285
schemaRowid, /* xRowid - read data */
295
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
300
** Decode a pointer to an sqlite3 object.
302
extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
305
** Register the schema virtual table module.
307
static int register_schema_module(
308
ClientData clientData, /* Not used */
309
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
310
int objc, /* Number of arguments */
311
Tcl_Obj *CONST objv[] /* Command arguments */
315
Tcl_WrongNumArgs(interp, 1, objv, "DB");
318
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
319
#ifndef SQLITE_OMIT_VIRTUALTABLE
320
sqlite3_create_module(db, "schema", &schemaModule, 0);
326
** Register commands with the TCL interpreter.
328
int Sqlitetestschema_Init(Tcl_Interp *interp){
331
Tcl_ObjCmdProc *xProc;
334
{ "register_schema_module", register_schema_module, 0 },
337
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
338
Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
339
aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
347
** Extension load function.
349
int sqlite3_extension_init(
352
const sqlite3_api_routines *pApi
354
SQLITE_EXTENSION_INIT2(pApi);
355
#ifndef SQLITE_OMIT_VIRTUALTABLE
356
sqlite3_create_module(db, "schema", &schemaModule, 0);