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