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