drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2006 June 13 |
| 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. |
| 15 | ** |
| 16 | ** The emphasis of this file is a virtual table that provides |
| 17 | ** access to TCL variables. |
| 18 | ** |
| 19 | ** $Id: test_tclvar.c,v 1.1 2006/06/13 23:51:35 drh Exp $ |
| 20 | */ |
| 21 | #include "sqliteInt.h" |
| 22 | #include "tcl.h" |
| 23 | #include "os.h" |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | typedef struct tclvar_vtab tclvar_vtab; |
| 28 | typedef struct tclvar_cursor tclvar_cursor; |
| 29 | |
| 30 | /* |
| 31 | ** A tclvar virtual-table object |
| 32 | */ |
| 33 | struct tclvar_vtab { |
| 34 | sqlite3_vtab base; |
| 35 | Tcl_Interp *interp; |
| 36 | }; |
| 37 | |
| 38 | /* A tclvar cursor object */ |
| 39 | struct tclvar_cursor { |
| 40 | sqlite3_vtab_cursor base; |
| 41 | Tcl_Obj *pList1, *pList2; |
| 42 | int i, j; |
| 43 | }; |
| 44 | |
| 45 | /* Methods for the tclvar module */ |
| 46 | static int tclvarConnect( |
| 47 | sqlite3 *db, |
| 48 | const sqlite3_module *pModule, |
| 49 | int argc, char **argv, |
| 50 | sqlite3_vtab **ppVtab |
| 51 | ){ |
| 52 | tclvar_vtab *pVtab; |
| 53 | static const char zSchema[] = |
| 54 | "CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)"; |
| 55 | pVtab = sqliteMalloc( sizeof(*pVtab) ); |
| 56 | if( pVtab==0 ) return SQLITE_NOMEM; |
| 57 | *ppVtab = &pVtab->base; |
| 58 | pVtab->base.pModule = pModule; |
| 59 | pVtab->interp = pModule->pAux; |
| 60 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 61 | sqlite3_declare_vtab(db, zSchema); |
| 62 | #endif |
| 63 | return SQLITE_OK; |
| 64 | } |
| 65 | /* Note that for this virtual table, the xCreate and xConnect |
| 66 | ** methods are identical. */ |
| 67 | static int tclvarDisconnect(sqlite3_vtab *pVtab){ |
| 68 | free(pVtab); |
| 69 | } |
| 70 | /* The xDisconnect and xDestroy methods are also the same */ |
| 71 | |
| 72 | static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 73 | tclvar_cursor *pCur; |
| 74 | pCur = sqliteMalloc(sizeof(tclvar_cursor)); |
| 75 | *ppCursor = &pCur->base; |
| 76 | return SQLITE_OK; |
| 77 | } |
| 78 | |
| 79 | static int tclvarClose(sqlite3_vtab_cursor *cur){ |
| 80 | tclvar_cursor *pCur = (tclvar_cursor *)cur; |
| 81 | if( pCur->pList1 ){ |
| 82 | Tcl_DecrRefCount(pCur->pList1); |
| 83 | } |
| 84 | if( pCur->pList2 ){ |
| 85 | Tcl_DecrRefCount(pCur->pList2); |
| 86 | } |
| 87 | sqliteFree(pCur); |
| 88 | return SQLITE_OK; |
| 89 | } |
| 90 | |
| 91 | static int tclvarNext(sqlite3_vtab_cursor *cur){ |
| 92 | tclvar_cursor *pCur = (tclvar_cursor *)cur; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
| 97 | tclvar_cursor *pCur = (tclvar_cursor*)cur; |
| 98 | return SQLITE_OK; |
| 99 | } |
| 100 | |
| 101 | static int tclvarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 102 | tclvar_cursor *pCur = (tclvar_cursor*)cur; |
| 103 | return SQLITE_OK; |
| 104 | } |
| 105 | |
| 106 | static int tclvarFilter( |
| 107 | sqlite3_vtab_cursor *pVtabCursor, |
| 108 | int idxNum, const char *idxStr, |
| 109 | int argc, sqlite3_value **argv |
| 110 | ){ |
| 111 | tclvar_cursor *pCur = (tclvar_cursor *)pVtabCursor; |
| 112 | tclvar_vtab *pVtab = (tclvar_vtab *)pCur->base.pVtab; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | */ |
| 118 | static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 119 | tclvar_vtab *pVtab = (tclvar_vtab *)tab; |
| 120 | return SQLITE_OK; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | ** A virtual table module that merely echos method calls into TCL |
| 125 | ** variables. |
| 126 | */ |
| 127 | static sqlite3_module tclvarModule = { |
| 128 | 0, /* iVersion */ |
| 129 | "tclvar", /* zName */ |
| 130 | 0, /* pAux */ |
| 131 | tclvarConnect, |
| 132 | tclvarConnect, |
| 133 | tclvarBestIndex, |
| 134 | tclvarDisconnect, |
| 135 | tclvarDisconnect, |
| 136 | tclvarOpen, /* xOpen - open a cursor */ |
| 137 | tclvarClose, /* xClose - close a cursor */ |
| 138 | tclvarFilter, /* xFilter - configure scan constraints */ |
| 139 | tclvarNext, /* xNext - advance a cursor */ |
| 140 | tclvarColumn, /* xColumn - read data */ |
| 141 | tclvarRowid /* xRowid - read data */ |
| 142 | }; |
| 143 | |
| 144 | /* |
| 145 | ** Decode a pointer to an sqlite3 object. |
| 146 | */ |
| 147 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 148 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 149 | return TCL_OK; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /* |
| 154 | ** Register the echo virtual table module. |
| 155 | */ |
| 156 | static int register_tclvar_module( |
| 157 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 158 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 159 | int objc, /* Number of arguments */ |
| 160 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 161 | ){ |
| 162 | sqlite3 *db; |
| 163 | if( objc!=2 ){ |
| 164 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 165 | return TCL_ERROR; |
| 166 | } |
| 167 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 168 | tclvarModule.pAux = interp; |
| 169 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 170 | sqlite3_create_module(db, "tclvar", &tclvarModule); |
| 171 | #endif |
| 172 | return TCL_OK; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | ** Register commands with the TCL interpreter. |
| 178 | */ |
| 179 | int Sqlitetesttclvar_Init(Tcl_Interp *interp){ |
| 180 | static struct { |
| 181 | char *zName; |
| 182 | Tcl_ObjCmdProc *xProc; |
| 183 | void *clientData; |
| 184 | } aObjCmd[] = { |
| 185 | { "register_tclvar_module", register_tclvar_module, 0 }, |
| 186 | }; |
| 187 | int i; |
| 188 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 189 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 190 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 191 | } |
| 192 | return TCL_OK; |
| 193 | } |