drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** A TCL Interface to SQLite |
| 13 | ** |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 14 | ** $Id: tclsqlite.c,v 1.46 2003/04/03 15:46:04 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 15 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 16 | #ifndef NO_TCL /* Omit this whole file if TCL is unavailable */ |
| 17 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 18 | #include "sqliteInt.h" |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 19 | #include "tcl.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 22 | #include <assert.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 25 | ** 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 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 35 | ** New SQL functions can be created as TCL scripts. Each such function |
| 36 | ** is described by an instance of the following structure. |
| 37 | */ |
| 38 | typedef struct SqlFunc SqlFunc; |
| 39 | struct SqlFunc { |
| 40 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 41 | char *zScript; /* The script to be run */ |
| 42 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 43 | }; |
| 44 | |
| 45 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 46 | ** There is one instance of this structure for each SQLite database |
| 47 | ** that has been opened by the SQLite TCL interface. |
| 48 | */ |
| 49 | typedef struct SqliteDb SqliteDb; |
| 50 | struct SqliteDb { |
| 51 | sqlite *db; /* The "real" database structure */ |
| 52 | Tcl_Interp *interp; /* The interpreter used for this database */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 53 | char *zBusy; /* The busy callback routine */ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 54 | char *zBegin; /* The begin-transaction callback routine */ |
| 55 | char *zCommit; /* The commit-transaction callback routine */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 56 | SqlFunc *pFunc; /* List of SQL functions */ |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 57 | int rc; /* Return code of most recent sqlite_exec() */ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 61 | ** An instance of this structure passes information thru the sqlite |
| 62 | ** logic from the original TCL command into the callback routine. |
| 63 | */ |
| 64 | typedef struct CallbackData CallbackData; |
| 65 | struct CallbackData { |
| 66 | Tcl_Interp *interp; /* The TCL interpreter */ |
| 67 | char *zArray; /* The array into which data is written */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 68 | Tcl_Obj *pCode; /* The code to execute for each row */ |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 69 | int once; /* Set for first callback only */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 70 | int tcl_rc; /* Return code from TCL script */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 71 | int nColName; /* Number of entries in the azColName[] array */ |
| 72 | char **azColName; /* Column names translated to UTF-8 */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 73 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 74 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 75 | #ifdef UTF_TRANSLATION_NEEDED |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 76 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 77 | ** Called for each row of the result. |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 78 | ** |
| 79 | ** This version is used when TCL expects UTF-8 data but the database |
| 80 | ** uses the ISO8859 format. A translation must occur from ISO8859 into |
| 81 | ** UTF-8. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 82 | */ |
| 83 | static int DbEvalCallback( |
| 84 | void *clientData, /* An instance of CallbackData */ |
| 85 | int nCol, /* Number of columns in the result */ |
| 86 | char ** azCol, /* Data for each column */ |
| 87 | char ** azN /* Name for each column */ |
| 88 | ){ |
| 89 | CallbackData *cbData = (CallbackData*)clientData; |
| 90 | int i, rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 91 | Tcl_DString dCol; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 92 | Tcl_DStringInit(&dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 93 | if( cbData->azColName==0 ){ |
| 94 | assert( cbData->once ); |
| 95 | cbData->once = 0; |
| 96 | if( cbData->zArray[0] ){ |
| 97 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 98 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 99 | cbData->azColName = malloc( nCol*sizeof(char*) ); |
| 100 | if( cbData->azColName==0 ){ return 1; } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 101 | cbData->nColName = nCol; |
| 102 | for(i=0; i<nCol; i++){ |
| 103 | Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 104 | cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 ); |
| 105 | if( cbData->azColName[i] ){ |
| 106 | strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol)); |
| 107 | }else{ |
| 108 | return 1; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 109 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 110 | if( cbData->zArray[0] ){ |
| 111 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", |
| 112 | Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
drh | 704027f | 2002-07-15 20:58:47 +0000 | [diff] [blame] | 113 | if( azN[nCol]!=0 ){ |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 114 | Tcl_DString dType; |
| 115 | Tcl_DStringInit(&dType); |
| 116 | Tcl_DStringAppend(&dType, "typeof:", -1); |
| 117 | Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1); |
| 118 | Tcl_DStringFree(&dCol); |
| 119 | Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol); |
| 120 | Tcl_SetVar2(cbData->interp, cbData->zArray, |
| 121 | Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol), |
| 122 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 123 | Tcl_DStringFree(&dType); |
| 124 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 125 | } |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 126 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 127 | Tcl_DStringFree(&dCol); |
| 128 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 129 | } |
| 130 | if( azCol!=0 ){ |
| 131 | if( cbData->zArray[0] ){ |
| 132 | for(i=0; i<nCol; i++){ |
| 133 | char *z = azCol[i]; |
| 134 | if( z==0 ) z = ""; |
| 135 | Tcl_DStringInit(&dCol); |
| 136 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 137 | Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i], |
| 138 | Tcl_DStringValue(&dCol), 0); |
| 139 | Tcl_DStringFree(&dCol); |
| 140 | } |
| 141 | }else{ |
| 142 | for(i=0; i<nCol; i++){ |
| 143 | char *z = azCol[i]; |
| 144 | if( z==0 ) z = ""; |
| 145 | Tcl_DStringInit(&dCol); |
| 146 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 147 | Tcl_SetVar(cbData->interp, cbData->azColName[i], |
| 148 | Tcl_DStringValue(&dCol), 0); |
| 149 | Tcl_DStringFree(&dCol); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
| 154 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 155 | cbData->tcl_rc = rc; |
| 156 | return rc!=TCL_OK; |
| 157 | } |
| 158 | #endif /* UTF_TRANSLATION_NEEDED */ |
| 159 | |
| 160 | #ifndef UTF_TRANSLATION_NEEDED |
| 161 | /* |
| 162 | ** Called for each row of the result. |
| 163 | ** |
| 164 | ** This version is used when either of the following is true: |
| 165 | ** |
| 166 | ** (1) This version of TCL uses UTF-8 and the data in the |
| 167 | ** SQLite database is already in the UTF-8 format. |
| 168 | ** |
| 169 | ** (2) This version of TCL uses ISO8859 and the data in the |
| 170 | ** SQLite database is already in the ISO8859 format. |
| 171 | */ |
| 172 | static int DbEvalCallback( |
| 173 | void *clientData, /* An instance of CallbackData */ |
| 174 | int nCol, /* Number of columns in the result */ |
| 175 | char ** azCol, /* Data for each column */ |
| 176 | char ** azN /* Name for each column */ |
| 177 | ){ |
| 178 | CallbackData *cbData = (CallbackData*)clientData; |
| 179 | int i, rc; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 180 | if( azCol==0 || (cbData->once && cbData->zArray[0]) ){ |
| 181 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
| 182 | for(i=0; i<nCol; i++){ |
| 183 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], |
| 184 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 185 | if( azN[nCol] ){ |
| 186 | char *z = sqlite_mprintf("typeof:%s", azN[i]); |
| 187 | Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol], |
| 188 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 189 | sqlite_freemem(z); |
| 190 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 191 | } |
| 192 | cbData->once = 0; |
| 193 | } |
| 194 | if( azCol!=0 ){ |
| 195 | if( cbData->zArray[0] ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 196 | for(i=0; i<nCol; i++){ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 197 | char *z = azCol[i]; |
| 198 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 199 | Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 200 | } |
| 201 | }else{ |
| 202 | for(i=0; i<nCol; i++){ |
| 203 | char *z = azCol[i]; |
| 204 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 205 | Tcl_SetVar(cbData->interp, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 208 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 209 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 210 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 211 | cbData->tcl_rc = rc; |
| 212 | return rc!=TCL_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 213 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 214 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 215 | |
| 216 | /* |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 217 | ** This is an alternative callback for database queries. Instead |
| 218 | ** of invoking a TCL script to handle the result, this callback just |
| 219 | ** appends each column of the result to a list. After the query |
| 220 | ** is complete, the list is returned. |
| 221 | */ |
| 222 | static int DbEvalCallback2( |
| 223 | void *clientData, /* An instance of CallbackData */ |
| 224 | int nCol, /* Number of columns in the result */ |
| 225 | char ** azCol, /* Data for each column */ |
| 226 | char ** azN /* Name for each column */ |
| 227 | ){ |
| 228 | Tcl_Obj *pList = (Tcl_Obj*)clientData; |
| 229 | int i; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 230 | if( azCol==0 ) return 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 231 | for(i=0; i<nCol; i++){ |
| 232 | Tcl_Obj *pElem; |
| 233 | if( azCol[i] && *azCol[i] ){ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 234 | #ifdef UTF_TRANSLATION_NEEDED |
| 235 | Tcl_DString dCol; |
| 236 | Tcl_DStringInit(&dCol); |
| 237 | Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol); |
| 238 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 239 | Tcl_DStringFree(&dCol); |
| 240 | #else |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 241 | pElem = Tcl_NewStringObj(azCol[i], -1); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 242 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 243 | }else{ |
| 244 | pElem = Tcl_NewObj(); |
| 245 | } |
| 246 | Tcl_ListObjAppendElement(0, pList, pElem); |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 252 | ** Called when the command is deleted. |
| 253 | */ |
| 254 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 255 | SqliteDb *pDb = (SqliteDb*)db; |
| 256 | sqlite_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 257 | while( pDb->pFunc ){ |
| 258 | SqlFunc *pFunc = pDb->pFunc; |
| 259 | pDb->pFunc = pFunc->pNext; |
| 260 | Tcl_Free((char*)pFunc); |
| 261 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 262 | if( pDb->zBusy ){ |
| 263 | Tcl_Free(pDb->zBusy); |
| 264 | } |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 265 | if( pDb->zBegin ){ |
| 266 | Tcl_Free(pDb->zBegin); |
| 267 | } |
| 268 | if( pDb->zCommit ){ |
| 269 | Tcl_Free(pDb->zCommit); |
| 270 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 271 | Tcl_Free((char*)pDb); |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | ** This routine is called when a database file is locked while trying |
| 276 | ** to execute SQL. |
| 277 | */ |
| 278 | static int DbBusyHandler(void *cd, const char *zTable, int nTries){ |
| 279 | SqliteDb *pDb = (SqliteDb*)cd; |
| 280 | int rc; |
| 281 | char zVal[30]; |
| 282 | char *zCmd; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 283 | Tcl_DString cmd; |
| 284 | |
| 285 | Tcl_DStringInit(&cmd); |
| 286 | Tcl_DStringAppend(&cmd, pDb->zBusy, -1); |
| 287 | Tcl_DStringAppendElement(&cmd, zTable); |
| 288 | sprintf(zVal, " %d", nTries); |
| 289 | Tcl_DStringAppend(&cmd, zVal, -1); |
| 290 | zCmd = Tcl_DStringValue(&cmd); |
| 291 | rc = Tcl_Eval(pDb->interp, zCmd); |
| 292 | Tcl_DStringFree(&cmd); |
| 293 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 294 | return 0; |
| 295 | } |
| 296 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 300 | ** This routine is called when a new transaction is started. The |
| 301 | ** TCL script in pDb->zBegin is executed. If it returns non-zero or |
| 302 | ** if it throws an exception, the transaction is aborted. |
| 303 | */ |
| 304 | static int DbBeginHandler(void *cd){ |
| 305 | SqliteDb *pDb = (SqliteDb*)cd; |
| 306 | int rc; |
| 307 | |
| 308 | rc = Tcl_Eval(pDb->interp, pDb->zBegin); |
| 309 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 310 | return 1; |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | ** This routine is called when a transaction is committed. The |
| 317 | ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
| 318 | ** if it throws an exception, the transaction is rolled back instead |
| 319 | ** of being committed. |
| 320 | */ |
| 321 | static int DbCommitHandler(void *cd){ |
| 322 | SqliteDb *pDb = (SqliteDb*)cd; |
| 323 | int rc; |
| 324 | |
| 325 | rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
| 326 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 327 | return 1; |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 333 | ** This routine is called to evaluate an SQL function implemented |
| 334 | ** using TCL script. |
| 335 | */ |
| 336 | static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){ |
| 337 | SqlFunc *p = sqlite_user_data(context); |
| 338 | Tcl_DString cmd; |
| 339 | int i; |
| 340 | int rc; |
| 341 | |
| 342 | Tcl_DStringInit(&cmd); |
| 343 | Tcl_DStringAppend(&cmd, p->zScript, -1); |
| 344 | for(i=0; i<argc; i++){ |
| 345 | Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : ""); |
| 346 | } |
| 347 | rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd)); |
| 348 | if( rc ){ |
| 349 | sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 350 | }else{ |
| 351 | sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 356 | ** The "sqlite" command below creates a new Tcl command for each |
| 357 | ** connection it opens to an SQLite database. This routine is invoked |
| 358 | ** whenever one of those connection-specific commands is executed |
| 359 | ** in Tcl. For example, if you run Tcl code like this: |
| 360 | ** |
| 361 | ** sqlite db1 "my_database" |
| 362 | ** db1 close |
| 363 | ** |
| 364 | ** The first command opens a connection to the "my_database" database |
| 365 | ** and calls that connection "db1". The second command causes this |
| 366 | ** subroutine to be invoked. |
| 367 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 368 | static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 369 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 370 | int choice; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 371 | static const char *DB_strs[] = { |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 372 | "begin_hook", "busy", "changes", |
| 373 | "close", "commit_hook", "complete", |
| 374 | "errorcode", "eval", "function", |
| 375 | "last_insert_rowid", "timeout", 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 376 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 377 | enum DB_enum { |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 378 | DB_BEGIN_HOOK, DB_BUSY, DB_CHANGES, |
| 379 | DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE, |
| 380 | DB_ERRORCODE, DB_EVAL, DB_FUNCTION, |
| 381 | DB_LAST_INSERT_ROWID, DB_TIMEOUT, |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | if( objc<2 ){ |
| 385 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 386 | return TCL_ERROR; |
| 387 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 388 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 389 | return TCL_ERROR; |
| 390 | } |
| 391 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 392 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 393 | |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 394 | /* $db begin_callback ?CALLBACK? |
| 395 | ** |
| 396 | ** Invoke the given callback at the beginning of every SQL transaction. |
| 397 | ** If the callback throws an exception or returns non-zero, then the |
| 398 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 399 | ** is disabled. |
| 400 | */ |
| 401 | case DB_BEGIN_HOOK: { |
| 402 | if( objc>3 ){ |
| 403 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 404 | }else if( objc==2 ){ |
| 405 | if( pDb->zBegin ){ |
| 406 | Tcl_AppendResult(interp, pDb->zBegin, 0); |
| 407 | } |
| 408 | }else{ |
| 409 | char *zBegin; |
| 410 | int len; |
| 411 | if( pDb->zBegin ){ |
| 412 | Tcl_Free(pDb->zBegin); |
| 413 | } |
| 414 | zBegin = Tcl_GetStringFromObj(objv[2], &len); |
| 415 | if( zBegin && len>0 ){ |
| 416 | pDb->zBegin = Tcl_Alloc( len + 1 ); |
| 417 | strcpy(pDb->zBegin, zBegin); |
| 418 | }else{ |
| 419 | pDb->zBegin = 0; |
| 420 | } |
| 421 | if( pDb->zBegin ){ |
| 422 | pDb->interp = interp; |
| 423 | sqlite_begin_hook(pDb->db, DbBeginHandler, pDb); |
| 424 | }else{ |
| 425 | sqlite_begin_hook(pDb->db, 0, 0); |
| 426 | } |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 431 | /* $db busy ?CALLBACK? |
| 432 | ** |
| 433 | ** Invoke the given callback if an SQL statement attempts to open |
| 434 | ** a locked database file. |
| 435 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 436 | case DB_BUSY: { |
| 437 | if( objc>3 ){ |
| 438 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 439 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 440 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 441 | if( pDb->zBusy ){ |
| 442 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 443 | } |
| 444 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 445 | char *zBusy; |
| 446 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 447 | if( pDb->zBusy ){ |
| 448 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 449 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 450 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 451 | if( zBusy && len>0 ){ |
| 452 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 453 | strcpy(pDb->zBusy, zBusy); |
| 454 | }else{ |
| 455 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 456 | } |
| 457 | if( pDb->zBusy ){ |
| 458 | pDb->interp = interp; |
| 459 | sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 460 | }else{ |
| 461 | sqlite_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 464 | break; |
| 465 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 466 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 467 | /* |
| 468 | ** $db changes |
| 469 | ** |
| 470 | ** Return the number of rows that were modified, inserted, or deleted by |
| 471 | ** the most recent "eval". |
| 472 | */ |
| 473 | case DB_CHANGES: { |
| 474 | Tcl_Obj *pResult; |
| 475 | int nChange; |
| 476 | if( objc!=2 ){ |
| 477 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 478 | return TCL_ERROR; |
| 479 | } |
| 480 | nChange = sqlite_changes(pDb->db); |
| 481 | pResult = Tcl_GetObjResult(interp); |
| 482 | Tcl_SetIntObj(pResult, nChange); |
| 483 | break; |
| 484 | } |
| 485 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 486 | /* $db close |
| 487 | ** |
| 488 | ** Shutdown the database |
| 489 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 490 | case DB_CLOSE: { |
| 491 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 492 | break; |
| 493 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 494 | |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame^] | 495 | /* $db commit_hook ?CALLBACK? |
| 496 | ** |
| 497 | ** Invoke the given callback just before committing every SQL transaction. |
| 498 | ** If the callback throws an exception or returns non-zero, then the |
| 499 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 500 | ** is disabled. |
| 501 | */ |
| 502 | case DB_COMMIT_HOOK: { |
| 503 | if( objc>3 ){ |
| 504 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 505 | }else if( objc==2 ){ |
| 506 | if( pDb->zCommit ){ |
| 507 | Tcl_AppendResult(interp, pDb->zCommit, 0); |
| 508 | } |
| 509 | }else{ |
| 510 | char *zCommit; |
| 511 | int len; |
| 512 | if( pDb->zCommit ){ |
| 513 | Tcl_Free(pDb->zCommit); |
| 514 | } |
| 515 | zCommit = Tcl_GetStringFromObj(objv[2], &len); |
| 516 | if( zCommit && len>0 ){ |
| 517 | pDb->zCommit = Tcl_Alloc( len + 1 ); |
| 518 | strcpy(pDb->zCommit, zCommit); |
| 519 | }else{ |
| 520 | pDb->zCommit = 0; |
| 521 | } |
| 522 | if( pDb->zCommit ){ |
| 523 | pDb->interp = interp; |
| 524 | sqlite_commit_hook(pDb->db, DbCommitHandler, pDb); |
| 525 | }else{ |
| 526 | sqlite_commit_hook(pDb->db, 0, 0); |
| 527 | } |
| 528 | } |
| 529 | break; |
| 530 | } |
| 531 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 532 | /* $db complete SQL |
| 533 | ** |
| 534 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 535 | ** additional lines of input are needed. This is similar to the |
| 536 | ** built-in "info complete" command of Tcl. |
| 537 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 538 | case DB_COMPLETE: { |
| 539 | Tcl_Obj *pResult; |
| 540 | int isComplete; |
| 541 | if( objc!=3 ){ |
| 542 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 543 | return TCL_ERROR; |
| 544 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 545 | isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
| 546 | pResult = Tcl_GetObjResult(interp); |
| 547 | Tcl_SetBooleanObj(pResult, isComplete); |
| 548 | break; |
| 549 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | ** $db errorcode |
| 553 | ** |
| 554 | ** Return the numeric error code that was returned by the most recent |
| 555 | ** call to sqlite_exec(). |
| 556 | */ |
| 557 | case DB_ERRORCODE: { |
| 558 | Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc)); |
| 559 | break; |
| 560 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | ** $db eval $sql ?array { ...code... }? |
| 564 | ** |
| 565 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 566 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 567 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 568 | ** If "array" is an empty string, then the values are placed in variables |
| 569 | ** that have the same name as the fields extracted by the query. |
| 570 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 571 | case DB_EVAL: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 572 | CallbackData cbData; |
| 573 | char *zErrMsg; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 574 | char *zSql; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 575 | int rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 576 | #ifdef UTF_TRANSLATION_NEEDED |
| 577 | Tcl_DString dSql; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 578 | int i; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 579 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 580 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 581 | if( objc!=5 && objc!=3 ){ |
| 582 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 583 | return TCL_ERROR; |
| 584 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 585 | pDb->interp = interp; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 586 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 587 | #ifdef UTF_TRANSLATION_NEEDED |
| 588 | Tcl_DStringInit(&dSql); |
| 589 | Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); |
| 590 | zSql = Tcl_DStringValue(&dSql); |
| 591 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 592 | Tcl_IncrRefCount(objv[2]); |
| 593 | if( objc==5 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 594 | cbData.interp = interp; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 595 | cbData.once = 1; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 596 | cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); |
| 597 | cbData.pCode = objv[4]; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 598 | cbData.tcl_rc = TCL_OK; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 599 | cbData.nColName = 0; |
| 600 | cbData.azColName = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 601 | zErrMsg = 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 602 | Tcl_IncrRefCount(objv[3]); |
| 603 | Tcl_IncrRefCount(objv[4]); |
| 604 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); |
| 605 | Tcl_DecrRefCount(objv[4]); |
| 606 | Tcl_DecrRefCount(objv[3]); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 607 | if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 608 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 609 | Tcl_Obj *pList = Tcl_NewObj(); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 610 | cbData.tcl_rc = TCL_OK; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 611 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); |
| 612 | Tcl_SetObjResult(interp, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 613 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 614 | pDb->rc = rc; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 615 | if( rc==SQLITE_ABORT ){ |
| 616 | if( zErrMsg ) free(zErrMsg); |
| 617 | rc = cbData.tcl_rc; |
| 618 | }else if( zErrMsg ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 619 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 620 | free(zErrMsg); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 621 | rc = TCL_ERROR; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 622 | }else if( rc!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 623 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 624 | rc = TCL_ERROR; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 625 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 626 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 627 | Tcl_DecrRefCount(objv[2]); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 628 | #ifdef UTF_TRANSLATION_NEEDED |
| 629 | Tcl_DStringFree(&dSql); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 630 | if( objc==5 && cbData.azColName ){ |
| 631 | for(i=0; i<cbData.nColName; i++){ |
| 632 | if( cbData.azColName[i] ) free(cbData.azColName[i]); |
| 633 | } |
| 634 | free(cbData.azColName); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 635 | cbData.azColName = 0; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 636 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 637 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 638 | return rc; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 639 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 640 | |
| 641 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 642 | ** $db function NAME SCRIPT |
| 643 | ** |
| 644 | ** Create a new SQL function called NAME. Whenever that function is |
| 645 | ** called, invoke SCRIPT to evaluate the function. |
| 646 | */ |
| 647 | case DB_FUNCTION: { |
| 648 | SqlFunc *pFunc; |
| 649 | char *zName; |
| 650 | char *zScript; |
| 651 | int nScript; |
| 652 | if( objc!=4 ){ |
| 653 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 654 | return TCL_ERROR; |
| 655 | } |
| 656 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 657 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 658 | pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 ); |
| 659 | if( pFunc==0 ) return TCL_ERROR; |
| 660 | pFunc->interp = interp; |
| 661 | pFunc->pNext = pDb->pFunc; |
| 662 | pFunc->zScript = (char*)&pFunc[1]; |
| 663 | strcpy(pFunc->zScript, zScript); |
| 664 | sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc); |
| 665 | sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC); |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 670 | ** $db last_insert_rowid |
| 671 | ** |
| 672 | ** Return an integer which is the ROWID for the most recent insert. |
| 673 | */ |
| 674 | case DB_LAST_INSERT_ROWID: { |
| 675 | Tcl_Obj *pResult; |
| 676 | int rowid; |
| 677 | if( objc!=2 ){ |
| 678 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 679 | return TCL_ERROR; |
| 680 | } |
| 681 | rowid = sqlite_last_insert_rowid(pDb->db); |
| 682 | pResult = Tcl_GetObjResult(interp); |
| 683 | Tcl_SetIntObj(pResult, rowid); |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 688 | ** $db timeout MILLESECONDS |
| 689 | ** |
| 690 | ** Delay for the number of milliseconds specified when a file is locked. |
| 691 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 692 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 693 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 694 | if( objc!=3 ){ |
| 695 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 696 | return TCL_ERROR; |
| 697 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 698 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 699 | sqlite_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 700 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 701 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 702 | } /* End of the SWITCH statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 703 | return TCL_OK; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | ** sqlite DBNAME FILENAME ?MODE? |
| 708 | ** |
| 709 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 710 | ** invoked, this routine runs to process that command. |
| 711 | ** |
| 712 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 713 | ** database connection. This command creates a new command named |
| 714 | ** DBNAME that is used to control that connection. The database |
| 715 | ** connection is deleted when the DBNAME command is deleted. |
| 716 | ** |
| 717 | ** The second argument is the name of the directory that contains |
| 718 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 719 | ** |
| 720 | ** For testing purposes, we also support the following: |
| 721 | ** |
| 722 | ** sqlite -encoding |
| 723 | ** |
| 724 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 725 | ** are UTF-8 and iso8859. |
| 726 | ** |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 727 | ** sqlite -version |
| 728 | ** |
| 729 | ** Return the version number of the SQLite library. |
| 730 | ** |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 731 | ** sqlite -tcl-uses-utf |
| 732 | ** |
| 733 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 734 | ** not. Used by tests to make sure the library was compiled |
| 735 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 736 | */ |
| 737 | static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ |
| 738 | int mode; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 739 | SqliteDb *p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 740 | char *zErrMsg; |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 741 | char zBuf[80]; |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 742 | if( argc==2 ){ |
| 743 | if( strcmp(argv[1],"-encoding")==0 ){ |
| 744 | Tcl_AppendResult(interp,sqlite_encoding,0); |
| 745 | return TCL_OK; |
| 746 | } |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 747 | if( strcmp(argv[1],"-version")==0 ){ |
| 748 | Tcl_AppendResult(interp,sqlite_version,0); |
| 749 | return TCL_OK; |
| 750 | } |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 751 | if( strcmp(argv[1],"-tcl-uses-utf")==0 ){ |
| 752 | #ifdef TCL_UTF_MAX |
| 753 | Tcl_AppendResult(interp,"1",0); |
| 754 | #else |
| 755 | Tcl_AppendResult(interp,"0",0); |
| 756 | #endif |
| 757 | return TCL_OK; |
| 758 | } |
| 759 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 760 | if( argc!=3 && argc!=4 ){ |
| 761 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 762 | " HANDLE FILENAME ?MODE?\"", 0); |
| 763 | return TCL_ERROR; |
| 764 | } |
| 765 | if( argc==3 ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 766 | mode = 0666; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 767 | }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ |
| 768 | return TCL_ERROR; |
| 769 | } |
| 770 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 771 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 772 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 773 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 774 | return TCL_ERROR; |
| 775 | } |
| 776 | memset(p, 0, sizeof(*p)); |
| 777 | p->db = sqlite_open(argv[2], mode, &zErrMsg); |
| 778 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 779 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 780 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 781 | free(zErrMsg); |
| 782 | return TCL_ERROR; |
| 783 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 784 | Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 785 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 786 | /* The return value is the value of the sqlite* pointer |
| 787 | */ |
| 788 | sprintf(zBuf, "%p", p->db); |
drh | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame] | 789 | if( strncmp(zBuf,"0x",2) ){ |
| 790 | sprintf(zBuf, "0x%p", p->db); |
| 791 | } |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 792 | Tcl_AppendResult(interp, zBuf, 0); |
| 793 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 794 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 795 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 796 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 797 | #ifdef SQLITE_TEST |
| 798 | { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 799 | extern void Md5_Register(sqlite*); |
| 800 | Md5_Register(p->db); |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 801 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 802 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 803 | return TCL_OK; |
| 804 | } |
| 805 | |
| 806 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 807 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 808 | ** library. |
| 809 | */ |
| 810 | #ifndef USE_TCL_STUBS |
| 811 | # undef Tcl_InitStubs |
| 812 | # define Tcl_InitStubs(a,b,c) |
| 813 | #endif |
| 814 | |
| 815 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 816 | ** Initialize this module. |
| 817 | ** |
| 818 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 819 | ** (Hence there is no namespace. There is no point in using a namespace |
| 820 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 821 | ** used to open a new SQLite database. See the DbMain() routine above |
| 822 | ** for additional information. |
| 823 | */ |
| 824 | int Sqlite_Init(Tcl_Interp *interp){ |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 825 | Tcl_InitStubs(interp, "8.0", 0); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 826 | Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 827 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 828 | return TCL_OK; |
| 829 | } |
| 830 | int Tclsqlite_Init(Tcl_Interp *interp){ |
| 831 | Tcl_InitStubs(interp, "8.0", 0); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 832 | Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 833 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 834 | return TCL_OK; |
| 835 | } |
| 836 | int Sqlite_SafeInit(Tcl_Interp *interp){ |
| 837 | return TCL_OK; |
| 838 | } |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 839 | int Tclsqlite_SafeInit(Tcl_Interp *interp){ |
| 840 | return TCL_OK; |
| 841 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 842 | |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 843 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 844 | /* |
| 845 | ** If compiled using mktclapp, this routine runs to initialize |
| 846 | ** everything. |
| 847 | */ |
| 848 | int Et_AppInit(Tcl_Interp *interp){ |
| 849 | return Sqlite_Init(interp); |
| 850 | } |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 851 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | ** If the macro TCLSH is defined and is one, then put in code for the |
| 855 | ** "main" routine that will initialize Tcl. |
| 856 | */ |
| 857 | #if defined(TCLSH) && TCLSH==1 |
| 858 | static char zMainloop[] = |
| 859 | "set line {}\n" |
| 860 | "while {![eof stdin]} {\n" |
| 861 | "if {$line!=\"\"} {\n" |
| 862 | "puts -nonewline \"> \"\n" |
| 863 | "} else {\n" |
| 864 | "puts -nonewline \"% \"\n" |
| 865 | "}\n" |
| 866 | "flush stdout\n" |
| 867 | "append line [gets stdin]\n" |
| 868 | "if {[info complete $line]} {\n" |
| 869 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 870 | "puts stderr \"Error: $result\"\n" |
| 871 | "} elseif {$result!=\"\"} {\n" |
| 872 | "puts $result\n" |
| 873 | "}\n" |
| 874 | "set line {}\n" |
| 875 | "} else {\n" |
| 876 | "append line \\n\n" |
| 877 | "}\n" |
| 878 | "}\n" |
| 879 | ; |
| 880 | |
| 881 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 882 | int TCLSH_MAIN(int argc, char **argv){ |
| 883 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 884 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 885 | interp = Tcl_CreateInterp(); |
| 886 | Sqlite_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 887 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 888 | { |
| 889 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 890 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 891 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 892 | extern int Md5_Init(Tcl_Interp*); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 893 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 894 | Sqlitetest2_Init(interp); |
| 895 | Sqlitetest3_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 896 | Md5_Init(interp); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 897 | } |
| 898 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 899 | if( argc>=2 ){ |
| 900 | int i; |
| 901 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 902 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 903 | for(i=2; i<argc; i++){ |
| 904 | Tcl_SetVar(interp, "argv", argv[i], |
| 905 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 906 | } |
| 907 | if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 908 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 909 | if( zInfo==0 ) zInfo = interp->result; |
| 910 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 911 | return 1; |
| 912 | } |
| 913 | }else{ |
| 914 | Tcl_GlobalEval(interp, zMainloop); |
| 915 | } |
| 916 | return 0; |
| 917 | } |
| 918 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 919 | |
| 920 | #endif /* !defined(NO_TCL) */ |