1 /* Jim Tcl version of the sqlite3 Tcl binding.
4 * This version is (c) Steve Bennett <steveb@workware.net.au>
5 * Copyright of the original version is below.
11 ** The author disclaims copyright to this source code. In place of
12 ** a legal notice, here is a blessing:
14 ** May you do good and not evil.
15 ** May you find forgiveness for yourself and forgive others.
16 ** May you share freely, never taking more than you give.
18 *************************************************************************
19 ** A TCL Interface to SQLite. Append this file to sqlite3.c and
20 ** compile the whole thing to build a TCL-enabled version of SQLite.
22 ** Compile-time options:
24 ** -D SQLITE_TEST When used in conjuction with -DTCLSH=1, add
25 ** hundreds of new commands used for testing
26 ** SQLite. This option implies -DSQLITE_TCLMD5.
29 #include <jim-config.h>
30 #include <jim-eventloop.h>
34 ** Some additional include files are needed if this file is not
35 ** appended to the amalgamation.
37 #ifndef SQLITE_AMALGAMATION
42 typedef unsigned char u8
;
46 #define NUM_PREPARED_STMTS 10
47 #define MAX_PREPARED_STMTS 100
50 ** If Jim Tcl uses UTF-8 and SQLite is configured to use iso8859, then we
55 ** have to do a translation when going between the two. Set the
56 ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
59 #if defined(JIM_UTF8) && !defined(SQLITE_UTF8)
60 # define UTF_TRANSLATION_NEEDED 1
61 # warning Jim Tcl can not translate encoding from iso8859 to utf-8
65 ** New SQL functions can be created as TCL scripts. Each such function
66 ** is described by an instance of the following structure.
68 typedef struct SqlFunc SqlFunc
;
70 Jim_Interp
*interp
; /* The TCL interpret to execute the function */
71 Jim_Obj
*pScript
; /* The Jim_Obj representation of the script */
72 int useEvalObjv
; /* True if it is safe to use Jim_EvalObjv */
73 char *zName
; /* Name of this function */
74 SqlFunc
*pNext
; /* Next function on the list of them all */
78 ** New collation sequences function can be created as TCL scripts. Each such
79 ** function is described by an instance of the following structure.
81 typedef struct SqlCollate SqlCollate
;
83 Jim_Interp
*interp
; /* The TCL interpret to execute the function */
84 char *zScript
; /* The script to be run */
85 SqlCollate
*pNext
; /* Next function on the list of them all */
89 ** Prepared statements are cached for faster execution. Each prepared
90 ** statement is described by an instance of the following structure.
92 typedef struct SqlPreparedStmt SqlPreparedStmt
;
93 struct SqlPreparedStmt
{
94 SqlPreparedStmt
*pNext
; /* Next in linked list */
95 SqlPreparedStmt
*pPrev
; /* Previous on the list */
96 sqlite3_stmt
*pStmt
; /* The prepared statement */
97 int nSql
; /* chars in zSql[] */
98 const char *zSql
; /* Text of the SQL statement */
99 int nParm
; /* Size of apParm array */
100 Jim_Obj
**apParm
; /* Array of referenced object pointers */
103 typedef struct IncrblobChannel IncrblobChannel
;
106 ** There is one instance of this structure for each SQLite database
107 ** that has been opened by the SQLite TCL interface.
109 typedef struct SqliteDb SqliteDb
;
111 sqlite3
*db
; /* The "real" database structure. MUST BE FIRST */
112 Jim_Interp
*interp
; /* The interpreter used for this database */
113 char *zBusy
; /* The busy callback routine */
114 char *zCommit
; /* The commit hook callback routine */
115 char *zTrace
; /* The trace callback routine */
116 char *zProfile
; /* The profile callback routine */
117 char *zProgress
; /* The progress callback routine */
118 char *zAuth
; /* The authorization callback routine */
119 int disableAuth
; /* Disable the authorizer if it exists */
120 char *zNull
; /* Text to substitute for an SQL NULL value */
121 SqlFunc
*pFunc
; /* List of SQL functions */
122 Jim_Obj
*pUpdateHook
; /* Update hook script (if any) */
123 Jim_Obj
*pRollbackHook
; /* Rollback hook script (if any) */
124 Jim_Obj
*pUnlockNotify
; /* Unlock notify script (if any) */
125 SqlCollate
*pCollate
; /* List of SQL collation functions */
126 int rc
; /* Return code of most recent sqlite3_exec() */
127 Jim_Obj
*pCollateNeeded
; /* Collation needed script */
128 SqlPreparedStmt
*stmtList
; /* List of prepared statements*/
129 SqlPreparedStmt
*stmtLast
; /* Last statement in the list */
130 int maxStmt
; /* The next maximum number of stmtList */
131 int nStmt
; /* Number of statements in stmtList */
132 IncrblobChannel
*pIncrblob
;/* Linked list of open incrblob channels */
133 int nStep
, nSort
; /* Statistics for most recent operation */
134 int nTransaction
; /* Number of nested [transaction] methods */
137 struct IncrblobChannel
{
138 sqlite3_blob
*pBlob
; /* sqlite3 blob handle */
139 SqliteDb
*pDb
; /* Associated database connection */
140 int iSeek
; /* Current seek offset */
141 Jim_Obj
*channel
; /* Channel identifier */
142 IncrblobChannel
*pNext
; /* Linked list of all open incrblob channels */
143 IncrblobChannel
*pPrev
; /* Linked list of all open incrblob channels */
147 ** Compute a string length that is limited to what can be stored in
148 ** lower 30 bits of a 32-bit signed integer.
150 static int strlen30(const char *z
){
152 while( *z2
){ z2
++; }
153 return 0x3fffffff & (int)(z2
- z
);
157 #ifndef SQLITE_OMIT_INCRBLOB
159 ** Close all incrblob channels opened using database connection pDb.
160 ** This is called when shutting down the database connection.
162 static void closeIncrblobChannels(SqliteDb
*pDb
){
164 IncrblobChannel
*pNext
;
166 for(p
=pDb
->pIncrblob
; p
; p
=pNext
){
169 /* Note: Calling unregister here call Jim_Close on the incrblob channel,
170 ** which deletes the IncrblobChannel structure at *p. So do not
171 ** call Jim_Free() here.
173 Jim_UnregisterChannel(pDb
->interp
, p
->channel
);
178 ** Close an incremental blob channel.
180 static int incrblobClose(ClientData instanceData
, Jim_Interp
*interp
){
181 IncrblobChannel
*p
= (IncrblobChannel
*)instanceData
;
182 int rc
= sqlite3_blob_close(p
->pBlob
);
183 sqlite3
*db
= p
->pDb
->db
;
185 /* Remove the channel from the SqliteDb.pIncrblob list. */
187 p
->pNext
->pPrev
= p
->pPrev
;
190 p
->pPrev
->pNext
= p
->pNext
;
192 if( p
->pDb
->pIncrblob
==p
){
193 p
->pDb
->pIncrblob
= p
->pNext
;
196 /* Free the IncrblobChannel structure */
200 Jim_SetResult(interp
, (char *)sqlite3_errmsg(db
), JIM_VOLATILE
);
207 ** Read data from an incremental blob channel.
209 static int incrblobInput(
210 ClientData instanceData
,
215 IncrblobChannel
*p
= (IncrblobChannel
*)instanceData
;
216 int nRead
= bufSize
; /* Number of bytes to read */
217 int nBlob
; /* Total size of the blob */
218 int rc
; /* sqlite error code */
220 nBlob
= sqlite3_blob_bytes(p
->pBlob
);
221 if( (p
->iSeek
+nRead
)>nBlob
){
222 nRead
= nBlob
-p
->iSeek
;
228 rc
= sqlite3_blob_read(p
->pBlob
, (void *)buf
, nRead
, p
->iSeek
);
239 ** Write data to an incremental blob channel.
241 static int incrblobOutput(
242 ClientData instanceData
,
247 IncrblobChannel
*p
= (IncrblobChannel
*)instanceData
;
248 int nWrite
= toWrite
; /* Number of bytes to write */
249 int nBlob
; /* Total size of the blob */
250 int rc
; /* sqlite error code */
252 nBlob
= sqlite3_blob_bytes(p
->pBlob
);
253 if( (p
->iSeek
+nWrite
)>nBlob
){
254 *errorCodePtr
= EINVAL
;
261 rc
= sqlite3_blob_write(p
->pBlob
, (void *)buf
, nWrite
, p
->iSeek
);
272 ** Seek an incremental blob channel.
274 static int incrblobSeek(
275 ClientData instanceData
,
280 IncrblobChannel
*p
= (IncrblobChannel
*)instanceData
;
290 p
->iSeek
= sqlite3_blob_bytes(p
->pBlob
) + offset
;
293 default: assert(!"Bad seekMode");
300 static void incrblobWatch(ClientData instanceData
, int mode
){
303 static int incrblobHandle(ClientData instanceData
, int dir
, ClientData
*hPtr
){
307 static Jim_ChannelType IncrblobChannelType
= {
308 "incrblob", /* typeName */
309 JIM_CHANNEL_VERSION_2
, /* version */
310 incrblobClose
, /* closeProc */
311 incrblobInput
, /* inputProc */
312 incrblobOutput
, /* outputProc */
313 incrblobSeek
, /* seekProc */
314 0, /* setOptionProc */
315 0, /* getOptionProc */
316 incrblobWatch
, /* watchProc (this is a no-op) */
317 incrblobHandle
, /* getHandleProc (always returns error) */
319 0, /* blockModeProc */
322 0, /* wideSeekProc */
326 ** Create a new incrblob channel.
328 static int createIncrblobChannel(
338 sqlite3
*db
= pDb
->db
;
341 int flags
= JIM_READABLE
|(isReadonly
? 0 : JIM_WRITABLE
);
343 /* This variable is used to name the channels: "incrblob_[incr count]" */
344 static int count
= 0;
347 rc
= sqlite3_blob_open(db
, zDb
, zTable
, zColumn
, iRow
, !isReadonly
, &pBlob
);
349 Jim_SetResult(interp
, (char *)sqlite3_errmsg(pDb
->db
), JIM_VOLATILE
);
353 p
= (IncrblobChannel
*)Jim_Alloc(sizeof(IncrblobChannel
));
357 sqlite3_snprintf(sizeof(zChannel
), zChannel
, "incrblob_%d", ++count
);
358 p
->channel
= Jim_CreateChannel(&IncrblobChannelType
, zChannel
, p
, flags
);
359 Jim_RegisterChannel(interp
, p
->channel
);
361 /* Link the new channel into the SqliteDb.pIncrblob list. */
362 p
->pNext
= pDb
->pIncrblob
;
370 Jim_SetResult(interp
, (char *)Jim_GetChannelName(p
->channel
), JIM_VOLATILE
);
373 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */
374 #define closeIncrblobChannels(pDb)
378 ** Look at the script prefix in pCmd. We will be executing this script
379 ** after first appending one or more arguments. This routine analyzes
380 ** the script to see if it is safe to use Jim_EvalObjv() on the script
381 ** rather than the more general Jim_EvalEx(). Jim_EvalObjv() is much
384 ** Scripts that are safe to use with Jim_EvalObjv() consists of a
385 ** command name followed by zero or more arguments with no [...] or $
386 ** or {...} or ; to be seen anywhere. Most callback scripts consist
387 ** of just a single procedure name and they meet this requirement.
389 static int safeToUseEvalObjv(Jim_Interp
*interp
, Jim_Obj
*pCmd
){
390 /* We could try to do something with Jim_Parse(). But we will instead
391 ** just do a search for forbidden characters. If any of the forbidden
392 ** characters appear in pCmd, we will report the string as unsafe.
396 z
= Jim_GetString(pCmd
, &n
);
399 if( c
=='$' || c
=='[' || c
==';' ) return 0;
405 ** Find an SqlFunc structure with the given name. Or create a new
406 ** one if an existing one cannot be found. Return a pointer to the
409 static SqlFunc
*findSqlFunc(SqliteDb
*pDb
, const char *zName
){
412 pNew
= (SqlFunc
*)Jim_Alloc( sizeof(*pNew
) + strlen30(zName
) + 1 );
413 pNew
->zName
= (char*)&pNew
[1];
414 for(i
=0; zName
[i
]; i
++){ pNew
->zName
[i
] = tolower((unsigned)zName
[i
]); }
416 for(p
=pDb
->pFunc
; p
; p
=p
->pNext
){
417 if( strcmp(p
->zName
, pNew
->zName
)==0 ){
418 Jim_Free((char*)pNew
);
422 pNew
->interp
= pDb
->interp
;
424 pNew
->pNext
= pDb
->pFunc
;
430 ** Finalize and free a list of prepared statements
432 static void flushStmtCache( SqliteDb
*pDb
){
433 SqlPreparedStmt
*pPreStmt
;
435 while( pDb
->stmtList
){
436 sqlite3_finalize( pDb
->stmtList
->pStmt
);
437 pPreStmt
= pDb
->stmtList
;
438 pDb
->stmtList
= pDb
->stmtList
->pNext
;
439 Jim_Free( (char*)pPreStmt
);
446 ** TCL calls this procedure when an sqlite3 database command is
449 static void DbDeleteCmd(Jim_Interp
*interp
, void *db
){
450 SqliteDb
*pDb
= (SqliteDb
*)db
;
452 closeIncrblobChannels(pDb
);
453 sqlite3_close(pDb
->db
);
455 SqlFunc
*pFunc
= pDb
->pFunc
;
456 pDb
->pFunc
= pFunc
->pNext
;
457 Jim_DecrRefCount(interp
, pFunc
->pScript
);
458 Jim_Free((char*)pFunc
);
460 while( pDb
->pCollate
){
461 SqlCollate
*pCollate
= pDb
->pCollate
;
462 pDb
->pCollate
= pCollate
->pNext
;
463 Jim_Free((char*)pCollate
);
466 Jim_Free(pDb
->zBusy
);
469 Jim_Free(pDb
->zTrace
);
472 Jim_Free(pDb
->zProfile
);
475 Jim_Free(pDb
->zAuth
);
478 Jim_Free(pDb
->zNull
);
480 if( pDb
->pUpdateHook
){
481 Jim_DecrRefCount(interp
, pDb
->pUpdateHook
);
483 if( pDb
->pRollbackHook
){
484 Jim_DecrRefCount(interp
, pDb
->pRollbackHook
);
486 if( pDb
->pCollateNeeded
){
487 Jim_DecrRefCount(interp
, pDb
->pCollateNeeded
);
489 Jim_Free((char*)pDb
);
493 ** This routine is called when a database file is locked while trying
496 static int DbBusyHandler(void *cd
, int nTries
){
497 SqliteDb
*pDb
= (SqliteDb
*)cd
;
502 sqlite3_snprintf(sizeof(zVal
), zVal
, "%d", nTries
);
504 objPtr
= Jim_NewStringObj(pDb
->interp
, pDb
->zBusy
, -1);
505 Jim_AppendStrings(pDb
->interp
, objPtr
, " ", zVal
, NULL
);
506 rc
= Jim_EvalObj(pDb
->interp
, objPtr
);
507 if( rc
!=JIM_OK
|| atoi(Jim_String(Jim_GetResult(pDb
->interp
))) ){
513 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
515 ** This routine is invoked as the 'progress callback' for the database.
517 static int DbProgressHandler(void *cd
){
518 SqliteDb
*pDb
= (SqliteDb
*)cd
;
521 assert( pDb
->zProgress
);
522 rc
= Jim_Eval(pDb
->interp
, pDb
->zProgress
);
523 if( rc
!=JIM_OK
|| atoi(Jim_String(Jim_GetResult(pDb
->interp
))) ){
530 #ifndef SQLITE_OMIT_TRACE
532 ** This routine is called by the SQLite trace handler whenever a new
533 ** block of SQL is executed. The TCL script in pDb->zTrace is executed.
535 static void DbTraceHandler(void *cd
, const char *zSql
){
536 SqliteDb
*pDb
= (SqliteDb
*)cd
;
538 Jim_Obj
*str
= Jim_NewStringObj(pDb
->interp
, pDb
->zTrace
, -1);
539 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zSql
, -1));
540 Jim_Eval(pDb
->interp
, zSql
);
541 Jim_SetEmptyResult(pDb
->interp
);
545 #ifndef SQLITE_OMIT_TRACE
547 ** This routine is called by the SQLite profile handler after a statement
548 ** SQL has executed. The TCL script in pDb->zProfile is evaluated.
550 static void DbProfileHandler(void *cd
, const char *zSql
, sqlite_uint64 tm
){
551 SqliteDb
*pDb
= (SqliteDb
*)cd
;
555 sqlite3_snprintf(sizeof(zTm
)-1, zTm
, "%lld", tm
);
556 str
= Jim_NewStringObj(pDb
->interp
, pDb
->zProfile
, -1);
557 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zSql
, -1));
558 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zTm
, -1));
559 Jim_EvalObj(pDb
->interp
, str
);
560 Jim_SetEmptyResult(pDb
->interp
);
565 ** This routine is called when a transaction is committed. The
566 ** TCL script in pDb->zCommit is executed. If it returns non-zero or
567 ** if it throws an exception, the transaction is rolled back instead
568 ** of being committed.
570 static int DbCommitHandler(void *cd
){
571 SqliteDb
*pDb
= (SqliteDb
*)cd
;
574 rc
= Jim_Eval(pDb
->interp
, pDb
->zCommit
);
575 if( rc
!=JIM_OK
|| atoi(Jim_String(Jim_GetResult(pDb
->interp
))) ){
581 static void DbRollbackHandler(void *clientData
){
582 SqliteDb
*pDb
= (SqliteDb
*)clientData
;
583 assert(pDb
->pRollbackHook
);
584 Jim_EvalObjBackground(pDb
->interp
, pDb
->pRollbackHook
);
587 #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
588 static void setTestUnlockNotifyVars(Jim_Interp
*interp
, int iArg
, int nArg
){
590 sprintf(zBuf
, "%d", iArg
);
591 Jim_SetVar(interp
, "sqlite_unlock_notify_arg", zBuf
, JIM_GLOBAL_ONLY
);
592 sprintf(zBuf
, "%d", nArg
);
593 Jim_SetVar(interp
, "sqlite_unlock_notify_argcount", zBuf
, JIM_GLOBAL_ONLY
);
596 # define setTestUnlockNotifyVars(x,y,z)
599 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
600 static void DbUnlockNotify(void **apArg
, int nArg
){
602 for(i
=0; i
<nArg
; i
++){
603 const int flags
= (JIM_EVAL_GLOBAL
|JIM_EVAL_DIRECT
);
604 SqliteDb
*pDb
= (SqliteDb
*)apArg
[i
];
605 setTestUnlockNotifyVars(pDb
->interp
, i
, nArg
);
606 assert( pDb
->pUnlockNotify
);
607 Jim_EvalObjEx(pDb
->interp
, pDb
->pUnlockNotify
, flags
);
608 Jim_DecrRefCount(interp
, pDb
->pUnlockNotify
);
609 pDb
->pUnlockNotify
= 0;
614 static void DbUpdateHandler(
621 SqliteDb
*pDb
= (SqliteDb
*)p
;
624 assert( pDb
->pUpdateHook
);
625 assert( op
==SQLITE_INSERT
|| op
==SQLITE_UPDATE
|| op
==SQLITE_DELETE
);
627 pCmd
= Jim_DuplicateObj(pDb
->interp
, pDb
->pUpdateHook
);
628 Jim_IncrRefCount(pCmd
);
629 Jim_ListAppendElement(0, pCmd
, Jim_NewStringObj(pDb
->interp
,
630 ( (op
==SQLITE_INSERT
)?"INSERT":(op
==SQLITE_UPDATE
)?"UPDATE":"DELETE"), -1));
631 Jim_ListAppendElement(pDb
->interp
, pCmd
, Jim_NewStringObj(pDb
->interp
, zDb
, -1));
632 Jim_ListAppendElement(pDb
->interp
, pCmd
, Jim_NewStringObj(pDb
->interp
, zTbl
, -1));
633 Jim_ListAppendElement(pDb
->interp
, pCmd
, Jim_NewIntObj(pDb
->interp
, rowid
));
634 Jim_EvalObj(pDb
->interp
, pCmd
);
637 static void tclCollateNeeded(
643 SqliteDb
*pDb
= (SqliteDb
*)pCtx
;
644 Jim_Obj
*pScript
= Jim_DuplicateObj(pDb
->interp
, pDb
->pCollateNeeded
);
645 //Jim_IncrRefCount(pScript);
646 Jim_ListAppendElement(pDb
->interp
, pScript
, Jim_NewStringObj(pDb
->interp
, zName
, -1));
647 Jim_EvalObj(pDb
->interp
, pScript
);
648 //Jim_DecrRefCount(pDb->interp, pScript);
652 ** This routine is called to evaluate an SQL collation function implemented
655 static int tclSqlCollate(
662 SqlCollate
*p
= (SqlCollate
*)pCtx
;
665 pCmd
= Jim_NewStringObj(p
->interp
, p
->zScript
, -1);
666 //Jim_IncrRefCount(pCmd);
667 Jim_ListAppendElement(p
->interp
, pCmd
, Jim_NewStringObj(p
->interp
, zA
, nA
));
668 Jim_ListAppendElement(p
->interp
, pCmd
, Jim_NewStringObj(p
->interp
, zB
, nB
));
669 Jim_EvalObj(p
->interp
, pCmd
);
670 //Jim_DecrRefCount(interp, pCmd);
671 return (atoi(Jim_String(Jim_GetResult(p
->interp
))));
675 ** This routine is called to evaluate an SQL function implemented
678 static void tclSqlFunc(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
){
679 SqlFunc
*p
= sqlite3_user_data(context
);
685 /* If there are no arguments to the function, call Jim_EvalObjEx on the
686 ** script object directly. This allows the TCL compiler to generate
687 ** bytecode for the command on the first invocation and thus make
688 ** subsequent invocations much faster. */
690 //Jim_IncrRefCount(pCmd);
691 rc
= Jim_EvalObj(p
->interp
, pCmd
);
692 //Jim_DecrRefCount(interp, pCmd);
694 /* If there are arguments to the function, make a shallow copy of the
695 ** script object, lappend the arguments, then evaluate the copy.
697 ** By "shallow" copy, we mean a only the outer list Jim_Obj is duplicated.
698 ** The new Jim_Obj contains pointers to the original list elements.
699 ** That way, when Jim_EvalObjv() is run and shimmers the first element
700 ** of the list to tclCmdNameType, that alternate representation will
701 ** be preserved and reused on the next invocation.
703 pCmd
= Jim_DuplicateObj(p
->interp
, p
->pScript
);
704 Jim_IncrRefCount(pCmd
);
705 for(i
=0; i
<argc
; i
++){
706 sqlite3_value
*pIn
= argv
[i
];
709 /* Set pVal to contain the i'th column of this row. */
710 switch( sqlite3_value_type(pIn
) ){
712 int bytes
= sqlite3_value_bytes(pIn
);
713 pVal
= Jim_NewStringObj(p
->interp
, sqlite3_value_blob(pIn
), bytes
);
716 case SQLITE_INTEGER
: {
717 sqlite_int64 v
= sqlite3_value_int64(pIn
);
718 pVal
= Jim_NewIntObj(p
->interp
, v
);
722 double r
= sqlite3_value_double(pIn
);
723 pVal
= Jim_NewDoubleObj(p
->interp
, r
);
727 pVal
= Jim_NewStringObj(p
->interp
, "", 0);
731 int bytes
= sqlite3_value_bytes(pIn
);
732 pVal
= Jim_NewStringObj(p
->interp
, (char *)sqlite3_value_text(pIn
), bytes
);
736 Jim_ListAppendElement(p
->interp
, pCmd
, pVal
);
738 if( !p
->useEvalObjv
){
739 /* Jim_EvalOb() will automatically call Jim_EvalObjVector() if pCmd
740 ** is a list without a string representation. To prevent this from
741 ** happening, make sure pCmd has a valid string representation */
744 rc
= Jim_EvalObj(p
->interp
, pCmd
);
745 Jim_DecrRefCount(p
->interp
, pCmd
);
748 if( rc
&& rc
!=JIM_RETURN
){
749 sqlite3_result_error(context
, Jim_String(Jim_GetResult(p
->interp
)), -1);
751 Jim_Obj
*pVar
= Jim_GetResult(p
->interp
);
754 /* XXX: Jim Tcl doesn't have bytearray or boolean */
755 const char *zType
= (pVar
->typePtr
? pVar
->typePtr
->name
: "");
758 if( c
=='b' && strcmp(zType
,"bytearray")==0 && pVar
->bytes
==0 ){
759 /* Only return a BLOB type if the Tcl variable is a bytearray and
760 ** has no string representation. */
761 data
= Jim_GetByteArrayFromObj(pVar
, &n
);
762 sqlite3_result_blob(context
, data
, n
, SQLITE_TRANSIENT
);
763 }else if( c
=='b' && strcmp(zType
,"boolean")==0 ){
764 Jim_GetWide(0, pVar
, &n
);
765 sqlite3_result_int(context
, n
);
768 if( c
=='d' && strcmp(zType
,"double")==0 ){
770 Jim_GetDouble(0, pVar
, &r
);
771 sqlite3_result_double(context
, r
);
772 /* XXX: Is a cooerced double better as a double or an int? */
773 }else if( (c
=='c' && strcmp(zType
,"coerced-double")==0) ||
774 (c
=='i' && strcmp(zType
,"int")==0) ){
776 Jim_GetWide(p
->interp
, pVar
, &v
);
777 sqlite3_result_int64(context
, v
);
779 data
= (unsigned char *)Jim_GetString(pVar
, &n
);
780 sqlite3_result_text(context
, (char *)data
, n
, SQLITE_TRANSIENT
);
785 #ifndef SQLITE_OMIT_AUTHORIZATION
787 ** This is the authentication function. It appends the authentication
788 ** type code and the two arguments to zCmd[] then invokes the result
789 ** on the interpreter. The reply is examined to determine if the
790 ** authentication fails or succeeds.
792 static int auth_callback(
804 SqliteDb
*pDb
= (SqliteDb
*)pArg
;
805 if( pDb
->disableAuth
) return SQLITE_OK
;
808 case SQLITE_COPY
: zCode
="SQLITE_COPY"; break;
809 case SQLITE_CREATE_INDEX
: zCode
="SQLITE_CREATE_INDEX"; break;
810 case SQLITE_CREATE_TABLE
: zCode
="SQLITE_CREATE_TABLE"; break;
811 case SQLITE_CREATE_TEMP_INDEX
: zCode
="SQLITE_CREATE_TEMP_INDEX"; break;
812 case SQLITE_CREATE_TEMP_TABLE
: zCode
="SQLITE_CREATE_TEMP_TABLE"; break;
813 case SQLITE_CREATE_TEMP_TRIGGER
: zCode
="SQLITE_CREATE_TEMP_TRIGGER"; break;
814 case SQLITE_CREATE_TEMP_VIEW
: zCode
="SQLITE_CREATE_TEMP_VIEW"; break;
815 case SQLITE_CREATE_TRIGGER
: zCode
="SQLITE_CREATE_TRIGGER"; break;
816 case SQLITE_CREATE_VIEW
: zCode
="SQLITE_CREATE_VIEW"; break;
817 case SQLITE_DELETE
: zCode
="SQLITE_DELETE"; break;
818 case SQLITE_DROP_INDEX
: zCode
="SQLITE_DROP_INDEX"; break;
819 case SQLITE_DROP_TABLE
: zCode
="SQLITE_DROP_TABLE"; break;
820 case SQLITE_DROP_TEMP_INDEX
: zCode
="SQLITE_DROP_TEMP_INDEX"; break;
821 case SQLITE_DROP_TEMP_TABLE
: zCode
="SQLITE_DROP_TEMP_TABLE"; break;
822 case SQLITE_DROP_TEMP_TRIGGER
: zCode
="SQLITE_DROP_TEMP_TRIGGER"; break;
823 case SQLITE_DROP_TEMP_VIEW
: zCode
="SQLITE_DROP_TEMP_VIEW"; break;
824 case SQLITE_DROP_TRIGGER
: zCode
="SQLITE_DROP_TRIGGER"; break;
825 case SQLITE_DROP_VIEW
: zCode
="SQLITE_DROP_VIEW"; break;
826 case SQLITE_INSERT
: zCode
="SQLITE_INSERT"; break;
827 case SQLITE_PRAGMA
: zCode
="SQLITE_PRAGMA"; break;
828 case SQLITE_READ
: zCode
="SQLITE_READ"; break;
829 case SQLITE_SELECT
: zCode
="SQLITE_SELECT"; break;
830 case SQLITE_TRANSACTION
: zCode
="SQLITE_TRANSACTION"; break;
831 case SQLITE_UPDATE
: zCode
="SQLITE_UPDATE"; break;
832 case SQLITE_ATTACH
: zCode
="SQLITE_ATTACH"; break;
833 case SQLITE_DETACH
: zCode
="SQLITE_DETACH"; break;
834 case SQLITE_ALTER_TABLE
: zCode
="SQLITE_ALTER_TABLE"; break;
835 case SQLITE_REINDEX
: zCode
="SQLITE_REINDEX"; break;
836 case SQLITE_ANALYZE
: zCode
="SQLITE_ANALYZE"; break;
837 case SQLITE_CREATE_VTABLE
: zCode
="SQLITE_CREATE_VTABLE"; break;
838 case SQLITE_DROP_VTABLE
: zCode
="SQLITE_DROP_VTABLE"; break;
839 case SQLITE_FUNCTION
: zCode
="SQLITE_FUNCTION"; break;
840 case SQLITE_SAVEPOINT
: zCode
="SQLITE_SAVEPOINT"; break;
841 default : zCode
="????"; break;
843 str
= Jim_NewStringObj(pDb
->interp
, pDb
->zAuth
, -1);
844 /* XXX: list or string here? */
845 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zCode
, -1));
847 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zArg1
, -1));
850 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zArg2
, -1));
853 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zArg3
, -1));
856 Jim_ListAppendElement(pDb
->interp
, str
, Jim_NewStringObj(pDb
->interp
, zArg4
, -1));
858 Jim_IncrRefCount(str
);
859 rc
= Jim_EvalGlobal(pDb
->interp
, Jim_String(str
));
860 Jim_DecrRefCount(pDb
->interp
, str
);
861 zReply
= Jim_String(Jim_GetResult(pDb
->interp
));
862 if( strcmp(zReply
,"SQLITE_OK")==0 ){
864 }else if( strcmp(zReply
,"SQLITE_DENY")==0 ){
866 }else if( strcmp(zReply
,"SQLITE_IGNORE")==0 ){
873 #endif /* SQLITE_OMIT_AUTHORIZATION */
876 ** Note that Jim Tcl can't do encoding conversion,
877 ** so this simply returns the string as an object.
879 static Jim_Obj
*dbTextToObj(Jim_Interp
*interp
, char const *zText
){
880 return Jim_NewStringObj(interp
, zText
? zText
: "", -1);
884 ** This routine reads a line of text from FILE in, stores
885 ** the text in memory obtained from malloc() and returns a pointer
886 ** to the text. NULL is returned at end of file.
888 ** The interface is like "readline" but no command-line editing
891 ** copied from shell.c from '.import' command
893 static char *local_getline(char *zPrompt
, FILE *in
){
900 zLine
= Jim_Alloc( nLine
);
905 nLine
= nLine
*2 + 100;
906 zLine
= Jim_Realloc(zLine
, nLine
);
907 if( zLine
==0 ) return 0;
909 if( fgets(&zLine
[n
], nLine
- n
, in
)==0 ){
918 while( zLine
[n
] ){ n
++; }
919 if( n
>0 && zLine
[n
-1]=='\n' ){
925 zLine
= Jim_Realloc( zLine
, n
+1 );
931 ** This function is part of the implementation of the command:
933 ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT
935 ** It is invoked after evaluating the script SCRIPT to commit or rollback
936 ** the transaction or savepoint opened by the [transaction] command.
938 static int DbTransPostCmd(
939 Jim_Interp
*interp
, /* Tcl interpreter */
941 int result
/* Result of evaluating SCRIPT */
943 static const char *azEnd
[] = {
944 "RELEASE _tcl_transaction", /* rc==JIM_ERR, nTransaction!=0 */
945 "COMMIT", /* rc!=JIM_ERR, nTransaction==0 */
946 "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction",
947 "ROLLBACK" /* rc==JIM_ERR, nTransaction==0 */
953 zEnd
= azEnd
[(rc
==JIM_ERR
)*2 + (pDb
->nTransaction
==0)];
956 if( sqlite3_exec(pDb
->db
, zEnd
, 0, 0, 0) ){
957 /* This is a tricky scenario to handle. The most likely cause of an
958 ** error is that the exec() above was an attempt to commit the
959 ** top-level transaction that returned SQLITE_BUSY. Or, less likely,
960 ** that an IO-error has occured. In either case, throw a Tcl exception
961 ** and try to rollback the transaction.
963 ** But it could also be that the user executed one or more BEGIN,
964 ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing
965 ** this method's logic. Not clear how this would be best handled.
968 Jim_AppendString(interp
, Jim_GetResult(interp
), sqlite3_errmsg(pDb
->db
), -1);
971 sqlite3_exec(pDb
->db
, "ROLLBACK", 0, 0, 0);
979 ** Search the cache for a prepared-statement object that implements the
980 ** first SQL statement in the buffer pointed to by parameter zIn. If
981 ** no such prepared-statement can be found, allocate and prepare a new
982 ** one. In either case, bind the current values of the relevant Tcl
983 ** variables to any $var, :var or @var variables in the statement. Before
984 ** returning, set *ppPreStmt to point to the prepared-statement object.
986 ** Output parameter *pzOut is set to point to the next SQL statement in
987 ** buffer zIn, or to the '\0' byte at the end of zIn if there is no
990 ** If successful, JIM_OK is returned. Otherwise, JIM_ERR is returned
991 ** and an error message loaded into interpreter pDb->interp.
993 static int dbPrepareAndBind(
994 SqliteDb
*pDb
, /* Database object */
995 char const *zIn
, /* SQL to compile */
996 char const **pzOut
, /* OUT: Pointer to next SQL statement */
997 SqlPreparedStmt
**ppPreStmt
/* OUT: Object used to cache statement */
999 const char *zSql
= zIn
; /* Pointer to first SQL statement in zIn */
1000 sqlite3_stmt
*pStmt
; /* Prepared statement object */
1001 SqlPreparedStmt
*pPreStmt
; /* Pointer to cached statement */
1002 int nSql
; /* Length of zSql in bytes */
1003 int nVar
; /* Number of variables in statement */
1004 int iParm
= 0; /* Next free entry in apParm */
1006 Jim_Interp
*interp
= pDb
->interp
;
1010 /* Trim spaces from the start of zSql and calculate the remaining length. */
1011 while( isspace((unsigned)zSql
[0]) ){ zSql
++; }
1012 nSql
= strlen30(zSql
);
1014 for(pPreStmt
= pDb
->stmtList
; pPreStmt
; pPreStmt
=pPreStmt
->pNext
){
1015 int n
= pPreStmt
->nSql
;
1017 && memcmp(pPreStmt
->zSql
, zSql
, n
)==0
1018 && (zSql
[n
]==0 || zSql
[n
-1]==';')
1020 pStmt
= pPreStmt
->pStmt
;
1021 *pzOut
= &zSql
[pPreStmt
->nSql
];
1023 /* When a prepared statement is found, unlink it from the
1024 ** cache list. It will later be added back to the beginning
1025 ** of the cache list in order to implement LRU replacement.
1027 if( pPreStmt
->pPrev
){
1028 pPreStmt
->pPrev
->pNext
= pPreStmt
->pNext
;
1030 pDb
->stmtList
= pPreStmt
->pNext
;
1032 if( pPreStmt
->pNext
){
1033 pPreStmt
->pNext
->pPrev
= pPreStmt
->pPrev
;
1035 pDb
->stmtLast
= pPreStmt
->pPrev
;
1038 nVar
= sqlite3_bind_parameter_count(pStmt
);
1043 /* If no prepared statement was found. Compile the SQL text. Also allocate
1044 ** a new SqlPreparedStmt structure. */
1048 if( SQLITE_OK
!=sqlite3_prepare_v2(pDb
->db
, zSql
, -1, &pStmt
, pzOut
) ){
1049 Jim_SetResult(interp
, dbTextToObj(pDb
->interp
, sqlite3_errmsg(pDb
->db
)));
1053 if( SQLITE_OK
!=sqlite3_errcode(pDb
->db
) ){
1054 /* A compile-time error in the statement. */
1055 Jim_SetResult(interp
, dbTextToObj(pDb
->interp
, sqlite3_errmsg(pDb
->db
)));
1058 /* The statement was a no-op. Continue to the next statement
1059 ** in the SQL string.
1065 assert( pPreStmt
==0 );
1066 nVar
= sqlite3_bind_parameter_count(pStmt
);
1067 nByte
= sizeof(SqlPreparedStmt
) + nVar
*sizeof(Jim_Obj
*);
1068 pPreStmt
= (SqlPreparedStmt
*)Jim_Alloc(nByte
);
1069 memset(pPreStmt
, 0, nByte
);
1071 pPreStmt
->pStmt
= pStmt
;
1072 pPreStmt
->nSql
= (*pzOut
- zSql
);
1073 pPreStmt
->zSql
= sqlite3_sql(pStmt
);
1074 pPreStmt
->apParm
= (Jim_Obj
**)&pPreStmt
[1];
1077 assert( strlen30(pPreStmt
->zSql
)==pPreStmt
->nSql
);
1078 assert( 0==memcmp(pPreStmt
->zSql
, zSql
, pPreStmt
->nSql
) );
1080 /* Bind values to parameters that begin with $ or : */
1081 for(i
=1; i
<=nVar
; i
++){
1082 const char *zVar
= sqlite3_bind_parameter_name(pStmt
, i
);
1083 if( zVar
!=0 && (zVar
[0]=='$' || zVar
[0]==':' || zVar
[0]=='@') ){
1084 Jim_Obj
*pVar
= Jim_GetVariableStr(interp
, &zVar
[1], 0);
1088 const char *zType
= (pVar
->typePtr
? pVar
->typePtr
->name
: "");
1090 /* XXX: Jim Tcl doesn't have bytearray or boolean */
1094 (c
=='b' && strcmp(zType
,"bytearray")==0 && pVar
->bytes
==0) ){
1095 /* Load a BLOB type if the Tcl variable is a bytearray and
1096 ** it has no string representation or the host
1097 ** parameter name begins with "@". */
1098 data
= Jim_GetByteArrayFromObj(pVar
, &n
);
1100 data
= (unsigned char *)Jim_GetString(pVar
, &n
);
1102 sqlite3_bind_blob(pStmt
, i
, data
, n
, SQLITE_STATIC
);
1103 Jim_IncrRefCount(pVar
);
1104 pPreStmt
->apParm
[iParm
++] = pVar
;
1106 }else if( c
=='b' && strcmp(zType
,"boolean")==0 ){
1107 Jim_GetWide(interp
, pVar
, &n
);
1108 sqlite3_bind_int(pStmt
, i
, n
);
1110 }else if( c
=='d' && strcmp(zType
,"double")==0 ){
1112 Jim_GetDouble(interp
, pVar
, &r
);
1113 sqlite3_bind_double(pStmt
, i
, r
);
1114 }else if( (c
=='c' && strcmp(zType
,"coerced-double")==0) ||
1115 (c
=='i' && strcmp(zType
,"int")==0) ){
1117 Jim_GetWide(interp
, pVar
, &v
);
1118 sqlite3_bind_int64(pStmt
, i
, v
);
1120 data
= (unsigned char *)Jim_GetString(pVar
, &n
);
1121 sqlite3_bind_text(pStmt
, i
, (char *)data
, n
, SQLITE_STATIC
);
1122 Jim_IncrRefCount(pVar
);
1123 pPreStmt
->apParm
[iParm
++] = pVar
;
1126 sqlite3_bind_null(pStmt
, i
);
1130 pPreStmt
->nParm
= iParm
;
1131 *ppPreStmt
= pPreStmt
;
1138 ** Release a statement reference obtained by calling dbPrepareAndBind().
1139 ** There should be exactly one call to this function for each call to
1140 ** dbPrepareAndBind().
1142 ** If the discard parameter is non-zero, then the statement is deleted
1143 ** immediately. Otherwise it is added to the LRU list and may be returned
1144 ** by a subsequent call to dbPrepareAndBind().
1146 static void dbReleaseStmt(
1147 SqliteDb
*pDb
, /* Database handle */
1148 SqlPreparedStmt
*pPreStmt
, /* Prepared statement handle to release */
1149 int discard
/* True to delete (not cache) the pPreStmt */
1153 /* Free the bound string and blob parameters */
1154 for(i
=0; i
<pPreStmt
->nParm
; i
++){
1155 Jim_DecrRefCount(pDb
->interp
, pPreStmt
->apParm
[i
]);
1157 pPreStmt
->nParm
= 0;
1159 if( pDb
->maxStmt
<=0 || discard
){
1160 /* If the cache is turned off, deallocated the statement */
1161 sqlite3_finalize(pPreStmt
->pStmt
);
1162 Jim_Free((char *)pPreStmt
);
1164 /* Add the prepared statement to the beginning of the cache list. */
1165 pPreStmt
->pNext
= pDb
->stmtList
;
1166 pPreStmt
->pPrev
= 0;
1167 if( pDb
->stmtList
){
1168 pDb
->stmtList
->pPrev
= pPreStmt
;
1170 pDb
->stmtList
= pPreStmt
;
1171 if( pDb
->stmtLast
==0 ){
1172 assert( pDb
->nStmt
==0 );
1173 pDb
->stmtLast
= pPreStmt
;
1175 assert( pDb
->nStmt
>0 );
1179 /* If we have too many statement in cache, remove the surplus from
1180 ** the end of the cache list. */
1181 while( pDb
->nStmt
>pDb
->maxStmt
){
1182 sqlite3_finalize(pDb
->stmtLast
->pStmt
);
1183 pDb
->stmtLast
= pDb
->stmtLast
->pPrev
;
1184 Jim_Free((char*)pDb
->stmtLast
->pNext
);
1185 pDb
->stmtLast
->pNext
= 0;
1192 ** Structure used with dbEvalXXX() functions:
1198 ** dbEvalColumnValue()
1200 typedef struct DbEvalContext DbEvalContext
;
1201 struct DbEvalContext
{
1202 SqliteDb
*pDb
; /* Database handle */
1203 Jim_Obj
*pSql
; /* Object holding string zSql */
1204 const char *zSql
; /* Remaining SQL to execute */
1205 SqlPreparedStmt
*pPreStmt
; /* Current statement */
1206 int nCol
; /* Number of columns returned by pStmt */
1207 Jim_Obj
*pArray
; /* Name of array variable */
1208 Jim_Obj
**apColName
; /* Array of column names */
1212 ** Release any cache of column names currently held as part of
1213 ** the DbEvalContext structure passed as the first argument.
1215 static void dbReleaseColumnNames(DbEvalContext
*p
){
1218 for(i
=0; i
<p
->nCol
; i
++){
1219 Jim_DecrRefCount(p
->pDb
->interp
, p
->apColName
[i
]);
1221 Jim_Free((char *)p
->apColName
);
1228 ** Initialize a DbEvalContext structure.
1230 ** If pArray is not NULL, then it contains the name of a Tcl array
1231 ** variable. The "*" member of this array is set to a list containing
1232 ** the names of the columns returned by the statement as part of each
1233 ** call to dbEvalStep(), in order from left to right. e.g. if the names
1234 ** of the returned columns are a, b and c, it does the equivalent of the
1237 ** set ${pArray}(*) {a b c}
1239 static void dbEvalInit(
1240 DbEvalContext
*p
, /* Pointer to structure to initialize */
1241 SqliteDb
*pDb
, /* Database handle */
1242 Jim_Obj
*pSql
, /* Object containing SQL script */
1243 Jim_Obj
*pArray
/* Name of Tcl array to set (*) element of */
1245 memset(p
, 0, sizeof(DbEvalContext
));
1247 p
->zSql
= Jim_String(pSql
);
1249 Jim_IncrRefCount(pSql
);
1252 Jim_IncrRefCount(pArray
);
1257 ** Obtain information about the row that the DbEvalContext passed as the
1258 ** first argument currently points to.
1260 static void dbEvalRowInfo(
1261 DbEvalContext
*p
, /* Evaluation context */
1262 int *pnCol
, /* OUT: Number of column names */
1263 Jim_Obj
***papColName
/* OUT: Array of column names */
1265 /* Compute column names */
1266 if( 0==p
->apColName
){
1267 sqlite3_stmt
*pStmt
= p
->pPreStmt
->pStmt
;
1268 int i
; /* Iterator variable */
1269 int nCol
; /* Number of columns returned by pStmt */
1270 Jim_Obj
**apColName
= 0; /* Array of column names */
1272 p
->nCol
= nCol
= sqlite3_column_count(pStmt
);
1273 if( nCol
>0 && (papColName
|| p
->pArray
) ){
1274 apColName
= (Jim_Obj
**)Jim_Alloc( sizeof(Jim_Obj
*)*nCol
);
1275 for(i
=0; i
<nCol
; i
++){
1276 apColName
[i
] = dbTextToObj(p
->pDb
->interp
, sqlite3_column_name(pStmt
,i
));
1277 Jim_IncrRefCount(apColName
[i
]);
1279 p
->apColName
= apColName
;
1282 /* If results are being stored in an array variable, then create
1283 ** the array(*) entry for that array
1286 Jim_Interp
*interp
= p
->pDb
->interp
;
1287 Jim_Obj
*pColList
= Jim_NewListObj(interp
, apColName
, nCol
);
1288 Jim_Obj
*pStar
= Jim_NewStringObj(interp
, "*", -1);
1289 Jim_IncrRefCount(pStar
);
1290 Jim_SetDictKeysVector(interp
, p
->pArray
, &pStar
, 1, pColList
, 0);
1291 Jim_DecrRefCount(interp
, pStar
);
1296 *papColName
= p
->apColName
;
1304 ** Return one of JIM_OK, JIM_BREAK or JIM_ERR. If JIM_ERR is
1305 ** returned, then an error message is stored in the interpreter before
1308 ** A return value of JIM_OK means there is a row of data available. The
1309 ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This
1310 ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If JIM_BREAK
1311 ** is returned, then the SQL script has finished executing and there are
1312 ** no further rows available. This is similar to SQLITE_DONE.
1314 static int dbEvalStep(DbEvalContext
*p
){
1315 while( p
->zSql
[0] || p
->pPreStmt
){
1317 if( p
->pPreStmt
==0 ){
1318 rc
= dbPrepareAndBind(p
->pDb
, p
->zSql
, &p
->zSql
, &p
->pPreStmt
);
1319 if( rc
!=JIM_OK
) return rc
;
1322 SqliteDb
*pDb
= p
->pDb
;
1323 SqlPreparedStmt
*pPreStmt
= p
->pPreStmt
;
1324 sqlite3_stmt
*pStmt
= pPreStmt
->pStmt
;
1326 rcs
= sqlite3_step(pStmt
);
1327 if( rcs
==SQLITE_ROW
){
1331 dbEvalRowInfo(p
, 0, 0);
1333 rcs
= sqlite3_reset(pStmt
);
1335 pDb
->nStep
= sqlite3_stmt_status(pStmt
,SQLITE_STMTSTATUS_FULLSCAN_STEP
,1);
1336 pDb
->nSort
= sqlite3_stmt_status(pStmt
,SQLITE_STMTSTATUS_SORT
,1);
1337 dbReleaseColumnNames(p
);
1340 if( rcs
!=SQLITE_OK
){
1341 /* If a run-time error occurs, report the error and stop reading
1343 Jim_SetResult(pDb
->interp
, dbTextToObj(pDb
->interp
, sqlite3_errmsg(pDb
->db
)));
1344 dbReleaseStmt(pDb
, pPreStmt
, 1);
1347 dbReleaseStmt(pDb
, pPreStmt
, 0);
1357 ** Free all resources currently held by the DbEvalContext structure passed
1358 ** as the first argument. There should be exactly one call to this function
1359 ** for each call to dbEvalInit().
1361 static void dbEvalFinalize(DbEvalContext
*p
){
1363 sqlite3_reset(p
->pPreStmt
->pStmt
);
1364 dbReleaseStmt(p
->pDb
, p
->pPreStmt
, 0);
1368 Jim_DecrRefCount(p
->pDb
->interp
, p
->pArray
);
1371 Jim_DecrRefCount(p
->pDb
->interp
, p
->pSql
);
1372 dbReleaseColumnNames(p
);
1376 ** Return a pointer to a Jim_Obj structure with ref-count 0 that contains
1377 ** the value for the iCol'th column of the row currently pointed to by
1378 ** the DbEvalContext structure passed as the first argument.
1380 static Jim_Obj
*dbEvalColumnValue(DbEvalContext
*p
, int iCol
){
1381 sqlite3_stmt
*pStmt
= p
->pPreStmt
->pStmt
;
1382 switch( sqlite3_column_type(pStmt
, iCol
) ){
1384 int bytes
= sqlite3_column_bytes(pStmt
, iCol
);
1385 const char *zBlob
= sqlite3_column_blob(pStmt
, iCol
);
1386 if( !zBlob
) bytes
= 0;
1387 //return Jim_NewByteArrayObj((u8*)zBlob, bytes);
1388 return Jim_NewStringObj(p
->pDb
->interp
, zBlob
, bytes
);
1390 case SQLITE_INTEGER
: {
1391 sqlite_int64 v
= sqlite3_column_int64(pStmt
, iCol
);
1392 return Jim_NewIntObj(p
->pDb
->interp
, v
);
1394 case SQLITE_FLOAT
: {
1395 return Jim_NewDoubleObj(p
->pDb
->interp
, sqlite3_column_double(pStmt
, iCol
));
1398 return dbTextToObj(p
->pDb
->interp
, p
->pDb
->zNull
);
1402 return dbTextToObj(p
->pDb
->interp
, (char *)sqlite3_column_text(pStmt
, iCol
));
1405 static int Jim_ObjSetVar2(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, Jim_Obj
*keyObjPtr
, Jim_Obj
*valObjPtr
)
1407 return Jim_SetDictKeysVector(interp
, nameObjPtr
, &keyObjPtr
, 1, valObjPtr
, 0);
1411 ** This function is part of the implementation of the command:
1413 ** $db eval SQL ?ARRAYNAME? SCRIPT
1415 static int DbEvalNextCmd(
1416 Jim_Interp
*interp
, /* Tcl interpreter */
1419 int result
/* Result so far */
1421 int rc
= result
; /* Return code */
1423 Jim_Obj
*pArray
= p
->pArray
;
1425 while( (rc
==JIM_OK
|| rc
==JIM_CONTINUE
) && JIM_OK
==(rc
= dbEvalStep(p
)) ){
1428 Jim_Obj
**apColName
;
1429 dbEvalRowInfo(p
, &nCol
, &apColName
);
1430 for(i
=0; i
<nCol
; i
++){
1431 Jim_Obj
*pVal
= dbEvalColumnValue(p
, i
);
1433 Jim_SetVariable(interp
, apColName
[i
], pVal
);
1435 Jim_ObjSetVar2(interp
, pArray
, apColName
[i
], pVal
);
1439 /* The required interpreter variables are now populated with the data
1440 ** from the current row.
1442 ** No NRE in Jim Tcl, so evaluate pScript directly and continue with the
1443 ** next iteration of this while(...) loop. */
1444 rc
= Jim_EvalObj(interp
, pScript
);
1447 Jim_DecrRefCount(interp
, pScript
);
1449 Jim_Free((char *)p
);
1451 if( rc
==JIM_OK
|| rc
==JIM_BREAK
){
1452 Jim_SetEmptyResult(interp
);
1459 ** The "sqlite" command below creates a new Tcl command for each
1460 ** connection it opens to an SQLite database. This routine is invoked
1461 ** whenever one of those connection-specific commands is executed
1462 ** in Tcl. For example, if you run Tcl code like this:
1464 ** sqlite3 db1 "my_database"
1467 ** The first command opens a connection to the "my_database" database
1468 ** and calls that connection "db1". The second command causes this
1469 ** subroutine to be invoked.
1471 static int DbObjCmd(Jim_Interp
*interp
, int objc
,Jim_Obj
*const*objv
){
1472 SqliteDb
*pDb
= (SqliteDb
*)Jim_CmdPrivData(interp
);
1475 static const char *DB_strs
[] = {
1476 "authorizer", "backup", "busy",
1477 "cache", "changes", "close",
1478 "collate", "collation_needed", "commit_hook",
1479 "complete", "copy", "enable_load_extension",
1480 "errorcode", "eval", "exists",
1481 "function", "incrblob", "interrupt",
1482 "last_insert_rowid", "nullvalue", "onecolumn",
1483 "profile", "progress", "rekey",
1484 "restore", "rollback_hook", "status",
1485 "timeout", "total_changes", "trace",
1486 "transaction", "unlock_notify", "update_hook",
1490 DB_AUTHORIZER
, DB_BACKUP
, DB_BUSY
,
1491 DB_CACHE
, DB_CHANGES
, DB_CLOSE
,
1492 DB_COLLATE
, DB_COLLATION_NEEDED
, DB_COMMIT_HOOK
,
1493 DB_COMPLETE
, DB_COPY
, DB_ENABLE_LOAD_EXTENSION
,
1494 DB_ERRORCODE
, DB_EVAL
, DB_EXISTS
,
1495 DB_FUNCTION
, DB_INCRBLOB
, DB_INTERRUPT
,
1496 DB_LAST_INSERT_ROWID
, DB_NULLVALUE
, DB_ONECOLUMN
,
1497 DB_PROFILE
, DB_PROGRESS
, DB_REKEY
,
1498 DB_RESTORE
, DB_ROLLBACK_HOOK
, DB_STATUS
,
1499 DB_TIMEOUT
, DB_TOTAL_CHANGES
, DB_TRACE
,
1500 DB_TRANSACTION
, DB_UNLOCK_NOTIFY
, DB_UPDATE_HOOK
,
1503 /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
1506 Jim_WrongNumArgs(interp
, 1, objv
, "SUBCOMMAND ...");
1509 if( Jim_GetEnum(interp
, objv
[1], DB_strs
, &choice
, "option", JIM_ERRMSG
| JIM_ENUM_ABBREV
) ){
1513 switch( (enum DB_enum
)choice
){
1515 /* $db authorizer ?CALLBACK?
1517 ** Invoke the given callback to authorize each SQL operation as it is
1518 ** compiled. 5 arguments are appended to the callback before it is
1521 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
1522 ** (2) First descriptive name (depends on authorization type)
1523 ** (3) Second descriptive name
1524 ** (4) Name of the database (ex: "main", "temp")
1525 ** (5) Name of trigger that is doing the access
1527 ** The callback should return on of the following strings: SQLITE_OK,
1528 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
1530 ** If this method is invoked with no arguments, the current authorization
1531 ** callback string is returned.
1533 case DB_AUTHORIZER
: {
1534 #ifdef SQLITE_OMIT_AUTHORIZATION
1535 Jim_SetResultString(interp
, "authorization not available in this build", -1);
1539 Jim_WrongNumArgs(interp
, 2, objv
, "?CALLBACK?");
1541 }else if( objc
==2 ){
1543 Jim_SetResultString(interp
, pDb
->zAuth
, -1);
1549 Jim_Free(pDb
->zAuth
);
1551 zAuth
= Jim_GetString(objv
[2], &len
);
1552 if( zAuth
&& len
>0 ){
1553 pDb
->zAuth
= Jim_Alloc( len
+ 1 );
1554 memcpy(pDb
->zAuth
, zAuth
, len
+1);
1559 pDb
->interp
= interp
;
1560 sqlite3_set_authorizer(pDb
->db
, auth_callback
, pDb
);
1562 sqlite3_set_authorizer(pDb
->db
, 0, 0);
1569 /* $db backup ?DATABASE? FILENAME
1571 ** Open or create a database file named FILENAME. Transfer the
1572 ** content of local database DATABASE (default: "main") into the
1573 ** FILENAME database.
1576 const char *zDestFile
;
1579 sqlite3_backup
*pBackup
;
1583 zDestFile
= Jim_String(objv
[2]);
1584 }else if( objc
==4 ){
1585 zSrcDb
= Jim_String(objv
[2]);
1586 zDestFile
= Jim_String(objv
[3]);
1588 Jim_WrongNumArgs(interp
, 2, objv
, "?DATABASE? FILENAME");
1591 rc
= sqlite3_open(zDestFile
, &pDest
);
1592 if( rc
!=SQLITE_OK
){
1593 Jim_SetResultFormatted(interp
, "cannot open target database: %s", sqlite3_errmsg(pDest
));
1594 sqlite3_close(pDest
);
1597 pBackup
= sqlite3_backup_init(pDest
, "main", pDb
->db
, zSrcDb
);
1599 Jim_SetResultFormatted(interp
, "backup failed: %s", sqlite3_errmsg(pDest
));
1600 sqlite3_close(pDest
);
1603 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
){}
1604 sqlite3_backup_finish(pBackup
);
1605 if( rc
==SQLITE_DONE
){
1608 Jim_SetResultFormatted(interp
, "backup failed: %s", sqlite3_errmsg(pDest
));
1611 sqlite3_close(pDest
);
1615 /* $db busy ?CALLBACK?
1617 ** Invoke the given callback if an SQL statement attempts to open
1618 ** a locked database file.
1622 Jim_WrongNumArgs(interp
, 2, objv
, "CALLBACK");
1624 }else if( objc
==2 ){
1626 Jim_SetResultString(interp
, pDb
->zBusy
, -1);
1632 Jim_Free(pDb
->zBusy
);
1634 zBusy
= Jim_GetString(objv
[2], &len
);
1635 if( zBusy
&& len
>0 ){
1636 pDb
->zBusy
= Jim_Alloc( len
+ 1 );
1637 memcpy(pDb
->zBusy
, zBusy
, len
+1);
1642 pDb
->interp
= interp
;
1643 sqlite3_busy_handler(pDb
->db
, DbBusyHandler
, pDb
);
1645 sqlite3_busy_handler(pDb
->db
, 0, 0);
1654 ** Flush the prepared statement cache, or set the maximum number of
1655 ** cached statements.
1661 Jim_WrongNumArgs(interp
, 1, objv
, "cache option ?arg?");
1664 subCmd
= Jim_String( objv
[2]);
1665 if( *subCmd
=='f' && strcmp(subCmd
,"flush")==0 ){
1667 Jim_WrongNumArgs(interp
, 2, objv
, "flush");
1670 flushStmtCache( pDb
);
1672 }else if( *subCmd
=='s' && strcmp(subCmd
,"size")==0 ){
1674 Jim_WrongNumArgs(interp
, 2, objv
, "size n");
1678 if( JIM_ERR
==Jim_GetWide(interp
, objv
[3], &w
) ){
1682 flushStmtCache( pDb
);
1684 }else if( w
>MAX_PREPARED_STMTS
){
1685 w
= MAX_PREPARED_STMTS
;
1691 Jim_SetResultFormatted(interp
, "bad option \"%#s\": must be flush or size", objv
[2]);
1699 ** Return the number of rows that were modified, inserted, or deleted by
1700 ** the most recent INSERT, UPDATE or DELETE statement, not including
1701 ** any changes made by trigger programs.
1705 Jim_WrongNumArgs(interp
, 2, objv
, "");
1708 Jim_SetResultInt(interp
, sqlite3_changes(pDb
->db
));
1714 ** Shutdown the database
1717 Jim_DeleteCommand(interp
, Jim_String(objv
[0]));
1722 ** $db collate NAME SCRIPT
1724 ** Create a new SQL collation function called NAME. Whenever
1725 ** that function is called, invoke SCRIPT to evaluate the function.
1728 SqlCollate
*pCollate
;
1730 const char *zScript
;
1733 Jim_WrongNumArgs(interp
, 2, objv
, "NAME SCRIPT");
1736 zName
= Jim_String(objv
[2]);
1737 zScript
= Jim_GetString(objv
[3], &nScript
);
1738 pCollate
= (SqlCollate
*)Jim_Alloc( sizeof(*pCollate
) + nScript
+ 1 );
1739 if( pCollate
==0 ) return JIM_ERR
;
1740 pCollate
->interp
= interp
;
1741 pCollate
->pNext
= pDb
->pCollate
;
1742 pCollate
->zScript
= (char*)&pCollate
[1];
1743 pDb
->pCollate
= pCollate
;
1744 memcpy(pCollate
->zScript
, zScript
, nScript
+1);
1745 if( sqlite3_create_collation(pDb
->db
, zName
, SQLITE_UTF8
,
1746 pCollate
, tclSqlCollate
) ){
1747 Jim_SetResultString(interp
, (char *)sqlite3_errmsg(pDb
->db
), -1);
1754 ** $db collation_needed SCRIPT
1756 ** Create a new SQL collation function called NAME. Whenever
1757 ** that function is called, invoke SCRIPT to evaluate the function.
1759 case DB_COLLATION_NEEDED
: {
1761 Jim_WrongNumArgs(interp
, 2, objv
, "SCRIPT");
1764 if( pDb
->pCollateNeeded
){
1765 Jim_DecrRefCount(interp
, pDb
->pCollateNeeded
);
1767 pDb
->pCollateNeeded
= Jim_DuplicateObj(pDb
->interp
, objv
[2]);
1768 Jim_IncrRefCount(pDb
->pCollateNeeded
);
1769 sqlite3_collation_needed(pDb
->db
, pDb
, tclCollateNeeded
);
1773 /* $db commit_hook ?CALLBACK?
1775 ** Invoke the given callback just before committing every SQL transaction.
1776 ** If the callback throws an exception or returns non-zero, then the
1777 ** transaction is aborted. If CALLBACK is an empty string, the callback
1780 case DB_COMMIT_HOOK
: {
1782 Jim_WrongNumArgs(interp
, 2, objv
, "?CALLBACK?");
1784 }else if( objc
==2 ){
1786 Jim_SetResultString(interp
, pDb
->zCommit
, -1);
1789 const char *zCommit
;
1792 Jim_Free(pDb
->zCommit
);
1794 zCommit
= Jim_GetString(objv
[2], &len
);
1795 if( zCommit
&& len
>0 ){
1796 pDb
->zCommit
= Jim_Alloc( len
+ 1 );
1797 memcpy(pDb
->zCommit
, zCommit
, len
+1);
1802 pDb
->interp
= interp
;
1803 sqlite3_commit_hook(pDb
->db
, DbCommitHandler
, pDb
);
1805 sqlite3_commit_hook(pDb
->db
, 0, 0);
1813 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
1814 ** additional lines of input are needed. This is similar to the
1815 ** built-in "info complete" command of Tcl.
1818 #ifndef SQLITE_OMIT_COMPLETE
1820 Jim_WrongNumArgs(interp
, 2, objv
, "SQL");
1823 Jim_SetResultInt(interp
, sqlite3_complete( Jim_String(objv
[2]) ));
1828 /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
1830 ** Copy data into table from filename, optionally using SEPARATOR
1831 ** as column separators. If a column contains a null string, or the
1832 ** value of NULLINDICATOR, a NULL is inserted for the column.
1833 ** conflict-algorithm is one of the sqlite conflict algorithms:
1834 ** rollback, abort, fail, ignore, replace
1835 ** On success, return the number of lines processed, not necessarily same
1836 ** as 'db changes' due to conflict-algorithm selected.
1838 ** This code is basically an implementation/enhancement of
1839 ** the sqlite3 shell.c ".import" command.
1841 ** This command usage is equivalent to the sqlite2.x COPY statement,
1842 ** which imports file data into a table using the PostgreSQL COPY file format:
1843 ** $db copy $conflit_algo $table_name $filename \t \\N
1846 const char *zTable
; /* Insert data into this table */
1847 const char *zFile
; /* The file from which to extract data */
1848 const char *zConflict
; /* The conflict algorithm to use */
1849 sqlite3_stmt
*pStmt
; /* A statement */
1850 int nCol
; /* Number of columns in the table */
1851 int nByte
; /* Number of bytes in an SQL string */
1852 int i
, j
; /* Loop counters */
1853 int nSep
; /* Number of bytes in zSep[] */
1854 int nNull
; /* Number of bytes in zNull[] */
1855 char *zSql
; /* An SQL statement */
1856 char *zLine
; /* A single line of input from the file */
1857 char **azCol
; /* zLine[] broken up into columns */
1858 char *zCommit
; /* How to commit changes */
1859 FILE *in
; /* The input file */
1860 int lineno
= 0; /* Line number of input file */
1861 char zLineNum
[80]; /* Line number print buffer */
1865 if( objc
<5 || objc
>7 ){
1866 Jim_WrongNumArgs(interp
, 2, objv
,
1867 "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
1871 zSep
= Jim_String(objv
[5]);
1876 zNull
= Jim_String(objv
[6]);
1880 zConflict
= Jim_String(objv
[2]);
1881 zTable
= Jim_String(objv
[3]);
1882 zFile
= Jim_String(objv
[4]);
1883 nSep
= strlen30(zSep
);
1884 nNull
= strlen30(zNull
);
1886 Jim_SetResultString(interp
, "Error: non-null separator required for copy", -1);
1889 if(strcmp(zConflict
, "rollback") != 0 &&
1890 strcmp(zConflict
, "abort" ) != 0 &&
1891 strcmp(zConflict
, "fail" ) != 0 &&
1892 strcmp(zConflict
, "ignore" ) != 0 &&
1893 strcmp(zConflict
, "replace" ) != 0 ) {
1894 Jim_SetResultFormatted(interp
, "Error: \"%s\", conflict-algorithm must be one of: rollback, "
1895 "abort, fail, ignore, or replace", zConflict
);
1898 zSql
= sqlite3_mprintf("SELECT * FROM '%q'", zTable
);
1900 Jim_SetResultFormatted(interp
, "Error: no such table: %s", zTable
);
1903 nByte
= strlen30(zSql
);
1904 rc
= sqlite3_prepare(pDb
->db
, zSql
, -1, &pStmt
, 0);
1907 Jim_SetResultFormatted(interp
, "Error: %s", sqlite3_errmsg(pDb
->db
));
1910 nCol
= sqlite3_column_count(pStmt
);
1912 sqlite3_finalize(pStmt
);
1916 zSql
= Jim_Alloc( nByte
+ 50 + nCol
*2 );
1917 sqlite3_snprintf(nByte
+50, zSql
, "INSERT OR %q INTO '%q' VALUES(?",
1920 for(i
=1; i
<nCol
; i
++){
1926 rc
= sqlite3_prepare(pDb
->db
, zSql
, -1, &pStmt
, 0);
1929 Jim_SetResultFormatted(interp
, "Error: %s", sqlite3_errmsg(pDb
->db
));
1930 sqlite3_finalize(pStmt
);
1933 in
= fopen(zFile
, "rb");
1935 Jim_SetResultFormatted(interp
, "Error: cannot open file: %s", zFile
);
1936 sqlite3_finalize(pStmt
);
1939 azCol
= Jim_Alloc( sizeof(azCol
[0])*(nCol
+1) );
1940 (void)sqlite3_exec(pDb
->db
, "BEGIN", 0, 0, 0);
1942 while( (zLine
= local_getline(0, in
))!=0 ){
1947 for(i
=0, z
=zLine
; *z
; z
++){
1948 if( *z
==zSep
[0] && strncmp(z
, zSep
, nSep
)==0 ){
1952 azCol
[i
] = &z
[nSep
];
1959 int nErr
= strlen30(zFile
) + 200;
1960 zErr
= Jim_Alloc(nErr
);
1961 sqlite3_snprintf(nErr
, zErr
,
1962 "Error: %s line %d: expected %d columns of data but found %d",
1963 zFile
, lineno
, nCol
, i
+1);
1964 Jim_SetResultString(interp
, zErr
, -1);
1966 zCommit
= "ROLLBACK";
1969 for(i
=0; i
<nCol
; i
++){
1970 /* check for null data, if so, bind as null */
1971 if( (nNull
>0 && strcmp(azCol
[i
], zNull
)==0)
1972 || strlen30(azCol
[i
])==0
1974 sqlite3_bind_null(pStmt
, i
+1);
1976 sqlite3_bind_text(pStmt
, i
+1, azCol
[i
], -1, SQLITE_STATIC
);
1979 sqlite3_step(pStmt
);
1980 rc
= sqlite3_reset(pStmt
);
1982 if( rc
!=SQLITE_OK
){
1983 Jim_SetResultFormatted(interp
, "Error: %s", sqlite3_errmsg(pDb
->db
));
1984 zCommit
= "ROLLBACK";
1990 sqlite3_finalize(pStmt
);
1991 (void)sqlite3_exec(pDb
->db
, zCommit
, 0, 0, 0);
1993 if( zCommit
[0] == 'C' ){
1994 /* success, set result as number of lines processed */
1995 Jim_SetResultInt(interp
, lineno
);
1998 /* failure, append lineno where failed */
1999 sqlite3_snprintf(sizeof(zLineNum
), zLineNum
,"%d",lineno
);
2000 Jim_AppendStrings(interp
, Jim_GetResult(interp
), ", failed while processing line: ", zLineNum
, NULL
);
2007 ** $db enable_load_extension BOOLEAN
2009 ** Turn the extension loading feature on or off. It if off by
2012 case DB_ENABLE_LOAD_EXTENSION
: {
2013 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2016 Jim_WrongNumArgs(interp
, 2, objv
, "BOOLEAN");
2019 if( Jim_GetLong(interp
, objv
[2], &onoff
) ){
2022 sqlite3_enable_load_extension(pDb
->db
, onoff
);
2025 Jim_SetResultString(interp
, "extension loading is turned off at compile-time", -1);
2033 ** Return the numeric error code that was returned by the most recent
2034 ** call to sqlite3_exec().
2036 case DB_ERRORCODE
: {
2037 Jim_SetResultInt(interp
, sqlite3_errcode(pDb
->db
));
2043 ** $db onecolumn $sql
2045 ** The onecolumn method is the equivalent of:
2046 ** lindex [$db eval $sql] 0
2049 case DB_ONECOLUMN
: {
2050 DbEvalContext sEval
;
2052 Jim_WrongNumArgs(interp
, 2, objv
, "SQL");
2056 dbEvalInit(&sEval
, pDb
, objv
[2], 0);
2057 rc
= dbEvalStep(&sEval
);
2058 if( choice
==DB_ONECOLUMN
){
2060 Jim_SetResult(interp
, dbEvalColumnValue(&sEval
, 0));
2062 }else if( rc
==JIM_BREAK
|| rc
==JIM_OK
){
2063 Jim_SetResultInt(interp
, rc
==JIM_OK
);
2065 dbEvalFinalize(&sEval
);
2067 if( rc
==JIM_BREAK
){
2074 ** $db eval $sql ?array? ?{ ...code... }?
2076 ** The SQL statement in $sql is evaluated. For each row, the values are
2077 ** placed in elements of the array named "array" and ...code... is executed.
2078 ** If "array" and "code" are omitted, then no callback is every invoked.
2079 ** If "array" is an empty string, then the values are placed in variables
2080 ** that have the same name as the fields extracted by the query.
2083 if( objc
<3 || objc
>5 ){
2084 Jim_WrongNumArgs(interp
, 2, objv
, "SQL ?ARRAY-NAME? ?SCRIPT?");
2089 DbEvalContext sEval
;
2090 Jim_Obj
*pRet
= Jim_NewListObj(interp
, NULL
, 0);
2091 Jim_IncrRefCount(pRet
);
2092 dbEvalInit(&sEval
, pDb
, objv
[2], 0);
2093 while( JIM_OK
==(rc
= dbEvalStep(&sEval
)) ){
2096 dbEvalRowInfo(&sEval
, &nCol
, 0);
2097 for(i
=0; i
<nCol
; i
++){
2098 Jim_ListAppendElement(interp
, pRet
, dbEvalColumnValue(&sEval
, i
));
2101 dbEvalFinalize(&sEval
);
2102 if( rc
==JIM_BREAK
){
2103 Jim_SetResult(interp
, pRet
);
2106 Jim_DecrRefCount(interp
, pRet
);
2109 Jim_Obj
*pArray
= 0;
2112 if( objc
==5 && Jim_Length(objv
[3]) ){
2115 pScript
= objv
[objc
-1];
2116 Jim_IncrRefCount(pScript
);
2118 p
= (DbEvalContext
*)Jim_Alloc(sizeof(DbEvalContext
));
2119 dbEvalInit(p
, pDb
, objv
[2], pArray
);
2121 rc
= DbEvalNextCmd(interp
, p
, pScript
, JIM_OK
);
2127 ** $db function NAME [-argcount N] SCRIPT
2129 ** Create a new SQL function called NAME. Whenever that function is
2130 ** called, invoke SCRIPT to evaluate the function.
2138 const char *z
= Jim_String(objv
[3]);
2139 int n
= strlen30(z
);
2140 if( n
>2 && strncmp(z
, "-argcount",n
)==0 ){
2141 if( Jim_GetLong(interp
, objv
[4], &nArg
) ) return JIM_ERR
;
2143 Jim_SetResultString(interp
, "number of arguments must be non-negative", -1);
2148 }else if( objc
!=4 ){
2149 Jim_WrongNumArgs(interp
, 2, objv
, "NAME [-argcount N] SCRIPT");
2154 zName
= Jim_String(objv
[2]);
2155 pFunc
= findSqlFunc(pDb
, zName
);
2156 if( pFunc
==0 ) return JIM_ERR
;
2157 if( pFunc
->pScript
){
2158 Jim_DecrRefCount(interp
, pFunc
->pScript
);
2160 pFunc
->pScript
= pScript
;
2161 Jim_IncrRefCount(pScript
);
2162 pFunc
->useEvalObjv
= safeToUseEvalObjv(interp
, pScript
);
2163 rc
= sqlite3_create_function(pDb
->db
, zName
, nArg
, SQLITE_UTF8
,
2164 pFunc
, tclSqlFunc
, 0, 0);
2165 if( rc
!=SQLITE_OK
){
2167 Jim_SetResultString(interp
, (char *)sqlite3_errmsg(pDb
->db
), -1);
2173 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID
2176 #ifdef SQLITE_OMIT_INCRBLOB
2177 Jim_SetResultString(interp
, "incrblob not available in this build", -1);
2181 const char *zDb
= "main";
2183 const char *zColumn
;
2186 /* Check for the -readonly option */
2187 if( objc
>3 && strcmp(Jim_GetString(objv
[2]), "-readonly")==0 ){
2191 if( objc
!=(5+isReadonly
) && objc
!=(6+isReadonly
) ){
2192 Jim_WrongNumArgs(interp
, 2, objv
, "?-readonly? ?DB? TABLE COLUMN ROWID");
2196 if( objc
==(6+isReadonly
) ){
2197 zDb
= Jim_GetString(objv
[2]);
2199 zTable
= Jim_GetString(objv
[objc
-3]);
2200 zColumn
= Jim_GetString(objv
[objc
-2]);
2201 rc
= Jim_GetWide(interp
, objv
[objc
-1], &iRow
);
2204 rc
= createIncrblobChannel(
2205 interp
, pDb
, zDb
, zTable
, zColumn
, iRow
, isReadonly
2215 ** Interrupt the execution of the inner-most SQL interpreter. This
2216 ** causes the SQL statement to return an error of SQLITE_INTERRUPT.
2218 case DB_INTERRUPT
: {
2219 sqlite3_interrupt(pDb
->db
);
2224 ** $db nullvalue ?STRING?
2226 ** Change text used when a NULL comes back from the database. If ?STRING?
2227 ** is not present, then the current string used for NULL is returned.
2228 ** If STRING is present, then STRING is returned.
2231 case DB_NULLVALUE
: {
2232 if( objc
!=2 && objc
!=3 ){
2233 Jim_WrongNumArgs(interp
, 2, objv
, "NULLVALUE");
2238 const char *zNull
= Jim_GetString(objv
[2], &len
);
2240 Jim_Free(pDb
->zNull
);
2242 if( zNull
&& len
>0 ){
2243 pDb
->zNull
= Jim_Alloc( len
+ 1 );
2244 strncpy(pDb
->zNull
, zNull
, len
);
2245 pDb
->zNull
[len
] = '\0';
2250 Jim_SetResult(interp
, dbTextToObj(interp
, pDb
->zNull
));
2255 ** $db last_insert_rowid
2257 ** Return an integer which is the ROWID for the most recent insert.
2259 case DB_LAST_INSERT_ROWID
: {
2261 Jim_WrongNumArgs(interp
, 2, objv
, "");
2264 Jim_SetResultInt(interp
, sqlite3_last_insert_rowid(pDb
->db
));
2269 ** The DB_ONECOLUMN method is implemented together with DB_EXISTS.
2272 /* $db progress ?N CALLBACK?
2274 ** Invoke the given callback every N virtual machine opcodes while executing
2279 if( pDb
->zProgress
){
2280 Jim_AppendString(interp
, Jim_GetResult(interp
), pDb
->zProgress
, -1);
2282 }else if( objc
==4 ){
2283 const char *zProgress
;
2286 if( JIM_OK
!=Jim_GetLong(interp
, objv
[2], &N
) ){
2289 if( pDb
->zProgress
){
2290 Jim_Free(pDb
->zProgress
);
2292 zProgress
= Jim_GetString(objv
[3], &len
);
2293 if( zProgress
&& len
>0 ){
2294 pDb
->zProgress
= Jim_Alloc( len
+ 1 );
2295 memcpy(pDb
->zProgress
, zProgress
, len
+1);
2299 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2300 if( pDb
->zProgress
){
2301 pDb
->interp
= interp
;
2302 sqlite3_progress_handler(pDb
->db
, N
, DbProgressHandler
, pDb
);
2304 sqlite3_progress_handler(pDb
->db
, 0, 0, 0);
2308 Jim_WrongNumArgs(interp
, 2, objv
, "N CALLBACK");
2314 /* $db profile ?CALLBACK?
2316 ** Make arrangements to invoke the CALLBACK routine after each SQL statement
2317 ** that has run. The text of the SQL and the amount of elapse time are
2318 ** appended to CALLBACK before the script is run.
2322 Jim_WrongNumArgs(interp
, 2, objv
, "?CALLBACK?");
2324 }else if( objc
==2 ){
2325 if( pDb
->zProfile
){
2326 Jim_SetResultString(interp
, pDb
->zProfile
, -1);
2329 const char *zProfile
;
2331 if( pDb
->zProfile
){
2332 Jim_Free(pDb
->zProfile
);
2334 zProfile
= Jim_GetString(objv
[2], &len
);
2335 if( zProfile
&& len
>0 ){
2336 pDb
->zProfile
= Jim_Alloc( len
+ 1 );
2337 memcpy(pDb
->zProfile
, zProfile
, len
+1);
2341 #ifndef SQLITE_OMIT_TRACE
2342 if( pDb
->zProfile
){
2343 pDb
->interp
= interp
;
2344 sqlite3_profile(pDb
->db
, DbProfileHandler
, pDb
);
2346 sqlite3_profile(pDb
->db
, 0, 0);
2356 ** Change the encryption key on the currently open database.
2362 Jim_WrongNumArgs(interp
, 2, objv
, "KEY");
2365 //pKey = Jim_GetByteArrayFromObj(objv[2], &nKey);
2366 pKey
= Jim_GetString(objv
[2], &nKey
);
2367 #ifdef SQLITE_HAS_CODEC
2368 rc
= sqlite3_rekey(pDb
->db
, pKey
, nKey
);
2370 Jim_SetResultString(interp
, sqlite3ErrStr(rc
), -1);
2377 /* $db restore ?DATABASE? FILENAME
2379 ** Open a database file named FILENAME. Transfer the content
2380 ** of FILENAME into the local database DATABASE (default: "main").
2383 const char *zSrcFile
;
2384 const char *zDestDb
;
2386 sqlite3_backup
*pBackup
;
2391 zSrcFile
= Jim_String(objv
[2]);
2392 }else if( objc
==4 ){
2393 zDestDb
= Jim_String(objv
[2]);
2394 zSrcFile
= Jim_String(objv
[3]);
2396 Jim_WrongNumArgs(interp
, 2, objv
, "?DATABASE? FILENAME");
2399 rc
= sqlite3_open_v2(zSrcFile
, &pSrc
, SQLITE_OPEN_READONLY
, 0);
2400 if( rc
!=SQLITE_OK
){
2401 Jim_SetResultFormatted(interp
, "cannot open source database: %s", sqlite3_errmsg(pSrc
));
2402 sqlite3_close(pSrc
);
2405 pBackup
= sqlite3_backup_init(pDb
->db
, zDestDb
, pSrc
, "main");
2407 Jim_SetResultFormatted(interp
, "restore failed: %s", sqlite3_errmsg(pDb
->db
));
2408 sqlite3_close(pSrc
);
2411 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
2412 || rc
==SQLITE_BUSY
){
2413 if( rc
==SQLITE_BUSY
){
2414 if( nTimeout
++ >= 3 ) break;
2418 sqlite3_backup_finish(pBackup
);
2419 if( rc
==SQLITE_DONE
){
2421 }else if( rc
==SQLITE_BUSY
|| rc
==SQLITE_LOCKED
){
2422 Jim_SetResultString(interp
, "restore failed: source database busy", -1);
2425 Jim_SetResultFormatted(interp
, "restore failed: %s", sqlite3_errmsg(pDb
->db
));
2428 sqlite3_close(pSrc
);
2433 ** $db status (step|sort)
2435 ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or
2436 ** SQLITE_STMTSTATUS_SORT for the most recent eval.
2442 Jim_WrongNumArgs(interp
, 2, objv
, "(step|sort)");
2445 zOp
= Jim_String(objv
[2]);
2446 if( strcmp(zOp
, "step")==0 ){
2448 }else if( strcmp(zOp
, "sort")==0 ){
2451 Jim_SetResultString(interp
, "bad argument: should be step or sort", -1);
2454 Jim_SetResultInt(interp
, v
);
2459 ** $db timeout MILLESECONDS
2461 ** Delay for the number of milliseconds specified when a file is locked.
2466 Jim_WrongNumArgs(interp
, 2, objv
, "MILLISECONDS");
2469 if( Jim_GetLong(interp
, objv
[2], &ms
) ) return JIM_ERR
;
2470 sqlite3_busy_timeout(pDb
->db
, ms
);
2475 ** $db total_changes
2477 ** Return the number of rows that were modified, inserted, or deleted
2478 ** since the database handle was created.
2480 case DB_TOTAL_CHANGES
: {
2482 Jim_WrongNumArgs(interp
, 2, objv
, "");
2485 Jim_SetResultInt(interp
, sqlite3_total_changes(pDb
->db
));
2489 /* $db trace ?CALLBACK?
2491 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
2492 ** that is executed. The text of the SQL is appended to CALLBACK before
2497 Jim_WrongNumArgs(interp
, 2, objv
, "?CALLBACK?");
2499 }else if( objc
==2 ){
2501 Jim_AppendString(interp
, Jim_GetResult(interp
), pDb
->zTrace
, -1);
2507 Jim_Free(pDb
->zTrace
);
2509 zTrace
= Jim_GetString(objv
[2], &len
);
2510 if( zTrace
&& len
>0 ){
2511 pDb
->zTrace
= Jim_Alloc( len
+ 1 );
2512 memcpy(pDb
->zTrace
, zTrace
, len
+1);
2516 #ifndef SQLITE_OMIT_TRACE
2518 pDb
->interp
= interp
;
2519 sqlite3_trace(pDb
->db
, DbTraceHandler
, pDb
);
2521 sqlite3_trace(pDb
->db
, 0, 0);
2528 /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT
2530 ** Start a new transaction (if we are not already in the midst of a
2531 ** transaction) and execute the TCL script SCRIPT. After SCRIPT
2532 ** completes, either commit the transaction or roll it back if SCRIPT
2533 ** throws an exception. Or if no new transation was started, do nothing.
2534 ** pass the exception on up the stack.
2536 ** This command was inspired by Dave Thomas's talk on Ruby at the
2537 ** 2005 O'Reilly Open Source Convention (OSCON).
2539 case DB_TRANSACTION
: {
2541 const char *zBegin
= "SAVEPOINT _tcl_transaction";
2542 if( objc
!=3 && objc
!=4 ){
2543 Jim_WrongNumArgs(interp
, 2, objv
, "[TYPE] SCRIPT");
2547 if( pDb
->nTransaction
==0 && objc
==4 ){
2548 static const char *TTYPE_strs
[] = {
2549 "deferred", "exclusive", "immediate", 0
2552 TTYPE_DEFERRED
, TTYPE_EXCLUSIVE
, TTYPE_IMMEDIATE
2555 if( Jim_GetEnum(interp
, objv
[2], TTYPE_strs
, &ttype
, "transaction type", JIM_ERRMSG
| JIM_ENUM_ABBREV
) ){
2558 switch( (enum TTYPE_enum
)ttype
){
2559 case TTYPE_DEFERRED
: /* no-op */; break;
2560 case TTYPE_EXCLUSIVE
: zBegin
= "BEGIN EXCLUSIVE"; break;
2561 case TTYPE_IMMEDIATE
: zBegin
= "BEGIN IMMEDIATE"; break;
2564 pScript
= objv
[objc
-1];
2566 /* Run the SQLite BEGIN command to open a transaction or savepoint. */
2568 rc
= sqlite3_exec(pDb
->db
, zBegin
, 0, 0, 0);
2570 if( rc
!=SQLITE_OK
){
2571 Jim_SetResultString(interp
, sqlite3_errmsg(pDb
->db
), -1);
2574 pDb
->nTransaction
++;
2576 /* No NRE in Jim Tcl, so evaluate the script directly, then
2577 ** call function DbTransPostCmd() to commit (or rollback) the transaction
2579 rc
= DbTransPostCmd(interp
, pDb
, Jim_EvalObj(interp
, pScript
));
2584 ** $db unlock_notify ?script?
2586 case DB_UNLOCK_NOTIFY
: {
2587 #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY
2588 Jim_SetResultString(interp
, "unlock_notify not available in this build", -1);
2591 if( objc
!=2 && objc
!=3 ){
2592 Jim_WrongNumArgs(interp
, 2, objv
, "?SCRIPT?");
2595 void (*xNotify
)(void **, int) = 0;
2596 void *pNotifyArg
= 0;
2598 if( pDb
->pUnlockNotify
){
2599 Jim_DecrRefCount(interp
, pDb
->pUnlockNotify
);
2600 pDb
->pUnlockNotify
= 0;
2604 xNotify
= DbUnlockNotify
;
2605 pNotifyArg
= (void *)pDb
;
2606 pDb
->pUnlockNotify
= objv
[2];
2607 Jim_IncrRefCount(pDb
->pUnlockNotify
);
2610 if( sqlite3_unlock_notify(pDb
->db
, xNotify
, pNotifyArg
) ){
2611 Jim_SetResultString(interp
, sqlite3_errmsg(pDb
->db
), -1);
2620 ** $db update_hook ?script?
2621 ** $db rollback_hook ?script?
2623 case DB_UPDATE_HOOK
:
2624 case DB_ROLLBACK_HOOK
: {
2626 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on
2627 ** whether [$db update_hook] or [$db rollback_hook] was invoked.
2630 if( choice
==DB_UPDATE_HOOK
){
2631 ppHook
= &pDb
->pUpdateHook
;
2633 ppHook
= &pDb
->pRollbackHook
;
2636 if( objc
!=2 && objc
!=3 ){
2637 Jim_WrongNumArgs(interp
, 2, objv
, "?SCRIPT?");
2641 Jim_SetResult(interp
, *ppHook
);
2643 Jim_DecrRefCount(interp
, *ppHook
);
2648 assert( !(*ppHook
) );
2649 if( Jim_Length(objv
[2])>0 ){
2651 Jim_IncrRefCount(*ppHook
);
2655 sqlite3_update_hook(pDb
->db
, (pDb
->pUpdateHook
?DbUpdateHandler
:0), pDb
);
2656 sqlite3_rollback_hook(pDb
->db
,(pDb
->pRollbackHook
?DbRollbackHandler
:0),pDb
);
2663 ** Return the version string for this database.
2666 Jim_SetResultString(interp
, sqlite3_libversion(), -1);
2671 } /* End of the SWITCH statement */
2676 ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN?
2677 ** ?-create BOOLEAN? ?-nomutex BOOLEAN?
2679 ** This is the main Tcl command. When the "sqlite" Tcl command is
2680 ** invoked, this routine runs to process that command.
2682 ** The first argument, DBNAME, is an arbitrary name for a new
2683 ** database connection. This command creates a new command named
2684 ** DBNAME that is used to control that connection. The database
2685 ** connection is deleted when the DBNAME command is deleted.
2687 ** The second argument is the name of the database file.
2690 static int DbMain(Jim_Interp
*interp
, int objc
, Jim_Obj
*const*objv
){
2692 const char *pKey
= 0;
2698 const char *zVfs
= 0;
2701 /* Not threading in Jim, so no mutexing is needed */
2702 flags
= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
| SQLITE_OPEN_NOMUTEX
;
2705 zArg
= Jim_String(objv
[1]);
2706 if( strcmp(zArg
,"-version")==0 ){
2707 Jim_SetResultString(interp
, sqlite3_version
, -1);
2710 if( strcmp(zArg
,"-has-codec")==0 ){
2711 #ifdef SQLITE_HAS_CODEC
2712 Jim_SetResultInt(interp
, 1);
2714 Jim_SetResultInt(interp
, 0);
2719 for(i
=3; i
+1<objc
; i
+=2){
2720 zArg
= Jim_String(objv
[i
]);
2721 if( strcmp(zArg
,"-key")==0 ){
2722 pKey
= Jim_GetString(objv
[i
+1], &nKey
);
2723 }else if( strcmp(zArg
, "-vfs")==0 ){
2725 zVfs
= Jim_String(objv
[i
]);
2726 }else if( strcmp(zArg
, "-readonly")==0 ){
2728 if( Jim_GetLong(interp
, objv
[i
+1], &b
) ) return JIM_ERR
;
2730 flags
&= ~(SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
);
2731 flags
|= SQLITE_OPEN_READONLY
;
2733 flags
&= ~SQLITE_OPEN_READONLY
;
2734 flags
|= SQLITE_OPEN_READWRITE
;
2736 }else if( strcmp(zArg
, "-create")==0 ){
2738 if( Jim_GetLong(interp
, objv
[i
+1], &b
) ) return JIM_ERR
;
2739 if( b
&& (flags
& SQLITE_OPEN_READONLY
)==0 ){
2740 flags
|= SQLITE_OPEN_CREATE
;
2742 flags
&= ~SQLITE_OPEN_CREATE
;
2744 }else if( strcmp(zArg
, "-nomutex")==0 ){
2746 if( Jim_GetLong(interp
, objv
[i
+1], &b
) ) return JIM_ERR
;
2748 flags
|= SQLITE_OPEN_NOMUTEX
;
2749 flags
&= ~SQLITE_OPEN_FULLMUTEX
;
2751 flags
&= ~SQLITE_OPEN_NOMUTEX
;
2753 }else if( strcmp(zArg
, "-fullmutex")==0 ){
2755 if( Jim_GetLong(interp
, objv
[i
+1], &b
) ) return JIM_ERR
;
2757 flags
|= SQLITE_OPEN_FULLMUTEX
;
2758 flags
&= ~SQLITE_OPEN_NOMUTEX
;
2760 flags
&= ~SQLITE_OPEN_FULLMUTEX
;
2763 Jim_SetResultFormatted(interp
, "unknown option: %s", zArg
);
2767 if( objc
<3 || (objc
&1)!=1 ){
2768 Jim_WrongNumArgs(interp
, 1, objv
,
2769 "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?"
2770 " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?"
2771 #ifdef SQLITE_HAS_CODEC
2778 p
= (SqliteDb
*)Jim_Alloc( sizeof(*p
) );
2779 memset(p
, 0, sizeof(*p
));
2780 zFile
= Jim_String(objv
[2]);
2781 sqlite3_open_v2(zFile
, &p
->db
, flags
, zVfs
);
2782 if( SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
2783 zErrMsg
= sqlite3_mprintf("%s", sqlite3_errmsg(p
->db
));
2784 sqlite3_close(p
->db
);
2787 #ifdef SQLITE_HAS_CODEC
2789 sqlite3_key(p
->db
, pKey
, nKey
);
2793 Jim_SetResultString(interp
, zErrMsg
, -1);
2795 sqlite3_free(zErrMsg
);
2798 p
->maxStmt
= NUM_PREPARED_STMTS
;
2800 zArg
= Jim_String(objv
[1]);
2801 Jim_CreateCommand(interp
, zArg
, DbObjCmd
, p
, DbDeleteCmd
);
2806 ** Make sure we have a PACKAGE_VERSION macro defined. This will be
2807 ** defined automatically by the TEA makefile. But other makefiles
2808 ** do not define it.
2810 #ifndef PACKAGE_VERSION
2811 # define PACKAGE_VERSION SQLITE_VERSION
2816 ** Initialize this module.
2818 ** This Tcl module contains only a single new Tcl command named "sqlite".
2819 ** (Hence there is no namespace. There is no point in using a namespace
2820 ** if the extension only supplies one new name!) The "sqlite" command is
2821 ** used to open a new SQLite database. See the DbMain() routine above
2822 ** for additional information.
2824 EXTERN
int Jim_sqlite3Init(Jim_Interp
*interp
){
2825 Jim_CreateCommand(interp
, "sqlite3", DbMain
, 0, 0);
2826 Jim_PackageProvide(interp
, "sqlite3", PACKAGE_VERSION
, 0);
2827 Jim_CreateCommand(interp
, "sqlite", DbMain
, 0, 0);
2828 Jim_PackageProvide(interp
, "sqlite", PACKAGE_VERSION
, 0);