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 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 16 | ** $Id: test8.c,v 1.35 2006/06/24 06:36:11 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 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 24 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 25 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 26 | typedef struct echo_vtab echo_vtab; |
| 27 | typedef struct echo_cursor echo_cursor; |
| 28 | |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 29 | /* |
| 30 | ** The test module defined in this file uses two global Tcl variables to |
| 31 | ** commicate with test-scripts: |
| 32 | ** |
| 33 | ** $::echo_module |
| 34 | ** $::echo_module_sync_fail |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 35 | ** |
| 36 | ** The variable ::echo_module is a list. Each time one of the following |
| 37 | ** methods is called, one or more elements are appended to the list. |
| 38 | ** This is used for automated testing of virtual table modules. |
| 39 | ** |
| 40 | ** The ::echo_module_sync_fail variable is set by test scripts and read |
| 41 | ** by code in this file. If it is set to the name of a real table in the |
| 42 | ** the database, then all xSync operations on echo virtual tables that |
| 43 | ** use the named table as a backing store will fail. |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 44 | */ |
| 45 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 46 | /* |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 47 | ** An echo virtual-table object. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 48 | ** |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 49 | ** echo.vtab.aIndex is an array of booleans. The nth entry is true if |
| 50 | ** the nth column of the real table is the left-most column of an index |
| 51 | ** (implicit or otherwise). In other words, if SQLite can optimize |
| 52 | ** a query like "SELECT * FROM real_table WHERE col = ?". |
| 53 | ** |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 54 | ** Member variable aCol[] contains copies of the column names of the real |
| 55 | ** table. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 56 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 57 | struct echo_vtab { |
| 58 | sqlite3_vtab base; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 59 | Tcl_Interp *interp; /* Tcl interpreter containing debug variables */ |
| 60 | sqlite3 *db; /* Database connection */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 61 | |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 62 | char *zTableName; /* Name of the real table */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 63 | char *zLogName; /* Name of the log table */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 64 | int nCol; /* Number of columns in the real table */ |
| 65 | int *aIndex; /* Array of size nCol. True if column has an index */ |
| 66 | char **aCol; /* Array of size nCol. Column names */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /* An echo cursor object */ |
| 70 | struct echo_cursor { |
| 71 | sqlite3_vtab_cursor base; |
| 72 | sqlite3_stmt *pStmt; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 75 | /* |
| 76 | ** Retrieve the column names for the table named zTab via database |
| 77 | ** connection db. SQLITE_OK is returned on success, or an sqlite error |
| 78 | ** code otherwise. |
| 79 | ** |
| 80 | ** If successful, the number of columns is written to *pnCol. *paCol is |
| 81 | ** set to point at sqliteMalloc()'d space containing the array of |
| 82 | ** nCol column names. The caller is responsible for calling sqliteFree |
| 83 | ** on *paCol. |
| 84 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 85 | static int getColumnNames( |
| 86 | sqlite3 *db, |
| 87 | const char *zTab, |
| 88 | char ***paCol, |
| 89 | int *pnCol |
| 90 | ){ |
| 91 | char **aCol = 0; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 92 | char *zSql; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 93 | sqlite3_stmt *pStmt = 0; |
| 94 | int rc = SQLITE_OK; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 95 | int nCol = 0; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 96 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 97 | /* Prepare the statement "SELECT * FROM <tbl>". The column names |
| 98 | ** of the result set of the compiled SELECT will be the same as |
| 99 | ** the column names of table <tbl>. |
| 100 | */ |
| 101 | zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab); |
| 102 | if( !zSql ){ |
| 103 | rc = SQLITE_NOMEM; |
| 104 | goto out; |
| 105 | } |
| 106 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 107 | sqliteFree(zSql); |
| 108 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 109 | if( rc==SQLITE_OK ){ |
| 110 | int ii; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 111 | int nBytes; |
| 112 | char *zSpace; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 113 | nCol = sqlite3_column_count(pStmt); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 114 | |
| 115 | /* Figure out how much space to allocate for the array of column names |
| 116 | ** (including space for the strings themselves). Then allocate it. |
| 117 | */ |
| 118 | nBytes = sizeof(char *) * nCol; |
| 119 | for(ii=0; ii<nCol; ii++){ |
| 120 | nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1); |
| 121 | } |
| 122 | aCol = (char **)sqliteMalloc(nBytes); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 123 | if( !aCol ){ |
| 124 | rc = SQLITE_NOMEM; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 125 | goto out; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 126 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 127 | |
| 128 | /* Copy the column names into the allocated space and set up the |
| 129 | ** pointers in the aCol[] array. |
| 130 | */ |
| 131 | zSpace = (char *)(&aCol[nCol]); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 132 | for(ii=0; ii<nCol; ii++){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 133 | aCol[ii] = zSpace; |
| 134 | zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii)); |
| 135 | zSpace++; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 136 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 137 | assert( (zSpace-nBytes)==(char *)aCol ); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | *paCol = aCol; |
| 141 | *pnCol = nCol; |
| 142 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 143 | out: |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 144 | sqlite3_finalize(pStmt); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 145 | return rc; |
| 146 | } |
| 147 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 148 | /* |
| 149 | ** Parameter zTab is the name of a table in database db with nCol |
| 150 | ** columns. This function allocates an array of integers nCol in |
| 151 | ** size and populates it according to any implicit or explicit |
| 152 | ** indices on table zTab. |
| 153 | ** |
| 154 | ** If successful, SQLITE_OK is returned and *paIndex set to point |
| 155 | ** at the allocated array. Otherwise, an error code is returned. |
| 156 | ** |
| 157 | ** See comments associated with the member variable aIndex above |
| 158 | ** "struct echo_vtab" for details of the contents of the array. |
| 159 | */ |
| 160 | static int getIndexArray( |
| 161 | sqlite3 *db, /* Database connection */ |
| 162 | const char *zTab, /* Name of table in database db */ |
| 163 | int nCol, |
| 164 | int **paIndex |
| 165 | ){ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 166 | sqlite3_stmt *pStmt = 0; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 167 | int *aIndex = 0; |
| 168 | int rc; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 169 | char *zSql; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 170 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 171 | /* Allocate space for the index array */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 172 | aIndex = (int *)sqliteMalloc(sizeof(int) * nCol); |
| 173 | if( !aIndex ){ |
| 174 | rc = SQLITE_NOMEM; |
| 175 | goto get_index_array_out; |
| 176 | } |
| 177 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 178 | /* Compile an sqlite pragma to loop through all indices on table zTab */ |
| 179 | zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab); |
| 180 | if( !zSql ){ |
| 181 | rc = SQLITE_NOMEM; |
| 182 | goto get_index_array_out; |
| 183 | } |
| 184 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 185 | sqliteFree(zSql); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 186 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 187 | /* For each index, figure out the left-most column and set the |
| 188 | ** corresponding entry in aIndex[] to 1. |
| 189 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 190 | while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 191 | const char *zIdx = sqlite3_column_text(pStmt, 1); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 192 | sqlite3_stmt *pStmt2 = 0; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 193 | zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx); |
| 194 | if( !zSql ){ |
| 195 | rc = SQLITE_NOMEM; |
| 196 | goto get_index_array_out; |
| 197 | } |
| 198 | rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0); |
| 199 | sqliteFree(zSql); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 200 | if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){ |
| 201 | int cid = sqlite3_column_int(pStmt2, 1); |
| 202 | assert( cid>=0 && cid<nCol ); |
| 203 | aIndex[cid] = 1; |
| 204 | } |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 205 | if( pStmt2 ){ |
| 206 | rc = sqlite3_finalize(pStmt2); |
| 207 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 208 | if( rc!=SQLITE_OK ){ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 209 | goto get_index_array_out; |
| 210 | } |
| 211 | } |
| 212 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 213 | |
| 214 | get_index_array_out: |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 215 | if( pStmt ){ |
| 216 | int rc2 = sqlite3_finalize(pStmt); |
| 217 | if( rc==SQLITE_OK ){ |
| 218 | rc = rc2; |
| 219 | } |
| 220 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 221 | if( rc!=SQLITE_OK ){ |
| 222 | sqliteFree(aIndex); |
| 223 | aIndex = 0; |
| 224 | } |
| 225 | *paIndex = aIndex; |
| 226 | return rc; |
| 227 | } |
| 228 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 229 | /* |
| 230 | ** Global Tcl variable $echo_module is a list. This routine appends |
| 231 | ** the string element zArg to that list in interpreter interp. |
| 232 | */ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 233 | static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 234 | int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 235 | Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 236 | } |
| 237 | |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 238 | /* |
| 239 | ** This function is called from within the echo-modules xCreate and |
| 240 | ** xConnect methods. The argc and argv arguments are copies of those |
| 241 | ** passed to the calling method. This function is responsible for |
| 242 | ** calling sqlite3_declare_vtab() to declare the schema of the virtual |
| 243 | ** table being created or connected. |
| 244 | ** |
| 245 | ** If the constructor was passed just one argument, i.e.: |
| 246 | ** |
| 247 | ** CREATE TABLE t1 AS echo(t2); |
| 248 | ** |
| 249 | ** Then t2 is assumed to be the name of a *real* database table. The |
| 250 | ** schema of the virtual table is declared by passing a copy of the |
| 251 | ** CREATE TABLE statement for the real table to sqlite3_declare_vtab(). |
| 252 | ** Hence, the virtual table should have exactly the same column names and |
| 253 | ** types as the real table. |
| 254 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 255 | static int echoDeclareVtab( |
| 256 | echo_vtab *pVtab, |
| 257 | sqlite3 *db, |
| 258 | int argc, |
| 259 | char **argv |
| 260 | ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 261 | int rc = SQLITE_OK; |
| 262 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 263 | if( argc>=4 ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 264 | sqlite3_stmt *pStmt = 0; |
| 265 | sqlite3_prepare(db, |
| 266 | "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", |
| 267 | -1, &pStmt, 0); |
danielk1977 | 70ba164 | 2006-06-21 16:02:42 +0000 | [diff] [blame] | 268 | sqlite3_bind_text(pStmt, 1, argv[3], -1, 0); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 269 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 270 | const char *zCreateTable = sqlite3_column_text(pStmt, 0); |
| 271 | sqlite3_declare_vtab(db, zCreateTable); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 272 | rc = sqlite3_finalize(pStmt); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 273 | } else { |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 274 | rc = sqlite3_finalize(pStmt); |
| 275 | if( rc==SQLITE_OK ){ |
| 276 | rc = SQLITE_ERROR; |
| 277 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 278 | } |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 279 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 280 | if( rc==SQLITE_OK ){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 281 | rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 282 | } |
| 283 | if( rc==SQLITE_OK ){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 284 | rc = getIndexArray(db, argv[3], pVtab->nCol, &pVtab->aIndex); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 285 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return rc; |
| 289 | } |
| 290 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 291 | /* |
| 292 | ** This function frees all runtime structures associated with the virtual |
| 293 | ** table pVtab. |
| 294 | */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 295 | static int echoDestructor(sqlite3_vtab *pVtab){ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 296 | echo_vtab *p = (echo_vtab*)pVtab; |
| 297 | sqliteFree(p->aIndex); |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 298 | sqliteFree(p->aCol); |
| 299 | sqliteFree(p->zTableName); |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 300 | sqliteFree(p->zLogName); |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 301 | sqliteFree(p); |
| 302 | return 0; |
| 303 | } |
| 304 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 305 | /* |
| 306 | ** This function is called to do the work of the xConnect() method - |
| 307 | ** to allocate the required in-memory structures for a newly connected |
| 308 | ** virtual table. |
| 309 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 310 | static int echoConstructor( |
| 311 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 312 | void *pAux, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 313 | int argc, char **argv, |
| 314 | sqlite3_vtab **ppVtab |
| 315 | ){ |
| 316 | int i; |
| 317 | echo_vtab *pVtab; |
| 318 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 319 | /* Allocate the sqlite3_vtab/echo_vtab structure itself */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 320 | pVtab = sqliteMalloc( sizeof(*pVtab) ); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 321 | if( !pVtab ){ |
| 322 | return SQLITE_NOMEM; |
| 323 | } |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 324 | pVtab->interp = (Tcl_Interp *)pAux; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 325 | pVtab->db = db; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 326 | |
| 327 | /* Allocate echo_vtab.zTableName */ |
danielk1977 | 70ba164 | 2006-06-21 16:02:42 +0000 | [diff] [blame] | 328 | pVtab->zTableName = sqlite3MPrintf("%s", argv[3]); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 329 | if( !pVtab->zTableName ){ |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 330 | echoDestructor((sqlite3_vtab *)pVtab); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 331 | return SQLITE_NOMEM; |
| 332 | } |
| 333 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 334 | /* Log the arguments to this function to Tcl var ::echo_module */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 335 | for(i=0; i<argc; i++){ |
| 336 | appendToEchoModule(pVtab->interp, argv[i]); |
| 337 | } |
| 338 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 339 | /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab |
| 340 | ** structure. If an error occurs, delete the sqlite3_vtab structure and |
| 341 | ** return an error code. |
| 342 | */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 343 | if( echoDeclareVtab(pVtab, db, argc, argv) ){ |
| 344 | echoDestructor((sqlite3_vtab *)pVtab); |
| 345 | return SQLITE_ERROR; |
| 346 | } |
| 347 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 348 | /* Success. Set *ppVtab and return */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 349 | *ppVtab = &pVtab->base; |
| 350 | return SQLITE_OK; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 351 | } |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 352 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 353 | /* |
| 354 | ** Echo virtual table module xCreate method. |
| 355 | */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 356 | static int echoCreate( |
| 357 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 358 | void *pAux, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 359 | int argc, char **argv, |
| 360 | sqlite3_vtab **ppVtab |
| 361 | ){ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 362 | int rc = SQLITE_OK; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 363 | appendToEchoModule((Tcl_Interp *)(pAux), "xCreate"); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 364 | rc = echoConstructor(db, pAux, argc, argv, ppVtab); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 365 | |
| 366 | /* If there were two arguments passed to the module at the SQL level |
| 367 | ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then |
| 368 | ** the second argument is used as a table name. Attempt to create |
| 369 | ** such a table with a single column, "logmsg". This table will |
| 370 | ** be used to log calls to the xUpdate method. It will be deleted |
| 371 | ** when the virtual table is DROPed. |
| 372 | ** |
| 373 | ** Note: The main point of this is to test that we can drop tables |
| 374 | ** from within an xDestroy method call. |
| 375 | */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 376 | if( rc==SQLITE_OK && argc==5 ){ |
| 377 | char *zSql; |
| 378 | echo_vtab *pVtab = *(echo_vtab **)ppVtab; |
| 379 | pVtab->zLogName = sqlite3MPrintf("%s", argv[4]); |
| 380 | zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName); |
| 381 | rc = sqlite3_exec(db, zSql, 0, 0, 0); |
| 382 | sqliteFree(zSql); |
| 383 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 384 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 385 | return rc; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 386 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | ** Echo virtual table module xConnect method. |
| 390 | */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 391 | static int echoConnect( |
| 392 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 393 | void *pAux, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 394 | int argc, char **argv, |
| 395 | sqlite3_vtab **ppVtab |
| 396 | ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 397 | appendToEchoModule((Tcl_Interp *)(pAux), "xConnect"); |
| 398 | return echoConstructor(db, pAux, argc, argv, ppVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 399 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 400 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 401 | /* |
| 402 | ** Echo virtual table module xDisconnect method. |
| 403 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 404 | static int echoDisconnect(sqlite3_vtab *pVtab){ |
| 405 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect"); |
| 406 | return echoDestructor(pVtab); |
| 407 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | ** Echo virtual table module xDestroy method. |
| 411 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 412 | static int echoDestroy(sqlite3_vtab *pVtab){ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 413 | int rc = SQLITE_OK; |
| 414 | echo_vtab *p = (echo_vtab *)pVtab; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 415 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy"); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 416 | |
| 417 | /* Drop the "log" table, if one exists (see echoCreate() for details) */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 418 | if( p && p->zLogName ){ |
| 419 | char *zSql; |
| 420 | zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName); |
| 421 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 422 | sqliteFree(zSql); |
| 423 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 424 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 425 | if( rc==SQLITE_OK ){ |
| 426 | rc = echoDestructor(pVtab); |
| 427 | } |
| 428 | return rc; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 429 | } |
| 430 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 431 | /* |
| 432 | ** Echo virtual table module xOpen method. |
| 433 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 434 | static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 435 | echo_cursor *pCur; |
| 436 | pCur = sqliteMalloc(sizeof(echo_cursor)); |
| 437 | *ppCursor = (sqlite3_vtab_cursor *)pCur; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 438 | return (pCur ? SQLITE_OK : SQLITE_NOMEM); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 439 | } |
| 440 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 441 | /* |
| 442 | ** Echo virtual table module xClose method. |
| 443 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 444 | static int echoClose(sqlite3_vtab_cursor *cur){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 445 | int rc; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 446 | echo_cursor *pCur = (echo_cursor *)cur; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 447 | sqlite3_stmt *pStmt = pCur->pStmt; |
| 448 | pCur->pStmt = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 449 | sqliteFree(pCur); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 450 | rc = sqlite3_finalize(pStmt); |
| 451 | return rc; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 452 | } |
| 453 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 454 | /* |
| 455 | ** Return non-zero if the cursor does not currently point to a valid record |
| 456 | ** (i.e if the scan has finished), or zero otherwise. |
| 457 | */ |
| 458 | static int echoEof(sqlite3_vtab_cursor *cur){ |
| 459 | return (((echo_cursor *)cur)->pStmt ? 0 : 1); |
| 460 | } |
| 461 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 462 | /* |
| 463 | ** Echo virtual table module xNext method. |
| 464 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 465 | static int echoNext(sqlite3_vtab_cursor *cur){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 466 | int rc; |
| 467 | echo_cursor *pCur = (echo_cursor *)cur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 468 | rc = sqlite3_step(pCur->pStmt); |
| 469 | |
| 470 | if( rc==SQLITE_ROW ){ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 471 | rc = SQLITE_OK; |
| 472 | }else{ |
| 473 | rc = sqlite3_finalize(pCur->pStmt); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 474 | pCur->pStmt = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return rc; |
| 478 | } |
| 479 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 480 | /* |
| 481 | ** Echo virtual table module xColumn method. |
| 482 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 483 | static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 484 | int iCol = i + 1; |
| 485 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
danielk1977 | fbbe005 | 2006-06-21 07:02:33 +0000 | [diff] [blame] | 486 | if( !pStmt ){ |
| 487 | sqlite3_result_null(ctx); |
| 488 | }else{ |
| 489 | assert( sqlite3_data_count(pStmt)>iCol ); |
| 490 | sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol)); |
| 491 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 492 | return SQLITE_OK; |
| 493 | } |
| 494 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 495 | /* |
| 496 | ** Echo virtual table module xRowid method. |
| 497 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 498 | static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 499 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 500 | *pRowid = sqlite3_column_int64(pStmt, 0); |
| 501 | return SQLITE_OK; |
| 502 | } |
| 503 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 504 | /* |
| 505 | ** Compute a simple hash of the null terminated string zString. |
| 506 | ** |
| 507 | ** This module uses only sqlite3_index_info.idxStr, not |
| 508 | ** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set |
| 509 | ** in echoBestIndex(), idxNum is set to the corresponding hash value. |
| 510 | ** In echoFilter(), code assert()s that the supplied idxNum value is |
| 511 | ** indeed the hash of the supplied idxStr. |
| 512 | */ |
| 513 | static int hashString(const char *zString){ |
| 514 | int val = 0; |
| 515 | int ii; |
| 516 | for(ii=0; zString[ii]; ii++){ |
| 517 | val = (val << 3) + (int)zString[ii]; |
| 518 | } |
| 519 | return val; |
| 520 | } |
| 521 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 522 | /* |
| 523 | ** Echo virtual table module xFilter method. |
| 524 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 525 | static int echoFilter( |
| 526 | sqlite3_vtab_cursor *pVtabCursor, |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 527 | int idxNum, const char *idxStr, |
| 528 | int argc, sqlite3_value **argv |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 529 | ){ |
| 530 | int rc; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 531 | int i; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 532 | |
| 533 | echo_cursor *pCur = (echo_cursor *)pVtabCursor; |
| 534 | echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab; |
| 535 | sqlite3 *db = pVtab->db; |
| 536 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 537 | /* Check that idxNum matches idxStr */ |
| 538 | assert( idxNum==hashString(idxStr) ); |
| 539 | |
| 540 | /* Log arguments to the ::echo_module Tcl variable */ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 541 | appendToEchoModule(pVtab->interp, "xFilter"); |
| 542 | appendToEchoModule(pVtab->interp, idxStr); |
| 543 | for(i=0; i<argc; i++){ |
| 544 | appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i])); |
| 545 | } |
| 546 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 547 | sqlite3_finalize(pCur->pStmt); |
| 548 | pCur->pStmt = 0; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 549 | |
| 550 | /* Prepare the SQL statement created by echoBestIndex and bind the |
| 551 | ** runtime parameters passed to this function to it. |
| 552 | */ |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 553 | rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0); |
danielk1977 | fbbe005 | 2006-06-21 07:02:33 +0000 | [diff] [blame] | 554 | assert( pCur->pStmt || rc!=SQLITE_OK ); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 555 | for(i=0; rc==SQLITE_OK && i<argc; i++){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 556 | sqlite3_bind_value(pCur->pStmt, i+1, argv[i]); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 557 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 558 | |
| 559 | /* If everything was successful, advance to the first row of the scan */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 560 | if( rc==SQLITE_OK ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 561 | rc = echoNext(pVtabCursor); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 562 | } |
| 563 | |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 564 | return rc; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 565 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 566 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | ** A helper function used by echoUpdate() and echoBestIndex() for |
| 570 | ** manipulating strings in concert with the sqlite3_mprintf() function. |
| 571 | ** |
| 572 | ** Parameter pzStr points to a pointer to a string allocated with |
| 573 | ** sqlite3_mprintf. The second parameter, zAppend, points to another |
| 574 | ** string. The two strings are concatenated together and *pzStr |
| 575 | ** set to point at the result. The initial buffer pointed to by *pzStr |
| 576 | ** is deallocated via sqlite3_free(). |
| 577 | ** |
| 578 | ** If the third argument, doFree, is true, then sqlite3_free() is |
| 579 | ** also called to free the buffer pointed to by zAppend. |
| 580 | */ |
| 581 | static void string_concat(char **pzStr, char *zAppend, int doFree){ |
| 582 | char *zIn = *pzStr; |
| 583 | if( zIn ){ |
| 584 | char *zTemp = zIn; |
| 585 | zIn = sqlite3_mprintf("%s%s", zIn, zAppend); |
| 586 | sqlite3_free(zTemp); |
| 587 | }else{ |
| 588 | zIn = sqlite3_mprintf("%s", zAppend); |
| 589 | } |
| 590 | *pzStr = zIn; |
| 591 | if( doFree ){ |
| 592 | sqlite3_free(zAppend); |
| 593 | } |
| 594 | } |
| 595 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 596 | /* |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 597 | ** The echo module implements the subset of query constraints and sort |
| 598 | ** orders that may take advantage of SQLite indices on the underlying |
| 599 | ** real table. For example, if the real table is declared as: |
| 600 | ** |
| 601 | ** CREATE TABLE real(a, b, c); |
| 602 | ** CREATE INDEX real_index ON real(b); |
| 603 | ** |
| 604 | ** then the echo module handles WHERE or ORDER BY clauses that refer |
| 605 | ** to the column "b", but not "a" or "c". If a multi-column index is |
| 606 | ** present, only it's left most column is considered. |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 607 | ** |
| 608 | ** This xBestIndex method encodes the proposed search strategy as |
| 609 | ** an SQL query on the real table underlying the virtual echo module |
| 610 | ** table and stores the query in sqlite3_index_info.idxStr. The SQL |
| 611 | ** statement is of the form: |
| 612 | ** |
| 613 | ** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>? |
| 614 | ** |
| 615 | ** where the <where-clause> and <order-by-clause> are determined |
| 616 | ** by the contents of the structure pointed to by the pIdxInfo argument. |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 617 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 618 | static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 619 | int ii; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 620 | char *zQuery = 0; |
| 621 | char *zNew; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 622 | int nArg = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 623 | const char *zSep = "WHERE"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 624 | echo_vtab *pVtab = (echo_vtab *)tab; |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 625 | sqlite3_stmt *pStmt = 0; |
| 626 | |
| 627 | int nRow; |
| 628 | int useIdx = 0; |
| 629 | int rc = SQLITE_OK; |
| 630 | |
| 631 | /* Determine the number of rows in the table and store this value in local |
| 632 | ** variable nRow. The 'estimated-cost' of the scan will be the number of |
| 633 | ** rows in the table for a linear scan, or the log (base 2) of the |
| 634 | ** number of rows if the proposed scan uses an index. |
| 635 | */ |
| 636 | zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName); |
| 637 | rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0); |
| 638 | if( rc!=SQLITE_OK ){ |
| 639 | return rc; |
| 640 | } |
| 641 | sqlite3_step(pStmt); |
| 642 | nRow = sqlite3_column_int(pStmt, 0); |
| 643 | rc = sqlite3_finalize(pStmt); |
| 644 | if( rc!=SQLITE_OK ){ |
| 645 | return rc; |
| 646 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 647 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 648 | zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 649 | for(ii=0; ii<pIdxInfo->nConstraint; ii++){ |
| 650 | const struct sqlite3_index_constraint *pConstraint; |
| 651 | struct sqlite3_index_constraint_usage *pUsage; |
| 652 | |
| 653 | pConstraint = &pIdxInfo->aConstraint[ii]; |
| 654 | pUsage = &pIdxInfo->aConstraintUsage[ii]; |
| 655 | |
| 656 | int iCol = pConstraint->iColumn; |
| 657 | if( pVtab->aIndex[iCol] ){ |
| 658 | char *zCol = pVtab->aCol[iCol]; |
| 659 | char *zOp = 0; |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 660 | useIdx = 1; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 661 | if( iCol<0 ){ |
| 662 | zCol = "rowid"; |
| 663 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 664 | switch( pConstraint->op ){ |
| 665 | case SQLITE_INDEX_CONSTRAINT_EQ: |
| 666 | zOp = "="; break; |
| 667 | case SQLITE_INDEX_CONSTRAINT_LT: |
| 668 | zOp = "<"; break; |
| 669 | case SQLITE_INDEX_CONSTRAINT_GT: |
| 670 | zOp = ">"; break; |
| 671 | case SQLITE_INDEX_CONSTRAINT_LE: |
| 672 | zOp = "<="; break; |
| 673 | case SQLITE_INDEX_CONSTRAINT_GE: |
| 674 | zOp = ">="; break; |
| 675 | case SQLITE_INDEX_CONSTRAINT_MATCH: |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 676 | zOp = "LIKE"; break; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 677 | } |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 678 | if( zOp[0]=='L' ){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 679 | zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')", |
| 680 | zSep, zCol); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 681 | } else { |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 682 | zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 683 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 684 | string_concat(&zQuery, zNew, 1); |
| 685 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 686 | zSep = "AND"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 687 | pUsage->argvIndex = ++nArg; |
| 688 | pUsage->omit = 1; |
| 689 | } |
| 690 | } |
danielk1977 | 47d0809 | 2006-06-14 07:41:31 +0000 | [diff] [blame] | 691 | |
| 692 | /* If there is only one term in the ORDER BY clause, and it is |
| 693 | ** on a column that this virtual table has an index for, then consume |
| 694 | ** the ORDER BY clause. |
| 695 | */ |
| 696 | if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){ |
| 697 | char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn]; |
| 698 | char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC"; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 699 | zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir); |
| 700 | string_concat(&zQuery, zNew, 1); |
danielk1977 | 47d0809 | 2006-06-14 07:41:31 +0000 | [diff] [blame] | 701 | pIdxInfo->orderByConsumed = 1; |
| 702 | } |
| 703 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 704 | appendToEchoModule(pVtab->interp, "xBestIndex");; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 705 | appendToEchoModule(pVtab->interp, zQuery); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 706 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 707 | pIdxInfo->idxNum = hashString(zQuery); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 708 | pIdxInfo->idxStr = zQuery; |
| 709 | pIdxInfo->needToFreeIdxStr = 1; |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 710 | if( useIdx ){ |
| 711 | /* Approximation of log2(nRow). */ |
| 712 | for( ii=0; ii<(sizeof(int)*8); ii++ ){ |
| 713 | if( nRow & (1<<ii) ){ |
| 714 | pIdxInfo->estimatedCost = (double)ii; |
| 715 | } |
| 716 | } |
| 717 | } else { |
| 718 | pIdxInfo->estimatedCost = (double)nRow; |
| 719 | } |
| 720 | return rc; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 721 | } |
| 722 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 723 | /* |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 724 | ** The xUpdate method for echo module virtual tables. |
| 725 | ** |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 726 | ** apData[0] apData[1] apData[2..] |
| 727 | ** |
| 728 | ** INTEGER DELETE |
| 729 | ** |
| 730 | ** INTEGER NULL (nCol args) UPDATE (do not set rowid) |
| 731 | ** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>) |
| 732 | ** |
| 733 | ** NULL NULL (nCol args) INSERT INTO (automatic rowid value) |
| 734 | ** NULL INTEGER (nCol args) INSERT (incl. rowid value) |
| 735 | ** |
| 736 | */ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 737 | int echoUpdate( |
| 738 | sqlite3_vtab *tab, |
| 739 | int nData, |
| 740 | sqlite3_value **apData, |
| 741 | sqlite_int64 *pRowid |
| 742 | ){ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 743 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 744 | sqlite3 *db = pVtab->db; |
| 745 | int rc = SQLITE_OK; |
| 746 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 747 | sqlite3_stmt *pStmt; |
| 748 | char *z = 0; /* SQL statement to execute */ |
| 749 | int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */ |
| 750 | int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */ |
| 751 | int i; /* Counter variable used by for loops */ |
| 752 | |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 753 | assert( nData==pVtab->nCol+2 || nData==1 ); |
| 754 | |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 755 | /* If apData[0] is an integer and nData>1 then do an UPDATE */ |
| 756 | if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 757 | z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 758 | char *zSep = " SET"; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 759 | |
| 760 | bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER); |
| 761 | bindArgZero = 1; |
| 762 | |
| 763 | if( bindArgOne ){ |
| 764 | string_concat(&z, " SET rowid=?1 ", 0); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 765 | zSep = ","; |
| 766 | } |
| 767 | for(i=2; i<nData; i++){ |
| 768 | if( apData[i]==0 ) continue; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 769 | string_concat(&z, sqlite3_mprintf( |
| 770 | "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 771 | zSep = ","; |
| 772 | } |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 773 | string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 774 | } |
| 775 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 776 | /* If apData[0] is an integer and nData==1 then do a DELETE */ |
| 777 | else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
| 778 | z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName); |
| 779 | bindArgZero = 1; |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 782 | /* If the first argument is NULL and there are more than two args, INSERT */ |
| 783 | else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 784 | int ii; |
| 785 | char *zInsert = 0; |
| 786 | char *zValues = 0; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 787 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 788 | zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName); |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 789 | if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){ |
| 790 | bindArgOne = 1; |
| 791 | zValues = sqlite3_mprintf("?"); |
| 792 | string_concat(&zInsert, "rowid", 0); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 793 | } |
| 794 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 795 | assert((pVtab->nCol+2)==nData); |
| 796 | for(ii=2; ii<nData; ii++){ |
| 797 | string_concat(&zInsert, |
| 798 | sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1); |
| 799 | string_concat(&zValues, |
| 800 | sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 801 | } |
| 802 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 803 | string_concat(&z, zInsert, 1); |
| 804 | string_concat(&z, ") VALUES(", 0); |
| 805 | string_concat(&z, zValues, 1); |
| 806 | string_concat(&z, ")", 0); |
| 807 | } |
| 808 | |
| 809 | /* Anything else is an error */ |
| 810 | else{ |
| 811 | assert(0); |
| 812 | return SQLITE_ERROR; |
| 813 | } |
| 814 | |
| 815 | rc = sqlite3_prepare(db, z, -1, &pStmt, 0); |
| 816 | assert( rc!=SQLITE_OK || pStmt ); |
| 817 | sqlite3_free(z); |
| 818 | if( rc==SQLITE_OK ) { |
| 819 | if( bindArgZero ){ |
| 820 | sqlite3_bind_value(pStmt, nData, apData[0]); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 821 | } |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 822 | if( bindArgOne ){ |
| 823 | sqlite3_bind_value(pStmt, 1, apData[1]); |
| 824 | } |
| 825 | for(i=2; i<nData; i++){ |
| 826 | if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]); |
| 827 | } |
| 828 | sqlite3_step(pStmt); |
| 829 | rc = sqlite3_finalize(pStmt); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 830 | } |
| 831 | |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 832 | if( pRowid && rc==SQLITE_OK ){ |
| 833 | *pRowid = sqlite3_last_insert_rowid(db); |
| 834 | } |
| 835 | |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 836 | return rc; |
| 837 | } |
| 838 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 839 | /* |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 840 | ** xBegin, xSync, xCommit and xRollback callbacks for echo module |
| 841 | ** virtual tables. Do nothing other than add the name of the callback |
| 842 | ** to the $::echo_module Tcl variable. |
| 843 | */ |
| 844 | static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){ |
| 845 | char *z; |
| 846 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 847 | z = sqlite3_mprintf("echo(%s)", pVtab->zTableName); |
| 848 | appendToEchoModule(pVtab->interp, zCall); |
| 849 | appendToEchoModule(pVtab->interp, z); |
| 850 | sqlite3_free(z); |
| 851 | return SQLITE_OK; |
| 852 | } |
| 853 | static int echoBegin(sqlite3_vtab *tab){ |
| 854 | return echoTransactionCall(tab, "xBegin"); |
| 855 | } |
| 856 | static int echoSync(sqlite3_vtab *tab){ |
| 857 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 858 | Tcl_Interp *interp = pVtab->interp; |
| 859 | const char *zVal; |
| 860 | |
| 861 | echoTransactionCall(tab, "xSync"); |
| 862 | |
| 863 | /* Check if the $::echo_module_sync_fail variable is defined. If it is, |
| 864 | ** and it is set to the name of the real table underlying this virtual |
| 865 | ** echo module table, then cause this xSync operation to fail. |
| 866 | */ |
| 867 | zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY); |
| 868 | if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ |
| 869 | return -1; |
| 870 | } |
| 871 | return SQLITE_OK; |
| 872 | } |
| 873 | static int echoCommit(sqlite3_vtab *tab){ |
| 874 | return echoTransactionCall(tab, "xCommit"); |
| 875 | } |
| 876 | static int echoRollback(sqlite3_vtab *tab){ |
| 877 | return echoTransactionCall(tab, "xRollback"); |
| 878 | } |
| 879 | |
| 880 | /* |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 881 | ** A virtual table module that merely "echos" the contents of another |
| 882 | ** table (like an SQL VIEW). |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 883 | */ |
| 884 | static sqlite3_module echoModule = { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 885 | 0, /* iVersion */ |
| 886 | "echo", /* zName */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 887 | echoCreate, |
| 888 | echoConnect, |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 889 | echoBestIndex, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 890 | echoDisconnect, |
| 891 | echoDestroy, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 892 | echoOpen, /* xOpen - open a cursor */ |
| 893 | echoClose, /* xClose - close a cursor */ |
| 894 | echoFilter, /* xFilter - configure scan constraints */ |
| 895 | echoNext, /* xNext - advance a cursor */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 896 | echoEof, /* xEof */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 897 | echoColumn, /* xColumn - read data */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 898 | echoRowid, /* xRowid - read data */ |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 899 | echoUpdate, /* xUpdate - write data */ |
| 900 | echoBegin, /* xBegin - begin transaction */ |
| 901 | echoSync, /* xSync - sync transaction */ |
| 902 | echoCommit, /* xCommit - commit transaction */ |
| 903 | echoRollback /* xRollback - rollback transaction */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 904 | }; |
| 905 | |
| 906 | /* |
| 907 | ** Decode a pointer to an sqlite3 object. |
| 908 | */ |
| 909 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 910 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 911 | return TCL_OK; |
| 912 | } |
| 913 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 914 | /* |
| 915 | ** Register the echo virtual table module. |
| 916 | */ |
| 917 | static int register_echo_module( |
| 918 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 919 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 920 | int objc, /* Number of arguments */ |
| 921 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 922 | ){ |
| 923 | sqlite3 *db; |
| 924 | if( objc!=2 ){ |
| 925 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 926 | return TCL_ERROR; |
| 927 | } |
| 928 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 929 | sqlite3_create_module(db, "echo", &echoModule, (void *)interp); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 930 | return TCL_OK; |
| 931 | } |
| 932 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 933 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 934 | |
| 935 | /* |
| 936 | ** Register commands with the TCL interpreter. |
| 937 | */ |
| 938 | int Sqlitetest8_Init(Tcl_Interp *interp){ |
| 939 | static struct { |
| 940 | char *zName; |
| 941 | Tcl_ObjCmdProc *xProc; |
| 942 | void *clientData; |
| 943 | } aObjCmd[] = { |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 944 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 945 | { "register_echo_module", register_echo_module, 0 }, |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 946 | #endif |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 947 | }; |
| 948 | int i; |
| 949 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 950 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 951 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 952 | } |
| 953 | return TCL_OK; |
| 954 | } |