drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1 | /* |
| 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> |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 38 | #if defined(_WIN32) |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 39 | # define WIN32_LEAN_AND_MEAN |
| 40 | # include <windows.h> |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 41 | #else |
| 42 | # include <unistd.h> |
| 43 | #endif |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <assert.h> |
| 47 | #include <ctype.h> |
| 48 | |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 49 | /* The suffix to append to the child command lines, if any */ |
| 50 | #if defined(_WIN32) |
| 51 | # define CMDLINE_SUFFIX "" |
mistachkin | fdd72c9 | 2013-04-11 21:13:10 +0000 | [diff] [blame^] | 52 | # define GETPID (int)GetCurrentProcessId |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 53 | #else |
| 54 | # define CMDLINE_SUFFIX "&" |
mistachkin | fdd72c9 | 2013-04-11 21:13:10 +0000 | [diff] [blame^] | 55 | # define GETPID getpid |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
drh | 841810c | 2013-04-08 13:59:11 +0000 | [diff] [blame] | 58 | /* Mark a parameter as unused to suppress compiler warnings */ |
| 59 | #define UNUSED_PARAMETER(x) (void)x |
| 60 | |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 61 | /* Global data |
| 62 | */ |
| 63 | static struct Global { |
| 64 | char *argv0; /* Name of the executable */ |
| 65 | const char *zVfs; /* Name of VFS to use. Often NULL meaning "default" */ |
| 66 | char *zDbFile; /* Name of the database */ |
| 67 | sqlite3 *db; /* Open connection to database */ |
| 68 | char *zErrLog; /* Filename for error log */ |
| 69 | FILE *pErrLog; /* Where to write errors */ |
| 70 | char *zLog; /* Name of output log file */ |
| 71 | FILE *pLog; /* Where to write log messages */ |
drh | e3be8c8 | 2013-04-11 11:53:45 +0000 | [diff] [blame] | 72 | char zName[32]; /* Symbolic name of this process */ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 73 | int taskId; /* Task ID. 0 means supervisor. */ |
| 74 | int iTrace; /* Tracing level */ |
| 75 | int bSqlTrace; /* True to trace SQL commands */ |
| 76 | int nError; /* Number of errors */ |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 77 | int nTest; /* Number of --match operators */ |
| 78 | int iTimeout; /* Milliseconds until a busy timeout */ |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 79 | int bSync; /* Call fsync() */ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 80 | } g; |
| 81 | |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 82 | /* Default timeout */ |
| 83 | #define DEFAULT_TIMEOUT 10000 |
| 84 | |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 85 | /* |
| 86 | ** Print a message adding zPrefix[] to the beginning of every line. |
| 87 | */ |
| 88 | static void printWithPrefix(FILE *pOut, const char *zPrefix, const char *zMsg){ |
| 89 | while( zMsg && zMsg[0] ){ |
| 90 | int i; |
| 91 | for(i=0; zMsg[i] && zMsg[i]!='\n' && zMsg[i]!='\r'; i++){} |
| 92 | fprintf(pOut, "%s%.*s\n", zPrefix, i, zMsg); |
| 93 | zMsg += i; |
| 94 | while( zMsg[0]=='\n' || zMsg[0]=='\r' ) zMsg++; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | ** Compare two pointers to strings, where the pointers might be NULL. |
| 100 | */ |
| 101 | static int safe_strcmp(const char *a, const char *b){ |
| 102 | if( a==b ) return 0; |
| 103 | if( a==0 ) return -1; |
| 104 | if( b==0 ) return 1; |
| 105 | return strcmp(a,b); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | ** Return TRUE if string z[] matches glob pattern zGlob[]. |
| 110 | ** Return FALSE if the pattern does not match. |
| 111 | ** |
| 112 | ** Globbing rules: |
| 113 | ** |
| 114 | ** '*' Matches any sequence of zero or more characters. |
| 115 | ** |
| 116 | ** '?' Matches exactly one character. |
| 117 | ** |
| 118 | ** [...] Matches one character from the enclosed list of |
| 119 | ** characters. |
| 120 | ** |
| 121 | ** [^...] Matches one character not in the enclosed list. |
| 122 | ** |
| 123 | ** '#' Matches any sequence of one or more digits with an |
| 124 | ** optional + or - sign in front |
| 125 | */ |
| 126 | int strglob(const char *zGlob, const char *z){ |
| 127 | int c, c2; |
| 128 | int invert; |
| 129 | int seen; |
| 130 | |
| 131 | while( (c = (*(zGlob++)))!=0 ){ |
| 132 | if( c=='*' ){ |
| 133 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 134 | if( c=='?' && (*(z++))==0 ) return 0; |
| 135 | } |
| 136 | if( c==0 ){ |
| 137 | return 1; |
| 138 | }else if( c=='[' ){ |
| 139 | while( *z && strglob(zGlob-1,z) ){ |
| 140 | z++; |
| 141 | } |
| 142 | return (*z)!=0; |
| 143 | } |
| 144 | while( (c2 = (*(z++)))!=0 ){ |
| 145 | while( c2!=c ){ |
| 146 | c2 = *(z++); |
| 147 | if( c2==0 ) return 0; |
| 148 | } |
| 149 | if( strglob(zGlob,z) ) return 1; |
| 150 | } |
| 151 | return 0; |
| 152 | }else if( c=='?' ){ |
| 153 | if( (*(z++))==0 ) return 0; |
| 154 | }else if( c=='[' ){ |
| 155 | int prior_c = 0; |
| 156 | seen = 0; |
| 157 | invert = 0; |
| 158 | c = *(z++); |
| 159 | if( c==0 ) return 0; |
| 160 | c2 = *(zGlob++); |
| 161 | if( c2=='^' ){ |
| 162 | invert = 1; |
| 163 | c2 = *(zGlob++); |
| 164 | } |
| 165 | if( c2==']' ){ |
| 166 | if( c==']' ) seen = 1; |
| 167 | c2 = *(zGlob++); |
| 168 | } |
| 169 | while( c2 && c2!=']' ){ |
| 170 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 171 | c2 = *(zGlob++); |
| 172 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 173 | prior_c = 0; |
| 174 | }else{ |
| 175 | if( c==c2 ){ |
| 176 | seen = 1; |
| 177 | } |
| 178 | prior_c = c2; |
| 179 | } |
| 180 | c2 = *(zGlob++); |
| 181 | } |
| 182 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 183 | }else if( c=='#' ){ |
| 184 | if( (z[0]=='-' || z[0]=='+') && isdigit(z[1]) ) z++; |
| 185 | if( !isdigit(z[0]) ) return 0; |
| 186 | z++; |
| 187 | while( isdigit(z[0]) ){ z++; } |
| 188 | }else{ |
| 189 | if( c!=(*(z++)) ) return 0; |
| 190 | } |
| 191 | } |
| 192 | return *z==0; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | ** Close output stream pOut if it is not stdout or stderr |
| 197 | */ |
| 198 | static void maybeClose(FILE *pOut){ |
| 199 | if( pOut!=stdout && pOut!=stderr ) fclose(pOut); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | ** Print an error message |
| 204 | */ |
| 205 | static void errorMessage(const char *zFormat, ...){ |
| 206 | va_list ap; |
| 207 | char *zMsg; |
| 208 | char zPrefix[30]; |
| 209 | va_start(ap, zFormat); |
| 210 | zMsg = sqlite3_vmprintf(zFormat, ap); |
| 211 | va_end(ap); |
| 212 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:ERROR: ", g.zName); |
| 213 | if( g.pLog ){ |
| 214 | printWithPrefix(g.pLog, zPrefix, zMsg); |
| 215 | fflush(g.pLog); |
| 216 | } |
| 217 | if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){ |
| 218 | printWithPrefix(g.pErrLog, zPrefix, zMsg); |
| 219 | fflush(g.pErrLog); |
| 220 | } |
| 221 | sqlite3_free(zMsg); |
| 222 | g.nError++; |
| 223 | } |
| 224 | |
| 225 | /* Forward declaration */ |
| 226 | static int trySql(const char*, ...); |
| 227 | |
| 228 | /* |
| 229 | ** Print an error message and then quit. |
| 230 | */ |
| 231 | static void fatalError(const char *zFormat, ...){ |
| 232 | va_list ap; |
| 233 | char *zMsg; |
| 234 | char zPrefix[30]; |
| 235 | va_start(ap, zFormat); |
| 236 | zMsg = sqlite3_vmprintf(zFormat, ap); |
| 237 | va_end(ap); |
| 238 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:FATAL: ", g.zName); |
| 239 | if( g.pLog ){ |
| 240 | printWithPrefix(g.pLog, zPrefix, zMsg); |
| 241 | fflush(g.pLog); |
| 242 | maybeClose(g.pLog); |
| 243 | } |
| 244 | if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){ |
| 245 | printWithPrefix(g.pErrLog, zPrefix, zMsg); |
| 246 | fflush(g.pErrLog); |
| 247 | maybeClose(g.pErrLog); |
| 248 | } |
| 249 | sqlite3_free(zMsg); |
| 250 | if( g.db ){ |
| 251 | int nTry = 0; |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 252 | g.iTimeout = 0; |
| 253 | while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY |
| 254 | && (nTry++)<100 ){ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 255 | sqlite3_sleep(10); |
| 256 | } |
| 257 | } |
| 258 | sqlite3_close(g.db); |
| 259 | exit(1); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /* |
| 264 | ** Print a log message |
| 265 | */ |
| 266 | static void logMessage(const char *zFormat, ...){ |
| 267 | va_list ap; |
| 268 | char *zMsg; |
| 269 | char zPrefix[30]; |
| 270 | va_start(ap, zFormat); |
| 271 | zMsg = sqlite3_vmprintf(zFormat, ap); |
| 272 | va_end(ap); |
| 273 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s: ", g.zName); |
| 274 | if( g.pLog ){ |
| 275 | printWithPrefix(g.pLog, zPrefix, zMsg); |
| 276 | fflush(g.pLog); |
| 277 | } |
| 278 | sqlite3_free(zMsg); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | ** Return the length of a string omitting trailing whitespace |
| 283 | */ |
| 284 | static int clipLength(const char *z){ |
| 285 | int n = (int)strlen(z); |
| 286 | while( n>0 && isspace(z[n-1]) ){ n--; } |
| 287 | return n; |
| 288 | } |
| 289 | |
| 290 | /* |
drh | 1bf44c7 | 2013-04-08 13:48:29 +0000 | [diff] [blame] | 291 | ** Auxiliary SQL function to return the name of the VFS |
| 292 | */ |
| 293 | static void vfsNameFunc( |
| 294 | sqlite3_context *context, |
| 295 | int argc, |
| 296 | sqlite3_value **argv |
| 297 | ){ |
| 298 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 299 | char *zVfs = 0; |
drh | 841810c | 2013-04-08 13:59:11 +0000 | [diff] [blame] | 300 | UNUSED_PARAMETER(argc); |
| 301 | UNUSED_PARAMETER(argv); |
drh | 1bf44c7 | 2013-04-08 13:48:29 +0000 | [diff] [blame] | 302 | sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zVfs); |
| 303 | if( zVfs ){ |
| 304 | sqlite3_result_text(context, zVfs, -1, sqlite3_free); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 309 | ** Busy handler with a g.iTimeout-millisecond timeout |
| 310 | */ |
| 311 | static int busyHandler(void *pCD, int count){ |
drh | 841810c | 2013-04-08 13:59:11 +0000 | [diff] [blame] | 312 | UNUSED_PARAMETER(pCD); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 313 | if( count*10>g.iTimeout ){ |
| 314 | if( g.iTimeout>0 ) errorMessage("timeout after %dms", g.iTimeout); |
| 315 | return 0; |
| 316 | } |
| 317 | sqlite3_sleep(10); |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | /* |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 322 | ** SQL Trace callback |
| 323 | */ |
| 324 | static void sqlTraceCallback(void *NotUsed1, const char *zSql){ |
drh | 841810c | 2013-04-08 13:59:11 +0000 | [diff] [blame] | 325 | UNUSED_PARAMETER(NotUsed1); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 326 | logMessage("[%.*s]", clipLength(zSql), zSql); |
| 327 | } |
| 328 | |
| 329 | /* |
drh | 1790bb3 | 2013-04-06 14:30:29 +0000 | [diff] [blame] | 330 | ** SQL error log callback |
| 331 | */ |
| 332 | static void sqlErrorCallback(void *pArg, int iErrCode, const char *zMsg){ |
drh | 841810c | 2013-04-08 13:59:11 +0000 | [diff] [blame] | 333 | UNUSED_PARAMETER(pArg); |
drh | 1790bb3 | 2013-04-06 14:30:29 +0000 | [diff] [blame] | 334 | if( (iErrCode&0xff)==SQLITE_SCHEMA && g.iTrace<3 ) return; |
drh | e5ebd22 | 2013-04-08 15:36:51 +0000 | [diff] [blame] | 335 | if( g.iTimeout==0 && (iErrCode&0xff)==SQLITE_BUSY && g.iTrace<3 ) return; |
drh | e3be8c8 | 2013-04-11 11:53:45 +0000 | [diff] [blame] | 336 | if( (iErrCode&0xff)==SQLITE_NOTICE ){ |
drh | ab755ac | 2013-04-09 18:36:36 +0000 | [diff] [blame] | 337 | logMessage("(info) %s", zMsg); |
| 338 | }else{ |
| 339 | errorMessage("(errcode=%d) %s", iErrCode, zMsg); |
| 340 | } |
drh | 1790bb3 | 2013-04-06 14:30:29 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 344 | ** Prepare an SQL statement. Issue a fatal error if unable. |
| 345 | */ |
| 346 | static sqlite3_stmt *prepareSql(const char *zFormat, ...){ |
| 347 | va_list ap; |
| 348 | char *zSql; |
| 349 | int rc; |
| 350 | sqlite3_stmt *pStmt = 0; |
| 351 | va_start(ap, zFormat); |
| 352 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 353 | va_end(ap); |
| 354 | rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0); |
| 355 | if( rc!=SQLITE_OK ){ |
| 356 | sqlite3_finalize(pStmt); |
| 357 | fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql); |
| 358 | } |
| 359 | sqlite3_free(zSql); |
| 360 | return pStmt; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | ** Run arbitrary SQL. Issue a fatal error on failure. |
| 365 | */ |
| 366 | static void runSql(const char *zFormat, ...){ |
| 367 | va_list ap; |
| 368 | char *zSql; |
| 369 | int rc; |
| 370 | va_start(ap, zFormat); |
| 371 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 372 | va_end(ap); |
| 373 | rc = sqlite3_exec(g.db, zSql, 0, 0, 0); |
| 374 | if( rc!=SQLITE_OK ){ |
| 375 | fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql); |
| 376 | } |
| 377 | sqlite3_free(zSql); |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | ** Try to run arbitrary SQL. Return success code. |
| 382 | */ |
| 383 | static int trySql(const char *zFormat, ...){ |
| 384 | va_list ap; |
| 385 | char *zSql; |
| 386 | int rc; |
| 387 | va_start(ap, zFormat); |
| 388 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 389 | va_end(ap); |
| 390 | rc = sqlite3_exec(g.db, zSql, 0, 0, 0); |
| 391 | sqlite3_free(zSql); |
| 392 | return rc; |
| 393 | } |
| 394 | |
| 395 | /* Structure for holding an arbitrary length string |
| 396 | */ |
| 397 | typedef struct String String; |
| 398 | struct String { |
| 399 | char *z; /* the string */ |
| 400 | int n; /* Slots of z[] used */ |
| 401 | int nAlloc; /* Slots of z[] allocated */ |
| 402 | }; |
| 403 | |
| 404 | /* Free a string */ |
| 405 | static void stringFree(String *p){ |
| 406 | if( p->z ) sqlite3_free(p->z); |
| 407 | memset(p, 0, sizeof(*p)); |
| 408 | } |
| 409 | |
| 410 | /* Append n bytes of text to a string. If n<0 append the entire string. */ |
| 411 | static void stringAppend(String *p, const char *z, int n){ |
| 412 | if( n<0 ) n = (int)strlen(z); |
| 413 | if( p->n+n>=p->nAlloc ){ |
| 414 | int nAlloc = p->nAlloc*2 + n + 100; |
| 415 | char *z = sqlite3_realloc(p->z, nAlloc); |
| 416 | if( z==0 ) fatalError("out of memory"); |
| 417 | p->z = z; |
| 418 | p->nAlloc = nAlloc; |
| 419 | } |
| 420 | memcpy(p->z+p->n, z, n); |
| 421 | p->n += n; |
| 422 | p->z[p->n] = 0; |
| 423 | } |
| 424 | |
| 425 | /* Reset a string to an empty string */ |
| 426 | static void stringReset(String *p){ |
| 427 | if( p->z==0 ) stringAppend(p, " ", 1); |
| 428 | p->n = 0; |
| 429 | p->z[0] = 0; |
| 430 | } |
| 431 | |
| 432 | /* Append a new token onto the end of the string */ |
| 433 | static void stringAppendTerm(String *p, const char *z){ |
| 434 | int i; |
| 435 | if( p->n ) stringAppend(p, " ", 1); |
| 436 | if( z==0 ){ |
| 437 | stringAppend(p, "nil", 3); |
| 438 | return; |
| 439 | } |
| 440 | for(i=0; z[i] && !isspace(z[i]); i++){} |
| 441 | if( i>0 && z[i]==0 ){ |
| 442 | stringAppend(p, z, i); |
| 443 | return; |
| 444 | } |
| 445 | stringAppend(p, "'", 1); |
| 446 | while( z[0] ){ |
| 447 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 448 | if( z[i] ){ |
| 449 | stringAppend(p, z, i+1); |
| 450 | stringAppend(p, "'", 1); |
| 451 | z += i+1; |
| 452 | }else{ |
| 453 | stringAppend(p, z, i); |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | stringAppend(p, "'", 1); |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | ** Callback function for evalSql() |
| 462 | */ |
| 463 | static int evalCallback(void *pCData, int argc, char **argv, char **azCol){ |
| 464 | String *p = (String*)pCData; |
| 465 | int i; |
drh | 841810c | 2013-04-08 13:59:11 +0000 | [diff] [blame] | 466 | UNUSED_PARAMETER(azCol); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 467 | for(i=0; i<argc; i++) stringAppendTerm(p, argv[i]); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | ** Run arbitrary SQL and record the results in an output string |
| 473 | ** given by the first parameter. |
| 474 | */ |
| 475 | static int evalSql(String *p, const char *zFormat, ...){ |
| 476 | va_list ap; |
| 477 | char *zSql; |
| 478 | int rc; |
| 479 | char *zErrMsg = 0; |
| 480 | va_start(ap, zFormat); |
| 481 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 482 | va_end(ap); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 483 | assert( g.iTimeout>0 ); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 484 | rc = sqlite3_exec(g.db, zSql, evalCallback, p, &zErrMsg); |
| 485 | sqlite3_free(zSql); |
| 486 | if( rc ){ |
| 487 | char zErr[30]; |
| 488 | sqlite3_snprintf(sizeof(zErr), zErr, "error(%d)", rc); |
| 489 | stringAppendTerm(p, zErr); |
| 490 | if( zErrMsg ){ |
| 491 | stringAppendTerm(p, zErrMsg); |
| 492 | sqlite3_free(zErrMsg); |
| 493 | } |
| 494 | } |
| 495 | return rc; |
| 496 | } |
| 497 | |
| 498 | /* |
drh | 1bf44c7 | 2013-04-08 13:48:29 +0000 | [diff] [blame] | 499 | ** Auxiliary SQL function to recursively evaluate SQL. |
| 500 | */ |
| 501 | static void evalFunc( |
| 502 | sqlite3_context *context, |
| 503 | int argc, |
| 504 | sqlite3_value **argv |
| 505 | ){ |
| 506 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 507 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 508 | String res; |
| 509 | char *zErrMsg = 0; |
| 510 | int rc; |
drh | 841810c | 2013-04-08 13:59:11 +0000 | [diff] [blame] | 511 | UNUSED_PARAMETER(argc); |
drh | 1bf44c7 | 2013-04-08 13:48:29 +0000 | [diff] [blame] | 512 | memset(&res, 0, sizeof(res)); |
| 513 | rc = sqlite3_exec(db, zSql, evalCallback, &res, &zErrMsg); |
| 514 | if( zErrMsg ){ |
| 515 | sqlite3_result_error(context, zErrMsg, -1); |
| 516 | sqlite3_free(zErrMsg); |
| 517 | }else if( rc ){ |
| 518 | sqlite3_result_error_code(context, rc); |
| 519 | }else{ |
| 520 | sqlite3_result_text(context, res.z, -1, SQLITE_TRANSIENT); |
| 521 | } |
| 522 | stringFree(&res); |
| 523 | } |
| 524 | |
| 525 | /* |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 526 | ** Look up the next task for client iClient in the database. |
| 527 | ** Return the task script and the task number and mark that |
| 528 | ** task as being under way. |
| 529 | */ |
| 530 | static int startScript( |
| 531 | int iClient, /* The client number */ |
| 532 | char **pzScript, /* Write task script here */ |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 533 | int *pTaskId, /* Write task number here */ |
| 534 | char **pzTaskName /* Name of the task */ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 535 | ){ |
| 536 | sqlite3_stmt *pStmt = 0; |
| 537 | int taskId; |
| 538 | int rc; |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 539 | int totalTime = 0; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 540 | |
| 541 | *pzScript = 0; |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 542 | g.iTimeout = 0; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 543 | while(1){ |
drh | f90e50f | 2013-04-08 19:13:48 +0000 | [diff] [blame] | 544 | rc = trySql("BEGIN IMMEDIATE"); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 545 | if( rc==SQLITE_BUSY ){ |
| 546 | sqlite3_sleep(10); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 547 | totalTime += 10; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 548 | continue; |
| 549 | } |
| 550 | if( rc!=SQLITE_OK ){ |
drh | 6adab7a | 2013-04-08 18:58:00 +0000 | [diff] [blame] | 551 | fatalError("in startScript: %s", sqlite3_errmsg(g.db)); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 552 | } |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 553 | if( g.nError || g.nTest ){ |
| 554 | runSql("UPDATE counters SET nError=nError+%d, nTest=nTest+%d", |
| 555 | g.nError, g.nTest); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 556 | g.nError = 0; |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 557 | g.nTest = 0; |
| 558 | } |
| 559 | pStmt = prepareSql("SELECT 1 FROM client WHERE id=%d AND wantHalt",iClient); |
| 560 | rc = sqlite3_step(pStmt); |
| 561 | sqlite3_finalize(pStmt); |
| 562 | if( rc==SQLITE_ROW ){ |
| 563 | runSql("DELETE FROM client WHERE id=%d", iClient); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 564 | g.iTimeout = DEFAULT_TIMEOUT; |
drh | f90e50f | 2013-04-08 19:13:48 +0000 | [diff] [blame] | 565 | runSql("COMMIT TRANSACTION;"); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 566 | return SQLITE_DONE; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 567 | } |
| 568 | pStmt = prepareSql( |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 569 | "SELECT script, id, name FROM task" |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 570 | " WHERE client=%d AND starttime IS NULL" |
| 571 | " ORDER BY id LIMIT 1", iClient); |
| 572 | rc = sqlite3_step(pStmt); |
| 573 | if( rc==SQLITE_ROW ){ |
| 574 | int n = sqlite3_column_bytes(pStmt, 0); |
drh | f90e50f | 2013-04-08 19:13:48 +0000 | [diff] [blame] | 575 | *pzScript = sqlite3_malloc(n+1); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 576 | strcpy(*pzScript, (const char*)sqlite3_column_text(pStmt, 0)); |
| 577 | *pTaskId = taskId = sqlite3_column_int(pStmt, 1); |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 578 | *pzTaskName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 2)); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 579 | sqlite3_finalize(pStmt); |
| 580 | runSql("UPDATE task" |
| 581 | " SET starttime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')" |
| 582 | " WHERE id=%d;", taskId); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 583 | g.iTimeout = DEFAULT_TIMEOUT; |
drh | f90e50f | 2013-04-08 19:13:48 +0000 | [diff] [blame] | 584 | runSql("COMMIT TRANSACTION;"); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 585 | return SQLITE_OK; |
| 586 | } |
| 587 | sqlite3_finalize(pStmt); |
| 588 | if( rc==SQLITE_DONE ){ |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 589 | if( totalTime>30000 ){ |
| 590 | errorMessage("Waited over 30 seconds with no work. Giving up."); |
| 591 | runSql("DELETE FROM client WHERE id=%d; COMMIT;", iClient); |
| 592 | sqlite3_close(g.db); |
| 593 | exit(1); |
| 594 | } |
drh | f90e50f | 2013-04-08 19:13:48 +0000 | [diff] [blame] | 595 | while( trySql("COMMIT")==SQLITE_BUSY ){ |
| 596 | sqlite3_sleep(10); |
| 597 | totalTime += 10; |
| 598 | } |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 599 | sqlite3_sleep(100); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 600 | totalTime += 100; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 601 | continue; |
| 602 | } |
| 603 | fatalError("%s", sqlite3_errmsg(g.db)); |
| 604 | } |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 605 | g.iTimeout = DEFAULT_TIMEOUT; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 609 | ** Mark a script as having finished. Remove the CLIENT table entry |
| 610 | ** if bShutdown is true. |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 611 | */ |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 612 | static int finishScript(int iClient, int taskId, int bShutdown){ |
| 613 | runSql("UPDATE task" |
| 614 | " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')" |
| 615 | " WHERE id=%d;", taskId); |
| 616 | if( bShutdown ){ |
| 617 | runSql("DELETE FROM client WHERE id=%d", iClient); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 618 | } |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 619 | return SQLITE_OK; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | ** Start up a client process for iClient, if it is not already |
| 624 | ** running. If the client is already running, then this routine |
| 625 | ** is a no-op. |
| 626 | */ |
| 627 | static void startClient(int iClient){ |
| 628 | runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient); |
| 629 | if( sqlite3_changes(g.db) ){ |
| 630 | char *zSys; |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 631 | int rc; |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 632 | zSys = sqlite3_mprintf( |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 633 | "%s \"%s\" --client %d --trace %d %s%s%s", |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 634 | g.argv0, g.zDbFile, iClient, g.iTrace, |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 635 | g.bSqlTrace ? "--sqltrace " : "", |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 636 | g.bSync ? "--sync " : "", |
| 637 | CMDLINE_SUFFIX |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 638 | ); |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 639 | #if !defined(_WIN32) |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 640 | rc = system(zSys); |
| 641 | if( rc ) errorMessage("system() fails with error code %d", rc); |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 642 | #else |
| 643 | { |
| 644 | STARTUPINFOA startupInfo; |
| 645 | PROCESS_INFORMATION processInfo; |
| 646 | memset(&startupInfo, 0, sizeof(startupInfo)); |
| 647 | startupInfo.cb = sizeof(startupInfo); |
| 648 | memset(&processInfo, 0, sizeof(processInfo)); |
| 649 | rc = CreateProcessA(NULL, zSys, NULL, NULL, FALSE, 0, NULL, NULL, |
| 650 | &startupInfo, &processInfo); |
| 651 | if( rc ){ |
| 652 | CloseHandle(processInfo.hThread); |
| 653 | CloseHandle(processInfo.hProcess); |
| 654 | }else{ |
| 655 | errorMessage("CreateProcessA() fails with error code %lu", |
| 656 | GetLastError()); |
| 657 | } |
drh | f012ae0 | 2013-04-06 14:04:22 +0000 | [diff] [blame] | 658 | } |
drh | f012ae0 | 2013-04-06 14:04:22 +0000 | [diff] [blame] | 659 | #endif |
mistachkin | 08d4189 | 2013-04-11 00:09:44 +0000 | [diff] [blame] | 660 | sqlite3_free(zSys); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 661 | } |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | ** Read the entire content of a file into memory |
| 666 | */ |
| 667 | static char *readFile(const char *zFilename){ |
| 668 | FILE *in = fopen(zFilename, "rb"); |
| 669 | long sz; |
| 670 | char *z; |
| 671 | if( in==0 ){ |
| 672 | fatalError("cannot open \"%s\" for reading", zFilename); |
| 673 | } |
| 674 | fseek(in, 0, SEEK_END); |
| 675 | sz = ftell(in); |
| 676 | rewind(in); |
| 677 | z = sqlite3_malloc( sz+1 ); |
| 678 | sz = (long)fread(z, 1, sz, in); |
| 679 | z[sz] = 0; |
| 680 | fclose(in); |
| 681 | return z; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | ** Return the length of the next token. |
| 686 | */ |
| 687 | static int tokenLength(const char *z, int *pnLine){ |
| 688 | int n = 0; |
| 689 | if( isspace(z[0]) || (z[0]=='/' && z[1]=='*') ){ |
| 690 | int inC = 0; |
| 691 | int c; |
| 692 | if( z[0]=='/' ){ |
| 693 | inC = 1; |
| 694 | n = 2; |
| 695 | } |
| 696 | while( (c = z[n++])!=0 ){ |
| 697 | if( c=='\n' ) (*pnLine)++; |
| 698 | if( isspace(c) ) continue; |
| 699 | if( inC && c=='*' && z[n]=='/' ){ |
| 700 | n++; |
| 701 | inC = 0; |
| 702 | }else if( !inC && c=='/' && z[n]=='*' ){ |
| 703 | n++; |
| 704 | inC = 1; |
| 705 | }else if( !inC ){ |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | n--; |
| 710 | }else if( z[0]=='-' && z[1]=='-' ){ |
| 711 | for(n=2; z[n] && z[n]!='\n'; n++){} |
| 712 | if( z[n] ){ (*pnLine)++; n++; } |
| 713 | }else if( z[0]=='"' || z[0]=='\'' ){ |
| 714 | int delim = z[0]; |
| 715 | for(n=1; z[n]; n++){ |
| 716 | if( z[n]=='\n' ) (*pnLine)++; |
| 717 | if( z[n]==delim ){ |
| 718 | n++; |
| 719 | if( z[n+1]!=delim ) break; |
| 720 | } |
| 721 | } |
| 722 | }else{ |
| 723 | int c; |
| 724 | for(n=1; (c = z[n])!=0 && !isspace(c) && c!='"' && c!='\'' && c!=';'; n++){} |
| 725 | } |
| 726 | return n; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | ** Copy a single token into a string buffer. |
| 731 | */ |
| 732 | static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){ |
| 733 | int i; |
| 734 | if( nIn<=0 ){ |
| 735 | zOut[0] = 0; |
| 736 | return 0; |
| 737 | } |
| 738 | for(i=0; i<nIn && i<nOut-1 && !isspace(zIn[i]); i++){ zOut[i] = zIn[i]; } |
| 739 | zOut[i] = 0; |
| 740 | return i; |
| 741 | } |
| 742 | |
| 743 | /* |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 744 | ** Find the number of characters up to the start of the next "--end" token. |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 745 | */ |
| 746 | static int findEnd(const char *z, int *pnLine){ |
| 747 | int n = 0; |
| 748 | while( z[n] && (strncmp(z+n,"--end",5) || !isspace(z[n+5])) ){ |
| 749 | n += tokenLength(z+n, pnLine); |
| 750 | } |
| 751 | return n; |
| 752 | } |
| 753 | |
| 754 | /* |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 755 | ** Find the number of characters up to the first character past the |
| 756 | ** of the next "--endif" or "--else" token. Nested --if commands are |
| 757 | ** also skipped. |
| 758 | */ |
| 759 | static int findEndif(const char *z, int stopAtElse, int *pnLine){ |
| 760 | int n = 0; |
| 761 | while( z[n] ){ |
| 762 | int len = tokenLength(z+n, pnLine); |
| 763 | if( (strncmp(z+n,"--endif",7)==0 && isspace(z[n+7])) |
| 764 | || (stopAtElse && strncmp(z+n,"--else",6)==0 && isspace(z[n+6])) |
| 765 | ){ |
| 766 | return n+len; |
| 767 | } |
| 768 | if( strncmp(z+n,"--if",4)==0 && isspace(z[n+4]) ){ |
| 769 | int skip = findEndif(z+n+len, 0, pnLine); |
| 770 | n += skip + len; |
| 771 | }else{ |
| 772 | n += len; |
| 773 | } |
| 774 | } |
| 775 | return n; |
| 776 | } |
| 777 | |
| 778 | /* |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 779 | ** Wait for a client process to complete all its tasks |
| 780 | */ |
| 781 | static void waitForClient(int iClient, int iTimeout, char *zErrPrefix){ |
| 782 | sqlite3_stmt *pStmt; |
| 783 | int rc; |
| 784 | if( iClient>0 ){ |
| 785 | pStmt = prepareSql( |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 786 | "SELECT 1 FROM task" |
| 787 | " WHERE client=%d" |
| 788 | " AND client IN (SELECT id FROM client)" |
| 789 | " AND endtime IS NULL", |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 790 | iClient); |
| 791 | }else{ |
| 792 | pStmt = prepareSql( |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 793 | "SELECT 1 FROM task" |
| 794 | " WHERE client IN (SELECT id FROM client)" |
| 795 | " AND endtime IS NULL"); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 796 | } |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 797 | g.iTimeout = 0; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 798 | while( ((rc = sqlite3_step(pStmt))==SQLITE_BUSY || rc==SQLITE_ROW) |
| 799 | && iTimeout>0 |
| 800 | ){ |
| 801 | sqlite3_reset(pStmt); |
| 802 | sqlite3_sleep(50); |
| 803 | iTimeout -= 50; |
| 804 | } |
| 805 | sqlite3_finalize(pStmt); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 806 | g.iTimeout = DEFAULT_TIMEOUT; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 807 | if( rc!=SQLITE_DONE ){ |
| 808 | if( zErrPrefix==0 ) zErrPrefix = ""; |
| 809 | if( iClient>0 ){ |
| 810 | errorMessage("%stimeout waiting for client %d", zErrPrefix, iClient); |
| 811 | }else{ |
| 812 | errorMessage("%stimeout waiting for all clients", zErrPrefix); |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 817 | /* Return a pointer to the tail of a filename |
| 818 | */ |
| 819 | static char *filenameTail(char *z){ |
| 820 | int i, j; |
| 821 | for(i=j=0; z[i]; i++) if( z[i]=='/' ) j = i+1; |
| 822 | return z+j; |
| 823 | } |
| 824 | |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 825 | /* Maximum number of arguments to a --command */ |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 826 | #define MX_ARG 2 |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 827 | |
| 828 | /* |
| 829 | ** Run a script. |
| 830 | */ |
| 831 | static void runScript( |
| 832 | int iClient, /* The client number, or 0 for the master */ |
| 833 | int taskId, /* The task ID for clients. 0 for master */ |
| 834 | char *zScript, /* Text of the script */ |
| 835 | char *zFilename /* File from which script was read. */ |
| 836 | ){ |
| 837 | int lineno = 1; |
| 838 | int prevLine = 1; |
| 839 | int ii = 0; |
| 840 | int iBegin = 0; |
| 841 | int n, c, j; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 842 | int len; |
| 843 | int nArg; |
| 844 | String sResult; |
| 845 | char zCmd[30]; |
| 846 | char zError[1000]; |
| 847 | char azArg[MX_ARG][100]; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 848 | |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 849 | memset(&sResult, 0, sizeof(sResult)); |
| 850 | stringReset(&sResult); |
| 851 | while( (c = zScript[ii])!=0 ){ |
| 852 | prevLine = lineno; |
| 853 | len = tokenLength(zScript+ii, &lineno); |
| 854 | if( isspace(c) || (c=='/' && zScript[ii+1]=='*') ){ |
| 855 | ii += len; |
| 856 | continue; |
| 857 | } |
| 858 | if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){ |
| 859 | ii += len; |
| 860 | continue; |
| 861 | } |
| 862 | |
| 863 | /* Run any prior SQL before processing the new --command */ |
| 864 | if( ii>iBegin ){ |
| 865 | char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin); |
| 866 | evalSql(&sResult, zSql); |
| 867 | sqlite3_free(zSql); |
| 868 | iBegin = ii + len; |
| 869 | } |
| 870 | |
| 871 | /* Parse the --command */ |
| 872 | if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii); |
| 873 | n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd)); |
| 874 | for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){ |
| 875 | while( n<len-2 && isspace(zScript[ii+2+n]) ){ n++; } |
| 876 | if( n>=len-2 ) break; |
| 877 | n += extractToken(zScript+ii+2+n, len-2-n, |
| 878 | azArg[nArg], sizeof(azArg[nArg])); |
| 879 | } |
| 880 | for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0; |
| 881 | |
| 882 | /* |
| 883 | ** --sleep N |
| 884 | ** |
| 885 | ** Pause for N milliseconds |
| 886 | */ |
| 887 | if( strcmp(zCmd, "sleep")==0 ){ |
| 888 | sqlite3_sleep(atoi(azArg[0])); |
| 889 | }else |
| 890 | |
| 891 | /* |
| 892 | ** --exit N |
| 893 | ** |
| 894 | ** Exit this process. If N>0 then exit without shutting down |
| 895 | ** SQLite. (In other words, simulate a crash.) |
| 896 | */ |
drh | 6adab7a | 2013-04-08 18:58:00 +0000 | [diff] [blame] | 897 | if( strcmp(zCmd, "exit")==0 && iClient>0 ){ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 898 | int rc = atoi(azArg[0]); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 899 | finishScript(iClient, taskId, 1); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 900 | if( rc==0 ) sqlite3_close(g.db); |
| 901 | exit(rc); |
| 902 | }else |
| 903 | |
| 904 | /* |
drh | 6adab7a | 2013-04-08 18:58:00 +0000 | [diff] [blame] | 905 | ** --finish |
| 906 | ** |
| 907 | ** Mark the current task as having finished, even if it is not. |
| 908 | ** This can be used in conjunction with --exit to simulate a crash. |
| 909 | */ |
| 910 | if( strcmp(zCmd, "finish")==0 && iClient>0 ){ |
| 911 | finishScript(iClient, taskId, 1); |
| 912 | }else |
| 913 | |
| 914 | /* |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 915 | ** --reset |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 916 | ** |
| 917 | ** Reset accumulated results back to an empty string |
| 918 | */ |
| 919 | if( strcmp(zCmd, "reset")==0 ){ |
| 920 | stringReset(&sResult); |
| 921 | }else |
| 922 | |
| 923 | /* |
| 924 | ** --match ANSWER... |
| 925 | ** |
| 926 | ** Check to see if output matches ANSWER. Report an error if not. |
| 927 | */ |
| 928 | if( strcmp(zCmd, "match")==0 ){ |
| 929 | int jj; |
| 930 | char *zAns = zScript+ii; |
| 931 | for(jj=7; jj<len-1 && isspace(zAns[jj]); jj++){} |
| 932 | zAns += jj; |
| 933 | if( strncmp(sResult.z, zAns, len-jj-1) ){ |
| 934 | errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]", |
| 935 | prevLine, zFilename, len-jj-1, zAns, sResult.z); |
| 936 | } |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 937 | g.nTest++; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 938 | stringReset(&sResult); |
| 939 | }else |
| 940 | |
| 941 | /* |
drh | 1bf44c7 | 2013-04-08 13:48:29 +0000 | [diff] [blame] | 942 | ** --output |
| 943 | ** |
| 944 | ** Output the result of the previous SQL. |
| 945 | */ |
| 946 | if( strcmp(zCmd, "output")==0 ){ |
| 947 | logMessage("%s", sResult.z); |
| 948 | }else |
| 949 | |
| 950 | /* |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 951 | ** --source FILENAME |
| 952 | ** |
| 953 | ** Run a subscript from a separate file. |
| 954 | */ |
| 955 | if( strcmp(zCmd, "source")==0 ){ |
drh | e348fc7 | 2013-04-06 18:35:07 +0000 | [diff] [blame] | 956 | char *zNewFile, *zNewScript; |
| 957 | char *zToDel = 0; |
| 958 | zNewFile = azArg[0]; |
| 959 | if( zNewFile[0]!='/' ){ |
| 960 | int k; |
| 961 | for(k=(int)strlen(zFilename)-1; k>=0 && zFilename[k]!='/'; k--){} |
| 962 | if( k>0 ){ |
| 963 | zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile); |
| 964 | } |
| 965 | } |
| 966 | zNewScript = readFile(zNewFile); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 967 | if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile); |
| 968 | runScript(0, 0, zNewScript, zNewFile); |
| 969 | sqlite3_free(zNewScript); |
| 970 | if( g.iTrace ) logMessage("end script [%s]\n", zNewFile); |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 971 | sqlite3_free(zToDel); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 972 | }else |
| 973 | |
| 974 | /* |
| 975 | ** --print MESSAGE.... |
| 976 | ** |
| 977 | ** Output the remainder of the line to the log file |
| 978 | */ |
| 979 | if( strcmp(zCmd, "print")==0 ){ |
| 980 | int jj; |
| 981 | for(jj=7; jj<len && isspace(zScript[ii+jj]); jj++){} |
| 982 | logMessage("%.*s", len-jj, zScript+ii+jj); |
| 983 | }else |
| 984 | |
| 985 | /* |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 986 | ** --if EXPR |
| 987 | ** |
| 988 | ** Skip forward to the next matching --endif or --else if EXPR is false. |
| 989 | */ |
| 990 | if( strcmp(zCmd, "if")==0 ){ |
| 991 | int jj, rc; |
| 992 | sqlite3_stmt *pStmt; |
| 993 | for(jj=4; jj<len && isspace(zScript[ii+jj]); jj++){} |
| 994 | pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj); |
| 995 | rc = sqlite3_step(pStmt); |
| 996 | if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){ |
| 997 | ii += findEndif(zScript+ii+len, 1, &lineno); |
| 998 | } |
| 999 | sqlite3_finalize(pStmt); |
| 1000 | }else |
| 1001 | |
| 1002 | /* |
| 1003 | ** --else |
| 1004 | ** |
| 1005 | ** This command can only be encountered if currently inside an --if that |
| 1006 | ** is true. Skip forward to the next matching --endif. |
| 1007 | */ |
| 1008 | if( strcmp(zCmd, "else")==0 ){ |
| 1009 | ii += findEndif(zScript+ii+len, 0, &lineno); |
| 1010 | }else |
| 1011 | |
| 1012 | /* |
| 1013 | ** --endif |
| 1014 | ** |
| 1015 | ** This command can only be encountered if currently inside an --if that |
| 1016 | ** is true or an --else of a false if. This is a no-op. |
| 1017 | */ |
| 1018 | if( strcmp(zCmd, "endif")==0 ){ |
| 1019 | /* no-op */ |
| 1020 | }else |
| 1021 | |
| 1022 | /* |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1023 | ** --start CLIENT |
| 1024 | ** |
| 1025 | ** Start up the given client. |
| 1026 | */ |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 1027 | if( strcmp(zCmd, "start")==0 && iClient==0 ){ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1028 | int iNewClient = atoi(azArg[0]); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1029 | if( iNewClient>0 ){ |
| 1030 | startClient(iNewClient); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1031 | } |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1032 | }else |
| 1033 | |
| 1034 | /* |
| 1035 | ** --wait CLIENT TIMEOUT |
| 1036 | ** |
| 1037 | ** Wait until all tasks complete for the given client. If CLIENT is |
| 1038 | ** "all" then wait for all clients to complete. Wait no longer than |
| 1039 | ** TIMEOUT milliseconds (default 10,000) |
| 1040 | */ |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 1041 | if( strcmp(zCmd, "wait")==0 && iClient==0 ){ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1042 | int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000; |
| 1043 | sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n", |
| 1044 | prevLine, zFilename); |
| 1045 | waitForClient(atoi(azArg[0]), iTimeout, zError); |
| 1046 | }else |
| 1047 | |
| 1048 | /* |
| 1049 | ** --task CLIENT |
| 1050 | ** <task-content-here> |
| 1051 | ** --end |
| 1052 | ** |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1053 | ** Assign work to a client. Start the client if it is not running |
| 1054 | ** already. |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1055 | */ |
drh | 7dfe8e2 | 2013-04-08 13:13:43 +0000 | [diff] [blame] | 1056 | if( strcmp(zCmd, "task")==0 && iClient==0 ){ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1057 | int iTarget = atoi(azArg[0]); |
| 1058 | int iEnd; |
| 1059 | char *zTask; |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1060 | char *zTName; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1061 | iEnd = findEnd(zScript+ii+len, &lineno); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1062 | if( iTarget<0 ){ |
| 1063 | errorMessage("line %d of %s: bad client number: %d", |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1064 | prevLine, zFilename, iTarget); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1065 | }else{ |
| 1066 | zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len); |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1067 | if( nArg>1 ){ |
| 1068 | zTName = sqlite3_mprintf("%s", azArg[1]); |
| 1069 | }else{ |
| 1070 | zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine); |
| 1071 | } |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1072 | startClient(iTarget); |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1073 | runSql("INSERT INTO task(client,script,name)" |
| 1074 | " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1075 | sqlite3_free(zTask); |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1076 | sqlite3_free(zTName); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1077 | } |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1078 | iEnd += tokenLength(zScript+ii+len+iEnd, &lineno); |
| 1079 | len += iEnd; |
| 1080 | iBegin = ii+len; |
| 1081 | }else |
| 1082 | |
| 1083 | /* error */{ |
| 1084 | errorMessage("line %d of %s: unknown command --%s", |
| 1085 | prevLine, zFilename, zCmd); |
| 1086 | } |
| 1087 | ii += len; |
| 1088 | } |
| 1089 | if( iBegin<ii ){ |
| 1090 | char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin); |
| 1091 | runSql(zSql); |
| 1092 | sqlite3_free(zSql); |
| 1093 | } |
| 1094 | stringFree(&sResult); |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | ** Look for a command-line option. If present, return a pointer. |
| 1099 | ** Return NULL if missing. |
| 1100 | ** |
| 1101 | ** hasArg==0 means the option is a flag. It is either present or not. |
| 1102 | ** hasArg==1 means the option has an argument. Return a pointer to the |
| 1103 | ** argument. |
| 1104 | */ |
| 1105 | static char *findOption( |
| 1106 | char **azArg, |
| 1107 | int *pnArg, |
| 1108 | const char *zOption, |
| 1109 | int hasArg |
| 1110 | ){ |
| 1111 | int i, j; |
| 1112 | char *zReturn = 0; |
| 1113 | int nArg = *pnArg; |
| 1114 | |
| 1115 | assert( hasArg==0 || hasArg==1 ); |
| 1116 | for(i=0; i<nArg; i++){ |
| 1117 | const char *z; |
| 1118 | if( i+hasArg >= nArg ) break; |
| 1119 | z = azArg[i]; |
| 1120 | if( z[0]!='-' ) continue; |
| 1121 | z++; |
| 1122 | if( z[0]=='-' ){ |
| 1123 | if( z[1]==0 ) break; |
| 1124 | z++; |
| 1125 | } |
| 1126 | if( strcmp(z,zOption)==0 ){ |
| 1127 | if( hasArg && i==nArg-1 ){ |
| 1128 | fatalError("command-line option \"--%s\" requires an argument", z); |
| 1129 | } |
| 1130 | if( hasArg ){ |
| 1131 | zReturn = azArg[i+1]; |
| 1132 | }else{ |
| 1133 | zReturn = azArg[i]; |
| 1134 | } |
| 1135 | j = i+1+(hasArg!=0); |
| 1136 | while( j<nArg ) azArg[i++] = azArg[j++]; |
| 1137 | *pnArg = i; |
| 1138 | return zReturn; |
| 1139 | } |
| 1140 | } |
| 1141 | return zReturn; |
| 1142 | } |
| 1143 | |
| 1144 | /* Print a usage message for the program and exit */ |
| 1145 | static void usage(const char *argv0){ |
| 1146 | int i; |
| 1147 | const char *zTail = argv0; |
| 1148 | for(i=0; argv0[i]; i++){ |
| 1149 | if( argv0[i]=='/' ) zTail = argv0+i+1; |
| 1150 | } |
| 1151 | fprintf(stderr,"Usage: %s DATABASE ?OPTIONS? ?SCRIPT?\n", zTail); |
| 1152 | exit(1); |
| 1153 | } |
| 1154 | |
| 1155 | /* Report on unrecognized arguments */ |
| 1156 | static void unrecognizedArguments( |
| 1157 | const char *argv0, |
| 1158 | int nArg, |
| 1159 | char **azArg |
| 1160 | ){ |
| 1161 | int i; |
| 1162 | fprintf(stderr,"%s: unrecognized arguments:", argv0); |
| 1163 | for(i=0; i<nArg; i++){ |
| 1164 | fprintf(stderr," %s", azArg[i]); |
| 1165 | } |
| 1166 | fprintf(stderr,"\n"); |
| 1167 | exit(1); |
| 1168 | } |
| 1169 | |
| 1170 | int main(int argc, char **argv){ |
| 1171 | const char *zClient; |
| 1172 | int iClient; |
drh | e348fc7 | 2013-04-06 18:35:07 +0000 | [diff] [blame] | 1173 | int n, i; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1174 | int openFlags = SQLITE_OPEN_READWRITE; |
| 1175 | int rc; |
| 1176 | char *zScript; |
| 1177 | int taskId; |
| 1178 | const char *zTrace; |
drh | e348fc7 | 2013-04-06 18:35:07 +0000 | [diff] [blame] | 1179 | const char *zCOption; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1180 | |
| 1181 | g.argv0 = argv[0]; |
| 1182 | g.iTrace = 1; |
| 1183 | if( argc<2 ) usage(argv[0]); |
| 1184 | g.zDbFile = argv[1]; |
drh | e348fc7 | 2013-04-06 18:35:07 +0000 | [diff] [blame] | 1185 | if( strglob("*.test", g.zDbFile) ) usage(argv[0]); |
| 1186 | if( strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID)!=0 ){ |
| 1187 | fprintf(stderr, "SQLite library and header mismatch\n" |
| 1188 | "Library: %s\n" |
| 1189 | "Header: %s\n", |
| 1190 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 1191 | exit(1); |
| 1192 | } |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1193 | n = argc-2; |
drh | e3be8c8 | 2013-04-11 11:53:45 +0000 | [diff] [blame] | 1194 | sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.mptest", GETPID()); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1195 | g.zVfs = findOption(argv+2, &n, "vfs", 1); |
| 1196 | zClient = findOption(argv+2, &n, "client", 1); |
| 1197 | g.zErrLog = findOption(argv+2, &n, "errlog", 1); |
| 1198 | g.zLog = findOption(argv+2, &n, "log", 1); |
| 1199 | zTrace = findOption(argv+2, &n, "trace", 1); |
| 1200 | if( zTrace ) g.iTrace = atoi(zTrace); |
| 1201 | if( findOption(argv+2, &n, "quiet", 0)!=0 ) g.iTrace = 0; |
| 1202 | g.bSqlTrace = findOption(argv+2, &n, "sqltrace", 0)!=0; |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 1203 | g.bSync = findOption(argv+2, &n, "sync", 0)!=0; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1204 | if( g.zErrLog ){ |
| 1205 | g.pErrLog = fopen(g.zErrLog, "a"); |
| 1206 | }else{ |
| 1207 | g.pErrLog = stderr; |
| 1208 | } |
| 1209 | if( g.zLog ){ |
| 1210 | g.pLog = fopen(g.zLog, "a"); |
| 1211 | }else{ |
| 1212 | g.pLog = stdout; |
| 1213 | } |
drh | e3be8c8 | 2013-04-11 11:53:45 +0000 | [diff] [blame] | 1214 | |
drh | 1790bb3 | 2013-04-06 14:30:29 +0000 | [diff] [blame] | 1215 | sqlite3_config(SQLITE_CONFIG_LOG, sqlErrorCallback, 0); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1216 | if( zClient ){ |
| 1217 | iClient = atoi(zClient); |
| 1218 | if( iClient<1 ) fatalError("illegal client number: %d\n", iClient); |
drh | e3be8c8 | 2013-04-11 11:53:45 +0000 | [diff] [blame] | 1219 | sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d", |
| 1220 | GETPID(), iClient); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1221 | }else{ |
drh | e348fc7 | 2013-04-06 18:35:07 +0000 | [diff] [blame] | 1222 | if( g.iTrace>0 ){ |
| 1223 | printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" ); |
| 1224 | for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){ |
| 1225 | printf("-DSQLITE_%s\n", zCOption); |
| 1226 | } |
| 1227 | fflush(stdout); |
| 1228 | } |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1229 | iClient = 0; |
| 1230 | unlink(g.zDbFile); |
| 1231 | openFlags |= SQLITE_OPEN_CREATE; |
| 1232 | } |
| 1233 | rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs); |
| 1234 | if( rc ) fatalError("cannot open [%s]", g.zDbFile); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1235 | sqlite3_busy_handler(g.db, busyHandler, 0); |
drh | 1bf44c7 | 2013-04-08 13:48:29 +0000 | [diff] [blame] | 1236 | sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0, |
| 1237 | vfsNameFunc, 0, 0); |
| 1238 | sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0, |
| 1239 | evalFunc, 0, 0); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1240 | g.iTimeout = DEFAULT_TIMEOUT; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1241 | if( g.bSqlTrace ) sqlite3_trace(g.db, sqlTraceCallback, 0); |
drh | bc94dbb | 2013-04-08 14:28:33 +0000 | [diff] [blame] | 1242 | if( !g.bSync ) trySql("PRAGMA synchronous=OFF"); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1243 | if( iClient>0 ){ |
| 1244 | if( n>0 ) unrecognizedArguments(argv[0], n, argv+2); |
| 1245 | if( g.iTrace ) logMessage("start-client"); |
| 1246 | while(1){ |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1247 | char *zTaskName = 0; |
| 1248 | rc = startScript(iClient, &zScript, &taskId, &zTaskName); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1249 | if( rc==SQLITE_DONE ) break; |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1250 | if( g.iTrace ) logMessage("begin %s (%d)", zTaskName, taskId); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1251 | runScript(iClient, taskId, zScript, zTaskName); |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1252 | if( g.iTrace ) logMessage("end %s (%d)", zTaskName, taskId); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1253 | finishScript(iClient, taskId, 0); |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1254 | sqlite3_free(zTaskName); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1255 | sqlite3_sleep(10); |
| 1256 | } |
| 1257 | if( g.iTrace ) logMessage("end-client"); |
| 1258 | }else{ |
| 1259 | sqlite3_stmt *pStmt; |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1260 | int iTimeout; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1261 | if( n==0 ){ |
| 1262 | fatalError("missing script filename"); |
| 1263 | } |
| 1264 | if( n>1 ) unrecognizedArguments(argv[0], n, argv+2); |
| 1265 | runSql( |
| 1266 | "CREATE TABLE task(\n" |
| 1267 | " id INTEGER PRIMARY KEY,\n" |
drh | 4c5298f | 2013-04-10 12:01:21 +0000 | [diff] [blame] | 1268 | " name TEXT,\n" |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1269 | " client INTEGER,\n" |
| 1270 | " starttime DATE,\n" |
| 1271 | " endtime DATE,\n" |
| 1272 | " script TEXT\n" |
| 1273 | ");" |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1274 | "CREATE INDEX task_i1 ON task(client, starttime);\n" |
| 1275 | "CREATE INDEX task_i2 ON task(client, endtime);\n" |
| 1276 | "CREATE TABLE counters(nError,nTest);\n" |
| 1277 | "INSERT INTO counters VALUES(0,0);\n" |
| 1278 | "CREATE TABLE client(id INTEGER PRIMARY KEY, wantHalt);\n" |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1279 | ); |
| 1280 | zScript = readFile(argv[2]); |
| 1281 | if( g.iTrace ) logMessage("begin script [%s]\n", argv[2]); |
| 1282 | runScript(0, 0, zScript, argv[2]); |
| 1283 | sqlite3_free(zScript); |
| 1284 | if( g.iTrace ) logMessage("end script [%s]\n", argv[2]); |
| 1285 | waitForClient(0, 2000, "during shutdown...\n"); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1286 | trySql("UPDATE client SET wantHalt=1"); |
| 1287 | sqlite3_sleep(10); |
| 1288 | g.iTimeout = 0; |
| 1289 | iTimeout = 1000; |
| 1290 | while( ((rc = trySql("SELECT 1 FROM client"))==SQLITE_BUSY |
| 1291 | || rc==SQLITE_ROW) && iTimeout>0 ){ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1292 | sqlite3_sleep(10); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1293 | iTimeout -= 10; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1294 | } |
| 1295 | sqlite3_sleep(100); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1296 | pStmt = prepareSql("SELECT nError, nTest FROM counters"); |
| 1297 | iTimeout = 1000; |
| 1298 | while( (rc = sqlite3_step(pStmt))==SQLITE_BUSY && iTimeout>0 ){ |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1299 | sqlite3_sleep(10); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1300 | iTimeout -= 10; |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1301 | } |
| 1302 | if( rc==SQLITE_ROW ){ |
| 1303 | g.nError += sqlite3_column_int(pStmt, 0); |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1304 | g.nTest += sqlite3_column_int(pStmt, 1); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1305 | } |
| 1306 | sqlite3_finalize(pStmt); |
| 1307 | } |
| 1308 | sqlite3_close(g.db); |
| 1309 | maybeClose(g.pLog); |
| 1310 | maybeClose(g.pErrLog); |
| 1311 | if( iClient==0 ){ |
drh | 3f5bc38 | 2013-04-06 13:09:11 +0000 | [diff] [blame] | 1312 | printf("Summary: %d errors in %d tests\n", g.nError, g.nTest); |
drh | 27338e6 | 2013-04-06 00:19:37 +0000 | [diff] [blame] | 1313 | } |
| 1314 | return g.nError>0; |
| 1315 | } |