|
1 /* |
|
2 ** 2001 September 15 |
|
3 ** |
|
4 ** Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiaries. All rights reserved. |
|
5 ** |
|
6 ** The author disclaims copyright to this source code. In place of |
|
7 ** a legal notice, here is a blessing: |
|
8 ** |
|
9 ** May you do good and not evil. |
|
10 ** May you find forgiveness for yourself and forgive others. |
|
11 ** May you share freely, never taking more than you give. |
|
12 ** |
|
13 ************************************************************************* |
|
14 ** A TCL Interface to SQLite. Append this file to sqlite3.c and |
|
15 ** compile the whole thing to build a TCL-enabled version of SQLite. |
|
16 ** |
|
17 ** $Id: tclsqlite.c,v 1.226 2008/09/23 10:12:15 drh Exp $ |
|
18 */ |
|
19 #include "tcl.h" |
|
20 #include <errno.h> |
|
21 |
|
22 /* |
|
23 ** Some additional include files are needed if this file is not |
|
24 ** appended to the amalgamation. |
|
25 */ |
|
26 #ifndef SQLITE_AMALGAMATION |
|
27 # include "sqliteInt.h" |
|
28 # include <stdlib.h> |
|
29 # include <string.h> |
|
30 # include <assert.h> |
|
31 # include <ctype.h> |
|
32 #endif |
|
33 |
|
34 #ifdef __SYMBIAN32__ |
|
35 int CopyTestFiles(void); |
|
36 int DeleteTestFiles(void); |
|
37 int PrintText(void*, Tcl_Interp*, int objc, Tcl_Obj* const* objv); |
|
38 void PrintS(const char* aText); |
|
39 #endif |
|
40 |
|
41 /* |
|
42 * Windows needs to know which symbols to export. Unix does not. |
|
43 * BUILD_sqlite should be undefined for Unix. |
|
44 */ |
|
45 #ifdef BUILD_sqlite |
|
46 #undef TCL_STORAGE_CLASS |
|
47 #define TCL_STORAGE_CLASS DLLEXPORT |
|
48 #endif /* BUILD_sqlite */ |
|
49 |
|
50 #define NUM_PREPARED_STMTS 10 |
|
51 #define MAX_PREPARED_STMTS 100 |
|
52 |
|
53 /* |
|
54 ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we |
|
55 ** have to do a translation when going between the two. Set the |
|
56 ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do |
|
57 ** this translation. |
|
58 */ |
|
59 #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) |
|
60 # define UTF_TRANSLATION_NEEDED 1 |
|
61 #endif |
|
62 |
|
63 /* |
|
64 ** New SQL functions can be created as TCL scripts. Each such function |
|
65 ** is described by an instance of the following structure. |
|
66 */ |
|
67 typedef struct SqlFunc SqlFunc; |
|
68 struct SqlFunc { |
|
69 Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
|
70 Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ |
|
71 int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ |
|
72 char *zName; /* Name of this function */ |
|
73 SqlFunc *pNext; /* Next function on the list of them all */ |
|
74 }; |
|
75 |
|
76 /* |
|
77 ** New collation sequences function can be created as TCL scripts. Each such |
|
78 ** function is described by an instance of the following structure. |
|
79 */ |
|
80 typedef struct SqlCollate SqlCollate; |
|
81 struct SqlCollate { |
|
82 Tcl_Interp *interp; /* The TCL interpret to execute the function */ |
|
83 char *zScript; /* The script to be run */ |
|
84 SqlCollate *pNext; /* Next function on the list of them all */ |
|
85 }; |
|
86 |
|
87 /* |
|
88 ** Prepared statements are cached for faster execution. Each prepared |
|
89 ** statement is described by an instance of the following structure. |
|
90 */ |
|
91 typedef struct SqlPreparedStmt SqlPreparedStmt; |
|
92 struct SqlPreparedStmt { |
|
93 SqlPreparedStmt *pNext; /* Next in linked list */ |
|
94 SqlPreparedStmt *pPrev; /* Previous on the list */ |
|
95 sqlite3_stmt *pStmt; /* The prepared statement */ |
|
96 int nSql; /* chars in zSql[] */ |
|
97 const char *zSql; /* Text of the SQL statement */ |
|
98 }; |
|
99 |
|
100 typedef struct IncrblobChannel IncrblobChannel; |
|
101 |
|
102 /* |
|
103 ** There is one instance of this structure for each SQLite database |
|
104 ** that has been opened by the SQLite TCL interface. |
|
105 */ |
|
106 typedef struct SqliteDb SqliteDb; |
|
107 struct SqliteDb { |
|
108 sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ |
|
109 Tcl_Interp *interp; /* The interpreter used for this database */ |
|
110 char *zBusy; /* The busy callback routine */ |
|
111 char *zCommit; /* The commit hook callback routine */ |
|
112 char *zTrace; /* The trace callback routine */ |
|
113 char *zProfile; /* The profile callback routine */ |
|
114 char *zProgress; /* The progress callback routine */ |
|
115 char *zAuth; /* The authorization callback routine */ |
|
116 int disableAuth; /* Disable the authorizer if it exists */ |
|
117 char *zNull; /* Text to substitute for an SQL NULL value */ |
|
118 SqlFunc *pFunc; /* List of SQL functions */ |
|
119 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ |
|
120 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ |
|
121 SqlCollate *pCollate; /* List of SQL collation functions */ |
|
122 int rc; /* Return code of most recent sqlite3_exec() */ |
|
123 Tcl_Obj *pCollateNeeded; /* Collation needed script */ |
|
124 SqlPreparedStmt *stmtList; /* List of prepared statements*/ |
|
125 SqlPreparedStmt *stmtLast; /* Last statement in the list */ |
|
126 int maxStmt; /* The next maximum number of stmtList */ |
|
127 int nStmt; /* Number of statements in stmtList */ |
|
128 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ |
|
129 }; |
|
130 |
|
131 struct IncrblobChannel { |
|
132 sqlite3_blob *pBlob; /* sqlite3 blob handle */ |
|
133 SqliteDb *pDb; /* Associated database connection */ |
|
134 int iSeek; /* Current seek offset */ |
|
135 Tcl_Channel channel; /* Channel identifier */ |
|
136 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ |
|
137 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ |
|
138 }; |
|
139 |
|
140 #ifndef SQLITE_OMIT_INCRBLOB |
|
141 /* |
|
142 ** Close all incrblob channels opened using database connection pDb. |
|
143 ** This is called when shutting down the database connection. |
|
144 */ |
|
145 static void closeIncrblobChannels(SqliteDb *pDb){ |
|
146 IncrblobChannel *p; |
|
147 IncrblobChannel *pNext; |
|
148 |
|
149 for(p=pDb->pIncrblob; p; p=pNext){ |
|
150 pNext = p->pNext; |
|
151 |
|
152 /* Note: Calling unregister here call Tcl_Close on the incrblob channel, |
|
153 ** which deletes the IncrblobChannel structure at *p. So do not |
|
154 ** call Tcl_Free() here. |
|
155 */ |
|
156 Tcl_UnregisterChannel(pDb->interp, p->channel); |
|
157 } |
|
158 } |
|
159 |
|
160 /* |
|
161 ** Close an incremental blob channel. |
|
162 */ |
|
163 static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ |
|
164 IncrblobChannel *p = (IncrblobChannel *)instanceData; |
|
165 int rc = sqlite3_blob_close(p->pBlob); |
|
166 sqlite3 *db = p->pDb->db; |
|
167 |
|
168 /* Remove the channel from the SqliteDb.pIncrblob list. */ |
|
169 if( p->pNext ){ |
|
170 p->pNext->pPrev = p->pPrev; |
|
171 } |
|
172 if( p->pPrev ){ |
|
173 p->pPrev->pNext = p->pNext; |
|
174 } |
|
175 if( p->pDb->pIncrblob==p ){ |
|
176 p->pDb->pIncrblob = p->pNext; |
|
177 } |
|
178 |
|
179 /* Free the IncrblobChannel structure */ |
|
180 Tcl_Free((char *)p); |
|
181 |
|
182 if( rc!=SQLITE_OK ){ |
|
183 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); |
|
184 return TCL_ERROR; |
|
185 } |
|
186 return TCL_OK; |
|
187 } |
|
188 |
|
189 /* |
|
190 ** Read data from an incremental blob channel. |
|
191 */ |
|
192 static int incrblobInput( |
|
193 ClientData instanceData, |
|
194 char *buf, |
|
195 int bufSize, |
|
196 int *errorCodePtr |
|
197 ){ |
|
198 IncrblobChannel *p = (IncrblobChannel *)instanceData; |
|
199 int nRead = bufSize; /* Number of bytes to read */ |
|
200 int nBlob; /* Total size of the blob */ |
|
201 int rc; /* sqlite error code */ |
|
202 |
|
203 nBlob = sqlite3_blob_bytes(p->pBlob); |
|
204 if( (p->iSeek+nRead)>nBlob ){ |
|
205 nRead = nBlob-p->iSeek; |
|
206 } |
|
207 if( nRead<=0 ){ |
|
208 return 0; |
|
209 } |
|
210 |
|
211 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); |
|
212 if( rc!=SQLITE_OK ){ |
|
213 *errorCodePtr = rc; |
|
214 return -1; |
|
215 } |
|
216 |
|
217 p->iSeek += nRead; |
|
218 return nRead; |
|
219 } |
|
220 |
|
221 /* |
|
222 ** Write data to an incremental blob channel. |
|
223 */ |
|
224 static int incrblobOutput( |
|
225 ClientData instanceData, |
|
226 CONST char *buf, |
|
227 int toWrite, |
|
228 int *errorCodePtr |
|
229 ){ |
|
230 IncrblobChannel *p = (IncrblobChannel *)instanceData; |
|
231 int nWrite = toWrite; /* Number of bytes to write */ |
|
232 int nBlob; /* Total size of the blob */ |
|
233 int rc; /* sqlite error code */ |
|
234 |
|
235 nBlob = sqlite3_blob_bytes(p->pBlob); |
|
236 if( (p->iSeek+nWrite)>nBlob ){ |
|
237 *errorCodePtr = EINVAL; |
|
238 return -1; |
|
239 } |
|
240 if( nWrite<=0 ){ |
|
241 return 0; |
|
242 } |
|
243 |
|
244 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); |
|
245 if( rc!=SQLITE_OK ){ |
|
246 *errorCodePtr = EIO; |
|
247 return -1; |
|
248 } |
|
249 |
|
250 p->iSeek += nWrite; |
|
251 return nWrite; |
|
252 } |
|
253 |
|
254 /* |
|
255 ** Seek an incremental blob channel. |
|
256 */ |
|
257 static int incrblobSeek( |
|
258 ClientData instanceData, |
|
259 long offset, |
|
260 int seekMode, |
|
261 int *errorCodePtr |
|
262 ){ |
|
263 IncrblobChannel *p = (IncrblobChannel *)instanceData; |
|
264 |
|
265 switch( seekMode ){ |
|
266 case SEEK_SET: |
|
267 p->iSeek = offset; |
|
268 break; |
|
269 case SEEK_CUR: |
|
270 p->iSeek += offset; |
|
271 break; |
|
272 case SEEK_END: |
|
273 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; |
|
274 break; |
|
275 |
|
276 default: assert(!"Bad seekMode"); |
|
277 } |
|
278 |
|
279 return p->iSeek; |
|
280 } |
|
281 |
|
282 |
|
283 static void incrblobWatch(ClientData instanceData, int mode){ |
|
284 /* NO-OP */ |
|
285 } |
|
286 static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ |
|
287 return TCL_ERROR; |
|
288 } |
|
289 |
|
290 static Tcl_ChannelType IncrblobChannelType = { |
|
291 "incrblob", /* typeName */ |
|
292 TCL_CHANNEL_VERSION_2, /* version */ |
|
293 incrblobClose, /* closeProc */ |
|
294 incrblobInput, /* inputProc */ |
|
295 incrblobOutput, /* outputProc */ |
|
296 incrblobSeek, /* seekProc */ |
|
297 0, /* setOptionProc */ |
|
298 0, /* getOptionProc */ |
|
299 incrblobWatch, /* watchProc (this is a no-op) */ |
|
300 incrblobHandle, /* getHandleProc (always returns error) */ |
|
301 0, /* close2Proc */ |
|
302 0, /* blockModeProc */ |
|
303 0, /* flushProc */ |
|
304 0, /* handlerProc */ |
|
305 0, /* wideSeekProc */ |
|
306 }; |
|
307 |
|
308 /* |
|
309 ** Create a new incrblob channel. |
|
310 */ |
|
311 static int createIncrblobChannel( |
|
312 Tcl_Interp *interp, |
|
313 SqliteDb *pDb, |
|
314 const char *zDb, |
|
315 const char *zTable, |
|
316 const char *zColumn, |
|
317 sqlite_int64 iRow, |
|
318 int isReadonly |
|
319 ){ |
|
320 IncrblobChannel *p; |
|
321 sqlite3 *db = pDb->db; |
|
322 sqlite3_blob *pBlob; |
|
323 int rc; |
|
324 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); |
|
325 |
|
326 /* This variable is used to name the channels: "incrblob_[incr count]" */ |
|
327 static int count = 0; |
|
328 char zChannel[64]; |
|
329 |
|
330 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); |
|
331 if( rc!=SQLITE_OK ){ |
|
332 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
|
333 return TCL_ERROR; |
|
334 } |
|
335 |
|
336 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); |
|
337 p->iSeek = 0; |
|
338 p->pBlob = pBlob; |
|
339 |
|
340 sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); |
|
341 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); |
|
342 Tcl_RegisterChannel(interp, p->channel); |
|
343 |
|
344 /* Link the new channel into the SqliteDb.pIncrblob list. */ |
|
345 p->pNext = pDb->pIncrblob; |
|
346 p->pPrev = 0; |
|
347 if( p->pNext ){ |
|
348 p->pNext->pPrev = p; |
|
349 } |
|
350 pDb->pIncrblob = p; |
|
351 p->pDb = pDb; |
|
352 |
|
353 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); |
|
354 return TCL_OK; |
|
355 } |
|
356 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ |
|
357 #define closeIncrblobChannels(pDb) |
|
358 #endif |
|
359 |
|
360 /* |
|
361 ** Look at the script prefix in pCmd. We will be executing this script |
|
362 ** after first appending one or more arguments. This routine analyzes |
|
363 ** the script to see if it is safe to use Tcl_EvalObjv() on the script |
|
364 ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much |
|
365 ** faster. |
|
366 ** |
|
367 ** Scripts that are safe to use with Tcl_EvalObjv() consists of a |
|
368 ** command name followed by zero or more arguments with no [...] or $ |
|
369 ** or {...} or ; to be seen anywhere. Most callback scripts consist |
|
370 ** of just a single procedure name and they meet this requirement. |
|
371 */ |
|
372 static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ |
|
373 /* We could try to do something with Tcl_Parse(). But we will instead |
|
374 ** just do a search for forbidden characters. If any of the forbidden |
|
375 ** characters appear in pCmd, we will report the string as unsafe. |
|
376 */ |
|
377 const char *z; |
|
378 int n; |
|
379 z = Tcl_GetStringFromObj(pCmd, &n); |
|
380 while( n-- > 0 ){ |
|
381 int c = *(z++); |
|
382 if( c=='$' || c=='[' || c==';' ) return 0; |
|
383 } |
|
384 return 1; |
|
385 } |
|
386 |
|
387 /* |
|
388 ** Find an SqlFunc structure with the given name. Or create a new |
|
389 ** one if an existing one cannot be found. Return a pointer to the |
|
390 ** structure. |
|
391 */ |
|
392 static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ |
|
393 SqlFunc *p, *pNew; |
|
394 int i; |
|
395 pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen(zName) + 1 ); |
|
396 pNew->zName = (char*)&pNew[1]; |
|
397 for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); } |
|
398 pNew->zName[i] = 0; |
|
399 for(p=pDb->pFunc; p; p=p->pNext){ |
|
400 if( strcmp(p->zName, pNew->zName)==0 ){ |
|
401 Tcl_Free((char*)pNew); |
|
402 return p; |
|
403 } |
|
404 } |
|
405 pNew->interp = pDb->interp; |
|
406 pNew->pScript = 0; |
|
407 pNew->pNext = pDb->pFunc; |
|
408 pDb->pFunc = pNew; |
|
409 return pNew; |
|
410 } |
|
411 |
|
412 /* |
|
413 ** Finalize and free a list of prepared statements |
|
414 */ |
|
415 static void flushStmtCache( SqliteDb *pDb ){ |
|
416 SqlPreparedStmt *pPreStmt; |
|
417 |
|
418 while( pDb->stmtList ){ |
|
419 sqlite3_finalize( pDb->stmtList->pStmt ); |
|
420 pPreStmt = pDb->stmtList; |
|
421 pDb->stmtList = pDb->stmtList->pNext; |
|
422 Tcl_Free( (char*)pPreStmt ); |
|
423 } |
|
424 pDb->nStmt = 0; |
|
425 pDb->stmtLast = 0; |
|
426 } |
|
427 |
|
428 /* |
|
429 ** TCL calls this procedure when an sqlite3 database command is |
|
430 ** deleted. |
|
431 */ |
|
432 static void DbDeleteCmd(void *db){ |
|
433 SqliteDb *pDb = (SqliteDb*)db; |
|
434 flushStmtCache(pDb); |
|
435 closeIncrblobChannels(pDb); |
|
436 sqlite3_close(pDb->db); |
|
437 while( pDb->pFunc ){ |
|
438 SqlFunc *pFunc = pDb->pFunc; |
|
439 pDb->pFunc = pFunc->pNext; |
|
440 Tcl_DecrRefCount(pFunc->pScript); |
|
441 Tcl_Free((char*)pFunc); |
|
442 } |
|
443 while( pDb->pCollate ){ |
|
444 SqlCollate *pCollate = pDb->pCollate; |
|
445 pDb->pCollate = pCollate->pNext; |
|
446 Tcl_Free((char*)pCollate); |
|
447 } |
|
448 if( pDb->zBusy ){ |
|
449 Tcl_Free(pDb->zBusy); |
|
450 } |
|
451 if( pDb->zTrace ){ |
|
452 Tcl_Free(pDb->zTrace); |
|
453 } |
|
454 if( pDb->zProfile ){ |
|
455 Tcl_Free(pDb->zProfile); |
|
456 } |
|
457 if( pDb->zAuth ){ |
|
458 Tcl_Free(pDb->zAuth); |
|
459 } |
|
460 if( pDb->zNull ){ |
|
461 Tcl_Free(pDb->zNull); |
|
462 } |
|
463 if( pDb->pUpdateHook ){ |
|
464 Tcl_DecrRefCount(pDb->pUpdateHook); |
|
465 } |
|
466 if( pDb->pRollbackHook ){ |
|
467 Tcl_DecrRefCount(pDb->pRollbackHook); |
|
468 } |
|
469 if( pDb->pCollateNeeded ){ |
|
470 Tcl_DecrRefCount(pDb->pCollateNeeded); |
|
471 } |
|
472 Tcl_Free((char*)pDb); |
|
473 } |
|
474 |
|
475 /* |
|
476 ** This routine is called when a database file is locked while trying |
|
477 ** to execute SQL. |
|
478 */ |
|
479 static int DbBusyHandler(void *cd, int nTries){ |
|
480 SqliteDb *pDb = (SqliteDb*)cd; |
|
481 int rc; |
|
482 char zVal[30]; |
|
483 |
|
484 sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); |
|
485 rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); |
|
486 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
|
487 return 0; |
|
488 } |
|
489 return 1; |
|
490 } |
|
491 |
|
492 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
|
493 /* |
|
494 ** This routine is invoked as the 'progress callback' for the database. |
|
495 */ |
|
496 static int DbProgressHandler(void *cd){ |
|
497 SqliteDb *pDb = (SqliteDb*)cd; |
|
498 int rc; |
|
499 |
|
500 assert( pDb->zProgress ); |
|
501 rc = Tcl_Eval(pDb->interp, pDb->zProgress); |
|
502 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
|
503 return 1; |
|
504 } |
|
505 return 0; |
|
506 } |
|
507 #endif |
|
508 |
|
509 #ifndef SQLITE_OMIT_TRACE |
|
510 /* |
|
511 ** This routine is called by the SQLite trace handler whenever a new |
|
512 ** block of SQL is executed. The TCL script in pDb->zTrace is executed. |
|
513 */ |
|
514 static void DbTraceHandler(void *cd, const char *zSql){ |
|
515 SqliteDb *pDb = (SqliteDb*)cd; |
|
516 Tcl_DString str; |
|
517 |
|
518 Tcl_DStringInit(&str); |
|
519 Tcl_DStringAppend(&str, pDb->zTrace, -1); |
|
520 Tcl_DStringAppendElement(&str, zSql); |
|
521 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
|
522 Tcl_DStringFree(&str); |
|
523 Tcl_ResetResult(pDb->interp); |
|
524 } |
|
525 #endif |
|
526 |
|
527 #ifndef SQLITE_OMIT_TRACE |
|
528 /* |
|
529 ** This routine is called by the SQLite profile handler after a statement |
|
530 ** SQL has executed. The TCL script in pDb->zProfile is evaluated. |
|
531 */ |
|
532 static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ |
|
533 SqliteDb *pDb = (SqliteDb*)cd; |
|
534 Tcl_DString str; |
|
535 char zTm[100]; |
|
536 |
|
537 sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); |
|
538 Tcl_DStringInit(&str); |
|
539 Tcl_DStringAppend(&str, pDb->zProfile, -1); |
|
540 Tcl_DStringAppendElement(&str, zSql); |
|
541 Tcl_DStringAppendElement(&str, zTm); |
|
542 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); |
|
543 Tcl_DStringFree(&str); |
|
544 Tcl_ResetResult(pDb->interp); |
|
545 } |
|
546 #endif |
|
547 |
|
548 /* |
|
549 ** This routine is called when a transaction is committed. The |
|
550 ** TCL script in pDb->zCommit is executed. If it returns non-zero or |
|
551 ** if it throws an exception, the transaction is rolled back instead |
|
552 ** of being committed. |
|
553 */ |
|
554 static int DbCommitHandler(void *cd){ |
|
555 SqliteDb *pDb = (SqliteDb*)cd; |
|
556 int rc; |
|
557 |
|
558 rc = Tcl_Eval(pDb->interp, pDb->zCommit); |
|
559 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ |
|
560 return 1; |
|
561 } |
|
562 return 0; |
|
563 } |
|
564 |
|
565 static void DbRollbackHandler(void *clientData){ |
|
566 SqliteDb *pDb = (SqliteDb*)clientData; |
|
567 assert(pDb->pRollbackHook); |
|
568 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ |
|
569 Tcl_BackgroundError(pDb->interp); |
|
570 } |
|
571 } |
|
572 |
|
573 static void DbUpdateHandler( |
|
574 void *p, |
|
575 int op, |
|
576 const char *zDb, |
|
577 const char *zTbl, |
|
578 sqlite_int64 rowid |
|
579 ){ |
|
580 SqliteDb *pDb = (SqliteDb *)p; |
|
581 Tcl_Obj *pCmd; |
|
582 |
|
583 assert( pDb->pUpdateHook ); |
|
584 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); |
|
585 |
|
586 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); |
|
587 Tcl_IncrRefCount(pCmd); |
|
588 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( |
|
589 ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); |
|
590 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); |
|
591 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); |
|
592 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); |
|
593 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); |
|
594 } |
|
595 |
|
596 static void tclCollateNeeded( |
|
597 void *pCtx, |
|
598 sqlite3 *db, |
|
599 int enc, |
|
600 const char *zName |
|
601 ){ |
|
602 SqliteDb *pDb = (SqliteDb *)pCtx; |
|
603 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); |
|
604 Tcl_IncrRefCount(pScript); |
|
605 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); |
|
606 Tcl_EvalObjEx(pDb->interp, pScript, 0); |
|
607 Tcl_DecrRefCount(pScript); |
|
608 } |
|
609 |
|
610 /* |
|
611 ** This routine is called to evaluate an SQL collation function implemented |
|
612 ** using TCL script. |
|
613 */ |
|
614 static int tclSqlCollate( |
|
615 void *pCtx, |
|
616 int nA, |
|
617 const void *zA, |
|
618 int nB, |
|
619 const void *zB |
|
620 ){ |
|
621 SqlCollate *p = (SqlCollate *)pCtx; |
|
622 Tcl_Obj *pCmd; |
|
623 |
|
624 pCmd = Tcl_NewStringObj(p->zScript, -1); |
|
625 Tcl_IncrRefCount(pCmd); |
|
626 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); |
|
627 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); |
|
628 Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
|
629 Tcl_DecrRefCount(pCmd); |
|
630 return (atoi(Tcl_GetStringResult(p->interp))); |
|
631 } |
|
632 |
|
633 /* |
|
634 ** This routine is called to evaluate an SQL function implemented |
|
635 ** using TCL script. |
|
636 */ |
|
637 static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ |
|
638 SqlFunc *p = sqlite3_user_data(context); |
|
639 Tcl_Obj *pCmd; |
|
640 int i; |
|
641 int rc; |
|
642 |
|
643 if( argc==0 ){ |
|
644 /* If there are no arguments to the function, call Tcl_EvalObjEx on the |
|
645 ** script object directly. This allows the TCL compiler to generate |
|
646 ** bytecode for the command on the first invocation and thus make |
|
647 ** subsequent invocations much faster. */ |
|
648 pCmd = p->pScript; |
|
649 Tcl_IncrRefCount(pCmd); |
|
650 rc = Tcl_EvalObjEx(p->interp, pCmd, 0); |
|
651 Tcl_DecrRefCount(pCmd); |
|
652 }else{ |
|
653 /* If there are arguments to the function, make a shallow copy of the |
|
654 ** script object, lappend the arguments, then evaluate the copy. |
|
655 ** |
|
656 ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated. |
|
657 ** The new Tcl_Obj contains pointers to the original list elements. |
|
658 ** That way, when Tcl_EvalObjv() is run and shimmers the first element |
|
659 ** of the list to tclCmdNameType, that alternate representation will |
|
660 ** be preserved and reused on the next invocation. |
|
661 */ |
|
662 Tcl_Obj **aArg; |
|
663 int nArg; |
|
664 if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ |
|
665 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
|
666 return; |
|
667 } |
|
668 pCmd = Tcl_NewListObj(nArg, aArg); |
|
669 Tcl_IncrRefCount(pCmd); |
|
670 for(i=0; i<argc; i++){ |
|
671 sqlite3_value *pIn = argv[i]; |
|
672 Tcl_Obj *pVal; |
|
673 |
|
674 /* Set pVal to contain the i'th column of this row. */ |
|
675 switch( sqlite3_value_type(pIn) ){ |
|
676 case SQLITE_BLOB: { |
|
677 int bytes = sqlite3_value_bytes(pIn); |
|
678 pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); |
|
679 break; |
|
680 } |
|
681 case SQLITE_INTEGER: { |
|
682 sqlite_int64 v = sqlite3_value_int64(pIn); |
|
683 if( v>=-2147483647 && v<=2147483647 ){ |
|
684 pVal = Tcl_NewIntObj(v); |
|
685 }else{ |
|
686 pVal = Tcl_NewWideIntObj(v); |
|
687 } |
|
688 break; |
|
689 } |
|
690 case SQLITE_FLOAT: { |
|
691 double r = sqlite3_value_double(pIn); |
|
692 pVal = Tcl_NewDoubleObj(r); |
|
693 break; |
|
694 } |
|
695 case SQLITE_NULL: { |
|
696 pVal = Tcl_NewStringObj("", 0); |
|
697 break; |
|
698 } |
|
699 default: { |
|
700 int bytes = sqlite3_value_bytes(pIn); |
|
701 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); |
|
702 break; |
|
703 } |
|
704 } |
|
705 rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); |
|
706 if( rc ){ |
|
707 Tcl_DecrRefCount(pCmd); |
|
708 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
|
709 return; |
|
710 } |
|
711 } |
|
712 if( !p->useEvalObjv ){ |
|
713 /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd |
|
714 ** is a list without a string representation. To prevent this from |
|
715 ** happening, make sure pCmd has a valid string representation */ |
|
716 Tcl_GetString(pCmd); |
|
717 } |
|
718 rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); |
|
719 Tcl_DecrRefCount(pCmd); |
|
720 } |
|
721 |
|
722 if( rc && rc!=TCL_RETURN ){ |
|
723 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); |
|
724 }else{ |
|
725 Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); |
|
726 int n; |
|
727 u8 *data; |
|
728 char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
|
729 char c = zType[0]; |
|
730 if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ |
|
731 /* Only return a BLOB type if the Tcl variable is a bytearray and |
|
732 ** has no string representation. */ |
|
733 data = Tcl_GetByteArrayFromObj(pVar, &n); |
|
734 sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); |
|
735 }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
|
736 Tcl_GetIntFromObj(0, pVar, &n); |
|
737 sqlite3_result_int(context, n); |
|
738 }else if( c=='d' && strcmp(zType,"double")==0 ){ |
|
739 double r; |
|
740 Tcl_GetDoubleFromObj(0, pVar, &r); |
|
741 sqlite3_result_double(context, r); |
|
742 }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
|
743 (c=='i' && strcmp(zType,"int")==0) ){ |
|
744 Tcl_WideInt v; |
|
745 Tcl_GetWideIntFromObj(0, pVar, &v); |
|
746 sqlite3_result_int64(context, v); |
|
747 }else{ |
|
748 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
|
749 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); |
|
750 } |
|
751 } |
|
752 } |
|
753 |
|
754 #ifndef SQLITE_OMIT_AUTHORIZATION |
|
755 /* |
|
756 ** This is the authentication function. It appends the authentication |
|
757 ** type code and the two arguments to zCmd[] then invokes the result |
|
758 ** on the interpreter. The reply is examined to determine if the |
|
759 ** authentication fails or succeeds. |
|
760 */ |
|
761 static int auth_callback( |
|
762 void *pArg, |
|
763 int code, |
|
764 const char *zArg1, |
|
765 const char *zArg2, |
|
766 const char *zArg3, |
|
767 const char *zArg4 |
|
768 ){ |
|
769 char *zCode; |
|
770 Tcl_DString str; |
|
771 int rc; |
|
772 const char *zReply; |
|
773 SqliteDb *pDb = (SqliteDb*)pArg; |
|
774 if( pDb->disableAuth ) return SQLITE_OK; |
|
775 |
|
776 switch( code ){ |
|
777 case SQLITE_COPY : zCode="SQLITE_COPY"; break; |
|
778 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; |
|
779 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; |
|
780 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; |
|
781 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; |
|
782 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; |
|
783 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; |
|
784 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; |
|
785 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; |
|
786 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; |
|
787 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; |
|
788 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; |
|
789 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; |
|
790 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; |
|
791 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; |
|
792 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; |
|
793 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; |
|
794 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; |
|
795 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; |
|
796 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; |
|
797 case SQLITE_READ : zCode="SQLITE_READ"; break; |
|
798 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; |
|
799 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; |
|
800 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; |
|
801 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; |
|
802 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; |
|
803 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; |
|
804 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; |
|
805 case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; |
|
806 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; |
|
807 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; |
|
808 case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; |
|
809 default : zCode="????"; break; |
|
810 } |
|
811 Tcl_DStringInit(&str); |
|
812 Tcl_DStringAppend(&str, pDb->zAuth, -1); |
|
813 Tcl_DStringAppendElement(&str, zCode); |
|
814 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); |
|
815 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); |
|
816 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); |
|
817 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); |
|
818 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); |
|
819 Tcl_DStringFree(&str); |
|
820 zReply = Tcl_GetStringResult(pDb->interp); |
|
821 if( strcmp(zReply,"SQLITE_OK")==0 ){ |
|
822 rc = SQLITE_OK; |
|
823 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ |
|
824 rc = SQLITE_DENY; |
|
825 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ |
|
826 rc = SQLITE_IGNORE; |
|
827 }else{ |
|
828 rc = 999; |
|
829 } |
|
830 return rc; |
|
831 } |
|
832 #endif /* SQLITE_OMIT_AUTHORIZATION */ |
|
833 |
|
834 /* |
|
835 ** zText is a pointer to text obtained via an sqlite3_result_text() |
|
836 ** or similar interface. This routine returns a Tcl string object, |
|
837 ** reference count set to 0, containing the text. If a translation |
|
838 ** between iso8859 and UTF-8 is required, it is preformed. |
|
839 */ |
|
840 static Tcl_Obj *dbTextToObj(char const *zText){ |
|
841 Tcl_Obj *pVal; |
|
842 #ifdef UTF_TRANSLATION_NEEDED |
|
843 Tcl_DString dCol; |
|
844 Tcl_DStringInit(&dCol); |
|
845 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); |
|
846 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); |
|
847 Tcl_DStringFree(&dCol); |
|
848 #else |
|
849 pVal = Tcl_NewStringObj(zText, -1); |
|
850 #endif |
|
851 return pVal; |
|
852 } |
|
853 |
|
854 /* |
|
855 ** This routine reads a line of text from FILE in, stores |
|
856 ** the text in memory obtained from malloc() and returns a pointer |
|
857 ** to the text. NULL is returned at end of file, or if malloc() |
|
858 ** fails. |
|
859 ** |
|
860 ** The interface is like "readline" but no command-line editing |
|
861 ** is done. |
|
862 ** |
|
863 ** copied from shell.c from '.import' command |
|
864 */ |
|
865 static char *local_getline(char *zPrompt, FILE *in){ |
|
866 char *zLine; |
|
867 int nLine; |
|
868 int n; |
|
869 int eol; |
|
870 |
|
871 nLine = 100; |
|
872 zLine = malloc( nLine ); |
|
873 if( zLine==0 ) return 0; |
|
874 n = 0; |
|
875 eol = 0; |
|
876 while( !eol ){ |
|
877 if( n+100>nLine ){ |
|
878 nLine = nLine*2 + 100; |
|
879 zLine = realloc(zLine, nLine); |
|
880 if( zLine==0 ) return 0; |
|
881 } |
|
882 if( fgets(&zLine[n], nLine - n, in)==0 ){ |
|
883 if( n==0 ){ |
|
884 free(zLine); |
|
885 return 0; |
|
886 } |
|
887 zLine[n] = 0; |
|
888 eol = 1; |
|
889 break; |
|
890 } |
|
891 while( zLine[n] ){ n++; } |
|
892 if( n>0 && zLine[n-1]=='\n' ){ |
|
893 n--; |
|
894 zLine[n] = 0; |
|
895 eol = 1; |
|
896 } |
|
897 } |
|
898 zLine = realloc( zLine, n+1 ); |
|
899 return zLine; |
|
900 } |
|
901 |
|
902 |
|
903 /* |
|
904 ** Figure out the column names for the data returned by the statement |
|
905 ** passed as the second argument. |
|
906 ** |
|
907 ** If parameter papColName is not NULL, then *papColName is set to point |
|
908 ** at an array allocated using Tcl_Alloc(). It is the callers responsibility |
|
909 ** to free this array using Tcl_Free(), and to decrement the reference |
|
910 ** count of each Tcl_Obj* member of the array. |
|
911 ** |
|
912 ** The return value of this function is the number of columns of data |
|
913 ** returned by pStmt (and hence the size of the *papColName array). |
|
914 ** |
|
915 ** If pArray is not NULL, then it contains the name of a Tcl array |
|
916 ** variable. The "*" member of this array is set to a list containing |
|
917 ** the names of the columns returned by the statement, in order from |
|
918 ** left to right. e.g. if the names of the returned columns are a, b and |
|
919 ** c, it does the equivalent of the tcl command: |
|
920 ** |
|
921 ** set ${pArray}(*) {a b c} |
|
922 */ |
|
923 static int |
|
924 computeColumnNames( |
|
925 Tcl_Interp *interp, |
|
926 sqlite3_stmt *pStmt, /* SQL statement */ |
|
927 Tcl_Obj ***papColName, /* OUT: Array of column names */ |
|
928 Tcl_Obj *pArray /* Name of array variable (may be null) */ |
|
929 ){ |
|
930 int nCol; |
|
931 |
|
932 /* Compute column names */ |
|
933 nCol = sqlite3_column_count(pStmt); |
|
934 if( papColName ){ |
|
935 int i; |
|
936 Tcl_Obj **apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
|
937 for(i=0; i<nCol; i++){ |
|
938 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
|
939 Tcl_IncrRefCount(apColName[i]); |
|
940 } |
|
941 |
|
942 /* If results are being stored in an array variable, then create |
|
943 ** the array(*) entry for that array |
|
944 */ |
|
945 if( pArray ){ |
|
946 Tcl_Obj *pColList = Tcl_NewObj(); |
|
947 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); |
|
948 Tcl_IncrRefCount(pColList); |
|
949 for(i=0; i<nCol; i++){ |
|
950 Tcl_ListObjAppendElement(interp, pColList, apColName[i]); |
|
951 } |
|
952 Tcl_IncrRefCount(pStar); |
|
953 Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0); |
|
954 Tcl_DecrRefCount(pColList); |
|
955 Tcl_DecrRefCount(pStar); |
|
956 } |
|
957 *papColName = apColName; |
|
958 } |
|
959 |
|
960 return nCol; |
|
961 } |
|
962 |
|
963 /* |
|
964 ** The "sqlite" command below creates a new Tcl command for each |
|
965 ** connection it opens to an SQLite database. This routine is invoked |
|
966 ** whenever one of those connection-specific commands is executed |
|
967 ** in Tcl. For example, if you run Tcl code like this: |
|
968 ** |
|
969 ** sqlite3 db1 "my_database" |
|
970 ** db1 close |
|
971 ** |
|
972 ** The first command opens a connection to the "my_database" database |
|
973 ** and calls that connection "db1". The second command causes this |
|
974 ** subroutine to be invoked. |
|
975 */ |
|
976 static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
|
977 SqliteDb *pDb = (SqliteDb*)cd; |
|
978 int choice; |
|
979 int rc = TCL_OK; |
|
980 static const char *DB_strs[] = { |
|
981 "authorizer", "busy", "cache", |
|
982 "changes", "close", "collate", |
|
983 "collation_needed", "commit_hook", "complete", |
|
984 "copy", "enable_load_extension","errorcode", |
|
985 "eval", "exists", "function", |
|
986 "incrblob", "interrupt", "last_insert_rowid", |
|
987 "nullvalue", "onecolumn", "profile", |
|
988 "progress", "rekey", "rollback_hook", |
|
989 "timeout", "total_changes", "trace", |
|
990 "transaction", "update_hook", "version", |
|
991 0 |
|
992 }; |
|
993 enum DB_enum { |
|
994 DB_AUTHORIZER, DB_BUSY, DB_CACHE, |
|
995 DB_CHANGES, DB_CLOSE, DB_COLLATE, |
|
996 DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE, |
|
997 DB_COPY, DB_ENABLE_LOAD_EXTENSION,DB_ERRORCODE, |
|
998 DB_EVAL, DB_EXISTS, DB_FUNCTION, |
|
999 DB_INCRBLOB, DB_INTERRUPT, DB_LAST_INSERT_ROWID, |
|
1000 DB_NULLVALUE, DB_ONECOLUMN, DB_PROFILE, |
|
1001 DB_PROGRESS, DB_REKEY, DB_ROLLBACK_HOOK, |
|
1002 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, |
|
1003 DB_TRANSACTION, DB_UPDATE_HOOK, DB_VERSION |
|
1004 }; |
|
1005 /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ |
|
1006 |
|
1007 if( objc<2 ){ |
|
1008 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
|
1009 return TCL_ERROR; |
|
1010 } |
|
1011 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ |
|
1012 return TCL_ERROR; |
|
1013 } |
|
1014 |
|
1015 switch( (enum DB_enum)choice ){ |
|
1016 |
|
1017 /* $db authorizer ?CALLBACK? |
|
1018 ** |
|
1019 ** Invoke the given callback to authorize each SQL operation as it is |
|
1020 ** compiled. 5 arguments are appended to the callback before it is |
|
1021 ** invoked: |
|
1022 ** |
|
1023 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) |
|
1024 ** (2) First descriptive name (depends on authorization type) |
|
1025 ** (3) Second descriptive name |
|
1026 ** (4) Name of the database (ex: "main", "temp") |
|
1027 ** (5) Name of trigger that is doing the access |
|
1028 ** |
|
1029 ** The callback should return on of the following strings: SQLITE_OK, |
|
1030 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. |
|
1031 ** |
|
1032 ** If this method is invoked with no arguments, the current authorization |
|
1033 ** callback string is returned. |
|
1034 */ |
|
1035 case DB_AUTHORIZER: { |
|
1036 #ifdef SQLITE_OMIT_AUTHORIZATION |
|
1037 Tcl_AppendResult(interp, "authorization not available in this build", 0); |
|
1038 return TCL_ERROR; |
|
1039 #else |
|
1040 if( objc>3 ){ |
|
1041 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
|
1042 return TCL_ERROR; |
|
1043 }else if( objc==2 ){ |
|
1044 if( pDb->zAuth ){ |
|
1045 Tcl_AppendResult(interp, pDb->zAuth, 0); |
|
1046 } |
|
1047 }else{ |
|
1048 char *zAuth; |
|
1049 int len; |
|
1050 if( pDb->zAuth ){ |
|
1051 Tcl_Free(pDb->zAuth); |
|
1052 } |
|
1053 zAuth = Tcl_GetStringFromObj(objv[2], &len); |
|
1054 if( zAuth && len>0 ){ |
|
1055 pDb->zAuth = Tcl_Alloc( len + 1 ); |
|
1056 memcpy(pDb->zAuth, zAuth, len+1); |
|
1057 }else{ |
|
1058 pDb->zAuth = 0; |
|
1059 } |
|
1060 if( pDb->zAuth ){ |
|
1061 pDb->interp = interp; |
|
1062 sqlite3_set_authorizer(pDb->db, auth_callback, pDb); |
|
1063 }else{ |
|
1064 sqlite3_set_authorizer(pDb->db, 0, 0); |
|
1065 } |
|
1066 } |
|
1067 #endif |
|
1068 break; |
|
1069 } |
|
1070 |
|
1071 /* $db busy ?CALLBACK? |
|
1072 ** |
|
1073 ** Invoke the given callback if an SQL statement attempts to open |
|
1074 ** a locked database file. |
|
1075 */ |
|
1076 case DB_BUSY: { |
|
1077 if( objc>3 ){ |
|
1078 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); |
|
1079 return TCL_ERROR; |
|
1080 }else if( objc==2 ){ |
|
1081 if( pDb->zBusy ){ |
|
1082 Tcl_AppendResult(interp, pDb->zBusy, 0); |
|
1083 } |
|
1084 }else{ |
|
1085 char *zBusy; |
|
1086 int len; |
|
1087 if( pDb->zBusy ){ |
|
1088 Tcl_Free(pDb->zBusy); |
|
1089 } |
|
1090 zBusy = Tcl_GetStringFromObj(objv[2], &len); |
|
1091 if( zBusy && len>0 ){ |
|
1092 pDb->zBusy = Tcl_Alloc( len + 1 ); |
|
1093 memcpy(pDb->zBusy, zBusy, len+1); |
|
1094 }else{ |
|
1095 pDb->zBusy = 0; |
|
1096 } |
|
1097 if( pDb->zBusy ){ |
|
1098 pDb->interp = interp; |
|
1099 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); |
|
1100 }else{ |
|
1101 sqlite3_busy_handler(pDb->db, 0, 0); |
|
1102 } |
|
1103 } |
|
1104 break; |
|
1105 } |
|
1106 |
|
1107 /* $db cache flush |
|
1108 ** $db cache size n |
|
1109 ** |
|
1110 ** Flush the prepared statement cache, or set the maximum number of |
|
1111 ** cached statements. |
|
1112 */ |
|
1113 case DB_CACHE: { |
|
1114 char *subCmd; |
|
1115 int n; |
|
1116 |
|
1117 if( objc<=2 ){ |
|
1118 Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); |
|
1119 return TCL_ERROR; |
|
1120 } |
|
1121 subCmd = Tcl_GetStringFromObj( objv[2], 0 ); |
|
1122 if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ |
|
1123 if( objc!=3 ){ |
|
1124 Tcl_WrongNumArgs(interp, 2, objv, "flush"); |
|
1125 return TCL_ERROR; |
|
1126 }else{ |
|
1127 flushStmtCache( pDb ); |
|
1128 } |
|
1129 }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ |
|
1130 if( objc!=4 ){ |
|
1131 Tcl_WrongNumArgs(interp, 2, objv, "size n"); |
|
1132 return TCL_ERROR; |
|
1133 }else{ |
|
1134 if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ |
|
1135 Tcl_AppendResult( interp, "cannot convert \"", |
|
1136 Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0); |
|
1137 return TCL_ERROR; |
|
1138 }else{ |
|
1139 if( n<0 ){ |
|
1140 flushStmtCache( pDb ); |
|
1141 n = 0; |
|
1142 }else if( n>MAX_PREPARED_STMTS ){ |
|
1143 n = MAX_PREPARED_STMTS; |
|
1144 } |
|
1145 pDb->maxStmt = n; |
|
1146 } |
|
1147 } |
|
1148 }else{ |
|
1149 Tcl_AppendResult( interp, "bad option \"", |
|
1150 Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0); |
|
1151 return TCL_ERROR; |
|
1152 } |
|
1153 break; |
|
1154 } |
|
1155 |
|
1156 /* $db changes |
|
1157 ** |
|
1158 ** Return the number of rows that were modified, inserted, or deleted by |
|
1159 ** the most recent INSERT, UPDATE or DELETE statement, not including |
|
1160 ** any changes made by trigger programs. |
|
1161 */ |
|
1162 case DB_CHANGES: { |
|
1163 Tcl_Obj *pResult; |
|
1164 if( objc!=2 ){ |
|
1165 Tcl_WrongNumArgs(interp, 2, objv, ""); |
|
1166 return TCL_ERROR; |
|
1167 } |
|
1168 pResult = Tcl_GetObjResult(interp); |
|
1169 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); |
|
1170 break; |
|
1171 } |
|
1172 |
|
1173 /* $db close |
|
1174 ** |
|
1175 ** Shutdown the database |
|
1176 */ |
|
1177 case DB_CLOSE: { |
|
1178 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); |
|
1179 break; |
|
1180 } |
|
1181 |
|
1182 /* |
|
1183 ** $db collate NAME SCRIPT |
|
1184 ** |
|
1185 ** Create a new SQL collation function called NAME. Whenever |
|
1186 ** that function is called, invoke SCRIPT to evaluate the function. |
|
1187 */ |
|
1188 case DB_COLLATE: { |
|
1189 SqlCollate *pCollate; |
|
1190 char *zName; |
|
1191 char *zScript; |
|
1192 int nScript; |
|
1193 if( objc!=4 ){ |
|
1194 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); |
|
1195 return TCL_ERROR; |
|
1196 } |
|
1197 zName = Tcl_GetStringFromObj(objv[2], 0); |
|
1198 zScript = Tcl_GetStringFromObj(objv[3], &nScript); |
|
1199 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); |
|
1200 if( pCollate==0 ) return TCL_ERROR; |
|
1201 pCollate->interp = interp; |
|
1202 pCollate->pNext = pDb->pCollate; |
|
1203 pCollate->zScript = (char*)&pCollate[1]; |
|
1204 pDb->pCollate = pCollate; |
|
1205 memcpy(pCollate->zScript, zScript, nScript+1); |
|
1206 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, |
|
1207 pCollate, tclSqlCollate) ){ |
|
1208 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
|
1209 return TCL_ERROR; |
|
1210 } |
|
1211 break; |
|
1212 } |
|
1213 |
|
1214 /* |
|
1215 ** $db collation_needed SCRIPT |
|
1216 ** |
|
1217 ** Create a new SQL collation function called NAME. Whenever |
|
1218 ** that function is called, invoke SCRIPT to evaluate the function. |
|
1219 */ |
|
1220 case DB_COLLATION_NEEDED: { |
|
1221 if( objc!=3 ){ |
|
1222 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); |
|
1223 return TCL_ERROR; |
|
1224 } |
|
1225 if( pDb->pCollateNeeded ){ |
|
1226 Tcl_DecrRefCount(pDb->pCollateNeeded); |
|
1227 } |
|
1228 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); |
|
1229 Tcl_IncrRefCount(pDb->pCollateNeeded); |
|
1230 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); |
|
1231 break; |
|
1232 } |
|
1233 |
|
1234 /* $db commit_hook ?CALLBACK? |
|
1235 ** |
|
1236 ** Invoke the given callback just before committing every SQL transaction. |
|
1237 ** If the callback throws an exception or returns non-zero, then the |
|
1238 ** transaction is aborted. If CALLBACK is an empty string, the callback |
|
1239 ** is disabled. |
|
1240 */ |
|
1241 case DB_COMMIT_HOOK: { |
|
1242 if( objc>3 ){ |
|
1243 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
|
1244 return TCL_ERROR; |
|
1245 }else if( objc==2 ){ |
|
1246 if( pDb->zCommit ){ |
|
1247 Tcl_AppendResult(interp, pDb->zCommit, 0); |
|
1248 } |
|
1249 }else{ |
|
1250 char *zCommit; |
|
1251 int len; |
|
1252 if( pDb->zCommit ){ |
|
1253 Tcl_Free(pDb->zCommit); |
|
1254 } |
|
1255 zCommit = Tcl_GetStringFromObj(objv[2], &len); |
|
1256 if( zCommit && len>0 ){ |
|
1257 pDb->zCommit = Tcl_Alloc( len + 1 ); |
|
1258 memcpy(pDb->zCommit, zCommit, len+1); |
|
1259 }else{ |
|
1260 pDb->zCommit = 0; |
|
1261 } |
|
1262 if( pDb->zCommit ){ |
|
1263 pDb->interp = interp; |
|
1264 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); |
|
1265 }else{ |
|
1266 sqlite3_commit_hook(pDb->db, 0, 0); |
|
1267 } |
|
1268 } |
|
1269 break; |
|
1270 } |
|
1271 |
|
1272 /* $db complete SQL |
|
1273 ** |
|
1274 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if |
|
1275 ** additional lines of input are needed. This is similar to the |
|
1276 ** built-in "info complete" command of Tcl. |
|
1277 */ |
|
1278 case DB_COMPLETE: { |
|
1279 #ifndef SQLITE_OMIT_COMPLETE |
|
1280 Tcl_Obj *pResult; |
|
1281 int isComplete; |
|
1282 if( objc!=3 ){ |
|
1283 Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
|
1284 return TCL_ERROR; |
|
1285 } |
|
1286 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); |
|
1287 pResult = Tcl_GetObjResult(interp); |
|
1288 Tcl_SetBooleanObj(pResult, isComplete); |
|
1289 #endif |
|
1290 break; |
|
1291 } |
|
1292 |
|
1293 /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? |
|
1294 ** |
|
1295 ** Copy data into table from filename, optionally using SEPARATOR |
|
1296 ** as column separators. If a column contains a null string, or the |
|
1297 ** value of NULLINDICATOR, a NULL is inserted for the column. |
|
1298 ** conflict-algorithm is one of the sqlite conflict algorithms: |
|
1299 ** rollback, abort, fail, ignore, replace |
|
1300 ** On success, return the number of lines processed, not necessarily same |
|
1301 ** as 'db changes' due to conflict-algorithm selected. |
|
1302 ** |
|
1303 ** This code is basically an implementation/enhancement of |
|
1304 ** the sqlite3 shell.c ".import" command. |
|
1305 ** |
|
1306 ** This command usage is equivalent to the sqlite2.x COPY statement, |
|
1307 ** which imports file data into a table using the PostgreSQL COPY file format: |
|
1308 ** $db copy $conflit_algo $table_name $filename \t \\N |
|
1309 */ |
|
1310 case DB_COPY: { |
|
1311 char *zTable; /* Insert data into this table */ |
|
1312 char *zFile; /* The file from which to extract data */ |
|
1313 char *zConflict; /* The conflict algorithm to use */ |
|
1314 sqlite3_stmt *pStmt; /* A statement */ |
|
1315 int nCol; /* Number of columns in the table */ |
|
1316 int nByte; /* Number of bytes in an SQL string */ |
|
1317 int i, j; /* Loop counters */ |
|
1318 int nSep; /* Number of bytes in zSep[] */ |
|
1319 int nNull; /* Number of bytes in zNull[] */ |
|
1320 char *zSql; /* An SQL statement */ |
|
1321 char *zLine; /* A single line of input from the file */ |
|
1322 char **azCol; /* zLine[] broken up into columns */ |
|
1323 char *zCommit; /* How to commit changes */ |
|
1324 FILE *in; /* The input file */ |
|
1325 int lineno = 0; /* Line number of input file */ |
|
1326 char zLineNum[80]; /* Line number print buffer */ |
|
1327 Tcl_Obj *pResult; /* interp result */ |
|
1328 |
|
1329 char *zSep; |
|
1330 char *zNull; |
|
1331 if( objc<5 || objc>7 ){ |
|
1332 Tcl_WrongNumArgs(interp, 2, objv, |
|
1333 "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); |
|
1334 return TCL_ERROR; |
|
1335 } |
|
1336 if( objc>=6 ){ |
|
1337 zSep = Tcl_GetStringFromObj(objv[5], 0); |
|
1338 }else{ |
|
1339 zSep = "\t"; |
|
1340 } |
|
1341 if( objc>=7 ){ |
|
1342 zNull = Tcl_GetStringFromObj(objv[6], 0); |
|
1343 }else{ |
|
1344 zNull = ""; |
|
1345 } |
|
1346 zConflict = Tcl_GetStringFromObj(objv[2], 0); |
|
1347 zTable = Tcl_GetStringFromObj(objv[3], 0); |
|
1348 zFile = Tcl_GetStringFromObj(objv[4], 0); |
|
1349 nSep = strlen(zSep); |
|
1350 nNull = strlen(zNull); |
|
1351 if( nSep==0 ){ |
|
1352 Tcl_AppendResult(interp,"Error: non-null separator required for copy",0); |
|
1353 return TCL_ERROR; |
|
1354 } |
|
1355 if(strcmp(zConflict, "rollback") != 0 && |
|
1356 strcmp(zConflict, "abort" ) != 0 && |
|
1357 strcmp(zConflict, "fail" ) != 0 && |
|
1358 strcmp(zConflict, "ignore" ) != 0 && |
|
1359 strcmp(zConflict, "replace" ) != 0 ) { |
|
1360 Tcl_AppendResult(interp, "Error: \"", zConflict, |
|
1361 "\", conflict-algorithm must be one of: rollback, " |
|
1362 "abort, fail, ignore, or replace", 0); |
|
1363 return TCL_ERROR; |
|
1364 } |
|
1365 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); |
|
1366 if( zSql==0 ){ |
|
1367 Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0); |
|
1368 return TCL_ERROR; |
|
1369 } |
|
1370 nByte = strlen(zSql); |
|
1371 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
|
1372 sqlite3_free(zSql); |
|
1373 if( rc ){ |
|
1374 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
|
1375 nCol = 0; |
|
1376 }else{ |
|
1377 nCol = sqlite3_column_count(pStmt); |
|
1378 } |
|
1379 sqlite3_finalize(pStmt); |
|
1380 if( nCol==0 ) { |
|
1381 return TCL_ERROR; |
|
1382 } |
|
1383 zSql = malloc( nByte + 50 + nCol*2 ); |
|
1384 if( zSql==0 ) { |
|
1385 Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
|
1386 return TCL_ERROR; |
|
1387 } |
|
1388 sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", |
|
1389 zConflict, zTable); |
|
1390 j = strlen(zSql); |
|
1391 for(i=1; i<nCol; i++){ |
|
1392 zSql[j++] = ','; |
|
1393 zSql[j++] = '?'; |
|
1394 } |
|
1395 zSql[j++] = ')'; |
|
1396 zSql[j] = 0; |
|
1397 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); |
|
1398 free(zSql); |
|
1399 if( rc ){ |
|
1400 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); |
|
1401 sqlite3_finalize(pStmt); |
|
1402 return TCL_ERROR; |
|
1403 } |
|
1404 in = fopen(zFile, "rb"); |
|
1405 if( in==0 ){ |
|
1406 Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); |
|
1407 sqlite3_finalize(pStmt); |
|
1408 return TCL_ERROR; |
|
1409 } |
|
1410 azCol = malloc( sizeof(azCol[0])*(nCol+1) ); |
|
1411 if( azCol==0 ) { |
|
1412 Tcl_AppendResult(interp, "Error: can't malloc()", 0); |
|
1413 fclose(in); |
|
1414 return TCL_ERROR; |
|
1415 } |
|
1416 (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); |
|
1417 zCommit = "COMMIT"; |
|
1418 while( (zLine = local_getline(0, in))!=0 ){ |
|
1419 char *z; |
|
1420 i = 0; |
|
1421 lineno++; |
|
1422 azCol[0] = zLine; |
|
1423 for(i=0, z=zLine; *z; z++){ |
|
1424 if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ |
|
1425 *z = 0; |
|
1426 i++; |
|
1427 if( i<nCol ){ |
|
1428 azCol[i] = &z[nSep]; |
|
1429 z += nSep-1; |
|
1430 } |
|
1431 } |
|
1432 } |
|
1433 if( i+1!=nCol ){ |
|
1434 char *zErr; |
|
1435 int nErr = strlen(zFile) + 200; |
|
1436 zErr = malloc(nErr); |
|
1437 if( zErr ){ |
|
1438 sqlite3_snprintf(nErr, zErr, |
|
1439 "Error: %s line %d: expected %d columns of data but found %d", |
|
1440 zFile, lineno, nCol, i+1); |
|
1441 Tcl_AppendResult(interp, zErr, 0); |
|
1442 free(zErr); |
|
1443 } |
|
1444 zCommit = "ROLLBACK"; |
|
1445 break; |
|
1446 } |
|
1447 for(i=0; i<nCol; i++){ |
|
1448 /* check for null data, if so, bind as null */ |
|
1449 if ((nNull>0 && strcmp(azCol[i], zNull)==0) || strlen(azCol[i])==0) { |
|
1450 sqlite3_bind_null(pStmt, i+1); |
|
1451 }else{ |
|
1452 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
|
1453 } |
|
1454 } |
|
1455 sqlite3_step(pStmt); |
|
1456 rc = sqlite3_reset(pStmt); |
|
1457 free(zLine); |
|
1458 if( rc!=SQLITE_OK ){ |
|
1459 Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0); |
|
1460 zCommit = "ROLLBACK"; |
|
1461 break; |
|
1462 } |
|
1463 } |
|
1464 free(azCol); |
|
1465 fclose(in); |
|
1466 sqlite3_finalize(pStmt); |
|
1467 (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); |
|
1468 |
|
1469 if( zCommit[0] == 'C' ){ |
|
1470 /* success, set result as number of lines processed */ |
|
1471 pResult = Tcl_GetObjResult(interp); |
|
1472 Tcl_SetIntObj(pResult, lineno); |
|
1473 rc = TCL_OK; |
|
1474 }else{ |
|
1475 /* failure, append lineno where failed */ |
|
1476 sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); |
|
1477 Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0); |
|
1478 rc = TCL_ERROR; |
|
1479 } |
|
1480 break; |
|
1481 } |
|
1482 |
|
1483 /* |
|
1484 ** $db enable_load_extension BOOLEAN |
|
1485 ** |
|
1486 ** Turn the extension loading feature on or off. It if off by |
|
1487 ** default. |
|
1488 */ |
|
1489 case DB_ENABLE_LOAD_EXTENSION: { |
|
1490 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
|
1491 int onoff; |
|
1492 if( objc!=3 ){ |
|
1493 Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); |
|
1494 return TCL_ERROR; |
|
1495 } |
|
1496 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
|
1497 return TCL_ERROR; |
|
1498 } |
|
1499 sqlite3_enable_load_extension(pDb->db, onoff); |
|
1500 break; |
|
1501 #else |
|
1502 Tcl_AppendResult(interp, "extension loading is turned off at compile-time", |
|
1503 0); |
|
1504 return TCL_ERROR; |
|
1505 #endif |
|
1506 } |
|
1507 |
|
1508 /* |
|
1509 ** $db errorcode |
|
1510 ** |
|
1511 ** Return the numeric error code that was returned by the most recent |
|
1512 ** call to sqlite3_exec(). |
|
1513 */ |
|
1514 case DB_ERRORCODE: { |
|
1515 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); |
|
1516 break; |
|
1517 } |
|
1518 |
|
1519 /* |
|
1520 ** $db eval $sql ?array? ?{ ...code... }? |
|
1521 ** $db onecolumn $sql |
|
1522 ** |
|
1523 ** The SQL statement in $sql is evaluated. For each row, the values are |
|
1524 ** placed in elements of the array named "array" and ...code... is executed. |
|
1525 ** If "array" and "code" are omitted, then no callback is every invoked. |
|
1526 ** If "array" is an empty string, then the values are placed in variables |
|
1527 ** that have the same name as the fields extracted by the query. |
|
1528 ** |
|
1529 ** The onecolumn method is the equivalent of: |
|
1530 ** lindex [$db eval $sql] 0 |
|
1531 */ |
|
1532 case DB_ONECOLUMN: |
|
1533 case DB_EVAL: |
|
1534 case DB_EXISTS: { |
|
1535 char const *zSql; /* Next SQL statement to execute */ |
|
1536 char const *zLeft; /* What is left after first stmt in zSql */ |
|
1537 sqlite3_stmt *pStmt; /* Compiled SQL statment */ |
|
1538 Tcl_Obj *pArray; /* Name of array into which results are written */ |
|
1539 Tcl_Obj *pScript; /* Script to run for each result set */ |
|
1540 Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */ |
|
1541 int nParm; /* Number of entries used in apParm[] */ |
|
1542 Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */ |
|
1543 Tcl_Obj *pRet; /* Value to be returned */ |
|
1544 SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */ |
|
1545 int rc2; |
|
1546 |
|
1547 if( choice==DB_EVAL ){ |
|
1548 if( objc<3 || objc>5 ){ |
|
1549 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
|
1550 return TCL_ERROR; |
|
1551 } |
|
1552 pRet = Tcl_NewObj(); |
|
1553 Tcl_IncrRefCount(pRet); |
|
1554 }else{ |
|
1555 if( objc!=3 ){ |
|
1556 Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
|
1557 return TCL_ERROR; |
|
1558 } |
|
1559 if( choice==DB_EXISTS ){ |
|
1560 pRet = Tcl_NewBooleanObj(0); |
|
1561 Tcl_IncrRefCount(pRet); |
|
1562 }else{ |
|
1563 pRet = 0; |
|
1564 } |
|
1565 } |
|
1566 if( objc==3 ){ |
|
1567 pArray = pScript = 0; |
|
1568 }else if( objc==4 ){ |
|
1569 pArray = 0; |
|
1570 pScript = objv[3]; |
|
1571 }else{ |
|
1572 pArray = objv[3]; |
|
1573 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0; |
|
1574 pScript = objv[4]; |
|
1575 } |
|
1576 |
|
1577 Tcl_IncrRefCount(objv[2]); |
|
1578 zSql = Tcl_GetStringFromObj(objv[2], 0); |
|
1579 while( rc==TCL_OK && zSql[0] ){ |
|
1580 int i; /* Loop counter */ |
|
1581 int nVar; /* Number of bind parameters in the pStmt */ |
|
1582 int nCol = -1; /* Number of columns in the result set */ |
|
1583 Tcl_Obj **apColName = 0; /* Array of column names */ |
|
1584 int len; /* String length of zSql */ |
|
1585 |
|
1586 /* Try to find a SQL statement that has already been compiled and |
|
1587 ** which matches the next sequence of SQL. |
|
1588 */ |
|
1589 pStmt = 0; |
|
1590 len = strlen(zSql); |
|
1591 for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ |
|
1592 int n = pPreStmt->nSql; |
|
1593 if( len>=n |
|
1594 && memcmp(pPreStmt->zSql, zSql, n)==0 |
|
1595 && (zSql[n]==0 || zSql[n-1]==';') |
|
1596 ){ |
|
1597 pStmt = pPreStmt->pStmt; |
|
1598 zLeft = &zSql[pPreStmt->nSql]; |
|
1599 |
|
1600 /* When a prepared statement is found, unlink it from the |
|
1601 ** cache list. It will later be added back to the beginning |
|
1602 ** of the cache list in order to implement LRU replacement. |
|
1603 */ |
|
1604 if( pPreStmt->pPrev ){ |
|
1605 pPreStmt->pPrev->pNext = pPreStmt->pNext; |
|
1606 }else{ |
|
1607 pDb->stmtList = pPreStmt->pNext; |
|
1608 } |
|
1609 if( pPreStmt->pNext ){ |
|
1610 pPreStmt->pNext->pPrev = pPreStmt->pPrev; |
|
1611 }else{ |
|
1612 pDb->stmtLast = pPreStmt->pPrev; |
|
1613 } |
|
1614 pDb->nStmt--; |
|
1615 break; |
|
1616 } |
|
1617 } |
|
1618 |
|
1619 /* If no prepared statement was found. Compile the SQL text |
|
1620 */ |
|
1621 if( pStmt==0 ){ |
|
1622 if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, &zLeft) ){ |
|
1623 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
|
1624 rc = TCL_ERROR; |
|
1625 break; |
|
1626 } |
|
1627 if( pStmt==0 ){ |
|
1628 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
|
1629 /* A compile-time error in the statement |
|
1630 */ |
|
1631 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
|
1632 rc = TCL_ERROR; |
|
1633 break; |
|
1634 }else{ |
|
1635 /* The statement was a no-op. Continue to the next statement |
|
1636 ** in the SQL string. |
|
1637 */ |
|
1638 zSql = zLeft; |
|
1639 continue; |
|
1640 } |
|
1641 } |
|
1642 assert( pPreStmt==0 ); |
|
1643 } |
|
1644 |
|
1645 /* Bind values to parameters that begin with $ or : |
|
1646 */ |
|
1647 nVar = sqlite3_bind_parameter_count(pStmt); |
|
1648 nParm = 0; |
|
1649 if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){ |
|
1650 apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0])); |
|
1651 }else{ |
|
1652 apParm = aParm; |
|
1653 } |
|
1654 for(i=1; i<=nVar; i++){ |
|
1655 const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
|
1656 if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ |
|
1657 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
|
1658 if( pVar ){ |
|
1659 int n; |
|
1660 u8 *data; |
|
1661 char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
|
1662 char c = zType[0]; |
|
1663 if( zVar[0]=='@' || |
|
1664 (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ |
|
1665 /* Load a BLOB type if the Tcl variable is a bytearray and |
|
1666 ** it has no string representation or the host |
|
1667 ** parameter name begins with "@". */ |
|
1668 data = Tcl_GetByteArrayFromObj(pVar, &n); |
|
1669 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
|
1670 Tcl_IncrRefCount(pVar); |
|
1671 apParm[nParm++] = pVar; |
|
1672 }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
|
1673 Tcl_GetIntFromObj(interp, pVar, &n); |
|
1674 sqlite3_bind_int(pStmt, i, n); |
|
1675 }else if( c=='d' && strcmp(zType,"double")==0 ){ |
|
1676 double r; |
|
1677 Tcl_GetDoubleFromObj(interp, pVar, &r); |
|
1678 sqlite3_bind_double(pStmt, i, r); |
|
1679 }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
|
1680 (c=='i' && strcmp(zType,"int")==0) ){ |
|
1681 Tcl_WideInt v; |
|
1682 Tcl_GetWideIntFromObj(interp, pVar, &v); |
|
1683 sqlite3_bind_int64(pStmt, i, v); |
|
1684 }else{ |
|
1685 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
|
1686 sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); |
|
1687 Tcl_IncrRefCount(pVar); |
|
1688 apParm[nParm++] = pVar; |
|
1689 } |
|
1690 }else{ |
|
1691 sqlite3_bind_null( pStmt, i ); |
|
1692 } |
|
1693 } |
|
1694 } |
|
1695 |
|
1696 /* Execute the SQL |
|
1697 */ |
|
1698 while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
1699 |
|
1700 /* Compute column names. This must be done after the first successful |
|
1701 ** call to sqlite3_step(), in case the query is recompiled and the |
|
1702 ** number or names of the returned columns changes. |
|
1703 */ |
|
1704 assert(!pArray||pScript); |
|
1705 if (nCol < 0) { |
|
1706 Tcl_Obj ***ap = (pScript?&apColName:0); |
|
1707 nCol = computeColumnNames(interp, pStmt, ap, pArray); |
|
1708 } |
|
1709 |
|
1710 for(i=0; i<nCol; i++){ |
|
1711 Tcl_Obj *pVal; |
|
1712 |
|
1713 /* Set pVal to contain the i'th column of this row. */ |
|
1714 switch( sqlite3_column_type(pStmt, i) ){ |
|
1715 case SQLITE_BLOB: { |
|
1716 int bytes = sqlite3_column_bytes(pStmt, i); |
|
1717 const char *zBlob = sqlite3_column_blob(pStmt, i); |
|
1718 if( !zBlob ) bytes = 0; |
|
1719 pVal = Tcl_NewByteArrayObj((u8*)zBlob, bytes); |
|
1720 break; |
|
1721 } |
|
1722 case SQLITE_INTEGER: { |
|
1723 sqlite_int64 v = sqlite3_column_int64(pStmt, i); |
|
1724 if( v>=-2147483647 && v<=2147483647 ){ |
|
1725 pVal = Tcl_NewIntObj(v); |
|
1726 }else{ |
|
1727 pVal = Tcl_NewWideIntObj(v); |
|
1728 } |
|
1729 break; |
|
1730 } |
|
1731 case SQLITE_FLOAT: { |
|
1732 double r = sqlite3_column_double(pStmt, i); |
|
1733 pVal = Tcl_NewDoubleObj(r); |
|
1734 break; |
|
1735 } |
|
1736 case SQLITE_NULL: { |
|
1737 pVal = dbTextToObj(pDb->zNull); |
|
1738 break; |
|
1739 } |
|
1740 default: { |
|
1741 pVal = dbTextToObj((char *)sqlite3_column_text(pStmt, i)); |
|
1742 break; |
|
1743 } |
|
1744 } |
|
1745 |
|
1746 if( pScript ){ |
|
1747 if( pArray==0 ){ |
|
1748 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
|
1749 }else{ |
|
1750 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
|
1751 } |
|
1752 }else if( choice==DB_ONECOLUMN ){ |
|
1753 assert( pRet==0 ); |
|
1754 if( pRet==0 ){ |
|
1755 pRet = pVal; |
|
1756 Tcl_IncrRefCount(pRet); |
|
1757 } |
|
1758 rc = TCL_BREAK; |
|
1759 i = nCol; |
|
1760 }else if( choice==DB_EXISTS ){ |
|
1761 Tcl_DecrRefCount(pRet); |
|
1762 pRet = Tcl_NewBooleanObj(1); |
|
1763 Tcl_IncrRefCount(pRet); |
|
1764 rc = TCL_BREAK; |
|
1765 i = nCol; |
|
1766 }else{ |
|
1767 Tcl_ListObjAppendElement(interp, pRet, pVal); |
|
1768 } |
|
1769 } |
|
1770 |
|
1771 if( pScript ){ |
|
1772 rc = Tcl_EvalObjEx(interp, pScript, 0); |
|
1773 if( rc==TCL_CONTINUE ){ |
|
1774 rc = TCL_OK; |
|
1775 } |
|
1776 } |
|
1777 } |
|
1778 if( rc==TCL_BREAK ){ |
|
1779 rc = TCL_OK; |
|
1780 } |
|
1781 |
|
1782 /* Free the column name objects */ |
|
1783 if( pScript ){ |
|
1784 /* If the query returned no rows, but an array variable was |
|
1785 ** specified, call computeColumnNames() now to populate the |
|
1786 ** arrayname(*) variable. |
|
1787 */ |
|
1788 if (pArray && nCol < 0) { |
|
1789 Tcl_Obj ***ap = (pScript?&apColName:0); |
|
1790 nCol = computeColumnNames(interp, pStmt, ap, pArray); |
|
1791 } |
|
1792 for(i=0; i<nCol; i++){ |
|
1793 Tcl_DecrRefCount(apColName[i]); |
|
1794 } |
|
1795 Tcl_Free((char*)apColName); |
|
1796 } |
|
1797 |
|
1798 /* Free the bound string and blob parameters */ |
|
1799 for(i=0; i<nParm; i++){ |
|
1800 Tcl_DecrRefCount(apParm[i]); |
|
1801 } |
|
1802 if( apParm!=aParm ){ |
|
1803 Tcl_Free((char*)apParm); |
|
1804 } |
|
1805 |
|
1806 /* Reset the statement. If the result code is SQLITE_SCHEMA, then |
|
1807 ** flush the statement cache and try the statement again. |
|
1808 */ |
|
1809 rc2 = sqlite3_reset(pStmt); |
|
1810 if( SQLITE_OK!=rc2 ){ |
|
1811 /* If a run-time error occurs, report the error and stop reading |
|
1812 ** the SQL |
|
1813 */ |
|
1814 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
|
1815 sqlite3_finalize(pStmt); |
|
1816 rc = TCL_ERROR; |
|
1817 if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
|
1818 break; |
|
1819 }else if( pDb->maxStmt<=0 ){ |
|
1820 /* If the cache is turned off, deallocated the statement */ |
|
1821 if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
|
1822 sqlite3_finalize(pStmt); |
|
1823 }else{ |
|
1824 /* Everything worked and the cache is operational. |
|
1825 ** Create a new SqlPreparedStmt structure if we need one. |
|
1826 ** (If we already have one we can just reuse it.) |
|
1827 */ |
|
1828 if( pPreStmt==0 ){ |
|
1829 len = zLeft - zSql; |
|
1830 pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) ); |
|
1831 if( pPreStmt==0 ) return TCL_ERROR; |
|
1832 pPreStmt->pStmt = pStmt; |
|
1833 pPreStmt->nSql = len; |
|
1834 pPreStmt->zSql = sqlite3_sql(pStmt); |
|
1835 assert( strlen(pPreStmt->zSql)==len ); |
|
1836 assert( 0==memcmp(pPreStmt->zSql, zSql, len) ); |
|
1837 } |
|
1838 |
|
1839 /* Add the prepared statement to the beginning of the cache list |
|
1840 */ |
|
1841 pPreStmt->pNext = pDb->stmtList; |
|
1842 pPreStmt->pPrev = 0; |
|
1843 if( pDb->stmtList ){ |
|
1844 pDb->stmtList->pPrev = pPreStmt; |
|
1845 } |
|
1846 pDb->stmtList = pPreStmt; |
|
1847 if( pDb->stmtLast==0 ){ |
|
1848 assert( pDb->nStmt==0 ); |
|
1849 pDb->stmtLast = pPreStmt; |
|
1850 }else{ |
|
1851 assert( pDb->nStmt>0 ); |
|
1852 } |
|
1853 pDb->nStmt++; |
|
1854 |
|
1855 /* If we have too many statement in cache, remove the surplus from the |
|
1856 ** end of the cache list. |
|
1857 */ |
|
1858 while( pDb->nStmt>pDb->maxStmt ){ |
|
1859 sqlite3_finalize(pDb->stmtLast->pStmt); |
|
1860 pDb->stmtLast = pDb->stmtLast->pPrev; |
|
1861 Tcl_Free((char*)pDb->stmtLast->pNext); |
|
1862 pDb->stmtLast->pNext = 0; |
|
1863 pDb->nStmt--; |
|
1864 } |
|
1865 } |
|
1866 |
|
1867 /* Proceed to the next statement */ |
|
1868 zSql = zLeft; |
|
1869 } |
|
1870 Tcl_DecrRefCount(objv[2]); |
|
1871 |
|
1872 if( pRet ){ |
|
1873 if( rc==TCL_OK ){ |
|
1874 Tcl_SetObjResult(interp, pRet); |
|
1875 } |
|
1876 Tcl_DecrRefCount(pRet); |
|
1877 }else if( rc==TCL_OK ){ |
|
1878 Tcl_ResetResult(interp); |
|
1879 } |
|
1880 break; |
|
1881 } |
|
1882 |
|
1883 /* |
|
1884 ** $db function NAME [-argcount N] SCRIPT |
|
1885 ** |
|
1886 ** Create a new SQL function called NAME. Whenever that function is |
|
1887 ** called, invoke SCRIPT to evaluate the function. |
|
1888 */ |
|
1889 case DB_FUNCTION: { |
|
1890 SqlFunc *pFunc; |
|
1891 Tcl_Obj *pScript; |
|
1892 char *zName; |
|
1893 int nArg = -1; |
|
1894 if( objc==6 ){ |
|
1895 const char *z = Tcl_GetString(objv[3]); |
|
1896 int n = strlen(z); |
|
1897 if( n>2 && strncmp(z, "-argcount",n)==0 ){ |
|
1898 if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR; |
|
1899 if( nArg<0 ){ |
|
1900 Tcl_AppendResult(interp, "number of arguments must be non-negative", |
|
1901 (char*)0); |
|
1902 return TCL_ERROR; |
|
1903 } |
|
1904 } |
|
1905 pScript = objv[5]; |
|
1906 }else if( objc!=4 ){ |
|
1907 Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT"); |
|
1908 return TCL_ERROR; |
|
1909 }else{ |
|
1910 pScript = objv[3]; |
|
1911 } |
|
1912 zName = Tcl_GetStringFromObj(objv[2], 0); |
|
1913 pFunc = findSqlFunc(pDb, zName); |
|
1914 if( pFunc==0 ) return TCL_ERROR; |
|
1915 if( pFunc->pScript ){ |
|
1916 Tcl_DecrRefCount(pFunc->pScript); |
|
1917 } |
|
1918 pFunc->pScript = pScript; |
|
1919 Tcl_IncrRefCount(pScript); |
|
1920 pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); |
|
1921 rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8, |
|
1922 pFunc, tclSqlFunc, 0, 0); |
|
1923 if( rc!=SQLITE_OK ){ |
|
1924 rc = TCL_ERROR; |
|
1925 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); |
|
1926 } |
|
1927 break; |
|
1928 } |
|
1929 |
|
1930 /* |
|
1931 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID |
|
1932 */ |
|
1933 case DB_INCRBLOB: { |
|
1934 #ifdef SQLITE_OMIT_INCRBLOB |
|
1935 Tcl_AppendResult(interp, "incrblob not available in this build", 0); |
|
1936 return TCL_ERROR; |
|
1937 #else |
|
1938 int isReadonly = 0; |
|
1939 const char *zDb = "main"; |
|
1940 const char *zTable; |
|
1941 const char *zColumn; |
|
1942 sqlite_int64 iRow; |
|
1943 |
|
1944 /* Check for the -readonly option */ |
|
1945 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ |
|
1946 isReadonly = 1; |
|
1947 } |
|
1948 |
|
1949 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ |
|
1950 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); |
|
1951 return TCL_ERROR; |
|
1952 } |
|
1953 |
|
1954 if( objc==(6+isReadonly) ){ |
|
1955 zDb = Tcl_GetString(objv[2]); |
|
1956 } |
|
1957 zTable = Tcl_GetString(objv[objc-3]); |
|
1958 zColumn = Tcl_GetString(objv[objc-2]); |
|
1959 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); |
|
1960 |
|
1961 if( rc==TCL_OK ){ |
|
1962 rc = createIncrblobChannel( |
|
1963 interp, pDb, zDb, zTable, zColumn, iRow, isReadonly |
|
1964 ); |
|
1965 } |
|
1966 #endif |
|
1967 break; |
|
1968 } |
|
1969 |
|
1970 /* |
|
1971 ** $db interrupt |
|
1972 ** |
|
1973 ** Interrupt the execution of the inner-most SQL interpreter. This |
|
1974 ** causes the SQL statement to return an error of SQLITE_INTERRUPT. |
|
1975 */ |
|
1976 case DB_INTERRUPT: { |
|
1977 sqlite3_interrupt(pDb->db); |
|
1978 break; |
|
1979 } |
|
1980 |
|
1981 /* |
|
1982 ** $db nullvalue ?STRING? |
|
1983 ** |
|
1984 ** Change text used when a NULL comes back from the database. If ?STRING? |
|
1985 ** is not present, then the current string used for NULL is returned. |
|
1986 ** If STRING is present, then STRING is returned. |
|
1987 ** |
|
1988 */ |
|
1989 case DB_NULLVALUE: { |
|
1990 if( objc!=2 && objc!=3 ){ |
|
1991 Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); |
|
1992 return TCL_ERROR; |
|
1993 } |
|
1994 if( objc==3 ){ |
|
1995 int len; |
|
1996 char *zNull = Tcl_GetStringFromObj(objv[2], &len); |
|
1997 if( pDb->zNull ){ |
|
1998 Tcl_Free(pDb->zNull); |
|
1999 } |
|
2000 if( zNull && len>0 ){ |
|
2001 pDb->zNull = Tcl_Alloc( len + 1 ); |
|
2002 strncpy(pDb->zNull, zNull, len); |
|
2003 pDb->zNull[len] = '\0'; |
|
2004 }else{ |
|
2005 pDb->zNull = 0; |
|
2006 } |
|
2007 } |
|
2008 Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull)); |
|
2009 break; |
|
2010 } |
|
2011 |
|
2012 /* |
|
2013 ** $db last_insert_rowid |
|
2014 ** |
|
2015 ** Return an integer which is the ROWID for the most recent insert. |
|
2016 */ |
|
2017 case DB_LAST_INSERT_ROWID: { |
|
2018 Tcl_Obj *pResult; |
|
2019 Tcl_WideInt rowid; |
|
2020 if( objc!=2 ){ |
|
2021 Tcl_WrongNumArgs(interp, 2, objv, ""); |
|
2022 return TCL_ERROR; |
|
2023 } |
|
2024 rowid = sqlite3_last_insert_rowid(pDb->db); |
|
2025 pResult = Tcl_GetObjResult(interp); |
|
2026 Tcl_SetWideIntObj(pResult, rowid); |
|
2027 break; |
|
2028 } |
|
2029 |
|
2030 /* |
|
2031 ** The DB_ONECOLUMN method is implemented together with DB_EVAL. |
|
2032 */ |
|
2033 |
|
2034 /* $db progress ?N CALLBACK? |
|
2035 ** |
|
2036 ** Invoke the given callback every N virtual machine opcodes while executing |
|
2037 ** queries. |
|
2038 */ |
|
2039 case DB_PROGRESS: { |
|
2040 if( objc==2 ){ |
|
2041 if( pDb->zProgress ){ |
|
2042 Tcl_AppendResult(interp, pDb->zProgress, 0); |
|
2043 } |
|
2044 }else if( objc==4 ){ |
|
2045 char *zProgress; |
|
2046 int len; |
|
2047 int N; |
|
2048 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ |
|
2049 return TCL_ERROR; |
|
2050 }; |
|
2051 if( pDb->zProgress ){ |
|
2052 Tcl_Free(pDb->zProgress); |
|
2053 } |
|
2054 zProgress = Tcl_GetStringFromObj(objv[3], &len); |
|
2055 if( zProgress && len>0 ){ |
|
2056 pDb->zProgress = Tcl_Alloc( len + 1 ); |
|
2057 memcpy(pDb->zProgress, zProgress, len+1); |
|
2058 }else{ |
|
2059 pDb->zProgress = 0; |
|
2060 } |
|
2061 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
|
2062 if( pDb->zProgress ){ |
|
2063 pDb->interp = interp; |
|
2064 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); |
|
2065 }else{ |
|
2066 sqlite3_progress_handler(pDb->db, 0, 0, 0); |
|
2067 } |
|
2068 #endif |
|
2069 }else{ |
|
2070 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); |
|
2071 return TCL_ERROR; |
|
2072 } |
|
2073 break; |
|
2074 } |
|
2075 |
|
2076 /* $db profile ?CALLBACK? |
|
2077 ** |
|
2078 ** Make arrangements to invoke the CALLBACK routine after each SQL statement |
|
2079 ** that has run. The text of the SQL and the amount of elapse time are |
|
2080 ** appended to CALLBACK before the script is run. |
|
2081 */ |
|
2082 case DB_PROFILE: { |
|
2083 if( objc>3 ){ |
|
2084 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
|
2085 return TCL_ERROR; |
|
2086 }else if( objc==2 ){ |
|
2087 if( pDb->zProfile ){ |
|
2088 Tcl_AppendResult(interp, pDb->zProfile, 0); |
|
2089 } |
|
2090 }else{ |
|
2091 char *zProfile; |
|
2092 int len; |
|
2093 if( pDb->zProfile ){ |
|
2094 Tcl_Free(pDb->zProfile); |
|
2095 } |
|
2096 zProfile = Tcl_GetStringFromObj(objv[2], &len); |
|
2097 if( zProfile && len>0 ){ |
|
2098 pDb->zProfile = Tcl_Alloc( len + 1 ); |
|
2099 memcpy(pDb->zProfile, zProfile, len+1); |
|
2100 }else{ |
|
2101 pDb->zProfile = 0; |
|
2102 } |
|
2103 #ifndef SQLITE_OMIT_TRACE |
|
2104 if( pDb->zProfile ){ |
|
2105 pDb->interp = interp; |
|
2106 sqlite3_profile(pDb->db, DbProfileHandler, pDb); |
|
2107 }else{ |
|
2108 sqlite3_profile(pDb->db, 0, 0); |
|
2109 } |
|
2110 #endif |
|
2111 } |
|
2112 break; |
|
2113 } |
|
2114 |
|
2115 /* |
|
2116 ** $db rekey KEY |
|
2117 ** |
|
2118 ** Change the encryption key on the currently open database. |
|
2119 */ |
|
2120 case DB_REKEY: { |
|
2121 int nKey; |
|
2122 void *pKey; |
|
2123 if( objc!=3 ){ |
|
2124 Tcl_WrongNumArgs(interp, 2, objv, "KEY"); |
|
2125 return TCL_ERROR; |
|
2126 } |
|
2127 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); |
|
2128 #ifdef SQLITE_HAS_CODEC |
|
2129 rc = sqlite3_rekey(pDb->db, pKey, nKey); |
|
2130 if( rc ){ |
|
2131 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
|
2132 rc = TCL_ERROR; |
|
2133 } |
|
2134 #endif |
|
2135 break; |
|
2136 } |
|
2137 |
|
2138 /* |
|
2139 ** $db timeout MILLESECONDS |
|
2140 ** |
|
2141 ** Delay for the number of milliseconds specified when a file is locked. |
|
2142 */ |
|
2143 case DB_TIMEOUT: { |
|
2144 int ms; |
|
2145 if( objc!=3 ){ |
|
2146 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); |
|
2147 return TCL_ERROR; |
|
2148 } |
|
2149 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; |
|
2150 sqlite3_busy_timeout(pDb->db, ms); |
|
2151 break; |
|
2152 } |
|
2153 |
|
2154 /* |
|
2155 ** $db total_changes |
|
2156 ** |
|
2157 ** Return the number of rows that were modified, inserted, or deleted |
|
2158 ** since the database handle was created. |
|
2159 */ |
|
2160 case DB_TOTAL_CHANGES: { |
|
2161 Tcl_Obj *pResult; |
|
2162 if( objc!=2 ){ |
|
2163 Tcl_WrongNumArgs(interp, 2, objv, ""); |
|
2164 return TCL_ERROR; |
|
2165 } |
|
2166 pResult = Tcl_GetObjResult(interp); |
|
2167 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); |
|
2168 break; |
|
2169 } |
|
2170 |
|
2171 /* $db trace ?CALLBACK? |
|
2172 ** |
|
2173 ** Make arrangements to invoke the CALLBACK routine for each SQL statement |
|
2174 ** that is executed. The text of the SQL is appended to CALLBACK before |
|
2175 ** it is executed. |
|
2176 */ |
|
2177 case DB_TRACE: { |
|
2178 if( objc>3 ){ |
|
2179 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); |
|
2180 return TCL_ERROR; |
|
2181 }else if( objc==2 ){ |
|
2182 if( pDb->zTrace ){ |
|
2183 Tcl_AppendResult(interp, pDb->zTrace, 0); |
|
2184 } |
|
2185 }else{ |
|
2186 char *zTrace; |
|
2187 int len; |
|
2188 if( pDb->zTrace ){ |
|
2189 Tcl_Free(pDb->zTrace); |
|
2190 } |
|
2191 zTrace = Tcl_GetStringFromObj(objv[2], &len); |
|
2192 if( zTrace && len>0 ){ |
|
2193 pDb->zTrace = Tcl_Alloc( len + 1 ); |
|
2194 memcpy(pDb->zTrace, zTrace, len+1); |
|
2195 }else{ |
|
2196 pDb->zTrace = 0; |
|
2197 } |
|
2198 #ifndef SQLITE_OMIT_TRACE |
|
2199 if( pDb->zTrace ){ |
|
2200 pDb->interp = interp; |
|
2201 sqlite3_trace(pDb->db, DbTraceHandler, pDb); |
|
2202 }else{ |
|
2203 sqlite3_trace(pDb->db, 0, 0); |
|
2204 } |
|
2205 #endif |
|
2206 } |
|
2207 break; |
|
2208 } |
|
2209 |
|
2210 /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT |
|
2211 ** |
|
2212 ** Start a new transaction (if we are not already in the midst of a |
|
2213 ** transaction) and execute the TCL script SCRIPT. After SCRIPT |
|
2214 ** completes, either commit the transaction or roll it back if SCRIPT |
|
2215 ** throws an exception. Or if no new transation was started, do nothing. |
|
2216 ** pass the exception on up the stack. |
|
2217 ** |
|
2218 ** This command was inspired by Dave Thomas's talk on Ruby at the |
|
2219 ** 2005 O'Reilly Open Source Convention (OSCON). |
|
2220 */ |
|
2221 case DB_TRANSACTION: { |
|
2222 int inTrans; |
|
2223 Tcl_Obj *pScript; |
|
2224 const char *zBegin = "BEGIN"; |
|
2225 if( objc!=3 && objc!=4 ){ |
|
2226 Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); |
|
2227 return TCL_ERROR; |
|
2228 } |
|
2229 if( objc==3 ){ |
|
2230 pScript = objv[2]; |
|
2231 } else { |
|
2232 static const char *TTYPE_strs[] = { |
|
2233 "deferred", "exclusive", "immediate", 0 |
|
2234 }; |
|
2235 enum TTYPE_enum { |
|
2236 TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE |
|
2237 }; |
|
2238 int ttype; |
|
2239 if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", |
|
2240 0, &ttype) ){ |
|
2241 return TCL_ERROR; |
|
2242 } |
|
2243 switch( (enum TTYPE_enum)ttype ){ |
|
2244 case TTYPE_DEFERRED: /* no-op */; break; |
|
2245 case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; |
|
2246 case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; |
|
2247 } |
|
2248 pScript = objv[3]; |
|
2249 } |
|
2250 inTrans = !sqlite3_get_autocommit(pDb->db); |
|
2251 if( !inTrans ){ |
|
2252 pDb->disableAuth++; |
|
2253 (void)sqlite3_exec(pDb->db, zBegin, 0, 0, 0); |
|
2254 pDb->disableAuth--; |
|
2255 } |
|
2256 rc = Tcl_EvalObjEx(interp, pScript, 0); |
|
2257 if( !inTrans ){ |
|
2258 const char *zEnd; |
|
2259 if( rc==TCL_ERROR ){ |
|
2260 zEnd = "ROLLBACK"; |
|
2261 } else { |
|
2262 zEnd = "COMMIT"; |
|
2263 } |
|
2264 pDb->disableAuth++; |
|
2265 if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ |
|
2266 sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); |
|
2267 } |
|
2268 pDb->disableAuth--; |
|
2269 } |
|
2270 break; |
|
2271 } |
|
2272 |
|
2273 /* |
|
2274 ** $db update_hook ?script? |
|
2275 ** $db rollback_hook ?script? |
|
2276 */ |
|
2277 case DB_UPDATE_HOOK: |
|
2278 case DB_ROLLBACK_HOOK: { |
|
2279 |
|
2280 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on |
|
2281 ** whether [$db update_hook] or [$db rollback_hook] was invoked. |
|
2282 */ |
|
2283 Tcl_Obj **ppHook; |
|
2284 if( choice==DB_UPDATE_HOOK ){ |
|
2285 ppHook = &pDb->pUpdateHook; |
|
2286 }else{ |
|
2287 ppHook = &pDb->pRollbackHook; |
|
2288 } |
|
2289 |
|
2290 if( objc!=2 && objc!=3 ){ |
|
2291 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); |
|
2292 return TCL_ERROR; |
|
2293 } |
|
2294 if( *ppHook ){ |
|
2295 Tcl_SetObjResult(interp, *ppHook); |
|
2296 if( objc==3 ){ |
|
2297 Tcl_DecrRefCount(*ppHook); |
|
2298 *ppHook = 0; |
|
2299 } |
|
2300 } |
|
2301 if( objc==3 ){ |
|
2302 assert( !(*ppHook) ); |
|
2303 if( Tcl_GetCharLength(objv[2])>0 ){ |
|
2304 *ppHook = objv[2]; |
|
2305 Tcl_IncrRefCount(*ppHook); |
|
2306 } |
|
2307 } |
|
2308 |
|
2309 sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); |
|
2310 sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); |
|
2311 |
|
2312 break; |
|
2313 } |
|
2314 |
|
2315 /* $db version |
|
2316 ** |
|
2317 ** Return the version string for this database. |
|
2318 */ |
|
2319 case DB_VERSION: { |
|
2320 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); |
|
2321 break; |
|
2322 } |
|
2323 |
|
2324 |
|
2325 } /* End of the SWITCH statement */ |
|
2326 return rc; |
|
2327 } |
|
2328 |
|
2329 /* |
|
2330 ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? |
|
2331 ** ?-create BOOLEAN? ?-nomutex BOOLEAN? |
|
2332 ** |
|
2333 ** This is the main Tcl command. When the "sqlite" Tcl command is |
|
2334 ** invoked, this routine runs to process that command. |
|
2335 ** |
|
2336 ** The first argument, DBNAME, is an arbitrary name for a new |
|
2337 ** database connection. This command creates a new command named |
|
2338 ** DBNAME that is used to control that connection. The database |
|
2339 ** connection is deleted when the DBNAME command is deleted. |
|
2340 ** |
|
2341 ** The second argument is the name of the database file. |
|
2342 ** |
|
2343 */ |
|
2344 static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
|
2345 SqliteDb *p; |
|
2346 void *pKey = 0; |
|
2347 int nKey = 0; |
|
2348 const char *zArg; |
|
2349 char *zErrMsg; |
|
2350 int i; |
|
2351 const char *zFile; |
|
2352 const char *zVfs = 0; |
|
2353 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; |
|
2354 Tcl_DString translatedFilename; |
|
2355 if( objc==2 ){ |
|
2356 zArg = Tcl_GetStringFromObj(objv[1], 0); |
|
2357 if( strcmp(zArg,"-version")==0 ){ |
|
2358 Tcl_AppendResult(interp,sqlite3_version,0); |
|
2359 return TCL_OK; |
|
2360 } |
|
2361 if( strcmp(zArg,"-has-codec")==0 ){ |
|
2362 #ifdef SQLITE_HAS_CODEC |
|
2363 Tcl_AppendResult(interp,"1",0); |
|
2364 #else |
|
2365 Tcl_AppendResult(interp,"0",0); |
|
2366 #endif |
|
2367 return TCL_OK; |
|
2368 } |
|
2369 } |
|
2370 for(i=3; i+1<objc; i+=2){ |
|
2371 zArg = Tcl_GetString(objv[i]); |
|
2372 if( strcmp(zArg,"-key")==0 ){ |
|
2373 pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); |
|
2374 }else if( strcmp(zArg, "-vfs")==0 ){ |
|
2375 i++; |
|
2376 zVfs = Tcl_GetString(objv[i]); |
|
2377 }else if( strcmp(zArg, "-readonly")==0 ){ |
|
2378 int b; |
|
2379 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
|
2380 if( b ){ |
|
2381 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
|
2382 flags |= SQLITE_OPEN_READONLY; |
|
2383 }else{ |
|
2384 flags &= ~SQLITE_OPEN_READONLY; |
|
2385 flags |= SQLITE_OPEN_READWRITE; |
|
2386 } |
|
2387 }else if( strcmp(zArg, "-create")==0 ){ |
|
2388 int b; |
|
2389 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
|
2390 if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ |
|
2391 flags |= SQLITE_OPEN_CREATE; |
|
2392 }else{ |
|
2393 flags &= ~SQLITE_OPEN_CREATE; |
|
2394 } |
|
2395 }else if( strcmp(zArg, "-nomutex")==0 ){ |
|
2396 int b; |
|
2397 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
|
2398 if( b ){ |
|
2399 flags |= SQLITE_OPEN_NOMUTEX; |
|
2400 flags &= ~SQLITE_OPEN_FULLMUTEX; |
|
2401 }else{ |
|
2402 flags &= ~SQLITE_OPEN_NOMUTEX; |
|
2403 } |
|
2404 }else if( strcmp(zArg, "-fullmutex")==0 ){ |
|
2405 int b; |
|
2406 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
|
2407 if( b ){ |
|
2408 flags |= SQLITE_OPEN_FULLMUTEX; |
|
2409 flags &= ~SQLITE_OPEN_NOMUTEX; |
|
2410 }else{ |
|
2411 flags &= ~SQLITE_OPEN_FULLMUTEX; |
|
2412 } |
|
2413 }else{ |
|
2414 Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); |
|
2415 return TCL_ERROR; |
|
2416 } |
|
2417 } |
|
2418 if( objc<3 || (objc&1)!=1 ){ |
|
2419 Tcl_WrongNumArgs(interp, 1, objv, |
|
2420 "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" |
|
2421 " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?" |
|
2422 #ifdef SQLITE_HAS_CODEC |
|
2423 " ?-key CODECKEY?" |
|
2424 #endif |
|
2425 ); |
|
2426 return TCL_ERROR; |
|
2427 } |
|
2428 zErrMsg = 0; |
|
2429 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); |
|
2430 if( p==0 ){ |
|
2431 Tcl_SetResult(interp, "malloc failed", TCL_STATIC); |
|
2432 return TCL_ERROR; |
|
2433 } |
|
2434 memset(p, 0, sizeof(*p)); |
|
2435 zFile = Tcl_GetStringFromObj(objv[2], 0); |
|
2436 zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); |
|
2437 sqlite3_open_v2(zFile, &p->db, flags, zVfs); |
|
2438 Tcl_DStringFree(&translatedFilename); |
|
2439 if( SQLITE_OK!=sqlite3_errcode(p->db) ){ |
|
2440 zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); |
|
2441 sqlite3_close(p->db); |
|
2442 p->db = 0; |
|
2443 } |
|
2444 #ifdef SQLITE_HAS_CODEC |
|
2445 if( p->db ){ |
|
2446 sqlite3_key(p->db, pKey, nKey); |
|
2447 } |
|
2448 #endif |
|
2449 if( p->db==0 ){ |
|
2450 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); |
|
2451 Tcl_Free((char*)p); |
|
2452 sqlite3_free(zErrMsg); |
|
2453 return TCL_ERROR; |
|
2454 } |
|
2455 p->maxStmt = NUM_PREPARED_STMTS; |
|
2456 p->interp = interp; |
|
2457 zArg = Tcl_GetStringFromObj(objv[1], 0); |
|
2458 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
|
2459 return TCL_OK; |
|
2460 } |
|
2461 |
|
2462 /* |
|
2463 ** Provide a dummy Tcl_InitStubs if we are using this as a static |
|
2464 ** library. |
|
2465 */ |
|
2466 #ifndef USE_TCL_STUBS |
|
2467 # undef Tcl_InitStubs |
|
2468 # define Tcl_InitStubs(a,b,c) |
|
2469 #endif |
|
2470 |
|
2471 /* |
|
2472 ** Make sure we have a PACKAGE_VERSION macro defined. This will be |
|
2473 ** defined automatically by the TEA makefile. But other makefiles |
|
2474 ** do not define it. |
|
2475 */ |
|
2476 #ifndef PACKAGE_VERSION |
|
2477 # define PACKAGE_VERSION SQLITE_VERSION |
|
2478 #endif |
|
2479 |
|
2480 /* |
|
2481 ** Initialize this module. |
|
2482 ** |
|
2483 ** This Tcl module contains only a single new Tcl command named "sqlite". |
|
2484 ** (Hence there is no namespace. There is no point in using a namespace |
|
2485 ** if the extension only supplies one new name!) The "sqlite" command is |
|
2486 ** used to open a new SQLite database. See the DbMain() routine above |
|
2487 ** for additional information. |
|
2488 */ |
|
2489 EXTERN int Sqlite3_Init(Tcl_Interp *interp){ |
|
2490 Tcl_InitStubs(interp, "8.4", 0); |
|
2491 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
|
2492 Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); |
|
2493 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
|
2494 Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION); |
|
2495 return TCL_OK; |
|
2496 } |
|
2497 EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
|
2498 EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
|
2499 EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
|
2500 EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
|
2501 EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
|
2502 EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
|
2503 EXTERN int Tclsqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
|
2504 |
|
2505 |
|
2506 #ifndef SQLITE_3_SUFFIX_ONLY |
|
2507 EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
|
2508 EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
|
2509 EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
|
2510 EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
|
2511 EXTERN int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
|
2512 EXTERN int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
|
2513 EXTERN int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
|
2514 EXTERN int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
|
2515 #endif |
|
2516 |
|
2517 #ifdef TCLSH |
|
2518 /***************************************************************************** |
|
2519 ** The code that follows is used to build standalone TCL interpreters |
|
2520 ** that are statically linked with SQLite. |
|
2521 */ |
|
2522 |
|
2523 /* |
|
2524 ** If the macro TCLSH is one, then put in code this for the |
|
2525 ** "main" routine that will initialize Tcl and take input from |
|
2526 ** standard input, or if a file is named on the command line |
|
2527 ** the TCL interpreter reads and evaluates that file. |
|
2528 */ |
|
2529 #if TCLSH==1 |
|
2530 static char zMainloop[] = |
|
2531 "set line {}\n" |
|
2532 "while {![eof stdin]} {\n" |
|
2533 "if {$line!=\"\"} {\n" |
|
2534 "puts -nonewline \"> \"\n" |
|
2535 "} else {\n" |
|
2536 "puts -nonewline \"% \"\n" |
|
2537 "}\n" |
|
2538 "flush stdout\n" |
|
2539 "append line [gets stdin]\n" |
|
2540 "if {[info complete $line]} {\n" |
|
2541 "if {[catch {uplevel #0 $line} result]} {\n" |
|
2542 "puts stderr \"Error: $result\"\n" |
|
2543 "} elseif {$result!=\"\"} {\n" |
|
2544 "puts $result\n" |
|
2545 "}\n" |
|
2546 "set line {}\n" |
|
2547 "} else {\n" |
|
2548 "append line \\n\n" |
|
2549 "}\n" |
|
2550 "}\n" |
|
2551 ; |
|
2552 #endif |
|
2553 |
|
2554 /* |
|
2555 ** If the macro TCLSH is two, then get the main loop code out of |
|
2556 ** the separate file "spaceanal_tcl.h". |
|
2557 */ |
|
2558 #if TCLSH==2 |
|
2559 static char zMainloop[] = |
|
2560 #include "spaceanal_tcl.h" |
|
2561 ; |
|
2562 #endif |
|
2563 |
|
2564 #define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
|
2565 int TCLSH_MAIN(int argc, char **argv){ |
|
2566 Tcl_Interp *interp; |
|
2567 #if defined(__SYMBIAN32__) |
|
2568 int isChildProcess = 0; |
|
2569 int oldArgc = 0; |
|
2570 int err = 0; |
|
2571 #endif |
|
2572 |
|
2573 PrintS("###TclSqlite3: New instance"); |
|
2574 Tcl_FindExecutable(argv[0]); |
|
2575 PrintS("###TclSqlite3: Create interpreter"); |
|
2576 interp = Tcl_CreateInterp(); |
|
2577 #if defined(__SYMBIAN32__) |
|
2578 PrintS("###TclSqlite3: Child process init"); |
|
2579 if (ChildProcessInit(&argc, &argv)) |
|
2580 { |
|
2581 oldArgc = argc; |
|
2582 argc = argc-4; |
|
2583 isChildProcess = 1; |
|
2584 } |
|
2585 else |
|
2586 { |
|
2587 err = CopyTestFiles(); |
|
2588 if(err != 0) |
|
2589 { |
|
2590 PrintS("###TclSqlite3: Exit-1"); |
|
2591 return 1; |
|
2592 } |
|
2593 } |
|
2594 #endif |
|
2595 PrintS("###TclSqlite3: Init"); |
|
2596 Sqlite3_Init(interp); |
|
2597 #ifdef SQLITE_TEST |
|
2598 { |
|
2599 extern int Md5_Init(Tcl_Interp*); |
|
2600 extern int Sqliteconfig_Init(Tcl_Interp*); |
|
2601 extern int Sqlitetest1_Init(Tcl_Interp*); |
|
2602 extern int Sqlitetest2_Init(Tcl_Interp*); |
|
2603 extern int Sqlitetest3_Init(Tcl_Interp*); |
|
2604 extern int Sqlitetest4_Init(Tcl_Interp*); |
|
2605 extern int Sqlitetest5_Init(Tcl_Interp*); |
|
2606 extern int Sqlitetest6_Init(Tcl_Interp*); |
|
2607 extern int Sqlitetest7_Init(Tcl_Interp*); |
|
2608 extern int Sqlitetest8_Init(Tcl_Interp*); |
|
2609 extern int Sqlitetest9_Init(Tcl_Interp*); |
|
2610 extern int Sqlitetestasync_Init(Tcl_Interp*); |
|
2611 extern int Sqlitetest_autoext_Init(Tcl_Interp*); |
|
2612 extern int Sqlitetest_func_Init(Tcl_Interp*); |
|
2613 extern int Sqlitetest_hexio_Init(Tcl_Interp*); |
|
2614 extern int Sqlitetest_malloc_Init(Tcl_Interp*); |
|
2615 extern int Sqlitetest_mutex_Init(Tcl_Interp*); |
|
2616 extern int Sqlitetestschema_Init(Tcl_Interp*); |
|
2617 extern int Sqlitetestsse_Init(Tcl_Interp*); |
|
2618 extern int Sqlitetesttclvar_Init(Tcl_Interp*); |
|
2619 extern int SqlitetestThread_Init(Tcl_Interp*); |
|
2620 extern int SqlitetestOnefile_Init(); |
|
2621 extern int SqlitetestOsinst_Init(Tcl_Interp*); |
|
2622 |
|
2623 Md5_Init(interp); |
|
2624 Sqliteconfig_Init(interp); |
|
2625 Sqlitetest1_Init(interp); |
|
2626 Sqlitetest2_Init(interp); |
|
2627 Sqlitetest3_Init(interp); |
|
2628 Sqlitetest4_Init(interp); |
|
2629 Sqlitetest5_Init(interp); |
|
2630 Sqlitetest6_Init(interp); |
|
2631 Sqlitetest7_Init(interp); |
|
2632 Sqlitetest8_Init(interp); |
|
2633 Sqlitetest9_Init(interp); |
|
2634 Sqlitetestasync_Init(interp); |
|
2635 Sqlitetest_autoext_Init(interp); |
|
2636 Sqlitetest_func_Init(interp); |
|
2637 Sqlitetest_hexio_Init(interp); |
|
2638 Sqlitetest_malloc_Init(interp); |
|
2639 Sqlitetest_mutex_Init(interp); |
|
2640 Sqlitetestschema_Init(interp); |
|
2641 Sqlitetesttclvar_Init(interp); |
|
2642 SqlitetestThread_Init(interp); |
|
2643 SqlitetestOnefile_Init(interp); |
|
2644 SqlitetestOsinst_Init(interp); |
|
2645 |
|
2646 #ifdef SQLITE_SSE |
|
2647 Sqlitetestsse_Init(interp); |
|
2648 #endif |
|
2649 } |
|
2650 #endif |
|
2651 if( argc>=2 || TCLSH==2 ){ |
|
2652 int i; |
|
2653 char zArgc[32]; |
|
2654 sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); |
|
2655 Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); |
|
2656 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); |
|
2657 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
|
2658 for(i=3-TCLSH; i<argc; i++){ |
|
2659 Tcl_SetVar(interp, "argv", argv[i], |
|
2660 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); |
|
2661 } |
|
2662 #ifdef __SYMBIAN32__ |
|
2663 Tcl_CreateObjCommand(interp, "delete_test_files", (Tcl_ObjCmdProc*)DeleteTestFiles, 0, 0); |
|
2664 Tcl_CreateObjCommand(interp, "print_text", (Tcl_ObjCmdProc*)PrintText, 0, 0); |
|
2665 #endif |
|
2666 PrintS("###TclSqlite3: Tests begin"); |
|
2667 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ |
|
2668 char errMsg[300]; |
|
2669 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); |
|
2670 if( zInfo==0 ) |
|
2671 zInfo = interp->result; |
|
2672 sprintf(errMsg, "###TclSqlite3: Error, argv=%s, zInfo=%s", *argv, zInfo); |
|
2673 PrintS(errMsg); |
|
2674 #ifdef __SYMBIAN32__ |
|
2675 ChildProcessCleanup(isChildProcess, oldArgc, argv); |
|
2676 #endif |
|
2677 PrintS("###TclSqlite3: Exit-2"); |
|
2678 return 1; |
|
2679 } |
|
2680 PrintS("###TclSqlite3: Tests end"); |
|
2681 } |
|
2682 if( argc<=1 || TCLSH==2 ){ |
|
2683 Tcl_GlobalEval(interp, zMainloop); |
|
2684 } |
|
2685 #if defined(__SYMBIAN32__) |
|
2686 ChildProcessCleanup(isChildProcess, oldArgc, argv); |
|
2687 #endif |
|
2688 PrintS("###TclSqlite3: Exit-3"); |
|
2689 return 0; |
|
2690 } |
|
2691 #endif /* TCLSH */ |