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 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 16 | ** $Id: test8.c,v 1.23 2006/06/15 15:38:42 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 | /* |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 28 | ** An echo virtual-table object. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 29 | ** |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 30 | ** echo.vtab.aIndex is an array of booleans. The nth entry is true if |
| 31 | ** the nth column of the real table is the left-most column of an index |
| 32 | ** (implicit or otherwise). In other words, if SQLite can optimize |
| 33 | ** a query like "SELECT * FROM real_table WHERE col = ?". |
| 34 | ** |
| 35 | ** Member variable contains copies of the column names of the real table. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 36 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 37 | struct echo_vtab { |
| 38 | sqlite3_vtab base; |
| 39 | Tcl_Interp *interp; |
| 40 | sqlite3 *db; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 41 | |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 42 | char *zTableName; /* Name of the real table */ |
| 43 | int nCol; /* Number of columns in the real table */ |
| 44 | int *aIndex; /* Array of size nCol. True if column has an index */ |
| 45 | char **aCol; /* Array of size nCol. Column names */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* An echo cursor object */ |
| 49 | struct echo_cursor { |
| 50 | sqlite3_vtab_cursor base; |
| 51 | sqlite3_stmt *pStmt; |
| 52 | int errcode; /* Error code */ |
| 53 | }; |
| 54 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 55 | static int getColumnNames( |
| 56 | sqlite3 *db, |
| 57 | const char *zTab, |
| 58 | char ***paCol, |
| 59 | int *pnCol |
| 60 | ){ |
| 61 | char **aCol = 0; |
| 62 | char zBuf[1024]; |
| 63 | sqlite3_stmt *pStmt = 0; |
| 64 | int rc = SQLITE_OK; |
| 65 | int nCol; |
| 66 | |
| 67 | sprintf(zBuf, "SELECT * FROM %s", zTab); |
| 68 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0); |
| 69 | if( rc==SQLITE_OK ){ |
| 70 | int ii; |
| 71 | nCol = sqlite3_column_count(pStmt); |
| 72 | aCol = sqliteMalloc(sizeof(char *) * nCol); |
| 73 | if( !aCol ){ |
| 74 | rc = SQLITE_NOMEM; |
| 75 | goto fail; |
| 76 | } |
| 77 | for(ii=0; ii<nCol; ii++){ |
| 78 | aCol[ii] = sqlite3StrDup(sqlite3_column_name(pStmt, ii)); |
| 79 | if( !aCol[ii] ){ |
| 80 | rc = SQLITE_NOMEM; |
| 81 | goto fail; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | *paCol = aCol; |
| 87 | *pnCol = nCol; |
| 88 | |
| 89 | fail: |
| 90 | sqlite3_finalize(pStmt); |
| 91 | if( rc!=SQLITE_OK && aCol ){ |
| 92 | int ii; |
| 93 | for(ii=0; ii<nCol; ii++){ |
| 94 | sqliteFree(aCol[ii]); |
| 95 | } |
| 96 | sqliteFree(aCol); |
| 97 | } |
| 98 | return rc; |
| 99 | } |
| 100 | |
| 101 | static int getIndexArray(sqlite3 *db, const char *zTab, int **paIndex){ |
| 102 | char zBuf[1024]; |
| 103 | sqlite3_stmt *pStmt = 0; |
| 104 | int nCol; |
| 105 | int *aIndex = 0; |
| 106 | int rc; |
| 107 | |
| 108 | sprintf(zBuf, "SELECT * FROM %s", zTab); |
| 109 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0); |
| 110 | nCol = sqlite3_column_count(pStmt); |
| 111 | |
| 112 | sqlite3_finalize(pStmt); |
| 113 | pStmt = 0; |
| 114 | if( rc!=SQLITE_OK ){ |
| 115 | goto get_index_array_out; |
| 116 | } |
| 117 | |
| 118 | aIndex = (int *)sqliteMalloc(sizeof(int) * nCol); |
| 119 | if( !aIndex ){ |
| 120 | rc = SQLITE_NOMEM; |
| 121 | goto get_index_array_out; |
| 122 | } |
| 123 | |
| 124 | sprintf(zBuf, "PRAGMA index_list(%s)", zTab); |
| 125 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0); |
| 126 | |
| 127 | while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 128 | sqlite3_stmt *pStmt2 = 0; |
| 129 | sprintf(zBuf, "PRAGMA index_info(%s)", sqlite3_column_text(pStmt, 1)); |
| 130 | rc = sqlite3_prepare(db, zBuf, -1, &pStmt2, 0); |
| 131 | if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){ |
| 132 | int cid = sqlite3_column_int(pStmt2, 1); |
| 133 | assert( cid>=0 && cid<nCol ); |
| 134 | aIndex[cid] = 1; |
| 135 | } |
| 136 | rc = sqlite3_finalize(pStmt2); |
| 137 | if( rc!=SQLITE_OK ){ |
| 138 | sqlite3_finalize(pStmt); |
| 139 | goto get_index_array_out; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | rc = sqlite3_finalize(pStmt); |
| 144 | |
| 145 | get_index_array_out: |
| 146 | if( rc!=SQLITE_OK ){ |
| 147 | sqliteFree(aIndex); |
| 148 | aIndex = 0; |
| 149 | } |
| 150 | *paIndex = aIndex; |
| 151 | return rc; |
| 152 | } |
| 153 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 154 | /* |
| 155 | ** Global Tcl variable $echo_module is a list. This routine appends |
| 156 | ** the string element zArg to that list in interpreter interp. |
| 157 | */ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 158 | static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 159 | int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 160 | Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 161 | } |
| 162 | |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 163 | /* |
| 164 | ** This function is called from within the echo-modules xCreate and |
| 165 | ** xConnect methods. The argc and argv arguments are copies of those |
| 166 | ** passed to the calling method. This function is responsible for |
| 167 | ** calling sqlite3_declare_vtab() to declare the schema of the virtual |
| 168 | ** table being created or connected. |
| 169 | ** |
| 170 | ** If the constructor was passed just one argument, i.e.: |
| 171 | ** |
| 172 | ** CREATE TABLE t1 AS echo(t2); |
| 173 | ** |
| 174 | ** Then t2 is assumed to be the name of a *real* database table. The |
| 175 | ** schema of the virtual table is declared by passing a copy of the |
| 176 | ** CREATE TABLE statement for the real table to sqlite3_declare_vtab(). |
| 177 | ** Hence, the virtual table should have exactly the same column names and |
| 178 | ** types as the real table. |
| 179 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 180 | static int echoDeclareVtab( |
| 181 | echo_vtab *pVtab, |
| 182 | sqlite3 *db, |
| 183 | int argc, |
| 184 | char **argv |
| 185 | ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 186 | int rc = SQLITE_OK; |
| 187 | |
| 188 | if( argc==2 ){ |
| 189 | sqlite3_stmt *pStmt = 0; |
| 190 | sqlite3_prepare(db, |
| 191 | "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", |
| 192 | -1, &pStmt, 0); |
| 193 | sqlite3_bind_text(pStmt, 1, argv[1], -1, 0); |
| 194 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 195 | const char *zCreateTable = sqlite3_column_text(pStmt, 0); |
drh | a75803d | 2006-06-12 12:50:23 +0000 | [diff] [blame] | 196 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 197 | sqlite3_declare_vtab(db, zCreateTable); |
drh | a75803d | 2006-06-12 12:50:23 +0000 | [diff] [blame] | 198 | #endif |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 199 | } else { |
| 200 | rc = SQLITE_ERROR; |
| 201 | } |
| 202 | sqlite3_finalize(pStmt); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 203 | if( rc==SQLITE_OK ){ |
| 204 | rc = getIndexArray(db, argv[1], &pVtab->aIndex); |
| 205 | } |
| 206 | if( rc==SQLITE_OK ){ |
| 207 | rc = getColumnNames(db, argv[1], &pVtab->aCol, &pVtab->nCol); |
| 208 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | return rc; |
| 212 | } |
| 213 | |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 214 | static int echoDestructor(sqlite3_vtab *pVtab){ |
| 215 | int ii; |
| 216 | echo_vtab *p = (echo_vtab*)pVtab; |
| 217 | sqliteFree(p->aIndex); |
| 218 | for(ii=0; ii<p->nCol; ii++){ |
| 219 | sqliteFree(p->aCol[ii]); |
| 220 | } |
| 221 | sqliteFree(p->aCol); |
| 222 | sqliteFree(p->zTableName); |
| 223 | sqliteFree(p); |
| 224 | return 0; |
| 225 | } |
| 226 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 227 | static int echoConstructor( |
| 228 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 229 | void *pAux, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 230 | int argc, char **argv, |
| 231 | sqlite3_vtab **ppVtab |
| 232 | ){ |
| 233 | int i; |
| 234 | echo_vtab *pVtab; |
| 235 | |
| 236 | pVtab = sqliteMalloc( sizeof(*pVtab) ); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 237 | pVtab->interp = (Tcl_Interp *)pAux; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 238 | pVtab->db = db; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 239 | pVtab->zTableName = sqlite3MPrintf("%s", argv[1]); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 240 | for(i=0; i<argc; i++){ |
| 241 | appendToEchoModule(pVtab->interp, argv[i]); |
| 242 | } |
| 243 | |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 244 | if( echoDeclareVtab(pVtab, db, argc, argv) ){ |
| 245 | echoDestructor((sqlite3_vtab *)pVtab); |
| 246 | return SQLITE_ERROR; |
| 247 | } |
| 248 | |
| 249 | *ppVtab = &pVtab->base; |
| 250 | return SQLITE_OK; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 251 | } |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 252 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 253 | /* Methods for the echo module */ |
| 254 | static int echoCreate( |
| 255 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 256 | void *pAux, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 257 | int argc, char **argv, |
| 258 | sqlite3_vtab **ppVtab |
| 259 | ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 260 | appendToEchoModule((Tcl_Interp *)(pAux), "xCreate"); |
| 261 | return echoConstructor(db, pAux, argc, argv, ppVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 262 | } |
| 263 | static int echoConnect( |
| 264 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 265 | void *pAux, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 266 | int argc, char **argv, |
| 267 | sqlite3_vtab **ppVtab |
| 268 | ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 269 | appendToEchoModule((Tcl_Interp *)(pAux), "xConnect"); |
| 270 | return echoConstructor(db, pAux, argc, argv, ppVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 271 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 272 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 273 | static int echoDisconnect(sqlite3_vtab *pVtab){ |
| 274 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect"); |
| 275 | return echoDestructor(pVtab); |
| 276 | } |
| 277 | static int echoDestroy(sqlite3_vtab *pVtab){ |
| 278 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy"); |
| 279 | return echoDestructor(pVtab); |
| 280 | } |
| 281 | |
| 282 | static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 283 | echo_cursor *pCur; |
| 284 | pCur = sqliteMalloc(sizeof(echo_cursor)); |
| 285 | *ppCursor = (sqlite3_vtab_cursor *)pCur; |
| 286 | return SQLITE_OK; |
| 287 | } |
| 288 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 289 | static int echoClose(sqlite3_vtab_cursor *cur){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 290 | echo_cursor *pCur = (echo_cursor *)cur; |
| 291 | sqlite3_finalize(pCur->pStmt); |
| 292 | sqliteFree(pCur); |
| 293 | return SQLITE_OK; |
| 294 | } |
| 295 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 296 | static int echoNext(sqlite3_vtab_cursor *cur){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 297 | int rc; |
| 298 | echo_cursor *pCur = (echo_cursor *)cur; |
| 299 | |
| 300 | rc = sqlite3_step(pCur->pStmt); |
| 301 | |
| 302 | if( rc==SQLITE_ROW ){ |
| 303 | rc = 1; |
| 304 | } else { |
| 305 | pCur->errcode = sqlite3_finalize(pCur->pStmt); |
| 306 | pCur->pStmt = 0; |
| 307 | rc = 0; |
| 308 | } |
| 309 | |
| 310 | return rc; |
| 311 | } |
| 312 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 313 | static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 314 | int iCol = i + 1; |
| 315 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 316 | |
| 317 | assert( sqlite3_data_count(pStmt)>iCol ); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 318 | sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol)); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 319 | return SQLITE_OK; |
| 320 | } |
| 321 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 322 | static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 323 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 324 | *pRowid = sqlite3_column_int64(pStmt, 0); |
| 325 | return SQLITE_OK; |
| 326 | } |
| 327 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 328 | /* |
| 329 | ** Compute a simple hash of the null terminated string zString. |
| 330 | ** |
| 331 | ** This module uses only sqlite3_index_info.idxStr, not |
| 332 | ** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set |
| 333 | ** in echoBestIndex(), idxNum is set to the corresponding hash value. |
| 334 | ** In echoFilter(), code assert()s that the supplied idxNum value is |
| 335 | ** indeed the hash of the supplied idxStr. |
| 336 | */ |
| 337 | static int hashString(const char *zString){ |
| 338 | int val = 0; |
| 339 | int ii; |
| 340 | for(ii=0; zString[ii]; ii++){ |
| 341 | val = (val << 3) + (int)zString[ii]; |
| 342 | } |
| 343 | return val; |
| 344 | } |
| 345 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 346 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 347 | static int echoFilter( |
| 348 | sqlite3_vtab_cursor *pVtabCursor, |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 349 | int idxNum, const char *idxStr, |
| 350 | int argc, sqlite3_value **argv |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 351 | ){ |
| 352 | int rc; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 353 | int i; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 354 | |
| 355 | echo_cursor *pCur = (echo_cursor *)pVtabCursor; |
| 356 | echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab; |
| 357 | sqlite3 *db = pVtab->db; |
| 358 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 359 | assert( idxNum==hashString(idxStr) ); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 360 | sqlite3_finalize(pCur->pStmt); |
| 361 | pCur->pStmt = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 362 | rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0); |
| 363 | for(i=0; i<argc; i++){ |
| 364 | switch( sqlite3_value_type(argv[i]) ){ |
| 365 | case SQLITE_INTEGER: { |
| 366 | sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i])); |
| 367 | break; |
| 368 | } |
| 369 | case SQLITE_FLOAT: { |
| 370 | sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i])); |
| 371 | break; |
| 372 | } |
| 373 | case SQLITE_NULL: { |
| 374 | sqlite3_bind_null(pCur->pStmt, i+1); |
| 375 | break; |
| 376 | } |
| 377 | case SQLITE_TEXT: { |
| 378 | sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]), |
| 379 | sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT); |
| 380 | break; |
| 381 | } |
| 382 | case SQLITE_BLOB: { |
| 383 | sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]), |
| 384 | sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT); |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 389 | if( rc==SQLITE_OK ){ |
| 390 | rc = echoNext(pVtabCursor); |
| 391 | } |
| 392 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 393 | appendToEchoModule(pVtab->interp, "xFilter"); |
| 394 | appendToEchoModule(pVtab->interp, idxStr); |
| 395 | for(i=0; i<argc; i++){ |
| 396 | appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i])); |
| 397 | } |
| 398 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 399 | return rc; |
| 400 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 401 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 402 | /* |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 403 | ** The echo module implements the subset of query constraints and sort |
| 404 | ** orders that may take advantage of SQLite indices on the underlying |
| 405 | ** real table. For example, if the real table is declared as: |
| 406 | ** |
| 407 | ** CREATE TABLE real(a, b, c); |
| 408 | ** CREATE INDEX real_index ON real(b); |
| 409 | ** |
| 410 | ** then the echo module handles WHERE or ORDER BY clauses that refer |
| 411 | ** to the column "b", but not "a" or "c". If a multi-column index is |
| 412 | ** present, only it's left most column is considered. |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 413 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 414 | static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 415 | int ii; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 416 | char *zQuery = 0; |
| 417 | char *zNew; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 418 | int nArg = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 419 | const char *zSep = "WHERE"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 420 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 421 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 422 | zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 423 | for(ii=0; ii<pIdxInfo->nConstraint; ii++){ |
| 424 | const struct sqlite3_index_constraint *pConstraint; |
| 425 | struct sqlite3_index_constraint_usage *pUsage; |
| 426 | |
| 427 | pConstraint = &pIdxInfo->aConstraint[ii]; |
| 428 | pUsage = &pIdxInfo->aConstraintUsage[ii]; |
| 429 | |
| 430 | int iCol = pConstraint->iColumn; |
| 431 | if( pVtab->aIndex[iCol] ){ |
| 432 | char *zCol = pVtab->aCol[iCol]; |
| 433 | char *zOp = 0; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 434 | if( iCol<0 ){ |
| 435 | zCol = "rowid"; |
| 436 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 437 | switch( pConstraint->op ){ |
| 438 | case SQLITE_INDEX_CONSTRAINT_EQ: |
| 439 | zOp = "="; break; |
| 440 | case SQLITE_INDEX_CONSTRAINT_LT: |
| 441 | zOp = "<"; break; |
| 442 | case SQLITE_INDEX_CONSTRAINT_GT: |
| 443 | zOp = ">"; break; |
| 444 | case SQLITE_INDEX_CONSTRAINT_LE: |
| 445 | zOp = "<="; break; |
| 446 | case SQLITE_INDEX_CONSTRAINT_GE: |
| 447 | zOp = ">="; break; |
| 448 | case SQLITE_INDEX_CONSTRAINT_MATCH: |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 449 | zOp = "LIKE"; break; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 450 | } |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 451 | if( zOp[0]=='L' ){ |
| 452 | zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')", |
| 453 | zQuery, zSep, zCol); |
| 454 | } else { |
| 455 | zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp); |
| 456 | } |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 457 | sqlite3_free(zQuery); |
| 458 | zQuery = zNew; |
| 459 | zSep = "AND"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 460 | pUsage->argvIndex = ++nArg; |
| 461 | pUsage->omit = 1; |
| 462 | } |
| 463 | } |
danielk1977 | 47d0809 | 2006-06-14 07:41:31 +0000 | [diff] [blame] | 464 | |
| 465 | /* If there is only one term in the ORDER BY clause, and it is |
| 466 | ** on a column that this virtual table has an index for, then consume |
| 467 | ** the ORDER BY clause. |
| 468 | */ |
| 469 | if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){ |
| 470 | char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn]; |
| 471 | char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC"; |
| 472 | zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir); |
| 473 | sqlite3_free(zQuery); |
| 474 | zQuery = zNew; |
| 475 | pIdxInfo->orderByConsumed = 1; |
| 476 | } |
| 477 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 478 | appendToEchoModule(pVtab->interp, "xBestIndex");; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 479 | appendToEchoModule(pVtab->interp, zQuery); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 480 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 481 | pIdxInfo->idxNum = hashString(zQuery); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 482 | pIdxInfo->idxStr = zQuery; |
| 483 | pIdxInfo->needToFreeIdxStr = 1; |
| 484 | pIdxInfo->estimatedCost = 1.0; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 485 | return SQLITE_OK; |
| 486 | } |
| 487 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 488 | static void string_concat(char **pzStr, char *zAppend, int doFree){ |
| 489 | char *zIn = *pzStr; |
| 490 | if( zIn ){ |
| 491 | char *zTemp = zIn; |
| 492 | zIn = sqlite3_mprintf("%s%s", zIn, zAppend); |
| 493 | sqlite3_free(zTemp); |
| 494 | }else{ |
| 495 | zIn = sqlite3_mprintf("%s", zAppend); |
| 496 | } |
| 497 | *pzStr = zIn; |
| 498 | if( doFree ){ |
| 499 | sqlite3_free(zAppend); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | ** apData[0] apData[1] apData[2..] |
| 505 | ** |
| 506 | ** INTEGER DELETE |
| 507 | ** |
| 508 | ** INTEGER NULL (nCol args) UPDATE (do not set rowid) |
| 509 | ** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>) |
| 510 | ** |
| 511 | ** NULL NULL (nCol args) INSERT INTO (automatic rowid value) |
| 512 | ** NULL INTEGER (nCol args) INSERT (incl. rowid value) |
| 513 | ** |
| 514 | */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 515 | int echoUpdate(sqlite3_vtab *tab, int nData, sqlite3_value **apData){ |
| 516 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 517 | sqlite3 *db = pVtab->db; |
| 518 | int rc = SQLITE_OK; |
| 519 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 520 | sqlite3_stmt *pStmt; |
| 521 | char *z = 0; /* SQL statement to execute */ |
| 522 | int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */ |
| 523 | int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */ |
| 524 | int i; /* Counter variable used by for loops */ |
| 525 | |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 526 | assert( nData==pVtab->nCol+2 || nData==1 ); |
| 527 | |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 528 | /* If apData[0] is an integer and nData>1 then do an UPDATE */ |
| 529 | if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 530 | z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 531 | char *zSep = " SET"; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 532 | |
| 533 | bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER); |
| 534 | bindArgZero = 1; |
| 535 | |
| 536 | if( bindArgOne ){ |
| 537 | string_concat(&z, " SET rowid=?1 ", 0); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 538 | zSep = ","; |
| 539 | } |
| 540 | for(i=2; i<nData; i++){ |
| 541 | if( apData[i]==0 ) continue; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 542 | string_concat(&z, sqlite3_mprintf( |
| 543 | "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 544 | zSep = ","; |
| 545 | } |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 546 | string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 547 | } |
| 548 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 549 | /* If apData[0] is an integer and nData==1 then do a DELETE */ |
| 550 | else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
| 551 | z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName); |
| 552 | bindArgZero = 1; |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 553 | } |
| 554 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 555 | /* If the first argument is NULL and there are more than two args, INSERT */ |
| 556 | else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 557 | int ii; |
| 558 | char *zInsert = 0; |
| 559 | char *zValues = 0; |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 560 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 561 | zInsert = sqlite3_mprintf("INSERT OR REPLACE INTO %Q (", pVtab->zTableName); |
| 562 | if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){ |
| 563 | bindArgOne = 1; |
| 564 | zValues = sqlite3_mprintf("?"); |
| 565 | string_concat(&zInsert, "rowid", 0); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 566 | } |
| 567 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 568 | assert((pVtab->nCol+2)==nData); |
| 569 | for(ii=2; ii<nData; ii++){ |
| 570 | string_concat(&zInsert, |
| 571 | sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1); |
| 572 | string_concat(&zValues, |
| 573 | sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 574 | } |
| 575 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 576 | string_concat(&z, zInsert, 1); |
| 577 | string_concat(&z, ") VALUES(", 0); |
| 578 | string_concat(&z, zValues, 1); |
| 579 | string_concat(&z, ")", 0); |
| 580 | } |
| 581 | |
| 582 | /* Anything else is an error */ |
| 583 | else{ |
| 584 | assert(0); |
| 585 | return SQLITE_ERROR; |
| 586 | } |
| 587 | |
| 588 | rc = sqlite3_prepare(db, z, -1, &pStmt, 0); |
| 589 | assert( rc!=SQLITE_OK || pStmt ); |
| 590 | sqlite3_free(z); |
| 591 | if( rc==SQLITE_OK ) { |
| 592 | if( bindArgZero ){ |
| 593 | sqlite3_bind_value(pStmt, nData, apData[0]); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 594 | } |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 595 | if( bindArgOne ){ |
| 596 | sqlite3_bind_value(pStmt, 1, apData[1]); |
| 597 | } |
| 598 | for(i=2; i<nData; i++){ |
| 599 | if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]); |
| 600 | } |
| 601 | sqlite3_step(pStmt); |
| 602 | rc = sqlite3_finalize(pStmt); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | return rc; |
| 606 | } |
| 607 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 608 | /* |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 609 | ** A virtual table module that merely echos method calls into TCL |
| 610 | ** variables. |
| 611 | */ |
| 612 | static sqlite3_module echoModule = { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 613 | 0, /* iVersion */ |
| 614 | "echo", /* zName */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 615 | echoCreate, |
| 616 | echoConnect, |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 617 | echoBestIndex, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 618 | echoDisconnect, |
| 619 | echoDestroy, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 620 | echoOpen, /* xOpen - open a cursor */ |
| 621 | echoClose, /* xClose - close a cursor */ |
| 622 | echoFilter, /* xFilter - configure scan constraints */ |
| 623 | echoNext, /* xNext - advance a cursor */ |
| 624 | echoColumn, /* xColumn - read data */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 625 | echoRowid, /* xRowid - read data */ |
| 626 | echoUpdate /* xUpdate - write data */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
| 629 | /* |
| 630 | ** Decode a pointer to an sqlite3 object. |
| 631 | */ |
| 632 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 633 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 634 | return TCL_OK; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /* |
| 639 | ** Register the echo virtual table module. |
| 640 | */ |
| 641 | static int register_echo_module( |
| 642 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 643 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 644 | int objc, /* Number of arguments */ |
| 645 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 646 | ){ |
| 647 | sqlite3 *db; |
| 648 | if( objc!=2 ){ |
| 649 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 650 | return TCL_ERROR; |
| 651 | } |
| 652 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 653 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 654 | sqlite3_create_module(db, "echo", &echoModule, (void *)interp); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 655 | #endif |
| 656 | return TCL_OK; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | /* |
| 661 | ** Register commands with the TCL interpreter. |
| 662 | */ |
| 663 | int Sqlitetest8_Init(Tcl_Interp *interp){ |
| 664 | static struct { |
| 665 | char *zName; |
| 666 | Tcl_ObjCmdProc *xProc; |
| 667 | void *clientData; |
| 668 | } aObjCmd[] = { |
| 669 | { "register_echo_module", register_echo_module, 0 }, |
| 670 | }; |
| 671 | int i; |
| 672 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 673 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 674 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 675 | } |
| 676 | return TCL_OK; |
| 677 | } |