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