1 /*
2 ** 2007 September 9
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 ** This file contains the implementation of some Tcl commands used to
14 ** test that sqlite3 database handles may be concurrently accessed by
15 ** multiple threads. Right now this only works on unix.
16 */
17
18 #include "sqliteInt.h"
19 #if defined(INCLUDE_SQLITE_TCL_H)
20 # include "sqlite_tcl.h"
21 #else
22 # include "tcl.h"
23 #endif
24
25 #if SQLITE_THREADSAFE
26
27 #include <errno.h>
28
29 #if !defined(_MSC_VER)
30 #include <unistd.h>
31 #endif
32
33 /*
34 ** One of these is allocated for each thread created by [sqlthread spawn].
35 */
36 typedef struct SqlThread SqlThread;
37 struct SqlThread {
38 Tcl_ThreadId parent; /* Thread id of parent thread */
39 Tcl_Interp *interp; /* Parent interpreter */
40 char *zScript; /* The script to execute. */
41 char *zVarname; /* Varname in parent script */
42 };
43
44 /*
45 ** A custom Tcl_Event type used by this module. When the event is
46 ** handled, script zScript is evaluated in interpreter interp. If
47 ** the evaluation throws an exception (returns TCL_ERROR), then the
48 ** error is handled by Tcl_BackgroundError(). If no error occurs,
49 ** the result is simply discarded.
50 */
51 typedef struct EvalEvent EvalEvent;
52 struct EvalEvent {
53 Tcl_Event base; /* Base class of type Tcl_Event */
54 char *zScript; /* The script to execute. */
55 Tcl_Interp *interp; /* The interpreter to execute it in. */
56 };
57
58 static Tcl_ObjCmdProc sqlthread_proc;
59 static Tcl_ObjCmdProc clock_seconds_proc;
60 #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
61 static Tcl_ObjCmdProc blocking_step_proc;
62 static Tcl_ObjCmdProc blocking_prepare_v2_proc;
63 #endif
64 int Sqlitetest1_Init(Tcl_Interp *);
65 int Sqlite3_Init(Tcl_Interp *);
66
67 /* Functions from main.c */
68 extern const char *sqlite3ErrName(int);
69
70 /* Functions from test1.c */
71 extern void *sqlite3TestTextToPtr(const char *);
72 extern int getDbPointer(Tcl_Interp *, const char *, sqlite3 **);
73 extern int sqlite3TestMakePointerStr(Tcl_Interp *, char *, void *);
74 extern int sqlite3TestErrCode(Tcl_Interp *, sqlite3 *, int);
75
76 /*
77 ** Handler for events of type EvalEvent.
78 */
tclScriptEvent(Tcl_Event * evPtr,int flags)79 static int SQLITE_TCLAPI tclScriptEvent(Tcl_Event *evPtr, int flags){
80 int rc;
81 EvalEvent *p = (EvalEvent *)evPtr;
82 rc = Tcl_Eval(p->interp, p->zScript);
83 if( rc!=TCL_OK ){
84 Tcl_BackgroundError(p->interp);
85 }
86 UNUSED_PARAMETER(flags);
87 return 1;
88 }
89
90 /*
91 ** Register an EvalEvent to evaluate the script pScript in the
92 ** parent interpreter/thread of SqlThread p.
93 */
postToParent(SqlThread * p,Tcl_Obj * pScript)94 static void postToParent(SqlThread *p, Tcl_Obj *pScript){
95 EvalEvent *pEvent;
96 char *zMsg;
97 int nMsg;
98
99 zMsg = Tcl_GetStringFromObj(pScript, &nMsg);
100 pEvent = (EvalEvent *)ckalloc(sizeof(EvalEvent)+nMsg+1);
101 pEvent->base.nextPtr = 0;
102 pEvent->base.proc = tclScriptEvent;
103 pEvent->zScript = (char *)&pEvent[1];
104 memcpy(pEvent->zScript, zMsg, nMsg+1);
105 pEvent->interp = p->interp;
106
107 Tcl_ThreadQueueEvent(p->parent, (Tcl_Event *)pEvent, TCL_QUEUE_TAIL);
108 Tcl_ThreadAlert(p->parent);
109 }
110
111 /*
112 ** The main function for threads created with [sqlthread spawn].
113 */
tclScriptThread(ClientData pSqlThread)114 static Tcl_ThreadCreateType tclScriptThread(ClientData pSqlThread){
115 Tcl_Interp *interp;
116 Tcl_Obj *pRes;
117 Tcl_Obj *pList;
118 int rc;
119 SqlThread *p = (SqlThread *)pSqlThread;
120 extern int Sqlitetest_mutex_Init(Tcl_Interp*);
121
122 interp = Tcl_CreateInterp();
123 Tcl_CreateObjCommand(interp, "clock_seconds", clock_seconds_proc, 0, 0);
124 Tcl_CreateObjCommand(interp, "sqlthread", sqlthread_proc, pSqlThread, 0);
125 #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
126 Tcl_CreateObjCommand(interp, "sqlite3_blocking_step", blocking_step_proc,0,0);
127 Tcl_CreateObjCommand(interp,
128 "sqlite3_blocking_prepare_v2", blocking_prepare_v2_proc, (void *)1, 0);
129 Tcl_CreateObjCommand(interp,
130 "sqlite3_nonblocking_prepare_v2", blocking_prepare_v2_proc, 0, 0);
131 #endif
132 Sqlitetest1_Init(interp);
133 Sqlitetest_mutex_Init(interp);
134 Sqlite3_Init(interp);
135
136 rc = Tcl_Eval(interp, p->zScript);
137 pRes = Tcl_GetObjResult(interp);
138 pList = Tcl_NewObj();
139 Tcl_IncrRefCount(pList);
140 Tcl_IncrRefCount(pRes);
141
142 if( rc!=TCL_OK ){
143 Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj("error", -1));
144 Tcl_ListObjAppendElement(interp, pList, pRes);
145 postToParent(p, pList);
146 Tcl_DecrRefCount(pList);
147 pList = Tcl_NewObj();
148 }
149
150 Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj("set", -1));
151 Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(p->zVarname, -1));
152 Tcl_ListObjAppendElement(interp, pList, pRes);
153 postToParent(p, pList);
154
155 ckfree((void *)p);
156 Tcl_DecrRefCount(pList);
157 Tcl_DecrRefCount(pRes);
158 Tcl_DeleteInterp(interp);
159 while( Tcl_DoOneEvent(TCL_ALL_EVENTS|TCL_DONT_WAIT) );
160 Tcl_ExitThread(0);
161 TCL_THREAD_CREATE_RETURN;
162 }
163
164 /*
165 ** sqlthread spawn VARNAME SCRIPT
166 **
167 ** Spawn a new thread with its own Tcl interpreter and run the
168 ** specified SCRIPT(s) in it. The thread terminates after running
169 ** the script. The result of the script is stored in the variable
170 ** VARNAME.
171 **
172 ** The caller can wait for the script to terminate using [vwait VARNAME].
173 */
sqlthread_spawn(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])174 static int SQLITE_TCLAPI sqlthread_spawn(
175 ClientData clientData,
176 Tcl_Interp *interp,
177 int objc,
178 Tcl_Obj *CONST objv[]
179 ){
180 Tcl_ThreadId x;
181 SqlThread *pNew;
182 int rc;
183
184 int nVarname; char *zVarname;
185 int nScript; char *zScript;
186
187 /* Parameters for thread creation */
188 const int nStack = TCL_THREAD_STACK_DEFAULT;
189 const int flags = TCL_THREAD_NOFLAGS;
190
191 assert(objc==4);
192 UNUSED_PARAMETER(clientData);
193 UNUSED_PARAMETER(objc);
194
195 zVarname = Tcl_GetStringFromObj(objv[2], &nVarname);
196 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
197
198 pNew = (SqlThread *)ckalloc(sizeof(SqlThread)+nVarname+nScript+2);
199 pNew->zVarname = (char *)&pNew[1];
200 pNew->zScript = (char *)&pNew->zVarname[nVarname+1];
201 memcpy(pNew->zVarname, zVarname, nVarname+1);
202 memcpy(pNew->zScript, zScript, nScript+1);
203 pNew->parent = Tcl_GetCurrentThread();
204 pNew->interp = interp;
205
206 rc = Tcl_CreateThread(&x, tclScriptThread, (void *)pNew, nStack, flags);
207 if( rc!=TCL_OK ){
208 Tcl_AppendResult(interp, "Error in Tcl_CreateThread()", 0);
209 ckfree((char *)pNew);
210 return TCL_ERROR;
211 }
212
213 return TCL_OK;
214 }
215
216 /*
217 ** sqlthread parent SCRIPT
218 **
219 ** This can be called by spawned threads only. It sends the specified
220 ** script back to the parent thread for execution. The result of
221 ** evaluating the SCRIPT is returned. The parent thread must enter
222 ** the event loop for this to work - otherwise the caller will
223 ** block indefinitely.
224 **
225 ** NOTE: At the moment, this doesn't work. FIXME.
226 */
sqlthread_parent(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])227 static int SQLITE_TCLAPI sqlthread_parent(
228 ClientData clientData,
229 Tcl_Interp *interp,
230 int objc,
231 Tcl_Obj *CONST objv[]
232 ){
233 EvalEvent *pEvent;
234 char *zMsg;
235 int nMsg;
236 SqlThread *p = (SqlThread *)clientData;
237
238 assert(objc==3);
239 UNUSED_PARAMETER(objc);
240
241 if( p==0 ){
242 Tcl_AppendResult(interp, "no parent thread", 0);
243 return TCL_ERROR;
244 }
245
246 zMsg = Tcl_GetStringFromObj(objv[2], &nMsg);
247 pEvent = (EvalEvent *)ckalloc(sizeof(EvalEvent)+nMsg+1);
248 pEvent->base.nextPtr = 0;
249 pEvent->base.proc = tclScriptEvent;
250 pEvent->zScript = (char *)&pEvent[1];
251 memcpy(pEvent->zScript, zMsg, nMsg+1);
252 pEvent->interp = p->interp;
253 Tcl_ThreadQueueEvent(p->parent, (Tcl_Event *)pEvent, TCL_QUEUE_TAIL);
254 Tcl_ThreadAlert(p->parent);
255
256 return TCL_OK;
257 }
258
xBusy(void * pArg,int nBusy)259 static int xBusy(void *pArg, int nBusy){
260 UNUSED_PARAMETER(pArg);
261 UNUSED_PARAMETER(nBusy);
262 sqlite3_sleep(50);
263 return 1; /* Try again... */
264 }
265
266 /*
267 ** sqlthread open
268 **
269 ** Open a database handle and return the string representation of
270 ** the pointer value.
271 */
sqlthread_open(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])272 static int SQLITE_TCLAPI sqlthread_open(
273 ClientData clientData,
274 Tcl_Interp *interp,
275 int objc,
276 Tcl_Obj *CONST objv[]
277 ){
278 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p);
279
280 const char *zFilename;
281 sqlite3 *db;
282 char zBuf[100];
283 extern int Md5_Register(sqlite3*,char**,const sqlite3_api_routines*);
284
285 UNUSED_PARAMETER(clientData);
286 UNUSED_PARAMETER(objc);
287
288 zFilename = Tcl_GetString(objv[2]);
289 sqlite3_open(zFilename, &db);
290 Md5_Register(db, 0, 0);
291 sqlite3_busy_handler(db, xBusy, 0);
292
293 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
294 Tcl_AppendResult(interp, zBuf, 0);
295
296 return TCL_OK;
297 }
298
299
300 /*
301 ** sqlthread open
302 **
303 ** Return the current thread-id (Tcl_GetCurrentThread()) cast to
304 ** an integer.
305 */
sqlthread_id(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])306 static int SQLITE_TCLAPI sqlthread_id(
307 ClientData clientData,
308 Tcl_Interp *interp,
309 int objc,
310 Tcl_Obj *CONST objv[]
311 ){
312 Tcl_ThreadId id = Tcl_GetCurrentThread();
313 Tcl_SetObjResult(interp, Tcl_NewIntObj(SQLITE_PTR_TO_INT(id)));
314 UNUSED_PARAMETER(clientData);
315 UNUSED_PARAMETER(objc);
316 UNUSED_PARAMETER(objv);
317 return TCL_OK;
318 }
319
320
321 /*
322 ** Dispatch routine for the sub-commands of [sqlthread].
323 */
sqlthread_proc(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])324 static int SQLITE_TCLAPI sqlthread_proc(
325 ClientData clientData,
326 Tcl_Interp *interp,
327 int objc,
328 Tcl_Obj *CONST objv[]
329 ){
330 struct SubCommand {
331 char *zName;
332 Tcl_ObjCmdProc *xProc;
333 int nArg;
334 char *zUsage;
335 } aSub[] = {
336 {"parent", sqlthread_parent, 1, "SCRIPT"},
337 {"spawn", sqlthread_spawn, 2, "VARNAME SCRIPT"},
338 {"open", sqlthread_open, 1, "DBNAME"},
339 {"id", sqlthread_id, 0, ""},
340 {0, 0, 0}
341 };
342 struct SubCommand *pSub;
343 int rc;
344 int iIndex;
345
346 if( objc<2 ){
347 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND");
348 return TCL_ERROR;
349 }
350
351 rc = Tcl_GetIndexFromObjStruct(
352 interp, objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iIndex
353 );
354 if( rc!=TCL_OK ) return rc;
355 pSub = &aSub[iIndex];
356
357 if( objc<(pSub->nArg+2) ){
358 Tcl_WrongNumArgs(interp, 2, objv, pSub->zUsage);
359 return TCL_ERROR;
360 }
361
362 return pSub->xProc(clientData, interp, objc, objv);
363 }
364
365 /*
366 ** The [clock_seconds] command. This is more or less the same as the
367 ** regular tcl [clock seconds], except that it is available in testfixture
368 ** when linked against both Tcl 8.4 and 8.5. Because [clock seconds] is
369 ** implemented as a script in Tcl 8.5, it is not usually available to
370 ** testfixture.
371 */
clock_seconds_proc(ClientData clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])372 static int SQLITE_TCLAPI clock_seconds_proc(
373 ClientData clientData,
374 Tcl_Interp *interp,
375 int objc,
376 Tcl_Obj *CONST objv[]
377 ){
378 Tcl_Time now;
379 Tcl_GetTime(&now);
380 Tcl_SetObjResult(interp, Tcl_NewIntObj(now.sec));
381 UNUSED_PARAMETER(clientData);
382 UNUSED_PARAMETER(objc);
383 UNUSED_PARAMETER(objv);
384 return TCL_OK;
385 }
386
387 /*************************************************************************
388 ** This block contains the implementation of the [sqlite3_blocking_step]
389 ** command available to threads created by [sqlthread spawn] commands. It
390 ** is only available on UNIX for now. This is because pthread condition
391 ** variables are used.
392 **
393 ** The source code for the C functions sqlite3_blocking_step(),
394 ** blocking_step_notify() and the structure UnlockNotification is
395 ** automatically extracted from this file and used as part of the
396 ** documentation for the sqlite3_unlock_notify() API function. This
397 ** should be considered if these functions are to be extended (i.e. to
398 ** support windows) in the future.
399 */
400 #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
401
402 /* BEGIN_SQLITE_BLOCKING_STEP */
403 /* This example uses the pthreads API */
404 #include <pthread.h>
405
406 /*
407 ** A pointer to an instance of this structure is passed as the user-context
408 ** pointer when registering for an unlock-notify callback.
409 */
410 typedef struct UnlockNotification UnlockNotification;
411 struct UnlockNotification {
412 int fired; /* True after unlock event has occurred */
413 pthread_cond_t cond; /* Condition variable to wait on */
414 pthread_mutex_t mutex; /* Mutex to protect structure */
415 };
416
417 /*
418 ** This function is an unlock-notify callback registered with SQLite.
419 */
unlock_notify_cb(void ** apArg,int nArg)420 static void unlock_notify_cb(void **apArg, int nArg){
421 int i;
422 for(i=0; i<nArg; i++){
423 UnlockNotification *p = (UnlockNotification *)apArg[i];
424 pthread_mutex_lock(&p->mutex);
425 p->fired = 1;
426 pthread_cond_signal(&p->cond);
427 pthread_mutex_unlock(&p->mutex);
428 }
429 }
430
431 /*
432 ** This function assumes that an SQLite API call (either sqlite3_prepare_v2()
433 ** or sqlite3_step()) has just returned SQLITE_LOCKED. The argument is the
434 ** associated database connection.
435 **
436 ** This function calls sqlite3_unlock_notify() to register for an
437 ** unlock-notify callback, then blocks until that callback is delivered
438 ** and returns SQLITE_OK. The caller should then retry the failed operation.
439 **
440 ** Or, if sqlite3_unlock_notify() indicates that to block would deadlock
441 ** the system, then this function returns SQLITE_LOCKED immediately. In
442 ** this case the caller should not retry the operation and should roll
443 ** back the current transaction (if any).
444 */
wait_for_unlock_notify(sqlite3 * db)445 static int wait_for_unlock_notify(sqlite3 *db){
446 int rc;
447 UnlockNotification un;
448
449 /* Initialize the UnlockNotification structure. */
450 un.fired = 0;
451 pthread_mutex_init(&un.mutex, 0);
452 pthread_cond_init(&un.cond, 0);
453
454 /* Register for an unlock-notify callback. */
455 rc = sqlite3_unlock_notify(db, unlock_notify_cb, (void *)&un);
456 assert( rc==SQLITE_LOCKED || rc==SQLITE_OK );
457
458 /* The call to sqlite3_unlock_notify() always returns either SQLITE_LOCKED
459 ** or SQLITE_OK.
460 **
461 ** If SQLITE_LOCKED was returned, then the system is deadlocked. In this
462 ** case this function needs to return SQLITE_LOCKED to the caller so
463 ** that the current transaction can be rolled back. Otherwise, block
464 ** until the unlock-notify callback is invoked, then return SQLITE_OK.
465 */
466 if( rc==SQLITE_OK ){
467 pthread_mutex_lock(&un.mutex);
468 if( !un.fired ){
469 pthread_cond_wait(&un.cond, &un.mutex);
470 }
471 pthread_mutex_unlock(&un.mutex);
472 }
473
474 /* Destroy the mutex and condition variables. */
475 pthread_cond_destroy(&un.cond);
476 pthread_mutex_destroy(&un.mutex);
477
478 return rc;
479 }
480
481 /*
482 ** This function is a wrapper around the SQLite function sqlite3_step().
483 ** It functions in the same way as step(), except that if a required
484 ** shared-cache lock cannot be obtained, this function may block waiting for
485 ** the lock to become available. In this scenario the normal API step()
486 ** function always returns SQLITE_LOCKED.
487 **
488 ** If this function returns SQLITE_LOCKED, the caller should rollback
489 ** the current transaction (if any) and try again later. Otherwise, the
490 ** system may become deadlocked.
491 */
sqlite3_blocking_step(sqlite3_stmt * pStmt)492 int sqlite3_blocking_step(sqlite3_stmt *pStmt){
493 int rc;
494 while( SQLITE_LOCKED==(rc = sqlite3_step(pStmt)) ){
495 rc = wait_for_unlock_notify(sqlite3_db_handle(pStmt));
496 if( rc!=SQLITE_OK ) break;
497 sqlite3_reset(pStmt);
498 }
499 return rc;
500 }
501
502 /*
503 ** This function is a wrapper around the SQLite function sqlite3_prepare_v2().
504 ** It functions in the same way as prepare_v2(), except that if a required
505 ** shared-cache lock cannot be obtained, this function may block waiting for
506 ** the lock to become available. In this scenario the normal API prepare_v2()
507 ** function always returns SQLITE_LOCKED.
508 **
509 ** If this function returns SQLITE_LOCKED, the caller should rollback
510 ** the current transaction (if any) and try again later. Otherwise, the
511 ** system may become deadlocked.
512 */
sqlite3_blocking_prepare_v2(sqlite3 * db,const char * zSql,int nSql,sqlite3_stmt ** ppStmt,const char ** pz)513 int sqlite3_blocking_prepare_v2(
514 sqlite3 *db, /* Database handle. */
515 const char *zSql, /* UTF-8 encoded SQL statement. */
516 int nSql, /* Length of zSql in bytes. */
517 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
518 const char **pz /* OUT: End of parsed string */
519 ){
520 int rc;
521 while( SQLITE_LOCKED==(rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, pz)) ){
522 rc = wait_for_unlock_notify(db);
523 if( rc!=SQLITE_OK ) break;
524 }
525 return rc;
526 }
527 /* END_SQLITE_BLOCKING_STEP */
528
529 /*
530 ** Usage: sqlite3_blocking_step STMT
531 **
532 ** Advance the statement to the next row.
533 */
blocking_step_proc(void * clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])534 static int SQLITE_TCLAPI blocking_step_proc(
535 void * clientData,
536 Tcl_Interp *interp,
537 int objc,
538 Tcl_Obj *CONST objv[]
539 ){
540
541 sqlite3_stmt *pStmt;
542 int rc;
543
544 if( objc!=2 ){
545 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
546 return TCL_ERROR;
547 }
548
549 pStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
550 rc = sqlite3_blocking_step(pStmt);
551
552 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), 0);
553 return TCL_OK;
554 }
555
556 /*
557 ** Usage: sqlite3_blocking_prepare_v2 DB sql bytes ?tailvar?
558 ** Usage: sqlite3_nonblocking_prepare_v2 DB sql bytes ?tailvar?
559 */
blocking_prepare_v2_proc(void * clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])560 static int SQLITE_TCLAPI blocking_prepare_v2_proc(
561 void * clientData,
562 Tcl_Interp *interp,
563 int objc,
564 Tcl_Obj *CONST objv[]
565 ){
566 sqlite3 *db;
567 const char *zSql;
568 int bytes;
569 const char *zTail = 0;
570 sqlite3_stmt *pStmt = 0;
571 char zBuf[50];
572 int rc;
573 int isBlocking = !(clientData==0);
574
575 if( objc!=5 && objc!=4 ){
576 Tcl_AppendResult(interp, "wrong # args: should be \"",
577 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
578 return TCL_ERROR;
579 }
580 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
581 zSql = Tcl_GetString(objv[2]);
582 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
583
584 if( isBlocking ){
585 rc = sqlite3_blocking_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
586 }else{
587 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
588 }
589
590 assert(rc==SQLITE_OK || pStmt==0);
591 if( zTail && objc>=5 ){
592 if( bytes>=0 ){
593 bytes = bytes - (zTail-zSql);
594 }
595 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
596 }
597 if( rc!=SQLITE_OK ){
598 assert( pStmt==0 );
599 sqlite3_snprintf(sizeof(zBuf), zBuf, "%s ", (char *)sqlite3ErrName(rc));
600 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
601 return TCL_ERROR;
602 }
603
604 if( pStmt ){
605 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
606 Tcl_AppendResult(interp, zBuf, 0);
607 }
608 return TCL_OK;
609 }
610
611 #endif /* SQLITE_OS_UNIX && SQLITE_ENABLE_UNLOCK_NOTIFY */
612 /*
613 ** End of implementation of [sqlite3_blocking_step].
614 ************************************************************************/
615
616 /*
617 ** Register commands with the TCL interpreter.
618 */
SqlitetestThread_Init(Tcl_Interp * interp)619 int SqlitetestThread_Init(Tcl_Interp *interp){
620 Tcl_CreateObjCommand(interp, "sqlthread", sqlthread_proc, 0, 0);
621 Tcl_CreateObjCommand(interp, "clock_seconds", clock_seconds_proc, 0, 0);
622 #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
623 Tcl_CreateObjCommand(interp, "sqlite3_blocking_step", blocking_step_proc,0,0);
624 Tcl_CreateObjCommand(interp,
625 "sqlite3_blocking_prepare_v2", blocking_prepare_v2_proc, (void *)1, 0);
626 Tcl_CreateObjCommand(interp,
627 "sqlite3_nonblocking_prepare_v2", blocking_prepare_v2_proc, 0, 0);
628 #endif
629 return TCL_OK;
630 }
631 #else
SqlitetestThread_Init(Tcl_Interp * interp)632 int SqlitetestThread_Init(Tcl_Interp *interp){
633 return TCL_OK;
634 }
635 #endif
636