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