175897234Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 375897234Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 675897234Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 1075897234Sdrh ** 1175897234Sdrh ************************************************************************* 1275897234Sdrh ** A TCL Interface to SQLite 1375897234Sdrh ** 14*c22bd47dSdrh ** $Id: tclsqlite.c,v 1.32 2002/05/10 13:14:07 drh Exp $ 1575897234Sdrh */ 166d31316cSdrh #ifndef NO_TCL /* Omit this whole file if TCL is unavailable */ 176d31316cSdrh 1875897234Sdrh #include "sqlite.h" 1917a68934Sdrh #include "tcl.h" 2075897234Sdrh #include <stdlib.h> 2175897234Sdrh #include <string.h> 22ce927065Sdrh #include <assert.h> 2375897234Sdrh 2475897234Sdrh /* 2598808babSdrh ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we 2698808babSdrh ** have to do a translation when going between the two. Set the 2798808babSdrh ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do 2898808babSdrh ** this translation. 2998808babSdrh */ 3098808babSdrh #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) 3198808babSdrh # define UTF_TRANSLATION_NEEDED 1 3298808babSdrh #endif 3398808babSdrh 3498808babSdrh /* 35bec3f402Sdrh ** There is one instance of this structure for each SQLite database 36bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 37bec3f402Sdrh */ 38bec3f402Sdrh typedef struct SqliteDb SqliteDb; 39bec3f402Sdrh struct SqliteDb { 40bec3f402Sdrh sqlite *db; /* The "real" database structure */ 41bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 426d31316cSdrh char *zBusy; /* The busy callback routine */ 43bec3f402Sdrh }; 44bec3f402Sdrh 45bec3f402Sdrh /* 4675897234Sdrh ** An instance of this structure passes information thru the sqlite 4775897234Sdrh ** logic from the original TCL command into the callback routine. 4875897234Sdrh */ 4975897234Sdrh typedef struct CallbackData CallbackData; 5075897234Sdrh struct CallbackData { 5175897234Sdrh Tcl_Interp *interp; /* The TCL interpreter */ 5275897234Sdrh char *zArray; /* The array into which data is written */ 536d31316cSdrh Tcl_Obj *pCode; /* The code to execute for each row */ 54ce927065Sdrh int once; /* Set for first callback only */ 55960e8c63Sdrh int tcl_rc; /* Return code from TCL script */ 5698808babSdrh int nColName; /* Number of entries in the azColName[] array */ 5798808babSdrh char **azColName; /* Column names translated to UTF-8 */ 5898808babSdrh }; 59297ecf14Sdrh 606d4abfbeSdrh #ifdef UTF_TRANSLATION_NEEDED 61297ecf14Sdrh /* 6275897234Sdrh ** Called for each row of the result. 636d4abfbeSdrh ** 646d4abfbeSdrh ** This version is used when TCL expects UTF-8 data but the database 656d4abfbeSdrh ** uses the ISO8859 format. A translation must occur from ISO8859 into 666d4abfbeSdrh ** UTF-8. 6775897234Sdrh */ 6875897234Sdrh static int DbEvalCallback( 6975897234Sdrh void *clientData, /* An instance of CallbackData */ 7075897234Sdrh int nCol, /* Number of columns in the result */ 7175897234Sdrh char ** azCol, /* Data for each column */ 7275897234Sdrh char ** azN /* Name for each column */ 7375897234Sdrh ){ 7475897234Sdrh CallbackData *cbData = (CallbackData*)clientData; 7575897234Sdrh int i, rc; 76297ecf14Sdrh Tcl_DString dCol; 776d4abfbeSdrh Tcl_DStringInit(&dCol); 78ce927065Sdrh if( cbData->azColName==0 ){ 79ce927065Sdrh assert( cbData->once ); 80ce927065Sdrh cbData->once = 0; 81ce927065Sdrh if( cbData->zArray[0] ){ 826d4abfbeSdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); 83ce927065Sdrh } 846d4abfbeSdrh cbData->azColName = malloc( nCol*sizeof(char*) ); 856d4abfbeSdrh if( cbData->azColName==0 ){ return 1; } 866d4abfbeSdrh cbData->nColName = nCol; 876d4abfbeSdrh for(i=0; i<nCol; i++){ 886d4abfbeSdrh Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol); 896d4abfbeSdrh cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 ); 906d4abfbeSdrh if( cbData->azColName[i] ){ 916d4abfbeSdrh strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol)); 92ce927065Sdrh }else{ 93ce927065Sdrh return 1; 946d4abfbeSdrh } 95ce927065Sdrh if( cbData->zArray[0] ){ 96ce927065Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", 97ce927065Sdrh Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE); 986d4abfbeSdrh } 996d4abfbeSdrh Tcl_DStringFree(&dCol); 1006d4abfbeSdrh } 1016d4abfbeSdrh } 1026d4abfbeSdrh if( azCol!=0 ){ 1036d4abfbeSdrh if( cbData->zArray[0] ){ 1046d4abfbeSdrh for(i=0; i<nCol; i++){ 1056d4abfbeSdrh char *z = azCol[i]; 1066d4abfbeSdrh if( z==0 ) z = ""; 1076d4abfbeSdrh Tcl_DStringInit(&dCol); 1086d4abfbeSdrh Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); 1096d4abfbeSdrh Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i], 1106d4abfbeSdrh Tcl_DStringValue(&dCol), 0); 1116d4abfbeSdrh Tcl_DStringFree(&dCol); 1126d4abfbeSdrh } 1136d4abfbeSdrh }else{ 1146d4abfbeSdrh for(i=0; i<nCol; i++){ 1156d4abfbeSdrh char *z = azCol[i]; 1166d4abfbeSdrh if( z==0 ) z = ""; 1176d4abfbeSdrh Tcl_DStringInit(&dCol); 1186d4abfbeSdrh Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); 1196d4abfbeSdrh Tcl_SetVar(cbData->interp, cbData->azColName[i], 1206d4abfbeSdrh Tcl_DStringValue(&dCol), 0); 1216d4abfbeSdrh Tcl_DStringFree(&dCol); 1226d4abfbeSdrh } 1236d4abfbeSdrh } 1246d4abfbeSdrh } 1256d4abfbeSdrh rc = Tcl_EvalObj(cbData->interp, cbData->pCode); 1266d4abfbeSdrh if( rc==TCL_CONTINUE ) rc = TCL_OK; 1276d4abfbeSdrh cbData->tcl_rc = rc; 1286d4abfbeSdrh return rc!=TCL_OK; 1296d4abfbeSdrh } 1306d4abfbeSdrh #endif /* UTF_TRANSLATION_NEEDED */ 1316d4abfbeSdrh 1326d4abfbeSdrh #ifndef UTF_TRANSLATION_NEEDED 1336d4abfbeSdrh /* 1346d4abfbeSdrh ** Called for each row of the result. 1356d4abfbeSdrh ** 1366d4abfbeSdrh ** This version is used when either of the following is true: 1376d4abfbeSdrh ** 1386d4abfbeSdrh ** (1) This version of TCL uses UTF-8 and the data in the 1396d4abfbeSdrh ** SQLite database is already in the UTF-8 format. 1406d4abfbeSdrh ** 1416d4abfbeSdrh ** (2) This version of TCL uses ISO8859 and the data in the 1426d4abfbeSdrh ** SQLite database is already in the ISO8859 format. 1436d4abfbeSdrh */ 1446d4abfbeSdrh static int DbEvalCallback( 1456d4abfbeSdrh void *clientData, /* An instance of CallbackData */ 1466d4abfbeSdrh int nCol, /* Number of columns in the result */ 1476d4abfbeSdrh char ** azCol, /* Data for each column */ 1486d4abfbeSdrh char ** azN /* Name for each column */ 1496d4abfbeSdrh ){ 1506d4abfbeSdrh CallbackData *cbData = (CallbackData*)clientData; 1516d4abfbeSdrh int i, rc; 1526a535340Sdrh if( azCol==0 || (cbData->once && cbData->zArray[0]) ){ 1539b0d0a8bSdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); 15475897234Sdrh for(i=0; i<nCol; i++){ 15575897234Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], 15675897234Sdrh TCL_LIST_ELEMENT|TCL_APPEND_VALUE); 15775897234Sdrh } 1586a535340Sdrh cbData->once = 0; 15975897234Sdrh } 1606a535340Sdrh if( azCol!=0 ){ 1616a535340Sdrh if( cbData->zArray[0] ){ 16275897234Sdrh for(i=0; i<nCol; i++){ 163c61053b7Sdrh char *z = azCol[i]; 164c61053b7Sdrh if( z==0 ) z = ""; 165c61053b7Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); 16675897234Sdrh } 16775897234Sdrh }else{ 16875897234Sdrh for(i=0; i<nCol; i++){ 169c61053b7Sdrh char *z = azCol[i]; 170c61053b7Sdrh if( z==0 ) z = ""; 171c61053b7Sdrh Tcl_SetVar(cbData->interp, azN[i], z, 0); 17275897234Sdrh } 17375897234Sdrh } 1746a535340Sdrh } 1756d31316cSdrh rc = Tcl_EvalObj(cbData->interp, cbData->pCode); 176960e8c63Sdrh if( rc==TCL_CONTINUE ) rc = TCL_OK; 177960e8c63Sdrh cbData->tcl_rc = rc; 178960e8c63Sdrh return rc!=TCL_OK; 17975897234Sdrh } 1806d4abfbeSdrh #endif 18175897234Sdrh 18275897234Sdrh /* 1836d31316cSdrh ** This is an alternative callback for database queries. Instead 1846d31316cSdrh ** of invoking a TCL script to handle the result, this callback just 1856d31316cSdrh ** appends each column of the result to a list. After the query 1866d31316cSdrh ** is complete, the list is returned. 1876d31316cSdrh */ 1886d31316cSdrh static int DbEvalCallback2( 1896d31316cSdrh void *clientData, /* An instance of CallbackData */ 1906d31316cSdrh int nCol, /* Number of columns in the result */ 1916d31316cSdrh char ** azCol, /* Data for each column */ 1926d31316cSdrh char ** azN /* Name for each column */ 1936d31316cSdrh ){ 1946d31316cSdrh Tcl_Obj *pList = (Tcl_Obj*)clientData; 1956d31316cSdrh int i; 1966a535340Sdrh if( azCol==0 ) return 0; 1976d31316cSdrh for(i=0; i<nCol; i++){ 1986d31316cSdrh Tcl_Obj *pElem; 1996d31316cSdrh if( azCol[i] && *azCol[i] ){ 200297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 201297ecf14Sdrh Tcl_DString dCol; 202297ecf14Sdrh Tcl_DStringInit(&dCol); 203297ecf14Sdrh Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol); 204297ecf14Sdrh pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); 205297ecf14Sdrh Tcl_DStringFree(&dCol); 206297ecf14Sdrh #else 2076d31316cSdrh pElem = Tcl_NewStringObj(azCol[i], -1); 208297ecf14Sdrh #endif 2096d31316cSdrh }else{ 2106d31316cSdrh pElem = Tcl_NewObj(); 2116d31316cSdrh } 2126d31316cSdrh Tcl_ListObjAppendElement(0, pList, pElem); 2136d31316cSdrh } 2146d31316cSdrh return 0; 2156d31316cSdrh } 2166d31316cSdrh 2176d31316cSdrh /* 21875897234Sdrh ** Called when the command is deleted. 21975897234Sdrh */ 22075897234Sdrh static void DbDeleteCmd(void *db){ 221bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 222bec3f402Sdrh sqlite_close(pDb->db); 223bec3f402Sdrh if( pDb->zBusy ){ 224bec3f402Sdrh Tcl_Free(pDb->zBusy); 225bec3f402Sdrh } 226bec3f402Sdrh Tcl_Free((char*)pDb); 227bec3f402Sdrh } 228bec3f402Sdrh 229bec3f402Sdrh /* 230bec3f402Sdrh ** This routine is called when a database file is locked while trying 231bec3f402Sdrh ** to execute SQL. 232bec3f402Sdrh */ 233bec3f402Sdrh static int DbBusyHandler(void *cd, const char *zTable, int nTries){ 234bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 235bec3f402Sdrh int rc; 236bec3f402Sdrh char zVal[30]; 237bec3f402Sdrh char *zCmd; 238bec3f402Sdrh Tcl_DString cmd; 239bec3f402Sdrh 240bec3f402Sdrh Tcl_DStringInit(&cmd); 241bec3f402Sdrh Tcl_DStringAppend(&cmd, pDb->zBusy, -1); 242bec3f402Sdrh Tcl_DStringAppendElement(&cmd, zTable); 243bec3f402Sdrh sprintf(zVal, " %d", nTries); 244bec3f402Sdrh Tcl_DStringAppend(&cmd, zVal, -1); 245bec3f402Sdrh zCmd = Tcl_DStringValue(&cmd); 246bec3f402Sdrh rc = Tcl_Eval(pDb->interp, zCmd); 247bec3f402Sdrh Tcl_DStringFree(&cmd); 248bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 249bec3f402Sdrh return 0; 250bec3f402Sdrh } 251bec3f402Sdrh return 1; 25275897234Sdrh } 25375897234Sdrh 25475897234Sdrh /* 25575897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 25675897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 25775897234Sdrh ** whenever one of those connection-specific commands is executed 25875897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 25975897234Sdrh ** 26075897234Sdrh ** sqlite db1 "my_database" 26175897234Sdrh ** db1 close 26275897234Sdrh ** 26375897234Sdrh ** The first command opens a connection to the "my_database" database 26475897234Sdrh ** and calls that connection "db1". The second command causes this 26575897234Sdrh ** subroutine to be invoked. 26675897234Sdrh */ 2676d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 268bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 2696d31316cSdrh int choice; 2706d31316cSdrh static char *DB_optStrs[] = { 271c8d30ac1Sdrh "busy", "changes", "close", "complete", 272c8d30ac1Sdrh "eval", "last_insert_rowid", "timeout", 0 2736d31316cSdrh }; 2746d31316cSdrh enum DB_opts { 275c8d30ac1Sdrh DB_BUSY, DB_CHANGES, DB_CLOSE, DB_COMPLETE, 276c8d30ac1Sdrh DB_EVAL, DB_LAST_INSERT_ROWID, DB_TIMEOUT 2776d31316cSdrh }; 2786d31316cSdrh 2796d31316cSdrh if( objc<2 ){ 2806d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 28175897234Sdrh return TCL_ERROR; 28275897234Sdrh } 2836d31316cSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){ 2846d31316cSdrh return TCL_ERROR; 2856d31316cSdrh } 2866d31316cSdrh 2876d31316cSdrh switch( (enum DB_opts)choice ){ 28875897234Sdrh 289bec3f402Sdrh /* $db busy ?CALLBACK? 290bec3f402Sdrh ** 291bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 292bec3f402Sdrh ** a locked database file. 293bec3f402Sdrh */ 2946d31316cSdrh case DB_BUSY: { 2956d31316cSdrh if( objc>3 ){ 2966d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 297bec3f402Sdrh return TCL_ERROR; 2986d31316cSdrh }else if( objc==2 ){ 299bec3f402Sdrh if( pDb->zBusy ){ 300bec3f402Sdrh Tcl_AppendResult(interp, pDb->zBusy, 0); 301bec3f402Sdrh } 302bec3f402Sdrh }else{ 3036d31316cSdrh char *zBusy; 3046d31316cSdrh int len; 305bec3f402Sdrh if( pDb->zBusy ){ 306bec3f402Sdrh Tcl_Free(pDb->zBusy); 3076d31316cSdrh } 3086d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 3096d31316cSdrh if( zBusy && len>0 ){ 3106d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 3116d31316cSdrh strcpy(pDb->zBusy, zBusy); 3126d31316cSdrh }else{ 313bec3f402Sdrh pDb->zBusy = 0; 314bec3f402Sdrh } 315bec3f402Sdrh if( pDb->zBusy ){ 316bec3f402Sdrh pDb->interp = interp; 317bec3f402Sdrh sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); 3186d31316cSdrh }else{ 3196d31316cSdrh sqlite_busy_handler(pDb->db, 0, 0); 320bec3f402Sdrh } 321bec3f402Sdrh } 3226d31316cSdrh break; 3236d31316cSdrh } 324bec3f402Sdrh 325c8d30ac1Sdrh /* 326c8d30ac1Sdrh ** $db changes 327c8d30ac1Sdrh ** 328c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 329c8d30ac1Sdrh ** the most recent "eval". 330c8d30ac1Sdrh */ 331c8d30ac1Sdrh case DB_CHANGES: { 332c8d30ac1Sdrh Tcl_Obj *pResult; 333c8d30ac1Sdrh int nChange; 334c8d30ac1Sdrh if( objc!=2 ){ 335c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 336c8d30ac1Sdrh return TCL_ERROR; 337c8d30ac1Sdrh } 338c8d30ac1Sdrh nChange = sqlite_changes(pDb->db); 339c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 340c8d30ac1Sdrh Tcl_SetIntObj(pResult, nChange); 341c8d30ac1Sdrh break; 342c8d30ac1Sdrh } 343c8d30ac1Sdrh 34475897234Sdrh /* $db close 34575897234Sdrh ** 34675897234Sdrh ** Shutdown the database 34775897234Sdrh */ 3486d31316cSdrh case DB_CLOSE: { 3496d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 3506d31316cSdrh break; 3516d31316cSdrh } 35275897234Sdrh 35375897234Sdrh /* $db complete SQL 35475897234Sdrh ** 35575897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 35675897234Sdrh ** additional lines of input are needed. This is similar to the 35775897234Sdrh ** built-in "info complete" command of Tcl. 35875897234Sdrh */ 3596d31316cSdrh case DB_COMPLETE: { 3606d31316cSdrh Tcl_Obj *pResult; 3616d31316cSdrh int isComplete; 3626d31316cSdrh if( objc!=3 ){ 3636d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 36475897234Sdrh return TCL_ERROR; 36575897234Sdrh } 3666d31316cSdrh isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); 3676d31316cSdrh pResult = Tcl_GetObjResult(interp); 3686d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 3696d31316cSdrh break; 3706d31316cSdrh } 37175897234Sdrh 37275897234Sdrh /* 37375897234Sdrh ** $db eval $sql ?array { ...code... }? 37475897234Sdrh ** 37575897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 376bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 37775897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 37875897234Sdrh ** If "array" is an empty string, then the values are placed in variables 37975897234Sdrh ** that have the same name as the fields extracted by the query. 38075897234Sdrh */ 3816d31316cSdrh case DB_EVAL: { 38275897234Sdrh CallbackData cbData; 38375897234Sdrh char *zErrMsg; 3846d31316cSdrh char *zSql; 38575897234Sdrh int rc; 386297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 387297ecf14Sdrh Tcl_DString dSql; 3886d4abfbeSdrh int i; 389297ecf14Sdrh #endif 39075897234Sdrh 3916d31316cSdrh if( objc!=5 && objc!=3 ){ 3926d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); 39375897234Sdrh return TCL_ERROR; 39475897234Sdrh } 395bec3f402Sdrh pDb->interp = interp; 3966d31316cSdrh zSql = Tcl_GetStringFromObj(objv[2], 0); 397297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 398297ecf14Sdrh Tcl_DStringInit(&dSql); 399297ecf14Sdrh Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); 400297ecf14Sdrh zSql = Tcl_DStringValue(&dSql); 401297ecf14Sdrh #endif 4026d31316cSdrh Tcl_IncrRefCount(objv[2]); 4036d31316cSdrh if( objc==5 ){ 40475897234Sdrh cbData.interp = interp; 405dcc581ccSdrh cbData.once = 1; 4066d31316cSdrh cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); 4076d31316cSdrh cbData.pCode = objv[4]; 408960e8c63Sdrh cbData.tcl_rc = TCL_OK; 4096d4abfbeSdrh cbData.nColName = 0; 4106d4abfbeSdrh cbData.azColName = 0; 41175897234Sdrh zErrMsg = 0; 4126d31316cSdrh Tcl_IncrRefCount(objv[3]); 4136d31316cSdrh Tcl_IncrRefCount(objv[4]); 4146d31316cSdrh rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); 4156d31316cSdrh Tcl_DecrRefCount(objv[4]); 4166d31316cSdrh Tcl_DecrRefCount(objv[3]); 417960e8c63Sdrh if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } 41875897234Sdrh }else{ 4196d31316cSdrh Tcl_Obj *pList = Tcl_NewObj(); 420960e8c63Sdrh cbData.tcl_rc = TCL_OK; 4216d31316cSdrh rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); 4226d31316cSdrh Tcl_SetObjResult(interp, pList); 42375897234Sdrh } 42475897234Sdrh if( zErrMsg ){ 42575897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 42675897234Sdrh free(zErrMsg); 427960e8c63Sdrh rc = TCL_ERROR; 4286d4abfbeSdrh }else if( rc!=SQLITE_OK && rc!=SQLITE_ABORT ){ 4296d4abfbeSdrh Tcl_AppendResult(interp, sqlite_error_string(rc), 0); 4306d4abfbeSdrh rc = TCL_ERROR; 431960e8c63Sdrh }else{ 432960e8c63Sdrh rc = cbData.tcl_rc; 43375897234Sdrh } 4346d31316cSdrh Tcl_DecrRefCount(objv[2]); 435297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 436297ecf14Sdrh Tcl_DStringFree(&dSql); 4376d4abfbeSdrh if( objc==5 && cbData.azColName ){ 4386d4abfbeSdrh for(i=0; i<cbData.nColName; i++){ 4396d4abfbeSdrh if( cbData.azColName[i] ) free(cbData.azColName[i]); 4406d4abfbeSdrh } 4416d4abfbeSdrh free(cbData.azColName); 442ce927065Sdrh cbData.azColName = 0; 4436d4abfbeSdrh } 444297ecf14Sdrh #endif 44575897234Sdrh return rc; 4466d31316cSdrh } 447bec3f402Sdrh 448bec3f402Sdrh /* 449af9ff33aSdrh ** $db last_insert_rowid 450af9ff33aSdrh ** 451af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 452af9ff33aSdrh */ 453af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 454af9ff33aSdrh Tcl_Obj *pResult; 455af9ff33aSdrh int rowid; 456af9ff33aSdrh if( objc!=2 ){ 457af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 458af9ff33aSdrh return TCL_ERROR; 459af9ff33aSdrh } 460af9ff33aSdrh rowid = sqlite_last_insert_rowid(pDb->db); 461af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 462af9ff33aSdrh Tcl_SetIntObj(pResult, rowid); 463af9ff33aSdrh break; 464af9ff33aSdrh } 465af9ff33aSdrh 466af9ff33aSdrh /* 467bec3f402Sdrh ** $db timeout MILLESECONDS 468bec3f402Sdrh ** 469bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 470bec3f402Sdrh */ 4716d31316cSdrh case DB_TIMEOUT: { 472bec3f402Sdrh int ms; 4736d31316cSdrh if( objc!=3 ){ 4746d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 475bec3f402Sdrh return TCL_ERROR; 47675897234Sdrh } 4776d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 478bec3f402Sdrh sqlite_busy_timeout(pDb->db, ms); 4796d31316cSdrh break; 48075897234Sdrh } 4816d31316cSdrh } /* End of the SWITCH statement */ 48275897234Sdrh return TCL_OK; 48375897234Sdrh } 48475897234Sdrh 48575897234Sdrh /* 48675897234Sdrh ** sqlite DBNAME FILENAME ?MODE? 48775897234Sdrh ** 48875897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 48975897234Sdrh ** invoked, this routine runs to process that command. 49075897234Sdrh ** 49175897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 49275897234Sdrh ** database connection. This command creates a new command named 49375897234Sdrh ** DBNAME that is used to control that connection. The database 49475897234Sdrh ** connection is deleted when the DBNAME command is deleted. 49575897234Sdrh ** 49675897234Sdrh ** The second argument is the name of the directory that contains 49775897234Sdrh ** the sqlite database that is to be accessed. 498fbc3eab8Sdrh ** 499fbc3eab8Sdrh ** For testing purposes, we also support the following: 500fbc3eab8Sdrh ** 501fbc3eab8Sdrh ** sqlite -encoding 502fbc3eab8Sdrh ** 503fbc3eab8Sdrh ** Return the encoding used by LIKE and GLOB operators. Choices 504fbc3eab8Sdrh ** are UTF-8 and iso8859. 505fbc3eab8Sdrh ** 506fbc3eab8Sdrh ** sqlite -tcl-uses-utf 507fbc3eab8Sdrh ** 508fbc3eab8Sdrh ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if 509fbc3eab8Sdrh ** not. Used by tests to make sure the library was compiled 510fbc3eab8Sdrh ** correctly. 51175897234Sdrh */ 51275897234Sdrh static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ 51375897234Sdrh int mode; 514bec3f402Sdrh SqliteDb *p; 51575897234Sdrh char *zErrMsg; 516fbc3eab8Sdrh if( argc==2 ){ 517fbc3eab8Sdrh if( strcmp(argv[1],"-encoding")==0 ){ 518fbc3eab8Sdrh Tcl_AppendResult(interp,sqlite_encoding,0); 519fbc3eab8Sdrh return TCL_OK; 520fbc3eab8Sdrh } 521fbc3eab8Sdrh if( strcmp(argv[1],"-tcl-uses-utf")==0 ){ 522fbc3eab8Sdrh #ifdef TCL_UTF_MAX 523fbc3eab8Sdrh Tcl_AppendResult(interp,"1",0); 524fbc3eab8Sdrh #else 525fbc3eab8Sdrh Tcl_AppendResult(interp,"0",0); 526fbc3eab8Sdrh #endif 527fbc3eab8Sdrh return TCL_OK; 528fbc3eab8Sdrh } 529fbc3eab8Sdrh } 53075897234Sdrh if( argc!=3 && argc!=4 ){ 53175897234Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 53275897234Sdrh " HANDLE FILENAME ?MODE?\"", 0); 53375897234Sdrh return TCL_ERROR; 53475897234Sdrh } 53575897234Sdrh if( argc==3 ){ 53658b9576bSdrh mode = 0666; 53775897234Sdrh }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ 53875897234Sdrh return TCL_ERROR; 53975897234Sdrh } 54075897234Sdrh zErrMsg = 0; 5414cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 54275897234Sdrh if( p==0 ){ 543bec3f402Sdrh Tcl_SetResult(interp, "malloc failed", TCL_STATIC); 544bec3f402Sdrh return TCL_ERROR; 545bec3f402Sdrh } 546bec3f402Sdrh memset(p, 0, sizeof(*p)); 547bec3f402Sdrh p->db = sqlite_open(argv[2], mode, &zErrMsg); 548bec3f402Sdrh if( p->db==0 ){ 54975897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 550bec3f402Sdrh Tcl_Free((char*)p); 55175897234Sdrh free(zErrMsg); 55275897234Sdrh return TCL_ERROR; 55375897234Sdrh } 5546d31316cSdrh Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); 555*c22bd47dSdrh 556*c22bd47dSdrh /* If compiled with SQLITE_TEST turned on, then register the "md5sum" 557*c22bd47dSdrh ** SQL function and return an integer which is the memory address of 558*c22bd47dSdrh ** the underlying sqlite* pointer. 559*c22bd47dSdrh */ 56028b4e489Sdrh #ifdef SQLITE_TEST 56128b4e489Sdrh { 562*c22bd47dSdrh char zBuf[40]; 56328b4e489Sdrh extern void Md5_Register(sqlite*); 56428b4e489Sdrh Md5_Register(p->db); 565*c22bd47dSdrh sprintf(zBuf, "%d", (int)p->db); 566*c22bd47dSdrh Tcl_AppendResult(interp, zBuf, 0); 56728b4e489Sdrh } 56828b4e489Sdrh #endif 56975897234Sdrh return TCL_OK; 57075897234Sdrh } 57175897234Sdrh 57275897234Sdrh /* 57390ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 57490ca9753Sdrh ** library. 57590ca9753Sdrh */ 57690ca9753Sdrh #ifndef USE_TCL_STUBS 57790ca9753Sdrh # undef Tcl_InitStubs 57890ca9753Sdrh # define Tcl_InitStubs(a,b,c) 57990ca9753Sdrh #endif 58090ca9753Sdrh 58190ca9753Sdrh /* 58275897234Sdrh ** Initialize this module. 58375897234Sdrh ** 58475897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 58575897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 58675897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 58775897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 58875897234Sdrh ** for additional information. 58975897234Sdrh */ 59075897234Sdrh int Sqlite_Init(Tcl_Interp *interp){ 59190ca9753Sdrh Tcl_InitStubs(interp, "8.0", 0); 59290ca9753Sdrh Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); 5936d4abfbeSdrh Tcl_PkgProvide(interp, "sqlite", "2.0"); 59490ca9753Sdrh return TCL_OK; 59590ca9753Sdrh } 59690ca9753Sdrh int Tclsqlite_Init(Tcl_Interp *interp){ 59790ca9753Sdrh Tcl_InitStubs(interp, "8.0", 0); 59875897234Sdrh Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); 5996d4abfbeSdrh Tcl_PkgProvide(interp, "sqlite", "2.0"); 60075897234Sdrh return TCL_OK; 60175897234Sdrh } 60275897234Sdrh int Sqlite_SafeInit(Tcl_Interp *interp){ 60375897234Sdrh return TCL_OK; 60475897234Sdrh } 60590ca9753Sdrh int Tclsqlite_SafeInit(Tcl_Interp *interp){ 60690ca9753Sdrh return TCL_OK; 60790ca9753Sdrh } 60875897234Sdrh 6093cebbde3Sdrh #if 0 61075897234Sdrh /* 61175897234Sdrh ** If compiled using mktclapp, this routine runs to initialize 61275897234Sdrh ** everything. 61375897234Sdrh */ 61475897234Sdrh int Et_AppInit(Tcl_Interp *interp){ 61575897234Sdrh return Sqlite_Init(interp); 61675897234Sdrh } 6173cebbde3Sdrh #endif 618348784efSdrh 619348784efSdrh /* 620348784efSdrh ** If the macro TCLSH is defined and is one, then put in code for the 621348784efSdrh ** "main" routine that will initialize Tcl. 622348784efSdrh */ 623348784efSdrh #if defined(TCLSH) && TCLSH==1 624348784efSdrh static char zMainloop[] = 625348784efSdrh "set line {}\n" 626348784efSdrh "while {![eof stdin]} {\n" 627348784efSdrh "if {$line!=\"\"} {\n" 628348784efSdrh "puts -nonewline \"> \"\n" 629348784efSdrh "} else {\n" 630348784efSdrh "puts -nonewline \"% \"\n" 631348784efSdrh "}\n" 632348784efSdrh "flush stdout\n" 633348784efSdrh "append line [gets stdin]\n" 634348784efSdrh "if {[info complete $line]} {\n" 635348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 636348784efSdrh "puts stderr \"Error: $result\"\n" 637348784efSdrh "} elseif {$result!=\"\"} {\n" 638348784efSdrh "puts $result\n" 639348784efSdrh "}\n" 640348784efSdrh "set line {}\n" 641348784efSdrh "} else {\n" 642348784efSdrh "append line \\n\n" 643348784efSdrh "}\n" 644348784efSdrh "}\n" 645348784efSdrh ; 646348784efSdrh 647348784efSdrh #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 648348784efSdrh int TCLSH_MAIN(int argc, char **argv){ 649348784efSdrh Tcl_Interp *interp; 650297ecf14Sdrh Tcl_FindExecutable(argv[0]); 651348784efSdrh interp = Tcl_CreateInterp(); 652348784efSdrh Sqlite_Init(interp); 653d9b0257aSdrh #ifdef SQLITE_TEST 654d1bf3512Sdrh { 655d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 6565c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 6575c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 658efc251daSdrh extern int Md5_Init(Tcl_Interp*); 659d1bf3512Sdrh Sqlitetest1_Init(interp); 6605c4d9703Sdrh Sqlitetest2_Init(interp); 6615c4d9703Sdrh Sqlitetest3_Init(interp); 662efc251daSdrh Md5_Init(interp); 663d1bf3512Sdrh } 664d1bf3512Sdrh #endif 665348784efSdrh if( argc>=2 ){ 666348784efSdrh int i; 667348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 668348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 669348784efSdrh for(i=2; i<argc; i++){ 670348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 671348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 672348784efSdrh } 673348784efSdrh if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 674c61053b7Sdrh char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 675c61053b7Sdrh if( zInfo==0 ) zInfo = interp->result; 676c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 677348784efSdrh return 1; 678348784efSdrh } 679348784efSdrh }else{ 680348784efSdrh Tcl_GlobalEval(interp, zMainloop); 681348784efSdrh } 682348784efSdrh return 0; 683348784efSdrh } 684348784efSdrh #endif /* TCLSH */ 6856d31316cSdrh 6866d31316cSdrh #endif /* !defined(NO_TCL) */ 687