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 | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 14 | ** $Id: tclsqlite.c,v 1.48 2003/04/23 12:25:24 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 | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 54 | char *zTrace; /* The trace callback routine */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 55 | char *zAuth; /* The authorization 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 | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 265 | if( pDb->zTrace ){ |
| 266 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 267 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 268 | if( pDb->zAuth ){ |
| 269 | Tcl_Free(pDb->zAuth); |
| 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 | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 300 | ** This routine is called by the SQLite trace handler whenever a new |
| 301 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 302 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 303 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 304 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 305 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 306 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 307 | Tcl_DStringInit(&str); |
| 308 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 309 | Tcl_DStringAppendElement(&str, zSql); |
| 310 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 311 | Tcl_DStringFree(&str); |
| 312 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 316 | ** This routine is called to evaluate an SQL function implemented |
| 317 | ** using TCL script. |
| 318 | */ |
| 319 | static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){ |
| 320 | SqlFunc *p = sqlite_user_data(context); |
| 321 | Tcl_DString cmd; |
| 322 | int i; |
| 323 | int rc; |
| 324 | |
| 325 | Tcl_DStringInit(&cmd); |
| 326 | Tcl_DStringAppend(&cmd, p->zScript, -1); |
| 327 | for(i=0; i<argc; i++){ |
| 328 | Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : ""); |
| 329 | } |
| 330 | rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd)); |
| 331 | if( rc ){ |
| 332 | sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 333 | }else{ |
| 334 | sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1); |
| 335 | } |
| 336 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 337 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 338 | /* |
| 339 | ** This is the authentication function. It appends the authentication |
| 340 | ** type code and the two arguments to zCmd[] then invokes the result |
| 341 | ** on the interpreter. The reply is examined to determine if the |
| 342 | ** authentication fails or succeeds. |
| 343 | */ |
| 344 | static int auth_callback( |
| 345 | void *pArg, |
| 346 | int code, |
| 347 | const char *zArg1, |
| 348 | const char *zArg2, |
| 349 | const char *zArg3, |
| 350 | const char *zArg4 |
| 351 | ){ |
| 352 | char *zCode; |
| 353 | Tcl_DString str; |
| 354 | int rc; |
| 355 | const char *zReply; |
| 356 | SqliteDb *pDb = (SqliteDb*)pArg; |
| 357 | |
| 358 | switch( code ){ |
| 359 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 360 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 361 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 362 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 363 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 364 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 365 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 366 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 367 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 368 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 369 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 370 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 371 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 372 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 373 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 374 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 375 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 376 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 377 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 378 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 379 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 380 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 381 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 382 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
| 383 | default : zCode="????"; break; |
| 384 | } |
| 385 | Tcl_DStringInit(&str); |
| 386 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 387 | Tcl_DStringAppendElement(&str, zCode); |
| 388 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 389 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 390 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 391 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 392 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 393 | Tcl_DStringFree(&str); |
| 394 | zReply = Tcl_GetStringResult(pDb->interp); |
| 395 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 396 | rc = SQLITE_OK; |
| 397 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 398 | rc = SQLITE_DENY; |
| 399 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 400 | rc = SQLITE_IGNORE; |
| 401 | }else{ |
| 402 | rc = 999; |
| 403 | } |
| 404 | return rc; |
| 405 | } |
| 406 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 409 | ** The "sqlite" command below creates a new Tcl command for each |
| 410 | ** connection it opens to an SQLite database. This routine is invoked |
| 411 | ** whenever one of those connection-specific commands is executed |
| 412 | ** in Tcl. For example, if you run Tcl code like this: |
| 413 | ** |
| 414 | ** sqlite db1 "my_database" |
| 415 | ** db1 close |
| 416 | ** |
| 417 | ** The first command opens a connection to the "my_database" database |
| 418 | ** and calls that connection "db1". The second command causes this |
| 419 | ** subroutine to be invoked. |
| 420 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 421 | 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] | 422 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 423 | int choice; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 424 | static const char *DB_strs[] = { |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 425 | "authorizer", "busy", "changes", |
| 426 | "close", "complete", "errorcode", |
| 427 | "eval", "function", "last_insert_rowid", |
| 428 | "timeout", "trace", 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 429 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 430 | enum DB_enum { |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 431 | DB_AUTHORIZER, DB_BUSY, DB_CHANGES, |
| 432 | DB_CLOSE, DB_COMPLETE, DB_ERRORCODE, |
| 433 | DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID, |
| 434 | DB_TIMEOUT, DB_TRACE, |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | if( objc<2 ){ |
| 438 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 439 | return TCL_ERROR; |
| 440 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 441 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 442 | return TCL_ERROR; |
| 443 | } |
| 444 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 445 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 446 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 447 | /* $db authorizer ?CALLBACK? |
| 448 | ** |
| 449 | ** Invoke the given callback to authorize each SQL operation as it is |
| 450 | ** compiled. 5 arguments are appended to the callback before it is |
| 451 | ** invoked: |
| 452 | ** |
| 453 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 454 | ** (2) First descriptive name (depends on authorization type) |
| 455 | ** (3) Second descriptive name |
| 456 | ** (4) Name of the database (ex: "main", "temp") |
| 457 | ** (5) Name of trigger that is doing the access |
| 458 | ** |
| 459 | ** The callback should return on of the following strings: SQLITE_OK, |
| 460 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 461 | ** |
| 462 | ** If this method is invoked with no arguments, the current authorization |
| 463 | ** callback string is returned. |
| 464 | */ |
| 465 | case DB_AUTHORIZER: { |
| 466 | if( objc>3 ){ |
| 467 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 468 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 469 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 470 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 471 | } |
| 472 | }else{ |
| 473 | char *zAuth; |
| 474 | int len; |
| 475 | if( pDb->zAuth ){ |
| 476 | Tcl_Free(pDb->zAuth); |
| 477 | } |
| 478 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 479 | if( zAuth && len>0 ){ |
| 480 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
| 481 | strcpy(pDb->zAuth, zAuth); |
| 482 | }else{ |
| 483 | pDb->zAuth = 0; |
| 484 | } |
| 485 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 486 | if( pDb->zAuth ){ |
| 487 | pDb->interp = interp; |
| 488 | sqlite_set_authorizer(pDb->db, auth_callback, pDb); |
| 489 | }else{ |
| 490 | sqlite_set_authorizer(pDb->db, 0, 0); |
| 491 | } |
| 492 | #endif |
| 493 | } |
| 494 | break; |
| 495 | } |
| 496 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 497 | /* $db busy ?CALLBACK? |
| 498 | ** |
| 499 | ** Invoke the given callback if an SQL statement attempts to open |
| 500 | ** a locked database file. |
| 501 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 502 | case DB_BUSY: { |
| 503 | if( objc>3 ){ |
| 504 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 505 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 506 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 507 | if( pDb->zBusy ){ |
| 508 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 509 | } |
| 510 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 511 | char *zBusy; |
| 512 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 513 | if( pDb->zBusy ){ |
| 514 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 515 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 516 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 517 | if( zBusy && len>0 ){ |
| 518 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 519 | strcpy(pDb->zBusy, zBusy); |
| 520 | }else{ |
| 521 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 522 | } |
| 523 | if( pDb->zBusy ){ |
| 524 | pDb->interp = interp; |
| 525 | sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 526 | }else{ |
| 527 | sqlite_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 530 | break; |
| 531 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 532 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 533 | /* |
| 534 | ** $db changes |
| 535 | ** |
| 536 | ** Return the number of rows that were modified, inserted, or deleted by |
| 537 | ** the most recent "eval". |
| 538 | */ |
| 539 | case DB_CHANGES: { |
| 540 | Tcl_Obj *pResult; |
| 541 | int nChange; |
| 542 | if( objc!=2 ){ |
| 543 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 544 | return TCL_ERROR; |
| 545 | } |
| 546 | nChange = sqlite_changes(pDb->db); |
| 547 | pResult = Tcl_GetObjResult(interp); |
| 548 | Tcl_SetIntObj(pResult, nChange); |
| 549 | break; |
| 550 | } |
| 551 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 552 | /* $db close |
| 553 | ** |
| 554 | ** Shutdown the database |
| 555 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 556 | case DB_CLOSE: { |
| 557 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 558 | break; |
| 559 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 560 | |
| 561 | /* $db complete SQL |
| 562 | ** |
| 563 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 564 | ** additional lines of input are needed. This is similar to the |
| 565 | ** built-in "info complete" command of Tcl. |
| 566 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 567 | case DB_COMPLETE: { |
| 568 | Tcl_Obj *pResult; |
| 569 | int isComplete; |
| 570 | if( objc!=3 ){ |
| 571 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 572 | return TCL_ERROR; |
| 573 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 574 | isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
| 575 | pResult = Tcl_GetObjResult(interp); |
| 576 | Tcl_SetBooleanObj(pResult, isComplete); |
| 577 | break; |
| 578 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | ** $db errorcode |
| 582 | ** |
| 583 | ** Return the numeric error code that was returned by the most recent |
| 584 | ** call to sqlite_exec(). |
| 585 | */ |
| 586 | case DB_ERRORCODE: { |
| 587 | Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc)); |
| 588 | break; |
| 589 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | ** $db eval $sql ?array { ...code... }? |
| 593 | ** |
| 594 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 595 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 596 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 597 | ** If "array" is an empty string, then the values are placed in variables |
| 598 | ** that have the same name as the fields extracted by the query. |
| 599 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 600 | case DB_EVAL: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 601 | CallbackData cbData; |
| 602 | char *zErrMsg; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 603 | char *zSql; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 604 | int rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 605 | #ifdef UTF_TRANSLATION_NEEDED |
| 606 | Tcl_DString dSql; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 607 | int i; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 608 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 609 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 610 | if( objc!=5 && objc!=3 ){ |
| 611 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 612 | return TCL_ERROR; |
| 613 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 614 | pDb->interp = interp; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 615 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 616 | #ifdef UTF_TRANSLATION_NEEDED |
| 617 | Tcl_DStringInit(&dSql); |
| 618 | Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); |
| 619 | zSql = Tcl_DStringValue(&dSql); |
| 620 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 621 | Tcl_IncrRefCount(objv[2]); |
| 622 | if( objc==5 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 623 | cbData.interp = interp; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 624 | cbData.once = 1; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 625 | cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); |
| 626 | cbData.pCode = objv[4]; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 627 | cbData.tcl_rc = TCL_OK; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 628 | cbData.nColName = 0; |
| 629 | cbData.azColName = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 630 | zErrMsg = 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 631 | Tcl_IncrRefCount(objv[3]); |
| 632 | Tcl_IncrRefCount(objv[4]); |
| 633 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); |
| 634 | Tcl_DecrRefCount(objv[4]); |
| 635 | Tcl_DecrRefCount(objv[3]); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 636 | if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 637 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 638 | Tcl_Obj *pList = Tcl_NewObj(); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 639 | cbData.tcl_rc = TCL_OK; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 640 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); |
| 641 | Tcl_SetObjResult(interp, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 642 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 643 | pDb->rc = rc; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 644 | if( rc==SQLITE_ABORT ){ |
| 645 | if( zErrMsg ) free(zErrMsg); |
| 646 | rc = cbData.tcl_rc; |
| 647 | }else if( zErrMsg ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 648 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 649 | free(zErrMsg); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 650 | rc = TCL_ERROR; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 651 | }else if( rc!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 652 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 653 | rc = TCL_ERROR; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 654 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 655 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 656 | Tcl_DecrRefCount(objv[2]); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 657 | #ifdef UTF_TRANSLATION_NEEDED |
| 658 | Tcl_DStringFree(&dSql); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 659 | if( objc==5 && cbData.azColName ){ |
| 660 | for(i=0; i<cbData.nColName; i++){ |
| 661 | if( cbData.azColName[i] ) free(cbData.azColName[i]); |
| 662 | } |
| 663 | free(cbData.azColName); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 664 | cbData.azColName = 0; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 665 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 666 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 667 | return rc; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 668 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 669 | |
| 670 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 671 | ** $db function NAME SCRIPT |
| 672 | ** |
| 673 | ** Create a new SQL function called NAME. Whenever that function is |
| 674 | ** called, invoke SCRIPT to evaluate the function. |
| 675 | */ |
| 676 | case DB_FUNCTION: { |
| 677 | SqlFunc *pFunc; |
| 678 | char *zName; |
| 679 | char *zScript; |
| 680 | int nScript; |
| 681 | if( objc!=4 ){ |
| 682 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 683 | return TCL_ERROR; |
| 684 | } |
| 685 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 686 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 687 | pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 ); |
| 688 | if( pFunc==0 ) return TCL_ERROR; |
| 689 | pFunc->interp = interp; |
| 690 | pFunc->pNext = pDb->pFunc; |
| 691 | pFunc->zScript = (char*)&pFunc[1]; |
| 692 | strcpy(pFunc->zScript, zScript); |
| 693 | sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc); |
| 694 | sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 699 | ** $db last_insert_rowid |
| 700 | ** |
| 701 | ** Return an integer which is the ROWID for the most recent insert. |
| 702 | */ |
| 703 | case DB_LAST_INSERT_ROWID: { |
| 704 | Tcl_Obj *pResult; |
| 705 | int rowid; |
| 706 | if( objc!=2 ){ |
| 707 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 708 | return TCL_ERROR; |
| 709 | } |
| 710 | rowid = sqlite_last_insert_rowid(pDb->db); |
| 711 | pResult = Tcl_GetObjResult(interp); |
| 712 | Tcl_SetIntObj(pResult, rowid); |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 717 | ** $db timeout MILLESECONDS |
| 718 | ** |
| 719 | ** Delay for the number of milliseconds specified when a file is locked. |
| 720 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 721 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 722 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 723 | if( objc!=3 ){ |
| 724 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 725 | return TCL_ERROR; |
| 726 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 727 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 728 | sqlite_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 729 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 730 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame^] | 731 | |
| 732 | /* $db trace ?CALLBACK? |
| 733 | ** |
| 734 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 735 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 736 | ** it is executed. |
| 737 | */ |
| 738 | case DB_TRACE: { |
| 739 | if( objc>3 ){ |
| 740 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 741 | }else if( objc==2 ){ |
| 742 | if( pDb->zTrace ){ |
| 743 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 744 | } |
| 745 | }else{ |
| 746 | char *zTrace; |
| 747 | int len; |
| 748 | if( pDb->zTrace ){ |
| 749 | Tcl_Free(pDb->zTrace); |
| 750 | } |
| 751 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 752 | if( zTrace && len>0 ){ |
| 753 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
| 754 | strcpy(pDb->zTrace, zTrace); |
| 755 | }else{ |
| 756 | pDb->zTrace = 0; |
| 757 | } |
| 758 | if( pDb->zTrace ){ |
| 759 | pDb->interp = interp; |
| 760 | sqlite_trace(pDb->db, DbTraceHandler, pDb); |
| 761 | }else{ |
| 762 | sqlite_trace(pDb->db, 0, 0); |
| 763 | } |
| 764 | } |
| 765 | break; |
| 766 | } |
| 767 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 768 | } /* End of the SWITCH statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 769 | return TCL_OK; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | ** sqlite DBNAME FILENAME ?MODE? |
| 774 | ** |
| 775 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 776 | ** invoked, this routine runs to process that command. |
| 777 | ** |
| 778 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 779 | ** database connection. This command creates a new command named |
| 780 | ** DBNAME that is used to control that connection. The database |
| 781 | ** connection is deleted when the DBNAME command is deleted. |
| 782 | ** |
| 783 | ** The second argument is the name of the directory that contains |
| 784 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 785 | ** |
| 786 | ** For testing purposes, we also support the following: |
| 787 | ** |
| 788 | ** sqlite -encoding |
| 789 | ** |
| 790 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 791 | ** are UTF-8 and iso8859. |
| 792 | ** |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 793 | ** sqlite -version |
| 794 | ** |
| 795 | ** Return the version number of the SQLite library. |
| 796 | ** |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 797 | ** sqlite -tcl-uses-utf |
| 798 | ** |
| 799 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 800 | ** not. Used by tests to make sure the library was compiled |
| 801 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 802 | */ |
| 803 | static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ |
| 804 | int mode; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 805 | SqliteDb *p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 806 | char *zErrMsg; |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 807 | char zBuf[80]; |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 808 | if( argc==2 ){ |
| 809 | if( strcmp(argv[1],"-encoding")==0 ){ |
| 810 | Tcl_AppendResult(interp,sqlite_encoding,0); |
| 811 | return TCL_OK; |
| 812 | } |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 813 | if( strcmp(argv[1],"-version")==0 ){ |
| 814 | Tcl_AppendResult(interp,sqlite_version,0); |
| 815 | return TCL_OK; |
| 816 | } |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 817 | if( strcmp(argv[1],"-tcl-uses-utf")==0 ){ |
| 818 | #ifdef TCL_UTF_MAX |
| 819 | Tcl_AppendResult(interp,"1",0); |
| 820 | #else |
| 821 | Tcl_AppendResult(interp,"0",0); |
| 822 | #endif |
| 823 | return TCL_OK; |
| 824 | } |
| 825 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 826 | if( argc!=3 && argc!=4 ){ |
| 827 | Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
| 828 | " HANDLE FILENAME ?MODE?\"", 0); |
| 829 | return TCL_ERROR; |
| 830 | } |
| 831 | if( argc==3 ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 832 | mode = 0666; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 833 | }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ |
| 834 | return TCL_ERROR; |
| 835 | } |
| 836 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 837 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 838 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 839 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 840 | return TCL_ERROR; |
| 841 | } |
| 842 | memset(p, 0, sizeof(*p)); |
| 843 | p->db = sqlite_open(argv[2], mode, &zErrMsg); |
| 844 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 845 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 846 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 847 | free(zErrMsg); |
| 848 | return TCL_ERROR; |
| 849 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 850 | Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 851 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 852 | /* The return value is the value of the sqlite* pointer |
| 853 | */ |
| 854 | sprintf(zBuf, "%p", p->db); |
drh | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame] | 855 | if( strncmp(zBuf,"0x",2) ){ |
| 856 | sprintf(zBuf, "0x%p", p->db); |
| 857 | } |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 858 | Tcl_AppendResult(interp, zBuf, 0); |
| 859 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 860 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 861 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 862 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 863 | #ifdef SQLITE_TEST |
| 864 | { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 865 | extern void Md5_Register(sqlite*); |
| 866 | Md5_Register(p->db); |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 867 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 868 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 869 | return TCL_OK; |
| 870 | } |
| 871 | |
| 872 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 873 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 874 | ** library. |
| 875 | */ |
| 876 | #ifndef USE_TCL_STUBS |
| 877 | # undef Tcl_InitStubs |
| 878 | # define Tcl_InitStubs(a,b,c) |
| 879 | #endif |
| 880 | |
| 881 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 882 | ** Initialize this module. |
| 883 | ** |
| 884 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 885 | ** (Hence there is no namespace. There is no point in using a namespace |
| 886 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 887 | ** used to open a new SQLite database. See the DbMain() routine above |
| 888 | ** for additional information. |
| 889 | */ |
| 890 | int Sqlite_Init(Tcl_Interp *interp){ |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 891 | Tcl_InitStubs(interp, "8.0", 0); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 892 | Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 893 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 894 | return TCL_OK; |
| 895 | } |
| 896 | int Tclsqlite_Init(Tcl_Interp *interp){ |
| 897 | Tcl_InitStubs(interp, "8.0", 0); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 898 | Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 899 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 900 | return TCL_OK; |
| 901 | } |
| 902 | int Sqlite_SafeInit(Tcl_Interp *interp){ |
| 903 | return TCL_OK; |
| 904 | } |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 905 | int Tclsqlite_SafeInit(Tcl_Interp *interp){ |
| 906 | return TCL_OK; |
| 907 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 908 | |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 909 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 910 | /* |
| 911 | ** If compiled using mktclapp, this routine runs to initialize |
| 912 | ** everything. |
| 913 | */ |
| 914 | int Et_AppInit(Tcl_Interp *interp){ |
| 915 | return Sqlite_Init(interp); |
| 916 | } |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 917 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 918 | |
| 919 | /* |
| 920 | ** If the macro TCLSH is defined and is one, then put in code for the |
| 921 | ** "main" routine that will initialize Tcl. |
| 922 | */ |
| 923 | #if defined(TCLSH) && TCLSH==1 |
| 924 | static char zMainloop[] = |
| 925 | "set line {}\n" |
| 926 | "while {![eof stdin]} {\n" |
| 927 | "if {$line!=\"\"} {\n" |
| 928 | "puts -nonewline \"> \"\n" |
| 929 | "} else {\n" |
| 930 | "puts -nonewline \"% \"\n" |
| 931 | "}\n" |
| 932 | "flush stdout\n" |
| 933 | "append line [gets stdin]\n" |
| 934 | "if {[info complete $line]} {\n" |
| 935 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 936 | "puts stderr \"Error: $result\"\n" |
| 937 | "} elseif {$result!=\"\"} {\n" |
| 938 | "puts $result\n" |
| 939 | "}\n" |
| 940 | "set line {}\n" |
| 941 | "} else {\n" |
| 942 | "append line \\n\n" |
| 943 | "}\n" |
| 944 | "}\n" |
| 945 | ; |
| 946 | |
| 947 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 948 | int TCLSH_MAIN(int argc, char **argv){ |
| 949 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 950 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 951 | interp = Tcl_CreateInterp(); |
| 952 | Sqlite_Init(interp); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 953 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 954 | { |
| 955 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 956 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 957 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 958 | extern int Md5_Init(Tcl_Interp*); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 959 | Sqlitetest1_Init(interp); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 960 | Sqlitetest2_Init(interp); |
| 961 | Sqlitetest3_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 962 | Md5_Init(interp); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 963 | } |
| 964 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 965 | if( argc>=2 ){ |
| 966 | int i; |
| 967 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 968 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 969 | for(i=2; i<argc; i++){ |
| 970 | Tcl_SetVar(interp, "argv", argv[i], |
| 971 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 972 | } |
| 973 | if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 974 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 975 | if( zInfo==0 ) zInfo = interp->result; |
| 976 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 977 | return 1; |
| 978 | } |
| 979 | }else{ |
| 980 | Tcl_GlobalEval(interp, zMainloop); |
| 981 | } |
| 982 | return 0; |
| 983 | } |
| 984 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 985 | |
| 986 | #endif /* !defined(NO_TCL) */ |