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