blob: bc98e348df40962b34861e34c333a22b18b25cf7 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** A TCL Interface to SQLite
13**
drhe6e04962005-07-23 02:17:03 +000014** $Id: tclsqlite.c,v 1.128 2005/07/23 02:17:03 drh Exp $
drh75897232000-05-29 14:26:00 +000015*/
drh6d313162000-09-21 13:01:35 +000016#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
17
drh06b27182002-06-26 20:06:05 +000018#include "sqliteInt.h"
drh895d7472004-08-20 16:02:39 +000019#include "hash.h"
drh17a68932001-01-31 13:28:08 +000020#include "tcl.h"
drh75897232000-05-29 14:26:00 +000021#include <stdlib.h>
22#include <string.h>
drhce927062001-11-09 13:41:09 +000023#include <assert.h>
drhd1e47332005-06-26 17:55:33 +000024#include <ctype.h>
drh75897232000-05-29 14:26:00 +000025
danielk1977a21c6b62005-01-24 10:25:59 +000026#define NUM_PREPARED_STMTS 10
drhfb7e7652005-01-24 00:28:42 +000027#define MAX_PREPARED_STMTS 100
28
drh75897232000-05-29 14:26:00 +000029/*
drh98808ba2001-10-18 12:34:46 +000030** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
31** have to do a translation when going between the two. Set the
32** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
33** this translation.
34*/
35#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
36# define UTF_TRANSLATION_NEEDED 1
37#endif
38
39/*
drhcabb0812002-09-14 13:47:32 +000040** New SQL functions can be created as TCL scripts. Each such function
41** is described by an instance of the following structure.
42*/
43typedef struct SqlFunc SqlFunc;
44struct SqlFunc {
45 Tcl_Interp *interp; /* The TCL interpret to execute the function */
drhd1e47332005-06-26 17:55:33 +000046 Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */
47 int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */
48 char *zName; /* Name of this function */
drhcabb0812002-09-14 13:47:32 +000049 SqlFunc *pNext; /* Next function on the list of them all */
50};
51
52/*
danielk19770202b292004-06-09 09:55:16 +000053** New collation sequences function can be created as TCL scripts. Each such
54** function is described by an instance of the following structure.
55*/
56typedef struct SqlCollate SqlCollate;
57struct SqlCollate {
58 Tcl_Interp *interp; /* The TCL interpret to execute the function */
59 char *zScript; /* The script to be run */
drhd1e47332005-06-26 17:55:33 +000060 SqlCollate *pNext; /* Next function on the list of them all */
danielk19770202b292004-06-09 09:55:16 +000061};
62
63/*
drhfb7e7652005-01-24 00:28:42 +000064** Prepared statements are cached for faster execution. Each prepared
65** statement is described by an instance of the following structure.
66*/
67typedef struct SqlPreparedStmt SqlPreparedStmt;
68struct SqlPreparedStmt {
69 SqlPreparedStmt *pNext; /* Next in linked list */
70 SqlPreparedStmt *pPrev; /* Previous on the list */
71 sqlite3_stmt *pStmt; /* The prepared statement */
72 int nSql; /* chars in zSql[] */
73 char zSql[1]; /* Text of the SQL statement */
74};
75
76/*
drhbec3f402000-08-04 13:49:02 +000077** There is one instance of this structure for each SQLite database
78** that has been opened by the SQLite TCL interface.
79*/
80typedef struct SqliteDb SqliteDb;
81struct SqliteDb {
drhd1e47332005-06-26 17:55:33 +000082 sqlite3 *db; /* The "real" database structure */
83 Tcl_Interp *interp; /* The interpreter used for this database */
84 char *zBusy; /* The busy callback routine */
85 char *zCommit; /* The commit hook callback routine */
86 char *zTrace; /* The trace callback routine */
87 char *zProgress; /* The progress callback routine */
88 char *zAuth; /* The authorization callback routine */
89 char *zNull; /* Text to substitute for an SQL NULL value */
90 SqlFunc *pFunc; /* List of SQL functions */
91 SqlCollate *pCollate; /* List of SQL collation functions */
92 int rc; /* Return code of most recent sqlite3_exec() */
93 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhfb7e7652005-01-24 00:28:42 +000094 SqlPreparedStmt *stmtList; /* List of prepared statements*/
95 SqlPreparedStmt *stmtLast; /* Last statement in the list */
96 int maxStmt; /* The next maximum number of stmtList */
97 int nStmt; /* Number of statements in stmtList */
drh98808ba2001-10-18 12:34:46 +000098};
drh297ecf12001-04-05 15:57:13 +000099
drh6d313162000-09-21 13:01:35 +0000100/*
drhd1e47332005-06-26 17:55:33 +0000101** Look at the script prefix in pCmd. We will be executing this script
102** after first appending one or more arguments. This routine analyzes
103** the script to see if it is safe to use Tcl_EvalObjv() on the script
104** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much
105** faster.
106**
107** Scripts that are safe to use with Tcl_EvalObjv() consists of a
108** command name followed by zero or more arguments with no [...] or $
109** or {...} or ; to be seen anywhere. Most callback scripts consist
110** of just a single procedure name and they meet this requirement.
111*/
112static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){
113 /* We could try to do something with Tcl_Parse(). But we will instead
114 ** just do a search for forbidden characters. If any of the forbidden
115 ** characters appear in pCmd, we will report the string as unsafe.
116 */
117 const char *z;
118 int n;
119 z = Tcl_GetStringFromObj(pCmd, &n);
120 while( n-- > 0 ){
121 int c = *(z++);
122 if( c=='$' || c=='[' || c==';' ) return 0;
123 }
124 return 1;
125}
126
127/*
128** Find an SqlFunc structure with the given name. Or create a new
129** one if an existing one cannot be found. Return a pointer to the
130** structure.
131*/
132static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){
133 SqlFunc *p, *pNew;
134 int i;
135 pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen(zName) + 1 );
136 pNew->zName = (char*)&pNew[1];
137 for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); }
138 pNew->zName[i] = 0;
139 for(p=pDb->pFunc; p; p=p->pNext){
140 if( strcmp(p->zName, pNew->zName)==0 ){
141 Tcl_Free((char*)pNew);
142 return p;
143 }
144 }
145 pNew->interp = pDb->interp;
146 pNew->pScript = 0;
147 pNew->pNext = pDb->pFunc;
148 pDb->pFunc = pNew;
149 return pNew;
150}
151
152/*
drhfb7e7652005-01-24 00:28:42 +0000153** Finalize and free a list of prepared statements
154*/
155static void flushStmtCache( SqliteDb *pDb ){
156 SqlPreparedStmt *pPreStmt;
157
158 while( pDb->stmtList ){
159 sqlite3_finalize( pDb->stmtList->pStmt );
160 pPreStmt = pDb->stmtList;
161 pDb->stmtList = pDb->stmtList->pNext;
162 Tcl_Free( (char*)pPreStmt );
163 }
164 pDb->nStmt = 0;
165 pDb->stmtLast = 0;
166}
167
168/*
drh895d7472004-08-20 16:02:39 +0000169** TCL calls this procedure when an sqlite3 database command is
170** deleted.
drh75897232000-05-29 14:26:00 +0000171*/
172static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000173 SqliteDb *pDb = (SqliteDb*)db;
drhfb7e7652005-01-24 00:28:42 +0000174 flushStmtCache(pDb);
danielk19776f8a5032004-05-10 10:34:51 +0000175 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000176 while( pDb->pFunc ){
177 SqlFunc *pFunc = pDb->pFunc;
178 pDb->pFunc = pFunc->pNext;
drhd1e47332005-06-26 17:55:33 +0000179 Tcl_DecrRefCount(pFunc->pScript);
drhcabb0812002-09-14 13:47:32 +0000180 Tcl_Free((char*)pFunc);
181 }
danielk19770202b292004-06-09 09:55:16 +0000182 while( pDb->pCollate ){
183 SqlCollate *pCollate = pDb->pCollate;
184 pDb->pCollate = pCollate->pNext;
185 Tcl_Free((char*)pCollate);
186 }
drhbec3f402000-08-04 13:49:02 +0000187 if( pDb->zBusy ){
188 Tcl_Free(pDb->zBusy);
189 }
drhb5a20d32003-04-23 12:25:23 +0000190 if( pDb->zTrace ){
191 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000192 }
drhe22a3342003-04-22 20:30:37 +0000193 if( pDb->zAuth ){
194 Tcl_Free(pDb->zAuth);
195 }
danielk197755c45f22005-04-03 23:54:43 +0000196 if( pDb->zNull ){
197 Tcl_Free(pDb->zNull);
198 }
drhbec3f402000-08-04 13:49:02 +0000199 Tcl_Free((char*)pDb);
200}
201
202/*
203** This routine is called when a database file is locked while trying
204** to execute SQL.
205*/
danielk19772a764eb2004-06-12 01:43:26 +0000206static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000207 SqliteDb *pDb = (SqliteDb*)cd;
208 int rc;
209 char zVal[30];
drhbec3f402000-08-04 13:49:02 +0000210
danielk19772a764eb2004-06-12 01:43:26 +0000211 sprintf(zVal, "%d", nTries);
drhd1e47332005-06-26 17:55:33 +0000212 rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0);
drhbec3f402000-08-04 13:49:02 +0000213 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
214 return 0;
215 }
216 return 1;
drh75897232000-05-29 14:26:00 +0000217}
218
219/*
danielk1977348bb5d2003-10-18 09:37:26 +0000220** This routine is invoked as the 'progress callback' for the database.
221*/
222static int DbProgressHandler(void *cd){
223 SqliteDb *pDb = (SqliteDb*)cd;
224 int rc;
225
226 assert( pDb->zProgress );
227 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
228 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
229 return 1;
230 }
231 return 0;
232}
233
234/*
drhb5a20d32003-04-23 12:25:23 +0000235** This routine is called by the SQLite trace handler whenever a new
236** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000237*/
drhb5a20d32003-04-23 12:25:23 +0000238static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000239 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000240 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000241
drhb5a20d32003-04-23 12:25:23 +0000242 Tcl_DStringInit(&str);
243 Tcl_DStringAppend(&str, pDb->zTrace, -1);
244 Tcl_DStringAppendElement(&str, zSql);
245 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
246 Tcl_DStringFree(&str);
247 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000248}
249
250/*
drhaa940ea2004-01-15 02:44:03 +0000251** This routine is called when a transaction is committed. The
252** TCL script in pDb->zCommit is executed. If it returns non-zero or
253** if it throws an exception, the transaction is rolled back instead
254** of being committed.
255*/
256static int DbCommitHandler(void *cd){
257 SqliteDb *pDb = (SqliteDb*)cd;
258 int rc;
259
260 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
261 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
262 return 1;
263 }
264 return 0;
265}
266
danielk19777cedc8d2004-06-10 10:50:08 +0000267static void tclCollateNeeded(
268 void *pCtx,
drh9bb575f2004-09-06 17:24:11 +0000269 sqlite3 *db,
danielk19777cedc8d2004-06-10 10:50:08 +0000270 int enc,
271 const char *zName
272){
273 SqliteDb *pDb = (SqliteDb *)pCtx;
274 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
275 Tcl_IncrRefCount(pScript);
276 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
277 Tcl_EvalObjEx(pDb->interp, pScript, 0);
278 Tcl_DecrRefCount(pScript);
279}
280
drhaa940ea2004-01-15 02:44:03 +0000281/*
danielk19770202b292004-06-09 09:55:16 +0000282** This routine is called to evaluate an SQL collation function implemented
283** using TCL script.
284*/
285static int tclSqlCollate(
286 void *pCtx,
287 int nA,
288 const void *zA,
289 int nB,
290 const void *zB
291){
292 SqlCollate *p = (SqlCollate *)pCtx;
293 Tcl_Obj *pCmd;
294
295 pCmd = Tcl_NewStringObj(p->zScript, -1);
296 Tcl_IncrRefCount(pCmd);
297 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
298 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
drhd1e47332005-06-26 17:55:33 +0000299 Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
danielk19770202b292004-06-09 09:55:16 +0000300 Tcl_DecrRefCount(pCmd);
301 return (atoi(Tcl_GetStringResult(p->interp)));
302}
303
304/*
drhcabb0812002-09-14 13:47:32 +0000305** This routine is called to evaluate an SQL function implemented
306** using TCL script.
307*/
drhfb7e7652005-01-24 00:28:42 +0000308static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){
danielk19776f8a5032004-05-10 10:34:51 +0000309 SqlFunc *p = sqlite3_user_data(context);
drhd1e47332005-06-26 17:55:33 +0000310 Tcl_Obj *pCmd;
drhcabb0812002-09-14 13:47:32 +0000311 int i;
312 int rc;
313
drhd1e47332005-06-26 17:55:33 +0000314 if( argc==0 ){
315 /* If there are no arguments to the function, call Tcl_EvalObjEx on the
316 ** script object directly. This allows the TCL compiler to generate
317 ** bytecode for the command on the first invocation and thus make
318 ** subsequent invocations much faster. */
319 pCmd = p->pScript;
320 Tcl_IncrRefCount(pCmd);
321 rc = Tcl_EvalObjEx(p->interp, pCmd, 0);
322 Tcl_DecrRefCount(pCmd);
323 }else{
324 /* If there are arguments to the function, make a shallow copy of the
325 ** script object, lappend the arguments, then evaluate the copy.
326 **
327 ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated.
328 ** The new Tcl_Obj contains pointers to the original list elements.
329 ** That way, when Tcl_EvalObjv() is run and shimmers the first element
330 ** of the list to tclCmdNameType, that alternate representation will
331 ** be preserved and reused on the next invocation.
332 */
333 Tcl_Obj **aArg;
334 int nArg;
335 if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){
336 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
337 return;
338 }
339 pCmd = Tcl_NewListObj(nArg, aArg);
340 Tcl_IncrRefCount(pCmd);
341 for(i=0; i<argc; i++){
342 sqlite3_value *pIn = argv[i];
343 Tcl_Obj *pVal;
344
345 /* Set pVal to contain the i'th column of this row. */
346 switch( sqlite3_value_type(pIn) ){
347 case SQLITE_BLOB: {
348 int bytes = sqlite3_value_bytes(pIn);
349 pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes);
350 break;
351 }
352 case SQLITE_INTEGER: {
353 sqlite_int64 v = sqlite3_value_int64(pIn);
354 if( v>=-2147483647 && v<=2147483647 ){
355 pVal = Tcl_NewIntObj(v);
356 }else{
357 pVal = Tcl_NewWideIntObj(v);
358 }
359 break;
360 }
361 case SQLITE_FLOAT: {
362 double r = sqlite3_value_double(pIn);
363 pVal = Tcl_NewDoubleObj(r);
364 break;
365 }
366 case SQLITE_NULL: {
367 pVal = Tcl_NewStringObj("", 0);
368 break;
369 }
370 default: {
371 int bytes = sqlite3_value_bytes(pIn);
372 pVal = Tcl_NewStringObj(sqlite3_value_text(pIn), bytes);
373 break;
374 }
375 }
376 rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
377 if( rc ){
378 Tcl_DecrRefCount(pCmd);
379 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
380 return;
381 }
danielk197751ad0ec2004-05-24 12:39:02 +0000382 }
drhd1e47332005-06-26 17:55:33 +0000383 if( !p->useEvalObjv ){
384 /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd
385 ** is a list without a string representation. To prevent this from
386 ** happening, make sure pCmd has a valid string representation */
387 Tcl_GetString(pCmd);
388 }
389 rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
390 Tcl_DecrRefCount(pCmd);
drhcabb0812002-09-14 13:47:32 +0000391 }
danielk1977562e8d32005-05-20 09:40:55 +0000392
drhc7f269d2005-05-05 10:30:29 +0000393 if( rc && rc!=TCL_RETURN ){
danielk19777e18c252004-05-25 11:47:24 +0000394 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000395 }else{
drhc7f269d2005-05-05 10:30:29 +0000396 Tcl_Obj *pVar = Tcl_GetObjResult(p->interp);
397 int n;
398 u8 *data;
399 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
400 char c = zType[0];
drhdf0bdda2005-06-25 19:31:48 +0000401 if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){
drhd1e47332005-06-26 17:55:33 +0000402 /* Only return a BLOB type if the Tcl variable is a bytearray and
drhdf0bdda2005-06-25 19:31:48 +0000403 ** has no string representation. */
drhc7f269d2005-05-05 10:30:29 +0000404 data = Tcl_GetByteArrayFromObj(pVar, &n);
405 sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT);
drhc7f269d2005-05-05 10:30:29 +0000406 }else if( (c=='b' && strcmp(zType,"boolean")==0) ||
407 (c=='i' && strcmp(zType,"int")==0) ){
408 Tcl_GetIntFromObj(0, pVar, &n);
409 sqlite3_result_int(context, n);
410 }else if( c=='d' && strcmp(zType,"double")==0 ){
411 double r;
412 Tcl_GetDoubleFromObj(0, pVar, &r);
413 sqlite3_result_double(context, r);
drhdf0bdda2005-06-25 19:31:48 +0000414 }else if( c=='w' && strcmp(zType,"wideInt")==0 ){
415 Tcl_WideInt v;
416 Tcl_GetWideIntFromObj(0, pVar, &v);
417 sqlite3_result_int64(context, v);
drhc7f269d2005-05-05 10:30:29 +0000418 }else{
419 data = Tcl_GetStringFromObj(pVar, &n);
420 sqlite3_result_text(context, data, n, SQLITE_TRANSIENT);
drhc7f269d2005-05-05 10:30:29 +0000421 }
drhcabb0812002-09-14 13:47:32 +0000422 }
423}
drh895d7472004-08-20 16:02:39 +0000424
drhe22a3342003-04-22 20:30:37 +0000425#ifndef SQLITE_OMIT_AUTHORIZATION
426/*
427** This is the authentication function. It appends the authentication
428** type code and the two arguments to zCmd[] then invokes the result
429** on the interpreter. The reply is examined to determine if the
430** authentication fails or succeeds.
431*/
432static int auth_callback(
433 void *pArg,
434 int code,
435 const char *zArg1,
436 const char *zArg2,
437 const char *zArg3,
438 const char *zArg4
439){
440 char *zCode;
441 Tcl_DString str;
442 int rc;
443 const char *zReply;
444 SqliteDb *pDb = (SqliteDb*)pArg;
445
446 switch( code ){
447 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
448 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
449 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
450 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
451 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
452 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
453 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
454 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
455 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
456 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
457 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
458 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
459 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
460 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
461 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
462 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
463 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
464 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
465 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
466 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
467 case SQLITE_READ : zCode="SQLITE_READ"; break;
468 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
469 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
470 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000471 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
472 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
danielk19771c8c23c2004-11-12 15:53:37 +0000473 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break;
danielk19771d54df82004-11-23 15:41:16 +0000474 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break;
drhe6e04962005-07-23 02:17:03 +0000475 case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break;
drhe22a3342003-04-22 20:30:37 +0000476 default : zCode="????"; break;
477 }
478 Tcl_DStringInit(&str);
479 Tcl_DStringAppend(&str, pDb->zAuth, -1);
480 Tcl_DStringAppendElement(&str, zCode);
481 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
482 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
483 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
484 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
485 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
486 Tcl_DStringFree(&str);
487 zReply = Tcl_GetStringResult(pDb->interp);
488 if( strcmp(zReply,"SQLITE_OK")==0 ){
489 rc = SQLITE_OK;
490 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
491 rc = SQLITE_DENY;
492 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
493 rc = SQLITE_IGNORE;
494 }else{
495 rc = 999;
496 }
497 return rc;
498}
499#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000500
501/*
danielk1977ef2cb632004-05-29 02:37:19 +0000502** zText is a pointer to text obtained via an sqlite3_result_text()
503** or similar interface. This routine returns a Tcl string object,
504** reference count set to 0, containing the text. If a translation
505** between iso8859 and UTF-8 is required, it is preformed.
506*/
507static Tcl_Obj *dbTextToObj(char const *zText){
508 Tcl_Obj *pVal;
509#ifdef UTF_TRANSLATION_NEEDED
510 Tcl_DString dCol;
511 Tcl_DStringInit(&dCol);
512 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
513 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
514 Tcl_DStringFree(&dCol);
515#else
516 pVal = Tcl_NewStringObj(zText, -1);
517#endif
518 return pVal;
519}
520
521/*
tpoindex1067fe12004-12-17 15:41:11 +0000522** This routine reads a line of text from FILE in, stores
523** the text in memory obtained from malloc() and returns a pointer
524** to the text. NULL is returned at end of file, or if malloc()
525** fails.
526**
527** The interface is like "readline" but no command-line editing
528** is done.
529**
530** copied from shell.c from '.import' command
531*/
532static char *local_getline(char *zPrompt, FILE *in){
533 char *zLine;
534 int nLine;
535 int n;
536 int eol;
537
538 nLine = 100;
539 zLine = malloc( nLine );
540 if( zLine==0 ) return 0;
541 n = 0;
542 eol = 0;
543 while( !eol ){
544 if( n+100>nLine ){
545 nLine = nLine*2 + 100;
546 zLine = realloc(zLine, nLine);
547 if( zLine==0 ) return 0;
548 }
549 if( fgets(&zLine[n], nLine - n, in)==0 ){
550 if( n==0 ){
551 free(zLine);
552 return 0;
553 }
554 zLine[n] = 0;
555 eol = 1;
556 break;
557 }
558 while( zLine[n] ){ n++; }
559 if( n>0 && zLine[n-1]=='\n' ){
560 n--;
561 zLine[n] = 0;
562 eol = 1;
563 }
564 }
565 zLine = realloc( zLine, n+1 );
566 return zLine;
567}
568
569/*
drh75897232000-05-29 14:26:00 +0000570** The "sqlite" command below creates a new Tcl command for each
571** connection it opens to an SQLite database. This routine is invoked
572** whenever one of those connection-specific commands is executed
573** in Tcl. For example, if you run Tcl code like this:
574**
drh9bb575f2004-09-06 17:24:11 +0000575** sqlite3 db1 "my_database"
drh75897232000-05-29 14:26:00 +0000576** db1 close
577**
578** The first command opens a connection to the "my_database" database
579** and calls that connection "db1". The second command causes this
580** subroutine to be invoked.
581*/
drh6d313162000-09-21 13:01:35 +0000582static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000583 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000584 int choice;
drh22fbcb82004-02-01 01:22:50 +0000585 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000586 static const char *DB_strs[] = {
drhfb7e7652005-01-24 00:28:42 +0000587 "authorizer", "busy", "cache",
588 "changes", "close", "collate",
589 "collation_needed", "commit_hook", "complete",
590 "copy", "errorcode", "eval",
danielk197755c45f22005-04-03 23:54:43 +0000591 "function", "last_insert_rowid", "nullvalue",
592 "onecolumn", "progress", "rekey",
593 "timeout", "total_changes", "trace",
594 "version",
drh0f14e2e2004-06-29 12:39:08 +0000595 0
drh6d313162000-09-21 13:01:35 +0000596 };
drh411995d2002-06-25 19:31:18 +0000597 enum DB_enum {
drhfb7e7652005-01-24 00:28:42 +0000598 DB_AUTHORIZER, DB_BUSY, DB_CACHE,
599 DB_CHANGES, DB_CLOSE, DB_COLLATE,
600 DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE,
601 DB_COPY, DB_ERRORCODE, DB_EVAL,
danielk197755c45f22005-04-03 23:54:43 +0000602 DB_FUNCTION, DB_LAST_INSERT_ROWID,DB_NULLVALUE,
603 DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
604 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
605 DB_VERSION
drh6d313162000-09-21 13:01:35 +0000606 };
tpoindex1067fe12004-12-17 15:41:11 +0000607 /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
drh6d313162000-09-21 13:01:35 +0000608
609 if( objc<2 ){
610 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000611 return TCL_ERROR;
612 }
drh411995d2002-06-25 19:31:18 +0000613 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000614 return TCL_ERROR;
615 }
616
drh411995d2002-06-25 19:31:18 +0000617 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000618
drhe22a3342003-04-22 20:30:37 +0000619 /* $db authorizer ?CALLBACK?
620 **
621 ** Invoke the given callback to authorize each SQL operation as it is
622 ** compiled. 5 arguments are appended to the callback before it is
623 ** invoked:
624 **
625 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
626 ** (2) First descriptive name (depends on authorization type)
627 ** (3) Second descriptive name
628 ** (4) Name of the database (ex: "main", "temp")
629 ** (5) Name of trigger that is doing the access
630 **
631 ** The callback should return on of the following strings: SQLITE_OK,
632 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
633 **
634 ** If this method is invoked with no arguments, the current authorization
635 ** callback string is returned.
636 */
637 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +0000638#ifdef SQLITE_OMIT_AUTHORIZATION
639 Tcl_AppendResult(interp, "authorization not available in this build", 0);
640 return TCL_ERROR;
641#else
drhe22a3342003-04-22 20:30:37 +0000642 if( objc>3 ){
643 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000644 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +0000645 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000646 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000647 Tcl_AppendResult(interp, pDb->zAuth, 0);
648 }
649 }else{
650 char *zAuth;
651 int len;
652 if( pDb->zAuth ){
653 Tcl_Free(pDb->zAuth);
654 }
655 zAuth = Tcl_GetStringFromObj(objv[2], &len);
656 if( zAuth && len>0 ){
657 pDb->zAuth = Tcl_Alloc( len + 1 );
658 strcpy(pDb->zAuth, zAuth);
659 }else{
660 pDb->zAuth = 0;
661 }
drhe22a3342003-04-22 20:30:37 +0000662 if( pDb->zAuth ){
663 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000664 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000665 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000666 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000667 }
drhe22a3342003-04-22 20:30:37 +0000668 }
drh1211de32004-07-26 12:24:22 +0000669#endif
drhe22a3342003-04-22 20:30:37 +0000670 break;
671 }
672
drhbec3f402000-08-04 13:49:02 +0000673 /* $db busy ?CALLBACK?
674 **
675 ** Invoke the given callback if an SQL statement attempts to open
676 ** a locked database file.
677 */
drh6d313162000-09-21 13:01:35 +0000678 case DB_BUSY: {
679 if( objc>3 ){
680 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000681 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000682 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000683 if( pDb->zBusy ){
684 Tcl_AppendResult(interp, pDb->zBusy, 0);
685 }
686 }else{
drh6d313162000-09-21 13:01:35 +0000687 char *zBusy;
688 int len;
drhbec3f402000-08-04 13:49:02 +0000689 if( pDb->zBusy ){
690 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000691 }
drh6d313162000-09-21 13:01:35 +0000692 zBusy = Tcl_GetStringFromObj(objv[2], &len);
693 if( zBusy && len>0 ){
694 pDb->zBusy = Tcl_Alloc( len + 1 );
695 strcpy(pDb->zBusy, zBusy);
696 }else{
697 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000698 }
699 if( pDb->zBusy ){
700 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000701 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000702 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000703 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000704 }
705 }
drh6d313162000-09-21 13:01:35 +0000706 break;
707 }
drhbec3f402000-08-04 13:49:02 +0000708
drhfb7e7652005-01-24 00:28:42 +0000709 /* $db cache flush
710 ** $db cache size n
711 **
712 ** Flush the prepared statement cache, or set the maximum number of
713 ** cached statements.
714 */
715 case DB_CACHE: {
716 char *subCmd;
717 int n;
718
719 if( objc<=2 ){
720 Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?");
721 return TCL_ERROR;
722 }
723 subCmd = Tcl_GetStringFromObj( objv[2], 0 );
724 if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){
725 if( objc!=3 ){
726 Tcl_WrongNumArgs(interp, 2, objv, "flush");
727 return TCL_ERROR;
728 }else{
729 flushStmtCache( pDb );
730 }
731 }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){
732 if( objc!=4 ){
733 Tcl_WrongNumArgs(interp, 2, objv, "size n");
734 return TCL_ERROR;
735 }else{
736 if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){
737 Tcl_AppendResult( interp, "cannot convert \"",
738 Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0);
739 return TCL_ERROR;
740 }else{
741 if( n<0 ){
742 flushStmtCache( pDb );
743 n = 0;
744 }else if( n>MAX_PREPARED_STMTS ){
745 n = MAX_PREPARED_STMTS;
746 }
747 pDb->maxStmt = n;
748 }
749 }
750 }else{
751 Tcl_AppendResult( interp, "bad option \"",
752 Tcl_GetStringFromObj(objv[0],0), "\": must be flush or size", 0);
753 return TCL_ERROR;
754 }
755 break;
756 }
757
danielk1977b28af712004-06-21 06:50:26 +0000758 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000759 **
760 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000761 ** the most recent INSERT, UPDATE or DELETE statement, not including
762 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000763 */
764 case DB_CHANGES: {
765 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000766 if( objc!=2 ){
767 Tcl_WrongNumArgs(interp, 2, objv, "");
768 return TCL_ERROR;
769 }
drhc8d30ac2002-04-12 10:08:59 +0000770 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000771 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000772 break;
773 }
774
drh75897232000-05-29 14:26:00 +0000775 /* $db close
776 **
777 ** Shutdown the database
778 */
drh6d313162000-09-21 13:01:35 +0000779 case DB_CLOSE: {
780 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
781 break;
782 }
drh75897232000-05-29 14:26:00 +0000783
drhaa940ea2004-01-15 02:44:03 +0000784 /* $db commit_hook ?CALLBACK?
785 **
786 ** Invoke the given callback just before committing every SQL transaction.
787 ** If the callback throws an exception or returns non-zero, then the
788 ** transaction is aborted. If CALLBACK is an empty string, the callback
789 ** is disabled.
790 */
791 case DB_COMMIT_HOOK: {
792 if( objc>3 ){
793 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000794 return TCL_ERROR;
drhaa940ea2004-01-15 02:44:03 +0000795 }else if( objc==2 ){
796 if( pDb->zCommit ){
797 Tcl_AppendResult(interp, pDb->zCommit, 0);
798 }
799 }else{
800 char *zCommit;
801 int len;
802 if( pDb->zCommit ){
803 Tcl_Free(pDb->zCommit);
804 }
805 zCommit = Tcl_GetStringFromObj(objv[2], &len);
806 if( zCommit && len>0 ){
807 pDb->zCommit = Tcl_Alloc( len + 1 );
808 strcpy(pDb->zCommit, zCommit);
809 }else{
810 pDb->zCommit = 0;
811 }
812 if( pDb->zCommit ){
813 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000814 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000815 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000816 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000817 }
818 }
819 break;
820 }
821
drh0f14e2e2004-06-29 12:39:08 +0000822 /*
823 ** $db collate NAME SCRIPT
824 **
825 ** Create a new SQL collation function called NAME. Whenever
826 ** that function is called, invoke SCRIPT to evaluate the function.
827 */
828 case DB_COLLATE: {
829 SqlCollate *pCollate;
830 char *zName;
831 char *zScript;
832 int nScript;
833 if( objc!=4 ){
834 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
835 return TCL_ERROR;
836 }
837 zName = Tcl_GetStringFromObj(objv[2], 0);
838 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
839 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
840 if( pCollate==0 ) return TCL_ERROR;
841 pCollate->interp = interp;
842 pCollate->pNext = pDb->pCollate;
843 pCollate->zScript = (char*)&pCollate[1];
844 pDb->pCollate = pCollate;
845 strcpy(pCollate->zScript, zScript);
846 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
847 pCollate, tclSqlCollate) ){
danielk19779636c4e2005-01-25 04:27:54 +0000848 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drh0f14e2e2004-06-29 12:39:08 +0000849 return TCL_ERROR;
850 }
851 break;
852 }
853
854 /*
855 ** $db collation_needed SCRIPT
856 **
857 ** Create a new SQL collation function called NAME. Whenever
858 ** that function is called, invoke SCRIPT to evaluate the function.
859 */
860 case DB_COLLATION_NEEDED: {
861 if( objc!=3 ){
862 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
863 return TCL_ERROR;
864 }
865 if( pDb->pCollateNeeded ){
866 Tcl_DecrRefCount(pDb->pCollateNeeded);
867 }
868 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
869 Tcl_IncrRefCount(pDb->pCollateNeeded);
870 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
871 break;
872 }
873
drh75897232000-05-29 14:26:00 +0000874 /* $db complete SQL
875 **
876 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
877 ** additional lines of input are needed. This is similar to the
878 ** built-in "info complete" command of Tcl.
879 */
drh6d313162000-09-21 13:01:35 +0000880 case DB_COMPLETE: {
drhccae6022005-02-26 17:31:26 +0000881#ifndef SQLITE_OMIT_COMPLETE
drh6d313162000-09-21 13:01:35 +0000882 Tcl_Obj *pResult;
883 int isComplete;
884 if( objc!=3 ){
885 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000886 return TCL_ERROR;
887 }
danielk19776f8a5032004-05-10 10:34:51 +0000888 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000889 pResult = Tcl_GetObjResult(interp);
890 Tcl_SetBooleanObj(pResult, isComplete);
drhccae6022005-02-26 17:31:26 +0000891#endif
drh6d313162000-09-21 13:01:35 +0000892 break;
893 }
drhdcd997e2003-01-31 17:21:49 +0000894
895 /*
896 ** $db errorcode
897 **
898 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000899 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000900 */
901 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000902 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000903 break;
904 }
drh75897232000-05-29 14:26:00 +0000905
906 /*
drh895d7472004-08-20 16:02:39 +0000907 ** $db eval $sql ?array? ?{ ...code... }?
drh1807ce32004-09-07 13:20:35 +0000908 ** $db onecolumn $sql
drh75897232000-05-29 14:26:00 +0000909 **
910 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000911 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000912 ** If "array" and "code" are omitted, then no callback is every invoked.
913 ** If "array" is an empty string, then the values are placed in variables
914 ** that have the same name as the fields extracted by the query.
drh1807ce32004-09-07 13:20:35 +0000915 **
916 ** The onecolumn method is the equivalent of:
917 ** lindex [$db eval $sql] 0
drh75897232000-05-29 14:26:00 +0000918 */
drh1807ce32004-09-07 13:20:35 +0000919 case DB_ONECOLUMN:
drh6d313162000-09-21 13:01:35 +0000920 case DB_EVAL: {
drh9d74b4c2004-08-24 15:23:34 +0000921 char const *zSql; /* Next SQL statement to execute */
922 char const *zLeft; /* What is left after first stmt in zSql */
923 sqlite3_stmt *pStmt; /* Compiled SQL statment */
drh92febd92004-08-20 18:34:20 +0000924 Tcl_Obj *pArray; /* Name of array into which results are written */
925 Tcl_Obj *pScript; /* Script to run for each result set */
drh1d895032004-08-26 00:56:05 +0000926 Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
927 int nParm; /* Number of entries used in apParm[] */
928 Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
drh1807ce32004-09-07 13:20:35 +0000929 Tcl_Obj *pRet; /* Value to be returned */
drhfb7e7652005-01-24 00:28:42 +0000930 SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */
931 int rc2;
danielk1977ef2cb632004-05-29 02:37:19 +0000932
drh1807ce32004-09-07 13:20:35 +0000933 if( choice==DB_ONECOLUMN ){
934 if( objc!=3 ){
935 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
936 return TCL_ERROR;
937 }
938 pRet = 0;
939 }else{
940 if( objc<3 || objc>5 ){
941 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
942 return TCL_ERROR;
943 }
944 pRet = Tcl_NewObj();
945 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000946 }
drh92febd92004-08-20 18:34:20 +0000947 if( objc==3 ){
948 pArray = pScript = 0;
949 }else if( objc==4 ){
950 pArray = 0;
951 pScript = objv[3];
952 }else{
953 pArray = objv[3];
954 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
955 pScript = objv[4];
956 }
danielk197730ccda12004-05-27 12:11:31 +0000957
drh1d895032004-08-26 00:56:05 +0000958 Tcl_IncrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +0000959 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh90b6bb12004-09-13 13:16:31 +0000960 while( rc==TCL_OK && zSql[0] ){
drhfb7e7652005-01-24 00:28:42 +0000961 int i; /* Loop counter */
962 int nVar; /* Number of bind parameters in the pStmt */
963 int nCol; /* Number of columns in the result set */
drh92febd92004-08-20 18:34:20 +0000964 Tcl_Obj **apColName = 0; /* Array of column names */
drhfb7e7652005-01-24 00:28:42 +0000965 int len; /* String length of zSql */
danielk197730ccda12004-05-27 12:11:31 +0000966
drhfb7e7652005-01-24 00:28:42 +0000967 /* Try to find a SQL statement that has already been compiled and
968 ** which matches the next sequence of SQL.
969 */
970 pStmt = 0;
971 pPreStmt = pDb->stmtList;
972 len = strlen(zSql);
973 if( pPreStmt && sqlite3_expired(pPreStmt->pStmt) ){
974 flushStmtCache(pDb);
975 pPreStmt = 0;
danielk197730ccda12004-05-27 12:11:31 +0000976 }
drhfb7e7652005-01-24 00:28:42 +0000977 for(; pPreStmt; pPreStmt=pPreStmt->pNext){
978 int n = pPreStmt->nSql;
979 if( len>=n
980 && memcmp(pPreStmt->zSql, zSql, n)==0
981 && (zSql[n]==0 || zSql[n-1]==';')
982 ){
983 pStmt = pPreStmt->pStmt;
984 zLeft = &zSql[pPreStmt->nSql];
985
986 /* When a prepared statement is found, unlink it from the
987 ** cache list. It will later be added back to the beginning
988 ** of the cache list in order to implement LRU replacement.
989 */
990 if( pPreStmt->pPrev ){
991 pPreStmt->pPrev->pNext = pPreStmt->pNext;
992 }else{
993 pDb->stmtList = pPreStmt->pNext;
994 }
995 if( pPreStmt->pNext ){
996 pPreStmt->pNext->pPrev = pPreStmt->pPrev;
997 }else{
998 pDb->stmtLast = pPreStmt->pPrev;
999 }
1000 pDb->nStmt--;
1001 break;
1002 }
1003 }
1004
1005 /* If no prepared statement was found. Compile the SQL text
1006 */
drh92febd92004-08-20 18:34:20 +00001007 if( pStmt==0 ){
drhfb7e7652005-01-24 00:28:42 +00001008 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
drh92febd92004-08-20 18:34:20 +00001009 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
1010 rc = TCL_ERROR;
1011 break;
danielk197730ccda12004-05-27 12:11:31 +00001012 }
drhfb7e7652005-01-24 00:28:42 +00001013 if( pStmt==0 ){
1014 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
1015 /* A compile-time error in the statement
1016 */
1017 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
1018 rc = TCL_ERROR;
1019 break;
1020 }else{
1021 /* The statement was a no-op. Continue to the next statement
1022 ** in the SQL string.
1023 */
1024 zSql = zLeft;
1025 continue;
1026 }
1027 }
1028 assert( pPreStmt==0 );
danielk197730ccda12004-05-27 12:11:31 +00001029 }
1030
drhfb7e7652005-01-24 00:28:42 +00001031 /* Bind values to parameters that begin with $ or :
1032 */
drh92febd92004-08-20 18:34:20 +00001033 nVar = sqlite3_bind_parameter_count(pStmt);
drh1d895032004-08-26 00:56:05 +00001034 nParm = 0;
1035 if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){
1036 apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0]));
1037 }else{
1038 apParm = aParm;
1039 }
drh92febd92004-08-20 18:34:20 +00001040 for(i=1; i<=nVar; i++){
1041 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
drhc8f90792005-01-12 00:08:24 +00001042 if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':') ){
drh92febd92004-08-20 18:34:20 +00001043 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
1044 if( pVar ){
1045 int n;
1046 u8 *data;
1047 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
1048 char c = zType[0];
drhdf0bdda2005-06-25 19:31:48 +00001049 if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){
1050 /* Only load a BLOB type if the Tcl variable is a bytearray and
1051 ** has no string representation. */
drh92febd92004-08-20 18:34:20 +00001052 data = Tcl_GetByteArrayFromObj(pVar, &n);
1053 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +00001054 Tcl_IncrRefCount(pVar);
1055 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +00001056 }else if( (c=='b' && strcmp(zType,"boolean")==0) ||
1057 (c=='i' && strcmp(zType,"int")==0) ){
1058 Tcl_GetIntFromObj(interp, pVar, &n);
1059 sqlite3_bind_int(pStmt, i, n);
1060 }else if( c=='d' && strcmp(zType,"double")==0 ){
1061 double r;
1062 Tcl_GetDoubleFromObj(interp, pVar, &r);
1063 sqlite3_bind_double(pStmt, i, r);
drhdf0bdda2005-06-25 19:31:48 +00001064 }else if( c=='w' && strcmp(zType,"wideInt")==0 ){
1065 Tcl_WideInt v;
1066 Tcl_GetWideIntFromObj(interp, pVar, &v);
1067 sqlite3_bind_int64(pStmt, i, v);
drh92febd92004-08-20 18:34:20 +00001068 }else{
1069 data = Tcl_GetStringFromObj(pVar, &n);
1070 sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +00001071 Tcl_IncrRefCount(pVar);
1072 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +00001073 }
drhfb7e7652005-01-24 00:28:42 +00001074 }else{
1075 sqlite3_bind_null( pStmt, i );
drh92febd92004-08-20 18:34:20 +00001076 }
1077 }
1078 }
drh92febd92004-08-20 18:34:20 +00001079
1080 /* Compute column names */
1081 nCol = sqlite3_column_count(pStmt);
1082 if( pScript ){
1083 apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
1084 if( apColName==0 ) break;
1085 for(i=0; i<nCol; i++){
1086 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
1087 Tcl_IncrRefCount(apColName[i]);
1088 }
1089 }
1090
1091 /* If results are being stored in an array variable, then create
1092 ** the array(*) entry for that array
1093 */
1094 if( pArray ){
1095 Tcl_Obj *pColList = Tcl_NewObj();
drh3ced14a2005-03-31 18:26:20 +00001096 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1);
drh92febd92004-08-20 18:34:20 +00001097 Tcl_IncrRefCount(pColList);
1098 for(i=0; i<nCol; i++){
1099 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
1100 }
drh3ced14a2005-03-31 18:26:20 +00001101 Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0);
1102 Tcl_DecrRefCount(pColList);
1103 Tcl_DecrRefCount(pStar);
drh92febd92004-08-20 18:34:20 +00001104 }
1105
1106 /* Execute the SQL
1107 */
drh90b6bb12004-09-13 13:16:31 +00001108 while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
drh92febd92004-08-20 18:34:20 +00001109 for(i=0; i<nCol; i++){
danielk197730ccda12004-05-27 12:11:31 +00001110 Tcl_Obj *pVal;
1111
1112 /* Set pVal to contain the i'th column of this row. */
drh92febd92004-08-20 18:34:20 +00001113 switch( sqlite3_column_type(pStmt, i) ){
1114 case SQLITE_BLOB: {
1115 int bytes = sqlite3_column_bytes(pStmt, i);
1116 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
1117 break;
1118 }
1119 case SQLITE_INTEGER: {
1120 sqlite_int64 v = sqlite3_column_int64(pStmt, i);
1121 if( v>=-2147483647 && v<=2147483647 ){
1122 pVal = Tcl_NewIntObj(v);
1123 }else{
1124 pVal = Tcl_NewWideIntObj(v);
1125 }
1126 break;
1127 }
1128 case SQLITE_FLOAT: {
1129 double r = sqlite3_column_double(pStmt, i);
1130 pVal = Tcl_NewDoubleObj(r);
1131 break;
1132 }
danielk197755c45f22005-04-03 23:54:43 +00001133 case SQLITE_NULL: {
1134 pVal = dbTextToObj(pDb->zNull);
1135 break;
1136 }
drh92febd92004-08-20 18:34:20 +00001137 default: {
1138 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
1139 break;
1140 }
danielk197730ccda12004-05-27 12:11:31 +00001141 }
1142
drh92febd92004-08-20 18:34:20 +00001143 if( pScript ){
1144 if( pArray==0 ){
1145 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +00001146 }else{
drh92febd92004-08-20 18:34:20 +00001147 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +00001148 }
drh1807ce32004-09-07 13:20:35 +00001149 }else if( choice==DB_ONECOLUMN ){
1150 if( pRet==0 ){
1151 pRet = pVal;
1152 Tcl_IncrRefCount(pRet);
1153 }
drh90b6bb12004-09-13 13:16:31 +00001154 rc = TCL_BREAK;
danielk197730ccda12004-05-27 12:11:31 +00001155 }else{
danielk197730ccda12004-05-27 12:11:31 +00001156 Tcl_ListObjAppendElement(interp, pRet, pVal);
1157 }
1158 }
1159
drh92febd92004-08-20 18:34:20 +00001160 if( pScript ){
1161 rc = Tcl_EvalObjEx(interp, pScript, 0);
drh90b6bb12004-09-13 13:16:31 +00001162 if( rc==TCL_CONTINUE ){
1163 rc = TCL_OK;
1164 }
danielk197730ccda12004-05-27 12:11:31 +00001165 }
1166 }
drh90b6bb12004-09-13 13:16:31 +00001167 if( rc==TCL_BREAK ){
1168 rc = TCL_OK;
1169 }
drh92febd92004-08-20 18:34:20 +00001170
1171 /* Free the column name objects */
1172 if( pScript ){
1173 for(i=0; i<nCol; i++){
1174 Tcl_DecrRefCount(apColName[i]);
1175 }
1176 Tcl_Free((char*)apColName);
1177 }
1178
drh1d895032004-08-26 00:56:05 +00001179 /* Free the bound string and blob parameters */
1180 for(i=0; i<nParm; i++){
1181 Tcl_DecrRefCount(apParm[i]);
1182 }
1183 if( apParm!=aParm ){
1184 Tcl_Free((char*)apParm);
1185 }
1186
drhfb7e7652005-01-24 00:28:42 +00001187 /* Reset the statement. If the result code is SQLITE_SCHEMA, then
1188 ** flush the statement cache and try the statement again.
drh92febd92004-08-20 18:34:20 +00001189 */
drhfb7e7652005-01-24 00:28:42 +00001190 rc2 = sqlite3_reset(pStmt);
1191 if( SQLITE_SCHEMA==rc2 ){
1192 /* After a schema change, flush the cache and try to run the
1193 ** statement again
1194 */
1195 flushStmtCache( pDb );
1196 sqlite3_finalize(pStmt);
1197 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
danielk197730ccda12004-05-27 12:11:31 +00001198 continue;
drhfb7e7652005-01-24 00:28:42 +00001199 }else if( SQLITE_OK!=rc2 ){
1200 /* If a run-time error occurs, report the error and stop reading
1201 ** the SQL
1202 */
danielk1977ef2cb632004-05-29 02:37:19 +00001203 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
drhfb7e7652005-01-24 00:28:42 +00001204 sqlite3_finalize(pStmt);
danielk197730ccda12004-05-27 12:11:31 +00001205 rc = TCL_ERROR;
drhfb7e7652005-01-24 00:28:42 +00001206 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
danielk197730ccda12004-05-27 12:11:31 +00001207 break;
drhfb7e7652005-01-24 00:28:42 +00001208 }else if( pDb->maxStmt<=0 ){
1209 /* If the cache is turned off, deallocated the statement */
1210 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
1211 sqlite3_finalize(pStmt);
1212 }else{
1213 /* Everything worked and the cache is operational.
1214 ** Create a new SqlPreparedStmt structure if we need one.
1215 ** (If we already have one we can just reuse it.)
1216 */
1217 if( pPreStmt==0 ){
1218 len = zLeft - zSql;
1219 pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) + len );
1220 if( pPreStmt==0 ) return TCL_ERROR;
1221 pPreStmt->pStmt = pStmt;
1222 pPreStmt->nSql = len;
1223 memcpy(pPreStmt->zSql, zSql, len);
1224 pPreStmt->zSql[len] = 0;
1225 }
1226
1227 /* Add the prepared statement to the beginning of the cache list
1228 */
1229 pPreStmt->pNext = pDb->stmtList;
1230 pPreStmt->pPrev = 0;
1231 if( pDb->stmtList ){
1232 pDb->stmtList->pPrev = pPreStmt;
1233 }
1234 pDb->stmtList = pPreStmt;
1235 if( pDb->stmtLast==0 ){
1236 assert( pDb->nStmt==0 );
1237 pDb->stmtLast = pPreStmt;
1238 }else{
1239 assert( pDb->nStmt>0 );
1240 }
1241 pDb->nStmt++;
1242
1243 /* If we have too many statement in cache, remove the surplus from the
1244 ** end of the cache list.
1245 */
1246 while( pDb->nStmt>pDb->maxStmt ){
1247 sqlite3_finalize(pDb->stmtLast->pStmt);
1248 pDb->stmtLast = pDb->stmtLast->pPrev;
1249 Tcl_Free((char*)pDb->stmtLast->pNext);
1250 pDb->stmtLast->pNext = 0;
1251 pDb->nStmt--;
1252 }
danielk197730ccda12004-05-27 12:11:31 +00001253 }
1254
drhfb7e7652005-01-24 00:28:42 +00001255 /* Proceed to the next statement */
danielk197730ccda12004-05-27 12:11:31 +00001256 zSql = zLeft;
1257 }
drh1d895032004-08-26 00:56:05 +00001258 Tcl_DecrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +00001259
drh1807ce32004-09-07 13:20:35 +00001260 if( pRet ){
1261 if( rc==TCL_OK ){
1262 Tcl_SetObjResult(interp, pRet);
1263 }
1264 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +00001265 }
danielk197730ccda12004-05-27 12:11:31 +00001266 break;
1267 }
drhbec3f402000-08-04 13:49:02 +00001268
1269 /*
drhcabb0812002-09-14 13:47:32 +00001270 ** $db function NAME SCRIPT
1271 **
1272 ** Create a new SQL function called NAME. Whenever that function is
1273 ** called, invoke SCRIPT to evaluate the function.
1274 */
1275 case DB_FUNCTION: {
1276 SqlFunc *pFunc;
drhd1e47332005-06-26 17:55:33 +00001277 Tcl_Obj *pScript;
drhcabb0812002-09-14 13:47:32 +00001278 char *zName;
drhcabb0812002-09-14 13:47:32 +00001279 if( objc!=4 ){
1280 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
1281 return TCL_ERROR;
1282 }
1283 zName = Tcl_GetStringFromObj(objv[2], 0);
drhd1e47332005-06-26 17:55:33 +00001284 pScript = objv[3];
1285 pFunc = findSqlFunc(pDb, zName);
drhcabb0812002-09-14 13:47:32 +00001286 if( pFunc==0 ) return TCL_ERROR;
drhd1e47332005-06-26 17:55:33 +00001287 if( pFunc->pScript ){
1288 Tcl_DecrRefCount(pFunc->pScript);
1289 }
1290 pFunc->pScript = pScript;
1291 Tcl_IncrRefCount(pScript);
1292 pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript);
danielk1977c8c11582004-06-29 13:41:21 +00001293 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +00001294 pFunc, tclSqlFunc, 0, 0);
drhfb7e7652005-01-24 00:28:42 +00001295 if( rc!=SQLITE_OK ){
danielk19779636c4e2005-01-25 04:27:54 +00001296 rc = TCL_ERROR;
1297 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drhfb7e7652005-01-24 00:28:42 +00001298 }else{
1299 /* Must flush any cached statements */
1300 flushStmtCache( pDb );
1301 }
drhcabb0812002-09-14 13:47:32 +00001302 break;
1303 }
1304
1305 /*
drhaf9ff332002-01-16 21:00:27 +00001306 ** $db last_insert_rowid
1307 **
1308 ** Return an integer which is the ROWID for the most recent insert.
1309 */
1310 case DB_LAST_INSERT_ROWID: {
1311 Tcl_Obj *pResult;
1312 int rowid;
1313 if( objc!=2 ){
1314 Tcl_WrongNumArgs(interp, 2, objv, "");
1315 return TCL_ERROR;
1316 }
danielk19776f8a5032004-05-10 10:34:51 +00001317 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +00001318 pResult = Tcl_GetObjResult(interp);
1319 Tcl_SetIntObj(pResult, rowid);
1320 break;
1321 }
1322
1323 /*
drh1807ce32004-09-07 13:20:35 +00001324 ** The DB_ONECOLUMN method is implemented together with DB_EVAL.
drh5d9d7572003-08-19 14:31:01 +00001325 */
drh1807ce32004-09-07 13:20:35 +00001326
1327 /* $db progress ?N CALLBACK?
1328 **
1329 ** Invoke the given callback every N virtual machine opcodes while executing
1330 ** queries.
1331 */
1332 case DB_PROGRESS: {
1333 if( objc==2 ){
1334 if( pDb->zProgress ){
1335 Tcl_AppendResult(interp, pDb->zProgress, 0);
1336 }
1337 }else if( objc==4 ){
1338 char *zProgress;
1339 int len;
1340 int N;
1341 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
1342 return TCL_ERROR;
1343 };
1344 if( pDb->zProgress ){
1345 Tcl_Free(pDb->zProgress);
1346 }
1347 zProgress = Tcl_GetStringFromObj(objv[3], &len);
1348 if( zProgress && len>0 ){
1349 pDb->zProgress = Tcl_Alloc( len + 1 );
1350 strcpy(pDb->zProgress, zProgress);
1351 }else{
1352 pDb->zProgress = 0;
1353 }
1354#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1355 if( pDb->zProgress ){
1356 pDb->interp = interp;
1357 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
1358 }else{
1359 sqlite3_progress_handler(pDb->db, 0, 0, 0);
1360 }
1361#endif
1362 }else{
1363 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
drh5d9d7572003-08-19 14:31:01 +00001364 return TCL_ERROR;
1365 }
drh5d9d7572003-08-19 14:31:01 +00001366 break;
1367 }
1368
1369 /*
drh22fbcb82004-02-01 01:22:50 +00001370 ** $db rekey KEY
1371 **
1372 ** Change the encryption key on the currently open database.
1373 */
1374 case DB_REKEY: {
1375 int nKey;
1376 void *pKey;
1377 if( objc!=3 ){
1378 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
1379 return TCL_ERROR;
1380 }
1381 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +00001382#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +00001383 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +00001384 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +00001385 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +00001386 rc = TCL_ERROR;
1387 }
1388#endif
1389 break;
1390 }
1391
1392 /*
drhbec3f402000-08-04 13:49:02 +00001393 ** $db timeout MILLESECONDS
1394 **
1395 ** Delay for the number of milliseconds specified when a file is locked.
1396 */
drh6d313162000-09-21 13:01:35 +00001397 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +00001398 int ms;
drh6d313162000-09-21 13:01:35 +00001399 if( objc!=3 ){
1400 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +00001401 return TCL_ERROR;
1402 }
drh6d313162000-09-21 13:01:35 +00001403 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001404 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +00001405 break;
drh75897232000-05-29 14:26:00 +00001406 }
drhb5a20d32003-04-23 12:25:23 +00001407
drh0f14e2e2004-06-29 12:39:08 +00001408 /*
danielk197755c45f22005-04-03 23:54:43 +00001409 ** $db nullvalue ?STRING?
1410 **
1411 ** Change text used when a NULL comes back from the database. If ?STRING?
1412 ** is not present, then the current string used for NULL is returned.
1413 ** If STRING is present, then STRING is returned.
1414 **
1415 */
1416 case DB_NULLVALUE: {
1417 if( objc!=2 && objc!=3 ){
1418 Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
1419 return TCL_ERROR;
1420 }
1421 if( objc==3 ){
1422 int len;
1423 char *zNull = Tcl_GetStringFromObj(objv[2], &len);
1424 if( pDb->zNull ){
1425 Tcl_Free(pDb->zNull);
1426 }
1427 if( zNull && len>0 ){
1428 pDb->zNull = Tcl_Alloc( len + 1 );
1429 strncpy(pDb->zNull, zNull, len);
1430 pDb->zNull[len] = '\0';
1431 }else{
1432 pDb->zNull = 0;
1433 }
1434 }
1435 Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull));
1436 break;
1437 }
1438
1439 /*
drh0f14e2e2004-06-29 12:39:08 +00001440 ** $db total_changes
1441 **
1442 ** Return the number of rows that were modified, inserted, or deleted
1443 ** since the database handle was created.
1444 */
1445 case DB_TOTAL_CHANGES: {
1446 Tcl_Obj *pResult;
1447 if( objc!=2 ){
1448 Tcl_WrongNumArgs(interp, 2, objv, "");
1449 return TCL_ERROR;
1450 }
1451 pResult = Tcl_GetObjResult(interp);
1452 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
1453 break;
1454 }
1455
drhb5a20d32003-04-23 12:25:23 +00001456 /* $db trace ?CALLBACK?
1457 **
1458 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
1459 ** that is executed. The text of the SQL is appended to CALLBACK before
1460 ** it is executed.
1461 */
1462 case DB_TRACE: {
1463 if( objc>3 ){
1464 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +00001465 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +00001466 }else if( objc==2 ){
1467 if( pDb->zTrace ){
1468 Tcl_AppendResult(interp, pDb->zTrace, 0);
1469 }
1470 }else{
1471 char *zTrace;
1472 int len;
1473 if( pDb->zTrace ){
1474 Tcl_Free(pDb->zTrace);
1475 }
1476 zTrace = Tcl_GetStringFromObj(objv[2], &len);
1477 if( zTrace && len>0 ){
1478 pDb->zTrace = Tcl_Alloc( len + 1 );
1479 strcpy(pDb->zTrace, zTrace);
1480 }else{
1481 pDb->zTrace = 0;
1482 }
1483 if( pDb->zTrace ){
1484 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001485 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00001486 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001487 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00001488 }
1489 }
1490 break;
1491 }
1492
tpoindex1067fe12004-12-17 15:41:11 +00001493 /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
1494 **
1495 ** Copy data into table from filename, optionally using SEPARATOR
1496 ** as column separators. If a column contains a null string, or the
1497 ** value of NULLINDICATOR, a NULL is inserted for the column.
1498 ** conflict-algorithm is one of the sqlite conflict algorithms:
1499 ** rollback, abort, fail, ignore, replace
1500 ** On success, return the number of lines processed, not necessarily same
1501 ** as 'db changes' due to conflict-algorithm selected.
1502 **
1503 ** This code is basically an implementation/enhancement of
1504 ** the sqlite3 shell.c ".import" command.
1505 **
1506 ** This command usage is equivalent to the sqlite2.x COPY statement,
1507 ** which imports file data into a table using the PostgreSQL COPY file format:
1508 ** $db copy $conflit_algo $table_name $filename \t \\N
1509 */
1510 case DB_COPY: {
tpoindex1067fe12004-12-17 15:41:11 +00001511 char *zTable; /* Insert data into this table */
1512 char *zFile; /* The file from which to extract data */
1513 char *zConflict; /* The conflict algorithm to use */
1514 sqlite3_stmt *pStmt; /* A statement */
1515 int rc; /* Result code */
1516 int nCol; /* Number of columns in the table */
1517 int nByte; /* Number of bytes in an SQL string */
1518 int i, j; /* Loop counters */
1519 int nSep; /* Number of bytes in zSep[] */
1520 int nNull; /* Number of bytes in zNull[] */
1521 char *zSql; /* An SQL statement */
1522 char *zLine; /* A single line of input from the file */
1523 char **azCol; /* zLine[] broken up into columns */
1524 char *zCommit; /* How to commit changes */
1525 FILE *in; /* The input file */
1526 int lineno = 0; /* Line number of input file */
1527 char zLineNum[80]; /* Line number print buffer */
1528 Tcl_Obj *pResult; /* interp result */
1529
drh9ee3cdc2004-12-17 20:48:06 +00001530 char *zSep;
1531 char *zNull;
1532 if( objc<5 || objc>7 ){
1533 Tcl_WrongNumArgs(interp, 2, objv,
1534 "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
1535 return TCL_ERROR;
1536 }
1537 if( objc>=6 ){
1538 zSep = Tcl_GetStringFromObj(objv[5], 0);
1539 }else{
1540 zSep = "\t";
1541 }
1542 if( objc>=7 ){
1543 zNull = Tcl_GetStringFromObj(objv[6], 0);
1544 }else{
1545 zNull = "";
1546 }
tpoindex1067fe12004-12-17 15:41:11 +00001547 zConflict = Tcl_GetStringFromObj(objv[2], 0);
1548 zTable = Tcl_GetStringFromObj(objv[3], 0);
1549 zFile = Tcl_GetStringFromObj(objv[4], 0);
1550 nSep = strlen(zSep);
1551 nNull = strlen(zNull);
1552 if( nSep==0 ){
1553 Tcl_AppendResult(interp, "Error: non-null separator required for copy", 0);
1554 return TCL_ERROR;
1555 }
1556 if(sqlite3StrICmp(zConflict, "rollback") != 0 &&
1557 sqlite3StrICmp(zConflict, "abort" ) != 0 &&
1558 sqlite3StrICmp(zConflict, "fail" ) != 0 &&
1559 sqlite3StrICmp(zConflict, "ignore" ) != 0 &&
1560 sqlite3StrICmp(zConflict, "replace" ) != 0 ) {
drh9ee3cdc2004-12-17 20:48:06 +00001561 Tcl_AppendResult(interp, "Error: \"", zConflict,
1562 "\", conflict-algorithm must be one of: rollback, "
1563 "abort, fail, ignore, or replace", 0);
tpoindex1067fe12004-12-17 15:41:11 +00001564 return TCL_ERROR;
1565 }
1566 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1567 if( zSql==0 ){
1568 Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
1569 return TCL_ERROR;
1570 }
1571 nByte = strlen(zSql);
1572 rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
1573 sqlite3_free(zSql);
1574 if( rc ){
1575 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1576 nCol = 0;
1577 }else{
1578 nCol = sqlite3_column_count(pStmt);
1579 }
1580 sqlite3_finalize(pStmt);
1581 if( nCol==0 ) {
1582 return TCL_ERROR;
1583 }
1584 zSql = malloc( nByte + 50 + nCol*2 );
1585 if( zSql==0 ) {
1586 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
1587 return TCL_ERROR;
1588 }
drh9ee3cdc2004-12-17 20:48:06 +00001589 sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
1590 zConflict, zTable);
tpoindex1067fe12004-12-17 15:41:11 +00001591 j = strlen(zSql);
1592 for(i=1; i<nCol; i++){
1593 zSql[j++] = ',';
1594 zSql[j++] = '?';
1595 }
1596 zSql[j++] = ')';
1597 zSql[j] = 0;
1598 rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
1599 free(zSql);
1600 if( rc ){
1601 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1602 sqlite3_finalize(pStmt);
1603 return TCL_ERROR;
1604 }
1605 in = fopen(zFile, "rb");
1606 if( in==0 ){
1607 Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL);
1608 sqlite3_finalize(pStmt);
1609 return TCL_ERROR;
1610 }
1611 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1612 if( azCol==0 ) {
1613 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
1614 return TCL_ERROR;
1615 }
1616 sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
1617 zCommit = "COMMIT";
1618 while( (zLine = local_getline(0, in))!=0 ){
1619 char *z;
1620 i = 0;
1621 lineno++;
1622 azCol[0] = zLine;
1623 for(i=0, z=zLine; *z; z++){
1624 if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
1625 *z = 0;
1626 i++;
1627 if( i<nCol ){
1628 azCol[i] = &z[nSep];
1629 z += nSep-1;
1630 }
1631 }
1632 }
1633 if( i+1!=nCol ){
1634 char *zErr;
1635 zErr = malloc(200 + strlen(zFile));
1636 sprintf(zErr,"Error: %s line %d: expected %d columns of data but found %d",
1637 zFile, lineno, nCol, i+1);
1638 Tcl_AppendResult(interp, zErr, 0);
1639 free(zErr);
1640 zCommit = "ROLLBACK";
1641 break;
1642 }
1643 for(i=0; i<nCol; i++){
1644 /* check for null data, if so, bind as null */
1645 if ((nNull>0 && strcmp(azCol[i], zNull)==0) || strlen(azCol[i])==0) {
1646 sqlite3_bind_null(pStmt, i+1);
1647 }else{
1648 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1649 }
1650 }
1651 sqlite3_step(pStmt);
1652 rc = sqlite3_reset(pStmt);
1653 free(zLine);
1654 if( rc!=SQLITE_OK ){
1655 Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0);
1656 zCommit = "ROLLBACK";
1657 break;
1658 }
1659 }
1660 free(azCol);
1661 fclose(in);
1662 sqlite3_finalize(pStmt);
1663 sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
1664
1665 if( zCommit[0] == 'C' ){
1666 /* success, set result as number of lines processed */
1667 pResult = Tcl_GetObjResult(interp);
1668 Tcl_SetIntObj(pResult, lineno);
1669 rc = TCL_OK;
1670 }else{
1671 /* failure, append lineno where failed */
1672 sprintf(zLineNum,"%d",lineno);
1673 Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0);
1674 rc = TCL_ERROR;
1675 }
1676 break;
1677 }
1678
danielk19774397de52005-01-12 12:44:03 +00001679 /* $db version
1680 **
1681 ** Return the version string for this database.
1682 */
1683 case DB_VERSION: {
1684 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC);
1685 break;
1686 }
1687
tpoindex1067fe12004-12-17 15:41:11 +00001688
drh6d313162000-09-21 13:01:35 +00001689 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00001690 return rc;
drh75897232000-05-29 14:26:00 +00001691}
1692
1693/*
drh9bb575f2004-09-06 17:24:11 +00001694** sqlite3 DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +00001695**
1696** This is the main Tcl command. When the "sqlite" Tcl command is
1697** invoked, this routine runs to process that command.
1698**
1699** The first argument, DBNAME, is an arbitrary name for a new
1700** database connection. This command creates a new command named
1701** DBNAME that is used to control that connection. The database
1702** connection is deleted when the DBNAME command is deleted.
1703**
1704** The second argument is the name of the directory that contains
1705** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +00001706**
1707** For testing purposes, we also support the following:
1708**
drh9bb575f2004-09-06 17:24:11 +00001709** sqlite3 -encoding
drhfbc3eab2001-04-06 16:13:42 +00001710**
1711** Return the encoding used by LIKE and GLOB operators. Choices
1712** are UTF-8 and iso8859.
1713**
drh9bb575f2004-09-06 17:24:11 +00001714** sqlite3 -version
drh647cb0e2002-11-04 19:32:25 +00001715**
1716** Return the version number of the SQLite library.
1717**
drh9bb575f2004-09-06 17:24:11 +00001718** sqlite3 -tcl-uses-utf
drhfbc3eab2001-04-06 16:13:42 +00001719**
1720** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1721** not. Used by tests to make sure the library was compiled
1722** correctly.
drh75897232000-05-29 14:26:00 +00001723*/
drh22fbcb82004-02-01 01:22:50 +00001724static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00001725 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001726 void *pKey = 0;
1727 int nKey = 0;
1728 const char *zArg;
drh75897232000-05-29 14:26:00 +00001729 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001730 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001731 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001732 if( objc==2 ){
1733 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001734 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001735 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001736 return TCL_OK;
1737 }
drh9eb9e262004-02-11 02:18:05 +00001738 if( strcmp(zArg,"-has-codec")==0 ){
1739#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001740 Tcl_AppendResult(interp,"1",0);
1741#else
1742 Tcl_AppendResult(interp,"0",0);
1743#endif
1744 return TCL_OK;
1745 }
1746 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001747#ifdef TCL_UTF_MAX
1748 Tcl_AppendResult(interp,"1",0);
1749#else
1750 Tcl_AppendResult(interp,"0",0);
1751#endif
1752 return TCL_OK;
1753 }
1754 }
drh22fbcb82004-02-01 01:22:50 +00001755 if( objc==5 || objc==6 ){
1756 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1757 if( strcmp(zArg,"-key")==0 ){
1758 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1759 objc -= 2;
1760 }
1761 }
1762 if( objc!=3 && objc!=4 ){
1763 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001764#ifdef SQLITE_HAS_CODEC
1765 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001766#else
1767 "HANDLE FILENAME ?MODE?"
1768#endif
1769 );
drh75897232000-05-29 14:26:00 +00001770 return TCL_ERROR;
1771 }
drh75897232000-05-29 14:26:00 +00001772 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001773 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001774 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001775 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1776 return TCL_ERROR;
1777 }
1778 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001779 zFile = Tcl_GetStringFromObj(objv[2], 0);
danielk19774f057f92004-06-08 00:02:33 +00001780 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001781 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1782 zErrMsg = strdup(sqlite3_errmsg(p->db));
1783 sqlite3_close(p->db);
1784 p->db = 0;
1785 }
drh2011d5f2004-07-22 02:40:37 +00001786#ifdef SQLITE_HAS_CODEC
1787 sqlite3_key(p->db, pKey, nKey);
drheb8ed702004-02-11 10:37:23 +00001788#endif
drhbec3f402000-08-04 13:49:02 +00001789 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001790 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001791 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001792 free(zErrMsg);
1793 return TCL_ERROR;
1794 }
drhfb7e7652005-01-24 00:28:42 +00001795 p->maxStmt = NUM_PREPARED_STMTS;
drh22fbcb82004-02-01 01:22:50 +00001796 zArg = Tcl_GetStringFromObj(objv[1], 0);
1797 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001798
drh06b27182002-06-26 20:06:05 +00001799 /* The return value is the value of the sqlite* pointer
1800 */
1801 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001802 if( strncmp(zBuf,"0x",2) ){
1803 sprintf(zBuf, "0x%p", p->db);
1804 }
drh06b27182002-06-26 20:06:05 +00001805 Tcl_AppendResult(interp, zBuf, 0);
1806
drhc22bd472002-05-10 13:14:07 +00001807 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001808 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001809 */
drh28b4e482002-03-11 02:06:13 +00001810#ifdef SQLITE_TEST
1811 {
drh9bb575f2004-09-06 17:24:11 +00001812 extern void Md5_Register(sqlite3*);
drh3b93bc82005-01-13 23:54:32 +00001813#ifdef SQLITE_MEMDEBUG
danielk197713073932004-06-30 11:54:06 +00001814 int mallocfail = sqlite3_iMallocFail;
1815 sqlite3_iMallocFail = 0;
danielk19775b59af82004-06-30 12:42:59 +00001816#endif
drhc22bd472002-05-10 13:14:07 +00001817 Md5_Register(p->db);
drh3b93bc82005-01-13 23:54:32 +00001818#ifdef SQLITE_MEMDEBUG
danielk197713073932004-06-30 11:54:06 +00001819 sqlite3_iMallocFail = mallocfail;
danielk19775b59af82004-06-30 12:42:59 +00001820#endif
drh06b27182002-06-26 20:06:05 +00001821 }
drh28b4e482002-03-11 02:06:13 +00001822#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001823 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001824 return TCL_OK;
1825}
1826
1827/*
drh90ca9752001-09-28 17:47:14 +00001828** Provide a dummy Tcl_InitStubs if we are using this as a static
1829** library.
1830*/
1831#ifndef USE_TCL_STUBS
1832# undef Tcl_InitStubs
1833# define Tcl_InitStubs(a,b,c)
1834#endif
1835
1836/*
drh75897232000-05-29 14:26:00 +00001837** Initialize this module.
1838**
1839** This Tcl module contains only a single new Tcl command named "sqlite".
1840** (Hence there is no namespace. There is no point in using a namespace
1841** if the extension only supplies one new name!) The "sqlite" command is
1842** used to open a new SQLite database. See the DbMain() routine above
1843** for additional information.
1844*/
drh38f82712004-06-18 17:10:16 +00001845int Sqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001846 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001847 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1848 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh49766d62005-01-08 18:42:28 +00001849 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1850 Tcl_PkgProvide(interp, "sqlite", "3.0");
drh90ca9752001-09-28 17:47:14 +00001851 return TCL_OK;
1852}
drh49766d62005-01-08 18:42:28 +00001853int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
1854int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1855int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1856
1857#ifndef SQLITE_3_SUFFIX_ONLY
1858int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
1859int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
1860int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1861int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1862#endif
drh75897232000-05-29 14:26:00 +00001863
drh3e27c022004-07-23 00:01:38 +00001864#ifdef TCLSH
1865/*****************************************************************************
1866** The code that follows is used to build standalone TCL interpreters
drh75897232000-05-29 14:26:00 +00001867*/
drh348784e2000-05-29 20:41:49 +00001868
1869/*
drh3e27c022004-07-23 00:01:38 +00001870** If the macro TCLSH is one, then put in code this for the
1871** "main" routine that will initialize Tcl and take input from
1872** standard input.
drh348784e2000-05-29 20:41:49 +00001873*/
drh3e27c022004-07-23 00:01:38 +00001874#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00001875static char zMainloop[] =
1876 "set line {}\n"
1877 "while {![eof stdin]} {\n"
1878 "if {$line!=\"\"} {\n"
1879 "puts -nonewline \"> \"\n"
1880 "} else {\n"
1881 "puts -nonewline \"% \"\n"
1882 "}\n"
1883 "flush stdout\n"
1884 "append line [gets stdin]\n"
1885 "if {[info complete $line]} {\n"
1886 "if {[catch {uplevel #0 $line} result]} {\n"
1887 "puts stderr \"Error: $result\"\n"
1888 "} elseif {$result!=\"\"} {\n"
1889 "puts $result\n"
1890 "}\n"
1891 "set line {}\n"
1892 "} else {\n"
1893 "append line \\n\n"
1894 "}\n"
1895 "}\n"
1896;
drh3e27c022004-07-23 00:01:38 +00001897#endif
1898
1899/*
1900** If the macro TCLSH is two, then get the main loop code out of
1901** the separate file "spaceanal_tcl.h".
1902*/
1903#if TCLSH==2
1904static char zMainloop[] =
1905#include "spaceanal_tcl.h"
1906;
1907#endif
drh348784e2000-05-29 20:41:49 +00001908
1909#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1910int TCLSH_MAIN(int argc, char **argv){
1911 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001912 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001913 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001914 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001915#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001916 {
1917 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001918 extern int Sqlitetest2_Init(Tcl_Interp*);
1919 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001920 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001921 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001922 extern int Md5_Init(Tcl_Interp*);
drh2e66f0b2005-04-28 17:18:48 +00001923 extern int Sqlitetestsse_Init(Tcl_Interp*);
1924
danielk19776490beb2004-05-11 06:17:21 +00001925 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001926 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001927 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001928 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001929 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001930 Md5_Init(interp);
drh89dec812005-04-28 19:03:37 +00001931#ifdef SQLITE_SSE
drh2e66f0b2005-04-28 17:18:48 +00001932 Sqlitetestsse_Init(interp);
1933#endif
drhd1bf3512001-04-07 15:24:33 +00001934 }
1935#endif
drh3e27c022004-07-23 00:01:38 +00001936 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001937 int i;
1938 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1939 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
drh61212b62004-12-02 20:17:00 +00001940 for(i=3-TCLSH; i<argc; i++){
drh348784e2000-05-29 20:41:49 +00001941 Tcl_SetVar(interp, "argv", argv[i],
1942 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1943 }
drh3e27c022004-07-23 00:01:38 +00001944 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001945 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001946 if( zInfo==0 ) zInfo = interp->result;
1947 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001948 return 1;
1949 }
drh3e27c022004-07-23 00:01:38 +00001950 }
1951 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001952 Tcl_GlobalEval(interp, zMainloop);
1953 }
1954 return 0;
1955}
1956#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001957
1958#endif /* !defined(NO_TCL) */