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 | ** |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 16 | ** $Id: test8.c,v 1.53 2007/08/23 02:47:53 drh Exp $ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "tcl.h" |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 23 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 24 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 25 | typedef struct echo_vtab echo_vtab; |
| 26 | typedef struct echo_cursor echo_cursor; |
| 27 | |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 28 | /* |
danielk1977 | 780b1d9 | 2007-03-30 14:56:34 +0000 | [diff] [blame] | 29 | ** The test module defined in this file uses four global Tcl variables to |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 30 | ** commicate with test-scripts: |
| 31 | ** |
| 32 | ** $::echo_module |
| 33 | ** $::echo_module_sync_fail |
danielk1977 | 5017dc3 | 2006-06-24 09:34:22 +0000 | [diff] [blame] | 34 | ** $::echo_module_begin_fail |
danielk1977 | 780b1d9 | 2007-03-30 14:56:34 +0000 | [diff] [blame] | 35 | ** $::echo_module_cost |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 36 | ** |
| 37 | ** The variable ::echo_module is a list. Each time one of the following |
| 38 | ** methods is called, one or more elements are appended to the list. |
| 39 | ** This is used for automated testing of virtual table modules. |
| 40 | ** |
| 41 | ** The ::echo_module_sync_fail variable is set by test scripts and read |
| 42 | ** by code in this file. If it is set to the name of a real table in the |
| 43 | ** the database, then all xSync operations on echo virtual tables that |
| 44 | ** use the named table as a backing store will fail. |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 47 | /* |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 48 | ** An echo virtual-table object. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 49 | ** |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 50 | ** echo.vtab.aIndex is an array of booleans. The nth entry is true if |
| 51 | ** the nth column of the real table is the left-most column of an index |
| 52 | ** (implicit or otherwise). In other words, if SQLite can optimize |
| 53 | ** a query like "SELECT * FROM real_table WHERE col = ?". |
| 54 | ** |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 55 | ** Member variable aCol[] contains copies of the column names of the real |
| 56 | ** table. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 57 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 58 | struct echo_vtab { |
| 59 | sqlite3_vtab base; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 60 | Tcl_Interp *interp; /* Tcl interpreter containing debug variables */ |
| 61 | sqlite3 *db; /* Database connection */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 62 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 63 | int isPattern; |
| 64 | char *zThis; /* Name of the echo table */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 65 | char *zTableName; /* Name of the real table */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 66 | char *zLogName; /* Name of the log table */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 67 | int nCol; /* Number of columns in the real table */ |
| 68 | int *aIndex; /* Array of size nCol. True if column has an index */ |
| 69 | char **aCol; /* Array of size nCol. Column names */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /* An echo cursor object */ |
| 73 | struct echo_cursor { |
| 74 | sqlite3_vtab_cursor base; |
| 75 | sqlite3_stmt *pStmt; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 78 | /* |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 79 | ** Convert an SQL-style quoted string into a normal string by removing |
| 80 | ** the quote characters. The conversion is done in-place. If the |
| 81 | ** input does not begin with a quote character, then this routine |
| 82 | ** is a no-op. |
| 83 | ** |
| 84 | ** Examples: |
| 85 | ** |
| 86 | ** "abc" becomes abc |
| 87 | ** 'xyz' becomes xyz |
| 88 | ** [pqr] becomes pqr |
| 89 | ** `mno` becomes mno |
| 90 | */ |
| 91 | static void dequoteString(char *z){ |
| 92 | int quote; |
| 93 | int i, j; |
| 94 | if( z==0 ) return; |
| 95 | quote = z[0]; |
| 96 | switch( quote ){ |
| 97 | case '\'': break; |
| 98 | case '"': break; |
| 99 | case '`': break; /* For MySQL compatibility */ |
| 100 | case '[': quote = ']'; break; /* For MS SqlServer compatibility */ |
| 101 | default: return; |
| 102 | } |
| 103 | for(i=1, j=0; z[i]; i++){ |
| 104 | if( z[i]==quote ){ |
| 105 | if( z[i+1]==quote ){ |
| 106 | z[j++] = quote; |
| 107 | i++; |
| 108 | }else{ |
| 109 | z[j++] = 0; |
| 110 | break; |
| 111 | } |
| 112 | }else{ |
| 113 | z[j++] = z[i]; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 119 | ** Retrieve the column names for the table named zTab via database |
| 120 | ** connection db. SQLITE_OK is returned on success, or an sqlite error |
| 121 | ** code otherwise. |
| 122 | ** |
| 123 | ** If successful, the number of columns is written to *pnCol. *paCol is |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 124 | ** set to point at sqlite3_malloc()'d space containing the array of |
| 125 | ** nCol column names. The caller is responsible for calling sqlite3_free |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 126 | ** on *paCol. |
| 127 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 128 | static int getColumnNames( |
| 129 | sqlite3 *db, |
| 130 | const char *zTab, |
| 131 | char ***paCol, |
| 132 | int *pnCol |
| 133 | ){ |
| 134 | char **aCol = 0; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 135 | char *zSql; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 136 | sqlite3_stmt *pStmt = 0; |
| 137 | int rc = SQLITE_OK; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 138 | int nCol = 0; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 139 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 140 | /* Prepare the statement "SELECT * FROM <tbl>". The column names |
| 141 | ** of the result set of the compiled SELECT will be the same as |
| 142 | ** the column names of table <tbl>. |
| 143 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 144 | zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 145 | if( !zSql ){ |
| 146 | rc = SQLITE_NOMEM; |
| 147 | goto out; |
| 148 | } |
| 149 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 150 | sqlite3_free(zSql); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 151 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 152 | if( rc==SQLITE_OK ){ |
| 153 | int ii; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 154 | int nBytes; |
| 155 | char *zSpace; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 156 | nCol = sqlite3_column_count(pStmt); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 157 | |
| 158 | /* Figure out how much space to allocate for the array of column names |
| 159 | ** (including space for the strings themselves). Then allocate it. |
| 160 | */ |
| 161 | nBytes = sizeof(char *) * nCol; |
| 162 | for(ii=0; ii<nCol; ii++){ |
| 163 | nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1); |
| 164 | } |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 165 | aCol = (char **)sqlite3MallocZero(nBytes); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 166 | if( !aCol ){ |
| 167 | rc = SQLITE_NOMEM; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 168 | goto out; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 169 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 170 | |
| 171 | /* Copy the column names into the allocated space and set up the |
| 172 | ** pointers in the aCol[] array. |
| 173 | */ |
| 174 | zSpace = (char *)(&aCol[nCol]); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 175 | for(ii=0; ii<nCol; ii++){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 176 | aCol[ii] = zSpace; |
| 177 | zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii)); |
| 178 | zSpace++; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 179 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 180 | assert( (zSpace-nBytes)==(char *)aCol ); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | *paCol = aCol; |
| 184 | *pnCol = nCol; |
| 185 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 186 | out: |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 187 | sqlite3_finalize(pStmt); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 188 | return rc; |
| 189 | } |
| 190 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 191 | /* |
| 192 | ** Parameter zTab is the name of a table in database db with nCol |
| 193 | ** columns. This function allocates an array of integers nCol in |
| 194 | ** size and populates it according to any implicit or explicit |
| 195 | ** indices on table zTab. |
| 196 | ** |
| 197 | ** If successful, SQLITE_OK is returned and *paIndex set to point |
| 198 | ** at the allocated array. Otherwise, an error code is returned. |
| 199 | ** |
| 200 | ** See comments associated with the member variable aIndex above |
| 201 | ** "struct echo_vtab" for details of the contents of the array. |
| 202 | */ |
| 203 | static int getIndexArray( |
| 204 | sqlite3 *db, /* Database connection */ |
| 205 | const char *zTab, /* Name of table in database db */ |
| 206 | int nCol, |
| 207 | int **paIndex |
| 208 | ){ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 209 | sqlite3_stmt *pStmt = 0; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 210 | int *aIndex = 0; |
| 211 | int rc; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 212 | char *zSql; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 213 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 214 | /* Allocate space for the index array */ |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 215 | aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 216 | if( !aIndex ){ |
| 217 | rc = SQLITE_NOMEM; |
| 218 | goto get_index_array_out; |
| 219 | } |
| 220 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 221 | /* Compile an sqlite pragma to loop through all indices on table zTab */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 222 | zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 223 | if( !zSql ){ |
| 224 | rc = SQLITE_NOMEM; |
| 225 | goto get_index_array_out; |
| 226 | } |
| 227 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 228 | sqlite3_free(zSql); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 229 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 230 | /* For each index, figure out the left-most column and set the |
| 231 | ** corresponding entry in aIndex[] to 1. |
| 232 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 233 | while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 234 | const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 235 | sqlite3_stmt *pStmt2 = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 236 | zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 237 | if( !zSql ){ |
| 238 | rc = SQLITE_NOMEM; |
| 239 | goto get_index_array_out; |
| 240 | } |
| 241 | rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 242 | sqlite3_free(zSql); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 243 | if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){ |
| 244 | int cid = sqlite3_column_int(pStmt2, 1); |
| 245 | assert( cid>=0 && cid<nCol ); |
| 246 | aIndex[cid] = 1; |
| 247 | } |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 248 | if( pStmt2 ){ |
| 249 | rc = sqlite3_finalize(pStmt2); |
| 250 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 251 | if( rc!=SQLITE_OK ){ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 252 | goto get_index_array_out; |
| 253 | } |
| 254 | } |
| 255 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 256 | |
| 257 | get_index_array_out: |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 258 | if( pStmt ){ |
| 259 | int rc2 = sqlite3_finalize(pStmt); |
| 260 | if( rc==SQLITE_OK ){ |
| 261 | rc = rc2; |
| 262 | } |
| 263 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 264 | if( rc!=SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 265 | sqlite3_free(aIndex); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 266 | aIndex = 0; |
| 267 | } |
| 268 | *paIndex = aIndex; |
| 269 | return rc; |
| 270 | } |
| 271 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 272 | /* |
| 273 | ** Global Tcl variable $echo_module is a list. This routine appends |
| 274 | ** the string element zArg to that list in interpreter interp. |
| 275 | */ |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 276 | static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 277 | int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 278 | Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 279 | } |
| 280 | |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 281 | /* |
| 282 | ** This function is called from within the echo-modules xCreate and |
| 283 | ** xConnect methods. The argc and argv arguments are copies of those |
| 284 | ** passed to the calling method. This function is responsible for |
| 285 | ** calling sqlite3_declare_vtab() to declare the schema of the virtual |
| 286 | ** table being created or connected. |
| 287 | ** |
| 288 | ** If the constructor was passed just one argument, i.e.: |
| 289 | ** |
| 290 | ** CREATE TABLE t1 AS echo(t2); |
| 291 | ** |
| 292 | ** Then t2 is assumed to be the name of a *real* database table. The |
| 293 | ** schema of the virtual table is declared by passing a copy of the |
| 294 | ** CREATE TABLE statement for the real table to sqlite3_declare_vtab(). |
| 295 | ** Hence, the virtual table should have exactly the same column names and |
| 296 | ** types as the real table. |
| 297 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 298 | static int echoDeclareVtab( |
| 299 | echo_vtab *pVtab, |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 300 | sqlite3 *db |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 301 | ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 302 | int rc = SQLITE_OK; |
| 303 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 304 | if( pVtab->zTableName ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 305 | sqlite3_stmt *pStmt = 0; |
| 306 | sqlite3_prepare(db, |
| 307 | "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", |
| 308 | -1, &pStmt, 0); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 309 | sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 310 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
danielk1977 | 7fa5dd1 | 2007-04-18 17:04:00 +0000 | [diff] [blame] | 311 | int rc2; |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 312 | const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0); |
danielk1977 | 7fa5dd1 | 2007-04-18 17:04:00 +0000 | [diff] [blame] | 313 | rc = sqlite3_declare_vtab(db, zCreateTable); |
| 314 | rc2 = sqlite3_finalize(pStmt); |
| 315 | if( rc==SQLITE_OK ){ |
| 316 | rc = rc2; |
| 317 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 318 | } else { |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 319 | rc = sqlite3_finalize(pStmt); |
| 320 | if( rc==SQLITE_OK ){ |
| 321 | rc = SQLITE_ERROR; |
| 322 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 323 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 324 | if( rc==SQLITE_OK ){ |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 325 | rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 326 | } |
| 327 | if( rc==SQLITE_OK ){ |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 328 | rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 329 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | return rc; |
| 333 | } |
| 334 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 335 | /* |
| 336 | ** This function frees all runtime structures associated with the virtual |
| 337 | ** table pVtab. |
| 338 | */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 339 | static int echoDestructor(sqlite3_vtab *pVtab){ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 340 | echo_vtab *p = (echo_vtab*)pVtab; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 341 | sqlite3_free(p->aIndex); |
| 342 | sqlite3_free(p->aCol); |
| 343 | sqlite3_free(p->zThis); |
| 344 | sqlite3_free(p->zTableName); |
| 345 | sqlite3_free(p->zLogName); |
| 346 | sqlite3_free(p); |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 350 | /* |
| 351 | ** This function is called to do the work of the xConnect() method - |
| 352 | ** to allocate the required in-memory structures for a newly connected |
| 353 | ** virtual table. |
| 354 | */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 355 | static int echoConstructor( |
| 356 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 357 | void *pAux, |
drh | e410296 | 2006-09-11 00:34:22 +0000 | [diff] [blame] | 358 | int argc, const char *const*argv, |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 359 | sqlite3_vtab **ppVtab, |
| 360 | char **pzErr |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 361 | ){ |
| 362 | int i; |
| 363 | echo_vtab *pVtab; |
| 364 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 365 | /* Allocate the sqlite3_vtab/echo_vtab structure itself */ |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 366 | pVtab = sqlite3MallocZero( sizeof(*pVtab) ); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 367 | if( !pVtab ){ |
| 368 | return SQLITE_NOMEM; |
| 369 | } |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 370 | pVtab->interp = (Tcl_Interp *)pAux; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 371 | pVtab->db = db; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 372 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 373 | /* Allocate echo_vtab.zThis */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 374 | pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 375 | if( !pVtab->zThis ){ |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 376 | echoDestructor((sqlite3_vtab *)pVtab); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 377 | return SQLITE_NOMEM; |
| 378 | } |
| 379 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 380 | /* Allocate echo_vtab.zTableName */ |
| 381 | if( argc>3 ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 382 | pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 383 | dequoteString(pVtab->zTableName); |
| 384 | if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 385 | char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1])); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 386 | sqlite3_free(pVtab->zTableName); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 387 | pVtab->zTableName = z; |
| 388 | pVtab->isPattern = 1; |
| 389 | } |
| 390 | if( !pVtab->zTableName ){ |
| 391 | echoDestructor((sqlite3_vtab *)pVtab); |
| 392 | return SQLITE_NOMEM; |
| 393 | } |
| 394 | } |
| 395 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 396 | /* Log the arguments to this function to Tcl var ::echo_module */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 397 | for(i=0; i<argc; i++){ |
| 398 | appendToEchoModule(pVtab->interp, argv[i]); |
| 399 | } |
| 400 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 401 | /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab |
| 402 | ** structure. If an error occurs, delete the sqlite3_vtab structure and |
| 403 | ** return an error code. |
| 404 | */ |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 405 | if( echoDeclareVtab(pVtab, db) ){ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 406 | echoDestructor((sqlite3_vtab *)pVtab); |
| 407 | return SQLITE_ERROR; |
| 408 | } |
| 409 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 410 | /* Success. Set *ppVtab and return */ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 411 | *ppVtab = &pVtab->base; |
| 412 | return SQLITE_OK; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 413 | } |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 414 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 415 | /* |
| 416 | ** Echo virtual table module xCreate method. |
| 417 | */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 418 | static int echoCreate( |
| 419 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 420 | void *pAux, |
drh | e410296 | 2006-09-11 00:34:22 +0000 | [diff] [blame] | 421 | int argc, const char *const*argv, |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 422 | sqlite3_vtab **ppVtab, |
| 423 | char **pzErr |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 424 | ){ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 425 | int rc = SQLITE_OK; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 426 | appendToEchoModule((Tcl_Interp *)(pAux), "xCreate"); |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 427 | rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 428 | |
| 429 | /* If there were two arguments passed to the module at the SQL level |
| 430 | ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then |
| 431 | ** the second argument is used as a table name. Attempt to create |
| 432 | ** such a table with a single column, "logmsg". This table will |
| 433 | ** be used to log calls to the xUpdate method. It will be deleted |
| 434 | ** when the virtual table is DROPed. |
| 435 | ** |
| 436 | ** Note: The main point of this is to test that we can drop tables |
| 437 | ** from within an xDestroy method call. |
| 438 | */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 439 | if( rc==SQLITE_OK && argc==5 ){ |
| 440 | char *zSql; |
| 441 | echo_vtab *pVtab = *(echo_vtab **)ppVtab; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 442 | pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]); |
| 443 | zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 444 | rc = sqlite3_exec(db, zSql, 0, 0, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 445 | sqlite3_free(zSql); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 446 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 447 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 448 | return rc; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 449 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 450 | |
| 451 | /* |
| 452 | ** Echo virtual table module xConnect method. |
| 453 | */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 454 | static int echoConnect( |
| 455 | sqlite3 *db, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 456 | void *pAux, |
drh | e410296 | 2006-09-11 00:34:22 +0000 | [diff] [blame] | 457 | int argc, const char *const*argv, |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 458 | sqlite3_vtab **ppVtab, |
| 459 | char **pzErr |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 460 | ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 461 | appendToEchoModule((Tcl_Interp *)(pAux), "xConnect"); |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 462 | return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 463 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 464 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 465 | /* |
| 466 | ** Echo virtual table module xDisconnect method. |
| 467 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 468 | static int echoDisconnect(sqlite3_vtab *pVtab){ |
| 469 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect"); |
| 470 | return echoDestructor(pVtab); |
| 471 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 472 | |
| 473 | /* |
| 474 | ** Echo virtual table module xDestroy method. |
| 475 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 476 | static int echoDestroy(sqlite3_vtab *pVtab){ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 477 | int rc = SQLITE_OK; |
| 478 | echo_vtab *p = (echo_vtab *)pVtab; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 479 | appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy"); |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 480 | |
| 481 | /* Drop the "log" table, if one exists (see echoCreate() for details) */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 482 | if( p && p->zLogName ){ |
| 483 | char *zSql; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 484 | zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 485 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 486 | sqlite3_free(zSql); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 487 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 488 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 489 | if( rc==SQLITE_OK ){ |
| 490 | rc = echoDestructor(pVtab); |
| 491 | } |
| 492 | return rc; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 493 | } |
| 494 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 495 | /* |
| 496 | ** Echo virtual table module xOpen method. |
| 497 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 498 | static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 499 | echo_cursor *pCur; |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 500 | pCur = sqlite3MallocZero(sizeof(echo_cursor)); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 501 | *ppCursor = (sqlite3_vtab_cursor *)pCur; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 502 | return (pCur ? SQLITE_OK : SQLITE_NOMEM); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 503 | } |
| 504 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 505 | /* |
| 506 | ** Echo virtual table module xClose method. |
| 507 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 508 | static int echoClose(sqlite3_vtab_cursor *cur){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 509 | int rc; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 510 | echo_cursor *pCur = (echo_cursor *)cur; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 511 | sqlite3_stmt *pStmt = pCur->pStmt; |
| 512 | pCur->pStmt = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 513 | sqlite3_free(pCur); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 514 | rc = sqlite3_finalize(pStmt); |
| 515 | return rc; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 516 | } |
| 517 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 518 | /* |
| 519 | ** Return non-zero if the cursor does not currently point to a valid record |
| 520 | ** (i.e if the scan has finished), or zero otherwise. |
| 521 | */ |
| 522 | static int echoEof(sqlite3_vtab_cursor *cur){ |
| 523 | return (((echo_cursor *)cur)->pStmt ? 0 : 1); |
| 524 | } |
| 525 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 526 | /* |
| 527 | ** Echo virtual table module xNext method. |
| 528 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 529 | static int echoNext(sqlite3_vtab_cursor *cur){ |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 530 | int rc = SQLITE_OK; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 531 | echo_cursor *pCur = (echo_cursor *)cur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 532 | |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 533 | if( pCur->pStmt ){ |
| 534 | rc = sqlite3_step(pCur->pStmt); |
| 535 | if( rc==SQLITE_ROW ){ |
| 536 | rc = SQLITE_OK; |
| 537 | }else{ |
| 538 | rc = sqlite3_finalize(pCur->pStmt); |
| 539 | pCur->pStmt = 0; |
| 540 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | return rc; |
| 544 | } |
| 545 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 546 | /* |
| 547 | ** Echo virtual table module xColumn method. |
| 548 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 549 | static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 550 | int iCol = i + 1; |
| 551 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
danielk1977 | fbbe005 | 2006-06-21 07:02:33 +0000 | [diff] [blame] | 552 | if( !pStmt ){ |
| 553 | sqlite3_result_null(ctx); |
| 554 | }else{ |
| 555 | assert( sqlite3_data_count(pStmt)>iCol ); |
| 556 | sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol)); |
| 557 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 558 | return SQLITE_OK; |
| 559 | } |
| 560 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 561 | /* |
| 562 | ** Echo virtual table module xRowid method. |
| 563 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 564 | static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 565 | sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; |
| 566 | *pRowid = sqlite3_column_int64(pStmt, 0); |
| 567 | return SQLITE_OK; |
| 568 | } |
| 569 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 570 | /* |
| 571 | ** Compute a simple hash of the null terminated string zString. |
| 572 | ** |
| 573 | ** This module uses only sqlite3_index_info.idxStr, not |
| 574 | ** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set |
| 575 | ** in echoBestIndex(), idxNum is set to the corresponding hash value. |
| 576 | ** In echoFilter(), code assert()s that the supplied idxNum value is |
| 577 | ** indeed the hash of the supplied idxStr. |
| 578 | */ |
| 579 | static int hashString(const char *zString){ |
| 580 | int val = 0; |
| 581 | int ii; |
| 582 | for(ii=0; zString[ii]; ii++){ |
| 583 | val = (val << 3) + (int)zString[ii]; |
| 584 | } |
| 585 | return val; |
| 586 | } |
| 587 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 588 | /* |
| 589 | ** Echo virtual table module xFilter method. |
| 590 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 591 | static int echoFilter( |
| 592 | sqlite3_vtab_cursor *pVtabCursor, |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 593 | int idxNum, const char *idxStr, |
| 594 | int argc, sqlite3_value **argv |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 595 | ){ |
| 596 | int rc; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 597 | int i; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 598 | |
| 599 | echo_cursor *pCur = (echo_cursor *)pVtabCursor; |
| 600 | echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab; |
| 601 | sqlite3 *db = pVtab->db; |
| 602 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 603 | /* Check that idxNum matches idxStr */ |
| 604 | assert( idxNum==hashString(idxStr) ); |
| 605 | |
| 606 | /* Log arguments to the ::echo_module Tcl variable */ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 607 | appendToEchoModule(pVtab->interp, "xFilter"); |
| 608 | appendToEchoModule(pVtab->interp, idxStr); |
| 609 | for(i=0; i<argc; i++){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 610 | appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i])); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 611 | } |
| 612 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 613 | sqlite3_finalize(pCur->pStmt); |
| 614 | pCur->pStmt = 0; |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 615 | |
| 616 | /* Prepare the SQL statement created by echoBestIndex and bind the |
| 617 | ** runtime parameters passed to this function to it. |
| 618 | */ |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 619 | rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0); |
danielk1977 | fbbe005 | 2006-06-21 07:02:33 +0000 | [diff] [blame] | 620 | assert( pCur->pStmt || rc!=SQLITE_OK ); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 621 | for(i=0; rc==SQLITE_OK && i<argc; i++){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 622 | sqlite3_bind_value(pCur->pStmt, i+1, argv[i]); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 623 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 624 | |
| 625 | /* If everything was successful, advance to the first row of the scan */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 626 | if( rc==SQLITE_OK ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 627 | rc = echoNext(pVtabCursor); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 628 | } |
| 629 | |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 630 | return rc; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 631 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 632 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | ** A helper function used by echoUpdate() and echoBestIndex() for |
| 636 | ** manipulating strings in concert with the sqlite3_mprintf() function. |
| 637 | ** |
| 638 | ** Parameter pzStr points to a pointer to a string allocated with |
| 639 | ** sqlite3_mprintf. The second parameter, zAppend, points to another |
| 640 | ** string. The two strings are concatenated together and *pzStr |
| 641 | ** set to point at the result. The initial buffer pointed to by *pzStr |
| 642 | ** is deallocated via sqlite3_free(). |
| 643 | ** |
| 644 | ** If the third argument, doFree, is true, then sqlite3_free() is |
| 645 | ** also called to free the buffer pointed to by zAppend. |
| 646 | */ |
| 647 | static void string_concat(char **pzStr, char *zAppend, int doFree){ |
| 648 | char *zIn = *pzStr; |
| 649 | if( zIn ){ |
| 650 | char *zTemp = zIn; |
| 651 | zIn = sqlite3_mprintf("%s%s", zIn, zAppend); |
| 652 | sqlite3_free(zTemp); |
| 653 | }else{ |
| 654 | zIn = sqlite3_mprintf("%s", zAppend); |
| 655 | } |
| 656 | *pzStr = zIn; |
| 657 | if( doFree ){ |
| 658 | sqlite3_free(zAppend); |
| 659 | } |
| 660 | } |
| 661 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 662 | /* |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 663 | ** The echo module implements the subset of query constraints and sort |
| 664 | ** orders that may take advantage of SQLite indices on the underlying |
| 665 | ** real table. For example, if the real table is declared as: |
| 666 | ** |
| 667 | ** CREATE TABLE real(a, b, c); |
| 668 | ** CREATE INDEX real_index ON real(b); |
| 669 | ** |
| 670 | ** then the echo module handles WHERE or ORDER BY clauses that refer |
| 671 | ** to the column "b", but not "a" or "c". If a multi-column index is |
| 672 | ** present, only it's left most column is considered. |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 673 | ** |
| 674 | ** This xBestIndex method encodes the proposed search strategy as |
| 675 | ** an SQL query on the real table underlying the virtual echo module |
| 676 | ** table and stores the query in sqlite3_index_info.idxStr. The SQL |
| 677 | ** statement is of the form: |
| 678 | ** |
| 679 | ** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>? |
| 680 | ** |
| 681 | ** where the <where-clause> and <order-by-clause> are determined |
| 682 | ** by the contents of the structure pointed to by the pIdxInfo argument. |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 683 | */ |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 684 | static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 685 | int ii; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 686 | char *zQuery = 0; |
| 687 | char *zNew; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 688 | int nArg = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 689 | const char *zSep = "WHERE"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 690 | echo_vtab *pVtab = (echo_vtab *)tab; |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 691 | sqlite3_stmt *pStmt = 0; |
danielk1977 | 780b1d9 | 2007-03-30 14:56:34 +0000 | [diff] [blame] | 692 | Tcl_Interp *interp = pVtab->interp; |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 693 | |
| 694 | int nRow; |
| 695 | int useIdx = 0; |
| 696 | int rc = SQLITE_OK; |
danielk1977 | 780b1d9 | 2007-03-30 14:56:34 +0000 | [diff] [blame] | 697 | int useCost = 0; |
| 698 | double cost; |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 699 | |
| 700 | /* Determine the number of rows in the table and store this value in local |
| 701 | ** variable nRow. The 'estimated-cost' of the scan will be the number of |
| 702 | ** rows in the table for a linear scan, or the log (base 2) of the |
| 703 | ** number of rows if the proposed scan uses an index. |
| 704 | */ |
danielk1977 | 780b1d9 | 2007-03-30 14:56:34 +0000 | [diff] [blame] | 705 | if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){ |
| 706 | cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY)); |
| 707 | useCost = 1; |
| 708 | } else { |
| 709 | zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName); |
| 710 | rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0); |
| 711 | sqlite3_free(zQuery); |
| 712 | if( rc!=SQLITE_OK ){ |
| 713 | return rc; |
| 714 | } |
| 715 | sqlite3_step(pStmt); |
| 716 | nRow = sqlite3_column_int(pStmt, 0); |
| 717 | rc = sqlite3_finalize(pStmt); |
| 718 | if( rc!=SQLITE_OK ){ |
| 719 | return rc; |
| 720 | } |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 721 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 722 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 723 | zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 724 | for(ii=0; ii<pIdxInfo->nConstraint; ii++){ |
| 725 | const struct sqlite3_index_constraint *pConstraint; |
| 726 | struct sqlite3_index_constraint_usage *pUsage; |
drh | 383736b | 2006-10-08 18:56:57 +0000 | [diff] [blame] | 727 | int iCol; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 728 | |
| 729 | pConstraint = &pIdxInfo->aConstraint[ii]; |
| 730 | pUsage = &pIdxInfo->aConstraintUsage[ii]; |
| 731 | |
drh | 383736b | 2006-10-08 18:56:57 +0000 | [diff] [blame] | 732 | iCol = pConstraint->iColumn; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 733 | if( pVtab->aIndex[iCol] ){ |
| 734 | char *zCol = pVtab->aCol[iCol]; |
| 735 | char *zOp = 0; |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 736 | useIdx = 1; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 737 | if( iCol<0 ){ |
| 738 | zCol = "rowid"; |
| 739 | } |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 740 | switch( pConstraint->op ){ |
| 741 | case SQLITE_INDEX_CONSTRAINT_EQ: |
| 742 | zOp = "="; break; |
| 743 | case SQLITE_INDEX_CONSTRAINT_LT: |
| 744 | zOp = "<"; break; |
| 745 | case SQLITE_INDEX_CONSTRAINT_GT: |
| 746 | zOp = ">"; break; |
| 747 | case SQLITE_INDEX_CONSTRAINT_LE: |
| 748 | zOp = "<="; break; |
| 749 | case SQLITE_INDEX_CONSTRAINT_GE: |
| 750 | zOp = ">="; break; |
| 751 | case SQLITE_INDEX_CONSTRAINT_MATCH: |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 752 | zOp = "LIKE"; break; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 753 | } |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 754 | if( zOp[0]=='L' ){ |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 755 | zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')", |
| 756 | zSep, zCol); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 757 | } else { |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 758 | zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 759 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 760 | string_concat(&zQuery, zNew, 1); |
| 761 | |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 762 | zSep = "AND"; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 763 | pUsage->argvIndex = ++nArg; |
| 764 | pUsage->omit = 1; |
| 765 | } |
| 766 | } |
danielk1977 | 47d0809 | 2006-06-14 07:41:31 +0000 | [diff] [blame] | 767 | |
| 768 | /* If there is only one term in the ORDER BY clause, and it is |
| 769 | ** on a column that this virtual table has an index for, then consume |
| 770 | ** the ORDER BY clause. |
| 771 | */ |
| 772 | if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 773 | int iCol = pIdxInfo->aOrderBy->iColumn; |
| 774 | char *zCol = pVtab->aCol[iCol]; |
danielk1977 | 47d0809 | 2006-06-14 07:41:31 +0000 | [diff] [blame] | 775 | char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC"; |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 776 | if( iCol<0 ){ |
| 777 | zCol = "rowid"; |
| 778 | } |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 779 | zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir); |
| 780 | string_concat(&zQuery, zNew, 1); |
danielk1977 | 47d0809 | 2006-06-14 07:41:31 +0000 | [diff] [blame] | 781 | pIdxInfo->orderByConsumed = 1; |
| 782 | } |
| 783 | |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 784 | appendToEchoModule(pVtab->interp, "xBestIndex");; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 785 | appendToEchoModule(pVtab->interp, zQuery); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 786 | |
danielk1977 | 9833153 | 2006-06-14 10:55:52 +0000 | [diff] [blame] | 787 | pIdxInfo->idxNum = hashString(zQuery); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 788 | pIdxInfo->idxStr = zQuery; |
| 789 | pIdxInfo->needToFreeIdxStr = 1; |
danielk1977 | 780b1d9 | 2007-03-30 14:56:34 +0000 | [diff] [blame] | 790 | if (useCost) { |
| 791 | pIdxInfo->estimatedCost = cost; |
| 792 | } else if( useIdx ){ |
danielk1977 | 74cdba4 | 2006-06-19 12:02:58 +0000 | [diff] [blame] | 793 | /* Approximation of log2(nRow). */ |
| 794 | for( ii=0; ii<(sizeof(int)*8); ii++ ){ |
| 795 | if( nRow & (1<<ii) ){ |
| 796 | pIdxInfo->estimatedCost = (double)ii; |
| 797 | } |
| 798 | } |
| 799 | } else { |
| 800 | pIdxInfo->estimatedCost = (double)nRow; |
| 801 | } |
| 802 | return rc; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 803 | } |
| 804 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 805 | /* |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 806 | ** The xUpdate method for echo module virtual tables. |
| 807 | ** |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 808 | ** apData[0] apData[1] apData[2..] |
| 809 | ** |
| 810 | ** INTEGER DELETE |
| 811 | ** |
| 812 | ** INTEGER NULL (nCol args) UPDATE (do not set rowid) |
| 813 | ** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>) |
| 814 | ** |
| 815 | ** NULL NULL (nCol args) INSERT INTO (automatic rowid value) |
| 816 | ** NULL INTEGER (nCol args) INSERT (incl. rowid value) |
| 817 | ** |
| 818 | */ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 819 | int echoUpdate( |
| 820 | sqlite3_vtab *tab, |
| 821 | int nData, |
| 822 | sqlite3_value **apData, |
| 823 | sqlite_int64 *pRowid |
| 824 | ){ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 825 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 826 | sqlite3 *db = pVtab->db; |
| 827 | int rc = SQLITE_OK; |
| 828 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 829 | sqlite3_stmt *pStmt; |
| 830 | char *z = 0; /* SQL statement to execute */ |
| 831 | int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */ |
| 832 | int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */ |
| 833 | int i; /* Counter variable used by for loops */ |
| 834 | |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 835 | assert( nData==pVtab->nCol+2 || nData==1 ); |
| 836 | |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 837 | /* If apData[0] is an integer and nData>1 then do an UPDATE */ |
| 838 | if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 839 | char *zSep = " SET"; |
drh | 383736b | 2006-10-08 18:56:57 +0000 | [diff] [blame] | 840 | z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName); |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 841 | |
| 842 | bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER); |
| 843 | bindArgZero = 1; |
| 844 | |
| 845 | if( bindArgOne ){ |
| 846 | string_concat(&z, " SET rowid=?1 ", 0); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 847 | zSep = ","; |
| 848 | } |
| 849 | for(i=2; i<nData; i++){ |
| 850 | if( apData[i]==0 ) continue; |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 851 | string_concat(&z, sqlite3_mprintf( |
| 852 | "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 853 | zSep = ","; |
| 854 | } |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 855 | string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1); |
drh | 5aec042 | 2006-06-14 23:43:31 +0000 | [diff] [blame] | 856 | } |
| 857 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 858 | /* If apData[0] is an integer and nData==1 then do a DELETE */ |
| 859 | else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ |
| 860 | z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName); |
| 861 | bindArgZero = 1; |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 862 | } |
| 863 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 864 | /* If the first argument is NULL and there are more than two args, INSERT */ |
| 865 | else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 866 | int ii; |
| 867 | char *zInsert = 0; |
| 868 | char *zValues = 0; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 869 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 870 | zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName); |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 871 | if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){ |
| 872 | bindArgOne = 1; |
| 873 | zValues = sqlite3_mprintf("?"); |
| 874 | string_concat(&zInsert, "rowid", 0); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 875 | } |
| 876 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 877 | assert((pVtab->nCol+2)==nData); |
| 878 | for(ii=2; ii<nData; ii++){ |
| 879 | string_concat(&zInsert, |
| 880 | sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1); |
| 881 | string_concat(&zValues, |
| 882 | sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 883 | } |
| 884 | |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 885 | string_concat(&z, zInsert, 1); |
| 886 | string_concat(&z, ") VALUES(", 0); |
| 887 | string_concat(&z, zValues, 1); |
| 888 | string_concat(&z, ")", 0); |
| 889 | } |
| 890 | |
| 891 | /* Anything else is an error */ |
| 892 | else{ |
| 893 | assert(0); |
| 894 | return SQLITE_ERROR; |
| 895 | } |
| 896 | |
| 897 | rc = sqlite3_prepare(db, z, -1, &pStmt, 0); |
| 898 | assert( rc!=SQLITE_OK || pStmt ); |
| 899 | sqlite3_free(z); |
| 900 | if( rc==SQLITE_OK ) { |
| 901 | if( bindArgZero ){ |
| 902 | sqlite3_bind_value(pStmt, nData, apData[0]); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 903 | } |
danielk1977 | 176f4d2 | 2006-06-15 10:41:15 +0000 | [diff] [blame] | 904 | if( bindArgOne ){ |
| 905 | sqlite3_bind_value(pStmt, 1, apData[1]); |
| 906 | } |
| 907 | for(i=2; i<nData; i++){ |
| 908 | if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]); |
| 909 | } |
| 910 | sqlite3_step(pStmt); |
| 911 | rc = sqlite3_finalize(pStmt); |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 912 | } |
| 913 | |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 914 | if( pRowid && rc==SQLITE_OK ){ |
| 915 | *pRowid = sqlite3_last_insert_rowid(db); |
| 916 | } |
| 917 | |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 918 | return rc; |
| 919 | } |
| 920 | |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 921 | /* |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 922 | ** xBegin, xSync, xCommit and xRollback callbacks for echo module |
| 923 | ** virtual tables. Do nothing other than add the name of the callback |
| 924 | ** to the $::echo_module Tcl variable. |
| 925 | */ |
| 926 | static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){ |
| 927 | char *z; |
| 928 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 929 | z = sqlite3_mprintf("echo(%s)", pVtab->zTableName); |
| 930 | appendToEchoModule(pVtab->interp, zCall); |
| 931 | appendToEchoModule(pVtab->interp, z); |
| 932 | sqlite3_free(z); |
| 933 | return SQLITE_OK; |
| 934 | } |
| 935 | static int echoBegin(sqlite3_vtab *tab){ |
danielk1977 | 5017dc3 | 2006-06-24 09:34:22 +0000 | [diff] [blame] | 936 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 937 | Tcl_Interp *interp = pVtab->interp; |
| 938 | const char *zVal; |
| 939 | |
| 940 | echoTransactionCall(tab, "xBegin"); |
| 941 | |
| 942 | /* Check if the $::echo_module_begin_fail variable is defined. If it is, |
| 943 | ** and it is set to the name of the real table underlying this virtual |
| 944 | ** echo module table, then cause this xSync operation to fail. |
| 945 | */ |
| 946 | zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY); |
| 947 | if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ |
| 948 | return SQLITE_ERROR; |
| 949 | } |
| 950 | return SQLITE_OK; |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 951 | } |
| 952 | static int echoSync(sqlite3_vtab *tab){ |
| 953 | echo_vtab *pVtab = (echo_vtab *)tab; |
| 954 | Tcl_Interp *interp = pVtab->interp; |
| 955 | const char *zVal; |
| 956 | |
| 957 | echoTransactionCall(tab, "xSync"); |
| 958 | |
| 959 | /* Check if the $::echo_module_sync_fail variable is defined. If it is, |
| 960 | ** and it is set to the name of the real table underlying this virtual |
| 961 | ** echo module table, then cause this xSync operation to fail. |
| 962 | */ |
| 963 | zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY); |
| 964 | if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ |
| 965 | return -1; |
| 966 | } |
| 967 | return SQLITE_OK; |
| 968 | } |
| 969 | static int echoCommit(sqlite3_vtab *tab){ |
| 970 | return echoTransactionCall(tab, "xCommit"); |
| 971 | } |
| 972 | static int echoRollback(sqlite3_vtab *tab){ |
| 973 | return echoTransactionCall(tab, "xRollback"); |
| 974 | } |
| 975 | |
| 976 | /* |
drh | e94b0c3 | 2006-07-08 18:09:15 +0000 | [diff] [blame] | 977 | ** Implementation of "GLOB" function on the echo module. Pass |
| 978 | ** all arguments to the ::echo_glob_overload procedure of TCL |
| 979 | ** and return the result of that procedure as a string. |
| 980 | */ |
| 981 | static void overloadedGlobFunction( |
| 982 | sqlite3_context *pContext, |
| 983 | int nArg, |
| 984 | sqlite3_value **apArg |
| 985 | ){ |
| 986 | Tcl_Interp *interp = sqlite3_user_data(pContext); |
| 987 | Tcl_DString str; |
| 988 | int i; |
| 989 | int rc; |
| 990 | Tcl_DStringInit(&str); |
| 991 | Tcl_DStringAppendElement(&str, "::echo_glob_overload"); |
| 992 | for(i=0; i<nArg; i++){ |
| 993 | Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i])); |
| 994 | } |
| 995 | rc = Tcl_Eval(interp, Tcl_DStringValue(&str)); |
| 996 | Tcl_DStringFree(&str); |
| 997 | if( rc ){ |
| 998 | sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1); |
| 999 | }else{ |
| 1000 | sqlite3_result_text(pContext, Tcl_GetStringResult(interp), |
| 1001 | -1, SQLITE_TRANSIENT); |
| 1002 | } |
| 1003 | Tcl_ResetResult(interp); |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | ** This is the xFindFunction implementation for the echo module. |
| 1008 | ** SQLite calls this routine when the first argument of a function |
| 1009 | ** is a column of an echo virtual table. This routine can optionally |
| 1010 | ** override the implementation of that function. It will choose to |
| 1011 | ** do so if the function is named "glob", and a TCL command named |
| 1012 | ** ::echo_glob_overload exists. |
| 1013 | */ |
| 1014 | static int echoFindFunction( |
| 1015 | sqlite3_vtab *vtab, |
| 1016 | int nArg, |
| 1017 | const char *zFuncName, |
| 1018 | void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), |
| 1019 | void **ppArg |
| 1020 | ){ |
| 1021 | echo_vtab *pVtab = (echo_vtab *)vtab; |
| 1022 | Tcl_Interp *interp = pVtab->interp; |
| 1023 | Tcl_CmdInfo info; |
| 1024 | if( strcmp(zFuncName,"glob")!=0 ){ |
| 1025 | return 0; |
| 1026 | } |
| 1027 | if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){ |
| 1028 | return 0; |
| 1029 | } |
| 1030 | *pxFunc = overloadedGlobFunction; |
| 1031 | *ppArg = interp; |
| 1032 | return 1; |
| 1033 | } |
| 1034 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 1035 | static int echoRename(sqlite3_vtab *vtab, const char *zNewName){ |
| 1036 | int rc = SQLITE_OK; |
| 1037 | echo_vtab *p = (echo_vtab *)vtab; |
| 1038 | |
| 1039 | if( p->isPattern ){ |
| 1040 | int nThis = strlen(p->zThis); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1041 | char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s", |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 1042 | p->zTableName, zNewName, &p->zTableName[nThis] |
| 1043 | ); |
| 1044 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1045 | sqlite3_free(zSql); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | return rc; |
| 1049 | } |
| 1050 | |
drh | e94b0c3 | 2006-07-08 18:09:15 +0000 | [diff] [blame] | 1051 | /* |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 1052 | ** A virtual table module that merely "echos" the contents of another |
| 1053 | ** table (like an SQL VIEW). |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1054 | */ |
| 1055 | static sqlite3_module echoModule = { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 1056 | 0, /* iVersion */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1057 | echoCreate, |
| 1058 | echoConnect, |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 1059 | echoBestIndex, |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1060 | echoDisconnect, |
| 1061 | echoDestroy, |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 1062 | echoOpen, /* xOpen - open a cursor */ |
| 1063 | echoClose, /* xClose - close a cursor */ |
| 1064 | echoFilter, /* xFilter - configure scan constraints */ |
| 1065 | echoNext, /* xNext - advance a cursor */ |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 1066 | echoEof, /* xEof */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 1067 | echoColumn, /* xColumn - read data */ |
danielk1977 | 26e4144 | 2006-06-14 15:16:35 +0000 | [diff] [blame] | 1068 | echoRowid, /* xRowid - read data */ |
danielk1977 | c69cdfd | 2006-06-17 09:39:55 +0000 | [diff] [blame] | 1069 | echoUpdate, /* xUpdate - write data */ |
| 1070 | echoBegin, /* xBegin - begin transaction */ |
| 1071 | echoSync, /* xSync - sync transaction */ |
| 1072 | echoCommit, /* xCommit - commit transaction */ |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 1073 | echoRollback, /* xRollback - rollback transaction */ |
drh | e94b0c3 | 2006-07-08 18:09:15 +0000 | [diff] [blame] | 1074 | echoFindFunction, /* xFindFunction - function overloading */ |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 1075 | echoRename, /* xRename - rename the table */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1076 | }; |
| 1077 | |
| 1078 | /* |
| 1079 | ** Decode a pointer to an sqlite3 object. |
| 1080 | */ |
| 1081 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 1082 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 1083 | return TCL_OK; |
| 1084 | } |
| 1085 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1086 | /* |
| 1087 | ** Register the echo virtual table module. |
| 1088 | */ |
| 1089 | static int register_echo_module( |
| 1090 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1091 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1092 | int objc, /* Number of arguments */ |
| 1093 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1094 | ){ |
| 1095 | sqlite3 *db; |
| 1096 | if( objc!=2 ){ |
| 1097 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 1098 | return TCL_ERROR; |
| 1099 | } |
| 1100 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 1101 | sqlite3_create_module(db, "echo", &echoModule, (void *)interp); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1102 | return TCL_OK; |
| 1103 | } |
| 1104 | |
danielk1977 | 5017dc3 | 2006-06-24 09:34:22 +0000 | [diff] [blame] | 1105 | /* |
| 1106 | ** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl: |
| 1107 | ** |
| 1108 | ** sqlite3_declare_vtab DB SQL |
| 1109 | */ |
| 1110 | static int declare_vtab( |
| 1111 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1112 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1113 | int objc, /* Number of arguments */ |
| 1114 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1115 | ){ |
| 1116 | sqlite3 *db; |
| 1117 | int rc; |
| 1118 | if( objc!=3 ){ |
| 1119 | Tcl_WrongNumArgs(interp, 1, objv, "DB SQL"); |
| 1120 | return TCL_ERROR; |
| 1121 | } |
| 1122 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1123 | rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2])); |
| 1124 | if( rc!=SQLITE_OK ){ |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 1125 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); |
danielk1977 | 5017dc3 | 2006-06-24 09:34:22 +0000 | [diff] [blame] | 1126 | return TCL_ERROR; |
| 1127 | } |
| 1128 | return TCL_OK; |
| 1129 | } |
| 1130 | |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 1131 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1132 | |
| 1133 | /* |
| 1134 | ** Register commands with the TCL interpreter. |
| 1135 | */ |
| 1136 | int Sqlitetest8_Init(Tcl_Interp *interp){ |
| 1137 | static struct { |
| 1138 | char *zName; |
| 1139 | Tcl_ObjCmdProc *xProc; |
| 1140 | void *clientData; |
| 1141 | } aObjCmd[] = { |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 1142 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1143 | { "register_echo_module", register_echo_module, 0 }, |
danielk1977 | 5017dc3 | 2006-06-24 09:34:22 +0000 | [diff] [blame] | 1144 | { "sqlite3_declare_vtab", declare_vtab, 0 }, |
danielk1977 | cc013f8 | 2006-06-24 06:36:11 +0000 | [diff] [blame] | 1145 | #endif |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1146 | }; |
| 1147 | int i; |
| 1148 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 1149 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 1150 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 1151 | } |
| 1152 | return TCL_OK; |
| 1153 | } |