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 | ************************************************************************* |
| 12 | ** A TCL Interface to SQLite |
| 13 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 14 | ** $Id: tclsqlite.c,v 1.104 2004/09/06 17:24:13 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 15 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 16 | #ifndef NO_TCL /* Omit this whole file if TCL is unavailable */ |
| 17 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 18 | #include "sqliteInt.h" |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 19 | #include "hash.h" |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 20 | #include "tcl.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 23 | #include <assert.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 26 | ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
| 27 | ** have to do a translation when going between the two. Set the |
| 28 | ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
| 29 | ** this translation. |
| 30 | */ |
| 31 | #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
| 32 | # define UTF_TRANSLATION_NEEDED 1 |
| 33 | #endif |
| 34 | |
| 35 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 36 | ** New SQL functions can be created as TCL scripts. Each such function |
| 37 | ** is described by an instance of the following structure. |
| 38 | */ |
| 39 | typedef struct SqlFunc SqlFunc; |
| 40 | struct SqlFunc { |
| 41 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 42 | char *zScript; /* The script to be run */ |
| 43 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 44 | }; |
| 45 | |
| 46 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 47 | ** New collation sequences function can be created as TCL scripts. Each such |
| 48 | ** function is described by an instance of the following structure. |
| 49 | */ |
| 50 | typedef struct SqlCollate SqlCollate; |
| 51 | struct SqlCollate { |
| 52 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 53 | char *zScript; /* The script to be run */ |
| 54 | SqlCollate *pNext; /* Next function on the list of them all */ |
| 55 | }; |
| 56 | |
| 57 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 58 | ** There is one instance of this structure for each SQLite database |
| 59 | ** that has been opened by the SQLite TCL interface. |
| 60 | */ |
| 61 | typedef struct SqliteDb SqliteDb; |
| 62 | struct SqliteDb { |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 63 | sqlite3 *db; /* The "real" database structure */ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 64 | Tcl_Interp *interp; /* The interpreter used for this database */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 65 | char *zBusy; /* The busy callback routine */ |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 66 | char *zCommit; /* The commit hook callback routine */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 67 | char *zTrace; /* The trace callback routine */ |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 68 | char *zProgress; /* The progress callback routine */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 69 | char *zAuth; /* The authorization callback routine */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 70 | SqlFunc *pFunc; /* List of SQL functions */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 71 | SqlCollate *pCollate; /* List of SQL collation functions */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 72 | int rc; /* Return code of most recent sqlite3_exec() */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 73 | Tcl_Obj *pCollateNeeded; /* Collation needed script */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 74 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 75 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 76 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 77 | ** This is a second alternative callback for database queries. A the |
| 78 | ** first column of the first row of the result is made the TCL result. |
| 79 | */ |
| 80 | static int DbEvalCallback3( |
| 81 | void *clientData, /* An instance of CallbackData */ |
| 82 | int nCol, /* Number of columns in the result */ |
| 83 | char ** azCol, /* Data for each column */ |
| 84 | char ** azN /* Name for each column */ |
| 85 | ){ |
| 86 | Tcl_Interp *interp = (Tcl_Interp*)clientData; |
| 87 | Tcl_Obj *pElem; |
| 88 | if( azCol==0 ) return 1; |
| 89 | if( nCol==0 ) return 1; |
| 90 | #ifdef UTF_TRANSLATION_NEEDED |
| 91 | { |
| 92 | Tcl_DString dCol; |
| 93 | Tcl_DStringInit(&dCol); |
| 94 | Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol); |
| 95 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 96 | Tcl_DStringFree(&dCol); |
| 97 | } |
| 98 | #else |
| 99 | pElem = Tcl_NewStringObj(azCol[0], -1); |
| 100 | #endif |
| 101 | Tcl_SetObjResult(interp, pElem); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 106 | ** TCL calls this procedure when an sqlite3 database command is |
| 107 | ** deleted. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 108 | */ |
| 109 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 110 | SqliteDb *pDb = (SqliteDb*)db; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 111 | sqlite3_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 112 | while( pDb->pFunc ){ |
| 113 | SqlFunc *pFunc = pDb->pFunc; |
| 114 | pDb->pFunc = pFunc->pNext; |
| 115 | Tcl_Free((char*)pFunc); |
| 116 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 117 | while( pDb->pCollate ){ |
| 118 | SqlCollate *pCollate = pDb->pCollate; |
| 119 | pDb->pCollate = pCollate->pNext; |
| 120 | Tcl_Free((char*)pCollate); |
| 121 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 122 | if( pDb->zBusy ){ |
| 123 | Tcl_Free(pDb->zBusy); |
| 124 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 125 | if( pDb->zTrace ){ |
| 126 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 127 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 128 | if( pDb->zAuth ){ |
| 129 | Tcl_Free(pDb->zAuth); |
| 130 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 131 | Tcl_Free((char*)pDb); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | ** This routine is called when a database file is locked while trying |
| 136 | ** to execute SQL. |
| 137 | */ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 138 | static int DbBusyHandler(void *cd, int nTries){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 139 | SqliteDb *pDb = (SqliteDb*)cd; |
| 140 | int rc; |
| 141 | char zVal[30]; |
| 142 | char *zCmd; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 143 | Tcl_DString cmd; |
| 144 | |
| 145 | Tcl_DStringInit(&cmd); |
| 146 | Tcl_DStringAppend(&cmd, pDb->zBusy, -1); |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 147 | sprintf(zVal, "%d", nTries); |
| 148 | Tcl_DStringAppendElement(&cmd, zVal); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 149 | zCmd = Tcl_DStringValue(&cmd); |
| 150 | rc = Tcl_Eval(pDb->interp, zCmd); |
| 151 | Tcl_DStringFree(&cmd); |
| 152 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 153 | return 0; |
| 154 | } |
| 155 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 159 | ** This routine is invoked as the 'progress callback' for the database. |
| 160 | */ |
| 161 | static int DbProgressHandler(void *cd){ |
| 162 | SqliteDb *pDb = (SqliteDb*)cd; |
| 163 | int rc; |
| 164 | |
| 165 | assert( pDb->zProgress ); |
| 166 | rc = Tcl_Eval(pDb->interp, pDb->zProgress); |
| 167 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 168 | return 1; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 174 | ** This routine is called by the SQLite trace handler whenever a new |
| 175 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 176 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 177 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 178 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 179 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 180 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 181 | Tcl_DStringInit(&str); |
| 182 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 183 | Tcl_DStringAppendElement(&str, zSql); |
| 184 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 185 | Tcl_DStringFree(&str); |
| 186 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 190 | ** This routine is called when a transaction is committed. The |
| 191 | ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
| 192 | ** if it throws an exception, the transaction is rolled back instead |
| 193 | ** of being committed. |
| 194 | */ |
| 195 | static int DbCommitHandler(void *cd){ |
| 196 | SqliteDb *pDb = (SqliteDb*)cd; |
| 197 | int rc; |
| 198 | |
| 199 | rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
| 200 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 201 | return 1; |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 206 | static void tclCollateNeeded( |
| 207 | void *pCtx, |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 208 | sqlite3 *db, |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 209 | int enc, |
| 210 | const char *zName |
| 211 | ){ |
| 212 | SqliteDb *pDb = (SqliteDb *)pCtx; |
| 213 | Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); |
| 214 | Tcl_IncrRefCount(pScript); |
| 215 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); |
| 216 | Tcl_EvalObjEx(pDb->interp, pScript, 0); |
| 217 | Tcl_DecrRefCount(pScript); |
| 218 | } |
| 219 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 220 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 221 | ** This routine is called to evaluate an SQL collation function implemented |
| 222 | ** using TCL script. |
| 223 | */ |
| 224 | static int tclSqlCollate( |
| 225 | void *pCtx, |
| 226 | int nA, |
| 227 | const void *zA, |
| 228 | int nB, |
| 229 | const void *zB |
| 230 | ){ |
| 231 | SqlCollate *p = (SqlCollate *)pCtx; |
| 232 | Tcl_Obj *pCmd; |
| 233 | |
| 234 | pCmd = Tcl_NewStringObj(p->zScript, -1); |
| 235 | Tcl_IncrRefCount(pCmd); |
| 236 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); |
| 237 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); |
| 238 | Tcl_EvalObjEx(p->interp, pCmd, 0); |
| 239 | Tcl_DecrRefCount(pCmd); |
| 240 | return (atoi(Tcl_GetStringResult(p->interp))); |
| 241 | } |
| 242 | |
| 243 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 244 | ** This routine is called to evaluate an SQL function implemented |
| 245 | ** using TCL script. |
| 246 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 247 | static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 248 | SqlFunc *p = sqlite3_user_data(context); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 249 | Tcl_DString cmd; |
| 250 | int i; |
| 251 | int rc; |
| 252 | |
| 253 | Tcl_DStringInit(&cmd); |
| 254 | Tcl_DStringAppend(&cmd, p->zScript, -1); |
| 255 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 256 | if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 257 | Tcl_DStringAppendElement(&cmd, ""); |
| 258 | }else{ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 259 | Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i])); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 260 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 261 | } |
| 262 | rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd)); |
| 263 | if( rc ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 264 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 265 | }else{ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 266 | sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, |
| 267 | SQLITE_TRANSIENT); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 270 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 271 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 272 | /* |
| 273 | ** This is the authentication function. It appends the authentication |
| 274 | ** type code and the two arguments to zCmd[] then invokes the result |
| 275 | ** on the interpreter. The reply is examined to determine if the |
| 276 | ** authentication fails or succeeds. |
| 277 | */ |
| 278 | static int auth_callback( |
| 279 | void *pArg, |
| 280 | int code, |
| 281 | const char *zArg1, |
| 282 | const char *zArg2, |
| 283 | const char *zArg3, |
| 284 | const char *zArg4 |
| 285 | ){ |
| 286 | char *zCode; |
| 287 | Tcl_DString str; |
| 288 | int rc; |
| 289 | const char *zReply; |
| 290 | SqliteDb *pDb = (SqliteDb*)pArg; |
| 291 | |
| 292 | switch( code ){ |
| 293 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 294 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 295 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 296 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 297 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 298 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 299 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 300 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 301 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 302 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 303 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 304 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 305 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 306 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 307 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 308 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 309 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 310 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 311 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 312 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 313 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 314 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 315 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 316 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
drh | 81e293b | 2003-06-06 19:00:42 +0000 | [diff] [blame] | 317 | case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
| 318 | case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 319 | default : zCode="????"; break; |
| 320 | } |
| 321 | Tcl_DStringInit(&str); |
| 322 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 323 | Tcl_DStringAppendElement(&str, zCode); |
| 324 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 325 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 326 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 327 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 328 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 329 | Tcl_DStringFree(&str); |
| 330 | zReply = Tcl_GetStringResult(pDb->interp); |
| 331 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 332 | rc = SQLITE_OK; |
| 333 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 334 | rc = SQLITE_DENY; |
| 335 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 336 | rc = SQLITE_IGNORE; |
| 337 | }else{ |
| 338 | rc = 999; |
| 339 | } |
| 340 | return rc; |
| 341 | } |
| 342 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 343 | |
| 344 | /* |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 345 | ** zText is a pointer to text obtained via an sqlite3_result_text() |
| 346 | ** or similar interface. This routine returns a Tcl string object, |
| 347 | ** reference count set to 0, containing the text. If a translation |
| 348 | ** between iso8859 and UTF-8 is required, it is preformed. |
| 349 | */ |
| 350 | static Tcl_Obj *dbTextToObj(char const *zText){ |
| 351 | Tcl_Obj *pVal; |
| 352 | #ifdef UTF_TRANSLATION_NEEDED |
| 353 | Tcl_DString dCol; |
| 354 | Tcl_DStringInit(&dCol); |
| 355 | Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); |
| 356 | pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 357 | Tcl_DStringFree(&dCol); |
| 358 | #else |
| 359 | pVal = Tcl_NewStringObj(zText, -1); |
| 360 | #endif |
| 361 | return pVal; |
| 362 | } |
| 363 | |
| 364 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 365 | ** The "sqlite" command below creates a new Tcl command for each |
| 366 | ** connection it opens to an SQLite database. This routine is invoked |
| 367 | ** whenever one of those connection-specific commands is executed |
| 368 | ** in Tcl. For example, if you run Tcl code like this: |
| 369 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 370 | ** sqlite3 db1 "my_database" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 371 | ** db1 close |
| 372 | ** |
| 373 | ** The first command opens a connection to the "my_database" database |
| 374 | ** and calls that connection "db1". The second command causes this |
| 375 | ** subroutine to be invoked. |
| 376 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 377 | 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] | 378 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 379 | int choice; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 380 | int rc = TCL_OK; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 381 | static const char *DB_strs[] = { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 382 | "authorizer", "busy", "changes", |
| 383 | "close", "collate", "collation_needed", |
| 384 | "commit_hook", "complete", "errorcode", |
| 385 | "eval", "function", "last_insert_rowid", |
| 386 | "onecolumn", "progress", "rekey", |
| 387 | "timeout", "total_changes", "trace", |
| 388 | 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 389 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 390 | enum DB_enum { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 391 | DB_AUTHORIZER, DB_BUSY, DB_CHANGES, |
| 392 | DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED, |
| 393 | DB_COMMIT_HOOK, DB_COMPLETE, DB_ERRORCODE, |
| 394 | DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID, |
| 395 | DB_ONECOLUMN, DB_PROGRESS, DB_REKEY, |
| 396 | DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | if( objc<2 ){ |
| 400 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 401 | return TCL_ERROR; |
| 402 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 403 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 404 | return TCL_ERROR; |
| 405 | } |
| 406 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 407 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 408 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 409 | /* $db authorizer ?CALLBACK? |
| 410 | ** |
| 411 | ** Invoke the given callback to authorize each SQL operation as it is |
| 412 | ** compiled. 5 arguments are appended to the callback before it is |
| 413 | ** invoked: |
| 414 | ** |
| 415 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 416 | ** (2) First descriptive name (depends on authorization type) |
| 417 | ** (3) Second descriptive name |
| 418 | ** (4) Name of the database (ex: "main", "temp") |
| 419 | ** (5) Name of trigger that is doing the access |
| 420 | ** |
| 421 | ** The callback should return on of the following strings: SQLITE_OK, |
| 422 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 423 | ** |
| 424 | ** If this method is invoked with no arguments, the current authorization |
| 425 | ** callback string is returned. |
| 426 | */ |
| 427 | case DB_AUTHORIZER: { |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 428 | #ifdef SQLITE_OMIT_AUTHORIZATION |
| 429 | Tcl_AppendResult(interp, "authorization not available in this build", 0); |
| 430 | return TCL_ERROR; |
| 431 | #else |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 432 | if( objc>3 ){ |
| 433 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 434 | return TCL_ERROR; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 435 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 436 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 437 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 438 | } |
| 439 | }else{ |
| 440 | char *zAuth; |
| 441 | int len; |
| 442 | if( pDb->zAuth ){ |
| 443 | Tcl_Free(pDb->zAuth); |
| 444 | } |
| 445 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 446 | if( zAuth && len>0 ){ |
| 447 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
| 448 | strcpy(pDb->zAuth, zAuth); |
| 449 | }else{ |
| 450 | pDb->zAuth = 0; |
| 451 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 452 | if( pDb->zAuth ){ |
| 453 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 454 | sqlite3_set_authorizer(pDb->db, auth_callback, pDb); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 455 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 456 | sqlite3_set_authorizer(pDb->db, 0, 0); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 457 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 458 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 459 | #endif |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 460 | break; |
| 461 | } |
| 462 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 463 | /* $db busy ?CALLBACK? |
| 464 | ** |
| 465 | ** Invoke the given callback if an SQL statement attempts to open |
| 466 | ** a locked database file. |
| 467 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 468 | case DB_BUSY: { |
| 469 | if( objc>3 ){ |
| 470 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 471 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 472 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 473 | if( pDb->zBusy ){ |
| 474 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 475 | } |
| 476 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 477 | char *zBusy; |
| 478 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 479 | if( pDb->zBusy ){ |
| 480 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 481 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 482 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 483 | if( zBusy && len>0 ){ |
| 484 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 485 | strcpy(pDb->zBusy, zBusy); |
| 486 | }else{ |
| 487 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 488 | } |
| 489 | if( pDb->zBusy ){ |
| 490 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 491 | sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 492 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 493 | sqlite3_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 496 | break; |
| 497 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 498 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 499 | /* $db progress ?N CALLBACK? |
| 500 | ** |
| 501 | ** Invoke the given callback every N virtual machine opcodes while executing |
| 502 | ** queries. |
| 503 | */ |
| 504 | case DB_PROGRESS: { |
| 505 | if( objc==2 ){ |
| 506 | if( pDb->zProgress ){ |
| 507 | Tcl_AppendResult(interp, pDb->zProgress, 0); |
| 508 | } |
| 509 | }else if( objc==4 ){ |
| 510 | char *zProgress; |
| 511 | int len; |
| 512 | int N; |
| 513 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ |
| 514 | return TCL_ERROR; |
| 515 | }; |
| 516 | if( pDb->zProgress ){ |
| 517 | Tcl_Free(pDb->zProgress); |
| 518 | } |
| 519 | zProgress = Tcl_GetStringFromObj(objv[3], &len); |
| 520 | if( zProgress && len>0 ){ |
| 521 | pDb->zProgress = Tcl_Alloc( len + 1 ); |
| 522 | strcpy(pDb->zProgress, zProgress); |
| 523 | }else{ |
| 524 | pDb->zProgress = 0; |
| 525 | } |
| 526 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 527 | if( pDb->zProgress ){ |
| 528 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 529 | sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 530 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 531 | sqlite3_progress_handler(pDb->db, 0, 0, 0); |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 532 | } |
| 533 | #endif |
| 534 | }else{ |
| 535 | Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); |
| 536 | return TCL_ERROR; |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 541 | /* $db changes |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 542 | ** |
| 543 | ** Return the number of rows that were modified, inserted, or deleted by |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 544 | ** the most recent INSERT, UPDATE or DELETE statement, not including |
| 545 | ** any changes made by trigger programs. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 546 | */ |
| 547 | case DB_CHANGES: { |
| 548 | Tcl_Obj *pResult; |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 549 | if( objc!=2 ){ |
| 550 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 551 | return TCL_ERROR; |
| 552 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 553 | pResult = Tcl_GetObjResult(interp); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 554 | Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | /* $db close |
| 559 | ** |
| 560 | ** Shutdown the database |
| 561 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 562 | case DB_CLOSE: { |
| 563 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 564 | break; |
| 565 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 566 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 567 | /* $db commit_hook ?CALLBACK? |
| 568 | ** |
| 569 | ** Invoke the given callback just before committing every SQL transaction. |
| 570 | ** If the callback throws an exception or returns non-zero, then the |
| 571 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 572 | ** is disabled. |
| 573 | */ |
| 574 | case DB_COMMIT_HOOK: { |
| 575 | if( objc>3 ){ |
| 576 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 577 | return TCL_ERROR; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 578 | }else if( objc==2 ){ |
| 579 | if( pDb->zCommit ){ |
| 580 | Tcl_AppendResult(interp, pDb->zCommit, 0); |
| 581 | } |
| 582 | }else{ |
| 583 | char *zCommit; |
| 584 | int len; |
| 585 | if( pDb->zCommit ){ |
| 586 | Tcl_Free(pDb->zCommit); |
| 587 | } |
| 588 | zCommit = Tcl_GetStringFromObj(objv[2], &len); |
| 589 | if( zCommit && len>0 ){ |
| 590 | pDb->zCommit = Tcl_Alloc( len + 1 ); |
| 591 | strcpy(pDb->zCommit, zCommit); |
| 592 | }else{ |
| 593 | pDb->zCommit = 0; |
| 594 | } |
| 595 | if( pDb->zCommit ){ |
| 596 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 597 | sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 598 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 599 | sqlite3_commit_hook(pDb->db, 0, 0); |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | break; |
| 603 | } |
| 604 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 605 | /* |
| 606 | ** $db collate NAME SCRIPT |
| 607 | ** |
| 608 | ** Create a new SQL collation function called NAME. Whenever |
| 609 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 610 | */ |
| 611 | case DB_COLLATE: { |
| 612 | SqlCollate *pCollate; |
| 613 | char *zName; |
| 614 | char *zScript; |
| 615 | int nScript; |
| 616 | if( objc!=4 ){ |
| 617 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 618 | return TCL_ERROR; |
| 619 | } |
| 620 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 621 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 622 | pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); |
| 623 | if( pCollate==0 ) return TCL_ERROR; |
| 624 | pCollate->interp = interp; |
| 625 | pCollate->pNext = pDb->pCollate; |
| 626 | pCollate->zScript = (char*)&pCollate[1]; |
| 627 | pDb->pCollate = pCollate; |
| 628 | strcpy(pCollate->zScript, zScript); |
| 629 | if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, |
| 630 | pCollate, tclSqlCollate) ){ |
| 631 | return TCL_ERROR; |
| 632 | } |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | ** $db collation_needed SCRIPT |
| 638 | ** |
| 639 | ** Create a new SQL collation function called NAME. Whenever |
| 640 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 641 | */ |
| 642 | case DB_COLLATION_NEEDED: { |
| 643 | if( objc!=3 ){ |
| 644 | Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); |
| 645 | return TCL_ERROR; |
| 646 | } |
| 647 | if( pDb->pCollateNeeded ){ |
| 648 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 649 | } |
| 650 | pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); |
| 651 | Tcl_IncrRefCount(pDb->pCollateNeeded); |
| 652 | sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); |
| 653 | break; |
| 654 | } |
| 655 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 656 | /* $db complete SQL |
| 657 | ** |
| 658 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 659 | ** additional lines of input are needed. This is similar to the |
| 660 | ** built-in "info complete" command of Tcl. |
| 661 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 662 | case DB_COMPLETE: { |
| 663 | Tcl_Obj *pResult; |
| 664 | int isComplete; |
| 665 | if( objc!=3 ){ |
| 666 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 667 | return TCL_ERROR; |
| 668 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 669 | isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 670 | pResult = Tcl_GetObjResult(interp); |
| 671 | Tcl_SetBooleanObj(pResult, isComplete); |
| 672 | break; |
| 673 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | ** $db errorcode |
| 677 | ** |
| 678 | ** Return the numeric error code that was returned by the most recent |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 679 | ** call to sqlite3_exec(). |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 680 | */ |
| 681 | case DB_ERRORCODE: { |
danielk1977 | f3ce83f | 2004-06-14 11:43:46 +0000 | [diff] [blame] | 682 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 685 | |
| 686 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 687 | ** $db eval $sql ?array? ?{ ...code... }? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 688 | ** |
| 689 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 690 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 691 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 692 | ** If "array" is an empty string, then the values are placed in variables |
| 693 | ** that have the same name as the fields extracted by the query. |
| 694 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 695 | case DB_EVAL: { |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 696 | char const *zSql; /* Next SQL statement to execute */ |
| 697 | char const *zLeft; /* What is left after first stmt in zSql */ |
| 698 | sqlite3_stmt *pStmt; /* Compiled SQL statment */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 699 | Tcl_Obj *pArray; /* Name of array into which results are written */ |
| 700 | Tcl_Obj *pScript; /* Script to run for each result set */ |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 701 | Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */ |
| 702 | int nParm; /* Number of entries used in apParm[] */ |
| 703 | Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 704 | |
| 705 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 706 | Tcl_IncrRefCount(pRet); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 707 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 708 | if( objc<3 || objc>5 ){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 709 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 710 | return TCL_ERROR; |
| 711 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 712 | if( objc==3 ){ |
| 713 | pArray = pScript = 0; |
| 714 | }else if( objc==4 ){ |
| 715 | pArray = 0; |
| 716 | pScript = objv[3]; |
| 717 | }else{ |
| 718 | pArray = objv[3]; |
| 719 | if( Tcl_GetString(pArray)[0]==0 ) pArray = 0; |
| 720 | pScript = objv[4]; |
| 721 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 722 | |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 723 | Tcl_IncrRefCount(objv[2]); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 724 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
| 725 | while( zSql[0] ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 726 | int i; /* Loop counter */ |
| 727 | int nVar; /* Number of wildcards in the SQL */ |
| 728 | int nCol; /* Number of columns in the result set */ |
| 729 | Tcl_Obj **apColName = 0; /* Array of column names */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 730 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 731 | /* Compile a single SQL statement */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 732 | if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 733 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 734 | rc = TCL_ERROR; |
| 735 | break; |
| 736 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 737 | if( pStmt==0 ){ |
| 738 | if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
| 739 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 740 | rc = TCL_ERROR; |
| 741 | break; |
| 742 | }else{ |
| 743 | zSql = zLeft; |
| 744 | continue; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 745 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 746 | } |
| 747 | |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 748 | /* Bind values to wildcards that begin with $ or : */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 749 | nVar = sqlite3_bind_parameter_count(pStmt); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 750 | nParm = 0; |
| 751 | if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){ |
| 752 | apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0])); |
| 753 | }else{ |
| 754 | apParm = aParm; |
| 755 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 756 | for(i=1; i<=nVar; i++){ |
| 757 | const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 758 | if( zVar[0]=='$' || zVar[0]==':' ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 759 | Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
| 760 | if( pVar ){ |
| 761 | int n; |
| 762 | u8 *data; |
| 763 | char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
| 764 | char c = zType[0]; |
| 765 | if( c=='b' && strcmp(zType,"bytearray")==0 ){ |
| 766 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 767 | sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 768 | Tcl_IncrRefCount(pVar); |
| 769 | apParm[nParm++] = pVar; |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 770 | }else if( (c=='b' && strcmp(zType,"boolean")==0) || |
| 771 | (c=='i' && strcmp(zType,"int")==0) ){ |
| 772 | Tcl_GetIntFromObj(interp, pVar, &n); |
| 773 | sqlite3_bind_int(pStmt, i, n); |
| 774 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 775 | double r; |
| 776 | Tcl_GetDoubleFromObj(interp, pVar, &r); |
| 777 | sqlite3_bind_double(pStmt, i, r); |
| 778 | }else{ |
| 779 | data = Tcl_GetStringFromObj(pVar, &n); |
| 780 | sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC); |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 781 | Tcl_IncrRefCount(pVar); |
| 782 | apParm[nParm++] = pVar; |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | |
| 789 | /* Compute column names */ |
| 790 | nCol = sqlite3_column_count(pStmt); |
| 791 | if( pScript ){ |
| 792 | apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
| 793 | if( apColName==0 ) break; |
| 794 | for(i=0; i<nCol; i++){ |
| 795 | apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
| 796 | Tcl_IncrRefCount(apColName[i]); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* If results are being stored in an array variable, then create |
| 801 | ** the array(*) entry for that array |
| 802 | */ |
| 803 | if( pArray ){ |
| 804 | Tcl_Obj *pColList = Tcl_NewObj(); |
| 805 | Tcl_IncrRefCount(pColList); |
| 806 | for(i=0; i<nCol; i++){ |
| 807 | Tcl_ListObjAppendElement(interp, pColList, apColName[i]); |
| 808 | } |
| 809 | Tcl_ObjSetVar2(interp, pArray, Tcl_NewStringObj("*",-1), pColList,0); |
| 810 | } |
| 811 | |
| 812 | /* Execute the SQL |
| 813 | */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 814 | while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 815 | for(i=0; i<nCol; i++){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 816 | Tcl_Obj *pVal; |
| 817 | |
| 818 | /* Set pVal to contain the i'th column of this row. */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 819 | switch( sqlite3_column_type(pStmt, i) ){ |
| 820 | case SQLITE_BLOB: { |
| 821 | int bytes = sqlite3_column_bytes(pStmt, i); |
| 822 | pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes); |
| 823 | break; |
| 824 | } |
| 825 | case SQLITE_INTEGER: { |
| 826 | sqlite_int64 v = sqlite3_column_int64(pStmt, i); |
| 827 | if( v>=-2147483647 && v<=2147483647 ){ |
| 828 | pVal = Tcl_NewIntObj(v); |
| 829 | }else{ |
| 830 | pVal = Tcl_NewWideIntObj(v); |
| 831 | } |
| 832 | break; |
| 833 | } |
| 834 | case SQLITE_FLOAT: { |
| 835 | double r = sqlite3_column_double(pStmt, i); |
| 836 | pVal = Tcl_NewDoubleObj(r); |
| 837 | break; |
| 838 | } |
| 839 | default: { |
| 840 | pVal = dbTextToObj(sqlite3_column_text(pStmt, i)); |
| 841 | break; |
| 842 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 843 | } |
| 844 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 845 | if( pScript ){ |
| 846 | if( pArray==0 ){ |
| 847 | Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 848 | }else{ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 849 | Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 850 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 851 | }else{ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 852 | Tcl_ListObjAppendElement(interp, pRet, pVal); |
| 853 | } |
| 854 | } |
| 855 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 856 | if( pScript ){ |
| 857 | rc = Tcl_EvalObjEx(interp, pScript, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 858 | if( rc!=TCL_ERROR ) rc = TCL_OK; |
| 859 | } |
| 860 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 861 | |
| 862 | /* Free the column name objects */ |
| 863 | if( pScript ){ |
| 864 | for(i=0; i<nCol; i++){ |
| 865 | Tcl_DecrRefCount(apColName[i]); |
| 866 | } |
| 867 | Tcl_Free((char*)apColName); |
| 868 | } |
| 869 | |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 870 | /* Free the bound string and blob parameters */ |
| 871 | for(i=0; i<nParm; i++){ |
| 872 | Tcl_DecrRefCount(apParm[i]); |
| 873 | } |
| 874 | if( apParm!=aParm ){ |
| 875 | Tcl_Free((char*)apParm); |
| 876 | } |
| 877 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 878 | /* Finalize the statement. If the result code is SQLITE_SCHEMA, then |
| 879 | ** try again to execute the same statement |
| 880 | */ |
| 881 | if( SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 882 | continue; |
| 883 | } |
| 884 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 885 | if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 886 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 887 | rc = TCL_ERROR; |
| 888 | break; |
| 889 | } |
| 890 | |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 891 | zSql = zLeft; |
| 892 | } |
drh | 1d89503 | 2004-08-26 00:56:05 +0000 | [diff] [blame] | 893 | Tcl_DecrRefCount(objv[2]); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 894 | |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 895 | if( rc==TCL_OK ){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 896 | Tcl_SetObjResult(interp, pRet); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 897 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 898 | Tcl_DecrRefCount(pRet); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 899 | |
| 900 | break; |
| 901 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 902 | |
| 903 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 904 | ** $db function NAME SCRIPT |
| 905 | ** |
| 906 | ** Create a new SQL function called NAME. Whenever that function is |
| 907 | ** called, invoke SCRIPT to evaluate the function. |
| 908 | */ |
| 909 | case DB_FUNCTION: { |
| 910 | SqlFunc *pFunc; |
| 911 | char *zName; |
| 912 | char *zScript; |
| 913 | int nScript; |
| 914 | if( objc!=4 ){ |
| 915 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 916 | return TCL_ERROR; |
| 917 | } |
| 918 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 919 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 920 | pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 ); |
| 921 | if( pFunc==0 ) return TCL_ERROR; |
| 922 | pFunc->interp = interp; |
| 923 | pFunc->pNext = pDb->pFunc; |
| 924 | pFunc->zScript = (char*)&pFunc[1]; |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 925 | pDb->pFunc = pFunc; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 926 | strcpy(pFunc->zScript, zScript); |
danielk1977 | c8c1158 | 2004-06-29 13:41:21 +0000 | [diff] [blame] | 927 | rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 928 | pFunc, tclSqlFunc, 0, 0); |
danielk1977 | c8c1158 | 2004-06-29 13:41:21 +0000 | [diff] [blame] | 929 | if( rc!=SQLITE_OK ) rc = TCL_ERROR; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 930 | break; |
| 931 | } |
| 932 | |
| 933 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 934 | ** $db last_insert_rowid |
| 935 | ** |
| 936 | ** Return an integer which is the ROWID for the most recent insert. |
| 937 | */ |
| 938 | case DB_LAST_INSERT_ROWID: { |
| 939 | Tcl_Obj *pResult; |
| 940 | int rowid; |
| 941 | if( objc!=2 ){ |
| 942 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 943 | return TCL_ERROR; |
| 944 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 945 | rowid = sqlite3_last_insert_rowid(pDb->db); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 946 | pResult = Tcl_GetObjResult(interp); |
| 947 | Tcl_SetIntObj(pResult, rowid); |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 952 | ** $db onecolumn SQL |
| 953 | ** |
| 954 | ** Return a single column from a single row of the given SQL query. |
| 955 | */ |
| 956 | case DB_ONECOLUMN: { |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 957 | char *zSql; |
| 958 | char *zErrMsg = 0; |
| 959 | if( objc!=3 ){ |
| 960 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
| 961 | return TCL_ERROR; |
| 962 | } |
| 963 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 964 | rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg); |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 965 | if( rc==SQLITE_ABORT ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 966 | rc = SQLITE_OK; |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 967 | }else if( zErrMsg ){ |
| 968 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 969 | free(zErrMsg); |
| 970 | rc = TCL_ERROR; |
| 971 | }else if( rc!=SQLITE_OK ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 972 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 973 | rc = TCL_ERROR; |
| 974 | } |
| 975 | break; |
| 976 | } |
| 977 | |
| 978 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 979 | ** $db rekey KEY |
| 980 | ** |
| 981 | ** Change the encryption key on the currently open database. |
| 982 | */ |
| 983 | case DB_REKEY: { |
| 984 | int nKey; |
| 985 | void *pKey; |
| 986 | if( objc!=3 ){ |
| 987 | Tcl_WrongNumArgs(interp, 2, objv, "KEY"); |
| 988 | return TCL_ERROR; |
| 989 | } |
| 990 | pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 991 | #ifdef SQLITE_HAS_CODEC |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 992 | rc = sqlite3_rekey(pDb->db, pKey, nKey); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 993 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 994 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 995 | rc = TCL_ERROR; |
| 996 | } |
| 997 | #endif |
| 998 | break; |
| 999 | } |
| 1000 | |
| 1001 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1002 | ** $db timeout MILLESECONDS |
| 1003 | ** |
| 1004 | ** Delay for the number of milliseconds specified when a file is locked. |
| 1005 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1006 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1007 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1008 | if( objc!=3 ){ |
| 1009 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1010 | return TCL_ERROR; |
| 1011 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1012 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1013 | sqlite3_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1014 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1015 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1016 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 1017 | /* |
| 1018 | ** $db total_changes |
| 1019 | ** |
| 1020 | ** Return the number of rows that were modified, inserted, or deleted |
| 1021 | ** since the database handle was created. |
| 1022 | */ |
| 1023 | case DB_TOTAL_CHANGES: { |
| 1024 | Tcl_Obj *pResult; |
| 1025 | if( objc!=2 ){ |
| 1026 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 1027 | return TCL_ERROR; |
| 1028 | } |
| 1029 | pResult = Tcl_GetObjResult(interp); |
| 1030 | Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); |
| 1031 | break; |
| 1032 | } |
| 1033 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1034 | /* $db trace ?CALLBACK? |
| 1035 | ** |
| 1036 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 1037 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 1038 | ** it is executed. |
| 1039 | */ |
| 1040 | case DB_TRACE: { |
| 1041 | if( objc>3 ){ |
| 1042 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 1043 | return TCL_ERROR; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1044 | }else if( objc==2 ){ |
| 1045 | if( pDb->zTrace ){ |
| 1046 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 1047 | } |
| 1048 | }else{ |
| 1049 | char *zTrace; |
| 1050 | int len; |
| 1051 | if( pDb->zTrace ){ |
| 1052 | Tcl_Free(pDb->zTrace); |
| 1053 | } |
| 1054 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 1055 | if( zTrace && len>0 ){ |
| 1056 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
| 1057 | strcpy(pDb->zTrace, zTrace); |
| 1058 | }else{ |
| 1059 | pDb->zTrace = 0; |
| 1060 | } |
| 1061 | if( pDb->zTrace ){ |
| 1062 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1063 | sqlite3_trace(pDb->db, DbTraceHandler, pDb); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1064 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1065 | sqlite3_trace(pDb->db, 0, 0); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | break; |
| 1069 | } |
| 1070 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1071 | } /* End of the SWITCH statement */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1072 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /* |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 1076 | ** sqlite3 DBNAME FILENAME ?MODE? ?-key KEY? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1077 | ** |
| 1078 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 1079 | ** invoked, this routine runs to process that command. |
| 1080 | ** |
| 1081 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 1082 | ** database connection. This command creates a new command named |
| 1083 | ** DBNAME that is used to control that connection. The database |
| 1084 | ** connection is deleted when the DBNAME command is deleted. |
| 1085 | ** |
| 1086 | ** The second argument is the name of the directory that contains |
| 1087 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1088 | ** |
| 1089 | ** For testing purposes, we also support the following: |
| 1090 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 1091 | ** sqlite3 -encoding |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1092 | ** |
| 1093 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 1094 | ** are UTF-8 and iso8859. |
| 1095 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 1096 | ** sqlite3 -version |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 1097 | ** |
| 1098 | ** Return the version number of the SQLite library. |
| 1099 | ** |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 1100 | ** sqlite3 -tcl-uses-utf |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1101 | ** |
| 1102 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 1103 | ** not. Used by tests to make sure the library was compiled |
| 1104 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1105 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1106 | 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] | 1107 | SqliteDb *p; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1108 | void *pKey = 0; |
| 1109 | int nKey = 0; |
| 1110 | const char *zArg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1111 | char *zErrMsg; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1112 | const char *zFile; |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1113 | char zBuf[80]; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1114 | if( objc==2 ){ |
| 1115 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1116 | if( strcmp(zArg,"-version")==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1117 | Tcl_AppendResult(interp,sqlite3_version,0); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 1118 | return TCL_OK; |
| 1119 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1120 | if( strcmp(zArg,"-has-codec")==0 ){ |
| 1121 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1122 | Tcl_AppendResult(interp,"1",0); |
| 1123 | #else |
| 1124 | Tcl_AppendResult(interp,"0",0); |
| 1125 | #endif |
| 1126 | return TCL_OK; |
| 1127 | } |
| 1128 | if( strcmp(zArg,"-tcl-uses-utf")==0 ){ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1129 | #ifdef TCL_UTF_MAX |
| 1130 | Tcl_AppendResult(interp,"1",0); |
| 1131 | #else |
| 1132 | Tcl_AppendResult(interp,"0",0); |
| 1133 | #endif |
| 1134 | return TCL_OK; |
| 1135 | } |
| 1136 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1137 | if( objc==5 || objc==6 ){ |
| 1138 | zArg = Tcl_GetStringFromObj(objv[objc-2], 0); |
| 1139 | if( strcmp(zArg,"-key")==0 ){ |
| 1140 | pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey); |
| 1141 | objc -= 2; |
| 1142 | } |
| 1143 | } |
| 1144 | if( objc!=3 && objc!=4 ){ |
| 1145 | Tcl_WrongNumArgs(interp, 1, objv, |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1146 | #ifdef SQLITE_HAS_CODEC |
| 1147 | "HANDLE FILENAME ?-key CODEC-KEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1148 | #else |
| 1149 | "HANDLE FILENAME ?MODE?" |
| 1150 | #endif |
| 1151 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1152 | return TCL_ERROR; |
| 1153 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1154 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 1155 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1156 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1157 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 1158 | return TCL_ERROR; |
| 1159 | } |
| 1160 | memset(p, 0, sizeof(*p)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1161 | zFile = Tcl_GetStringFromObj(objv[2], 0); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1162 | sqlite3_open(zFile, &p->db); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 1163 | if( SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 1164 | zErrMsg = strdup(sqlite3_errmsg(p->db)); |
| 1165 | sqlite3_close(p->db); |
| 1166 | p->db = 0; |
| 1167 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1168 | #ifdef SQLITE_HAS_CODEC |
| 1169 | sqlite3_key(p->db, pKey, nKey); |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 1170 | #endif |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1171 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1172 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1173 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1174 | free(zErrMsg); |
| 1175 | return TCL_ERROR; |
| 1176 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1177 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
| 1178 | Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1179 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1180 | /* The return value is the value of the sqlite* pointer |
| 1181 | */ |
| 1182 | sprintf(zBuf, "%p", p->db); |
drh | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame] | 1183 | if( strncmp(zBuf,"0x",2) ){ |
| 1184 | sprintf(zBuf, "0x%p", p->db); |
| 1185 | } |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1186 | Tcl_AppendResult(interp, zBuf, 0); |
| 1187 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1188 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1189 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1190 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 1191 | #ifdef SQLITE_TEST |
| 1192 | { |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame^] | 1193 | extern void Md5_Register(sqlite3*); |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1194 | #ifdef SQLITE_DEBUG |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 1195 | int mallocfail = sqlite3_iMallocFail; |
| 1196 | sqlite3_iMallocFail = 0; |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1197 | #endif |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1198 | Md5_Register(p->db); |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1199 | #ifdef SQLITE_DEBUG |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 1200 | sqlite3_iMallocFail = mallocfail; |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1201 | #endif |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1202 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 1203 | #endif |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1204 | p->interp = interp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1205 | return TCL_OK; |
| 1206 | } |
| 1207 | |
| 1208 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1209 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 1210 | ** library. |
| 1211 | */ |
| 1212 | #ifndef USE_TCL_STUBS |
| 1213 | # undef Tcl_InitStubs |
| 1214 | # define Tcl_InitStubs(a,b,c) |
| 1215 | #endif |
| 1216 | |
| 1217 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1218 | ** Initialize this module. |
| 1219 | ** |
| 1220 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 1221 | ** (Hence there is no namespace. There is no point in using a namespace |
| 1222 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 1223 | ** used to open a new SQLite database. See the DbMain() routine above |
| 1224 | ** for additional information. |
| 1225 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1226 | int Sqlite3_Init(Tcl_Interp *interp){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1227 | Tcl_InitStubs(interp, "8.4", 0); |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 1228 | Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
| 1229 | Tcl_PkgProvide(interp, "sqlite3", "3.0"); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1230 | return TCL_OK; |
| 1231 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1232 | int Tclsqlite3_Init(Tcl_Interp *interp){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1233 | Tcl_InitStubs(interp, "8.4", 0); |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 1234 | Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
| 1235 | Tcl_PkgProvide(interp, "sqlite3", "3.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1236 | return TCL_OK; |
| 1237 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1238 | int Sqlite3_SafeInit(Tcl_Interp *interp){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1239 | return TCL_OK; |
| 1240 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1241 | int Tclsqlite3_SafeInit(Tcl_Interp *interp){ |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1242 | return TCL_OK; |
| 1243 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1244 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1245 | #ifdef TCLSH |
| 1246 | /***************************************************************************** |
| 1247 | ** The code that follows is used to build standalone TCL interpreters |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1248 | */ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1249 | |
| 1250 | /* |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1251 | ** If the macro TCLSH is one, then put in code this for the |
| 1252 | ** "main" routine that will initialize Tcl and take input from |
| 1253 | ** standard input. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1254 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1255 | #if TCLSH==1 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1256 | static char zMainloop[] = |
| 1257 | "set line {}\n" |
| 1258 | "while {![eof stdin]} {\n" |
| 1259 | "if {$line!=\"\"} {\n" |
| 1260 | "puts -nonewline \"> \"\n" |
| 1261 | "} else {\n" |
| 1262 | "puts -nonewline \"% \"\n" |
| 1263 | "}\n" |
| 1264 | "flush stdout\n" |
| 1265 | "append line [gets stdin]\n" |
| 1266 | "if {[info complete $line]} {\n" |
| 1267 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 1268 | "puts stderr \"Error: $result\"\n" |
| 1269 | "} elseif {$result!=\"\"} {\n" |
| 1270 | "puts $result\n" |
| 1271 | "}\n" |
| 1272 | "set line {}\n" |
| 1273 | "} else {\n" |
| 1274 | "append line \\n\n" |
| 1275 | "}\n" |
| 1276 | "}\n" |
| 1277 | ; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1278 | #endif |
| 1279 | |
| 1280 | /* |
| 1281 | ** If the macro TCLSH is two, then get the main loop code out of |
| 1282 | ** the separate file "spaceanal_tcl.h". |
| 1283 | */ |
| 1284 | #if TCLSH==2 |
| 1285 | static char zMainloop[] = |
| 1286 | #include "spaceanal_tcl.h" |
| 1287 | ; |
| 1288 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1289 | |
| 1290 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 1291 | int TCLSH_MAIN(int argc, char **argv){ |
| 1292 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1293 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1294 | interp = Tcl_CreateInterp(); |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1295 | Sqlite3_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1296 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1297 | { |
| 1298 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1299 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 1300 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 1301 | extern int Sqlitetest4_Init(Tcl_Interp*); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 1302 | extern int Sqlitetest5_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1303 | extern int Md5_Init(Tcl_Interp*); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 1304 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1305 | Sqlitetest2_Init(interp); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1306 | Sqlitetest3_Init(interp); |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1307 | Sqlitetest4_Init(interp); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 1308 | Sqlitetest5_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1309 | Md5_Init(interp); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1310 | } |
| 1311 | #endif |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1312 | if( argc>=2 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1313 | int i; |
| 1314 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 1315 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 1316 | for(i=2; i<argc; i++){ |
| 1317 | Tcl_SetVar(interp, "argv", argv[i], |
| 1318 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 1319 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1320 | if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1321 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1322 | if( zInfo==0 ) zInfo = interp->result; |
| 1323 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1324 | return 1; |
| 1325 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1326 | } |
| 1327 | if( argc<=1 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1328 | Tcl_GlobalEval(interp, zMainloop); |
| 1329 | } |
| 1330 | return 0; |
| 1331 | } |
| 1332 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1333 | |
| 1334 | #endif /* !defined(NO_TCL) */ |