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