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