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