sl@0: /* sl@0: ** 2006 June 13 sl@0: ** sl@0: ** The author disclaims copyright to this source code. In place of sl@0: ** a legal notice, here is a blessing: sl@0: ** sl@0: ** May you do good and not evil. sl@0: ** May you find forgiveness for yourself and forgive others. sl@0: ** May you share freely, never taking more than you give. sl@0: ** sl@0: ************************************************************************* sl@0: ** Code for testing the virtual table interfaces. This code sl@0: ** is not included in the SQLite library. It is used for automated sl@0: ** testing of the SQLite library. sl@0: ** sl@0: ** The emphasis of this file is a virtual table that provides sl@0: ** access to TCL variables. sl@0: ** sl@0: ** $Id: test_tclvar.c,v 1.17 2008/08/12 14:48:41 danielk1977 Exp $ sl@0: */ sl@0: #include "sqliteInt.h" sl@0: #include "tcl.h" sl@0: #include sl@0: #include sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: sl@0: typedef struct tclvar_vtab tclvar_vtab; sl@0: typedef struct tclvar_cursor tclvar_cursor; sl@0: sl@0: /* sl@0: ** A tclvar virtual-table object sl@0: */ sl@0: struct tclvar_vtab { sl@0: sqlite3_vtab base; sl@0: Tcl_Interp *interp; sl@0: }; sl@0: sl@0: /* A tclvar cursor object */ sl@0: struct tclvar_cursor { sl@0: sqlite3_vtab_cursor base; sl@0: sl@0: Tcl_Obj *pList1; /* Result of [info vars ?pattern?] */ sl@0: Tcl_Obj *pList2; /* Result of [array names [lindex $pList1 $i1]] */ sl@0: int i1; /* Current item in pList1 */ sl@0: int i2; /* Current item (if any) in pList2 */ sl@0: }; sl@0: sl@0: /* Methods for the tclvar module */ sl@0: static int tclvarConnect( sl@0: sqlite3 *db, sl@0: void *pAux, sl@0: int argc, const char *const*argv, sl@0: sqlite3_vtab **ppVtab, sl@0: char **pzErr sl@0: ){ sl@0: tclvar_vtab *pVtab; sl@0: static const char zSchema[] = sl@0: "CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)"; sl@0: pVtab = sqlite3MallocZero( sizeof(*pVtab) ); sl@0: if( pVtab==0 ) return SQLITE_NOMEM; sl@0: *ppVtab = &pVtab->base; sl@0: pVtab->interp = (Tcl_Interp *)pAux; sl@0: sqlite3_declare_vtab(db, zSchema); sl@0: return SQLITE_OK; sl@0: } sl@0: /* Note that for this virtual table, the xCreate and xConnect sl@0: ** methods are identical. */ sl@0: sl@0: static int tclvarDisconnect(sqlite3_vtab *pVtab){ sl@0: sqlite3_free(pVtab); sl@0: return SQLITE_OK; sl@0: } sl@0: /* The xDisconnect and xDestroy methods are also the same */ sl@0: sl@0: /* sl@0: ** Open a new tclvar cursor. sl@0: */ sl@0: static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ sl@0: tclvar_cursor *pCur; sl@0: pCur = sqlite3MallocZero(sizeof(tclvar_cursor)); sl@0: *ppCursor = &pCur->base; sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** Close a tclvar cursor. sl@0: */ sl@0: static int tclvarClose(sqlite3_vtab_cursor *cur){ sl@0: tclvar_cursor *pCur = (tclvar_cursor *)cur; sl@0: if( pCur->pList1 ){ sl@0: Tcl_DecrRefCount(pCur->pList1); sl@0: } sl@0: if( pCur->pList2 ){ sl@0: Tcl_DecrRefCount(pCur->pList2); sl@0: } sl@0: sqlite3_free(pCur); sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** Returns 1 if data is ready, or 0 if not. sl@0: */ sl@0: static int next2(Tcl_Interp *interp, tclvar_cursor *pCur, Tcl_Obj *pObj){ sl@0: Tcl_Obj *p; sl@0: sl@0: if( pObj ){ sl@0: if( !pCur->pList2 ){ sl@0: p = Tcl_NewStringObj("array names", -1); sl@0: Tcl_IncrRefCount(p); sl@0: Tcl_ListObjAppendElement(0, p, pObj); sl@0: Tcl_EvalObjEx(interp, p, TCL_EVAL_GLOBAL); sl@0: Tcl_DecrRefCount(p); sl@0: pCur->pList2 = Tcl_GetObjResult(interp); sl@0: Tcl_IncrRefCount(pCur->pList2); sl@0: assert( pCur->i2==0 ); sl@0: }else{ sl@0: int n = 0; sl@0: pCur->i2++; sl@0: Tcl_ListObjLength(0, pCur->pList2, &n); sl@0: if( pCur->i2>=n ){ sl@0: Tcl_DecrRefCount(pCur->pList2); sl@0: pCur->pList2 = 0; sl@0: pCur->i2 = 0; sl@0: return 0; sl@0: } sl@0: } sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int tclvarNext(sqlite3_vtab_cursor *cur){ sl@0: Tcl_Obj *pObj; sl@0: int n = 0; sl@0: int ok = 0; sl@0: sl@0: tclvar_cursor *pCur = (tclvar_cursor *)cur; sl@0: Tcl_Interp *interp = ((tclvar_vtab *)(cur->pVtab))->interp; sl@0: sl@0: Tcl_ListObjLength(0, pCur->pList1, &n); sl@0: while( !ok && pCur->i1pList1, pCur->i1, &pObj); sl@0: ok = next2(interp, pCur, pObj); sl@0: if( !ok ){ sl@0: pCur->i1++; sl@0: } sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: static int tclvarFilter( sl@0: sqlite3_vtab_cursor *pVtabCursor, sl@0: int idxNum, const char *idxStr, sl@0: int argc, sqlite3_value **argv sl@0: ){ sl@0: tclvar_cursor *pCur = (tclvar_cursor *)pVtabCursor; sl@0: Tcl_Interp *interp = ((tclvar_vtab *)(pVtabCursor->pVtab))->interp; sl@0: sl@0: Tcl_Obj *p = Tcl_NewStringObj("info vars", -1); sl@0: Tcl_IncrRefCount(p); sl@0: sl@0: assert( argc==0 || argc==1 ); sl@0: if( argc==1 ){ sl@0: Tcl_Obj *pArg = Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1); sl@0: Tcl_ListObjAppendElement(0, p, pArg); sl@0: } sl@0: Tcl_EvalObjEx(interp, p, TCL_EVAL_GLOBAL); sl@0: pCur->pList1 = Tcl_GetObjResult(interp); sl@0: Tcl_IncrRefCount(pCur->pList1); sl@0: assert( pCur->i1==0 && pCur->i2==0 && pCur->pList2==0 ); sl@0: sl@0: Tcl_DecrRefCount(p); sl@0: return tclvarNext(pVtabCursor); sl@0: } sl@0: sl@0: static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ sl@0: Tcl_Obj *p1; sl@0: Tcl_Obj *p2; sl@0: const char *z1; sl@0: const char *z2 = ""; sl@0: tclvar_cursor *pCur = (tclvar_cursor*)cur; sl@0: Tcl_Interp *interp = ((tclvar_vtab *)cur->pVtab)->interp; sl@0: sl@0: Tcl_ListObjIndex(interp, pCur->pList1, pCur->i1, &p1); sl@0: Tcl_ListObjIndex(interp, pCur->pList2, pCur->i2, &p2); sl@0: z1 = Tcl_GetString(p1); sl@0: if( p2 ){ sl@0: z2 = Tcl_GetString(p2); sl@0: } sl@0: switch (i) { sl@0: case 0: { sl@0: sqlite3_result_text(ctx, z1, -1, SQLITE_TRANSIENT); sl@0: break; sl@0: } sl@0: case 1: { sl@0: sqlite3_result_text(ctx, z2, -1, SQLITE_TRANSIENT); sl@0: break; sl@0: } sl@0: case 2: { sl@0: Tcl_Obj *pVal = Tcl_GetVar2Ex(interp, z1, *z2?z2:0, TCL_GLOBAL_ONLY); sl@0: sqlite3_result_text(ctx, Tcl_GetString(pVal), -1, SQLITE_TRANSIENT); sl@0: break; sl@0: } sl@0: } sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: static int tclvarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ sl@0: *pRowid = 0; sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: static int tclvarEof(sqlite3_vtab_cursor *cur){ sl@0: tclvar_cursor *pCur = (tclvar_cursor*)cur; sl@0: return (pCur->pList2?0:1); sl@0: } sl@0: sl@0: static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ sl@0: int ii; sl@0: sl@0: for(ii=0; iinConstraint; ii++){ sl@0: struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii]; sl@0: if( pCons->iColumn==0 && pCons->usable sl@0: && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ sl@0: struct sqlite3_index_constraint_usage *pUsage; sl@0: pUsage = &pIdxInfo->aConstraintUsage[ii]; sl@0: pUsage->omit = 0; sl@0: pUsage->argvIndex = 1; sl@0: return SQLITE_OK; sl@0: } sl@0: } sl@0: sl@0: for(ii=0; iinConstraint; ii++){ sl@0: struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii]; sl@0: if( pCons->iColumn==0 && pCons->usable sl@0: && pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){ sl@0: struct sqlite3_index_constraint_usage *pUsage; sl@0: pUsage = &pIdxInfo->aConstraintUsage[ii]; sl@0: pUsage->omit = 1; sl@0: pUsage->argvIndex = 1; sl@0: return SQLITE_OK; sl@0: } sl@0: } sl@0: sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** A virtual table module that provides read-only access to a sl@0: ** Tcl global variable namespace. sl@0: */ sl@0: static sqlite3_module tclvarModule = { sl@0: 0, /* iVersion */ sl@0: tclvarConnect, sl@0: tclvarConnect, sl@0: tclvarBestIndex, sl@0: tclvarDisconnect, sl@0: tclvarDisconnect, sl@0: tclvarOpen, /* xOpen - open a cursor */ sl@0: tclvarClose, /* xClose - close a cursor */ sl@0: tclvarFilter, /* xFilter - configure scan constraints */ sl@0: tclvarNext, /* xNext - advance a cursor */ sl@0: tclvarEof, /* xEof - check for end of scan */ sl@0: tclvarColumn, /* xColumn - read data */ sl@0: tclvarRowid, /* xRowid - read data */ sl@0: 0, /* xUpdate */ sl@0: 0, /* xBegin */ sl@0: 0, /* xSync */ sl@0: 0, /* xCommit */ sl@0: 0, /* xRollback */ sl@0: 0, /* xFindMethod */ sl@0: 0, /* xRename */ sl@0: }; sl@0: sl@0: /* sl@0: ** Decode a pointer to an sqlite3 object. sl@0: */ sl@0: extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); sl@0: sl@0: /* sl@0: ** Register the echo virtual table module. sl@0: */ sl@0: static int register_tclvar_module( sl@0: ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ sl@0: Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ sl@0: int objc, /* Number of arguments */ sl@0: Tcl_Obj *CONST objv[] /* Command arguments */ sl@0: ){ sl@0: sqlite3 *db; sl@0: if( objc!=2 ){ sl@0: Tcl_WrongNumArgs(interp, 1, objv, "DB"); sl@0: return TCL_ERROR; sl@0: } sl@0: if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: sqlite3_create_module(db, "tclvar", &tclvarModule, (void *)interp); sl@0: #endif sl@0: return TCL_OK; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: sl@0: /* sl@0: ** Register commands with the TCL interpreter. sl@0: */ sl@0: int Sqlitetesttclvar_Init(Tcl_Interp *interp){ sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: static struct { sl@0: char *zName; sl@0: Tcl_ObjCmdProc *xProc; sl@0: void *clientData; sl@0: } aObjCmd[] = { sl@0: { "register_tclvar_module", register_tclvar_module, 0 }, sl@0: }; sl@0: int i; sl@0: for(i=0; i