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 | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 14 | ** |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame^] | 15 | ** $Id: tclsqlite.c,v 1.235 2009/02/03 16:51:25 danielk1977 Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 16 | */ |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 17 | #include "tcl.h" |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 18 | #include <errno.h> |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | ** Some additional include files are needed if this file is not |
| 22 | ** appended to the amalgamation. |
| 23 | */ |
| 24 | #ifndef SQLITE_AMALGAMATION |
| 25 | # include "sqliteInt.h" |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 26 | # include <stdlib.h> |
| 27 | # include <string.h> |
| 28 | # include <assert.h> |
| 29 | # include <ctype.h> |
| 30 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 31 | |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Windows needs to know which symbols to export. Unix does not. |
| 34 | * BUILD_sqlite should be undefined for Unix. |
| 35 | */ |
| 36 | #ifdef BUILD_sqlite |
| 37 | #undef TCL_STORAGE_CLASS |
| 38 | #define TCL_STORAGE_CLASS DLLEXPORT |
| 39 | #endif /* BUILD_sqlite */ |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 40 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 41 | #define NUM_PREPARED_STMTS 10 |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 42 | #define MAX_PREPARED_STMTS 100 |
| 43 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 44 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 45 | ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
| 46 | ** have to do a translation when going between the two. Set the |
| 47 | ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
| 48 | ** this translation. |
| 49 | */ |
| 50 | #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
| 51 | # define UTF_TRANSLATION_NEEDED 1 |
| 52 | #endif |
| 53 | |
| 54 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 55 | ** New SQL functions can be created as TCL scripts. Each such function |
| 56 | ** is described by an instance of the following structure. |
| 57 | */ |
| 58 | typedef struct SqlFunc SqlFunc; |
| 59 | struct SqlFunc { |
| 60 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 61 | Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ |
| 62 | int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ |
| 63 | char *zName; /* Name of this function */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 64 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 65 | }; |
| 66 | |
| 67 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 68 | ** New collation sequences function can be created as TCL scripts. Each such |
| 69 | ** function is described by an instance of the following structure. |
| 70 | */ |
| 71 | typedef struct SqlCollate SqlCollate; |
| 72 | struct SqlCollate { |
| 73 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 74 | char *zScript; /* The script to be run */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 75 | SqlCollate *pNext; /* Next function on the list of them all */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 79 | ** Prepared statements are cached for faster execution. Each prepared |
| 80 | ** statement is described by an instance of the following structure. |
| 81 | */ |
| 82 | typedef struct SqlPreparedStmt SqlPreparedStmt; |
| 83 | struct SqlPreparedStmt { |
| 84 | SqlPreparedStmt *pNext; /* Next in linked list */ |
| 85 | SqlPreparedStmt *pPrev; /* Previous on the list */ |
| 86 | sqlite3_stmt *pStmt; /* The prepared statement */ |
| 87 | int nSql; /* chars in zSql[] */ |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 88 | const char *zSql; /* Text of the SQL statement */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 91 | typedef struct IncrblobChannel IncrblobChannel; |
| 92 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 93 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 94 | ** There is one instance of this structure for each SQLite database |
| 95 | ** that has been opened by the SQLite TCL interface. |
| 96 | */ |
| 97 | typedef struct SqliteDb SqliteDb; |
| 98 | struct SqliteDb { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 99 | sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 100 | Tcl_Interp *interp; /* The interpreter used for this database */ |
| 101 | char *zBusy; /* The busy callback routine */ |
| 102 | char *zCommit; /* The commit hook callback routine */ |
| 103 | char *zTrace; /* The trace callback routine */ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 104 | char *zProfile; /* The profile callback routine */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 105 | char *zProgress; /* The progress callback routine */ |
| 106 | char *zAuth; /* The authorization callback routine */ |
drh | 1f1549f | 2008-08-26 21:33:34 +0000 | [diff] [blame] | 107 | int disableAuth; /* Disable the authorizer if it exists */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 108 | char *zNull; /* Text to substitute for an SQL NULL value */ |
| 109 | SqlFunc *pFunc; /* List of SQL functions */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 110 | Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 111 | Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 112 | SqlCollate *pCollate; /* List of SQL collation functions */ |
| 113 | int rc; /* Return code of most recent sqlite3_exec() */ |
| 114 | Tcl_Obj *pCollateNeeded; /* Collation needed script */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 115 | SqlPreparedStmt *stmtList; /* List of prepared statements*/ |
| 116 | SqlPreparedStmt *stmtLast; /* Last statement in the list */ |
| 117 | int maxStmt; /* The next maximum number of stmtList */ |
| 118 | int nStmt; /* Number of statements in stmtList */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 119 | IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 120 | int nStep, nSort; /* Statistics for most recent operation */ |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 121 | int nTransaction; /* Number of nested [transaction] methods */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 122 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 123 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 124 | struct IncrblobChannel { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 125 | sqlite3_blob *pBlob; /* sqlite3 blob handle */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 126 | SqliteDb *pDb; /* Associated database connection */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 127 | int iSeek; /* Current seek offset */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 128 | Tcl_Channel channel; /* Channel identifier */ |
| 129 | IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ |
| 130 | IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 133 | /* |
| 134 | ** Compute a string length that is limited to what can be stored in |
| 135 | ** lower 30 bits of a 32-bit signed integer. |
| 136 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 137 | static int strlen30(const char *z){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 138 | const char *z2 = z; |
| 139 | while( *z2 ){ z2++; } |
| 140 | return 0x3fffffff & (int)(z2 - z); |
| 141 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 142 | |
| 143 | |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 144 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 145 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 146 | ** Close all incrblob channels opened using database connection pDb. |
| 147 | ** This is called when shutting down the database connection. |
| 148 | */ |
| 149 | static void closeIncrblobChannels(SqliteDb *pDb){ |
| 150 | IncrblobChannel *p; |
| 151 | IncrblobChannel *pNext; |
| 152 | |
| 153 | for(p=pDb->pIncrblob; p; p=pNext){ |
| 154 | pNext = p->pNext; |
| 155 | |
| 156 | /* Note: Calling unregister here call Tcl_Close on the incrblob channel, |
| 157 | ** which deletes the IncrblobChannel structure at *p. So do not |
| 158 | ** call Tcl_Free() here. |
| 159 | */ |
| 160 | Tcl_UnregisterChannel(pDb->interp, p->channel); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /* |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 165 | ** Close an incremental blob channel. |
| 166 | */ |
| 167 | static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ |
| 168 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 169 | int rc = sqlite3_blob_close(p->pBlob); |
| 170 | sqlite3 *db = p->pDb->db; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 171 | |
| 172 | /* Remove the channel from the SqliteDb.pIncrblob list. */ |
| 173 | if( p->pNext ){ |
| 174 | p->pNext->pPrev = p->pPrev; |
| 175 | } |
| 176 | if( p->pPrev ){ |
| 177 | p->pPrev->pNext = p->pNext; |
| 178 | } |
| 179 | if( p->pDb->pIncrblob==p ){ |
| 180 | p->pDb->pIncrblob = p->pNext; |
| 181 | } |
| 182 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 183 | /* Free the IncrblobChannel structure */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 184 | Tcl_Free((char *)p); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 185 | |
| 186 | if( rc!=SQLITE_OK ){ |
| 187 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); |
| 188 | return TCL_ERROR; |
| 189 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 190 | return TCL_OK; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | ** Read data from an incremental blob channel. |
| 195 | */ |
| 196 | static int incrblobInput( |
| 197 | ClientData instanceData, |
| 198 | char *buf, |
| 199 | int bufSize, |
| 200 | int *errorCodePtr |
| 201 | ){ |
| 202 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 203 | int nRead = bufSize; /* Number of bytes to read */ |
| 204 | int nBlob; /* Total size of the blob */ |
| 205 | int rc; /* sqlite error code */ |
| 206 | |
| 207 | nBlob = sqlite3_blob_bytes(p->pBlob); |
| 208 | if( (p->iSeek+nRead)>nBlob ){ |
| 209 | nRead = nBlob-p->iSeek; |
| 210 | } |
| 211 | if( nRead<=0 ){ |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); |
| 216 | if( rc!=SQLITE_OK ){ |
| 217 | *errorCodePtr = rc; |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | p->iSeek += nRead; |
| 222 | return nRead; |
| 223 | } |
| 224 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 225 | /* |
| 226 | ** Write data to an incremental blob channel. |
| 227 | */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 228 | static int incrblobOutput( |
| 229 | ClientData instanceData, |
| 230 | CONST char *buf, |
| 231 | int toWrite, |
| 232 | int *errorCodePtr |
| 233 | ){ |
| 234 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 235 | int nWrite = toWrite; /* Number of bytes to write */ |
| 236 | int nBlob; /* Total size of the blob */ |
| 237 | int rc; /* sqlite error code */ |
| 238 | |
| 239 | nBlob = sqlite3_blob_bytes(p->pBlob); |
| 240 | if( (p->iSeek+nWrite)>nBlob ){ |
| 241 | *errorCodePtr = EINVAL; |
| 242 | return -1; |
| 243 | } |
| 244 | if( nWrite<=0 ){ |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); |
| 249 | if( rc!=SQLITE_OK ){ |
| 250 | *errorCodePtr = EIO; |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | p->iSeek += nWrite; |
| 255 | return nWrite; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | ** Seek an incremental blob channel. |
| 260 | */ |
| 261 | static int incrblobSeek( |
| 262 | ClientData instanceData, |
| 263 | long offset, |
| 264 | int seekMode, |
| 265 | int *errorCodePtr |
| 266 | ){ |
| 267 | IncrblobChannel *p = (IncrblobChannel *)instanceData; |
| 268 | |
| 269 | switch( seekMode ){ |
| 270 | case SEEK_SET: |
| 271 | p->iSeek = offset; |
| 272 | break; |
| 273 | case SEEK_CUR: |
| 274 | p->iSeek += offset; |
| 275 | break; |
| 276 | case SEEK_END: |
| 277 | p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; |
| 278 | break; |
| 279 | |
| 280 | default: assert(!"Bad seekMode"); |
| 281 | } |
| 282 | |
| 283 | return p->iSeek; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | static void incrblobWatch(ClientData instanceData, int mode){ |
| 288 | /* NO-OP */ |
| 289 | } |
| 290 | static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ |
| 291 | return TCL_ERROR; |
| 292 | } |
| 293 | |
| 294 | static Tcl_ChannelType IncrblobChannelType = { |
| 295 | "incrblob", /* typeName */ |
| 296 | TCL_CHANNEL_VERSION_2, /* version */ |
| 297 | incrblobClose, /* closeProc */ |
| 298 | incrblobInput, /* inputProc */ |
| 299 | incrblobOutput, /* outputProc */ |
| 300 | incrblobSeek, /* seekProc */ |
| 301 | 0, /* setOptionProc */ |
| 302 | 0, /* getOptionProc */ |
| 303 | incrblobWatch, /* watchProc (this is a no-op) */ |
| 304 | incrblobHandle, /* getHandleProc (always returns error) */ |
| 305 | 0, /* close2Proc */ |
| 306 | 0, /* blockModeProc */ |
| 307 | 0, /* flushProc */ |
| 308 | 0, /* handlerProc */ |
| 309 | 0, /* wideSeekProc */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | /* |
| 313 | ** Create a new incrblob channel. |
| 314 | */ |
| 315 | static int createIncrblobChannel( |
| 316 | Tcl_Interp *interp, |
| 317 | SqliteDb *pDb, |
| 318 | const char *zDb, |
| 319 | const char *zTable, |
| 320 | const char *zColumn, |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 321 | sqlite_int64 iRow, |
| 322 | int isReadonly |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 323 | ){ |
| 324 | IncrblobChannel *p; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 325 | sqlite3 *db = pDb->db; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 326 | sqlite3_blob *pBlob; |
| 327 | int rc; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 328 | int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 329 | |
| 330 | /* This variable is used to name the channels: "incrblob_[incr count]" */ |
| 331 | static int count = 0; |
| 332 | char zChannel[64]; |
| 333 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 334 | rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 335 | if( rc!=SQLITE_OK ){ |
| 336 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
| 337 | return TCL_ERROR; |
| 338 | } |
| 339 | |
| 340 | p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); |
| 341 | p->iSeek = 0; |
| 342 | p->pBlob = pBlob; |
| 343 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 344 | sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 345 | p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); |
| 346 | Tcl_RegisterChannel(interp, p->channel); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 347 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 348 | /* Link the new channel into the SqliteDb.pIncrblob list. */ |
| 349 | p->pNext = pDb->pIncrblob; |
| 350 | p->pPrev = 0; |
| 351 | if( p->pNext ){ |
| 352 | p->pNext->pPrev = p; |
| 353 | } |
| 354 | pDb->pIncrblob = p; |
| 355 | p->pDb = pDb; |
| 356 | |
| 357 | Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 358 | return TCL_OK; |
| 359 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 360 | #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ |
| 361 | #define closeIncrblobChannels(pDb) |
| 362 | #endif |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 363 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 364 | /* |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 365 | ** Look at the script prefix in pCmd. We will be executing this script |
| 366 | ** after first appending one or more arguments. This routine analyzes |
| 367 | ** the script to see if it is safe to use Tcl_EvalObjv() on the script |
| 368 | ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much |
| 369 | ** faster. |
| 370 | ** |
| 371 | ** Scripts that are safe to use with Tcl_EvalObjv() consists of a |
| 372 | ** command name followed by zero or more arguments with no [...] or $ |
| 373 | ** or {...} or ; to be seen anywhere. Most callback scripts consist |
| 374 | ** of just a single procedure name and they meet this requirement. |
| 375 | */ |
| 376 | static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ |
| 377 | /* We could try to do something with Tcl_Parse(). But we will instead |
| 378 | ** just do a search for forbidden characters. If any of the forbidden |
| 379 | ** characters appear in pCmd, we will report the string as unsafe. |
| 380 | */ |
| 381 | const char *z; |
| 382 | int n; |
| 383 | z = Tcl_GetStringFromObj(pCmd, &n); |
| 384 | while( n-- > 0 ){ |
| 385 | int c = *(z++); |
| 386 | if( c=='$' || c=='[' || c==';' ) return 0; |
| 387 | } |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | ** Find an SqlFunc structure with the given name. Or create a new |
| 393 | ** one if an existing one cannot be found. Return a pointer to the |
| 394 | ** structure. |
| 395 | */ |
| 396 | static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ |
| 397 | SqlFunc *p, *pNew; |
| 398 | int i; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 399 | pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen30(zName) + 1 ); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 400 | pNew->zName = (char*)&pNew[1]; |
| 401 | for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); } |
| 402 | pNew->zName[i] = 0; |
| 403 | for(p=pDb->pFunc; p; p=p->pNext){ |
| 404 | if( strcmp(p->zName, pNew->zName)==0 ){ |
| 405 | Tcl_Free((char*)pNew); |
| 406 | return p; |
| 407 | } |
| 408 | } |
| 409 | pNew->interp = pDb->interp; |
| 410 | pNew->pScript = 0; |
| 411 | pNew->pNext = pDb->pFunc; |
| 412 | pDb->pFunc = pNew; |
| 413 | return pNew; |
| 414 | } |
| 415 | |
| 416 | /* |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 417 | ** Finalize and free a list of prepared statements |
| 418 | */ |
| 419 | static void flushStmtCache( SqliteDb *pDb ){ |
| 420 | SqlPreparedStmt *pPreStmt; |
| 421 | |
| 422 | while( pDb->stmtList ){ |
| 423 | sqlite3_finalize( pDb->stmtList->pStmt ); |
| 424 | pPreStmt = pDb->stmtList; |
| 425 | pDb->stmtList = pDb->stmtList->pNext; |
| 426 | Tcl_Free( (char*)pPreStmt ); |
| 427 | } |
| 428 | pDb->nStmt = 0; |
| 429 | pDb->stmtLast = 0; |
| 430 | } |
| 431 | |
| 432 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 433 | ** TCL calls this procedure when an sqlite3 database command is |
| 434 | ** deleted. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 435 | */ |
| 436 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 437 | SqliteDb *pDb = (SqliteDb*)db; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 438 | flushStmtCache(pDb); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 439 | closeIncrblobChannels(pDb); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 440 | sqlite3_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 441 | while( pDb->pFunc ){ |
| 442 | SqlFunc *pFunc = pDb->pFunc; |
| 443 | pDb->pFunc = pFunc->pNext; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 444 | Tcl_DecrRefCount(pFunc->pScript); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 445 | Tcl_Free((char*)pFunc); |
| 446 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 447 | while( pDb->pCollate ){ |
| 448 | SqlCollate *pCollate = pDb->pCollate; |
| 449 | pDb->pCollate = pCollate->pNext; |
| 450 | Tcl_Free((char*)pCollate); |
| 451 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 452 | if( pDb->zBusy ){ |
| 453 | Tcl_Free(pDb->zBusy); |
| 454 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 455 | if( pDb->zTrace ){ |
| 456 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 457 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 458 | if( pDb->zProfile ){ |
| 459 | Tcl_Free(pDb->zProfile); |
| 460 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 461 | if( pDb->zAuth ){ |
| 462 | Tcl_Free(pDb->zAuth); |
| 463 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 464 | if( pDb->zNull ){ |
| 465 | Tcl_Free(pDb->zNull); |
| 466 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 467 | if( pDb->pUpdateHook ){ |
| 468 | Tcl_DecrRefCount(pDb->pUpdateHook); |
| 469 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 470 | if( pDb->pRollbackHook ){ |
| 471 | Tcl_DecrRefCount(pDb->pRollbackHook); |
| 472 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 473 | if( pDb->pCollateNeeded ){ |
| 474 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 475 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 476 | Tcl_Free((char*)pDb); |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | ** This routine is called when a database file is locked while trying |
| 481 | ** to execute SQL. |
| 482 | */ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 483 | static int DbBusyHandler(void *cd, int nTries){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 484 | SqliteDb *pDb = (SqliteDb*)cd; |
| 485 | int rc; |
| 486 | char zVal[30]; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 487 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 488 | sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 489 | rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 490 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 491 | return 0; |
| 492 | } |
| 493 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 494 | } |
| 495 | |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 496 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 497 | /* |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 498 | ** This routine is invoked as the 'progress callback' for the database. |
| 499 | */ |
| 500 | static int DbProgressHandler(void *cd){ |
| 501 | SqliteDb *pDb = (SqliteDb*)cd; |
| 502 | int rc; |
| 503 | |
| 504 | assert( pDb->zProgress ); |
| 505 | rc = Tcl_Eval(pDb->interp, pDb->zProgress); |
| 506 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 507 | return 1; |
| 508 | } |
| 509 | return 0; |
| 510 | } |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 511 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 512 | |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 513 | #ifndef SQLITE_OMIT_TRACE |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 514 | /* |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 515 | ** This routine is called by the SQLite trace handler whenever a new |
| 516 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 517 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 518 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 519 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 520 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 521 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 522 | Tcl_DStringInit(&str); |
| 523 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 524 | Tcl_DStringAppendElement(&str, zSql); |
| 525 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 526 | Tcl_DStringFree(&str); |
| 527 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 528 | } |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 529 | #endif |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 530 | |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 531 | #ifndef SQLITE_OMIT_TRACE |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 532 | /* |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 533 | ** This routine is called by the SQLite profile handler after a statement |
| 534 | ** SQL has executed. The TCL script in pDb->zProfile is evaluated. |
| 535 | */ |
| 536 | static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ |
| 537 | SqliteDb *pDb = (SqliteDb*)cd; |
| 538 | Tcl_DString str; |
| 539 | char zTm[100]; |
| 540 | |
| 541 | sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); |
| 542 | Tcl_DStringInit(&str); |
| 543 | Tcl_DStringAppend(&str, pDb->zProfile, -1); |
| 544 | Tcl_DStringAppendElement(&str, zSql); |
| 545 | Tcl_DStringAppendElement(&str, zTm); |
| 546 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 547 | Tcl_DStringFree(&str); |
| 548 | Tcl_ResetResult(pDb->interp); |
| 549 | } |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 550 | #endif |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 551 | |
| 552 | /* |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 553 | ** This routine is called when a transaction is committed. The |
| 554 | ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
| 555 | ** if it throws an exception, the transaction is rolled back instead |
| 556 | ** of being committed. |
| 557 | */ |
| 558 | static int DbCommitHandler(void *cd){ |
| 559 | SqliteDb *pDb = (SqliteDb*)cd; |
| 560 | int rc; |
| 561 | |
| 562 | rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
| 563 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 564 | return 1; |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 569 | static void DbRollbackHandler(void *clientData){ |
| 570 | SqliteDb *pDb = (SqliteDb*)clientData; |
| 571 | assert(pDb->pRollbackHook); |
| 572 | if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ |
| 573 | Tcl_BackgroundError(pDb->interp); |
| 574 | } |
| 575 | } |
| 576 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 577 | static void DbUpdateHandler( |
| 578 | void *p, |
| 579 | int op, |
| 580 | const char *zDb, |
| 581 | const char *zTbl, |
| 582 | sqlite_int64 rowid |
| 583 | ){ |
| 584 | SqliteDb *pDb = (SqliteDb *)p; |
| 585 | Tcl_Obj *pCmd; |
| 586 | |
| 587 | assert( pDb->pUpdateHook ); |
| 588 | assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); |
| 589 | |
| 590 | pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); |
| 591 | Tcl_IncrRefCount(pCmd); |
| 592 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( |
| 593 | ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); |
| 594 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); |
| 595 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); |
| 596 | Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); |
| 597 | Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); |
| 598 | } |
| 599 | |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 600 | static void tclCollateNeeded( |
| 601 | void *pCtx, |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 602 | sqlite3 *db, |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 603 | int enc, |
| 604 | const char *zName |
| 605 | ){ |
| 606 | SqliteDb *pDb = (SqliteDb *)pCtx; |
| 607 | Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); |
| 608 | Tcl_IncrRefCount(pScript); |
| 609 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); |
| 610 | Tcl_EvalObjEx(pDb->interp, pScript, 0); |
| 611 | Tcl_DecrRefCount(pScript); |
| 612 | } |
| 613 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 614 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 615 | ** This routine is called to evaluate an SQL collation function implemented |
| 616 | ** using TCL script. |
| 617 | */ |
| 618 | static int tclSqlCollate( |
| 619 | void *pCtx, |
| 620 | int nA, |
| 621 | const void *zA, |
| 622 | int nB, |
| 623 | const void *zB |
| 624 | ){ |
| 625 | SqlCollate *p = (SqlCollate *)pCtx; |
| 626 | Tcl_Obj *pCmd; |
| 627 | |
| 628 | pCmd = Tcl_NewStringObj(p->zScript, -1); |
| 629 | Tcl_IncrRefCount(pCmd); |
| 630 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); |
| 631 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 632 | Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 633 | Tcl_DecrRefCount(pCmd); |
| 634 | return (atoi(Tcl_GetStringResult(p->interp))); |
| 635 | } |
| 636 | |
| 637 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 638 | ** This routine is called to evaluate an SQL function implemented |
| 639 | ** using TCL script. |
| 640 | */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 641 | static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 642 | SqlFunc *p = sqlite3_user_data(context); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 643 | Tcl_Obj *pCmd; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 644 | int i; |
| 645 | int rc; |
| 646 | |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 647 | if( argc==0 ){ |
| 648 | /* If there are no arguments to the function, call Tcl_EvalObjEx on the |
| 649 | ** script object directly. This allows the TCL compiler to generate |
| 650 | ** bytecode for the command on the first invocation and thus make |
| 651 | ** subsequent invocations much faster. */ |
| 652 | pCmd = p->pScript; |
| 653 | Tcl_IncrRefCount(pCmd); |
| 654 | rc = Tcl_EvalObjEx(p->interp, pCmd, 0); |
| 655 | Tcl_DecrRefCount(pCmd); |
| 656 | }else{ |
| 657 | /* If there are arguments to the function, make a shallow copy of the |
| 658 | ** script object, lappend the arguments, then evaluate the copy. |
| 659 | ** |
| 660 | ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated. |
| 661 | ** The new Tcl_Obj contains pointers to the original list elements. |
| 662 | ** That way, when Tcl_EvalObjv() is run and shimmers the first element |
| 663 | ** of the list to tclCmdNameType, that alternate representation will |
| 664 | ** be preserved and reused on the next invocation. |
| 665 | */ |
| 666 | Tcl_Obj **aArg; |
| 667 | int nArg; |
| 668 | if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ |
| 669 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 670 | return; |
| 671 | } |
| 672 | pCmd = Tcl_NewListObj(nArg, aArg); |
| 673 | Tcl_IncrRefCount(pCmd); |
| 674 | for(i=0; i<argc; i++){ |
| 675 | sqlite3_value *pIn = argv[i]; |
| 676 | Tcl_Obj *pVal; |
| 677 | |
| 678 | /* Set pVal to contain the i'th column of this row. */ |
| 679 | switch( sqlite3_value_type(pIn) ){ |
| 680 | case SQLITE_BLOB: { |
| 681 | int bytes = sqlite3_value_bytes(pIn); |
| 682 | pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); |
| 683 | break; |
| 684 | } |
| 685 | case SQLITE_INTEGER: { |
| 686 | sqlite_int64 v = sqlite3_value_int64(pIn); |
| 687 | if( v>=-2147483647 && v<=2147483647 ){ |
| 688 | pVal = Tcl_NewIntObj(v); |
| 689 | }else{ |
| 690 | pVal = Tcl_NewWideIntObj(v); |
| 691 | } |
| 692 | break; |
| 693 | } |
| 694 | case SQLITE_FLOAT: { |
| 695 | double r = sqlite3_value_double(pIn); |
| 696 | pVal = Tcl_NewDoubleObj(r); |
| 697 | break; |
| 698 | } |
| 699 | case SQLITE_NULL: { |
| 700 | pVal = Tcl_NewStringObj("", 0); |
| 701 | break; |
| 702 | } |
| 703 | default: { |
| 704 | int bytes = sqlite3_value_bytes(pIn); |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 705 | pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | } |
| 709 | rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); |
| 710 | if( rc ){ |
| 711 | Tcl_DecrRefCount(pCmd); |
| 712 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 713 | return; |
| 714 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 715 | } |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 716 | if( !p->useEvalObjv ){ |
| 717 | /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd |
| 718 | ** is a list without a string representation. To prevent this from |
| 719 | ** happening, make sure pCmd has a valid string representation */ |
| 720 | Tcl_GetString(pCmd); |
| 721 | } |
| 722 | rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
| 723 | Tcl_DecrRefCount(pCmd); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 724 | } |
danielk1977 | 562e8d3 | 2005-05-20 09:40:55 +0000 | [diff] [blame] | 725 | |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 726 | if( rc && rc!=TCL_RETURN ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 727 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 728 | }else{ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 729 | Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); |
| 730 | int n; |
| 731 | u8 *data; |
| 732 | char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
| 733 | char c = zType[0]; |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 734 | if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 735 | /* Only return a BLOB type if the Tcl variable is a bytearray and |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 736 | ** has no string representation. */ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 737 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 738 | sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); |
drh | 985e0c6 | 2007-06-26 22:55:37 +0000 | [diff] [blame] | 739 | }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 740 | Tcl_GetIntFromObj(0, pVar, &n); |
| 741 | sqlite3_result_int(context, n); |
| 742 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 743 | double r; |
| 744 | Tcl_GetDoubleFromObj(0, pVar, &r); |
| 745 | sqlite3_result_double(context, r); |
drh | 985e0c6 | 2007-06-26 22:55:37 +0000 | [diff] [blame] | 746 | }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
| 747 | (c=='i' && strcmp(zType,"int")==0) ){ |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 748 | Tcl_WideInt v; |
| 749 | Tcl_GetWideIntFromObj(0, pVar, &v); |
| 750 | sqlite3_result_int64(context, v); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 751 | }else{ |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 752 | data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
| 753 | sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 754 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 757 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 758 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 759 | /* |
| 760 | ** This is the authentication function. It appends the authentication |
| 761 | ** type code and the two arguments to zCmd[] then invokes the result |
| 762 | ** on the interpreter. The reply is examined to determine if the |
| 763 | ** authentication fails or succeeds. |
| 764 | */ |
| 765 | static int auth_callback( |
| 766 | void *pArg, |
| 767 | int code, |
| 768 | const char *zArg1, |
| 769 | const char *zArg2, |
| 770 | const char *zArg3, |
| 771 | const char *zArg4 |
| 772 | ){ |
| 773 | char *zCode; |
| 774 | Tcl_DString str; |
| 775 | int rc; |
| 776 | const char *zReply; |
| 777 | SqliteDb *pDb = (SqliteDb*)pArg; |
drh | 1f1549f | 2008-08-26 21:33:34 +0000 | [diff] [blame] | 778 | if( pDb->disableAuth ) return SQLITE_OK; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 779 | |
| 780 | switch( code ){ |
| 781 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 782 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 783 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 784 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 785 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 786 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 787 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 788 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 789 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 790 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 791 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 792 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 793 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 794 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 795 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 796 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 797 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 798 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 799 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 800 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 801 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 802 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 803 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 804 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
drh | 81e293b | 2003-06-06 19:00:42 +0000 | [diff] [blame] | 805 | case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
| 806 | case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
danielk1977 | 1c8c23c | 2004-11-12 15:53:37 +0000 | [diff] [blame] | 807 | case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 808 | case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 809 | case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 810 | case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; |
| 811 | case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 812 | case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 813 | case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 814 | default : zCode="????"; break; |
| 815 | } |
| 816 | Tcl_DStringInit(&str); |
| 817 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 818 | Tcl_DStringAppendElement(&str, zCode); |
| 819 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 820 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 821 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 822 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 823 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 824 | Tcl_DStringFree(&str); |
| 825 | zReply = Tcl_GetStringResult(pDb->interp); |
| 826 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 827 | rc = SQLITE_OK; |
| 828 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 829 | rc = SQLITE_DENY; |
| 830 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 831 | rc = SQLITE_IGNORE; |
| 832 | }else{ |
| 833 | rc = 999; |
| 834 | } |
| 835 | return rc; |
| 836 | } |
| 837 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 838 | |
| 839 | /* |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 840 | ** zText is a pointer to text obtained via an sqlite3_result_text() |
| 841 | ** or similar interface. This routine returns a Tcl string object, |
| 842 | ** reference count set to 0, containing the text. If a translation |
| 843 | ** between iso8859 and UTF-8 is required, it is preformed. |
| 844 | */ |
| 845 | static Tcl_Obj *dbTextToObj(char const *zText){ |
| 846 | Tcl_Obj *pVal; |
| 847 | #ifdef UTF_TRANSLATION_NEEDED |
| 848 | Tcl_DString dCol; |
| 849 | Tcl_DStringInit(&dCol); |
| 850 | Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); |
| 851 | pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 852 | Tcl_DStringFree(&dCol); |
| 853 | #else |
| 854 | pVal = Tcl_NewStringObj(zText, -1); |
| 855 | #endif |
| 856 | return pVal; |
| 857 | } |
| 858 | |
| 859 | /* |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 860 | ** This routine reads a line of text from FILE in, stores |
| 861 | ** the text in memory obtained from malloc() and returns a pointer |
| 862 | ** to the text. NULL is returned at end of file, or if malloc() |
| 863 | ** fails. |
| 864 | ** |
| 865 | ** The interface is like "readline" but no command-line editing |
| 866 | ** is done. |
| 867 | ** |
| 868 | ** copied from shell.c from '.import' command |
| 869 | */ |
| 870 | static char *local_getline(char *zPrompt, FILE *in){ |
| 871 | char *zLine; |
| 872 | int nLine; |
| 873 | int n; |
| 874 | int eol; |
| 875 | |
| 876 | nLine = 100; |
| 877 | zLine = malloc( nLine ); |
| 878 | if( zLine==0 ) return 0; |
| 879 | n = 0; |
| 880 | eol = 0; |
| 881 | while( !eol ){ |
| 882 | if( n+100>nLine ){ |
| 883 | nLine = nLine*2 + 100; |
| 884 | zLine = realloc(zLine, nLine); |
| 885 | if( zLine==0 ) return 0; |
| 886 | } |
| 887 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 888 | if( n==0 ){ |
| 889 | free(zLine); |
| 890 | return 0; |
| 891 | } |
| 892 | zLine[n] = 0; |
| 893 | eol = 1; |
| 894 | break; |
| 895 | } |
| 896 | while( zLine[n] ){ n++; } |
| 897 | if( n>0 && zLine[n-1]=='\n' ){ |
| 898 | n--; |
| 899 | zLine[n] = 0; |
| 900 | eol = 1; |
| 901 | } |
| 902 | } |
| 903 | zLine = realloc( zLine, n+1 ); |
| 904 | return zLine; |
| 905 | } |
| 906 | |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | ** Figure out the column names for the data returned by the statement |
| 910 | ** passed as the second argument. |
| 911 | ** |
| 912 | ** If parameter papColName is not NULL, then *papColName is set to point |
| 913 | ** at an array allocated using Tcl_Alloc(). It is the callers responsibility |
| 914 | ** to free this array using Tcl_Free(), and to decrement the reference |
| 915 | ** count of each Tcl_Obj* member of the array. |
| 916 | ** |
| 917 | ** The return value of this function is the number of columns of data |
| 918 | ** returned by pStmt (and hence the size of the *papColName array). |
| 919 | ** |
| 920 | ** If pArray is not NULL, then it contains the name of a Tcl array |
| 921 | ** variable. The "*" member of this array is set to a list containing |
| 922 | ** the names of the columns returned by the statement, in order from |
| 923 | ** left to right. e.g. if the names of the returned columns are a, b and |
| 924 | ** c, it does the equivalent of the tcl command: |
| 925 | ** |
| 926 | ** set ${pArray}(*) {a b c} |
| 927 | */ |
| 928 | static int |
| 929 | computeColumnNames( |
| 930 | Tcl_Interp *interp, |
| 931 | sqlite3_stmt *pStmt, /* SQL statement */ |
| 932 | Tcl_Obj ***papColName, /* OUT: Array of column names */ |
| 933 | Tcl_Obj *pArray /* Name of array variable (may be null) */ |
| 934 | ){ |
| 935 | int nCol; |
| 936 | |
| 937 | /* Compute column names */ |
| 938 | nCol = sqlite3_column_count(pStmt); |
| 939 | if( papColName ){ |
| 940 | int i; |
| 941 | Tcl_Obj **apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
| 942 | for(i=0; i<nCol; i++){ |
| 943 | apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
| 944 | Tcl_IncrRefCount(apColName[i]); |
| 945 | } |
| 946 | |
| 947 | /* If results are being stored in an array variable, then create |
| 948 | ** the array(*) entry for that array |
| 949 | */ |
| 950 | if( pArray ){ |
| 951 | Tcl_Obj *pColList = Tcl_NewObj(); |
| 952 | Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); |
| 953 | Tcl_IncrRefCount(pColList); |
| 954 | for(i=0; i<nCol; i++){ |
| 955 | Tcl_ListObjAppendElement(interp, pColList, apColName[i]); |
| 956 | } |
| 957 | Tcl_IncrRefCount(pStar); |
| 958 | Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0); |
| 959 | Tcl_DecrRefCount(pColList); |
| 960 | Tcl_DecrRefCount(pStar); |
| 961 | } |
| 962 | *papColName = apColName; |
| 963 | } |
| 964 | |
| 965 | return nCol; |
| 966 | } |
| 967 | |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 968 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 969 | ** The "sqlite" command below creates a new Tcl command for each |
| 970 | ** connection it opens to an SQLite database. This routine is invoked |
| 971 | ** whenever one of those connection-specific commands is executed |
| 972 | ** in Tcl. For example, if you run Tcl code like this: |
| 973 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 974 | ** sqlite3 db1 "my_database" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 975 | ** db1 close |
| 976 | ** |
| 977 | ** The first command opens a connection to the "my_database" database |
| 978 | ** and calls that connection "db1". The second command causes this |
| 979 | ** subroutine to be invoked. |
| 980 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 981 | static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 982 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 983 | int choice; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 984 | int rc = TCL_OK; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 985 | static const char *DB_strs[] = { |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 986 | "authorizer", "busy", "cache", |
| 987 | "changes", "close", "collate", |
| 988 | "collation_needed", "commit_hook", "complete", |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 989 | "copy", "enable_load_extension","errorcode", |
| 990 | "eval", "exists", "function", |
drh | 59fffd0 | 2007-06-19 17:48:57 +0000 | [diff] [blame] | 991 | "incrblob", "interrupt", "last_insert_rowid", |
| 992 | "nullvalue", "onecolumn", "profile", |
| 993 | "progress", "rekey", "rollback_hook", |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 994 | "status", "timeout", "total_changes", |
| 995 | "trace", "transaction", "update_hook", |
| 996 | "version", 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 997 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 998 | enum DB_enum { |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 999 | DB_AUTHORIZER, DB_BUSY, DB_CACHE, |
| 1000 | DB_CHANGES, DB_CLOSE, DB_COLLATE, |
| 1001 | DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE, |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 1002 | DB_COPY, DB_ENABLE_LOAD_EXTENSION,DB_ERRORCODE, |
| 1003 | DB_EVAL, DB_EXISTS, DB_FUNCTION, |
drh | 59fffd0 | 2007-06-19 17:48:57 +0000 | [diff] [blame] | 1004 | DB_INCRBLOB, DB_INTERRUPT, DB_LAST_INSERT_ROWID, |
| 1005 | DB_NULLVALUE, DB_ONECOLUMN, DB_PROFILE, |
| 1006 | DB_PROGRESS, DB_REKEY, DB_ROLLBACK_HOOK, |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 1007 | DB_STATUS, DB_TIMEOUT, DB_TOTAL_CHANGES, |
| 1008 | DB_TRACE, DB_TRANSACTION, DB_UPDATE_HOOK, |
| 1009 | DB_VERSION |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1010 | }; |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 1011 | /* 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] | 1012 | |
| 1013 | if( objc<2 ){ |
| 1014 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1015 | return TCL_ERROR; |
| 1016 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 1017 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1018 | return TCL_ERROR; |
| 1019 | } |
| 1020 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 1021 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1022 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1023 | /* $db authorizer ?CALLBACK? |
| 1024 | ** |
| 1025 | ** Invoke the given callback to authorize each SQL operation as it is |
| 1026 | ** compiled. 5 arguments are appended to the callback before it is |
| 1027 | ** invoked: |
| 1028 | ** |
| 1029 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 1030 | ** (2) First descriptive name (depends on authorization type) |
| 1031 | ** (3) Second descriptive name |
| 1032 | ** (4) Name of the database (ex: "main", "temp") |
| 1033 | ** (5) Name of trigger that is doing the access |
| 1034 | ** |
| 1035 | ** The callback should return on of the following strings: SQLITE_OK, |
| 1036 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 1037 | ** |
| 1038 | ** If this method is invoked with no arguments, the current authorization |
| 1039 | ** callback string is returned. |
| 1040 | */ |
| 1041 | case DB_AUTHORIZER: { |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 1042 | #ifdef SQLITE_OMIT_AUTHORIZATION |
| 1043 | Tcl_AppendResult(interp, "authorization not available in this build", 0); |
| 1044 | return TCL_ERROR; |
| 1045 | #else |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1046 | if( objc>3 ){ |
| 1047 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1048 | return TCL_ERROR; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1049 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1050 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1051 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 1052 | } |
| 1053 | }else{ |
| 1054 | char *zAuth; |
| 1055 | int len; |
| 1056 | if( pDb->zAuth ){ |
| 1057 | Tcl_Free(pDb->zAuth); |
| 1058 | } |
| 1059 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 1060 | if( zAuth && len>0 ){ |
| 1061 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1062 | memcpy(pDb->zAuth, zAuth, len+1); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1063 | }else{ |
| 1064 | pDb->zAuth = 0; |
| 1065 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1066 | if( pDb->zAuth ){ |
| 1067 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1068 | sqlite3_set_authorizer(pDb->db, auth_callback, pDb); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1069 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1070 | sqlite3_set_authorizer(pDb->db, 0, 0); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1071 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1072 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 1073 | #endif |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1074 | break; |
| 1075 | } |
| 1076 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1077 | /* $db busy ?CALLBACK? |
| 1078 | ** |
| 1079 | ** Invoke the given callback if an SQL statement attempts to open |
| 1080 | ** a locked database file. |
| 1081 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1082 | case DB_BUSY: { |
| 1083 | if( objc>3 ){ |
| 1084 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1085 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1086 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1087 | if( pDb->zBusy ){ |
| 1088 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 1089 | } |
| 1090 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1091 | char *zBusy; |
| 1092 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1093 | if( pDb->zBusy ){ |
| 1094 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1095 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1096 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 1097 | if( zBusy && len>0 ){ |
| 1098 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1099 | memcpy(pDb->zBusy, zBusy, len+1); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1100 | }else{ |
| 1101 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1102 | } |
| 1103 | if( pDb->zBusy ){ |
| 1104 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1105 | sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1106 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1107 | sqlite3_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1110 | break; |
| 1111 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1112 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1113 | /* $db cache flush |
| 1114 | ** $db cache size n |
| 1115 | ** |
| 1116 | ** Flush the prepared statement cache, or set the maximum number of |
| 1117 | ** cached statements. |
| 1118 | */ |
| 1119 | case DB_CACHE: { |
| 1120 | char *subCmd; |
| 1121 | int n; |
| 1122 | |
| 1123 | if( objc<=2 ){ |
| 1124 | Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); |
| 1125 | return TCL_ERROR; |
| 1126 | } |
| 1127 | subCmd = Tcl_GetStringFromObj( objv[2], 0 ); |
| 1128 | if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ |
| 1129 | if( objc!=3 ){ |
| 1130 | Tcl_WrongNumArgs(interp, 2, objv, "flush"); |
| 1131 | return TCL_ERROR; |
| 1132 | }else{ |
| 1133 | flushStmtCache( pDb ); |
| 1134 | } |
| 1135 | }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ |
| 1136 | if( objc!=4 ){ |
| 1137 | Tcl_WrongNumArgs(interp, 2, objv, "size n"); |
| 1138 | return TCL_ERROR; |
| 1139 | }else{ |
| 1140 | if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ |
| 1141 | Tcl_AppendResult( interp, "cannot convert \"", |
| 1142 | Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0); |
| 1143 | return TCL_ERROR; |
| 1144 | }else{ |
| 1145 | if( n<0 ){ |
| 1146 | flushStmtCache( pDb ); |
| 1147 | n = 0; |
| 1148 | }else if( n>MAX_PREPARED_STMTS ){ |
| 1149 | n = MAX_PREPARED_STMTS; |
| 1150 | } |
| 1151 | pDb->maxStmt = n; |
| 1152 | } |
| 1153 | } |
| 1154 | }else{ |
| 1155 | Tcl_AppendResult( interp, "bad option \"", |
danielk1977 | 191fadc | 2007-10-23 08:17:48 +0000 | [diff] [blame] | 1156 | Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1157 | return TCL_ERROR; |
| 1158 | } |
| 1159 | break; |
| 1160 | } |
| 1161 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1162 | /* $db changes |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1163 | ** |
| 1164 | ** Return the number of rows that were modified, inserted, or deleted by |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1165 | ** the most recent INSERT, UPDATE or DELETE statement, not including |
| 1166 | ** any changes made by trigger programs. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1167 | */ |
| 1168 | case DB_CHANGES: { |
| 1169 | Tcl_Obj *pResult; |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1170 | if( objc!=2 ){ |
| 1171 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 1172 | return TCL_ERROR; |
| 1173 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 1174 | pResult = Tcl_GetObjResult(interp); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1175 | Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 1176 | break; |
| 1177 | } |
| 1178 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1179 | /* $db close |
| 1180 | ** |
| 1181 | ** Shutdown the database |
| 1182 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1183 | case DB_CLOSE: { |
| 1184 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 1185 | break; |
| 1186 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1187 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1188 | /* |
| 1189 | ** $db collate NAME SCRIPT |
| 1190 | ** |
| 1191 | ** Create a new SQL collation function called NAME. Whenever |
| 1192 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 1193 | */ |
| 1194 | case DB_COLLATE: { |
| 1195 | SqlCollate *pCollate; |
| 1196 | char *zName; |
| 1197 | char *zScript; |
| 1198 | int nScript; |
| 1199 | if( objc!=4 ){ |
| 1200 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 1201 | return TCL_ERROR; |
| 1202 | } |
| 1203 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 1204 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 1205 | pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); |
| 1206 | if( pCollate==0 ) return TCL_ERROR; |
| 1207 | pCollate->interp = interp; |
| 1208 | pCollate->pNext = pDb->pCollate; |
| 1209 | pCollate->zScript = (char*)&pCollate[1]; |
| 1210 | pDb->pCollate = pCollate; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1211 | memcpy(pCollate->zScript, zScript, nScript+1); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1212 | if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, |
| 1213 | pCollate, tclSqlCollate) ){ |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1214 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1215 | return TCL_ERROR; |
| 1216 | } |
| 1217 | break; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | ** $db collation_needed SCRIPT |
| 1222 | ** |
| 1223 | ** Create a new SQL collation function called NAME. Whenever |
| 1224 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 1225 | */ |
| 1226 | case DB_COLLATION_NEEDED: { |
| 1227 | if( objc!=3 ){ |
| 1228 | Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); |
| 1229 | return TCL_ERROR; |
| 1230 | } |
| 1231 | if( pDb->pCollateNeeded ){ |
| 1232 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 1233 | } |
| 1234 | pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); |
| 1235 | Tcl_IncrRefCount(pDb->pCollateNeeded); |
| 1236 | sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); |
| 1237 | break; |
| 1238 | } |
| 1239 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1240 | /* $db commit_hook ?CALLBACK? |
| 1241 | ** |
| 1242 | ** Invoke the given callback just before committing every SQL transaction. |
| 1243 | ** If the callback throws an exception or returns non-zero, then the |
| 1244 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 1245 | ** is disabled. |
| 1246 | */ |
| 1247 | case DB_COMMIT_HOOK: { |
| 1248 | if( objc>3 ){ |
| 1249 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 1250 | return TCL_ERROR; |
| 1251 | }else if( objc==2 ){ |
| 1252 | if( pDb->zCommit ){ |
| 1253 | Tcl_AppendResult(interp, pDb->zCommit, 0); |
| 1254 | } |
| 1255 | }else{ |
| 1256 | char *zCommit; |
| 1257 | int len; |
| 1258 | if( pDb->zCommit ){ |
| 1259 | Tcl_Free(pDb->zCommit); |
| 1260 | } |
| 1261 | zCommit = Tcl_GetStringFromObj(objv[2], &len); |
| 1262 | if( zCommit && len>0 ){ |
| 1263 | pDb->zCommit = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1264 | memcpy(pDb->zCommit, zCommit, len+1); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1265 | }else{ |
| 1266 | pDb->zCommit = 0; |
| 1267 | } |
| 1268 | if( pDb->zCommit ){ |
| 1269 | pDb->interp = interp; |
| 1270 | sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); |
| 1271 | }else{ |
| 1272 | sqlite3_commit_hook(pDb->db, 0, 0); |
| 1273 | } |
| 1274 | } |
| 1275 | break; |
| 1276 | } |
| 1277 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1278 | /* $db complete SQL |
| 1279 | ** |
| 1280 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 1281 | ** additional lines of input are needed. This is similar to the |
| 1282 | ** built-in "info complete" command of Tcl. |
| 1283 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1284 | case DB_COMPLETE: { |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 1285 | #ifndef SQLITE_OMIT_COMPLETE |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1286 | Tcl_Obj *pResult; |
| 1287 | int isComplete; |
| 1288 | if( objc!=3 ){ |
| 1289 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1290 | return TCL_ERROR; |
| 1291 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1292 | isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1293 | pResult = Tcl_GetObjResult(interp); |
| 1294 | Tcl_SetBooleanObj(pResult, isComplete); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 1295 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1296 | break; |
| 1297 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1298 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1299 | /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? |
| 1300 | ** |
| 1301 | ** Copy data into table from filename, optionally using SEPARATOR |
| 1302 | ** as column separators. If a column contains a null string, or the |
| 1303 | ** value of NULLINDICATOR, a NULL is inserted for the column. |
| 1304 | ** conflict-algorithm is one of the sqlite conflict algorithms: |
| 1305 | ** rollback, abort, fail, ignore, replace |
| 1306 | ** On success, return the number of lines processed, not necessarily same |
| 1307 | ** as 'db changes' due to conflict-algorithm selected. |
| 1308 | ** |
| 1309 | ** This code is basically an implementation/enhancement of |
| 1310 | ** the sqlite3 shell.c ".import" command. |
| 1311 | ** |
| 1312 | ** This command usage is equivalent to the sqlite2.x COPY statement, |
| 1313 | ** which imports file data into a table using the PostgreSQL COPY file format: |
| 1314 | ** $db copy $conflit_algo $table_name $filename \t \\N |
| 1315 | */ |
| 1316 | case DB_COPY: { |
| 1317 | char *zTable; /* Insert data into this table */ |
| 1318 | char *zFile; /* The file from which to extract data */ |
| 1319 | char *zConflict; /* The conflict algorithm to use */ |
| 1320 | sqlite3_stmt *pStmt; /* A statement */ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1321 | int nCol; /* Number of columns in the table */ |
| 1322 | int nByte; /* Number of bytes in an SQL string */ |
| 1323 | int i, j; /* Loop counters */ |
| 1324 | int nSep; /* Number of bytes in zSep[] */ |
| 1325 | int nNull; /* Number of bytes in zNull[] */ |
| 1326 | char *zSql; /* An SQL statement */ |
| 1327 | char *zLine; /* A single line of input from the file */ |
| 1328 | char **azCol; /* zLine[] broken up into columns */ |
| 1329 | char *zCommit; /* How to commit changes */ |
| 1330 | FILE *in; /* The input file */ |
| 1331 | int lineno = 0; /* Line number of input file */ |
| 1332 | char zLineNum[80]; /* Line number print buffer */ |
| 1333 | Tcl_Obj *pResult; /* interp result */ |
| 1334 | |
| 1335 | char *zSep; |
| 1336 | char *zNull; |
| 1337 | if( objc<5 || objc>7 ){ |
| 1338 | Tcl_WrongNumArgs(interp, 2, objv, |
| 1339 | "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); |
| 1340 | return TCL_ERROR; |
| 1341 | } |
| 1342 | if( objc>=6 ){ |
| 1343 | zSep = Tcl_GetStringFromObj(objv[5], 0); |
| 1344 | }else{ |
| 1345 | zSep = "\t"; |
| 1346 | } |
| 1347 | if( objc>=7 ){ |
| 1348 | zNull = Tcl_GetStringFromObj(objv[6], 0); |
| 1349 | }else{ |
| 1350 | zNull = ""; |
| 1351 | } |
| 1352 | zConflict = Tcl_GetStringFromObj(objv[2], 0); |
| 1353 | zTable = Tcl_GetStringFromObj(objv[3], 0); |
| 1354 | zFile = Tcl_GetStringFromObj(objv[4], 0); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1355 | nSep = strlen30(zSep); |
| 1356 | nNull = strlen30(zNull); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1357 | if( nSep==0 ){ |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 1358 | Tcl_AppendResult(interp,"Error: non-null separator required for copy",0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1359 | return TCL_ERROR; |
| 1360 | } |
drh | 3e59c01 | 2008-09-23 10:12:13 +0000 | [diff] [blame] | 1361 | if(strcmp(zConflict, "rollback") != 0 && |
| 1362 | strcmp(zConflict, "abort" ) != 0 && |
| 1363 | strcmp(zConflict, "fail" ) != 0 && |
| 1364 | strcmp(zConflict, "ignore" ) != 0 && |
| 1365 | strcmp(zConflict, "replace" ) != 0 ) { |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1366 | Tcl_AppendResult(interp, "Error: \"", zConflict, |
| 1367 | "\", conflict-algorithm must be one of: rollback, " |
| 1368 | "abort, fail, ignore, or replace", 0); |
| 1369 | return TCL_ERROR; |
| 1370 | } |
| 1371 | zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); |
| 1372 | if( zSql==0 ){ |
| 1373 | Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0); |
| 1374 | return TCL_ERROR; |
| 1375 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1376 | nByte = strlen30(zSql); |
drh | 3e701a1 | 2007-02-01 01:53:44 +0000 | [diff] [blame] | 1377 | rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1378 | sqlite3_free(zSql); |
| 1379 | if( rc ){ |
| 1380 | Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
| 1381 | nCol = 0; |
| 1382 | }else{ |
| 1383 | nCol = sqlite3_column_count(pStmt); |
| 1384 | } |
| 1385 | sqlite3_finalize(pStmt); |
| 1386 | if( nCol==0 ) { |
| 1387 | return TCL_ERROR; |
| 1388 | } |
| 1389 | zSql = malloc( nByte + 50 + nCol*2 ); |
| 1390 | if( zSql==0 ) { |
| 1391 | Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
| 1392 | return TCL_ERROR; |
| 1393 | } |
| 1394 | sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", |
| 1395 | zConflict, zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1396 | j = strlen30(zSql); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1397 | for(i=1; i<nCol; i++){ |
| 1398 | zSql[j++] = ','; |
| 1399 | zSql[j++] = '?'; |
| 1400 | } |
| 1401 | zSql[j++] = ')'; |
| 1402 | zSql[j] = 0; |
drh | 3e701a1 | 2007-02-01 01:53:44 +0000 | [diff] [blame] | 1403 | rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1404 | free(zSql); |
| 1405 | if( rc ){ |
| 1406 | Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
| 1407 | sqlite3_finalize(pStmt); |
| 1408 | return TCL_ERROR; |
| 1409 | } |
| 1410 | in = fopen(zFile, "rb"); |
| 1411 | if( in==0 ){ |
| 1412 | Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); |
| 1413 | sqlite3_finalize(pStmt); |
| 1414 | return TCL_ERROR; |
| 1415 | } |
| 1416 | azCol = malloc( sizeof(azCol[0])*(nCol+1) ); |
| 1417 | if( azCol==0 ) { |
| 1418 | Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1419 | fclose(in); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1420 | return TCL_ERROR; |
| 1421 | } |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 1422 | (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1423 | zCommit = "COMMIT"; |
| 1424 | while( (zLine = local_getline(0, in))!=0 ){ |
| 1425 | char *z; |
| 1426 | i = 0; |
| 1427 | lineno++; |
| 1428 | azCol[0] = zLine; |
| 1429 | for(i=0, z=zLine; *z; z++){ |
| 1430 | if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ |
| 1431 | *z = 0; |
| 1432 | i++; |
| 1433 | if( i<nCol ){ |
| 1434 | azCol[i] = &z[nSep]; |
| 1435 | z += nSep-1; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | if( i+1!=nCol ){ |
| 1440 | char *zErr; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1441 | int nErr = strlen30(zFile) + 200; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1442 | zErr = malloc(nErr); |
drh | c1f4494 | 2006-05-10 14:39:13 +0000 | [diff] [blame] | 1443 | if( zErr ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1444 | sqlite3_snprintf(nErr, zErr, |
drh | c1f4494 | 2006-05-10 14:39:13 +0000 | [diff] [blame] | 1445 | "Error: %s line %d: expected %d columns of data but found %d", |
| 1446 | zFile, lineno, nCol, i+1); |
| 1447 | Tcl_AppendResult(interp, zErr, 0); |
| 1448 | free(zErr); |
| 1449 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1450 | zCommit = "ROLLBACK"; |
| 1451 | break; |
| 1452 | } |
| 1453 | for(i=0; i<nCol; i++){ |
| 1454 | /* check for null data, if so, bind as null */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1455 | if( (nNull>0 && strcmp(azCol[i], zNull)==0) |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1456 | || strlen30(azCol[i])==0 |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1457 | ){ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1458 | sqlite3_bind_null(pStmt, i+1); |
| 1459 | }else{ |
| 1460 | sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
| 1461 | } |
| 1462 | } |
| 1463 | sqlite3_step(pStmt); |
| 1464 | rc = sqlite3_reset(pStmt); |
| 1465 | free(zLine); |
| 1466 | if( rc!=SQLITE_OK ){ |
| 1467 | Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0); |
| 1468 | zCommit = "ROLLBACK"; |
| 1469 | break; |
| 1470 | } |
| 1471 | } |
| 1472 | free(azCol); |
| 1473 | fclose(in); |
| 1474 | sqlite3_finalize(pStmt); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 1475 | (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1476 | |
| 1477 | if( zCommit[0] == 'C' ){ |
| 1478 | /* success, set result as number of lines processed */ |
| 1479 | pResult = Tcl_GetObjResult(interp); |
| 1480 | Tcl_SetIntObj(pResult, lineno); |
| 1481 | rc = TCL_OK; |
| 1482 | }else{ |
| 1483 | /* failure, append lineno where failed */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1484 | sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1485 | Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0); |
| 1486 | rc = TCL_ERROR; |
| 1487 | } |
| 1488 | break; |
| 1489 | } |
| 1490 | |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1491 | /* |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 1492 | ** $db enable_load_extension BOOLEAN |
| 1493 | ** |
| 1494 | ** Turn the extension loading feature on or off. It if off by |
| 1495 | ** default. |
| 1496 | */ |
| 1497 | case DB_ENABLE_LOAD_EXTENSION: { |
drh | f533acc | 2006-12-19 18:57:11 +0000 | [diff] [blame] | 1498 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 1499 | int onoff; |
| 1500 | if( objc!=3 ){ |
| 1501 | Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); |
| 1502 | return TCL_ERROR; |
| 1503 | } |
| 1504 | if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
| 1505 | return TCL_ERROR; |
| 1506 | } |
| 1507 | sqlite3_enable_load_extension(pDb->db, onoff); |
| 1508 | break; |
drh | f533acc | 2006-12-19 18:57:11 +0000 | [diff] [blame] | 1509 | #else |
| 1510 | Tcl_AppendResult(interp, "extension loading is turned off at compile-time", |
| 1511 | 0); |
| 1512 | return TCL_ERROR; |
| 1513 | #endif |
drh | 4144905 | 2006-07-06 17:08:48 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /* |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1517 | ** $db errorcode |
| 1518 | ** |
| 1519 | ** Return the numeric error code that was returned by the most recent |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1520 | ** call to sqlite3_exec(). |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1521 | */ |
| 1522 | case DB_ERRORCODE: { |
danielk1977 | f3ce83f | 2004-06-14 11:43:46 +0000 | [diff] [blame] | 1523 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1526 | |
| 1527 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1528 | ** $db eval $sql ?array? ?{ ...code... }? |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1529 | ** $db onecolumn $sql |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1530 | ** |
| 1531 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1532 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1533 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 1534 | ** If "array" is an empty string, then the values are placed in variables |
| 1535 | ** that have the same name as the fields extracted by the query. |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1536 | ** |
| 1537 | ** The onecolumn method is the equivalent of: |
| 1538 | ** lindex [$db eval $sql] 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1539 | */ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1540 | case DB_ONECOLUMN: |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1541 | case DB_EVAL: |
| 1542 | case DB_EXISTS: { |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 1543 | char const *zSql; /* Next SQL statement to execute */ |
| 1544 | char const *zLeft; /* What is left after first stmt in zSql */ |
| 1545 | sqlite3_stmt *pStmt; /* Compiled SQL statment */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1546 | Tcl_Obj *pArray; /* Name of array into which results are written */ |
| 1547 | Tcl_Obj *pScript; /* Script to run for each result set */ |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1548 | Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */ |
| 1549 | int nParm; /* Number of entries used in apParm[] */ |
| 1550 | Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1551 | Tcl_Obj *pRet; /* Value to be returned */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1552 | SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */ |
| 1553 | int rc2; |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 1554 | |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1555 | if( choice==DB_EVAL ){ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1556 | if( objc<3 || objc>5 ){ |
| 1557 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
| 1558 | return TCL_ERROR; |
| 1559 | } |
| 1560 | pRet = Tcl_NewObj(); |
| 1561 | Tcl_IncrRefCount(pRet); |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1562 | }else{ |
| 1563 | if( objc!=3 ){ |
| 1564 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
| 1565 | return TCL_ERROR; |
| 1566 | } |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1567 | if( choice==DB_EXISTS ){ |
drh | 446a9b8 | 2006-01-04 18:13:26 +0000 | [diff] [blame] | 1568 | pRet = Tcl_NewBooleanObj(0); |
| 1569 | Tcl_IncrRefCount(pRet); |
| 1570 | }else{ |
| 1571 | pRet = 0; |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1572 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1573 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1574 | if( objc==3 ){ |
| 1575 | pArray = pScript = 0; |
| 1576 | }else if( objc==4 ){ |
| 1577 | pArray = 0; |
| 1578 | pScript = objv[3]; |
| 1579 | }else{ |
| 1580 | pArray = objv[3]; |
| 1581 | if( Tcl_GetString(pArray)[0]==0 ) pArray = 0; |
| 1582 | pScript = objv[4]; |
| 1583 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1584 | |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1585 | Tcl_IncrRefCount(objv[2]); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1586 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1587 | while( rc==TCL_OK && zSql[0] ){ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1588 | int i; /* Loop counter */ |
| 1589 | int nVar; /* Number of bind parameters in the pStmt */ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1590 | int nCol = -1; /* Number of columns in the result set */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1591 | Tcl_Obj **apColName = 0; /* Array of column names */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1592 | int len; /* String length of zSql */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1593 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1594 | /* Try to find a SQL statement that has already been compiled and |
| 1595 | ** which matches the next sequence of SQL. |
| 1596 | */ |
| 1597 | pStmt = 0; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1598 | len = strlen30(zSql); |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1599 | for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1600 | int n = pPreStmt->nSql; |
| 1601 | if( len>=n |
| 1602 | && memcmp(pPreStmt->zSql, zSql, n)==0 |
| 1603 | && (zSql[n]==0 || zSql[n-1]==';') |
| 1604 | ){ |
| 1605 | pStmt = pPreStmt->pStmt; |
| 1606 | zLeft = &zSql[pPreStmt->nSql]; |
| 1607 | |
| 1608 | /* When a prepared statement is found, unlink it from the |
| 1609 | ** cache list. It will later be added back to the beginning |
| 1610 | ** of the cache list in order to implement LRU replacement. |
| 1611 | */ |
| 1612 | if( pPreStmt->pPrev ){ |
| 1613 | pPreStmt->pPrev->pNext = pPreStmt->pNext; |
| 1614 | }else{ |
| 1615 | pDb->stmtList = pPreStmt->pNext; |
| 1616 | } |
| 1617 | if( pPreStmt->pNext ){ |
| 1618 | pPreStmt->pNext->pPrev = pPreStmt->pPrev; |
| 1619 | }else{ |
| 1620 | pDb->stmtLast = pPreStmt->pPrev; |
| 1621 | } |
| 1622 | pDb->nStmt--; |
| 1623 | break; |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | /* If no prepared statement was found. Compile the SQL text |
| 1628 | */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1629 | if( pStmt==0 ){ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1630 | if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, &zLeft) ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1631 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 1632 | rc = TCL_ERROR; |
| 1633 | break; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1634 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1635 | if( pStmt==0 ){ |
| 1636 | if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
| 1637 | /* A compile-time error in the statement |
| 1638 | */ |
| 1639 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 1640 | rc = TCL_ERROR; |
| 1641 | break; |
| 1642 | }else{ |
| 1643 | /* The statement was a no-op. Continue to the next statement |
| 1644 | ** in the SQL string. |
| 1645 | */ |
| 1646 | zSql = zLeft; |
| 1647 | continue; |
| 1648 | } |
| 1649 | } |
| 1650 | assert( pPreStmt==0 ); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1653 | /* Bind values to parameters that begin with $ or : |
| 1654 | */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1655 | nVar = sqlite3_bind_parameter_count(pStmt); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1656 | nParm = 0; |
| 1657 | if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){ |
| 1658 | apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0])); |
| 1659 | }else{ |
| 1660 | apParm = aParm; |
| 1661 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1662 | for(i=1; i<=nVar; i++){ |
| 1663 | const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
drh | 4f5e80f | 2007-06-19 17:15:46 +0000 | [diff] [blame] | 1664 | if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1665 | Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
| 1666 | if( pVar ){ |
| 1667 | int n; |
| 1668 | u8 *data; |
| 1669 | char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
| 1670 | char c = zType[0]; |
drh | 1c74781 | 2007-06-19 23:01:41 +0000 | [diff] [blame] | 1671 | if( zVar[0]=='@' || |
| 1672 | (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ |
| 1673 | /* Load a BLOB type if the Tcl variable is a bytearray and |
| 1674 | ** it has no string representation or the host |
drh | 4f5e80f | 2007-06-19 17:15:46 +0000 | [diff] [blame] | 1675 | ** parameter name begins with "@". */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1676 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 1677 | sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1678 | Tcl_IncrRefCount(pVar); |
| 1679 | apParm[nParm++] = pVar; |
drh | 985e0c6 | 2007-06-26 22:55:37 +0000 | [diff] [blame] | 1680 | }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1681 | Tcl_GetIntFromObj(interp, pVar, &n); |
| 1682 | sqlite3_bind_int(pStmt, i, n); |
| 1683 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 1684 | double r; |
| 1685 | Tcl_GetDoubleFromObj(interp, pVar, &r); |
| 1686 | sqlite3_bind_double(pStmt, i, r); |
drh | 985e0c6 | 2007-06-26 22:55:37 +0000 | [diff] [blame] | 1687 | }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
| 1688 | (c=='i' && strcmp(zType,"int")==0) ){ |
drh | df0bdda | 2005-06-25 19:31:48 +0000 | [diff] [blame] | 1689 | Tcl_WideInt v; |
| 1690 | Tcl_GetWideIntFromObj(interp, pVar, &v); |
| 1691 | sqlite3_bind_int64(pStmt, i, v); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1692 | }else{ |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 1693 | data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
drh | b08c2a7 | 2008-04-16 00:28:13 +0000 | [diff] [blame] | 1694 | sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1695 | Tcl_IncrRefCount(pVar); |
| 1696 | apParm[nParm++] = pVar; |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1697 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1698 | }else{ |
| 1699 | sqlite3_bind_null( pStmt, i ); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1703 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1704 | /* Execute the SQL |
| 1705 | */ |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1706 | while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1707 | |
| 1708 | /* Compute column names. This must be done after the first successful |
| 1709 | ** call to sqlite3_step(), in case the query is recompiled and the |
| 1710 | ** number or names of the returned columns changes. |
| 1711 | */ |
| 1712 | assert(!pArray||pScript); |
| 1713 | if (nCol < 0) { |
| 1714 | Tcl_Obj ***ap = (pScript?&apColName:0); |
| 1715 | nCol = computeColumnNames(interp, pStmt, ap, pArray); |
| 1716 | } |
| 1717 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1718 | for(i=0; i<nCol; i++){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1719 | Tcl_Obj *pVal; |
| 1720 | |
| 1721 | /* Set pVal to contain the i'th column of this row. */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1722 | switch( sqlite3_column_type(pStmt, i) ){ |
| 1723 | case SQLITE_BLOB: { |
| 1724 | int bytes = sqlite3_column_bytes(pStmt, i); |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 1725 | const char *zBlob = sqlite3_column_blob(pStmt, i); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1726 | if( !zBlob ) bytes = 0; |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 1727 | pVal = Tcl_NewByteArrayObj((u8*)zBlob, bytes); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1728 | break; |
| 1729 | } |
| 1730 | case SQLITE_INTEGER: { |
| 1731 | sqlite_int64 v = sqlite3_column_int64(pStmt, i); |
| 1732 | if( v>=-2147483647 && v<=2147483647 ){ |
| 1733 | pVal = Tcl_NewIntObj(v); |
| 1734 | }else{ |
| 1735 | pVal = Tcl_NewWideIntObj(v); |
| 1736 | } |
| 1737 | break; |
| 1738 | } |
| 1739 | case SQLITE_FLOAT: { |
| 1740 | double r = sqlite3_column_double(pStmt, i); |
| 1741 | pVal = Tcl_NewDoubleObj(r); |
| 1742 | break; |
| 1743 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 1744 | case SQLITE_NULL: { |
| 1745 | pVal = dbTextToObj(pDb->zNull); |
| 1746 | break; |
| 1747 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1748 | default: { |
danielk1977 | 00fd957 | 2005-12-07 06:27:43 +0000 | [diff] [blame] | 1749 | pVal = dbTextToObj((char *)sqlite3_column_text(pStmt, i)); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1750 | break; |
| 1751 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1754 | if( pScript ){ |
| 1755 | if( pArray==0 ){ |
| 1756 | Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1757 | }else{ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1758 | Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1759 | } |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1760 | }else if( choice==DB_ONECOLUMN ){ |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1761 | assert( pRet==0 ); |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1762 | if( pRet==0 ){ |
| 1763 | pRet = pVal; |
| 1764 | Tcl_IncrRefCount(pRet); |
| 1765 | } |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1766 | rc = TCL_BREAK; |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1767 | i = nCol; |
| 1768 | }else if( choice==DB_EXISTS ){ |
drh | 446a9b8 | 2006-01-04 18:13:26 +0000 | [diff] [blame] | 1769 | Tcl_DecrRefCount(pRet); |
| 1770 | pRet = Tcl_NewBooleanObj(1); |
| 1771 | Tcl_IncrRefCount(pRet); |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 1772 | rc = TCL_BREAK; |
| 1773 | i = nCol; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1774 | }else{ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1775 | Tcl_ListObjAppendElement(interp, pRet, pVal); |
| 1776 | } |
| 1777 | } |
| 1778 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1779 | if( pScript ){ |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 1780 | pDb->nStep = sqlite3_stmt_status(pStmt, |
| 1781 | SQLITE_STMTSTATUS_FULLSCAN_STEP, 0); |
| 1782 | pDb->nSort = sqlite3_stmt_status(pStmt, |
| 1783 | SQLITE_STMTSTATUS_SORT, 0); |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1784 | rc = Tcl_EvalObjEx(interp, pScript, 0); |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1785 | if( rc==TCL_CONTINUE ){ |
| 1786 | rc = TCL_OK; |
| 1787 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } |
drh | 90b6bb1 | 2004-09-13 13:16:31 +0000 | [diff] [blame] | 1790 | if( rc==TCL_BREAK ){ |
| 1791 | rc = TCL_OK; |
| 1792 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1793 | |
| 1794 | /* Free the column name objects */ |
| 1795 | if( pScript ){ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1796 | /* If the query returned no rows, but an array variable was |
| 1797 | ** specified, call computeColumnNames() now to populate the |
| 1798 | ** arrayname(*) variable. |
| 1799 | */ |
| 1800 | if (pArray && nCol < 0) { |
| 1801 | Tcl_Obj ***ap = (pScript?&apColName:0); |
| 1802 | nCol = computeColumnNames(interp, pStmt, ap, pArray); |
| 1803 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1804 | for(i=0; i<nCol; i++){ |
| 1805 | Tcl_DecrRefCount(apColName[i]); |
| 1806 | } |
| 1807 | Tcl_Free((char*)apColName); |
| 1808 | } |
| 1809 | |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1810 | /* Free the bound string and blob parameters */ |
| 1811 | for(i=0; i<nParm; i++){ |
| 1812 | Tcl_DecrRefCount(apParm[i]); |
| 1813 | } |
| 1814 | if( apParm!=aParm ){ |
| 1815 | Tcl_Free((char*)apParm); |
| 1816 | } |
| 1817 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1818 | /* Reset the statement. If the result code is SQLITE_SCHEMA, then |
| 1819 | ** flush the statement cache and try the statement again. |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1820 | */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1821 | rc2 = sqlite3_reset(pStmt); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 1822 | pDb->nStep = sqlite3_stmt_status(pStmt, |
| 1823 | SQLITE_STMTSTATUS_FULLSCAN_STEP, 1); |
| 1824 | pDb->nSort = sqlite3_stmt_status(pStmt, |
| 1825 | SQLITE_STMTSTATUS_SORT, 1); |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1826 | if( SQLITE_OK!=rc2 ){ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1827 | /* If a run-time error occurs, report the error and stop reading |
| 1828 | ** the SQL |
| 1829 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 1830 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1831 | sqlite3_finalize(pStmt); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1832 | rc = TCL_ERROR; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1833 | if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1834 | break; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1835 | }else if( pDb->maxStmt<=0 ){ |
| 1836 | /* If the cache is turned off, deallocated the statement */ |
| 1837 | if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
| 1838 | sqlite3_finalize(pStmt); |
| 1839 | }else{ |
| 1840 | /* Everything worked and the cache is operational. |
| 1841 | ** Create a new SqlPreparedStmt structure if we need one. |
| 1842 | ** (If we already have one we can just reuse it.) |
| 1843 | */ |
| 1844 | if( pPreStmt==0 ){ |
| 1845 | len = zLeft - zSql; |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 1846 | pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) ); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1847 | if( pPreStmt==0 ) return TCL_ERROR; |
| 1848 | pPreStmt->pStmt = pStmt; |
| 1849 | pPreStmt->nSql = len; |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 1850 | pPreStmt->zSql = sqlite3_sql(pStmt); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1851 | assert( strlen30(pPreStmt->zSql)==len ); |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 1852 | assert( 0==memcmp(pPreStmt->zSql, zSql, len) ); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | /* Add the prepared statement to the beginning of the cache list |
| 1856 | */ |
| 1857 | pPreStmt->pNext = pDb->stmtList; |
| 1858 | pPreStmt->pPrev = 0; |
| 1859 | if( pDb->stmtList ){ |
| 1860 | pDb->stmtList->pPrev = pPreStmt; |
| 1861 | } |
| 1862 | pDb->stmtList = pPreStmt; |
| 1863 | if( pDb->stmtLast==0 ){ |
| 1864 | assert( pDb->nStmt==0 ); |
| 1865 | pDb->stmtLast = pPreStmt; |
| 1866 | }else{ |
| 1867 | assert( pDb->nStmt>0 ); |
| 1868 | } |
| 1869 | pDb->nStmt++; |
| 1870 | |
| 1871 | /* If we have too many statement in cache, remove the surplus from the |
| 1872 | ** end of the cache list. |
| 1873 | */ |
| 1874 | while( pDb->nStmt>pDb->maxStmt ){ |
| 1875 | sqlite3_finalize(pDb->stmtLast->pStmt); |
| 1876 | pDb->stmtLast = pDb->stmtLast->pPrev; |
| 1877 | Tcl_Free((char*)pDb->stmtLast->pNext); |
| 1878 | pDb->stmtLast->pNext = 0; |
| 1879 | pDb->nStmt--; |
| 1880 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1883 | /* Proceed to the next statement */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1884 | zSql = zLeft; |
| 1885 | } |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 1886 | Tcl_DecrRefCount(objv[2]); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1887 | |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 1888 | if( pRet ){ |
| 1889 | if( rc==TCL_OK ){ |
| 1890 | Tcl_SetObjResult(interp, pRet); |
| 1891 | } |
| 1892 | Tcl_DecrRefCount(pRet); |
drh | 050be32 | 2006-07-12 00:18:40 +0000 | [diff] [blame] | 1893 | }else if( rc==TCL_OK ){ |
| 1894 | Tcl_ResetResult(interp); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1895 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 1896 | break; |
| 1897 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1898 | |
| 1899 | /* |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 1900 | ** $db function NAME [-argcount N] SCRIPT |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1901 | ** |
| 1902 | ** Create a new SQL function called NAME. Whenever that function is |
| 1903 | ** called, invoke SCRIPT to evaluate the function. |
| 1904 | */ |
| 1905 | case DB_FUNCTION: { |
| 1906 | SqlFunc *pFunc; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1907 | Tcl_Obj *pScript; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1908 | char *zName; |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 1909 | int nArg = -1; |
| 1910 | if( objc==6 ){ |
| 1911 | const char *z = Tcl_GetString(objv[3]); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1912 | int n = strlen30(z); |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 1913 | if( n>2 && strncmp(z, "-argcount",n)==0 ){ |
| 1914 | if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR; |
| 1915 | if( nArg<0 ){ |
| 1916 | Tcl_AppendResult(interp, "number of arguments must be non-negative", |
| 1917 | (char*)0); |
| 1918 | return TCL_ERROR; |
| 1919 | } |
| 1920 | } |
| 1921 | pScript = objv[5]; |
| 1922 | }else if( objc!=4 ){ |
| 1923 | Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT"); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1924 | return TCL_ERROR; |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 1925 | }else{ |
| 1926 | pScript = objv[3]; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1927 | } |
| 1928 | zName = Tcl_GetStringFromObj(objv[2], 0); |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1929 | pFunc = findSqlFunc(pDb, zName); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1930 | if( pFunc==0 ) return TCL_ERROR; |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 1931 | if( pFunc->pScript ){ |
| 1932 | Tcl_DecrRefCount(pFunc->pScript); |
| 1933 | } |
| 1934 | pFunc->pScript = pScript; |
| 1935 | Tcl_IncrRefCount(pScript); |
| 1936 | pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 1937 | rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1938 | pFunc, tclSqlFunc, 0, 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1939 | if( rc!=SQLITE_OK ){ |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1940 | rc = TCL_ERROR; |
| 1941 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1942 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1943 | break; |
| 1944 | } |
| 1945 | |
| 1946 | /* |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 1947 | ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1948 | */ |
| 1949 | case DB_INCRBLOB: { |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 1950 | #ifdef SQLITE_OMIT_INCRBLOB |
| 1951 | Tcl_AppendResult(interp, "incrblob not available in this build", 0); |
| 1952 | return TCL_ERROR; |
| 1953 | #else |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 1954 | int isReadonly = 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1955 | const char *zDb = "main"; |
| 1956 | const char *zTable; |
| 1957 | const char *zColumn; |
| 1958 | sqlite_int64 iRow; |
| 1959 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 1960 | /* Check for the -readonly option */ |
| 1961 | if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ |
| 1962 | isReadonly = 1; |
| 1963 | } |
| 1964 | |
| 1965 | if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ |
| 1966 | Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1967 | return TCL_ERROR; |
| 1968 | } |
| 1969 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 1970 | if( objc==(6+isReadonly) ){ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1971 | zDb = Tcl_GetString(objv[2]); |
| 1972 | } |
| 1973 | zTable = Tcl_GetString(objv[objc-3]); |
| 1974 | zColumn = Tcl_GetString(objv[objc-2]); |
| 1975 | rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); |
| 1976 | |
| 1977 | if( rc==TCL_OK ){ |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 1978 | rc = createIncrblobChannel( |
| 1979 | interp, pDb, zDb, zTable, zColumn, iRow, isReadonly |
| 1980 | ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1981 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 1982 | #endif |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1983 | break; |
| 1984 | } |
| 1985 | |
| 1986 | /* |
drh | f11bded | 2006-07-17 00:02:44 +0000 | [diff] [blame] | 1987 | ** $db interrupt |
| 1988 | ** |
| 1989 | ** Interrupt the execution of the inner-most SQL interpreter. This |
| 1990 | ** causes the SQL statement to return an error of SQLITE_INTERRUPT. |
| 1991 | */ |
| 1992 | case DB_INTERRUPT: { |
| 1993 | sqlite3_interrupt(pDb->db); |
| 1994 | break; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1998 | ** $db nullvalue ?STRING? |
| 1999 | ** |
| 2000 | ** Change text used when a NULL comes back from the database. If ?STRING? |
| 2001 | ** is not present, then the current string used for NULL is returned. |
| 2002 | ** If STRING is present, then STRING is returned. |
| 2003 | ** |
| 2004 | */ |
| 2005 | case DB_NULLVALUE: { |
| 2006 | if( objc!=2 && objc!=3 ){ |
| 2007 | Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); |
| 2008 | return TCL_ERROR; |
| 2009 | } |
| 2010 | if( objc==3 ){ |
| 2011 | int len; |
| 2012 | char *zNull = Tcl_GetStringFromObj(objv[2], &len); |
| 2013 | if( pDb->zNull ){ |
| 2014 | Tcl_Free(pDb->zNull); |
| 2015 | } |
| 2016 | if( zNull && len>0 ){ |
| 2017 | pDb->zNull = Tcl_Alloc( len + 1 ); |
| 2018 | strncpy(pDb->zNull, zNull, len); |
| 2019 | pDb->zNull[len] = '\0'; |
| 2020 | }else{ |
| 2021 | pDb->zNull = 0; |
| 2022 | } |
| 2023 | } |
| 2024 | Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull)); |
| 2025 | break; |
| 2026 | } |
| 2027 | |
| 2028 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2029 | ** $db last_insert_rowid |
| 2030 | ** |
| 2031 | ** Return an integer which is the ROWID for the most recent insert. |
| 2032 | */ |
| 2033 | case DB_LAST_INSERT_ROWID: { |
| 2034 | Tcl_Obj *pResult; |
drh | f7e678d | 2006-06-21 19:30:34 +0000 | [diff] [blame] | 2035 | Tcl_WideInt rowid; |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2036 | if( objc!=2 ){ |
| 2037 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 2038 | return TCL_ERROR; |
| 2039 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2040 | rowid = sqlite3_last_insert_rowid(pDb->db); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2041 | pResult = Tcl_GetObjResult(interp); |
drh | f7e678d | 2006-06-21 19:30:34 +0000 | [diff] [blame] | 2042 | Tcl_SetWideIntObj(pResult, rowid); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | /* |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2047 | ** The DB_ONECOLUMN method is implemented together with DB_EVAL. |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2048 | */ |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2049 | |
| 2050 | /* $db progress ?N CALLBACK? |
| 2051 | ** |
| 2052 | ** Invoke the given callback every N virtual machine opcodes while executing |
| 2053 | ** queries. |
| 2054 | */ |
| 2055 | case DB_PROGRESS: { |
| 2056 | if( objc==2 ){ |
| 2057 | if( pDb->zProgress ){ |
| 2058 | Tcl_AppendResult(interp, pDb->zProgress, 0); |
| 2059 | } |
| 2060 | }else if( objc==4 ){ |
| 2061 | char *zProgress; |
| 2062 | int len; |
| 2063 | int N; |
| 2064 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2065 | return TCL_ERROR; |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2066 | }; |
| 2067 | if( pDb->zProgress ){ |
| 2068 | Tcl_Free(pDb->zProgress); |
| 2069 | } |
| 2070 | zProgress = Tcl_GetStringFromObj(objv[3], &len); |
| 2071 | if( zProgress && len>0 ){ |
| 2072 | pDb->zProgress = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2073 | memcpy(pDb->zProgress, zProgress, len+1); |
drh | 1807ce3 | 2004-09-07 13:20:35 +0000 | [diff] [blame] | 2074 | }else{ |
| 2075 | pDb->zProgress = 0; |
| 2076 | } |
| 2077 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 2078 | if( pDb->zProgress ){ |
| 2079 | pDb->interp = interp; |
| 2080 | sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); |
| 2081 | }else{ |
| 2082 | sqlite3_progress_handler(pDb->db, 0, 0, 0); |
| 2083 | } |
| 2084 | #endif |
| 2085 | }else{ |
| 2086 | Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2087 | return TCL_ERROR; |
| 2088 | } |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2089 | break; |
| 2090 | } |
| 2091 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2092 | /* $db profile ?CALLBACK? |
| 2093 | ** |
| 2094 | ** Make arrangements to invoke the CALLBACK routine after each SQL statement |
| 2095 | ** that has run. The text of the SQL and the amount of elapse time are |
| 2096 | ** appended to CALLBACK before the script is run. |
| 2097 | */ |
| 2098 | case DB_PROFILE: { |
| 2099 | if( objc>3 ){ |
| 2100 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 2101 | return TCL_ERROR; |
| 2102 | }else if( objc==2 ){ |
| 2103 | if( pDb->zProfile ){ |
| 2104 | Tcl_AppendResult(interp, pDb->zProfile, 0); |
| 2105 | } |
| 2106 | }else{ |
| 2107 | char *zProfile; |
| 2108 | int len; |
| 2109 | if( pDb->zProfile ){ |
| 2110 | Tcl_Free(pDb->zProfile); |
| 2111 | } |
| 2112 | zProfile = Tcl_GetStringFromObj(objv[2], &len); |
| 2113 | if( zProfile && len>0 ){ |
| 2114 | pDb->zProfile = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2115 | memcpy(pDb->zProfile, zProfile, len+1); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2116 | }else{ |
| 2117 | pDb->zProfile = 0; |
| 2118 | } |
| 2119 | #ifndef SQLITE_OMIT_TRACE |
| 2120 | if( pDb->zProfile ){ |
| 2121 | pDb->interp = interp; |
| 2122 | sqlite3_profile(pDb->db, DbProfileHandler, pDb); |
| 2123 | }else{ |
| 2124 | sqlite3_profile(pDb->db, 0, 0); |
| 2125 | } |
| 2126 | #endif |
| 2127 | } |
| 2128 | break; |
| 2129 | } |
| 2130 | |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 2131 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2132 | ** $db rekey KEY |
| 2133 | ** |
| 2134 | ** Change the encryption key on the currently open database. |
| 2135 | */ |
| 2136 | case DB_REKEY: { |
| 2137 | int nKey; |
| 2138 | void *pKey; |
| 2139 | if( objc!=3 ){ |
| 2140 | Tcl_WrongNumArgs(interp, 2, objv, "KEY"); |
| 2141 | return TCL_ERROR; |
| 2142 | } |
| 2143 | pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2144 | #ifdef SQLITE_HAS_CODEC |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2145 | rc = sqlite3_rekey(pDb->db, pKey, nKey); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2146 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2147 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2148 | rc = TCL_ERROR; |
| 2149 | } |
| 2150 | #endif |
| 2151 | break; |
| 2152 | } |
| 2153 | |
| 2154 | /* |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2155 | ** $db status (step|sort) |
| 2156 | ** |
| 2157 | ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or |
| 2158 | ** SQLITE_STMTSTATUS_SORT for the most recent eval. |
| 2159 | */ |
| 2160 | case DB_STATUS: { |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2161 | int v; |
| 2162 | const char *zOp; |
| 2163 | if( objc!=3 ){ |
| 2164 | Tcl_WrongNumArgs(interp, 2, objv, "(step|sort)"); |
| 2165 | return TCL_ERROR; |
| 2166 | } |
| 2167 | zOp = Tcl_GetString(objv[2]); |
| 2168 | if( strcmp(zOp, "step")==0 ){ |
| 2169 | v = pDb->nStep; |
| 2170 | }else if( strcmp(zOp, "sort")==0 ){ |
| 2171 | v = pDb->nSort; |
| 2172 | }else{ |
| 2173 | Tcl_AppendResult(interp, "bad argument: should be step or sort", |
| 2174 | (char*)0); |
| 2175 | return TCL_ERROR; |
| 2176 | } |
| 2177 | Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); |
| 2178 | break; |
| 2179 | } |
| 2180 | |
| 2181 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2182 | ** $db timeout MILLESECONDS |
| 2183 | ** |
| 2184 | ** Delay for the number of milliseconds specified when a file is locked. |
| 2185 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2186 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2187 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2188 | if( objc!=3 ){ |
| 2189 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2190 | return TCL_ERROR; |
| 2191 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2192 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2193 | sqlite3_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2194 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2195 | } |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 2196 | |
| 2197 | /* |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 2198 | ** $db total_changes |
| 2199 | ** |
| 2200 | ** Return the number of rows that were modified, inserted, or deleted |
| 2201 | ** since the database handle was created. |
| 2202 | */ |
| 2203 | case DB_TOTAL_CHANGES: { |
| 2204 | Tcl_Obj *pResult; |
| 2205 | if( objc!=2 ){ |
| 2206 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 2207 | return TCL_ERROR; |
| 2208 | } |
| 2209 | pResult = Tcl_GetObjResult(interp); |
| 2210 | Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); |
| 2211 | break; |
| 2212 | } |
| 2213 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2214 | /* $db trace ?CALLBACK? |
| 2215 | ** |
| 2216 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 2217 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 2218 | ** it is executed. |
| 2219 | */ |
| 2220 | case DB_TRACE: { |
| 2221 | if( objc>3 ){ |
| 2222 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 2223 | return TCL_ERROR; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2224 | }else if( objc==2 ){ |
| 2225 | if( pDb->zTrace ){ |
| 2226 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 2227 | } |
| 2228 | }else{ |
| 2229 | char *zTrace; |
| 2230 | int len; |
| 2231 | if( pDb->zTrace ){ |
| 2232 | Tcl_Free(pDb->zTrace); |
| 2233 | } |
| 2234 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 2235 | if( zTrace && len>0 ){ |
| 2236 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2237 | memcpy(pDb->zTrace, zTrace, len+1); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2238 | }else{ |
| 2239 | pDb->zTrace = 0; |
| 2240 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2241 | #ifndef SQLITE_OMIT_TRACE |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2242 | if( pDb->zTrace ){ |
| 2243 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2244 | sqlite3_trace(pDb->db, DbTraceHandler, pDb); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2245 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2246 | sqlite3_trace(pDb->db, 0, 0); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2247 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2248 | #endif |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 2249 | } |
| 2250 | break; |
| 2251 | } |
| 2252 | |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2253 | /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT |
| 2254 | ** |
| 2255 | ** Start a new transaction (if we are not already in the midst of a |
| 2256 | ** transaction) and execute the TCL script SCRIPT. After SCRIPT |
| 2257 | ** completes, either commit the transaction or roll it back if SCRIPT |
| 2258 | ** throws an exception. Or if no new transation was started, do nothing. |
| 2259 | ** pass the exception on up the stack. |
| 2260 | ** |
| 2261 | ** This command was inspired by Dave Thomas's talk on Ruby at the |
| 2262 | ** 2005 O'Reilly Open Source Convention (OSCON). |
| 2263 | */ |
| 2264 | case DB_TRANSACTION: { |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2265 | Tcl_Obj *pScript; |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2266 | const char *zBegin = "SAVEPOINT _tcl_transaction"; |
| 2267 | const char *zEnd; |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2268 | if( objc!=3 && objc!=4 ){ |
| 2269 | Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); |
| 2270 | return TCL_ERROR; |
| 2271 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2272 | |
| 2273 | if( pDb->nTransaction ){ |
| 2274 | zBegin = "SAVEPOINT _tcl_transaction"; |
| 2275 | }else if( pDb->nTransaction==0 && objc==4 ){ |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2276 | static const char *TTYPE_strs[] = { |
drh | ce60401 | 2005-08-16 11:11:34 +0000 | [diff] [blame] | 2277 | "deferred", "exclusive", "immediate", 0 |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2278 | }; |
| 2279 | enum TTYPE_enum { |
| 2280 | TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE |
| 2281 | }; |
| 2282 | int ttype; |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 2283 | if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2284 | 0, &ttype) ){ |
| 2285 | return TCL_ERROR; |
| 2286 | } |
| 2287 | switch( (enum TTYPE_enum)ttype ){ |
| 2288 | case TTYPE_DEFERRED: /* no-op */; break; |
| 2289 | case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; |
| 2290 | case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; |
| 2291 | } |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2292 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2293 | pScript = objv[objc-1]; |
| 2294 | |
| 2295 | pDb->disableAuth++; |
| 2296 | rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); |
| 2297 | pDb->disableAuth--; |
| 2298 | if( rc!=SQLITE_OK ){ |
| 2299 | Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
| 2300 | return TCL_ERROR; |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2301 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2302 | |
| 2303 | pDb->nTransaction++; |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2304 | rc = Tcl_EvalObjEx(interp, pScript, 0); |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2305 | pDb->nTransaction--; |
| 2306 | |
| 2307 | if( rc!=TCL_ERROR ){ |
| 2308 | if( pDb->nTransaction ){ |
| 2309 | zEnd = "RELEASE _tcl_transaction"; |
| 2310 | }else{ |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2311 | zEnd = "COMMIT"; |
| 2312 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2313 | }else{ |
| 2314 | if( pDb->nTransaction ){ |
| 2315 | zEnd = "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction"; |
| 2316 | }else{ |
| 2317 | zEnd = "ROLLBACK"; |
drh | 109b435 | 2007-06-12 18:50:13 +0000 | [diff] [blame] | 2318 | } |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2319 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame] | 2320 | |
| 2321 | pDb->disableAuth++; |
| 2322 | if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ |
| 2323 | /* This is a tricky scenario to handle. The most likely cause of an |
| 2324 | ** error is that the exec() above was an attempt to commit the |
| 2325 | ** top-level transaction that returned SQLITE_BUSY. Or, less likely, |
| 2326 | ** that an IO-error has occured. In either case, throw a Tcl exception |
| 2327 | ** and try to rollback the transaction. |
| 2328 | ** |
| 2329 | ** But it could also be that the user executed one or more BEGIN, |
| 2330 | ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing |
| 2331 | ** this method's logic. Not clear how this would be best handled. |
| 2332 | */ |
| 2333 | if( rc!=TCL_ERROR ){ |
| 2334 | Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
| 2335 | rc = TCL_ERROR; |
| 2336 | } |
| 2337 | sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); |
| 2338 | } |
| 2339 | pDb->disableAuth--; |
| 2340 | |
drh | 3d21423 | 2005-08-02 12:21:08 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | } |
| 2343 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2344 | /* |
| 2345 | ** $db update_hook ?script? |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2346 | ** $db rollback_hook ?script? |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2347 | */ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2348 | case DB_UPDATE_HOOK: |
| 2349 | case DB_ROLLBACK_HOOK: { |
| 2350 | |
| 2351 | /* set ppHook to point at pUpdateHook or pRollbackHook, depending on |
| 2352 | ** whether [$db update_hook] or [$db rollback_hook] was invoked. |
| 2353 | */ |
| 2354 | Tcl_Obj **ppHook; |
| 2355 | if( choice==DB_UPDATE_HOOK ){ |
| 2356 | ppHook = &pDb->pUpdateHook; |
| 2357 | }else{ |
| 2358 | ppHook = &pDb->pRollbackHook; |
| 2359 | } |
| 2360 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2361 | if( objc!=2 && objc!=3 ){ |
| 2362 | Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); |
| 2363 | return TCL_ERROR; |
| 2364 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2365 | if( *ppHook ){ |
| 2366 | Tcl_SetObjResult(interp, *ppHook); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2367 | if( objc==3 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2368 | Tcl_DecrRefCount(*ppHook); |
| 2369 | *ppHook = 0; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | if( objc==3 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2373 | assert( !(*ppHook) ); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2374 | if( Tcl_GetCharLength(objv[2])>0 ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2375 | *ppHook = objv[2]; |
| 2376 | Tcl_IncrRefCount(*ppHook); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2377 | } |
| 2378 | } |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 2379 | |
| 2380 | sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); |
| 2381 | sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); |
| 2382 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2383 | break; |
| 2384 | } |
| 2385 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 2386 | /* $db version |
| 2387 | ** |
| 2388 | ** Return the version string for this database. |
| 2389 | */ |
| 2390 | case DB_VERSION: { |
| 2391 | Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); |
| 2392 | break; |
| 2393 | } |
| 2394 | |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 2395 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 2396 | } /* End of the SWITCH statement */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2397 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | /* |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2401 | ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2402 | ** ?-create BOOLEAN? ?-nomutex BOOLEAN? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2403 | ** |
| 2404 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 2405 | ** invoked, this routine runs to process that command. |
| 2406 | ** |
| 2407 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 2408 | ** database connection. This command creates a new command named |
| 2409 | ** DBNAME that is used to control that connection. The database |
| 2410 | ** connection is deleted when the DBNAME command is deleted. |
| 2411 | ** |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2412 | ** The second argument is the name of the database file. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2413 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2414 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2415 | static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2416 | SqliteDb *p; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2417 | void *pKey = 0; |
| 2418 | int nKey = 0; |
| 2419 | const char *zArg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2420 | char *zErrMsg; |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2421 | int i; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2422 | const char *zFile; |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2423 | const char *zVfs = 0; |
drh | cc91e7f | 2008-09-03 01:08:00 +0000 | [diff] [blame] | 2424 | int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2425 | Tcl_DString translatedFilename; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2426 | if( objc==2 ){ |
| 2427 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2428 | if( strcmp(zArg,"-version")==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2429 | Tcl_AppendResult(interp,sqlite3_version,0); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 2430 | return TCL_OK; |
| 2431 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2432 | if( strcmp(zArg,"-has-codec")==0 ){ |
| 2433 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2434 | Tcl_AppendResult(interp,"1",0); |
| 2435 | #else |
| 2436 | Tcl_AppendResult(interp,"0",0); |
| 2437 | #endif |
| 2438 | return TCL_OK; |
| 2439 | } |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 2440 | } |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2441 | for(i=3; i+1<objc; i+=2){ |
| 2442 | zArg = Tcl_GetString(objv[i]); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2443 | if( strcmp(zArg,"-key")==0 ){ |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2444 | pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); |
| 2445 | }else if( strcmp(zArg, "-vfs")==0 ){ |
| 2446 | i++; |
| 2447 | zVfs = Tcl_GetString(objv[i]); |
| 2448 | }else if( strcmp(zArg, "-readonly")==0 ){ |
| 2449 | int b; |
| 2450 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
| 2451 | if( b ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2452 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2453 | flags |= SQLITE_OPEN_READONLY; |
| 2454 | }else{ |
| 2455 | flags &= ~SQLITE_OPEN_READONLY; |
| 2456 | flags |= SQLITE_OPEN_READWRITE; |
| 2457 | } |
| 2458 | }else if( strcmp(zArg, "-create")==0 ){ |
| 2459 | int b; |
| 2460 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2461 | if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2462 | flags |= SQLITE_OPEN_CREATE; |
| 2463 | }else{ |
| 2464 | flags &= ~SQLITE_OPEN_CREATE; |
| 2465 | } |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2466 | }else if( strcmp(zArg, "-nomutex")==0 ){ |
| 2467 | int b; |
| 2468 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
| 2469 | if( b ){ |
| 2470 | flags |= SQLITE_OPEN_NOMUTEX; |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2471 | flags &= ~SQLITE_OPEN_FULLMUTEX; |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2472 | }else{ |
| 2473 | flags &= ~SQLITE_OPEN_NOMUTEX; |
| 2474 | } |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2475 | }else if( strcmp(zArg, "-fullmutex")==0 ){ |
| 2476 | int b; |
| 2477 | if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
| 2478 | if( b ){ |
| 2479 | flags |= SQLITE_OPEN_FULLMUTEX; |
| 2480 | flags &= ~SQLITE_OPEN_NOMUTEX; |
| 2481 | }else{ |
| 2482 | flags &= ~SQLITE_OPEN_FULLMUTEX; |
| 2483 | } |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2484 | }else{ |
| 2485 | Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); |
| 2486 | return TCL_ERROR; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2487 | } |
| 2488 | } |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2489 | if( objc<3 || (objc&1)!=1 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2490 | Tcl_WrongNumArgs(interp, 1, objv, |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2491 | "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2492 | " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?" |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 2493 | #ifdef SQLITE_HAS_CODEC |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2494 | " ?-key CODECKEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2495 | #endif |
| 2496 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2497 | return TCL_ERROR; |
| 2498 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2499 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 2500 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2501 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2502 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 2503 | return TCL_ERROR; |
| 2504 | } |
| 2505 | memset(p, 0, sizeof(*p)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2506 | zFile = Tcl_GetStringFromObj(objv[2], 0); |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2507 | zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2508 | sqlite3_open_v2(zFile, &p->db, flags, zVfs); |
drh | 882e8e4 | 2006-08-24 02:42:27 +0000 | [diff] [blame] | 2509 | Tcl_DStringFree(&translatedFilename); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2510 | if( SQLITE_OK!=sqlite3_errcode(p->db) ){ |
drh | 9404d50 | 2006-12-19 18:46:08 +0000 | [diff] [blame] | 2511 | zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2512 | sqlite3_close(p->db); |
| 2513 | p->db = 0; |
| 2514 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2515 | #ifdef SQLITE_HAS_CODEC |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2516 | if( p->db ){ |
| 2517 | sqlite3_key(p->db, pKey, nKey); |
| 2518 | } |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 2519 | #endif |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2520 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2521 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 2522 | Tcl_Free((char*)p); |
drh | 9404d50 | 2006-12-19 18:46:08 +0000 | [diff] [blame] | 2523 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2524 | return TCL_ERROR; |
| 2525 | } |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2526 | p->maxStmt = NUM_PREPARED_STMTS; |
drh | 5169bbc | 2006-08-24 14:59:45 +0000 | [diff] [blame] | 2527 | p->interp = interp; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2528 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
| 2529 | Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2530 | return TCL_OK; |
| 2531 | } |
| 2532 | |
| 2533 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 2534 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 2535 | ** library. |
| 2536 | */ |
| 2537 | #ifndef USE_TCL_STUBS |
| 2538 | # undef Tcl_InitStubs |
| 2539 | # define Tcl_InitStubs(a,b,c) |
| 2540 | #endif |
| 2541 | |
| 2542 | /* |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 2543 | ** Make sure we have a PACKAGE_VERSION macro defined. This will be |
| 2544 | ** defined automatically by the TEA makefile. But other makefiles |
| 2545 | ** do not define it. |
| 2546 | */ |
| 2547 | #ifndef PACKAGE_VERSION |
| 2548 | # define PACKAGE_VERSION SQLITE_VERSION |
| 2549 | #endif |
| 2550 | |
| 2551 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2552 | ** Initialize this module. |
| 2553 | ** |
| 2554 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 2555 | ** (Hence there is no namespace. There is no point in using a namespace |
| 2556 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 2557 | ** used to open a new SQLite database. See the DbMain() routine above |
| 2558 | ** for additional information. |
| 2559 | */ |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 2560 | EXTERN int Sqlite3_Init(Tcl_Interp *interp){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 2561 | Tcl_InitStubs(interp, "8.4", 0); |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 2562 | Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 2563 | Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 2564 | Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 29bc461 | 2005-10-05 10:40:15 +0000 | [diff] [blame] | 2565 | Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 2566 | return TCL_OK; |
| 2567 | } |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 2568 | EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 2569 | EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
| 2570 | EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
drh | e2c3a65 | 2008-09-23 09:58:46 +0000 | [diff] [blame] | 2571 | EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 2572 | EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 2573 | EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 2574 | EXTERN int Tclsqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
| 2575 | |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 2576 | |
| 2577 | #ifndef SQLITE_3_SUFFIX_ONLY |
drh | ad6e137 | 2006-07-10 21:15:51 +0000 | [diff] [blame] | 2578 | EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 2579 | EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
| 2580 | EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
| 2581 | EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
drh | e2c3a65 | 2008-09-23 09:58:46 +0000 | [diff] [blame] | 2582 | EXTERN int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 2583 | EXTERN int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 2584 | EXTERN int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
| 2585 | EXTERN int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
drh | 49766d6 | 2005-01-08 18:42:28 +0000 | [diff] [blame] | 2586 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2587 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2588 | #ifdef TCLSH |
| 2589 | /***************************************************************************** |
| 2590 | ** The code that follows is used to build standalone TCL interpreters |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2591 | ** that are statically linked with SQLite. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2592 | */ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2593 | |
| 2594 | /* |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2595 | ** If the macro TCLSH is one, then put in code this for the |
| 2596 | ** "main" routine that will initialize Tcl and take input from |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 2597 | ** standard input, or if a file is named on the command line |
| 2598 | ** the TCL interpreter reads and evaluates that file. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2599 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2600 | #if TCLSH==1 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2601 | static char zMainloop[] = |
| 2602 | "set line {}\n" |
| 2603 | "while {![eof stdin]} {\n" |
| 2604 | "if {$line!=\"\"} {\n" |
| 2605 | "puts -nonewline \"> \"\n" |
| 2606 | "} else {\n" |
| 2607 | "puts -nonewline \"% \"\n" |
| 2608 | "}\n" |
| 2609 | "flush stdout\n" |
| 2610 | "append line [gets stdin]\n" |
| 2611 | "if {[info complete $line]} {\n" |
| 2612 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 2613 | "puts stderr \"Error: $result\"\n" |
| 2614 | "} elseif {$result!=\"\"} {\n" |
| 2615 | "puts $result\n" |
| 2616 | "}\n" |
| 2617 | "set line {}\n" |
| 2618 | "} else {\n" |
| 2619 | "append line \\n\n" |
| 2620 | "}\n" |
| 2621 | "}\n" |
| 2622 | ; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2623 | #endif |
| 2624 | |
| 2625 | /* |
| 2626 | ** If the macro TCLSH is two, then get the main loop code out of |
| 2627 | ** the separate file "spaceanal_tcl.h". |
| 2628 | */ |
| 2629 | #if TCLSH==2 |
| 2630 | static char zMainloop[] = |
| 2631 | #include "spaceanal_tcl.h" |
| 2632 | ; |
| 2633 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2634 | |
| 2635 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 2636 | int TCLSH_MAIN(int argc, char **argv){ |
| 2637 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 2638 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2639 | interp = Tcl_CreateInterp(); |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 2640 | Sqlite3_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 2641 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 2642 | { |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 2643 | extern int Md5_Init(Tcl_Interp*); |
| 2644 | extern int Sqliteconfig_Init(Tcl_Interp*); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 2645 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 2646 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 2647 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 2648 | extern int Sqlitetest4_Init(Tcl_Interp*); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 2649 | extern int Sqlitetest5_Init(Tcl_Interp*); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2650 | extern int Sqlitetest6_Init(Tcl_Interp*); |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 2651 | extern int Sqlitetest7_Init(Tcl_Interp*); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2652 | extern int Sqlitetest8_Init(Tcl_Interp*); |
danielk1977 | a713f2c | 2007-03-29 12:19:11 +0000 | [diff] [blame] | 2653 | extern int Sqlitetest9_Init(Tcl_Interp*); |
drh | 2366940 | 2006-01-09 17:29:52 +0000 | [diff] [blame] | 2654 | extern int Sqlitetestasync_Init(Tcl_Interp*); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2655 | extern int Sqlitetest_autoext_Init(Tcl_Interp*); |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2656 | extern int Sqlitetest_func_Init(Tcl_Interp*); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 2657 | extern int Sqlitetest_hexio_Init(Tcl_Interp*); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 2658 | extern int Sqlitetest_malloc_Init(Tcl_Interp*); |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 2659 | extern int Sqlitetest_mutex_Init(Tcl_Interp*); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 2660 | extern int Sqlitetestschema_Init(Tcl_Interp*); |
| 2661 | extern int Sqlitetestsse_Init(Tcl_Interp*); |
| 2662 | extern int Sqlitetesttclvar_Init(Tcl_Interp*); |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 2663 | extern int SqlitetestThread_Init(Tcl_Interp*); |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 2664 | extern int SqlitetestOnefile_Init(); |
danielk1977 | 5d1f5aa | 2008-04-10 14:51:00 +0000 | [diff] [blame] | 2665 | extern int SqlitetestOsinst_Init(Tcl_Interp*); |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame^] | 2666 | extern int Sqlitetestbackup_Init(Tcl_Interp*); |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 2667 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 2668 | Md5_Init(interp); |
| 2669 | Sqliteconfig_Init(interp); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 2670 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 2671 | Sqlitetest2_Init(interp); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2672 | Sqlitetest3_Init(interp); |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2673 | Sqlitetest4_Init(interp); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 2674 | Sqlitetest5_Init(interp); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2675 | Sqlitetest6_Init(interp); |
drh | 29c636b | 2006-01-09 23:40:25 +0000 | [diff] [blame] | 2676 | Sqlitetest7_Init(interp); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2677 | Sqlitetest8_Init(interp); |
danielk1977 | a713f2c | 2007-03-29 12:19:11 +0000 | [diff] [blame] | 2678 | Sqlitetest9_Init(interp); |
drh | 2366940 | 2006-01-09 17:29:52 +0000 | [diff] [blame] | 2679 | Sqlitetestasync_Init(interp); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2680 | Sqlitetest_autoext_Init(interp); |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2681 | Sqlitetest_func_Init(interp); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 2682 | Sqlitetest_hexio_Init(interp); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 2683 | Sqlitetest_malloc_Init(interp); |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 2684 | Sqlitetest_mutex_Init(interp); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 2685 | Sqlitetestschema_Init(interp); |
| 2686 | Sqlitetesttclvar_Init(interp); |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 2687 | SqlitetestThread_Init(interp); |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 2688 | SqlitetestOnefile_Init(interp); |
danielk1977 | 5d1f5aa | 2008-04-10 14:51:00 +0000 | [diff] [blame] | 2689 | SqlitetestOsinst_Init(interp); |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame^] | 2690 | Sqlitetestbackup_Init(interp); |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 2691 | |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 2692 | #ifdef SQLITE_SSE |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 2693 | Sqlitetestsse_Init(interp); |
| 2694 | #endif |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 2695 | } |
| 2696 | #endif |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2697 | if( argc>=2 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2698 | int i; |
shess | ad42c3a | 2006-08-22 23:53:46 +0000 | [diff] [blame] | 2699 | char zArgc[32]; |
| 2700 | sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); |
| 2701 | Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2702 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 2703 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
drh | 61212b6 | 2004-12-02 20:17:00 +0000 | [diff] [blame] | 2704 | for(i=3-TCLSH; i<argc; i++){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2705 | Tcl_SetVar(interp, "argv", argv[i], |
| 2706 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 2707 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2708 | if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 2709 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | a81c64a | 2009-01-14 23:38:02 +0000 | [diff] [blame] | 2710 | if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 2711 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2712 | return 1; |
| 2713 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 2714 | } |
| 2715 | if( argc<=1 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2716 | Tcl_GlobalEval(interp, zMainloop); |
| 2717 | } |
| 2718 | return 0; |
| 2719 | } |
| 2720 | #endif /* TCLSH */ |