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