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