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