sqllogictest
Check-in [87d382225d]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Client-side sorting is not in place, but the rest compiles and links.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 87d382225d967d0970c3904ae388807a3e83ee63
User & Date: drh 2008-11-29 21:44:54
Context
2008-11-29
22:31
The sqllogictest program builds and runs on Linux. check-in: 2cffc96fd2 user: drh tags: trunk
21:44
Client-side sorting is not in place, but the rest compiles and links. check-in: 87d382225d user: drh tags: trunk
19:41
Initial check-in of untested code. check-in: 7ab3183840 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added src/Makefile.





























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/make
#
#### The suffix to add to executable files.  ".exe" for windows.
#    Nothing for unix.
#
E =

#### C Compile and options for use in building executables that 
#    will run on the target platform.
#
CC = gcc -g -Wall

#### Extra arguments for linking the finished binary. 
#
LIB = $(LDFLAGS)

# You should not need to change anything below this line
###############################################################################
#
OBJ = \
  sqlite3.o

sqllogictest$(E):	sqllogictest.c sqllogictest.h $(OBJ)
	$(CC) -o sqllogictest$(E) sqllogictest.c $(OBJ)

sqlite3.o:	sqlite3.c sqlite3.h
	$(CC) -c sqlite3.c -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION

clean:
	rm -f $(OBJ)

Changes to src/sqllogictest.c.

22
23
24
25
26
27
28
29


30
31
32
33
34
35
36
..
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
..
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146



147
148
149
150
151
152
153
154
155
156

157
158
159
160
161
162
163
164
165
166
167
168
169



170
171
172


173







174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

196
197
198

199
200
201
202
203


204
205
206
207
208
209
210
211
212
213
214

215
216
217
218
219
220
221
...
225
226
227
228
229
230
231

232
233
234
235
236
237
238
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
...
277
278
279
280
281
282
283

284
285
286
287
288
































































































































289
290
291
292
293
294
295
...
301
302
303
304
305
306
307


308
309
310
311
312
313
314
...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372








373
374
375
376
377
378
379
380
381


382

















































































































































383
384
385
386
387
388
389
390
*******************************************************************************
**
** This main driver for the sqllogictest program.
*/
#include "sqllogictest.h"
#include <stdio.h>
#include <stdlib.h>




/*****************************************************************************
** Here begins the implementation of the SQLite DbEngine object.
**
** Use this interface as a model for other database engine interfaces.
*/
#include "sqlite3.h"
................................................................................
  void *pConn,                /* Connection created by xConnect */
  const char *zSql            /* SQL statement to evaluate */
){
  int rc;
  sqlite3 *db;

  db = (sqlite3*)pConn;
  rc = sqlite3_exec(db, zSql, 0, 0, 0)
  return rc!=SQLITE_OK;
}

/*
** Structure used to accumulate a result set.
*/
typedef struct ResAccum ResAccum;
................................................................................
  char **azValue;   /* Array of pointers to values, each malloced separately */
  int nAlloc;       /* Number of slots allocated in azValue */
  int nUsed;        /* Number of slots in azValue used */
};

/*
** Append a value to a result set.  zValue is copied into memory obtained
** from malloc.  If zValue==0 then a "<NULL>" string is appended.  If
** zValue[0]==0 then an "<EMPTY-STRING>" is appended.  Control characters
** and non-printable characters are converted to "@".
*/
static void appendValue(ResAccum *p, const char *zValue){
  char *z;
  if( zValue==0 ){
    zValue = "<NULL>";
  }else if( zValue[0]==0 ){
    zValue = "<EMPTY-STRING>";
  }
  z = sqlite3_mprintf("%s", zValue);
  if( z==0 ){
    fprintf(stderr, "out of memory at %s:%d\n", __FILE__, __LINE__);
    exit(1);



  }
  if( p->nUsed>=p->nAlloc ){
    char **az;
    p->nAlloc += 200;
    az = sqlite3_realloc(p->azValue, p->nAlloc*sizeof(p->azValue[0]));
    if( az==0 ){
      fprintf(stderr, "out of memory at %s:%d\n", __FILE__, __LINE__);
      exit(1);
    }
    p->azValue = az;
  }
  p->azValue[p->nUsed++] = z;
  while( *z ){
    if( *z<' ' || *z>'~' ) *z = '@';
    z++;
  }
}

/*
** This interface runs a query and accumulates the results into an array
** of pointers to strings.  *pazResult is made to point to the resulting
** array and *pnResult is set to the number of elements in the array.
**
** NULL values in the result set should be represented by a string "<NULL>".
** Empty strings should be shown as "<EMPTY-STRING>".  Unprintable and
** control characters should be rendered as "@".



*/
static int sqliteQuery(
  void *pConn,                /* Connection created by xConnect */
  const char *zSql,           /* SQL statement to evaluate */
  const char *zType,          /* One character for each column of result */
  char ***pazResult,          /* RETURN:  Array of result values */
  int *pnResult               /* RETURN:  Number of result values */
){
  sqlite3 *db;
  sqlite3_stmt *pStmt;

  ResAccum res;
  char zBuffer[200];

  memset(&res, 0, sizeof(res));
  db = (sqlite3*)pConn;
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  if( rc!=SQLITE_OK ){
    sqlite3_finalize(pStmt);
    return 1;
  }
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    int i;
    for(i=0; zType[i]; i++){



      switch( zType[i] ){
        case 'S': {
          char *zValue = sqlite3_column_text(pStmt, i);


          appendValue(&res, zValue);







          break;
        }
        case 'I': {
          int ii = sqlite3_column_int(pStmt, i);
          sqlite3_snprintf(sizeof(zBuffer), zBuffer, "%d", ii);
          appendValue(&res, zValue);
          break;
        }
        case 'R': {
          double r = sqlite3_column_double(pStmt, i);
          sqlite3_snprintf(sizeof(zBuffer), zBuffer, "%.3f", r);
          appendValue(&res, zValue);
          break;
        }
        default: {
          sqlite3_finalize(pStmt);
          fprintf(stderr, "Unknown character in type-string: %c\n", zType[i]);
          return 1;
        }
      }
    }
  }

  sqlite3_finalize(pStmt);
  *pazResult = res.azValue;
  *pnResult = res.nUsed;

}

/*
** This interface is called to free the memory that was returned
** by xQuery.


*/
static int sqliteFreeResults(
  void *pConn,                /* Connection created by xConnect */
  char **azResult,            /* The results to be freed */
  int nResult                 /* Number of rows of result */
){
  int i;
  for(i=0; i<nResult; i++){
    sqlite3_free(azResult[i]);
  }
  sqlite3_free(azResult);

}

/*
** This routine is called to close a connection previously opened
** by xConnect.
**
** This routine may or may not delete the database.  Whichever way
................................................................................
** method.  The SQLite interface takes the latter approach.
*/
static int sqliteDisconnect(
  void *pConn                 /* Connection created by xConnect */
){
  sqlite3 *db = (sqlite3*)pConn;
  sqlite3_close(db);

}

/*
** This routine registers the SQLite database engine with the main
** driver.  New database engine interfaces should have a single
** routine similar to this one.  The main() function below should be
** modified to call that routine upon startup.
................................................................................
  */
  static const DbEngine sqliteDbEngine = {
     "SQLite",             /* zName */
     0,                    /* pAuxData */
     sqliteConnect,        /* xConnect */
     sqliteStatement,      /* xStatement */
     sqliteQuery,          /* xQuery */
     sqliteFreeResult,     /* xFreeResult */
     sqliteDisconnect      /* xDisconnect */
  };
  sqllogictestRegisterEngine(&sqliteDbEngine);
}

/*
**************** End of the SQLite database engine interface *****************
................................................................................
  apEngine[nEngine-1] = p;
}

/*
** Print a usage comment and die
*/
static void usage(const char *argv0){

  fprintf(stderr, "Usage: %s [-verify] [-engine ENGINE] [-connect STR] script",
    argv0);
  exit(1);
}


































































































































/*
** This is the main routine.  This routine runs first.  It processes
** command-line arguments then runs the test.
*/
int main(int argc, char **argv){
  int verifyMode = 0;                  /* True if in -verify mode */
................................................................................
  char *zScript;                       /* Content of the script */
  long nScript;                        /* Size of the script in bytes */
  void *pConn;                         /* Connection to the database engine */
  int rc;                              /* Result code from subroutine call */
  int nErr = 0;                        /* Number of errors */
  int nResult;                         /* Number of query results */
  char **azResult;                     /* Query result vector */


  

  /* Add calls to the registration procedures for new database engine
  ** interfaces here
  */
  registerSqlite();

................................................................................
      usage(argv[0]);
    }
  }

  /* Check for errors and missing arguments.  Find the database engine
  ** to use for this run.
  */
  if( zScript==0 ){
    fprintf(stderr, "%s: no input script specified\n", argv[0]);
    usage(argv[0]);
  }
  for(i=0; i<nEngine; i++){
    if( strcmp(zDbEngine, apEngine[i]->zName)==0 ){
      pEngine = apEngine[i];
      break;
................................................................................
  }

  /*
  ** Read the entire script file content into memory
  */
  in = fopen(zScriptFile, "rb");
  if( in==0 ){
    fprintf(stderr, "%s: cannot open input script %s\n", argv[0], zScriptFile);
    exit(1);
  }
  fseek(in, 0L, SEEK_END);
  nScript = ftell(in);
  zScript = malloc( nScript+1 );
  if( zScript==0 ){
    fprintf(stderr, "%s: out of memory on %s:%d\n", argv[0], __FILE__,__LINE__);
    exit(1);
  }
  fseek(in, 0L, SEEK_SET);
  fread(zScript, 1, nScript, in);
  fclose(in);
  zScript[nScript] = 0;









  /* Open the database engine under test
  */
  rc = pEngine->xConnect(pEngine->pAuxData, zConnectStr, &pConn);
  if( rc ){
    fprintf(stderr, "%s: unable to connect to database\n", argv[0]);
    exit(1);
  }





















































































































































  /* Finally, shutdown the database connection.  Return the number of errors.
  */
  rc = pEngine->xDisconnect(pConn);
  if( rc ){
    fprintf(stderr, "%s: disconnection from database failed\n", argv[0]);
  }
  return nErr; 
}







|
>
>







 







|







 







|
<
<



|
<
<
<
<
|
|
|
|
>
>
>












<
<
<
<










>
>
>








|
|
>
|
|











>
>
>
|
|
|
>
>
|
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>



>





>
>











>







 







>







 







|







 







>
|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>







 







|







 







|






|






>
>
>
>
>
>
>
>



|





>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
...
101
102
103
104
105
106
107
108


109
110
111
112




113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131




132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
...
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
...
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
*******************************************************************************
**
** This main driver for the sqllogictest program.
*/
#include "sqllogictest.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>

/*****************************************************************************
** Here begins the implementation of the SQLite DbEngine object.
**
** Use this interface as a model for other database engine interfaces.
*/
#include "sqlite3.h"
................................................................................
  void *pConn,                /* Connection created by xConnect */
  const char *zSql            /* SQL statement to evaluate */
){
  int rc;
  sqlite3 *db;

  db = (sqlite3*)pConn;
  rc = sqlite3_exec(db, zSql, 0, 0, 0);
  return rc!=SQLITE_OK;
}

/*
** Structure used to accumulate a result set.
*/
typedef struct ResAccum ResAccum;
................................................................................
  char **azValue;   /* Array of pointers to values, each malloced separately */
  int nAlloc;       /* Number of slots allocated in azValue */
  int nUsed;        /* Number of slots in azValue used */
};

/*
** Append a value to a result set.  zValue is copied into memory obtained
** from malloc.  Or if zValue is NULL, then a NULL pointer is appended.


*/
static void appendValue(ResAccum *p, const char *zValue){
  char *z;
  if( zValue ){




    z = sqlite3_mprintf("%s", zValue);
    if( z==0 ){
      fprintf(stderr, "out of memory at %s:%d\n", __FILE__, __LINE__);
      exit(1);
    }
  }else{
    z = 0;
  }
  if( p->nUsed>=p->nAlloc ){
    char **az;
    p->nAlloc += 200;
    az = sqlite3_realloc(p->azValue, p->nAlloc*sizeof(p->azValue[0]));
    if( az==0 ){
      fprintf(stderr, "out of memory at %s:%d\n", __FILE__, __LINE__);
      exit(1);
    }
    p->azValue = az;
  }
  p->azValue[p->nUsed++] = z;




}

/*
** This interface runs a query and accumulates the results into an array
** of pointers to strings.  *pazResult is made to point to the resulting
** array and *pnResult is set to the number of elements in the array.
**
** NULL values in the result set should be represented by a string "<NULL>".
** Empty strings should be shown as "<EMPTY-STRING>".  Unprintable and
** control characters should be rendered as "@".
**
** Return 0 on success and 1 if there is an error.  It is not necessary
** to initialize *pazResult or *pnResult if an error occurs.
*/
static int sqliteQuery(
  void *pConn,                /* Connection created by xConnect */
  const char *zSql,           /* SQL statement to evaluate */
  const char *zType,          /* One character for each column of result */
  char ***pazResult,          /* RETURN:  Array of result values */
  int *pnResult               /* RETURN:  Number of result values */
){
  sqlite3 *db;                /* The database connection */
  sqlite3_stmt *pStmt;        /* Prepared statement */
  int rc;                     /* Result code from subroutine calls */
  ResAccum res;               /* query result accumulator */
  char zBuffer[200];          /* Buffer to render numbers */

  memset(&res, 0, sizeof(res));
  db = (sqlite3*)pConn;
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  if( rc!=SQLITE_OK ){
    sqlite3_finalize(pStmt);
    return 1;
  }
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    int i;
    for(i=0; zType[i]; i++){
      if( sqlite3_column_type(pStmt, i)==SQLITE_NULL ){
        appendValue(&res, "<NULL>");
      }else{
        switch( zType[i] ){
          case 'S': {
            const char *zValue = (const char*)sqlite3_column_text(pStmt, i);
            char *z;
            if( zValue[0]==0 ) zValue = "<EMPTY-STRING>";
            appendValue(&res, zValue);

            /* Convert non-printing and control characters to '@' */
            z = res.azValue[res.nUsed-1];
            while( *z ){
              if( *z<' ' || *z>'~' ){ *z = '@'; }
              z++;
            }
            break;
          }
          case 'I': {
            int ii = sqlite3_column_int(pStmt, i);
            sqlite3_snprintf(sizeof(zBuffer), zBuffer, "%d", ii);
            appendValue(&res, zBuffer);
            break;
          }
          case 'R': {
            double r = sqlite3_column_double(pStmt, i);
            sqlite3_snprintf(sizeof(zBuffer), zBuffer, "%.3f", r);
            appendValue(&res, zBuffer);
            break;
          }
          default: {
            sqlite3_finalize(pStmt);
            fprintf(stderr, "Unknown character in type-string: %c\n", zType[i]);
            return 1;
          }
        }
      }
    }
  }
  sqlite3_finalize(pStmt);
  *pazResult = res.azValue;
  *pnResult = res.nUsed;
  return 0;
}

/*
** This interface is called to free the memory that was returned
** by xQuery.
**
** It might be the case that nResult==0 or azResult==0.
*/
static int sqliteFreeResults(
  void *pConn,                /* Connection created by xConnect */
  char **azResult,            /* The results to be freed */
  int nResult                 /* Number of rows of result */
){
  int i;
  for(i=0; i<nResult; i++){
    sqlite3_free(azResult[i]);
  }
  sqlite3_free(azResult);
  return 0;
}

/*
** This routine is called to close a connection previously opened
** by xConnect.
**
** This routine may or may not delete the database.  Whichever way
................................................................................
** method.  The SQLite interface takes the latter approach.
*/
static int sqliteDisconnect(
  void *pConn                 /* Connection created by xConnect */
){
  sqlite3 *db = (sqlite3*)pConn;
  sqlite3_close(db);
  return 0;
}

/*
** This routine registers the SQLite database engine with the main
** driver.  New database engine interfaces should have a single
** routine similar to this one.  The main() function below should be
** modified to call that routine upon startup.
................................................................................
  */
  static const DbEngine sqliteDbEngine = {
     "SQLite",             /* zName */
     0,                    /* pAuxData */
     sqliteConnect,        /* xConnect */
     sqliteStatement,      /* xStatement */
     sqliteQuery,          /* xQuery */
     sqliteFreeResults,    /* xFreeResults */
     sqliteDisconnect      /* xDisconnect */
  };
  sqllogictestRegisterEngine(&sqliteDbEngine);
}

/*
**************** End of the SQLite database engine interface *****************
................................................................................
  apEngine[nEngine-1] = p;
}

/*
** Print a usage comment and die
*/
static void usage(const char *argv0){
  fprintf(stderr,
    "Usage: %s [-verify] [-engine DBENGINE] [-connection STR] script\n",
    argv0);
  exit(1);
}

/*
** A structure to keep track of the state of scanning the input script.
*/
typedef struct Script Script;
struct Script {
  char *zScript;       /* Complete text of the input script */
  int iCur;            /* Index in zScript of start of current line */
  char *zStart;        /* Pointer to start of current line */
  int len;             /* Length of current line */
  int iNext;           /* index of start of next line */
  int nLine;           /* line number for the current line */
  int iEnd;            /* Index in zScript of '\000' at end of script */
  int startLine;       /* Line number of start of current record */
  int copyFlag;        /* If true, copy lines to output as they are read */
  char azToken[3][20]; /* tokenization of a line */
};

/*
** Advance the cursor to the start of the next non-comment line of the
** script.  Make p->zStart point to the start of the line.  Make p->len
** be the length of the line.  Zero-terminate the line.  Any \r at the
** end of the line is removed.
**
** Return 1 on success.  Return 0 and no-op at end-of-file.
*/
static int nextLine(Script *p){
  int i;

  /* Loop until a non-comment line is found, or until end-of-file */
  while(1){

    /* When we reach end-of-file, return 0 */
    if( p->iNext>=p->iEnd ){
      return 0;
    }

    /* Advance the cursor to the next line */
    p->iCur = p->iNext;
    p->nLine++;
    p->zStart = &p->zScript[p->iCur];
    for(i=p->iCur; i<p->iEnd && p->zScript[i]!='\n'; i++){}
    p->zScript[i] = 0;
    p->len = i - p->iCur - 1;
    p->iNext = i+1;

    /* If the current line ends in a \r then remove the \r. */
    if( p->len>0 && p->zScript[i-1]=='\r' ){
      p->len--;
      p->zScript[i-1] = 0;
    }

    /* If the line consists of all spaces, make it an empty line */
    for(i=i-2; i>=p->iCur && isspace(p->zScript[i]); i--){}
    if( i<p->iCur ){
      p->zStart[0] = 0;
    }

    /* If the copy flag is set, write the line to standard output */
    if( p->copyFlag ){
      printf("%s\n", p->zStart);
    }

    /* If the line is not a comment line, then we are finished, so break
    ** out of the loop.  If the line is a comment, the loop will repeat in
    ** order to skip this line. */
    if( p->zStart[0]!='#' ) break;
  }
  return 1;
}

/*
** Advance the cursor to the start of the next record.  To do this,
** first skip over the tail section of the record in which we are
** currently located, then skip over blank lines.
**
** Return 1 on success.  Return 0 at end-of-file.
*/
static int findStartOfNextRecord(Script *p){

  /* Skip over any existing content to find a blank line */
  while( p->zStart[0] && p->iCur<p->iEnd ){
    nextLine(p);
  }

  /* Skip over one or more blank lines to find the first line of the
  ** new record */
  while( p->zStart[0]==0 && p->iCur<p->iEnd ){
    nextLine(p);
  }

  /* Return 1 if we have not reached end of file. */
  return p->iCur<p->iEnd;
}

/*
** Find a single token in a string.  Return the index of the start
** of the token and the length of the token.
*/
static void findToken(const char *z, int *piStart, int *pLen){
  int i;
  int iStart;
  for(i=0; isspace(z[i]); i++){}
  *piStart = iStart = i;
  while( z[i] && !isspace(z[i]) ){ i++; }
  *pLen = i - iStart;
}

/*
** tokenize the current line in up to 3 tokens and store those values
** into p->azToken[0], p->azToken[1], and p->azToken[2].  Record the
** current line in p->startLine.
*/
static void tokenizeLine(Script *p){
  int i, j, k;
  int len, n;
  for(i=0; i<3; i++) p->azToken[i][0] = 0;
  for(i=j=0; j<p->len && i<3; i++){
    findToken(&p->zStart[j], &k, &len);
    j += k;
    n = len;
    if( n>=sizeof(p->azToken[0]) ){
      n = sizeof(p->azToken[0])-1;
    }
    memcpy(p->azToken[i], &p->zStart[j], n);
    p->azToken[i][n] = 0;
    j += n+1;
  }
}

/*
** This is the main routine.  This routine runs first.  It processes
** command-line arguments then runs the test.
*/
int main(int argc, char **argv){
  int verifyMode = 0;                  /* True if in -verify mode */
................................................................................
  char *zScript;                       /* Content of the script */
  long nScript;                        /* Size of the script in bytes */
  void *pConn;                         /* Connection to the database engine */
  int rc;                              /* Result code from subroutine call */
  int nErr = 0;                        /* Number of errors */
  int nResult;                         /* Number of query results */
  char **azResult;                     /* Query result vector */
  Script sScript;                      /* Script parsing status */
  FILE *in;                            /* For reading script */
  

  /* Add calls to the registration procedures for new database engine
  ** interfaces here
  */
  registerSqlite();

................................................................................
      usage(argv[0]);
    }
  }

  /* Check for errors and missing arguments.  Find the database engine
  ** to use for this run.
  */
  if( zScriptFile==0 ){
    fprintf(stderr, "%s: no input script specified\n", argv[0]);
    usage(argv[0]);
  }
  for(i=0; i<nEngine; i++){
    if( strcmp(zDbEngine, apEngine[i]->zName)==0 ){
      pEngine = apEngine[i];
      break;
................................................................................
  }

  /*
  ** Read the entire script file content into memory
  */
  in = fopen(zScriptFile, "rb");
  if( in==0 ){
    fprintf(stderr, "%s: cannot open for reading\n", zScriptFile);
    exit(1);
  }
  fseek(in, 0L, SEEK_END);
  nScript = ftell(in);
  zScript = malloc( nScript+1 );
  if( zScript==0 ){
    fprintf(stderr, "out of memory at %s:%d\n", __FILE__,__LINE__);
    exit(1);
  }
  fseek(in, 0L, SEEK_SET);
  fread(zScript, 1, nScript, in);
  fclose(in);
  zScript[nScript] = 0;

  /* Initialize the sScript structure so that the cursor will be pointing
  ** to the start of the first line in the file after nextLine() is called
  ** once. */
  memset(&sScript, 0, sizeof(sScript));
  sScript.zScript = zScript;
  sScript.iEnd = nScript;
  sScript.copyFlag = !verifyMode;

  /* Open the database engine under test
  */
  rc = pEngine->xConnect(pEngine->pAuxData, zConnection, &pConn);
  if( rc ){
    fprintf(stderr, "%s: unable to connect to database\n", argv[0]);
    exit(1);
  }

  /* Loop over all records in the file */
  while( findStartOfNextRecord(&sScript) ){

    /* Tokenizer the first line of the record.  This also records the
    ** line number of the first record in sScript.startLine */
    tokenizeLine(&sScript);

    /* Figure out the record type and do appropriate processing */
    if( strcmp(sScript.azToken[0],"statement")==0 ){
      int k = 0;

      /* Extract the SQL from second and subsequent lines of the
      ** record.  Copy the SQL into contiguous memory at the beginning
      ** of zScript - we are guaranteed to have enough space there. */
      while( nextLine(&sScript) && sScript.zStart[0] ){
        if( k>0 ) zScript[k++] = '\n';
        memmove(&zScript[k], sScript.zStart, sScript.len);
        k += sScript.len;
      }
      zScript[k] = 0;

      /* Run the statement.  Remember the results */
      rc = pEngine->xStatement(pConn, zScript);

      /* Check to see if we are expecting success or failure */
      if( strcmp(sScript.azToken[1],"ok")==0 ){
        /* do nothing if we expect success */
      }else if( strcmp(sScript.azToken[1],"error")==0 ){
        /* Invert the result if we expect failure */
        rc = !rc;
      }else{
        fprintf(stderr, "%s:%d: statement argument should be 'ok' or 'error'\n",
                zScriptFile, sScript.startLine);
        nErr++;
        rc = 0;
      }

      /* Report an error if the results do not match expectation */
      if( rc ){
        fprintf(stderr, "%s:%d: statement error\n",
                zScriptFile, sScript.startLine);
        nErr++;
      }
    }else if( strcmp(sScript.azToken[0],"query")==0 ){
      int k = 0;
      int c;

      /* Verify that the type string consists of one or more characters
      ** from the set "SIR". */
      for(k=0; (c = sScript.azToken[1][k])!=0; k++){
        if( c!='S' && c!='I' && c!='R' ){
          fprintf(stderr, "%s:%d: unknown type character '%c' in type string\n",
                  zScriptFile, sScript.startLine, c);
          nErr++;
          break;
        }
      }
      if( c!=0 ) continue;

      /* Extract the SQL from second and subsequent lines of the record
      ** until the first "----" line or until end of record.
      */
      while( nextLine(&sScript) && sScript.zStart[0]
             && strcmp(sScript.zStart,"----")!=0 ){
        if( k>0 ) zScript[k++] = '\n';
        memmove(&zScript[k], sScript.zStart, sScript.len);
        k += sScript.len;
      }
      zScript[k] = 0;

      /* Run the query */
      nResult = 0;
      azResult = 0;
      rc = pEngine->xQuery(pConn, zScript, sScript.azToken[1],
                           &azResult, &nResult);
      if( rc ){
        fprintf(stderr, "%s:%d: query failed\n",
                zScriptFile, sScript.startLine);
        pEngine->xFreeResults(pConn, azResult, nResult);
        nErr++;
        continue;
      }

      /* Do any required sorting of query results */
      if( sScript.azToken[2][0]==0 || strcmp(sScript.azToken[2],"nosort")==0 ){
        /* Do no sorting */
      }else if( strcmp(sScript.azToken[2],"rowsort")==0 ){
        /* Row-oriented sorting */
        /*rowsort(azResult, nResult, strlen(sScript.azToken[2]));*/
      }else if( strcmp(sScript.azToken[2],"valuesort")==0 ){
        /* Sort all values independently */
        /*valuesort(azResult, nResult);*/
      }else{
        fprintf(stderr, "%s:%d: unknown sort method: '%s'\n",
                zScriptFile, sScript.startLine, sScript.azToken[2]);
        nErr++;
      }

      if( verifyMode ){
        /* In verify mode, first skip over the ---- line if we are still
        ** pointing at it. */
        if( strcmp(sScript.zStart, "----")==0 ) nextLine(&sScript);

        /* Compare subsequent lines of the script against the results
        ** from the query.  Report an error if any differences are found.
        */
        for(i=0; i<nResult && sScript.zStart[0]; nextLine(&sScript), i++){
          if( strcmp(sScript.zStart, azResult[i])!=0 ){
            fprintf(stderr,"%s:%d: wrong result\n", zScriptFile,
                    sScript.nLine);
            nErr++;
            break;
          }
        }
      }else{
        /* In completion mode, first make sure we have output an ---- line.
        ** Output such a line now if we have not already done so.
        */
        if( strcmp(sScript.zStart, "----")!=0 ){
          printf("----\n");
        }

        /* Skip over any existing results.  They will be ignored.
        */
        sScript.copyFlag = 0;
        while( sScript.zStart[0]!=0 && sScript.iCur<sScript.iEnd ){
          nextLine(&sScript);
        }
        sScript.copyFlag = 1;

        /* Output the results obtained by running the query
        */
        for(i=0; i<nResult; i++){
          printf("%s\n", azResult[i]);
        }
      }

      /* Free the query results */
      pEngine->xFreeResults(pConn, azResult, nResult);
    }else{
      /* An unrecognized record type is an error */
      fprintf(stderr, "%s:%d: unknown record type: '%s'\n",
              zScriptFile, sScript.startLine, sScript.azToken[0]);
      nErr++;
    }
  }


  /* Finally, shutdown the database connection.  Return the number of errors.
  */
  rc = pEngine->xDisconnect(pConn);
  if( rc ){
    fprintf(stderr, "%s: disconnection from database failed\n", argv[0]);
  }
  return nErr; 
}