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 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame^] | 16 | ** $Id: test8.c,v 1.21 2006/06/15 07:29:01 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 | |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 39 | char *zTableName; /* Name of the real table */ |
| 40 | int nCol; /* Number of columns in the real table */ |
| 41 | int *aIndex; /* Array of size nCol. True if column has an index */ |
| 42 | char **aCol; /* Array of size nCol. Column names */ |
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 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 200 | if( rc==SQLITE_OK ){ |
| 201 | rc = getIndexArray(db, argv[1], &pVtab->aIndex); |
| 202 | } |
| 203 | if( rc==SQLITE_OK ){ |
| 204 | rc = getColumnNames(db, argv[1], &pVtab->aCol, &pVtab->nCol); |
| 205 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return rc; |
| 209 | } |
| 210 | |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 211 | static int echoDestructor(sqlite3_vtab *pVtab){ |
| 212 | int ii; |
| 213 | echo_vtab *p = (echo_vtab*)pVtab; |
| 214 | sqliteFree(p->aIndex); |
| 215 | for(ii=0; ii<p->nCol; ii++){ |
| 216 | sqliteFree(p->aCol[ii]); |
| 217 | } |
| 218 | sqliteFree(p->aCol); |
| 219 | sqliteFree(p->zTableName); |
| 220 | sqliteFree(p); |
| 221 | return 0; |
| 222 | } |
| 223 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 224 | static int echoConstructor( |
| 225 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 226 | void *pAux, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 227 | int argc, char **argv, |
| 228 | sqlite3_vtab **ppVtab |
| 229 | ){ |
| 230 | int i; |
| 231 | echo_vtab *pVtab; |
| 232 | |
| 233 | pVtab = sqliteMalloc( sizeof(*pVtab) ); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 234 | pVtab->interp = (Tcl_Interp *)pAux; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 235 | pVtab->db = db; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 236 | pVtab->zTableName = sqlite3MPrintf("%s", argv[1]); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 237 | for(i=0; i<argc; i++){ |
| 238 | appendToEchoModule(pVtab->interp, argv[i]); |
| 239 | } |
| 240 | |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 241 | if( echoDeclareVtab(pVtab, db, argc, argv) ){ |
| 242 | echoDestructor((sqlite3_vtab *)pVtab); |
| 243 | return SQLITE_ERROR; |
| 244 | } |
| 245 | |
| 246 | *ppVtab = &pVtab->base; |
| 247 | return SQLITE_OK; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 248 | } |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 249 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 250 | /* Methods for the echo module */ |
| 251 | static int echoCreate( |
| 252 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 253 | void *pAux, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 254 | int argc, char **argv, |
| 255 | sqlite3_vtab **ppVtab |
| 256 | ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 257 | appendToEchoModule((Tcl_Interp *)(pAux), "xCreate"); |
| 258 | return echoConstructor(db, pAux, argc, argv, ppVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 259 | } |
| 260 | static int echoConnect( |
| 261 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 262 | void *pAux, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 263 | int argc, char **argv, |
| 264 | sqlite3_vtab **ppVtab |
| 265 | ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 266 | appendToEchoModule((Tcl_Interp *)(pAux), "xConnect"); |
| 267 | return echoConstructor(db, pAux, argc, argv, ppVtab); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 268 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 269 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 270 | static int echoDisconnect(sqlite3_vtab *pVtab){ |
| 271 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect"); |
| 272 | return echoDestructor(pVtab); |
| 273 | } |
| 274 | static int echoDestroy(sqlite3_vtab *pVtab){ |
| 275 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy"); |
| 276 | return echoDestructor(pVtab); |
| 277 | } |
| 278 | |
| 279 | static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 280 | echo_cursor *pCur; |
| 281 | pCur = sqliteMalloc(sizeof(echo_cursor)); |
| 282 | *ppCursor = (sqlite3_vtab_cursor *)pCur; |
| 283 | return SQLITE_OK; |
| 284 | } |
| 285 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 286 | static int echoClose(sqlite3_vtab_cursor *cur){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 287 | echo_cursor *pCur = (echo_cursor *)cur; |
| 288 | sqlite3_finalize(pCur->pStmt); |
| 289 | sqliteFree(pCur); |
| 290 | return SQLITE_OK; |
| 291 | } |
| 292 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 293 | static int echoNext(sqlite3_vtab_cursor *cur){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 294 | int rc; |
| 295 | echo_cursor *pCur = (echo_cursor *)cur; |
| 296 | |
| 297 | rc = sqlite3_step(pCur->pStmt); |
| 298 | |
| 299 | if( rc==SQLITE_ROW ){ |
| 300 | rc = 1; |
| 301 | } else { |
| 302 | pCur->errcode = sqlite3_finalize(pCur->pStmt); |
| 303 | pCur->pStmt = 0; |
| 304 | rc = 0; |
| 305 | } |
| 306 | |
| 307 | return rc; |
| 308 | } |
| 309 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 310 | static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 311 | int iCol = i + 1; |
| 312 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 313 | |
| 314 | assert( sqlite3_data_count(pStmt)>iCol ); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 315 | sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol)); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 316 | return SQLITE_OK; |
| 317 | } |
| 318 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 319 | static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 320 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 321 | *pRowid = sqlite3_column_int64(pStmt, 0); |
| 322 | return SQLITE_OK; |
| 323 | } |
| 324 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 325 | /* |
| 326 | ** Compute a simple hash of the null terminated string zString. |
| 327 | ** |
| 328 | ** This module uses only sqlite3_index_info.idxStr, not |
| 329 | ** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set |
| 330 | ** in echoBestIndex(), idxNum is set to the corresponding hash value. |
| 331 | ** In echoFilter(), code assert()s that the supplied idxNum value is |
| 332 | ** indeed the hash of the supplied idxStr. |
| 333 | */ |
| 334 | static int hashString(const char *zString){ |
| 335 | int val = 0; |
| 336 | int ii; |
| 337 | for(ii=0; zString[ii]; ii++){ |
| 338 | val = (val << 3) + (int)zString[ii]; |
| 339 | } |
| 340 | return val; |
| 341 | } |
| 342 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 343 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 344 | static int echoFilter( |
| 345 | sqlite3_vtab_cursor *pVtabCursor, |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 346 | int idxNum, const char *idxStr, |
| 347 | int argc, sqlite3_value **argv |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 348 | ){ |
| 349 | int rc; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 350 | int i; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 351 | |
| 352 | echo_cursor *pCur = (echo_cursor *)pVtabCursor; |
| 353 | echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab; |
| 354 | sqlite3 *db = pVtab->db; |
| 355 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 356 | assert( idxNum==hashString(idxStr) ); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 357 | sqlite3_finalize(pCur->pStmt); |
| 358 | pCur->pStmt = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 359 | rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0); |
| 360 | for(i=0; i<argc; i++){ |
| 361 | switch( sqlite3_value_type(argv[i]) ){ |
| 362 | case SQLITE_INTEGER: { |
| 363 | sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i])); |
| 364 | break; |
| 365 | } |
| 366 | case SQLITE_FLOAT: { |
| 367 | sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i])); |
| 368 | break; |
| 369 | } |
| 370 | case SQLITE_NULL: { |
| 371 | sqlite3_bind_null(pCur->pStmt, i+1); |
| 372 | break; |
| 373 | } |
| 374 | case SQLITE_TEXT: { |
| 375 | sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]), |
| 376 | sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT); |
| 377 | break; |
| 378 | } |
| 379 | case SQLITE_BLOB: { |
| 380 | sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]), |
| 381 | sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT); |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 386 | if( rc==SQLITE_OK ){ |
| 387 | rc = echoNext(pVtabCursor); |
| 388 | } |
| 389 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 390 | appendToEchoModule(pVtab->interp, "xFilter"); |
| 391 | appendToEchoModule(pVtab->interp, idxStr); |
| 392 | for(i=0; i<argc; i++){ |
| 393 | appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i])); |
| 394 | } |
| 395 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 396 | return rc; |
| 397 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 398 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 399 | /* |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 400 | ** The echo module implements the subset of query constraints and sort |
| 401 | ** orders that may take advantage of SQLite indices on the underlying |
| 402 | ** real table. For example, if the real table is declared as: |
| 403 | ** |
| 404 | ** CREATE TABLE real(a, b, c); |
| 405 | ** CREATE INDEX real_index ON real(b); |
| 406 | ** |
| 407 | ** then the echo module handles WHERE or ORDER BY clauses that refer |
| 408 | ** to the column "b", but not "a" or "c". If a multi-column index is |
| 409 | ** present, only it's left most column is considered. |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 410 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 411 | static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 412 | int ii; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 413 | char *zQuery = 0; |
| 414 | char *zNew; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 415 | int nArg = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 416 | const char *zSep = "WHERE"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 417 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 418 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 419 | zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 420 | for(ii=0; ii<pIdxInfo->nConstraint; ii++){ |
| 421 | const struct sqlite3_index_constraint *pConstraint; |
| 422 | struct sqlite3_index_constraint_usage *pUsage; |
| 423 | |
| 424 | pConstraint = &pIdxInfo->aConstraint[ii]; |
| 425 | pUsage = &pIdxInfo->aConstraintUsage[ii]; |
| 426 | |
| 427 | int iCol = pConstraint->iColumn; |
| 428 | if( pVtab->aIndex[iCol] ){ |
| 429 | char *zCol = pVtab->aCol[iCol]; |
| 430 | char *zOp = 0; |
| 431 | switch( pConstraint->op ){ |
| 432 | case SQLITE_INDEX_CONSTRAINT_EQ: |
| 433 | zOp = "="; break; |
| 434 | case SQLITE_INDEX_CONSTRAINT_LT: |
| 435 | zOp = "<"; break; |
| 436 | case SQLITE_INDEX_CONSTRAINT_GT: |
| 437 | zOp = ">"; break; |
| 438 | case SQLITE_INDEX_CONSTRAINT_LE: |
| 439 | zOp = "<="; break; |
| 440 | case SQLITE_INDEX_CONSTRAINT_GE: |
| 441 | zOp = ">="; break; |
| 442 | case SQLITE_INDEX_CONSTRAINT_MATCH: |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 443 | zOp = "LIKE"; break; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 444 | } |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 445 | if( zOp[0]=='L' ){ |
| 446 | zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')", |
| 447 | zQuery, zSep, zCol); |
| 448 | } else { |
| 449 | zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp); |
| 450 | } |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 451 | sqlite3_free(zQuery); |
| 452 | zQuery = zNew; |
| 453 | zSep = "AND"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 454 | pUsage->argvIndex = ++nArg; |
| 455 | pUsage->omit = 1; |
| 456 | } |
| 457 | } |
danielk1977 | 47d0809 | 2006-06-14 07:41:31 +0000 | [diff] [blame] | 458 | |
| 459 | /* If there is only one term in the ORDER BY clause, and it is |
| 460 | ** on a column that this virtual table has an index for, then consume |
| 461 | ** the ORDER BY clause. |
| 462 | */ |
| 463 | if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){ |
| 464 | char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn]; |
| 465 | char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC"; |
| 466 | zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir); |
| 467 | sqlite3_free(zQuery); |
| 468 | zQuery = zNew; |
| 469 | pIdxInfo->orderByConsumed = 1; |
| 470 | } |
| 471 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 472 | appendToEchoModule(pVtab->interp, "xBestIndex");; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 473 | appendToEchoModule(pVtab->interp, zQuery); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 474 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 475 | pIdxInfo->idxNum = hashString(zQuery); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 476 | pIdxInfo->idxStr = zQuery; |
| 477 | pIdxInfo->needToFreeIdxStr = 1; |
| 478 | pIdxInfo->estimatedCost = 1.0; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 479 | return SQLITE_OK; |
| 480 | } |
| 481 | |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 482 | int echoUpdate(sqlite3_vtab *tab, int nData, sqlite3_value **apData){ |
| 483 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 484 | sqlite3 *db = pVtab->db; |
| 485 | int rc = SQLITE_OK; |
| 486 | |
| 487 | assert( nData==pVtab->nCol+2 || nData==1 ); |
| 488 | |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 489 | /* If apData[0] is an integer and nData>1 then do an UPDATE */ |
| 490 | if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
| 491 | char *zUpdate = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName); |
| 492 | char *zSep = " SET"; |
| 493 | char *zTemp; |
| 494 | int i, j; |
| 495 | sqlite3_stmt *pStmt; |
| 496 | |
| 497 | if( apData[1] && sqlite3_value_type(apData[1]) && |
| 498 | sqlite3_value_int64(apData[0])!=sqlite3_value_int64(apData[1]) ){ |
| 499 | zTemp = sqlite3_mprintf("%s SET rowid=%lld", zUpdate, zSep, |
| 500 | sqlite3_value_int64(apData[1])); |
| 501 | sqlite3_free(zUpdate); |
| 502 | zUpdate = zTemp; |
| 503 | zSep = ","; |
| 504 | } |
| 505 | for(i=2; i<nData; i++){ |
| 506 | if( apData[i]==0 ) continue; |
| 507 | zTemp = sqlite3_mprintf("%s%s %Q=?", zUpdate, zSep, pVtab->aCol[i-2]); |
| 508 | sqlite3_free(zUpdate); |
| 509 | zUpdate = zTemp; |
| 510 | zSep = ","; |
| 511 | } |
| 512 | zTemp = sqlite3_mprintf("%s WHERE rowid=%lld", zUpdate, |
| 513 | sqlite3_value_int64(apData[0])); |
| 514 | sqlite3_free(zUpdate); |
| 515 | zUpdate = zTemp; |
| 516 | rc = sqlite3_prepare(db, zUpdate, -1, &pStmt, 0); |
| 517 | assert( rc!=SQLITE_OK || pStmt ); |
| 518 | if( rc ) return rc; |
| 519 | for(i=2, j=1; i<nData; i++){ |
| 520 | if( apData[i]==0 ) continue; |
| 521 | switch( sqlite3_value_type(apData[i]) ){ |
| 522 | case SQLITE_INTEGER: { |
| 523 | sqlite3_bind_int64(pStmt, j, sqlite3_value_int64(apData[i])); |
| 524 | break; |
| 525 | } |
| 526 | case SQLITE_FLOAT: { |
| 527 | sqlite3_bind_double(pStmt, j, sqlite3_value_double(apData[i])); |
| 528 | break; |
| 529 | } |
| 530 | case SQLITE_NULL: { |
| 531 | sqlite3_bind_null(pStmt, j); |
| 532 | break; |
| 533 | } |
| 534 | case SQLITE_TEXT: { |
| 535 | sqlite3_bind_text(pStmt, j, sqlite3_value_text(apData[i]), |
| 536 | sqlite3_value_bytes(apData[i]), SQLITE_TRANSIENT); |
| 537 | break; |
| 538 | } |
| 539 | case SQLITE_BLOB: { |
| 540 | sqlite3_bind_blob(pStmt, j, sqlite3_value_blob(apData[i]), |
| 541 | sqlite3_value_bytes(apData[i]), SQLITE_TRANSIENT); |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | j++; |
| 546 | } |
| 547 | sqlite3_step(pStmt); |
| 548 | rc = sqlite3_finalize(pStmt); |
| 549 | return rc; |
| 550 | } |
| 551 | |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 552 | /* If apData[0] is an integer, delete the identified row */ |
| 553 | if( sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
| 554 | const char *zFormat = "DELETE FROM %Q WHERE rowid = ?"; |
| 555 | char *zDelete = sqlite3_mprintf(zFormat, pVtab->zTableName); |
| 556 | if( !zDelete ){ |
| 557 | rc = SQLITE_NOMEM; |
| 558 | }else{ |
| 559 | sqlite3_stmt *pStmt = 0; |
| 560 | rc = sqlite3_prepare(db, zDelete, -1, &pStmt, 0); |
| 561 | assert( rc!=SQLITE_OK || pStmt ); |
| 562 | if( rc==SQLITE_OK ){ |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame^] | 563 | sqlite3_bind_value(pStmt, 1, apData[0]); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 564 | sqlite3_step(pStmt); |
| 565 | rc = sqlite3_finalize(pStmt); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* If there is more than a single argument, INSERT a new row */ |
| 571 | if( rc==SQLITE_OK && nData>1 ){ |
| 572 | int ii; |
| 573 | char *zInsert = 0; |
| 574 | char *zValues = 0; |
| 575 | char *zQuery = 0; |
| 576 | const char *zTab = pVtab->zTableName; |
| 577 | |
| 578 | zInsert = sqlite3_mprintf("INSERT OR REPLACE INTO %Q (rowid", zTab); |
| 579 | zValues = sqlite3_mprintf("?"); |
| 580 | |
| 581 | for(ii=0; ii<pVtab->nCol && zInsert && zValues; ii++){ |
| 582 | char *zNew = sqlite3_mprintf("%s, %Q", zInsert, pVtab->aCol[ii]); |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame^] | 583 | sqlite3_free(zInsert); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 584 | zInsert = zNew; |
| 585 | |
| 586 | zNew = sqlite3_mprintf("%s, ?", zValues); |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame^] | 587 | sqlite3_free(zValues); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 588 | zValues = zNew; |
| 589 | } |
| 590 | |
| 591 | if( zInsert && zValues ){ |
| 592 | zQuery = sqlite3_mprintf("%s) VALUES(%s)", zInsert, zValues); |
| 593 | } |
| 594 | if( zQuery ){ |
| 595 | sqlite3_stmt *pStmt = 0; |
| 596 | rc = sqlite3_prepare(db, zQuery, -1, &pStmt, 0); |
| 597 | for(ii=1; rc==SQLITE_OK && ii<nData; ii++){ |
| 598 | rc = sqlite3_bind_value(pStmt, ii, apData[ii]); |
| 599 | } |
| 600 | if( rc==SQLITE_OK ){ |
| 601 | sqlite3_step(pStmt); |
| 602 | rc = sqlite3_finalize(pStmt); |
| 603 | }else{ |
| 604 | sqlite3_finalize(pStmt); |
| 605 | } |
| 606 | } |
| 607 | |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame^] | 608 | sqlite3_free(zValues); |
| 609 | sqlite3_free(zInsert); |
| 610 | sqlite3_free(zQuery); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 611 | if( rc==SQLITE_OK && (!zValues || !zInsert || !zQuery) ){ |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame^] | 612 | sqlite3FailedMalloc(); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 613 | rc = SQLITE_NOMEM; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return rc; |
| 618 | } |
| 619 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 620 | /* |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 621 | ** A virtual table module that merely echos method calls into TCL |
| 622 | ** variables. |
| 623 | */ |
| 624 | static sqlite3_module echoModule = { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 625 | 0, /* iVersion */ |
| 626 | "echo", /* zName */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 627 | echoCreate, |
| 628 | echoConnect, |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 629 | echoBestIndex, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 630 | echoDisconnect, |
| 631 | echoDestroy, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 632 | echoOpen, /* xOpen - open a cursor */ |
| 633 | echoClose, /* xClose - close a cursor */ |
| 634 | echoFilter, /* xFilter - configure scan constraints */ |
| 635 | echoNext, /* xNext - advance a cursor */ |
| 636 | echoColumn, /* xColumn - read data */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 637 | echoRowid, /* xRowid - read data */ |
| 638 | echoUpdate /* xUpdate - write data */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 639 | }; |
| 640 | |
| 641 | /* |
| 642 | ** Decode a pointer to an sqlite3 object. |
| 643 | */ |
| 644 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 645 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 646 | return TCL_OK; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | /* |
| 651 | ** Register the echo virtual table module. |
| 652 | */ |
| 653 | static int register_echo_module( |
| 654 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 655 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 656 | int objc, /* Number of arguments */ |
| 657 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 658 | ){ |
| 659 | sqlite3 *db; |
| 660 | if( objc!=2 ){ |
| 661 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 662 | return TCL_ERROR; |
| 663 | } |
| 664 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 665 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 666 | sqlite3_create_module(db, "echo", &echoModule, (void *)interp); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 667 | #endif |
| 668 | return TCL_OK; |
| 669 | } |
| 670 | |
| 671 | |
| 672 | /* |
| 673 | ** Register commands with the TCL interpreter. |
| 674 | */ |
| 675 | int Sqlitetest8_Init(Tcl_Interp *interp){ |
| 676 | static struct { |
| 677 | char *zName; |
| 678 | Tcl_ObjCmdProc *xProc; |
| 679 | void *clientData; |
| 680 | } aObjCmd[] = { |
| 681 | { "register_echo_module", register_echo_module, 0 }, |
| 682 | }; |
| 683 | int i; |
| 684 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 685 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 686 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 687 | } |
| 688 | return TCL_OK; |
| 689 | } |