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