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