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 | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 26 | ** $Id: tclsqlite.c,v 1.5 2000/06/04 12:58:38 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | */ |
| 28 | #include "sqlite.h" |
| 29 | #include <tcl.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | /* |
| 34 | ** An instance of this structure passes information thru the sqlite |
| 35 | ** logic from the original TCL command into the callback routine. |
| 36 | */ |
| 37 | typedef struct CallbackData CallbackData; |
| 38 | struct CallbackData { |
| 39 | Tcl_Interp *interp; /* The TCL interpreter */ |
| 40 | char *zArray; /* The array into which data is written */ |
| 41 | char *zCode; /* The code to execute for each row */ |
| 42 | int once; /* Set only for the first invocation of callback */ |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | ** Called for each row of the result. |
| 47 | */ |
| 48 | static int DbEvalCallback( |
| 49 | void *clientData, /* An instance of CallbackData */ |
| 50 | int nCol, /* Number of columns in the result */ |
| 51 | char ** azCol, /* Data for each column */ |
| 52 | char ** azN /* Name for each column */ |
| 53 | ){ |
| 54 | CallbackData *cbData = (CallbackData*)clientData; |
| 55 | int i, rc; |
| 56 | if( cbData->zArray[0] ){ |
| 57 | if( cbData->once ){ |
| 58 | for(i=0; i<nCol; i++){ |
| 59 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], |
| 60 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 61 | } |
| 62 | } |
| 63 | for(i=0; i<nCol; i++){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 64 | char *z = azCol[i]; |
| 65 | if( z==0 ) z = ""; |
| 66 | Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 67 | } |
| 68 | }else{ |
| 69 | for(i=0; i<nCol; i++){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 70 | char *z = azCol[i]; |
| 71 | if( z==0 ) z = ""; |
| 72 | Tcl_SetVar(cbData->interp, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | cbData->once = 0; |
| 76 | rc = Tcl_Eval(cbData->interp, cbData->zCode); |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | ** Called when the command is deleted. |
| 82 | */ |
| 83 | static void DbDeleteCmd(void *db){ |
| 84 | sqlite_close((sqlite*)db); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | ** The "sqlite" command below creates a new Tcl command for each |
| 89 | ** connection it opens to an SQLite database. This routine is invoked |
| 90 | ** whenever one of those connection-specific commands is executed |
| 91 | ** in Tcl. For example, if you run Tcl code like this: |
| 92 | ** |
| 93 | ** sqlite db1 "my_database" |
| 94 | ** db1 close |
| 95 | ** |
| 96 | ** The first command opens a connection to the "my_database" database |
| 97 | ** and calls that connection "db1". The second command causes this |
| 98 | ** subroutine to be invoked. |
| 99 | */ |
| 100 | static int DbCmd(void *cd, Tcl_Interp *interp, int argc, char **argv){ |
| 101 | char *z; |
| 102 | int n, c; |
| 103 | sqlite *db = cd; |
| 104 | if( argc<2 ){ |
| 105 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 106 | " SUBCOMMAND ...\"", 0); |
| 107 | return TCL_ERROR; |
| 108 | } |
| 109 | z = argv[1]; |
| 110 | n = strlen(z); |
| 111 | c = z[0]; |
| 112 | |
| 113 | /* $db close |
| 114 | ** |
| 115 | ** Shutdown the database |
| 116 | */ |
| 117 | if( c=='c' && n>=2 && strncmp(z,"close",n)==0 ){ |
| 118 | Tcl_DeleteCommand(interp, argv[0]); |
| 119 | }else |
| 120 | |
| 121 | /* $db complete SQL |
| 122 | ** |
| 123 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 124 | ** additional lines of input are needed. This is similar to the |
| 125 | ** built-in "info complete" command of Tcl. |
| 126 | */ |
| 127 | if( c=='c' && n>=2 && strncmp(z,"complete",n)==0 ){ |
| 128 | char *zRes; |
| 129 | if( argc!=3 ){ |
| 130 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 131 | " complete SQL\"", 0); |
| 132 | return TCL_ERROR; |
| 133 | } |
| 134 | zRes = sqlite_complete(argv[2]) ? "1" : "0"; |
| 135 | Tcl_SetResult(interp, zRes, TCL_VOLATILE); |
| 136 | }else |
| 137 | |
| 138 | /* |
| 139 | ** $db eval $sql ?array { ...code... }? |
| 140 | ** |
| 141 | ** The SQL statement in $sql is evaluated. For each row, the values are |
| 142 | ** placed in elements of the array named "array" and ...code.. is executed. |
| 143 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 144 | ** If "array" is an empty string, then the values are placed in variables |
| 145 | ** that have the same name as the fields extracted by the query. |
| 146 | */ |
| 147 | if( c=='e' && strncmp(z,"eval",n)==0 ){ |
| 148 | CallbackData cbData; |
| 149 | char *zErrMsg; |
| 150 | int rc; |
| 151 | |
| 152 | if( argc!=5 && argc!=3 ){ |
| 153 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 154 | " eval SQL ?ARRAY-NAME CODE?", 0); |
| 155 | return TCL_ERROR; |
| 156 | } |
| 157 | if( argc==5 ){ |
| 158 | cbData.interp = interp; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 159 | cbData.once = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 160 | cbData.zArray = argv[3]; |
| 161 | cbData.zCode = argv[4]; |
| 162 | zErrMsg = 0; |
| 163 | rc = sqlite_exec(db, argv[2], DbEvalCallback, &cbData, &zErrMsg); |
| 164 | }else{ |
| 165 | rc = sqlite_exec(db, argv[2], 0, 0, &zErrMsg); |
| 166 | } |
| 167 | if( zErrMsg ){ |
| 168 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 169 | free(zErrMsg); |
| 170 | } |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | /* The default |
| 175 | */ |
| 176 | else{ |
| 177 | Tcl_AppendResult(interp,"unknown subcommand \"", z, |
| 178 | "\" - should be one of: close complete eval", 0); |
| 179 | return TCL_ERROR; |
| 180 | } |
| 181 | return TCL_OK; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | ** sqlite DBNAME FILENAME ?MODE? |
| 186 | ** |
| 187 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 188 | ** invoked, this routine runs to process that command. |
| 189 | ** |
| 190 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 191 | ** database connection. This command creates a new command named |
| 192 | ** DBNAME that is used to control that connection. The database |
| 193 | ** connection is deleted when the DBNAME command is deleted. |
| 194 | ** |
| 195 | ** The second argument is the name of the directory that contains |
| 196 | ** the sqlite database that is to be accessed. |
| 197 | */ |
| 198 | static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ |
| 199 | int mode; |
| 200 | sqlite *p; |
| 201 | char *zErrMsg; |
| 202 | if( argc!=3 && argc!=4 ){ |
| 203 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 204 | " HANDLE FILENAME ?MODE?\"", 0); |
| 205 | return TCL_ERROR; |
| 206 | } |
| 207 | if( argc==3 ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 208 | mode = 0666; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 209 | }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ |
| 210 | return TCL_ERROR; |
| 211 | } |
| 212 | zErrMsg = 0; |
| 213 | p = sqlite_open(argv[2], mode, &zErrMsg); |
| 214 | if( p==0 ){ |
| 215 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 216 | free(zErrMsg); |
| 217 | return TCL_ERROR; |
| 218 | } |
| 219 | Tcl_CreateCommand(interp, argv[1], DbCmd, p, DbDeleteCmd); |
| 220 | return TCL_OK; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | ** Initialize this module. |
| 225 | ** |
| 226 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 227 | ** (Hence there is no namespace. There is no point in using a namespace |
| 228 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 229 | ** used to open a new SQLite database. See the DbMain() routine above |
| 230 | ** for additional information. |
| 231 | */ |
| 232 | int Sqlite_Init(Tcl_Interp *interp){ |
| 233 | Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); |
| 234 | return TCL_OK; |
| 235 | } |
| 236 | int Sqlite_SafeInit(Tcl_Interp *interp){ |
| 237 | return TCL_OK; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | ** If compiled using mktclapp, this routine runs to initialize |
| 242 | ** everything. |
| 243 | */ |
| 244 | int Et_AppInit(Tcl_Interp *interp){ |
| 245 | return Sqlite_Init(interp); |
| 246 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | ** If the macro TCLSH is defined and is one, then put in code for the |
| 250 | ** "main" routine that will initialize Tcl. |
| 251 | */ |
| 252 | #if defined(TCLSH) && TCLSH==1 |
| 253 | static char zMainloop[] = |
| 254 | "set line {}\n" |
| 255 | "while {![eof stdin]} {\n" |
| 256 | "if {$line!=\"\"} {\n" |
| 257 | "puts -nonewline \"> \"\n" |
| 258 | "} else {\n" |
| 259 | "puts -nonewline \"% \"\n" |
| 260 | "}\n" |
| 261 | "flush stdout\n" |
| 262 | "append line [gets stdin]\n" |
| 263 | "if {[info complete $line]} {\n" |
| 264 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 265 | "puts stderr \"Error: $result\"\n" |
| 266 | "} elseif {$result!=\"\"} {\n" |
| 267 | "puts $result\n" |
| 268 | "}\n" |
| 269 | "set line {}\n" |
| 270 | "} else {\n" |
| 271 | "append line \\n\n" |
| 272 | "}\n" |
| 273 | "}\n" |
| 274 | ; |
| 275 | |
| 276 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 277 | int TCLSH_MAIN(int argc, char **argv){ |
| 278 | Tcl_Interp *interp; |
| 279 | interp = Tcl_CreateInterp(); |
| 280 | Sqlite_Init(interp); |
| 281 | if( argc>=2 ){ |
| 282 | int i; |
| 283 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 284 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 285 | for(i=2; i<argc; i++){ |
| 286 | Tcl_SetVar(interp, "argv", argv[i], |
| 287 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 288 | } |
| 289 | if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 290 | char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
| 291 | if( zInfo==0 ) zInfo = interp->result; |
| 292 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 293 | return 1; |
| 294 | } |
| 295 | }else{ |
| 296 | Tcl_GlobalEval(interp, zMainloop); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | #endif /* TCLSH */ |