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 | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame^] | 14 | ** $Id: tclsqlite.c,v 1.62 2004/05/07 17:57:50 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 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 24 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 25 | /* |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 26 | ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
| 27 | ** have to do a translation when going between the two. Set the |
| 28 | ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
| 29 | ** this translation. |
| 30 | */ |
| 31 | #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
| 32 | # define UTF_TRANSLATION_NEEDED 1 |
| 33 | #endif |
| 34 | |
| 35 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 36 | ** New SQL functions can be created as TCL scripts. Each such function |
| 37 | ** is described by an instance of the following structure. |
| 38 | */ |
| 39 | typedef struct SqlFunc SqlFunc; |
| 40 | struct SqlFunc { |
| 41 | Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
| 42 | char *zScript; /* The script to be run */ |
| 43 | SqlFunc *pNext; /* Next function on the list of them all */ |
| 44 | }; |
| 45 | |
| 46 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 47 | ** There is one instance of this structure for each SQLite database |
| 48 | ** that has been opened by the SQLite TCL interface. |
| 49 | */ |
| 50 | typedef struct SqliteDb SqliteDb; |
| 51 | struct SqliteDb { |
| 52 | sqlite *db; /* The "real" database structure */ |
| 53 | Tcl_Interp *interp; /* The interpreter used for this database */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 54 | char *zBusy; /* The busy callback routine */ |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 55 | char *zCommit; /* The commit hook callback routine */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 56 | char *zTrace; /* The trace callback routine */ |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 57 | char *zProgress; /* The progress callback routine */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 58 | char *zAuth; /* The authorization callback routine */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 59 | SqlFunc *pFunc; /* List of SQL functions */ |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 60 | int rc; /* Return code of most recent sqlite_exec() */ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 64 | ** An instance of this structure passes information thru the sqlite |
| 65 | ** logic from the original TCL command into the callback routine. |
| 66 | */ |
| 67 | typedef struct CallbackData CallbackData; |
| 68 | struct CallbackData { |
| 69 | Tcl_Interp *interp; /* The TCL interpreter */ |
| 70 | char *zArray; /* The array into which data is written */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 71 | Tcl_Obj *pCode; /* The code to execute for each row */ |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 72 | int once; /* Set for first callback only */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 73 | int tcl_rc; /* Return code from TCL script */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 74 | int nColName; /* Number of entries in the azColName[] array */ |
| 75 | char **azColName; /* Column names translated to UTF-8 */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 76 | }; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 77 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 78 | #ifdef UTF_TRANSLATION_NEEDED |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 79 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 80 | ** Called for each row of the result. |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 81 | ** |
| 82 | ** This version is used when TCL expects UTF-8 data but the database |
| 83 | ** uses the ISO8859 format. A translation must occur from ISO8859 into |
| 84 | ** UTF-8. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 85 | */ |
| 86 | static int DbEvalCallback( |
| 87 | void *clientData, /* An instance of CallbackData */ |
| 88 | int nCol, /* Number of columns in the result */ |
| 89 | char ** azCol, /* Data for each column */ |
| 90 | char ** azN /* Name for each column */ |
| 91 | ){ |
| 92 | CallbackData *cbData = (CallbackData*)clientData; |
| 93 | int i, rc; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 94 | Tcl_DString dCol; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 95 | Tcl_DStringInit(&dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 96 | if( cbData->azColName==0 ){ |
| 97 | assert( cbData->once ); |
| 98 | cbData->once = 0; |
| 99 | if( cbData->zArray[0] ){ |
| 100 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 101 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 102 | cbData->azColName = malloc( nCol*sizeof(char*) ); |
| 103 | if( cbData->azColName==0 ){ return 1; } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 104 | cbData->nColName = nCol; |
| 105 | for(i=0; i<nCol; i++){ |
| 106 | Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 107 | cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 ); |
| 108 | if( cbData->azColName[i] ){ |
| 109 | strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol)); |
| 110 | }else{ |
| 111 | return 1; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 112 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 113 | if( cbData->zArray[0] ){ |
| 114 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", |
| 115 | Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
drh | 704027f | 2002-07-15 20:58:47 +0000 | [diff] [blame] | 116 | if( azN[nCol]!=0 ){ |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 117 | Tcl_DString dType; |
| 118 | Tcl_DStringInit(&dType); |
| 119 | Tcl_DStringAppend(&dType, "typeof:", -1); |
| 120 | Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1); |
| 121 | Tcl_DStringFree(&dCol); |
| 122 | Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol); |
| 123 | Tcl_SetVar2(cbData->interp, cbData->zArray, |
| 124 | Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol), |
| 125 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 126 | Tcl_DStringFree(&dType); |
| 127 | } |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 128 | } |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 129 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 130 | Tcl_DStringFree(&dCol); |
| 131 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 132 | } |
| 133 | if( azCol!=0 ){ |
| 134 | if( cbData->zArray[0] ){ |
| 135 | for(i=0; i<nCol; i++){ |
| 136 | char *z = azCol[i]; |
| 137 | if( z==0 ) z = ""; |
| 138 | Tcl_DStringInit(&dCol); |
| 139 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 140 | Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i], |
| 141 | Tcl_DStringValue(&dCol), 0); |
| 142 | Tcl_DStringFree(&dCol); |
| 143 | } |
| 144 | }else{ |
| 145 | for(i=0; i<nCol; i++){ |
| 146 | char *z = azCol[i]; |
| 147 | if( z==0 ) z = ""; |
| 148 | Tcl_DStringInit(&dCol); |
| 149 | Tcl_ExternalToUtfDString(NULL, z, -1, &dCol); |
| 150 | Tcl_SetVar(cbData->interp, cbData->azColName[i], |
| 151 | Tcl_DStringValue(&dCol), 0); |
| 152 | Tcl_DStringFree(&dCol); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
| 157 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 158 | cbData->tcl_rc = rc; |
| 159 | return rc!=TCL_OK; |
| 160 | } |
| 161 | #endif /* UTF_TRANSLATION_NEEDED */ |
| 162 | |
| 163 | #ifndef UTF_TRANSLATION_NEEDED |
| 164 | /* |
| 165 | ** Called for each row of the result. |
| 166 | ** |
| 167 | ** This version is used when either of the following is true: |
| 168 | ** |
| 169 | ** (1) This version of TCL uses UTF-8 and the data in the |
| 170 | ** SQLite database is already in the UTF-8 format. |
| 171 | ** |
| 172 | ** (2) This version of TCL uses ISO8859 and the data in the |
| 173 | ** SQLite database is already in the ISO8859 format. |
| 174 | */ |
| 175 | static int DbEvalCallback( |
| 176 | void *clientData, /* An instance of CallbackData */ |
| 177 | int nCol, /* Number of columns in the result */ |
| 178 | char ** azCol, /* Data for each column */ |
| 179 | char ** azN /* Name for each column */ |
| 180 | ){ |
| 181 | CallbackData *cbData = (CallbackData*)clientData; |
| 182 | int i, rc; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 183 | if( azCol==0 || (cbData->once && cbData->zArray[0]) ){ |
| 184 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0); |
| 185 | for(i=0; i<nCol; i++){ |
| 186 | Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], |
| 187 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 188 | if( azN[nCol] ){ |
| 189 | char *z = sqlite_mprintf("typeof:%s", azN[i]); |
| 190 | Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol], |
| 191 | TCL_LIST_ELEMENT|TCL_APPEND_VALUE); |
| 192 | sqlite_freemem(z); |
| 193 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 194 | } |
| 195 | cbData->once = 0; |
| 196 | } |
| 197 | if( azCol!=0 ){ |
| 198 | if( cbData->zArray[0] ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 199 | for(i=0; i<nCol; i++){ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 200 | char *z = azCol[i]; |
| 201 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 202 | Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 203 | } |
| 204 | }else{ |
| 205 | for(i=0; i<nCol; i++){ |
| 206 | char *z = azCol[i]; |
| 207 | if( z==0 ) z = ""; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 208 | Tcl_SetVar(cbData->interp, azN[i], z, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 212 | rc = Tcl_EvalObj(cbData->interp, cbData->pCode); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 213 | if( rc==TCL_CONTINUE ) rc = TCL_OK; |
| 214 | cbData->tcl_rc = rc; |
| 215 | return rc!=TCL_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 216 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 217 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 218 | |
| 219 | /* |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 220 | ** This is an alternative callback for database queries. Instead |
| 221 | ** of invoking a TCL script to handle the result, this callback just |
| 222 | ** appends each column of the result to a list. After the query |
| 223 | ** is complete, the list is returned. |
| 224 | */ |
| 225 | static int DbEvalCallback2( |
| 226 | void *clientData, /* An instance of CallbackData */ |
| 227 | int nCol, /* Number of columns in the result */ |
| 228 | char ** azCol, /* Data for each column */ |
| 229 | char ** azN /* Name for each column */ |
| 230 | ){ |
| 231 | Tcl_Obj *pList = (Tcl_Obj*)clientData; |
| 232 | int i; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 233 | if( azCol==0 ) return 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 234 | for(i=0; i<nCol; i++){ |
| 235 | Tcl_Obj *pElem; |
| 236 | if( azCol[i] && *azCol[i] ){ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 237 | #ifdef UTF_TRANSLATION_NEEDED |
| 238 | Tcl_DString dCol; |
| 239 | Tcl_DStringInit(&dCol); |
| 240 | Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol); |
| 241 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 242 | Tcl_DStringFree(&dCol); |
| 243 | #else |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 244 | pElem = Tcl_NewStringObj(azCol[i], -1); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 245 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 246 | }else{ |
| 247 | pElem = Tcl_NewObj(); |
| 248 | } |
| 249 | Tcl_ListObjAppendElement(0, pList, pElem); |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 255 | ** This is a second alternative callback for database queries. A the |
| 256 | ** first column of the first row of the result is made the TCL result. |
| 257 | */ |
| 258 | static int DbEvalCallback3( |
| 259 | void *clientData, /* An instance of CallbackData */ |
| 260 | int nCol, /* Number of columns in the result */ |
| 261 | char ** azCol, /* Data for each column */ |
| 262 | char ** azN /* Name for each column */ |
| 263 | ){ |
| 264 | Tcl_Interp *interp = (Tcl_Interp*)clientData; |
| 265 | Tcl_Obj *pElem; |
| 266 | if( azCol==0 ) return 1; |
| 267 | if( nCol==0 ) return 1; |
| 268 | #ifdef UTF_TRANSLATION_NEEDED |
| 269 | { |
| 270 | Tcl_DString dCol; |
| 271 | Tcl_DStringInit(&dCol); |
| 272 | Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol); |
| 273 | pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
| 274 | Tcl_DStringFree(&dCol); |
| 275 | } |
| 276 | #else |
| 277 | pElem = Tcl_NewStringObj(azCol[0], -1); |
| 278 | #endif |
| 279 | Tcl_SetObjResult(interp, pElem); |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 284 | ** Called when the command is deleted. |
| 285 | */ |
| 286 | static void DbDeleteCmd(void *db){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 287 | SqliteDb *pDb = (SqliteDb*)db; |
| 288 | sqlite_close(pDb->db); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 289 | while( pDb->pFunc ){ |
| 290 | SqlFunc *pFunc = pDb->pFunc; |
| 291 | pDb->pFunc = pFunc->pNext; |
| 292 | Tcl_Free((char*)pFunc); |
| 293 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 294 | if( pDb->zBusy ){ |
| 295 | Tcl_Free(pDb->zBusy); |
| 296 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 297 | if( pDb->zTrace ){ |
| 298 | Tcl_Free(pDb->zTrace); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 299 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 300 | if( pDb->zAuth ){ |
| 301 | Tcl_Free(pDb->zAuth); |
| 302 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 303 | Tcl_Free((char*)pDb); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | ** This routine is called when a database file is locked while trying |
| 308 | ** to execute SQL. |
| 309 | */ |
| 310 | static int DbBusyHandler(void *cd, const char *zTable, int nTries){ |
| 311 | SqliteDb *pDb = (SqliteDb*)cd; |
| 312 | int rc; |
| 313 | char zVal[30]; |
| 314 | char *zCmd; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 315 | Tcl_DString cmd; |
| 316 | |
| 317 | Tcl_DStringInit(&cmd); |
| 318 | Tcl_DStringAppend(&cmd, pDb->zBusy, -1); |
| 319 | Tcl_DStringAppendElement(&cmd, zTable); |
| 320 | sprintf(zVal, " %d", nTries); |
| 321 | Tcl_DStringAppend(&cmd, zVal, -1); |
| 322 | zCmd = Tcl_DStringValue(&cmd); |
| 323 | rc = Tcl_Eval(pDb->interp, zCmd); |
| 324 | Tcl_DStringFree(&cmd); |
| 325 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 326 | return 0; |
| 327 | } |
| 328 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 332 | ** This routine is invoked as the 'progress callback' for the database. |
| 333 | */ |
| 334 | static int DbProgressHandler(void *cd){ |
| 335 | SqliteDb *pDb = (SqliteDb*)cd; |
| 336 | int rc; |
| 337 | |
| 338 | assert( pDb->zProgress ); |
| 339 | rc = Tcl_Eval(pDb->interp, pDb->zProgress); |
| 340 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 341 | return 1; |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 347 | ** This routine is called by the SQLite trace handler whenever a new |
| 348 | ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 349 | */ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 350 | static void DbTraceHandler(void *cd, const char *zSql){ |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 351 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 352 | Tcl_DString str; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 353 | |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 354 | Tcl_DStringInit(&str); |
| 355 | Tcl_DStringAppend(&str, pDb->zTrace, -1); |
| 356 | Tcl_DStringAppendElement(&str, zSql); |
| 357 | Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
| 358 | Tcl_DStringFree(&str); |
| 359 | Tcl_ResetResult(pDb->interp); |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 363 | ** This routine is called when a transaction is committed. The |
| 364 | ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
| 365 | ** if it throws an exception, the transaction is rolled back instead |
| 366 | ** of being committed. |
| 367 | */ |
| 368 | static int DbCommitHandler(void *cd){ |
| 369 | SqliteDb *pDb = (SqliteDb*)cd; |
| 370 | int rc; |
| 371 | |
| 372 | rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
| 373 | if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
| 374 | return 1; |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 380 | ** This routine is called to evaluate an SQL function implemented |
| 381 | ** using TCL script. |
| 382 | */ |
| 383 | static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){ |
| 384 | SqlFunc *p = sqlite_user_data(context); |
| 385 | Tcl_DString cmd; |
| 386 | int i; |
| 387 | int rc; |
| 388 | |
| 389 | Tcl_DStringInit(&cmd); |
| 390 | Tcl_DStringAppend(&cmd, p->zScript, -1); |
| 391 | for(i=0; i<argc; i++){ |
| 392 | Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : ""); |
| 393 | } |
| 394 | rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd)); |
| 395 | if( rc ){ |
| 396 | sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1); |
| 397 | }else{ |
| 398 | sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1); |
| 399 | } |
| 400 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 401 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 402 | /* |
| 403 | ** This is the authentication function. It appends the authentication |
| 404 | ** type code and the two arguments to zCmd[] then invokes the result |
| 405 | ** on the interpreter. The reply is examined to determine if the |
| 406 | ** authentication fails or succeeds. |
| 407 | */ |
| 408 | static int auth_callback( |
| 409 | void *pArg, |
| 410 | int code, |
| 411 | const char *zArg1, |
| 412 | const char *zArg2, |
| 413 | const char *zArg3, |
| 414 | const char *zArg4 |
| 415 | ){ |
| 416 | char *zCode; |
| 417 | Tcl_DString str; |
| 418 | int rc; |
| 419 | const char *zReply; |
| 420 | SqliteDb *pDb = (SqliteDb*)pArg; |
| 421 | |
| 422 | switch( code ){ |
| 423 | case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
| 424 | case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
| 425 | case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
| 426 | case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
| 427 | case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
| 428 | case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
| 429 | case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
| 430 | case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
| 431 | case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
| 432 | case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
| 433 | case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
| 434 | case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
| 435 | case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
| 436 | case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
| 437 | case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
| 438 | case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
| 439 | case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
| 440 | case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
| 441 | case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
| 442 | case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
| 443 | case SQLITE_READ : zCode="SQLITE_READ"; break; |
| 444 | case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
| 445 | case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
| 446 | case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
drh | 81e293b | 2003-06-06 19:00:42 +0000 | [diff] [blame] | 447 | case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
| 448 | case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 449 | default : zCode="????"; break; |
| 450 | } |
| 451 | Tcl_DStringInit(&str); |
| 452 | Tcl_DStringAppend(&str, pDb->zAuth, -1); |
| 453 | Tcl_DStringAppendElement(&str, zCode); |
| 454 | Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
| 455 | Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
| 456 | Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
| 457 | Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
| 458 | rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
| 459 | Tcl_DStringFree(&str); |
| 460 | zReply = Tcl_GetStringResult(pDb->interp); |
| 461 | if( strcmp(zReply,"SQLITE_OK")==0 ){ |
| 462 | rc = SQLITE_OK; |
| 463 | }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
| 464 | rc = SQLITE_DENY; |
| 465 | }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
| 466 | rc = SQLITE_IGNORE; |
| 467 | }else{ |
| 468 | rc = 999; |
| 469 | } |
| 470 | return rc; |
| 471 | } |
| 472 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 473 | |
| 474 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 475 | ** The "sqlite" command below creates a new Tcl command for each |
| 476 | ** connection it opens to an SQLite database. This routine is invoked |
| 477 | ** whenever one of those connection-specific commands is executed |
| 478 | ** in Tcl. For example, if you run Tcl code like this: |
| 479 | ** |
| 480 | ** sqlite db1 "my_database" |
| 481 | ** db1 close |
| 482 | ** |
| 483 | ** The first command opens a connection to the "my_database" database |
| 484 | ** and calls that connection "db1". The second command causes this |
| 485 | ** subroutine to be invoked. |
| 486 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 487 | 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] | 488 | SqliteDb *pDb = (SqliteDb*)cd; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 489 | int choice; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 490 | int rc = TCL_OK; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 491 | static const char *DB_strs[] = { |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 492 | "authorizer", "busy", "changes", |
| 493 | "close", "commit_hook", "complete", |
| 494 | "errorcode", "eval", "function", |
| 495 | "last_insert_rowid", "last_statement_changes", "onecolumn", |
| 496 | "progress", "rekey", "timeout", |
| 497 | "trace", |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 498 | 0 |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 499 | }; |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 500 | enum DB_enum { |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 501 | DB_AUTHORIZER, DB_BUSY, DB_CHANGES, |
| 502 | DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE, |
| 503 | DB_ERRORCODE, DB_EVAL, DB_FUNCTION, |
| 504 | DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN, |
| 505 | DB_PROGRESS, DB_REKEY, DB_TIMEOUT, |
| 506 | DB_TRACE |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | if( objc<2 ){ |
| 510 | Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 511 | return TCL_ERROR; |
| 512 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 513 | if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 514 | return TCL_ERROR; |
| 515 | } |
| 516 | |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 517 | switch( (enum DB_enum)choice ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 518 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 519 | /* $db authorizer ?CALLBACK? |
| 520 | ** |
| 521 | ** Invoke the given callback to authorize each SQL operation as it is |
| 522 | ** compiled. 5 arguments are appended to the callback before it is |
| 523 | ** invoked: |
| 524 | ** |
| 525 | ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
| 526 | ** (2) First descriptive name (depends on authorization type) |
| 527 | ** (3) Second descriptive name |
| 528 | ** (4) Name of the database (ex: "main", "temp") |
| 529 | ** (5) Name of trigger that is doing the access |
| 530 | ** |
| 531 | ** The callback should return on of the following strings: SQLITE_OK, |
| 532 | ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
| 533 | ** |
| 534 | ** If this method is invoked with no arguments, the current authorization |
| 535 | ** callback string is returned. |
| 536 | */ |
| 537 | case DB_AUTHORIZER: { |
| 538 | if( objc>3 ){ |
| 539 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 540 | }else if( objc==2 ){ |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 541 | if( pDb->zAuth ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 542 | Tcl_AppendResult(interp, pDb->zAuth, 0); |
| 543 | } |
| 544 | }else{ |
| 545 | char *zAuth; |
| 546 | int len; |
| 547 | if( pDb->zAuth ){ |
| 548 | Tcl_Free(pDb->zAuth); |
| 549 | } |
| 550 | zAuth = Tcl_GetStringFromObj(objv[2], &len); |
| 551 | if( zAuth && len>0 ){ |
| 552 | pDb->zAuth = Tcl_Alloc( len + 1 ); |
| 553 | strcpy(pDb->zAuth, zAuth); |
| 554 | }else{ |
| 555 | pDb->zAuth = 0; |
| 556 | } |
| 557 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 558 | if( pDb->zAuth ){ |
| 559 | pDb->interp = interp; |
| 560 | sqlite_set_authorizer(pDb->db, auth_callback, pDb); |
| 561 | }else{ |
| 562 | sqlite_set_authorizer(pDb->db, 0, 0); |
| 563 | } |
| 564 | #endif |
| 565 | } |
| 566 | break; |
| 567 | } |
| 568 | |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 569 | /* $db busy ?CALLBACK? |
| 570 | ** |
| 571 | ** Invoke the given callback if an SQL statement attempts to open |
| 572 | ** a locked database file. |
| 573 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 574 | case DB_BUSY: { |
| 575 | if( objc>3 ){ |
| 576 | Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 577 | return TCL_ERROR; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 578 | }else if( objc==2 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 579 | if( pDb->zBusy ){ |
| 580 | Tcl_AppendResult(interp, pDb->zBusy, 0); |
| 581 | } |
| 582 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 583 | char *zBusy; |
| 584 | int len; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 585 | if( pDb->zBusy ){ |
| 586 | Tcl_Free(pDb->zBusy); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 587 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 588 | zBusy = Tcl_GetStringFromObj(objv[2], &len); |
| 589 | if( zBusy && len>0 ){ |
| 590 | pDb->zBusy = Tcl_Alloc( len + 1 ); |
| 591 | strcpy(pDb->zBusy, zBusy); |
| 592 | }else{ |
| 593 | pDb->zBusy = 0; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 594 | } |
| 595 | if( pDb->zBusy ){ |
| 596 | pDb->interp = interp; |
| 597 | sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 598 | }else{ |
| 599 | sqlite_busy_handler(pDb->db, 0, 0); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 602 | break; |
| 603 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 604 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 605 | /* $db progress ?N CALLBACK? |
| 606 | ** |
| 607 | ** Invoke the given callback every N virtual machine opcodes while executing |
| 608 | ** queries. |
| 609 | */ |
| 610 | case DB_PROGRESS: { |
| 611 | if( objc==2 ){ |
| 612 | if( pDb->zProgress ){ |
| 613 | Tcl_AppendResult(interp, pDb->zProgress, 0); |
| 614 | } |
| 615 | }else if( objc==4 ){ |
| 616 | char *zProgress; |
| 617 | int len; |
| 618 | int N; |
| 619 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ |
| 620 | return TCL_ERROR; |
| 621 | }; |
| 622 | if( pDb->zProgress ){ |
| 623 | Tcl_Free(pDb->zProgress); |
| 624 | } |
| 625 | zProgress = Tcl_GetStringFromObj(objv[3], &len); |
| 626 | if( zProgress && len>0 ){ |
| 627 | pDb->zProgress = Tcl_Alloc( len + 1 ); |
| 628 | strcpy(pDb->zProgress, zProgress); |
| 629 | }else{ |
| 630 | pDb->zProgress = 0; |
| 631 | } |
| 632 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 633 | if( pDb->zProgress ){ |
| 634 | pDb->interp = interp; |
| 635 | sqlite_progress_handler(pDb->db, N, DbProgressHandler, pDb); |
| 636 | }else{ |
| 637 | sqlite_progress_handler(pDb->db, 0, 0, 0); |
| 638 | } |
| 639 | #endif |
| 640 | }else{ |
| 641 | Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); |
| 642 | return TCL_ERROR; |
| 643 | } |
| 644 | break; |
| 645 | } |
| 646 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 647 | /* |
| 648 | ** $db changes |
| 649 | ** |
| 650 | ** Return the number of rows that were modified, inserted, or deleted by |
| 651 | ** the most recent "eval". |
| 652 | */ |
| 653 | case DB_CHANGES: { |
| 654 | Tcl_Obj *pResult; |
| 655 | int nChange; |
| 656 | if( objc!=2 ){ |
| 657 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 658 | return TCL_ERROR; |
| 659 | } |
| 660 | nChange = sqlite_changes(pDb->db); |
| 661 | pResult = Tcl_GetObjResult(interp); |
| 662 | Tcl_SetIntObj(pResult, nChange); |
| 663 | break; |
| 664 | } |
| 665 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 666 | /* |
| 667 | ** $db last_statement_changes |
| 668 | ** |
| 669 | ** Return the number of rows that were modified, inserted, or deleted by |
| 670 | ** the last statment to complete execution (excluding changes due to |
| 671 | ** triggers) |
| 672 | */ |
| 673 | case DB_LAST_STATEMENT_CHANGES: { |
| 674 | Tcl_Obj *pResult; |
| 675 | int lsChange; |
| 676 | if( objc!=2 ){ |
| 677 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 678 | return TCL_ERROR; |
| 679 | } |
| 680 | lsChange = sqlite_last_statement_changes(pDb->db); |
| 681 | pResult = Tcl_GetObjResult(interp); |
| 682 | Tcl_SetIntObj(pResult, lsChange); |
| 683 | break; |
| 684 | } |
| 685 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 686 | /* $db close |
| 687 | ** |
| 688 | ** Shutdown the database |
| 689 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 690 | case DB_CLOSE: { |
| 691 | Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
| 692 | break; |
| 693 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 694 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 695 | /* $db commit_hook ?CALLBACK? |
| 696 | ** |
| 697 | ** Invoke the given callback just before committing every SQL transaction. |
| 698 | ** If the callback throws an exception or returns non-zero, then the |
| 699 | ** transaction is aborted. If CALLBACK is an empty string, the callback |
| 700 | ** is disabled. |
| 701 | */ |
| 702 | case DB_COMMIT_HOOK: { |
| 703 | if( objc>3 ){ |
| 704 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 705 | }else if( objc==2 ){ |
| 706 | if( pDb->zCommit ){ |
| 707 | Tcl_AppendResult(interp, pDb->zCommit, 0); |
| 708 | } |
| 709 | }else{ |
| 710 | char *zCommit; |
| 711 | int len; |
| 712 | if( pDb->zCommit ){ |
| 713 | Tcl_Free(pDb->zCommit); |
| 714 | } |
| 715 | zCommit = Tcl_GetStringFromObj(objv[2], &len); |
| 716 | if( zCommit && len>0 ){ |
| 717 | pDb->zCommit = Tcl_Alloc( len + 1 ); |
| 718 | strcpy(pDb->zCommit, zCommit); |
| 719 | }else{ |
| 720 | pDb->zCommit = 0; |
| 721 | } |
| 722 | if( pDb->zCommit ){ |
| 723 | pDb->interp = interp; |
| 724 | sqlite_commit_hook(pDb->db, DbCommitHandler, pDb); |
| 725 | }else{ |
| 726 | sqlite_commit_hook(pDb->db, 0, 0); |
| 727 | } |
| 728 | } |
| 729 | break; |
| 730 | } |
| 731 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 732 | /* $db complete SQL |
| 733 | ** |
| 734 | ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
| 735 | ** additional lines of input are needed. This is similar to the |
| 736 | ** built-in "info complete" command of Tcl. |
| 737 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 738 | case DB_COMPLETE: { |
| 739 | Tcl_Obj *pResult; |
| 740 | int isComplete; |
| 741 | if( objc!=3 ){ |
| 742 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 743 | return TCL_ERROR; |
| 744 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 745 | isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
| 746 | pResult = Tcl_GetObjResult(interp); |
| 747 | Tcl_SetBooleanObj(pResult, isComplete); |
| 748 | break; |
| 749 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | ** $db errorcode |
| 753 | ** |
| 754 | ** Return the numeric error code that was returned by the most recent |
| 755 | ** call to sqlite_exec(). |
| 756 | */ |
| 757 | case DB_ERRORCODE: { |
| 758 | Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc)); |
| 759 | break; |
| 760 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 761 | |
| 762 | /* |
| 763 | ** $db eval $sql ?array { ...code... }? |
| 764 | ** |
| 765 | ** The SQL statement in $sql is evaluated. For each row, the values are |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 766 | ** placed in elements of the array named "array" and ...code... is executed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 767 | ** If "array" and "code" are omitted, then no callback is every invoked. |
| 768 | ** If "array" is an empty string, then the values are placed in variables |
| 769 | ** that have the same name as the fields extracted by the query. |
| 770 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 771 | case DB_EVAL: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 772 | CallbackData cbData; |
| 773 | char *zErrMsg; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 774 | char *zSql; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 775 | #ifdef UTF_TRANSLATION_NEEDED |
| 776 | Tcl_DString dSql; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 777 | int i; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 778 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 779 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 780 | if( objc!=5 && objc!=3 ){ |
| 781 | Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 782 | return TCL_ERROR; |
| 783 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 784 | pDb->interp = interp; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 785 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 786 | #ifdef UTF_TRANSLATION_NEEDED |
| 787 | Tcl_DStringInit(&dSql); |
| 788 | Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql); |
| 789 | zSql = Tcl_DStringValue(&dSql); |
| 790 | #endif |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 791 | Tcl_IncrRefCount(objv[2]); |
| 792 | if( objc==5 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 793 | cbData.interp = interp; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 794 | cbData.once = 1; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 795 | cbData.zArray = Tcl_GetStringFromObj(objv[3], 0); |
| 796 | cbData.pCode = objv[4]; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 797 | cbData.tcl_rc = TCL_OK; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 798 | cbData.nColName = 0; |
| 799 | cbData.azColName = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 800 | zErrMsg = 0; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 801 | Tcl_IncrRefCount(objv[3]); |
| 802 | Tcl_IncrRefCount(objv[4]); |
| 803 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg); |
| 804 | Tcl_DecrRefCount(objv[4]); |
| 805 | Tcl_DecrRefCount(objv[3]); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 806 | if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 807 | }else{ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 808 | Tcl_Obj *pList = Tcl_NewObj(); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 809 | cbData.tcl_rc = TCL_OK; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 810 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg); |
| 811 | Tcl_SetObjResult(interp, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 812 | } |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 813 | pDb->rc = rc; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 814 | if( rc==SQLITE_ABORT ){ |
| 815 | if( zErrMsg ) free(zErrMsg); |
| 816 | rc = cbData.tcl_rc; |
| 817 | }else if( zErrMsg ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 818 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 819 | free(zErrMsg); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 820 | rc = TCL_ERROR; |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 821 | }else if( rc!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 822 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 823 | rc = TCL_ERROR; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 824 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 825 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 826 | Tcl_DecrRefCount(objv[2]); |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 827 | #ifdef UTF_TRANSLATION_NEEDED |
| 828 | Tcl_DStringFree(&dSql); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 829 | if( objc==5 && cbData.azColName ){ |
| 830 | for(i=0; i<cbData.nColName; i++){ |
| 831 | if( cbData.azColName[i] ) free(cbData.azColName[i]); |
| 832 | } |
| 833 | free(cbData.azColName); |
drh | ce92706 | 2001-11-09 13:41:09 +0000 | [diff] [blame] | 834 | cbData.azColName = 0; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 835 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 836 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 837 | return rc; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 838 | } |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 839 | |
| 840 | /* |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 841 | ** $db function NAME SCRIPT |
| 842 | ** |
| 843 | ** Create a new SQL function called NAME. Whenever that function is |
| 844 | ** called, invoke SCRIPT to evaluate the function. |
| 845 | */ |
| 846 | case DB_FUNCTION: { |
| 847 | SqlFunc *pFunc; |
| 848 | char *zName; |
| 849 | char *zScript; |
| 850 | int nScript; |
| 851 | if( objc!=4 ){ |
| 852 | Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
| 853 | return TCL_ERROR; |
| 854 | } |
| 855 | zName = Tcl_GetStringFromObj(objv[2], 0); |
| 856 | zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
| 857 | pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 ); |
| 858 | if( pFunc==0 ) return TCL_ERROR; |
| 859 | pFunc->interp = interp; |
| 860 | pFunc->pNext = pDb->pFunc; |
| 861 | pFunc->zScript = (char*)&pFunc[1]; |
| 862 | strcpy(pFunc->zScript, zScript); |
| 863 | sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc); |
| 864 | sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC); |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 869 | ** $db last_insert_rowid |
| 870 | ** |
| 871 | ** Return an integer which is the ROWID for the most recent insert. |
| 872 | */ |
| 873 | case DB_LAST_INSERT_ROWID: { |
| 874 | Tcl_Obj *pResult; |
| 875 | int rowid; |
| 876 | if( objc!=2 ){ |
| 877 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 878 | return TCL_ERROR; |
| 879 | } |
| 880 | rowid = sqlite_last_insert_rowid(pDb->db); |
| 881 | pResult = Tcl_GetObjResult(interp); |
| 882 | Tcl_SetIntObj(pResult, rowid); |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | /* |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 887 | ** $db onecolumn SQL |
| 888 | ** |
| 889 | ** Return a single column from a single row of the given SQL query. |
| 890 | */ |
| 891 | case DB_ONECOLUMN: { |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 892 | char *zSql; |
| 893 | char *zErrMsg = 0; |
| 894 | if( objc!=3 ){ |
| 895 | Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
| 896 | return TCL_ERROR; |
| 897 | } |
| 898 | zSql = Tcl_GetStringFromObj(objv[2], 0); |
| 899 | rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg); |
| 900 | if( rc==SQLITE_ABORT ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 901 | rc = SQLITE_OK; |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 902 | }else if( zErrMsg ){ |
| 903 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
| 904 | free(zErrMsg); |
| 905 | rc = TCL_ERROR; |
| 906 | }else if( rc!=SQLITE_OK ){ |
| 907 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 908 | rc = TCL_ERROR; |
| 909 | } |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 914 | ** $db rekey KEY |
| 915 | ** |
| 916 | ** Change the encryption key on the currently open database. |
| 917 | */ |
| 918 | case DB_REKEY: { |
| 919 | int nKey; |
| 920 | void *pKey; |
| 921 | if( objc!=3 ){ |
| 922 | Tcl_WrongNumArgs(interp, 2, objv, "KEY"); |
| 923 | return TCL_ERROR; |
| 924 | } |
| 925 | pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 926 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 927 | rc = sqlite_rekey(pDb->db, pKey, nKey); |
| 928 | if( rc ){ |
| 929 | Tcl_AppendResult(interp, sqlite_error_string(rc), 0); |
| 930 | rc = TCL_ERROR; |
| 931 | } |
| 932 | #endif |
| 933 | break; |
| 934 | } |
| 935 | |
| 936 | /* |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 937 | ** $db timeout MILLESECONDS |
| 938 | ** |
| 939 | ** Delay for the number of milliseconds specified when a file is locked. |
| 940 | */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 941 | case DB_TIMEOUT: { |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 942 | int ms; |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 943 | if( objc!=3 ){ |
| 944 | Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 945 | return TCL_ERROR; |
| 946 | } |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 947 | if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 948 | sqlite_busy_timeout(pDb->db, ms); |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 949 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 950 | } |
drh | b5a20d3 | 2003-04-23 12:25:23 +0000 | [diff] [blame] | 951 | |
| 952 | /* $db trace ?CALLBACK? |
| 953 | ** |
| 954 | ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
| 955 | ** that is executed. The text of the SQL is appended to CALLBACK before |
| 956 | ** it is executed. |
| 957 | */ |
| 958 | case DB_TRACE: { |
| 959 | if( objc>3 ){ |
| 960 | Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
| 961 | }else if( objc==2 ){ |
| 962 | if( pDb->zTrace ){ |
| 963 | Tcl_AppendResult(interp, pDb->zTrace, 0); |
| 964 | } |
| 965 | }else{ |
| 966 | char *zTrace; |
| 967 | int len; |
| 968 | if( pDb->zTrace ){ |
| 969 | Tcl_Free(pDb->zTrace); |
| 970 | } |
| 971 | zTrace = Tcl_GetStringFromObj(objv[2], &len); |
| 972 | if( zTrace && len>0 ){ |
| 973 | pDb->zTrace = Tcl_Alloc( len + 1 ); |
| 974 | strcpy(pDb->zTrace, zTrace); |
| 975 | }else{ |
| 976 | pDb->zTrace = 0; |
| 977 | } |
| 978 | if( pDb->zTrace ){ |
| 979 | pDb->interp = interp; |
| 980 | sqlite_trace(pDb->db, DbTraceHandler, pDb); |
| 981 | }else{ |
| 982 | sqlite_trace(pDb->db, 0, 0); |
| 983 | } |
| 984 | } |
| 985 | break; |
| 986 | } |
| 987 | |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 988 | } /* End of the SWITCH statement */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 989 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 993 | ** sqlite DBNAME FILENAME ?MODE? ?-key KEY? |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 994 | ** |
| 995 | ** This is the main Tcl command. When the "sqlite" Tcl command is |
| 996 | ** invoked, this routine runs to process that command. |
| 997 | ** |
| 998 | ** The first argument, DBNAME, is an arbitrary name for a new |
| 999 | ** database connection. This command creates a new command named |
| 1000 | ** DBNAME that is used to control that connection. The database |
| 1001 | ** connection is deleted when the DBNAME command is deleted. |
| 1002 | ** |
| 1003 | ** The second argument is the name of the directory that contains |
| 1004 | ** the sqlite database that is to be accessed. |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1005 | ** |
| 1006 | ** For testing purposes, we also support the following: |
| 1007 | ** |
| 1008 | ** sqlite -encoding |
| 1009 | ** |
| 1010 | ** Return the encoding used by LIKE and GLOB operators. Choices |
| 1011 | ** are UTF-8 and iso8859. |
| 1012 | ** |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 1013 | ** sqlite -version |
| 1014 | ** |
| 1015 | ** Return the version number of the SQLite library. |
| 1016 | ** |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1017 | ** sqlite -tcl-uses-utf |
| 1018 | ** |
| 1019 | ** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if |
| 1020 | ** not. Used by tests to make sure the library was compiled |
| 1021 | ** correctly. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1022 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1023 | static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1024 | int mode; |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1025 | SqliteDb *p; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1026 | void *pKey = 0; |
| 1027 | int nKey = 0; |
| 1028 | const char *zArg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1029 | char *zErrMsg; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1030 | const char *zFile; |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1031 | char zBuf[80]; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1032 | if( objc==2 ){ |
| 1033 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
| 1034 | if( strcmp(zArg,"-encoding")==0 ){ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1035 | Tcl_AppendResult(interp,sqlite_encoding,0); |
| 1036 | return TCL_OK; |
| 1037 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1038 | if( strcmp(zArg,"-version")==0 ){ |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 1039 | Tcl_AppendResult(interp,sqlite_version,0); |
| 1040 | return TCL_OK; |
| 1041 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1042 | if( strcmp(zArg,"-has-codec")==0 ){ |
| 1043 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1044 | Tcl_AppendResult(interp,"1",0); |
| 1045 | #else |
| 1046 | Tcl_AppendResult(interp,"0",0); |
| 1047 | #endif |
| 1048 | return TCL_OK; |
| 1049 | } |
| 1050 | if( strcmp(zArg,"-tcl-uses-utf")==0 ){ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 1051 | #ifdef TCL_UTF_MAX |
| 1052 | Tcl_AppendResult(interp,"1",0); |
| 1053 | #else |
| 1054 | Tcl_AppendResult(interp,"0",0); |
| 1055 | #endif |
| 1056 | return TCL_OK; |
| 1057 | } |
| 1058 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1059 | if( objc==5 || objc==6 ){ |
| 1060 | zArg = Tcl_GetStringFromObj(objv[objc-2], 0); |
| 1061 | if( strcmp(zArg,"-key")==0 ){ |
| 1062 | pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey); |
| 1063 | objc -= 2; |
| 1064 | } |
| 1065 | } |
| 1066 | if( objc!=3 && objc!=4 ){ |
| 1067 | Tcl_WrongNumArgs(interp, 1, objv, |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1068 | #ifdef SQLITE_HAS_CODEC |
| 1069 | "HANDLE FILENAME ?-key CODEC-KEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1070 | #else |
| 1071 | "HANDLE FILENAME ?MODE?" |
| 1072 | #endif |
| 1073 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1074 | return TCL_ERROR; |
| 1075 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1076 | if( objc==3 ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 1077 | mode = 0666; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1078 | }else if( Tcl_GetIntFromObj(interp, objv[3], &mode)!=TCL_OK ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1079 | return TCL_ERROR; |
| 1080 | } |
| 1081 | zErrMsg = 0; |
drh | 4cdc9e8 | 2000-08-04 14:56:24 +0000 | [diff] [blame] | 1082 | p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1083 | if( p==0 ){ |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1084 | Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
| 1085 | return TCL_ERROR; |
| 1086 | } |
| 1087 | memset(p, 0, sizeof(*p)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1088 | zFile = Tcl_GetStringFromObj(objv[2], 0); |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1089 | #ifdef SQLITE_HAS_CODEC |
drh | e384a4e | 2004-02-12 20:49:36 +0000 | [diff] [blame] | 1090 | p->db = sqlite_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg); |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 1091 | #else |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1092 | p->db = sqlite_open(zFile, mode, &zErrMsg); |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 1093 | #endif |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1094 | if( p->db==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1095 | Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
drh | bec3f40 | 2000-08-04 13:49:02 +0000 | [diff] [blame] | 1096 | Tcl_Free((char*)p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1097 | free(zErrMsg); |
| 1098 | return TCL_ERROR; |
| 1099 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1100 | zArg = Tcl_GetStringFromObj(objv[1], 0); |
| 1101 | Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1102 | |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1103 | /* The return value is the value of the sqlite* pointer |
| 1104 | */ |
| 1105 | sprintf(zBuf, "%p", p->db); |
drh | 5e5377f | 2002-07-07 17:12:36 +0000 | [diff] [blame] | 1106 | if( strncmp(zBuf,"0x",2) ){ |
| 1107 | sprintf(zBuf, "0x%p", p->db); |
| 1108 | } |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1109 | Tcl_AppendResult(interp, zBuf, 0); |
| 1110 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1111 | /* If compiled with SQLITE_TEST turned on, then register the "md5sum" |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1112 | ** SQL function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1113 | */ |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 1114 | #ifdef SQLITE_TEST |
| 1115 | { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1116 | extern void Md5_Register(sqlite*); |
| 1117 | Md5_Register(p->db); |
drh | 06b2718 | 2002-06-26 20:06:05 +0000 | [diff] [blame] | 1118 | } |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 1119 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1120 | return TCL_OK; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1124 | ** Provide a dummy Tcl_InitStubs if we are using this as a static |
| 1125 | ** library. |
| 1126 | */ |
| 1127 | #ifndef USE_TCL_STUBS |
| 1128 | # undef Tcl_InitStubs |
| 1129 | # define Tcl_InitStubs(a,b,c) |
| 1130 | #endif |
| 1131 | |
| 1132 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1133 | ** Initialize this module. |
| 1134 | ** |
| 1135 | ** This Tcl module contains only a single new Tcl command named "sqlite". |
| 1136 | ** (Hence there is no namespace. There is no point in using a namespace |
| 1137 | ** if the extension only supplies one new name!) The "sqlite" command is |
| 1138 | ** used to open a new SQLite database. See the DbMain() routine above |
| 1139 | ** for additional information. |
| 1140 | */ |
| 1141 | int Sqlite_Init(Tcl_Interp *interp){ |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1142 | Tcl_InitStubs(interp, "8.0", 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1143 | Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1144 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1145 | return TCL_OK; |
| 1146 | } |
| 1147 | int Tclsqlite_Init(Tcl_Interp *interp){ |
| 1148 | Tcl_InitStubs(interp, "8.0", 0); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1149 | Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1150 | Tcl_PkgProvide(interp, "sqlite", "2.0"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1151 | return TCL_OK; |
| 1152 | } |
| 1153 | int Sqlite_SafeInit(Tcl_Interp *interp){ |
| 1154 | return TCL_OK; |
| 1155 | } |
drh | 90ca975 | 2001-09-28 17:47:14 +0000 | [diff] [blame] | 1156 | int Tclsqlite_SafeInit(Tcl_Interp *interp){ |
| 1157 | return TCL_OK; |
| 1158 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1159 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1160 | |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 1161 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1162 | /* |
| 1163 | ** If compiled using mktclapp, this routine runs to initialize |
| 1164 | ** everything. |
| 1165 | */ |
| 1166 | int Et_AppInit(Tcl_Interp *interp){ |
| 1167 | return Sqlite_Init(interp); |
| 1168 | } |
drh | 3cebbde | 2000-10-19 14:59:27 +0000 | [diff] [blame] | 1169 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1170 | |
| 1171 | /* |
| 1172 | ** If the macro TCLSH is defined and is one, then put in code for the |
| 1173 | ** "main" routine that will initialize Tcl. |
| 1174 | */ |
| 1175 | #if defined(TCLSH) && TCLSH==1 |
| 1176 | static char zMainloop[] = |
| 1177 | "set line {}\n" |
| 1178 | "while {![eof stdin]} {\n" |
| 1179 | "if {$line!=\"\"} {\n" |
| 1180 | "puts -nonewline \"> \"\n" |
| 1181 | "} else {\n" |
| 1182 | "puts -nonewline \"% \"\n" |
| 1183 | "}\n" |
| 1184 | "flush stdout\n" |
| 1185 | "append line [gets stdin]\n" |
| 1186 | "if {[info complete $line]} {\n" |
| 1187 | "if {[catch {uplevel #0 $line} result]} {\n" |
| 1188 | "puts stderr \"Error: $result\"\n" |
| 1189 | "} elseif {$result!=\"\"} {\n" |
| 1190 | "puts $result\n" |
| 1191 | "}\n" |
| 1192 | "set line {}\n" |
| 1193 | "} else {\n" |
| 1194 | "append line \\n\n" |
| 1195 | "}\n" |
| 1196 | "}\n" |
| 1197 | ; |
| 1198 | |
| 1199 | #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
| 1200 | int TCLSH_MAIN(int argc, char **argv){ |
| 1201 | Tcl_Interp *interp; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 1202 | Tcl_FindExecutable(argv[0]); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1203 | interp = Tcl_CreateInterp(); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1204 | /* Sqlite_Init(interp); */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1205 | #ifdef SQLITE_TEST |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1206 | { |
| 1207 | extern int Sqlitetest1_Init(Tcl_Interp*); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1208 | extern int Sqlitetest2_Init(Tcl_Interp*); |
| 1209 | extern int Sqlitetest3_Init(Tcl_Interp*); |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 1210 | extern int Sqlitetest4_Init(Tcl_Interp*); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 1211 | extern int Sqlitetest5_Init(Tcl_Interp*); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1212 | extern int Md5_Init(Tcl_Interp*); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1213 | /* Sqlitetest1_Init(interp); */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1214 | Sqlitetest2_Init(interp); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame^] | 1215 | Sqlitetest3_Init(interp); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1216 | /* Sqlitetest4_Init(interp); */ |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 1217 | Sqlitetest5_Init(interp); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 1218 | Md5_Init(interp); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1219 | } |
| 1220 | #endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1221 | if( argc>=2 ){ |
| 1222 | int i; |
| 1223 | Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
| 1224 | Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
| 1225 | for(i=2; i<argc; i++){ |
| 1226 | Tcl_SetVar(interp, "argv", argv[i], |
| 1227 | TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
| 1228 | } |
| 1229 | if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1230 | const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1231 | if( zInfo==0 ) zInfo = interp->result; |
| 1232 | fprintf(stderr,"%s: %s\n", *argv, zInfo); |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1233 | return 1; |
| 1234 | } |
| 1235 | }else{ |
| 1236 | Tcl_GlobalEval(interp, zMainloop); |
| 1237 | } |
| 1238 | return 0; |
| 1239 | } |
| 1240 | #endif /* TCLSH */ |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 1241 | |
| 1242 | #endif /* !defined(NO_TCL) */ |