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*fa173a76Sdrh ** $Id: tclsqlite.c,v 1.37 2002/07/10 21:26:01 drh Exp $ 1575897234Sdrh */ 166d31316cSdrh #ifndef NO_TCL /* Omit this whole file if TCL is unavailable */ 176d31316cSdrh 1806b2718aSdrh #include "sqliteInt.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] ){ 96*fa173a76Sdrh Tcl_DString dType; 97*fa173a76Sdrh Tcl_DStringInit(&dType); 98ce927065Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", 99ce927065Sdrh Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE); 100*fa173a76Sdrh Tcl_DStringAppend(&dType, "typeof:", -1); 101*fa173a76Sdrh Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1); 102*fa173a76Sdrh Tcl_DStringFree(&dCol); 103*fa173a76Sdrh Tcl_ExternalToUtfDString(NULL, azN[i+argc+1], -1, &dCol); 104*fa173a76Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, 105*fa173a76Sdrh Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol), 106*fa173a76Sdrh TCL_LIST_ELEMENT|TCL_APPEND_VALUE); 107*fa173a76Sdrh Tcl_DStringFree(&dType); 1086d4abfbeSdrh } 109*fa173a76Sdrh 1106d4abfbeSdrh Tcl_DStringFree(&dCol); 1116d4abfbeSdrh } 1126d4abfbeSdrh } 1136d4abfbeSdrh if( azCol!=0 ){ 1146d4abfbeSdrh if( cbData->zArray[0] ){ 1156d4abfbeSdrh for(i=0; i<nCol; i++){ 1166d4abfbeSdrh char *z = azCol[i]; 1176d4abfbeSdrh if( z==0 ) z = ""; 1186d4abfbeSdrh Tcl_DStringInit(&dCol); 1196d4abfbeSdrh Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); 1206d4abfbeSdrh Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i], 1216d4abfbeSdrh Tcl_DStringValue(&dCol), 0); 1226d4abfbeSdrh Tcl_DStringFree(&dCol); 1236d4abfbeSdrh } 1246d4abfbeSdrh }else{ 1256d4abfbeSdrh for(i=0; i<nCol; i++){ 1266d4abfbeSdrh char *z = azCol[i]; 1276d4abfbeSdrh if( z==0 ) z = ""; 1286d4abfbeSdrh Tcl_DStringInit(&dCol); 1296d4abfbeSdrh Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); 1306d4abfbeSdrh Tcl_SetVar(cbData->interp, cbData->azColName[i], 1316d4abfbeSdrh Tcl_DStringValue(&dCol), 0); 1326d4abfbeSdrh Tcl_DStringFree(&dCol); 1336d4abfbeSdrh } 1346d4abfbeSdrh } 1356d4abfbeSdrh } 1366d4abfbeSdrh rc = Tcl_EvalObj(cbData->interp, cbData->pCode); 1376d4abfbeSdrh if( rc==TCL_CONTINUE ) rc = TCL_OK; 1386d4abfbeSdrh cbData->tcl_rc = rc; 1396d4abfbeSdrh return rc!=TCL_OK; 1406d4abfbeSdrh } 1416d4abfbeSdrh #endif /* UTF_TRANSLATION_NEEDED */ 1426d4abfbeSdrh 1436d4abfbeSdrh #ifndef UTF_TRANSLATION_NEEDED 1446d4abfbeSdrh /* 1456d4abfbeSdrh ** Called for each row of the result. 1466d4abfbeSdrh ** 1476d4abfbeSdrh ** This version is used when either of the following is true: 1486d4abfbeSdrh ** 1496d4abfbeSdrh ** (1) This version of TCL uses UTF-8 and the data in the 1506d4abfbeSdrh ** SQLite database is already in the UTF-8 format. 1516d4abfbeSdrh ** 1526d4abfbeSdrh ** (2) This version of TCL uses ISO8859 and the data in the 1536d4abfbeSdrh ** SQLite database is already in the ISO8859 format. 1546d4abfbeSdrh */ 1556d4abfbeSdrh static int DbEvalCallback( 1566d4abfbeSdrh void *clientData, /* An instance of CallbackData */ 1576d4abfbeSdrh int nCol, /* Number of columns in the result */ 1586d4abfbeSdrh char ** azCol, /* Data for each column */ 1596d4abfbeSdrh char ** azN /* Name for each column */ 1606d4abfbeSdrh ){ 1616d4abfbeSdrh CallbackData *cbData = (CallbackData*)clientData; 1626d4abfbeSdrh int i, rc; 1636a535340Sdrh if( azCol==0 || (cbData->once && cbData->zArray[0]) ){ 1649b0d0a8bSdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); 16575897234Sdrh for(i=0; i<nCol; i++){ 166*fa173a76Sdrh char *z; 16775897234Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], 16875897234Sdrh TCL_LIST_ELEMENT|TCL_APPEND_VALUE); 169*fa173a76Sdrh z = sqlite_mprintf("typeof:%s", azN[i]); 170*fa173a76Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol+1], 171*fa173a76Sdrh TCL_LIST_ELEMENT|TCL_APPEND_VALUE); 172*fa173a76Sdrh sqlite_freemem(z); 17375897234Sdrh } 1746a535340Sdrh cbData->once = 0; 17575897234Sdrh } 1766a535340Sdrh if( azCol!=0 ){ 1776a535340Sdrh if( cbData->zArray[0] ){ 17875897234Sdrh for(i=0; i<nCol; i++){ 179c61053b7Sdrh char *z = azCol[i]; 180c61053b7Sdrh if( z==0 ) z = ""; 181c61053b7Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); 18275897234Sdrh } 18375897234Sdrh }else{ 18475897234Sdrh for(i=0; i<nCol; i++){ 185c61053b7Sdrh char *z = azCol[i]; 186c61053b7Sdrh if( z==0 ) z = ""; 187c61053b7Sdrh Tcl_SetVar(cbData->interp, azN[i], z, 0); 18875897234Sdrh } 18975897234Sdrh } 1906a535340Sdrh } 1916d31316cSdrh rc = Tcl_EvalObj(cbData->interp, cbData->pCode); 192960e8c63Sdrh if( rc==TCL_CONTINUE ) rc = TCL_OK; 193960e8c63Sdrh cbData->tcl_rc = rc; 194960e8c63Sdrh return rc!=TCL_OK; 19575897234Sdrh } 1966d4abfbeSdrh #endif 19775897234Sdrh 19875897234Sdrh /* 1996d31316cSdrh ** This is an alternative callback for database queries. Instead 2006d31316cSdrh ** of invoking a TCL script to handle the result, this callback just 2016d31316cSdrh ** appends each column of the result to a list. After the query 2026d31316cSdrh ** is complete, the list is returned. 2036d31316cSdrh */ 2046d31316cSdrh static int DbEvalCallback2( 2056d31316cSdrh void *clientData, /* An instance of CallbackData */ 2066d31316cSdrh int nCol, /* Number of columns in the result */ 2076d31316cSdrh char ** azCol, /* Data for each column */ 2086d31316cSdrh char ** azN /* Name for each column */ 2096d31316cSdrh ){ 2106d31316cSdrh Tcl_Obj *pList = (Tcl_Obj*)clientData; 2116d31316cSdrh int i; 2126a535340Sdrh if( azCol==0 ) return 0; 2136d31316cSdrh for(i=0; i<nCol; i++){ 2146d31316cSdrh Tcl_Obj *pElem; 2156d31316cSdrh if( azCol[i] && *azCol[i] ){ 216297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 217297ecf14Sdrh Tcl_DString dCol; 218297ecf14Sdrh Tcl_DStringInit(&dCol); 219297ecf14Sdrh Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol); 220297ecf14Sdrh pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); 221297ecf14Sdrh Tcl_DStringFree(&dCol); 222297ecf14Sdrh #else 2236d31316cSdrh pElem = Tcl_NewStringObj(azCol[i], -1); 224297ecf14Sdrh #endif 2256d31316cSdrh }else{ 2266d31316cSdrh pElem = Tcl_NewObj(); 2276d31316cSdrh } 2286d31316cSdrh Tcl_ListObjAppendElement(0, pList, pElem); 2296d31316cSdrh } 2306d31316cSdrh return 0; 2316d31316cSdrh } 2326d31316cSdrh 2336d31316cSdrh /* 23475897234Sdrh ** Called when the command is deleted. 23575897234Sdrh */ 23675897234Sdrh static void DbDeleteCmd(void *db){ 237bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 238bec3f402Sdrh sqlite_close(pDb->db); 239bec3f402Sdrh if( pDb->zBusy ){ 240bec3f402Sdrh Tcl_Free(pDb->zBusy); 241bec3f402Sdrh } 242bec3f402Sdrh Tcl_Free((char*)pDb); 243bec3f402Sdrh } 244bec3f402Sdrh 245bec3f402Sdrh /* 246bec3f402Sdrh ** This routine is called when a database file is locked while trying 247bec3f402Sdrh ** to execute SQL. 248bec3f402Sdrh */ 249bec3f402Sdrh static int DbBusyHandler(void *cd, const char *zTable, int nTries){ 250bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 251bec3f402Sdrh int rc; 252bec3f402Sdrh char zVal[30]; 253bec3f402Sdrh char *zCmd; 254bec3f402Sdrh Tcl_DString cmd; 255bec3f402Sdrh 256bec3f402Sdrh Tcl_DStringInit(&cmd); 257bec3f402Sdrh Tcl_DStringAppend(&cmd, pDb->zBusy, -1); 258bec3f402Sdrh Tcl_DStringAppendElement(&cmd, zTable); 259bec3f402Sdrh sprintf(zVal, " %d", nTries); 260bec3f402Sdrh Tcl_DStringAppend(&cmd, zVal, -1); 261bec3f402Sdrh zCmd = Tcl_DStringValue(&cmd); 262bec3f402Sdrh rc = Tcl_Eval(pDb->interp, zCmd); 263bec3f402Sdrh Tcl_DStringFree(&cmd); 264bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 265bec3f402Sdrh return 0; 266bec3f402Sdrh } 267bec3f402Sdrh return 1; 26875897234Sdrh } 26975897234Sdrh 27075897234Sdrh /* 27175897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 27275897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 27375897234Sdrh ** whenever one of those connection-specific commands is executed 27475897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 27575897234Sdrh ** 27675897234Sdrh ** sqlite db1 "my_database" 27775897234Sdrh ** db1 close 27875897234Sdrh ** 27975897234Sdrh ** The first command opens a connection to the "my_database" database 28075897234Sdrh ** and calls that connection "db1". The second command causes this 28175897234Sdrh ** subroutine to be invoked. 28275897234Sdrh */ 2836d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 284bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 2856d31316cSdrh int choice; 2860de8c112Sdrh static const char *DB_strs[] = { 287411995dcSdrh "busy", "changes", "close", 288411995dcSdrh "complete", "eval", "last_insert_rowid", 289411995dcSdrh "open_aux_file", "timeout", 0 2906d31316cSdrh }; 291411995dcSdrh enum DB_enum { 292411995dcSdrh DB_BUSY, DB_CHANGES, DB_CLOSE, 293411995dcSdrh DB_COMPLETE, DB_EVAL, DB_LAST_INSERT_ROWID, 294411995dcSdrh DB_OPEN_AUX_FILE, DB_TIMEOUT, 2956d31316cSdrh }; 2966d31316cSdrh 2976d31316cSdrh if( objc<2 ){ 2986d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 29975897234Sdrh return TCL_ERROR; 30075897234Sdrh } 301411995dcSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 3026d31316cSdrh return TCL_ERROR; 3036d31316cSdrh } 3046d31316cSdrh 305411995dcSdrh switch( (enum DB_enum)choice ){ 30675897234Sdrh 307bec3f402Sdrh /* $db busy ?CALLBACK? 308bec3f402Sdrh ** 309bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 310bec3f402Sdrh ** a locked database file. 311bec3f402Sdrh */ 3126d31316cSdrh case DB_BUSY: { 3136d31316cSdrh if( objc>3 ){ 3146d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 315bec3f402Sdrh return TCL_ERROR; 3166d31316cSdrh }else if( objc==2 ){ 317bec3f402Sdrh if( pDb->zBusy ){ 318bec3f402Sdrh Tcl_AppendResult(interp, pDb->zBusy, 0); 319bec3f402Sdrh } 320bec3f402Sdrh }else{ 3216d31316cSdrh char *zBusy; 3226d31316cSdrh int len; 323bec3f402Sdrh if( pDb->zBusy ){ 324bec3f402Sdrh Tcl_Free(pDb->zBusy); 3256d31316cSdrh } 3266d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 3276d31316cSdrh if( zBusy && len>0 ){ 3286d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 3296d31316cSdrh strcpy(pDb->zBusy, zBusy); 3306d31316cSdrh }else{ 331bec3f402Sdrh pDb->zBusy = 0; 332bec3f402Sdrh } 333bec3f402Sdrh if( pDb->zBusy ){ 334bec3f402Sdrh pDb->interp = interp; 335bec3f402Sdrh sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); 3366d31316cSdrh }else{ 3376d31316cSdrh sqlite_busy_handler(pDb->db, 0, 0); 338bec3f402Sdrh } 339bec3f402Sdrh } 3406d31316cSdrh break; 3416d31316cSdrh } 342bec3f402Sdrh 343c8d30ac1Sdrh /* 344c8d30ac1Sdrh ** $db changes 345c8d30ac1Sdrh ** 346c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 347c8d30ac1Sdrh ** the most recent "eval". 348c8d30ac1Sdrh */ 349c8d30ac1Sdrh case DB_CHANGES: { 350c8d30ac1Sdrh Tcl_Obj *pResult; 351c8d30ac1Sdrh int nChange; 352c8d30ac1Sdrh if( objc!=2 ){ 353c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 354c8d30ac1Sdrh return TCL_ERROR; 355c8d30ac1Sdrh } 356c8d30ac1Sdrh nChange = sqlite_changes(pDb->db); 357c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 358c8d30ac1Sdrh Tcl_SetIntObj(pResult, nChange); 359c8d30ac1Sdrh break; 360c8d30ac1Sdrh } 361c8d30ac1Sdrh 36275897234Sdrh /* $db close 36375897234Sdrh ** 36475897234Sdrh ** Shutdown the database 36575897234Sdrh */ 3666d31316cSdrh case DB_CLOSE: { 3676d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 3686d31316cSdrh break; 3696d31316cSdrh } 37075897234Sdrh 37175897234Sdrh /* $db complete SQL 37275897234Sdrh ** 37375897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 37475897234Sdrh ** additional lines of input are needed. This is similar to the 37575897234Sdrh ** built-in "info complete" command of Tcl. 37675897234Sdrh */ 3776d31316cSdrh case DB_COMPLETE: { 3786d31316cSdrh Tcl_Obj *pResult; 3796d31316cSdrh int isComplete; 3806d31316cSdrh if( objc!=3 ){ 3816d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 38275897234Sdrh return TCL_ERROR; 38375897234Sdrh } 3846d31316cSdrh isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); 3856d31316cSdrh pResult = Tcl_GetObjResult(interp); 3866d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 3876d31316cSdrh break; 3886d31316cSdrh } 38975897234Sdrh 39075897234Sdrh /* 39175897234Sdrh ** $db eval $sql ?array { ...code... }? 39275897234Sdrh ** 39375897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 394bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 39575897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 39675897234Sdrh ** If "array" is an empty string, then the values are placed in variables 39775897234Sdrh ** that have the same name as the fields extracted by the query. 39875897234Sdrh */ 3996d31316cSdrh case DB_EVAL: { 40075897234Sdrh CallbackData cbData; 40175897234Sdrh char *zErrMsg; 4026d31316cSdrh char *zSql; 40375897234Sdrh int rc; 404297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 405297ecf14Sdrh Tcl_DString dSql; 4066d4abfbeSdrh int i; 407297ecf14Sdrh #endif 40875897234Sdrh 4096d31316cSdrh if( objc!=5 && objc!=3 ){ 4106d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); 41175897234Sdrh return TCL_ERROR; 41275897234Sdrh } 413bec3f402Sdrh pDb->interp = interp; 4146d31316cSdrh zSql = Tcl_GetStringFromObj(objv[2], 0); 415297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 416297ecf14Sdrh Tcl_DStringInit(&dSql); 417297ecf14Sdrh Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); 418297ecf14Sdrh zSql = Tcl_DStringValue(&dSql); 419297ecf14Sdrh #endif 4206d31316cSdrh Tcl_IncrRefCount(objv[2]); 4216d31316cSdrh if( objc==5 ){ 42275897234Sdrh cbData.interp = interp; 423dcc581ccSdrh cbData.once = 1; 4246d31316cSdrh cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); 4256d31316cSdrh cbData.pCode = objv[4]; 426960e8c63Sdrh cbData.tcl_rc = TCL_OK; 4276d4abfbeSdrh cbData.nColName = 0; 4286d4abfbeSdrh cbData.azColName = 0; 42975897234Sdrh zErrMsg = 0; 4306d31316cSdrh Tcl_IncrRefCount(objv[3]); 4316d31316cSdrh Tcl_IncrRefCount(objv[4]); 4326d31316cSdrh rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); 4336d31316cSdrh Tcl_DecrRefCount(objv[4]); 4346d31316cSdrh Tcl_DecrRefCount(objv[3]); 435960e8c63Sdrh if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } 43675897234Sdrh }else{ 4376d31316cSdrh Tcl_Obj *pList = Tcl_NewObj(); 438960e8c63Sdrh cbData.tcl_rc = TCL_OK; 4396d31316cSdrh rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); 4406d31316cSdrh Tcl_SetObjResult(interp, pList); 44175897234Sdrh } 44275897234Sdrh if( zErrMsg ){ 44375897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 44475897234Sdrh free(zErrMsg); 445960e8c63Sdrh rc = TCL_ERROR; 4466d4abfbeSdrh }else if( rc!=SQLITE_OK && rc!=SQLITE_ABORT ){ 4476d4abfbeSdrh Tcl_AppendResult(interp, sqlite_error_string(rc), 0); 4486d4abfbeSdrh rc = TCL_ERROR; 449960e8c63Sdrh }else{ 450960e8c63Sdrh rc = cbData.tcl_rc; 45175897234Sdrh } 4526d31316cSdrh Tcl_DecrRefCount(objv[2]); 453297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 454297ecf14Sdrh Tcl_DStringFree(&dSql); 4556d4abfbeSdrh if( objc==5 && cbData.azColName ){ 4566d4abfbeSdrh for(i=0; i<cbData.nColName; i++){ 4576d4abfbeSdrh if( cbData.azColName[i] ) free(cbData.azColName[i]); 4586d4abfbeSdrh } 4596d4abfbeSdrh free(cbData.azColName); 460ce927065Sdrh cbData.azColName = 0; 4616d4abfbeSdrh } 462297ecf14Sdrh #endif 46375897234Sdrh return rc; 4646d31316cSdrh } 465bec3f402Sdrh 466bec3f402Sdrh /* 467af9ff33aSdrh ** $db last_insert_rowid 468af9ff33aSdrh ** 469af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 470af9ff33aSdrh */ 471af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 472af9ff33aSdrh Tcl_Obj *pResult; 473af9ff33aSdrh int rowid; 474af9ff33aSdrh if( objc!=2 ){ 475af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 476af9ff33aSdrh return TCL_ERROR; 477af9ff33aSdrh } 478af9ff33aSdrh rowid = sqlite_last_insert_rowid(pDb->db); 479af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 480af9ff33aSdrh Tcl_SetIntObj(pResult, rowid); 481af9ff33aSdrh break; 482af9ff33aSdrh } 483af9ff33aSdrh 484af9ff33aSdrh /* 485411995dcSdrh ** $db open_aux_file FILENAME 486411995dcSdrh ** 487411995dcSdrh ** Begin using FILENAME as the database file used to store temporary 488411995dcSdrh ** tables. 489411995dcSdrh */ 490411995dcSdrh case DB_OPEN_AUX_FILE: { 491411995dcSdrh const char *zFilename; 492411995dcSdrh char *zErrMsg = 0; 493411995dcSdrh int rc; 494411995dcSdrh if( objc!=3 ){ 495411995dcSdrh Tcl_WrongNumArgs(interp, 2, objv, "FILENAME"); 496411995dcSdrh return TCL_ERROR; 497411995dcSdrh } 498411995dcSdrh zFilename = Tcl_GetStringFromObj(objv[2], 0); 499411995dcSdrh rc = sqlite_open_aux_file(pDb->db, zFilename, &zErrMsg); 500411995dcSdrh if( rc!=0 ){ 501411995dcSdrh if( zErrMsg ){ 502411995dcSdrh Tcl_AppendResult(interp, zErrMsg, 0); 503411995dcSdrh free(zErrMsg); 504411995dcSdrh }else{ 505411995dcSdrh Tcl_AppendResult(interp, sqlite_error_string(rc), 0); 506411995dcSdrh } 507411995dcSdrh return TCL_ERROR; 508411995dcSdrh } 509411995dcSdrh break; 510411995dcSdrh } 511411995dcSdrh 512411995dcSdrh /* 513bec3f402Sdrh ** $db timeout MILLESECONDS 514bec3f402Sdrh ** 515bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 516bec3f402Sdrh */ 5176d31316cSdrh case DB_TIMEOUT: { 518bec3f402Sdrh int ms; 5196d31316cSdrh if( objc!=3 ){ 5206d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 521bec3f402Sdrh return TCL_ERROR; 52275897234Sdrh } 5236d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 524bec3f402Sdrh sqlite_busy_timeout(pDb->db, ms); 5256d31316cSdrh break; 52675897234Sdrh } 5276d31316cSdrh } /* End of the SWITCH statement */ 52875897234Sdrh return TCL_OK; 52975897234Sdrh } 53075897234Sdrh 53175897234Sdrh /* 53275897234Sdrh ** sqlite DBNAME FILENAME ?MODE? 53375897234Sdrh ** 53475897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 53575897234Sdrh ** invoked, this routine runs to process that command. 53675897234Sdrh ** 53775897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 53875897234Sdrh ** database connection. This command creates a new command named 53975897234Sdrh ** DBNAME that is used to control that connection. The database 54075897234Sdrh ** connection is deleted when the DBNAME command is deleted. 54175897234Sdrh ** 54275897234Sdrh ** The second argument is the name of the directory that contains 54375897234Sdrh ** the sqlite database that is to be accessed. 544fbc3eab8Sdrh ** 545fbc3eab8Sdrh ** For testing purposes, we also support the following: 546fbc3eab8Sdrh ** 547fbc3eab8Sdrh ** sqlite -encoding 548fbc3eab8Sdrh ** 549fbc3eab8Sdrh ** Return the encoding used by LIKE and GLOB operators. Choices 550fbc3eab8Sdrh ** are UTF-8 and iso8859. 551fbc3eab8Sdrh ** 552fbc3eab8Sdrh ** sqlite -tcl-uses-utf 553fbc3eab8Sdrh ** 554fbc3eab8Sdrh ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if 555fbc3eab8Sdrh ** not. Used by tests to make sure the library was compiled 556fbc3eab8Sdrh ** correctly. 55775897234Sdrh */ 55875897234Sdrh static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ 55975897234Sdrh int mode; 560bec3f402Sdrh SqliteDb *p; 56175897234Sdrh char *zErrMsg; 56206b2718aSdrh char zBuf[80]; 563fbc3eab8Sdrh if( argc==2 ){ 564fbc3eab8Sdrh if( strcmp(argv[1],"-encoding")==0 ){ 565fbc3eab8Sdrh Tcl_AppendResult(interp,sqlite_encoding,0); 566fbc3eab8Sdrh return TCL_OK; 567fbc3eab8Sdrh } 568fbc3eab8Sdrh if( strcmp(argv[1],"-tcl-uses-utf")==0 ){ 569fbc3eab8Sdrh #ifdef TCL_UTF_MAX 570fbc3eab8Sdrh Tcl_AppendResult(interp,"1",0); 571fbc3eab8Sdrh #else 572fbc3eab8Sdrh Tcl_AppendResult(interp,"0",0); 573fbc3eab8Sdrh #endif 574fbc3eab8Sdrh return TCL_OK; 575fbc3eab8Sdrh } 576fbc3eab8Sdrh } 57775897234Sdrh if( argc!=3 && argc!=4 ){ 57875897234Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 57975897234Sdrh " HANDLE FILENAME ?MODE?\"", 0); 58075897234Sdrh return TCL_ERROR; 58175897234Sdrh } 58275897234Sdrh if( argc==3 ){ 58358b9576bSdrh mode = 0666; 58475897234Sdrh }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ 58575897234Sdrh return TCL_ERROR; 58675897234Sdrh } 58775897234Sdrh zErrMsg = 0; 5884cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 58975897234Sdrh if( p==0 ){ 590bec3f402Sdrh Tcl_SetResult(interp, "malloc failed", TCL_STATIC); 591bec3f402Sdrh return TCL_ERROR; 592bec3f402Sdrh } 593bec3f402Sdrh memset(p, 0, sizeof(*p)); 594bec3f402Sdrh p->db = sqlite_open(argv[2], mode, &zErrMsg); 595bec3f402Sdrh if( p->db==0 ){ 59675897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 597bec3f402Sdrh Tcl_Free((char*)p); 59875897234Sdrh free(zErrMsg); 59975897234Sdrh return TCL_ERROR; 60075897234Sdrh } 6016d31316cSdrh Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); 602c22bd47dSdrh 60306b2718aSdrh /* The return value is the value of the sqlite* pointer 60406b2718aSdrh */ 60506b2718aSdrh sprintf(zBuf, "%p", p->db); 6065e5377fbSdrh if( strncmp(zBuf,"0x",2) ){ 6075e5377fbSdrh sprintf(zBuf, "0x%p", p->db); 6085e5377fbSdrh } 60906b2718aSdrh Tcl_AppendResult(interp, zBuf, 0); 61006b2718aSdrh 611c22bd47dSdrh /* If compiled with SQLITE_TEST turned on, then register the "md5sum" 61206b2718aSdrh ** SQL function. 613c22bd47dSdrh */ 61428b4e489Sdrh #ifdef SQLITE_TEST 61528b4e489Sdrh { 61628b4e489Sdrh extern void Md5_Register(sqlite*); 61728b4e489Sdrh Md5_Register(p->db); 61828b4e489Sdrh } 61928b4e489Sdrh #endif 62075897234Sdrh return TCL_OK; 62175897234Sdrh } 62275897234Sdrh 62375897234Sdrh /* 62490ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 62590ca9753Sdrh ** library. 62690ca9753Sdrh */ 62790ca9753Sdrh #ifndef USE_TCL_STUBS 62890ca9753Sdrh # undef Tcl_InitStubs 62990ca9753Sdrh # define Tcl_InitStubs(a,b,c) 63090ca9753Sdrh #endif 63190ca9753Sdrh 63290ca9753Sdrh /* 63375897234Sdrh ** Initialize this module. 63475897234Sdrh ** 63575897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 63675897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 63775897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 63875897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 63975897234Sdrh ** for additional information. 64075897234Sdrh */ 64175897234Sdrh int Sqlite_Init(Tcl_Interp *interp){ 64290ca9753Sdrh Tcl_InitStubs(interp, "8.0", 0); 64390ca9753Sdrh Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); 6446d4abfbeSdrh Tcl_PkgProvide(interp, "sqlite", "2.0"); 64590ca9753Sdrh return TCL_OK; 64690ca9753Sdrh } 64790ca9753Sdrh int Tclsqlite_Init(Tcl_Interp *interp){ 64890ca9753Sdrh Tcl_InitStubs(interp, "8.0", 0); 64975897234Sdrh Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); 6506d4abfbeSdrh Tcl_PkgProvide(interp, "sqlite", "2.0"); 65175897234Sdrh return TCL_OK; 65275897234Sdrh } 65375897234Sdrh int Sqlite_SafeInit(Tcl_Interp *interp){ 65475897234Sdrh return TCL_OK; 65575897234Sdrh } 65690ca9753Sdrh int Tclsqlite_SafeInit(Tcl_Interp *interp){ 65790ca9753Sdrh return TCL_OK; 65890ca9753Sdrh } 65975897234Sdrh 6603cebbde3Sdrh #if 0 66175897234Sdrh /* 66275897234Sdrh ** If compiled using mktclapp, this routine runs to initialize 66375897234Sdrh ** everything. 66475897234Sdrh */ 66575897234Sdrh int Et_AppInit(Tcl_Interp *interp){ 66675897234Sdrh return Sqlite_Init(interp); 66775897234Sdrh } 6683cebbde3Sdrh #endif 669348784efSdrh 670348784efSdrh /* 671348784efSdrh ** If the macro TCLSH is defined and is one, then put in code for the 672348784efSdrh ** "main" routine that will initialize Tcl. 673348784efSdrh */ 674348784efSdrh #if defined(TCLSH) && TCLSH==1 675348784efSdrh static char zMainloop[] = 676348784efSdrh "set line {}\n" 677348784efSdrh "while {![eof stdin]} {\n" 678348784efSdrh "if {$line!=\"\"} {\n" 679348784efSdrh "puts -nonewline \"> \"\n" 680348784efSdrh "} else {\n" 681348784efSdrh "puts -nonewline \"% \"\n" 682348784efSdrh "}\n" 683348784efSdrh "flush stdout\n" 684348784efSdrh "append line [gets stdin]\n" 685348784efSdrh "if {[info complete $line]} {\n" 686348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 687348784efSdrh "puts stderr \"Error: $result\"\n" 688348784efSdrh "} elseif {$result!=\"\"} {\n" 689348784efSdrh "puts $result\n" 690348784efSdrh "}\n" 691348784efSdrh "set line {}\n" 692348784efSdrh "} else {\n" 693348784efSdrh "append line \\n\n" 694348784efSdrh "}\n" 695348784efSdrh "}\n" 696348784efSdrh ; 697348784efSdrh 698348784efSdrh #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 699348784efSdrh int TCLSH_MAIN(int argc, char **argv){ 700348784efSdrh Tcl_Interp *interp; 701297ecf14Sdrh Tcl_FindExecutable(argv[0]); 702348784efSdrh interp = Tcl_CreateInterp(); 703348784efSdrh Sqlite_Init(interp); 704d9b0257aSdrh #ifdef SQLITE_TEST 705d1bf3512Sdrh { 706d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 7075c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 7085c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 709efc251daSdrh extern int Md5_Init(Tcl_Interp*); 710d1bf3512Sdrh Sqlitetest1_Init(interp); 7115c4d9703Sdrh Sqlitetest2_Init(interp); 7125c4d9703Sdrh Sqlitetest3_Init(interp); 713efc251daSdrh Md5_Init(interp); 714d1bf3512Sdrh } 715d1bf3512Sdrh #endif 716348784efSdrh if( argc>=2 ){ 717348784efSdrh int i; 718348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 719348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 720348784efSdrh for(i=2; i<argc; i++){ 721348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 722348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 723348784efSdrh } 724348784efSdrh if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 7250de8c112Sdrh const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 726c61053b7Sdrh if( zInfo==0 ) zInfo = interp->result; 727c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 728348784efSdrh return 1; 729348784efSdrh } 730348784efSdrh }else{ 731348784efSdrh Tcl_GlobalEval(interp, zMainloop); 732348784efSdrh } 733348784efSdrh return 0; 734348784efSdrh } 735348784efSdrh #endif /* TCLSH */ 7366d31316cSdrh 7376d31316cSdrh #endif /* !defined(NO_TCL) */ 738