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