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 | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame^] | 14 | ** $Id: tclsqlite.c,v 1.50 2003/08/19 14:31: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 | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 19 | #include "tcl.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 22 | #include <assert.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 25 | ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
| 26 | ** have to do a translation when going between the two. Set the |
| 27 | ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
| 28 | ** this translation. |
| 29 | */ |
| 30 | #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
| 31 | # define UTF_TRANSLATION_NEEDED 1 |
| 32 | #endif |
| 33 | |
| 34 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 35 | ** New SQL functions can be created as TCL scripts. Each such function |
| 36 | ** is described by an instance of the following structure. |
| 37 | */ |
| 38 | typedef struct SqlFunc SqlFunc; |
| 39 | struct SqlFunc { |
| 40 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 41 | char *zScript; /* The script to be run */ |
| 42 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 43 | }; |
| 44 | |
| 45 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 46 | ** There is one instance of this structure for each SQLite database |
| 47 | ** that has been opened by the SQLite TCL interface. |
| 48 | */ |
| 49 | typedef struct SqliteDb SqliteDb; |
| 50 | struct SqliteDb { |
| 51 | sqlite *db; /* The "real" database structure */ |
| 52 | Tcl_Interp *interp; /* The interpreter used for this database */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 53 | char *zBusy; /* The busy callback routine */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 54 | char *zTrace; /* The trace callback routine */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 55 | char *zAuth; /* The authorization callback routine */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 56 | SqlFunc *pFunc; /* List of SQL functions */ |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 57 | int rc; /* Return code of most recent sqlite_exec() */ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 61 | ** An instance of this structure passes information thru the sqlite |
| 62 | ** logic from the original TCL command into the callback routine. |
| 63 | */ |
| 64 | typedef struct CallbackData CallbackData; |
| 65 | struct CallbackData { |
| 66 | Tcl_Interp *interp; /* The TCL interpreter */ |
| 67 | char *zArray; /* The array into which data is written */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 68 | Tcl_Obj *pCode; /* The code to execute for each row */ |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 69 | int once; /* Set for first callback only */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 70 | int tcl_rc; /* Return code from TCL script */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 71 | int nColName; /* Number of entries in the azColName[] array */ |
| 72 | char **azColName; /* Column names translated to UTF-8 */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 73 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 74 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 75 | #ifdef UTF_TRANSLATION_NEEDED |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 76 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 77 | ** Called for each row of the result. |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 78 | ** |
| 79 | ** This version is used when TCL expects UTF-8 data but the database |
| 80 | ** uses the ISO8859 format. A translation must occur from ISO8859 into |
| 81 | ** UTF-8. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 82 | */ |
| 83 | static int DbEvalCallback( |
| 84 | void *clientData, /* An instance of CallbackData */ |
| 85 | int nCol, /* Number of columns in the result */ |
| 86 | char ** azCol, /* Data for each column */ |
| 87 | char ** azN /* Name for each column */ |
| 88 | ){ |
| 89 | CallbackData *cbData = (CallbackData*)clientData; |
| 90 | int i, rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 91 | Tcl_DString dCol; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 92 | Tcl_DStringInit(&dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 93 | if( cbData->azColName==0 ){ |
| 94 | assert( cbData->once ); |
| 95 | cbData->once = 0; |
| 96 | if( cbData->zArray[0] ){ |
| 97 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 98 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 99 | cbData->azColName = malloc( nCol*sizeof(char*) ); |
| 100 | if( cbData->azColName==0 ){ return 1; } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 101 | cbData->nColName = nCol; |
| 102 | for(i=0; i<nCol; i++){ |
| 103 | Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 104 | cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 ); |
| 105 | if( cbData->azColName[i] ){ |
| 106 | strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol)); |
| 107 | }else{ |
| 108 | return 1; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 109 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 110 | if( cbData->zArray[0] ){ |
| 111 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", |
| 112 | Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
drh | 704027f | 2002-07-15 20:58:47 +0000 | [diff] [blame] | 113 | if( azN[nCol]!=0 ){ |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 114 | Tcl_DString dType; |
| 115 | Tcl_DStringInit(&dType); |
| 116 | Tcl_DStringAppend(&dType, "typeof:", -1); |
| 117 | Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1); |
| 118 | Tcl_DStringFree(&dCol); |
| 119 | Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol); |
| 120 | Tcl_SetVar2(cbData->interp, cbData->zArray, |
| 121 | Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol), |
| 122 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 123 | Tcl_DStringFree(&dType); |
| 124 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 125 | } |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 126 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 127 | Tcl_DStringFree(&dCol); |
| 128 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 129 | } |
| 130 | if( azCol!=0 ){ |
| 131 | if( cbData->zArray[0] ){ |
| 132 | for(i=0; i<nCol; i++){ |
| 133 | char *z = azCol[i]; |
| 134 | if( z==0 ) z = ""; |
| 135 | Tcl_DStringInit(&dCol); |
| 136 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 137 | Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i], |
| 138 | Tcl_DStringValue(&dCol), 0); |
| 139 | Tcl_DStringFree(&dCol); |
| 140 | } |
| 141 | }else{ |
| 142 | for(i=0; i<nCol; i++){ |
| 143 | char *z = azCol[i]; |
| 144 | if( z==0 ) z = ""; |
| 145 | Tcl_DStringInit(&dCol); |
| 146 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 147 | Tcl_SetVar(cbData->interp, cbData->azColName[i], |
| 148 | Tcl_DStringValue(&dCol), 0); |
| 149 | Tcl_DStringFree(&dCol); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
| 154 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 155 | cbData->tcl_rc = rc; |
| 156 | return rc!=TCL_OK; |
| 157 | } |
| 158 | #endif /* UTF_TRANSLATION_NEEDED */ |
| 159 | |
| 160 | #ifndef UTF_TRANSLATION_NEEDED |
| 161 | /* |
| 162 | ** Called for each row of the result. |
| 163 | ** |
| 164 | ** This version is used when either of the following is true: |
| 165 | ** |
| 166 | ** (1) This version of TCL uses UTF-8 and the data in the |
| 167 | ** SQLite database is already in the UTF-8 format. |
| 168 | ** |
| 169 | ** (2) This version of TCL uses ISO8859 and the data in the |
| 170 | ** SQLite database is already in the ISO8859 format. |
| 171 | */ |
| 172 | static int DbEvalCallback( |
| 173 | void *clientData, /* An instance of CallbackData */ |
| 174 | int nCol, /* Number of columns in the result */ |
| 175 | char ** azCol, /* Data for each column */ |
| 176 | char ** azN /* Name for each column */ |
| 177 | ){ |
| 178 | CallbackData *cbData = (CallbackData*)clientData; |
| 179 | int i, rc; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 180 | if( azCol==0 || (cbData->once && cbData->zArray[0]) ){ |
| 181 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
| 182 | for(i=0; i<nCol; i++){ |
| 183 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], |
| 184 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 185 | if( azN[nCol] ){ |
| 186 | char *z = sqlite_mprintf("typeof:%s", azN[i]); |
| 187 | Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol], |
| 188 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 189 | sqlite_freemem(z); |
| 190 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 191 | } |
| 192 | cbData->once = 0; |
| 193 | } |
| 194 | if( azCol!=0 ){ |
| 195 | if( cbData->zArray[0] ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 196 | for(i=0; i<nCol; i++){ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 197 | char *z = azCol[i]; |
| 198 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 199 | Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 200 | } |
| 201 | }else{ |
| 202 | for(i=0; i<nCol; i++){ |
| 203 | char *z = azCol[i]; |
| 204 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 205 | Tcl_SetVar(cbData->interp, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 208 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 209 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 210 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 211 | cbData->tcl_rc = rc; |
| 212 | return rc!=TCL_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 213 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 214 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 215 | |
| 216 | /* |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 217 | ** This is an alternative callback for database queries. Instead |
| 218 | ** of invoking a TCL script to handle the result, this callback just |
| 219 | ** appends each column of the result to a list. After the query |
| 220 | ** is complete, the list is returned. |
| 221 | */ |
| 222 | static int DbEvalCallback2( |
| 223 | void *clientData, /* An instance of CallbackData */ |
| 224 | int nCol, /* Number of columns in the result */ |
| 225 | char ** azCol, /* Data for each column */ |
| 226 | char ** azN /* Name for each column */ |
| 227 | ){ |
| 228 | Tcl_Obj *pList = (Tcl_Obj*)clientData; |
| 229 | int i; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 230 | if( azCol==0 ) return 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 231 | for(i=0; i<nCol; i++){ |
| 232 | Tcl_Obj *pElem; |
| 233 | if( azCol[i] && *azCol[i] ){ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 234 | #ifdef UTF_TRANSLATION_NEEDED |
| 235 | Tcl_DString dCol; |
| 236 | Tcl_DStringInit(&dCol); |
| 237 | Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol); |
| 238 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 239 | Tcl_DStringFree(&dCol); |
| 240 | #else |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 241 | pElem = Tcl_NewStringObj(azCol[i], -1); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 242 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 243 | }else{ |
| 244 | pElem = Tcl_NewObj(); |
| 245 | } |
| 246 | Tcl_ListObjAppendElement(0, pList, pElem); |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame^] | 252 | ** This is a second alternative callback for database queries. A the |
| 253 | ** first column of the first row of the result is made the TCL result. |
| 254 | */ |
| 255 | static int DbEvalCallback3( |
| 256 | void *clientData, /* An instance of CallbackData */ |
| 257 | int nCol, /* Number of columns in the result */ |
| 258 | char ** azCol, /* Data for each column */ |
| 259 | char ** azN /* Name for each column */ |
| 260 | ){ |
| 261 | Tcl_Interp *interp = (Tcl_Interp*)clientData; |
| 262 | Tcl_Obj *pElem; |
| 263 | if( azCol==0 ) return 1; |
| 264 | if( nCol==0 ) return 1; |
| 265 | #ifdef UTF_TRANSLATION_NEEDED |
| 266 | { |
| 267 | Tcl_DString dCol; |
| 268 | Tcl_DStringInit(&dCol); |
| 269 | Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol); |
| 270 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 271 | Tcl_DStringFree(&dCol); |
| 272 | } |
| 273 | #else |
| 274 | pElem = Tcl_NewStringObj(azCol[0], -1); |
| 275 | #endif |
| 276 | Tcl_SetObjResult(interp, pElem); |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 281 | ** Called when the command is deleted. |
| 282 | */ |
| 283 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 284 | SqliteDb *pDb = (SqliteDb*)db; |
| 285 | sqlite_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 286 | while( pDb->pFunc ){ |
| 287 | SqlFunc *pFunc = pDb->pFunc; |
| 288 | pDb->pFunc = pFunc->pNext; |
| 289 | Tcl_Free((char*)pFunc); |
| 290 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 291 | if( pDb->zBusy ){ |
| 292 | Tcl_Free(pDb->zBusy); |
| 293 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 294 | if( pDb->zTrace ){ |
| 295 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 296 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 297 | if( pDb->zAuth ){ |
| 298 | Tcl_Free(pDb->zAuth); |
| 299 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 300 | Tcl_Free((char*)pDb); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | ** This routine is called when a database file is locked while trying |
| 305 | ** to execute SQL. |
| 306 | */ |
| 307 | static int DbBusyHandler(void *cd, const char *zTable, int nTries){ |
| 308 | SqliteDb *pDb = (SqliteDb*)cd; |
| 309 | int rc; |
| 310 | char zVal[30]; |
| 311 | char *zCmd; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 312 | Tcl_DString cmd; |
| 313 | |
| 314 | Tcl_DStringInit(&cmd); |
| 315 | Tcl_DStringAppend(&cmd, pDb->zBusy, -1); |
| 316 | Tcl_DStringAppendElement(&cmd, zTable); |
| 317 | sprintf(zVal, " %d", nTries); |
| 318 | Tcl_DStringAppend(&cmd, zVal, -1); |
| 319 | zCmd = Tcl_DStringValue(&cmd); |
| 320 | rc = Tcl_Eval(pDb->interp, zCmd); |
| 321 | Tcl_DStringFree(&cmd); |
| 322 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 323 | return 0; |
| 324 | } |
| 325 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 329 | ** This routine is called by the SQLite trace handler whenever a new |
| 330 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 331 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 332 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 333 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 334 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 335 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 336 | Tcl_DStringInit(&str); |
| 337 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 338 | Tcl_DStringAppendElement(&str, zSql); |
| 339 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 340 | Tcl_DStringFree(&str); |
| 341 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 345 | ** This routine is called to evaluate an SQL function implemented |
| 346 | ** using TCL script. |
| 347 | */ |
| 348 | static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){ |
| 349 | SqlFunc *p = sqlite_user_data(context); |
| 350 | Tcl_DString cmd; |
| 351 | int i; |
| 352 | int rc; |
| 353 | |
| 354 | Tcl_DStringInit(&cmd); |
| 355 | Tcl_DStringAppend(&cmd, p->zScript, -1); |
| 356 | for(i=0; i<argc; i++){ |
| 357 | Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : ""); |
| 358 | } |
| 359 | rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd)); |
| 360 | if( rc ){ |
| 361 | sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 362 | }else{ |
| 363 | sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1); |
| 364 | } |
| 365 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 366 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 367 | /* |
| 368 | ** This is the authentication function. It appends the authentication |
| 369 | ** type code and the two arguments to zCmd[] then invokes the result |
| 370 | ** on the interpreter. The reply is examined to determine if the |
| 371 | ** authentication fails or succeeds. |
| 372 | */ |
| 373 | static int auth_callback( |
| 374 | void *pArg, |
| 375 | int code, |
| 376 | const char *zArg1, |
| 377 | const char *zArg2, |
| 378 | const char *zArg3, |
| 379 | const char *zArg4 |
| 380 | ){ |
| 381 | char *zCode; |
| 382 | Tcl_DString str; |
| 383 | int rc; |
| 384 | const char *zReply; |
| 385 | SqliteDb *pDb = (SqliteDb*)pArg; |
| 386 | |
| 387 | switch( code ){ |
| 388 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 389 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 390 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 391 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 392 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 393 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 394 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 395 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 396 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 397 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 398 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 399 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 400 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 401 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 402 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 403 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 404 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 405 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 406 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 407 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 408 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 409 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 410 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 411 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
drh | 81e293b | 2003-06-06 19:00:42 +0000 | [diff] [blame] | 412 | case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
| 413 | case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 414 | default : zCode="????"; break; |
| 415 | } |
| 416 | Tcl_DStringInit(&str); |
| 417 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 418 | Tcl_DStringAppendElement(&str, zCode); |
| 419 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 420 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 421 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 422 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 423 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 424 | Tcl_DStringFree(&str); |
| 425 | zReply = Tcl_GetStringResult(pDb->interp); |
| 426 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 427 | rc = SQLITE_OK; |
| 428 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 429 | rc = SQLITE_DENY; |
| 430 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 431 | rc = SQLITE_IGNORE; |
| 432 | }else{ |
| 433 | rc = 999; |
| 434 | } |
| 435 | return rc; |
| 436 | } |
| 437 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 438 | |
| 439 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 440 | ** The "sqlite" command below creates a new Tcl command for each |
| 441 | ** connection it opens to an SQLite database. This routine is invoked |
| 442 | ** whenever one of those connection-specific commands is executed |
| 443 | ** in Tcl. For example, if you run Tcl code like this: |
| 444 | ** |
| 445 | ** sqlite db1 "my_database" |
| 446 | ** db1 close |
| 447 | ** |
| 448 | ** The first command opens a connection to the "my_database" database |
| 449 | ** and calls that connection "db1". The second command causes this |
| 450 | ** subroutine to be invoked. |
| 451 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 452 | 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] | 453 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 454 | int choice; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 455 | static const char *DB_strs[] = { |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 456 | "authorizer", "busy", "changes", |
| 457 | "close", "complete", "errorcode", |
| 458 | "eval", "function", "last_insert_rowid", |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame^] | 459 | "onecolumn", "timeout", "trace", |
| 460 | 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 461 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 462 | enum DB_enum { |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 463 | DB_AUTHORIZER, DB_BUSY, DB_CHANGES, |
| 464 | DB_CLOSE, DB_COMPLETE, DB_ERRORCODE, |
| 465 | DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID, |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame^] | 466 | DB_ONECOLUMN, DB_TIMEOUT, DB_TRACE, |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | if( objc<2 ){ |
| 470 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 471 | return TCL_ERROR; |
| 472 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 473 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 474 | return TCL_ERROR; |
| 475 | } |
| 476 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 477 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 478 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 479 | /* $db authorizer ?CALLBACK? |
| 480 | ** |
| 481 | ** Invoke the given callback to authorize each SQL operation as it is |
| 482 | ** compiled. 5 arguments are appended to the callback before it is |
| 483 | ** invoked: |
| 484 | ** |
| 485 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 486 | ** (2) First descriptive name (depends on authorization type) |
| 487 | ** (3) Second descriptive name |
| 488 | ** (4) Name of the database (ex: "main", "temp") |
| 489 | ** (5) Name of trigger that is doing the access |
| 490 | ** |
| 491 | ** The callback should return on of the following strings: SQLITE_OK, |
| 492 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 493 | ** |
| 494 | ** If this method is invoked with no arguments, the current authorization |
| 495 | ** callback string is returned. |
| 496 | */ |
| 497 | case DB_AUTHORIZER: { |
| 498 | if( objc>3 ){ |
| 499 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 500 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 501 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 502 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 503 | } |
| 504 | }else{ |
| 505 | char *zAuth; |
| 506 | int len; |
| 507 | if( pDb->zAuth ){ |
| 508 | Tcl_Free(pDb->zAuth); |
| 509 | } |
| 510 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 511 | if( zAuth && len>0 ){ |
| 512 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
| 513 | strcpy(pDb->zAuth, zAuth); |
| 514 | }else{ |
| 515 | pDb->zAuth = 0; |
| 516 | } |
| 517 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 518 | if( pDb->zAuth ){ |
| 519 | pDb->interp = interp; |
| 520 | sqlite_set_authorizer(pDb->db, auth_callback, pDb); |
| 521 | }else{ |
| 522 | sqlite_set_authorizer(pDb->db, 0, 0); |
| 523 | } |
| 524 | #endif |
| 525 | } |
| 526 | break; |
| 527 | } |
| 528 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 529 | /* $db busy ?CALLBACK? |
| 530 | ** |
| 531 | ** Invoke the given callback if an SQL statement attempts to open |
| 532 | ** a locked database file. |
| 533 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 534 | case DB_BUSY: { |
| 535 | if( objc>3 ){ |
| 536 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 537 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 538 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 539 | if( pDb->zBusy ){ |
| 540 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 541 | } |
| 542 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 543 | char *zBusy; |
| 544 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 545 | if( pDb->zBusy ){ |
| 546 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 547 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 548 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 549 | if( zBusy && len>0 ){ |
| 550 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 551 | strcpy(pDb->zBusy, zBusy); |
| 552 | }else{ |
| 553 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 554 | } |
| 555 | if( pDb->zBusy ){ |
| 556 | pDb->interp = interp; |
| 557 | sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 558 | }else{ |
| 559 | sqlite_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 562 | break; |
| 563 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 564 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 565 | /* |
| 566 | ** $db changes |
| 567 | ** |
| 568 | ** Return the number of rows that were modified, inserted, or deleted by |
| 569 | ** the most recent "eval". |
| 570 | */ |
| 571 | case DB_CHANGES: { |
| 572 | Tcl_Obj *pResult; |
| 573 | int nChange; |
| 574 | if( objc!=2 ){ |
| 575 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 576 | return TCL_ERROR; |
| 577 | } |
| 578 | nChange = sqlite_changes(pDb->db); |
| 579 | pResult = Tcl_GetObjResult(interp); |
| 580 | Tcl_SetIntObj(pResult, nChange); |
| 581 | break; |
| 582 | } |
| 583 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 584 | /* $db close |
| 585 | ** |
| 586 | ** Shutdown the database |
| 587 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 588 | case DB_CLOSE: { |
| 589 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 590 | break; |
| 591 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 592 | |
| 593 | /* $db complete SQL |
| 594 | ** |
| 595 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 596 | ** additional lines of input are needed. This is similar to the |
| 597 | ** built-in "info complete" command of Tcl. |
| 598 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 599 | case DB_COMPLETE: { |
| 600 | Tcl_Obj *pResult; |
| 601 | int isComplete; |
| 602 | if( objc!=3 ){ |
| 603 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 604 | return TCL_ERROR; |
| 605 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 606 | isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
| 607 | pResult = Tcl_GetObjResult(interp); |
| 608 | Tcl_SetBooleanObj(pResult, isComplete); |
| 609 | break; |
| 610 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | ** $db errorcode |
| 614 | ** |
| 615 | ** Return the numeric error code that was returned by the most recent |
| 616 | ** call to sqlite_exec(). |
| 617 | */ |
| 618 | case DB_ERRORCODE: { |
| 619 | Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc)); |
| 620 | break; |
| 621 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | ** $db eval $sql ?array { ...code... }? |
| 625 | ** |
| 626 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 627 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 628 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 629 | ** If "array" is an empty string, then the values are placed in variables |
| 630 | ** that have the same name as the fields extracted by the query. |
| 631 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 632 | case DB_EVAL: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 633 | CallbackData cbData; |
| 634 | char *zErrMsg; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 635 | char *zSql; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 636 | int rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 637 | #ifdef UTF_TRANSLATION_NEEDED |
| 638 | Tcl_DString dSql; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 639 | int i; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 640 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 641 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 642 | if( objc!=5 && objc!=3 ){ |
| 643 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 644 | return TCL_ERROR; |
| 645 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 646 | pDb->interp = interp; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 647 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 648 | #ifdef UTF_TRANSLATION_NEEDED |
| 649 | Tcl_DStringInit(&dSql); |
| 650 | Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); |
| 651 | zSql = Tcl_DStringValue(&dSql); |
| 652 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 653 | Tcl_IncrRefCount(objv[2]); |
| 654 | if( objc==5 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 655 | cbData.interp = interp; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 656 | cbData.once = 1; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 657 | cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); |
| 658 | cbData.pCode = objv[4]; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 659 | cbData.tcl_rc = TCL_OK; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 660 | cbData.nColName = 0; |
| 661 | cbData.azColName = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 662 | zErrMsg = 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 663 | Tcl_IncrRefCount(objv[3]); |
| 664 | Tcl_IncrRefCount(objv[4]); |
| 665 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); |
| 666 | Tcl_DecrRefCount(objv[4]); |
| 667 | Tcl_DecrRefCount(objv[3]); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 668 | if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 669 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 670 | Tcl_Obj *pList = Tcl_NewObj(); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 671 | cbData.tcl_rc = TCL_OK; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 672 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); |
| 673 | Tcl_SetObjResult(interp, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 674 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 675 | pDb->rc = rc; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 676 | if( rc==SQLITE_ABORT ){ |
| 677 | if( zErrMsg ) free(zErrMsg); |
| 678 | rc = cbData.tcl_rc; |
| 679 | }else if( zErrMsg ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 680 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 681 | free(zErrMsg); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 682 | rc = TCL_ERROR; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 683 | }else if( rc!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 684 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 685 | rc = TCL_ERROR; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 686 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 687 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 688 | Tcl_DecrRefCount(objv[2]); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 689 | #ifdef UTF_TRANSLATION_NEEDED |
| 690 | Tcl_DStringFree(&dSql); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 691 | if( objc==5 && cbData.azColName ){ |
| 692 | for(i=0; i<cbData.nColName; i++){ |
| 693 | if( cbData.azColName[i] ) free(cbData.azColName[i]); |
| 694 | } |
| 695 | free(cbData.azColName); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 696 | cbData.azColName = 0; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 697 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 698 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 699 | return rc; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 700 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 701 | |
| 702 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 703 | ** $db function NAME SCRIPT |
| 704 | ** |
| 705 | ** Create a new SQL function called NAME. Whenever that function is |
| 706 | ** called, invoke SCRIPT to evaluate the function. |
| 707 | */ |
| 708 | case DB_FUNCTION: { |
| 709 | SqlFunc *pFunc; |
| 710 | char *zName; |
| 711 | char *zScript; |
| 712 | int nScript; |
| 713 | if( objc!=4 ){ |
| 714 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 715 | return TCL_ERROR; |
| 716 | } |
| 717 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 718 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 719 | pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 ); |
| 720 | if( pFunc==0 ) return TCL_ERROR; |
| 721 | pFunc->interp = interp; |
| 722 | pFunc->pNext = pDb->pFunc; |
| 723 | pFunc->zScript = (char*)&pFunc[1]; |
| 724 | strcpy(pFunc->zScript, zScript); |
| 725 | sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc); |
| 726 | sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC); |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 731 | ** $db last_insert_rowid |
| 732 | ** |
| 733 | ** Return an integer which is the ROWID for the most recent insert. |
| 734 | */ |
| 735 | case DB_LAST_INSERT_ROWID: { |
| 736 | Tcl_Obj *pResult; |
| 737 | int rowid; |
| 738 | if( objc!=2 ){ |
| 739 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 740 | return TCL_ERROR; |
| 741 | } |
| 742 | rowid = sqlite_last_insert_rowid(pDb->db); |
| 743 | pResult = Tcl_GetObjResult(interp); |
| 744 | Tcl_SetIntObj(pResult, rowid); |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame^] | 749 | ** $db onecolumn SQL |
| 750 | ** |
| 751 | ** Return a single column from a single row of the given SQL query. |
| 752 | */ |
| 753 | case DB_ONECOLUMN: { |
| 754 | int rc; |
| 755 | char *zSql; |
| 756 | char *zErrMsg = 0; |
| 757 | if( objc!=3 ){ |
| 758 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
| 759 | return TCL_ERROR; |
| 760 | } |
| 761 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
| 762 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg); |
| 763 | if( rc==SQLITE_ABORT ){ |
| 764 | /* Do nothing. This is normal. */ |
| 765 | }else if( zErrMsg ){ |
| 766 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 767 | free(zErrMsg); |
| 768 | rc = TCL_ERROR; |
| 769 | }else if( rc!=SQLITE_OK ){ |
| 770 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 771 | rc = TCL_ERROR; |
| 772 | } |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 777 | ** $db timeout MILLESECONDS |
| 778 | ** |
| 779 | ** Delay for the number of milliseconds specified when a file is locked. |
| 780 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 781 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 782 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 783 | if( objc!=3 ){ |
| 784 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 785 | return TCL_ERROR; |
| 786 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 787 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 788 | sqlite_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 789 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 790 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 791 | |
| 792 | /* $db trace ?CALLBACK? |
| 793 | ** |
| 794 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 795 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 796 | ** it is executed. |
| 797 | */ |
| 798 | case DB_TRACE: { |
| 799 | if( objc>3 ){ |
| 800 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 801 | }else if( objc==2 ){ |
| 802 | if( pDb->zTrace ){ |
| 803 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 804 | } |
| 805 | }else{ |
| 806 | char *zTrace; |
| 807 | int len; |
| 808 | if( pDb->zTrace ){ |
| 809 | Tcl_Free(pDb->zTrace); |
| 810 | } |
| 811 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 812 | if( zTrace && len>0 ){ |
| 813 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
| 814 | strcpy(pDb->zTrace, zTrace); |
| 815 | }else{ |
| 816 | pDb->zTrace = 0; |
| 817 | } |
| 818 | if( pDb->zTrace ){ |
| 819 | pDb->interp = interp; |
| 820 | sqlite_trace(pDb->db, DbTraceHandler, pDb); |
| 821 | }else{ |
| 822 | sqlite_trace(pDb->db, 0, 0); |
| 823 | } |
| 824 | } |
| 825 | break; |
| 826 | } |
| 827 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 828 | } /* End of the SWITCH statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 829 | return TCL_OK; |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | ** sqlite DBNAME FILENAME ?MODE? |
| 834 | ** |
| 835 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 836 | ** invoked, this routine runs to process that command. |
| 837 | ** |
| 838 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 839 | ** database connection. This command creates a new command named |
| 840 | ** DBNAME that is used to control that connection. The database |
| 841 | ** connection is deleted when the DBNAME command is deleted. |
| 842 | ** |
| 843 | ** The second argument is the name of the directory that contains |
| 844 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 845 | ** |
| 846 | ** For testing purposes, we also support the following: |
| 847 | ** |
| 848 | ** sqlite -encoding |
| 849 | ** |
| 850 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 851 | ** are UTF-8 and iso8859. |
| 852 | ** |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 853 | ** sqlite -version |
| 854 | ** |
| 855 | ** Return the version number of the SQLite library. |
| 856 | ** |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 857 | ** sqlite -tcl-uses-utf |
| 858 | ** |
| 859 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 860 | ** not. Used by tests to make sure the library was compiled |
| 861 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 862 | */ |
| 863 | static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ |
| 864 | int mode; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 865 | SqliteDb *p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 866 | char *zErrMsg; |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 867 | char zBuf[80]; |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 868 | if( argc==2 ){ |
| 869 | if( strcmp(argv[1],"-encoding")==0 ){ |
| 870 | Tcl_AppendResult(interp,sqlite_encoding,0); |
| 871 | return TCL_OK; |
| 872 | } |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 873 | if( strcmp(argv[1],"-version")==0 ){ |
| 874 | Tcl_AppendResult(interp,sqlite_version,0); |
| 875 | return TCL_OK; |
| 876 | } |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 877 | if( strcmp(argv[1],"-tcl-uses-utf")==0 ){ |
| 878 | #ifdef TCL_UTF_MAX |
| 879 | Tcl_AppendResult(interp,"1",0); |
| 880 | #else |
| 881 | Tcl_AppendResult(interp,"0",0); |
| 882 | #endif |
| 883 | return TCL_OK; |
| 884 | } |
| 885 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 886 | if( argc!=3 && argc!=4 ){ |
| 887 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 888 | " HANDLE FILENAME ?MODE?\"", 0); |
| 889 | return TCL_ERROR; |
| 890 | } |
| 891 | if( argc==3 ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 892 | mode = 0666; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 893 | }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ |
| 894 | return TCL_ERROR; |
| 895 | } |
| 896 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 897 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 898 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 899 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 900 | return TCL_ERROR; |
| 901 | } |
| 902 | memset(p, 0, sizeof(*p)); |
| 903 | p->db = sqlite_open(argv[2], mode, &zErrMsg); |
| 904 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 905 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 906 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 907 | free(zErrMsg); |
| 908 | return TCL_ERROR; |
| 909 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 910 | Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 911 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 912 | /* The return value is the value of the sqlite* pointer |
| 913 | */ |
| 914 | sprintf(zBuf, "%p", p->db); |
drh | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame] | 915 | if( strncmp(zBuf,"0x",2) ){ |
| 916 | sprintf(zBuf, "0x%p", p->db); |
| 917 | } |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 918 | Tcl_AppendResult(interp, zBuf, 0); |
| 919 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 920 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 921 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 922 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 923 | #ifdef SQLITE_TEST |
| 924 | { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 925 | extern void Md5_Register(sqlite*); |
| 926 | Md5_Register(p->db); |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 927 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 928 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 929 | return TCL_OK; |
| 930 | } |
| 931 | |
| 932 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 933 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 934 | ** library. |
| 935 | */ |
| 936 | #ifndef USE_TCL_STUBS |
| 937 | # undef Tcl_InitStubs |
| 938 | # define Tcl_InitStubs(a,b,c) |
| 939 | #endif |
| 940 | |
| 941 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 942 | ** Initialize this module. |
| 943 | ** |
| 944 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 945 | ** (Hence there is no namespace. There is no point in using a namespace |
| 946 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 947 | ** used to open a new SQLite database. See the DbMain() routine above |
| 948 | ** for additional information. |
| 949 | */ |
| 950 | int Sqlite_Init(Tcl_Interp *interp){ |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 951 | Tcl_InitStubs(interp, "8.0", 0); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 952 | Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 953 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 954 | return TCL_OK; |
| 955 | } |
| 956 | int Tclsqlite_Init(Tcl_Interp *interp){ |
| 957 | Tcl_InitStubs(interp, "8.0", 0); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 958 | Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 959 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 960 | return TCL_OK; |
| 961 | } |
| 962 | int Sqlite_SafeInit(Tcl_Interp *interp){ |
| 963 | return TCL_OK; |
| 964 | } |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 965 | int Tclsqlite_SafeInit(Tcl_Interp *interp){ |
| 966 | return TCL_OK; |
| 967 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 968 | |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 969 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 970 | /* |
| 971 | ** If compiled using mktclapp, this routine runs to initialize |
| 972 | ** everything. |
| 973 | */ |
| 974 | int Et_AppInit(Tcl_Interp *interp){ |
| 975 | return Sqlite_Init(interp); |
| 976 | } |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 977 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 978 | |
| 979 | /* |
| 980 | ** If the macro TCLSH is defined and is one, then put in code for the |
| 981 | ** "main" routine that will initialize Tcl. |
| 982 | */ |
| 983 | #if defined(TCLSH) && TCLSH==1 |
| 984 | static char zMainloop[] = |
| 985 | "set line {}\n" |
| 986 | "while {![eof stdin]} {\n" |
| 987 | "if {$line!=\"\"} {\n" |
| 988 | "puts -nonewline \"> \"\n" |
| 989 | "} else {\n" |
| 990 | "puts -nonewline \"% \"\n" |
| 991 | "}\n" |
| 992 | "flush stdout\n" |
| 993 | "append line [gets stdin]\n" |
| 994 | "if {[info complete $line]} {\n" |
| 995 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 996 | "puts stderr \"Error: $result\"\n" |
| 997 | "} elseif {$result!=\"\"} {\n" |
| 998 | "puts $result\n" |
| 999 | "}\n" |
| 1000 | "set line {}\n" |
| 1001 | "} else {\n" |
| 1002 | "append line \\n\n" |
| 1003 | "}\n" |
| 1004 | "}\n" |
| 1005 | ; |
| 1006 | |
| 1007 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 1008 | int TCLSH_MAIN(int argc, char **argv){ |
| 1009 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1010 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1011 | interp = Tcl_CreateInterp(); |
| 1012 | Sqlite_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1013 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1014 | { |
| 1015 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1016 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 1017 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1018 | extern int Md5_Init(Tcl_Interp*); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1019 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1020 | Sqlitetest2_Init(interp); |
| 1021 | Sqlitetest3_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1022 | Md5_Init(interp); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1023 | } |
| 1024 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1025 | if( argc>=2 ){ |
| 1026 | int i; |
| 1027 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 1028 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 1029 | for(i=2; i<argc; i++){ |
| 1030 | Tcl_SetVar(interp, "argv", argv[i], |
| 1031 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 1032 | } |
| 1033 | if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1034 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1035 | if( zInfo==0 ) zInfo = interp->result; |
| 1036 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1037 | return 1; |
| 1038 | } |
| 1039 | }else{ |
| 1040 | Tcl_GlobalEval(interp, zMainloop); |
| 1041 | } |
| 1042 | return 0; |
| 1043 | } |
| 1044 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1045 | |
| 1046 | #endif /* !defined(NO_TCL) */ |