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 | ** |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 16 | ** $Id: test8.c,v 1.6 2006/06/13 01:04:53 drh 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 | */ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 28 | static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 29 | int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 30 | Tcl_SetVar(interp, "echo_module", zArg, flags); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 31 | } |
| 32 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 33 | static void appendToEchoTable(Tcl_Interp *interp, const char *zArg){ |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 34 | int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 35 | Tcl_SetVar(interp, "echo_module", zArg, flags); |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 36 | } |
| 37 | |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 38 | /* |
| 39 | ** This function is called from within the echo-modules xCreate and |
| 40 | ** xConnect methods. The argc and argv arguments are copies of those |
| 41 | ** passed to the calling method. This function is responsible for |
| 42 | ** calling sqlite3_declare_vtab() to declare the schema of the virtual |
| 43 | ** table being created or connected. |
| 44 | ** |
| 45 | ** If the constructor was passed just one argument, i.e.: |
| 46 | ** |
| 47 | ** CREATE TABLE t1 AS echo(t2); |
| 48 | ** |
| 49 | ** Then t2 is assumed to be the name of a *real* database table. The |
| 50 | ** schema of the virtual table is declared by passing a copy of the |
| 51 | ** CREATE TABLE statement for the real table to sqlite3_declare_vtab(). |
| 52 | ** Hence, the virtual table should have exactly the same column names and |
| 53 | ** types as the real table. |
| 54 | */ |
| 55 | static int echoDeclareVtab(sqlite3 *db, int argc, char **argv){ |
| 56 | int rc = SQLITE_OK; |
| 57 | |
| 58 | if( argc==2 ){ |
| 59 | sqlite3_stmt *pStmt = 0; |
| 60 | sqlite3_prepare(db, |
| 61 | "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", |
| 62 | -1, &pStmt, 0); |
| 63 | sqlite3_bind_text(pStmt, 1, argv[1], -1, 0); |
| 64 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 65 | const char *zCreateTable = sqlite3_column_text(pStmt, 0); |
drh | a75803d | 2006-06-12 12:50:23 +0000 | [diff] [blame] | 66 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 67 | sqlite3_declare_vtab(db, zCreateTable); |
drh | a75803d | 2006-06-12 12:50:23 +0000 | [diff] [blame] | 68 | #endif |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 69 | } else { |
| 70 | rc = SQLITE_ERROR; |
| 71 | } |
| 72 | sqlite3_finalize(pStmt); |
| 73 | } |
| 74 | |
| 75 | return rc; |
| 76 | } |
| 77 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 78 | /* An echo vtab object */ |
| 79 | typedef struct echo_vtab echo_vtab; |
| 80 | struct echo_vtab { |
| 81 | sqlite3_vtab base; |
| 82 | Tcl_Interp *interp; |
| 83 | }; |
| 84 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 85 | /* Methods for the echo module */ |
| 86 | static int echoCreate( |
| 87 | sqlite3 *db, |
| 88 | const sqlite3_module *pModule, |
| 89 | int argc, char **argv, |
| 90 | sqlite3_vtab **ppVtab |
| 91 | ){ |
| 92 | int i; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 93 | echo_vtab *pVtab; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 94 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 95 | pVtab = sqliteMalloc( sizeof(*pVtab) ); |
| 96 | *ppVtab = &pVtab->base; |
| 97 | pVtab->base.pModule = pModule; |
| 98 | pVtab->interp = pModule->pAux; |
| 99 | appendToEchoModule(pVtab->interp, "xCreate"); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 100 | for(i=0; i<argc; i++){ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 101 | appendToEchoModule(pVtab->interp, argv[i]); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 102 | } |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 103 | |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 104 | echoDeclareVtab(db, argc, argv); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | static int echoConnect( |
| 108 | sqlite3 *db, |
| 109 | const sqlite3_module *pModule, |
| 110 | int argc, char **argv, |
| 111 | sqlite3_vtab **ppVtab |
| 112 | ){ |
| 113 | int i; |
| 114 | Tcl_Interp *interp = pModule->pAux; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 115 | echo_vtab *pVtab; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 116 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 117 | pVtab = sqliteMalloc( sizeof(*pVtab) ); |
| 118 | *ppVtab = &pVtab->base; |
| 119 | pVtab->base.pModule = pModule; |
| 120 | pVtab->interp = pModule->pAux; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 121 | Tcl_SetVar(interp, "echo_module", "xConnect", TCL_GLOBAL_ONLY); |
| 122 | for(i=0; i<argc; i++){ |
| 123 | Tcl_SetVar(interp, "echo_module", argv[i], |
| 124 | TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
| 125 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 126 | |
| 127 | echoDeclareVtab(db, argc, argv); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | static int echoDisconnect(sqlite3_vtab *pVtab){ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 131 | echo_vtab *p = (echo_vtab*)pVtab; |
| 132 | appendToEchoTable(p->interp, "xDisconnect"); |
| 133 | sqliteFree(pVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | static int echoDestroy(sqlite3_vtab *pVtab){ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 137 | echo_vtab *p = (echo_vtab*)pVtab; |
| 138 | Tcl_SetVar(p->interp, "echo_module", "xDestroy", |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 139 | TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 144 | ** The xBestIndex method for the echo module always returns |
| 145 | ** an index of 123. |
| 146 | */ |
| 147 | static int echoBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){ |
| 148 | pIdxInfo->idxNum = 123; |
| 149 | return SQLITE_OK; |
| 150 | } |
| 151 | |
| 152 | /* |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 153 | ** A virtual table module that merely echos method calls into TCL |
| 154 | ** variables. |
| 155 | */ |
| 156 | static sqlite3_module echoModule = { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 157 | 0, /* iVersion */ |
| 158 | "echo", /* zName */ |
| 159 | 0, /* pAux */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 160 | echoCreate, |
| 161 | echoConnect, |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame^] | 162 | echoBestIndex, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 163 | echoDisconnect, |
| 164 | echoDestroy, |
| 165 | }; |
| 166 | |
| 167 | /* |
| 168 | ** Decode a pointer to an sqlite3 object. |
| 169 | */ |
| 170 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 171 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 172 | return TCL_OK; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | ** Register the echo virtual table module. |
| 178 | */ |
| 179 | static int register_echo_module( |
| 180 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 181 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 182 | int objc, /* Number of arguments */ |
| 183 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 184 | ){ |
| 185 | sqlite3 *db; |
| 186 | if( objc!=2 ){ |
| 187 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 188 | return TCL_ERROR; |
| 189 | } |
| 190 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 191 | echoModule.pAux = interp; |
| 192 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 193 | sqlite3_create_module(db, "echo", &echoModule); |
| 194 | #endif |
| 195 | return TCL_OK; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* |
| 200 | ** Register commands with the TCL interpreter. |
| 201 | */ |
| 202 | int Sqlitetest8_Init(Tcl_Interp *interp){ |
| 203 | static struct { |
| 204 | char *zName; |
| 205 | Tcl_ObjCmdProc *xProc; |
| 206 | void *clientData; |
| 207 | } aObjCmd[] = { |
| 208 | { "register_echo_module", register_echo_module, 0 }, |
| 209 | }; |
| 210 | int i; |
| 211 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 212 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 213 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 214 | } |
| 215 | return TCL_OK; |
| 216 | } |