drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame^] | 1 | /* |
| 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. |
| 15 | ** |
| 16 | ** $Id: test8.c,v 1.1 2006/06/11 23:41:56 drh Exp $ |
| 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "tcl.h" |
| 20 | #include "os.h" |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | /* Methods for the echo module */ |
| 25 | static int echoCreate( |
| 26 | sqlite3 *db, |
| 27 | const sqlite3_module *pModule, |
| 28 | int argc, char **argv, |
| 29 | sqlite3_vtab **ppVtab |
| 30 | ){ |
| 31 | int i; |
| 32 | Tcl_Interp *interp = pModule->pAux; |
| 33 | *ppVtab = pModule->pAux; |
| 34 | |
| 35 | Tcl_SetVar(interp, "echo_module", "xCreate", TCL_GLOBAL_ONLY); |
| 36 | for(i=0; i<argc; i++){ |
| 37 | Tcl_SetVar(interp, "echo_module", argv[i], |
| 38 | TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | static int echoConnect( |
| 43 | sqlite3 *db, |
| 44 | const sqlite3_module *pModule, |
| 45 | int argc, char **argv, |
| 46 | sqlite3_vtab **ppVtab |
| 47 | ){ |
| 48 | int i; |
| 49 | Tcl_Interp *interp = pModule->pAux; |
| 50 | *ppVtab = pModule->pAux; |
| 51 | |
| 52 | Tcl_SetVar(interp, "echo_module", "xConnect", TCL_GLOBAL_ONLY); |
| 53 | for(i=0; i<argc; i++){ |
| 54 | Tcl_SetVar(interp, "echo_module", argv[i], |
| 55 | TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | static int echoDisconnect(sqlite3_vtab *pVtab){ |
| 60 | Tcl_Interp *interp = (Tcl_Interp*)pVtab; |
| 61 | Tcl_SetVar(interp, "echo_module", "xDisconnect", |
| 62 | TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
| 63 | return 0; |
| 64 | } |
| 65 | static int echoDestroy(sqlite3_vtab *pVtab){ |
| 66 | Tcl_Interp *interp = (Tcl_Interp*)pVtab; |
| 67 | Tcl_SetVar(interp, "echo_module", "xDestroy", |
| 68 | TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | ** A virtual table module that merely echos method calls into TCL |
| 74 | ** variables. |
| 75 | */ |
| 76 | static sqlite3_module echoModule = { |
| 77 | 0, |
| 78 | "echo", |
| 79 | 0, |
| 80 | echoCreate, |
| 81 | echoConnect, |
| 82 | 0, |
| 83 | echoDisconnect, |
| 84 | echoDestroy, |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | ** Decode a pointer to an sqlite3 object. |
| 89 | */ |
| 90 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 91 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 92 | return TCL_OK; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* |
| 97 | ** Register the echo virtual table module. |
| 98 | */ |
| 99 | static int register_echo_module( |
| 100 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 101 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 102 | int objc, /* Number of arguments */ |
| 103 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 104 | ){ |
| 105 | sqlite3 *db; |
| 106 | if( objc!=2 ){ |
| 107 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 108 | return TCL_ERROR; |
| 109 | } |
| 110 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 111 | echoModule.pAux = interp; |
| 112 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 113 | sqlite3_create_module(db, "echo", &echoModule); |
| 114 | #endif |
| 115 | return TCL_OK; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /* |
| 120 | ** Register commands with the TCL interpreter. |
| 121 | */ |
| 122 | int Sqlitetest8_Init(Tcl_Interp *interp){ |
| 123 | static struct { |
| 124 | char *zName; |
| 125 | Tcl_ObjCmdProc *xProc; |
| 126 | void *clientData; |
| 127 | } aObjCmd[] = { |
| 128 | { "register_echo_module", register_echo_module, 0 }, |
| 129 | }; |
| 130 | int i; |
| 131 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 132 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 133 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 134 | } |
| 135 | return TCL_OK; |
| 136 | } |