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