Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 660d21ea4ec93991f9f22f7d787c3400c2e92c5a [file] [log] [blame]
danielk1977954ce992006-06-15 15:59:191/*
2** 2006 June 10
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
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.
10**
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.
danielk1977954ce992006-06-15 15:59:1915*/
16
drh428397c2006-06-17 13:21:3217/* 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.
danielk1977954ce992006-06-15 15:59:1920*/
21#define SCHEMA \
22"CREATE TABLE x(" \
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 */ \
31")"
32
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.
36*/
37#ifdef SQLITE_TEST
mistachkin52b1dbb2016-07-28 14:37:0438# include "sqliteInt.h"
drh064b6812024-07-30 15:49:0239# include "tclsqlite.h"
danielk1977954ce992006-06-15 15:59:1940#else
mistachkin52b1dbb2016-07-28 14:37:0441# include "sqlite3ext.h"
danielk1977954ce992006-06-15 15:59:1942 SQLITE_EXTENSION_INIT1
danielk1977954ce992006-06-15 15:59:1943#endif
44
45#include <stdlib.h>
46#include <string.h>
47#include <assert.h>
48
49typedef struct schema_vtab schema_vtab;
50typedef struct schema_cursor schema_cursor;
51
52/* A schema table object */
53struct schema_vtab {
54 sqlite3_vtab base;
55 sqlite3 *db;
56};
57
58/* A schema table cursor object */
59struct schema_cursor {
60 sqlite3_vtab_cursor base;
61 sqlite3_stmt *pDbList;
62 sqlite3_stmt *pTableList;
63 sqlite3_stmt *pColumnList;
64 int rowid;
65};
66
67/*
drh26e4a8b2008-05-01 17:16:5268** None of this works unless we have virtual tables.
69*/
70#ifndef SQLITE_OMIT_VIRTUALTABLE
71
72/*
danielk1977954ce992006-06-15 15:59:1973** Table destructor for the schema module.
74*/
75static int schemaDestroy(sqlite3_vtab *pVtab){
drh17435752007-08-16 04:30:3876 sqlite3_free(pVtab);
danielk1977954ce992006-06-15 15:59:1977 return 0;
78}
79
80/*
81** Table constructor for the schema module.
82*/
83static int schemaCreate(
84 sqlite3 *db,
85 void *pAux,
drhe4102962006-09-11 00:34:2286 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:5887 sqlite3_vtab **ppVtab,
88 char **pzErr
danielk1977954ce992006-06-15 15:59:1989){
90 int rc = SQLITE_NOMEM;
drh17435752007-08-16 04:30:3891 schema_vtab *pVtab = sqlite3_malloc(sizeof(schema_vtab));
danielk1977954ce992006-06-15 15:59:1992 if( pVtab ){
93 memset(pVtab, 0, sizeof(schema_vtab));
94 pVtab->db = db;
danielk19774b2688a2006-06-20 11:01:0795#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977954ce992006-06-15 15:59:1996 rc = sqlite3_declare_vtab(db, SCHEMA);
danielk19774b2688a2006-06-20 11:01:0797#endif
danielk1977954ce992006-06-15 15:59:1998 }
99 *ppVtab = (sqlite3_vtab *)pVtab;
100 return rc;
101}
102
103/*
104** Open a new cursor on the schema table.
105*/
106static int schemaOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
107 int rc = SQLITE_NOMEM;
108 schema_cursor *pCur;
drh17435752007-08-16 04:30:38109 pCur = sqlite3_malloc(sizeof(schema_cursor));
danielk1977954ce992006-06-15 15:59:19110 if( pCur ){
111 memset(pCur, 0, sizeof(schema_cursor));
112 *ppCursor = (sqlite3_vtab_cursor *)pCur;
113 rc = SQLITE_OK;
114 }
115 return rc;
116}
117
118/*
119** Close a schema table cursor.
120*/
121static 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);
drh17435752007-08-16 04:30:38126 sqlite3_free(pCur);
danielk1977954ce992006-06-15 15:59:19127 return SQLITE_OK;
128}
129
130/*
131** Retrieve a column of data.
132*/
133static int schemaColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
134 schema_cursor *pCur = (schema_cursor *)cur;
135 switch( i ){
136 case 0:
137 sqlite3_result_value(ctx, sqlite3_column_value(pCur->pDbList, 1));
138 break;
139 case 1:
140 sqlite3_result_value(ctx, sqlite3_column_value(pCur->pTableList, 0));
141 break;
142 default:
143 sqlite3_result_value(ctx, sqlite3_column_value(pCur->pColumnList, i-2));
144 break;
145 }
146 return SQLITE_OK;
147}
148
149/*
150** Retrieve the current rowid.
151*/
152static int schemaRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
153 schema_cursor *pCur = (schema_cursor *)cur;
154 *pRowid = pCur->rowid;
155 return SQLITE_OK;
156}
157
158static int finalize(sqlite3_stmt **ppStmt){
159 int rc = sqlite3_finalize(*ppStmt);
160 *ppStmt = 0;
161 return rc;
162}
163
danielk1977a298e902006-06-22 09:53:48164static int schemaEof(sqlite3_vtab_cursor *cur){
165 schema_cursor *pCur = (schema_cursor *)cur;
166 return (pCur->pDbList ? 0 : 1);
167}
168
danielk1977954ce992006-06-15 15:59:19169/*
170** Advance the cursor to the next row.
171*/
172static int schemaNext(sqlite3_vtab_cursor *cur){
danielk19775017dc32006-06-24 09:34:22173 int rc = SQLITE_OK;
danielk1977954ce992006-06-15 15:59:19174 schema_cursor *pCur = (schema_cursor *)cur;
175 schema_vtab *pVtab = (schema_vtab *)(cur->pVtab);
176 char *zSql = 0;
177
178 while( !pCur->pColumnList || SQLITE_ROW!=sqlite3_step(pCur->pColumnList) ){
danielk1977a298e902006-06-22 09:53:48179 if( SQLITE_OK!=(rc = finalize(&pCur->pColumnList)) ) goto next_exit;
danielk1977954ce992006-06-15 15:59:19180
181 while( !pCur->pTableList || SQLITE_ROW!=sqlite3_step(pCur->pTableList) ){
danielk1977a298e902006-06-22 09:53:48182 if( SQLITE_OK!=(rc = finalize(&pCur->pTableList)) ) goto next_exit;
danielk1977954ce992006-06-15 15:59:19183
184 assert(pCur->pDbList);
185 while( SQLITE_ROW!=sqlite3_step(pCur->pDbList) ){
danielk1977a298e902006-06-22 09:53:48186 rc = finalize(&pCur->pDbList);
187 goto next_exit;
danielk1977954ce992006-06-15 15:59:19188 }
189
190 /* Set zSql to the SQL to pull the list of tables from the
drh1e32bed2020-06-19 13:33:53191 ** sqlite_schema (or sqlite_temp_schema) table of the database
peter.d.reid60ec9142014-09-06 16:39:46192 ** identified by the row pointed to by the SQL statement pCur->pDbList
danielk1977954ce992006-06-15 15:59:19193 ** (iterating through a "PRAGMA database_list;" statement).
194 */
195 if( sqlite3_column_int(pCur->pDbList, 0)==1 ){
196 zSql = sqlite3_mprintf(
drh1e32bed2020-06-19 13:33:53197 "SELECT name FROM sqlite_temp_schema WHERE type='table'"
danielk1977954ce992006-06-15 15:59:19198 );
199 }else{
200 sqlite3_stmt *pDbList = pCur->pDbList;
201 zSql = sqlite3_mprintf(
drh1e32bed2020-06-19 13:33:53202 "SELECT name FROM %Q.sqlite_schema WHERE type='table'",
danielk1977954ce992006-06-15 15:59:19203 sqlite3_column_text(pDbList, 1)
204 );
205 }
206 if( !zSql ){
207 rc = SQLITE_NOMEM;
danielk1977a298e902006-06-22 09:53:48208 goto next_exit;
danielk1977954ce992006-06-15 15:59:19209 }
210
211 rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pTableList, 0);
212 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48213 if( rc!=SQLITE_OK ) goto next_exit;
danielk1977954ce992006-06-15 15:59:19214 }
215
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
218 ** pCur->pTableList.
219 */
220 zSql = sqlite3_mprintf("PRAGMA %Q.table_info(%Q)",
221 sqlite3_column_text(pCur->pDbList, 1),
222 sqlite3_column_text(pCur->pTableList, 0)
223 );
224
225 if( !zSql ){
226 rc = SQLITE_NOMEM;
danielk1977a298e902006-06-22 09:53:48227 goto next_exit;
danielk1977954ce992006-06-15 15:59:19228 }
229 rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pColumnList, 0);
230 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48231 if( rc!=SQLITE_OK ) goto next_exit;
danielk1977954ce992006-06-15 15:59:19232 }
233 pCur->rowid++;
234
danielk1977a298e902006-06-22 09:53:48235next_exit:
danielk1977954ce992006-06-15 15:59:19236 /* TODO: Handle rc */
danielk1977a298e902006-06-22 09:53:48237 return rc;
danielk1977954ce992006-06-15 15:59:19238}
239
240/*
241** Reset a schema table cursor.
242*/
243static int schemaFilter(
244 sqlite3_vtab_cursor *pVtabCursor,
245 int idxNum, const char *idxStr,
246 int argc, sqlite3_value **argv
247){
248 int rc;
249 schema_vtab *pVtab = (schema_vtab *)(pVtabCursor->pVtab);
250 schema_cursor *pCur = (schema_cursor *)pVtabCursor;
251 pCur->rowid = 0;
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);
257}
258
259/*
260** Analyse the WHERE condition.
261*/
262static int schemaBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
263 return SQLITE_OK;
264}
265
266/*
267** A virtual table module that merely echos method calls into TCL
268** variables.
269*/
270static sqlite3_module schemaModule = {
271 0, /* iVersion */
danielk1977954ce992006-06-15 15:59:19272 schemaCreate,
273 schemaCreate,
274 schemaBestIndex,
275 schemaDestroy,
276 schemaDestroy,
277 schemaOpen, /* xOpen - open a cursor */
278 schemaClose, /* xClose - close a cursor */
279 schemaFilter, /* xFilter - configure scan constraints */
280 schemaNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48281 schemaEof, /* xEof */
danielk1977954ce992006-06-15 15:59:19282 schemaColumn, /* xColumn - read data */
283 schemaRowid, /* xRowid - read data */
drhb7f6f682006-07-08 17:06:43284 0, /* xUpdate */
285 0, /* xBegin */
286 0, /* xSync */
287 0, /* xCommit */
288 0, /* xRollback */
289 0, /* xFindMethod */
danielk1977c033b642007-06-27 16:26:07290 0, /* xRename */
drh19358872023-10-06 12:51:05291 0, /* xSavepoint */
292 0, /* xRelease */
293 0, /* xRollbackTo */
294 0, /* xShadowName */
295 0 /* xIntegrity */
danielk1977954ce992006-06-15 15:59:19296};
297
drh26e4a8b2008-05-01 17:16:52298#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
danielk1977954ce992006-06-15 15:59:19299
300#ifdef SQLITE_TEST
301
302/*
303** Decode a pointer to an sqlite3 object.
304*/
drh24b58dd2008-07-07 14:50:14305extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
danielk1977954ce992006-06-15 15:59:19306
307/*
308** Register the schema virtual table module.
309*/
mistachkin7617e4a2016-07-28 17:11:20310static int SQLITE_TCLAPI register_schema_module(
danielk1977954ce992006-06-15 15:59:19311 ClientData clientData, /* Not used */
312 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
313 int objc, /* Number of arguments */
314 Tcl_Obj *CONST objv[] /* Command arguments */
315){
316 sqlite3 *db;
317 if( objc!=2 ){
318 Tcl_WrongNumArgs(interp, 1, objv, "DB");
319 return TCL_ERROR;
320 }
danielk19771f6eec52006-06-16 06:17:47321 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977954ce992006-06-15 15:59:19322#ifndef SQLITE_OMIT_VIRTUALTABLE
323 sqlite3_create_module(db, "schema", &schemaModule, 0);
324#endif
325 return TCL_OK;
326}
327
328/*
329** Register commands with the TCL interpreter.
330*/
331int Sqlitetestschema_Init(Tcl_Interp *interp){
332 static struct {
333 char *zName;
334 Tcl_ObjCmdProc *xProc;
335 void *clientData;
336 } aObjCmd[] = {
337 { "register_schema_module", register_schema_module, 0 },
338 };
339 int i;
340 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
341 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
342 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
343 }
344 return TCL_OK;
345}
346
347#else
348
349/*
350** Extension load function.
351*/
mistachkin049d4872013-07-04 23:53:56352#ifdef _WIN32
353__declspec(dllexport)
354#endif
355int sqlite3_schema_init(
danielk1977954ce992006-06-15 15:59:19356 sqlite3 *db,
357 char **pzErrMsg,
358 const sqlite3_api_routines *pApi
359){
360 SQLITE_EXTENSION_INIT2(pApi);
danielk19774b2688a2006-06-20 11:01:07361#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977954ce992006-06-15 15:59:19362 sqlite3_create_module(db, "schema", &schemaModule, 0);
danielk19774b2688a2006-06-20 11:01:07363#endif
danielk1977954ce992006-06-15 15:59:19364 return 0;
365}
366
367#endif