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 | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 16 | ** $Id: test8.c,v 1.11 2006/06/13 23:51:35 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 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 24 | typedef struct echo_vtab echo_vtab; |
| 25 | typedef struct echo_cursor echo_cursor; |
| 26 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 27 | /* |
| 28 | ** An echo virtual-table object |
| 29 | ** |
| 30 | ** If it is not NULL, the aHasIndex array is allocated so that it has |
| 31 | ** the same number of entries as there are columns in the underlying |
| 32 | ** real table. |
| 33 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 34 | struct echo_vtab { |
| 35 | sqlite3_vtab base; |
| 36 | Tcl_Interp *interp; |
| 37 | sqlite3 *db; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 38 | char *zTableName; /* Name of the real table */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 39 | char *zStmt; /* "SELECT rowid, * FROM <real-table-name> " */ |
| 40 | |
| 41 | int *aIndex; |
| 42 | int nCol; |
| 43 | char **aCol; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* An echo cursor object */ |
| 47 | struct echo_cursor { |
| 48 | sqlite3_vtab_cursor base; |
| 49 | sqlite3_stmt *pStmt; |
| 50 | int errcode; /* Error code */ |
| 51 | }; |
| 52 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 53 | static int getColumnNames( |
| 54 | sqlite3 *db, |
| 55 | const char *zTab, |
| 56 | char ***paCol, |
| 57 | int *pnCol |
| 58 | ){ |
| 59 | char **aCol = 0; |
| 60 | char zBuf[1024]; |
| 61 | sqlite3_stmt *pStmt = 0; |
| 62 | int rc = SQLITE_OK; |
| 63 | int nCol; |
| 64 | |
| 65 | sprintf(zBuf, "SELECT * FROM %s", zTab); |
| 66 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0); |
| 67 | if( rc==SQLITE_OK ){ |
| 68 | int ii; |
| 69 | nCol = sqlite3_column_count(pStmt); |
| 70 | aCol = sqliteMalloc(sizeof(char *) * nCol); |
| 71 | if( !aCol ){ |
| 72 | rc = SQLITE_NOMEM; |
| 73 | goto fail; |
| 74 | } |
| 75 | for(ii=0; ii<nCol; ii++){ |
| 76 | aCol[ii] = sqlite3StrDup(sqlite3_column_name(pStmt, ii)); |
| 77 | if( !aCol[ii] ){ |
| 78 | rc = SQLITE_NOMEM; |
| 79 | goto fail; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | *paCol = aCol; |
| 85 | *pnCol = nCol; |
| 86 | |
| 87 | fail: |
| 88 | sqlite3_finalize(pStmt); |
| 89 | if( rc!=SQLITE_OK && aCol ){ |
| 90 | int ii; |
| 91 | for(ii=0; ii<nCol; ii++){ |
| 92 | sqliteFree(aCol[ii]); |
| 93 | } |
| 94 | sqliteFree(aCol); |
| 95 | } |
| 96 | return rc; |
| 97 | } |
| 98 | |
| 99 | static int getIndexArray(sqlite3 *db, const char *zTab, int **paIndex){ |
| 100 | char zBuf[1024]; |
| 101 | sqlite3_stmt *pStmt = 0; |
| 102 | int nCol; |
| 103 | int *aIndex = 0; |
| 104 | int rc; |
| 105 | |
| 106 | sprintf(zBuf, "SELECT * FROM %s", zTab); |
| 107 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0); |
| 108 | nCol = sqlite3_column_count(pStmt); |
| 109 | |
| 110 | sqlite3_finalize(pStmt); |
| 111 | pStmt = 0; |
| 112 | if( rc!=SQLITE_OK ){ |
| 113 | goto get_index_array_out; |
| 114 | } |
| 115 | |
| 116 | aIndex = (int *)sqliteMalloc(sizeof(int) * nCol); |
| 117 | if( !aIndex ){ |
| 118 | rc = SQLITE_NOMEM; |
| 119 | goto get_index_array_out; |
| 120 | } |
| 121 | |
| 122 | sprintf(zBuf, "PRAGMA index_list(%s)", zTab); |
| 123 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0); |
| 124 | |
| 125 | while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 126 | sqlite3_stmt *pStmt2 = 0; |
| 127 | sprintf(zBuf, "PRAGMA index_info(%s)", sqlite3_column_text(pStmt, 1)); |
| 128 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt2, 0); |
| 129 | if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){ |
| 130 | int cid = sqlite3_column_int(pStmt2, 1); |
| 131 | assert( cid>=0 && cid<nCol ); |
| 132 | aIndex[cid] = 1; |
| 133 | } |
| 134 | rc = sqlite3_finalize(pStmt2); |
| 135 | if( rc!=SQLITE_OK ){ |
| 136 | sqlite3_finalize(pStmt); |
| 137 | goto get_index_array_out; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | rc = sqlite3_finalize(pStmt); |
| 142 | |
| 143 | get_index_array_out: |
| 144 | if( rc!=SQLITE_OK ){ |
| 145 | sqliteFree(aIndex); |
| 146 | aIndex = 0; |
| 147 | } |
| 148 | *paIndex = aIndex; |
| 149 | return rc; |
| 150 | } |
| 151 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 152 | /* |
| 153 | ** Global Tcl variable $echo_module is a list. This routine appends |
| 154 | ** the string element zArg to that list in interpreter interp. |
| 155 | */ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 156 | static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 157 | int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 158 | Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 159 | } |
| 160 | |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 161 | /* |
| 162 | ** This function is called from within the echo-modules xCreate and |
| 163 | ** xConnect methods. The argc and argv arguments are copies of those |
| 164 | ** passed to the calling method. This function is responsible for |
| 165 | ** calling sqlite3_declare_vtab() to declare the schema of the virtual |
| 166 | ** table being created or connected. |
| 167 | ** |
| 168 | ** If the constructor was passed just one argument, i.e.: |
| 169 | ** |
| 170 | ** CREATE TABLE t1 AS echo(t2); |
| 171 | ** |
| 172 | ** Then t2 is assumed to be the name of a *real* database table. The |
| 173 | ** schema of the virtual table is declared by passing a copy of the |
| 174 | ** CREATE TABLE statement for the real table to sqlite3_declare_vtab(). |
| 175 | ** Hence, the virtual table should have exactly the same column names and |
| 176 | ** types as the real table. |
| 177 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 178 | static int echoDeclareVtab( |
| 179 | echo_vtab *pVtab, |
| 180 | sqlite3 *db, |
| 181 | int argc, |
| 182 | char **argv |
| 183 | ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 184 | int rc = SQLITE_OK; |
| 185 | |
| 186 | if( argc==2 ){ |
| 187 | sqlite3_stmt *pStmt = 0; |
| 188 | sqlite3_prepare(db, |
| 189 | "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", |
| 190 | -1, &pStmt, 0); |
| 191 | sqlite3_bind_text(pStmt, 1, argv[1], -1, 0); |
| 192 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 193 | const char *zCreateTable = sqlite3_column_text(pStmt, 0); |
drh | a75803d | 2006-06-12 12:50:23 +0000 | [diff] [blame] | 194 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 195 | sqlite3_declare_vtab(db, zCreateTable); |
drh | a75803d | 2006-06-12 12:50:23 +0000 | [diff] [blame] | 196 | #endif |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 197 | } else { |
| 198 | rc = SQLITE_ERROR; |
| 199 | } |
| 200 | sqlite3_finalize(pStmt); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 201 | pVtab->zStmt = sqlite3MPrintf("SELECT rowid, * FROM %s ", argv[1]); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 202 | if( rc==SQLITE_OK ){ |
| 203 | rc = getIndexArray(db, argv[1], &pVtab->aIndex); |
| 204 | } |
| 205 | if( rc==SQLITE_OK ){ |
| 206 | rc = getColumnNames(db, argv[1], &pVtab->aCol, &pVtab->nCol); |
| 207 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return rc; |
| 211 | } |
| 212 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 213 | static int echoConstructor( |
| 214 | sqlite3 *db, |
| 215 | const sqlite3_module *pModule, |
| 216 | int argc, char **argv, |
| 217 | sqlite3_vtab **ppVtab |
| 218 | ){ |
| 219 | int i; |
| 220 | echo_vtab *pVtab; |
| 221 | |
| 222 | pVtab = sqliteMalloc( sizeof(*pVtab) ); |
| 223 | |
| 224 | *ppVtab = &pVtab->base; |
| 225 | pVtab->base.pModule = pModule; |
| 226 | pVtab->interp = pModule->pAux; |
| 227 | pVtab->db = db; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 228 | pVtab->zTableName = sqlite3MPrintf("%s", argv[1]); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 229 | for(i=0; i<argc; i++){ |
| 230 | appendToEchoModule(pVtab->interp, argv[i]); |
| 231 | } |
| 232 | |
| 233 | echoDeclareVtab(pVtab, db, argc, argv); |
| 234 | return 0; |
| 235 | } |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 236 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 237 | /* Methods for the echo module */ |
| 238 | static int echoCreate( |
| 239 | sqlite3 *db, |
| 240 | const sqlite3_module *pModule, |
| 241 | int argc, char **argv, |
| 242 | sqlite3_vtab **ppVtab |
| 243 | ){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 244 | appendToEchoModule((Tcl_Interp *)(pModule->pAux), "xCreate"); |
| 245 | return echoConstructor(db, pModule, argc, argv, ppVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 246 | } |
| 247 | static int echoConnect( |
| 248 | sqlite3 *db, |
| 249 | const sqlite3_module *pModule, |
| 250 | int argc, char **argv, |
| 251 | sqlite3_vtab **ppVtab |
| 252 | ){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 253 | appendToEchoModule((Tcl_Interp *)(pModule->pAux), "xConnect"); |
| 254 | return echoConstructor(db, pModule, argc, argv, ppVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 255 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 256 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 257 | static int echoDestructor(sqlite3_vtab *pVtab){ |
| 258 | int ii; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 259 | echo_vtab *p = (echo_vtab*)pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 260 | sqliteFree(p->zStmt); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 261 | sqliteFree(p->aIndex); |
| 262 | for(ii=0; ii<p->nCol; ii++){ |
| 263 | sqliteFree(p->aCol[ii]); |
| 264 | } |
| 265 | sqliteFree(p->aCol); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 266 | sqliteFree(p->zTableName); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 267 | sqliteFree(p); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 271 | static int echoDisconnect(sqlite3_vtab *pVtab){ |
| 272 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect"); |
| 273 | return echoDestructor(pVtab); |
| 274 | } |
| 275 | static int echoDestroy(sqlite3_vtab *pVtab){ |
| 276 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy"); |
| 277 | return echoDestructor(pVtab); |
| 278 | } |
| 279 | |
| 280 | static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 281 | echo_cursor *pCur; |
| 282 | pCur = sqliteMalloc(sizeof(echo_cursor)); |
| 283 | *ppCursor = (sqlite3_vtab_cursor *)pCur; |
| 284 | return SQLITE_OK; |
| 285 | } |
| 286 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 287 | static int echoClose(sqlite3_vtab_cursor *cur){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 288 | echo_cursor *pCur = (echo_cursor *)cur; |
| 289 | sqlite3_finalize(pCur->pStmt); |
| 290 | sqliteFree(pCur); |
| 291 | return SQLITE_OK; |
| 292 | } |
| 293 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 294 | static int echoNext(sqlite3_vtab_cursor *cur){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 295 | int rc; |
| 296 | echo_cursor *pCur = (echo_cursor *)cur; |
| 297 | |
| 298 | rc = sqlite3_step(pCur->pStmt); |
| 299 | |
| 300 | if( rc==SQLITE_ROW ){ |
| 301 | rc = 1; |
| 302 | } else { |
| 303 | pCur->errcode = sqlite3_finalize(pCur->pStmt); |
| 304 | pCur->pStmt = 0; |
| 305 | rc = 0; |
| 306 | } |
| 307 | |
| 308 | return rc; |
| 309 | } |
| 310 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 311 | static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 312 | int iCol = i + 1; |
| 313 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 314 | |
| 315 | assert( sqlite3_data_count(pStmt)>iCol ); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 316 | sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol)); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 317 | return SQLITE_OK; |
| 318 | } |
| 319 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 320 | static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 321 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 322 | *pRowid = sqlite3_column_int64(pStmt, 0); |
| 323 | return SQLITE_OK; |
| 324 | } |
| 325 | |
| 326 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 327 | static int echoFilter( |
| 328 | sqlite3_vtab_cursor *pVtabCursor, |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 329 | int idxNum, const char *idxStr, |
| 330 | int argc, sqlite3_value **argv |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 331 | ){ |
| 332 | int rc; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 333 | int i; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 334 | |
| 335 | echo_cursor *pCur = (echo_cursor *)pVtabCursor; |
| 336 | echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab; |
| 337 | sqlite3 *db = pVtab->db; |
| 338 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 339 | sqlite3_finalize(pCur->pStmt); |
| 340 | pCur->pStmt = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 341 | rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0); |
| 342 | for(i=0; i<argc; i++){ |
| 343 | switch( sqlite3_value_type(argv[i]) ){ |
| 344 | case SQLITE_INTEGER: { |
| 345 | sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i])); |
| 346 | break; |
| 347 | } |
| 348 | case SQLITE_FLOAT: { |
| 349 | sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i])); |
| 350 | break; |
| 351 | } |
| 352 | case SQLITE_NULL: { |
| 353 | sqlite3_bind_null(pCur->pStmt, i+1); |
| 354 | break; |
| 355 | } |
| 356 | case SQLITE_TEXT: { |
| 357 | sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]), |
| 358 | sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT); |
| 359 | break; |
| 360 | } |
| 361 | case SQLITE_BLOB: { |
| 362 | sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]), |
| 363 | sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT); |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 368 | if( rc==SQLITE_OK ){ |
| 369 | rc = echoNext(pVtabCursor); |
| 370 | } |
| 371 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 372 | appendToEchoModule(pVtab->interp, "xFilter"); |
| 373 | appendToEchoModule(pVtab->interp, idxStr); |
| 374 | for(i=0; i<argc; i++){ |
| 375 | appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i])); |
| 376 | } |
| 377 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 378 | return rc; |
| 379 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 380 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 381 | /* |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 382 | ** The echo module implements the subset of query constraints and sort |
| 383 | ** orders that may take advantage of SQLite indices on the underlying |
| 384 | ** real table. For example, if the real table is declared as: |
| 385 | ** |
| 386 | ** CREATE TABLE real(a, b, c); |
| 387 | ** CREATE INDEX real_index ON real(b); |
| 388 | ** |
| 389 | ** then the echo module handles WHERE or ORDER BY clauses that refer |
| 390 | ** to the column "b", but not "a" or "c". If a multi-column index is |
| 391 | ** present, only it's left most column is considered. |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 392 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 393 | static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 394 | int ii; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 395 | char *zQuery = 0; |
| 396 | char *zNew; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 397 | int nArg = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 398 | const char *zSep = "WHERE"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 399 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 400 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 401 | zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 402 | for(ii=0; ii<pIdxInfo->nConstraint; ii++){ |
| 403 | const struct sqlite3_index_constraint *pConstraint; |
| 404 | struct sqlite3_index_constraint_usage *pUsage; |
| 405 | |
| 406 | pConstraint = &pIdxInfo->aConstraint[ii]; |
| 407 | pUsage = &pIdxInfo->aConstraintUsage[ii]; |
| 408 | |
| 409 | int iCol = pConstraint->iColumn; |
| 410 | if( pVtab->aIndex[iCol] ){ |
| 411 | char *zCol = pVtab->aCol[iCol]; |
| 412 | char *zOp = 0; |
| 413 | switch( pConstraint->op ){ |
| 414 | case SQLITE_INDEX_CONSTRAINT_EQ: |
| 415 | zOp = "="; break; |
| 416 | case SQLITE_INDEX_CONSTRAINT_LT: |
| 417 | zOp = "<"; break; |
| 418 | case SQLITE_INDEX_CONSTRAINT_GT: |
| 419 | zOp = ">"; break; |
| 420 | case SQLITE_INDEX_CONSTRAINT_LE: |
| 421 | zOp = "<="; break; |
| 422 | case SQLITE_INDEX_CONSTRAINT_GE: |
| 423 | zOp = ">="; break; |
| 424 | case SQLITE_INDEX_CONSTRAINT_MATCH: |
| 425 | zOp = "MATCH"; break; |
| 426 | } |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 427 | zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp); |
| 428 | sqlite3_free(zQuery); |
| 429 | zQuery = zNew; |
| 430 | zSep = "AND"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 431 | pUsage->argvIndex = ++nArg; |
| 432 | pUsage->omit = 1; |
| 433 | } |
| 434 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 435 | appendToEchoModule(pVtab->interp, "xBestIndex");; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 436 | appendToEchoModule(pVtab->interp, zQuery); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 437 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame^] | 438 | pIdxInfo->idxStr = zQuery; |
| 439 | pIdxInfo->needToFreeIdxStr = 1; |
| 440 | pIdxInfo->estimatedCost = 1.0; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 441 | return SQLITE_OK; |
| 442 | } |
| 443 | |
| 444 | /* |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 445 | ** A virtual table module that merely echos method calls into TCL |
| 446 | ** variables. |
| 447 | */ |
| 448 | static sqlite3_module echoModule = { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 449 | 0, /* iVersion */ |
| 450 | "echo", /* zName */ |
| 451 | 0, /* pAux */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 452 | echoCreate, |
| 453 | echoConnect, |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 454 | echoBestIndex, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 455 | echoDisconnect, |
| 456 | echoDestroy, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 457 | echoOpen, /* xOpen - open a cursor */ |
| 458 | echoClose, /* xClose - close a cursor */ |
| 459 | echoFilter, /* xFilter - configure scan constraints */ |
| 460 | echoNext, /* xNext - advance a cursor */ |
| 461 | echoColumn, /* xColumn - read data */ |
| 462 | echoRowid /* xRowid - read data */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | /* |
| 466 | ** Decode a pointer to an sqlite3 object. |
| 467 | */ |
| 468 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 469 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 470 | return TCL_OK; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /* |
| 475 | ** Register the echo virtual table module. |
| 476 | */ |
| 477 | static int register_echo_module( |
| 478 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 479 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 480 | int objc, /* Number of arguments */ |
| 481 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 482 | ){ |
| 483 | sqlite3 *db; |
| 484 | if( objc!=2 ){ |
| 485 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 486 | return TCL_ERROR; |
| 487 | } |
| 488 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 489 | echoModule.pAux = interp; |
| 490 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 491 | sqlite3_create_module(db, "echo", &echoModule); |
| 492 | #endif |
| 493 | return TCL_OK; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /* |
| 498 | ** Register commands with the TCL interpreter. |
| 499 | */ |
| 500 | int Sqlitetest8_Init(Tcl_Interp *interp){ |
| 501 | static struct { |
| 502 | char *zName; |
| 503 | Tcl_ObjCmdProc *xProc; |
| 504 | void *clientData; |
| 505 | } aObjCmd[] = { |
| 506 | { "register_echo_module", register_echo_module, 0 }, |
| 507 | }; |
| 508 | int i; |
| 509 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 510 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 511 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 512 | } |
| 513 | return TCL_OK; |
| 514 | } |