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