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*28b4e489Sdrh ** $Id: tclsqlite.c,v 1.30 2002/03/11 02:06:13 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[] = { 271af9ff33aSdrh "busy", "close", "complete", "eval", "last_insert_rowid", "timeout", 0 2726d31316cSdrh }; 2736d31316cSdrh enum DB_opts { 274af9ff33aSdrh DB_BUSY, DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_LAST_INSERT_ROWID, DB_TIMEOUT 2756d31316cSdrh }; 2766d31316cSdrh 2776d31316cSdrh if( objc<2 ){ 2786d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 27975897234Sdrh return TCL_ERROR; 28075897234Sdrh } 2816d31316cSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){ 2826d31316cSdrh return TCL_ERROR; 2836d31316cSdrh } 2846d31316cSdrh 2856d31316cSdrh switch( (enum DB_opts)choice ){ 28675897234Sdrh 287bec3f402Sdrh /* $db busy ?CALLBACK? 288bec3f402Sdrh ** 289bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 290bec3f402Sdrh ** a locked database file. 291bec3f402Sdrh */ 2926d31316cSdrh case DB_BUSY: { 2936d31316cSdrh if( objc>3 ){ 2946d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 295bec3f402Sdrh return TCL_ERROR; 2966d31316cSdrh }else if( objc==2 ){ 297bec3f402Sdrh if( pDb->zBusy ){ 298bec3f402Sdrh Tcl_AppendResult(interp, pDb->zBusy, 0); 299bec3f402Sdrh } 300bec3f402Sdrh }else{ 3016d31316cSdrh char *zBusy; 3026d31316cSdrh int len; 303bec3f402Sdrh if( pDb->zBusy ){ 304bec3f402Sdrh Tcl_Free(pDb->zBusy); 3056d31316cSdrh } 3066d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 3076d31316cSdrh if( zBusy && len>0 ){ 3086d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 3096d31316cSdrh strcpy(pDb->zBusy, zBusy); 3106d31316cSdrh }else{ 311bec3f402Sdrh pDb->zBusy = 0; 312bec3f402Sdrh } 313bec3f402Sdrh if( pDb->zBusy ){ 314bec3f402Sdrh pDb->interp = interp; 315bec3f402Sdrh sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); 3166d31316cSdrh }else{ 3176d31316cSdrh sqlite_busy_handler(pDb->db, 0, 0); 318bec3f402Sdrh } 319bec3f402Sdrh } 3206d31316cSdrh break; 3216d31316cSdrh } 322bec3f402Sdrh 32375897234Sdrh /* $db close 32475897234Sdrh ** 32575897234Sdrh ** Shutdown the database 32675897234Sdrh */ 3276d31316cSdrh case DB_CLOSE: { 3286d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 3296d31316cSdrh break; 3306d31316cSdrh } 33175897234Sdrh 33275897234Sdrh /* $db complete SQL 33375897234Sdrh ** 33475897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 33575897234Sdrh ** additional lines of input are needed. This is similar to the 33675897234Sdrh ** built-in "info complete" command of Tcl. 33775897234Sdrh */ 3386d31316cSdrh case DB_COMPLETE: { 3396d31316cSdrh Tcl_Obj *pResult; 3406d31316cSdrh int isComplete; 3416d31316cSdrh if( objc!=3 ){ 3426d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 34375897234Sdrh return TCL_ERROR; 34475897234Sdrh } 3456d31316cSdrh isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); 3466d31316cSdrh pResult = Tcl_GetObjResult(interp); 3476d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 3486d31316cSdrh break; 3496d31316cSdrh } 35075897234Sdrh 35175897234Sdrh /* 35275897234Sdrh ** $db eval $sql ?array { ...code... }? 35375897234Sdrh ** 35475897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 355bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 35675897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 35775897234Sdrh ** If "array" is an empty string, then the values are placed in variables 35875897234Sdrh ** that have the same name as the fields extracted by the query. 35975897234Sdrh */ 3606d31316cSdrh case DB_EVAL: { 36175897234Sdrh CallbackData cbData; 36275897234Sdrh char *zErrMsg; 3636d31316cSdrh char *zSql; 36475897234Sdrh int rc; 365297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 366297ecf14Sdrh Tcl_DString dSql; 3676d4abfbeSdrh int i; 368297ecf14Sdrh #endif 36975897234Sdrh 3706d31316cSdrh if( objc!=5 && objc!=3 ){ 3716d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); 37275897234Sdrh return TCL_ERROR; 37375897234Sdrh } 374bec3f402Sdrh pDb->interp = interp; 3756d31316cSdrh zSql = Tcl_GetStringFromObj(objv[2], 0); 376297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 377297ecf14Sdrh Tcl_DStringInit(&dSql); 378297ecf14Sdrh Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); 379297ecf14Sdrh zSql = Tcl_DStringValue(&dSql); 380297ecf14Sdrh #endif 3816d31316cSdrh Tcl_IncrRefCount(objv[2]); 3826d31316cSdrh if( objc==5 ){ 38375897234Sdrh cbData.interp = interp; 384dcc581ccSdrh cbData.once = 1; 3856d31316cSdrh cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); 3866d31316cSdrh cbData.pCode = objv[4]; 387960e8c63Sdrh cbData.tcl_rc = TCL_OK; 3886d4abfbeSdrh cbData.nColName = 0; 3896d4abfbeSdrh cbData.azColName = 0; 39075897234Sdrh zErrMsg = 0; 3916d31316cSdrh Tcl_IncrRefCount(objv[3]); 3926d31316cSdrh Tcl_IncrRefCount(objv[4]); 3936d31316cSdrh rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); 3946d31316cSdrh Tcl_DecrRefCount(objv[4]); 3956d31316cSdrh Tcl_DecrRefCount(objv[3]); 396960e8c63Sdrh if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } 39775897234Sdrh }else{ 3986d31316cSdrh Tcl_Obj *pList = Tcl_NewObj(); 399960e8c63Sdrh cbData.tcl_rc = TCL_OK; 4006d31316cSdrh rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); 4016d31316cSdrh Tcl_SetObjResult(interp, pList); 40275897234Sdrh } 40375897234Sdrh if( zErrMsg ){ 40475897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 40575897234Sdrh free(zErrMsg); 406960e8c63Sdrh rc = TCL_ERROR; 4076d4abfbeSdrh }else if( rc!=SQLITE_OK && rc!=SQLITE_ABORT ){ 4086d4abfbeSdrh Tcl_AppendResult(interp, sqlite_error_string(rc), 0); 4096d4abfbeSdrh rc = TCL_ERROR; 410960e8c63Sdrh }else{ 411960e8c63Sdrh rc = cbData.tcl_rc; 41275897234Sdrh } 4136d31316cSdrh Tcl_DecrRefCount(objv[2]); 414297ecf14Sdrh #ifdef UTF_TRANSLATION_NEEDED 415297ecf14Sdrh Tcl_DStringFree(&dSql); 4166d4abfbeSdrh if( objc==5 && cbData.azColName ){ 4176d4abfbeSdrh for(i=0; i<cbData.nColName; i++){ 4186d4abfbeSdrh if( cbData.azColName[i] ) free(cbData.azColName[i]); 4196d4abfbeSdrh } 4206d4abfbeSdrh free(cbData.azColName); 421ce927065Sdrh cbData.azColName = 0; 4226d4abfbeSdrh } 423297ecf14Sdrh #endif 42475897234Sdrh return rc; 4256d31316cSdrh } 426bec3f402Sdrh 427bec3f402Sdrh /* 428af9ff33aSdrh ** $db last_insert_rowid 429af9ff33aSdrh ** 430af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 431af9ff33aSdrh */ 432af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 433af9ff33aSdrh Tcl_Obj *pResult; 434af9ff33aSdrh int rowid; 435af9ff33aSdrh if( objc!=2 ){ 436af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 437af9ff33aSdrh return TCL_ERROR; 438af9ff33aSdrh } 439af9ff33aSdrh rowid = sqlite_last_insert_rowid(pDb->db); 440af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 441af9ff33aSdrh Tcl_SetIntObj(pResult, rowid); 442af9ff33aSdrh break; 443af9ff33aSdrh } 444af9ff33aSdrh 445af9ff33aSdrh /* 446bec3f402Sdrh ** $db timeout MILLESECONDS 447bec3f402Sdrh ** 448bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 449bec3f402Sdrh */ 4506d31316cSdrh case DB_TIMEOUT: { 451bec3f402Sdrh int ms; 4526d31316cSdrh if( objc!=3 ){ 4536d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 454bec3f402Sdrh return TCL_ERROR; 45575897234Sdrh } 4566d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 457bec3f402Sdrh sqlite_busy_timeout(pDb->db, ms); 4586d31316cSdrh break; 45975897234Sdrh } 4606d31316cSdrh } /* End of the SWITCH statement */ 46175897234Sdrh return TCL_OK; 46275897234Sdrh } 46375897234Sdrh 46475897234Sdrh /* 46575897234Sdrh ** sqlite DBNAME FILENAME ?MODE? 46675897234Sdrh ** 46775897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 46875897234Sdrh ** invoked, this routine runs to process that command. 46975897234Sdrh ** 47075897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 47175897234Sdrh ** database connection. This command creates a new command named 47275897234Sdrh ** DBNAME that is used to control that connection. The database 47375897234Sdrh ** connection is deleted when the DBNAME command is deleted. 47475897234Sdrh ** 47575897234Sdrh ** The second argument is the name of the directory that contains 47675897234Sdrh ** the sqlite database that is to be accessed. 477fbc3eab8Sdrh ** 478fbc3eab8Sdrh ** For testing purposes, we also support the following: 479fbc3eab8Sdrh ** 480fbc3eab8Sdrh ** sqlite -encoding 481fbc3eab8Sdrh ** 482fbc3eab8Sdrh ** Return the encoding used by LIKE and GLOB operators. Choices 483fbc3eab8Sdrh ** are UTF-8 and iso8859. 484fbc3eab8Sdrh ** 485fbc3eab8Sdrh ** sqlite -tcl-uses-utf 486fbc3eab8Sdrh ** 487fbc3eab8Sdrh ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if 488fbc3eab8Sdrh ** not. Used by tests to make sure the library was compiled 489fbc3eab8Sdrh ** correctly. 49075897234Sdrh */ 49175897234Sdrh static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ 49275897234Sdrh int mode; 493bec3f402Sdrh SqliteDb *p; 49475897234Sdrh char *zErrMsg; 495fbc3eab8Sdrh if( argc==2 ){ 496fbc3eab8Sdrh if( strcmp(argv[1],"-encoding")==0 ){ 497fbc3eab8Sdrh Tcl_AppendResult(interp,sqlite_encoding,0); 498fbc3eab8Sdrh return TCL_OK; 499fbc3eab8Sdrh } 500fbc3eab8Sdrh if( strcmp(argv[1],"-tcl-uses-utf")==0 ){ 501fbc3eab8Sdrh #ifdef TCL_UTF_MAX 502fbc3eab8Sdrh Tcl_AppendResult(interp,"1",0); 503fbc3eab8Sdrh #else 504fbc3eab8Sdrh Tcl_AppendResult(interp,"0",0); 505fbc3eab8Sdrh #endif 506fbc3eab8Sdrh return TCL_OK; 507fbc3eab8Sdrh } 508fbc3eab8Sdrh } 50975897234Sdrh if( argc!=3 && argc!=4 ){ 51075897234Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 51175897234Sdrh " HANDLE FILENAME ?MODE?\"", 0); 51275897234Sdrh return TCL_ERROR; 51375897234Sdrh } 51475897234Sdrh if( argc==3 ){ 51558b9576bSdrh mode = 0666; 51675897234Sdrh }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ 51775897234Sdrh return TCL_ERROR; 51875897234Sdrh } 51975897234Sdrh zErrMsg = 0; 5204cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 52175897234Sdrh if( p==0 ){ 522bec3f402Sdrh Tcl_SetResult(interp, "malloc failed", TCL_STATIC); 523bec3f402Sdrh return TCL_ERROR; 524bec3f402Sdrh } 525bec3f402Sdrh memset(p, 0, sizeof(*p)); 526bec3f402Sdrh p->db = sqlite_open(argv[2], mode, &zErrMsg); 527bec3f402Sdrh if( p->db==0 ){ 52875897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 529bec3f402Sdrh Tcl_Free((char*)p); 53075897234Sdrh free(zErrMsg); 53175897234Sdrh return TCL_ERROR; 53275897234Sdrh } 5336d31316cSdrh Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); 534*28b4e489Sdrh #ifdef SQLITE_TEST 535*28b4e489Sdrh { 536*28b4e489Sdrh extern void Md5_Register(sqlite*); 537*28b4e489Sdrh Md5_Register(p->db); 538*28b4e489Sdrh } 539*28b4e489Sdrh #endif 54075897234Sdrh return TCL_OK; 54175897234Sdrh } 54275897234Sdrh 54375897234Sdrh /* 54490ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 54590ca9753Sdrh ** library. 54690ca9753Sdrh */ 54790ca9753Sdrh #ifndef USE_TCL_STUBS 54890ca9753Sdrh # undef Tcl_InitStubs 54990ca9753Sdrh # define Tcl_InitStubs(a,b,c) 55090ca9753Sdrh #endif 55190ca9753Sdrh 55290ca9753Sdrh /* 55375897234Sdrh ** Initialize this module. 55475897234Sdrh ** 55575897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 55675897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 55775897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 55875897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 55975897234Sdrh ** for additional information. 56075897234Sdrh */ 56175897234Sdrh int Sqlite_Init(Tcl_Interp *interp){ 56290ca9753Sdrh Tcl_InitStubs(interp, "8.0", 0); 56390ca9753Sdrh Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); 5646d4abfbeSdrh Tcl_PkgProvide(interp, "sqlite", "2.0"); 56590ca9753Sdrh return TCL_OK; 56690ca9753Sdrh } 56790ca9753Sdrh int Tclsqlite_Init(Tcl_Interp *interp){ 56890ca9753Sdrh Tcl_InitStubs(interp, "8.0", 0); 56975897234Sdrh Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); 5706d4abfbeSdrh Tcl_PkgProvide(interp, "sqlite", "2.0"); 57175897234Sdrh return TCL_OK; 57275897234Sdrh } 57375897234Sdrh int Sqlite_SafeInit(Tcl_Interp *interp){ 57475897234Sdrh return TCL_OK; 57575897234Sdrh } 57690ca9753Sdrh int Tclsqlite_SafeInit(Tcl_Interp *interp){ 57790ca9753Sdrh return TCL_OK; 57890ca9753Sdrh } 57975897234Sdrh 5803cebbde3Sdrh #if 0 58175897234Sdrh /* 58275897234Sdrh ** If compiled using mktclapp, this routine runs to initialize 58375897234Sdrh ** everything. 58475897234Sdrh */ 58575897234Sdrh int Et_AppInit(Tcl_Interp *interp){ 58675897234Sdrh return Sqlite_Init(interp); 58775897234Sdrh } 5883cebbde3Sdrh #endif 589348784efSdrh 590348784efSdrh /* 591348784efSdrh ** If the macro TCLSH is defined and is one, then put in code for the 592348784efSdrh ** "main" routine that will initialize Tcl. 593348784efSdrh */ 594348784efSdrh #if defined(TCLSH) && TCLSH==1 595348784efSdrh static char zMainloop[] = 596348784efSdrh "set line {}\n" 597348784efSdrh "while {![eof stdin]} {\n" 598348784efSdrh "if {$line!=\"\"} {\n" 599348784efSdrh "puts -nonewline \"> \"\n" 600348784efSdrh "} else {\n" 601348784efSdrh "puts -nonewline \"% \"\n" 602348784efSdrh "}\n" 603348784efSdrh "flush stdout\n" 604348784efSdrh "append line [gets stdin]\n" 605348784efSdrh "if {[info complete $line]} {\n" 606348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 607348784efSdrh "puts stderr \"Error: $result\"\n" 608348784efSdrh "} elseif {$result!=\"\"} {\n" 609348784efSdrh "puts $result\n" 610348784efSdrh "}\n" 611348784efSdrh "set line {}\n" 612348784efSdrh "} else {\n" 613348784efSdrh "append line \\n\n" 614348784efSdrh "}\n" 615348784efSdrh "}\n" 616348784efSdrh ; 617348784efSdrh 618348784efSdrh #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 619348784efSdrh int TCLSH_MAIN(int argc, char **argv){ 620348784efSdrh Tcl_Interp *interp; 621297ecf14Sdrh Tcl_FindExecutable(argv[0]); 622348784efSdrh interp = Tcl_CreateInterp(); 623348784efSdrh Sqlite_Init(interp); 624d9b0257aSdrh #ifdef SQLITE_TEST 625d1bf3512Sdrh { 626d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 6275c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 6285c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 629efc251daSdrh extern int Md5_Init(Tcl_Interp*); 630d1bf3512Sdrh Sqlitetest1_Init(interp); 6315c4d9703Sdrh Sqlitetest2_Init(interp); 6325c4d9703Sdrh Sqlitetest3_Init(interp); 633efc251daSdrh Md5_Init(interp); 634d1bf3512Sdrh } 635d1bf3512Sdrh #endif 636348784efSdrh if( argc>=2 ){ 637348784efSdrh int i; 638348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 639348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 640348784efSdrh for(i=2; i<argc; i++){ 641348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 642348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 643348784efSdrh } 644348784efSdrh if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 645c61053b7Sdrh char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 646c61053b7Sdrh if( zInfo==0 ) zInfo = interp->result; 647c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 648348784efSdrh return 1; 649348784efSdrh } 650348784efSdrh }else{ 651348784efSdrh Tcl_GlobalEval(interp, zMainloop); 652348784efSdrh } 653348784efSdrh return 0; 654348784efSdrh } 655348784efSdrh #endif /* TCLSH */ 6566d31316cSdrh 6576d31316cSdrh #endif /* !defined(NO_TCL) */ 658