Snapshot of upstream SQLite 3.8.8.3
[sqlcipher.git] / src / test7.c
blob93bf1e4898b004ce10dc131a1f4258b8a3f20aaf
1 /*
2 ** 2006 January 09
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.
11 *************************************************************************
12 ** Code for testing the client/server version of the SQLite library.
13 ** Derived from test4.c.
15 #include "sqliteInt.h"
16 #include "tcl.h"
19 ** This test only works on UNIX with a SQLITE_THREADSAFE build that includes
20 ** the SQLITE_SERVER option.
22 #if defined(SQLITE_SERVER) && !defined(SQLITE_OMIT_SHARED_CACHE) && \
23 SQLITE_OS_UNIX && SQLITE_THREADSAFE
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <sched.h>
29 #include <ctype.h>
32 ** Interfaces defined in server.c
34 int sqlite3_client_open(const char*, sqlite3**);
35 int sqlite3_client_prepare(sqlite3*,const char*,int,
36 sqlite3_stmt**,const char**);
37 int sqlite3_client_step(sqlite3_stmt*);
38 int sqlite3_client_reset(sqlite3_stmt*);
39 int sqlite3_client_finalize(sqlite3_stmt*);
40 int sqlite3_client_close(sqlite3*);
41 int sqlite3_server_start(void);
42 int sqlite3_server_stop(void);
43 void sqlite3_server_start2(int *pnDecr);
46 ** Each thread is controlled by an instance of the following
47 ** structure.
49 typedef struct Thread Thread;
50 struct Thread {
51 /* The first group of fields are writable by the supervisor thread
52 ** and read-only to the client threads
54 char *zFilename; /* Name of database file */
55 void (*xOp)(Thread*); /* next operation to do */
56 char *zArg; /* argument usable by xOp */
57 volatile int opnum; /* Operation number */
58 volatile int busy; /* True if this thread is in use */
60 /* The next group of fields are writable by the client threads
61 ** but read-only to the superviser thread.
63 volatile int completed; /* Number of operations completed */
64 sqlite3 *db; /* Open database */
65 sqlite3_stmt *pStmt; /* Pending operation */
66 char *zErr; /* operation error */
67 char *zStaticErr; /* Static error message */
68 int rc; /* operation return code */
69 int argc; /* number of columns in result */
70 const char *argv[100]; /* result columns */
71 const char *colv[100]; /* result column names */
73 /* Initialized to 1 by the supervisor thread when the client is
74 ** created, and then deemed read-only to the supervisor thread.
75 ** Is set to 0 by the server thread belonging to this client
76 ** just before it exits.
78 int nServer; /* Number of server threads running */
82 ** There can be as many as 26 threads running at once. Each is named
83 ** by a capital letter: A, B, C, ..., Y, Z.
85 #define N_THREAD 26
86 static Thread threadset[N_THREAD];
89 ** The main loop for a thread. Threads use busy waiting.
91 static void *client_main(void *pArg){
92 Thread *p = (Thread*)pArg;
93 if( p->db ){
94 sqlite3_client_close(p->db);
96 sqlite3_client_open(p->zFilename, &p->db);
97 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
98 p->zErr = strdup(sqlite3_errmsg(p->db));
99 sqlite3_client_close(p->db);
100 p->db = 0;
102 p->pStmt = 0;
103 p->completed = 1;
104 while( p->opnum<=p->completed ) sched_yield();
105 while( p->xOp ){
106 if( p->zErr && p->zErr!=p->zStaticErr ){
107 sqlite3_free(p->zErr);
108 p->zErr = 0;
110 (*p->xOp)(p);
111 p->completed++;
112 while( p->opnum<=p->completed ) sched_yield();
114 if( p->pStmt ){
115 sqlite3_client_finalize(p->pStmt);
116 p->pStmt = 0;
118 if( p->db ){
119 sqlite3_client_close(p->db);
120 p->db = 0;
122 if( p->zErr && p->zErr!=p->zStaticErr ){
123 sqlite3_free(p->zErr);
124 p->zErr = 0;
126 p->completed++;
127 #ifndef SQLITE_OMIT_DEPRECATED
128 sqlite3_thread_cleanup();
129 #endif
130 return 0;
134 ** Get a thread ID which is an upper case letter. Return the index.
135 ** If the argument is not a valid thread ID put an error message in
136 ** the interpreter and return -1.
138 static int parse_client_id(Tcl_Interp *interp, const char *zArg){
139 if( zArg==0 || zArg[0]==0 || zArg[1]!=0 || !isupper((unsigned char)zArg[0]) ){
140 Tcl_AppendResult(interp, "thread ID must be an upper case letter", 0);
141 return -1;
143 return zArg[0] - 'A';
147 ** Usage: client_create NAME FILENAME
149 ** NAME should be an upper case letter. Start the thread running with
150 ** an open connection to the given database.
152 static int tcl_client_create(
153 void *NotUsed,
154 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
155 int argc, /* Number of arguments */
156 const char **argv /* Text of each argument */
158 int i;
159 pthread_t x;
160 int rc;
162 if( argc!=3 ){
163 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
164 " ID FILENAME", 0);
165 return TCL_ERROR;
167 i = parse_client_id(interp, argv[1]);
168 if( i<0 ) return TCL_ERROR;
169 if( threadset[i].busy ){
170 Tcl_AppendResult(interp, "thread ", argv[1], " is already running", 0);
171 return TCL_ERROR;
173 threadset[i].busy = 1;
174 sqlite3_free(threadset[i].zFilename);
175 threadset[i].zFilename = sqlite3_mprintf("%s", argv[2]);
176 threadset[i].opnum = 1;
177 threadset[i].completed = 0;
178 rc = pthread_create(&x, 0, client_main, &threadset[i]);
179 if( rc ){
180 Tcl_AppendResult(interp, "failed to create the thread", 0);
181 sqlite3_free(threadset[i].zFilename);
182 threadset[i].busy = 0;
183 return TCL_ERROR;
185 pthread_detach(x);
186 if( threadset[i].nServer==0 ){
187 threadset[i].nServer = 1;
188 sqlite3_server_start2(&threadset[i].nServer);
190 return TCL_OK;
194 ** Wait for a thread to reach its idle state.
196 static void client_wait(Thread *p){
197 while( p->opnum>p->completed ) sched_yield();
201 ** Usage: client_wait ID
203 ** Wait on thread ID to reach its idle state.
205 static int tcl_client_wait(
206 void *NotUsed,
207 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
208 int argc, /* Number of arguments */
209 const char **argv /* Text of each argument */
211 int i;
213 if( argc!=2 ){
214 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
215 " ID", 0);
216 return TCL_ERROR;
218 i = parse_client_id(interp, argv[1]);
219 if( i<0 ) return TCL_ERROR;
220 if( !threadset[i].busy ){
221 Tcl_AppendResult(interp, "no such thread", 0);
222 return TCL_ERROR;
224 client_wait(&threadset[i]);
225 return TCL_OK;
229 ** Stop a thread.
231 static void stop_thread(Thread *p){
232 client_wait(p);
233 p->xOp = 0;
234 p->opnum++;
235 client_wait(p);
236 sqlite3_free(p->zArg);
237 p->zArg = 0;
238 sqlite3_free(p->zFilename);
239 p->zFilename = 0;
240 p->busy = 0;
244 ** Usage: client_halt ID
246 ** Cause a client thread to shut itself down. Wait for the shutdown to be
247 ** completed. If ID is "*" then stop all client threads.
249 static int tcl_client_halt(
250 void *NotUsed,
251 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
252 int argc, /* Number of arguments */
253 const char **argv /* Text of each argument */
255 int i;
257 if( argc!=2 ){
258 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
259 " ID", 0);
260 return TCL_ERROR;
262 if( argv[1][0]=='*' && argv[1][1]==0 ){
263 for(i=0; i<N_THREAD; i++){
264 if( threadset[i].busy ){
265 stop_thread(&threadset[i]);
268 }else{
269 i = parse_client_id(interp, argv[1]);
270 if( i<0 ) return TCL_ERROR;
271 if( !threadset[i].busy ){
272 Tcl_AppendResult(interp, "no such thread", 0);
273 return TCL_ERROR;
275 stop_thread(&threadset[i]);
278 /* If no client threads are still running, also stop the server */
279 for(i=0; i<N_THREAD && threadset[i].busy==0; i++){}
280 if( i>=N_THREAD ){
281 sqlite3_server_stop();
282 while( 1 ){
283 for(i=0; i<N_THREAD && threadset[i].nServer==0; i++);
284 if( i==N_THREAD ) break;
285 sched_yield();
288 return TCL_OK;
292 ** Usage: client_argc ID
294 ** Wait on the most recent client_step to complete, then return the
295 ** number of columns in the result set.
297 static int tcl_client_argc(
298 void *NotUsed,
299 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
300 int argc, /* Number of arguments */
301 const char **argv /* Text of each argument */
303 int i;
304 char zBuf[100];
306 if( argc!=2 ){
307 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
308 " ID", 0);
309 return TCL_ERROR;
311 i = parse_client_id(interp, argv[1]);
312 if( i<0 ) return TCL_ERROR;
313 if( !threadset[i].busy ){
314 Tcl_AppendResult(interp, "no such thread", 0);
315 return TCL_ERROR;
317 client_wait(&threadset[i]);
318 sprintf(zBuf, "%d", threadset[i].argc);
319 Tcl_AppendResult(interp, zBuf, 0);
320 return TCL_OK;
324 ** Usage: client_argv ID N
326 ** Wait on the most recent client_step to complete, then return the
327 ** value of the N-th columns in the result set.
329 static int tcl_client_argv(
330 void *NotUsed,
331 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
332 int argc, /* Number of arguments */
333 const char **argv /* Text of each argument */
335 int i;
336 int n;
338 if( argc!=3 ){
339 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
340 " ID N", 0);
341 return TCL_ERROR;
343 i = parse_client_id(interp, argv[1]);
344 if( i<0 ) return TCL_ERROR;
345 if( !threadset[i].busy ){
346 Tcl_AppendResult(interp, "no such thread", 0);
347 return TCL_ERROR;
349 if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR;
350 client_wait(&threadset[i]);
351 if( n<0 || n>=threadset[i].argc ){
352 Tcl_AppendResult(interp, "column number out of range", 0);
353 return TCL_ERROR;
355 Tcl_AppendResult(interp, threadset[i].argv[n], 0);
356 return TCL_OK;
360 ** Usage: client_colname ID N
362 ** Wait on the most recent client_step to complete, then return the
363 ** name of the N-th columns in the result set.
365 static int tcl_client_colname(
366 void *NotUsed,
367 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
368 int argc, /* Number of arguments */
369 const char **argv /* Text of each argument */
371 int i;
372 int n;
374 if( argc!=3 ){
375 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
376 " ID N", 0);
377 return TCL_ERROR;
379 i = parse_client_id(interp, argv[1]);
380 if( i<0 ) return TCL_ERROR;
381 if( !threadset[i].busy ){
382 Tcl_AppendResult(interp, "no such thread", 0);
383 return TCL_ERROR;
385 if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR;
386 client_wait(&threadset[i]);
387 if( n<0 || n>=threadset[i].argc ){
388 Tcl_AppendResult(interp, "column number out of range", 0);
389 return TCL_ERROR;
391 Tcl_AppendResult(interp, threadset[i].colv[n], 0);
392 return TCL_OK;
395 extern const char *sqlite3ErrName(int);
398 ** Usage: client_result ID
400 ** Wait on the most recent operation to complete, then return the
401 ** result code from that operation.
403 static int tcl_client_result(
404 void *NotUsed,
405 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
406 int argc, /* Number of arguments */
407 const char **argv /* Text of each argument */
409 int i;
410 const char *zName;
412 if( argc!=2 ){
413 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
414 " ID", 0);
415 return TCL_ERROR;
417 i = parse_client_id(interp, argv[1]);
418 if( i<0 ) return TCL_ERROR;
419 if( !threadset[i].busy ){
420 Tcl_AppendResult(interp, "no such thread", 0);
421 return TCL_ERROR;
423 client_wait(&threadset[i]);
424 zName = sqlite3ErrName(threadset[i].rc);
425 Tcl_AppendResult(interp, zName, 0);
426 return TCL_OK;
430 ** Usage: client_error ID
432 ** Wait on the most recent operation to complete, then return the
433 ** error string.
435 static int tcl_client_error(
436 void *NotUsed,
437 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
438 int argc, /* Number of arguments */
439 const char **argv /* Text of each argument */
441 int i;
443 if( argc!=2 ){
444 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
445 " ID", 0);
446 return TCL_ERROR;
448 i = parse_client_id(interp, argv[1]);
449 if( i<0 ) return TCL_ERROR;
450 if( !threadset[i].busy ){
451 Tcl_AppendResult(interp, "no such thread", 0);
452 return TCL_ERROR;
454 client_wait(&threadset[i]);
455 Tcl_AppendResult(interp, threadset[i].zErr, 0);
456 return TCL_OK;
460 ** This procedure runs in the thread to compile an SQL statement.
462 static void do_compile(Thread *p){
463 if( p->db==0 ){
464 p->zErr = p->zStaticErr = "no database is open";
465 p->rc = SQLITE_ERROR;
466 return;
468 if( p->pStmt ){
469 sqlite3_client_finalize(p->pStmt);
470 p->pStmt = 0;
472 p->rc = sqlite3_client_prepare(p->db, p->zArg, -1, &p->pStmt, 0);
476 ** Usage: client_compile ID SQL
478 ** Compile a new virtual machine.
480 static int tcl_client_compile(
481 void *NotUsed,
482 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
483 int argc, /* Number of arguments */
484 const char **argv /* Text of each argument */
486 int i;
487 if( argc!=3 ){
488 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
489 " ID SQL", 0);
490 return TCL_ERROR;
492 i = parse_client_id(interp, argv[1]);
493 if( i<0 ) return TCL_ERROR;
494 if( !threadset[i].busy ){
495 Tcl_AppendResult(interp, "no such thread", 0);
496 return TCL_ERROR;
498 client_wait(&threadset[i]);
499 threadset[i].xOp = do_compile;
500 sqlite3_free(threadset[i].zArg);
501 threadset[i].zArg = sqlite3_mprintf("%s", argv[2]);
502 threadset[i].opnum++;
503 return TCL_OK;
507 ** This procedure runs in the thread to step the virtual machine.
509 static void do_step(Thread *p){
510 int i;
511 if( p->pStmt==0 ){
512 p->zErr = p->zStaticErr = "no virtual machine available";
513 p->rc = SQLITE_ERROR;
514 return;
516 p->rc = sqlite3_client_step(p->pStmt);
517 if( p->rc==SQLITE_ROW ){
518 p->argc = sqlite3_column_count(p->pStmt);
519 for(i=0; i<sqlite3_data_count(p->pStmt); i++){
520 p->argv[i] = (char*)sqlite3_column_text(p->pStmt, i);
522 for(i=0; i<p->argc; i++){
523 p->colv[i] = sqlite3_column_name(p->pStmt, i);
529 ** Usage: client_step ID
531 ** Advance the virtual machine by one step
533 static int tcl_client_step(
534 void *NotUsed,
535 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
536 int argc, /* Number of arguments */
537 const char **argv /* Text of each argument */
539 int i;
540 if( argc!=2 ){
541 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
542 " IDL", 0);
543 return TCL_ERROR;
545 i = parse_client_id(interp, argv[1]);
546 if( i<0 ) return TCL_ERROR;
547 if( !threadset[i].busy ){
548 Tcl_AppendResult(interp, "no such thread", 0);
549 return TCL_ERROR;
551 client_wait(&threadset[i]);
552 threadset[i].xOp = do_step;
553 threadset[i].opnum++;
554 return TCL_OK;
558 ** This procedure runs in the thread to finalize a virtual machine.
560 static void do_finalize(Thread *p){
561 if( p->pStmt==0 ){
562 p->zErr = p->zStaticErr = "no virtual machine available";
563 p->rc = SQLITE_ERROR;
564 return;
566 p->rc = sqlite3_client_finalize(p->pStmt);
567 p->pStmt = 0;
571 ** Usage: client_finalize ID
573 ** Finalize the virtual machine.
575 static int tcl_client_finalize(
576 void *NotUsed,
577 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
578 int argc, /* Number of arguments */
579 const char **argv /* Text of each argument */
581 int i;
582 if( argc!=2 ){
583 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
584 " IDL", 0);
585 return TCL_ERROR;
587 i = parse_client_id(interp, argv[1]);
588 if( i<0 ) return TCL_ERROR;
589 if( !threadset[i].busy ){
590 Tcl_AppendResult(interp, "no such thread", 0);
591 return TCL_ERROR;
593 client_wait(&threadset[i]);
594 threadset[i].xOp = do_finalize;
595 sqlite3_free(threadset[i].zArg);
596 threadset[i].zArg = 0;
597 threadset[i].opnum++;
598 return TCL_OK;
602 ** This procedure runs in the thread to reset a virtual machine.
604 static void do_reset(Thread *p){
605 if( p->pStmt==0 ){
606 p->zErr = p->zStaticErr = "no virtual machine available";
607 p->rc = SQLITE_ERROR;
608 return;
610 p->rc = sqlite3_client_reset(p->pStmt);
611 p->pStmt = 0;
615 ** Usage: client_reset ID
617 ** Finalize the virtual machine.
619 static int tcl_client_reset(
620 void *NotUsed,
621 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
622 int argc, /* Number of arguments */
623 const char **argv /* Text of each argument */
625 int i;
626 if( argc!=2 ){
627 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
628 " IDL", 0);
629 return TCL_ERROR;
631 i = parse_client_id(interp, argv[1]);
632 if( i<0 ) return TCL_ERROR;
633 if( !threadset[i].busy ){
634 Tcl_AppendResult(interp, "no such thread", 0);
635 return TCL_ERROR;
637 client_wait(&threadset[i]);
638 threadset[i].xOp = do_reset;
639 sqlite3_free(threadset[i].zArg);
640 threadset[i].zArg = 0;
641 threadset[i].opnum++;
642 return TCL_OK;
646 ** Usage: client_swap ID ID
648 ** Interchange the sqlite* pointer between two threads.
650 static int tcl_client_swap(
651 void *NotUsed,
652 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
653 int argc, /* Number of arguments */
654 const char **argv /* Text of each argument */
656 int i, j;
657 sqlite3 *temp;
658 if( argc!=3 ){
659 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
660 " ID1 ID2", 0);
661 return TCL_ERROR;
663 i = parse_client_id(interp, argv[1]);
664 if( i<0 ) return TCL_ERROR;
665 if( !threadset[i].busy ){
666 Tcl_AppendResult(interp, "no such thread", 0);
667 return TCL_ERROR;
669 client_wait(&threadset[i]);
670 j = parse_client_id(interp, argv[2]);
671 if( j<0 ) return TCL_ERROR;
672 if( !threadset[j].busy ){
673 Tcl_AppendResult(interp, "no such thread", 0);
674 return TCL_ERROR;
676 client_wait(&threadset[j]);
677 temp = threadset[i].db;
678 threadset[i].db = threadset[j].db;
679 threadset[j].db = temp;
680 return TCL_OK;
684 ** Register commands with the TCL interpreter.
686 int Sqlitetest7_Init(Tcl_Interp *interp){
687 static struct {
688 char *zName;
689 Tcl_CmdProc *xProc;
690 } aCmd[] = {
691 { "client_create", (Tcl_CmdProc*)tcl_client_create },
692 { "client_wait", (Tcl_CmdProc*)tcl_client_wait },
693 { "client_halt", (Tcl_CmdProc*)tcl_client_halt },
694 { "client_argc", (Tcl_CmdProc*)tcl_client_argc },
695 { "client_argv", (Tcl_CmdProc*)tcl_client_argv },
696 { "client_colname", (Tcl_CmdProc*)tcl_client_colname },
697 { "client_result", (Tcl_CmdProc*)tcl_client_result },
698 { "client_error", (Tcl_CmdProc*)tcl_client_error },
699 { "client_compile", (Tcl_CmdProc*)tcl_client_compile },
700 { "client_step", (Tcl_CmdProc*)tcl_client_step },
701 { "client_reset", (Tcl_CmdProc*)tcl_client_reset },
702 { "client_finalize", (Tcl_CmdProc*)tcl_client_finalize },
703 { "client_swap", (Tcl_CmdProc*)tcl_client_swap },
705 int i;
707 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
708 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
710 return TCL_OK;
712 #else
713 int Sqlitetest7_Init(Tcl_Interp *interp){ return TCL_OK; }
714 #endif /* SQLITE_OS_UNIX */