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