Update contrib.
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 ** $Id: test_tclvar.c,v 1.17 2008/08/12 14:48:41 danielk1977 Exp $
21 #include "sqliteInt.h"
26 #ifndef SQLITE_OMIT_VIRTUALTABLE
28 typedef struct tclvar_vtab tclvar_vtab;
29 typedef struct tclvar_cursor tclvar_cursor;
32 ** A tclvar virtual-table object
39 /* A tclvar cursor object */
40 struct tclvar_cursor {
41 sqlite3_vtab_cursor base;
43 Tcl_Obj *pList1; /* Result of [info vars ?pattern?] */
44 Tcl_Obj *pList2; /* Result of [array names [lindex $pList1 $i1]] */
45 int i1; /* Current item in pList1 */
46 int i2; /* Current item (if any) in pList2 */
49 /* Methods for the tclvar module */
50 static int tclvarConnect(
53 int argc, const char *const*argv,
54 sqlite3_vtab **ppVtab,
58 static const char zSchema[] =
59 "CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)";
60 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
61 if( pVtab==0 ) return SQLITE_NOMEM;
62 *ppVtab = &pVtab->base;
63 pVtab->interp = (Tcl_Interp *)pAux;
64 sqlite3_declare_vtab(db, zSchema);
67 /* Note that for this virtual table, the xCreate and xConnect
68 ** methods are identical. */
70 static int tclvarDisconnect(sqlite3_vtab *pVtab){
74 /* The xDisconnect and xDestroy methods are also the same */
77 ** Open a new tclvar cursor.
79 static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
81 pCur = sqlite3MallocZero(sizeof(tclvar_cursor));
82 *ppCursor = &pCur->base;
87 ** Close a tclvar cursor.
89 static int tclvarClose(sqlite3_vtab_cursor *cur){
90 tclvar_cursor *pCur = (tclvar_cursor *)cur;
92 Tcl_DecrRefCount(pCur->pList1);
95 Tcl_DecrRefCount(pCur->pList2);
102 ** Returns 1 if data is ready, or 0 if not.
104 static int next2(Tcl_Interp *interp, tclvar_cursor *pCur, Tcl_Obj *pObj){
109 p = Tcl_NewStringObj("array names", -1);
111 Tcl_ListObjAppendElement(0, p, pObj);
112 Tcl_EvalObjEx(interp, p, TCL_EVAL_GLOBAL);
114 pCur->pList2 = Tcl_GetObjResult(interp);
115 Tcl_IncrRefCount(pCur->pList2);
116 assert( pCur->i2==0 );
120 Tcl_ListObjLength(0, pCur->pList2, &n);
122 Tcl_DecrRefCount(pCur->pList2);
133 static int tclvarNext(sqlite3_vtab_cursor *cur){
138 tclvar_cursor *pCur = (tclvar_cursor *)cur;
139 Tcl_Interp *interp = ((tclvar_vtab *)(cur->pVtab))->interp;
141 Tcl_ListObjLength(0, pCur->pList1, &n);
142 while( !ok && pCur->i1<n ){
143 Tcl_ListObjIndex(0, pCur->pList1, pCur->i1, &pObj);
144 ok = next2(interp, pCur, pObj);
153 static int tclvarFilter(
154 sqlite3_vtab_cursor *pVtabCursor,
155 int idxNum, const char *idxStr,
156 int argc, sqlite3_value **argv
158 tclvar_cursor *pCur = (tclvar_cursor *)pVtabCursor;
159 Tcl_Interp *interp = ((tclvar_vtab *)(pVtabCursor->pVtab))->interp;
161 Tcl_Obj *p = Tcl_NewStringObj("info vars", -1);
164 assert( argc==0 || argc==1 );
166 Tcl_Obj *pArg = Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1);
167 Tcl_ListObjAppendElement(0, p, pArg);
169 Tcl_EvalObjEx(interp, p, TCL_EVAL_GLOBAL);
170 pCur->pList1 = Tcl_GetObjResult(interp);
171 Tcl_IncrRefCount(pCur->pList1);
172 assert( pCur->i1==0 && pCur->i2==0 && pCur->pList2==0 );
175 return tclvarNext(pVtabCursor);
178 static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
183 tclvar_cursor *pCur = (tclvar_cursor*)cur;
184 Tcl_Interp *interp = ((tclvar_vtab *)cur->pVtab)->interp;
186 Tcl_ListObjIndex(interp, pCur->pList1, pCur->i1, &p1);
187 Tcl_ListObjIndex(interp, pCur->pList2, pCur->i2, &p2);
188 z1 = Tcl_GetString(p1);
190 z2 = Tcl_GetString(p2);
194 sqlite3_result_text(ctx, z1, -1, SQLITE_TRANSIENT);
198 sqlite3_result_text(ctx, z2, -1, SQLITE_TRANSIENT);
202 Tcl_Obj *pVal = Tcl_GetVar2Ex(interp, z1, *z2?z2:0, TCL_GLOBAL_ONLY);
203 sqlite3_result_text(ctx, Tcl_GetString(pVal), -1, SQLITE_TRANSIENT);
210 static int tclvarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
215 static int tclvarEof(sqlite3_vtab_cursor *cur){
216 tclvar_cursor *pCur = (tclvar_cursor*)cur;
217 return (pCur->pList2?0:1);
220 static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
223 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
224 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
225 if( pCons->iColumn==0 && pCons->usable
226 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
227 struct sqlite3_index_constraint_usage *pUsage;
228 pUsage = &pIdxInfo->aConstraintUsage[ii];
230 pUsage->argvIndex = 1;
235 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
236 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
237 if( pCons->iColumn==0 && pCons->usable
238 && pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
239 struct sqlite3_index_constraint_usage *pUsage;
240 pUsage = &pIdxInfo->aConstraintUsage[ii];
242 pUsage->argvIndex = 1;
251 ** A virtual table module that provides read-only access to a
252 ** Tcl global variable namespace.
254 static sqlite3_module tclvarModule = {
261 tclvarOpen, /* xOpen - open a cursor */
262 tclvarClose, /* xClose - close a cursor */
263 tclvarFilter, /* xFilter - configure scan constraints */
264 tclvarNext, /* xNext - advance a cursor */
265 tclvarEof, /* xEof - check for end of scan */
266 tclvarColumn, /* xColumn - read data */
267 tclvarRowid, /* xRowid - read data */
278 ** Decode a pointer to an sqlite3 object.
280 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
283 ** Register the echo virtual table module.
285 static int register_tclvar_module(
286 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
287 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
288 int objc, /* Number of arguments */
289 Tcl_Obj *CONST objv[] /* Command arguments */
293 Tcl_WrongNumArgs(interp, 1, objv, "DB");
296 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
297 #ifndef SQLITE_OMIT_VIRTUALTABLE
298 sqlite3_create_module(db, "tclvar", &tclvarModule, (void *)interp);
307 ** Register commands with the TCL interpreter.
309 int Sqlitetesttclvar_Init(Tcl_Interp *interp){
310 #ifndef SQLITE_OMIT_VIRTUALTABLE
313 Tcl_ObjCmdProc *xProc;
316 { "register_tclvar_module", register_tclvar_module, 0 },
319 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
320 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
321 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);