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 | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 14 | ** $Id: tclsqlite.c,v 1.100 2004/08/20 18:34:20 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 | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 18 | #include "sqliteInt.h" |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 19 | #include "hash.h" |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 20 | #include "tcl.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 23 | #include <assert.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 26 | ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
| 27 | ** have to do a translation when going between the two. Set the |
| 28 | ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
| 29 | ** this translation. |
| 30 | */ |
| 31 | #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
| 32 | # define UTF_TRANSLATION_NEEDED 1 |
| 33 | #endif |
| 34 | |
| 35 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 36 | ** New SQL functions can be created as TCL scripts. Each such function |
| 37 | ** is described by an instance of the following structure. |
| 38 | */ |
| 39 | typedef struct SqlFunc SqlFunc; |
| 40 | struct SqlFunc { |
| 41 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 42 | char *zScript; /* The script to be run */ |
| 43 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 44 | }; |
| 45 | |
| 46 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 47 | ** New collation sequences function can be created as TCL scripts. Each such |
| 48 | ** function is described by an instance of the following structure. |
| 49 | */ |
| 50 | typedef struct SqlCollate SqlCollate; |
| 51 | struct SqlCollate { |
| 52 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 53 | char *zScript; /* The script to be run */ |
| 54 | SqlCollate *pNext; /* Next function on the list of them all */ |
| 55 | }; |
| 56 | |
| 57 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 58 | ** There is one instance of this structure for each SQLite database |
| 59 | ** that has been opened by the SQLite TCL interface. |
| 60 | */ |
| 61 | typedef struct SqliteDb SqliteDb; |
| 62 | struct SqliteDb { |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 63 | sqlite3 *db; /* The "real" database structure */ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 64 | Tcl_Interp *interp; /* The interpreter used for this database */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 65 | char *zBusy; /* The busy callback routine */ |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 66 | char *zCommit; /* The commit hook callback routine */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 67 | char *zTrace; /* The trace callback routine */ |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 68 | char *zProgress; /* The progress callback routine */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 69 | char *zAuth; /* The authorization callback routine */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 70 | SqlFunc *pFunc; /* List of SQL functions */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 71 | SqlCollate *pCollate; /* List of SQL collation functions */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 72 | int rc; /* Return code of most recent sqlite3_exec() */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 73 | Tcl_Obj *pCollateNeeded; /* Collation needed script */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 74 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 75 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 76 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 77 | ** This is a second alternative callback for database queries. A the |
| 78 | ** first column of the first row of the result is made the TCL result. |
| 79 | */ |
| 80 | static int DbEvalCallback3( |
| 81 | void *clientData, /* An instance of CallbackData */ |
| 82 | int nCol, /* Number of columns in the result */ |
| 83 | char ** azCol, /* Data for each column */ |
| 84 | char ** azN /* Name for each column */ |
| 85 | ){ |
| 86 | Tcl_Interp *interp = (Tcl_Interp*)clientData; |
| 87 | Tcl_Obj *pElem; |
| 88 | if( azCol==0 ) return 1; |
| 89 | if( nCol==0 ) return 1; |
| 90 | #ifdef UTF_TRANSLATION_NEEDED |
| 91 | { |
| 92 | Tcl_DString dCol; |
| 93 | Tcl_DStringInit(&dCol); |
| 94 | Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol); |
| 95 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 96 | Tcl_DStringFree(&dCol); |
| 97 | } |
| 98 | #else |
| 99 | pElem = Tcl_NewStringObj(azCol[0], -1); |
| 100 | #endif |
| 101 | Tcl_SetObjResult(interp, pElem); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 106 | ** TCL calls this procedure when an sqlite3 database command is |
| 107 | ** deleted. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 108 | */ |
| 109 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 110 | SqliteDb *pDb = (SqliteDb*)db; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 111 | sqlite3_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 112 | while( pDb->pFunc ){ |
| 113 | SqlFunc *pFunc = pDb->pFunc; |
| 114 | pDb->pFunc = pFunc->pNext; |
| 115 | Tcl_Free((char*)pFunc); |
| 116 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 117 | while( pDb->pCollate ){ |
| 118 | SqlCollate *pCollate = pDb->pCollate; |
| 119 | pDb->pCollate = pCollate->pNext; |
| 120 | Tcl_Free((char*)pCollate); |
| 121 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 122 | if( pDb->zBusy ){ |
| 123 | Tcl_Free(pDb->zBusy); |
| 124 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 125 | if( pDb->zTrace ){ |
| 126 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 127 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 128 | if( pDb->zAuth ){ |
| 129 | Tcl_Free(pDb->zAuth); |
| 130 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 131 | Tcl_Free((char*)pDb); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | ** This routine is called when a database file is locked while trying |
| 136 | ** to execute SQL. |
| 137 | */ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 138 | static int DbBusyHandler(void *cd, int nTries){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 139 | SqliteDb *pDb = (SqliteDb*)cd; |
| 140 | int rc; |
| 141 | char zVal[30]; |
| 142 | char *zCmd; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 143 | Tcl_DString cmd; |
| 144 | |
| 145 | Tcl_DStringInit(&cmd); |
| 146 | Tcl_DStringAppend(&cmd, pDb->zBusy, -1); |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 147 | sprintf(zVal, "%d", nTries); |
| 148 | Tcl_DStringAppendElement(&cmd, zVal); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 149 | zCmd = Tcl_DStringValue(&cmd); |
| 150 | rc = Tcl_Eval(pDb->interp, zCmd); |
| 151 | Tcl_DStringFree(&cmd); |
| 152 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 153 | return 0; |
| 154 | } |
| 155 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 159 | ** This routine is invoked as the 'progress callback' for the database. |
| 160 | */ |
| 161 | static int DbProgressHandler(void *cd){ |
| 162 | SqliteDb *pDb = (SqliteDb*)cd; |
| 163 | int rc; |
| 164 | |
| 165 | assert( pDb->zProgress ); |
| 166 | rc = Tcl_Eval(pDb->interp, pDb->zProgress); |
| 167 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 168 | return 1; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 174 | ** This routine is called by the SQLite trace handler whenever a new |
| 175 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 176 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 177 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 178 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 179 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 180 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 181 | Tcl_DStringInit(&str); |
| 182 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 183 | Tcl_DStringAppendElement(&str, zSql); |
| 184 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 185 | Tcl_DStringFree(&str); |
| 186 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 190 | ** This routine is called when a transaction is committed. The |
| 191 | ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
| 192 | ** if it throws an exception, the transaction is rolled back instead |
| 193 | ** of being committed. |
| 194 | */ |
| 195 | static int DbCommitHandler(void *cd){ |
| 196 | SqliteDb *pDb = (SqliteDb*)cd; |
| 197 | int rc; |
| 198 | |
| 199 | rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
| 200 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 201 | return 1; |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 206 | static void tclCollateNeeded( |
| 207 | void *pCtx, |
| 208 | sqlite *db, |
| 209 | int enc, |
| 210 | const char *zName |
| 211 | ){ |
| 212 | SqliteDb *pDb = (SqliteDb *)pCtx; |
| 213 | Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); |
| 214 | Tcl_IncrRefCount(pScript); |
| 215 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); |
| 216 | Tcl_EvalObjEx(pDb->interp, pScript, 0); |
| 217 | Tcl_DecrRefCount(pScript); |
| 218 | } |
| 219 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 220 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 221 | ** This routine is called to evaluate an SQL collation function implemented |
| 222 | ** using TCL script. |
| 223 | */ |
| 224 | static int tclSqlCollate( |
| 225 | void *pCtx, |
| 226 | int nA, |
| 227 | const void *zA, |
| 228 | int nB, |
| 229 | const void *zB |
| 230 | ){ |
| 231 | SqlCollate *p = (SqlCollate *)pCtx; |
| 232 | Tcl_Obj *pCmd; |
| 233 | |
| 234 | pCmd = Tcl_NewStringObj(p->zScript, -1); |
| 235 | Tcl_IncrRefCount(pCmd); |
| 236 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); |
| 237 | Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); |
| 238 | Tcl_EvalObjEx(p->interp, pCmd, 0); |
| 239 | Tcl_DecrRefCount(pCmd); |
| 240 | return (atoi(Tcl_GetStringResult(p->interp))); |
| 241 | } |
| 242 | |
| 243 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 244 | ** This routine is called to evaluate an SQL function implemented |
| 245 | ** using TCL script. |
| 246 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 247 | static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 248 | SqlFunc *p = sqlite3_user_data(context); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 249 | Tcl_DString cmd; |
| 250 | int i; |
| 251 | int rc; |
| 252 | |
| 253 | Tcl_DStringInit(&cmd); |
| 254 | Tcl_DStringAppend(&cmd, p->zScript, -1); |
| 255 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 256 | if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 257 | Tcl_DStringAppendElement(&cmd, ""); |
| 258 | }else{ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 259 | Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i])); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 260 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 261 | } |
| 262 | rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd)); |
| 263 | if( rc ){ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 264 | sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 265 | }else{ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 266 | sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, |
| 267 | SQLITE_TRANSIENT); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 270 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 271 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 272 | /* |
| 273 | ** This is the authentication function. It appends the authentication |
| 274 | ** type code and the two arguments to zCmd[] then invokes the result |
| 275 | ** on the interpreter. The reply is examined to determine if the |
| 276 | ** authentication fails or succeeds. |
| 277 | */ |
| 278 | static int auth_callback( |
| 279 | void *pArg, |
| 280 | int code, |
| 281 | const char *zArg1, |
| 282 | const char *zArg2, |
| 283 | const char *zArg3, |
| 284 | const char *zArg4 |
| 285 | ){ |
| 286 | char *zCode; |
| 287 | Tcl_DString str; |
| 288 | int rc; |
| 289 | const char *zReply; |
| 290 | SqliteDb *pDb = (SqliteDb*)pArg; |
| 291 | |
| 292 | switch( code ){ |
| 293 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 294 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 295 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 296 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 297 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 298 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 299 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 300 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 301 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 302 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 303 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 304 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 305 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 306 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 307 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 308 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 309 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 310 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 311 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 312 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 313 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 314 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 315 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 316 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
drh | 81e293b | 2003-06-06 19:00:42 +0000 | [diff] [blame] | 317 | case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
| 318 | case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 319 | default : zCode="????"; break; |
| 320 | } |
| 321 | Tcl_DStringInit(&str); |
| 322 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 323 | Tcl_DStringAppendElement(&str, zCode); |
| 324 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 325 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 326 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 327 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 328 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 329 | Tcl_DStringFree(&str); |
| 330 | zReply = Tcl_GetStringResult(pDb->interp); |
| 331 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 332 | rc = SQLITE_OK; |
| 333 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 334 | rc = SQLITE_DENY; |
| 335 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 336 | rc = SQLITE_IGNORE; |
| 337 | }else{ |
| 338 | rc = 999; |
| 339 | } |
| 340 | return rc; |
| 341 | } |
| 342 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 343 | |
| 344 | /* |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 345 | ** zText is a pointer to text obtained via an sqlite3_result_text() |
| 346 | ** or similar interface. This routine returns a Tcl string object, |
| 347 | ** reference count set to 0, containing the text. If a translation |
| 348 | ** between iso8859 and UTF-8 is required, it is preformed. |
| 349 | */ |
| 350 | static Tcl_Obj *dbTextToObj(char const *zText){ |
| 351 | Tcl_Obj *pVal; |
| 352 | #ifdef UTF_TRANSLATION_NEEDED |
| 353 | Tcl_DString dCol; |
| 354 | Tcl_DStringInit(&dCol); |
| 355 | Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); |
| 356 | pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 357 | Tcl_DStringFree(&dCol); |
| 358 | #else |
| 359 | pVal = Tcl_NewStringObj(zText, -1); |
| 360 | #endif |
| 361 | return pVal; |
| 362 | } |
| 363 | |
| 364 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 365 | ** The "sqlite" command below creates a new Tcl command for each |
| 366 | ** connection it opens to an SQLite database. This routine is invoked |
| 367 | ** whenever one of those connection-specific commands is executed |
| 368 | ** in Tcl. For example, if you run Tcl code like this: |
| 369 | ** |
| 370 | ** sqlite db1 "my_database" |
| 371 | ** db1 close |
| 372 | ** |
| 373 | ** The first command opens a connection to the "my_database" database |
| 374 | ** and calls that connection "db1". The second command causes this |
| 375 | ** subroutine to be invoked. |
| 376 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 377 | 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] | 378 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 379 | int choice; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 380 | int rc = TCL_OK; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 381 | static const char *DB_strs[] = { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 382 | "authorizer", "busy", "changes", |
| 383 | "close", "collate", "collation_needed", |
| 384 | "commit_hook", "complete", "errorcode", |
| 385 | "eval", "function", "last_insert_rowid", |
| 386 | "onecolumn", "progress", "rekey", |
| 387 | "timeout", "total_changes", "trace", |
| 388 | 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 389 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 390 | enum DB_enum { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 391 | DB_AUTHORIZER, DB_BUSY, DB_CHANGES, |
| 392 | DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED, |
| 393 | DB_COMMIT_HOOK, DB_COMPLETE, DB_ERRORCODE, |
| 394 | DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID, |
| 395 | DB_ONECOLUMN, DB_PROGRESS, DB_REKEY, |
| 396 | DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | if( objc<2 ){ |
| 400 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 401 | return TCL_ERROR; |
| 402 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 403 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 404 | return TCL_ERROR; |
| 405 | } |
| 406 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 407 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 408 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 409 | /* $db authorizer ?CALLBACK? |
| 410 | ** |
| 411 | ** Invoke the given callback to authorize each SQL operation as it is |
| 412 | ** compiled. 5 arguments are appended to the callback before it is |
| 413 | ** invoked: |
| 414 | ** |
| 415 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 416 | ** (2) First descriptive name (depends on authorization type) |
| 417 | ** (3) Second descriptive name |
| 418 | ** (4) Name of the database (ex: "main", "temp") |
| 419 | ** (5) Name of trigger that is doing the access |
| 420 | ** |
| 421 | ** The callback should return on of the following strings: SQLITE_OK, |
| 422 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 423 | ** |
| 424 | ** If this method is invoked with no arguments, the current authorization |
| 425 | ** callback string is returned. |
| 426 | */ |
| 427 | case DB_AUTHORIZER: { |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 428 | #ifdef SQLITE_OMIT_AUTHORIZATION |
| 429 | Tcl_AppendResult(interp, "authorization not available in this build", 0); |
| 430 | return TCL_ERROR; |
| 431 | #else |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 432 | if( objc>3 ){ |
| 433 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 434 | return TCL_ERROR; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 435 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 436 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 437 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 438 | } |
| 439 | }else{ |
| 440 | char *zAuth; |
| 441 | int len; |
| 442 | if( pDb->zAuth ){ |
| 443 | Tcl_Free(pDb->zAuth); |
| 444 | } |
| 445 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 446 | if( zAuth && len>0 ){ |
| 447 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
| 448 | strcpy(pDb->zAuth, zAuth); |
| 449 | }else{ |
| 450 | pDb->zAuth = 0; |
| 451 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 452 | if( pDb->zAuth ){ |
| 453 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 454 | sqlite3_set_authorizer(pDb->db, auth_callback, pDb); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 455 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 456 | sqlite3_set_authorizer(pDb->db, 0, 0); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 457 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 458 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 459 | #endif |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 460 | break; |
| 461 | } |
| 462 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 463 | /* $db busy ?CALLBACK? |
| 464 | ** |
| 465 | ** Invoke the given callback if an SQL statement attempts to open |
| 466 | ** a locked database file. |
| 467 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 468 | case DB_BUSY: { |
| 469 | if( objc>3 ){ |
| 470 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 471 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 472 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 473 | if( pDb->zBusy ){ |
| 474 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 475 | } |
| 476 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 477 | char *zBusy; |
| 478 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 479 | if( pDb->zBusy ){ |
| 480 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 481 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 482 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 483 | if( zBusy && len>0 ){ |
| 484 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 485 | strcpy(pDb->zBusy, zBusy); |
| 486 | }else{ |
| 487 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 488 | } |
| 489 | if( pDb->zBusy ){ |
| 490 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 491 | sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 492 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 493 | sqlite3_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 496 | break; |
| 497 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 498 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 499 | /* $db progress ?N CALLBACK? |
| 500 | ** |
| 501 | ** Invoke the given callback every N virtual machine opcodes while executing |
| 502 | ** queries. |
| 503 | */ |
| 504 | case DB_PROGRESS: { |
| 505 | if( objc==2 ){ |
| 506 | if( pDb->zProgress ){ |
| 507 | Tcl_AppendResult(interp, pDb->zProgress, 0); |
| 508 | } |
| 509 | }else if( objc==4 ){ |
| 510 | char *zProgress; |
| 511 | int len; |
| 512 | int N; |
| 513 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ |
| 514 | return TCL_ERROR; |
| 515 | }; |
| 516 | if( pDb->zProgress ){ |
| 517 | Tcl_Free(pDb->zProgress); |
| 518 | } |
| 519 | zProgress = Tcl_GetStringFromObj(objv[3], &len); |
| 520 | if( zProgress && len>0 ){ |
| 521 | pDb->zProgress = Tcl_Alloc( len + 1 ); |
| 522 | strcpy(pDb->zProgress, zProgress); |
| 523 | }else{ |
| 524 | pDb->zProgress = 0; |
| 525 | } |
| 526 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 527 | if( pDb->zProgress ){ |
| 528 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 529 | sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 530 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 531 | sqlite3_progress_handler(pDb->db, 0, 0, 0); |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 532 | } |
| 533 | #endif |
| 534 | }else{ |
| 535 | Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); |
| 536 | return TCL_ERROR; |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 541 | /* $db changes |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 542 | ** |
| 543 | ** Return the number of rows that were modified, inserted, or deleted by |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 544 | ** the most recent INSERT, UPDATE or DELETE statement, not including |
| 545 | ** any changes made by trigger programs. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 546 | */ |
| 547 | case DB_CHANGES: { |
| 548 | Tcl_Obj *pResult; |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 549 | if( objc!=2 ){ |
| 550 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 551 | return TCL_ERROR; |
| 552 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 553 | pResult = Tcl_GetObjResult(interp); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 554 | Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | /* $db close |
| 559 | ** |
| 560 | ** Shutdown the database |
| 561 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 562 | case DB_CLOSE: { |
| 563 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 564 | break; |
| 565 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 566 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 567 | /* $db commit_hook ?CALLBACK? |
| 568 | ** |
| 569 | ** Invoke the given callback just before committing every SQL transaction. |
| 570 | ** If the callback throws an exception or returns non-zero, then the |
| 571 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 572 | ** is disabled. |
| 573 | */ |
| 574 | case DB_COMMIT_HOOK: { |
| 575 | if( objc>3 ){ |
| 576 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 577 | return TCL_ERROR; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 578 | }else if( objc==2 ){ |
| 579 | if( pDb->zCommit ){ |
| 580 | Tcl_AppendResult(interp, pDb->zCommit, 0); |
| 581 | } |
| 582 | }else{ |
| 583 | char *zCommit; |
| 584 | int len; |
| 585 | if( pDb->zCommit ){ |
| 586 | Tcl_Free(pDb->zCommit); |
| 587 | } |
| 588 | zCommit = Tcl_GetStringFromObj(objv[2], &len); |
| 589 | if( zCommit && len>0 ){ |
| 590 | pDb->zCommit = Tcl_Alloc( len + 1 ); |
| 591 | strcpy(pDb->zCommit, zCommit); |
| 592 | }else{ |
| 593 | pDb->zCommit = 0; |
| 594 | } |
| 595 | if( pDb->zCommit ){ |
| 596 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 597 | sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 598 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 599 | sqlite3_commit_hook(pDb->db, 0, 0); |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | break; |
| 603 | } |
| 604 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 605 | /* |
| 606 | ** $db collate NAME SCRIPT |
| 607 | ** |
| 608 | ** Create a new SQL collation function called NAME. Whenever |
| 609 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 610 | */ |
| 611 | case DB_COLLATE: { |
| 612 | SqlCollate *pCollate; |
| 613 | char *zName; |
| 614 | char *zScript; |
| 615 | int nScript; |
| 616 | if( objc!=4 ){ |
| 617 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 618 | return TCL_ERROR; |
| 619 | } |
| 620 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 621 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 622 | pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); |
| 623 | if( pCollate==0 ) return TCL_ERROR; |
| 624 | pCollate->interp = interp; |
| 625 | pCollate->pNext = pDb->pCollate; |
| 626 | pCollate->zScript = (char*)&pCollate[1]; |
| 627 | pDb->pCollate = pCollate; |
| 628 | strcpy(pCollate->zScript, zScript); |
| 629 | if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, |
| 630 | pCollate, tclSqlCollate) ){ |
| 631 | return TCL_ERROR; |
| 632 | } |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | ** $db collation_needed SCRIPT |
| 638 | ** |
| 639 | ** Create a new SQL collation function called NAME. Whenever |
| 640 | ** that function is called, invoke SCRIPT to evaluate the function. |
| 641 | */ |
| 642 | case DB_COLLATION_NEEDED: { |
| 643 | if( objc!=3 ){ |
| 644 | Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); |
| 645 | return TCL_ERROR; |
| 646 | } |
| 647 | if( pDb->pCollateNeeded ){ |
| 648 | Tcl_DecrRefCount(pDb->pCollateNeeded); |
| 649 | } |
| 650 | pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); |
| 651 | Tcl_IncrRefCount(pDb->pCollateNeeded); |
| 652 | sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); |
| 653 | break; |
| 654 | } |
| 655 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 656 | /* $db complete SQL |
| 657 | ** |
| 658 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 659 | ** additional lines of input are needed. This is similar to the |
| 660 | ** built-in "info complete" command of Tcl. |
| 661 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 662 | case DB_COMPLETE: { |
| 663 | Tcl_Obj *pResult; |
| 664 | int isComplete; |
| 665 | if( objc!=3 ){ |
| 666 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 667 | return TCL_ERROR; |
| 668 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 669 | isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 670 | pResult = Tcl_GetObjResult(interp); |
| 671 | Tcl_SetBooleanObj(pResult, isComplete); |
| 672 | break; |
| 673 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | ** $db errorcode |
| 677 | ** |
| 678 | ** Return the numeric error code that was returned by the most recent |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 679 | ** call to sqlite3_exec(). |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 680 | */ |
| 681 | case DB_ERRORCODE: { |
danielk1977 | f3ce83f | 2004-06-14 11:43:46 +0000 | [diff] [blame] | 682 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 685 | |
| 686 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 687 | ** $db eval $sql ?array? ?{ ...code... }? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 688 | ** |
| 689 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 690 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 691 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 692 | ** If "array" is an empty string, then the values are placed in variables |
| 693 | ** that have the same name as the fields extracted by the query. |
| 694 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 695 | case DB_EVAL: { |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 696 | char const *zSql; |
| 697 | char const *zLeft; |
| 698 | sqlite3_stmt *pStmt; |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 699 | Tcl_Obj *pArray; /* Name of array into which results are written */ |
| 700 | Tcl_Obj *pScript; /* Script to run for each result set */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 701 | |
| 702 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 703 | Tcl_IncrRefCount(pRet); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 704 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 705 | if( objc<3 || objc>5 ){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 706 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 707 | return TCL_ERROR; |
| 708 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 709 | if( objc==3 ){ |
| 710 | pArray = pScript = 0; |
| 711 | }else if( objc==4 ){ |
| 712 | pArray = 0; |
| 713 | pScript = objv[3]; |
| 714 | }else{ |
| 715 | pArray = objv[3]; |
| 716 | if( Tcl_GetString(pArray)[0]==0 ) pArray = 0; |
| 717 | pScript = objv[4]; |
| 718 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 719 | |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 720 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
| 721 | while( zSql[0] ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 722 | int i; /* Loop counter */ |
| 723 | int nVar; /* Number of wildcards in the SQL */ |
| 724 | int nCol; /* Number of columns in the result set */ |
| 725 | Tcl_Obj **apColName = 0; /* Array of column names */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 726 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 727 | /* Compile a single SQL statement */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 728 | if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 729 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 730 | rc = TCL_ERROR; |
| 731 | break; |
| 732 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 733 | if( pStmt==0 ){ |
| 734 | if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
| 735 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
| 736 | rc = TCL_ERROR; |
| 737 | break; |
| 738 | }else{ |
| 739 | zSql = zLeft; |
| 740 | continue; |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 741 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 742 | } |
| 743 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 744 | /* Bind values to wildcards that begin with $ */ |
| 745 | nVar = sqlite3_bind_parameter_count(pStmt); |
| 746 | for(i=1; i<=nVar; i++){ |
| 747 | const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
| 748 | if( zVar[0]=='$' ){ |
| 749 | Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
| 750 | if( pVar ){ |
| 751 | int n; |
| 752 | u8 *data; |
| 753 | char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
| 754 | char c = zType[0]; |
| 755 | if( c=='b' && strcmp(zType,"bytearray")==0 ){ |
| 756 | data = Tcl_GetByteArrayFromObj(pVar, &n); |
| 757 | sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
| 758 | }else if( (c=='b' && strcmp(zType,"boolean")==0) || |
| 759 | (c=='i' && strcmp(zType,"int")==0) ){ |
| 760 | Tcl_GetIntFromObj(interp, pVar, &n); |
| 761 | sqlite3_bind_int(pStmt, i, n); |
| 762 | }else if( c=='d' && strcmp(zType,"double")==0 ){ |
| 763 | double r; |
| 764 | Tcl_GetDoubleFromObj(interp, pVar, &r); |
| 765 | sqlite3_bind_double(pStmt, i, r); |
| 766 | }else{ |
| 767 | data = Tcl_GetStringFromObj(pVar, &n); |
| 768 | sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | |
| 775 | /* Compute column names */ |
| 776 | nCol = sqlite3_column_count(pStmt); |
| 777 | if( pScript ){ |
| 778 | apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
| 779 | if( apColName==0 ) break; |
| 780 | for(i=0; i<nCol; i++){ |
| 781 | apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
| 782 | Tcl_IncrRefCount(apColName[i]); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | /* If results are being stored in an array variable, then create |
| 787 | ** the array(*) entry for that array |
| 788 | */ |
| 789 | if( pArray ){ |
| 790 | Tcl_Obj *pColList = Tcl_NewObj(); |
| 791 | Tcl_IncrRefCount(pColList); |
| 792 | for(i=0; i<nCol; i++){ |
| 793 | Tcl_ListObjAppendElement(interp, pColList, apColName[i]); |
| 794 | } |
| 795 | Tcl_ObjSetVar2(interp, pArray, Tcl_NewStringObj("*",-1), pColList,0); |
| 796 | } |
| 797 | |
| 798 | /* Execute the SQL |
| 799 | */ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 800 | while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 801 | for(i=0; i<nCol; i++){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 802 | Tcl_Obj *pVal; |
| 803 | |
| 804 | /* Set pVal to contain the i'th column of this row. */ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 805 | switch( sqlite3_column_type(pStmt, i) ){ |
| 806 | case SQLITE_BLOB: { |
| 807 | int bytes = sqlite3_column_bytes(pStmt, i); |
| 808 | pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes); |
| 809 | break; |
| 810 | } |
| 811 | case SQLITE_INTEGER: { |
| 812 | sqlite_int64 v = sqlite3_column_int64(pStmt, i); |
| 813 | if( v>=-2147483647 && v<=2147483647 ){ |
| 814 | pVal = Tcl_NewIntObj(v); |
| 815 | }else{ |
| 816 | pVal = Tcl_NewWideIntObj(v); |
| 817 | } |
| 818 | break; |
| 819 | } |
| 820 | case SQLITE_FLOAT: { |
| 821 | double r = sqlite3_column_double(pStmt, i); |
| 822 | pVal = Tcl_NewDoubleObj(r); |
| 823 | break; |
| 824 | } |
| 825 | default: { |
| 826 | pVal = dbTextToObj(sqlite3_column_text(pStmt, i)); |
| 827 | break; |
| 828 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 829 | } |
| 830 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 831 | if( pScript ){ |
| 832 | if( pArray==0 ){ |
| 833 | Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 834 | }else{ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 835 | Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 836 | } |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 837 | }else{ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 838 | Tcl_ListObjAppendElement(interp, pRet, pVal); |
| 839 | } |
| 840 | } |
| 841 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 842 | if( pScript ){ |
| 843 | rc = Tcl_EvalObjEx(interp, pScript, 0); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 844 | if( rc!=TCL_ERROR ) rc = TCL_OK; |
| 845 | } |
| 846 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 847 | |
| 848 | /* Free the column name objects */ |
| 849 | if( pScript ){ |
| 850 | for(i=0; i<nCol; i++){ |
| 851 | Tcl_DecrRefCount(apColName[i]); |
| 852 | } |
| 853 | Tcl_Free((char*)apColName); |
| 854 | } |
| 855 | |
| 856 | /* Finalize the statement. If the result code is SQLITE_SCHEMA, then |
| 857 | ** try again to execute the same statement |
| 858 | */ |
| 859 | if( SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 860 | continue; |
| 861 | } |
| 862 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 863 | if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 864 | Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 865 | rc = TCL_ERROR; |
| 866 | break; |
| 867 | } |
| 868 | |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 869 | zSql = zLeft; |
| 870 | } |
| 871 | |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 872 | if( rc==TCL_OK ){ |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 873 | Tcl_SetObjResult(interp, pRet); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 874 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 875 | Tcl_DecrRefCount(pRet); |
danielk1977 | 30ccda1 | 2004-05-27 12:11:31 +0000 | [diff] [blame] | 876 | |
| 877 | break; |
| 878 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 879 | |
| 880 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 881 | ** $db function NAME SCRIPT |
| 882 | ** |
| 883 | ** Create a new SQL function called NAME. Whenever that function is |
| 884 | ** called, invoke SCRIPT to evaluate the function. |
| 885 | */ |
| 886 | case DB_FUNCTION: { |
| 887 | SqlFunc *pFunc; |
| 888 | char *zName; |
| 889 | char *zScript; |
| 890 | int nScript; |
| 891 | if( objc!=4 ){ |
| 892 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 893 | return TCL_ERROR; |
| 894 | } |
| 895 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 896 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 897 | pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 ); |
| 898 | if( pFunc==0 ) return TCL_ERROR; |
| 899 | pFunc->interp = interp; |
| 900 | pFunc->pNext = pDb->pFunc; |
| 901 | pFunc->zScript = (char*)&pFunc[1]; |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 902 | pDb->pFunc = pFunc; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 903 | strcpy(pFunc->zScript, zScript); |
danielk1977 | c8c1158 | 2004-06-29 13:41:21 +0000 | [diff] [blame] | 904 | rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 905 | pFunc, tclSqlFunc, 0, 0); |
danielk1977 | c8c1158 | 2004-06-29 13:41:21 +0000 | [diff] [blame] | 906 | if( rc!=SQLITE_OK ) rc = TCL_ERROR; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 907 | break; |
| 908 | } |
| 909 | |
| 910 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 911 | ** $db last_insert_rowid |
| 912 | ** |
| 913 | ** Return an integer which is the ROWID for the most recent insert. |
| 914 | */ |
| 915 | case DB_LAST_INSERT_ROWID: { |
| 916 | Tcl_Obj *pResult; |
| 917 | int rowid; |
| 918 | if( objc!=2 ){ |
| 919 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 920 | return TCL_ERROR; |
| 921 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 922 | rowid = sqlite3_last_insert_rowid(pDb->db); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 923 | pResult = Tcl_GetObjResult(interp); |
| 924 | Tcl_SetIntObj(pResult, rowid); |
| 925 | break; |
| 926 | } |
| 927 | |
| 928 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 929 | ** $db onecolumn SQL |
| 930 | ** |
| 931 | ** Return a single column from a single row of the given SQL query. |
| 932 | */ |
| 933 | case DB_ONECOLUMN: { |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 934 | char *zSql; |
| 935 | char *zErrMsg = 0; |
| 936 | if( objc!=3 ){ |
| 937 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
| 938 | return TCL_ERROR; |
| 939 | } |
| 940 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 941 | rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg); |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 942 | if( rc==SQLITE_ABORT ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 943 | rc = SQLITE_OK; |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 944 | }else if( zErrMsg ){ |
| 945 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 946 | free(zErrMsg); |
| 947 | rc = TCL_ERROR; |
| 948 | }else if( rc!=SQLITE_OK ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 949 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 950 | rc = TCL_ERROR; |
| 951 | } |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 956 | ** $db rekey KEY |
| 957 | ** |
| 958 | ** Change the encryption key on the currently open database. |
| 959 | */ |
| 960 | case DB_REKEY: { |
| 961 | int nKey; |
| 962 | void *pKey; |
| 963 | if( objc!=3 ){ |
| 964 | Tcl_WrongNumArgs(interp, 2, objv, "KEY"); |
| 965 | return TCL_ERROR; |
| 966 | } |
| 967 | pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 968 | #ifdef SQLITE_HAS_CODEC |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 969 | rc = sqlite3_rekey(pDb->db, pKey, nKey); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 970 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 971 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 972 | rc = TCL_ERROR; |
| 973 | } |
| 974 | #endif |
| 975 | break; |
| 976 | } |
| 977 | |
| 978 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 979 | ** $db timeout MILLESECONDS |
| 980 | ** |
| 981 | ** Delay for the number of milliseconds specified when a file is locked. |
| 982 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 983 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 984 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 985 | if( objc!=3 ){ |
| 986 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 987 | return TCL_ERROR; |
| 988 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 989 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 990 | sqlite3_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 991 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 992 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 993 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 994 | /* |
| 995 | ** $db total_changes |
| 996 | ** |
| 997 | ** Return the number of rows that were modified, inserted, or deleted |
| 998 | ** since the database handle was created. |
| 999 | */ |
| 1000 | case DB_TOTAL_CHANGES: { |
| 1001 | Tcl_Obj *pResult; |
| 1002 | if( objc!=2 ){ |
| 1003 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 1004 | return TCL_ERROR; |
| 1005 | } |
| 1006 | pResult = Tcl_GetObjResult(interp); |
| 1007 | Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); |
| 1008 | break; |
| 1009 | } |
| 1010 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1011 | /* $db trace ?CALLBACK? |
| 1012 | ** |
| 1013 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 1014 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 1015 | ** it is executed. |
| 1016 | */ |
| 1017 | case DB_TRACE: { |
| 1018 | if( objc>3 ){ |
| 1019 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 1020 | return TCL_ERROR; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1021 | }else if( objc==2 ){ |
| 1022 | if( pDb->zTrace ){ |
| 1023 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 1024 | } |
| 1025 | }else{ |
| 1026 | char *zTrace; |
| 1027 | int len; |
| 1028 | if( pDb->zTrace ){ |
| 1029 | Tcl_Free(pDb->zTrace); |
| 1030 | } |
| 1031 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 1032 | if( zTrace && len>0 ){ |
| 1033 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
| 1034 | strcpy(pDb->zTrace, zTrace); |
| 1035 | }else{ |
| 1036 | pDb->zTrace = 0; |
| 1037 | } |
| 1038 | if( pDb->zTrace ){ |
| 1039 | pDb->interp = interp; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1040 | sqlite3_trace(pDb->db, DbTraceHandler, pDb); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1041 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1042 | sqlite3_trace(pDb->db, 0, 0); |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | break; |
| 1046 | } |
| 1047 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1048 | } /* End of the SWITCH statement */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1049 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1053 | ** sqlite DBNAME FILENAME ?MODE? ?-key KEY? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1054 | ** |
| 1055 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 1056 | ** invoked, this routine runs to process that command. |
| 1057 | ** |
| 1058 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 1059 | ** database connection. This command creates a new command named |
| 1060 | ** DBNAME that is used to control that connection. The database |
| 1061 | ** connection is deleted when the DBNAME command is deleted. |
| 1062 | ** |
| 1063 | ** The second argument is the name of the directory that contains |
| 1064 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1065 | ** |
| 1066 | ** For testing purposes, we also support the following: |
| 1067 | ** |
| 1068 | ** sqlite -encoding |
| 1069 | ** |
| 1070 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 1071 | ** are UTF-8 and iso8859. |
| 1072 | ** |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 1073 | ** sqlite -version |
| 1074 | ** |
| 1075 | ** Return the version number of the SQLite library. |
| 1076 | ** |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1077 | ** sqlite -tcl-uses-utf |
| 1078 | ** |
| 1079 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 1080 | ** not. Used by tests to make sure the library was compiled |
| 1081 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1082 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1083 | static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1084 | SqliteDb *p; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1085 | void *pKey = 0; |
| 1086 | int nKey = 0; |
| 1087 | const char *zArg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1088 | char *zErrMsg; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1089 | const char *zFile; |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1090 | char zBuf[80]; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1091 | if( objc==2 ){ |
| 1092 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1093 | if( strcmp(zArg,"-version")==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1094 | Tcl_AppendResult(interp,sqlite3_version,0); |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 1095 | return TCL_OK; |
| 1096 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1097 | if( strcmp(zArg,"-has-codec")==0 ){ |
| 1098 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1099 | Tcl_AppendResult(interp,"1",0); |
| 1100 | #else |
| 1101 | Tcl_AppendResult(interp,"0",0); |
| 1102 | #endif |
| 1103 | return TCL_OK; |
| 1104 | } |
| 1105 | if( strcmp(zArg,"-tcl-uses-utf")==0 ){ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1106 | #ifdef TCL_UTF_MAX |
| 1107 | Tcl_AppendResult(interp,"1",0); |
| 1108 | #else |
| 1109 | Tcl_AppendResult(interp,"0",0); |
| 1110 | #endif |
| 1111 | return TCL_OK; |
| 1112 | } |
| 1113 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1114 | if( objc==5 || objc==6 ){ |
| 1115 | zArg = Tcl_GetStringFromObj(objv[objc-2], 0); |
| 1116 | if( strcmp(zArg,"-key")==0 ){ |
| 1117 | pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey); |
| 1118 | objc -= 2; |
| 1119 | } |
| 1120 | } |
| 1121 | if( objc!=3 && objc!=4 ){ |
| 1122 | Tcl_WrongNumArgs(interp, 1, objv, |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1123 | #ifdef SQLITE_HAS_CODEC |
| 1124 | "HANDLE FILENAME ?-key CODEC-KEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1125 | #else |
| 1126 | "HANDLE FILENAME ?MODE?" |
| 1127 | #endif |
| 1128 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1129 | return TCL_ERROR; |
| 1130 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1131 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 1132 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1133 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1134 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 1135 | return TCL_ERROR; |
| 1136 | } |
| 1137 | memset(p, 0, sizeof(*p)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1138 | zFile = Tcl_GetStringFromObj(objv[2], 0); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1139 | sqlite3_open(zFile, &p->db); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 1140 | if( SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 1141 | zErrMsg = strdup(sqlite3_errmsg(p->db)); |
| 1142 | sqlite3_close(p->db); |
| 1143 | p->db = 0; |
| 1144 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1145 | #ifdef SQLITE_HAS_CODEC |
| 1146 | sqlite3_key(p->db, pKey, nKey); |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 1147 | #endif |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1148 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1149 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1150 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1151 | free(zErrMsg); |
| 1152 | return TCL_ERROR; |
| 1153 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1154 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
| 1155 | Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1156 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1157 | /* The return value is the value of the sqlite* pointer |
| 1158 | */ |
| 1159 | sprintf(zBuf, "%p", p->db); |
drh | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame] | 1160 | if( strncmp(zBuf,"0x",2) ){ |
| 1161 | sprintf(zBuf, "0x%p", p->db); |
| 1162 | } |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1163 | Tcl_AppendResult(interp, zBuf, 0); |
| 1164 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1165 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1166 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1167 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 1168 | #ifdef SQLITE_TEST |
| 1169 | { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1170 | extern void Md5_Register(sqlite*); |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1171 | #ifdef SQLITE_DEBUG |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 1172 | int mallocfail = sqlite3_iMallocFail; |
| 1173 | sqlite3_iMallocFail = 0; |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1174 | #endif |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1175 | Md5_Register(p->db); |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1176 | #ifdef SQLITE_DEBUG |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 1177 | sqlite3_iMallocFail = mallocfail; |
danielk1977 | 5b59af8 | 2004-06-30 12:42:59 +0000 | [diff] [blame] | 1178 | #endif |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1179 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 1180 | #endif |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1181 | p->interp = interp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1182 | return TCL_OK; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1186 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 1187 | ** library. |
| 1188 | */ |
| 1189 | #ifndef USE_TCL_STUBS |
| 1190 | # undef Tcl_InitStubs |
| 1191 | # define Tcl_InitStubs(a,b,c) |
| 1192 | #endif |
| 1193 | |
| 1194 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1195 | ** Initialize this module. |
| 1196 | ** |
| 1197 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 1198 | ** (Hence there is no namespace. There is no point in using a namespace |
| 1199 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 1200 | ** used to open a new SQLite database. See the DbMain() routine above |
| 1201 | ** for additional information. |
| 1202 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1203 | int Sqlite3_Init(Tcl_Interp *interp){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1204 | Tcl_InitStubs(interp, "8.4", 0); |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 1205 | Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
| 1206 | Tcl_PkgProvide(interp, "sqlite3", "3.0"); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1207 | return TCL_OK; |
| 1208 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1209 | int Tclsqlite3_Init(Tcl_Interp *interp){ |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 1210 | Tcl_InitStubs(interp, "8.4", 0); |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 1211 | Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
| 1212 | Tcl_PkgProvide(interp, "sqlite3", "3.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1213 | return TCL_OK; |
| 1214 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1215 | int Sqlite3_SafeInit(Tcl_Interp *interp){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1216 | return TCL_OK; |
| 1217 | } |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1218 | int Tclsqlite3_SafeInit(Tcl_Interp *interp){ |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1219 | return TCL_OK; |
| 1220 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1221 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1222 | #ifdef TCLSH |
| 1223 | /***************************************************************************** |
| 1224 | ** The code that follows is used to build standalone TCL interpreters |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1225 | */ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1226 | |
| 1227 | /* |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1228 | ** If the macro TCLSH is one, then put in code this for the |
| 1229 | ** "main" routine that will initialize Tcl and take input from |
| 1230 | ** standard input. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1231 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1232 | #if TCLSH==1 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1233 | static char zMainloop[] = |
| 1234 | "set line {}\n" |
| 1235 | "while {![eof stdin]} {\n" |
| 1236 | "if {$line!=\"\"} {\n" |
| 1237 | "puts -nonewline \"> \"\n" |
| 1238 | "} else {\n" |
| 1239 | "puts -nonewline \"% \"\n" |
| 1240 | "}\n" |
| 1241 | "flush stdout\n" |
| 1242 | "append line [gets stdin]\n" |
| 1243 | "if {[info complete $line]} {\n" |
| 1244 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 1245 | "puts stderr \"Error: $result\"\n" |
| 1246 | "} elseif {$result!=\"\"} {\n" |
| 1247 | "puts $result\n" |
| 1248 | "}\n" |
| 1249 | "set line {}\n" |
| 1250 | "} else {\n" |
| 1251 | "append line \\n\n" |
| 1252 | "}\n" |
| 1253 | "}\n" |
| 1254 | ; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1255 | #endif |
| 1256 | |
| 1257 | /* |
| 1258 | ** If the macro TCLSH is two, then get the main loop code out of |
| 1259 | ** the separate file "spaceanal_tcl.h". |
| 1260 | */ |
| 1261 | #if TCLSH==2 |
| 1262 | static char zMainloop[] = |
| 1263 | #include "spaceanal_tcl.h" |
| 1264 | ; |
| 1265 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1266 | |
| 1267 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 1268 | int TCLSH_MAIN(int argc, char **argv){ |
| 1269 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1270 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1271 | interp = Tcl_CreateInterp(); |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 1272 | Sqlite3_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1273 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1274 | { |
| 1275 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1276 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 1277 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 1278 | extern int Sqlitetest4_Init(Tcl_Interp*); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 1279 | extern int Sqlitetest5_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1280 | extern int Md5_Init(Tcl_Interp*); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 1281 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1282 | Sqlitetest2_Init(interp); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1283 | Sqlitetest3_Init(interp); |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1284 | Sqlitetest4_Init(interp); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 1285 | Sqlitetest5_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1286 | Md5_Init(interp); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1287 | } |
| 1288 | #endif |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1289 | if( argc>=2 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1290 | int i; |
| 1291 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 1292 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 1293 | for(i=2; i<argc; i++){ |
| 1294 | Tcl_SetVar(interp, "argv", argv[i], |
| 1295 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 1296 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1297 | if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1298 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1299 | if( zInfo==0 ) zInfo = interp->result; |
| 1300 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1301 | return 1; |
| 1302 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 1303 | } |
| 1304 | if( argc<=1 || TCLSH==2 ){ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1305 | Tcl_GlobalEval(interp, zMainloop); |
| 1306 | } |
| 1307 | return 0; |
| 1308 | } |
| 1309 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1310 | |
| 1311 | #endif /* !defined(NO_TCL) */ |