drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 12 | ** A TCL Interface to SQLite. Append this file to sqlite3.c and |
| 13 | ** compile the whole thing to build a TCL-enabled version of SQLite. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 14 | ** |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 15 | ** $Id: tclsqlite.c,v 1.181 2007/05/02 13:16:31 danielk1977 Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 16 | */ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 17 | #include "tcl.h" |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 18 | #include <errno.h> |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | ** Some additional include files are needed if this file is not |
| 22 | ** appended to the amalgamation. |
| 23 | */ |
| 24 | #ifndef SQLITE_AMALGAMATION |
| 25 | # include "sqliteInt.h" |
| 26 | # include "hash.h" |
| 27 | # include <stdlib.h> |
| 28 | # include <string.h> |
| 29 | # include <assert.h> |
| 30 | # include <ctype.h> |
| 31 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 32 | |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 33 | /* |
| 34 | * Windows needs to know which symbols to export. Unix does not. |
| 35 | * BUILD_sqlite should be undefined for Unix. |
| 36 | */ |
| 37 | #ifdef BUILD_sqlite |
| 38 | #undef TCL_STORAGE_CLASS |
| 39 | #define TCL_STORAGE_CLASS DLLEXPORT |
| 40 | #endif /* BUILD_sqlite */ |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 41 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 42 | #define NUM_PREPARED_STMTS 10 |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 43 | #define MAX_PREPARED_STMTS 100 |
| 44 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 45 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 46 | ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
| 47 | ** have to do a translation when going between the two. Set the |
| 48 | ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
| 49 | ** this translation. |
| 50 | */ |
| 51 | #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
| 52 | # define UTF_TRANSLATION_NEEDED 1 |
| 53 | #endif |
| 54 | |
| 55 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 56 | ** New SQL functions can be created as TCL scripts. Each such function |
| 57 | ** is described by an instance of the following structure. |
| 58 | */ |
| 59 | typedef struct SqlFunc SqlFunc; |
| 60 | struct SqlFunc { |
| 61 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 62 | Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ |
| 63 | int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ |
| 64 | char *zName; /* Name of this function */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 65 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 66 | }; |
| 67 | |
| 68 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 69 | ** New collation sequences function can be created as TCL scripts. Each such |
| 70 | ** function is described by an instance of the following structure. |
| 71 | */ |
| 72 | typedef struct SqlCollate SqlCollate; |
| 73 | struct SqlCollate { |
| 74 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 75 | char *zScript; /* The script to be run */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 76 | SqlCollate *pNext; /* Next function on the list of them all */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /* |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 80 | ** Prepared statements are cached for faster execution. Each prepared |
| 81 | ** statement is described by an instance of the following structure. |
| 82 | */ |
| 83 | typedef struct SqlPreparedStmt SqlPreparedStmt; |
| 84 | struct SqlPreparedStmt { |
| 85 | SqlPreparedStmt *pNext; /* Next in linked list */ |
| 86 | SqlPreparedStmt *pPrev; /* Previous on the list */ |
| 87 | sqlite3_stmt *pStmt; /* The prepared statement */ |
| 88 | int nSql; /* chars in zSql[] */ |
| 89 | char zSql[1]; /* Text of the SQL statement */ |
| 90 | }; |
| 91 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 92 | typedef struct IncrblobChannel IncrblobChannel; |
| 93 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 94 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 95 | ** There is one instance of this structure for each SQLite database |
| 96 | ** that has been opened by the SQLite TCL interface. |
| 97 | */ |
| 98 | typedef struct SqliteDb SqliteDb; |
| 99 | struct SqliteDb { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 100 | sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 101 | Tcl_Interp *interp; /* The interpreter used for this database */ |
| 102 | char *zBusy; /* The busy callback routine */ |
| 103 | char *zCommit; /* The commit hook callback routine */ |
| 104 | char *zTrace; /* The trace callback routine */ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 105 | char *zProfile; /* The profile callback routine */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 106 | char *zProgress; /* The progress callback routine */ |
| 107 | char *zAuth; /* The authorization callback routine */ |
| 108 | char *zNull; /* Text to substitute for an SQL NULL value */ |
| 109 | SqlFunc *pFunc; /* List of SQL functions */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 110 | Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 111 | Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 112 | SqlCollate *pCollate; /* List of SQL collation functions */ |
| 113 | int rc; /* Return code of most recent sqlite3_exec() */ |
| 114 | Tcl_Obj *pCollateNeeded; /* Collation needed script */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 115 | SqlPreparedStmt *stmtList; /* List of prepared statements*/ |
| 116 | SqlPreparedStmt *stmtLast; /* Last statement in the list */ |
| 117 | int maxStmt; /* The next maximum number of stmtList */ |
| 118 | int nStmt; /* Number of statements in stmtList */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 119 | IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 120 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 121 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 122 | struct IncrblobChannel { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 123 | SqliteDb *pDb; /* Associated database connection */ |
| 124 | sqlite3_blob *pBlob; /* sqlite3 blob handle */ |
| 125 | int iSeek; /* Current seek offset */ |
| 126 | |
| 127 | Tcl_Channel channel; /* Channel identifier */ |
| 128 | IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ |
| 129 | IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 133 | ** Close all incrblob channels opened using database connection pDb. |
| 134 | ** This is called when shutting down the database connection. |
| 135 | */ |
| 136 | static void closeIncrblobChannels(SqliteDb *pDb){ |
| 137 | IncrblobChannel *p; |
| 138 | IncrblobChannel *pNext; |
| 139 | |
| 140 | for(p=pDb->pIncrblob; p; p=pNext){ |
| 141 | pNext = p->pNext; |
| 142 | |
| 143 | /* Note: Calling unregister here call Tcl_Close on the incrblob channel, |
| 144 | ** which deletes the IncrblobChannel structure at *p. So do not |
| 145 | ** call Tcl_Free() here. |
| 146 | */ |
| 147 | Tcl_UnregisterChannel(pDb->interp, p->channel); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 152 | ** Close an incremental blob channel. |
| 153 | */ |
| 154 | static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ |
| 155 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 156 | sqlite3_blob_close(p->pBlob); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 157 | |
| 158 | /* Remove the channel from the SqliteDb.pIncrblob list. */ |
| 159 | if( p->pNext ){ |
| 160 | p->pNext->pPrev = p->pPrev; |
| 161 | } |
| 162 | if( p->pPrev ){ |
| 163 | p->pPrev->pNext = p->pNext; |
| 164 | } |
| 165 | if( p->pDb->pIncrblob==p ){ |
| 166 | p->pDb->pIncrblob = p->pNext; |
| 167 | } |
| 168 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 169 | Tcl_Free((char *)p); |
| 170 | return TCL_OK; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | ** Read data from an incremental blob channel. |
| 175 | */ |
| 176 | static int incrblobInput( |
| 177 | ClientData instanceData, |
| 178 | char *buf, |
| 179 | int bufSize, |
| 180 | int *errorCodePtr |
| 181 | ){ |
| 182 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 183 | int nRead = bufSize; /* Number of bytes to read */ |
| 184 | int nBlob; /* Total size of the blob */ |
| 185 | int rc; /* sqlite error code */ |
| 186 | |
| 187 | nBlob = sqlite3_blob_bytes(p->pBlob); |
| 188 | if( (p->iSeek+nRead)>nBlob ){ |
| 189 | nRead = nBlob-p->iSeek; |
| 190 | } |
| 191 | if( nRead<=0 ){ |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); |
| 196 | if( rc!=SQLITE_OK ){ |
| 197 | *errorCodePtr = rc; |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | p->iSeek += nRead; |
| 202 | return nRead; |
| 203 | } |
| 204 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 205 | /* |
| 206 | ** Write data to an incremental blob channel. |
| 207 | */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 208 | static int incrblobOutput( |
| 209 | ClientData instanceData, |
| 210 | CONST char *buf, |
| 211 | int toWrite, |
| 212 | int *errorCodePtr |
| 213 | ){ |
| 214 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 215 | int nWrite = toWrite; /* Number of bytes to write */ |
| 216 | int nBlob; /* Total size of the blob */ |
| 217 | int rc; /* sqlite error code */ |
| 218 | |
| 219 | nBlob = sqlite3_blob_bytes(p->pBlob); |
| 220 | if( (p->iSeek+nWrite)>nBlob ){ |
| 221 | *errorCodePtr = EINVAL; |
| 222 | return -1; |
| 223 | } |
| 224 | if( nWrite<=0 ){ |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); |
| 229 | if( rc!=SQLITE_OK ){ |
| 230 | *errorCodePtr = EIO; |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | p->iSeek += nWrite; |
| 235 | return nWrite; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | ** Seek an incremental blob channel. |
| 240 | */ |
| 241 | static int incrblobSeek( |
| 242 | ClientData instanceData, |
| 243 | long offset, |
| 244 | int seekMode, |
| 245 | int *errorCodePtr |
| 246 | ){ |
| 247 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 248 | |
| 249 | switch( seekMode ){ |
| 250 | case SEEK_SET: |
| 251 | p->iSeek = offset; |
| 252 | break; |
| 253 | case SEEK_CUR: |
| 254 | p->iSeek += offset; |
| 255 | break; |
| 256 | case SEEK_END: |
| 257 | p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; |
| 258 | break; |
| 259 | |
| 260 | default: assert(!"Bad seekMode"); |
| 261 | } |
| 262 | |
| 263 | return p->iSeek; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | static void incrblobWatch(ClientData instanceData, int mode){ |
| 268 | /* NO-OP */ |
| 269 | } |
| 270 | static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ |
| 271 | return TCL_ERROR; |
| 272 | } |
| 273 | |
| 274 | static Tcl_ChannelType IncrblobChannelType = { |
| 275 | "incrblob", /* typeName */ |
| 276 | TCL_CHANNEL_VERSION_2, /* version */ |
| 277 | incrblobClose, /* closeProc */ |
| 278 | incrblobInput, /* inputProc */ |
| 279 | incrblobOutput, /* outputProc */ |
| 280 | incrblobSeek, /* seekProc */ |
| 281 | 0, /* setOptionProc */ |
| 282 | 0, /* getOptionProc */ |
| 283 | incrblobWatch, /* watchProc (this is a no-op) */ |
| 284 | incrblobHandle, /* getHandleProc (always returns error) */ |
| 285 | 0, /* close2Proc */ |
| 286 | 0, /* blockModeProc */ |
| 287 | 0, /* flushProc */ |
| 288 | 0, /* handlerProc */ |
| 289 | 0, /* wideSeekProc */ |
| 290 | 0, /* threadActionProc */ |
| 291 | }; |
| 292 | |
| 293 | /* |
| 294 | ** Create a new incrblob channel. |
| 295 | */ |
| 296 | static int createIncrblobChannel( |
| 297 | Tcl_Interp *interp, |
| 298 | SqliteDb *pDb, |
| 299 | const char *zDb, |
| 300 | const char *zTable, |
| 301 | const char *zColumn, |
| 302 | sqlite_int64 iRow |
| 303 | ){ |
| 304 | IncrblobChannel *p; |
| 305 | sqlite3_blob *pBlob; |
| 306 | int rc; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 307 | int flags = TCL_READABLE|TCL_WRITABLE; |
| 308 | |
| 309 | /* This variable is used to name the channels: "incrblob_[incr count]" */ |
| 310 | static int count = 0; |
| 311 | char zChannel[64]; |
| 312 | |
| 313 | rc = sqlite3_blob_open(pDb->db, zDb, zTable, zColumn, iRow, 1, &pBlob); |
| 314 | if( rc!=SQLITE_OK ){ |
| 315 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
| 316 | return TCL_ERROR; |
| 317 | } |
| 318 | |
| 319 | p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); |
| 320 | p->iSeek = 0; |
| 321 | p->pBlob = pBlob; |
| 322 | |
| 323 | sprintf(zChannel, "incrblob_%d", ++count); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 324 | p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); |
| 325 | Tcl_RegisterChannel(interp, p->channel); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 326 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 327 | /* Link the new channel into the SqliteDb.pIncrblob list. */ |
| 328 | p->pNext = pDb->pIncrblob; |
| 329 | p->pPrev = 0; |
| 330 | if( p->pNext ){ |
| 331 | p->pNext->pPrev = p; |
| 332 | } |
| 333 | pDb->pIncrblob = p; |
| 334 | p->pDb = pDb; |
| 335 | |
| 336 | Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 337 | return TCL_OK; |
| 338 | } |
| 339 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 340 | /* |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 341 | ** Look at the script prefix in pCmd. We will be executing this script |
| 342 | ** after first appending one or more arguments. This routine analyzes |
| 343 | ** the script to see if it is safe to use Tcl_EvalObjv() on the script |
| 344 | ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much |
| 345 | ** faster. |
| 346 | ** |
| 347 | ** Scripts that are safe to use with Tcl_EvalObjv() consists of a |
| 348 | ** command name followed by zero or more arguments with no [...] or $ |
| 349 | ** or {...} or ; to be seen anywhere. Most callback scripts consist |
| 350 | ** of just a single procedure name and they meet this requirement. |
| 351 | */ |
| 352 | static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ |
| 353 | /* We could try to do something with Tcl_Parse(). But we will instead |
| 354 | ** just do a search for forbidden characters. If any of the forbidden |
| 355 | ** characters appear in pCmd, we will report the string as unsafe. |
| 356 | */ |
| 357 | const char *z; |
| 358 | int n; |
| 359 | z = Tcl_GetStringFromObj(pCmd, &n); |
| 360 | while( n-- > 0 ){ |
| 361 | int c = *(z++); |
| 362 | if( c=='$' || c=='[' || c==';' ) return 0; |
| 363 | } |
| 364 | return 1; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | ** Find an SqlFunc structure with the given name. Or create a new |
| 369 | ** one if an existing one cannot be found. Return a pointer to the |
| 370 | ** structure. |
| 371 | */ |
| 372 | static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ |
| 373 | SqlFunc *p, *pNew; |
| 374 | int i; |
| 375 | pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen(zName) + 1 ); |
| 376 | pNew->zName = (char*)&pNew[1]; |
| 377 | for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); } |
| 378 | pNew->zName[i] = 0; |
| 379 | for(p=pDb->pFunc; p; p=p->pNext){ |
| 380 | if( strcmp(p->zName, pNew->zName)==0 ){ |
| 381 | Tcl_Free((char*)pNew); |
| 382 | return p; |
| 383 | } |
| 384 | } |
| 385 | pNew->interp = pDb->interp; |
| 386 | pNew->pScript = 0; |
| 387 | pNew->pNext = pDb->pFunc; |
| 388 | pDb->pFunc = pNew; |
| 389 | return pNew; |
| 390 | } |
| 391 | |
| 392 | /* |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 393 | ** Finalize and free a list of prepared statements |
| 394 | */ |
| 395 | static void flushStmtCache( SqliteDb *pDb ){ |
| 396 | SqlPreparedStmt *pPreStmt; |
| 397 | |
| 398 | while( pDb->stmtList ){ |
| 399 | sqlite3_finalize( pDb->stmtList->pStmt ); |
| 400 | pPreStmt = pDb->stmtList; |
| 401 | pDb->stmtList = pDb->stmtList->pNext; |
| 402 | Tcl_Free( (char*)pPreStmt ); |
| 403 | } |
| 404 | pDb->nStmt = 0; |
| 405 | pDb->stmtLast = 0; |
| 406 | } |
| 407 | |
| 408 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 409 | ** TCL calls this procedure when an sqlite3 database command is |
| 410 | ** deleted. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 411 | */ |
| 412 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 413 | SqliteDb *pDb = (SqliteDb*)db; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 414 | flushStmtCache(pDb); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame^] | 415 | closeIncrblobChannels(pDb); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 416 | sqlite3_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 417 | while( pDb->pFunc ){ |
| 418 | SqlFunc *pFunc = pDb->pFunc; |
| 419 | pDb->pFunc = pFunc->pNext; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 420 | Tcl_DecrRefCount(pFunc->pScript); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 421 | Tcl_Free((char*)pFunc); |
| 422 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 423 | while( pDb->pCollate ){ |
| 424 | SqlCollate *pCollate = pDb->pCollate; |
| 425 | pDb->pCollate = pCollate->pNext; |
| 426 | Tcl_Free((char*)pCollate); |
| 427 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 428 | if( pDb->zBusy ){ |
| 429 | Tcl_Free(pDb->zBusy); |
| 430 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 431 | if( pDb->zTrace ){ |
| 432 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 433 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 434 | if( pDb->zProfile ){ |
| 435 | Tcl_Free(pDb->zProfile); |
| 436 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 437 | if( pDb->zAuth ){ |
| 438 | Tcl_Free(pDb->zAuth); |
| 439 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 440 | if( pDb->zNull ){ |
| 441 | Tcl_Free(pDb->zNull); |
| 442 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 443 | if( pDb->pUpdateHook ){ |
| 444 | Tcl_DecrRefCount(pDb->pUpdateHook); |
| 445 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 446 | if( pDb->pRollbackHook ){ |
| 447 | Tcl_DecrRefCount(pDb->pRollbackHook); |
| 448 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 449 | if( pDb->pCollateNeeded ){ |
| 450 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 451 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 452 | Tcl_Free((char*)pDb); |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | ** This routine is called when a database file is locked while trying |
| 457 | ** to execute SQL. |
| 458 | */ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 459 | static int DbBusyHandler(void *cd, int nTries){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 460 | SqliteDb *pDb = (SqliteDb*)cd; |
| 461 | int rc; |
| 462 | char zVal[30]; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 463 | |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 464 | sprintf(zVal, "%d", nTries); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 465 | rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 466 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 467 | return 0; |
| 468 | } |
| 469 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 473 | ** This routine is invoked as the 'progress callback' for the database. |
| 474 | */ |
| 475 | static int DbProgressHandler(void *cd){ |
| 476 | SqliteDb *pDb = (SqliteDb*)cd; |
| 477 | int rc; |
| 478 | |
| 479 | assert( pDb->zProgress ); |
| 480 | rc = Tcl_Eval(pDb->interp, pDb->zProgress); |
| 481 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 482 | return 1; |
| 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 487 | #ifndef SQLITE_OMIT_TRACE |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 488 | /* |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 489 | ** This routine is called by the SQLite trace handler whenever a new |
| 490 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 491 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 492 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 493 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 494 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 495 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 496 | Tcl_DStringInit(&str); |
| 497 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 498 | Tcl_DStringAppendElement(&str, zSql); |
| 499 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 500 | Tcl_DStringFree(&str); |
| 501 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 502 | } |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 503 | #endif |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 504 | |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 505 | #ifndef SQLITE_OMIT_TRACE |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 506 | /* |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 507 | ** This routine is called by the SQLite profile handler after a statement |
| 508 | ** SQL has executed. The TCL script in pDb->zProfile is evaluated. |
| 509 | */ |
| 510 | static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ |
| 511 | SqliteDb *pDb = (SqliteDb*)cd; |
| 512 | Tcl_DString str; |
| 513 | char zTm[100]; |
| 514 | |
| 515 | sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); |
| 516 | Tcl_DStringInit(&str); |
| 517 | Tcl_DStringAppend(&str, pDb->zProfile, -1); |
| 518 | Tcl_DStringAppendElement(&str, zSql); |
| 519 | Tcl_DStringAppendElement(&str, zTm); |
| 520 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 521 | Tcl_DStringFree(&str); |
| 522 | Tcl_ResetResult(pDb->interp); |
| 523 | } |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 524 | #endif |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 525 | |
| 526 | /* |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 527 | ** This routine is called when a transaction is committed. The |
| 528 | ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
| 529 | ** if it throws an exception, the transaction is rolled back instead |
| 530 | ** of being committed. |
| 531 | */ |
| 532 | static int DbCommitHandler(void *cd){ |
| 533 | SqliteDb *pDb = (SqliteDb*)cd; |
| 534 | int rc; |
| 535 | |
| 536 | rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
| 537 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 538 | return 1; |
| 539 | } |
| 540 | return 0; |
| 541 | } |
| 542 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 543 | static void DbRollbackHandler(void *clientData){ |
| 544 | SqliteDb *pDb = (SqliteDb*)clientData; |
| 545 | assert(pDb->pRollbackHook); |
| 546 | if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ |
| 547 | Tcl_BackgroundError(pDb->interp); |
| 548 | } |
| 549 | } |
| 550 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 551 | static void DbUpdateHandler( |
| 552 | void *p, |
| 553 | int op, |
| 554 | const char *zDb, |
| 555 | const char *zTbl, |
| 556 | sqlite_int64 rowid |
| 557 | ){ |
| 558 | SqliteDb *pDb = (SqliteDb *)p; |
| 559 | Tcl_Obj *pCmd; |
| 560 | |
| 561 | assert( pDb->pUpdateHook ); |
| 562 | assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); |
| 563 | |
| 564 | pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); |
| 565 | Tcl_IncrRefCount(pCmd); |
| 566 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( |
| 567 | ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); |
| 568 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); |
| 569 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); |
| 570 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); |
| 571 | Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); |
| 572 | } |
| 573 | |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 574 | static void tclCollateNeeded( |
| 575 | void *pCtx, |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 576 | sqlite3 *db, |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 577 | int enc, |
| 578 | const char *zName |
| 579 | ){ |
| 580 | SqliteDb *pDb = (SqliteDb *)pCtx; |
| 581 | Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); |
| 582 | Tcl_IncrRefCount(pScript); |
| 583 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); |
| 584 | Tcl_EvalObjEx(pDb->interp, pScript, 0); |
| 585 | Tcl_DecrRefCount(pScript); |
| 586 | } |
| 587 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 588 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 589 | ** This routine is called to evaluate an SQL collation function implemented |
| 590 | ** using TCL script. |
| 591 | */ |
| 592 | static int tclSqlCollate( |
| 593 | void *pCtx, |
| 594 | int nA, |
| 595 | const void *zA, |
| 596 | int nB, |
| 597 | const void *zB |
| 598 | ){ |
| 599 | SqlCollate *p = (SqlCollate *)pCtx; |
| 600 | Tcl_Obj *pCmd; |
| 601 | |
| 602 | pCmd = Tcl_NewStringObj(p->zScript, -1); |
| 603 | Tcl_IncrRefCount(pCmd); |
| 604 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); |
| 605 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 606 | Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 607 | Tcl_DecrRefCount(pCmd); |
| 608 | return (atoi(Tcl_GetStringResult(p->interp))); |
| 609 | } |
| 610 | |
| 611 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 612 | ** This routine is called to evaluate an SQL function implemented |
| 613 | ** using TCL script. |
| 614 | */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 615 | static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 616 | SqlFunc *p = sqlite3_user_data(context); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 617 | Tcl_Obj *pCmd; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 618 | int i; |
| 619 | int rc; |
| 620 | |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 621 | if( argc==0 ){ |
| 622 | /* If there are no arguments to the function, call Tcl_EvalObjEx on the |
| 623 | ** script object directly. This allows the TCL compiler to generate |
| 624 | ** bytecode for the command on the first invocation and thus make |
| 625 | ** subsequent invocations much faster. */ |
| 626 | pCmd = p->pScript; |
| 627 | Tcl_IncrRefCount(pCmd); |
| 628 | rc = Tcl_EvalObjEx(p->interp, pCmd, 0); |
| 629 | Tcl_DecrRefCount(pCmd); |
| 630 | }else{ |
| 631 | /* If there are arguments to the function, make a shallow copy of the |
| 632 | ** script object, lappend the arguments, then evaluate the copy. |
| 633 | ** |
| 634 | ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated. |
| 635 | ** The new Tcl_Obj contains pointers to the original list elements. |
| 636 | ** That way, when Tcl_EvalObjv() is run and shimmers the first element |
| 637 | ** of the list to tclCmdNameType, that alternate representation will |
| 638 | ** be preserved and reused on the next invocation. |
| 639 | */ |
| 640 | Tcl_Obj **aArg; |
| 641 | int nArg; |
| 642 | if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ |
| 643 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 644 | return; |
| 645 | } |
| 646 | pCmd = Tcl_NewListObj(nArg, aArg); |
| 647 | Tcl_IncrRefCount(pCmd); |
| 648 | for(i=0; i<argc; i++){ |
| 649 | sqlite3_value *pIn = argv[i]; |
| 650 | Tcl_Obj *pVal; |
| 651 | |
| 652 | /* Set pVal to contain the i'th column of this row. */ |
| 653 | switch( sqlite3_value_type(pIn) ){ |
| 654 | case SQLITE_BLOB: { |
| 655 | int bytes = sqlite3_value_bytes(pIn); |
| 656 | pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); |
| 657 | break; |
| 658 | } |
| 659 | case SQLITE_INTEGER: { |
| 660 | sqlite_int64 v = sqlite3_value_int64(pIn); |
| 661 | if( v>=-2147483647 && v<=2147483647 ){ |
| 662 | pVal = Tcl_NewIntObj(v); |
| 663 | }else{ |
| 664 | pVal = Tcl_NewWideIntObj(v); |
| 665 | } |
| 666 | break; |
| 667 | } |
| 668 | case SQLITE_FLOAT: { |
| 669 | double r = sqlite3_value_double(pIn); |
| 670 | pVal = Tcl_NewDoubleObj(r); |
| 671 | break; |
| 672 | } |
| 673 | case SQLITE_NULL: { |
| 674 | pVal = Tcl_NewStringObj("", 0); |
| 675 | break; |
| 676 | } |
| 677 | default: { |
| 678 | int bytes = sqlite3_value_bytes(pIn); |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 679 | pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 680 | break; |
| 681 | } |
| 682 | } |
| 683 | rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); |
| 684 | if( rc ){ |
| 685 | Tcl_DecrRefCount(pCmd); |
| 686 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 687 | return; |
| 688 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 689 | } |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 690 | if( !p->useEvalObjv ){ |
| 691 | /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd |
| 692 | ** is a list without a string representation. To prevent this from |
| 693 | ** happening, make sure pCmd has a valid string representation */ |
| 694 | Tcl_GetString(pCmd); |
| 695 | } |
| 696 | rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
| 697 | Tcl_DecrRefCount(pCmd); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 698 | } |
danielk1977 | 562e8d3 | 2005-05-20 09:40:55 +0000 | [diff] [blame] | 699 | |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 700 | if( rc && rc!=TCL_RETURN ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 701 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 702 | }else{ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 703 | Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); |
| 704 | int n; |
| 705 | u8 *data; |
| 706 | char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
| 707 | char c = zType[0]; |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 708 | if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 709 | /* Only return a BLOB type if the Tcl variable is a bytearray and |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 710 | ** has no string representation. */ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 711 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 712 | sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 713 | }else if( (c=='b' && strcmp(zType,"boolean")==0) || |
| 714 | (c=='i' && strcmp(zType,"int")==0) ){ |
| 715 | Tcl_GetIntFromObj(0, pVar, &n); |
| 716 | sqlite3_result_int(context, n); |
| 717 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 718 | double r; |
| 719 | Tcl_GetDoubleFromObj(0, pVar, &r); |
| 720 | sqlite3_result_double(context, r); |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 721 | }else if( c=='w' && strcmp(zType,"wideInt")==0 ){ |
| 722 | Tcl_WideInt v; |
| 723 | Tcl_GetWideIntFromObj(0, pVar, &v); |
| 724 | sqlite3_result_int64(context, v); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 725 | }else{ |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 726 | data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
| 727 | sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 728 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 731 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 732 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 733 | /* |
| 734 | ** This is the authentication function. It appends the authentication |
| 735 | ** type code and the two arguments to zCmd[] then invokes the result |
| 736 | ** on the interpreter. The reply is examined to determine if the |
| 737 | ** authentication fails or succeeds. |
| 738 | */ |
| 739 | static int auth_callback( |
| 740 | void *pArg, |
| 741 | int code, |
| 742 | const char *zArg1, |
| 743 | const char *zArg2, |
| 744 | const char *zArg3, |
| 745 | const char *zArg4 |
| 746 | ){ |
| 747 | char *zCode; |
| 748 | Tcl_DString str; |
| 749 | int rc; |
| 750 | const char *zReply; |
| 751 | SqliteDb *pDb = (SqliteDb*)pArg; |
| 752 | |
| 753 | switch( code ){ |
| 754 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 755 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 756 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 757 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 758 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 759 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 760 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 761 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 762 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 763 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 764 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 765 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 766 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 767 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 768 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 769 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 770 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 771 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 772 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 773 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 774 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 775 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 776 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 777 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
drh | 81e293b | 2003-06-06 19:00:42 +0000 | [diff] [blame] | 778 | case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
| 779 | case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
danielk1977 | 1c8c23c | 2004-11-12 15:53:37 +0000 | [diff] [blame] | 780 | case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 781 | case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 782 | case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 783 | case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; |
| 784 | case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 785 | case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 786 | default : zCode="????"; break; |
| 787 | } |
| 788 | Tcl_DStringInit(&str); |
| 789 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 790 | Tcl_DStringAppendElement(&str, zCode); |
| 791 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 792 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 793 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 794 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 795 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 796 | Tcl_DStringFree(&str); |
| 797 | zReply = Tcl_GetStringResult(pDb->interp); |
| 798 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 799 | rc = SQLITE_OK; |
| 800 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 801 | rc = SQLITE_DENY; |
| 802 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 803 | rc = SQLITE_IGNORE; |
| 804 | }else{ |
| 805 | rc = 999; |
| 806 | } |
| 807 | return rc; |
| 808 | } |
| 809 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 810 | |
| 811 | /* |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 812 | ** zText is a pointer to text obtained via an sqlite3_result_text() |
| 813 | ** or similar interface. This routine returns a Tcl string object, |
| 814 | ** reference count set to 0, containing the text. If a translation |
| 815 | ** between iso8859 and UTF-8 is required, it is preformed. |
| 816 | */ |
| 817 | static Tcl_Obj *dbTextToObj(char const *zText){ |
| 818 | Tcl_Obj *pVal; |
| 819 | #ifdef UTF_TRANSLATION_NEEDED |
| 820 | Tcl_DString dCol; |
| 821 | Tcl_DStringInit(&dCol); |
| 822 | Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); |
| 823 | pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 824 | Tcl_DStringFree(&dCol); |
| 825 | #else |
| 826 | pVal = Tcl_NewStringObj(zText, -1); |
| 827 | #endif |
| 828 | return pVal; |
| 829 | } |
| 830 | |
| 831 | /* |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 832 | ** This routine reads a line of text from FILE in, stores |
| 833 | ** the text in memory obtained from malloc() and returns a pointer |
| 834 | ** to the text. NULL is returned at end of file, or if malloc() |
| 835 | ** fails. |
| 836 | ** |
| 837 | ** The interface is like "readline" but no command-line editing |
| 838 | ** is done. |
| 839 | ** |
| 840 | ** copied from shell.c from '.import' command |
| 841 | */ |
| 842 | static char *local_getline(char *zPrompt, FILE *in){ |
| 843 | char *zLine; |
| 844 | int nLine; |
| 845 | int n; |
| 846 | int eol; |
| 847 | |
| 848 | nLine = 100; |
| 849 | zLine = malloc( nLine ); |
| 850 | if( zLine==0 ) return 0; |
| 851 | n = 0; |
| 852 | eol = 0; |
| 853 | while( !eol ){ |
| 854 | if( n+100>nLine ){ |
| 855 | nLine = nLine*2 + 100; |
| 856 | zLine = realloc(zLine, nLine); |
| 857 | if( zLine==0 ) return 0; |
| 858 | } |
| 859 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 860 | if( n==0 ){ |
| 861 | free(zLine); |
| 862 | return 0; |
| 863 | } |
| 864 | zLine[n] = 0; |
| 865 | eol = 1; |
| 866 | break; |
| 867 | } |
| 868 | while( zLine[n] ){ n++; } |
| 869 | if( n>0 && zLine[n-1]=='\n' ){ |
| 870 | n--; |
| 871 | zLine[n] = 0; |
| 872 | eol = 1; |
| 873 | } |
| 874 | } |
| 875 | zLine = realloc( zLine, n+1 ); |
| 876 | return zLine; |
| 877 | } |
| 878 | |
| 879 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 880 | ** The "sqlite" command below creates a new Tcl command for each |
| 881 | ** connection it opens to an SQLite database. This routine is invoked |
| 882 | ** whenever one of those connection-specific commands is executed |
| 883 | ** in Tcl. For example, if you run Tcl code like this: |
| 884 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 885 | ** sqlite3 db1 "my_database" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 886 | ** db1 close |
| 887 | ** |
| 888 | ** The first command opens a connection to the "my_database" database |
| 889 | ** and calls that connection "db1". The second command causes this |
| 890 | ** subroutine to be invoked. |
| 891 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 892 | static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 893 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 894 | int choice; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 895 | int rc = TCL_OK; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 896 | static const char *DB_strs[] = { |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 897 | "authorizer", "busy", "cache", |
| 898 | "changes", "close", "collate", |
| 899 | "collation_needed", "commit_hook", "complete", |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 900 | "copy", "enable_load_extension","errorcode", |
| 901 | "eval", "exists", "function", |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 902 | "incrblob", |
drh | f11bded | 2006-07-17 00:02:44 +0000 | [diff] [blame] | 903 | "interrupt", "last_insert_rowid", "nullvalue", |
| 904 | "onecolumn", "profile", "progress", |
| 905 | "rekey", "rollback_hook", "timeout", |
| 906 | "total_changes", "trace", "transaction", |
| 907 | "update_hook", "version", 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 908 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 909 | enum DB_enum { |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 910 | DB_AUTHORIZER, DB_BUSY, DB_CACHE, |
| 911 | DB_CHANGES, DB_CLOSE, DB_COLLATE, |
| 912 | DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE, |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 913 | DB_COPY, DB_ENABLE_LOAD_EXTENSION,DB_ERRORCODE, |
| 914 | DB_EVAL, DB_EXISTS, DB_FUNCTION, |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 915 | DB_INCRBLOB, |
drh | f11bded | 2006-07-17 00:02:44 +0000 | [diff] [blame] | 916 | DB_INTERRUPT, DB_LAST_INSERT_ROWID,DB_NULLVALUE, |
| 917 | DB_ONECOLUMN, DB_PROFILE, DB_PROGRESS, |
| 918 | DB_REKEY, DB_ROLLBACK_HOOK, DB_TIMEOUT, |
| 919 | DB_TOTAL_CHANGES, DB_TRACE, DB_TRANSACTION, |
| 920 | DB_UPDATE_HOOK, DB_VERSION, |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 921 | }; |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 922 | /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 923 | |
| 924 | if( objc<2 ){ |
| 925 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 926 | return TCL_ERROR; |
| 927 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 928 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 929 | return TCL_ERROR; |
| 930 | } |
| 931 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 932 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 933 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 934 | /* $db authorizer ?CALLBACK? |
| 935 | ** |
| 936 | ** Invoke the given callback to authorize each SQL operation as it is |
| 937 | ** compiled. 5 arguments are appended to the callback before it is |
| 938 | ** invoked: |
| 939 | ** |
| 940 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 941 | ** (2) First descriptive name (depends on authorization type) |
| 942 | ** (3) Second descriptive name |
| 943 | ** (4) Name of the database (ex: "main", "temp") |
| 944 | ** (5) Name of trigger that is doing the access |
| 945 | ** |
| 946 | ** The callback should return on of the following strings: SQLITE_OK, |
| 947 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 948 | ** |
| 949 | ** If this method is invoked with no arguments, the current authorization |
| 950 | ** callback string is returned. |
| 951 | */ |
| 952 | case DB_AUTHORIZER: { |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 953 | #ifdef SQLITE_OMIT_AUTHORIZATION |
| 954 | Tcl_AppendResult(interp, "authorization not available in this build", 0); |
| 955 | return TCL_ERROR; |
| 956 | #else |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 957 | if( objc>3 ){ |
| 958 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 959 | return TCL_ERROR; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 960 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 961 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 962 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 963 | } |
| 964 | }else{ |
| 965 | char *zAuth; |
| 966 | int len; |
| 967 | if( pDb->zAuth ){ |
| 968 | Tcl_Free(pDb->zAuth); |
| 969 | } |
| 970 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 971 | if( zAuth && len>0 ){ |
| 972 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
| 973 | strcpy(pDb->zAuth, zAuth); |
| 974 | }else{ |
| 975 | pDb->zAuth = 0; |
| 976 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 977 | if( pDb->zAuth ){ |
| 978 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 979 | sqlite3_set_authorizer(pDb->db, auth_callback, pDb); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 980 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 981 | sqlite3_set_authorizer(pDb->db, 0, 0); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 982 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 983 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 984 | #endif |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
| 987 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 988 | /* $db busy ?CALLBACK? |
| 989 | ** |
| 990 | ** Invoke the given callback if an SQL statement attempts to open |
| 991 | ** a locked database file. |
| 992 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 993 | case DB_BUSY: { |
| 994 | if( objc>3 ){ |
| 995 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 996 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 997 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 998 | if( pDb->zBusy ){ |
| 999 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 1000 | } |
| 1001 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1002 | char *zBusy; |
| 1003 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1004 | if( pDb->zBusy ){ |
| 1005 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1006 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1007 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 1008 | if( zBusy && len>0 ){ |
| 1009 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 1010 | strcpy(pDb->zBusy, zBusy); |
| 1011 | }else{ |
| 1012 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1013 | } |
| 1014 | if( pDb->zBusy ){ |
| 1015 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1016 | sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1017 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1018 | sqlite3_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1023 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1024 | /* $db cache flush |
| 1025 | ** $db cache size n |
| 1026 | ** |
| 1027 | ** Flush the prepared statement cache, or set the maximum number of |
| 1028 | ** cached statements. |
| 1029 | */ |
| 1030 | case DB_CACHE: { |
| 1031 | char *subCmd; |
| 1032 | int n; |
| 1033 | |
| 1034 | if( objc<=2 ){ |
| 1035 | Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); |
| 1036 | return TCL_ERROR; |
| 1037 | } |
| 1038 | subCmd = Tcl_GetStringFromObj( objv[2], 0 ); |
| 1039 | if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ |
| 1040 | if( objc!=3 ){ |
| 1041 | Tcl_WrongNumArgs(interp, 2, objv, "flush"); |
| 1042 | return TCL_ERROR; |
| 1043 | }else{ |
| 1044 | flushStmtCache( pDb ); |
| 1045 | } |
| 1046 | }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ |
| 1047 | if( objc!=4 ){ |
| 1048 | Tcl_WrongNumArgs(interp, 2, objv, "size n"); |
| 1049 | return TCL_ERROR; |
| 1050 | }else{ |
| 1051 | if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ |
| 1052 | Tcl_AppendResult( interp, "cannot convert \"", |
| 1053 | Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0); |
| 1054 | return TCL_ERROR; |
| 1055 | }else{ |
| 1056 | if( n<0 ){ |
| 1057 | flushStmtCache( pDb ); |
| 1058 | n = 0; |
| 1059 | }else if( n>MAX_PREPARED_STMTS ){ |
| 1060 | n = MAX_PREPARED_STMTS; |
| 1061 | } |
| 1062 | pDb->maxStmt = n; |
| 1063 | } |
| 1064 | } |
| 1065 | }else{ |
| 1066 | Tcl_AppendResult( interp, "bad option \"", |
| 1067 | Tcl_GetStringFromObj(objv[0],0), "\": must be flush or size", 0); |
| 1068 | return TCL_ERROR; |
| 1069 | } |
| 1070 | break; |
| 1071 | } |
| 1072 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1073 | /* $db changes |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1074 | ** |
| 1075 | ** Return the number of rows that were modified, inserted, or deleted by |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1076 | ** the most recent INSERT, UPDATE or DELETE statement, not including |
| 1077 | ** any changes made by trigger programs. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1078 | */ |
| 1079 | case DB_CHANGES: { |
| 1080 | Tcl_Obj *pResult; |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1081 | if( objc!=2 ){ |
| 1082 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 1083 | return TCL_ERROR; |
| 1084 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1085 | pResult = Tcl_GetObjResult(interp); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1086 | Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | } |
| 1089 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1090 | /* $db close |
| 1091 | ** |
| 1092 | ** Shutdown the database |
| 1093 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1094 | case DB_CLOSE: { |
| 1095 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 1096 | break; |
| 1097 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1098 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1099 | /* |
| 1100 | ** $db collate NAME SCRIPT |
| 1101 | ** |
| 1102 | ** Create a new SQL collation function called NAME. Whenever |
| 1103 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 1104 | */ |
| 1105 | case DB_COLLATE: { |
| 1106 | SqlCollate *pCollate; |
| 1107 | char *zName; |
| 1108 | char *zScript; |
| 1109 | int nScript; |
| 1110 | if( objc!=4 ){ |
| 1111 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 1112 | return TCL_ERROR; |
| 1113 | } |
| 1114 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 1115 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 1116 | pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); |
| 1117 | if( pCollate==0 ) return TCL_ERROR; |
| 1118 | pCollate->interp = interp; |
| 1119 | pCollate->pNext = pDb->pCollate; |
| 1120 | pCollate->zScript = (char*)&pCollate[1]; |
| 1121 | pDb->pCollate = pCollate; |
| 1122 | strcpy(pCollate->zScript, zScript); |
| 1123 | if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, |
| 1124 | pCollate, tclSqlCollate) ){ |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1125 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1126 | return TCL_ERROR; |
| 1127 | } |
| 1128 | break; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | ** $db collation_needed SCRIPT |
| 1133 | ** |
| 1134 | ** Create a new SQL collation function called NAME. Whenever |
| 1135 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 1136 | */ |
| 1137 | case DB_COLLATION_NEEDED: { |
| 1138 | if( objc!=3 ){ |
| 1139 | Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); |
| 1140 | return TCL_ERROR; |
| 1141 | } |
| 1142 | if( pDb->pCollateNeeded ){ |
| 1143 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 1144 | } |
| 1145 | pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); |
| 1146 | Tcl_IncrRefCount(pDb->pCollateNeeded); |
| 1147 | sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); |
| 1148 | break; |
| 1149 | } |
| 1150 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1151 | /* $db commit_hook ?CALLBACK? |
| 1152 | ** |
| 1153 | ** Invoke the given callback just before committing every SQL transaction. |
| 1154 | ** If the callback throws an exception or returns non-zero, then the |
| 1155 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 1156 | ** is disabled. |
| 1157 | */ |
| 1158 | case DB_COMMIT_HOOK: { |
| 1159 | if( objc>3 ){ |
| 1160 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 1161 | return TCL_ERROR; |
| 1162 | }else if( objc==2 ){ |
| 1163 | if( pDb->zCommit ){ |
| 1164 | Tcl_AppendResult(interp, pDb->zCommit, 0); |
| 1165 | } |
| 1166 | }else{ |
| 1167 | char *zCommit; |
| 1168 | int len; |
| 1169 | if( pDb->zCommit ){ |
| 1170 | Tcl_Free(pDb->zCommit); |
| 1171 | } |
| 1172 | zCommit = Tcl_GetStringFromObj(objv[2], &len); |
| 1173 | if( zCommit && len>0 ){ |
| 1174 | pDb->zCommit = Tcl_Alloc( len + 1 ); |
| 1175 | strcpy(pDb->zCommit, zCommit); |
| 1176 | }else{ |
| 1177 | pDb->zCommit = 0; |
| 1178 | } |
| 1179 | if( pDb->zCommit ){ |
| 1180 | pDb->interp = interp; |
| 1181 | sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); |
| 1182 | }else{ |
| 1183 | sqlite3_commit_hook(pDb->db, 0, 0); |
| 1184 | } |
| 1185 | } |
| 1186 | break; |
| 1187 | } |
| 1188 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1189 | /* $db complete SQL |
| 1190 | ** |
| 1191 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 1192 | ** additional lines of input are needed. This is similar to the |
| 1193 | ** built-in "info complete" command of Tcl. |
| 1194 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1195 | case DB_COMPLETE: { |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 1196 | #ifndef SQLITE_OMIT_COMPLETE |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1197 | Tcl_Obj *pResult; |
| 1198 | int isComplete; |
| 1199 | if( objc!=3 ){ |
| 1200 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1201 | return TCL_ERROR; |
| 1202 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1203 | isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1204 | pResult = Tcl_GetObjResult(interp); |
| 1205 | Tcl_SetBooleanObj(pResult, isComplete); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 1206 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1209 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1210 | /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? |
| 1211 | ** |
| 1212 | ** Copy data into table from filename, optionally using SEPARATOR |
| 1213 | ** as column separators. If a column contains a null string, or the |
| 1214 | ** value of NULLINDICATOR, a NULL is inserted for the column. |
| 1215 | ** conflict-algorithm is one of the sqlite conflict algorithms: |
| 1216 | ** rollback, abort, fail, ignore, replace |
| 1217 | ** On success, return the number of lines processed, not necessarily same |
| 1218 | ** as 'db changes' due to conflict-algorithm selected. |
| 1219 | ** |
| 1220 | ** This code is basically an implementation/enhancement of |
| 1221 | ** the sqlite3 shell.c ".import" command. |
| 1222 | ** |
| 1223 | ** This command usage is equivalent to the sqlite2.x COPY statement, |
| 1224 | ** which imports file data into a table using the PostgreSQL COPY file format: |
| 1225 | ** $db copy $conflit_algo $table_name $filename \t \\N |
| 1226 | */ |
| 1227 | case DB_COPY: { |
| 1228 | char *zTable; /* Insert data into this table */ |
| 1229 | char *zFile; /* The file from which to extract data */ |
| 1230 | char *zConflict; /* The conflict algorithm to use */ |
| 1231 | sqlite3_stmt *pStmt; /* A statement */ |
| 1232 | int rc; /* Result code */ |
| 1233 | int nCol; /* Number of columns in the table */ |
| 1234 | int nByte; /* Number of bytes in an SQL string */ |
| 1235 | int i, j; /* Loop counters */ |
| 1236 | int nSep; /* Number of bytes in zSep[] */ |
| 1237 | int nNull; /* Number of bytes in zNull[] */ |
| 1238 | char *zSql; /* An SQL statement */ |
| 1239 | char *zLine; /* A single line of input from the file */ |
| 1240 | char **azCol; /* zLine[] broken up into columns */ |
| 1241 | char *zCommit; /* How to commit changes */ |
| 1242 | FILE *in; /* The input file */ |
| 1243 | int lineno = 0; /* Line number of input file */ |
| 1244 | char zLineNum[80]; /* Line number print buffer */ |
| 1245 | Tcl_Obj *pResult; /* interp result */ |
| 1246 | |
| 1247 | char *zSep; |
| 1248 | char *zNull; |
| 1249 | if( objc<5 || objc>7 ){ |
| 1250 | Tcl_WrongNumArgs(interp, 2, objv, |
| 1251 | "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); |
| 1252 | return TCL_ERROR; |
| 1253 | } |
| 1254 | if( objc>=6 ){ |
| 1255 | zSep = Tcl_GetStringFromObj(objv[5], 0); |
| 1256 | }else{ |
| 1257 | zSep = "\t"; |
| 1258 | } |
| 1259 | if( objc>=7 ){ |
| 1260 | zNull = Tcl_GetStringFromObj(objv[6], 0); |
| 1261 | }else{ |
| 1262 | zNull = ""; |
| 1263 | } |
| 1264 | zConflict = Tcl_GetStringFromObj(objv[2], 0); |
| 1265 | zTable = Tcl_GetStringFromObj(objv[3], 0); |
| 1266 | zFile = Tcl_GetStringFromObj(objv[4], 0); |
| 1267 | nSep = strlen(zSep); |
| 1268 | nNull = strlen(zNull); |
| 1269 | if( nSep==0 ){ |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 1270 | Tcl_AppendResult(interp,"Error: non-null separator required for copy",0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1271 | return TCL_ERROR; |
| 1272 | } |
| 1273 | if(sqlite3StrICmp(zConflict, "rollback") != 0 && |
| 1274 | sqlite3StrICmp(zConflict, "abort" ) != 0 && |
| 1275 | sqlite3StrICmp(zConflict, "fail" ) != 0 && |
| 1276 | sqlite3StrICmp(zConflict, "ignore" ) != 0 && |
| 1277 | sqlite3StrICmp(zConflict, "replace" ) != 0 ) { |
| 1278 | Tcl_AppendResult(interp, "Error: \"", zConflict, |
| 1279 | "\", conflict-algorithm must be one of: rollback, " |
| 1280 | "abort, fail, ignore, or replace", 0); |
| 1281 | return TCL_ERROR; |
| 1282 | } |
| 1283 | zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); |
| 1284 | if( zSql==0 ){ |
| 1285 | Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0); |
| 1286 | return TCL_ERROR; |
| 1287 | } |
| 1288 | nByte = strlen(zSql); |
drh | 3e701a1 | 2007-02-01 01:53:44 +0000 | [diff] [blame] | 1289 | rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1290 | sqlite3_free(zSql); |
| 1291 | if( rc ){ |
| 1292 | Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
| 1293 | nCol = 0; |
| 1294 | }else{ |
| 1295 | nCol = sqlite3_column_count(pStmt); |
| 1296 | } |
| 1297 | sqlite3_finalize(pStmt); |
| 1298 | if( nCol==0 ) { |
| 1299 | return TCL_ERROR; |
| 1300 | } |
| 1301 | zSql = malloc( nByte + 50 + nCol*2 ); |
| 1302 | if( zSql==0 ) { |
| 1303 | Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
| 1304 | return TCL_ERROR; |
| 1305 | } |
| 1306 | sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", |
| 1307 | zConflict, zTable); |
| 1308 | j = strlen(zSql); |
| 1309 | for(i=1; i<nCol; i++){ |
| 1310 | zSql[j++] = ','; |
| 1311 | zSql[j++] = '?'; |
| 1312 | } |
| 1313 | zSql[j++] = ')'; |
| 1314 | zSql[j] = 0; |
drh | 3e701a1 | 2007-02-01 01:53:44 +0000 | [diff] [blame] | 1315 | rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1316 | free(zSql); |
| 1317 | if( rc ){ |
| 1318 | Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
| 1319 | sqlite3_finalize(pStmt); |
| 1320 | return TCL_ERROR; |
| 1321 | } |
| 1322 | in = fopen(zFile, "rb"); |
| 1323 | if( in==0 ){ |
| 1324 | Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); |
| 1325 | sqlite3_finalize(pStmt); |
| 1326 | return TCL_ERROR; |
| 1327 | } |
| 1328 | azCol = malloc( sizeof(azCol[0])*(nCol+1) ); |
| 1329 | if( azCol==0 ) { |
| 1330 | Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1331 | fclose(in); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1332 | return TCL_ERROR; |
| 1333 | } |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 1334 | (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1335 | zCommit = "COMMIT"; |
| 1336 | while( (zLine = local_getline(0, in))!=0 ){ |
| 1337 | char *z; |
| 1338 | i = 0; |
| 1339 | lineno++; |
| 1340 | azCol[0] = zLine; |
| 1341 | for(i=0, z=zLine; *z; z++){ |
| 1342 | if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ |
| 1343 | *z = 0; |
| 1344 | i++; |
| 1345 | if( i<nCol ){ |
| 1346 | azCol[i] = &z[nSep]; |
| 1347 | z += nSep-1; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | if( i+1!=nCol ){ |
| 1352 | char *zErr; |
| 1353 | zErr = malloc(200 + strlen(zFile)); |
drh | c1f4494 | 2006-05-10 14:39:13 +0000 | [diff] [blame] | 1354 | if( zErr ){ |
| 1355 | sprintf(zErr, |
| 1356 | "Error: %s line %d: expected %d columns of data but found %d", |
| 1357 | zFile, lineno, nCol, i+1); |
| 1358 | Tcl_AppendResult(interp, zErr, 0); |
| 1359 | free(zErr); |
| 1360 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1361 | zCommit = "ROLLBACK"; |
| 1362 | break; |
| 1363 | } |
| 1364 | for(i=0; i<nCol; i++){ |
| 1365 | /* check for null data, if so, bind as null */ |
| 1366 | if ((nNull>0 && strcmp(azCol[i], zNull)==0) || strlen(azCol[i])==0) { |
| 1367 | sqlite3_bind_null(pStmt, i+1); |
| 1368 | }else{ |
| 1369 | sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
| 1370 | } |
| 1371 | } |
| 1372 | sqlite3_step(pStmt); |
| 1373 | rc = sqlite3_reset(pStmt); |
| 1374 | free(zLine); |
| 1375 | if( rc!=SQLITE_OK ){ |
| 1376 | Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0); |
| 1377 | zCommit = "ROLLBACK"; |
| 1378 | break; |
| 1379 | } |
| 1380 | } |
| 1381 | free(azCol); |
| 1382 | fclose(in); |
| 1383 | sqlite3_finalize(pStmt); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 1384 | (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1385 | |
| 1386 | if( zCommit[0] == 'C' ){ |
| 1387 | /* success, set result as number of lines processed */ |
| 1388 | pResult = Tcl_GetObjResult(interp); |
| 1389 | Tcl_SetIntObj(pResult, lineno); |
| 1390 | rc = TCL_OK; |
| 1391 | }else{ |
| 1392 | /* failure, append lineno where failed */ |
| 1393 | sprintf(zLineNum,"%d",lineno); |
| 1394 | Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0); |
| 1395 | rc = TCL_ERROR; |
| 1396 | } |
| 1397 | break; |
| 1398 | } |
| 1399 | |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1400 | /* |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 1401 | ** $db enable_load_extension BOOLEAN |
| 1402 | ** |
| 1403 | ** Turn the extension loading feature on or off. It if off by |
| 1404 | ** default. |
| 1405 | */ |
| 1406 | case DB_ENABLE_LOAD_EXTENSION: { |
drh | f533acc | 2006-12-19 18:57:11 +0000 | [diff] [blame] | 1407 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 1408 | int onoff; |
| 1409 | if( objc!=3 ){ |
| 1410 | Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); |
| 1411 | return TCL_ERROR; |
| 1412 | } |
| 1413 | if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
| 1414 | return TCL_ERROR; |
| 1415 | } |
| 1416 | sqlite3_enable_load_extension(pDb->db, onoff); |
| 1417 | break; |
drh | f533acc | 2006-12-19 18:57:11 +0000 | [diff] [blame] | 1418 | #else |
| 1419 | Tcl_AppendResult(interp, "extension loading is turned off at compile-time", |
| 1420 | 0); |
| 1421 | return TCL_ERROR; |
| 1422 | #endif |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1426 | ** $db errorcode |
| 1427 | ** |
| 1428 | ** Return the numeric error code that was returned by the most recent |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1429 | ** call to sqlite3_exec(). |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1430 | */ |
| 1431 | case DB_ERRORCODE: { |
danielk1977 | f3ce83f | 2004-06-14 11:43:46 +0000 | [diff] [blame] | 1432 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1433 | break; |
| 1434 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1435 | |
| 1436 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1437 | ** $db eval $sql ?array? ?{ ...code... }? |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1438 | ** $db onecolumn $sql |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1439 | ** |
| 1440 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1441 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1442 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 1443 | ** If "array" is an empty string, then the values are placed in variables |
| 1444 | ** that have the same name as the fields extracted by the query. |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1445 | ** |
| 1446 | ** The onecolumn method is the equivalent of: |
| 1447 | ** lindex [$db eval $sql] 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1448 | */ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1449 | case DB_ONECOLUMN: |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1450 | case DB_EVAL: |
| 1451 | case DB_EXISTS: { |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 1452 | char const *zSql; /* Next SQL statement to execute */ |
| 1453 | char const *zLeft; /* What is left after first stmt in zSql */ |
| 1454 | sqlite3_stmt *pStmt; /* Compiled SQL statment */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1455 | Tcl_Obj *pArray; /* Name of array into which results are written */ |
| 1456 | Tcl_Obj *pScript; /* Script to run for each result set */ |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1457 | Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */ |
| 1458 | int nParm; /* Number of entries used in apParm[] */ |
| 1459 | Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1460 | Tcl_Obj *pRet; /* Value to be returned */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1461 | SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */ |
| 1462 | int rc2; |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 1463 | |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1464 | if( choice==DB_EVAL ){ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1465 | if( objc<3 || objc>5 ){ |
| 1466 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
| 1467 | return TCL_ERROR; |
| 1468 | } |
| 1469 | pRet = Tcl_NewObj(); |
| 1470 | Tcl_IncrRefCount(pRet); |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1471 | }else{ |
| 1472 | if( objc!=3 ){ |
| 1473 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
| 1474 | return TCL_ERROR; |
| 1475 | } |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1476 | if( choice==DB_EXISTS ){ |
drh | 446a9b8 | 2006-01-04 18:13:26 +0000 | [diff] [blame] | 1477 | pRet = Tcl_NewBooleanObj(0); |
| 1478 | Tcl_IncrRefCount(pRet); |
| 1479 | }else{ |
| 1480 | pRet = 0; |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1481 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1482 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1483 | if( objc==3 ){ |
| 1484 | pArray = pScript = 0; |
| 1485 | }else if( objc==4 ){ |
| 1486 | pArray = 0; |
| 1487 | pScript = objv[3]; |
| 1488 | }else{ |
| 1489 | pArray = objv[3]; |
| 1490 | if( Tcl_GetString(pArray)[0]==0 ) pArray = 0; |
| 1491 | pScript = objv[4]; |
| 1492 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1493 | |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1494 | Tcl_IncrRefCount(objv[2]); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1495 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1496 | while( rc==TCL_OK && zSql[0] ){ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1497 | int i; /* Loop counter */ |
| 1498 | int nVar; /* Number of bind parameters in the pStmt */ |
| 1499 | int nCol; /* Number of columns in the result set */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1500 | Tcl_Obj **apColName = 0; /* Array of column names */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1501 | int len; /* String length of zSql */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1502 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1503 | /* Try to find a SQL statement that has already been compiled and |
| 1504 | ** which matches the next sequence of SQL. |
| 1505 | */ |
| 1506 | pStmt = 0; |
| 1507 | pPreStmt = pDb->stmtList; |
| 1508 | len = strlen(zSql); |
| 1509 | if( pPreStmt && sqlite3_expired(pPreStmt->pStmt) ){ |
| 1510 | flushStmtCache(pDb); |
| 1511 | pPreStmt = 0; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1512 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1513 | for(; pPreStmt; pPreStmt=pPreStmt->pNext){ |
| 1514 | int n = pPreStmt->nSql; |
| 1515 | if( len>=n |
| 1516 | && memcmp(pPreStmt->zSql, zSql, n)==0 |
| 1517 | && (zSql[n]==0 || zSql[n-1]==';') |
| 1518 | ){ |
| 1519 | pStmt = pPreStmt->pStmt; |
| 1520 | zLeft = &zSql[pPreStmt->nSql]; |
| 1521 | |
| 1522 | /* When a prepared statement is found, unlink it from the |
| 1523 | ** cache list. It will later be added back to the beginning |
| 1524 | ** of the cache list in order to implement LRU replacement. |
| 1525 | */ |
| 1526 | if( pPreStmt->pPrev ){ |
| 1527 | pPreStmt->pPrev->pNext = pPreStmt->pNext; |
| 1528 | }else{ |
| 1529 | pDb->stmtList = pPreStmt->pNext; |
| 1530 | } |
| 1531 | if( pPreStmt->pNext ){ |
| 1532 | pPreStmt->pNext->pPrev = pPreStmt->pPrev; |
| 1533 | }else{ |
| 1534 | pDb->stmtLast = pPreStmt->pPrev; |
| 1535 | } |
| 1536 | pDb->nStmt--; |
| 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | /* If no prepared statement was found. Compile the SQL text |
| 1542 | */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1543 | if( pStmt==0 ){ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1544 | if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1545 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 1546 | rc = TCL_ERROR; |
| 1547 | break; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1548 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1549 | if( pStmt==0 ){ |
| 1550 | if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
| 1551 | /* A compile-time error in the statement |
| 1552 | */ |
| 1553 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 1554 | rc = TCL_ERROR; |
| 1555 | break; |
| 1556 | }else{ |
| 1557 | /* The statement was a no-op. Continue to the next statement |
| 1558 | ** in the SQL string. |
| 1559 | */ |
| 1560 | zSql = zLeft; |
| 1561 | continue; |
| 1562 | } |
| 1563 | } |
| 1564 | assert( pPreStmt==0 ); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1567 | /* Bind values to parameters that begin with $ or : |
| 1568 | */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1569 | nVar = sqlite3_bind_parameter_count(pStmt); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1570 | nParm = 0; |
| 1571 | if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){ |
| 1572 | apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0])); |
| 1573 | }else{ |
| 1574 | apParm = aParm; |
| 1575 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1576 | for(i=1; i<=nVar; i++){ |
| 1577 | const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
drh | c8f9079 | 2005-01-12 00:08:24 +0000 | [diff] [blame] | 1578 | if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':') ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1579 | Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
| 1580 | if( pVar ){ |
| 1581 | int n; |
| 1582 | u8 *data; |
| 1583 | char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
| 1584 | char c = zType[0]; |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 1585 | if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ |
| 1586 | /* Only load a BLOB type if the Tcl variable is a bytearray and |
| 1587 | ** has no string representation. */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1588 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 1589 | sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1590 | Tcl_IncrRefCount(pVar); |
| 1591 | apParm[nParm++] = pVar; |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1592 | }else if( (c=='b' && strcmp(zType,"boolean")==0) || |
| 1593 | (c=='i' && strcmp(zType,"int")==0) ){ |
| 1594 | Tcl_GetIntFromObj(interp, pVar, &n); |
| 1595 | sqlite3_bind_int(pStmt, i, n); |
| 1596 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 1597 | double r; |
| 1598 | Tcl_GetDoubleFromObj(interp, pVar, &r); |
| 1599 | sqlite3_bind_double(pStmt, i, r); |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 1600 | }else if( c=='w' && strcmp(zType,"wideInt")==0 ){ |
| 1601 | Tcl_WideInt v; |
| 1602 | Tcl_GetWideIntFromObj(interp, pVar, &v); |
| 1603 | sqlite3_bind_int64(pStmt, i, v); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1604 | }else{ |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 1605 | data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
| 1606 | sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1607 | Tcl_IncrRefCount(pVar); |
| 1608 | apParm[nParm++] = pVar; |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1609 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1610 | }else{ |
| 1611 | sqlite3_bind_null( pStmt, i ); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1615 | |
| 1616 | /* Compute column names */ |
| 1617 | nCol = sqlite3_column_count(pStmt); |
| 1618 | if( pScript ){ |
| 1619 | apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
| 1620 | if( apColName==0 ) break; |
| 1621 | for(i=0; i<nCol; i++){ |
| 1622 | apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
| 1623 | Tcl_IncrRefCount(apColName[i]); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | /* If results are being stored in an array variable, then create |
| 1628 | ** the array(*) entry for that array |
| 1629 | */ |
| 1630 | if( pArray ){ |
| 1631 | Tcl_Obj *pColList = Tcl_NewObj(); |
drh | 3ced14a | 2005-03-31 18:26:20 +0000 | [diff] [blame] | 1632 | Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1633 | Tcl_IncrRefCount(pColList); |
| 1634 | for(i=0; i<nCol; i++){ |
| 1635 | Tcl_ListObjAppendElement(interp, pColList, apColName[i]); |
| 1636 | } |
drh | 3ced14a | 2005-03-31 18:26:20 +0000 | [diff] [blame] | 1637 | Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0); |
| 1638 | Tcl_DecrRefCount(pColList); |
| 1639 | Tcl_DecrRefCount(pStar); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /* Execute the SQL |
| 1643 | */ |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1644 | while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1645 | for(i=0; i<nCol; i++){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1646 | Tcl_Obj *pVal; |
| 1647 | |
| 1648 | /* Set pVal to contain the i'th column of this row. */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1649 | switch( sqlite3_column_type(pStmt, i) ){ |
| 1650 | case SQLITE_BLOB: { |
| 1651 | int bytes = sqlite3_column_bytes(pStmt, i); |
| 1652 | pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes); |
| 1653 | break; |
| 1654 | } |
| 1655 | case SQLITE_INTEGER: { |
| 1656 | sqlite_int64 v = sqlite3_column_int64(pStmt, i); |
| 1657 | if( v>=-2147483647 && v<=2147483647 ){ |
| 1658 | pVal = Tcl_NewIntObj(v); |
| 1659 | }else{ |
| 1660 | pVal = Tcl_NewWideIntObj(v); |
| 1661 | } |
| 1662 | break; |
| 1663 | } |
| 1664 | case SQLITE_FLOAT: { |
| 1665 | double r = sqlite3_column_double(pStmt, i); |
| 1666 | pVal = Tcl_NewDoubleObj(r); |
| 1667 | break; |
| 1668 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 1669 | case SQLITE_NULL: { |
| 1670 | pVal = dbTextToObj(pDb->zNull); |
| 1671 | break; |
| 1672 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1673 | default: { |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 1674 | pVal = dbTextToObj((char *)sqlite3_column_text(pStmt, i)); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1675 | break; |
| 1676 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1679 | if( pScript ){ |
| 1680 | if( pArray==0 ){ |
| 1681 | Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1682 | }else{ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1683 | Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1684 | } |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1685 | }else if( choice==DB_ONECOLUMN ){ |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1686 | assert( pRet==0 ); |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1687 | if( pRet==0 ){ |
| 1688 | pRet = pVal; |
| 1689 | Tcl_IncrRefCount(pRet); |
| 1690 | } |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1691 | rc = TCL_BREAK; |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1692 | i = nCol; |
| 1693 | }else if( choice==DB_EXISTS ){ |
drh | 446a9b8 | 2006-01-04 18:13:26 +0000 | [diff] [blame] | 1694 | Tcl_DecrRefCount(pRet); |
| 1695 | pRet = Tcl_NewBooleanObj(1); |
| 1696 | Tcl_IncrRefCount(pRet); |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1697 | rc = TCL_BREAK; |
| 1698 | i = nCol; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1699 | }else{ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1700 | Tcl_ListObjAppendElement(interp, pRet, pVal); |
| 1701 | } |
| 1702 | } |
| 1703 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1704 | if( pScript ){ |
| 1705 | rc = Tcl_EvalObjEx(interp, pScript, 0); |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1706 | if( rc==TCL_CONTINUE ){ |
| 1707 | rc = TCL_OK; |
| 1708 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1711 | if( rc==TCL_BREAK ){ |
| 1712 | rc = TCL_OK; |
| 1713 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1714 | |
| 1715 | /* Free the column name objects */ |
| 1716 | if( pScript ){ |
| 1717 | for(i=0; i<nCol; i++){ |
| 1718 | Tcl_DecrRefCount(apColName[i]); |
| 1719 | } |
| 1720 | Tcl_Free((char*)apColName); |
| 1721 | } |
| 1722 | |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1723 | /* Free the bound string and blob parameters */ |
| 1724 | for(i=0; i<nParm; i++){ |
| 1725 | Tcl_DecrRefCount(apParm[i]); |
| 1726 | } |
| 1727 | if( apParm!=aParm ){ |
| 1728 | Tcl_Free((char*)apParm); |
| 1729 | } |
| 1730 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1731 | /* Reset the statement. If the result code is SQLITE_SCHEMA, then |
| 1732 | ** flush the statement cache and try the statement again. |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1733 | */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1734 | rc2 = sqlite3_reset(pStmt); |
| 1735 | if( SQLITE_SCHEMA==rc2 ){ |
| 1736 | /* After a schema change, flush the cache and try to run the |
| 1737 | ** statement again |
| 1738 | */ |
| 1739 | flushStmtCache( pDb ); |
| 1740 | sqlite3_finalize(pStmt); |
| 1741 | if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1742 | continue; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1743 | }else if( SQLITE_OK!=rc2 ){ |
| 1744 | /* If a run-time error occurs, report the error and stop reading |
| 1745 | ** the SQL |
| 1746 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 1747 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1748 | sqlite3_finalize(pStmt); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1749 | rc = TCL_ERROR; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1750 | if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1751 | break; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1752 | }else if( pDb->maxStmt<=0 ){ |
| 1753 | /* If the cache is turned off, deallocated the statement */ |
| 1754 | if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
| 1755 | sqlite3_finalize(pStmt); |
| 1756 | }else{ |
| 1757 | /* Everything worked and the cache is operational. |
| 1758 | ** Create a new SqlPreparedStmt structure if we need one. |
| 1759 | ** (If we already have one we can just reuse it.) |
| 1760 | */ |
| 1761 | if( pPreStmt==0 ){ |
| 1762 | len = zLeft - zSql; |
| 1763 | pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) + len ); |
| 1764 | if( pPreStmt==0 ) return TCL_ERROR; |
| 1765 | pPreStmt->pStmt = pStmt; |
| 1766 | pPreStmt->nSql = len; |
| 1767 | memcpy(pPreStmt->zSql, zSql, len); |
| 1768 | pPreStmt->zSql[len] = 0; |
| 1769 | } |
| 1770 | |
| 1771 | /* Add the prepared statement to the beginning of the cache list |
| 1772 | */ |
| 1773 | pPreStmt->pNext = pDb->stmtList; |
| 1774 | pPreStmt->pPrev = 0; |
| 1775 | if( pDb->stmtList ){ |
| 1776 | pDb->stmtList->pPrev = pPreStmt; |
| 1777 | } |
| 1778 | pDb->stmtList = pPreStmt; |
| 1779 | if( pDb->stmtLast==0 ){ |
| 1780 | assert( pDb->nStmt==0 ); |
| 1781 | pDb->stmtLast = pPreStmt; |
| 1782 | }else{ |
| 1783 | assert( pDb->nStmt>0 ); |
| 1784 | } |
| 1785 | pDb->nStmt++; |
| 1786 | |
| 1787 | /* If we have too many statement in cache, remove the surplus from the |
| 1788 | ** end of the cache list. |
| 1789 | */ |
| 1790 | while( pDb->nStmt>pDb->maxStmt ){ |
| 1791 | sqlite3_finalize(pDb->stmtLast->pStmt); |
| 1792 | pDb->stmtLast = pDb->stmtLast->pPrev; |
| 1793 | Tcl_Free((char*)pDb->stmtLast->pNext); |
| 1794 | pDb->stmtLast->pNext = 0; |
| 1795 | pDb->nStmt--; |
| 1796 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1799 | /* Proceed to the next statement */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1800 | zSql = zLeft; |
| 1801 | } |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1802 | Tcl_DecrRefCount(objv[2]); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1803 | |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1804 | if( pRet ){ |
| 1805 | if( rc==TCL_OK ){ |
| 1806 | Tcl_SetObjResult(interp, pRet); |
| 1807 | } |
| 1808 | Tcl_DecrRefCount(pRet); |
drh | 050be32 | 2006-07-12 00:18:40 +0000 | [diff] [blame] | 1809 | }else if( rc==TCL_OK ){ |
| 1810 | Tcl_ResetResult(interp); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1811 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1812 | break; |
| 1813 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1814 | |
| 1815 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1816 | ** $db function NAME SCRIPT |
| 1817 | ** |
| 1818 | ** Create a new SQL function called NAME. Whenever that function is |
| 1819 | ** called, invoke SCRIPT to evaluate the function. |
| 1820 | */ |
| 1821 | case DB_FUNCTION: { |
| 1822 | SqlFunc *pFunc; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1823 | Tcl_Obj *pScript; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1824 | char *zName; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1825 | if( objc!=4 ){ |
| 1826 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 1827 | return TCL_ERROR; |
| 1828 | } |
| 1829 | zName = Tcl_GetStringFromObj(objv[2], 0); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1830 | pScript = objv[3]; |
| 1831 | pFunc = findSqlFunc(pDb, zName); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1832 | if( pFunc==0 ) return TCL_ERROR; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1833 | if( pFunc->pScript ){ |
| 1834 | Tcl_DecrRefCount(pFunc->pScript); |
| 1835 | } |
| 1836 | pFunc->pScript = pScript; |
| 1837 | Tcl_IncrRefCount(pScript); |
| 1838 | pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); |
danielk1977 | c8c1158 | 2004-06-29 13:41:21 +0000 | [diff] [blame] | 1839 | rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1840 | pFunc, tclSqlFunc, 0, 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1841 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1842 | rc = TCL_ERROR; |
| 1843 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1844 | }else{ |
| 1845 | /* Must flush any cached statements */ |
| 1846 | flushStmtCache( pDb ); |
| 1847 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | } |
| 1850 | |
| 1851 | /* |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1852 | ** $db incrblob ?DB? TABLE COLUMN ROWID |
| 1853 | */ |
| 1854 | case DB_INCRBLOB: { |
| 1855 | const char *zDb = "main"; |
| 1856 | const char *zTable; |
| 1857 | const char *zColumn; |
| 1858 | sqlite_int64 iRow; |
| 1859 | |
| 1860 | if( objc!=5 && objc!=6 ){ |
| 1861 | Tcl_WrongNumArgs(interp, 2, objv, "?DB? TABLE ROWID"); |
| 1862 | return TCL_ERROR; |
| 1863 | } |
| 1864 | |
| 1865 | if( objc==6 ){ |
| 1866 | zDb = Tcl_GetString(objv[2]); |
| 1867 | } |
| 1868 | zTable = Tcl_GetString(objv[objc-3]); |
| 1869 | zColumn = Tcl_GetString(objv[objc-2]); |
| 1870 | rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); |
| 1871 | |
| 1872 | if( rc==TCL_OK ){ |
| 1873 | rc = createIncrblobChannel(interp, pDb, zDb, zTable, zColumn, iRow); |
| 1874 | } |
| 1875 | break; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
drh | f11bded | 2006-07-17 00:02:44 +0000 | [diff] [blame] | 1879 | ** $db interrupt |
| 1880 | ** |
| 1881 | ** Interrupt the execution of the inner-most SQL interpreter. This |
| 1882 | ** causes the SQL statement to return an error of SQLITE_INTERRUPT. |
| 1883 | */ |
| 1884 | case DB_INTERRUPT: { |
| 1885 | sqlite3_interrupt(pDb->db); |
| 1886 | break; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1890 | ** $db nullvalue ?STRING? |
| 1891 | ** |
| 1892 | ** Change text used when a NULL comes back from the database. If ?STRING? |
| 1893 | ** is not present, then the current string used for NULL is returned. |
| 1894 | ** If STRING is present, then STRING is returned. |
| 1895 | ** |
| 1896 | */ |
| 1897 | case DB_NULLVALUE: { |
| 1898 | if( objc!=2 && objc!=3 ){ |
| 1899 | Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); |
| 1900 | return TCL_ERROR; |
| 1901 | } |
| 1902 | if( objc==3 ){ |
| 1903 | int len; |
| 1904 | char *zNull = Tcl_GetStringFromObj(objv[2], &len); |
| 1905 | if( pDb->zNull ){ |
| 1906 | Tcl_Free(pDb->zNull); |
| 1907 | } |
| 1908 | if( zNull && len>0 ){ |
| 1909 | pDb->zNull = Tcl_Alloc( len + 1 ); |
| 1910 | strncpy(pDb->zNull, zNull, len); |
| 1911 | pDb->zNull[len] = '\0'; |
| 1912 | }else{ |
| 1913 | pDb->zNull = 0; |
| 1914 | } |
| 1915 | } |
| 1916 | Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull)); |
| 1917 | break; |
| 1918 | } |
| 1919 | |
| 1920 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 1921 | ** $db last_insert_rowid |
| 1922 | ** |
| 1923 | ** Return an integer which is the ROWID for the most recent insert. |
| 1924 | */ |
| 1925 | case DB_LAST_INSERT_ROWID: { |
| 1926 | Tcl_Obj *pResult; |
drh | f7e678d | 2006-06-21 19:30:34 +0000 | [diff] [blame] | 1927 | Tcl_WideInt rowid; |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 1928 | if( objc!=2 ){ |
| 1929 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 1930 | return TCL_ERROR; |
| 1931 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1932 | rowid = sqlite3_last_insert_rowid(pDb->db); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 1933 | pResult = Tcl_GetObjResult(interp); |
drh | f7e678d | 2006-06-21 19:30:34 +0000 | [diff] [blame] | 1934 | Tcl_SetWideIntObj(pResult, rowid); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 1935 | break; |
| 1936 | } |
| 1937 | |
| 1938 | /* |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1939 | ** The DB_ONECOLUMN method is implemented together with DB_EVAL. |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 1940 | */ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1941 | |
| 1942 | /* $db progress ?N CALLBACK? |
| 1943 | ** |
| 1944 | ** Invoke the given callback every N virtual machine opcodes while executing |
| 1945 | ** queries. |
| 1946 | */ |
| 1947 | case DB_PROGRESS: { |
| 1948 | if( objc==2 ){ |
| 1949 | if( pDb->zProgress ){ |
| 1950 | Tcl_AppendResult(interp, pDb->zProgress, 0); |
| 1951 | } |
| 1952 | }else if( objc==4 ){ |
| 1953 | char *zProgress; |
| 1954 | int len; |
| 1955 | int N; |
| 1956 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ |
| 1957 | return TCL_ERROR; |
| 1958 | }; |
| 1959 | if( pDb->zProgress ){ |
| 1960 | Tcl_Free(pDb->zProgress); |
| 1961 | } |
| 1962 | zProgress = Tcl_GetStringFromObj(objv[3], &len); |
| 1963 | if( zProgress && len>0 ){ |
| 1964 | pDb->zProgress = Tcl_Alloc( len + 1 ); |
| 1965 | strcpy(pDb->zProgress, zProgress); |
| 1966 | }else{ |
| 1967 | pDb->zProgress = 0; |
| 1968 | } |
| 1969 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1970 | if( pDb->zProgress ){ |
| 1971 | pDb->interp = interp; |
| 1972 | sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); |
| 1973 | }else{ |
| 1974 | sqlite3_progress_handler(pDb->db, 0, 0, 0); |
| 1975 | } |
| 1976 | #endif |
| 1977 | }else{ |
| 1978 | Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 1979 | return TCL_ERROR; |
| 1980 | } |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 1981 | break; |
| 1982 | } |
| 1983 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1984 | /* $db profile ?CALLBACK? |
| 1985 | ** |
| 1986 | ** Make arrangements to invoke the CALLBACK routine after each SQL statement |
| 1987 | ** that has run. The text of the SQL and the amount of elapse time are |
| 1988 | ** appended to CALLBACK before the script is run. |
| 1989 | */ |
| 1990 | case DB_PROFILE: { |
| 1991 | if( objc>3 ){ |
| 1992 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 1993 | return TCL_ERROR; |
| 1994 | }else if( objc==2 ){ |
| 1995 | if( pDb->zProfile ){ |
| 1996 | Tcl_AppendResult(interp, pDb->zProfile, 0); |
| 1997 | } |
| 1998 | }else{ |
| 1999 | char *zProfile; |
| 2000 | int len; |
| 2001 | if( pDb->zProfile ){ |
| 2002 | Tcl_Free(pDb->zProfile); |
| 2003 | } |
| 2004 | zProfile = Tcl_GetStringFromObj(objv[2], &len); |
| 2005 | if( zProfile && len>0 ){ |
| 2006 | pDb->zProfile = Tcl_Alloc( len + 1 ); |
| 2007 | strcpy(pDb->zProfile, zProfile); |
| 2008 | }else{ |
| 2009 | pDb->zProfile = 0; |
| 2010 | } |
| 2011 | #ifndef SQLITE_OMIT_TRACE |
| 2012 | if( pDb->zProfile ){ |
| 2013 | pDb->interp = interp; |
| 2014 | sqlite3_profile(pDb->db, DbProfileHandler, pDb); |
| 2015 | }else{ |
| 2016 | sqlite3_profile(pDb->db, 0, 0); |
| 2017 | } |
| 2018 | #endif |
| 2019 | } |
| 2020 | break; |
| 2021 | } |
| 2022 | |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2023 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2024 | ** $db rekey KEY |
| 2025 | ** |
| 2026 | ** Change the encryption key on the currently open database. |
| 2027 | */ |
| 2028 | case DB_REKEY: { |
| 2029 | int nKey; |
| 2030 | void *pKey; |
| 2031 | if( objc!=3 ){ |
| 2032 | Tcl_WrongNumArgs(interp, 2, objv, "KEY"); |
| 2033 | return TCL_ERROR; |
| 2034 | } |
| 2035 | pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2036 | #ifdef SQLITE_HAS_CODEC |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2037 | rc = sqlite3_rekey(pDb->db, pKey, nKey); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2038 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2039 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2040 | rc = TCL_ERROR; |
| 2041 | } |
| 2042 | #endif |
| 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2047 | ** $db timeout MILLESECONDS |
| 2048 | ** |
| 2049 | ** Delay for the number of milliseconds specified when a file is locked. |
| 2050 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2051 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2052 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2053 | if( objc!=3 ){ |
| 2054 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2055 | return TCL_ERROR; |
| 2056 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2057 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2058 | sqlite3_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2059 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2060 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 2061 | |
| 2062 | /* |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 2063 | ** $db total_changes |
| 2064 | ** |
| 2065 | ** Return the number of rows that were modified, inserted, or deleted |
| 2066 | ** since the database handle was created. |
| 2067 | */ |
| 2068 | case DB_TOTAL_CHANGES: { |
| 2069 | Tcl_Obj *pResult; |
| 2070 | if( objc!=2 ){ |
| 2071 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 2072 | return TCL_ERROR; |
| 2073 | } |
| 2074 | pResult = Tcl_GetObjResult(interp); |
| 2075 | Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); |
| 2076 | break; |
| 2077 | } |
| 2078 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2079 | /* $db trace ?CALLBACK? |
| 2080 | ** |
| 2081 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 2082 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 2083 | ** it is executed. |
| 2084 | */ |
| 2085 | case DB_TRACE: { |
| 2086 | if( objc>3 ){ |
| 2087 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 2088 | return TCL_ERROR; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2089 | }else if( objc==2 ){ |
| 2090 | if( pDb->zTrace ){ |
| 2091 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 2092 | } |
| 2093 | }else{ |
| 2094 | char *zTrace; |
| 2095 | int len; |
| 2096 | if( pDb->zTrace ){ |
| 2097 | Tcl_Free(pDb->zTrace); |
| 2098 | } |
| 2099 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 2100 | if( zTrace && len>0 ){ |
| 2101 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
| 2102 | strcpy(pDb->zTrace, zTrace); |
| 2103 | }else{ |
| 2104 | pDb->zTrace = 0; |
| 2105 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2106 | #ifndef SQLITE_OMIT_TRACE |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2107 | if( pDb->zTrace ){ |
| 2108 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2109 | sqlite3_trace(pDb->db, DbTraceHandler, pDb); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2110 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2111 | sqlite3_trace(pDb->db, 0, 0); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2112 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2113 | #endif |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2114 | } |
| 2115 | break; |
| 2116 | } |
| 2117 | |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2118 | /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT |
| 2119 | ** |
| 2120 | ** Start a new transaction (if we are not already in the midst of a |
| 2121 | ** transaction) and execute the TCL script SCRIPT. After SCRIPT |
| 2122 | ** completes, either commit the transaction or roll it back if SCRIPT |
| 2123 | ** throws an exception. Or if no new transation was started, do nothing. |
| 2124 | ** pass the exception on up the stack. |
| 2125 | ** |
| 2126 | ** This command was inspired by Dave Thomas's talk on Ruby at the |
| 2127 | ** 2005 O'Reilly Open Source Convention (OSCON). |
| 2128 | */ |
| 2129 | case DB_TRANSACTION: { |
| 2130 | int inTrans; |
| 2131 | Tcl_Obj *pScript; |
| 2132 | const char *zBegin = "BEGIN"; |
| 2133 | if( objc!=3 && objc!=4 ){ |
| 2134 | Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); |
| 2135 | return TCL_ERROR; |
| 2136 | } |
| 2137 | if( objc==3 ){ |
| 2138 | pScript = objv[2]; |
| 2139 | } else { |
| 2140 | static const char *TTYPE_strs[] = { |
drh | ce60401 | 2005-08-16 11:11:34 +0000 | [diff] [blame] | 2141 | "deferred", "exclusive", "immediate", 0 |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2142 | }; |
| 2143 | enum TTYPE_enum { |
| 2144 | TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE |
| 2145 | }; |
| 2146 | int ttype; |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 2147 | if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2148 | 0, &ttype) ){ |
| 2149 | return TCL_ERROR; |
| 2150 | } |
| 2151 | switch( (enum TTYPE_enum)ttype ){ |
| 2152 | case TTYPE_DEFERRED: /* no-op */; break; |
| 2153 | case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; |
| 2154 | case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; |
| 2155 | } |
| 2156 | pScript = objv[3]; |
| 2157 | } |
| 2158 | inTrans = !sqlite3_get_autocommit(pDb->db); |
| 2159 | if( !inTrans ){ |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 2160 | (void)sqlite3_exec(pDb->db, zBegin, 0, 0, 0); |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2161 | } |
| 2162 | rc = Tcl_EvalObjEx(interp, pScript, 0); |
| 2163 | if( !inTrans ){ |
| 2164 | const char *zEnd; |
| 2165 | if( rc==TCL_ERROR ){ |
| 2166 | zEnd = "ROLLBACK"; |
| 2167 | } else { |
| 2168 | zEnd = "COMMIT"; |
| 2169 | } |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 2170 | (void)sqlite3_exec(pDb->db, zEnd, 0, 0, 0); |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2171 | } |
| 2172 | break; |
| 2173 | } |
| 2174 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2175 | /* |
| 2176 | ** $db update_hook ?script? |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2177 | ** $db rollback_hook ?script? |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2178 | */ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2179 | case DB_UPDATE_HOOK: |
| 2180 | case DB_ROLLBACK_HOOK: { |
| 2181 | |
| 2182 | /* set ppHook to point at pUpdateHook or pRollbackHook, depending on |
| 2183 | ** whether [$db update_hook] or [$db rollback_hook] was invoked. |
| 2184 | */ |
| 2185 | Tcl_Obj **ppHook; |
| 2186 | if( choice==DB_UPDATE_HOOK ){ |
| 2187 | ppHook = &pDb->pUpdateHook; |
| 2188 | }else{ |
| 2189 | ppHook = &pDb->pRollbackHook; |
| 2190 | } |
| 2191 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2192 | if( objc!=2 && objc!=3 ){ |
| 2193 | Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); |
| 2194 | return TCL_ERROR; |
| 2195 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2196 | if( *ppHook ){ |
| 2197 | Tcl_SetObjResult(interp, *ppHook); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2198 | if( objc==3 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2199 | Tcl_DecrRefCount(*ppHook); |
| 2200 | *ppHook = 0; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2201 | } |
| 2202 | } |
| 2203 | if( objc==3 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2204 | assert( !(*ppHook) ); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2205 | if( Tcl_GetCharLength(objv[2])>0 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2206 | *ppHook = objv[2]; |
| 2207 | Tcl_IncrRefCount(*ppHook); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2208 | } |
| 2209 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2210 | |
| 2211 | sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); |
| 2212 | sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); |
| 2213 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | } |
| 2216 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 2217 | /* $db version |
| 2218 | ** |
| 2219 | ** Return the version string for this database. |
| 2220 | */ |
| 2221 | case DB_VERSION: { |
| 2222 | Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); |
| 2223 | break; |
| 2224 | } |
| 2225 | |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 2226 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2227 | } /* End of the SWITCH statement */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2228 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | /* |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2232 | ** sqlite3 DBNAME FILENAME ?MODE? ?-key KEY? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2233 | ** |
| 2234 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 2235 | ** invoked, this routine runs to process that command. |
| 2236 | ** |
| 2237 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 2238 | ** database connection. This command creates a new command named |
| 2239 | ** DBNAME that is used to control that connection. The database |
| 2240 | ** connection is deleted when the DBNAME command is deleted. |
| 2241 | ** |
| 2242 | ** The second argument is the name of the directory that contains |
| 2243 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2244 | ** |
| 2245 | ** For testing purposes, we also support the following: |
| 2246 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2247 | ** sqlite3 -encoding |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2248 | ** |
| 2249 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 2250 | ** are UTF-8 and iso8859. |
| 2251 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2252 | ** sqlite3 -version |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 2253 | ** |
| 2254 | ** Return the version number of the SQLite library. |
| 2255 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2256 | ** sqlite3 -tcl-uses-utf |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2257 | ** |
| 2258 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 2259 | ** not. Used by tests to make sure the library was compiled |
| 2260 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2261 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2262 | static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2263 | SqliteDb *p; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2264 | void *pKey = 0; |
| 2265 | int nKey = 0; |
| 2266 | const char *zArg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2267 | char *zErrMsg; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2268 | const char *zFile; |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2269 | Tcl_DString translatedFilename; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2270 | if( objc==2 ){ |
| 2271 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2272 | if( strcmp(zArg,"-version")==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2273 | Tcl_AppendResult(interp,sqlite3_version,0); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 2274 | return TCL_OK; |
| 2275 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2276 | if( strcmp(zArg,"-has-codec")==0 ){ |
| 2277 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2278 | Tcl_AppendResult(interp,"1",0); |
| 2279 | #else |
| 2280 | Tcl_AppendResult(interp,"0",0); |
| 2281 | #endif |
| 2282 | return TCL_OK; |
| 2283 | } |
| 2284 | if( strcmp(zArg,"-tcl-uses-utf")==0 ){ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2285 | #ifdef TCL_UTF_MAX |
| 2286 | Tcl_AppendResult(interp,"1",0); |
| 2287 | #else |
| 2288 | Tcl_AppendResult(interp,"0",0); |
| 2289 | #endif |
| 2290 | return TCL_OK; |
| 2291 | } |
| 2292 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2293 | if( objc==5 || objc==6 ){ |
| 2294 | zArg = Tcl_GetStringFromObj(objv[objc-2], 0); |
| 2295 | if( strcmp(zArg,"-key")==0 ){ |
| 2296 | pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey); |
| 2297 | objc -= 2; |
| 2298 | } |
| 2299 | } |
| 2300 | if( objc!=3 && objc!=4 ){ |
| 2301 | Tcl_WrongNumArgs(interp, 1, objv, |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2302 | #ifdef SQLITE_HAS_CODEC |
| 2303 | "HANDLE FILENAME ?-key CODEC-KEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2304 | #else |
| 2305 | "HANDLE FILENAME ?MODE?" |
| 2306 | #endif |
| 2307 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2308 | return TCL_ERROR; |
| 2309 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2310 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 2311 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2312 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2313 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 2314 | return TCL_ERROR; |
| 2315 | } |
| 2316 | memset(p, 0, sizeof(*p)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2317 | zFile = Tcl_GetStringFromObj(objv[2], 0); |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2318 | zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 2319 | sqlite3_open(zFile, &p->db); |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2320 | Tcl_DStringFree(&translatedFilename); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2321 | if( SQLITE_OK!=sqlite3_errcode(p->db) ){ |
drh | 9404d50 | 2006-12-19 18:46:08 +0000 | [diff] [blame] | 2322 | zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2323 | sqlite3_close(p->db); |
| 2324 | p->db = 0; |
| 2325 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2326 | #ifdef SQLITE_HAS_CODEC |
| 2327 | sqlite3_key(p->db, pKey, nKey); |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 2328 | #endif |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2329 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2330 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2331 | Tcl_Free((char*)p); |
drh | 9404d50 | 2006-12-19 18:46:08 +0000 | [diff] [blame] | 2332 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2333 | return TCL_ERROR; |
| 2334 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2335 | p->maxStmt = NUM_PREPARED_STMTS; |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 2336 | p->interp = interp; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2337 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
| 2338 | Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 2339 | |
| 2340 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 2341 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 2342 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 2343 | #ifdef SQLITE_TEST |
| 2344 | { |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2345 | extern void Md5_Register(sqlite3*); |
drh | 3b93bc8 | 2005-01-13 23:54:32 +0000 | [diff] [blame] | 2346 | #ifdef SQLITE_MEMDEBUG |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 2347 | int mallocfail = sqlite3_iMallocFail; |
| 2348 | sqlite3_iMallocFail = 0; |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 2349 | #endif |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 2350 | Md5_Register(p->db); |
drh | 3b93bc8 | 2005-01-13 23:54:32 +0000 | [diff] [blame] | 2351 | #ifdef SQLITE_MEMDEBUG |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 2352 | sqlite3_iMallocFail = mallocfail; |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 2353 | #endif |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2354 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 2355 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2356 | return TCL_OK; |
| 2357 | } |
| 2358 | |
| 2359 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 2360 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 2361 | ** library. |
| 2362 | */ |
| 2363 | #ifndef USE_TCL_STUBS |
| 2364 | # undef Tcl_InitStubs |
| 2365 | # define Tcl_InitStubs(a,b,c) |
| 2366 | #endif |
| 2367 | |
| 2368 | /* |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 2369 | ** Make sure we have a PACKAGE_VERSION macro defined. This will be |
| 2370 | ** defined automatically by the TEA makefile. But other makefiles |
| 2371 | ** do not define it. |
| 2372 | */ |
| 2373 | #ifndef PACKAGE_VERSION |
| 2374 | # define PACKAGE_VERSION SQLITE_VERSION |
| 2375 | #endif |
| 2376 | |
| 2377 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2378 | ** Initialize this module. |
| 2379 | ** |
| 2380 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 2381 | ** (Hence there is no namespace. There is no point in using a namespace |
| 2382 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 2383 | ** used to open a new SQLite database. See the DbMain() routine above |
| 2384 | ** for additional information. |
| 2385 | */ |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 2386 | EXTERN int Sqlite3_Init(Tcl_Interp *interp){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 2387 | Tcl_InitStubs(interp, "8.4", 0); |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 2388 | Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 2389 | Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 2390 | Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 2391 | Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 2392 | return TCL_OK; |
| 2393 | } |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 2394 | EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 2395 | EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
| 2396 | EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 2397 | |
| 2398 | #ifndef SQLITE_3_SUFFIX_ONLY |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 2399 | EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 2400 | EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 2401 | EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
| 2402 | EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 2403 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2404 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2405 | #ifdef TCLSH |
| 2406 | /***************************************************************************** |
| 2407 | ** The code that follows is used to build standalone TCL interpreters |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2408 | */ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2409 | |
| 2410 | /* |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2411 | ** If the macro TCLSH is one, then put in code this for the |
| 2412 | ** "main" routine that will initialize Tcl and take input from |
| 2413 | ** standard input. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2414 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2415 | #if TCLSH==1 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2416 | static char zMainloop[] = |
| 2417 | "set line {}\n" |
| 2418 | "while {![eof stdin]} {\n" |
| 2419 | "if {$line!=\"\"} {\n" |
| 2420 | "puts -nonewline \"> \"\n" |
| 2421 | "} else {\n" |
| 2422 | "puts -nonewline \"% \"\n" |
| 2423 | "}\n" |
| 2424 | "flush stdout\n" |
| 2425 | "append line [gets stdin]\n" |
| 2426 | "if {[info complete $line]} {\n" |
| 2427 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 2428 | "puts stderr \"Error: $result\"\n" |
| 2429 | "} elseif {$result!=\"\"} {\n" |
| 2430 | "puts $result\n" |
| 2431 | "}\n" |
| 2432 | "set line {}\n" |
| 2433 | "} else {\n" |
| 2434 | "append line \\n\n" |
| 2435 | "}\n" |
| 2436 | "}\n" |
| 2437 | ; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2438 | #endif |
| 2439 | |
| 2440 | /* |
| 2441 | ** If the macro TCLSH is two, then get the main loop code out of |
| 2442 | ** the separate file "spaceanal_tcl.h". |
| 2443 | */ |
| 2444 | #if TCLSH==2 |
| 2445 | static char zMainloop[] = |
| 2446 | #include "spaceanal_tcl.h" |
| 2447 | ; |
| 2448 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2449 | |
| 2450 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 2451 | int TCLSH_MAIN(int argc, char **argv){ |
| 2452 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 2453 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2454 | interp = Tcl_CreateInterp(); |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 2455 | Sqlite3_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2456 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 2457 | { |
| 2458 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 2459 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 2460 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 2461 | extern int Sqlitetest4_Init(Tcl_Interp*); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 2462 | extern int Sqlitetest5_Init(Tcl_Interp*); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2463 | extern int Sqlitetest6_Init(Tcl_Interp*); |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 2464 | extern int Sqlitetest7_Init(Tcl_Interp*); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2465 | extern int Sqlitetest8_Init(Tcl_Interp*); |
danielk1977 | a713f2c | 2007-03-29 12:19:11 +0000 | [diff] [blame] | 2466 | extern int Sqlitetest9_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 2467 | extern int Md5_Init(Tcl_Interp*); |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 2468 | extern int Sqlitetestsse_Init(Tcl_Interp*); |
drh | 2366940 | 2006-01-09 17:29:52 +0000 | [diff] [blame] | 2469 | extern int Sqlitetestasync_Init(Tcl_Interp*); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 2470 | extern int Sqlitetesttclvar_Init(Tcl_Interp*); |
danielk1977 | 954ce99 | 2006-06-15 15:59:19 +0000 | [diff] [blame] | 2471 | extern int Sqlitetestschema_Init(Tcl_Interp*); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2472 | extern int Sqlitetest_autoext_Init(Tcl_Interp*); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 2473 | extern int Sqlitetest_hexio_Init(Tcl_Interp*); |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 2474 | |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 2475 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 2476 | Sqlitetest2_Init(interp); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2477 | Sqlitetest3_Init(interp); |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2478 | Sqlitetest4_Init(interp); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 2479 | Sqlitetest5_Init(interp); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2480 | Sqlitetest6_Init(interp); |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 2481 | Sqlitetest7_Init(interp); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2482 | Sqlitetest8_Init(interp); |
danielk1977 | a713f2c | 2007-03-29 12:19:11 +0000 | [diff] [blame] | 2483 | Sqlitetest9_Init(interp); |
drh | 2366940 | 2006-01-09 17:29:52 +0000 | [diff] [blame] | 2484 | Sqlitetestasync_Init(interp); |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 2485 | Sqlitetesttclvar_Init(interp); |
danielk1977 | 954ce99 | 2006-06-15 15:59:19 +0000 | [diff] [blame] | 2486 | Sqlitetestschema_Init(interp); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2487 | Sqlitetest_autoext_Init(interp); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 2488 | Sqlitetest_hexio_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 2489 | Md5_Init(interp); |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 2490 | #ifdef SQLITE_SSE |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 2491 | Sqlitetestsse_Init(interp); |
| 2492 | #endif |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 2493 | } |
| 2494 | #endif |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2495 | if( argc>=2 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2496 | int i; |
shess | ad42c3a | 2006-08-22 23:53:46 +0000 | [diff] [blame] | 2497 | char zArgc[32]; |
| 2498 | sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); |
| 2499 | Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2500 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 2501 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
drh | 61212b6 | 2004-12-02 20:17:00 +0000 | [diff] [blame] | 2502 | for(i=3-TCLSH; i<argc; i++){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2503 | Tcl_SetVar(interp, "argv", argv[i], |
| 2504 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 2505 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2506 | if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 2507 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 2508 | if( zInfo==0 ) zInfo = interp->result; |
| 2509 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2510 | return 1; |
| 2511 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2512 | } |
| 2513 | if( argc<=1 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2514 | Tcl_GlobalEval(interp, zMainloop); |
| 2515 | } |
| 2516 | return 0; |
| 2517 | } |
| 2518 | #endif /* TCLSH */ |