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