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