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