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 | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 26 | ** $Id: tclsqlite.c,v 1.14 2001/04/03 16:53:22 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 | /* |
| 60 | ** Called for each row of the result. |
| 61 | */ |
| 62 | static int DbEvalCallback( |
| 63 | void *clientData, /* An instance of CallbackData */ |
| 64 | int nCol, /* Number of columns in the result */ |
| 65 | char ** azCol, /* Data for each column */ |
| 66 | char ** azN /* Name for each column */ |
| 67 | ){ |
| 68 | CallbackData *cbData = (CallbackData*)clientData; |
| 69 | int i, rc; |
| 70 | if( cbData->zArray[0] ){ |
| 71 | if( cbData->once ){ |
drh | 9b0d0a8 | 2000-09-30 22:46:05 +0000 | [diff] [blame] | 72 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 73 | for(i=0; i<nCol; i++){ |
| 74 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], |
| 75 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 76 | } |
| 77 | } |
| 78 | for(i=0; i<nCol; i++){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 79 | char *z = azCol[i]; |
| 80 | if( z==0 ) z = ""; |
| 81 | Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 82 | } |
| 83 | }else{ |
| 84 | for(i=0; i<nCol; i++){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 85 | char *z = azCol[i]; |
| 86 | if( z==0 ) z = ""; |
| 87 | Tcl_SetVar(cbData->interp, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | cbData->once = 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 91 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 92 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 93 | cbData->tcl_rc = rc; |
| 94 | return rc!=TCL_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 98 | ** This is an alternative callback for database queries. Instead |
| 99 | ** of invoking a TCL script to handle the result, this callback just |
| 100 | ** appends each column of the result to a list. After the query |
| 101 | ** is complete, the list is returned. |
| 102 | */ |
| 103 | static int DbEvalCallback2( |
| 104 | void *clientData, /* An instance of CallbackData */ |
| 105 | int nCol, /* Number of columns in the result */ |
| 106 | char ** azCol, /* Data for each column */ |
| 107 | char ** azN /* Name for each column */ |
| 108 | ){ |
| 109 | Tcl_Obj *pList = (Tcl_Obj*)clientData; |
| 110 | int i; |
| 111 | for(i=0; i<nCol; i++){ |
| 112 | Tcl_Obj *pElem; |
| 113 | if( azCol[i] && *azCol[i] ){ |
| 114 | pElem = Tcl_NewStringObj(azCol[i], -1); |
| 115 | }else{ |
| 116 | pElem = Tcl_NewObj(); |
| 117 | } |
| 118 | Tcl_ListObjAppendElement(0, pList, pElem); |
| 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 124 | ** Called when the command is deleted. |
| 125 | */ |
| 126 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 127 | SqliteDb *pDb = (SqliteDb*)db; |
| 128 | sqlite_close(pDb->db); |
| 129 | if( pDb->zBusy ){ |
| 130 | Tcl_Free(pDb->zBusy); |
| 131 | } |
| 132 | Tcl_Free((char*)pDb); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | ** This routine is called when a database file is locked while trying |
| 137 | ** to execute SQL. |
| 138 | */ |
| 139 | static int DbBusyHandler(void *cd, const char *zTable, int nTries){ |
| 140 | SqliteDb *pDb = (SqliteDb*)cd; |
| 141 | int rc; |
| 142 | char zVal[30]; |
| 143 | char *zCmd; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 144 | Tcl_DString cmd; |
| 145 | |
| 146 | Tcl_DStringInit(&cmd); |
| 147 | Tcl_DStringAppend(&cmd, pDb->zBusy, -1); |
| 148 | Tcl_DStringAppendElement(&cmd, zTable); |
| 149 | sprintf(zVal, " %d", nTries); |
| 150 | Tcl_DStringAppend(&cmd, zVal, -1); |
| 151 | zCmd = Tcl_DStringValue(&cmd); |
| 152 | rc = Tcl_Eval(pDb->interp, zCmd); |
| 153 | Tcl_DStringFree(&cmd); |
| 154 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 155 | return 0; |
| 156 | } |
| 157 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
| 161 | ** The "sqlite" command below creates a new Tcl command for each |
| 162 | ** connection it opens to an SQLite database. This routine is invoked |
| 163 | ** whenever one of those connection-specific commands is executed |
| 164 | ** in Tcl. For example, if you run Tcl code like this: |
| 165 | ** |
| 166 | ** sqlite db1 "my_database" |
| 167 | ** db1 close |
| 168 | ** |
| 169 | ** The first command opens a connection to the "my_database" database |
| 170 | ** and calls that connection "db1". The second command causes this |
| 171 | ** subroutine to be invoked. |
| 172 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 173 | 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] | 174 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 175 | int choice; |
| 176 | static char *DB_optStrs[] = { |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 177 | "busy", "close", "complete", "eval", "timeout", 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 178 | }; |
| 179 | enum DB_opts { |
| 180 | DB_BUSY, DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_TIMEOUT |
| 181 | }; |
| 182 | |
| 183 | if( objc<2 ){ |
| 184 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 185 | return TCL_ERROR; |
| 186 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 187 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){ |
| 188 | return TCL_ERROR; |
| 189 | } |
| 190 | |
| 191 | switch( (enum DB_opts)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 192 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 193 | /* $db busy ?CALLBACK? |
| 194 | ** |
| 195 | ** Invoke the given callback if an SQL statement attempts to open |
| 196 | ** a locked database file. |
| 197 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 198 | case DB_BUSY: { |
| 199 | if( objc>3 ){ |
| 200 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 201 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 202 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 203 | if( pDb->zBusy ){ |
| 204 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 205 | } |
| 206 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 207 | char *zBusy; |
| 208 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 209 | if( pDb->zBusy ){ |
| 210 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 211 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 212 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 213 | if( zBusy && len>0 ){ |
| 214 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 215 | strcpy(pDb->zBusy, zBusy); |
| 216 | }else{ |
| 217 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 218 | } |
| 219 | if( pDb->zBusy ){ |
| 220 | pDb->interp = interp; |
| 221 | sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 222 | }else{ |
| 223 | sqlite_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 226 | break; |
| 227 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 228 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 229 | /* $db close |
| 230 | ** |
| 231 | ** Shutdown the database |
| 232 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 233 | case DB_CLOSE: { |
| 234 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 235 | break; |
| 236 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 237 | |
| 238 | /* $db complete SQL |
| 239 | ** |
| 240 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 241 | ** additional lines of input are needed. This is similar to the |
| 242 | ** built-in "info complete" command of Tcl. |
| 243 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 244 | case DB_COMPLETE: { |
| 245 | Tcl_Obj *pResult; |
| 246 | int isComplete; |
| 247 | if( objc!=3 ){ |
| 248 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 249 | return TCL_ERROR; |
| 250 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 251 | isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
| 252 | pResult = Tcl_GetObjResult(interp); |
| 253 | Tcl_SetBooleanObj(pResult, isComplete); |
| 254 | break; |
| 255 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | ** $db eval $sql ?array { ...code... }? |
| 259 | ** |
| 260 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 261 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 262 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 263 | ** If "array" is an empty string, then the values are placed in variables |
| 264 | ** that have the same name as the fields extracted by the query. |
| 265 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 266 | case DB_EVAL: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 267 | CallbackData cbData; |
| 268 | char *zErrMsg; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 269 | char *zSql; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 270 | int rc; |
| 271 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 272 | if( objc!=5 && objc!=3 ){ |
| 273 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 274 | return TCL_ERROR; |
| 275 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 276 | pDb->interp = interp; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 277 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
| 278 | Tcl_IncrRefCount(objv[2]); |
| 279 | if( objc==5 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 280 | cbData.interp = interp; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 281 | cbData.once = 1; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 282 | cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); |
| 283 | cbData.pCode = objv[4]; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 284 | cbData.tcl_rc = TCL_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 285 | zErrMsg = 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 286 | Tcl_IncrRefCount(objv[3]); |
| 287 | Tcl_IncrRefCount(objv[4]); |
| 288 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); |
| 289 | Tcl_DecrRefCount(objv[4]); |
| 290 | Tcl_DecrRefCount(objv[3]); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 291 | if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 292 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 293 | Tcl_Obj *pList = Tcl_NewObj(); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 294 | cbData.tcl_rc = TCL_OK; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 295 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); |
| 296 | Tcl_SetObjResult(interp, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 297 | } |
| 298 | if( zErrMsg ){ |
| 299 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 300 | free(zErrMsg); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 301 | rc = TCL_ERROR; |
| 302 | }else{ |
| 303 | rc = cbData.tcl_rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 305 | Tcl_DecrRefCount(objv[2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 306 | return rc; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 307 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | ** $db timeout MILLESECONDS |
| 311 | ** |
| 312 | ** Delay for the number of milliseconds specified when a file is locked. |
| 313 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 314 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 315 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 316 | if( objc!=3 ){ |
| 317 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 318 | return TCL_ERROR; |
| 319 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 320 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 321 | sqlite_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 322 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 323 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 324 | } /* End of the SWITCH statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 325 | return TCL_OK; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | ** sqlite DBNAME FILENAME ?MODE? |
| 330 | ** |
| 331 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 332 | ** invoked, this routine runs to process that command. |
| 333 | ** |
| 334 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 335 | ** database connection. This command creates a new command named |
| 336 | ** DBNAME that is used to control that connection. The database |
| 337 | ** connection is deleted when the DBNAME command is deleted. |
| 338 | ** |
| 339 | ** The second argument is the name of the directory that contains |
| 340 | ** the sqlite database that is to be accessed. |
| 341 | */ |
| 342 | static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ |
| 343 | int mode; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 344 | SqliteDb *p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 345 | char *zErrMsg; |
| 346 | if( argc!=3 && argc!=4 ){ |
| 347 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 348 | " HANDLE FILENAME ?MODE?\"", 0); |
| 349 | return TCL_ERROR; |
| 350 | } |
| 351 | if( argc==3 ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 352 | mode = 0666; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 353 | }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ |
| 354 | return TCL_ERROR; |
| 355 | } |
| 356 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 357 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 358 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 359 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 360 | return TCL_ERROR; |
| 361 | } |
| 362 | memset(p, 0, sizeof(*p)); |
| 363 | p->db = sqlite_open(argv[2], mode, &zErrMsg); |
| 364 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 365 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 366 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 367 | free(zErrMsg); |
| 368 | return TCL_ERROR; |
| 369 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 370 | Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 371 | return TCL_OK; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | ** Initialize this module. |
| 376 | ** |
| 377 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 378 | ** (Hence there is no namespace. There is no point in using a namespace |
| 379 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 380 | ** used to open a new SQLite database. See the DbMain() routine above |
| 381 | ** for additional information. |
| 382 | */ |
| 383 | int Sqlite_Init(Tcl_Interp *interp){ |
| 384 | Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); |
drh | 167a4b1 | 2000-08-17 09:49:59 +0000 | [diff] [blame] | 385 | Tcl_PkgProvide(interp, "sqlite", "1.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 386 | return TCL_OK; |
| 387 | } |
| 388 | int Sqlite_SafeInit(Tcl_Interp *interp){ |
| 389 | return TCL_OK; |
| 390 | } |
| 391 | |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 392 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 393 | /* |
| 394 | ** If compiled using mktclapp, this routine runs to initialize |
| 395 | ** everything. |
| 396 | */ |
| 397 | int Et_AppInit(Tcl_Interp *interp){ |
| 398 | return Sqlite_Init(interp); |
| 399 | } |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 400 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 401 | |
| 402 | /* |
| 403 | ** If the macro TCLSH is defined and is one, then put in code for the |
| 404 | ** "main" routine that will initialize Tcl. |
| 405 | */ |
| 406 | #if defined(TCLSH) && TCLSH==1 |
| 407 | static char zMainloop[] = |
| 408 | "set line {}\n" |
| 409 | "while {![eof stdin]} {\n" |
| 410 | "if {$line!=\"\"} {\n" |
| 411 | "puts -nonewline \"> \"\n" |
| 412 | "} else {\n" |
| 413 | "puts -nonewline \"% \"\n" |
| 414 | "}\n" |
| 415 | "flush stdout\n" |
| 416 | "append line [gets stdin]\n" |
| 417 | "if {[info complete $line]} {\n" |
| 418 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 419 | "puts stderr \"Error: $result\"\n" |
| 420 | "} elseif {$result!=\"\"} {\n" |
| 421 | "puts $result\n" |
| 422 | "}\n" |
| 423 | "set line {}\n" |
| 424 | "} else {\n" |
| 425 | "append line \\n\n" |
| 426 | "}\n" |
| 427 | "}\n" |
| 428 | ; |
| 429 | |
| 430 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 431 | int TCLSH_MAIN(int argc, char **argv){ |
| 432 | Tcl_Interp *interp; |
| 433 | interp = Tcl_CreateInterp(); |
| 434 | Sqlite_Init(interp); |
| 435 | if( argc>=2 ){ |
| 436 | int i; |
| 437 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 438 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 439 | for(i=2; i<argc; i++){ |
| 440 | Tcl_SetVar(interp, "argv", argv[i], |
| 441 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 442 | } |
| 443 | if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 444 | char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
| 445 | if( zInfo==0 ) zInfo = interp->result; |
| 446 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 447 | return 1; |
| 448 | } |
| 449 | }else{ |
| 450 | Tcl_GlobalEval(interp, zMainloop); |
| 451 | } |
| 452 | return 0; |
| 453 | } |
| 454 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 455 | |
| 456 | #endif /* !defined(NO_TCL) */ |