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 | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame^] | 14 | ** $Id: tclsqlite.c,v 1.36 2002/07/07 17:12:36 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 | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 35 | ** There is one instance of this structure for each SQLite database |
| 36 | ** that has been opened by the SQLite TCL interface. |
| 37 | */ |
| 38 | typedef struct SqliteDb SqliteDb; |
| 39 | struct SqliteDb { |
| 40 | sqlite *db; /* The "real" database structure */ |
| 41 | Tcl_Interp *interp; /* The interpreter used for this database */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 42 | char *zBusy; /* The busy callback routine */ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 46 | ** An instance of this structure passes information thru the sqlite |
| 47 | ** logic from the original TCL command into the callback routine. |
| 48 | */ |
| 49 | typedef struct CallbackData CallbackData; |
| 50 | struct CallbackData { |
| 51 | Tcl_Interp *interp; /* The TCL interpreter */ |
| 52 | char *zArray; /* The array into which data is written */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 53 | Tcl_Obj *pCode; /* The code to execute for each row */ |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 54 | int once; /* Set for first callback only */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 55 | int tcl_rc; /* Return code from TCL script */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 56 | int nColName; /* Number of entries in the azColName[] array */ |
| 57 | char **azColName; /* Column names translated to UTF-8 */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 58 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 59 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 60 | #ifdef UTF_TRANSLATION_NEEDED |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 61 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 62 | ** Called for each row of the result. |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 63 | ** |
| 64 | ** This version is used when TCL expects UTF-8 data but the database |
| 65 | ** uses the ISO8859 format. A translation must occur from ISO8859 into |
| 66 | ** UTF-8. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 67 | */ |
| 68 | static int DbEvalCallback( |
| 69 | void *clientData, /* An instance of CallbackData */ |
| 70 | int nCol, /* Number of columns in the result */ |
| 71 | char ** azCol, /* Data for each column */ |
| 72 | char ** azN /* Name for each column */ |
| 73 | ){ |
| 74 | CallbackData *cbData = (CallbackData*)clientData; |
| 75 | int i, rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 76 | Tcl_DString dCol; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 77 | Tcl_DStringInit(&dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 78 | if( cbData->azColName==0 ){ |
| 79 | assert( cbData->once ); |
| 80 | cbData->once = 0; |
| 81 | if( cbData->zArray[0] ){ |
| 82 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 83 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 84 | cbData->azColName = malloc( nCol*sizeof(char*) ); |
| 85 | if( cbData->azColName==0 ){ return 1; } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 86 | cbData->nColName = nCol; |
| 87 | for(i=0; i<nCol; i++){ |
| 88 | Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 89 | cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 ); |
| 90 | if( cbData->azColName[i] ){ |
| 91 | strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol)); |
| 92 | }else{ |
| 93 | return 1; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 94 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 95 | if( cbData->zArray[0] ){ |
| 96 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", |
| 97 | Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 98 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 99 | Tcl_DStringFree(&dCol); |
| 100 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 101 | } |
| 102 | if( azCol!=0 ){ |
| 103 | if( cbData->zArray[0] ){ |
| 104 | for(i=0; i<nCol; i++){ |
| 105 | char *z = azCol[i]; |
| 106 | if( z==0 ) z = ""; |
| 107 | Tcl_DStringInit(&dCol); |
| 108 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 109 | Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i], |
| 110 | Tcl_DStringValue(&dCol), 0); |
| 111 | Tcl_DStringFree(&dCol); |
| 112 | } |
| 113 | }else{ |
| 114 | for(i=0; i<nCol; i++){ |
| 115 | char *z = azCol[i]; |
| 116 | if( z==0 ) z = ""; |
| 117 | Tcl_DStringInit(&dCol); |
| 118 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 119 | Tcl_SetVar(cbData->interp, cbData->azColName[i], |
| 120 | Tcl_DStringValue(&dCol), 0); |
| 121 | Tcl_DStringFree(&dCol); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
| 126 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 127 | cbData->tcl_rc = rc; |
| 128 | return rc!=TCL_OK; |
| 129 | } |
| 130 | #endif /* UTF_TRANSLATION_NEEDED */ |
| 131 | |
| 132 | #ifndef UTF_TRANSLATION_NEEDED |
| 133 | /* |
| 134 | ** Called for each row of the result. |
| 135 | ** |
| 136 | ** This version is used when either of the following is true: |
| 137 | ** |
| 138 | ** (1) This version of TCL uses UTF-8 and the data in the |
| 139 | ** SQLite database is already in the UTF-8 format. |
| 140 | ** |
| 141 | ** (2) This version of TCL uses ISO8859 and the data in the |
| 142 | ** SQLite database is already in the ISO8859 format. |
| 143 | */ |
| 144 | static int DbEvalCallback( |
| 145 | void *clientData, /* An instance of CallbackData */ |
| 146 | int nCol, /* Number of columns in the result */ |
| 147 | char ** azCol, /* Data for each column */ |
| 148 | char ** azN /* Name for each column */ |
| 149 | ){ |
| 150 | CallbackData *cbData = (CallbackData*)clientData; |
| 151 | int i, rc; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 152 | if( azCol==0 || (cbData->once && cbData->zArray[0]) ){ |
| 153 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
| 154 | for(i=0; i<nCol; i++){ |
| 155 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], |
| 156 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 157 | } |
| 158 | cbData->once = 0; |
| 159 | } |
| 160 | if( azCol!=0 ){ |
| 161 | if( cbData->zArray[0] ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 162 | for(i=0; i<nCol; i++){ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 163 | char *z = azCol[i]; |
| 164 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 165 | Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 166 | } |
| 167 | }else{ |
| 168 | for(i=0; i<nCol; i++){ |
| 169 | char *z = azCol[i]; |
| 170 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 171 | Tcl_SetVar(cbData->interp, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 174 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 175 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 176 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 177 | cbData->tcl_rc = rc; |
| 178 | return rc!=TCL_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 179 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 180 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 181 | |
| 182 | /* |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 183 | ** This is an alternative callback for database queries. Instead |
| 184 | ** of invoking a TCL script to handle the result, this callback just |
| 185 | ** appends each column of the result to a list. After the query |
| 186 | ** is complete, the list is returned. |
| 187 | */ |
| 188 | static int DbEvalCallback2( |
| 189 | void *clientData, /* An instance of CallbackData */ |
| 190 | int nCol, /* Number of columns in the result */ |
| 191 | char ** azCol, /* Data for each column */ |
| 192 | char ** azN /* Name for each column */ |
| 193 | ){ |
| 194 | Tcl_Obj *pList = (Tcl_Obj*)clientData; |
| 195 | int i; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 196 | if( azCol==0 ) return 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 197 | for(i=0; i<nCol; i++){ |
| 198 | Tcl_Obj *pElem; |
| 199 | if( azCol[i] && *azCol[i] ){ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 200 | #ifdef UTF_TRANSLATION_NEEDED |
| 201 | Tcl_DString dCol; |
| 202 | Tcl_DStringInit(&dCol); |
| 203 | Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol); |
| 204 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 205 | Tcl_DStringFree(&dCol); |
| 206 | #else |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 207 | pElem = Tcl_NewStringObj(azCol[i], -1); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 208 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 209 | }else{ |
| 210 | pElem = Tcl_NewObj(); |
| 211 | } |
| 212 | Tcl_ListObjAppendElement(0, pList, pElem); |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 218 | ** Called when the command is deleted. |
| 219 | */ |
| 220 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 221 | SqliteDb *pDb = (SqliteDb*)db; |
| 222 | sqlite_close(pDb->db); |
| 223 | if( pDb->zBusy ){ |
| 224 | Tcl_Free(pDb->zBusy); |
| 225 | } |
| 226 | Tcl_Free((char*)pDb); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | ** This routine is called when a database file is locked while trying |
| 231 | ** to execute SQL. |
| 232 | */ |
| 233 | static int DbBusyHandler(void *cd, const char *zTable, int nTries){ |
| 234 | SqliteDb *pDb = (SqliteDb*)cd; |
| 235 | int rc; |
| 236 | char zVal[30]; |
| 237 | char *zCmd; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 238 | Tcl_DString cmd; |
| 239 | |
| 240 | Tcl_DStringInit(&cmd); |
| 241 | Tcl_DStringAppend(&cmd, pDb->zBusy, -1); |
| 242 | Tcl_DStringAppendElement(&cmd, zTable); |
| 243 | sprintf(zVal, " %d", nTries); |
| 244 | Tcl_DStringAppend(&cmd, zVal, -1); |
| 245 | zCmd = Tcl_DStringValue(&cmd); |
| 246 | rc = Tcl_Eval(pDb->interp, zCmd); |
| 247 | Tcl_DStringFree(&cmd); |
| 248 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 249 | return 0; |
| 250 | } |
| 251 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
| 255 | ** The "sqlite" command below creates a new Tcl command for each |
| 256 | ** connection it opens to an SQLite database. This routine is invoked |
| 257 | ** whenever one of those connection-specific commands is executed |
| 258 | ** in Tcl. For example, if you run Tcl code like this: |
| 259 | ** |
| 260 | ** sqlite db1 "my_database" |
| 261 | ** db1 close |
| 262 | ** |
| 263 | ** The first command opens a connection to the "my_database" database |
| 264 | ** and calls that connection "db1". The second command causes this |
| 265 | ** subroutine to be invoked. |
| 266 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 267 | 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] | 268 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 269 | int choice; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 270 | static const char *DB_strs[] = { |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 271 | "busy", "changes", "close", |
| 272 | "complete", "eval", "last_insert_rowid", |
| 273 | "open_aux_file", "timeout", 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 274 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 275 | enum DB_enum { |
| 276 | DB_BUSY, DB_CHANGES, DB_CLOSE, |
| 277 | DB_COMPLETE, DB_EVAL, DB_LAST_INSERT_ROWID, |
| 278 | DB_OPEN_AUX_FILE, DB_TIMEOUT, |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | if( objc<2 ){ |
| 282 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 283 | return TCL_ERROR; |
| 284 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 285 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 286 | return TCL_ERROR; |
| 287 | } |
| 288 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 289 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 290 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 291 | /* $db busy ?CALLBACK? |
| 292 | ** |
| 293 | ** Invoke the given callback if an SQL statement attempts to open |
| 294 | ** a locked database file. |
| 295 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 296 | case DB_BUSY: { |
| 297 | if( objc>3 ){ |
| 298 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 299 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 300 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 301 | if( pDb->zBusy ){ |
| 302 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 303 | } |
| 304 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 305 | char *zBusy; |
| 306 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 307 | if( pDb->zBusy ){ |
| 308 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 309 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 310 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 311 | if( zBusy && len>0 ){ |
| 312 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 313 | strcpy(pDb->zBusy, zBusy); |
| 314 | }else{ |
| 315 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 316 | } |
| 317 | if( pDb->zBusy ){ |
| 318 | pDb->interp = interp; |
| 319 | sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 320 | }else{ |
| 321 | sqlite_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 324 | break; |
| 325 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 326 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 327 | /* |
| 328 | ** $db changes |
| 329 | ** |
| 330 | ** Return the number of rows that were modified, inserted, or deleted by |
| 331 | ** the most recent "eval". |
| 332 | */ |
| 333 | case DB_CHANGES: { |
| 334 | Tcl_Obj *pResult; |
| 335 | int nChange; |
| 336 | if( objc!=2 ){ |
| 337 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 338 | return TCL_ERROR; |
| 339 | } |
| 340 | nChange = sqlite_changes(pDb->db); |
| 341 | pResult = Tcl_GetObjResult(interp); |
| 342 | Tcl_SetIntObj(pResult, nChange); |
| 343 | break; |
| 344 | } |
| 345 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 346 | /* $db close |
| 347 | ** |
| 348 | ** Shutdown the database |
| 349 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 350 | case DB_CLOSE: { |
| 351 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 352 | break; |
| 353 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 354 | |
| 355 | /* $db complete SQL |
| 356 | ** |
| 357 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 358 | ** additional lines of input are needed. This is similar to the |
| 359 | ** built-in "info complete" command of Tcl. |
| 360 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 361 | case DB_COMPLETE: { |
| 362 | Tcl_Obj *pResult; |
| 363 | int isComplete; |
| 364 | if( objc!=3 ){ |
| 365 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 366 | return TCL_ERROR; |
| 367 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 368 | isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
| 369 | pResult = Tcl_GetObjResult(interp); |
| 370 | Tcl_SetBooleanObj(pResult, isComplete); |
| 371 | break; |
| 372 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | ** $db eval $sql ?array { ...code... }? |
| 376 | ** |
| 377 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 378 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 379 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 380 | ** If "array" is an empty string, then the values are placed in variables |
| 381 | ** that have the same name as the fields extracted by the query. |
| 382 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 383 | case DB_EVAL: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 384 | CallbackData cbData; |
| 385 | char *zErrMsg; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 386 | char *zSql; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 387 | int rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 388 | #ifdef UTF_TRANSLATION_NEEDED |
| 389 | Tcl_DString dSql; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 390 | int i; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 391 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 392 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 393 | if( objc!=5 && objc!=3 ){ |
| 394 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 395 | return TCL_ERROR; |
| 396 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 397 | pDb->interp = interp; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 398 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 399 | #ifdef UTF_TRANSLATION_NEEDED |
| 400 | Tcl_DStringInit(&dSql); |
| 401 | Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); |
| 402 | zSql = Tcl_DStringValue(&dSql); |
| 403 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 404 | Tcl_IncrRefCount(objv[2]); |
| 405 | if( objc==5 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 406 | cbData.interp = interp; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 407 | cbData.once = 1; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 408 | cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); |
| 409 | cbData.pCode = objv[4]; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 410 | cbData.tcl_rc = TCL_OK; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 411 | cbData.nColName = 0; |
| 412 | cbData.azColName = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 413 | zErrMsg = 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 414 | Tcl_IncrRefCount(objv[3]); |
| 415 | Tcl_IncrRefCount(objv[4]); |
| 416 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); |
| 417 | Tcl_DecrRefCount(objv[4]); |
| 418 | Tcl_DecrRefCount(objv[3]); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 419 | if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 420 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 421 | Tcl_Obj *pList = Tcl_NewObj(); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 422 | cbData.tcl_rc = TCL_OK; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 423 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); |
| 424 | Tcl_SetObjResult(interp, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 425 | } |
| 426 | if( zErrMsg ){ |
| 427 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 428 | free(zErrMsg); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 429 | rc = TCL_ERROR; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 430 | }else if( rc!=SQLITE_OK && rc!=SQLITE_ABORT ){ |
| 431 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 432 | rc = TCL_ERROR; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 433 | }else{ |
| 434 | rc = cbData.tcl_rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 435 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 436 | Tcl_DecrRefCount(objv[2]); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 437 | #ifdef UTF_TRANSLATION_NEEDED |
| 438 | Tcl_DStringFree(&dSql); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 439 | if( objc==5 && cbData.azColName ){ |
| 440 | for(i=0; i<cbData.nColName; i++){ |
| 441 | if( cbData.azColName[i] ) free(cbData.azColName[i]); |
| 442 | } |
| 443 | free(cbData.azColName); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 444 | cbData.azColName = 0; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 445 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 446 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 447 | return rc; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 448 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 449 | |
| 450 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 451 | ** $db last_insert_rowid |
| 452 | ** |
| 453 | ** Return an integer which is the ROWID for the most recent insert. |
| 454 | */ |
| 455 | case DB_LAST_INSERT_ROWID: { |
| 456 | Tcl_Obj *pResult; |
| 457 | int rowid; |
| 458 | if( objc!=2 ){ |
| 459 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 460 | return TCL_ERROR; |
| 461 | } |
| 462 | rowid = sqlite_last_insert_rowid(pDb->db); |
| 463 | pResult = Tcl_GetObjResult(interp); |
| 464 | Tcl_SetIntObj(pResult, rowid); |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | /* |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 469 | ** $db open_aux_file FILENAME |
| 470 | ** |
| 471 | ** Begin using FILENAME as the database file used to store temporary |
| 472 | ** tables. |
| 473 | */ |
| 474 | case DB_OPEN_AUX_FILE: { |
| 475 | const char *zFilename; |
| 476 | char *zErrMsg = 0; |
| 477 | int rc; |
| 478 | if( objc!=3 ){ |
| 479 | Tcl_WrongNumArgs(interp, 2, objv, "FILENAME"); |
| 480 | return TCL_ERROR; |
| 481 | } |
| 482 | zFilename = Tcl_GetStringFromObj(objv[2], 0); |
| 483 | rc = sqlite_open_aux_file(pDb->db, zFilename, &zErrMsg); |
| 484 | if( rc!=0 ){ |
| 485 | if( zErrMsg ){ |
| 486 | Tcl_AppendResult(interp, zErrMsg, 0); |
| 487 | free(zErrMsg); |
| 488 | }else{ |
| 489 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 490 | } |
| 491 | return TCL_ERROR; |
| 492 | } |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 497 | ** $db timeout MILLESECONDS |
| 498 | ** |
| 499 | ** Delay for the number of milliseconds specified when a file is locked. |
| 500 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 501 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 502 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 503 | if( objc!=3 ){ |
| 504 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 505 | return TCL_ERROR; |
| 506 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 507 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 508 | sqlite_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 509 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 510 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 511 | } /* End of the SWITCH statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 512 | return TCL_OK; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | ** sqlite DBNAME FILENAME ?MODE? |
| 517 | ** |
| 518 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 519 | ** invoked, this routine runs to process that command. |
| 520 | ** |
| 521 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 522 | ** database connection. This command creates a new command named |
| 523 | ** DBNAME that is used to control that connection. The database |
| 524 | ** connection is deleted when the DBNAME command is deleted. |
| 525 | ** |
| 526 | ** The second argument is the name of the directory that contains |
| 527 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 528 | ** |
| 529 | ** For testing purposes, we also support the following: |
| 530 | ** |
| 531 | ** sqlite -encoding |
| 532 | ** |
| 533 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 534 | ** are UTF-8 and iso8859. |
| 535 | ** |
| 536 | ** sqlite -tcl-uses-utf |
| 537 | ** |
| 538 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 539 | ** not. Used by tests to make sure the library was compiled |
| 540 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 541 | */ |
| 542 | static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ |
| 543 | int mode; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 544 | SqliteDb *p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 545 | char *zErrMsg; |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 546 | char zBuf[80]; |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 547 | if( argc==2 ){ |
| 548 | if( strcmp(argv[1],"-encoding")==0 ){ |
| 549 | Tcl_AppendResult(interp,sqlite_encoding,0); |
| 550 | return TCL_OK; |
| 551 | } |
| 552 | if( strcmp(argv[1],"-tcl-uses-utf")==0 ){ |
| 553 | #ifdef TCL_UTF_MAX |
| 554 | Tcl_AppendResult(interp,"1",0); |
| 555 | #else |
| 556 | Tcl_AppendResult(interp,"0",0); |
| 557 | #endif |
| 558 | return TCL_OK; |
| 559 | } |
| 560 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 561 | if( argc!=3 && argc!=4 ){ |
| 562 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 563 | " HANDLE FILENAME ?MODE?\"", 0); |
| 564 | return TCL_ERROR; |
| 565 | } |
| 566 | if( argc==3 ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 567 | mode = 0666; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 568 | }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ |
| 569 | return TCL_ERROR; |
| 570 | } |
| 571 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 572 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 573 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 574 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 575 | return TCL_ERROR; |
| 576 | } |
| 577 | memset(p, 0, sizeof(*p)); |
| 578 | p->db = sqlite_open(argv[2], mode, &zErrMsg); |
| 579 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 580 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 581 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 582 | free(zErrMsg); |
| 583 | return TCL_ERROR; |
| 584 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 585 | Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 586 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 587 | /* The return value is the value of the sqlite* pointer |
| 588 | */ |
| 589 | sprintf(zBuf, "%p", p->db); |
drh | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame^] | 590 | if( strncmp(zBuf,"0x",2) ){ |
| 591 | sprintf(zBuf, "0x%p", p->db); |
| 592 | } |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 593 | Tcl_AppendResult(interp, zBuf, 0); |
| 594 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 595 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 596 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 597 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 598 | #ifdef SQLITE_TEST |
| 599 | { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 600 | extern void Md5_Register(sqlite*); |
| 601 | Md5_Register(p->db); |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 602 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 603 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 604 | return TCL_OK; |
| 605 | } |
| 606 | |
| 607 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 608 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 609 | ** library. |
| 610 | */ |
| 611 | #ifndef USE_TCL_STUBS |
| 612 | # undef Tcl_InitStubs |
| 613 | # define Tcl_InitStubs(a,b,c) |
| 614 | #endif |
| 615 | |
| 616 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 617 | ** Initialize this module. |
| 618 | ** |
| 619 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 620 | ** (Hence there is no namespace. There is no point in using a namespace |
| 621 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 622 | ** used to open a new SQLite database. See the DbMain() routine above |
| 623 | ** for additional information. |
| 624 | */ |
| 625 | int Sqlite_Init(Tcl_Interp *interp){ |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 626 | Tcl_InitStubs(interp, "8.0", 0); |
| 627 | Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 628 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 629 | return TCL_OK; |
| 630 | } |
| 631 | int Tclsqlite_Init(Tcl_Interp *interp){ |
| 632 | Tcl_InitStubs(interp, "8.0", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 633 | Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 634 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 635 | return TCL_OK; |
| 636 | } |
| 637 | int Sqlite_SafeInit(Tcl_Interp *interp){ |
| 638 | return TCL_OK; |
| 639 | } |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 640 | int Tclsqlite_SafeInit(Tcl_Interp *interp){ |
| 641 | return TCL_OK; |
| 642 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 643 | |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 644 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 645 | /* |
| 646 | ** If compiled using mktclapp, this routine runs to initialize |
| 647 | ** everything. |
| 648 | */ |
| 649 | int Et_AppInit(Tcl_Interp *interp){ |
| 650 | return Sqlite_Init(interp); |
| 651 | } |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 652 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | ** If the macro TCLSH is defined and is one, then put in code for the |
| 656 | ** "main" routine that will initialize Tcl. |
| 657 | */ |
| 658 | #if defined(TCLSH) && TCLSH==1 |
| 659 | static char zMainloop[] = |
| 660 | "set line {}\n" |
| 661 | "while {![eof stdin]} {\n" |
| 662 | "if {$line!=\"\"} {\n" |
| 663 | "puts -nonewline \"> \"\n" |
| 664 | "} else {\n" |
| 665 | "puts -nonewline \"% \"\n" |
| 666 | "}\n" |
| 667 | "flush stdout\n" |
| 668 | "append line [gets stdin]\n" |
| 669 | "if {[info complete $line]} {\n" |
| 670 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 671 | "puts stderr \"Error: $result\"\n" |
| 672 | "} elseif {$result!=\"\"} {\n" |
| 673 | "puts $result\n" |
| 674 | "}\n" |
| 675 | "set line {}\n" |
| 676 | "} else {\n" |
| 677 | "append line \\n\n" |
| 678 | "}\n" |
| 679 | "}\n" |
| 680 | ; |
| 681 | |
| 682 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 683 | int TCLSH_MAIN(int argc, char **argv){ |
| 684 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 685 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 686 | interp = Tcl_CreateInterp(); |
| 687 | Sqlite_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 688 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 689 | { |
| 690 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 691 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 692 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 693 | extern int Md5_Init(Tcl_Interp*); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 694 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 695 | Sqlitetest2_Init(interp); |
| 696 | Sqlitetest3_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 697 | Md5_Init(interp); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 698 | } |
| 699 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 700 | if( argc>=2 ){ |
| 701 | int i; |
| 702 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 703 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 704 | for(i=2; i<argc; i++){ |
| 705 | Tcl_SetVar(interp, "argv", argv[i], |
| 706 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 707 | } |
| 708 | if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 709 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 710 | if( zInfo==0 ) zInfo = interp->result; |
| 711 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 712 | return 1; |
| 713 | } |
| 714 | }else{ |
| 715 | Tcl_GlobalEval(interp, zMainloop); |
| 716 | } |
| 717 | return 0; |
| 718 | } |
| 719 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 720 | |
| 721 | #endif /* !defined(NO_TCL) */ |