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