blob: 5a9c8bf6b4e56b527eb6b06f841f5f26d847d322 [file] [log] [blame]
drh27338e62013-04-06 00:19:37 +00001/*
2** 2013-04-05
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 is a program used for testing SQLite, and specifically for testing
14** the ability of independent processes to access the same SQLite database
15** concurrently.
16**
17** Compile this program as follows:
18**
19** gcc -g -c -Wall sqlite3.c $(OPTS)
20** gcc -g -o mptest mptest.c sqlite3.o $(LIBS)
21**
22** Recommended options:
23**
24** -DHAVE_USLEEP
25** -DSQLITE_NO_SYNC
26** -DSQLITE_THREADSAFE=0
27** -DSQLITE_OMIT_LOAD_EXTENSION
28**
29** Run like this:
30**
31** ./mptest $database $script
32**
33** where $database is the database to use for testing and $script is a
34** test script.
35*/
36#include "sqlite3.h"
37#include <stdio.h>
drhbc94dbb2013-04-08 14:28:33 +000038#if defined(_WIN32)
mistachkin08d41892013-04-11 00:09:44 +000039# define WIN32_LEAN_AND_MEAN
40# include <windows.h>
drhbc94dbb2013-04-08 14:28:33 +000041#else
42# include <unistd.h>
43#endif
drh27338e62013-04-06 00:19:37 +000044#include <stdlib.h>
45#include <string.h>
46#include <assert.h>
47#include <ctype.h>
48
drhc56fac72015-10-29 13:48:15 +000049#define ISSPACE(X) isspace((unsigned char)(X))
50#define ISDIGIT(X) isdigit((unsigned char)(X))
51
mistachkin08d41892013-04-11 00:09:44 +000052/* The suffix to append to the child command lines, if any */
53#if defined(_WIN32)
mistachkinfdd72c92013-04-11 21:13:10 +000054# define GETPID (int)GetCurrentProcessId
mistachkin08d41892013-04-11 00:09:44 +000055#else
mistachkinfdd72c92013-04-11 21:13:10 +000056# define GETPID getpid
mistachkin08d41892013-04-11 00:09:44 +000057#endif
58
mistachkin25a72de2015-03-31 17:58:13 +000059/* The directory separator character(s) */
60#if defined(_WIN32)
61# define isDirSep(c) (((c) == '/') || ((c) == '\\'))
62#else
63# define isDirSep(c) ((c) == '/')
64#endif
65
drh841810c2013-04-08 13:59:11 +000066/* Mark a parameter as unused to suppress compiler warnings */
67#define UNUSED_PARAMETER(x) (void)x
68
drh27338e62013-04-06 00:19:37 +000069/* Global data
70*/
71static struct Global {
72 char *argv0; /* Name of the executable */
73 const char *zVfs; /* Name of VFS to use. Often NULL meaning "default" */
74 char *zDbFile; /* Name of the database */
75 sqlite3 *db; /* Open connection to database */
76 char *zErrLog; /* Filename for error log */
77 FILE *pErrLog; /* Where to write errors */
78 char *zLog; /* Name of output log file */
79 FILE *pLog; /* Where to write log messages */
drhe3be8c82013-04-11 11:53:45 +000080 char zName[32]; /* Symbolic name of this process */
drh27338e62013-04-06 00:19:37 +000081 int taskId; /* Task ID. 0 means supervisor. */
82 int iTrace; /* Tracing level */
83 int bSqlTrace; /* True to trace SQL commands */
drhbc082812013-04-18 15:11:03 +000084 int bIgnoreSqlErrors; /* Ignore errors in SQL statements */
drh27338e62013-04-06 00:19:37 +000085 int nError; /* Number of errors */
drh3f5bc382013-04-06 13:09:11 +000086 int nTest; /* Number of --match operators */
87 int iTimeout; /* Milliseconds until a busy timeout */
drhbc94dbb2013-04-08 14:28:33 +000088 int bSync; /* Call fsync() */
drh27338e62013-04-06 00:19:37 +000089} g;
90
drh3f5bc382013-04-06 13:09:11 +000091/* Default timeout */
92#define DEFAULT_TIMEOUT 10000
93
drh27338e62013-04-06 00:19:37 +000094/*
95** Print a message adding zPrefix[] to the beginning of every line.
96*/
97static void printWithPrefix(FILE *pOut, const char *zPrefix, const char *zMsg){
98 while( zMsg && zMsg[0] ){
99 int i;
100 for(i=0; zMsg[i] && zMsg[i]!='\n' && zMsg[i]!='\r'; i++){}
101 fprintf(pOut, "%s%.*s\n", zPrefix, i, zMsg);
102 zMsg += i;
103 while( zMsg[0]=='\n' || zMsg[0]=='\r' ) zMsg++;
104 }
105}
106
107/*
108** Compare two pointers to strings, where the pointers might be NULL.
109*/
110static int safe_strcmp(const char *a, const char *b){
111 if( a==b ) return 0;
112 if( a==0 ) return -1;
113 if( b==0 ) return 1;
114 return strcmp(a,b);
115}
116
117/*
118** Return TRUE if string z[] matches glob pattern zGlob[].
119** Return FALSE if the pattern does not match.
120**
121** Globbing rules:
122**
123** '*' Matches any sequence of zero or more characters.
124**
125** '?' Matches exactly one character.
126**
127** [...] Matches one character from the enclosed list of
128** characters.
129**
130** [^...] Matches one character not in the enclosed list.
131**
132** '#' Matches any sequence of one or more digits with an
133** optional + or - sign in front
134*/
135int strglob(const char *zGlob, const char *z){
136 int c, c2;
137 int invert;
138 int seen;
139
140 while( (c = (*(zGlob++)))!=0 ){
141 if( c=='*' ){
142 while( (c=(*(zGlob++))) == '*' || c=='?' ){
143 if( c=='?' && (*(z++))==0 ) return 0;
144 }
145 if( c==0 ){
146 return 1;
147 }else if( c=='[' ){
148 while( *z && strglob(zGlob-1,z) ){
149 z++;
150 }
151 return (*z)!=0;
152 }
153 while( (c2 = (*(z++)))!=0 ){
154 while( c2!=c ){
155 c2 = *(z++);
156 if( c2==0 ) return 0;
157 }
158 if( strglob(zGlob,z) ) return 1;
159 }
160 return 0;
161 }else if( c=='?' ){
162 if( (*(z++))==0 ) return 0;
163 }else if( c=='[' ){
164 int prior_c = 0;
165 seen = 0;
166 invert = 0;
167 c = *(z++);
168 if( c==0 ) return 0;
169 c2 = *(zGlob++);
170 if( c2=='^' ){
171 invert = 1;
172 c2 = *(zGlob++);
173 }
174 if( c2==']' ){
175 if( c==']' ) seen = 1;
176 c2 = *(zGlob++);
177 }
178 while( c2 && c2!=']' ){
179 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
180 c2 = *(zGlob++);
181 if( c>=prior_c && c<=c2 ) seen = 1;
182 prior_c = 0;
183 }else{
184 if( c==c2 ){
185 seen = 1;
186 }
187 prior_c = c2;
188 }
189 c2 = *(zGlob++);
190 }
191 if( c2==0 || (seen ^ invert)==0 ) return 0;
192 }else if( c=='#' ){
drhc56fac72015-10-29 13:48:15 +0000193 if( (z[0]=='-' || z[0]=='+') && ISDIGIT(z[1]) ) z++;
194 if( !ISDIGIT(z[0]) ) return 0;
drh27338e62013-04-06 00:19:37 +0000195 z++;
drhc56fac72015-10-29 13:48:15 +0000196 while( ISDIGIT(z[0]) ){ z++; }
drh27338e62013-04-06 00:19:37 +0000197 }else{
198 if( c!=(*(z++)) ) return 0;
199 }
200 }
201 return *z==0;
202}
203
204/*
205** Close output stream pOut if it is not stdout or stderr
206*/
207static void maybeClose(FILE *pOut){
208 if( pOut!=stdout && pOut!=stderr ) fclose(pOut);
209}
210
211/*
212** Print an error message
213*/
214static void errorMessage(const char *zFormat, ...){
215 va_list ap;
216 char *zMsg;
217 char zPrefix[30];
218 va_start(ap, zFormat);
219 zMsg = sqlite3_vmprintf(zFormat, ap);
220 va_end(ap);
221 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:ERROR: ", g.zName);
222 if( g.pLog ){
223 printWithPrefix(g.pLog, zPrefix, zMsg);
224 fflush(g.pLog);
225 }
226 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
227 printWithPrefix(g.pErrLog, zPrefix, zMsg);
228 fflush(g.pErrLog);
229 }
230 sqlite3_free(zMsg);
231 g.nError++;
232}
233
234/* Forward declaration */
235static int trySql(const char*, ...);
236
237/*
238** Print an error message and then quit.
239*/
240static void fatalError(const char *zFormat, ...){
241 va_list ap;
242 char *zMsg;
243 char zPrefix[30];
244 va_start(ap, zFormat);
245 zMsg = sqlite3_vmprintf(zFormat, ap);
246 va_end(ap);
247 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:FATAL: ", g.zName);
248 if( g.pLog ){
249 printWithPrefix(g.pLog, zPrefix, zMsg);
250 fflush(g.pLog);
251 maybeClose(g.pLog);
252 }
253 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
254 printWithPrefix(g.pErrLog, zPrefix, zMsg);
255 fflush(g.pErrLog);
256 maybeClose(g.pErrLog);
257 }
258 sqlite3_free(zMsg);
259 if( g.db ){
260 int nTry = 0;
drh3f5bc382013-04-06 13:09:11 +0000261 g.iTimeout = 0;
262 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY
263 && (nTry++)<100 ){
drh27338e62013-04-06 00:19:37 +0000264 sqlite3_sleep(10);
265 }
266 }
267 sqlite3_close(g.db);
268 exit(1);
269}
270
271
272/*
273** Print a log message
274*/
275static void logMessage(const char *zFormat, ...){
276 va_list ap;
277 char *zMsg;
278 char zPrefix[30];
279 va_start(ap, zFormat);
280 zMsg = sqlite3_vmprintf(zFormat, ap);
281 va_end(ap);
282 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s: ", g.zName);
283 if( g.pLog ){
284 printWithPrefix(g.pLog, zPrefix, zMsg);
285 fflush(g.pLog);
286 }
287 sqlite3_free(zMsg);
288}
289
290/*
291** Return the length of a string omitting trailing whitespace
292*/
293static int clipLength(const char *z){
294 int n = (int)strlen(z);
drhc56fac72015-10-29 13:48:15 +0000295 while( n>0 && ISSPACE(z[n-1]) ){ n--; }
drh27338e62013-04-06 00:19:37 +0000296 return n;
297}
298
299/*
drh1bf44c72013-04-08 13:48:29 +0000300** Auxiliary SQL function to return the name of the VFS
301*/
302static void vfsNameFunc(
303 sqlite3_context *context,
304 int argc,
305 sqlite3_value **argv
306){
307 sqlite3 *db = sqlite3_context_db_handle(context);
308 char *zVfs = 0;
drh841810c2013-04-08 13:59:11 +0000309 UNUSED_PARAMETER(argc);
310 UNUSED_PARAMETER(argv);
drh1bf44c72013-04-08 13:48:29 +0000311 sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zVfs);
312 if( zVfs ){
313 sqlite3_result_text(context, zVfs, -1, sqlite3_free);
314 }
315}
316
317/*
drh3f5bc382013-04-06 13:09:11 +0000318** Busy handler with a g.iTimeout-millisecond timeout
319*/
320static int busyHandler(void *pCD, int count){
drh841810c2013-04-08 13:59:11 +0000321 UNUSED_PARAMETER(pCD);
drh3f5bc382013-04-06 13:09:11 +0000322 if( count*10>g.iTimeout ){
323 if( g.iTimeout>0 ) errorMessage("timeout after %dms", g.iTimeout);
324 return 0;
325 }
326 sqlite3_sleep(10);
327 return 1;
328}
329
330/*
drh27338e62013-04-06 00:19:37 +0000331** SQL Trace callback
332*/
333static void sqlTraceCallback(void *NotUsed1, const char *zSql){
drh841810c2013-04-08 13:59:11 +0000334 UNUSED_PARAMETER(NotUsed1);
drh27338e62013-04-06 00:19:37 +0000335 logMessage("[%.*s]", clipLength(zSql), zSql);
336}
337
338/*
drh1790bb32013-04-06 14:30:29 +0000339** SQL error log callback
340*/
341static void sqlErrorCallback(void *pArg, int iErrCode, const char *zMsg){
drh841810c2013-04-08 13:59:11 +0000342 UNUSED_PARAMETER(pArg);
drhbc082812013-04-18 15:11:03 +0000343 if( iErrCode==SQLITE_ERROR && g.bIgnoreSqlErrors ) return;
drh1790bb32013-04-06 14:30:29 +0000344 if( (iErrCode&0xff)==SQLITE_SCHEMA && g.iTrace<3 ) return;
drhe5ebd222013-04-08 15:36:51 +0000345 if( g.iTimeout==0 && (iErrCode&0xff)==SQLITE_BUSY && g.iTrace<3 ) return;
drhe3be8c82013-04-11 11:53:45 +0000346 if( (iErrCode&0xff)==SQLITE_NOTICE ){
drhab755ac2013-04-09 18:36:36 +0000347 logMessage("(info) %s", zMsg);
348 }else{
349 errorMessage("(errcode=%d) %s", iErrCode, zMsg);
350 }
drh1790bb32013-04-06 14:30:29 +0000351}
352
353/*
drh27338e62013-04-06 00:19:37 +0000354** Prepare an SQL statement. Issue a fatal error if unable.
355*/
356static sqlite3_stmt *prepareSql(const char *zFormat, ...){
357 va_list ap;
358 char *zSql;
359 int rc;
360 sqlite3_stmt *pStmt = 0;
361 va_start(ap, zFormat);
362 zSql = sqlite3_vmprintf(zFormat, ap);
363 va_end(ap);
364 rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
365 if( rc!=SQLITE_OK ){
366 sqlite3_finalize(pStmt);
367 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
368 }
369 sqlite3_free(zSql);
370 return pStmt;
371}
372
373/*
374** Run arbitrary SQL. Issue a fatal error on failure.
375*/
376static void runSql(const char *zFormat, ...){
377 va_list ap;
378 char *zSql;
379 int rc;
380 va_start(ap, zFormat);
381 zSql = sqlite3_vmprintf(zFormat, ap);
382 va_end(ap);
383 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
384 if( rc!=SQLITE_OK ){
385 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
386 }
387 sqlite3_free(zSql);
388}
389
390/*
391** Try to run arbitrary SQL. Return success code.
392*/
393static int trySql(const char *zFormat, ...){
394 va_list ap;
395 char *zSql;
396 int rc;
397 va_start(ap, zFormat);
398 zSql = sqlite3_vmprintf(zFormat, ap);
399 va_end(ap);
400 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
401 sqlite3_free(zSql);
402 return rc;
403}
404
405/* Structure for holding an arbitrary length string
406*/
407typedef struct String String;
408struct String {
409 char *z; /* the string */
410 int n; /* Slots of z[] used */
411 int nAlloc; /* Slots of z[] allocated */
412};
413
414/* Free a string */
415static void stringFree(String *p){
416 if( p->z ) sqlite3_free(p->z);
417 memset(p, 0, sizeof(*p));
418}
419
420/* Append n bytes of text to a string. If n<0 append the entire string. */
421static void stringAppend(String *p, const char *z, int n){
422 if( n<0 ) n = (int)strlen(z);
423 if( p->n+n>=p->nAlloc ){
424 int nAlloc = p->nAlloc*2 + n + 100;
425 char *z = sqlite3_realloc(p->z, nAlloc);
426 if( z==0 ) fatalError("out of memory");
427 p->z = z;
428 p->nAlloc = nAlloc;
429 }
430 memcpy(p->z+p->n, z, n);
431 p->n += n;
432 p->z[p->n] = 0;
433}
434
435/* Reset a string to an empty string */
436static void stringReset(String *p){
437 if( p->z==0 ) stringAppend(p, " ", 1);
438 p->n = 0;
439 p->z[0] = 0;
440}
441
442/* Append a new token onto the end of the string */
443static void stringAppendTerm(String *p, const char *z){
444 int i;
445 if( p->n ) stringAppend(p, " ", 1);
446 if( z==0 ){
447 stringAppend(p, "nil", 3);
448 return;
449 }
drhc56fac72015-10-29 13:48:15 +0000450 for(i=0; z[i] && !ISSPACE(z[i]); i++){}
drh27338e62013-04-06 00:19:37 +0000451 if( i>0 && z[i]==0 ){
452 stringAppend(p, z, i);
453 return;
454 }
455 stringAppend(p, "'", 1);
456 while( z[0] ){
457 for(i=0; z[i] && z[i]!='\''; i++){}
458 if( z[i] ){
459 stringAppend(p, z, i+1);
460 stringAppend(p, "'", 1);
461 z += i+1;
462 }else{
463 stringAppend(p, z, i);
464 break;
465 }
466 }
467 stringAppend(p, "'", 1);
468}
469
470/*
471** Callback function for evalSql()
472*/
473static int evalCallback(void *pCData, int argc, char **argv, char **azCol){
474 String *p = (String*)pCData;
475 int i;
drh841810c2013-04-08 13:59:11 +0000476 UNUSED_PARAMETER(azCol);
drh27338e62013-04-06 00:19:37 +0000477 for(i=0; i<argc; i++) stringAppendTerm(p, argv[i]);
478 return 0;
479}
480
481/*
482** Run arbitrary SQL and record the results in an output string
483** given by the first parameter.
484*/
485static int evalSql(String *p, const char *zFormat, ...){
486 va_list ap;
487 char *zSql;
488 int rc;
489 char *zErrMsg = 0;
490 va_start(ap, zFormat);
491 zSql = sqlite3_vmprintf(zFormat, ap);
492 va_end(ap);
drh3f5bc382013-04-06 13:09:11 +0000493 assert( g.iTimeout>0 );
drh27338e62013-04-06 00:19:37 +0000494 rc = sqlite3_exec(g.db, zSql, evalCallback, p, &zErrMsg);
495 sqlite3_free(zSql);
496 if( rc ){
497 char zErr[30];
498 sqlite3_snprintf(sizeof(zErr), zErr, "error(%d)", rc);
499 stringAppendTerm(p, zErr);
500 if( zErrMsg ){
501 stringAppendTerm(p, zErrMsg);
502 sqlite3_free(zErrMsg);
503 }
504 }
505 return rc;
506}
507
508/*
drh1bf44c72013-04-08 13:48:29 +0000509** Auxiliary SQL function to recursively evaluate SQL.
510*/
511static void evalFunc(
512 sqlite3_context *context,
513 int argc,
514 sqlite3_value **argv
515){
516 sqlite3 *db = sqlite3_context_db_handle(context);
517 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
518 String res;
519 char *zErrMsg = 0;
520 int rc;
drh841810c2013-04-08 13:59:11 +0000521 UNUSED_PARAMETER(argc);
drh1bf44c72013-04-08 13:48:29 +0000522 memset(&res, 0, sizeof(res));
523 rc = sqlite3_exec(db, zSql, evalCallback, &res, &zErrMsg);
524 if( zErrMsg ){
525 sqlite3_result_error(context, zErrMsg, -1);
526 sqlite3_free(zErrMsg);
527 }else if( rc ){
528 sqlite3_result_error_code(context, rc);
529 }else{
530 sqlite3_result_text(context, res.z, -1, SQLITE_TRANSIENT);
531 }
532 stringFree(&res);
533}
534
535/*
drh27338e62013-04-06 00:19:37 +0000536** Look up the next task for client iClient in the database.
537** Return the task script and the task number and mark that
538** task as being under way.
539*/
540static int startScript(
541 int iClient, /* The client number */
542 char **pzScript, /* Write task script here */
drh4c5298f2013-04-10 12:01:21 +0000543 int *pTaskId, /* Write task number here */
544 char **pzTaskName /* Name of the task */
drh27338e62013-04-06 00:19:37 +0000545){
546 sqlite3_stmt *pStmt = 0;
547 int taskId;
548 int rc;
drh3f5bc382013-04-06 13:09:11 +0000549 int totalTime = 0;
drh27338e62013-04-06 00:19:37 +0000550
551 *pzScript = 0;
drh3f5bc382013-04-06 13:09:11 +0000552 g.iTimeout = 0;
drh27338e62013-04-06 00:19:37 +0000553 while(1){
drhf90e50f2013-04-08 19:13:48 +0000554 rc = trySql("BEGIN IMMEDIATE");
drh27338e62013-04-06 00:19:37 +0000555 if( rc==SQLITE_BUSY ){
556 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +0000557 totalTime += 10;
drh27338e62013-04-06 00:19:37 +0000558 continue;
559 }
560 if( rc!=SQLITE_OK ){
drh6adab7a2013-04-08 18:58:00 +0000561 fatalError("in startScript: %s", sqlite3_errmsg(g.db));
drh27338e62013-04-06 00:19:37 +0000562 }
drh3f5bc382013-04-06 13:09:11 +0000563 if( g.nError || g.nTest ){
564 runSql("UPDATE counters SET nError=nError+%d, nTest=nTest+%d",
565 g.nError, g.nTest);
drh27338e62013-04-06 00:19:37 +0000566 g.nError = 0;
drh3f5bc382013-04-06 13:09:11 +0000567 g.nTest = 0;
568 }
569 pStmt = prepareSql("SELECT 1 FROM client WHERE id=%d AND wantHalt",iClient);
570 rc = sqlite3_step(pStmt);
571 sqlite3_finalize(pStmt);
572 if( rc==SQLITE_ROW ){
573 runSql("DELETE FROM client WHERE id=%d", iClient);
drh3f5bc382013-04-06 13:09:11 +0000574 g.iTimeout = DEFAULT_TIMEOUT;
drhf90e50f2013-04-08 19:13:48 +0000575 runSql("COMMIT TRANSACTION;");
drh3f5bc382013-04-06 13:09:11 +0000576 return SQLITE_DONE;
drh27338e62013-04-06 00:19:37 +0000577 }
578 pStmt = prepareSql(
drh4c5298f2013-04-10 12:01:21 +0000579 "SELECT script, id, name FROM task"
drh27338e62013-04-06 00:19:37 +0000580 " WHERE client=%d AND starttime IS NULL"
581 " ORDER BY id LIMIT 1", iClient);
582 rc = sqlite3_step(pStmt);
583 if( rc==SQLITE_ROW ){
584 int n = sqlite3_column_bytes(pStmt, 0);
drhf90e50f2013-04-08 19:13:48 +0000585 *pzScript = sqlite3_malloc(n+1);
drh27338e62013-04-06 00:19:37 +0000586 strcpy(*pzScript, (const char*)sqlite3_column_text(pStmt, 0));
587 *pTaskId = taskId = sqlite3_column_int(pStmt, 1);
drh4c5298f2013-04-10 12:01:21 +0000588 *pzTaskName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 2));
drh27338e62013-04-06 00:19:37 +0000589 sqlite3_finalize(pStmt);
590 runSql("UPDATE task"
591 " SET starttime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
592 " WHERE id=%d;", taskId);
drh3f5bc382013-04-06 13:09:11 +0000593 g.iTimeout = DEFAULT_TIMEOUT;
drhf90e50f2013-04-08 19:13:48 +0000594 runSql("COMMIT TRANSACTION;");
drh27338e62013-04-06 00:19:37 +0000595 return SQLITE_OK;
596 }
597 sqlite3_finalize(pStmt);
598 if( rc==SQLITE_DONE ){
drh3f5bc382013-04-06 13:09:11 +0000599 if( totalTime>30000 ){
600 errorMessage("Waited over 30 seconds with no work. Giving up.");
601 runSql("DELETE FROM client WHERE id=%d; COMMIT;", iClient);
602 sqlite3_close(g.db);
603 exit(1);
604 }
drhf90e50f2013-04-08 19:13:48 +0000605 while( trySql("COMMIT")==SQLITE_BUSY ){
606 sqlite3_sleep(10);
607 totalTime += 10;
608 }
drh27338e62013-04-06 00:19:37 +0000609 sqlite3_sleep(100);
drh3f5bc382013-04-06 13:09:11 +0000610 totalTime += 100;
drh27338e62013-04-06 00:19:37 +0000611 continue;
612 }
613 fatalError("%s", sqlite3_errmsg(g.db));
614 }
drh3f5bc382013-04-06 13:09:11 +0000615 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +0000616}
617
618/*
drh3f5bc382013-04-06 13:09:11 +0000619** Mark a script as having finished. Remove the CLIENT table entry
620** if bShutdown is true.
drh27338e62013-04-06 00:19:37 +0000621*/
drh3f5bc382013-04-06 13:09:11 +0000622static int finishScript(int iClient, int taskId, int bShutdown){
623 runSql("UPDATE task"
624 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
625 " WHERE id=%d;", taskId);
626 if( bShutdown ){
627 runSql("DELETE FROM client WHERE id=%d", iClient);
drh27338e62013-04-06 00:19:37 +0000628 }
drh3f5bc382013-04-06 13:09:11 +0000629 return SQLITE_OK;
630}
631
632/*
633** Start up a client process for iClient, if it is not already
634** running. If the client is already running, then this routine
635** is a no-op.
636*/
637static void startClient(int iClient){
638 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient);
639 if( sqlite3_changes(g.db) ){
640 char *zSys;
drhbc94dbb2013-04-08 14:28:33 +0000641 int rc;
drh739ee7f2013-04-12 01:04:36 +0000642 zSys = sqlite3_mprintf("%s \"%s\" --client %d --trace %d",
643 g.argv0, g.zDbFile, iClient, g.iTrace);
644 if( g.bSqlTrace ){
645 zSys = sqlite3_mprintf("%z --sqltrace", zSys);
646 }
647 if( g.bSync ){
648 zSys = sqlite3_mprintf("%z --sync", zSys);
649 }
650 if( g.zVfs ){
651 zSys = sqlite3_mprintf("%z --vfs \"%s\"", zSys, g.zVfs);
652 }
653 if( g.iTrace>=2 ) logMessage("system('%q')", zSys);
mistachkin08d41892013-04-11 00:09:44 +0000654#if !defined(_WIN32)
drh739ee7f2013-04-12 01:04:36 +0000655 zSys = sqlite3_mprintf("%z &", zSys);
drhbc94dbb2013-04-08 14:28:33 +0000656 rc = system(zSys);
657 if( rc ) errorMessage("system() fails with error code %d", rc);
mistachkin08d41892013-04-11 00:09:44 +0000658#else
659 {
660 STARTUPINFOA startupInfo;
661 PROCESS_INFORMATION processInfo;
662 memset(&startupInfo, 0, sizeof(startupInfo));
663 startupInfo.cb = sizeof(startupInfo);
664 memset(&processInfo, 0, sizeof(processInfo));
665 rc = CreateProcessA(NULL, zSys, NULL, NULL, FALSE, 0, NULL, NULL,
666 &startupInfo, &processInfo);
667 if( rc ){
668 CloseHandle(processInfo.hThread);
669 CloseHandle(processInfo.hProcess);
670 }else{
671 errorMessage("CreateProcessA() fails with error code %lu",
672 GetLastError());
673 }
drhf012ae02013-04-06 14:04:22 +0000674 }
drhf012ae02013-04-06 14:04:22 +0000675#endif
mistachkin08d41892013-04-11 00:09:44 +0000676 sqlite3_free(zSys);
drh3f5bc382013-04-06 13:09:11 +0000677 }
drh27338e62013-04-06 00:19:37 +0000678}
679
680/*
681** Read the entire content of a file into memory
682*/
683static char *readFile(const char *zFilename){
684 FILE *in = fopen(zFilename, "rb");
685 long sz;
686 char *z;
687 if( in==0 ){
688 fatalError("cannot open \"%s\" for reading", zFilename);
689 }
690 fseek(in, 0, SEEK_END);
691 sz = ftell(in);
692 rewind(in);
693 z = sqlite3_malloc( sz+1 );
694 sz = (long)fread(z, 1, sz, in);
695 z[sz] = 0;
696 fclose(in);
697 return z;
698}
699
700/*
701** Return the length of the next token.
702*/
703static int tokenLength(const char *z, int *pnLine){
704 int n = 0;
drhc56fac72015-10-29 13:48:15 +0000705 if( ISSPACE(z[0]) || (z[0]=='/' && z[1]=='*') ){
drh27338e62013-04-06 00:19:37 +0000706 int inC = 0;
707 int c;
708 if( z[0]=='/' ){
709 inC = 1;
710 n = 2;
711 }
712 while( (c = z[n++])!=0 ){
713 if( c=='\n' ) (*pnLine)++;
drhc56fac72015-10-29 13:48:15 +0000714 if( ISSPACE(c) ) continue;
drh27338e62013-04-06 00:19:37 +0000715 if( inC && c=='*' && z[n]=='/' ){
716 n++;
717 inC = 0;
718 }else if( !inC && c=='/' && z[n]=='*' ){
719 n++;
720 inC = 1;
721 }else if( !inC ){
722 break;
723 }
724 }
725 n--;
726 }else if( z[0]=='-' && z[1]=='-' ){
727 for(n=2; z[n] && z[n]!='\n'; n++){}
728 if( z[n] ){ (*pnLine)++; n++; }
729 }else if( z[0]=='"' || z[0]=='\'' ){
730 int delim = z[0];
731 for(n=1; z[n]; n++){
732 if( z[n]=='\n' ) (*pnLine)++;
733 if( z[n]==delim ){
734 n++;
735 if( z[n+1]!=delim ) break;
736 }
737 }
738 }else{
739 int c;
drhc56fac72015-10-29 13:48:15 +0000740 for(n=1; (c = z[n])!=0 && !ISSPACE(c) && c!='"' && c!='\'' && c!=';'; n++){}
drh27338e62013-04-06 00:19:37 +0000741 }
742 return n;
743}
744
745/*
746** Copy a single token into a string buffer.
747*/
748static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){
749 int i;
750 if( nIn<=0 ){
751 zOut[0] = 0;
752 return 0;
753 }
drhc56fac72015-10-29 13:48:15 +0000754 for(i=0; i<nIn && i<nOut-1 && !ISSPACE(zIn[i]); i++){ zOut[i] = zIn[i]; }
drh27338e62013-04-06 00:19:37 +0000755 zOut[i] = 0;
756 return i;
757}
758
759/*
drh7dfe8e22013-04-08 13:13:43 +0000760** Find the number of characters up to the start of the next "--end" token.
drh27338e62013-04-06 00:19:37 +0000761*/
762static int findEnd(const char *z, int *pnLine){
763 int n = 0;
drhc56fac72015-10-29 13:48:15 +0000764 while( z[n] && (strncmp(z+n,"--end",5) || !ISSPACE(z[n+5])) ){
drh27338e62013-04-06 00:19:37 +0000765 n += tokenLength(z+n, pnLine);
766 }
767 return n;
768}
769
770/*
drh7dfe8e22013-04-08 13:13:43 +0000771** Find the number of characters up to the first character past the
772** of the next "--endif" or "--else" token. Nested --if commands are
773** also skipped.
774*/
775static int findEndif(const char *z, int stopAtElse, int *pnLine){
776 int n = 0;
777 while( z[n] ){
778 int len = tokenLength(z+n, pnLine);
drhc56fac72015-10-29 13:48:15 +0000779 if( (strncmp(z+n,"--endif",7)==0 && ISSPACE(z[n+7]))
780 || (stopAtElse && strncmp(z+n,"--else",6)==0 && ISSPACE(z[n+6]))
drh7dfe8e22013-04-08 13:13:43 +0000781 ){
782 return n+len;
783 }
drhc56fac72015-10-29 13:48:15 +0000784 if( strncmp(z+n,"--if",4)==0 && ISSPACE(z[n+4]) ){
drh7dfe8e22013-04-08 13:13:43 +0000785 int skip = findEndif(z+n+len, 0, pnLine);
786 n += skip + len;
787 }else{
788 n += len;
789 }
790 }
791 return n;
792}
793
794/*
drh27338e62013-04-06 00:19:37 +0000795** Wait for a client process to complete all its tasks
796*/
797static void waitForClient(int iClient, int iTimeout, char *zErrPrefix){
798 sqlite3_stmt *pStmt;
799 int rc;
800 if( iClient>0 ){
801 pStmt = prepareSql(
drh3f5bc382013-04-06 13:09:11 +0000802 "SELECT 1 FROM task"
803 " WHERE client=%d"
804 " AND client IN (SELECT id FROM client)"
805 " AND endtime IS NULL",
drh27338e62013-04-06 00:19:37 +0000806 iClient);
807 }else{
808 pStmt = prepareSql(
drh3f5bc382013-04-06 13:09:11 +0000809 "SELECT 1 FROM task"
810 " WHERE client IN (SELECT id FROM client)"
811 " AND endtime IS NULL");
drh27338e62013-04-06 00:19:37 +0000812 }
drh3f5bc382013-04-06 13:09:11 +0000813 g.iTimeout = 0;
drh27338e62013-04-06 00:19:37 +0000814 while( ((rc = sqlite3_step(pStmt))==SQLITE_BUSY || rc==SQLITE_ROW)
815 && iTimeout>0
816 ){
817 sqlite3_reset(pStmt);
818 sqlite3_sleep(50);
819 iTimeout -= 50;
820 }
821 sqlite3_finalize(pStmt);
drh3f5bc382013-04-06 13:09:11 +0000822 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +0000823 if( rc!=SQLITE_DONE ){
824 if( zErrPrefix==0 ) zErrPrefix = "";
825 if( iClient>0 ){
826 errorMessage("%stimeout waiting for client %d", zErrPrefix, iClient);
827 }else{
828 errorMessage("%stimeout waiting for all clients", zErrPrefix);
829 }
830 }
831}
832
drh4c5298f2013-04-10 12:01:21 +0000833/* Return a pointer to the tail of a filename
834*/
835static char *filenameTail(char *z){
836 int i, j;
mistachkin25a72de2015-03-31 17:58:13 +0000837 for(i=j=0; z[i]; i++) if( isDirSep(z[i]) ) j = i+1;
drh4c5298f2013-04-10 12:01:21 +0000838 return z+j;
839}
840
drhbc082812013-04-18 15:11:03 +0000841/*
842** Interpret zArg as a boolean value. Return either 0 or 1.
843*/
844static int booleanValue(char *zArg){
845 int i;
846 if( zArg==0 ) return 0;
847 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
848 if( i>0 && zArg[i]==0 ) return atoi(zArg);
849 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
850 return 1;
851 }
852 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
853 return 0;
854 }
855 errorMessage("unknown boolean: [%s]", zArg);
856 return 0;
857}
858
859
860/* This routine exists as a convenient place to set a debugger
861** breakpoint.
862*/
863static void test_breakpoint(void){ static volatile int cnt = 0; cnt++; }
864
drh27338e62013-04-06 00:19:37 +0000865/* Maximum number of arguments to a --command */
drh7dfe8e22013-04-08 13:13:43 +0000866#define MX_ARG 2
drh27338e62013-04-06 00:19:37 +0000867
868/*
869** Run a script.
870*/
871static void runScript(
872 int iClient, /* The client number, or 0 for the master */
873 int taskId, /* The task ID for clients. 0 for master */
874 char *zScript, /* Text of the script */
875 char *zFilename /* File from which script was read. */
876){
877 int lineno = 1;
878 int prevLine = 1;
879 int ii = 0;
880 int iBegin = 0;
881 int n, c, j;
drh27338e62013-04-06 00:19:37 +0000882 int len;
883 int nArg;
884 String sResult;
885 char zCmd[30];
886 char zError[1000];
887 char azArg[MX_ARG][100];
drh27338e62013-04-06 00:19:37 +0000888
drh27338e62013-04-06 00:19:37 +0000889 memset(&sResult, 0, sizeof(sResult));
890 stringReset(&sResult);
891 while( (c = zScript[ii])!=0 ){
892 prevLine = lineno;
893 len = tokenLength(zScript+ii, &lineno);
drhc56fac72015-10-29 13:48:15 +0000894 if( ISSPACE(c) || (c=='/' && zScript[ii+1]=='*') ){
drh27338e62013-04-06 00:19:37 +0000895 ii += len;
896 continue;
897 }
898 if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){
899 ii += len;
900 continue;
901 }
902
903 /* Run any prior SQL before processing the new --command */
904 if( ii>iBegin ){
905 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
906 evalSql(&sResult, zSql);
907 sqlite3_free(zSql);
908 iBegin = ii + len;
909 }
910
911 /* Parse the --command */
912 if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii);
913 n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd));
914 for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){
drhc56fac72015-10-29 13:48:15 +0000915 while( n<len-2 && ISSPACE(zScript[ii+2+n]) ){ n++; }
drh27338e62013-04-06 00:19:37 +0000916 if( n>=len-2 ) break;
917 n += extractToken(zScript+ii+2+n, len-2-n,
918 azArg[nArg], sizeof(azArg[nArg]));
919 }
920 for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0;
921
922 /*
923 ** --sleep N
924 **
925 ** Pause for N milliseconds
926 */
927 if( strcmp(zCmd, "sleep")==0 ){
928 sqlite3_sleep(atoi(azArg[0]));
929 }else
930
931 /*
932 ** --exit N
933 **
934 ** Exit this process. If N>0 then exit without shutting down
935 ** SQLite. (In other words, simulate a crash.)
936 */
drh87f9caa2013-04-17 18:56:16 +0000937 if( strcmp(zCmd, "exit")==0 ){
drh27338e62013-04-06 00:19:37 +0000938 int rc = atoi(azArg[0]);
drh3f5bc382013-04-06 13:09:11 +0000939 finishScript(iClient, taskId, 1);
drh27338e62013-04-06 00:19:37 +0000940 if( rc==0 ) sqlite3_close(g.db);
941 exit(rc);
942 }else
943
944 /*
drh87f9caa2013-04-17 18:56:16 +0000945 ** --testcase NAME
946 **
drh93c8c452013-04-18 20:33:41 +0000947 ** Begin a new test case. Announce in the log that the test case
948 ** has begun.
drh87f9caa2013-04-17 18:56:16 +0000949 */
950 if( strcmp(zCmd, "testcase")==0 ){
951 if( g.iTrace==1 ) logMessage("%.*s", len - 1, zScript+ii);
952 stringReset(&sResult);
953 }else
954
955 /*
drh6adab7a2013-04-08 18:58:00 +0000956 ** --finish
957 **
958 ** Mark the current task as having finished, even if it is not.
959 ** This can be used in conjunction with --exit to simulate a crash.
960 */
961 if( strcmp(zCmd, "finish")==0 && iClient>0 ){
962 finishScript(iClient, taskId, 1);
963 }else
964
965 /*
drh7dfe8e22013-04-08 13:13:43 +0000966 ** --reset
drh27338e62013-04-06 00:19:37 +0000967 **
968 ** Reset accumulated results back to an empty string
969 */
970 if( strcmp(zCmd, "reset")==0 ){
971 stringReset(&sResult);
972 }else
973
974 /*
975 ** --match ANSWER...
976 **
977 ** Check to see if output matches ANSWER. Report an error if not.
978 */
979 if( strcmp(zCmd, "match")==0 ){
980 int jj;
981 char *zAns = zScript+ii;
drhc56fac72015-10-29 13:48:15 +0000982 for(jj=7; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
drh27338e62013-04-06 00:19:37 +0000983 zAns += jj;
drh44fddca2013-04-17 19:42:17 +0000984 if( len-jj-1!=sResult.n || strncmp(sResult.z, zAns, len-jj-1) ){
drh27338e62013-04-06 00:19:37 +0000985 errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]",
986 prevLine, zFilename, len-jj-1, zAns, sResult.z);
987 }
drh3f5bc382013-04-06 13:09:11 +0000988 g.nTest++;
drh27338e62013-04-06 00:19:37 +0000989 stringReset(&sResult);
990 }else
991
992 /*
drh87f9caa2013-04-17 18:56:16 +0000993 ** --glob ANSWER...
994 ** --notglob ANSWER....
995 **
996 ** Check to see if output does or does not match the glob pattern
997 ** ANSWER.
998 */
999 if( strcmp(zCmd, "glob")==0 || strcmp(zCmd, "notglob")==0 ){
1000 int jj;
1001 char *zAns = zScript+ii;
1002 char *zCopy;
1003 int isGlob = (zCmd[0]=='g');
drhc56fac72015-10-29 13:48:15 +00001004 for(jj=9-3*isGlob; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
drh87f9caa2013-04-17 18:56:16 +00001005 zAns += jj;
1006 zCopy = sqlite3_mprintf("%.*s", len-jj-1, zAns);
1007 if( (sqlite3_strglob(zCopy, sResult.z)==0)^isGlob ){
1008 errorMessage("line %d of %s:\nExpected [%s]\n Got [%s]",
1009 prevLine, zFilename, zCopy, sResult.z);
1010 }
1011 sqlite3_free(zCopy);
1012 g.nTest++;
1013 stringReset(&sResult);
1014 }else
1015
1016 /*
drh1bf44c72013-04-08 13:48:29 +00001017 ** --output
1018 **
1019 ** Output the result of the previous SQL.
1020 */
1021 if( strcmp(zCmd, "output")==0 ){
1022 logMessage("%s", sResult.z);
1023 }else
1024
1025 /*
drh27338e62013-04-06 00:19:37 +00001026 ** --source FILENAME
1027 **
1028 ** Run a subscript from a separate file.
1029 */
1030 if( strcmp(zCmd, "source")==0 ){
drhe348fc72013-04-06 18:35:07 +00001031 char *zNewFile, *zNewScript;
1032 char *zToDel = 0;
1033 zNewFile = azArg[0];
mistachkin25a72de2015-03-31 17:58:13 +00001034 if( !isDirSep(zNewFile[0]) ){
drhe348fc72013-04-06 18:35:07 +00001035 int k;
mistachkin25a72de2015-03-31 17:58:13 +00001036 for(k=(int)strlen(zFilename)-1; k>=0 && !isDirSep(zFilename[k]); k--){}
drhe348fc72013-04-06 18:35:07 +00001037 if( k>0 ){
1038 zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile);
1039 }
1040 }
1041 zNewScript = readFile(zNewFile);
drh27338e62013-04-06 00:19:37 +00001042 if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile);
1043 runScript(0, 0, zNewScript, zNewFile);
1044 sqlite3_free(zNewScript);
1045 if( g.iTrace ) logMessage("end script [%s]\n", zNewFile);
drhbc94dbb2013-04-08 14:28:33 +00001046 sqlite3_free(zToDel);
drh27338e62013-04-06 00:19:37 +00001047 }else
1048
1049 /*
1050 ** --print MESSAGE....
1051 **
1052 ** Output the remainder of the line to the log file
1053 */
1054 if( strcmp(zCmd, "print")==0 ){
1055 int jj;
drhc56fac72015-10-29 13:48:15 +00001056 for(jj=7; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
drh27338e62013-04-06 00:19:37 +00001057 logMessage("%.*s", len-jj, zScript+ii+jj);
1058 }else
1059
1060 /*
drh7dfe8e22013-04-08 13:13:43 +00001061 ** --if EXPR
1062 **
1063 ** Skip forward to the next matching --endif or --else if EXPR is false.
1064 */
1065 if( strcmp(zCmd, "if")==0 ){
1066 int jj, rc;
1067 sqlite3_stmt *pStmt;
drhc56fac72015-10-29 13:48:15 +00001068 for(jj=4; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
drh7dfe8e22013-04-08 13:13:43 +00001069 pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj);
1070 rc = sqlite3_step(pStmt);
1071 if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){
1072 ii += findEndif(zScript+ii+len, 1, &lineno);
1073 }
1074 sqlite3_finalize(pStmt);
1075 }else
1076
1077 /*
1078 ** --else
1079 **
1080 ** This command can only be encountered if currently inside an --if that
1081 ** is true. Skip forward to the next matching --endif.
1082 */
1083 if( strcmp(zCmd, "else")==0 ){
1084 ii += findEndif(zScript+ii+len, 0, &lineno);
1085 }else
1086
1087 /*
1088 ** --endif
1089 **
1090 ** This command can only be encountered if currently inside an --if that
1091 ** is true or an --else of a false if. This is a no-op.
1092 */
1093 if( strcmp(zCmd, "endif")==0 ){
1094 /* no-op */
1095 }else
1096
1097 /*
drh27338e62013-04-06 00:19:37 +00001098 ** --start CLIENT
1099 **
1100 ** Start up the given client.
1101 */
drh7dfe8e22013-04-08 13:13:43 +00001102 if( strcmp(zCmd, "start")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001103 int iNewClient = atoi(azArg[0]);
drh3f5bc382013-04-06 13:09:11 +00001104 if( iNewClient>0 ){
1105 startClient(iNewClient);
drh27338e62013-04-06 00:19:37 +00001106 }
drh27338e62013-04-06 00:19:37 +00001107 }else
1108
1109 /*
1110 ** --wait CLIENT TIMEOUT
1111 **
1112 ** Wait until all tasks complete for the given client. If CLIENT is
1113 ** "all" then wait for all clients to complete. Wait no longer than
1114 ** TIMEOUT milliseconds (default 10,000)
1115 */
drh7dfe8e22013-04-08 13:13:43 +00001116 if( strcmp(zCmd, "wait")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001117 int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000;
1118 sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n",
1119 prevLine, zFilename);
1120 waitForClient(atoi(azArg[0]), iTimeout, zError);
1121 }else
1122
1123 /*
1124 ** --task CLIENT
1125 ** <task-content-here>
1126 ** --end
1127 **
drh3f5bc382013-04-06 13:09:11 +00001128 ** Assign work to a client. Start the client if it is not running
1129 ** already.
drh27338e62013-04-06 00:19:37 +00001130 */
drh7dfe8e22013-04-08 13:13:43 +00001131 if( strcmp(zCmd, "task")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001132 int iTarget = atoi(azArg[0]);
1133 int iEnd;
1134 char *zTask;
drh4c5298f2013-04-10 12:01:21 +00001135 char *zTName;
drh27338e62013-04-06 00:19:37 +00001136 iEnd = findEnd(zScript+ii+len, &lineno);
drh3f5bc382013-04-06 13:09:11 +00001137 if( iTarget<0 ){
1138 errorMessage("line %d of %s: bad client number: %d",
drh27338e62013-04-06 00:19:37 +00001139 prevLine, zFilename, iTarget);
drh3f5bc382013-04-06 13:09:11 +00001140 }else{
1141 zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len);
drh4c5298f2013-04-10 12:01:21 +00001142 if( nArg>1 ){
1143 zTName = sqlite3_mprintf("%s", azArg[1]);
1144 }else{
1145 zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine);
1146 }
drh3f5bc382013-04-06 13:09:11 +00001147 startClient(iTarget);
drh4c5298f2013-04-10 12:01:21 +00001148 runSql("INSERT INTO task(client,script,name)"
1149 " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName);
drh3f5bc382013-04-06 13:09:11 +00001150 sqlite3_free(zTask);
drh4c5298f2013-04-10 12:01:21 +00001151 sqlite3_free(zTName);
drh27338e62013-04-06 00:19:37 +00001152 }
drh27338e62013-04-06 00:19:37 +00001153 iEnd += tokenLength(zScript+ii+len+iEnd, &lineno);
1154 len += iEnd;
1155 iBegin = ii+len;
1156 }else
1157
drhbc082812013-04-18 15:11:03 +00001158 /*
1159 ** --breakpoint
1160 **
1161 ** This command calls "test_breakpoint()" which is a routine provided
1162 ** as a convenient place to set a debugger breakpoint.
1163 */
1164 if( strcmp(zCmd, "breakpoint")==0 ){
1165 test_breakpoint();
1166 }else
1167
1168 /*
1169 ** --show-sql-errors BOOLEAN
1170 **
1171 ** Turn display of SQL errors on and off.
1172 */
1173 if( strcmp(zCmd, "show-sql-errors")==0 ){
1174 g.bIgnoreSqlErrors = nArg>=1 ? !booleanValue(azArg[0]) : 1;
1175 }else
1176
1177
drh27338e62013-04-06 00:19:37 +00001178 /* error */{
1179 errorMessage("line %d of %s: unknown command --%s",
1180 prevLine, zFilename, zCmd);
1181 }
1182 ii += len;
1183 }
1184 if( iBegin<ii ){
1185 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
1186 runSql(zSql);
1187 sqlite3_free(zSql);
1188 }
1189 stringFree(&sResult);
1190}
1191
1192/*
1193** Look for a command-line option. If present, return a pointer.
1194** Return NULL if missing.
1195**
1196** hasArg==0 means the option is a flag. It is either present or not.
1197** hasArg==1 means the option has an argument. Return a pointer to the
1198** argument.
1199*/
1200static char *findOption(
1201 char **azArg,
1202 int *pnArg,
1203 const char *zOption,
1204 int hasArg
1205){
1206 int i, j;
1207 char *zReturn = 0;
1208 int nArg = *pnArg;
1209
1210 assert( hasArg==0 || hasArg==1 );
1211 for(i=0; i<nArg; i++){
1212 const char *z;
1213 if( i+hasArg >= nArg ) break;
1214 z = azArg[i];
1215 if( z[0]!='-' ) continue;
1216 z++;
1217 if( z[0]=='-' ){
1218 if( z[1]==0 ) break;
1219 z++;
1220 }
1221 if( strcmp(z,zOption)==0 ){
1222 if( hasArg && i==nArg-1 ){
1223 fatalError("command-line option \"--%s\" requires an argument", z);
1224 }
1225 if( hasArg ){
1226 zReturn = azArg[i+1];
1227 }else{
1228 zReturn = azArg[i];
1229 }
1230 j = i+1+(hasArg!=0);
1231 while( j<nArg ) azArg[i++] = azArg[j++];
1232 *pnArg = i;
1233 return zReturn;
1234 }
1235 }
1236 return zReturn;
1237}
1238
1239/* Print a usage message for the program and exit */
1240static void usage(const char *argv0){
1241 int i;
1242 const char *zTail = argv0;
1243 for(i=0; argv0[i]; i++){
mistachkin25a72de2015-03-31 17:58:13 +00001244 if( isDirSep(argv0[i]) ) zTail = argv0+i+1;
drh27338e62013-04-06 00:19:37 +00001245 }
1246 fprintf(stderr,"Usage: %s DATABASE ?OPTIONS? ?SCRIPT?\n", zTail);
1247 exit(1);
1248}
1249
1250/* Report on unrecognized arguments */
1251static void unrecognizedArguments(
1252 const char *argv0,
1253 int nArg,
1254 char **azArg
1255){
1256 int i;
1257 fprintf(stderr,"%s: unrecognized arguments:", argv0);
1258 for(i=0; i<nArg; i++){
1259 fprintf(stderr," %s", azArg[i]);
1260 }
1261 fprintf(stderr,"\n");
1262 exit(1);
1263}
1264
mistachkin44723ce2015-03-21 02:22:37 +00001265int SQLITE_CDECL main(int argc, char **argv){
drh27338e62013-04-06 00:19:37 +00001266 const char *zClient;
1267 int iClient;
drhe348fc72013-04-06 18:35:07 +00001268 int n, i;
drh27338e62013-04-06 00:19:37 +00001269 int openFlags = SQLITE_OPEN_READWRITE;
1270 int rc;
1271 char *zScript;
1272 int taskId;
1273 const char *zTrace;
drhe348fc72013-04-06 18:35:07 +00001274 const char *zCOption;
drhcc285c52015-03-11 14:34:38 +00001275 const char *zJMode;
1276 const char *zNRep;
1277 int nRep = 1, iRep;
drh27338e62013-04-06 00:19:37 +00001278
1279 g.argv0 = argv[0];
1280 g.iTrace = 1;
1281 if( argc<2 ) usage(argv[0]);
1282 g.zDbFile = argv[1];
drhe348fc72013-04-06 18:35:07 +00001283 if( strglob("*.test", g.zDbFile) ) usage(argv[0]);
1284 if( strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID)!=0 ){
1285 fprintf(stderr, "SQLite library and header mismatch\n"
1286 "Library: %s\n"
1287 "Header: %s\n",
1288 sqlite3_sourceid(), SQLITE_SOURCE_ID);
1289 exit(1);
1290 }
drh27338e62013-04-06 00:19:37 +00001291 n = argc-2;
drhe3be8c82013-04-11 11:53:45 +00001292 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.mptest", GETPID());
drhcc285c52015-03-11 14:34:38 +00001293 zJMode = findOption(argv+2, &n, "journalmode", 1);
1294 zNRep = findOption(argv+2, &n, "repeat", 1);
1295 if( zNRep ) nRep = atoi(zNRep);
1296 if( nRep<1 ) nRep = 1;
drh27338e62013-04-06 00:19:37 +00001297 g.zVfs = findOption(argv+2, &n, "vfs", 1);
1298 zClient = findOption(argv+2, &n, "client", 1);
1299 g.zErrLog = findOption(argv+2, &n, "errlog", 1);
1300 g.zLog = findOption(argv+2, &n, "log", 1);
1301 zTrace = findOption(argv+2, &n, "trace", 1);
1302 if( zTrace ) g.iTrace = atoi(zTrace);
1303 if( findOption(argv+2, &n, "quiet", 0)!=0 ) g.iTrace = 0;
1304 g.bSqlTrace = findOption(argv+2, &n, "sqltrace", 0)!=0;
drhbc94dbb2013-04-08 14:28:33 +00001305 g.bSync = findOption(argv+2, &n, "sync", 0)!=0;
drh27338e62013-04-06 00:19:37 +00001306 if( g.zErrLog ){
1307 g.pErrLog = fopen(g.zErrLog, "a");
1308 }else{
1309 g.pErrLog = stderr;
1310 }
1311 if( g.zLog ){
1312 g.pLog = fopen(g.zLog, "a");
1313 }else{
1314 g.pLog = stdout;
1315 }
drhe3be8c82013-04-11 11:53:45 +00001316
drh1790bb32013-04-06 14:30:29 +00001317 sqlite3_config(SQLITE_CONFIG_LOG, sqlErrorCallback, 0);
drh27338e62013-04-06 00:19:37 +00001318 if( zClient ){
1319 iClient = atoi(zClient);
1320 if( iClient<1 ) fatalError("illegal client number: %d\n", iClient);
drhe3be8c82013-04-11 11:53:45 +00001321 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d",
1322 GETPID(), iClient);
drh27338e62013-04-06 00:19:37 +00001323 }else{
drhe348fc72013-04-06 18:35:07 +00001324 if( g.iTrace>0 ){
drh8773b852015-03-31 14:18:29 +00001325 printf("BEGIN: %s", argv[0]);
1326 for(i=1; i<argc; i++) printf(" %s", argv[i]);
1327 printf("\n");
drhe348fc72013-04-06 18:35:07 +00001328 printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" );
1329 for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){
1330 printf("-DSQLITE_%s\n", zCOption);
1331 }
1332 fflush(stdout);
1333 }
drh27338e62013-04-06 00:19:37 +00001334 iClient = 0;
1335 unlink(g.zDbFile);
1336 openFlags |= SQLITE_OPEN_CREATE;
1337 }
1338 rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs);
1339 if( rc ) fatalError("cannot open [%s]", g.zDbFile);
drh4c451962015-03-31 18:05:49 +00001340 if( zJMode ){
1341#if defined(_WIN32)
1342 if( sqlite3_stricmp(zJMode,"persist")==0
1343 || sqlite3_stricmp(zJMode,"truncate")==0
1344 ){
1345 printf("Changing journal mode to DELETE from %s", zJMode);
1346 zJMode = "DELETE";
1347 }
1348#endif
1349 runSql("PRAGMA journal_mode=%Q;", zJMode);
1350 }
drh2c32ed72015-03-31 18:18:53 +00001351 if( !g.bSync ) trySql("PRAGMA synchronous=OFF");
drh87f9caa2013-04-17 18:56:16 +00001352 sqlite3_enable_load_extension(g.db, 1);
drh3f5bc382013-04-06 13:09:11 +00001353 sqlite3_busy_handler(g.db, busyHandler, 0);
drh1bf44c72013-04-08 13:48:29 +00001354 sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0,
1355 vfsNameFunc, 0, 0);
1356 sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0,
1357 evalFunc, 0, 0);
drh3f5bc382013-04-06 13:09:11 +00001358 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +00001359 if( g.bSqlTrace ) sqlite3_trace(g.db, sqlTraceCallback, 0);
1360 if( iClient>0 ){
1361 if( n>0 ) unrecognizedArguments(argv[0], n, argv+2);
1362 if( g.iTrace ) logMessage("start-client");
1363 while(1){
drh4c5298f2013-04-10 12:01:21 +00001364 char *zTaskName = 0;
1365 rc = startScript(iClient, &zScript, &taskId, &zTaskName);
drh27338e62013-04-06 00:19:37 +00001366 if( rc==SQLITE_DONE ) break;
drh4c5298f2013-04-10 12:01:21 +00001367 if( g.iTrace ) logMessage("begin %s (%d)", zTaskName, taskId);
drh27338e62013-04-06 00:19:37 +00001368 runScript(iClient, taskId, zScript, zTaskName);
drh4c5298f2013-04-10 12:01:21 +00001369 if( g.iTrace ) logMessage("end %s (%d)", zTaskName, taskId);
drh3f5bc382013-04-06 13:09:11 +00001370 finishScript(iClient, taskId, 0);
drh4c5298f2013-04-10 12:01:21 +00001371 sqlite3_free(zTaskName);
drh27338e62013-04-06 00:19:37 +00001372 sqlite3_sleep(10);
1373 }
1374 if( g.iTrace ) logMessage("end-client");
1375 }else{
1376 sqlite3_stmt *pStmt;
drh3f5bc382013-04-06 13:09:11 +00001377 int iTimeout;
drh27338e62013-04-06 00:19:37 +00001378 if( n==0 ){
1379 fatalError("missing script filename");
1380 }
1381 if( n>1 ) unrecognizedArguments(argv[0], n, argv+2);
1382 runSql(
drhcc285c52015-03-11 14:34:38 +00001383 "DROP TABLE IF EXISTS task;\n"
1384 "DROP TABLE IF EXISTS counters;\n"
1385 "DROP TABLE IF EXISTS client;\n"
drh27338e62013-04-06 00:19:37 +00001386 "CREATE TABLE task(\n"
1387 " id INTEGER PRIMARY KEY,\n"
drh4c5298f2013-04-10 12:01:21 +00001388 " name TEXT,\n"
drh27338e62013-04-06 00:19:37 +00001389 " client INTEGER,\n"
1390 " starttime DATE,\n"
1391 " endtime DATE,\n"
1392 " script TEXT\n"
1393 ");"
drh3f5bc382013-04-06 13:09:11 +00001394 "CREATE INDEX task_i1 ON task(client, starttime);\n"
1395 "CREATE INDEX task_i2 ON task(client, endtime);\n"
1396 "CREATE TABLE counters(nError,nTest);\n"
1397 "INSERT INTO counters VALUES(0,0);\n"
1398 "CREATE TABLE client(id INTEGER PRIMARY KEY, wantHalt);\n"
drh27338e62013-04-06 00:19:37 +00001399 );
1400 zScript = readFile(argv[2]);
drhcc285c52015-03-11 14:34:38 +00001401 for(iRep=1; iRep<=nRep; iRep++){
1402 if( g.iTrace ) logMessage("begin script [%s] cycle %d\n", argv[2], iRep);
1403 runScript(0, 0, zScript, argv[2]);
1404 if( g.iTrace ) logMessage("end script [%s] cycle %d\n", argv[2], iRep);
1405 }
drh27338e62013-04-06 00:19:37 +00001406 sqlite3_free(zScript);
drh27338e62013-04-06 00:19:37 +00001407 waitForClient(0, 2000, "during shutdown...\n");
drh3f5bc382013-04-06 13:09:11 +00001408 trySql("UPDATE client SET wantHalt=1");
1409 sqlite3_sleep(10);
1410 g.iTimeout = 0;
1411 iTimeout = 1000;
1412 while( ((rc = trySql("SELECT 1 FROM client"))==SQLITE_BUSY
1413 || rc==SQLITE_ROW) && iTimeout>0 ){
drh27338e62013-04-06 00:19:37 +00001414 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +00001415 iTimeout -= 10;
drh27338e62013-04-06 00:19:37 +00001416 }
1417 sqlite3_sleep(100);
drh3f5bc382013-04-06 13:09:11 +00001418 pStmt = prepareSql("SELECT nError, nTest FROM counters");
1419 iTimeout = 1000;
1420 while( (rc = sqlite3_step(pStmt))==SQLITE_BUSY && iTimeout>0 ){
drh27338e62013-04-06 00:19:37 +00001421 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +00001422 iTimeout -= 10;
drh27338e62013-04-06 00:19:37 +00001423 }
1424 if( rc==SQLITE_ROW ){
1425 g.nError += sqlite3_column_int(pStmt, 0);
drh3f5bc382013-04-06 13:09:11 +00001426 g.nTest += sqlite3_column_int(pStmt, 1);
drh27338e62013-04-06 00:19:37 +00001427 }
1428 sqlite3_finalize(pStmt);
1429 }
drhcc285c52015-03-11 14:34:38 +00001430 sqlite3_close(g.db);
drh27338e62013-04-06 00:19:37 +00001431 maybeClose(g.pLog);
1432 maybeClose(g.pErrLog);
1433 if( iClient==0 ){
drhbd41d562014-12-30 20:40:32 +00001434 printf("Summary: %d errors out of %d tests\n", g.nError, g.nTest);
drh8773b852015-03-31 14:18:29 +00001435 printf("END: %s", argv[0]);
1436 for(i=1; i<argc; i++) printf(" %s", argv[i]);
1437 printf("\n");
drh27338e62013-04-06 00:19:37 +00001438 }
1439 return g.nError>0;
1440}