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