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 | 57a0227 | 2009-10-22 20:52:05 +0000 | [diff] [blame] | 14 | ** |
| 15 | ** Compile-time options: |
| 16 | ** |
| 17 | ** -DTCLSH=1 Add a "main()" routine that works as a tclsh. |
| 18 | ** |
| 19 | ** -DSQLITE_TCLMD5 When used in conjuction with -DTCLSH=1, add |
| 20 | ** four new commands to the TCL interpreter for |
| 21 | ** generating MD5 checksums: md5, md5file, |
| 22 | ** md5-10x8, and md5file-10x8. |
| 23 | ** |
| 24 | ** -DSQLITE_TEST When used in conjuction with -DTCLSH=1, add |
| 25 | ** hundreds of new commands used for testing |
| 26 | ** SQLite. This option implies -DSQLITE_TCLMD5. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | */ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 28 | #include "tcl.h" |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 29 | #include <errno.h> |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | ** Some additional include files are needed if this file is not |
| 33 | ** appended to the amalgamation. |
| 34 | */ |
| 35 | #ifndef SQLITE_AMALGAMATION |
drh | 65e8c82 | 2009-12-01 13:57:48 +0000 | [diff] [blame] | 36 | # include "sqlite3.h" |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 37 | # include <stdlib.h> |
| 38 | # include <string.h> |
| 39 | # include <assert.h> |
drh | 65e8c82 | 2009-12-01 13:57:48 +0000 | [diff] [blame] | 40 | typedef unsigned char u8; |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 41 | #endif |
drh | eb20638 | 2009-10-24 15:51:33 +0000 | [diff] [blame] | 42 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 43 | |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 44 | /* |
| 45 | * Windows needs to know which symbols to export. Unix does not. |
| 46 | * BUILD_sqlite should be undefined for Unix. |
| 47 | */ |
| 48 | #ifdef BUILD_sqlite |
| 49 | #undef TCL_STORAGE_CLASS |
| 50 | #define TCL_STORAGE_CLASS DLLEXPORT |
| 51 | #endif /* BUILD_sqlite */ |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 52 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 53 | #define NUM_PREPARED_STMTS 10 |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 54 | #define MAX_PREPARED_STMTS 100 |
| 55 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 56 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 57 | ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
| 58 | ** have to do a translation when going between the two. Set the |
| 59 | ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
| 60 | ** this translation. |
| 61 | */ |
| 62 | #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
| 63 | # define UTF_TRANSLATION_NEEDED 1 |
| 64 | #endif |
| 65 | |
| 66 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 67 | ** New SQL functions can be created as TCL scripts. Each such function |
| 68 | ** is described by an instance of the following structure. |
| 69 | */ |
| 70 | typedef struct SqlFunc SqlFunc; |
| 71 | struct SqlFunc { |
| 72 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 73 | Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ |
| 74 | int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ |
| 75 | char *zName; /* Name of this function */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 76 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 77 | }; |
| 78 | |
| 79 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 80 | ** New collation sequences function can be created as TCL scripts. Each such |
| 81 | ** function is described by an instance of the following structure. |
| 82 | */ |
| 83 | typedef struct SqlCollate SqlCollate; |
| 84 | struct SqlCollate { |
| 85 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 86 | char *zScript; /* The script to be run */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 87 | SqlCollate *pNext; /* Next function on the list of them all */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /* |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 91 | ** Prepared statements are cached for faster execution. Each prepared |
| 92 | ** statement is described by an instance of the following structure. |
| 93 | */ |
| 94 | typedef struct SqlPreparedStmt SqlPreparedStmt; |
| 95 | struct SqlPreparedStmt { |
| 96 | SqlPreparedStmt *pNext; /* Next in linked list */ |
| 97 | SqlPreparedStmt *pPrev; /* Previous on the list */ |
| 98 | sqlite3_stmt *pStmt; /* The prepared statement */ |
| 99 | int nSql; /* chars in zSql[] */ |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 100 | const char *zSql; /* Text of the SQL statement */ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 101 | int nParm; /* Size of apParm array */ |
| 102 | Tcl_Obj **apParm; /* Array of referenced object pointers */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 105 | typedef struct IncrblobChannel IncrblobChannel; |
| 106 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 107 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 108 | ** There is one instance of this structure for each SQLite database |
| 109 | ** that has been opened by the SQLite TCL interface. |
| 110 | */ |
| 111 | typedef struct SqliteDb SqliteDb; |
| 112 | struct SqliteDb { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 113 | sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 114 | Tcl_Interp *interp; /* The interpreter used for this database */ |
| 115 | char *zBusy; /* The busy callback routine */ |
| 116 | char *zCommit; /* The commit hook callback routine */ |
| 117 | char *zTrace; /* The trace callback routine */ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 118 | char *zProfile; /* The profile callback routine */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 119 | char *zProgress; /* The progress callback routine */ |
| 120 | char *zAuth; /* The authorization callback routine */ |
drh | 1f1549f | 2008-08-26 21:33:34 +0000 | [diff] [blame] | 121 | int disableAuth; /* Disable the authorizer if it exists */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 122 | char *zNull; /* Text to substitute for an SQL NULL value */ |
| 123 | SqlFunc *pFunc; /* List of SQL functions */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 124 | Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 125 | Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 126 | Tcl_Obj *pWalHook; /* WAL hook script (if any) */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 127 | Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 128 | SqlCollate *pCollate; /* List of SQL collation functions */ |
| 129 | int rc; /* Return code of most recent sqlite3_exec() */ |
| 130 | Tcl_Obj *pCollateNeeded; /* Collation needed script */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 131 | SqlPreparedStmt *stmtList; /* List of prepared statements*/ |
| 132 | SqlPreparedStmt *stmtLast; /* Last statement in the list */ |
| 133 | int maxStmt; /* The next maximum number of stmtList */ |
| 134 | int nStmt; /* Number of statements in stmtList */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 135 | IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ |
drh | 3c379b0 | 2010-04-07 19:31:59 +0000 | [diff] [blame] | 136 | int nStep, nSort, nIndex; /* Statistics for most recent operation */ |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 137 | int nTransaction; /* Number of nested [transaction] methods */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 138 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 139 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 140 | struct IncrblobChannel { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 141 | sqlite3_blob *pBlob; /* sqlite3 blob handle */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 142 | SqliteDb *pDb; /* Associated database connection */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 143 | int iSeek; /* Current seek offset */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 144 | Tcl_Channel channel; /* Channel identifier */ |
| 145 | IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ |
| 146 | IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 149 | /* |
| 150 | ** Compute a string length that is limited to what can be stored in |
| 151 | ** lower 30 bits of a 32-bit signed integer. |
| 152 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 153 | static int strlen30(const char *z){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 154 | const char *z2 = z; |
| 155 | while( *z2 ){ z2++; } |
| 156 | return 0x3fffffff & (int)(z2 - z); |
| 157 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 158 | |
| 159 | |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 160 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 161 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 162 | ** Close all incrblob channels opened using database connection pDb. |
| 163 | ** This is called when shutting down the database connection. |
| 164 | */ |
| 165 | static void closeIncrblobChannels(SqliteDb *pDb){ |
| 166 | IncrblobChannel *p; |
| 167 | IncrblobChannel *pNext; |
| 168 | |
| 169 | for(p=pDb->pIncrblob; p; p=pNext){ |
| 170 | pNext = p->pNext; |
| 171 | |
| 172 | /* Note: Calling unregister here call Tcl_Close on the incrblob channel, |
| 173 | ** which deletes the IncrblobChannel structure at *p. So do not |
| 174 | ** call Tcl_Free() here. |
| 175 | */ |
| 176 | Tcl_UnregisterChannel(pDb->interp, p->channel); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 181 | ** Close an incremental blob channel. |
| 182 | */ |
| 183 | static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ |
| 184 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 185 | int rc = sqlite3_blob_close(p->pBlob); |
| 186 | sqlite3 *db = p->pDb->db; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 187 | |
| 188 | /* Remove the channel from the SqliteDb.pIncrblob list. */ |
| 189 | if( p->pNext ){ |
| 190 | p->pNext->pPrev = p->pPrev; |
| 191 | } |
| 192 | if( p->pPrev ){ |
| 193 | p->pPrev->pNext = p->pNext; |
| 194 | } |
| 195 | if( p->pDb->pIncrblob==p ){ |
| 196 | p->pDb->pIncrblob = p->pNext; |
| 197 | } |
| 198 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 199 | /* Free the IncrblobChannel structure */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 200 | Tcl_Free((char *)p); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 201 | |
| 202 | if( rc!=SQLITE_OK ){ |
| 203 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); |
| 204 | return TCL_ERROR; |
| 205 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 206 | return TCL_OK; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | ** Read data from an incremental blob channel. |
| 211 | */ |
| 212 | static int incrblobInput( |
| 213 | ClientData instanceData, |
| 214 | char *buf, |
| 215 | int bufSize, |
| 216 | int *errorCodePtr |
| 217 | ){ |
| 218 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 219 | int nRead = bufSize; /* Number of bytes to read */ |
| 220 | int nBlob; /* Total size of the blob */ |
| 221 | int rc; /* sqlite error code */ |
| 222 | |
| 223 | nBlob = sqlite3_blob_bytes(p->pBlob); |
| 224 | if( (p->iSeek+nRead)>nBlob ){ |
| 225 | nRead = nBlob-p->iSeek; |
| 226 | } |
| 227 | if( nRead<=0 ){ |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); |
| 232 | if( rc!=SQLITE_OK ){ |
| 233 | *errorCodePtr = rc; |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | p->iSeek += nRead; |
| 238 | return nRead; |
| 239 | } |
| 240 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 241 | /* |
| 242 | ** Write data to an incremental blob channel. |
| 243 | */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 244 | static int incrblobOutput( |
| 245 | ClientData instanceData, |
| 246 | CONST char *buf, |
| 247 | int toWrite, |
| 248 | int *errorCodePtr |
| 249 | ){ |
| 250 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 251 | int nWrite = toWrite; /* Number of bytes to write */ |
| 252 | int nBlob; /* Total size of the blob */ |
| 253 | int rc; /* sqlite error code */ |
| 254 | |
| 255 | nBlob = sqlite3_blob_bytes(p->pBlob); |
| 256 | if( (p->iSeek+nWrite)>nBlob ){ |
| 257 | *errorCodePtr = EINVAL; |
| 258 | return -1; |
| 259 | } |
| 260 | if( nWrite<=0 ){ |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); |
| 265 | if( rc!=SQLITE_OK ){ |
| 266 | *errorCodePtr = EIO; |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | p->iSeek += nWrite; |
| 271 | return nWrite; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | ** Seek an incremental blob channel. |
| 276 | */ |
| 277 | static int incrblobSeek( |
| 278 | ClientData instanceData, |
| 279 | long offset, |
| 280 | int seekMode, |
| 281 | int *errorCodePtr |
| 282 | ){ |
| 283 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 284 | |
| 285 | switch( seekMode ){ |
| 286 | case SEEK_SET: |
| 287 | p->iSeek = offset; |
| 288 | break; |
| 289 | case SEEK_CUR: |
| 290 | p->iSeek += offset; |
| 291 | break; |
| 292 | case SEEK_END: |
| 293 | p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; |
| 294 | break; |
| 295 | |
| 296 | default: assert(!"Bad seekMode"); |
| 297 | } |
| 298 | |
| 299 | return p->iSeek; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | static void incrblobWatch(ClientData instanceData, int mode){ |
| 304 | /* NO-OP */ |
| 305 | } |
| 306 | static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ |
| 307 | return TCL_ERROR; |
| 308 | } |
| 309 | |
| 310 | static Tcl_ChannelType IncrblobChannelType = { |
| 311 | "incrblob", /* typeName */ |
| 312 | TCL_CHANNEL_VERSION_2, /* version */ |
| 313 | incrblobClose, /* closeProc */ |
| 314 | incrblobInput, /* inputProc */ |
| 315 | incrblobOutput, /* outputProc */ |
| 316 | incrblobSeek, /* seekProc */ |
| 317 | 0, /* setOptionProc */ |
| 318 | 0, /* getOptionProc */ |
| 319 | incrblobWatch, /* watchProc (this is a no-op) */ |
| 320 | incrblobHandle, /* getHandleProc (always returns error) */ |
| 321 | 0, /* close2Proc */ |
| 322 | 0, /* blockModeProc */ |
| 323 | 0, /* flushProc */ |
| 324 | 0, /* handlerProc */ |
| 325 | 0, /* wideSeekProc */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | /* |
| 329 | ** Create a new incrblob channel. |
| 330 | */ |
| 331 | static int createIncrblobChannel( |
| 332 | Tcl_Interp *interp, |
| 333 | SqliteDb *pDb, |
| 334 | const char *zDb, |
| 335 | const char *zTable, |
| 336 | const char *zColumn, |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 337 | sqlite_int64 iRow, |
| 338 | int isReadonly |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 339 | ){ |
| 340 | IncrblobChannel *p; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 341 | sqlite3 *db = pDb->db; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 342 | sqlite3_blob *pBlob; |
| 343 | int rc; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 344 | int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 345 | |
| 346 | /* This variable is used to name the channels: "incrblob_[incr count]" */ |
| 347 | static int count = 0; |
| 348 | char zChannel[64]; |
| 349 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 350 | rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 351 | if( rc!=SQLITE_OK ){ |
| 352 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
| 353 | return TCL_ERROR; |
| 354 | } |
| 355 | |
| 356 | p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); |
| 357 | p->iSeek = 0; |
| 358 | p->pBlob = pBlob; |
| 359 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 360 | sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 361 | p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); |
| 362 | Tcl_RegisterChannel(interp, p->channel); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 363 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 364 | /* Link the new channel into the SqliteDb.pIncrblob list. */ |
| 365 | p->pNext = pDb->pIncrblob; |
| 366 | p->pPrev = 0; |
| 367 | if( p->pNext ){ |
| 368 | p->pNext->pPrev = p; |
| 369 | } |
| 370 | pDb->pIncrblob = p; |
| 371 | p->pDb = pDb; |
| 372 | |
| 373 | Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 374 | return TCL_OK; |
| 375 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 376 | #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ |
| 377 | #define closeIncrblobChannels(pDb) |
| 378 | #endif |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 379 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 380 | /* |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 381 | ** Look at the script prefix in pCmd. We will be executing this script |
| 382 | ** after first appending one or more arguments. This routine analyzes |
| 383 | ** the script to see if it is safe to use Tcl_EvalObjv() on the script |
| 384 | ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much |
| 385 | ** faster. |
| 386 | ** |
| 387 | ** Scripts that are safe to use with Tcl_EvalObjv() consists of a |
| 388 | ** command name followed by zero or more arguments with no [...] or $ |
| 389 | ** or {...} or ; to be seen anywhere. Most callback scripts consist |
| 390 | ** of just a single procedure name and they meet this requirement. |
| 391 | */ |
| 392 | static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ |
| 393 | /* We could try to do something with Tcl_Parse(). But we will instead |
| 394 | ** just do a search for forbidden characters. If any of the forbidden |
| 395 | ** characters appear in pCmd, we will report the string as unsafe. |
| 396 | */ |
| 397 | const char *z; |
| 398 | int n; |
| 399 | z = Tcl_GetStringFromObj(pCmd, &n); |
| 400 | while( n-- > 0 ){ |
| 401 | int c = *(z++); |
| 402 | if( c=='$' || c=='[' || c==';' ) return 0; |
| 403 | } |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | ** Find an SqlFunc structure with the given name. Or create a new |
| 409 | ** one if an existing one cannot be found. Return a pointer to the |
| 410 | ** structure. |
| 411 | */ |
| 412 | static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ |
| 413 | SqlFunc *p, *pNew; |
| 414 | int i; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 415 | pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen30(zName) + 1 ); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 416 | pNew->zName = (char*)&pNew[1]; |
| 417 | for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); } |
| 418 | pNew->zName[i] = 0; |
| 419 | for(p=pDb->pFunc; p; p=p->pNext){ |
| 420 | if( strcmp(p->zName, pNew->zName)==0 ){ |
| 421 | Tcl_Free((char*)pNew); |
| 422 | return p; |
| 423 | } |
| 424 | } |
| 425 | pNew->interp = pDb->interp; |
| 426 | pNew->pScript = 0; |
| 427 | pNew->pNext = pDb->pFunc; |
| 428 | pDb->pFunc = pNew; |
| 429 | return pNew; |
| 430 | } |
| 431 | |
| 432 | /* |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 433 | ** Finalize and free a list of prepared statements |
| 434 | */ |
| 435 | static void flushStmtCache( SqliteDb *pDb ){ |
| 436 | SqlPreparedStmt *pPreStmt; |
| 437 | |
| 438 | while( pDb->stmtList ){ |
| 439 | sqlite3_finalize( pDb->stmtList->pStmt ); |
| 440 | pPreStmt = pDb->stmtList; |
| 441 | pDb->stmtList = pDb->stmtList->pNext; |
| 442 | Tcl_Free( (char*)pPreStmt ); |
| 443 | } |
| 444 | pDb->nStmt = 0; |
| 445 | pDb->stmtLast = 0; |
| 446 | } |
| 447 | |
| 448 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 449 | ** TCL calls this procedure when an sqlite3 database command is |
| 450 | ** deleted. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 451 | */ |
| 452 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 453 | SqliteDb *pDb = (SqliteDb*)db; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 454 | flushStmtCache(pDb); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 455 | closeIncrblobChannels(pDb); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 456 | sqlite3_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 457 | while( pDb->pFunc ){ |
| 458 | SqlFunc *pFunc = pDb->pFunc; |
| 459 | pDb->pFunc = pFunc->pNext; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 460 | Tcl_DecrRefCount(pFunc->pScript); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 461 | Tcl_Free((char*)pFunc); |
| 462 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 463 | while( pDb->pCollate ){ |
| 464 | SqlCollate *pCollate = pDb->pCollate; |
| 465 | pDb->pCollate = pCollate->pNext; |
| 466 | Tcl_Free((char*)pCollate); |
| 467 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 468 | if( pDb->zBusy ){ |
| 469 | Tcl_Free(pDb->zBusy); |
| 470 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 471 | if( pDb->zTrace ){ |
| 472 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 473 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 474 | if( pDb->zProfile ){ |
| 475 | Tcl_Free(pDb->zProfile); |
| 476 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 477 | if( pDb->zAuth ){ |
| 478 | Tcl_Free(pDb->zAuth); |
| 479 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 480 | if( pDb->zNull ){ |
| 481 | Tcl_Free(pDb->zNull); |
| 482 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 483 | if( pDb->pUpdateHook ){ |
| 484 | Tcl_DecrRefCount(pDb->pUpdateHook); |
| 485 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 486 | if( pDb->pRollbackHook ){ |
| 487 | Tcl_DecrRefCount(pDb->pRollbackHook); |
| 488 | } |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 489 | if( pDb->pWalHook ){ |
| 490 | Tcl_DecrRefCount(pDb->pWalHook); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 491 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 492 | if( pDb->pCollateNeeded ){ |
| 493 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 494 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 495 | Tcl_Free((char*)pDb); |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | ** This routine is called when a database file is locked while trying |
| 500 | ** to execute SQL. |
| 501 | */ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 502 | static int DbBusyHandler(void *cd, int nTries){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 503 | SqliteDb *pDb = (SqliteDb*)cd; |
| 504 | int rc; |
| 505 | char zVal[30]; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 506 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 507 | sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 508 | rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 509 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 510 | return 0; |
| 511 | } |
| 512 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 513 | } |
| 514 | |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 515 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 516 | /* |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 517 | ** This routine is invoked as the 'progress callback' for the database. |
| 518 | */ |
| 519 | static int DbProgressHandler(void *cd){ |
| 520 | SqliteDb *pDb = (SqliteDb*)cd; |
| 521 | int rc; |
| 522 | |
| 523 | assert( pDb->zProgress ); |
| 524 | rc = Tcl_Eval(pDb->interp, pDb->zProgress); |
| 525 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 526 | return 1; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 530 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 531 | |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 532 | #ifndef SQLITE_OMIT_TRACE |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 533 | /* |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 534 | ** This routine is called by the SQLite trace handler whenever a new |
| 535 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 536 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 537 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 538 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 539 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 540 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 541 | Tcl_DStringInit(&str); |
| 542 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 543 | Tcl_DStringAppendElement(&str, zSql); |
| 544 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 545 | Tcl_DStringFree(&str); |
| 546 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 547 | } |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 548 | #endif |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 549 | |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 550 | #ifndef SQLITE_OMIT_TRACE |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 551 | /* |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 552 | ** This routine is called by the SQLite profile handler after a statement |
| 553 | ** SQL has executed. The TCL script in pDb->zProfile is evaluated. |
| 554 | */ |
| 555 | static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ |
| 556 | SqliteDb *pDb = (SqliteDb*)cd; |
| 557 | Tcl_DString str; |
| 558 | char zTm[100]; |
| 559 | |
| 560 | sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); |
| 561 | Tcl_DStringInit(&str); |
| 562 | Tcl_DStringAppend(&str, pDb->zProfile, -1); |
| 563 | Tcl_DStringAppendElement(&str, zSql); |
| 564 | Tcl_DStringAppendElement(&str, zTm); |
| 565 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 566 | Tcl_DStringFree(&str); |
| 567 | Tcl_ResetResult(pDb->interp); |
| 568 | } |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 569 | #endif |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 570 | |
| 571 | /* |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 572 | ** This routine is called when a transaction is committed. The |
| 573 | ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
| 574 | ** if it throws an exception, the transaction is rolled back instead |
| 575 | ** of being committed. |
| 576 | */ |
| 577 | static int DbCommitHandler(void *cd){ |
| 578 | SqliteDb *pDb = (SqliteDb*)cd; |
| 579 | int rc; |
| 580 | |
| 581 | rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
| 582 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 583 | return 1; |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 588 | static void DbRollbackHandler(void *clientData){ |
| 589 | SqliteDb *pDb = (SqliteDb*)clientData; |
| 590 | assert(pDb->pRollbackHook); |
| 591 | if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ |
| 592 | Tcl_BackgroundError(pDb->interp); |
| 593 | } |
| 594 | } |
| 595 | |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 596 | /* |
| 597 | ** This procedure handles wal_hook callbacks. |
| 598 | */ |
| 599 | static int DbWalHandler( |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 600 | void *clientData, |
| 601 | sqlite3 *db, |
| 602 | const char *zDb, |
| 603 | int nEntry |
| 604 | ){ |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 605 | int ret = SQLITE_OK; |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 606 | Tcl_Obj *p; |
| 607 | SqliteDb *pDb = (SqliteDb*)clientData; |
| 608 | Tcl_Interp *interp = pDb->interp; |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 609 | assert(pDb->pWalHook); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 610 | |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 611 | p = Tcl_DuplicateObj(pDb->pWalHook); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 612 | Tcl_IncrRefCount(p); |
| 613 | Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); |
| 614 | Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); |
| 615 | if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) |
| 616 | || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) |
| 617 | ){ |
| 618 | Tcl_BackgroundError(interp); |
| 619 | } |
| 620 | Tcl_DecrRefCount(p); |
| 621 | |
| 622 | return ret; |
| 623 | } |
| 624 | |
drh | bcf4f48 | 2009-03-27 12:44:35 +0000 | [diff] [blame] | 625 | #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 626 | static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ |
| 627 | char zBuf[64]; |
| 628 | sprintf(zBuf, "%d", iArg); |
| 629 | Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); |
| 630 | sprintf(zBuf, "%d", nArg); |
| 631 | Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); |
| 632 | } |
| 633 | #else |
drh | bcf4f48 | 2009-03-27 12:44:35 +0000 | [diff] [blame] | 634 | # define setTestUnlockNotifyVars(x,y,z) |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 635 | #endif |
| 636 | |
drh | 69910da | 2009-03-27 12:32:54 +0000 | [diff] [blame] | 637 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 638 | static void DbUnlockNotify(void **apArg, int nArg){ |
| 639 | int i; |
| 640 | for(i=0; i<nArg; i++){ |
| 641 | const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); |
| 642 | SqliteDb *pDb = (SqliteDb *)apArg[i]; |
| 643 | setTestUnlockNotifyVars(pDb->interp, i, nArg); |
| 644 | assert( pDb->pUnlockNotify); |
| 645 | Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); |
| 646 | Tcl_DecrRefCount(pDb->pUnlockNotify); |
| 647 | pDb->pUnlockNotify = 0; |
| 648 | } |
| 649 | } |
drh | 69910da | 2009-03-27 12:32:54 +0000 | [diff] [blame] | 650 | #endif |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 651 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 652 | static void DbUpdateHandler( |
| 653 | void *p, |
| 654 | int op, |
| 655 | const char *zDb, |
| 656 | const char *zTbl, |
| 657 | sqlite_int64 rowid |
| 658 | ){ |
| 659 | SqliteDb *pDb = (SqliteDb *)p; |
| 660 | Tcl_Obj *pCmd; |
| 661 | |
| 662 | assert( pDb->pUpdateHook ); |
| 663 | assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); |
| 664 | |
| 665 | pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); |
| 666 | Tcl_IncrRefCount(pCmd); |
| 667 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( |
| 668 | ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); |
| 669 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); |
| 670 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); |
| 671 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); |
| 672 | Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); |
| 673 | } |
| 674 | |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 675 | static void tclCollateNeeded( |
| 676 | void *pCtx, |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 677 | sqlite3 *db, |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 678 | int enc, |
| 679 | const char *zName |
| 680 | ){ |
| 681 | SqliteDb *pDb = (SqliteDb *)pCtx; |
| 682 | Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); |
| 683 | Tcl_IncrRefCount(pScript); |
| 684 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); |
| 685 | Tcl_EvalObjEx(pDb->interp, pScript, 0); |
| 686 | Tcl_DecrRefCount(pScript); |
| 687 | } |
| 688 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 689 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 690 | ** This routine is called to evaluate an SQL collation function implemented |
| 691 | ** using TCL script. |
| 692 | */ |
| 693 | static int tclSqlCollate( |
| 694 | void *pCtx, |
| 695 | int nA, |
| 696 | const void *zA, |
| 697 | int nB, |
| 698 | const void *zB |
| 699 | ){ |
| 700 | SqlCollate *p = (SqlCollate *)pCtx; |
| 701 | Tcl_Obj *pCmd; |
| 702 | |
| 703 | pCmd = Tcl_NewStringObj(p->zScript, -1); |
| 704 | Tcl_IncrRefCount(pCmd); |
| 705 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); |
| 706 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 707 | Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 708 | Tcl_DecrRefCount(pCmd); |
| 709 | return (atoi(Tcl_GetStringResult(p->interp))); |
| 710 | } |
| 711 | |
| 712 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 713 | ** This routine is called to evaluate an SQL function implemented |
| 714 | ** using TCL script. |
| 715 | */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 716 | static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 717 | SqlFunc *p = sqlite3_user_data(context); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 718 | Tcl_Obj *pCmd; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 719 | int i; |
| 720 | int rc; |
| 721 | |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 722 | if( argc==0 ){ |
| 723 | /* If there are no arguments to the function, call Tcl_EvalObjEx on the |
| 724 | ** script object directly. This allows the TCL compiler to generate |
| 725 | ** bytecode for the command on the first invocation and thus make |
| 726 | ** subsequent invocations much faster. */ |
| 727 | pCmd = p->pScript; |
| 728 | Tcl_IncrRefCount(pCmd); |
| 729 | rc = Tcl_EvalObjEx(p->interp, pCmd, 0); |
| 730 | Tcl_DecrRefCount(pCmd); |
| 731 | }else{ |
| 732 | /* If there are arguments to the function, make a shallow copy of the |
| 733 | ** script object, lappend the arguments, then evaluate the copy. |
| 734 | ** |
| 735 | ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated. |
| 736 | ** The new Tcl_Obj contains pointers to the original list elements. |
| 737 | ** That way, when Tcl_EvalObjv() is run and shimmers the first element |
| 738 | ** of the list to tclCmdNameType, that alternate representation will |
| 739 | ** be preserved and reused on the next invocation. |
| 740 | */ |
| 741 | Tcl_Obj **aArg; |
| 742 | int nArg; |
| 743 | if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ |
| 744 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 745 | return; |
| 746 | } |
| 747 | pCmd = Tcl_NewListObj(nArg, aArg); |
| 748 | Tcl_IncrRefCount(pCmd); |
| 749 | for(i=0; i<argc; i++){ |
| 750 | sqlite3_value *pIn = argv[i]; |
| 751 | Tcl_Obj *pVal; |
| 752 | |
| 753 | /* Set pVal to contain the i'th column of this row. */ |
| 754 | switch( sqlite3_value_type(pIn) ){ |
| 755 | case SQLITE_BLOB: { |
| 756 | int bytes = sqlite3_value_bytes(pIn); |
| 757 | pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); |
| 758 | break; |
| 759 | } |
| 760 | case SQLITE_INTEGER: { |
| 761 | sqlite_int64 v = sqlite3_value_int64(pIn); |
| 762 | if( v>=-2147483647 && v<=2147483647 ){ |
| 763 | pVal = Tcl_NewIntObj(v); |
| 764 | }else{ |
| 765 | pVal = Tcl_NewWideIntObj(v); |
| 766 | } |
| 767 | break; |
| 768 | } |
| 769 | case SQLITE_FLOAT: { |
| 770 | double r = sqlite3_value_double(pIn); |
| 771 | pVal = Tcl_NewDoubleObj(r); |
| 772 | break; |
| 773 | } |
| 774 | case SQLITE_NULL: { |
| 775 | pVal = Tcl_NewStringObj("", 0); |
| 776 | break; |
| 777 | } |
| 778 | default: { |
| 779 | int bytes = sqlite3_value_bytes(pIn); |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 780 | pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | } |
| 784 | rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); |
| 785 | if( rc ){ |
| 786 | Tcl_DecrRefCount(pCmd); |
| 787 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 788 | return; |
| 789 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 790 | } |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 791 | if( !p->useEvalObjv ){ |
| 792 | /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd |
| 793 | ** is a list without a string representation. To prevent this from |
| 794 | ** happening, make sure pCmd has a valid string representation */ |
| 795 | Tcl_GetString(pCmd); |
| 796 | } |
| 797 | rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
| 798 | Tcl_DecrRefCount(pCmd); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 799 | } |
danielk1977 | 562e8d3 | 2005-05-20 09:40:55 +0000 | [diff] [blame] | 800 | |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 801 | if( rc && rc!=TCL_RETURN ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 802 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 803 | }else{ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 804 | Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); |
| 805 | int n; |
| 806 | u8 *data; |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 807 | const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 808 | char c = zType[0]; |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 809 | if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 810 | /* Only return a BLOB type if the Tcl variable is a bytearray and |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 811 | ** has no string representation. */ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 812 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 813 | sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); |
drh | 985e0c6 | 2007-06-26 22:55:37 +0000 | [diff] [blame] | 814 | }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 815 | Tcl_GetIntFromObj(0, pVar, &n); |
| 816 | sqlite3_result_int(context, n); |
| 817 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 818 | double r; |
| 819 | Tcl_GetDoubleFromObj(0, pVar, &r); |
| 820 | sqlite3_result_double(context, r); |
drh | 985e0c6 | 2007-06-26 22:55:37 +0000 | [diff] [blame] | 821 | }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
| 822 | (c=='i' && strcmp(zType,"int")==0) ){ |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 823 | Tcl_WideInt v; |
| 824 | Tcl_GetWideIntFromObj(0, pVar, &v); |
| 825 | sqlite3_result_int64(context, v); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 826 | }else{ |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 827 | data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
| 828 | sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 829 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 830 | } |
| 831 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 832 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 833 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 834 | /* |
| 835 | ** This is the authentication function. It appends the authentication |
| 836 | ** type code and the two arguments to zCmd[] then invokes the result |
| 837 | ** on the interpreter. The reply is examined to determine if the |
| 838 | ** authentication fails or succeeds. |
| 839 | */ |
| 840 | static int auth_callback( |
| 841 | void *pArg, |
| 842 | int code, |
| 843 | const char *zArg1, |
| 844 | const char *zArg2, |
| 845 | const char *zArg3, |
| 846 | const char *zArg4 |
| 847 | ){ |
| 848 | char *zCode; |
| 849 | Tcl_DString str; |
| 850 | int rc; |
| 851 | const char *zReply; |
| 852 | SqliteDb *pDb = (SqliteDb*)pArg; |
drh | 1f1549f | 2008-08-26 21:33:34 +0000 | [diff] [blame] | 853 | if( pDb->disableAuth ) return SQLITE_OK; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 854 | |
| 855 | switch( code ){ |
| 856 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 857 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 858 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 859 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 860 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 861 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 862 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 863 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 864 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 865 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 866 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 867 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 868 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 869 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 870 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 871 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 872 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 873 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 874 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 875 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 876 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 877 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 878 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 879 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
drh | 81e293b | 2003-06-06 19:00:42 +0000 | [diff] [blame] | 880 | case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
| 881 | case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
danielk1977 | 1c8c23c | 2004-11-12 15:53:37 +0000 | [diff] [blame] | 882 | case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 883 | case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 884 | case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 885 | case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; |
| 886 | case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 887 | case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 888 | case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 889 | default : zCode="????"; break; |
| 890 | } |
| 891 | Tcl_DStringInit(&str); |
| 892 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 893 | Tcl_DStringAppendElement(&str, zCode); |
| 894 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 895 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 896 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 897 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 898 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 899 | Tcl_DStringFree(&str); |
| 900 | zReply = Tcl_GetStringResult(pDb->interp); |
| 901 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 902 | rc = SQLITE_OK; |
| 903 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 904 | rc = SQLITE_DENY; |
| 905 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 906 | rc = SQLITE_IGNORE; |
| 907 | }else{ |
| 908 | rc = 999; |
| 909 | } |
| 910 | return rc; |
| 911 | } |
| 912 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 913 | |
| 914 | /* |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 915 | ** zText is a pointer to text obtained via an sqlite3_result_text() |
| 916 | ** or similar interface. This routine returns a Tcl string object, |
| 917 | ** reference count set to 0, containing the text. If a translation |
| 918 | ** between iso8859 and UTF-8 is required, it is preformed. |
| 919 | */ |
| 920 | static Tcl_Obj *dbTextToObj(char const *zText){ |
| 921 | Tcl_Obj *pVal; |
| 922 | #ifdef UTF_TRANSLATION_NEEDED |
| 923 | Tcl_DString dCol; |
| 924 | Tcl_DStringInit(&dCol); |
| 925 | Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); |
| 926 | pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 927 | Tcl_DStringFree(&dCol); |
| 928 | #else |
| 929 | pVal = Tcl_NewStringObj(zText, -1); |
| 930 | #endif |
| 931 | return pVal; |
| 932 | } |
| 933 | |
| 934 | /* |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 935 | ** This routine reads a line of text from FILE in, stores |
| 936 | ** the text in memory obtained from malloc() and returns a pointer |
| 937 | ** to the text. NULL is returned at end of file, or if malloc() |
| 938 | ** fails. |
| 939 | ** |
| 940 | ** The interface is like "readline" but no command-line editing |
| 941 | ** is done. |
| 942 | ** |
| 943 | ** copied from shell.c from '.import' command |
| 944 | */ |
| 945 | static char *local_getline(char *zPrompt, FILE *in){ |
| 946 | char *zLine; |
| 947 | int nLine; |
| 948 | int n; |
| 949 | int eol; |
| 950 | |
| 951 | nLine = 100; |
| 952 | zLine = malloc( nLine ); |
| 953 | if( zLine==0 ) return 0; |
| 954 | n = 0; |
| 955 | eol = 0; |
| 956 | while( !eol ){ |
| 957 | if( n+100>nLine ){ |
| 958 | nLine = nLine*2 + 100; |
| 959 | zLine = realloc(zLine, nLine); |
| 960 | if( zLine==0 ) return 0; |
| 961 | } |
| 962 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 963 | if( n==0 ){ |
| 964 | free(zLine); |
| 965 | return 0; |
| 966 | } |
| 967 | zLine[n] = 0; |
| 968 | eol = 1; |
| 969 | break; |
| 970 | } |
| 971 | while( zLine[n] ){ n++; } |
| 972 | if( n>0 && zLine[n-1]=='\n' ){ |
| 973 | n--; |
| 974 | zLine[n] = 0; |
| 975 | eol = 1; |
| 976 | } |
| 977 | } |
| 978 | zLine = realloc( zLine, n+1 ); |
| 979 | return zLine; |
| 980 | } |
| 981 | |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 982 | |
| 983 | /* |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 984 | ** This function is part of the implementation of the command: |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 985 | ** |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 986 | ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 987 | ** |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 988 | ** It is invoked after evaluating the script SCRIPT to commit or rollback |
| 989 | ** the transaction or savepoint opened by the [transaction] command. |
| 990 | */ |
| 991 | static int DbTransPostCmd( |
| 992 | ClientData data[], /* data[0] is the Sqlite3Db* for $db */ |
| 993 | Tcl_Interp *interp, /* Tcl interpreter */ |
| 994 | int result /* Result of evaluating SCRIPT */ |
| 995 | ){ |
| 996 | static const char *azEnd[] = { |
| 997 | "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ |
| 998 | "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ |
| 999 | "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", |
| 1000 | "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ |
| 1001 | }; |
| 1002 | SqliteDb *pDb = (SqliteDb*)data[0]; |
| 1003 | int rc = result; |
| 1004 | const char *zEnd; |
| 1005 | |
| 1006 | pDb->nTransaction--; |
| 1007 | zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; |
| 1008 | |
| 1009 | pDb->disableAuth++; |
| 1010 | if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ |
| 1011 | /* This is a tricky scenario to handle. The most likely cause of an |
| 1012 | ** error is that the exec() above was an attempt to commit the |
| 1013 | ** top-level transaction that returned SQLITE_BUSY. Or, less likely, |
| 1014 | ** that an IO-error has occured. In either case, throw a Tcl exception |
| 1015 | ** and try to rollback the transaction. |
| 1016 | ** |
| 1017 | ** But it could also be that the user executed one or more BEGIN, |
| 1018 | ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing |
| 1019 | ** this method's logic. Not clear how this would be best handled. |
| 1020 | */ |
| 1021 | if( rc!=TCL_ERROR ){ |
| 1022 | Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
| 1023 | rc = TCL_ERROR; |
| 1024 | } |
| 1025 | sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); |
| 1026 | } |
| 1027 | pDb->disableAuth--; |
| 1028 | |
| 1029 | return rc; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | ** Search the cache for a prepared-statement object that implements the |
| 1034 | ** first SQL statement in the buffer pointed to by parameter zIn. If |
| 1035 | ** no such prepared-statement can be found, allocate and prepare a new |
| 1036 | ** one. In either case, bind the current values of the relevant Tcl |
| 1037 | ** variables to any $var, :var or @var variables in the statement. Before |
| 1038 | ** returning, set *ppPreStmt to point to the prepared-statement object. |
| 1039 | ** |
| 1040 | ** Output parameter *pzOut is set to point to the next SQL statement in |
| 1041 | ** buffer zIn, or to the '\0' byte at the end of zIn if there is no |
| 1042 | ** next statement. |
| 1043 | ** |
| 1044 | ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned |
| 1045 | ** and an error message loaded into interpreter pDb->interp. |
| 1046 | */ |
| 1047 | static int dbPrepareAndBind( |
| 1048 | SqliteDb *pDb, /* Database object */ |
| 1049 | char const *zIn, /* SQL to compile */ |
| 1050 | char const **pzOut, /* OUT: Pointer to next SQL statement */ |
| 1051 | SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ |
| 1052 | ){ |
| 1053 | const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ |
| 1054 | sqlite3_stmt *pStmt; /* Prepared statement object */ |
| 1055 | SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ |
| 1056 | int nSql; /* Length of zSql in bytes */ |
| 1057 | int nVar; /* Number of variables in statement */ |
| 1058 | int iParm = 0; /* Next free entry in apParm */ |
| 1059 | int i; |
| 1060 | Tcl_Interp *interp = pDb->interp; |
| 1061 | |
| 1062 | *ppPreStmt = 0; |
| 1063 | |
| 1064 | /* Trim spaces from the start of zSql and calculate the remaining length. */ |
| 1065 | while( isspace(zSql[0]) ){ zSql++; } |
| 1066 | nSql = strlen30(zSql); |
| 1067 | |
| 1068 | for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ |
| 1069 | int n = pPreStmt->nSql; |
| 1070 | if( nSql>=n |
| 1071 | && memcmp(pPreStmt->zSql, zSql, n)==0 |
| 1072 | && (zSql[n]==0 || zSql[n-1]==';') |
| 1073 | ){ |
| 1074 | pStmt = pPreStmt->pStmt; |
| 1075 | *pzOut = &zSql[pPreStmt->nSql]; |
| 1076 | |
| 1077 | /* When a prepared statement is found, unlink it from the |
| 1078 | ** cache list. It will later be added back to the beginning |
| 1079 | ** of the cache list in order to implement LRU replacement. |
| 1080 | */ |
| 1081 | if( pPreStmt->pPrev ){ |
| 1082 | pPreStmt->pPrev->pNext = pPreStmt->pNext; |
| 1083 | }else{ |
| 1084 | pDb->stmtList = pPreStmt->pNext; |
| 1085 | } |
| 1086 | if( pPreStmt->pNext ){ |
| 1087 | pPreStmt->pNext->pPrev = pPreStmt->pPrev; |
| 1088 | }else{ |
| 1089 | pDb->stmtLast = pPreStmt->pPrev; |
| 1090 | } |
| 1091 | pDb->nStmt--; |
| 1092 | nVar = sqlite3_bind_parameter_count(pStmt); |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* If no prepared statement was found. Compile the SQL text. Also allocate |
| 1098 | ** a new SqlPreparedStmt structure. */ |
| 1099 | if( pPreStmt==0 ){ |
| 1100 | int nByte; |
| 1101 | |
| 1102 | if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, pzOut) ){ |
| 1103 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 1104 | return TCL_ERROR; |
| 1105 | } |
| 1106 | if( pStmt==0 ){ |
| 1107 | if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
| 1108 | /* A compile-time error in the statement. */ |
| 1109 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 1110 | return TCL_ERROR; |
| 1111 | }else{ |
| 1112 | /* The statement was a no-op. Continue to the next statement |
| 1113 | ** in the SQL string. |
| 1114 | */ |
| 1115 | return TCL_OK; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | assert( pPreStmt==0 ); |
| 1120 | nVar = sqlite3_bind_parameter_count(pStmt); |
| 1121 | nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); |
| 1122 | pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); |
| 1123 | memset(pPreStmt, 0, nByte); |
| 1124 | |
| 1125 | pPreStmt->pStmt = pStmt; |
| 1126 | pPreStmt->nSql = (*pzOut - zSql); |
| 1127 | pPreStmt->zSql = sqlite3_sql(pStmt); |
| 1128 | pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; |
| 1129 | } |
| 1130 | assert( pPreStmt ); |
| 1131 | assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); |
| 1132 | assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); |
| 1133 | |
| 1134 | /* Bind values to parameters that begin with $ or : */ |
| 1135 | for(i=1; i<=nVar; i++){ |
| 1136 | const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
| 1137 | if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ |
| 1138 | Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
| 1139 | if( pVar ){ |
| 1140 | int n; |
| 1141 | u8 *data; |
| 1142 | const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); |
| 1143 | char c = zType[0]; |
| 1144 | if( zVar[0]=='@' || |
| 1145 | (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ |
| 1146 | /* Load a BLOB type if the Tcl variable is a bytearray and |
| 1147 | ** it has no string representation or the host |
| 1148 | ** parameter name begins with "@". */ |
| 1149 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 1150 | sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
| 1151 | Tcl_IncrRefCount(pVar); |
| 1152 | pPreStmt->apParm[iParm++] = pVar; |
| 1153 | }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
| 1154 | Tcl_GetIntFromObj(interp, pVar, &n); |
| 1155 | sqlite3_bind_int(pStmt, i, n); |
| 1156 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 1157 | double r; |
| 1158 | Tcl_GetDoubleFromObj(interp, pVar, &r); |
| 1159 | sqlite3_bind_double(pStmt, i, r); |
| 1160 | }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
| 1161 | (c=='i' && strcmp(zType,"int")==0) ){ |
| 1162 | Tcl_WideInt v; |
| 1163 | Tcl_GetWideIntFromObj(interp, pVar, &v); |
| 1164 | sqlite3_bind_int64(pStmt, i, v); |
| 1165 | }else{ |
| 1166 | data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
| 1167 | sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); |
| 1168 | Tcl_IncrRefCount(pVar); |
| 1169 | pPreStmt->apParm[iParm++] = pVar; |
| 1170 | } |
| 1171 | }else{ |
| 1172 | sqlite3_bind_null(pStmt, i); |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | pPreStmt->nParm = iParm; |
| 1177 | *ppPreStmt = pPreStmt; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1178 | |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1179 | return TCL_OK; |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | /* |
| 1184 | ** Release a statement reference obtained by calling dbPrepareAndBind(). |
| 1185 | ** There should be exactly one call to this function for each call to |
| 1186 | ** dbPrepareAndBind(). |
| 1187 | ** |
| 1188 | ** If the discard parameter is non-zero, then the statement is deleted |
| 1189 | ** immediately. Otherwise it is added to the LRU list and may be returned |
| 1190 | ** by a subsequent call to dbPrepareAndBind(). |
| 1191 | */ |
| 1192 | static void dbReleaseStmt( |
| 1193 | SqliteDb *pDb, /* Database handle */ |
| 1194 | SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ |
| 1195 | int discard /* True to delete (not cache) the pPreStmt */ |
| 1196 | ){ |
| 1197 | int i; |
| 1198 | |
| 1199 | /* Free the bound string and blob parameters */ |
| 1200 | for(i=0; i<pPreStmt->nParm; i++){ |
| 1201 | Tcl_DecrRefCount(pPreStmt->apParm[i]); |
| 1202 | } |
| 1203 | pPreStmt->nParm = 0; |
| 1204 | |
| 1205 | if( pDb->maxStmt<=0 || discard ){ |
| 1206 | /* If the cache is turned off, deallocated the statement */ |
| 1207 | sqlite3_finalize(pPreStmt->pStmt); |
| 1208 | Tcl_Free((char *)pPreStmt); |
| 1209 | }else{ |
| 1210 | /* Add the prepared statement to the beginning of the cache list. */ |
| 1211 | pPreStmt->pNext = pDb->stmtList; |
| 1212 | pPreStmt->pPrev = 0; |
| 1213 | if( pDb->stmtList ){ |
| 1214 | pDb->stmtList->pPrev = pPreStmt; |
| 1215 | } |
| 1216 | pDb->stmtList = pPreStmt; |
| 1217 | if( pDb->stmtLast==0 ){ |
| 1218 | assert( pDb->nStmt==0 ); |
| 1219 | pDb->stmtLast = pPreStmt; |
| 1220 | }else{ |
| 1221 | assert( pDb->nStmt>0 ); |
| 1222 | } |
| 1223 | pDb->nStmt++; |
| 1224 | |
| 1225 | /* If we have too many statement in cache, remove the surplus from |
| 1226 | ** the end of the cache list. */ |
| 1227 | while( pDb->nStmt>pDb->maxStmt ){ |
| 1228 | sqlite3_finalize(pDb->stmtLast->pStmt); |
| 1229 | pDb->stmtLast = pDb->stmtLast->pPrev; |
| 1230 | Tcl_Free((char*)pDb->stmtLast->pNext); |
| 1231 | pDb->stmtLast->pNext = 0; |
| 1232 | pDb->nStmt--; |
| 1233 | } |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | ** Structure used with dbEvalXXX() functions: |
| 1239 | ** |
| 1240 | ** dbEvalInit() |
| 1241 | ** dbEvalStep() |
| 1242 | ** dbEvalFinalize() |
| 1243 | ** dbEvalRowInfo() |
| 1244 | ** dbEvalColumnValue() |
| 1245 | */ |
| 1246 | typedef struct DbEvalContext DbEvalContext; |
| 1247 | struct DbEvalContext { |
| 1248 | SqliteDb *pDb; /* Database handle */ |
| 1249 | Tcl_Obj *pSql; /* Object holding string zSql */ |
| 1250 | const char *zSql; /* Remaining SQL to execute */ |
| 1251 | SqlPreparedStmt *pPreStmt; /* Current statement */ |
| 1252 | int nCol; /* Number of columns returned by pStmt */ |
| 1253 | Tcl_Obj *pArray; /* Name of array variable */ |
| 1254 | Tcl_Obj **apColName; /* Array of column names */ |
| 1255 | }; |
| 1256 | |
| 1257 | /* |
| 1258 | ** Release any cache of column names currently held as part of |
| 1259 | ** the DbEvalContext structure passed as the first argument. |
| 1260 | */ |
| 1261 | static void dbReleaseColumnNames(DbEvalContext *p){ |
| 1262 | if( p->apColName ){ |
| 1263 | int i; |
| 1264 | for(i=0; i<p->nCol; i++){ |
| 1265 | Tcl_DecrRefCount(p->apColName[i]); |
| 1266 | } |
| 1267 | Tcl_Free((char *)p->apColName); |
| 1268 | p->apColName = 0; |
| 1269 | } |
| 1270 | p->nCol = 0; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | ** Initialize a DbEvalContext structure. |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1275 | ** |
| 1276 | ** If pArray is not NULL, then it contains the name of a Tcl array |
| 1277 | ** variable. The "*" member of this array is set to a list containing |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1278 | ** the names of the columns returned by the statement as part of each |
| 1279 | ** call to dbEvalStep(), in order from left to right. e.g. if the names |
| 1280 | ** of the returned columns are a, b and c, it does the equivalent of the |
| 1281 | ** tcl command: |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1282 | ** |
| 1283 | ** set ${pArray}(*) {a b c} |
| 1284 | */ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1285 | static void dbEvalInit( |
| 1286 | DbEvalContext *p, /* Pointer to structure to initialize */ |
| 1287 | SqliteDb *pDb, /* Database handle */ |
| 1288 | Tcl_Obj *pSql, /* Object containing SQL script */ |
| 1289 | Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1290 | ){ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1291 | memset(p, 0, sizeof(DbEvalContext)); |
| 1292 | p->pDb = pDb; |
| 1293 | p->zSql = Tcl_GetString(pSql); |
| 1294 | p->pSql = pSql; |
| 1295 | Tcl_IncrRefCount(pSql); |
| 1296 | if( pArray ){ |
| 1297 | p->pArray = pArray; |
| 1298 | Tcl_IncrRefCount(pArray); |
| 1299 | } |
| 1300 | } |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1301 | |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1302 | /* |
| 1303 | ** Obtain information about the row that the DbEvalContext passed as the |
| 1304 | ** first argument currently points to. |
| 1305 | */ |
| 1306 | static void dbEvalRowInfo( |
| 1307 | DbEvalContext *p, /* Evaluation context */ |
| 1308 | int *pnCol, /* OUT: Number of column names */ |
| 1309 | Tcl_Obj ***papColName /* OUT: Array of column names */ |
| 1310 | ){ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1311 | /* Compute column names */ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1312 | if( 0==p->apColName ){ |
| 1313 | sqlite3_stmt *pStmt = p->pPreStmt->pStmt; |
| 1314 | int i; /* Iterator variable */ |
| 1315 | int nCol; /* Number of columns returned by pStmt */ |
| 1316 | Tcl_Obj **apColName = 0; /* Array of column names */ |
| 1317 | |
| 1318 | p->nCol = nCol = sqlite3_column_count(pStmt); |
| 1319 | if( nCol>0 && (papColName || p->pArray) ){ |
| 1320 | apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
| 1321 | for(i=0; i<nCol; i++){ |
| 1322 | apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
| 1323 | Tcl_IncrRefCount(apColName[i]); |
| 1324 | } |
| 1325 | p->apColName = apColName; |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* If results are being stored in an array variable, then create |
| 1329 | ** the array(*) entry for that array |
| 1330 | */ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1331 | if( p->pArray ){ |
| 1332 | Tcl_Interp *interp = p->pDb->interp; |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1333 | Tcl_Obj *pColList = Tcl_NewObj(); |
| 1334 | Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1335 | |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1336 | for(i=0; i<nCol; i++){ |
| 1337 | Tcl_ListObjAppendElement(interp, pColList, apColName[i]); |
| 1338 | } |
| 1339 | Tcl_IncrRefCount(pStar); |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1340 | Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1341 | Tcl_DecrRefCount(pStar); |
| 1342 | } |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1345 | if( papColName ){ |
| 1346 | *papColName = p->apColName; |
| 1347 | } |
| 1348 | if( pnCol ){ |
| 1349 | *pnCol = p->nCol; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | /* |
| 1354 | ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is |
| 1355 | ** returned, then an error message is stored in the interpreter before |
| 1356 | ** returning. |
| 1357 | ** |
| 1358 | ** A return value of TCL_OK means there is a row of data available. The |
| 1359 | ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This |
| 1360 | ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK |
| 1361 | ** is returned, then the SQL script has finished executing and there are |
| 1362 | ** no further rows available. This is similar to SQLITE_DONE. |
| 1363 | */ |
| 1364 | static int dbEvalStep(DbEvalContext *p){ |
| 1365 | while( p->zSql[0] || p->pPreStmt ){ |
| 1366 | int rc; |
| 1367 | if( p->pPreStmt==0 ){ |
| 1368 | rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); |
| 1369 | if( rc!=TCL_OK ) return rc; |
| 1370 | }else{ |
| 1371 | int rcs; |
| 1372 | SqliteDb *pDb = p->pDb; |
| 1373 | SqlPreparedStmt *pPreStmt = p->pPreStmt; |
| 1374 | sqlite3_stmt *pStmt = pPreStmt->pStmt; |
| 1375 | |
| 1376 | rcs = sqlite3_step(pStmt); |
| 1377 | if( rcs==SQLITE_ROW ){ |
| 1378 | return TCL_OK; |
| 1379 | } |
| 1380 | if( p->pArray ){ |
| 1381 | dbEvalRowInfo(p, 0, 0); |
| 1382 | } |
| 1383 | rcs = sqlite3_reset(pStmt); |
| 1384 | |
| 1385 | pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); |
| 1386 | pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); |
drh | 3c379b0 | 2010-04-07 19:31:59 +0000 | [diff] [blame] | 1387 | pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1388 | dbReleaseColumnNames(p); |
| 1389 | p->pPreStmt = 0; |
| 1390 | |
| 1391 | if( rcs!=SQLITE_OK ){ |
| 1392 | /* If a run-time error occurs, report the error and stop reading |
| 1393 | ** the SQL. */ |
| 1394 | Tcl_SetObjResult(pDb->interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 1395 | dbReleaseStmt(pDb, pPreStmt, 1); |
| 1396 | return TCL_ERROR; |
| 1397 | }else{ |
| 1398 | dbReleaseStmt(pDb, pPreStmt, 0); |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | /* Finished */ |
| 1404 | return TCL_BREAK; |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | ** Free all resources currently held by the DbEvalContext structure passed |
| 1409 | ** as the first argument. There should be exactly one call to this function |
| 1410 | ** for each call to dbEvalInit(). |
| 1411 | */ |
| 1412 | static void dbEvalFinalize(DbEvalContext *p){ |
| 1413 | if( p->pPreStmt ){ |
| 1414 | sqlite3_reset(p->pPreStmt->pStmt); |
| 1415 | dbReleaseStmt(p->pDb, p->pPreStmt, 0); |
| 1416 | p->pPreStmt = 0; |
| 1417 | } |
| 1418 | if( p->pArray ){ |
| 1419 | Tcl_DecrRefCount(p->pArray); |
| 1420 | p->pArray = 0; |
| 1421 | } |
| 1422 | Tcl_DecrRefCount(p->pSql); |
| 1423 | dbReleaseColumnNames(p); |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains |
| 1428 | ** the value for the iCol'th column of the row currently pointed to by |
| 1429 | ** the DbEvalContext structure passed as the first argument. |
| 1430 | */ |
| 1431 | static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ |
| 1432 | sqlite3_stmt *pStmt = p->pPreStmt->pStmt; |
| 1433 | switch( sqlite3_column_type(pStmt, iCol) ){ |
| 1434 | case SQLITE_BLOB: { |
| 1435 | int bytes = sqlite3_column_bytes(pStmt, iCol); |
| 1436 | const char *zBlob = sqlite3_column_blob(pStmt, iCol); |
| 1437 | if( !zBlob ) bytes = 0; |
| 1438 | return Tcl_NewByteArrayObj((u8*)zBlob, bytes); |
| 1439 | } |
| 1440 | case SQLITE_INTEGER: { |
| 1441 | sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); |
| 1442 | if( v>=-2147483647 && v<=2147483647 ){ |
| 1443 | return Tcl_NewIntObj(v); |
| 1444 | }else{ |
| 1445 | return Tcl_NewWideIntObj(v); |
| 1446 | } |
| 1447 | } |
| 1448 | case SQLITE_FLOAT: { |
| 1449 | return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); |
| 1450 | } |
| 1451 | case SQLITE_NULL: { |
| 1452 | return dbTextToObj(p->pDb->zNull); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | return dbTextToObj((char *)sqlite3_column_text(pStmt, iCol)); |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | ** If using Tcl version 8.6 or greater, use the NR functions to avoid |
| 1461 | ** recursive evalution of scripts by the [db eval] and [db trans] |
| 1462 | ** commands. Even if the headers used while compiling the extension |
| 1463 | ** are 8.6 or newer, the code still tests the Tcl version at runtime. |
| 1464 | ** This allows stubs-enabled builds to be used with older Tcl libraries. |
| 1465 | */ |
| 1466 | #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 1467 | # define SQLITE_TCL_NRE 1 |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1468 | static int DbUseNre(void){ |
| 1469 | int major, minor; |
| 1470 | Tcl_GetVersion(&major, &minor, 0, 0); |
| 1471 | return( (major==8 && minor>=6) || major>8 ); |
| 1472 | } |
| 1473 | #else |
| 1474 | /* |
| 1475 | ** Compiling using headers earlier than 8.6. In this case NR cannot be |
| 1476 | ** used, so DbUseNre() to always return zero. Add #defines for the other |
| 1477 | ** Tcl_NRxxx() functions to prevent them from causing compilation errors, |
| 1478 | ** even though the only invocations of them are within conditional blocks |
| 1479 | ** of the form: |
| 1480 | ** |
| 1481 | ** if( DbUseNre() ) { ... } |
| 1482 | */ |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 1483 | # define SQLITE_TCL_NRE 0 |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 1484 | # define DbUseNre() 0 |
| 1485 | # define Tcl_NRAddCallback(a,b,c,d,e,f) 0 |
| 1486 | # define Tcl_NREvalObj(a,b,c) 0 |
| 1487 | # define Tcl_NRCreateCommand(a,b,c,d,e,f) 0 |
| 1488 | #endif |
| 1489 | |
| 1490 | /* |
| 1491 | ** This function is part of the implementation of the command: |
| 1492 | ** |
| 1493 | ** $db eval SQL ?ARRAYNAME? SCRIPT |
| 1494 | */ |
| 1495 | static int DbEvalNextCmd( |
| 1496 | ClientData data[], /* data[0] is the (DbEvalContext*) */ |
| 1497 | Tcl_Interp *interp, /* Tcl interpreter */ |
| 1498 | int result /* Result so far */ |
| 1499 | ){ |
| 1500 | int rc = result; /* Return code */ |
| 1501 | |
| 1502 | /* The first element of the data[] array is a pointer to a DbEvalContext |
| 1503 | ** structure allocated using Tcl_Alloc(). The second element of data[] |
| 1504 | ** is a pointer to a Tcl_Obj containing the script to run for each row |
| 1505 | ** returned by the queries encapsulated in data[0]. */ |
| 1506 | DbEvalContext *p = (DbEvalContext *)data[0]; |
| 1507 | Tcl_Obj *pScript = (Tcl_Obj *)data[1]; |
| 1508 | Tcl_Obj *pArray = p->pArray; |
| 1509 | |
| 1510 | while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ |
| 1511 | int i; |
| 1512 | int nCol; |
| 1513 | Tcl_Obj **apColName; |
| 1514 | dbEvalRowInfo(p, &nCol, &apColName); |
| 1515 | for(i=0; i<nCol; i++){ |
| 1516 | Tcl_Obj *pVal = dbEvalColumnValue(p, i); |
| 1517 | if( pArray==0 ){ |
| 1518 | Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
| 1519 | }else{ |
| 1520 | Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | /* The required interpreter variables are now populated with the data |
| 1525 | ** from the current row. If using NRE, schedule callbacks to evaluate |
| 1526 | ** script pScript, then to invoke this function again to fetch the next |
| 1527 | ** row (or clean up if there is no next row or the script throws an |
| 1528 | ** exception). After scheduling the callbacks, return control to the |
| 1529 | ** caller. |
| 1530 | ** |
| 1531 | ** If not using NRE, evaluate pScript directly and continue with the |
| 1532 | ** next iteration of this while(...) loop. */ |
| 1533 | if( DbUseNre() ){ |
| 1534 | Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); |
| 1535 | return Tcl_NREvalObj(interp, pScript, 0); |
| 1536 | }else{ |
| 1537 | rc = Tcl_EvalObjEx(interp, pScript, 0); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | Tcl_DecrRefCount(pScript); |
| 1542 | dbEvalFinalize(p); |
| 1543 | Tcl_Free((char *)p); |
| 1544 | |
| 1545 | if( rc==TCL_OK || rc==TCL_BREAK ){ |
| 1546 | Tcl_ResetResult(interp); |
| 1547 | rc = TCL_OK; |
| 1548 | } |
| 1549 | return rc; |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 1552 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1553 | ** The "sqlite" command below creates a new Tcl command for each |
| 1554 | ** connection it opens to an SQLite database. This routine is invoked |
| 1555 | ** whenever one of those connection-specific commands is executed |
| 1556 | ** in Tcl. For example, if you run Tcl code like this: |
| 1557 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1558 | ** sqlite3 db1 "my_database" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1559 | ** db1 close |
| 1560 | ** |
| 1561 | ** The first command opens a connection to the "my_database" database |
| 1562 | ** and calls that connection "db1". The second command causes this |
| 1563 | ** subroutine to be invoked. |
| 1564 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1565 | 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] | 1566 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1567 | int choice; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1568 | int rc = TCL_OK; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1569 | static const char *DB_strs[] = { |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1570 | "authorizer", "backup", "busy", |
| 1571 | "cache", "changes", "close", |
| 1572 | "collate", "collation_needed", "commit_hook", |
| 1573 | "complete", "copy", "enable_load_extension", |
| 1574 | "errorcode", "eval", "exists", |
| 1575 | "function", "incrblob", "interrupt", |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 1576 | "last_insert_rowid", "nullvalue", "onecolumn", |
| 1577 | "profile", "progress", "rekey", |
| 1578 | "restore", "rollback_hook", "status", |
| 1579 | "timeout", "total_changes", "trace", |
| 1580 | "transaction", "unlock_notify", "update_hook", |
| 1581 | "version", "wal_hook", 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1582 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 1583 | enum DB_enum { |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1584 | DB_AUTHORIZER, DB_BACKUP, DB_BUSY, |
| 1585 | DB_CACHE, DB_CHANGES, DB_CLOSE, |
| 1586 | DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, |
| 1587 | DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, |
| 1588 | DB_ERRORCODE, DB_EVAL, DB_EXISTS, |
| 1589 | DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 1590 | DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, |
| 1591 | DB_PROFILE, DB_PROGRESS, DB_REKEY, |
| 1592 | DB_RESTORE, DB_ROLLBACK_HOOK, DB_STATUS, |
| 1593 | DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, |
| 1594 | DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, |
| 1595 | DB_VERSION, DB_WAL_HOOK |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1596 | }; |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 1597 | /* 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] | 1598 | |
| 1599 | if( objc<2 ){ |
| 1600 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1601 | return TCL_ERROR; |
| 1602 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 1603 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1604 | return TCL_ERROR; |
| 1605 | } |
| 1606 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 1607 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1608 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1609 | /* $db authorizer ?CALLBACK? |
| 1610 | ** |
| 1611 | ** Invoke the given callback to authorize each SQL operation as it is |
| 1612 | ** compiled. 5 arguments are appended to the callback before it is |
| 1613 | ** invoked: |
| 1614 | ** |
| 1615 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 1616 | ** (2) First descriptive name (depends on authorization type) |
| 1617 | ** (3) Second descriptive name |
| 1618 | ** (4) Name of the database (ex: "main", "temp") |
| 1619 | ** (5) Name of trigger that is doing the access |
| 1620 | ** |
| 1621 | ** The callback should return on of the following strings: SQLITE_OK, |
| 1622 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 1623 | ** |
| 1624 | ** If this method is invoked with no arguments, the current authorization |
| 1625 | ** callback string is returned. |
| 1626 | */ |
| 1627 | case DB_AUTHORIZER: { |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 1628 | #ifdef SQLITE_OMIT_AUTHORIZATION |
| 1629 | Tcl_AppendResult(interp, "authorization not available in this build", 0); |
| 1630 | return TCL_ERROR; |
| 1631 | #else |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1632 | if( objc>3 ){ |
| 1633 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1634 | return TCL_ERROR; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1635 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1636 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1637 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 1638 | } |
| 1639 | }else{ |
| 1640 | char *zAuth; |
| 1641 | int len; |
| 1642 | if( pDb->zAuth ){ |
| 1643 | Tcl_Free(pDb->zAuth); |
| 1644 | } |
| 1645 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 1646 | if( zAuth && len>0 ){ |
| 1647 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1648 | memcpy(pDb->zAuth, zAuth, len+1); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1649 | }else{ |
| 1650 | pDb->zAuth = 0; |
| 1651 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1652 | if( pDb->zAuth ){ |
| 1653 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1654 | sqlite3_set_authorizer(pDb->db, auth_callback, pDb); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1655 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1656 | sqlite3_set_authorizer(pDb->db, 0, 0); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1657 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1658 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 1659 | #endif |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1660 | break; |
| 1661 | } |
| 1662 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1663 | /* $db backup ?DATABASE? FILENAME |
| 1664 | ** |
| 1665 | ** Open or create a database file named FILENAME. Transfer the |
| 1666 | ** content of local database DATABASE (default: "main") into the |
| 1667 | ** FILENAME database. |
| 1668 | */ |
| 1669 | case DB_BACKUP: { |
| 1670 | const char *zDestFile; |
| 1671 | const char *zSrcDb; |
| 1672 | sqlite3 *pDest; |
| 1673 | sqlite3_backup *pBackup; |
| 1674 | |
| 1675 | if( objc==3 ){ |
| 1676 | zSrcDb = "main"; |
| 1677 | zDestFile = Tcl_GetString(objv[2]); |
| 1678 | }else if( objc==4 ){ |
| 1679 | zSrcDb = Tcl_GetString(objv[2]); |
| 1680 | zDestFile = Tcl_GetString(objv[3]); |
| 1681 | }else{ |
| 1682 | Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); |
| 1683 | return TCL_ERROR; |
| 1684 | } |
| 1685 | rc = sqlite3_open(zDestFile, &pDest); |
| 1686 | if( rc!=SQLITE_OK ){ |
| 1687 | Tcl_AppendResult(interp, "cannot open target database: ", |
| 1688 | sqlite3_errmsg(pDest), (char*)0); |
| 1689 | sqlite3_close(pDest); |
| 1690 | return TCL_ERROR; |
| 1691 | } |
| 1692 | pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); |
| 1693 | if( pBackup==0 ){ |
| 1694 | Tcl_AppendResult(interp, "backup failed: ", |
| 1695 | sqlite3_errmsg(pDest), (char*)0); |
| 1696 | sqlite3_close(pDest); |
| 1697 | return TCL_ERROR; |
| 1698 | } |
| 1699 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 1700 | sqlite3_backup_finish(pBackup); |
| 1701 | if( rc==SQLITE_DONE ){ |
| 1702 | rc = TCL_OK; |
| 1703 | }else{ |
| 1704 | Tcl_AppendResult(interp, "backup failed: ", |
| 1705 | sqlite3_errmsg(pDest), (char*)0); |
| 1706 | rc = TCL_ERROR; |
| 1707 | } |
| 1708 | sqlite3_close(pDest); |
| 1709 | break; |
| 1710 | } |
| 1711 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1712 | /* $db busy ?CALLBACK? |
| 1713 | ** |
| 1714 | ** Invoke the given callback if an SQL statement attempts to open |
| 1715 | ** a locked database file. |
| 1716 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1717 | case DB_BUSY: { |
| 1718 | if( objc>3 ){ |
| 1719 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1720 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1721 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1722 | if( pDb->zBusy ){ |
| 1723 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 1724 | } |
| 1725 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1726 | char *zBusy; |
| 1727 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1728 | if( pDb->zBusy ){ |
| 1729 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1730 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1731 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 1732 | if( zBusy && len>0 ){ |
| 1733 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1734 | memcpy(pDb->zBusy, zBusy, len+1); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1735 | }else{ |
| 1736 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1737 | } |
| 1738 | if( pDb->zBusy ){ |
| 1739 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1740 | sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1741 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1742 | sqlite3_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1745 | break; |
| 1746 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1747 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1748 | /* $db cache flush |
| 1749 | ** $db cache size n |
| 1750 | ** |
| 1751 | ** Flush the prepared statement cache, or set the maximum number of |
| 1752 | ** cached statements. |
| 1753 | */ |
| 1754 | case DB_CACHE: { |
| 1755 | char *subCmd; |
| 1756 | int n; |
| 1757 | |
| 1758 | if( objc<=2 ){ |
| 1759 | Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); |
| 1760 | return TCL_ERROR; |
| 1761 | } |
| 1762 | subCmd = Tcl_GetStringFromObj( objv[2], 0 ); |
| 1763 | if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ |
| 1764 | if( objc!=3 ){ |
| 1765 | Tcl_WrongNumArgs(interp, 2, objv, "flush"); |
| 1766 | return TCL_ERROR; |
| 1767 | }else{ |
| 1768 | flushStmtCache( pDb ); |
| 1769 | } |
| 1770 | }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ |
| 1771 | if( objc!=4 ){ |
| 1772 | Tcl_WrongNumArgs(interp, 2, objv, "size n"); |
| 1773 | return TCL_ERROR; |
| 1774 | }else{ |
| 1775 | if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ |
| 1776 | Tcl_AppendResult( interp, "cannot convert \"", |
| 1777 | Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0); |
| 1778 | return TCL_ERROR; |
| 1779 | }else{ |
| 1780 | if( n<0 ){ |
| 1781 | flushStmtCache( pDb ); |
| 1782 | n = 0; |
| 1783 | }else if( n>MAX_PREPARED_STMTS ){ |
| 1784 | n = MAX_PREPARED_STMTS; |
| 1785 | } |
| 1786 | pDb->maxStmt = n; |
| 1787 | } |
| 1788 | } |
| 1789 | }else{ |
| 1790 | Tcl_AppendResult( interp, "bad option \"", |
danielk1977 | 191fadc | 2007-10-23 08:17:48 +0000 | [diff] [blame] | 1791 | Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1792 | return TCL_ERROR; |
| 1793 | } |
| 1794 | break; |
| 1795 | } |
| 1796 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1797 | /* $db changes |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1798 | ** |
| 1799 | ** Return the number of rows that were modified, inserted, or deleted by |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1800 | ** the most recent INSERT, UPDATE or DELETE statement, not including |
| 1801 | ** any changes made by trigger programs. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1802 | */ |
| 1803 | case DB_CHANGES: { |
| 1804 | Tcl_Obj *pResult; |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1805 | if( objc!=2 ){ |
| 1806 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 1807 | return TCL_ERROR; |
| 1808 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1809 | pResult = Tcl_GetObjResult(interp); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1810 | Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 1811 | break; |
| 1812 | } |
| 1813 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1814 | /* $db close |
| 1815 | ** |
| 1816 | ** Shutdown the database |
| 1817 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1818 | case DB_CLOSE: { |
| 1819 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 1820 | break; |
| 1821 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1822 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1823 | /* |
| 1824 | ** $db collate NAME SCRIPT |
| 1825 | ** |
| 1826 | ** Create a new SQL collation function called NAME. Whenever |
| 1827 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 1828 | */ |
| 1829 | case DB_COLLATE: { |
| 1830 | SqlCollate *pCollate; |
| 1831 | char *zName; |
| 1832 | char *zScript; |
| 1833 | int nScript; |
| 1834 | if( objc!=4 ){ |
| 1835 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 1836 | return TCL_ERROR; |
| 1837 | } |
| 1838 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 1839 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 1840 | pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); |
| 1841 | if( pCollate==0 ) return TCL_ERROR; |
| 1842 | pCollate->interp = interp; |
| 1843 | pCollate->pNext = pDb->pCollate; |
| 1844 | pCollate->zScript = (char*)&pCollate[1]; |
| 1845 | pDb->pCollate = pCollate; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1846 | memcpy(pCollate->zScript, zScript, nScript+1); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1847 | if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, |
| 1848 | pCollate, tclSqlCollate) ){ |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1849 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1850 | return TCL_ERROR; |
| 1851 | } |
| 1852 | break; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | ** $db collation_needed SCRIPT |
| 1857 | ** |
| 1858 | ** Create a new SQL collation function called NAME. Whenever |
| 1859 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 1860 | */ |
| 1861 | case DB_COLLATION_NEEDED: { |
| 1862 | if( objc!=3 ){ |
| 1863 | Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); |
| 1864 | return TCL_ERROR; |
| 1865 | } |
| 1866 | if( pDb->pCollateNeeded ){ |
| 1867 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 1868 | } |
| 1869 | pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); |
| 1870 | Tcl_IncrRefCount(pDb->pCollateNeeded); |
| 1871 | sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); |
| 1872 | break; |
| 1873 | } |
| 1874 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1875 | /* $db commit_hook ?CALLBACK? |
| 1876 | ** |
| 1877 | ** Invoke the given callback just before committing every SQL transaction. |
| 1878 | ** If the callback throws an exception or returns non-zero, then the |
| 1879 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 1880 | ** is disabled. |
| 1881 | */ |
| 1882 | case DB_COMMIT_HOOK: { |
| 1883 | if( objc>3 ){ |
| 1884 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 1885 | return TCL_ERROR; |
| 1886 | }else if( objc==2 ){ |
| 1887 | if( pDb->zCommit ){ |
| 1888 | Tcl_AppendResult(interp, pDb->zCommit, 0); |
| 1889 | } |
| 1890 | }else{ |
| 1891 | char *zCommit; |
| 1892 | int len; |
| 1893 | if( pDb->zCommit ){ |
| 1894 | Tcl_Free(pDb->zCommit); |
| 1895 | } |
| 1896 | zCommit = Tcl_GetStringFromObj(objv[2], &len); |
| 1897 | if( zCommit && len>0 ){ |
| 1898 | pDb->zCommit = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1899 | memcpy(pDb->zCommit, zCommit, len+1); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1900 | }else{ |
| 1901 | pDb->zCommit = 0; |
| 1902 | } |
| 1903 | if( pDb->zCommit ){ |
| 1904 | pDb->interp = interp; |
| 1905 | sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); |
| 1906 | }else{ |
| 1907 | sqlite3_commit_hook(pDb->db, 0, 0); |
| 1908 | } |
| 1909 | } |
| 1910 | break; |
| 1911 | } |
| 1912 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1913 | /* $db complete SQL |
| 1914 | ** |
| 1915 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 1916 | ** additional lines of input are needed. This is similar to the |
| 1917 | ** built-in "info complete" command of Tcl. |
| 1918 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1919 | case DB_COMPLETE: { |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 1920 | #ifndef SQLITE_OMIT_COMPLETE |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1921 | Tcl_Obj *pResult; |
| 1922 | int isComplete; |
| 1923 | if( objc!=3 ){ |
| 1924 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1925 | return TCL_ERROR; |
| 1926 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1927 | isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1928 | pResult = Tcl_GetObjResult(interp); |
| 1929 | Tcl_SetBooleanObj(pResult, isComplete); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 1930 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1931 | break; |
| 1932 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1933 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1934 | /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? |
| 1935 | ** |
| 1936 | ** Copy data into table from filename, optionally using SEPARATOR |
| 1937 | ** as column separators. If a column contains a null string, or the |
| 1938 | ** value of NULLINDICATOR, a NULL is inserted for the column. |
| 1939 | ** conflict-algorithm is one of the sqlite conflict algorithms: |
| 1940 | ** rollback, abort, fail, ignore, replace |
| 1941 | ** On success, return the number of lines processed, not necessarily same |
| 1942 | ** as 'db changes' due to conflict-algorithm selected. |
| 1943 | ** |
| 1944 | ** This code is basically an implementation/enhancement of |
| 1945 | ** the sqlite3 shell.c ".import" command. |
| 1946 | ** |
| 1947 | ** This command usage is equivalent to the sqlite2.x COPY statement, |
| 1948 | ** which imports file data into a table using the PostgreSQL COPY file format: |
| 1949 | ** $db copy $conflit_algo $table_name $filename \t \\N |
| 1950 | */ |
| 1951 | case DB_COPY: { |
| 1952 | char *zTable; /* Insert data into this table */ |
| 1953 | char *zFile; /* The file from which to extract data */ |
| 1954 | char *zConflict; /* The conflict algorithm to use */ |
| 1955 | sqlite3_stmt *pStmt; /* A statement */ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1956 | int nCol; /* Number of columns in the table */ |
| 1957 | int nByte; /* Number of bytes in an SQL string */ |
| 1958 | int i, j; /* Loop counters */ |
| 1959 | int nSep; /* Number of bytes in zSep[] */ |
| 1960 | int nNull; /* Number of bytes in zNull[] */ |
| 1961 | char *zSql; /* An SQL statement */ |
| 1962 | char *zLine; /* A single line of input from the file */ |
| 1963 | char **azCol; /* zLine[] broken up into columns */ |
| 1964 | char *zCommit; /* How to commit changes */ |
| 1965 | FILE *in; /* The input file */ |
| 1966 | int lineno = 0; /* Line number of input file */ |
| 1967 | char zLineNum[80]; /* Line number print buffer */ |
| 1968 | Tcl_Obj *pResult; /* interp result */ |
| 1969 | |
| 1970 | char *zSep; |
| 1971 | char *zNull; |
| 1972 | if( objc<5 || objc>7 ){ |
| 1973 | Tcl_WrongNumArgs(interp, 2, objv, |
| 1974 | "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); |
| 1975 | return TCL_ERROR; |
| 1976 | } |
| 1977 | if( objc>=6 ){ |
| 1978 | zSep = Tcl_GetStringFromObj(objv[5], 0); |
| 1979 | }else{ |
| 1980 | zSep = "\t"; |
| 1981 | } |
| 1982 | if( objc>=7 ){ |
| 1983 | zNull = Tcl_GetStringFromObj(objv[6], 0); |
| 1984 | }else{ |
| 1985 | zNull = ""; |
| 1986 | } |
| 1987 | zConflict = Tcl_GetStringFromObj(objv[2], 0); |
| 1988 | zTable = Tcl_GetStringFromObj(objv[3], 0); |
| 1989 | zFile = Tcl_GetStringFromObj(objv[4], 0); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1990 | nSep = strlen30(zSep); |
| 1991 | nNull = strlen30(zNull); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1992 | if( nSep==0 ){ |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 1993 | Tcl_AppendResult(interp,"Error: non-null separator required for copy",0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1994 | return TCL_ERROR; |
| 1995 | } |
drh | 3e59c01 | 2008-09-23 10:12:13 +0000 | [diff] [blame] | 1996 | if(strcmp(zConflict, "rollback") != 0 && |
| 1997 | strcmp(zConflict, "abort" ) != 0 && |
| 1998 | strcmp(zConflict, "fail" ) != 0 && |
| 1999 | strcmp(zConflict, "ignore" ) != 0 && |
| 2000 | strcmp(zConflict, "replace" ) != 0 ) { |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2001 | Tcl_AppendResult(interp, "Error: \"", zConflict, |
| 2002 | "\", conflict-algorithm must be one of: rollback, " |
| 2003 | "abort, fail, ignore, or replace", 0); |
| 2004 | return TCL_ERROR; |
| 2005 | } |
| 2006 | zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); |
| 2007 | if( zSql==0 ){ |
| 2008 | Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0); |
| 2009 | return TCL_ERROR; |
| 2010 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2011 | nByte = strlen30(zSql); |
drh | 3e701a1 | 2007-02-01 01:53:44 +0000 | [diff] [blame] | 2012 | rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2013 | sqlite3_free(zSql); |
| 2014 | if( rc ){ |
| 2015 | Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
| 2016 | nCol = 0; |
| 2017 | }else{ |
| 2018 | nCol = sqlite3_column_count(pStmt); |
| 2019 | } |
| 2020 | sqlite3_finalize(pStmt); |
| 2021 | if( nCol==0 ) { |
| 2022 | return TCL_ERROR; |
| 2023 | } |
| 2024 | zSql = malloc( nByte + 50 + nCol*2 ); |
| 2025 | if( zSql==0 ) { |
| 2026 | Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
| 2027 | return TCL_ERROR; |
| 2028 | } |
| 2029 | sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", |
| 2030 | zConflict, zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2031 | j = strlen30(zSql); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2032 | for(i=1; i<nCol; i++){ |
| 2033 | zSql[j++] = ','; |
| 2034 | zSql[j++] = '?'; |
| 2035 | } |
| 2036 | zSql[j++] = ')'; |
| 2037 | zSql[j] = 0; |
drh | 3e701a1 | 2007-02-01 01:53:44 +0000 | [diff] [blame] | 2038 | rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2039 | free(zSql); |
| 2040 | if( rc ){ |
| 2041 | Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
| 2042 | sqlite3_finalize(pStmt); |
| 2043 | return TCL_ERROR; |
| 2044 | } |
| 2045 | in = fopen(zFile, "rb"); |
| 2046 | if( in==0 ){ |
| 2047 | Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); |
| 2048 | sqlite3_finalize(pStmt); |
| 2049 | return TCL_ERROR; |
| 2050 | } |
| 2051 | azCol = malloc( sizeof(azCol[0])*(nCol+1) ); |
| 2052 | if( azCol==0 ) { |
| 2053 | Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 2054 | fclose(in); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2055 | return TCL_ERROR; |
| 2056 | } |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 2057 | (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2058 | zCommit = "COMMIT"; |
| 2059 | while( (zLine = local_getline(0, in))!=0 ){ |
| 2060 | char *z; |
| 2061 | i = 0; |
| 2062 | lineno++; |
| 2063 | azCol[0] = zLine; |
| 2064 | for(i=0, z=zLine; *z; z++){ |
| 2065 | if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ |
| 2066 | *z = 0; |
| 2067 | i++; |
| 2068 | if( i<nCol ){ |
| 2069 | azCol[i] = &z[nSep]; |
| 2070 | z += nSep-1; |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | if( i+1!=nCol ){ |
| 2075 | char *zErr; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2076 | int nErr = strlen30(zFile) + 200; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2077 | zErr = malloc(nErr); |
drh | c1f4494 | 2006-05-10 14:39:13 +0000 | [diff] [blame] | 2078 | if( zErr ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2079 | sqlite3_snprintf(nErr, zErr, |
drh | c1f4494 | 2006-05-10 14:39:13 +0000 | [diff] [blame] | 2080 | "Error: %s line %d: expected %d columns of data but found %d", |
| 2081 | zFile, lineno, nCol, i+1); |
| 2082 | Tcl_AppendResult(interp, zErr, 0); |
| 2083 | free(zErr); |
| 2084 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2085 | zCommit = "ROLLBACK"; |
| 2086 | break; |
| 2087 | } |
| 2088 | for(i=0; i<nCol; i++){ |
| 2089 | /* check for null data, if so, bind as null */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2090 | if( (nNull>0 && strcmp(azCol[i], zNull)==0) |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2091 | || strlen30(azCol[i])==0 |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2092 | ){ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2093 | sqlite3_bind_null(pStmt, i+1); |
| 2094 | }else{ |
| 2095 | sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
| 2096 | } |
| 2097 | } |
| 2098 | sqlite3_step(pStmt); |
| 2099 | rc = sqlite3_reset(pStmt); |
| 2100 | free(zLine); |
| 2101 | if( rc!=SQLITE_OK ){ |
| 2102 | Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0); |
| 2103 | zCommit = "ROLLBACK"; |
| 2104 | break; |
| 2105 | } |
| 2106 | } |
| 2107 | free(azCol); |
| 2108 | fclose(in); |
| 2109 | sqlite3_finalize(pStmt); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 2110 | (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2111 | |
| 2112 | if( zCommit[0] == 'C' ){ |
| 2113 | /* success, set result as number of lines processed */ |
| 2114 | pResult = Tcl_GetObjResult(interp); |
| 2115 | Tcl_SetIntObj(pResult, lineno); |
| 2116 | rc = TCL_OK; |
| 2117 | }else{ |
| 2118 | /* failure, append lineno where failed */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2119 | sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2120 | Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0); |
| 2121 | rc = TCL_ERROR; |
| 2122 | } |
| 2123 | break; |
| 2124 | } |
| 2125 | |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 2126 | /* |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 2127 | ** $db enable_load_extension BOOLEAN |
| 2128 | ** |
| 2129 | ** Turn the extension loading feature on or off. It if off by |
| 2130 | ** default. |
| 2131 | */ |
| 2132 | case DB_ENABLE_LOAD_EXTENSION: { |
drh | f533acc | 2006-12-19 18:57:11 +0000 | [diff] [blame] | 2133 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 2134 | int onoff; |
| 2135 | if( objc!=3 ){ |
| 2136 | Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); |
| 2137 | return TCL_ERROR; |
| 2138 | } |
| 2139 | if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
| 2140 | return TCL_ERROR; |
| 2141 | } |
| 2142 | sqlite3_enable_load_extension(pDb->db, onoff); |
| 2143 | break; |
drh | f533acc | 2006-12-19 18:57:11 +0000 | [diff] [blame] | 2144 | #else |
| 2145 | Tcl_AppendResult(interp, "extension loading is turned off at compile-time", |
| 2146 | 0); |
| 2147 | return TCL_ERROR; |
| 2148 | #endif |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /* |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 2152 | ** $db errorcode |
| 2153 | ** |
| 2154 | ** Return the numeric error code that was returned by the most recent |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2155 | ** call to sqlite3_exec(). |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 2156 | */ |
| 2157 | case DB_ERRORCODE: { |
danielk1977 | f3ce83f | 2004-06-14 11:43:46 +0000 | [diff] [blame] | 2158 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 2159 | break; |
| 2160 | } |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2161 | |
| 2162 | /* |
| 2163 | ** $db exists $sql |
| 2164 | ** $db onecolumn $sql |
| 2165 | ** |
| 2166 | ** The onecolumn method is the equivalent of: |
| 2167 | ** lindex [$db eval $sql] 0 |
| 2168 | */ |
| 2169 | case DB_EXISTS: |
| 2170 | case DB_ONECOLUMN: { |
| 2171 | DbEvalContext sEval; |
| 2172 | if( objc!=3 ){ |
| 2173 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
| 2174 | return TCL_ERROR; |
| 2175 | } |
| 2176 | |
| 2177 | dbEvalInit(&sEval, pDb, objv[2], 0); |
| 2178 | rc = dbEvalStep(&sEval); |
| 2179 | if( choice==DB_ONECOLUMN ){ |
| 2180 | if( rc==TCL_OK ){ |
| 2181 | Tcl_SetObjResult(interp, dbEvalColumnValue(&sEval, 0)); |
| 2182 | } |
| 2183 | }else if( rc==TCL_BREAK || rc==TCL_OK ){ |
| 2184 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc==TCL_OK)); |
| 2185 | } |
| 2186 | dbEvalFinalize(&sEval); |
| 2187 | |
| 2188 | if( rc==TCL_BREAK ){ |
| 2189 | rc = TCL_OK; |
| 2190 | } |
| 2191 | break; |
| 2192 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2193 | |
| 2194 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 2195 | ** $db eval $sql ?array? ?{ ...code... }? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2196 | ** |
| 2197 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2198 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2199 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 2200 | ** If "array" is an empty string, then the values are placed in variables |
| 2201 | ** that have the same name as the fields extracted by the query. |
| 2202 | */ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2203 | case DB_EVAL: { |
| 2204 | if( objc<3 || objc>5 ){ |
| 2205 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
| 2206 | return TCL_ERROR; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 2207 | } |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2208 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 2209 | if( objc==3 ){ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2210 | DbEvalContext sEval; |
| 2211 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 2212 | Tcl_IncrRefCount(pRet); |
| 2213 | dbEvalInit(&sEval, pDb, objv[2], 0); |
| 2214 | while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ |
| 2215 | int i; |
| 2216 | int nCol; |
| 2217 | dbEvalRowInfo(&sEval, &nCol, 0); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 2218 | for(i=0; i<nCol; i++){ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2219 | Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 2220 | } |
| 2221 | } |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2222 | dbEvalFinalize(&sEval); |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 2223 | if( rc==TCL_BREAK ){ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2224 | Tcl_SetObjResult(interp, pRet); |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 2225 | rc = TCL_OK; |
| 2226 | } |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2227 | Tcl_DecrRefCount(pRet); |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2228 | }else{ |
| 2229 | ClientData cd[2]; |
| 2230 | DbEvalContext *p; |
| 2231 | Tcl_Obj *pArray = 0; |
| 2232 | Tcl_Obj *pScript; |
| 2233 | |
| 2234 | if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ |
| 2235 | pArray = objv[3]; |
| 2236 | } |
| 2237 | pScript = objv[objc-1]; |
| 2238 | Tcl_IncrRefCount(pScript); |
| 2239 | |
| 2240 | p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); |
| 2241 | dbEvalInit(p, pDb, objv[2], pArray); |
| 2242 | |
| 2243 | cd[0] = (void *)p; |
| 2244 | cd[1] = (void *)pScript; |
| 2245 | rc = DbEvalNextCmd(cd, interp, TCL_OK); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 2246 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 2247 | break; |
| 2248 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 2251 | ** $db function NAME [-argcount N] SCRIPT |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2252 | ** |
| 2253 | ** Create a new SQL function called NAME. Whenever that function is |
| 2254 | ** called, invoke SCRIPT to evaluate the function. |
| 2255 | */ |
| 2256 | case DB_FUNCTION: { |
| 2257 | SqlFunc *pFunc; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 2258 | Tcl_Obj *pScript; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2259 | char *zName; |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 2260 | int nArg = -1; |
| 2261 | if( objc==6 ){ |
| 2262 | const char *z = Tcl_GetString(objv[3]); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2263 | int n = strlen30(z); |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 2264 | if( n>2 && strncmp(z, "-argcount",n)==0 ){ |
| 2265 | if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR; |
| 2266 | if( nArg<0 ){ |
| 2267 | Tcl_AppendResult(interp, "number of arguments must be non-negative", |
| 2268 | (char*)0); |
| 2269 | return TCL_ERROR; |
| 2270 | } |
| 2271 | } |
| 2272 | pScript = objv[5]; |
| 2273 | }else if( objc!=4 ){ |
| 2274 | Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT"); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2275 | return TCL_ERROR; |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 2276 | }else{ |
| 2277 | pScript = objv[3]; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2278 | } |
| 2279 | zName = Tcl_GetStringFromObj(objv[2], 0); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 2280 | pFunc = findSqlFunc(pDb, zName); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2281 | if( pFunc==0 ) return TCL_ERROR; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 2282 | if( pFunc->pScript ){ |
| 2283 | Tcl_DecrRefCount(pFunc->pScript); |
| 2284 | } |
| 2285 | pFunc->pScript = pScript; |
| 2286 | Tcl_IncrRefCount(pScript); |
| 2287 | pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 2288 | rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2289 | pFunc, tclSqlFunc, 0, 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2290 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 2291 | rc = TCL_ERROR; |
| 2292 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2293 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2294 | break; |
| 2295 | } |
| 2296 | |
| 2297 | /* |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 2298 | ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 2299 | */ |
| 2300 | case DB_INCRBLOB: { |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 2301 | #ifdef SQLITE_OMIT_INCRBLOB |
| 2302 | Tcl_AppendResult(interp, "incrblob not available in this build", 0); |
| 2303 | return TCL_ERROR; |
| 2304 | #else |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 2305 | int isReadonly = 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 2306 | const char *zDb = "main"; |
| 2307 | const char *zTable; |
| 2308 | const char *zColumn; |
| 2309 | sqlite_int64 iRow; |
| 2310 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 2311 | /* Check for the -readonly option */ |
| 2312 | if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ |
| 2313 | isReadonly = 1; |
| 2314 | } |
| 2315 | |
| 2316 | if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ |
| 2317 | Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 2318 | return TCL_ERROR; |
| 2319 | } |
| 2320 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 2321 | if( objc==(6+isReadonly) ){ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 2322 | zDb = Tcl_GetString(objv[2]); |
| 2323 | } |
| 2324 | zTable = Tcl_GetString(objv[objc-3]); |
| 2325 | zColumn = Tcl_GetString(objv[objc-2]); |
| 2326 | rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); |
| 2327 | |
| 2328 | if( rc==TCL_OK ){ |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 2329 | rc = createIncrblobChannel( |
| 2330 | interp, pDb, zDb, zTable, zColumn, iRow, isReadonly |
| 2331 | ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 2332 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 2333 | #endif |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | } |
| 2336 | |
| 2337 | /* |
drh | f11bded | 2006-07-17 00:02:44 +0000 | [diff] [blame] | 2338 | ** $db interrupt |
| 2339 | ** |
| 2340 | ** Interrupt the execution of the inner-most SQL interpreter. This |
| 2341 | ** causes the SQL statement to return an error of SQLITE_INTERRUPT. |
| 2342 | */ |
| 2343 | case DB_INTERRUPT: { |
| 2344 | sqlite3_interrupt(pDb->db); |
| 2345 | break; |
| 2346 | } |
| 2347 | |
| 2348 | /* |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2349 | ** $db nullvalue ?STRING? |
| 2350 | ** |
| 2351 | ** Change text used when a NULL comes back from the database. If ?STRING? |
| 2352 | ** is not present, then the current string used for NULL is returned. |
| 2353 | ** If STRING is present, then STRING is returned. |
| 2354 | ** |
| 2355 | */ |
| 2356 | case DB_NULLVALUE: { |
| 2357 | if( objc!=2 && objc!=3 ){ |
| 2358 | Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); |
| 2359 | return TCL_ERROR; |
| 2360 | } |
| 2361 | if( objc==3 ){ |
| 2362 | int len; |
| 2363 | char *zNull = Tcl_GetStringFromObj(objv[2], &len); |
| 2364 | if( pDb->zNull ){ |
| 2365 | Tcl_Free(pDb->zNull); |
| 2366 | } |
| 2367 | if( zNull && len>0 ){ |
| 2368 | pDb->zNull = Tcl_Alloc( len + 1 ); |
| 2369 | strncpy(pDb->zNull, zNull, len); |
| 2370 | pDb->zNull[len] = '\0'; |
| 2371 | }else{ |
| 2372 | pDb->zNull = 0; |
| 2373 | } |
| 2374 | } |
| 2375 | Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull)); |
| 2376 | break; |
| 2377 | } |
| 2378 | |
| 2379 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2380 | ** $db last_insert_rowid |
| 2381 | ** |
| 2382 | ** Return an integer which is the ROWID for the most recent insert. |
| 2383 | */ |
| 2384 | case DB_LAST_INSERT_ROWID: { |
| 2385 | Tcl_Obj *pResult; |
drh | f7e678d | 2006-06-21 19:30:34 +0000 | [diff] [blame] | 2386 | Tcl_WideInt rowid; |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2387 | if( objc!=2 ){ |
| 2388 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 2389 | return TCL_ERROR; |
| 2390 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2391 | rowid = sqlite3_last_insert_rowid(pDb->db); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2392 | pResult = Tcl_GetObjResult(interp); |
drh | f7e678d | 2006-06-21 19:30:34 +0000 | [diff] [blame] | 2393 | Tcl_SetWideIntObj(pResult, rowid); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2394 | break; |
| 2395 | } |
| 2396 | |
| 2397 | /* |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2398 | ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2399 | */ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2400 | |
| 2401 | /* $db progress ?N CALLBACK? |
| 2402 | ** |
| 2403 | ** Invoke the given callback every N virtual machine opcodes while executing |
| 2404 | ** queries. |
| 2405 | */ |
| 2406 | case DB_PROGRESS: { |
| 2407 | if( objc==2 ){ |
| 2408 | if( pDb->zProgress ){ |
| 2409 | Tcl_AppendResult(interp, pDb->zProgress, 0); |
| 2410 | } |
| 2411 | }else if( objc==4 ){ |
| 2412 | char *zProgress; |
| 2413 | int len; |
| 2414 | int N; |
| 2415 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2416 | return TCL_ERROR; |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2417 | }; |
| 2418 | if( pDb->zProgress ){ |
| 2419 | Tcl_Free(pDb->zProgress); |
| 2420 | } |
| 2421 | zProgress = Tcl_GetStringFromObj(objv[3], &len); |
| 2422 | if( zProgress && len>0 ){ |
| 2423 | pDb->zProgress = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2424 | memcpy(pDb->zProgress, zProgress, len+1); |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2425 | }else{ |
| 2426 | pDb->zProgress = 0; |
| 2427 | } |
| 2428 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 2429 | if( pDb->zProgress ){ |
| 2430 | pDb->interp = interp; |
| 2431 | sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); |
| 2432 | }else{ |
| 2433 | sqlite3_progress_handler(pDb->db, 0, 0, 0); |
| 2434 | } |
| 2435 | #endif |
| 2436 | }else{ |
| 2437 | Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2438 | return TCL_ERROR; |
| 2439 | } |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2440 | break; |
| 2441 | } |
| 2442 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2443 | /* $db profile ?CALLBACK? |
| 2444 | ** |
| 2445 | ** Make arrangements to invoke the CALLBACK routine after each SQL statement |
| 2446 | ** that has run. The text of the SQL and the amount of elapse time are |
| 2447 | ** appended to CALLBACK before the script is run. |
| 2448 | */ |
| 2449 | case DB_PROFILE: { |
| 2450 | if( objc>3 ){ |
| 2451 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 2452 | return TCL_ERROR; |
| 2453 | }else if( objc==2 ){ |
| 2454 | if( pDb->zProfile ){ |
| 2455 | Tcl_AppendResult(interp, pDb->zProfile, 0); |
| 2456 | } |
| 2457 | }else{ |
| 2458 | char *zProfile; |
| 2459 | int len; |
| 2460 | if( pDb->zProfile ){ |
| 2461 | Tcl_Free(pDb->zProfile); |
| 2462 | } |
| 2463 | zProfile = Tcl_GetStringFromObj(objv[2], &len); |
| 2464 | if( zProfile && len>0 ){ |
| 2465 | pDb->zProfile = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2466 | memcpy(pDb->zProfile, zProfile, len+1); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2467 | }else{ |
| 2468 | pDb->zProfile = 0; |
| 2469 | } |
| 2470 | #ifndef SQLITE_OMIT_TRACE |
| 2471 | if( pDb->zProfile ){ |
| 2472 | pDb->interp = interp; |
| 2473 | sqlite3_profile(pDb->db, DbProfileHandler, pDb); |
| 2474 | }else{ |
| 2475 | sqlite3_profile(pDb->db, 0, 0); |
| 2476 | } |
| 2477 | #endif |
| 2478 | } |
| 2479 | break; |
| 2480 | } |
| 2481 | |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2482 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2483 | ** $db rekey KEY |
| 2484 | ** |
| 2485 | ** Change the encryption key on the currently open database. |
| 2486 | */ |
| 2487 | case DB_REKEY: { |
| 2488 | int nKey; |
| 2489 | void *pKey; |
| 2490 | if( objc!=3 ){ |
| 2491 | Tcl_WrongNumArgs(interp, 2, objv, "KEY"); |
| 2492 | return TCL_ERROR; |
| 2493 | } |
| 2494 | pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2495 | #ifdef SQLITE_HAS_CODEC |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2496 | rc = sqlite3_rekey(pDb->db, pKey, nKey); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2497 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2498 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2499 | rc = TCL_ERROR; |
| 2500 | } |
| 2501 | #endif |
| 2502 | break; |
| 2503 | } |
| 2504 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 2505 | /* $db restore ?DATABASE? FILENAME |
| 2506 | ** |
| 2507 | ** Open a database file named FILENAME. Transfer the content |
| 2508 | ** of FILENAME into the local database DATABASE (default: "main"). |
| 2509 | */ |
| 2510 | case DB_RESTORE: { |
| 2511 | const char *zSrcFile; |
| 2512 | const char *zDestDb; |
| 2513 | sqlite3 *pSrc; |
| 2514 | sqlite3_backup *pBackup; |
| 2515 | int nTimeout = 0; |
| 2516 | |
| 2517 | if( objc==3 ){ |
| 2518 | zDestDb = "main"; |
| 2519 | zSrcFile = Tcl_GetString(objv[2]); |
| 2520 | }else if( objc==4 ){ |
| 2521 | zDestDb = Tcl_GetString(objv[2]); |
| 2522 | zSrcFile = Tcl_GetString(objv[3]); |
| 2523 | }else{ |
| 2524 | Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); |
| 2525 | return TCL_ERROR; |
| 2526 | } |
| 2527 | rc = sqlite3_open_v2(zSrcFile, &pSrc, SQLITE_OPEN_READONLY, 0); |
| 2528 | if( rc!=SQLITE_OK ){ |
| 2529 | Tcl_AppendResult(interp, "cannot open source database: ", |
| 2530 | sqlite3_errmsg(pSrc), (char*)0); |
| 2531 | sqlite3_close(pSrc); |
| 2532 | return TCL_ERROR; |
| 2533 | } |
| 2534 | pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); |
| 2535 | if( pBackup==0 ){ |
| 2536 | Tcl_AppendResult(interp, "restore failed: ", |
| 2537 | sqlite3_errmsg(pDb->db), (char*)0); |
| 2538 | sqlite3_close(pSrc); |
| 2539 | return TCL_ERROR; |
| 2540 | } |
| 2541 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 2542 | || rc==SQLITE_BUSY ){ |
| 2543 | if( rc==SQLITE_BUSY ){ |
| 2544 | if( nTimeout++ >= 3 ) break; |
| 2545 | sqlite3_sleep(100); |
| 2546 | } |
| 2547 | } |
| 2548 | sqlite3_backup_finish(pBackup); |
| 2549 | if( rc==SQLITE_DONE ){ |
| 2550 | rc = TCL_OK; |
| 2551 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 2552 | Tcl_AppendResult(interp, "restore failed: source database busy", |
| 2553 | (char*)0); |
| 2554 | rc = TCL_ERROR; |
| 2555 | }else{ |
| 2556 | Tcl_AppendResult(interp, "restore failed: ", |
| 2557 | sqlite3_errmsg(pDb->db), (char*)0); |
| 2558 | rc = TCL_ERROR; |
| 2559 | } |
| 2560 | sqlite3_close(pSrc); |
| 2561 | break; |
| 2562 | } |
| 2563 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2564 | /* |
drh | 3c379b0 | 2010-04-07 19:31:59 +0000 | [diff] [blame] | 2565 | ** $db status (step|sort|autoindex) |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2566 | ** |
| 2567 | ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or |
| 2568 | ** SQLITE_STMTSTATUS_SORT for the most recent eval. |
| 2569 | */ |
| 2570 | case DB_STATUS: { |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2571 | int v; |
| 2572 | const char *zOp; |
| 2573 | if( objc!=3 ){ |
| 2574 | Tcl_WrongNumArgs(interp, 2, objv, "(step|sort)"); |
| 2575 | return TCL_ERROR; |
| 2576 | } |
| 2577 | zOp = Tcl_GetString(objv[2]); |
| 2578 | if( strcmp(zOp, "step")==0 ){ |
| 2579 | v = pDb->nStep; |
| 2580 | }else if( strcmp(zOp, "sort")==0 ){ |
| 2581 | v = pDb->nSort; |
drh | 3c379b0 | 2010-04-07 19:31:59 +0000 | [diff] [blame] | 2582 | }else if( strcmp(zOp, "autoindex")==0 ){ |
| 2583 | v = pDb->nIndex; |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2584 | }else{ |
drh | 3c379b0 | 2010-04-07 19:31:59 +0000 | [diff] [blame] | 2585 | Tcl_AppendResult(interp, |
| 2586 | "bad argument: should be autoindex, step, or sort", |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2587 | (char*)0); |
| 2588 | return TCL_ERROR; |
| 2589 | } |
| 2590 | Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); |
| 2591 | break; |
| 2592 | } |
| 2593 | |
| 2594 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2595 | ** $db timeout MILLESECONDS |
| 2596 | ** |
| 2597 | ** Delay for the number of milliseconds specified when a file is locked. |
| 2598 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2599 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2600 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2601 | if( objc!=3 ){ |
| 2602 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2603 | return TCL_ERROR; |
| 2604 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2605 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2606 | sqlite3_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2607 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2608 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 2609 | |
| 2610 | /* |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 2611 | ** $db total_changes |
| 2612 | ** |
| 2613 | ** Return the number of rows that were modified, inserted, or deleted |
| 2614 | ** since the database handle was created. |
| 2615 | */ |
| 2616 | case DB_TOTAL_CHANGES: { |
| 2617 | Tcl_Obj *pResult; |
| 2618 | if( objc!=2 ){ |
| 2619 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 2620 | return TCL_ERROR; |
| 2621 | } |
| 2622 | pResult = Tcl_GetObjResult(interp); |
| 2623 | Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); |
| 2624 | break; |
| 2625 | } |
| 2626 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2627 | /* $db trace ?CALLBACK? |
| 2628 | ** |
| 2629 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 2630 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 2631 | ** it is executed. |
| 2632 | */ |
| 2633 | case DB_TRACE: { |
| 2634 | if( objc>3 ){ |
| 2635 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 2636 | return TCL_ERROR; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2637 | }else if( objc==2 ){ |
| 2638 | if( pDb->zTrace ){ |
| 2639 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 2640 | } |
| 2641 | }else{ |
| 2642 | char *zTrace; |
| 2643 | int len; |
| 2644 | if( pDb->zTrace ){ |
| 2645 | Tcl_Free(pDb->zTrace); |
| 2646 | } |
| 2647 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 2648 | if( zTrace && len>0 ){ |
| 2649 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2650 | memcpy(pDb->zTrace, zTrace, len+1); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2651 | }else{ |
| 2652 | pDb->zTrace = 0; |
| 2653 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2654 | #ifndef SQLITE_OMIT_TRACE |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2655 | if( pDb->zTrace ){ |
| 2656 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2657 | sqlite3_trace(pDb->db, DbTraceHandler, pDb); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2658 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2659 | sqlite3_trace(pDb->db, 0, 0); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2660 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2661 | #endif |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2662 | } |
| 2663 | break; |
| 2664 | } |
| 2665 | |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2666 | /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT |
| 2667 | ** |
| 2668 | ** Start a new transaction (if we are not already in the midst of a |
| 2669 | ** transaction) and execute the TCL script SCRIPT. After SCRIPT |
| 2670 | ** completes, either commit the transaction or roll it back if SCRIPT |
| 2671 | ** throws an exception. Or if no new transation was started, do nothing. |
| 2672 | ** pass the exception on up the stack. |
| 2673 | ** |
| 2674 | ** This command was inspired by Dave Thomas's talk on Ruby at the |
| 2675 | ** 2005 O'Reilly Open Source Convention (OSCON). |
| 2676 | */ |
| 2677 | case DB_TRANSACTION: { |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2678 | Tcl_Obj *pScript; |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2679 | const char *zBegin = "SAVEPOINT _tcl_transaction"; |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2680 | if( objc!=3 && objc!=4 ){ |
| 2681 | Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); |
| 2682 | return TCL_ERROR; |
| 2683 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2684 | |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2685 | if( pDb->nTransaction==0 && objc==4 ){ |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2686 | static const char *TTYPE_strs[] = { |
drh | ce60401 | 2005-08-16 11:11:34 +0000 | [diff] [blame] | 2687 | "deferred", "exclusive", "immediate", 0 |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2688 | }; |
| 2689 | enum TTYPE_enum { |
| 2690 | TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE |
| 2691 | }; |
| 2692 | int ttype; |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 2693 | if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2694 | 0, &ttype) ){ |
| 2695 | return TCL_ERROR; |
| 2696 | } |
| 2697 | switch( (enum TTYPE_enum)ttype ){ |
| 2698 | case TTYPE_DEFERRED: /* no-op */; break; |
| 2699 | case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; |
| 2700 | case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; |
| 2701 | } |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2702 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2703 | pScript = objv[objc-1]; |
| 2704 | |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2705 | /* Run the SQLite BEGIN command to open a transaction or savepoint. */ |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2706 | pDb->disableAuth++; |
| 2707 | rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); |
| 2708 | pDb->disableAuth--; |
| 2709 | if( rc!=SQLITE_OK ){ |
| 2710 | Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
| 2711 | return TCL_ERROR; |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2712 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2713 | pDb->nTransaction++; |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2714 | |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2715 | /* If using NRE, schedule a callback to invoke the script pScript, then |
| 2716 | ** a second callback to commit (or rollback) the transaction or savepoint |
| 2717 | ** opened above. If not using NRE, evaluate the script directly, then |
| 2718 | ** call function DbTransPostCmd() to commit (or rollback) the transaction |
| 2719 | ** or savepoint. */ |
| 2720 | if( DbUseNre() ){ |
| 2721 | Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); |
| 2722 | Tcl_NREvalObj(interp, pScript, 0); |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2723 | }else{ |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2724 | rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2725 | } |
| 2726 | break; |
| 2727 | } |
| 2728 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2729 | /* |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2730 | ** $db unlock_notify ?script? |
| 2731 | */ |
| 2732 | case DB_UNLOCK_NOTIFY: { |
| 2733 | #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 2734 | Tcl_AppendResult(interp, "unlock_notify not available in this build", 0); |
| 2735 | rc = TCL_ERROR; |
| 2736 | #else |
| 2737 | if( objc!=2 && objc!=3 ){ |
| 2738 | Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); |
| 2739 | rc = TCL_ERROR; |
| 2740 | }else{ |
| 2741 | void (*xNotify)(void **, int) = 0; |
| 2742 | void *pNotifyArg = 0; |
| 2743 | |
| 2744 | if( pDb->pUnlockNotify ){ |
| 2745 | Tcl_DecrRefCount(pDb->pUnlockNotify); |
| 2746 | pDb->pUnlockNotify = 0; |
| 2747 | } |
| 2748 | |
| 2749 | if( objc==3 ){ |
| 2750 | xNotify = DbUnlockNotify; |
| 2751 | pNotifyArg = (void *)pDb; |
| 2752 | pDb->pUnlockNotify = objv[2]; |
| 2753 | Tcl_IncrRefCount(pDb->pUnlockNotify); |
| 2754 | } |
| 2755 | |
| 2756 | if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ |
| 2757 | Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
| 2758 | rc = TCL_ERROR; |
| 2759 | } |
| 2760 | } |
| 2761 | #endif |
| 2762 | break; |
| 2763 | } |
| 2764 | |
| 2765 | /* |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 2766 | ** $db wal_hook ?script? |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2767 | ** $db update_hook ?script? |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2768 | ** $db rollback_hook ?script? |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2769 | */ |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 2770 | case DB_WAL_HOOK: |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2771 | case DB_UPDATE_HOOK: |
| 2772 | case DB_ROLLBACK_HOOK: { |
| 2773 | |
| 2774 | /* set ppHook to point at pUpdateHook or pRollbackHook, depending on |
| 2775 | ** whether [$db update_hook] or [$db rollback_hook] was invoked. |
| 2776 | */ |
| 2777 | Tcl_Obj **ppHook; |
| 2778 | if( choice==DB_UPDATE_HOOK ){ |
| 2779 | ppHook = &pDb->pUpdateHook; |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 2780 | }else if( choice==DB_WAL_HOOK ){ |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 2781 | ppHook = &pDb->pWalHook; |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2782 | }else{ |
| 2783 | ppHook = &pDb->pRollbackHook; |
| 2784 | } |
| 2785 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2786 | if( objc!=2 && objc!=3 ){ |
| 2787 | Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); |
| 2788 | return TCL_ERROR; |
| 2789 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2790 | if( *ppHook ){ |
| 2791 | Tcl_SetObjResult(interp, *ppHook); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2792 | if( objc==3 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2793 | Tcl_DecrRefCount(*ppHook); |
| 2794 | *ppHook = 0; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2795 | } |
| 2796 | } |
| 2797 | if( objc==3 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2798 | assert( !(*ppHook) ); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2799 | if( Tcl_GetCharLength(objv[2])>0 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2800 | *ppHook = objv[2]; |
| 2801 | Tcl_IncrRefCount(*ppHook); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2804 | |
| 2805 | sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); |
| 2806 | sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 2807 | sqlite3_wal_hook(pDb->db,(pDb->pWalHook?DbWalHandler:0),pDb); |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2808 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2809 | break; |
| 2810 | } |
| 2811 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 2812 | /* $db version |
| 2813 | ** |
| 2814 | ** Return the version string for this database. |
| 2815 | */ |
| 2816 | case DB_VERSION: { |
| 2817 | Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); |
| 2818 | break; |
| 2819 | } |
| 2820 | |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 2821 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2822 | } /* End of the SWITCH statement */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2823 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 2826 | #if SQLITE_TCL_NRE |
| 2827 | /* |
| 2828 | ** Adaptor that provides an objCmd interface to the NRE-enabled |
| 2829 | ** interface implementation. |
| 2830 | */ |
| 2831 | static int DbObjCmdAdaptor( |
| 2832 | void *cd, |
| 2833 | Tcl_Interp *interp, |
| 2834 | int objc, |
| 2835 | Tcl_Obj *const*objv |
| 2836 | ){ |
| 2837 | return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); |
| 2838 | } |
| 2839 | #endif /* SQLITE_TCL_NRE */ |
| 2840 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2841 | /* |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2842 | ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2843 | ** ?-create BOOLEAN? ?-nomutex BOOLEAN? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2844 | ** |
| 2845 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 2846 | ** invoked, this routine runs to process that command. |
| 2847 | ** |
| 2848 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 2849 | ** database connection. This command creates a new command named |
| 2850 | ** DBNAME that is used to control that connection. The database |
| 2851 | ** connection is deleted when the DBNAME command is deleted. |
| 2852 | ** |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2853 | ** The second argument is the name of the database file. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2854 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2855 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2856 | 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] | 2857 | SqliteDb *p; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2858 | void *pKey = 0; |
| 2859 | int nKey = 0; |
| 2860 | const char *zArg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2861 | char *zErrMsg; |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2862 | int i; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2863 | const char *zFile; |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2864 | const char *zVfs = 0; |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 2865 | int flags; |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2866 | Tcl_DString translatedFilename; |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 2867 | |
| 2868 | /* In normal use, each TCL interpreter runs in a single thread. So |
| 2869 | ** by default, we can turn of mutexing on SQLite database connections. |
| 2870 | ** However, for testing purposes it is useful to have mutexes turned |
| 2871 | ** on. So, by default, mutexes default off. But if compiled with |
| 2872 | ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. |
| 2873 | */ |
| 2874 | #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX |
| 2875 | flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; |
| 2876 | #else |
| 2877 | flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; |
| 2878 | #endif |
| 2879 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2880 | if( objc==2 ){ |
| 2881 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2882 | if( strcmp(zArg,"-version")==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2883 | Tcl_AppendResult(interp,sqlite3_version,0); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 2884 | return TCL_OK; |
| 2885 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2886 | if( strcmp(zArg,"-has-codec")==0 ){ |
| 2887 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2888 | Tcl_AppendResult(interp,"1",0); |
| 2889 | #else |
| 2890 | Tcl_AppendResult(interp,"0",0); |
| 2891 | #endif |
| 2892 | return TCL_OK; |
| 2893 | } |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2894 | } |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2895 | for(i=3; i+1<objc; i+=2){ |
| 2896 | zArg = Tcl_GetString(objv[i]); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2897 | if( strcmp(zArg,"-key")==0 ){ |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2898 | pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); |
| 2899 | }else if( strcmp(zArg, "-vfs")==0 ){ |
dan | 3c3dd7b | 2010-06-22 11:10:40 +0000 | [diff] [blame] | 2900 | zVfs = Tcl_GetString(objv[i+1]); |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2901 | }else if( strcmp(zArg, "-readonly")==0 ){ |
| 2902 | int b; |
| 2903 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
| 2904 | if( b ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2905 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2906 | flags |= SQLITE_OPEN_READONLY; |
| 2907 | }else{ |
| 2908 | flags &= ~SQLITE_OPEN_READONLY; |
| 2909 | flags |= SQLITE_OPEN_READWRITE; |
| 2910 | } |
| 2911 | }else if( strcmp(zArg, "-create")==0 ){ |
| 2912 | int b; |
| 2913 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2914 | if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2915 | flags |= SQLITE_OPEN_CREATE; |
| 2916 | }else{ |
| 2917 | flags &= ~SQLITE_OPEN_CREATE; |
| 2918 | } |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2919 | }else if( strcmp(zArg, "-nomutex")==0 ){ |
| 2920 | int b; |
| 2921 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
| 2922 | if( b ){ |
| 2923 | flags |= SQLITE_OPEN_NOMUTEX; |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2924 | flags &= ~SQLITE_OPEN_FULLMUTEX; |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2925 | }else{ |
| 2926 | flags &= ~SQLITE_OPEN_NOMUTEX; |
| 2927 | } |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2928 | }else if( strcmp(zArg, "-fullmutex")==0 ){ |
| 2929 | int b; |
| 2930 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
| 2931 | if( b ){ |
| 2932 | flags |= SQLITE_OPEN_FULLMUTEX; |
| 2933 | flags &= ~SQLITE_OPEN_NOMUTEX; |
| 2934 | }else{ |
| 2935 | flags &= ~SQLITE_OPEN_FULLMUTEX; |
| 2936 | } |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2937 | }else{ |
| 2938 | Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); |
| 2939 | return TCL_ERROR; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2940 | } |
| 2941 | } |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2942 | if( objc<3 || (objc&1)!=1 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2943 | Tcl_WrongNumArgs(interp, 1, objv, |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2944 | "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2945 | " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?" |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2946 | #ifdef SQLITE_HAS_CODEC |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2947 | " ?-key CODECKEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2948 | #endif |
| 2949 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2950 | return TCL_ERROR; |
| 2951 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2952 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 2953 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2954 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2955 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 2956 | return TCL_ERROR; |
| 2957 | } |
| 2958 | memset(p, 0, sizeof(*p)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2959 | zFile = Tcl_GetStringFromObj(objv[2], 0); |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2960 | zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2961 | sqlite3_open_v2(zFile, &p->db, flags, zVfs); |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2962 | Tcl_DStringFree(&translatedFilename); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2963 | if( SQLITE_OK!=sqlite3_errcode(p->db) ){ |
drh | 9404d50 | 2006-12-19 18:46:08 +0000 | [diff] [blame] | 2964 | zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2965 | sqlite3_close(p->db); |
| 2966 | p->db = 0; |
| 2967 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2968 | #ifdef SQLITE_HAS_CODEC |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2969 | if( p->db ){ |
| 2970 | sqlite3_key(p->db, pKey, nKey); |
| 2971 | } |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 2972 | #endif |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2973 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2974 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2975 | Tcl_Free((char*)p); |
drh | 9404d50 | 2006-12-19 18:46:08 +0000 | [diff] [blame] | 2976 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2977 | return TCL_ERROR; |
| 2978 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2979 | p->maxStmt = NUM_PREPARED_STMTS; |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 2980 | p->interp = interp; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2981 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2982 | if( DbUseNre() ){ |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 2983 | Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, |
| 2984 | (char*)p, DbDeleteCmd); |
dan | 4a4c11a | 2009-10-06 14:59:02 +0000 | [diff] [blame] | 2985 | }else{ |
| 2986 | Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
| 2987 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2988 | return TCL_OK; |
| 2989 | } |
| 2990 | |
| 2991 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 2992 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 2993 | ** library. |
| 2994 | */ |
| 2995 | #ifndef USE_TCL_STUBS |
| 2996 | # undef Tcl_InitStubs |
| 2997 | # define Tcl_InitStubs(a,b,c) |
| 2998 | #endif |
| 2999 | |
| 3000 | /* |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 3001 | ** Make sure we have a PACKAGE_VERSION macro defined. This will be |
| 3002 | ** defined automatically by the TEA makefile. But other makefiles |
| 3003 | ** do not define it. |
| 3004 | */ |
| 3005 | #ifndef PACKAGE_VERSION |
| 3006 | # define PACKAGE_VERSION SQLITE_VERSION |
| 3007 | #endif |
| 3008 | |
| 3009 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3010 | ** Initialize this module. |
| 3011 | ** |
| 3012 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 3013 | ** (Hence there is no namespace. There is no point in using a namespace |
| 3014 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 3015 | ** used to open a new SQLite database. See the DbMain() routine above |
| 3016 | ** for additional information. |
| 3017 | */ |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 3018 | EXTERN int Sqlite3_Init(Tcl_Interp *interp){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 3019 | Tcl_InitStubs(interp, "8.4", 0); |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 3020 | Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 3021 | Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 3022 | Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 3023 | Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 3024 | return TCL_OK; |
| 3025 | } |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 3026 | EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 3027 | EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
| 3028 | EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
drh | e2c3a65 | 2008-09-23 09:58:46 +0000 | [diff] [blame] | 3029 | EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 3030 | EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 3031 | EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 3032 | EXTERN int Tclsqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
| 3033 | |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 3034 | |
| 3035 | #ifndef SQLITE_3_SUFFIX_ONLY |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 3036 | EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 3037 | EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 3038 | EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
| 3039 | EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
drh | e2c3a65 | 2008-09-23 09:58:46 +0000 | [diff] [blame] | 3040 | EXTERN int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 3041 | EXTERN int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 3042 | EXTERN int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 3043 | EXTERN int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 3044 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3045 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3046 | #ifdef TCLSH |
| 3047 | /***************************************************************************** |
drh | 57a0227 | 2009-10-22 20:52:05 +0000 | [diff] [blame] | 3048 | ** All of the code that follows is used to build standalone TCL interpreters |
| 3049 | ** that are statically linked with SQLite. Enable these by compiling |
| 3050 | ** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard |
| 3051 | ** tclsh but with SQLite built in. An n of 2 generates the SQLite space |
| 3052 | ** analysis program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3053 | */ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3054 | |
drh | 57a0227 | 2009-10-22 20:52:05 +0000 | [diff] [blame] | 3055 | #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) |
| 3056 | /* |
| 3057 | * This code implements the MD5 message-digest algorithm. |
| 3058 | * The algorithm is due to Ron Rivest. This code was |
| 3059 | * written by Colin Plumb in 1993, no copyright is claimed. |
| 3060 | * This code is in the public domain; do with it what you wish. |
| 3061 | * |
| 3062 | * Equivalent code is available from RSA Data Security, Inc. |
| 3063 | * This code has been tested against that, and is equivalent, |
| 3064 | * except that you don't need to include two pages of legalese |
| 3065 | * with every copy. |
| 3066 | * |
| 3067 | * To compute the message digest of a chunk of bytes, declare an |
| 3068 | * MD5Context structure, pass it to MD5Init, call MD5Update as |
| 3069 | * needed on buffers full of bytes, and then call MD5Final, which |
| 3070 | * will fill a supplied 16-byte array with the digest. |
| 3071 | */ |
| 3072 | |
| 3073 | /* |
| 3074 | * If compiled on a machine that doesn't have a 32-bit integer, |
| 3075 | * you just set "uint32" to the appropriate datatype for an |
| 3076 | * unsigned 32-bit integer. For example: |
| 3077 | * |
| 3078 | * cc -Duint32='unsigned long' md5.c |
| 3079 | * |
| 3080 | */ |
| 3081 | #ifndef uint32 |
| 3082 | # define uint32 unsigned int |
| 3083 | #endif |
| 3084 | |
| 3085 | struct MD5Context { |
| 3086 | int isInit; |
| 3087 | uint32 buf[4]; |
| 3088 | uint32 bits[2]; |
| 3089 | unsigned char in[64]; |
| 3090 | }; |
| 3091 | typedef struct MD5Context MD5Context; |
| 3092 | |
| 3093 | /* |
| 3094 | * Note: this code is harmless on little-endian machines. |
| 3095 | */ |
| 3096 | static void byteReverse (unsigned char *buf, unsigned longs){ |
| 3097 | uint32 t; |
| 3098 | do { |
| 3099 | t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | |
| 3100 | ((unsigned)buf[1]<<8 | buf[0]); |
| 3101 | *(uint32 *)buf = t; |
| 3102 | buf += 4; |
| 3103 | } while (--longs); |
| 3104 | } |
| 3105 | /* The four core functions - F1 is optimized somewhat */ |
| 3106 | |
| 3107 | /* #define F1(x, y, z) (x & y | ~x & z) */ |
| 3108 | #define F1(x, y, z) (z ^ (x & (y ^ z))) |
| 3109 | #define F2(x, y, z) F1(z, x, y) |
| 3110 | #define F3(x, y, z) (x ^ y ^ z) |
| 3111 | #define F4(x, y, z) (y ^ (x | ~z)) |
| 3112 | |
| 3113 | /* This is the central step in the MD5 algorithm. */ |
| 3114 | #define MD5STEP(f, w, x, y, z, data, s) \ |
| 3115 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) |
| 3116 | |
| 3117 | /* |
| 3118 | * The core of the MD5 algorithm, this alters an existing MD5 hash to |
| 3119 | * reflect the addition of 16 longwords of new data. MD5Update blocks |
| 3120 | * the data and converts bytes into longwords for this routine. |
| 3121 | */ |
| 3122 | static void MD5Transform(uint32 buf[4], const uint32 in[16]){ |
| 3123 | register uint32 a, b, c, d; |
| 3124 | |
| 3125 | a = buf[0]; |
| 3126 | b = buf[1]; |
| 3127 | c = buf[2]; |
| 3128 | d = buf[3]; |
| 3129 | |
| 3130 | MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); |
| 3131 | MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); |
| 3132 | MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); |
| 3133 | MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); |
| 3134 | MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); |
| 3135 | MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); |
| 3136 | MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); |
| 3137 | MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); |
| 3138 | MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); |
| 3139 | MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); |
| 3140 | MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); |
| 3141 | MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); |
| 3142 | MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); |
| 3143 | MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); |
| 3144 | MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); |
| 3145 | MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); |
| 3146 | |
| 3147 | MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); |
| 3148 | MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); |
| 3149 | MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); |
| 3150 | MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); |
| 3151 | MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); |
| 3152 | MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); |
| 3153 | MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); |
| 3154 | MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); |
| 3155 | MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); |
| 3156 | MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); |
| 3157 | MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); |
| 3158 | MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); |
| 3159 | MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); |
| 3160 | MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); |
| 3161 | MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); |
| 3162 | MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); |
| 3163 | |
| 3164 | MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); |
| 3165 | MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); |
| 3166 | MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); |
| 3167 | MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); |
| 3168 | MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); |
| 3169 | MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); |
| 3170 | MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); |
| 3171 | MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); |
| 3172 | MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); |
| 3173 | MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); |
| 3174 | MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); |
| 3175 | MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); |
| 3176 | MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); |
| 3177 | MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); |
| 3178 | MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); |
| 3179 | MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); |
| 3180 | |
| 3181 | MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); |
| 3182 | MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); |
| 3183 | MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); |
| 3184 | MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); |
| 3185 | MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); |
| 3186 | MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); |
| 3187 | MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); |
| 3188 | MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); |
| 3189 | MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); |
| 3190 | MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); |
| 3191 | MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); |
| 3192 | MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); |
| 3193 | MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); |
| 3194 | MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); |
| 3195 | MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); |
| 3196 | MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); |
| 3197 | |
| 3198 | buf[0] += a; |
| 3199 | buf[1] += b; |
| 3200 | buf[2] += c; |
| 3201 | buf[3] += d; |
| 3202 | } |
| 3203 | |
| 3204 | /* |
| 3205 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious |
| 3206 | * initialization constants. |
| 3207 | */ |
| 3208 | static void MD5Init(MD5Context *ctx){ |
| 3209 | ctx->isInit = 1; |
| 3210 | ctx->buf[0] = 0x67452301; |
| 3211 | ctx->buf[1] = 0xefcdab89; |
| 3212 | ctx->buf[2] = 0x98badcfe; |
| 3213 | ctx->buf[3] = 0x10325476; |
| 3214 | ctx->bits[0] = 0; |
| 3215 | ctx->bits[1] = 0; |
| 3216 | } |
| 3217 | |
| 3218 | /* |
| 3219 | * Update context to reflect the concatenation of another buffer full |
| 3220 | * of bytes. |
| 3221 | */ |
| 3222 | static |
| 3223 | void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ |
| 3224 | uint32 t; |
| 3225 | |
| 3226 | /* Update bitcount */ |
| 3227 | |
| 3228 | t = ctx->bits[0]; |
| 3229 | if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) |
| 3230 | ctx->bits[1]++; /* Carry from low to high */ |
| 3231 | ctx->bits[1] += len >> 29; |
| 3232 | |
| 3233 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ |
| 3234 | |
| 3235 | /* Handle any leading odd-sized chunks */ |
| 3236 | |
| 3237 | if ( t ) { |
| 3238 | unsigned char *p = (unsigned char *)ctx->in + t; |
| 3239 | |
| 3240 | t = 64-t; |
| 3241 | if (len < t) { |
| 3242 | memcpy(p, buf, len); |
| 3243 | return; |
| 3244 | } |
| 3245 | memcpy(p, buf, t); |
| 3246 | byteReverse(ctx->in, 16); |
| 3247 | MD5Transform(ctx->buf, (uint32 *)ctx->in); |
| 3248 | buf += t; |
| 3249 | len -= t; |
| 3250 | } |
| 3251 | |
| 3252 | /* Process data in 64-byte chunks */ |
| 3253 | |
| 3254 | while (len >= 64) { |
| 3255 | memcpy(ctx->in, buf, 64); |
| 3256 | byteReverse(ctx->in, 16); |
| 3257 | MD5Transform(ctx->buf, (uint32 *)ctx->in); |
| 3258 | buf += 64; |
| 3259 | len -= 64; |
| 3260 | } |
| 3261 | |
| 3262 | /* Handle any remaining bytes of data. */ |
| 3263 | |
| 3264 | memcpy(ctx->in, buf, len); |
| 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | * Final wrapup - pad to 64-byte boundary with the bit pattern |
| 3269 | * 1 0* (64-bit count of bits processed, MSB-first) |
| 3270 | */ |
| 3271 | static void MD5Final(unsigned char digest[16], MD5Context *ctx){ |
| 3272 | unsigned count; |
| 3273 | unsigned char *p; |
| 3274 | |
| 3275 | /* Compute number of bytes mod 64 */ |
| 3276 | count = (ctx->bits[0] >> 3) & 0x3F; |
| 3277 | |
| 3278 | /* Set the first char of padding to 0x80. This is safe since there is |
| 3279 | always at least one byte free */ |
| 3280 | p = ctx->in + count; |
| 3281 | *p++ = 0x80; |
| 3282 | |
| 3283 | /* Bytes of padding needed to make 64 bytes */ |
| 3284 | count = 64 - 1 - count; |
| 3285 | |
| 3286 | /* Pad out to 56 mod 64 */ |
| 3287 | if (count < 8) { |
| 3288 | /* Two lots of padding: Pad the first block to 64 bytes */ |
| 3289 | memset(p, 0, count); |
| 3290 | byteReverse(ctx->in, 16); |
| 3291 | MD5Transform(ctx->buf, (uint32 *)ctx->in); |
| 3292 | |
| 3293 | /* Now fill the next block with 56 bytes */ |
| 3294 | memset(ctx->in, 0, 56); |
| 3295 | } else { |
| 3296 | /* Pad block to 56 bytes */ |
| 3297 | memset(p, 0, count-8); |
| 3298 | } |
| 3299 | byteReverse(ctx->in, 14); |
| 3300 | |
| 3301 | /* Append length in bits and transform */ |
| 3302 | ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; |
| 3303 | ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; |
| 3304 | |
| 3305 | MD5Transform(ctx->buf, (uint32 *)ctx->in); |
| 3306 | byteReverse((unsigned char *)ctx->buf, 4); |
| 3307 | memcpy(digest, ctx->buf, 16); |
| 3308 | memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */ |
| 3309 | } |
| 3310 | |
| 3311 | /* |
| 3312 | ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. |
| 3313 | */ |
| 3314 | static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ |
| 3315 | static char const zEncode[] = "0123456789abcdef"; |
| 3316 | int i, j; |
| 3317 | |
| 3318 | for(j=i=0; i<16; i++){ |
| 3319 | int a = digest[i]; |
| 3320 | zBuf[j++] = zEncode[(a>>4)&0xf]; |
| 3321 | zBuf[j++] = zEncode[a & 0xf]; |
| 3322 | } |
| 3323 | zBuf[j] = 0; |
| 3324 | } |
| 3325 | |
| 3326 | |
| 3327 | /* |
| 3328 | ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers |
| 3329 | ** each representing 16 bits of the digest and separated from each |
| 3330 | ** other by a "-" character. |
| 3331 | */ |
| 3332 | static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ |
| 3333 | int i, j; |
| 3334 | unsigned int x; |
| 3335 | for(i=j=0; i<16; i+=2){ |
| 3336 | x = digest[i]*256 + digest[i+1]; |
| 3337 | if( i>0 ) zDigest[j++] = '-'; |
| 3338 | sprintf(&zDigest[j], "%05u", x); |
| 3339 | j += 5; |
| 3340 | } |
| 3341 | zDigest[j] = 0; |
| 3342 | } |
| 3343 | |
| 3344 | /* |
| 3345 | ** A TCL command for md5. The argument is the text to be hashed. The |
| 3346 | ** Result is the hash in base64. |
| 3347 | */ |
| 3348 | static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ |
| 3349 | MD5Context ctx; |
| 3350 | unsigned char digest[16]; |
| 3351 | char zBuf[50]; |
| 3352 | void (*converter)(unsigned char*, char*); |
| 3353 | |
| 3354 | if( argc!=2 ){ |
| 3355 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 3356 | " TEXT\"", 0); |
| 3357 | return TCL_ERROR; |
| 3358 | } |
| 3359 | MD5Init(&ctx); |
| 3360 | MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); |
| 3361 | MD5Final(digest, &ctx); |
| 3362 | converter = (void(*)(unsigned char*,char*))cd; |
| 3363 | converter(digest, zBuf); |
| 3364 | Tcl_AppendResult(interp, zBuf, (char*)0); |
| 3365 | return TCL_OK; |
| 3366 | } |
| 3367 | |
| 3368 | /* |
| 3369 | ** A TCL command to take the md5 hash of a file. The argument is the |
| 3370 | ** name of the file. |
| 3371 | */ |
| 3372 | static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ |
| 3373 | FILE *in; |
| 3374 | MD5Context ctx; |
| 3375 | void (*converter)(unsigned char*, char*); |
| 3376 | unsigned char digest[16]; |
| 3377 | char zBuf[10240]; |
| 3378 | |
| 3379 | if( argc!=2 ){ |
| 3380 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 3381 | " FILENAME\"", 0); |
| 3382 | return TCL_ERROR; |
| 3383 | } |
| 3384 | in = fopen(argv[1],"rb"); |
| 3385 | if( in==0 ){ |
| 3386 | Tcl_AppendResult(interp,"unable to open file \"", argv[1], |
| 3387 | "\" for reading", 0); |
| 3388 | return TCL_ERROR; |
| 3389 | } |
| 3390 | MD5Init(&ctx); |
| 3391 | for(;;){ |
| 3392 | int n; |
| 3393 | n = fread(zBuf, 1, sizeof(zBuf), in); |
| 3394 | if( n<=0 ) break; |
| 3395 | MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); |
| 3396 | } |
| 3397 | fclose(in); |
| 3398 | MD5Final(digest, &ctx); |
| 3399 | converter = (void(*)(unsigned char*,char*))cd; |
| 3400 | converter(digest, zBuf); |
| 3401 | Tcl_AppendResult(interp, zBuf, (char*)0); |
| 3402 | return TCL_OK; |
| 3403 | } |
| 3404 | |
| 3405 | /* |
| 3406 | ** Register the four new TCL commands for generating MD5 checksums |
| 3407 | ** with the TCL interpreter. |
| 3408 | */ |
| 3409 | int Md5_Init(Tcl_Interp *interp){ |
| 3410 | Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, |
| 3411 | MD5DigestToBase16, 0); |
| 3412 | Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, |
| 3413 | MD5DigestToBase10x8, 0); |
| 3414 | Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, |
| 3415 | MD5DigestToBase16, 0); |
| 3416 | Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, |
| 3417 | MD5DigestToBase10x8, 0); |
| 3418 | return TCL_OK; |
| 3419 | } |
| 3420 | #endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ |
| 3421 | |
| 3422 | #if defined(SQLITE_TEST) |
| 3423 | /* |
| 3424 | ** During testing, the special md5sum() aggregate function is available. |
| 3425 | ** inside SQLite. The following routines implement that function. |
| 3426 | */ |
| 3427 | static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 3428 | MD5Context *p; |
| 3429 | int i; |
| 3430 | if( argc<1 ) return; |
| 3431 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
| 3432 | if( p==0 ) return; |
| 3433 | if( !p->isInit ){ |
| 3434 | MD5Init(p); |
| 3435 | } |
| 3436 | for(i=0; i<argc; i++){ |
| 3437 | const char *zData = (char*)sqlite3_value_text(argv[i]); |
| 3438 | if( zData ){ |
| 3439 | MD5Update(p, (unsigned char*)zData, strlen(zData)); |
| 3440 | } |
| 3441 | } |
| 3442 | } |
| 3443 | static void md5finalize(sqlite3_context *context){ |
| 3444 | MD5Context *p; |
| 3445 | unsigned char digest[16]; |
| 3446 | char zBuf[33]; |
| 3447 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
| 3448 | MD5Final(digest,p); |
| 3449 | MD5DigestToBase16(digest, zBuf); |
| 3450 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 3451 | } |
| 3452 | int Md5_Register(sqlite3 *db){ |
| 3453 | int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, |
| 3454 | md5step, md5finalize); |
| 3455 | sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ |
| 3456 | return rc; |
| 3457 | } |
| 3458 | #endif /* defined(SQLITE_TEST) */ |
| 3459 | |
| 3460 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3461 | /* |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3462 | ** If the macro TCLSH is one, then put in code this for the |
| 3463 | ** "main" routine that will initialize Tcl and take input from |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 3464 | ** standard input, or if a file is named on the command line |
| 3465 | ** the TCL interpreter reads and evaluates that file. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3466 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3467 | #if TCLSH==1 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3468 | static char zMainloop[] = |
| 3469 | "set line {}\n" |
| 3470 | "while {![eof stdin]} {\n" |
| 3471 | "if {$line!=\"\"} {\n" |
| 3472 | "puts -nonewline \"> \"\n" |
| 3473 | "} else {\n" |
| 3474 | "puts -nonewline \"% \"\n" |
| 3475 | "}\n" |
| 3476 | "flush stdout\n" |
| 3477 | "append line [gets stdin]\n" |
| 3478 | "if {[info complete $line]} {\n" |
| 3479 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 3480 | "puts stderr \"Error: $result\"\n" |
| 3481 | "} elseif {$result!=\"\"} {\n" |
| 3482 | "puts $result\n" |
| 3483 | "}\n" |
| 3484 | "set line {}\n" |
| 3485 | "} else {\n" |
| 3486 | "append line \\n\n" |
| 3487 | "}\n" |
| 3488 | "}\n" |
| 3489 | ; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3490 | #endif |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame^] | 3491 | #if TCLSH==2 |
| 3492 | static char zMainloop[] = |
| 3493 | #include "spaceanal_tcl.h" |
| 3494 | ; |
| 3495 | #endif |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3496 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 3497 | #ifdef SQLITE_TEST |
| 3498 | static void init_all(Tcl_Interp *); |
| 3499 | static int init_all_cmd( |
| 3500 | ClientData cd, |
| 3501 | Tcl_Interp *interp, |
| 3502 | int objc, |
| 3503 | Tcl_Obj *CONST objv[] |
| 3504 | ){ |
danielk1977 | 0a54907 | 2009-02-17 16:29:10 +0000 | [diff] [blame] | 3505 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 3506 | Tcl_Interp *slave; |
| 3507 | if( objc!=2 ){ |
| 3508 | Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); |
| 3509 | return TCL_ERROR; |
| 3510 | } |
| 3511 | |
| 3512 | slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); |
| 3513 | if( !slave ){ |
| 3514 | return TCL_ERROR; |
| 3515 | } |
| 3516 | |
| 3517 | init_all(slave); |
| 3518 | return TCL_OK; |
| 3519 | } |
| 3520 | #endif |
| 3521 | |
| 3522 | /* |
| 3523 | ** Configure the interpreter passed as the first argument to have access |
| 3524 | ** to the commands and linked variables that make up: |
| 3525 | ** |
| 3526 | ** * the [sqlite3] extension itself, |
| 3527 | ** |
| 3528 | ** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and |
| 3529 | ** |
| 3530 | ** * If SQLITE_TEST is set, the various test interfaces used by the Tcl |
| 3531 | ** test suite. |
| 3532 | */ |
| 3533 | static void init_all(Tcl_Interp *interp){ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 3534 | Sqlite3_Init(interp); |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 3535 | |
drh | 57a0227 | 2009-10-22 20:52:05 +0000 | [diff] [blame] | 3536 | #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) |
| 3537 | Md5_Init(interp); |
| 3538 | #endif |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 3539 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3540 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 3541 | { |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 3542 | extern int Sqliteconfig_Init(Tcl_Interp*); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 3543 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3544 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 3545 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 3546 | extern int Sqlitetest4_Init(Tcl_Interp*); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 3547 | extern int Sqlitetest5_Init(Tcl_Interp*); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 3548 | extern int Sqlitetest6_Init(Tcl_Interp*); |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 3549 | extern int Sqlitetest7_Init(Tcl_Interp*); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3550 | extern int Sqlitetest8_Init(Tcl_Interp*); |
danielk1977 | a713f2c | 2007-03-29 12:19:11 +0000 | [diff] [blame] | 3551 | extern int Sqlitetest9_Init(Tcl_Interp*); |
drh | 2366940 | 2006-01-09 17:29:52 +0000 | [diff] [blame] | 3552 | extern int Sqlitetestasync_Init(Tcl_Interp*); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 3553 | extern int Sqlitetest_autoext_Init(Tcl_Interp*); |
dan | 0a7a915 | 2010-04-07 07:57:38 +0000 | [diff] [blame] | 3554 | extern int Sqlitetest_demovfs_Init(Tcl_Interp *); |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 3555 | extern int Sqlitetest_func_Init(Tcl_Interp*); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 3556 | extern int Sqlitetest_hexio_Init(Tcl_Interp*); |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 3557 | extern int Sqlitetest_init_Init(Tcl_Interp*); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 3558 | extern int Sqlitetest_malloc_Init(Tcl_Interp*); |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 3559 | extern int Sqlitetest_mutex_Init(Tcl_Interp*); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 3560 | extern int Sqlitetestschema_Init(Tcl_Interp*); |
| 3561 | extern int Sqlitetestsse_Init(Tcl_Interp*); |
| 3562 | extern int Sqlitetesttclvar_Init(Tcl_Interp*); |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 3563 | extern int SqlitetestThread_Init(Tcl_Interp*); |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 3564 | extern int SqlitetestOnefile_Init(); |
danielk1977 | 5d1f5aa | 2008-04-10 14:51:00 +0000 | [diff] [blame] | 3565 | extern int SqlitetestOsinst_Init(Tcl_Interp*); |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 3566 | extern int Sqlitetestbackup_Init(Tcl_Interp*); |
drh | 522efc6 | 2009-11-10 17:24:37 +0000 | [diff] [blame] | 3567 | extern int Sqlitetestintarray_Init(Tcl_Interp*); |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 3568 | extern int Sqlitetestvfs_Init(Tcl_Interp *); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 3569 | extern int SqlitetestStat_Init(Tcl_Interp*); |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 3570 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 3571 | Sqliteconfig_Init(interp); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 3572 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3573 | Sqlitetest2_Init(interp); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3574 | Sqlitetest3_Init(interp); |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 3575 | Sqlitetest4_Init(interp); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 3576 | Sqlitetest5_Init(interp); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 3577 | Sqlitetest6_Init(interp); |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 3578 | Sqlitetest7_Init(interp); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3579 | Sqlitetest8_Init(interp); |
danielk1977 | a713f2c | 2007-03-29 12:19:11 +0000 | [diff] [blame] | 3580 | Sqlitetest9_Init(interp); |
drh | 2366940 | 2006-01-09 17:29:52 +0000 | [diff] [blame] | 3581 | Sqlitetestasync_Init(interp); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 3582 | Sqlitetest_autoext_Init(interp); |
dan | 0a7a915 | 2010-04-07 07:57:38 +0000 | [diff] [blame] | 3583 | Sqlitetest_demovfs_Init(interp); |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 3584 | Sqlitetest_func_Init(interp); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 3585 | Sqlitetest_hexio_Init(interp); |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 3586 | Sqlitetest_init_Init(interp); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 3587 | Sqlitetest_malloc_Init(interp); |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 3588 | Sqlitetest_mutex_Init(interp); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 3589 | Sqlitetestschema_Init(interp); |
| 3590 | Sqlitetesttclvar_Init(interp); |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 3591 | SqlitetestThread_Init(interp); |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 3592 | SqlitetestOnefile_Init(interp); |
danielk1977 | 5d1f5aa | 2008-04-10 14:51:00 +0000 | [diff] [blame] | 3593 | SqlitetestOsinst_Init(interp); |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 3594 | Sqlitetestbackup_Init(interp); |
drh | 522efc6 | 2009-11-10 17:24:37 +0000 | [diff] [blame] | 3595 | Sqlitetestintarray_Init(interp); |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 3596 | Sqlitetestvfs_Init(interp); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 3597 | SqlitetestStat_Init(interp); |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 3598 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 3599 | Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0); |
| 3600 | |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 3601 | #ifdef SQLITE_SSE |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 3602 | Sqlitetestsse_Init(interp); |
| 3603 | #endif |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 3604 | } |
| 3605 | #endif |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 3609 | int TCLSH_MAIN(int argc, char **argv){ |
| 3610 | Tcl_Interp *interp; |
| 3611 | |
| 3612 | /* Call sqlite3_shutdown() once before doing anything else. This is to |
| 3613 | ** test that sqlite3_shutdown() can be safely called by a process before |
| 3614 | ** sqlite3_initialize() is. */ |
| 3615 | sqlite3_shutdown(); |
| 3616 | |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame^] | 3617 | #if TCLSH==2 |
| 3618 | sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); |
| 3619 | #endif |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 3620 | Tcl_FindExecutable(argv[0]); |
| 3621 | |
| 3622 | interp = Tcl_CreateInterp(); |
| 3623 | init_all(interp); |
drh | c728597 | 2009-11-10 01:13:25 +0000 | [diff] [blame] | 3624 | if( argc>=2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3625 | int i; |
shess | ad42c3a | 2006-08-22 23:53:46 +0000 | [diff] [blame] | 3626 | char zArgc[32]; |
| 3627 | sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); |
| 3628 | Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3629 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 3630 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
drh | 61212b6 | 2004-12-02 20:17:00 +0000 | [diff] [blame] | 3631 | for(i=3-TCLSH; i<argc; i++){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3632 | Tcl_SetVar(interp, "argv", argv[i], |
| 3633 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 3634 | } |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame^] | 3635 | if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 3636 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | a81c64a | 2009-01-14 23:38:02 +0000 | [diff] [blame] | 3637 | if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 3638 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3639 | return 1; |
| 3640 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3641 | } |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame^] | 3642 | if( TCLSH==2 || argc<=1 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 3643 | Tcl_GlobalEval(interp, zMainloop); |
| 3644 | } |
| 3645 | return 0; |
| 3646 | } |
| 3647 | #endif /* TCLSH */ |