blob: 3f76bdae6ddf0fdb9b5c767d3f389f04a319b596 [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**
drh89dec812005-04-28 19:03:37 +000014** $Id: tclsqlite.c,v 1.123 2005/04/28 19:03:37 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>
drh75897232000-05-29 14:26:00 +000024
danielk1977a21c6b62005-01-24 10:25:59 +000025#define NUM_PREPARED_STMTS 10
drhfb7e7652005-01-24 00:28:42 +000026#define MAX_PREPARED_STMTS 100
27
drh75897232000-05-29 14:26:00 +000028/*
drh98808ba2001-10-18 12:34:46 +000029** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
30** have to do a translation when going between the two. Set the
31** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
32** this translation.
33*/
34#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
35# define UTF_TRANSLATION_NEEDED 1
36#endif
37
38/*
drhcabb0812002-09-14 13:47:32 +000039** New SQL functions can be created as TCL scripts. Each such function
40** is described by an instance of the following structure.
41*/
42typedef struct SqlFunc SqlFunc;
43struct SqlFunc {
44 Tcl_Interp *interp; /* The TCL interpret to execute the function */
45 char *zScript; /* The script to be run */
46 SqlFunc *pNext; /* Next function on the list of them all */
47};
48
49/*
danielk19770202b292004-06-09 09:55:16 +000050** New collation sequences function can be created as TCL scripts. Each such
51** function is described by an instance of the following structure.
52*/
53typedef struct SqlCollate SqlCollate;
54struct SqlCollate {
55 Tcl_Interp *interp; /* The TCL interpret to execute the function */
56 char *zScript; /* The script to be run */
57 SqlCollate *pNext; /* Next function on the list of them all */
58};
59
60/*
drhfb7e7652005-01-24 00:28:42 +000061** Prepared statements are cached for faster execution. Each prepared
62** statement is described by an instance of the following structure.
63*/
64typedef struct SqlPreparedStmt SqlPreparedStmt;
65struct SqlPreparedStmt {
66 SqlPreparedStmt *pNext; /* Next in linked list */
67 SqlPreparedStmt *pPrev; /* Previous on the list */
68 sqlite3_stmt *pStmt; /* The prepared statement */
69 int nSql; /* chars in zSql[] */
70 char zSql[1]; /* Text of the SQL statement */
71};
72
73/*
drhbec3f402000-08-04 13:49:02 +000074** There is one instance of this structure for each SQLite database
75** that has been opened by the SQLite TCL interface.
76*/
77typedef struct SqliteDb SqliteDb;
78struct SqliteDb {
drh895d7472004-08-20 16:02:39 +000079 sqlite3 *db; /* The "real" database structure */
drhbec3f402000-08-04 13:49:02 +000080 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000081 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000082 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000083 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000084 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000085 char *zAuth; /* The authorization callback routine */
danielk197755c45f22005-04-03 23:54:43 +000086 char *zNull; /* Text to substitute for an SQL NULL value */
drhcabb0812002-09-14 13:47:32 +000087 SqlFunc *pFunc; /* List of SQL functions */
danielk19770202b292004-06-09 09:55:16 +000088 SqlCollate *pCollate; /* List of SQL collation functions */
danielk19776f8a5032004-05-10 10:34:51 +000089 int rc; /* Return code of most recent sqlite3_exec() */
danielk19777cedc8d2004-06-10 10:50:08 +000090 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhfb7e7652005-01-24 00:28:42 +000091 SqlPreparedStmt *stmtList; /* List of prepared statements*/
92 SqlPreparedStmt *stmtLast; /* Last statement in the list */
93 int maxStmt; /* The next maximum number of stmtList */
94 int nStmt; /* Number of statements in stmtList */
drh98808ba2001-10-18 12:34:46 +000095};
drh297ecf12001-04-05 15:57:13 +000096
drh6d313162000-09-21 13:01:35 +000097/*
drhfb7e7652005-01-24 00:28:42 +000098** Finalize and free a list of prepared statements
99*/
100static void flushStmtCache( SqliteDb *pDb ){
101 SqlPreparedStmt *pPreStmt;
102
103 while( pDb->stmtList ){
104 sqlite3_finalize( pDb->stmtList->pStmt );
105 pPreStmt = pDb->stmtList;
106 pDb->stmtList = pDb->stmtList->pNext;
107 Tcl_Free( (char*)pPreStmt );
108 }
109 pDb->nStmt = 0;
110 pDb->stmtLast = 0;
111}
112
113/*
drh895d7472004-08-20 16:02:39 +0000114** TCL calls this procedure when an sqlite3 database command is
115** deleted.
drh75897232000-05-29 14:26:00 +0000116*/
117static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000118 SqliteDb *pDb = (SqliteDb*)db;
drhfb7e7652005-01-24 00:28:42 +0000119 flushStmtCache(pDb);
danielk19776f8a5032004-05-10 10:34:51 +0000120 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000121 while( pDb->pFunc ){
122 SqlFunc *pFunc = pDb->pFunc;
123 pDb->pFunc = pFunc->pNext;
124 Tcl_Free((char*)pFunc);
125 }
danielk19770202b292004-06-09 09:55:16 +0000126 while( pDb->pCollate ){
127 SqlCollate *pCollate = pDb->pCollate;
128 pDb->pCollate = pCollate->pNext;
129 Tcl_Free((char*)pCollate);
130 }
drhbec3f402000-08-04 13:49:02 +0000131 if( pDb->zBusy ){
132 Tcl_Free(pDb->zBusy);
133 }
drhb5a20d32003-04-23 12:25:23 +0000134 if( pDb->zTrace ){
135 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000136 }
drhe22a3342003-04-22 20:30:37 +0000137 if( pDb->zAuth ){
138 Tcl_Free(pDb->zAuth);
139 }
danielk197755c45f22005-04-03 23:54:43 +0000140 if( pDb->zNull ){
141 Tcl_Free(pDb->zNull);
142 }
drhbec3f402000-08-04 13:49:02 +0000143 Tcl_Free((char*)pDb);
144}
145
146/*
147** This routine is called when a database file is locked while trying
148** to execute SQL.
149*/
danielk19772a764eb2004-06-12 01:43:26 +0000150static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000151 SqliteDb *pDb = (SqliteDb*)cd;
152 int rc;
153 char zVal[30];
154 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000155 Tcl_DString cmd;
156
157 Tcl_DStringInit(&cmd);
158 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
danielk19772a764eb2004-06-12 01:43:26 +0000159 sprintf(zVal, "%d", nTries);
160 Tcl_DStringAppendElement(&cmd, zVal);
drhbec3f402000-08-04 13:49:02 +0000161 zCmd = Tcl_DStringValue(&cmd);
162 rc = Tcl_Eval(pDb->interp, zCmd);
163 Tcl_DStringFree(&cmd);
164 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
165 return 0;
166 }
167 return 1;
drh75897232000-05-29 14:26:00 +0000168}
169
170/*
danielk1977348bb5d2003-10-18 09:37:26 +0000171** This routine is invoked as the 'progress callback' for the database.
172*/
173static int DbProgressHandler(void *cd){
174 SqliteDb *pDb = (SqliteDb*)cd;
175 int rc;
176
177 assert( pDb->zProgress );
178 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
179 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
180 return 1;
181 }
182 return 0;
183}
184
185/*
drhb5a20d32003-04-23 12:25:23 +0000186** This routine is called by the SQLite trace handler whenever a new
187** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000188*/
drhb5a20d32003-04-23 12:25:23 +0000189static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000190 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000191 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000192
drhb5a20d32003-04-23 12:25:23 +0000193 Tcl_DStringInit(&str);
194 Tcl_DStringAppend(&str, pDb->zTrace, -1);
195 Tcl_DStringAppendElement(&str, zSql);
196 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
197 Tcl_DStringFree(&str);
198 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000199}
200
201/*
drhaa940ea2004-01-15 02:44:03 +0000202** This routine is called when a transaction is committed. The
203** TCL script in pDb->zCommit is executed. If it returns non-zero or
204** if it throws an exception, the transaction is rolled back instead
205** of being committed.
206*/
207static int DbCommitHandler(void *cd){
208 SqliteDb *pDb = (SqliteDb*)cd;
209 int rc;
210
211 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
212 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
213 return 1;
214 }
215 return 0;
216}
217
danielk19777cedc8d2004-06-10 10:50:08 +0000218static void tclCollateNeeded(
219 void *pCtx,
drh9bb575f2004-09-06 17:24:11 +0000220 sqlite3 *db,
danielk19777cedc8d2004-06-10 10:50:08 +0000221 int enc,
222 const char *zName
223){
224 SqliteDb *pDb = (SqliteDb *)pCtx;
225 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
226 Tcl_IncrRefCount(pScript);
227 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
228 Tcl_EvalObjEx(pDb->interp, pScript, 0);
229 Tcl_DecrRefCount(pScript);
230}
231
drhaa940ea2004-01-15 02:44:03 +0000232/*
danielk19770202b292004-06-09 09:55:16 +0000233** This routine is called to evaluate an SQL collation function implemented
234** using TCL script.
235*/
236static int tclSqlCollate(
237 void *pCtx,
238 int nA,
239 const void *zA,
240 int nB,
241 const void *zB
242){
243 SqlCollate *p = (SqlCollate *)pCtx;
244 Tcl_Obj *pCmd;
245
246 pCmd = Tcl_NewStringObj(p->zScript, -1);
247 Tcl_IncrRefCount(pCmd);
248 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
249 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
250 Tcl_EvalObjEx(p->interp, pCmd, 0);
251 Tcl_DecrRefCount(pCmd);
252 return (atoi(Tcl_GetStringResult(p->interp)));
253}
254
255/*
drhcabb0812002-09-14 13:47:32 +0000256** This routine is called to evaluate an SQL function implemented
257** using TCL script.
258*/
drhfb7e7652005-01-24 00:28:42 +0000259static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){
danielk19776f8a5032004-05-10 10:34:51 +0000260 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000261 Tcl_DString cmd;
262 int i;
263 int rc;
264
265 Tcl_DStringInit(&cmd);
266 Tcl_DStringAppend(&cmd, p->zScript, -1);
267 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000268 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000269 Tcl_DStringAppendElement(&cmd, "");
270 }else{
drh4f26d6c2004-05-26 23:25:30 +0000271 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000272 }
drhcabb0812002-09-14 13:47:32 +0000273 }
274 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
275 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000276 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000277 }else{
danielk1977d8123362004-06-12 09:25:12 +0000278 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
279 SQLITE_TRANSIENT);
drhcabb0812002-09-14 13:47:32 +0000280 }
281}
drh895d7472004-08-20 16:02:39 +0000282
drhe22a3342003-04-22 20:30:37 +0000283#ifndef SQLITE_OMIT_AUTHORIZATION
284/*
285** This is the authentication function. It appends the authentication
286** type code and the two arguments to zCmd[] then invokes the result
287** on the interpreter. The reply is examined to determine if the
288** authentication fails or succeeds.
289*/
290static int auth_callback(
291 void *pArg,
292 int code,
293 const char *zArg1,
294 const char *zArg2,
295 const char *zArg3,
296 const char *zArg4
297){
298 char *zCode;
299 Tcl_DString str;
300 int rc;
301 const char *zReply;
302 SqliteDb *pDb = (SqliteDb*)pArg;
303
304 switch( code ){
305 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
306 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
307 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
308 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
309 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
310 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
311 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
312 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
313 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
314 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
315 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
316 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
317 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
318 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
319 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
320 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
321 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
322 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
323 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
324 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
325 case SQLITE_READ : zCode="SQLITE_READ"; break;
326 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
327 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
328 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000329 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
330 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
danielk19771c8c23c2004-11-12 15:53:37 +0000331 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break;
danielk19771d54df82004-11-23 15:41:16 +0000332 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break;
drhe22a3342003-04-22 20:30:37 +0000333 default : zCode="????"; break;
334 }
335 Tcl_DStringInit(&str);
336 Tcl_DStringAppend(&str, pDb->zAuth, -1);
337 Tcl_DStringAppendElement(&str, zCode);
338 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
339 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
340 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
341 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
342 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
343 Tcl_DStringFree(&str);
344 zReply = Tcl_GetStringResult(pDb->interp);
345 if( strcmp(zReply,"SQLITE_OK")==0 ){
346 rc = SQLITE_OK;
347 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
348 rc = SQLITE_DENY;
349 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
350 rc = SQLITE_IGNORE;
351 }else{
352 rc = 999;
353 }
354 return rc;
355}
356#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000357
358/*
danielk1977ef2cb632004-05-29 02:37:19 +0000359** zText is a pointer to text obtained via an sqlite3_result_text()
360** or similar interface. This routine returns a Tcl string object,
361** reference count set to 0, containing the text. If a translation
362** between iso8859 and UTF-8 is required, it is preformed.
363*/
364static Tcl_Obj *dbTextToObj(char const *zText){
365 Tcl_Obj *pVal;
366#ifdef UTF_TRANSLATION_NEEDED
367 Tcl_DString dCol;
368 Tcl_DStringInit(&dCol);
369 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
370 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
371 Tcl_DStringFree(&dCol);
372#else
373 pVal = Tcl_NewStringObj(zText, -1);
374#endif
375 return pVal;
376}
377
378/*
tpoindex1067fe12004-12-17 15:41:11 +0000379** This routine reads a line of text from FILE in, stores
380** the text in memory obtained from malloc() and returns a pointer
381** to the text. NULL is returned at end of file, or if malloc()
382** fails.
383**
384** The interface is like "readline" but no command-line editing
385** is done.
386**
387** copied from shell.c from '.import' command
388*/
389static char *local_getline(char *zPrompt, FILE *in){
390 char *zLine;
391 int nLine;
392 int n;
393 int eol;
394
395 nLine = 100;
396 zLine = malloc( nLine );
397 if( zLine==0 ) return 0;
398 n = 0;
399 eol = 0;
400 while( !eol ){
401 if( n+100>nLine ){
402 nLine = nLine*2 + 100;
403 zLine = realloc(zLine, nLine);
404 if( zLine==0 ) return 0;
405 }
406 if( fgets(&zLine[n], nLine - n, in)==0 ){
407 if( n==0 ){
408 free(zLine);
409 return 0;
410 }
411 zLine[n] = 0;
412 eol = 1;
413 break;
414 }
415 while( zLine[n] ){ n++; }
416 if( n>0 && zLine[n-1]=='\n' ){
417 n--;
418 zLine[n] = 0;
419 eol = 1;
420 }
421 }
422 zLine = realloc( zLine, n+1 );
423 return zLine;
424}
425
426/*
drh75897232000-05-29 14:26:00 +0000427** The "sqlite" command below creates a new Tcl command for each
428** connection it opens to an SQLite database. This routine is invoked
429** whenever one of those connection-specific commands is executed
430** in Tcl. For example, if you run Tcl code like this:
431**
drh9bb575f2004-09-06 17:24:11 +0000432** sqlite3 db1 "my_database"
drh75897232000-05-29 14:26:00 +0000433** db1 close
434**
435** The first command opens a connection to the "my_database" database
436** and calls that connection "db1". The second command causes this
437** subroutine to be invoked.
438*/
drh6d313162000-09-21 13:01:35 +0000439static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000440 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000441 int choice;
drh22fbcb82004-02-01 01:22:50 +0000442 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000443 static const char *DB_strs[] = {
drhfb7e7652005-01-24 00:28:42 +0000444 "authorizer", "busy", "cache",
445 "changes", "close", "collate",
446 "collation_needed", "commit_hook", "complete",
447 "copy", "errorcode", "eval",
danielk197755c45f22005-04-03 23:54:43 +0000448 "function", "last_insert_rowid", "nullvalue",
449 "onecolumn", "progress", "rekey",
450 "timeout", "total_changes", "trace",
451 "version",
drh0f14e2e2004-06-29 12:39:08 +0000452 0
drh6d313162000-09-21 13:01:35 +0000453 };
drh411995d2002-06-25 19:31:18 +0000454 enum DB_enum {
drhfb7e7652005-01-24 00:28:42 +0000455 DB_AUTHORIZER, DB_BUSY, DB_CACHE,
456 DB_CHANGES, DB_CLOSE, DB_COLLATE,
457 DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE,
458 DB_COPY, DB_ERRORCODE, DB_EVAL,
danielk197755c45f22005-04-03 23:54:43 +0000459 DB_FUNCTION, DB_LAST_INSERT_ROWID,DB_NULLVALUE,
460 DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
461 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
462 DB_VERSION
drh6d313162000-09-21 13:01:35 +0000463 };
tpoindex1067fe12004-12-17 15:41:11 +0000464 /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
drh6d313162000-09-21 13:01:35 +0000465
466 if( objc<2 ){
467 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000468 return TCL_ERROR;
469 }
drh411995d2002-06-25 19:31:18 +0000470 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000471 return TCL_ERROR;
472 }
473
drh411995d2002-06-25 19:31:18 +0000474 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000475
drhe22a3342003-04-22 20:30:37 +0000476 /* $db authorizer ?CALLBACK?
477 **
478 ** Invoke the given callback to authorize each SQL operation as it is
479 ** compiled. 5 arguments are appended to the callback before it is
480 ** invoked:
481 **
482 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
483 ** (2) First descriptive name (depends on authorization type)
484 ** (3) Second descriptive name
485 ** (4) Name of the database (ex: "main", "temp")
486 ** (5) Name of trigger that is doing the access
487 **
488 ** The callback should return on of the following strings: SQLITE_OK,
489 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
490 **
491 ** If this method is invoked with no arguments, the current authorization
492 ** callback string is returned.
493 */
494 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +0000495#ifdef SQLITE_OMIT_AUTHORIZATION
496 Tcl_AppendResult(interp, "authorization not available in this build", 0);
497 return TCL_ERROR;
498#else
drhe22a3342003-04-22 20:30:37 +0000499 if( objc>3 ){
500 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000501 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +0000502 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000503 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000504 Tcl_AppendResult(interp, pDb->zAuth, 0);
505 }
506 }else{
507 char *zAuth;
508 int len;
509 if( pDb->zAuth ){
510 Tcl_Free(pDb->zAuth);
511 }
512 zAuth = Tcl_GetStringFromObj(objv[2], &len);
513 if( zAuth && len>0 ){
514 pDb->zAuth = Tcl_Alloc( len + 1 );
515 strcpy(pDb->zAuth, zAuth);
516 }else{
517 pDb->zAuth = 0;
518 }
drhe22a3342003-04-22 20:30:37 +0000519 if( pDb->zAuth ){
520 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000521 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000522 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000523 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000524 }
drhe22a3342003-04-22 20:30:37 +0000525 }
drh1211de32004-07-26 12:24:22 +0000526#endif
drhe22a3342003-04-22 20:30:37 +0000527 break;
528 }
529
drhbec3f402000-08-04 13:49:02 +0000530 /* $db busy ?CALLBACK?
531 **
532 ** Invoke the given callback if an SQL statement attempts to open
533 ** a locked database file.
534 */
drh6d313162000-09-21 13:01:35 +0000535 case DB_BUSY: {
536 if( objc>3 ){
537 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000538 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000539 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000540 if( pDb->zBusy ){
541 Tcl_AppendResult(interp, pDb->zBusy, 0);
542 }
543 }else{
drh6d313162000-09-21 13:01:35 +0000544 char *zBusy;
545 int len;
drhbec3f402000-08-04 13:49:02 +0000546 if( pDb->zBusy ){
547 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000548 }
drh6d313162000-09-21 13:01:35 +0000549 zBusy = Tcl_GetStringFromObj(objv[2], &len);
550 if( zBusy && len>0 ){
551 pDb->zBusy = Tcl_Alloc( len + 1 );
552 strcpy(pDb->zBusy, zBusy);
553 }else{
554 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000555 }
556 if( pDb->zBusy ){
557 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000558 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000559 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000560 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000561 }
562 }
drh6d313162000-09-21 13:01:35 +0000563 break;
564 }
drhbec3f402000-08-04 13:49:02 +0000565
drhfb7e7652005-01-24 00:28:42 +0000566 /* $db cache flush
567 ** $db cache size n
568 **
569 ** Flush the prepared statement cache, or set the maximum number of
570 ** cached statements.
571 */
572 case DB_CACHE: {
573 char *subCmd;
574 int n;
575
576 if( objc<=2 ){
577 Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?");
578 return TCL_ERROR;
579 }
580 subCmd = Tcl_GetStringFromObj( objv[2], 0 );
581 if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){
582 if( objc!=3 ){
583 Tcl_WrongNumArgs(interp, 2, objv, "flush");
584 return TCL_ERROR;
585 }else{
586 flushStmtCache( pDb );
587 }
588 }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){
589 if( objc!=4 ){
590 Tcl_WrongNumArgs(interp, 2, objv, "size n");
591 return TCL_ERROR;
592 }else{
593 if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){
594 Tcl_AppendResult( interp, "cannot convert \"",
595 Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0);
596 return TCL_ERROR;
597 }else{
598 if( n<0 ){
599 flushStmtCache( pDb );
600 n = 0;
601 }else if( n>MAX_PREPARED_STMTS ){
602 n = MAX_PREPARED_STMTS;
603 }
604 pDb->maxStmt = n;
605 }
606 }
607 }else{
608 Tcl_AppendResult( interp, "bad option \"",
609 Tcl_GetStringFromObj(objv[0],0), "\": must be flush or size", 0);
610 return TCL_ERROR;
611 }
612 break;
613 }
614
danielk1977b28af712004-06-21 06:50:26 +0000615 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000616 **
617 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000618 ** the most recent INSERT, UPDATE or DELETE statement, not including
619 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000620 */
621 case DB_CHANGES: {
622 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000623 if( objc!=2 ){
624 Tcl_WrongNumArgs(interp, 2, objv, "");
625 return TCL_ERROR;
626 }
drhc8d30ac2002-04-12 10:08:59 +0000627 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000628 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000629 break;
630 }
631
drh75897232000-05-29 14:26:00 +0000632 /* $db close
633 **
634 ** Shutdown the database
635 */
drh6d313162000-09-21 13:01:35 +0000636 case DB_CLOSE: {
637 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
638 break;
639 }
drh75897232000-05-29 14:26:00 +0000640
drhaa940ea2004-01-15 02:44:03 +0000641 /* $db commit_hook ?CALLBACK?
642 **
643 ** Invoke the given callback just before committing every SQL transaction.
644 ** If the callback throws an exception or returns non-zero, then the
645 ** transaction is aborted. If CALLBACK is an empty string, the callback
646 ** is disabled.
647 */
648 case DB_COMMIT_HOOK: {
649 if( objc>3 ){
650 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000651 return TCL_ERROR;
drhaa940ea2004-01-15 02:44:03 +0000652 }else if( objc==2 ){
653 if( pDb->zCommit ){
654 Tcl_AppendResult(interp, pDb->zCommit, 0);
655 }
656 }else{
657 char *zCommit;
658 int len;
659 if( pDb->zCommit ){
660 Tcl_Free(pDb->zCommit);
661 }
662 zCommit = Tcl_GetStringFromObj(objv[2], &len);
663 if( zCommit && len>0 ){
664 pDb->zCommit = Tcl_Alloc( len + 1 );
665 strcpy(pDb->zCommit, zCommit);
666 }else{
667 pDb->zCommit = 0;
668 }
669 if( pDb->zCommit ){
670 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000671 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000672 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000673 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000674 }
675 }
676 break;
677 }
678
drh0f14e2e2004-06-29 12:39:08 +0000679 /*
680 ** $db collate NAME SCRIPT
681 **
682 ** Create a new SQL collation function called NAME. Whenever
683 ** that function is called, invoke SCRIPT to evaluate the function.
684 */
685 case DB_COLLATE: {
686 SqlCollate *pCollate;
687 char *zName;
688 char *zScript;
689 int nScript;
690 if( objc!=4 ){
691 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
692 return TCL_ERROR;
693 }
694 zName = Tcl_GetStringFromObj(objv[2], 0);
695 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
696 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
697 if( pCollate==0 ) return TCL_ERROR;
698 pCollate->interp = interp;
699 pCollate->pNext = pDb->pCollate;
700 pCollate->zScript = (char*)&pCollate[1];
701 pDb->pCollate = pCollate;
702 strcpy(pCollate->zScript, zScript);
703 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
704 pCollate, tclSqlCollate) ){
danielk19779636c4e2005-01-25 04:27:54 +0000705 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drh0f14e2e2004-06-29 12:39:08 +0000706 return TCL_ERROR;
707 }
708 break;
709 }
710
711 /*
712 ** $db collation_needed SCRIPT
713 **
714 ** Create a new SQL collation function called NAME. Whenever
715 ** that function is called, invoke SCRIPT to evaluate the function.
716 */
717 case DB_COLLATION_NEEDED: {
718 if( objc!=3 ){
719 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
720 return TCL_ERROR;
721 }
722 if( pDb->pCollateNeeded ){
723 Tcl_DecrRefCount(pDb->pCollateNeeded);
724 }
725 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
726 Tcl_IncrRefCount(pDb->pCollateNeeded);
727 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
728 break;
729 }
730
drh75897232000-05-29 14:26:00 +0000731 /* $db complete SQL
732 **
733 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
734 ** additional lines of input are needed. This is similar to the
735 ** built-in "info complete" command of Tcl.
736 */
drh6d313162000-09-21 13:01:35 +0000737 case DB_COMPLETE: {
drhccae6022005-02-26 17:31:26 +0000738#ifndef SQLITE_OMIT_COMPLETE
drh6d313162000-09-21 13:01:35 +0000739 Tcl_Obj *pResult;
740 int isComplete;
741 if( objc!=3 ){
742 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000743 return TCL_ERROR;
744 }
danielk19776f8a5032004-05-10 10:34:51 +0000745 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000746 pResult = Tcl_GetObjResult(interp);
747 Tcl_SetBooleanObj(pResult, isComplete);
drhccae6022005-02-26 17:31:26 +0000748#endif
drh6d313162000-09-21 13:01:35 +0000749 break;
750 }
drhdcd997e2003-01-31 17:21:49 +0000751
752 /*
753 ** $db errorcode
754 **
755 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000756 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000757 */
758 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000759 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000760 break;
761 }
drh75897232000-05-29 14:26:00 +0000762
763 /*
drh895d7472004-08-20 16:02:39 +0000764 ** $db eval $sql ?array? ?{ ...code... }?
drh1807ce32004-09-07 13:20:35 +0000765 ** $db onecolumn $sql
drh75897232000-05-29 14:26:00 +0000766 **
767 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000768 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000769 ** If "array" and "code" are omitted, then no callback is every invoked.
770 ** If "array" is an empty string, then the values are placed in variables
771 ** that have the same name as the fields extracted by the query.
drh1807ce32004-09-07 13:20:35 +0000772 **
773 ** The onecolumn method is the equivalent of:
774 ** lindex [$db eval $sql] 0
drh75897232000-05-29 14:26:00 +0000775 */
drh1807ce32004-09-07 13:20:35 +0000776 case DB_ONECOLUMN:
drh6d313162000-09-21 13:01:35 +0000777 case DB_EVAL: {
drh9d74b4c2004-08-24 15:23:34 +0000778 char const *zSql; /* Next SQL statement to execute */
779 char const *zLeft; /* What is left after first stmt in zSql */
780 sqlite3_stmt *pStmt; /* Compiled SQL statment */
drh92febd92004-08-20 18:34:20 +0000781 Tcl_Obj *pArray; /* Name of array into which results are written */
782 Tcl_Obj *pScript; /* Script to run for each result set */
drh1d895032004-08-26 00:56:05 +0000783 Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
784 int nParm; /* Number of entries used in apParm[] */
785 Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
drh1807ce32004-09-07 13:20:35 +0000786 Tcl_Obj *pRet; /* Value to be returned */
drhfb7e7652005-01-24 00:28:42 +0000787 SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */
788 int rc2;
danielk1977ef2cb632004-05-29 02:37:19 +0000789
drh1807ce32004-09-07 13:20:35 +0000790 if( choice==DB_ONECOLUMN ){
791 if( objc!=3 ){
792 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
793 return TCL_ERROR;
794 }
795 pRet = 0;
796 }else{
797 if( objc<3 || objc>5 ){
798 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
799 return TCL_ERROR;
800 }
801 pRet = Tcl_NewObj();
802 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000803 }
drh92febd92004-08-20 18:34:20 +0000804 if( objc==3 ){
805 pArray = pScript = 0;
806 }else if( objc==4 ){
807 pArray = 0;
808 pScript = objv[3];
809 }else{
810 pArray = objv[3];
811 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
812 pScript = objv[4];
813 }
danielk197730ccda12004-05-27 12:11:31 +0000814
drh1d895032004-08-26 00:56:05 +0000815 Tcl_IncrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +0000816 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh90b6bb12004-09-13 13:16:31 +0000817 while( rc==TCL_OK && zSql[0] ){
drhfb7e7652005-01-24 00:28:42 +0000818 int i; /* Loop counter */
819 int nVar; /* Number of bind parameters in the pStmt */
820 int nCol; /* Number of columns in the result set */
drh92febd92004-08-20 18:34:20 +0000821 Tcl_Obj **apColName = 0; /* Array of column names */
drhfb7e7652005-01-24 00:28:42 +0000822 int len; /* String length of zSql */
danielk197730ccda12004-05-27 12:11:31 +0000823
drhfb7e7652005-01-24 00:28:42 +0000824 /* Try to find a SQL statement that has already been compiled and
825 ** which matches the next sequence of SQL.
826 */
827 pStmt = 0;
828 pPreStmt = pDb->stmtList;
829 len = strlen(zSql);
830 if( pPreStmt && sqlite3_expired(pPreStmt->pStmt) ){
831 flushStmtCache(pDb);
832 pPreStmt = 0;
danielk197730ccda12004-05-27 12:11:31 +0000833 }
drhfb7e7652005-01-24 00:28:42 +0000834 for(; pPreStmt; pPreStmt=pPreStmt->pNext){
835 int n = pPreStmt->nSql;
836 if( len>=n
837 && memcmp(pPreStmt->zSql, zSql, n)==0
838 && (zSql[n]==0 || zSql[n-1]==';')
839 ){
840 pStmt = pPreStmt->pStmt;
841 zLeft = &zSql[pPreStmt->nSql];
842
843 /* When a prepared statement is found, unlink it from the
844 ** cache list. It will later be added back to the beginning
845 ** of the cache list in order to implement LRU replacement.
846 */
847 if( pPreStmt->pPrev ){
848 pPreStmt->pPrev->pNext = pPreStmt->pNext;
849 }else{
850 pDb->stmtList = pPreStmt->pNext;
851 }
852 if( pPreStmt->pNext ){
853 pPreStmt->pNext->pPrev = pPreStmt->pPrev;
854 }else{
855 pDb->stmtLast = pPreStmt->pPrev;
856 }
857 pDb->nStmt--;
858 break;
859 }
860 }
861
862 /* If no prepared statement was found. Compile the SQL text
863 */
drh92febd92004-08-20 18:34:20 +0000864 if( pStmt==0 ){
drhfb7e7652005-01-24 00:28:42 +0000865 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
drh92febd92004-08-20 18:34:20 +0000866 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
867 rc = TCL_ERROR;
868 break;
danielk197730ccda12004-05-27 12:11:31 +0000869 }
drhfb7e7652005-01-24 00:28:42 +0000870 if( pStmt==0 ){
871 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
872 /* A compile-time error in the statement
873 */
874 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
875 rc = TCL_ERROR;
876 break;
877 }else{
878 /* The statement was a no-op. Continue to the next statement
879 ** in the SQL string.
880 */
881 zSql = zLeft;
882 continue;
883 }
884 }
885 assert( pPreStmt==0 );
danielk197730ccda12004-05-27 12:11:31 +0000886 }
887
drhfb7e7652005-01-24 00:28:42 +0000888 /* Bind values to parameters that begin with $ or :
889 */
drh92febd92004-08-20 18:34:20 +0000890 nVar = sqlite3_bind_parameter_count(pStmt);
drh1d895032004-08-26 00:56:05 +0000891 nParm = 0;
892 if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){
893 apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0]));
894 }else{
895 apParm = aParm;
896 }
drh92febd92004-08-20 18:34:20 +0000897 for(i=1; i<=nVar; i++){
898 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
drhc8f90792005-01-12 00:08:24 +0000899 if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':') ){
drh92febd92004-08-20 18:34:20 +0000900 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
901 if( pVar ){
902 int n;
903 u8 *data;
904 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
905 char c = zType[0];
906 if( c=='b' && strcmp(zType,"bytearray")==0 ){
907 data = Tcl_GetByteArrayFromObj(pVar, &n);
908 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +0000909 Tcl_IncrRefCount(pVar);
910 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +0000911 }else if( (c=='b' && strcmp(zType,"boolean")==0) ||
912 (c=='i' && strcmp(zType,"int")==0) ){
913 Tcl_GetIntFromObj(interp, pVar, &n);
914 sqlite3_bind_int(pStmt, i, n);
915 }else if( c=='d' && strcmp(zType,"double")==0 ){
916 double r;
917 Tcl_GetDoubleFromObj(interp, pVar, &r);
918 sqlite3_bind_double(pStmt, i, r);
919 }else{
920 data = Tcl_GetStringFromObj(pVar, &n);
921 sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +0000922 Tcl_IncrRefCount(pVar);
923 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +0000924 }
drhfb7e7652005-01-24 00:28:42 +0000925 }else{
926 sqlite3_bind_null( pStmt, i );
drh92febd92004-08-20 18:34:20 +0000927 }
928 }
929 }
drh92febd92004-08-20 18:34:20 +0000930
931 /* Compute column names */
932 nCol = sqlite3_column_count(pStmt);
933 if( pScript ){
934 apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
935 if( apColName==0 ) break;
936 for(i=0; i<nCol; i++){
937 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
938 Tcl_IncrRefCount(apColName[i]);
939 }
940 }
941
942 /* If results are being stored in an array variable, then create
943 ** the array(*) entry for that array
944 */
945 if( pArray ){
946 Tcl_Obj *pColList = Tcl_NewObj();
drh3ced14a2005-03-31 18:26:20 +0000947 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1);
drh92febd92004-08-20 18:34:20 +0000948 Tcl_IncrRefCount(pColList);
949 for(i=0; i<nCol; i++){
950 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
951 }
drh3ced14a2005-03-31 18:26:20 +0000952 Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0);
953 Tcl_DecrRefCount(pColList);
954 Tcl_DecrRefCount(pStar);
drh92febd92004-08-20 18:34:20 +0000955 }
956
957 /* Execute the SQL
958 */
drh90b6bb12004-09-13 13:16:31 +0000959 while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
drh92febd92004-08-20 18:34:20 +0000960 for(i=0; i<nCol; i++){
danielk197730ccda12004-05-27 12:11:31 +0000961 Tcl_Obj *pVal;
962
963 /* Set pVal to contain the i'th column of this row. */
drh92febd92004-08-20 18:34:20 +0000964 switch( sqlite3_column_type(pStmt, i) ){
965 case SQLITE_BLOB: {
966 int bytes = sqlite3_column_bytes(pStmt, i);
967 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
968 break;
969 }
970 case SQLITE_INTEGER: {
971 sqlite_int64 v = sqlite3_column_int64(pStmt, i);
972 if( v>=-2147483647 && v<=2147483647 ){
973 pVal = Tcl_NewIntObj(v);
974 }else{
975 pVal = Tcl_NewWideIntObj(v);
976 }
977 break;
978 }
979 case SQLITE_FLOAT: {
980 double r = sqlite3_column_double(pStmt, i);
981 pVal = Tcl_NewDoubleObj(r);
982 break;
983 }
danielk197755c45f22005-04-03 23:54:43 +0000984 case SQLITE_NULL: {
985 pVal = dbTextToObj(pDb->zNull);
986 break;
987 }
drh92febd92004-08-20 18:34:20 +0000988 default: {
989 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
990 break;
991 }
danielk197730ccda12004-05-27 12:11:31 +0000992 }
993
drh92febd92004-08-20 18:34:20 +0000994 if( pScript ){
995 if( pArray==0 ){
996 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000997 }else{
drh92febd92004-08-20 18:34:20 +0000998 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000999 }
drh1807ce32004-09-07 13:20:35 +00001000 }else if( choice==DB_ONECOLUMN ){
1001 if( pRet==0 ){
1002 pRet = pVal;
1003 Tcl_IncrRefCount(pRet);
1004 }
drh90b6bb12004-09-13 13:16:31 +00001005 rc = TCL_BREAK;
danielk197730ccda12004-05-27 12:11:31 +00001006 }else{
danielk197730ccda12004-05-27 12:11:31 +00001007 Tcl_ListObjAppendElement(interp, pRet, pVal);
1008 }
1009 }
1010
drh92febd92004-08-20 18:34:20 +00001011 if( pScript ){
1012 rc = Tcl_EvalObjEx(interp, pScript, 0);
drh90b6bb12004-09-13 13:16:31 +00001013 if( rc==TCL_CONTINUE ){
1014 rc = TCL_OK;
1015 }
danielk197730ccda12004-05-27 12:11:31 +00001016 }
1017 }
drh90b6bb12004-09-13 13:16:31 +00001018 if( rc==TCL_BREAK ){
1019 rc = TCL_OK;
1020 }
drh92febd92004-08-20 18:34:20 +00001021
1022 /* Free the column name objects */
1023 if( pScript ){
1024 for(i=0; i<nCol; i++){
1025 Tcl_DecrRefCount(apColName[i]);
1026 }
1027 Tcl_Free((char*)apColName);
1028 }
1029
drh1d895032004-08-26 00:56:05 +00001030 /* Free the bound string and blob parameters */
1031 for(i=0; i<nParm; i++){
1032 Tcl_DecrRefCount(apParm[i]);
1033 }
1034 if( apParm!=aParm ){
1035 Tcl_Free((char*)apParm);
1036 }
1037
drhfb7e7652005-01-24 00:28:42 +00001038 /* Reset the statement. If the result code is SQLITE_SCHEMA, then
1039 ** flush the statement cache and try the statement again.
drh92febd92004-08-20 18:34:20 +00001040 */
drhfb7e7652005-01-24 00:28:42 +00001041 rc2 = sqlite3_reset(pStmt);
1042 if( SQLITE_SCHEMA==rc2 ){
1043 /* After a schema change, flush the cache and try to run the
1044 ** statement again
1045 */
1046 flushStmtCache( pDb );
1047 sqlite3_finalize(pStmt);
1048 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
danielk197730ccda12004-05-27 12:11:31 +00001049 continue;
drhfb7e7652005-01-24 00:28:42 +00001050 }else if( SQLITE_OK!=rc2 ){
1051 /* If a run-time error occurs, report the error and stop reading
1052 ** the SQL
1053 */
danielk1977ef2cb632004-05-29 02:37:19 +00001054 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
drhfb7e7652005-01-24 00:28:42 +00001055 sqlite3_finalize(pStmt);
danielk197730ccda12004-05-27 12:11:31 +00001056 rc = TCL_ERROR;
drhfb7e7652005-01-24 00:28:42 +00001057 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
danielk197730ccda12004-05-27 12:11:31 +00001058 break;
drhfb7e7652005-01-24 00:28:42 +00001059 }else if( pDb->maxStmt<=0 ){
1060 /* If the cache is turned off, deallocated the statement */
1061 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
1062 sqlite3_finalize(pStmt);
1063 }else{
1064 /* Everything worked and the cache is operational.
1065 ** Create a new SqlPreparedStmt structure if we need one.
1066 ** (If we already have one we can just reuse it.)
1067 */
1068 if( pPreStmt==0 ){
1069 len = zLeft - zSql;
1070 pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) + len );
1071 if( pPreStmt==0 ) return TCL_ERROR;
1072 pPreStmt->pStmt = pStmt;
1073 pPreStmt->nSql = len;
1074 memcpy(pPreStmt->zSql, zSql, len);
1075 pPreStmt->zSql[len] = 0;
1076 }
1077
1078 /* Add the prepared statement to the beginning of the cache list
1079 */
1080 pPreStmt->pNext = pDb->stmtList;
1081 pPreStmt->pPrev = 0;
1082 if( pDb->stmtList ){
1083 pDb->stmtList->pPrev = pPreStmt;
1084 }
1085 pDb->stmtList = pPreStmt;
1086 if( pDb->stmtLast==0 ){
1087 assert( pDb->nStmt==0 );
1088 pDb->stmtLast = pPreStmt;
1089 }else{
1090 assert( pDb->nStmt>0 );
1091 }
1092 pDb->nStmt++;
1093
1094 /* If we have too many statement in cache, remove the surplus from the
1095 ** end of the cache list.
1096 */
1097 while( pDb->nStmt>pDb->maxStmt ){
1098 sqlite3_finalize(pDb->stmtLast->pStmt);
1099 pDb->stmtLast = pDb->stmtLast->pPrev;
1100 Tcl_Free((char*)pDb->stmtLast->pNext);
1101 pDb->stmtLast->pNext = 0;
1102 pDb->nStmt--;
1103 }
danielk197730ccda12004-05-27 12:11:31 +00001104 }
1105
drhfb7e7652005-01-24 00:28:42 +00001106 /* Proceed to the next statement */
danielk197730ccda12004-05-27 12:11:31 +00001107 zSql = zLeft;
1108 }
drh1d895032004-08-26 00:56:05 +00001109 Tcl_DecrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +00001110
drh1807ce32004-09-07 13:20:35 +00001111 if( pRet ){
1112 if( rc==TCL_OK ){
1113 Tcl_SetObjResult(interp, pRet);
1114 }
1115 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +00001116 }
danielk197730ccda12004-05-27 12:11:31 +00001117 break;
1118 }
drhbec3f402000-08-04 13:49:02 +00001119
1120 /*
drhcabb0812002-09-14 13:47:32 +00001121 ** $db function NAME SCRIPT
1122 **
1123 ** Create a new SQL function called NAME. Whenever that function is
1124 ** called, invoke SCRIPT to evaluate the function.
1125 */
1126 case DB_FUNCTION: {
1127 SqlFunc *pFunc;
1128 char *zName;
1129 char *zScript;
1130 int nScript;
1131 if( objc!=4 ){
1132 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
1133 return TCL_ERROR;
1134 }
1135 zName = Tcl_GetStringFromObj(objv[2], 0);
1136 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
1137 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
1138 if( pFunc==0 ) return TCL_ERROR;
1139 pFunc->interp = interp;
1140 pFunc->pNext = pDb->pFunc;
1141 pFunc->zScript = (char*)&pFunc[1];
drh0f14e2e2004-06-29 12:39:08 +00001142 pDb->pFunc = pFunc;
drhcabb0812002-09-14 13:47:32 +00001143 strcpy(pFunc->zScript, zScript);
danielk1977c8c11582004-06-29 13:41:21 +00001144 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +00001145 pFunc, tclSqlFunc, 0, 0);
drhfb7e7652005-01-24 00:28:42 +00001146 if( rc!=SQLITE_OK ){
danielk19779636c4e2005-01-25 04:27:54 +00001147 rc = TCL_ERROR;
1148 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drhfb7e7652005-01-24 00:28:42 +00001149 }else{
1150 /* Must flush any cached statements */
1151 flushStmtCache( pDb );
1152 }
drhcabb0812002-09-14 13:47:32 +00001153 break;
1154 }
1155
1156 /*
drhaf9ff332002-01-16 21:00:27 +00001157 ** $db last_insert_rowid
1158 **
1159 ** Return an integer which is the ROWID for the most recent insert.
1160 */
1161 case DB_LAST_INSERT_ROWID: {
1162 Tcl_Obj *pResult;
1163 int rowid;
1164 if( objc!=2 ){
1165 Tcl_WrongNumArgs(interp, 2, objv, "");
1166 return TCL_ERROR;
1167 }
danielk19776f8a5032004-05-10 10:34:51 +00001168 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +00001169 pResult = Tcl_GetObjResult(interp);
1170 Tcl_SetIntObj(pResult, rowid);
1171 break;
1172 }
1173
1174 /*
drh1807ce32004-09-07 13:20:35 +00001175 ** The DB_ONECOLUMN method is implemented together with DB_EVAL.
drh5d9d7572003-08-19 14:31:01 +00001176 */
drh1807ce32004-09-07 13:20:35 +00001177
1178 /* $db progress ?N CALLBACK?
1179 **
1180 ** Invoke the given callback every N virtual machine opcodes while executing
1181 ** queries.
1182 */
1183 case DB_PROGRESS: {
1184 if( objc==2 ){
1185 if( pDb->zProgress ){
1186 Tcl_AppendResult(interp, pDb->zProgress, 0);
1187 }
1188 }else if( objc==4 ){
1189 char *zProgress;
1190 int len;
1191 int N;
1192 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
1193 return TCL_ERROR;
1194 };
1195 if( pDb->zProgress ){
1196 Tcl_Free(pDb->zProgress);
1197 }
1198 zProgress = Tcl_GetStringFromObj(objv[3], &len);
1199 if( zProgress && len>0 ){
1200 pDb->zProgress = Tcl_Alloc( len + 1 );
1201 strcpy(pDb->zProgress, zProgress);
1202 }else{
1203 pDb->zProgress = 0;
1204 }
1205#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1206 if( pDb->zProgress ){
1207 pDb->interp = interp;
1208 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
1209 }else{
1210 sqlite3_progress_handler(pDb->db, 0, 0, 0);
1211 }
1212#endif
1213 }else{
1214 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
drh5d9d7572003-08-19 14:31:01 +00001215 return TCL_ERROR;
1216 }
drh5d9d7572003-08-19 14:31:01 +00001217 break;
1218 }
1219
1220 /*
drh22fbcb82004-02-01 01:22:50 +00001221 ** $db rekey KEY
1222 **
1223 ** Change the encryption key on the currently open database.
1224 */
1225 case DB_REKEY: {
1226 int nKey;
1227 void *pKey;
1228 if( objc!=3 ){
1229 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
1230 return TCL_ERROR;
1231 }
1232 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +00001233#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +00001234 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +00001235 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +00001236 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +00001237 rc = TCL_ERROR;
1238 }
1239#endif
1240 break;
1241 }
1242
1243 /*
drhbec3f402000-08-04 13:49:02 +00001244 ** $db timeout MILLESECONDS
1245 **
1246 ** Delay for the number of milliseconds specified when a file is locked.
1247 */
drh6d313162000-09-21 13:01:35 +00001248 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +00001249 int ms;
drh6d313162000-09-21 13:01:35 +00001250 if( objc!=3 ){
1251 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +00001252 return TCL_ERROR;
1253 }
drh6d313162000-09-21 13:01:35 +00001254 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001255 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +00001256 break;
drh75897232000-05-29 14:26:00 +00001257 }
drhb5a20d32003-04-23 12:25:23 +00001258
drh0f14e2e2004-06-29 12:39:08 +00001259 /*
danielk197755c45f22005-04-03 23:54:43 +00001260 ** $db nullvalue ?STRING?
1261 **
1262 ** Change text used when a NULL comes back from the database. If ?STRING?
1263 ** is not present, then the current string used for NULL is returned.
1264 ** If STRING is present, then STRING is returned.
1265 **
1266 */
1267 case DB_NULLVALUE: {
1268 if( objc!=2 && objc!=3 ){
1269 Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
1270 return TCL_ERROR;
1271 }
1272 if( objc==3 ){
1273 int len;
1274 char *zNull = Tcl_GetStringFromObj(objv[2], &len);
1275 if( pDb->zNull ){
1276 Tcl_Free(pDb->zNull);
1277 }
1278 if( zNull && len>0 ){
1279 pDb->zNull = Tcl_Alloc( len + 1 );
1280 strncpy(pDb->zNull, zNull, len);
1281 pDb->zNull[len] = '\0';
1282 }else{
1283 pDb->zNull = 0;
1284 }
1285 }
1286 Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull));
1287 break;
1288 }
1289
1290 /*
drh0f14e2e2004-06-29 12:39:08 +00001291 ** $db total_changes
1292 **
1293 ** Return the number of rows that were modified, inserted, or deleted
1294 ** since the database handle was created.
1295 */
1296 case DB_TOTAL_CHANGES: {
1297 Tcl_Obj *pResult;
1298 if( objc!=2 ){
1299 Tcl_WrongNumArgs(interp, 2, objv, "");
1300 return TCL_ERROR;
1301 }
1302 pResult = Tcl_GetObjResult(interp);
1303 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
1304 break;
1305 }
1306
drhb5a20d32003-04-23 12:25:23 +00001307 /* $db trace ?CALLBACK?
1308 **
1309 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
1310 ** that is executed. The text of the SQL is appended to CALLBACK before
1311 ** it is executed.
1312 */
1313 case DB_TRACE: {
1314 if( objc>3 ){
1315 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +00001316 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +00001317 }else if( objc==2 ){
1318 if( pDb->zTrace ){
1319 Tcl_AppendResult(interp, pDb->zTrace, 0);
1320 }
1321 }else{
1322 char *zTrace;
1323 int len;
1324 if( pDb->zTrace ){
1325 Tcl_Free(pDb->zTrace);
1326 }
1327 zTrace = Tcl_GetStringFromObj(objv[2], &len);
1328 if( zTrace && len>0 ){
1329 pDb->zTrace = Tcl_Alloc( len + 1 );
1330 strcpy(pDb->zTrace, zTrace);
1331 }else{
1332 pDb->zTrace = 0;
1333 }
1334 if( pDb->zTrace ){
1335 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001336 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00001337 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001338 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00001339 }
1340 }
1341 break;
1342 }
1343
tpoindex1067fe12004-12-17 15:41:11 +00001344 /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
1345 **
1346 ** Copy data into table from filename, optionally using SEPARATOR
1347 ** as column separators. If a column contains a null string, or the
1348 ** value of NULLINDICATOR, a NULL is inserted for the column.
1349 ** conflict-algorithm is one of the sqlite conflict algorithms:
1350 ** rollback, abort, fail, ignore, replace
1351 ** On success, return the number of lines processed, not necessarily same
1352 ** as 'db changes' due to conflict-algorithm selected.
1353 **
1354 ** This code is basically an implementation/enhancement of
1355 ** the sqlite3 shell.c ".import" command.
1356 **
1357 ** This command usage is equivalent to the sqlite2.x COPY statement,
1358 ** which imports file data into a table using the PostgreSQL COPY file format:
1359 ** $db copy $conflit_algo $table_name $filename \t \\N
1360 */
1361 case DB_COPY: {
tpoindex1067fe12004-12-17 15:41:11 +00001362 char *zTable; /* Insert data into this table */
1363 char *zFile; /* The file from which to extract data */
1364 char *zConflict; /* The conflict algorithm to use */
1365 sqlite3_stmt *pStmt; /* A statement */
1366 int rc; /* Result code */
1367 int nCol; /* Number of columns in the table */
1368 int nByte; /* Number of bytes in an SQL string */
1369 int i, j; /* Loop counters */
1370 int nSep; /* Number of bytes in zSep[] */
1371 int nNull; /* Number of bytes in zNull[] */
1372 char *zSql; /* An SQL statement */
1373 char *zLine; /* A single line of input from the file */
1374 char **azCol; /* zLine[] broken up into columns */
1375 char *zCommit; /* How to commit changes */
1376 FILE *in; /* The input file */
1377 int lineno = 0; /* Line number of input file */
1378 char zLineNum[80]; /* Line number print buffer */
1379 Tcl_Obj *pResult; /* interp result */
1380
drh9ee3cdc2004-12-17 20:48:06 +00001381 char *zSep;
1382 char *zNull;
1383 if( objc<5 || objc>7 ){
1384 Tcl_WrongNumArgs(interp, 2, objv,
1385 "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
1386 return TCL_ERROR;
1387 }
1388 if( objc>=6 ){
1389 zSep = Tcl_GetStringFromObj(objv[5], 0);
1390 }else{
1391 zSep = "\t";
1392 }
1393 if( objc>=7 ){
1394 zNull = Tcl_GetStringFromObj(objv[6], 0);
1395 }else{
1396 zNull = "";
1397 }
tpoindex1067fe12004-12-17 15:41:11 +00001398 zConflict = Tcl_GetStringFromObj(objv[2], 0);
1399 zTable = Tcl_GetStringFromObj(objv[3], 0);
1400 zFile = Tcl_GetStringFromObj(objv[4], 0);
1401 nSep = strlen(zSep);
1402 nNull = strlen(zNull);
1403 if( nSep==0 ){
1404 Tcl_AppendResult(interp, "Error: non-null separator required for copy", 0);
1405 return TCL_ERROR;
1406 }
1407 if(sqlite3StrICmp(zConflict, "rollback") != 0 &&
1408 sqlite3StrICmp(zConflict, "abort" ) != 0 &&
1409 sqlite3StrICmp(zConflict, "fail" ) != 0 &&
1410 sqlite3StrICmp(zConflict, "ignore" ) != 0 &&
1411 sqlite3StrICmp(zConflict, "replace" ) != 0 ) {
drh9ee3cdc2004-12-17 20:48:06 +00001412 Tcl_AppendResult(interp, "Error: \"", zConflict,
1413 "\", conflict-algorithm must be one of: rollback, "
1414 "abort, fail, ignore, or replace", 0);
tpoindex1067fe12004-12-17 15:41:11 +00001415 return TCL_ERROR;
1416 }
1417 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1418 if( zSql==0 ){
1419 Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
1420 return TCL_ERROR;
1421 }
1422 nByte = strlen(zSql);
1423 rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
1424 sqlite3_free(zSql);
1425 if( rc ){
1426 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1427 nCol = 0;
1428 }else{
1429 nCol = sqlite3_column_count(pStmt);
1430 }
1431 sqlite3_finalize(pStmt);
1432 if( nCol==0 ) {
1433 return TCL_ERROR;
1434 }
1435 zSql = malloc( nByte + 50 + nCol*2 );
1436 if( zSql==0 ) {
1437 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
1438 return TCL_ERROR;
1439 }
drh9ee3cdc2004-12-17 20:48:06 +00001440 sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
1441 zConflict, zTable);
tpoindex1067fe12004-12-17 15:41:11 +00001442 j = strlen(zSql);
1443 for(i=1; i<nCol; i++){
1444 zSql[j++] = ',';
1445 zSql[j++] = '?';
1446 }
1447 zSql[j++] = ')';
1448 zSql[j] = 0;
1449 rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
1450 free(zSql);
1451 if( rc ){
1452 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1453 sqlite3_finalize(pStmt);
1454 return TCL_ERROR;
1455 }
1456 in = fopen(zFile, "rb");
1457 if( in==0 ){
1458 Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL);
1459 sqlite3_finalize(pStmt);
1460 return TCL_ERROR;
1461 }
1462 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1463 if( azCol==0 ) {
1464 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
1465 return TCL_ERROR;
1466 }
1467 sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
1468 zCommit = "COMMIT";
1469 while( (zLine = local_getline(0, in))!=0 ){
1470 char *z;
1471 i = 0;
1472 lineno++;
1473 azCol[0] = zLine;
1474 for(i=0, z=zLine; *z; z++){
1475 if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
1476 *z = 0;
1477 i++;
1478 if( i<nCol ){
1479 azCol[i] = &z[nSep];
1480 z += nSep-1;
1481 }
1482 }
1483 }
1484 if( i+1!=nCol ){
1485 char *zErr;
1486 zErr = malloc(200 + strlen(zFile));
1487 sprintf(zErr,"Error: %s line %d: expected %d columns of data but found %d",
1488 zFile, lineno, nCol, i+1);
1489 Tcl_AppendResult(interp, zErr, 0);
1490 free(zErr);
1491 zCommit = "ROLLBACK";
1492 break;
1493 }
1494 for(i=0; i<nCol; i++){
1495 /* check for null data, if so, bind as null */
1496 if ((nNull>0 && strcmp(azCol[i], zNull)==0) || strlen(azCol[i])==0) {
1497 sqlite3_bind_null(pStmt, i+1);
1498 }else{
1499 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1500 }
1501 }
1502 sqlite3_step(pStmt);
1503 rc = sqlite3_reset(pStmt);
1504 free(zLine);
1505 if( rc!=SQLITE_OK ){
1506 Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0);
1507 zCommit = "ROLLBACK";
1508 break;
1509 }
1510 }
1511 free(azCol);
1512 fclose(in);
1513 sqlite3_finalize(pStmt);
1514 sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
1515
1516 if( zCommit[0] == 'C' ){
1517 /* success, set result as number of lines processed */
1518 pResult = Tcl_GetObjResult(interp);
1519 Tcl_SetIntObj(pResult, lineno);
1520 rc = TCL_OK;
1521 }else{
1522 /* failure, append lineno where failed */
1523 sprintf(zLineNum,"%d",lineno);
1524 Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0);
1525 rc = TCL_ERROR;
1526 }
1527 break;
1528 }
1529
danielk19774397de52005-01-12 12:44:03 +00001530 /* $db version
1531 **
1532 ** Return the version string for this database.
1533 */
1534 case DB_VERSION: {
1535 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC);
1536 break;
1537 }
1538
tpoindex1067fe12004-12-17 15:41:11 +00001539
drh6d313162000-09-21 13:01:35 +00001540 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00001541 return rc;
drh75897232000-05-29 14:26:00 +00001542}
1543
1544/*
drh9bb575f2004-09-06 17:24:11 +00001545** sqlite3 DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +00001546**
1547** This is the main Tcl command. When the "sqlite" Tcl command is
1548** invoked, this routine runs to process that command.
1549**
1550** The first argument, DBNAME, is an arbitrary name for a new
1551** database connection. This command creates a new command named
1552** DBNAME that is used to control that connection. The database
1553** connection is deleted when the DBNAME command is deleted.
1554**
1555** The second argument is the name of the directory that contains
1556** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +00001557**
1558** For testing purposes, we also support the following:
1559**
drh9bb575f2004-09-06 17:24:11 +00001560** sqlite3 -encoding
drhfbc3eab2001-04-06 16:13:42 +00001561**
1562** Return the encoding used by LIKE and GLOB operators. Choices
1563** are UTF-8 and iso8859.
1564**
drh9bb575f2004-09-06 17:24:11 +00001565** sqlite3 -version
drh647cb0e2002-11-04 19:32:25 +00001566**
1567** Return the version number of the SQLite library.
1568**
drh9bb575f2004-09-06 17:24:11 +00001569** sqlite3 -tcl-uses-utf
drhfbc3eab2001-04-06 16:13:42 +00001570**
1571** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1572** not. Used by tests to make sure the library was compiled
1573** correctly.
drh75897232000-05-29 14:26:00 +00001574*/
drh22fbcb82004-02-01 01:22:50 +00001575static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00001576 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001577 void *pKey = 0;
1578 int nKey = 0;
1579 const char *zArg;
drh75897232000-05-29 14:26:00 +00001580 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001581 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001582 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001583 if( objc==2 ){
1584 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001585 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001586 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001587 return TCL_OK;
1588 }
drh9eb9e262004-02-11 02:18:05 +00001589 if( strcmp(zArg,"-has-codec")==0 ){
1590#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001591 Tcl_AppendResult(interp,"1",0);
1592#else
1593 Tcl_AppendResult(interp,"0",0);
1594#endif
1595 return TCL_OK;
1596 }
1597 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001598#ifdef TCL_UTF_MAX
1599 Tcl_AppendResult(interp,"1",0);
1600#else
1601 Tcl_AppendResult(interp,"0",0);
1602#endif
1603 return TCL_OK;
1604 }
1605 }
drh22fbcb82004-02-01 01:22:50 +00001606 if( objc==5 || objc==6 ){
1607 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1608 if( strcmp(zArg,"-key")==0 ){
1609 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1610 objc -= 2;
1611 }
1612 }
1613 if( objc!=3 && objc!=4 ){
1614 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001615#ifdef SQLITE_HAS_CODEC
1616 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001617#else
1618 "HANDLE FILENAME ?MODE?"
1619#endif
1620 );
drh75897232000-05-29 14:26:00 +00001621 return TCL_ERROR;
1622 }
drh75897232000-05-29 14:26:00 +00001623 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001624 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001625 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001626 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1627 return TCL_ERROR;
1628 }
1629 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001630 zFile = Tcl_GetStringFromObj(objv[2], 0);
danielk19774f057f92004-06-08 00:02:33 +00001631 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001632 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1633 zErrMsg = strdup(sqlite3_errmsg(p->db));
1634 sqlite3_close(p->db);
1635 p->db = 0;
1636 }
drh2011d5f2004-07-22 02:40:37 +00001637#ifdef SQLITE_HAS_CODEC
1638 sqlite3_key(p->db, pKey, nKey);
drheb8ed702004-02-11 10:37:23 +00001639#endif
drhbec3f402000-08-04 13:49:02 +00001640 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001641 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001642 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001643 free(zErrMsg);
1644 return TCL_ERROR;
1645 }
drhfb7e7652005-01-24 00:28:42 +00001646 p->maxStmt = NUM_PREPARED_STMTS;
drh22fbcb82004-02-01 01:22:50 +00001647 zArg = Tcl_GetStringFromObj(objv[1], 0);
1648 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001649
drh06b27182002-06-26 20:06:05 +00001650 /* The return value is the value of the sqlite* pointer
1651 */
1652 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001653 if( strncmp(zBuf,"0x",2) ){
1654 sprintf(zBuf, "0x%p", p->db);
1655 }
drh06b27182002-06-26 20:06:05 +00001656 Tcl_AppendResult(interp, zBuf, 0);
1657
drhc22bd472002-05-10 13:14:07 +00001658 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001659 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001660 */
drh28b4e482002-03-11 02:06:13 +00001661#ifdef SQLITE_TEST
1662 {
drh9bb575f2004-09-06 17:24:11 +00001663 extern void Md5_Register(sqlite3*);
drh3b93bc82005-01-13 23:54:32 +00001664#ifdef SQLITE_MEMDEBUG
danielk197713073932004-06-30 11:54:06 +00001665 int mallocfail = sqlite3_iMallocFail;
1666 sqlite3_iMallocFail = 0;
danielk19775b59af82004-06-30 12:42:59 +00001667#endif
drhc22bd472002-05-10 13:14:07 +00001668 Md5_Register(p->db);
drh3b93bc82005-01-13 23:54:32 +00001669#ifdef SQLITE_MEMDEBUG
danielk197713073932004-06-30 11:54:06 +00001670 sqlite3_iMallocFail = mallocfail;
danielk19775b59af82004-06-30 12:42:59 +00001671#endif
drh06b27182002-06-26 20:06:05 +00001672 }
drh28b4e482002-03-11 02:06:13 +00001673#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001674 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001675 return TCL_OK;
1676}
1677
1678/*
drh90ca9752001-09-28 17:47:14 +00001679** Provide a dummy Tcl_InitStubs if we are using this as a static
1680** library.
1681*/
1682#ifndef USE_TCL_STUBS
1683# undef Tcl_InitStubs
1684# define Tcl_InitStubs(a,b,c)
1685#endif
1686
1687/*
drh75897232000-05-29 14:26:00 +00001688** Initialize this module.
1689**
1690** This Tcl module contains only a single new Tcl command named "sqlite".
1691** (Hence there is no namespace. There is no point in using a namespace
1692** if the extension only supplies one new name!) The "sqlite" command is
1693** used to open a new SQLite database. See the DbMain() routine above
1694** for additional information.
1695*/
drh38f82712004-06-18 17:10:16 +00001696int Sqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001697 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001698 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1699 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh49766d62005-01-08 18:42:28 +00001700 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1701 Tcl_PkgProvide(interp, "sqlite", "3.0");
drh90ca9752001-09-28 17:47:14 +00001702 return TCL_OK;
1703}
drh49766d62005-01-08 18:42:28 +00001704int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
1705int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1706int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1707
1708#ifndef SQLITE_3_SUFFIX_ONLY
1709int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
1710int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
1711int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1712int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
1713#endif
drh75897232000-05-29 14:26:00 +00001714
drh3e27c022004-07-23 00:01:38 +00001715#ifdef TCLSH
1716/*****************************************************************************
1717** The code that follows is used to build standalone TCL interpreters
drh75897232000-05-29 14:26:00 +00001718*/
drh348784e2000-05-29 20:41:49 +00001719
1720/*
drh3e27c022004-07-23 00:01:38 +00001721** If the macro TCLSH is one, then put in code this for the
1722** "main" routine that will initialize Tcl and take input from
1723** standard input.
drh348784e2000-05-29 20:41:49 +00001724*/
drh3e27c022004-07-23 00:01:38 +00001725#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00001726static char zMainloop[] =
1727 "set line {}\n"
1728 "while {![eof stdin]} {\n"
1729 "if {$line!=\"\"} {\n"
1730 "puts -nonewline \"> \"\n"
1731 "} else {\n"
1732 "puts -nonewline \"% \"\n"
1733 "}\n"
1734 "flush stdout\n"
1735 "append line [gets stdin]\n"
1736 "if {[info complete $line]} {\n"
1737 "if {[catch {uplevel #0 $line} result]} {\n"
1738 "puts stderr \"Error: $result\"\n"
1739 "} elseif {$result!=\"\"} {\n"
1740 "puts $result\n"
1741 "}\n"
1742 "set line {}\n"
1743 "} else {\n"
1744 "append line \\n\n"
1745 "}\n"
1746 "}\n"
1747;
drh3e27c022004-07-23 00:01:38 +00001748#endif
1749
1750/*
1751** If the macro TCLSH is two, then get the main loop code out of
1752** the separate file "spaceanal_tcl.h".
1753*/
1754#if TCLSH==2
1755static char zMainloop[] =
1756#include "spaceanal_tcl.h"
1757;
1758#endif
drh348784e2000-05-29 20:41:49 +00001759
1760#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1761int TCLSH_MAIN(int argc, char **argv){
1762 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001763 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001764 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001765 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001766#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001767 {
1768 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001769 extern int Sqlitetest2_Init(Tcl_Interp*);
1770 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001771 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001772 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001773 extern int Md5_Init(Tcl_Interp*);
drh2e66f0b2005-04-28 17:18:48 +00001774 extern int Sqlitetestsse_Init(Tcl_Interp*);
1775
danielk19776490beb2004-05-11 06:17:21 +00001776 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001777 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001778 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001779 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001780 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001781 Md5_Init(interp);
drh89dec812005-04-28 19:03:37 +00001782#ifdef SQLITE_SSE
drh2e66f0b2005-04-28 17:18:48 +00001783 Sqlitetestsse_Init(interp);
1784#endif
drhd1bf3512001-04-07 15:24:33 +00001785 }
1786#endif
drh3e27c022004-07-23 00:01:38 +00001787 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001788 int i;
1789 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1790 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
drh61212b62004-12-02 20:17:00 +00001791 for(i=3-TCLSH; i<argc; i++){
drh348784e2000-05-29 20:41:49 +00001792 Tcl_SetVar(interp, "argv", argv[i],
1793 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1794 }
drh3e27c022004-07-23 00:01:38 +00001795 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001796 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001797 if( zInfo==0 ) zInfo = interp->result;
1798 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001799 return 1;
1800 }
drh3e27c022004-07-23 00:01:38 +00001801 }
1802 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001803 Tcl_GlobalEval(interp, zMainloop);
1804 }
1805 return 0;
1806}
1807#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001808
1809#endif /* !defined(NO_TCL) */