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 ** The emphasis of this file is a virtual table that provides
17 ** access to TCL variables.
19 #include "sqliteInt.h"
24 #ifndef SQLITE_OMIT_VIRTUALTABLE
26 typedef struct tclvar_vtab tclvar_vtab
;
27 typedef struct tclvar_cursor tclvar_cursor
;
30 ** A tclvar virtual-table object
37 /* A tclvar cursor object */
38 struct tclvar_cursor
{
39 sqlite3_vtab_cursor base
;
41 Tcl_Obj
*pList1
; /* Result of [info vars ?pattern?] */
42 Tcl_Obj
*pList2
; /* Result of [array names [lindex $pList1 $i1]] */
43 int i1
; /* Current item in pList1 */
44 int i2
; /* Current item (if any) in pList2 */
47 /* Methods for the tclvar module */
48 static int tclvarConnect(
51 int argc
, const char *const*argv
,
52 sqlite3_vtab
**ppVtab
,
56 static const char zSchema
[] =
57 "CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)";
58 pVtab
= sqlite3MallocZero( sizeof(*pVtab
) );
59 if( pVtab
==0 ) return SQLITE_NOMEM
;
60 *ppVtab
= &pVtab
->base
;
61 pVtab
->interp
= (Tcl_Interp
*)pAux
;
62 sqlite3_declare_vtab(db
, zSchema
);
65 /* Note that for this virtual table, the xCreate and xConnect
66 ** methods are identical. */
68 static int tclvarDisconnect(sqlite3_vtab
*pVtab
){
72 /* The xDisconnect and xDestroy methods are also the same */
75 ** Open a new tclvar cursor.
77 static int tclvarOpen(sqlite3_vtab
*pVTab
, sqlite3_vtab_cursor
**ppCursor
){
79 pCur
= sqlite3MallocZero(sizeof(tclvar_cursor
));
80 *ppCursor
= &pCur
->base
;
85 ** Close a tclvar cursor.
87 static int tclvarClose(sqlite3_vtab_cursor
*cur
){
88 tclvar_cursor
*pCur
= (tclvar_cursor
*)cur
;
90 Tcl_DecrRefCount(pCur
->pList1
);
93 Tcl_DecrRefCount(pCur
->pList2
);
100 ** Returns 1 if data is ready, or 0 if not.
102 static int next2(Tcl_Interp
*interp
, tclvar_cursor
*pCur
, Tcl_Obj
*pObj
){
107 p
= Tcl_NewStringObj("array names", -1);
109 Tcl_ListObjAppendElement(0, p
, pObj
);
110 Tcl_EvalObjEx(interp
, p
, TCL_EVAL_GLOBAL
);
112 pCur
->pList2
= Tcl_GetObjResult(interp
);
113 Tcl_IncrRefCount(pCur
->pList2
);
114 assert( pCur
->i2
==0 );
118 Tcl_ListObjLength(0, pCur
->pList2
, &n
);
120 Tcl_DecrRefCount(pCur
->pList2
);
131 static int tclvarNext(sqlite3_vtab_cursor
*cur
){
136 tclvar_cursor
*pCur
= (tclvar_cursor
*)cur
;
137 Tcl_Interp
*interp
= ((tclvar_vtab
*)(cur
->pVtab
))->interp
;
139 Tcl_ListObjLength(0, pCur
->pList1
, &n
);
140 while( !ok
&& pCur
->i1
<n
){
141 Tcl_ListObjIndex(0, pCur
->pList1
, pCur
->i1
, &pObj
);
142 ok
= next2(interp
, pCur
, pObj
);
151 static int tclvarFilter(
152 sqlite3_vtab_cursor
*pVtabCursor
,
153 int idxNum
, const char *idxStr
,
154 int argc
, sqlite3_value
**argv
156 tclvar_cursor
*pCur
= (tclvar_cursor
*)pVtabCursor
;
157 Tcl_Interp
*interp
= ((tclvar_vtab
*)(pVtabCursor
->pVtab
))->interp
;
159 Tcl_Obj
*p
= Tcl_NewStringObj("info vars", -1);
162 assert( argc
==0 || argc
==1 );
164 Tcl_Obj
*pArg
= Tcl_NewStringObj((char*)sqlite3_value_text(argv
[0]), -1);
165 Tcl_ListObjAppendElement(0, p
, pArg
);
167 Tcl_EvalObjEx(interp
, p
, TCL_EVAL_GLOBAL
);
169 Tcl_DecrRefCount(pCur
->pList1
);
172 Tcl_DecrRefCount(pCur
->pList2
);
177 pCur
->pList1
= Tcl_GetObjResult(interp
);
178 Tcl_IncrRefCount(pCur
->pList1
);
179 assert( pCur
->i1
==0 && pCur
->i2
==0 && pCur
->pList2
==0 );
182 return tclvarNext(pVtabCursor
);
185 static int tclvarColumn(sqlite3_vtab_cursor
*cur
, sqlite3_context
*ctx
, int i
){
190 tclvar_cursor
*pCur
= (tclvar_cursor
*)cur
;
191 Tcl_Interp
*interp
= ((tclvar_vtab
*)cur
->pVtab
)->interp
;
193 Tcl_ListObjIndex(interp
, pCur
->pList1
, pCur
->i1
, &p1
);
194 Tcl_ListObjIndex(interp
, pCur
->pList2
, pCur
->i2
, &p2
);
195 z1
= Tcl_GetString(p1
);
197 z2
= Tcl_GetString(p2
);
201 sqlite3_result_text(ctx
, z1
, -1, SQLITE_TRANSIENT
);
205 sqlite3_result_text(ctx
, z2
, -1, SQLITE_TRANSIENT
);
209 Tcl_Obj
*pVal
= Tcl_GetVar2Ex(interp
, z1
, *z2
?z2
:0, TCL_GLOBAL_ONLY
);
210 sqlite3_result_text(ctx
, Tcl_GetString(pVal
), -1, SQLITE_TRANSIENT
);
217 static int tclvarRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
222 static int tclvarEof(sqlite3_vtab_cursor
*cur
){
223 tclvar_cursor
*pCur
= (tclvar_cursor
*)cur
;
224 return (pCur
->pList2
?0:1);
227 static int tclvarBestIndex(sqlite3_vtab
*tab
, sqlite3_index_info
*pIdxInfo
){
230 for(ii
=0; ii
<pIdxInfo
->nConstraint
; ii
++){
231 struct sqlite3_index_constraint
const *pCons
= &pIdxInfo
->aConstraint
[ii
];
232 if( pCons
->iColumn
==0 && pCons
->usable
233 && pCons
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
234 struct sqlite3_index_constraint_usage
*pUsage
;
235 pUsage
= &pIdxInfo
->aConstraintUsage
[ii
];
237 pUsage
->argvIndex
= 1;
242 for(ii
=0; ii
<pIdxInfo
->nConstraint
; ii
++){
243 struct sqlite3_index_constraint
const *pCons
= &pIdxInfo
->aConstraint
[ii
];
244 if( pCons
->iColumn
==0 && pCons
->usable
245 && pCons
->op
==SQLITE_INDEX_CONSTRAINT_MATCH
){
246 struct sqlite3_index_constraint_usage
*pUsage
;
247 pUsage
= &pIdxInfo
->aConstraintUsage
[ii
];
249 pUsage
->argvIndex
= 1;
258 ** A virtual table module that provides read-only access to a
259 ** Tcl global variable namespace.
261 static sqlite3_module tclvarModule
= {
268 tclvarOpen
, /* xOpen - open a cursor */
269 tclvarClose
, /* xClose - close a cursor */
270 tclvarFilter
, /* xFilter - configure scan constraints */
271 tclvarNext
, /* xNext - advance a cursor */
272 tclvarEof
, /* xEof - check for end of scan */
273 tclvarColumn
, /* xColumn - read data */
274 tclvarRowid
, /* xRowid - read data */
285 ** Decode a pointer to an sqlite3 object.
287 extern int getDbPointer(Tcl_Interp
*interp
, const char *zA
, sqlite3
**ppDb
);
290 ** Register the echo virtual table module.
292 static int register_tclvar_module(
293 ClientData clientData
, /* Pointer to sqlite3_enable_XXX function */
294 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
295 int objc
, /* Number of arguments */
296 Tcl_Obj
*CONST objv
[] /* Command arguments */
300 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
303 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
304 #ifndef SQLITE_OMIT_VIRTUALTABLE
305 sqlite3_create_module(db
, "tclvar", &tclvarModule
, (void *)interp
);
314 ** Register commands with the TCL interpreter.
316 int Sqlitetesttclvar_Init(Tcl_Interp
*interp
){
317 #ifndef SQLITE_OMIT_VIRTUALTABLE
320 Tcl_ObjCmdProc
*xProc
;
323 { "register_tclvar_module", register_tclvar_module
, 0 },
326 for(i
=0; i
<sizeof(aObjCmd
)/sizeof(aObjCmd
[0]); i
++){
327 Tcl_CreateObjCommand(interp
, aObjCmd
[i
].zName
,
328 aObjCmd
[i
].xProc
, aObjCmd
[i
].clientData
, 0);