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