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