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