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.
17 /* The code in this file defines a sqlite3 virtual-table module that
18 ** provides a read-only view of the current database schema. There is one
19 ** row in the schema table for each column in the database schema.
23 "database," /* Name of database (i.e. main, temp etc.) */ \
24 "tablename," /* Name of table */ \
25 "cid," /* Column number (from left-to-right, 0 upward) */ \
26 "name," /* Column name */ \
27 "type," /* Specified type (i.e. VARCHAR(32)) */ \
28 "not_null," /* Boolean. True if NOT NULL was specified */ \
29 "dflt_value," /* Default value for this column */ \
30 "pk" /* True if this column is part of the primary key */ \
33 /* If SQLITE_TEST is defined this code is preprocessed for use as part
34 ** of the sqlite test binary "testfixture". Otherwise it is preprocessed
35 ** to be compiled into an sqlite dynamic extension.
38 #include "sqliteInt.h"
41 #include "sqlite3ext.h"
42 SQLITE_EXTENSION_INIT1
49 typedef struct schema_vtab schema_vtab
;
50 typedef struct schema_cursor schema_cursor
;
52 /* A schema table object */
58 /* A schema table cursor object */
59 struct schema_cursor
{
60 sqlite3_vtab_cursor base
;
61 sqlite3_stmt
*pDbList
;
62 sqlite3_stmt
*pTableList
;
63 sqlite3_stmt
*pColumnList
;
68 ** None of this works unless we have virtual tables.
70 #ifndef SQLITE_OMIT_VIRTUALTABLE
73 ** Table destructor for the schema module.
75 static int schemaDestroy(sqlite3_vtab
*pVtab
){
81 ** Table constructor for the schema module.
83 static int schemaCreate(
86 int argc
, const char *const*argv
,
87 sqlite3_vtab
**ppVtab
,
90 int rc
= SQLITE_NOMEM
;
91 schema_vtab
*pVtab
= sqlite3_malloc(sizeof(schema_vtab
));
93 memset(pVtab
, 0, sizeof(schema_vtab
));
95 #ifndef SQLITE_OMIT_VIRTUALTABLE
96 rc
= sqlite3_declare_vtab(db
, SCHEMA
);
99 *ppVtab
= (sqlite3_vtab
*)pVtab
;
104 ** Open a new cursor on the schema table.
106 static int schemaOpen(sqlite3_vtab
*pVTab
, sqlite3_vtab_cursor
**ppCursor
){
107 int rc
= SQLITE_NOMEM
;
109 pCur
= sqlite3_malloc(sizeof(schema_cursor
));
111 memset(pCur
, 0, sizeof(schema_cursor
));
112 *ppCursor
= (sqlite3_vtab_cursor
*)pCur
;
119 ** Close a schema table cursor.
121 static int schemaClose(sqlite3_vtab_cursor
*cur
){
122 schema_cursor
*pCur
= (schema_cursor
*)cur
;
123 sqlite3_finalize(pCur
->pDbList
);
124 sqlite3_finalize(pCur
->pTableList
);
125 sqlite3_finalize(pCur
->pColumnList
);
131 ** Retrieve a column of data.
133 static int schemaColumn(sqlite3_vtab_cursor
*cur
, sqlite3_context
*ctx
, int i
){
134 schema_cursor
*pCur
= (schema_cursor
*)cur
;
137 sqlite3_result_value(ctx
, sqlite3_column_value(pCur
->pDbList
, 1));
140 sqlite3_result_value(ctx
, sqlite3_column_value(pCur
->pTableList
, 0));
143 sqlite3_result_value(ctx
, sqlite3_column_value(pCur
->pColumnList
, i
-2));
150 ** Retrieve the current rowid.
152 static int schemaRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
153 schema_cursor
*pCur
= (schema_cursor
*)cur
;
154 *pRowid
= pCur
->rowid
;
158 static int finalize(sqlite3_stmt
**ppStmt
){
159 int rc
= sqlite3_finalize(*ppStmt
);
164 static int schemaEof(sqlite3_vtab_cursor
*cur
){
165 schema_cursor
*pCur
= (schema_cursor
*)cur
;
166 return (pCur
->pDbList
? 0 : 1);
170 ** Advance the cursor to the next row.
172 static int schemaNext(sqlite3_vtab_cursor
*cur
){
174 schema_cursor
*pCur
= (schema_cursor
*)cur
;
175 schema_vtab
*pVtab
= (schema_vtab
*)(cur
->pVtab
);
178 while( !pCur
->pColumnList
|| SQLITE_ROW
!=sqlite3_step(pCur
->pColumnList
) ){
179 if( SQLITE_OK
!=(rc
= finalize(&pCur
->pColumnList
)) ) goto next_exit
;
181 while( !pCur
->pTableList
|| SQLITE_ROW
!=sqlite3_step(pCur
->pTableList
) ){
182 if( SQLITE_OK
!=(rc
= finalize(&pCur
->pTableList
)) ) goto next_exit
;
184 assert(pCur
->pDbList
);
185 while( SQLITE_ROW
!=sqlite3_step(pCur
->pDbList
) ){
186 rc
= finalize(&pCur
->pDbList
);
190 /* Set zSql to the SQL to pull the list of tables from the
191 ** sqlite_master (or sqlite_temp_master) table of the database
192 ** identfied by the row pointed to by the SQL statement pCur->pDbList
193 ** (iterating through a "PRAGMA database_list;" statement).
195 if( sqlite3_column_int(pCur
->pDbList
, 0)==1 ){
196 zSql
= sqlite3_mprintf(
197 "SELECT name FROM sqlite_temp_master WHERE type='table'"
200 sqlite3_stmt
*pDbList
= pCur
->pDbList
;
201 zSql
= sqlite3_mprintf(
202 "SELECT name FROM %Q.sqlite_master WHERE type='table'",
203 sqlite3_column_text(pDbList
, 1)
211 rc
= sqlite3_prepare(pVtab
->db
, zSql
, -1, &pCur
->pTableList
, 0);
213 if( rc
!=SQLITE_OK
) goto next_exit
;
216 /* Set zSql to the SQL to the table_info pragma for the table currently
217 ** identified by the rows pointed to by statements pCur->pDbList and
220 zSql
= sqlite3_mprintf("PRAGMA %Q.table_info(%Q)",
221 sqlite3_column_text(pCur
->pDbList
, 1),
222 sqlite3_column_text(pCur
->pTableList
, 0)
229 rc
= sqlite3_prepare(pVtab
->db
, zSql
, -1, &pCur
->pColumnList
, 0);
231 if( rc
!=SQLITE_OK
) goto next_exit
;
236 /* TODO: Handle rc */
241 ** Reset a schema table cursor.
243 static int schemaFilter(
244 sqlite3_vtab_cursor
*pVtabCursor
,
245 int idxNum
, const char *idxStr
,
246 int argc
, sqlite3_value
**argv
249 schema_vtab
*pVtab
= (schema_vtab
*)(pVtabCursor
->pVtab
);
250 schema_cursor
*pCur
= (schema_cursor
*)pVtabCursor
;
252 finalize(&pCur
->pTableList
);
253 finalize(&pCur
->pColumnList
);
254 finalize(&pCur
->pDbList
);
255 rc
= sqlite3_prepare(pVtab
->db
,"PRAGMA database_list", -1, &pCur
->pDbList
, 0);
256 return (rc
==SQLITE_OK
? schemaNext(pVtabCursor
) : rc
);
260 ** Analyse the WHERE condition.
262 static int schemaBestIndex(sqlite3_vtab
*tab
, sqlite3_index_info
*pIdxInfo
){
267 ** A virtual table module that merely echos method calls into TCL
270 static sqlite3_module schemaModule
= {
277 schemaOpen
, /* xOpen - open a cursor */
278 schemaClose
, /* xClose - close a cursor */
279 schemaFilter
, /* xFilter - configure scan constraints */
280 schemaNext
, /* xNext - advance a cursor */
281 schemaEof
, /* xEof */
282 schemaColumn
, /* xColumn - read data */
283 schemaRowid
, /* xRowid - read data */
293 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
298 ** Decode a pointer to an sqlite3 object.
300 extern int getDbPointer(Tcl_Interp
*interp
, const char *zA
, sqlite3
**ppDb
);
303 ** Register the schema virtual table module.
305 static int register_schema_module(
306 ClientData clientData
, /* Not used */
307 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
308 int objc
, /* Number of arguments */
309 Tcl_Obj
*CONST objv
[] /* Command arguments */
313 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
316 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
317 #ifndef SQLITE_OMIT_VIRTUALTABLE
318 sqlite3_create_module(db
, "schema", &schemaModule
, 0);
324 ** Register commands with the TCL interpreter.
326 int Sqlitetestschema_Init(Tcl_Interp
*interp
){
329 Tcl_ObjCmdProc
*xProc
;
332 { "register_schema_module", register_schema_module
, 0 },
335 for(i
=0; i
<sizeof(aObjCmd
)/sizeof(aObjCmd
[0]); i
++){
336 Tcl_CreateObjCommand(interp
, aObjCmd
[i
].zName
,
337 aObjCmd
[i
].xProc
, aObjCmd
[i
].clientData
, 0);
345 ** Extension load function.
347 int sqlite3_extension_init(
350 const sqlite3_api_routines
*pApi
352 SQLITE_EXTENSION_INIT2(pApi
);
353 #ifndef SQLITE_OMIT_VIRTUALTABLE
354 sqlite3_create_module(db
, "schema", &schemaModule
, 0);