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