blob: b8e2a5ee3ced187089f8ac013f7df49051851d64 [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**
drhfa173a72002-07-10 21:26:00 +000014** $Id: tclsqlite.c,v 1.37 2002/07/10 21:26:01 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"
drh17a68932001-01-31 13:28:08 +000019#include "tcl.h"
drh75897232000-05-29 14:26:00 +000020#include <stdlib.h>
21#include <string.h>
drhce927062001-11-09 13:41:09 +000022#include <assert.h>
drh75897232000-05-29 14:26:00 +000023
24/*
drh98808ba2001-10-18 12:34:46 +000025** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
26** have to do a translation when going between the two. Set the
27** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
28** this translation.
29*/
30#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
31# define UTF_TRANSLATION_NEEDED 1
32#endif
33
34/*
drhbec3f402000-08-04 13:49:02 +000035** There is one instance of this structure for each SQLite database
36** that has been opened by the SQLite TCL interface.
37*/
38typedef struct SqliteDb SqliteDb;
39struct SqliteDb {
40 sqlite *db; /* The "real" database structure */
41 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000042 char *zBusy; /* The busy callback routine */
drhbec3f402000-08-04 13:49:02 +000043};
44
45/*
drh75897232000-05-29 14:26:00 +000046** An instance of this structure passes information thru the sqlite
47** logic from the original TCL command into the callback routine.
48*/
49typedef struct CallbackData CallbackData;
50struct CallbackData {
51 Tcl_Interp *interp; /* The TCL interpreter */
52 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000053 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000054 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000055 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000056 int nColName; /* Number of entries in the azColName[] array */
57 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000058};
drh297ecf12001-04-05 15:57:13 +000059
drh6d4abfb2001-10-22 02:58:08 +000060#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000061/*
drh75897232000-05-29 14:26:00 +000062** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000063**
64** This version is used when TCL expects UTF-8 data but the database
65** uses the ISO8859 format. A translation must occur from ISO8859 into
66** UTF-8.
drh75897232000-05-29 14:26:00 +000067*/
68static int DbEvalCallback(
69 void *clientData, /* An instance of CallbackData */
70 int nCol, /* Number of columns in the result */
71 char ** azCol, /* Data for each column */
72 char ** azN /* Name for each column */
73){
74 CallbackData *cbData = (CallbackData*)clientData;
75 int i, rc;
drh297ecf12001-04-05 15:57:13 +000076 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000077 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000078 if( cbData->azColName==0 ){
79 assert( cbData->once );
80 cbData->once = 0;
81 if( cbData->zArray[0] ){
82 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +000083 }
drhce927062001-11-09 13:41:09 +000084 cbData->azColName = malloc( nCol*sizeof(char*) );
85 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +000086 cbData->nColName = nCol;
87 for(i=0; i<nCol; i++){
88 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +000089 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
90 if( cbData->azColName[i] ){
91 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
92 }else{
93 return 1;
drh6d4abfb2001-10-22 02:58:08 +000094 }
drhce927062001-11-09 13:41:09 +000095 if( cbData->zArray[0] ){
drhfa173a72002-07-10 21:26:00 +000096 Tcl_DString dType;
97 Tcl_DStringInit(&dType);
drhce927062001-11-09 13:41:09 +000098 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
99 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drhfa173a72002-07-10 21:26:00 +0000100 Tcl_DStringAppend(&dType, "typeof:", -1);
101 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
102 Tcl_DStringFree(&dCol);
103 Tcl_ExternalToUtfDString(NULL, azN[i+argc+1], -1, &dCol);
104 Tcl_SetVar2(cbData->interp, cbData->zArray,
105 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
106 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
107 Tcl_DStringFree(&dType);
drhce927062001-11-09 13:41:09 +0000108 }
drhfa173a72002-07-10 21:26:00 +0000109
drh6d4abfb2001-10-22 02:58:08 +0000110 Tcl_DStringFree(&dCol);
111 }
drh6d4abfb2001-10-22 02:58:08 +0000112 }
113 if( azCol!=0 ){
114 if( cbData->zArray[0] ){
115 for(i=0; i<nCol; i++){
116 char *z = azCol[i];
117 if( z==0 ) z = "";
118 Tcl_DStringInit(&dCol);
119 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
120 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
121 Tcl_DStringValue(&dCol), 0);
122 Tcl_DStringFree(&dCol);
123 }
124 }else{
125 for(i=0; i<nCol; i++){
126 char *z = azCol[i];
127 if( z==0 ) z = "";
128 Tcl_DStringInit(&dCol);
129 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
130 Tcl_SetVar(cbData->interp, cbData->azColName[i],
131 Tcl_DStringValue(&dCol), 0);
132 Tcl_DStringFree(&dCol);
133 }
134 }
135 }
136 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
137 if( rc==TCL_CONTINUE ) rc = TCL_OK;
138 cbData->tcl_rc = rc;
139 return rc!=TCL_OK;
140}
141#endif /* UTF_TRANSLATION_NEEDED */
142
143#ifndef UTF_TRANSLATION_NEEDED
144/*
145** Called for each row of the result.
146**
147** This version is used when either of the following is true:
148**
149** (1) This version of TCL uses UTF-8 and the data in the
150** SQLite database is already in the UTF-8 format.
151**
152** (2) This version of TCL uses ISO8859 and the data in the
153** SQLite database is already in the ISO8859 format.
154*/
155static int DbEvalCallback(
156 void *clientData, /* An instance of CallbackData */
157 int nCol, /* Number of columns in the result */
158 char ** azCol, /* Data for each column */
159 char ** azN /* Name for each column */
160){
161 CallbackData *cbData = (CallbackData*)clientData;
162 int i, rc;
drh6a535342001-10-19 16:44:56 +0000163 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
164 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
165 for(i=0; i<nCol; i++){
drhfa173a72002-07-10 21:26:00 +0000166 char *z;
drh6a535342001-10-19 16:44:56 +0000167 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
168 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drhfa173a72002-07-10 21:26:00 +0000169 z = sqlite_mprintf("typeof:%s", azN[i]);
170 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol+1],
171 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
172 sqlite_freemem(z);
drh6a535342001-10-19 16:44:56 +0000173 }
174 cbData->once = 0;
175 }
176 if( azCol!=0 ){
177 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000178 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000179 char *z = azCol[i];
180 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000181 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000182 }
183 }else{
184 for(i=0; i<nCol; i++){
185 char *z = azCol[i];
186 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000187 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000188 }
189 }
drh75897232000-05-29 14:26:00 +0000190 }
drh6d313162000-09-21 13:01:35 +0000191 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000192 if( rc==TCL_CONTINUE ) rc = TCL_OK;
193 cbData->tcl_rc = rc;
194 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000195}
drh6d4abfb2001-10-22 02:58:08 +0000196#endif
drh75897232000-05-29 14:26:00 +0000197
198/*
drh6d313162000-09-21 13:01:35 +0000199** This is an alternative callback for database queries. Instead
200** of invoking a TCL script to handle the result, this callback just
201** appends each column of the result to a list. After the query
202** is complete, the list is returned.
203*/
204static int DbEvalCallback2(
205 void *clientData, /* An instance of CallbackData */
206 int nCol, /* Number of columns in the result */
207 char ** azCol, /* Data for each column */
208 char ** azN /* Name for each column */
209){
210 Tcl_Obj *pList = (Tcl_Obj*)clientData;
211 int i;
drh6a535342001-10-19 16:44:56 +0000212 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000213 for(i=0; i<nCol; i++){
214 Tcl_Obj *pElem;
215 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000216#ifdef UTF_TRANSLATION_NEEDED
217 Tcl_DString dCol;
218 Tcl_DStringInit(&dCol);
219 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
220 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
221 Tcl_DStringFree(&dCol);
222#else
drh6d313162000-09-21 13:01:35 +0000223 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000224#endif
drh6d313162000-09-21 13:01:35 +0000225 }else{
226 pElem = Tcl_NewObj();
227 }
228 Tcl_ListObjAppendElement(0, pList, pElem);
229 }
230 return 0;
231}
232
233/*
drh75897232000-05-29 14:26:00 +0000234** Called when the command is deleted.
235*/
236static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000237 SqliteDb *pDb = (SqliteDb*)db;
238 sqlite_close(pDb->db);
239 if( pDb->zBusy ){
240 Tcl_Free(pDb->zBusy);
241 }
242 Tcl_Free((char*)pDb);
243}
244
245/*
246** This routine is called when a database file is locked while trying
247** to execute SQL.
248*/
249static int DbBusyHandler(void *cd, const char *zTable, int nTries){
250 SqliteDb *pDb = (SqliteDb*)cd;
251 int rc;
252 char zVal[30];
253 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000254 Tcl_DString cmd;
255
256 Tcl_DStringInit(&cmd);
257 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
258 Tcl_DStringAppendElement(&cmd, zTable);
259 sprintf(zVal, " %d", nTries);
260 Tcl_DStringAppend(&cmd, zVal, -1);
261 zCmd = Tcl_DStringValue(&cmd);
262 rc = Tcl_Eval(pDb->interp, zCmd);
263 Tcl_DStringFree(&cmd);
264 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
265 return 0;
266 }
267 return 1;
drh75897232000-05-29 14:26:00 +0000268}
269
270/*
271** The "sqlite" command below creates a new Tcl command for each
272** connection it opens to an SQLite database. This routine is invoked
273** whenever one of those connection-specific commands is executed
274** in Tcl. For example, if you run Tcl code like this:
275**
276** sqlite db1 "my_database"
277** db1 close
278**
279** The first command opens a connection to the "my_database" database
280** and calls that connection "db1". The second command causes this
281** subroutine to be invoked.
282*/
drh6d313162000-09-21 13:01:35 +0000283static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000284 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000285 int choice;
drh0de8c112002-07-06 16:32:14 +0000286 static const char *DB_strs[] = {
drh411995d2002-06-25 19:31:18 +0000287 "busy", "changes", "close",
288 "complete", "eval", "last_insert_rowid",
289 "open_aux_file", "timeout", 0
drh6d313162000-09-21 13:01:35 +0000290 };
drh411995d2002-06-25 19:31:18 +0000291 enum DB_enum {
292 DB_BUSY, DB_CHANGES, DB_CLOSE,
293 DB_COMPLETE, DB_EVAL, DB_LAST_INSERT_ROWID,
294 DB_OPEN_AUX_FILE, DB_TIMEOUT,
drh6d313162000-09-21 13:01:35 +0000295 };
296
297 if( objc<2 ){
298 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000299 return TCL_ERROR;
300 }
drh411995d2002-06-25 19:31:18 +0000301 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000302 return TCL_ERROR;
303 }
304
drh411995d2002-06-25 19:31:18 +0000305 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000306
drhbec3f402000-08-04 13:49:02 +0000307 /* $db busy ?CALLBACK?
308 **
309 ** Invoke the given callback if an SQL statement attempts to open
310 ** a locked database file.
311 */
drh6d313162000-09-21 13:01:35 +0000312 case DB_BUSY: {
313 if( objc>3 ){
314 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000315 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000316 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000317 if( pDb->zBusy ){
318 Tcl_AppendResult(interp, pDb->zBusy, 0);
319 }
320 }else{
drh6d313162000-09-21 13:01:35 +0000321 char *zBusy;
322 int len;
drhbec3f402000-08-04 13:49:02 +0000323 if( pDb->zBusy ){
324 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000325 }
drh6d313162000-09-21 13:01:35 +0000326 zBusy = Tcl_GetStringFromObj(objv[2], &len);
327 if( zBusy && len>0 ){
328 pDb->zBusy = Tcl_Alloc( len + 1 );
329 strcpy(pDb->zBusy, zBusy);
330 }else{
331 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000332 }
333 if( pDb->zBusy ){
334 pDb->interp = interp;
335 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000336 }else{
337 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000338 }
339 }
drh6d313162000-09-21 13:01:35 +0000340 break;
341 }
drhbec3f402000-08-04 13:49:02 +0000342
drhc8d30ac2002-04-12 10:08:59 +0000343 /*
344 ** $db changes
345 **
346 ** Return the number of rows that were modified, inserted, or deleted by
347 ** the most recent "eval".
348 */
349 case DB_CHANGES: {
350 Tcl_Obj *pResult;
351 int nChange;
352 if( objc!=2 ){
353 Tcl_WrongNumArgs(interp, 2, objv, "");
354 return TCL_ERROR;
355 }
356 nChange = sqlite_changes(pDb->db);
357 pResult = Tcl_GetObjResult(interp);
358 Tcl_SetIntObj(pResult, nChange);
359 break;
360 }
361
drh75897232000-05-29 14:26:00 +0000362 /* $db close
363 **
364 ** Shutdown the database
365 */
drh6d313162000-09-21 13:01:35 +0000366 case DB_CLOSE: {
367 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
368 break;
369 }
drh75897232000-05-29 14:26:00 +0000370
371 /* $db complete SQL
372 **
373 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
374 ** additional lines of input are needed. This is similar to the
375 ** built-in "info complete" command of Tcl.
376 */
drh6d313162000-09-21 13:01:35 +0000377 case DB_COMPLETE: {
378 Tcl_Obj *pResult;
379 int isComplete;
380 if( objc!=3 ){
381 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000382 return TCL_ERROR;
383 }
drh6d313162000-09-21 13:01:35 +0000384 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
385 pResult = Tcl_GetObjResult(interp);
386 Tcl_SetBooleanObj(pResult, isComplete);
387 break;
388 }
drh75897232000-05-29 14:26:00 +0000389
390 /*
391 ** $db eval $sql ?array { ...code... }?
392 **
393 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000394 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000395 ** If "array" and "code" are omitted, then no callback is every invoked.
396 ** If "array" is an empty string, then the values are placed in variables
397 ** that have the same name as the fields extracted by the query.
398 */
drh6d313162000-09-21 13:01:35 +0000399 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000400 CallbackData cbData;
401 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000402 char *zSql;
drh75897232000-05-29 14:26:00 +0000403 int rc;
drh297ecf12001-04-05 15:57:13 +0000404#ifdef UTF_TRANSLATION_NEEDED
405 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000406 int i;
drh297ecf12001-04-05 15:57:13 +0000407#endif
drh75897232000-05-29 14:26:00 +0000408
drh6d313162000-09-21 13:01:35 +0000409 if( objc!=5 && objc!=3 ){
410 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000411 return TCL_ERROR;
412 }
drhbec3f402000-08-04 13:49:02 +0000413 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000414 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000415#ifdef UTF_TRANSLATION_NEEDED
416 Tcl_DStringInit(&dSql);
417 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
418 zSql = Tcl_DStringValue(&dSql);
419#endif
drh6d313162000-09-21 13:01:35 +0000420 Tcl_IncrRefCount(objv[2]);
421 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000422 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000423 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000424 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
425 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000426 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000427 cbData.nColName = 0;
428 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000429 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000430 Tcl_IncrRefCount(objv[3]);
431 Tcl_IncrRefCount(objv[4]);
432 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
433 Tcl_DecrRefCount(objv[4]);
434 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000435 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000436 }else{
drh6d313162000-09-21 13:01:35 +0000437 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000438 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000439 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
440 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000441 }
442 if( zErrMsg ){
443 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
444 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000445 rc = TCL_ERROR;
drh6d4abfb2001-10-22 02:58:08 +0000446 }else if( rc!=SQLITE_OK && rc!=SQLITE_ABORT ){
447 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
448 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000449 }else{
450 rc = cbData.tcl_rc;
drh75897232000-05-29 14:26:00 +0000451 }
drh6d313162000-09-21 13:01:35 +0000452 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000453#ifdef UTF_TRANSLATION_NEEDED
454 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000455 if( objc==5 && cbData.azColName ){
456 for(i=0; i<cbData.nColName; i++){
457 if( cbData.azColName[i] ) free(cbData.azColName[i]);
458 }
459 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000460 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000461 }
drh297ecf12001-04-05 15:57:13 +0000462#endif
drh75897232000-05-29 14:26:00 +0000463 return rc;
drh6d313162000-09-21 13:01:35 +0000464 }
drhbec3f402000-08-04 13:49:02 +0000465
466 /*
drhaf9ff332002-01-16 21:00:27 +0000467 ** $db last_insert_rowid
468 **
469 ** Return an integer which is the ROWID for the most recent insert.
470 */
471 case DB_LAST_INSERT_ROWID: {
472 Tcl_Obj *pResult;
473 int rowid;
474 if( objc!=2 ){
475 Tcl_WrongNumArgs(interp, 2, objv, "");
476 return TCL_ERROR;
477 }
478 rowid = sqlite_last_insert_rowid(pDb->db);
479 pResult = Tcl_GetObjResult(interp);
480 Tcl_SetIntObj(pResult, rowid);
481 break;
482 }
483
484 /*
drh411995d2002-06-25 19:31:18 +0000485 ** $db open_aux_file FILENAME
486 **
487 ** Begin using FILENAME as the database file used to store temporary
488 ** tables.
489 */
490 case DB_OPEN_AUX_FILE: {
491 const char *zFilename;
492 char *zErrMsg = 0;
493 int rc;
494 if( objc!=3 ){
495 Tcl_WrongNumArgs(interp, 2, objv, "FILENAME");
496 return TCL_ERROR;
497 }
498 zFilename = Tcl_GetStringFromObj(objv[2], 0);
499 rc = sqlite_open_aux_file(pDb->db, zFilename, &zErrMsg);
500 if( rc!=0 ){
501 if( zErrMsg ){
502 Tcl_AppendResult(interp, zErrMsg, 0);
503 free(zErrMsg);
504 }else{
505 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
506 }
507 return TCL_ERROR;
508 }
509 break;
510 }
511
512 /*
drhbec3f402000-08-04 13:49:02 +0000513 ** $db timeout MILLESECONDS
514 **
515 ** Delay for the number of milliseconds specified when a file is locked.
516 */
drh6d313162000-09-21 13:01:35 +0000517 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000518 int ms;
drh6d313162000-09-21 13:01:35 +0000519 if( objc!=3 ){
520 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000521 return TCL_ERROR;
522 }
drh6d313162000-09-21 13:01:35 +0000523 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000524 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000525 break;
drh75897232000-05-29 14:26:00 +0000526 }
drh6d313162000-09-21 13:01:35 +0000527 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000528 return TCL_OK;
529}
530
531/*
532** sqlite DBNAME FILENAME ?MODE?
533**
534** This is the main Tcl command. When the "sqlite" Tcl command is
535** invoked, this routine runs to process that command.
536**
537** The first argument, DBNAME, is an arbitrary name for a new
538** database connection. This command creates a new command named
539** DBNAME that is used to control that connection. The database
540** connection is deleted when the DBNAME command is deleted.
541**
542** The second argument is the name of the directory that contains
543** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000544**
545** For testing purposes, we also support the following:
546**
547** sqlite -encoding
548**
549** Return the encoding used by LIKE and GLOB operators. Choices
550** are UTF-8 and iso8859.
551**
552** sqlite -tcl-uses-utf
553**
554** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
555** not. Used by tests to make sure the library was compiled
556** correctly.
drh75897232000-05-29 14:26:00 +0000557*/
558static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
559 int mode;
drhbec3f402000-08-04 13:49:02 +0000560 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000561 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000562 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000563 if( argc==2 ){
564 if( strcmp(argv[1],"-encoding")==0 ){
565 Tcl_AppendResult(interp,sqlite_encoding,0);
566 return TCL_OK;
567 }
568 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
569#ifdef TCL_UTF_MAX
570 Tcl_AppendResult(interp,"1",0);
571#else
572 Tcl_AppendResult(interp,"0",0);
573#endif
574 return TCL_OK;
575 }
576 }
drh75897232000-05-29 14:26:00 +0000577 if( argc!=3 && argc!=4 ){
578 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
579 " HANDLE FILENAME ?MODE?\"", 0);
580 return TCL_ERROR;
581 }
582 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000583 mode = 0666;
drh75897232000-05-29 14:26:00 +0000584 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
585 return TCL_ERROR;
586 }
587 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000588 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000589 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000590 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
591 return TCL_ERROR;
592 }
593 memset(p, 0, sizeof(*p));
594 p->db = sqlite_open(argv[2], mode, &zErrMsg);
595 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000596 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000597 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000598 free(zErrMsg);
599 return TCL_ERROR;
600 }
drh6d313162000-09-21 13:01:35 +0000601 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000602
drh06b27182002-06-26 20:06:05 +0000603 /* The return value is the value of the sqlite* pointer
604 */
605 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000606 if( strncmp(zBuf,"0x",2) ){
607 sprintf(zBuf, "0x%p", p->db);
608 }
drh06b27182002-06-26 20:06:05 +0000609 Tcl_AppendResult(interp, zBuf, 0);
610
drhc22bd472002-05-10 13:14:07 +0000611 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000612 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000613 */
drh28b4e482002-03-11 02:06:13 +0000614#ifdef SQLITE_TEST
615 {
drhc22bd472002-05-10 13:14:07 +0000616 extern void Md5_Register(sqlite*);
617 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000618 }
drh28b4e482002-03-11 02:06:13 +0000619#endif
drh75897232000-05-29 14:26:00 +0000620 return TCL_OK;
621}
622
623/*
drh90ca9752001-09-28 17:47:14 +0000624** Provide a dummy Tcl_InitStubs if we are using this as a static
625** library.
626*/
627#ifndef USE_TCL_STUBS
628# undef Tcl_InitStubs
629# define Tcl_InitStubs(a,b,c)
630#endif
631
632/*
drh75897232000-05-29 14:26:00 +0000633** Initialize this module.
634**
635** This Tcl module contains only a single new Tcl command named "sqlite".
636** (Hence there is no namespace. There is no point in using a namespace
637** if the extension only supplies one new name!) The "sqlite" command is
638** used to open a new SQLite database. See the DbMain() routine above
639** for additional information.
640*/
641int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000642 Tcl_InitStubs(interp, "8.0", 0);
643 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000644 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +0000645 return TCL_OK;
646}
647int Tclsqlite_Init(Tcl_Interp *interp){
648 Tcl_InitStubs(interp, "8.0", 0);
drh75897232000-05-29 14:26:00 +0000649 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000650 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +0000651 return TCL_OK;
652}
653int Sqlite_SafeInit(Tcl_Interp *interp){
654 return TCL_OK;
655}
drh90ca9752001-09-28 17:47:14 +0000656int Tclsqlite_SafeInit(Tcl_Interp *interp){
657 return TCL_OK;
658}
drh75897232000-05-29 14:26:00 +0000659
drh3cebbde2000-10-19 14:59:27 +0000660#if 0
drh75897232000-05-29 14:26:00 +0000661/*
662** If compiled using mktclapp, this routine runs to initialize
663** everything.
664*/
665int Et_AppInit(Tcl_Interp *interp){
666 return Sqlite_Init(interp);
667}
drh3cebbde2000-10-19 14:59:27 +0000668#endif
drh348784e2000-05-29 20:41:49 +0000669
670/*
671** If the macro TCLSH is defined and is one, then put in code for the
672** "main" routine that will initialize Tcl.
673*/
674#if defined(TCLSH) && TCLSH==1
675static char zMainloop[] =
676 "set line {}\n"
677 "while {![eof stdin]} {\n"
678 "if {$line!=\"\"} {\n"
679 "puts -nonewline \"> \"\n"
680 "} else {\n"
681 "puts -nonewline \"% \"\n"
682 "}\n"
683 "flush stdout\n"
684 "append line [gets stdin]\n"
685 "if {[info complete $line]} {\n"
686 "if {[catch {uplevel #0 $line} result]} {\n"
687 "puts stderr \"Error: $result\"\n"
688 "} elseif {$result!=\"\"} {\n"
689 "puts $result\n"
690 "}\n"
691 "set line {}\n"
692 "} else {\n"
693 "append line \\n\n"
694 "}\n"
695 "}\n"
696;
697
698#define TCLSH_MAIN main /* Needed to fake out mktclapp */
699int TCLSH_MAIN(int argc, char **argv){
700 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000701 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000702 interp = Tcl_CreateInterp();
703 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000704#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000705 {
706 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000707 extern int Sqlitetest2_Init(Tcl_Interp*);
708 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000709 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000710 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000711 Sqlitetest2_Init(interp);
712 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000713 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000714 }
715#endif
drh348784e2000-05-29 20:41:49 +0000716 if( argc>=2 ){
717 int i;
718 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
719 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
720 for(i=2; i<argc; i++){
721 Tcl_SetVar(interp, "argv", argv[i],
722 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
723 }
724 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +0000725 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +0000726 if( zInfo==0 ) zInfo = interp->result;
727 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000728 return 1;
729 }
730 }else{
731 Tcl_GlobalEval(interp, zMainloop);
732 }
733 return 0;
734}
735#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000736
737#endif /* !defined(NO_TCL) */