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