sqllogictest
Check-in [2cffc96fd2]
Not logged in

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

Overview
Comment:The sqllogictest program builds and runs on Linux.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2cffc96fd2f2175c885b13524dd3e8e67d1349ff
User & Date: drh 2008-11-29 22:31:28
Context
2008-11-29
22:50
Change the typestring character for strings from "S" to "T". check-in: 9c52972f34 user: drh tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/sqllogictest.c.

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
...
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
...
445
446
447
448
449
450
451

452
453
454
455
456
457
458
...
520
521
522
523
524
525
526

527
528
529
530
531
532
533
...
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

561
562
563
564
565
566
567
...
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
/*
** 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;
}

................................................................................
** 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 */
................................................................................
  const DbEngine *pEngine = 0;         /* Pointer to DbEngine object */
  int i;                               /* Loop counter */
  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
................................................................................
  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 ){
................................................................................
    /* 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;
................................................................................
          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; 
}







|











|













>
>
>






|


|





>




|

|




|





|



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











>
|
>
>
>





|







 







>

|





|




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







 







>







 







>







 







|

|






>







 







>
>
>
>
>
>




>
|
|

|









>













|
>
>


>
|









|




|
|










|



<
<
<
<
<
<
<
<





>
>
>
>
>
>
>
>
>













|




>
>
>
>
>
>
>
>



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
...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
...
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
...
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
...
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715








716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
** 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 *zLine;         /* 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->zLine 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 ){
      p->iCur = p->iEnd;
      p->zLine = &p->zScript[p->iEnd];
      p->len = 0;
      return 0;
    }

    /* Advance the cursor to the next line */
    p->iCur = p->iNext;
    p->nLine++;
    p->zLine = &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;
    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--;
      i--;
      p->zScript[i-1] = 0;
    }

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

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

    /* 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->zLine[0]!='#' ) break;
  }
  return 1;
}

/*
** Look ahead to the next line and return TRUE if it is a blank line.
** But do not advance to the next line yet.
*/
static int nextIsBlank(Script *p){
  int i = p->iNext;
  if( i>=p->iEnd ) return 1;
  while( i<p->iEnd && isspace(p->zScript[i]) ){
    if( p->zScript[i]=='\n' ) return 1;
    i++;
  }
  return 0;
}

/*
** 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 */
  if( p->iCur>0 ){
    while( p->zLine[0] && p->iCur<p->iEnd ){
      nextLine(p);
    }
  }else{
    nextLine(p);
  }

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

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

................................................................................
** 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;
  p->startLine = p->nLine;
  for(i=j=0; j<p->len && i<3; i++){
    findToken(&p->zLine[j], &k, &len);
    j += k;
    n = len;
    if( n>=sizeof(p->azToken[0]) ){
      n = sizeof(p->azToken[0])-1;
    }
    memcpy(p->azToken[i], &p->zLine[j], n);
    p->azToken[i][n] = 0;
    j += n+1;
  }
}

/*
** The number columns in a row of the current result set
*/
static int nColumn = 0;

/*
** Comparison function for sorting the result set.
*/
static int rowCompare(const void *pA, const void *pB){
  const char **azA = (const char**)pA;
  const char **azB = (const char**)pB;
  int c = 0, i;
  for(i=0; c==0 && i<nColumn; i++){
    c = strcmp(azA[i], azB[i]);
  }
  return c;
}


/*
** 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 */
................................................................................
  const DbEngine *pEngine = 0;         /* Pointer to DbEngine object */
  int i;                               /* Loop counter */
  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 nCmd = 0;                        /* Number of SQL statements processed */
  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
................................................................................
  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.zLine = zScript;
  sScript.iEnd = nScript;
  sScript.copyFlag = !verifyMode;

  /* Open the database engine under test
  */
  rc = pEngine->xConnect(pEngine->pAuxData, zConnection, &pConn);
  if( rc ){
................................................................................
    /* 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.zLine[0] ){
        if( k>0 ) zScript[k++] = '\n';
        memmove(&zScript[k], sScript.zLine, sScript.len);
        k += sScript.len;
      }
      zScript[k] = 0;

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

      /* 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;
................................................................................
          fprintf(stderr, "%s:%d: unknown type character '%c' in type string\n",
                  zScriptFile, sScript.startLine, c);
          nErr++;
          break;
        }
      }
      if( c!=0 ) continue;
      if( k<=0 ){
        fprintf(stderr, "%s:%d: missing type string\n",
                zScriptFile, sScript.startLine);
        nErr++;
        break;
      }

      /* Extract the SQL from second and subsequent lines of the record
      ** until the first "----" line or until end of record.
      */
      k = 0;
      while( !nextIsBlank(&sScript) && nextLine(&sScript) && sScript.zLine[0]
             && strcmp(sScript.zLine,"----")!=0 ){
        if( k>0 ) zScript[k++] = '\n';
        memmove(&zScript[k], sScript.zLine, 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);
      nCmd++;
      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 */
        nColumn = strlen(sScript.azToken[1]);
        qsort(azResult, nResult/nColumn, sizeof(azResult[0])*nColumn,
              rowCompare);
      }else if( strcmp(sScript.azToken[2],"valuesort")==0 ){
        /* Sort all values independently */
        nColumn = 1;
        qsort(azResult, nResult, sizeof(azResult[0]), rowCompare);
      }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.zLine, "----")==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.zLine[0]; nextLine(&sScript), i++){
          if( strcmp(sScript.zLine, 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.zLine, "----")!=0 ){
          printf("----\n");
        }









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

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

      /* 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++;
    }
  }


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

  /* Report the number of errors and quit.
  */
  if( verifyMode ){
    printf("%s: %d errors out of %d SQL statement\n",
           zScriptFile, nErr, nCmd);
  }
  return nErr; 
}