/ Check-in [ed28f15e]
Login

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

Overview
Comment:Merge all changes from the 3.18.0 release.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA3-256:ed28f15e052b34f6642c68711fa3ed765c396262327fd3dd9c520b346a4deb81
User & Date: drh 2017-03-30 20:26:06
Context
2017-04-08
00:25
Merge the latest trunk changes. check-in: dd16439e user: drh tags: apple-osx
2017-03-30
20:26
Merge all changes from the 3.18.0 release. check-in: ed28f15e user: drh tags: apple-osx
2017-03-28
18:48
Version 3.18.0 check-in: 424a0d38 user: drh tags: release, branch-3.18, version-3.18.0
2017-03-13
18:31
Merge all recent changes from trunk. check-in: 3d04b2cd user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   630    630   	$(LTLINK) -o $@ $(FUZZERSHELL_OPT) \
   631    631   	  $(TOP)/tool/fuzzershell.c sqlite3.c $(TLIBS)
   632    632   
   633    633   fuzzcheck$(TEXE):	$(FUZZCHECK_SRC) sqlite3.c sqlite3.h
   634    634   	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(FUZZCHECK_SRC) sqlite3.c $(TLIBS)
   635    635   
   636    636   ossshell$(TEXE):	$(TOP)/test/ossfuzz.c $(TOP)/test/ossshell.c sqlite3.c sqlite3.h
   637         -	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/ossshell.c sqlite3.c sqlite3.h $(TLIBS)
          637  +	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/ossshell.c \
          638  +             $(TOP)/test/ossfuzz.c sqlite3.c $(TLIBS)
   638    639   
   639    640   dbfuzz$(TEXE):	$(TOP)/test/dbfuzz.c sqlite3.c sqlite3.h
   640    641   	$(LTLINK) -o $@ $(DBFUZZ_OPT) $(TOP)/test/dbfuzz.c sqlite3.c $(TLIBS)
   641    642   
   642    643   mptester$(TEXE):	sqlite3.lo $(TOP)/mptest/mptest.c
   643    644   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.lo \
   644    645   		$(TLIBS) -rpath "$(libdir)"
................................................................................
  1163   1164   	echo "static const char *zMainloop = " >> $@
  1164   1165   	$(TCLSH_CMD) $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
  1165   1166   	echo "; return zMainloop; }" >> $@
  1166   1167   
  1167   1168   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
  1168   1169   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
  1169   1170   
         1171  +dbdump$(TEXE): $(TOP)/ext/misc/dbdump.c sqlite3.lo
         1172  +	$(LTLINK) -DDBDUMP_STANDALONE -o $@ \
         1173  +           $(TOP)/ext/misc/dbdump.c sqlite3.lo $(TLIBS)
         1174  +
  1170   1175   showdb$(TEXE):	$(TOP)/tool/showdb.c sqlite3.lo
  1171   1176   	$(LTLINK) -o $@ $(TOP)/tool/showdb.c sqlite3.lo $(TLIBS)
  1172   1177   
  1173   1178   showstat4$(TEXE):	$(TOP)/tool/showstat4.c sqlite3.lo
  1174   1179   	$(LTLINK) -o $@ $(TOP)/tool/showstat4.c sqlite3.lo $(TLIBS)
  1175   1180   
  1176   1181   showjournal$(TEXE):	$(TOP)/tool/showjournal.c sqlite3.lo

Changes to Makefile.msc.

  2177   2177   	$(TCLSH_CMD) $(TOP)\tool\tostr.tcl $(TOP)\tool\spaceanal.tcl >> $@
  2178   2178   	echo ; return zMainloop; } >> $@
  2179   2179   
  2180   2180   sqlite3_analyzer.exe:	sqlite3_analyzer.c $(LIBRESOBJS)
  2181   2181   	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_analyzer.c \
  2182   2182   		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  2183   2183   
         2184  +dbdump.exe:	$(TOP)\ext\misc\dbdump.c $(SQLITE3C) $(SQLITE3H)
         2185  +	$(LTLINK) $(NO_WARN) -DDBDUMP_STANDALONE $(TOP)\ext\misc\dbdump.c $(SQLITE3C) \
         2186  +		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS)
         2187  +
  2184   2188   testloadext.lo:	$(TOP)\src\test_loadext.c
  2185   2189   	$(LTCOMPILE) $(NO_WARN) -c $(TOP)\src\test_loadext.c
  2186   2190   
  2187   2191   testloadext.dll:	testloadext.lo
  2188   2192   	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /OUT:$@ testloadext.lo
  2189   2193   
  2190   2194   showdb.exe:	$(TOP)\tool\showdb.c $(SQLITE3C) $(SQLITE3H)
................................................................................
  2255   2259   	-rmdir /Q/S .deps 2>NUL
  2256   2260   	-rmdir /Q/S .libs 2>NUL
  2257   2261   	-rmdir /Q/S tsrc 2>NUL
  2258   2262   	del /Q .target_source 2>NUL
  2259   2263   	del /Q tclsqlite3.exe $(SQLITETCLH) $(SQLITETCLDECLSH) 2>NUL
  2260   2264   	del /Q testloadext.dll 2>NUL
  2261   2265   	del /Q testfixture.exe test.db 2>NUL
  2262         -	del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe 2>NUL
         2266  +	del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe dbdump.exe 2>NUL
  2263   2267   	del /Q changeset.exe 2>NUL
  2264   2268   	del /Q showjournal.exe showstat4.exe showwal.exe speedtest1.exe 2>NUL
  2265   2269   	del /Q mptester.exe wordcount.exe rbu.exe srcck1.exe 2>NUL
  2266   2270   	del /Q sqlite3.c sqlite3-*.c 2>NUL
  2267   2271   	del /Q sqlite3rc.h 2>NUL
  2268   2272   	del /Q shell.c sqlite3ext.h sqlite3session.h 2>NUL
  2269   2273   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL
  2270   2274   	del /Q sqlite-*-output.vsix 2>NUL
  2271   2275   	del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe dbhash.exe 2>NUL
  2272   2276   	del /Q fts5.* fts5parse.* 2>NUL
  2273   2277   # <</mark>>

Changes to README.md.

     4      4   engine.  Some test scripts are also include.  However, many other test scripts
     5      5   and most of the documentation are managed separately.
     6      6   
     7      7   If you are reading this on a Git mirror someplace, you are doing it wrong.
     8      8   The [official repository](https://www.sqlite.org/src/) is better.  Go there
     9      9   now.
    10     10   
           11  +## Obtaining The Code
           12  +
           13  +SQLite sources are managed using the
           14  +[Fossil](https://www.fossil-scm.org/), a distributed version control system
           15  +that was specifically designed to support SQLite development.
           16  +If you do not want to use Fossil, you can download tarballs or ZIP
           17  +archives as follows:
           18  +
           19  +  *  Lastest trunk check-in:
           20  +     <https://www.sqlite.org/src/tarball/sqlite.tar.gz> or
           21  +     <https://www.sqlite.org/src/zip/sqlite.zip>.
           22  +
           23  +  *  Latest release:
           24  +     <https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release> or
           25  +     <https://www.sqlite.org/src/zip/sqlite.zip?r=release>.
           26  +
           27  +  *  For other check-ins, substitute an appropriate branch name or
           28  +     tag or hash prefix for "release" in the URLs of the previous
           29  +     bullet.  Or browse the [timeline](https://www.sqlite.org/src/timeline)
           30  +     to locate the check-in desired, click on its information page link,
           31  +     then click on the "Tarball" or "ZIP Archive" links on the information
           32  +     page.
           33  +
           34  +If you do want to use Fossil to check out the source tree, 
           35  +first install Fossil version 2.0 or later.
           36  +(Source tarballs and precompiled binaries available
           37  +[here](https://www.fossil-scm.org/fossil/uv/download.html).)
           38  +Then run commands like this:
           39  +
           40  +        mkdir ~/sqlite
           41  +        cd ~/sqlite
           42  +        fossil clone https://www.sqlite.org/src sqlite.fossil
           43  +        fossil open sqlite.fossil
           44  +    
           45  +After setting up a repository using the steps above, you can always
           46  +update to the lastest version using:
           47  +
           48  +        fossil update trunk   ;# latest trunk check-in
           49  +        fossil update release ;# latest official release
           50  +
           51  +Or type "fossil ui" to get a web-based user interface.
           52  +
    11     53   ## Compiling
    12     54   
    13     55   First create a directory in which to place
    14     56   the build products.  It is recommended, but not required, that the
    15     57   build directory be separate from the source directory.  Cd into the
    16     58   build directory and then from the build directory run the configure
    17     59   script found at the root of the source tree.  Then run "make".
    18     60   
    19     61   For example:
    20     62   
    21         -    tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
    22         -    mkdir bld                ;#  Build will occur in a sibling directory
    23         -    cd bld                   ;#  Change to the build directory
    24         -    ../sqlite/configure      ;#  Run the configure script
    25         -    make                     ;#  Run the makefile.
    26         -    make sqlite3.c           ;#  Build the "amalgamation" source file
    27         -    make test                ;#  Run some tests (requires Tcl)
           63  +        tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
           64  +        mkdir bld                ;#  Build will occur in a sibling directory
           65  +        cd bld                   ;#  Change to the build directory
           66  +        ../sqlite/configure      ;#  Run the configure script
           67  +        make                     ;#  Run the makefile.
           68  +        make sqlite3.c           ;#  Build the "amalgamation" source file
           69  +        make test                ;#  Run some tests (requires Tcl)
    28     70   
    29     71   See the makefile for additional targets.
    30     72   
    31     73   The configure script uses autoconf 2.61 and libtool.  If the configure
    32     74   script does not work out for you, there is a generic makefile named
    33     75   "Makefile.linux-gcc" in the top directory of the source tree that you
    34     76   can copy and edit to suit your needs.  Comments on the generic makefile
................................................................................
    39     81   On Windows, all applicable build products can be compiled with MSVC.
    40     82   First open the command prompt window associated with the desired compiler
    41     83   version (e.g. "Developer Command Prompt for VS2013").  Next, use NMAKE
    42     84   with the provided "Makefile.msc" to build one of the supported targets.
    43     85   
    44     86   For example:
    45     87   
    46         -    mkdir bld
    47         -    cd bld
    48         -    nmake /f Makefile.msc TOP=..\sqlite
    49         -    nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
    50         -    nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
    51         -    nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
    52         -    nmake /f Makefile.msc test TOP=..\sqlite
           88  +        mkdir bld
           89  +        cd bld
           90  +        nmake /f Makefile.msc TOP=..\sqlite
           91  +        nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
           92  +        nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
           93  +        nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
           94  +        nmake /f Makefile.msc test TOP=..\sqlite
    53     95   
    54     96   There are several build options that can be set via the NMAKE command
    55     97   line.  For example, to build for WinRT, simply add "FOR_WINRT=1" argument
    56     98   to the "sqlite3.dll" command line above.  When debugging into the SQLite
    57     99   code, adding the "DEBUG=1" argument to one of the above command lines is
    58    100   recommended.
    59    101   

Changes to autoconf/Makefile.msc.

   640    640   !IF $(DEBUG)>0 || $(API_ARMOR)!=0 || $(FOR_WIN10)!=0
   641    641   TCC = $(TCC) -DSQLITE_ENABLE_API_ARMOR=1
   642    642   RCC = $(RCC) -DSQLITE_ENABLE_API_ARMOR=1
   643    643   !ENDIF
   644    644   
   645    645   !IF $(DEBUG)>2
   646    646   TCC = $(TCC) -DSQLITE_DEBUG=1
          647  +TCC = $(TCC) -DSQLITE_ENABLE_WHERETRACE -DSQLITE_ENABLE_SELECTTRACE
   647    648   RCC = $(RCC) -DSQLITE_DEBUG=1
   648    649   !ENDIF
   649    650   
   650    651   !IF $(DEBUG)>4 || $(OSTRACE)!=0
   651    652   TCC = $(TCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   652    653   RCC = $(RCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   653    654   !ENDIF

Changes to autoconf/configure.ac.

    51     51   
    52     52   AS_IF([ test x"$enable_editline" != xno ],[
    53     53     AC_CHECK_HEADERS([editline/readline.h],[
    54     54       sLIBS=$LIBS
    55     55       LIBS=""
    56     56       AC_SEARCH_LIBS([readline],[edit],[
    57     57         AC_DEFINE([HAVE_EDITLINE],1,Define to use BSD editline)
    58         -      READLINE_LIBS=$LIBS
           58  +      READLINE_LIBS="$LIBS -ltinfo"
    59     59         enable_readline=no
    60         -    ])
           60  +    ],[],[-ltinfo])
    61     61       AS_UNSET(ac_cv_search_readline)
    62     62       LIBS=$sLIBS
    63     63     ])
    64     64   ])
    65     65   
    66     66   AS_IF([ test x"$enable_readline" != xno ],[
    67     67     AC_CHECK_HEADERS([readline/readline.h],[

Name change from ext/README.txt to ext/README.md.

     1         -Version loadable extensions to SQLite are found in subfolders
     2         -of this folder.
            1  +## Loadable Extensions
            2  +
            3  +Various [loadable extensions](https://www.sqlite.org/loadext.html) for
            4  +SQLite are found in subfolders.
            5  +
            6  +Most subfolders are dedicated to a single loadable extension (for
            7  +example FTS5, or RTREE).  But the misc/ subfolder contains a collection
            8  +of smaller single-file extensions.

Changes to ext/fts3/fts3.c.

   345    345     if( (v & mask2)==0 ){ var = v; return ret; }
   346    346   
   347    347   /* 
   348    348   ** Read a 64-bit variable-length integer from memory starting at p[0].
   349    349   ** Return the number of bytes read, or 0 on error.
   350    350   ** The value is stored in *v.
   351    351   */
   352         -int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
   353         -  const char *pStart = p;
          352  +int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
          353  +  const unsigned char *p = (const unsigned char*)pBuf;
          354  +  const unsigned char *pStart = p;
   354    355     u32 a;
   355    356     u64 b;
   356    357     int shift;
   357    358   
   358    359     GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
   359    360     GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
   360    361     GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
................................................................................
  1393   1394     zCsr += nDb;
  1394   1395   
  1395   1396     /* Fill in the azColumn array */
  1396   1397     for(iCol=0; iCol<nCol; iCol++){
  1397   1398       char *z; 
  1398   1399       int n = 0;
  1399   1400       z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
  1400         -    memcpy(zCsr, z, n);
         1401  +    if( n>0 ){
         1402  +      memcpy(zCsr, z, n);
         1403  +    }
  1401   1404       zCsr[n] = '\0';
  1402   1405       sqlite3Fts3Dequote(zCsr);
  1403   1406       p->azColumn[iCol] = zCsr;
  1404   1407       zCsr += n+1;
  1405   1408       assert( zCsr <= &((char *)p)[nByte] );
  1406   1409     }
  1407   1410   

Changes to ext/fts3/fts3_unicode.c.

   132    132     unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
   133    133     int bAlnum,                     /* Replace Isalnum() return value with this */
   134    134     const char *zIn,                /* Array of characters to make exceptions */
   135    135     int nIn                         /* Length of z in bytes */
   136    136   ){
   137    137     const unsigned char *z = (const unsigned char *)zIn;
   138    138     const unsigned char *zTerm = &z[nIn];
   139         -  int iCode;
          139  +  unsigned int iCode;
   140    140     int nEntry = 0;
   141    141   
   142    142     assert( bAlnum==0 || bAlnum==1 );
   143    143   
   144    144     while( z<zTerm ){
   145    145       READ_UTF8(z, zTerm, iCode);
   146         -    assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
   147         -    if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
   148         -     && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
          146  +    assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
          147  +    if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
          148  +     && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0 
   149    149       ){
   150    150         nEntry++;
   151    151       }
   152    152     }
   153    153   
   154    154     if( nEntry ){
   155    155       int *aNew;                    /* New aiException[] array */
................................................................................
   158    158       aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
   159    159       if( aNew==0 ) return SQLITE_NOMEM;
   160    160       nNew = p->nException;
   161    161   
   162    162       z = (const unsigned char *)zIn;
   163    163       while( z<zTerm ){
   164    164         READ_UTF8(z, zTerm, iCode);
   165         -      if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
   166         -       && sqlite3FtsUnicodeIsdiacritic(iCode)==0
          165  +      if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
          166  +       && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
   167    167         ){
   168    168           int i, j;
   169         -        for(i=0; i<nNew && aNew[i]<iCode; i++);
          169  +        for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
   170    170           for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
   171         -        aNew[i] = iCode;
          171  +        aNew[i] = (int)iCode;
   172    172           nNew++;
   173    173         }
   174    174       }
   175    175       p->aiException = aNew;
   176    176       p->nException = nNew;
   177    177     }
   178    178   
................................................................................
   314    314     int *pnToken,                   /* OUT: Number of bytes at *paToken */
   315    315     int *piStart,                   /* OUT: Starting offset of token */
   316    316     int *piEnd,                     /* OUT: Ending offset of token */
   317    317     int *piPos                      /* OUT: Position integer of token */
   318    318   ){
   319    319     unicode_cursor *pCsr = (unicode_cursor *)pC;
   320    320     unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
   321         -  int iCode = 0;
          321  +  unsigned int iCode = 0;
   322    322     char *zOut;
   323    323     const unsigned char *z = &pCsr->aInput[pCsr->iOff];
   324    324     const unsigned char *zStart = z;
   325    325     const unsigned char *zEnd;
   326    326     const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
   327    327   
   328    328     /* Scan past any delimiter characters before the start of the next token.
   329    329     ** Return SQLITE_DONE early if this takes us all the way to the end of 
   330    330     ** the input.  */
   331    331     while( z<zTerm ){
   332    332       READ_UTF8(z, zTerm, iCode);
   333         -    if( unicodeIsAlnum(p, iCode) ) break;
          333  +    if( unicodeIsAlnum(p, (int)iCode) ) break;
   334    334       zStart = z;
   335    335     }
   336    336     if( zStart>=zTerm ) return SQLITE_DONE;
   337    337   
   338    338     zOut = pCsr->zToken;
   339    339     do {
   340    340       int iOut;
................................................................................
   346    346         zOut = &zNew[zOut - pCsr->zToken];
   347    347         pCsr->zToken = zNew;
   348    348         pCsr->nAlloc += 64;
   349    349       }
   350    350   
   351    351       /* Write the folded case of the last character read to the output */
   352    352       zEnd = z;
   353         -    iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
          353  +    iOut = sqlite3FtsUnicodeFold((int)iCode, p->bRemoveDiacritic);
   354    354       if( iOut ){
   355    355         WRITE_UTF8(zOut, iOut);
   356    356       }
   357    357   
   358    358       /* If the cursor is not at EOF, read the next character */
   359    359       if( z>=zTerm ) break;
   360    360       READ_UTF8(z, zTerm, iCode);
   361         -  }while( unicodeIsAlnum(p, iCode) 
   362         -       || sqlite3FtsUnicodeIsdiacritic(iCode)
          361  +  }while( unicodeIsAlnum(p, (int)iCode) 
          362  +       || sqlite3FtsUnicodeIsdiacritic((int)iCode)
   363    363     );
   364    364   
   365    365     /* Set the output variables and return. */
   366    366     pCsr->iOff = (int)(z - pCsr->aInput);
   367    367     *paToken = pCsr->zToken;
   368    368     *pnToken = (int)(zOut - pCsr->zToken);
   369    369     *piStart = (int)(zStart - pCsr->aInput);

Changes to ext/fts3/fts3_unicode2.c.

   123    123       0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
   124    124       0x380400F0,
   125    125     };
   126    126     static const unsigned int aAscii[4] = {
   127    127       0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
   128    128     };
   129    129   
   130         -  if( c<128 ){
   131         -    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
   132         -  }else if( c<(1<<22) ){
          130  +  if( (unsigned int)c<128 ){
          131  +    return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
          132  +  }else if( (unsigned int)c<(1<<22) ){
   133    133       unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
   134    134       int iRes = 0;
   135    135       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   136    136       int iLo = 0;
   137    137       while( iHi>=iLo ){
   138    138         int iTest = (iHi + iLo) / 2;
   139    139         if( key >= aEntry[iTest] ){
................................................................................
   318    318      65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, 
   319    319      65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, 
   320    320      65514, 65521, 65527, 65528, 65529, 
   321    321     };
   322    322   
   323    323     int ret = c;
   324    324   
   325         -  assert( c>=0 );
   326    325     assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
   327    326   
   328    327     if( c<128 ){
   329    328       if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
   330    329     }else if( c<65536 ){
          330  +    const struct TableEntry *p;
   331    331       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   332    332       int iLo = 0;
   333    333       int iRes = -1;
   334    334   
          335  +    assert( c>aEntry[0].iCode );
   335    336       while( iHi>=iLo ){
   336    337         int iTest = (iHi + iLo) / 2;
   337    338         int cmp = (c - aEntry[iTest].iCode);
   338    339         if( cmp>=0 ){
   339    340           iRes = iTest;
   340    341           iLo = iTest+1;
   341    342         }else{
   342    343           iHi = iTest-1;
   343    344         }
   344    345       }
   345         -    assert( iRes<0 || c>=aEntry[iRes].iCode );
   346    346   
   347         -    if( iRes>=0 ){
   348         -      const struct TableEntry *p = &aEntry[iRes];
   349         -      if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
   350         -        ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
   351         -        assert( ret>0 );
   352         -      }
          347  +    assert( iRes>=0 && c>=aEntry[iRes].iCode );
          348  +    p = &aEntry[iRes];
          349  +    if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
          350  +      ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
          351  +      assert( ret>0 );
   353    352       }
   354    353   
   355    354       if( bRemoveDiacritic ) ret = remove_diacritic(ret);
   356    355     }
   357    356     
   358    357     else if( c>=66560 && c<66600 ){
   359    358       ret = c + 40;
   360    359     }
   361    360   
   362    361     return ret;
   363    362   }
   364    363   #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
   365    364   #endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */

Changes to ext/fts3/fts3_write.c.

  4952   4952     return rc;
  4953   4953   }
  4954   4954   
  4955   4955   /*
  4956   4956   ** Convert the text beginning at *pz into an integer and return
  4957   4957   ** its value.  Advance *pz to point to the first character past
  4958   4958   ** the integer.
         4959  +**
         4960  +** This function used for parameters to merge= and incrmerge=
         4961  +** commands. 
  4959   4962   */
  4960   4963   static int fts3Getint(const char **pz){
  4961   4964     const char *z = *pz;
  4962   4965     int i = 0;
  4963         -  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
         4966  +  while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
  4964   4967     *pz = z;
  4965   4968     return i;
  4966   4969   }
  4967   4970   
  4968   4971   /*
  4969   4972   ** Process statements of the form:
  4970   4973   **

Added ext/fts3/tool/fts3cov.sh.

            1  +#!/bin/sh
            2  +
            3  +set -e
            4  +
            5  +srcdir=`dirname $(dirname $(dirname $(dirname $0)))`
            6  +./testfixture $srcdir/test/fts3.test --output=fts3cov-out.txt
            7  +
            8  +echo ""
            9  +
           10  +for f in `ls $srcdir/ext/fts3/*.c` 
           11  +do
           12  +  f=`basename $f`
           13  +  echo -ne "$f: "
           14  +  gcov -b $f | grep Taken | sed 's/Taken at least once://'
           15  +done
           16  +

Changes to ext/fts3/unicode/mkunicode.tcl.

   223    223     puts "** is less than zero."
   224    224     puts "*/"
   225    225     puts "int ${zFunc}\(int c)\{"
   226    226     an_print_range_array $lRange
   227    227     an_print_ascii_bitmap $lRange
   228    228     puts {
   229    229     if( (unsigned int)c<128 ){
   230         -    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
          230  +    return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
   231    231     }else if( (unsigned int)c<(1<<22) ){
   232    232       unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
   233    233       int iRes = 0;
   234    234       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   235    235       int iLo = 0;
   236    236       while( iHi>=iLo ){
   237    237         int iTest = (iHi + iLo) / 2;

Added ext/misc/README.md.

            1  +## Miscellaneous Extensions
            2  +
            3  +This folder contains a collection of smaller loadable extensions.
            4  +See <https://www.sqlite.org/loadext.html> for instructions on how
            5  +to compile and use loadable extensions.
            6  +Each extension in this folder is implemented in a single file of C code.
            7  +
            8  +Each source file contains a description in its header comment.  See the
            9  +header comments for details about each extension.  Additional notes are
           10  +as follows:
           11  +
           12  +  *  **carray.c** &mdash;  This module implements the
           13  +     [carray](https://www.sqlite.org/carray.html) table-valued function.
           14  +     It is a good example of how to go about implementing a custom
           15  +     [table-valued function](https://www.sqlite.org/vtab.html#tabfunc2).
           16  +
           17  +  *  **dbdump.c** &mdash;  This is not actually a loadable extension, but
           18  +     rather a library that implements an approximate equivalent to the
           19  +     ".dump" command of the
           20  +     [command-line shell](https://www.sqlite.org/cli.html).
           21  +
           22  +  *  **memvfs.c** &mdash;  This file implements a custom
           23  +     [VFS](https://www.sqlite.org/vfs.html) that stores an entire database
           24  +     file in a single block of RAM.  It serves as a good example of how
           25  +     to implement a simple custom VFS.
           26  +
           27  +  *  **rot13.c** &mdash;  This file implements the very simple rot13()
           28  +     substitution function.  This file makes a good template for implementing
           29  +     new custom SQL functions for SQLite.
           30  +
           31  +  *  **series.c** &mdash;  This is an implementation of the
           32  +     "generate_series" [virtual table](https://www.sqlite.org/vtab.html).
           33  +     It can make a good template for new custom virtual table implementations.
           34  +
           35  +  *  **shathree.c** &mdash;  An implementation of the sha3() and
           36  +     sha3_query() SQL functions.  The file is named "shathree.c" instead
           37  +     of "sha3.c" because the default entry point names in SQLite are based
           38  +     on the source filename with digits removed, so if we used the name
           39  +     "sha3.c" then the entry point would conflict with the prior "sha1.c"
           40  +     extension.

Added ext/misc/dbdump.c.

            1  +/*
            2  +** 2016-03-13
            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 file implements a C-language subroutine that converts the content
           14  +** of an SQLite database into UTF-8 text SQL statements that can be used
           15  +** to exactly recreate the original database.  ROWID values are preserved.
           16  +**
           17  +** A prototype of the implemented subroutine is this:
           18  +**
           19  +**   int sqlite3_db_dump(
           20  +**          sqlite3 *db,
           21  +**          const char *zSchema,
           22  +**          const char *zTable,
           23  +**          void (*xCallback)(void*, const char*),
           24  +**          void *pArg
           25  +**   );
           26  +**
           27  +** The db parameter is the database connection.  zSchema is the schema within
           28  +** that database which is to be dumped.  Usually the zSchema is "main" but
           29  +** can also be "temp" or any ATTACH-ed database.  If zTable is not NULL, then
           30  +** only the content of that one table is dumped.  If zTable is NULL, then all
           31  +** tables are dumped.
           32  +**
           33  +** The generate text is passed to xCallback() in multiple calls.  The second
           34  +** argument to xCallback() is a copy of the pArg parameter.  The first
           35  +** argument is some of the output text that this routine generates.  The
           36  +** signature to xCallback() is designed to make it compatible with fputs().
           37  +**
           38  +** The sqlite3_db_dump() subroutine returns SQLITE_OK on success or some error
           39  +** code if it encounters a problem.
           40  +**
           41  +** If this file is compiled with -DDBDUMP_STANDALONE then a "main()" routine
           42  +** is included so that this routine becomes a command-line utility.  The
           43  +** command-line utility takes two or three arguments which are the name
           44  +** of the database file, the schema, and optionally the table, forming the
           45  +** first three arguments of a single call to the library routine.
           46  +*/
           47  +#include "sqlite3.h"
           48  +#include <stdarg.h>
           49  +#include <string.h>
           50  +#include <ctype.h>
           51  +
           52  +/*
           53  +** The state of the dump process.
           54  +*/
           55  +typedef struct DState DState;
           56  +struct DState {
           57  +  sqlite3 *db;                /* The database connection */
           58  +  int nErr;                   /* Number of errors seen so far */
           59  +  int rc;                     /* Error code */
           60  +  int writableSchema;                    /* True if in writable_schema mode */
           61  +  int (*xCallback)(const char*,void*);   /* Send output here */
           62  +  void *pArg;                            /* Argument to xCallback() */
           63  +};
           64  +
           65  +/*
           66  +** A variable length string to which one can append text.
           67  +*/
           68  +typedef struct DText DText;
           69  +struct DText {
           70  +  char *z;           /* The text */
           71  +  int n;             /* Number of bytes of content in z[] */
           72  +  int nAlloc;        /* Number of bytes allocated to z[] */
           73  +};
           74  +
           75  +/*
           76  +** Initialize and destroy a DText object
           77  +*/
           78  +static void initText(DText *p){
           79  +  memset(p, 0, sizeof(*p));
           80  +}
           81  +static void freeText(DText *p){
           82  +  sqlite3_free(p->z);
           83  +  initText(p);
           84  +}
           85  +
           86  +/* zIn is either a pointer to a NULL-terminated string in memory obtained
           87  +** from malloc(), or a NULL pointer. The string pointed to by zAppend is
           88  +** added to zIn, and the result returned in memory obtained from malloc().
           89  +** zIn, if it was not NULL, is freed.
           90  +**
           91  +** If the third argument, quote, is not '\0', then it is used as a
           92  +** quote character for zAppend.
           93  +*/
           94  +static void appendText(DText *p, char const *zAppend, char quote){
           95  +  int len;
           96  +  int i;
           97  +  int nAppend = (int)(strlen(zAppend) & 0x3fffffff);
           98  +
           99  +  len = nAppend+p->n+1;
          100  +  if( quote ){
          101  +    len += 2;
          102  +    for(i=0; i<nAppend; i++){
          103  +      if( zAppend[i]==quote ) len++;
          104  +    }
          105  +  }
          106  +
          107  +  if( p->n+len>=p->nAlloc ){
          108  +    char *zNew;
          109  +    p->nAlloc = p->nAlloc*2 + len + 20;
          110  +    zNew = sqlite3_realloc(p->z, p->nAlloc);
          111  +    if( zNew==0 ){
          112  +      freeText(p);
          113  +      return;
          114  +    }
          115  +    p->z = zNew;
          116  +  }
          117  +
          118  +  if( quote ){
          119  +    char *zCsr = p->z+p->n;
          120  +    *zCsr++ = quote;
          121  +    for(i=0; i<nAppend; i++){
          122  +      *zCsr++ = zAppend[i];
          123  +      if( zAppend[i]==quote ) *zCsr++ = quote;
          124  +    }
          125  +    *zCsr++ = quote;
          126  +    p->n = (int)(zCsr - p->z);
          127  +    *zCsr = '\0';
          128  +  }else{
          129  +    memcpy(p->z+p->n, zAppend, nAppend);
          130  +    p->n += nAppend;
          131  +    p->z[p->n] = '\0';
          132  +  }
          133  +}
          134  +
          135  +/*
          136  +** Attempt to determine if identifier zName needs to be quoted, either
          137  +** because it contains non-alphanumeric characters, or because it is an
          138  +** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
          139  +** that quoting is required.
          140  +**
          141  +** Return '"' if quoting is required.  Return 0 if no quoting is required.
          142  +*/
          143  +static char quoteChar(const char *zName){
          144  +  /* All SQLite keywords, in alphabetical order */
          145  +  static const char *azKeywords[] = {
          146  +    "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
          147  +    "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
          148  +    "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
          149  +    "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
          150  +    "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
          151  +    "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
          152  +    "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
          153  +    "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
          154  +    "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
          155  +    "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
          156  +    "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
          157  +    "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
          158  +    "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
          159  +    "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
          160  +    "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
          161  +    "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
          162  +    "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
          163  +    "WITH", "WITHOUT",
          164  +  };
          165  +  int i, lwr, upr, mid, c;
          166  +  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
          167  +  for(i=0; zName[i]; i++){
          168  +    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
          169  +  }
          170  +  lwr = 0;
          171  +  upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
          172  +  while( lwr<=upr ){
          173  +    mid = (lwr+upr)/2;
          174  +    c = sqlite3_stricmp(azKeywords[mid], zName);
          175  +    if( c==0 ) return '"';
          176  +    if( c<0 ){
          177  +      lwr = mid+1;
          178  +    }else{
          179  +      upr = mid-1;
          180  +    }
          181  +  }
          182  +  return 0;
          183  +}
          184  +
          185  +
          186  +/*
          187  +** Release memory previously allocated by tableColumnList().
          188  +*/
          189  +static void freeColumnList(char **azCol){
          190  +  int i;
          191  +  for(i=1; azCol[i]; i++){
          192  +    sqlite3_free(azCol[i]);
          193  +  }
          194  +  /* azCol[0] is a static string */
          195  +  sqlite3_free(azCol);
          196  +}
          197  +
          198  +/*
          199  +** Return a list of pointers to strings which are the names of all
          200  +** columns in table zTab.   The memory to hold the names is dynamically
          201  +** allocated and must be released by the caller using a subsequent call
          202  +** to freeColumnList().
          203  +**
          204  +** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
          205  +** value that needs to be preserved, then azCol[0] is filled in with the
          206  +** name of the rowid column.
          207  +**
          208  +** The first regular column in the table is azCol[1].  The list is terminated
          209  +** by an entry with azCol[i]==0.
          210  +*/
          211  +static char **tableColumnList(DState *p, const char *zTab){
          212  +  char **azCol = 0;
          213  +  sqlite3_stmt *pStmt = 0;
          214  +  char *zSql;
          215  +  int nCol = 0;
          216  +  int nAlloc = 0;
          217  +  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
          218  +  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
          219  +  int preserveRowid = 1;
          220  +  int rc;
          221  +
          222  +  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
          223  +  if( zSql==0 ) return 0;
          224  +  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
          225  +  sqlite3_free(zSql);
          226  +  if( rc ) return 0;
          227  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
          228  +    if( nCol>=nAlloc-2 ){
          229  +      char **azNew;
          230  +      nAlloc = nAlloc*2 + nCol + 10;
          231  +      azNew = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
          232  +      if( azNew==0 ) goto col_oom;
          233  +      azCol = azNew;
          234  +      azCol[0] = 0;
          235  +    }
          236  +    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
          237  +    if( azCol[nCol]==0 ) goto col_oom;
          238  +    if( sqlite3_column_int(pStmt, 5) ){
          239  +      nPK++;
          240  +      if( nPK==1
          241  +       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
          242  +                          "INTEGER")==0 
          243  +      ){
          244  +        isIPK = 1;
          245  +      }else{
          246  +        isIPK = 0;
          247  +      }
          248  +    }
          249  +  }
          250  +  sqlite3_finalize(pStmt);
          251  +  pStmt = 0;
          252  +  azCol[nCol+1] = 0;
          253  +
          254  +  /* The decision of whether or not a rowid really needs to be preserved
          255  +  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
          256  +  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
          257  +  ** rowids on tables where the rowid is inaccessible because there are other
          258  +  ** columns in the table named "rowid", "_rowid_", and "oid".
          259  +  */
          260  +  if( isIPK ){
          261  +    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
          262  +    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
          263  +    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
          264  +    ** ROWID aliases.  To distinguish these cases, check to see if
          265  +    ** there is a "pk" entry in "PRAGMA index_list".  There will be
          266  +    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
          267  +    */
          268  +    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
          269  +                           " WHERE origin='pk'", zTab);
          270  +    if( zSql==0 ) goto col_oom;
          271  +    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
          272  +    sqlite3_free(zSql);
          273  +    if( rc ){
          274  +      freeColumnList(azCol);
          275  +      return 0;
          276  +    }
          277  +    rc = sqlite3_step(pStmt);
          278  +    sqlite3_finalize(pStmt);
          279  +    pStmt = 0;
          280  +    preserveRowid = rc==SQLITE_ROW;
          281  +  }
          282  +  if( preserveRowid ){
          283  +    /* Only preserve the rowid if we can find a name to use for the
          284  +    ** rowid */
          285  +    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
          286  +    int i, j;
          287  +    for(j=0; j<3; j++){
          288  +      for(i=1; i<=nCol; i++){
          289  +        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
          290  +      }
          291  +      if( i>nCol ){
          292  +        /* At this point, we know that azRowid[j] is not the name of any
          293  +        ** ordinary column in the table.  Verify that azRowid[j] is a valid
          294  +        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
          295  +        ** tables will fail this last check */
          296  +        int rc;
          297  +        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
          298  +        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
          299  +        break;
          300  +      }
          301  +    }
          302  +  }
          303  +  return azCol;
          304  +
          305  +col_oom:
          306  +  sqlite3_finalize(pStmt);
          307  +  freeColumnList(azCol);
          308  +  p->nErr++;
          309  +  p->rc = SQLITE_NOMEM;
          310  +  return 0;
          311  +}
          312  +
          313  +/*
          314  +** Send mprintf-formatted content to the output callback.
          315  +*/
          316  +static void output_formatted(DState *p, const char *zFormat, ...){
          317  +  va_list ap;
          318  +  char *z;
          319  +  va_start(ap, zFormat);
          320  +  z = sqlite3_vmprintf(zFormat, ap);
          321  +  va_end(ap);
          322  +  p->xCallback(z, p->pArg);
          323  +  sqlite3_free(z);
          324  +}
          325  +
          326  +/*
          327  +** Output the given string as a quoted string using SQL quoting conventions.
          328  +**
          329  +** The "\n" and "\r" characters are converted to char(10) and char(13)
          330  +** to prevent them from being transformed by end-of-line translators.
          331  +*/
          332  +static void output_quoted_string(DState *p, const unsigned char *z){
          333  +  int i;
          334  +  char c;
          335  +  int inQuote = 0;
          336  +  int bStarted = 0;
          337  +
          338  +  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
          339  +  if( c==0 ){
          340  +    output_formatted(p, "'%s'", z);
          341  +    return;
          342  +  }
          343  +  while( *z ){
          344  +    for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
          345  +    if( c=='\'' ) i++;
          346  +    if( i ){
          347  +      if( !inQuote ){
          348  +        if( bStarted ) p->xCallback("||", p->pArg);
          349  +        p->xCallback("'", p->pArg);
          350  +        inQuote = 1;
          351  +      }
          352  +      output_formatted(p, "%.*s", i, z);
          353  +      z += i;
          354  +      bStarted = 1;
          355  +    }
          356  +    if( c=='\'' ){
          357  +      p->xCallback("'", p->pArg);
          358  +      continue;
          359  +    }
          360  +    if( inQuote ){
          361  +      p->xCallback("'", p->pArg);
          362  +      inQuote = 0;
          363  +    }
          364  +    if( c==0 ){
          365  +      break;
          366  +    }
          367  +    for(i=0; (c = z[i])=='\r' || c=='\n'; i++){
          368  +      if( bStarted ) p->xCallback("||", p->pArg);
          369  +      output_formatted(p, "char(%d)", c);
          370  +      bStarted = 1;
          371  +    }
          372  +    z += i;
          373  +  }
          374  +  if( inQuote ) p->xCallback("'", p->pArg);
          375  +}
          376  +
          377  +/*
          378  +** This is an sqlite3_exec callback routine used for dumping the database.
          379  +** Each row received by this callback consists of a table name,
          380  +** the table type ("index" or "table") and SQL to create the table.
          381  +** This routine should print text sufficient to recreate the table.
          382  +*/
          383  +static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
          384  +  int rc;
          385  +  const char *zTable;
          386  +  const char *zType;
          387  +  const char *zSql;
          388  +  DState *p = (DState*)pArg;
          389  +  sqlite3_stmt *pStmt;
          390  +
          391  +  (void)azCol;
          392  +  if( nArg!=3 ) return 1;
          393  +  zTable = azArg[0];
          394  +  zType = azArg[1];
          395  +  zSql = azArg[2];
          396  +
          397  +  if( strcmp(zTable, "sqlite_sequence")==0 ){
          398  +    p->xCallback("DELETE FROM sqlite_sequence;\n", p->pArg);
          399  +  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
          400  +    p->xCallback("ANALYZE sqlite_master;\n", p->pArg);
          401  +  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
          402  +    return 0;
          403  +  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
          404  +    if( !p->writableSchema ){
          405  +      p->xCallback("PRAGMA writable_schema=ON;\n", p->pArg);
          406  +      p->writableSchema = 1;
          407  +    }
          408  +    output_formatted(p,
          409  +       "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
          410  +       "VALUES('table','%q','%q',0,'%q');",
          411  +       zTable, zTable, zSql);
          412  +    return 0;
          413  +  }else{
          414  +    if( sqlite3_strglob("CREATE TABLE ['\"]*", zSql)==0 ){
          415  +      p->xCallback("CREATE TABLE IF NOT EXISTS ", p->pArg);
          416  +      p->xCallback(zSql+13, p->pArg);
          417  +    }else{
          418  +      p->xCallback(zSql, p->pArg);
          419  +    }
          420  +    p->xCallback(";\n", p->pArg);
          421  +  }
          422  +
          423  +  if( strcmp(zType, "table")==0 ){
          424  +    DText sSelect;
          425  +    DText sTable;
          426  +    char **azCol;
          427  +    int i;
          428  +    int nCol;
          429  +
          430  +    azCol = tableColumnList(p, zTable);
          431  +    if( azCol==0 ) return 0;
          432  +
          433  +    initText(&sTable);
          434  +    appendText(&sTable, "INSERT INTO ", 0);
          435  +
          436  +    /* Always quote the table name, even if it appears to be pure ascii,
          437  +    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
          438  +    appendText(&sTable, zTable, quoteChar(zTable));
          439  +
          440  +    /* If preserving the rowid, add a column list after the table name.
          441  +    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
          442  +    ** instead of the usual "INSERT INTO tab VALUES(...)".
          443  +    */
          444  +    if( azCol[0] ){
          445  +      appendText(&sTable, "(", 0);
          446  +      appendText(&sTable, azCol[0], 0);
          447  +      for(i=1; azCol[i]; i++){
          448  +        appendText(&sTable, ",", 0);
          449  +        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
          450  +      }
          451  +      appendText(&sTable, ")", 0);
          452  +    }
          453  +    appendText(&sTable, " VALUES(", 0);
          454  +
          455  +    /* Build an appropriate SELECT statement */
          456  +    initText(&sSelect);
          457  +    appendText(&sSelect, "SELECT ", 0);
          458  +    if( azCol[0] ){
          459  +      appendText(&sSelect, azCol[0], 0);
          460  +      appendText(&sSelect, ",", 0);
          461  +    }
          462  +    for(i=1; azCol[i]; i++){
          463  +      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
          464  +      if( azCol[i+1] ){
          465  +        appendText(&sSelect, ",", 0);
          466  +      }
          467  +    }
          468  +    nCol = i;
          469  +    if( azCol[0]==0 ) nCol--;
          470  +    freeColumnList(azCol);
          471  +    appendText(&sSelect, " FROM ", 0);
          472  +    appendText(&sSelect, zTable, quoteChar(zTable));
          473  +
          474  +    rc = sqlite3_prepare_v2(p->db, sSelect.z, -1, &pStmt, 0);
          475  +    if( rc!=SQLITE_OK ){
          476  +      p->nErr++;
          477  +      if( p->rc==SQLITE_OK ) p->rc = rc;
          478  +    }else{
          479  +      while( SQLITE_ROW==sqlite3_step(pStmt) ){
          480  +        p->xCallback(sTable.z, p->pArg);
          481  +        for(i=0; i<nCol; i++){
          482  +          if( i ) p->xCallback(",", p->pArg);
          483  +          switch( sqlite3_column_type(pStmt,i) ){
          484  +            case SQLITE_INTEGER: {
          485  +              output_formatted(p, "%lld", sqlite3_column_int64(pStmt,i));
          486  +              break;
          487  +            }
          488  +            case SQLITE_FLOAT: {
          489  +              double r = sqlite3_column_double(pStmt,i);
          490  +              output_formatted(p, "%!.20g", r);
          491  +              break;
          492  +            }
          493  +            case SQLITE_NULL: {
          494  +              p->xCallback("NULL", p->pArg);
          495  +              break;
          496  +            }
          497  +            case SQLITE_TEXT: {
          498  +              output_quoted_string(p, sqlite3_column_text(pStmt,i));
          499  +              break;
          500  +            }
          501  +            case SQLITE_BLOB: {
          502  +              int nByte = sqlite3_column_bytes(pStmt,i);
          503  +              unsigned char *a = (unsigned char*)sqlite3_column_blob(pStmt,i);
          504  +              int j;
          505  +              p->xCallback("x'", p->pArg);
          506  +              for(j=0; j<nByte; j++){
          507  +                char zWord[3];
          508  +                zWord[0] = "0123456789abcdef"[(a[j]>>4)&15];
          509  +                zWord[1] = "0123456789abcdef"[a[j]&15];
          510  +                zWord[2] = 0;
          511  +                p->xCallback(zWord, p->pArg);
          512  +              }
          513  +              p->xCallback("'", p->pArg);
          514  +              break;
          515  +            }
          516  +          }
          517  +        }
          518  +        p->xCallback(");\n", p->pArg);
          519  +      }
          520  +    }
          521  +    sqlite3_finalize(pStmt);
          522  +    freeText(&sTable);
          523  +    freeText(&sSelect);
          524  +  }
          525  +  return 0;
          526  +}
          527  +
          528  +
          529  +/*
          530  +** Execute a query statement that will generate SQL output.  Print
          531  +** the result columns, comma-separated, on a line and then add a
          532  +** semicolon terminator to the end of that line.
          533  +**
          534  +** If the number of columns is 1 and that column contains text "--"
          535  +** then write the semicolon on a separate line.  That way, if a
          536  +** "--" comment occurs at the end of the statement, the comment
          537  +** won't consume the semicolon terminator.
          538  +*/
          539  +static void output_sql_from_query(
          540  +  DState *p,               /* Query context */
          541  +  const char *zSelect,     /* SELECT statement to extract content */
          542  +  ...
          543  +){
          544  +  sqlite3_stmt *pSelect;
          545  +  int rc;
          546  +  int nResult;
          547  +  int i;
          548  +  const char *z;
          549  +  char *zSql;
          550  +  va_list ap;
          551  +  va_start(ap, zSelect);
          552  +  zSql = sqlite3_vmprintf(zSelect, ap);
          553  +  va_end(ap);
          554  +  if( zSql==0 ){
          555  +    p->rc = SQLITE_NOMEM;
          556  +    p->nErr++;
          557  +    return;
          558  +  }
          559  +  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pSelect, 0);
          560  +  sqlite3_free(zSql);
          561  +  if( rc!=SQLITE_OK || !pSelect ){
          562  +    output_formatted(p, "/**** ERROR: (%d) %s *****/\n", rc,
          563  +                sqlite3_errmsg(p->db));
          564  +    p->nErr++;
          565  +    return;
          566  +  }
          567  +  rc = sqlite3_step(pSelect);
          568  +  nResult = sqlite3_column_count(pSelect);
          569  +  while( rc==SQLITE_ROW ){
          570  +    z = (const char*)sqlite3_column_text(pSelect, 0);
          571  +    p->xCallback(z, p->pArg);
          572  +    for(i=1; i<nResult; i++){
          573  +      p->xCallback(",", p->pArg);
          574  +      p->xCallback((const char*)sqlite3_column_text(pSelect,i), p->pArg);
          575  +    }
          576  +    if( z==0 ) z = "";
          577  +    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
          578  +    if( z[0] ){
          579  +      p->xCallback("\n;\n", p->pArg);
          580  +    }else{
          581  +      p->xCallback(";\n", p->pArg);
          582  +    }
          583  +    rc = sqlite3_step(pSelect);
          584  +  }
          585  +  rc = sqlite3_finalize(pSelect);
          586  +  if( rc!=SQLITE_OK ){
          587  +    output_formatted(p, "/**** ERROR: (%d) %s *****/\n", rc,
          588  +                     sqlite3_errmsg(p->db));
          589  +    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
          590  +  }
          591  +}
          592  +
          593  +/*
          594  +** Run zQuery.  Use dump_callback() as the callback routine so that
          595  +** the contents of the query are output as SQL statements.
          596  +**
          597  +** If we get a SQLITE_CORRUPT error, rerun the query after appending
          598  +** "ORDER BY rowid DESC" to the end.
          599  +*/
          600  +static void run_schema_dump_query(
          601  +  DState *p,
          602  +  const char *zQuery,
          603  +  ...
          604  +){
          605  +  char *zErr = 0;
          606  +  char *z;
          607  +  va_list ap;
          608  +  va_start(ap, zQuery);
          609  +  z = sqlite3_vmprintf(zQuery, ap);
          610  +  va_end(ap); 
          611  +  sqlite3_exec(p->db, z, dump_callback, p, &zErr);
          612  +  sqlite3_free(z);
          613  +  if( zErr ){
          614  +    output_formatted(p, "/****** %s ******/\n", zErr);
          615  +    sqlite3_free(zErr);
          616  +    p->nErr++;
          617  +    zErr = 0;
          618  +  }
          619  +}
          620  +
          621  +/*
          622  +** Convert an SQLite database into SQL statements that will recreate that
          623  +** database.
          624  +*/
          625  +int sqlite3_db_dump(
          626  +  sqlite3 *db,               /* The database connection */
          627  +  const char *zSchema,       /* Which schema to dump.  Usually "main". */
          628  +  const char *zTable,        /* Which table to dump.  NULL means everything. */
          629  +  int (*xCallback)(const char*,void*),   /* Output sent to this callback */
          630  +  void *pArg                             /* Second argument of the callback */
          631  +){
          632  +  DState x;
          633  +  memset(&x, 0, sizeof(x));
          634  +  x.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
          635  +  if( x.rc ) return x.rc;
          636  +  x.db = db;
          637  +  x.xCallback = xCallback;
          638  +  x.pArg = pArg;
          639  +  xCallback("PRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;\n", pArg);
          640  +  if( zTable==0 ){
          641  +    run_schema_dump_query(&x,
          642  +      "SELECT name, type, sql FROM \"%w\".sqlite_master "
          643  +      "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'",
          644  +      zSchema
          645  +    );
          646  +    run_schema_dump_query(&x,
          647  +      "SELECT name, type, sql FROM \"%w\".sqlite_master "
          648  +      "WHERE name=='sqlite_sequence'", zSchema
          649  +    );
          650  +    output_sql_from_query(&x,
          651  +      "SELECT sql FROM sqlite_master "
          652  +      "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
          653  +    );
          654  +  }else{
          655  +    run_schema_dump_query(&x,
          656  +      "SELECT name, type, sql FROM \"%w\".sqlite_master "
          657  +      "WHERE tbl_name=%Q COLLATE nocase AND type=='table'"
          658  +      "  AND sql NOT NULL",
          659  +      zSchema, zTable
          660  +    );
          661  +    output_sql_from_query(&x,
          662  +      "SELECT sql FROM \"%w\".sqlite_master "
          663  +      "WHERE sql NOT NULL"
          664  +      "  AND type IN ('index','trigger','view')"
          665  +      "  AND tbl_name=%Q COLLATE nocase",
          666  +      zSchema, zTable
          667  +    ); 
          668  +  }
          669  +  if( x.writableSchema ){
          670  +    xCallback("PRAGMA writable_schema=OFF;\n", pArg);
          671  +  }
          672  +  xCallback(x.nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n", pArg);
          673  +  sqlite3_exec(db, "COMMIT", 0, 0, 0);
          674  +  return x.rc;
          675  +}
          676  +
          677  +
          678  +
          679  +/* The generic subroutine is above.  The code the follows implements
          680  +** the command-line interface.
          681  +*/
          682  +#ifdef DBDUMP_STANDALONE
          683  +#include <stdio.h>
          684  +
          685  +/*
          686  +** Command-line interface
          687  +*/
          688  +int main(int argc, char **argv){
          689  +  sqlite3 *db;
          690  +  const char *zDb;
          691  +  const char *zSchema;
          692  +  const char *zTable = 0;
          693  +  int rc;
          694  +
          695  +  if( argc<2 || argc>4 ){
          696  +    fprintf(stderr, "Usage: %s DATABASE ?SCHEMA? ?TABLE?\n", argv[0]);
          697  +    return 1;
          698  +  }
          699  +  zDb = argv[1];
          700  +  zSchema = argc>=3 ? argv[2] : "main";
          701  +  zTable = argc==4 ? argv[3] : 0;
          702  +
          703  +  rc = sqlite3_open(zDb, &db);
          704  +  if( rc ){
          705  +    fprintf(stderr, "Cannot open \"%s\": %s\n", zDb, sqlite3_errmsg(db));
          706  +    sqlite3_close(db);
          707  +    return 1;
          708  +  }
          709  +  rc = sqlite3_db_dump(db, zSchema, zTable, 
          710  +          (int(*)(const char*,void*))fputs, (void*)stdout);
          711  +  if( rc ){
          712  +    fprintf(stderr, "Error: sqlite3_db_dump() returns %d\n", rc);
          713  +  }
          714  +  sqlite3_close(db);
          715  +  return rc!=SQLITE_OK;  
          716  +}
          717  +#endif /* DBDUMP_STANDALONE */

Changes to ext/misc/json1.c.

   134    134   };
   135    135   
   136    136   /* Bit values for the JsonNode.jnFlag field
   137    137   */
   138    138   #define JNODE_RAW     0x01         /* Content is raw, not JSON encoded */
   139    139   #define JNODE_ESCAPE  0x02         /* Content is text with \ escapes */
   140    140   #define JNODE_REMOVE  0x04         /* Do not output */
   141         -#define JNODE_REPLACE 0x08         /* Replace with JsonNode.iVal */
   142         -#define JNODE_APPEND  0x10         /* More ARRAY/OBJECT entries at u.iAppend */
   143         -#define JNODE_LABEL   0x20         /* Is a label of an object */
          141  +#define JNODE_REPLACE 0x08         /* Replace with JsonNode.u.iReplace */
          142  +#define JNODE_PATCH   0x10         /* Patch with JsonNode.u.pPatch */
          143  +#define JNODE_APPEND  0x20         /* More ARRAY/OBJECT entries at u.iAppend */
          144  +#define JNODE_LABEL   0x40         /* Is a label of an object */
   144    145   
   145    146   
   146    147   /* A single node of parsed JSON
   147    148   */
   148    149   struct JsonNode {
   149    150     u8 eType;              /* One of the JSON_ type values */
   150    151     u8 jnFlags;            /* JNODE flags */
   151         -  u8 iVal;               /* Replacement value when JNODE_REPLACE */
   152    152     u32 n;                 /* Bytes of content, or number of sub-nodes */
   153    153     union {
   154    154       const char *zJContent; /* Content for INT, REAL, and STRING */
   155    155       u32 iAppend;           /* More terms for ARRAY and OBJECT */
   156    156       u32 iKey;              /* Key for ARRAY objects in json_tree() */
          157  +    u32 iReplace;          /* Replacement content for JNODE_REPLACE */
          158  +    JsonNode *pPatch;      /* Node chain of patch for JNODE_PATCH */
   157    159     } u;
   158    160   };
   159    161   
   160    162   /* A completely parsed JSON string
   161    163   */
   162    164   struct JsonParse {
   163    165     u32 nNode;         /* Number of slots of aNode[] used */
................................................................................
   406    408   ** the number of JsonNode objects that are encoded.
   407    409   */
   408    410   static void jsonRenderNode(
   409    411     JsonNode *pNode,               /* The node to render */
   410    412     JsonString *pOut,              /* Write JSON here */
   411    413     sqlite3_value **aReplace       /* Replacement values */
   412    414   ){
          415  +  if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
          416  +    if( pNode->jnFlags & JNODE_REPLACE ){
          417  +      jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
          418  +      return;
          419  +    }
          420  +    pNode = pNode->u.pPatch;
          421  +  }
   413    422     switch( pNode->eType ){
   414    423       default: {
   415    424         assert( pNode->eType==JSON_NULL );
   416    425         jsonAppendRaw(pOut, "null", 4);
   417    426         break;
   418    427       }
   419    428       case JSON_TRUE: {
................................................................................
   437    446         break;
   438    447       }
   439    448       case JSON_ARRAY: {
   440    449         u32 j = 1;
   441    450         jsonAppendChar(pOut, '[');
   442    451         for(;;){
   443    452           while( j<=pNode->n ){
   444         -          if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
   445         -            if( pNode[j].jnFlags & JNODE_REPLACE ){
   446         -              jsonAppendSeparator(pOut);
   447         -              jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
   448         -            }
   449         -          }else{
          453  +          if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
   450    454               jsonAppendSeparator(pOut);
   451    455               jsonRenderNode(&pNode[j], pOut, aReplace);
   452    456             }
   453    457             j += jsonNodeSize(&pNode[j]);
   454    458           }
   455    459           if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
   456    460           pNode = &pNode[pNode->u.iAppend];
................................................................................
   464    468         jsonAppendChar(pOut, '{');
   465    469         for(;;){
   466    470           while( j<=pNode->n ){
   467    471             if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
   468    472               jsonAppendSeparator(pOut);
   469    473               jsonRenderNode(&pNode[j], pOut, aReplace);
   470    474               jsonAppendChar(pOut, ':');
   471         -            if( pNode[j+1].jnFlags & JNODE_REPLACE ){
   472         -              jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
   473         -            }else{
   474         -              jsonRenderNode(&pNode[j+1], pOut, aReplace);
   475         -            }
          475  +            jsonRenderNode(&pNode[j+1], pOut, aReplace);
   476    476             }
   477    477             j += 1 + jsonNodeSize(&pNode[j+1]);
   478    478           }
   479    479           if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
   480    480           pNode = &pNode[pNode->u.iAppend];
   481    481           j = 1;
   482    482         }
................................................................................
   695    695     JsonNode *p;
   696    696     if( pParse->nNode>=pParse->nAlloc ){
   697    697       return jsonParseAddNodeExpand(pParse, eType, n, zContent);
   698    698     }
   699    699     p = &pParse->aNode[pParse->nNode];
   700    700     p->eType = (u8)eType;
   701    701     p->jnFlags = 0;
   702         -  p->iVal = 0;
   703    702     p->n = n;
   704    703     p->u.zJContent = zContent;
   705    704     return pParse->nNode++;
   706    705   }
   707    706   
   708    707   /*
   709    708   ** Return true if z[] begins with 4 (or more) hexadecimal digits
................................................................................
  1161   1160   ){
  1162   1161     char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
  1163   1162                                  zFuncName);
  1164   1163     sqlite3_result_error(pCtx, zMsg, -1);
  1165   1164     sqlite3_free(zMsg);     
  1166   1165   }
  1167   1166   
         1167  +/*
         1168  +** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
         1169  +*/
         1170  +static void jsonRemoveAllNulls(JsonNode *pNode){
         1171  +  int i, n;
         1172  +  assert( pNode->eType==JSON_OBJECT );
         1173  +  n = pNode->n;
         1174  +  for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
         1175  +    switch( pNode[i].eType ){
         1176  +      case JSON_NULL:
         1177  +        pNode[i].jnFlags |= JNODE_REMOVE;
         1178  +        break;
         1179  +      case JSON_OBJECT:
         1180  +        jsonRemoveAllNulls(&pNode[i]);
         1181  +        break;
         1182  +    }
         1183  +  }
         1184  +}
         1185  +
  1168   1186   
  1169   1187   /****************************************************************************
  1170   1188   ** SQL functions used for testing and debugging
  1171   1189   ****************************************************************************/
  1172   1190   
  1173   1191   #ifdef SQLITE_DEBUG
  1174   1192   /*
................................................................................
  1352   1370       jsonAppendChar(&jx, ']');
  1353   1371       jsonResult(&jx);
  1354   1372       sqlite3_result_subtype(ctx, JSON_SUBTYPE);
  1355   1373     }
  1356   1374     jsonReset(&jx);
  1357   1375     jsonParseReset(&x);
  1358   1376   }
         1377  +
         1378  +/* This is the RFC 7396 MergePatch algorithm.
         1379  +*/
         1380  +static JsonNode *jsonMergePatch(
         1381  +  JsonParse *pParse,   /* The JSON parser that contains the TARGET */
         1382  +  int iTarget,         /* Node of the TARGET in pParse */
         1383  +  JsonNode *pPatch     /* The PATCH */
         1384  +){
         1385  +  u32 i, j;
         1386  +  u32 iRoot;
         1387  +  JsonNode *pTarget;
         1388  +  if( pPatch->eType!=JSON_OBJECT ){
         1389  +    return pPatch;
         1390  +  }
         1391  +  assert( iTarget>=0 && iTarget<pParse->nNode );
         1392  +  pTarget = &pParse->aNode[iTarget];
         1393  +  assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
         1394  +  if( pTarget->eType!=JSON_OBJECT ){
         1395  +    jsonRemoveAllNulls(pPatch);
         1396  +    return pPatch;
         1397  +  }
         1398  +  iRoot = iTarget;
         1399  +  for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
         1400  +    u32 nKey;
         1401  +    const char *zKey;
         1402  +    assert( pPatch[i].eType==JSON_STRING );
         1403  +    assert( pPatch[i].jnFlags & JNODE_LABEL );
         1404  +    nKey = pPatch[i].n;
         1405  +    zKey = pPatch[i].u.zJContent;
         1406  +    assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
         1407  +    for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
         1408  +      assert( pTarget[j].eType==JSON_STRING );
         1409  +      assert( pTarget[j].jnFlags & JNODE_LABEL );
         1410  +      assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
         1411  +      if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
         1412  +        if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
         1413  +        if( pPatch[i+1].eType==JSON_NULL ){
         1414  +          pTarget[j+1].jnFlags |= JNODE_REMOVE;
         1415  +        }else{
         1416  +          JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
         1417  +          if( pNew==0 ) return 0;
         1418  +          pTarget = &pParse->aNode[iTarget];
         1419  +          if( pNew!=&pTarget[j+1] ){
         1420  +            pTarget[j+1].u.pPatch = pNew;
         1421  +            pTarget[j+1].jnFlags |= JNODE_PATCH;
         1422  +          }
         1423  +        }
         1424  +        break;
         1425  +      }
         1426  +    }
         1427  +    if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
         1428  +      int iStart, iPatch;
         1429  +      iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
         1430  +      jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
         1431  +      iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
         1432  +      if( pParse->oom ) return 0;
         1433  +      jsonRemoveAllNulls(pPatch);
         1434  +      pTarget = &pParse->aNode[iTarget];
         1435  +      pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
         1436  +      pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
         1437  +      iRoot = iStart;
         1438  +      pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
         1439  +      pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
         1440  +    }
         1441  +  }
         1442  +  return pTarget;
         1443  +}
         1444  +
         1445  +/*
         1446  +** Implementation of the json_mergepatch(JSON1,JSON2) function.  Return a JSON
         1447  +** object that is the result of running the RFC 7396 MergePatch() algorithm
         1448  +** on the two arguments.
         1449  +*/
         1450  +static void jsonPatchFunc(
         1451  +  sqlite3_context *ctx,
         1452  +  int argc,
         1453  +  sqlite3_value **argv
         1454  +){
         1455  +  JsonParse x;     /* The JSON that is being patched */
         1456  +  JsonParse y;     /* The patch */
         1457  +  JsonNode *pResult;   /* The result of the merge */
         1458  +
         1459  +  UNUSED_PARAM(argc);
         1460  +  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
         1461  +  if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
         1462  +    jsonParseReset(&x);
         1463  +    return;
         1464  +  }
         1465  +  pResult = jsonMergePatch(&x, 0, y.aNode);
         1466  +  assert( pResult!=0 || x.oom );
         1467  +  if( pResult ){
         1468  +    jsonReturnJson(pResult, ctx, 0);
         1469  +  }else{
         1470  +    sqlite3_result_error_nomem(ctx);
         1471  +  }
         1472  +  jsonParseReset(&x);
         1473  +  jsonParseReset(&y);
         1474  +}
         1475  +
  1359   1476   
  1360   1477   /*
  1361   1478   ** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
  1362   1479   ** object that contains all name/value given in arguments.  Or if any name
  1363   1480   ** is not a string or if any value is a BLOB, throw an error.
  1364   1481   */
  1365   1482   static void jsonObjectFunc(
................................................................................
  1456   1573     assert( x.nNode );
  1457   1574     for(i=1; i<(u32)argc; i+=2){
  1458   1575       zPath = (const char*)sqlite3_value_text(argv[i]);
  1459   1576       pNode = jsonLookup(&x, zPath, 0, ctx);
  1460   1577       if( x.nErr ) goto replace_err;
  1461   1578       if( pNode ){
  1462   1579         pNode->jnFlags |= (u8)JNODE_REPLACE;
  1463         -      pNode->iVal = (u8)(i+1);
         1580  +      pNode->u.iReplace = i + 1;
  1464   1581       }
  1465   1582     }
  1466   1583     if( x.aNode[0].jnFlags & JNODE_REPLACE ){
  1467         -    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
         1584  +    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
  1468   1585     }else{
  1469   1586       jsonReturnJson(x.aNode, ctx, argv);
  1470   1587     }
  1471   1588   replace_err:
  1472   1589     jsonParseReset(&x);
  1473   1590   }
  1474   1591   
................................................................................
  1510   1627       if( x.oom ){
  1511   1628         sqlite3_result_error_nomem(ctx);
  1512   1629         goto jsonSetDone;
  1513   1630       }else if( x.nErr ){
  1514   1631         goto jsonSetDone;
  1515   1632       }else if( pNode && (bApnd || bIsSet) ){
  1516   1633         pNode->jnFlags |= (u8)JNODE_REPLACE;
  1517         -      pNode->iVal = (u8)(i+1);
         1634  +      pNode->u.iReplace = i + 1;
  1518   1635       }
  1519   1636     }
  1520   1637     if( x.aNode[0].jnFlags & JNODE_REPLACE ){
  1521         -    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
         1638  +    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
  1522   1639     }else{
  1523   1640       jsonReturnJson(x.aNode, ctx, argv);
  1524   1641     }
  1525   1642   jsonSetDone:
  1526   1643     jsonParseReset(&x);
  1527   1644   }
  1528   1645   
................................................................................
  2157   2274       { "json",                 1, 0,   jsonRemoveFunc        },
  2158   2275       { "json_array",          -1, 0,   jsonArrayFunc         },
  2159   2276       { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
  2160   2277       { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
  2161   2278       { "json_extract",        -1, 0,   jsonExtractFunc       },
  2162   2279       { "json_insert",         -1, 0,   jsonSetFunc           },
  2163   2280       { "json_object",         -1, 0,   jsonObjectFunc        },
         2281  +    { "json_patch",           2, 0,   jsonPatchFunc         },
  2164   2282       { "json_quote",           1, 0,   jsonQuoteFunc         },
  2165   2283       { "json_remove",         -1, 0,   jsonRemoveFunc        },
  2166   2284       { "json_replace",        -1, 0,   jsonReplaceFunc       },
  2167   2285       { "json_set",            -1, 1,   jsonSetFunc           },
  2168   2286       { "json_type",            1, 0,   jsonTypeFunc          },
  2169   2287       { "json_type",            2, 0,   jsonTypeFunc          },
  2170   2288       { "json_valid",           1, 0,   jsonValidFunc         },

Changes to ext/misc/shathree.c.

    17     17   **     sha3_query(Y,SIZE)
    18     18   **
    19     19   ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
    20     20   ** X is NULL.
    21     21   **
    22     22   ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
    23     23   ** and returns a hash of their results.
           24  +**
           25  +** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
           26  +** is used.  If SIZE is included it must be one of the integers 224, 256,
           27  +** 384, or 512, to determine SHA3 hash variant that is computed.
    24     28   */
    25     29   #include "sqlite3ext.h"
    26     30   SQLITE_EXTENSION_INIT1
    27     31   #include <assert.h>
    28     32   #include <string.h>
    29     33   #include <stdarg.h>
    30     34   typedef sqlite3_uint64 u64;

Changes to ext/rtree/rtree.c.

   455    455     memcpy(&x, p, 8);
   456    456     return (i64)__builtin_bswap64(x);
   457    457   #elif SQLITE_BYTEORDER==4321
   458    458     i64 x;
   459    459     memcpy(&x, p, 8);
   460    460     return x;
   461    461   #else
   462         -  return (
   463         -    (((i64)p[0]) << 56) + 
   464         -    (((i64)p[1]) << 48) + 
   465         -    (((i64)p[2]) << 40) + 
   466         -    (((i64)p[3]) << 32) + 
   467         -    (((i64)p[4]) << 24) + 
   468         -    (((i64)p[5]) << 16) + 
   469         -    (((i64)p[6]) <<  8) + 
   470         -    (((i64)p[7]) <<  0)
          462  +  return (i64)(
          463  +    (((u64)p[0]) << 56) + 
          464  +    (((u64)p[1]) << 48) + 
          465  +    (((u64)p[2]) << 40) + 
          466  +    (((u64)p[3]) << 32) + 
          467  +    (((u64)p[4]) << 24) + 
          468  +    (((u64)p[5]) << 16) + 
          469  +    (((u64)p[6]) <<  8) + 
          470  +    (((u64)p[7]) <<  0)
   471    471     );
   472    472   #endif
   473    473   }
   474    474   
   475    475   /*
   476    476   ** Functions to serialize a 16 bit integer, 32 bit real number and
   477    477   ** 64 bit integer. The value returned is the number of bytes written

Changes to main.mk.

   745    745   	echo "static const char *zMainloop = " >> $@
   746    746   	tclsh $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
   747    747   	echo "; return zMainloop; }" >> $@
   748    748   
   749    749   sqlite3_analyzer$(EXE): sqlite3_analyzer.c
   750    750   	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
   751    751   
          752  +dbdump$(EXE):	$(TOP)/ext/misc/dbdump.c sqlite3.o
          753  +	$(TCCX) -DDBDUMP_STANDALONE -o dbdump$(EXE) \
          754  +            $(TOP)/ext/misc/dbdump.c sqlite3.o $(THREADLIB)
          755  +
   752    756   # Rules to build the 'testfixture' application.
   753    757   #
   754    758   TESTFIXTURE_FLAGS  = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
   755    759   TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
   756    760   TESTFIXTURE_FLAGS += -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
   757    761   TESTFIXTURE_FLAGS += -DSQLITE_DEFAULT_PAGE_SIZE=1024
   758    762   

Changes to src/analyze.c.

  1200   1200         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  1201   1201       }else{
  1202   1202         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  1203   1203         int j, k, regKey;
  1204   1204         regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
  1205   1205         for(j=0; j<pPk->nKeyCol; j++){
  1206   1206           k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
  1207         -        assert( k>=0 && k<pTab->nCol );
         1207  +        assert( k>=0 && k<pIdx->nColumn );
  1208   1208           sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
  1209   1209           VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
  1210   1210         }
  1211   1211         sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
  1212   1212         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
  1213   1213       }
  1214   1214   #endif

Changes to src/btree.c.

  2863   2863       BTREE_AUTOVACUUM_INCR
  2864   2864     );
  2865   2865     sqlite3BtreeLeave(p);
  2866   2866     return rc;
  2867   2867   #endif
  2868   2868   }
  2869   2869   
         2870  +/*
         2871  +** If the user has not set the safety-level for this database connection
         2872  +** using "PRAGMA synchronous", and if the safety-level is not already
         2873  +** set to the value passed to this function as the second parameter,
         2874  +** set it so.
         2875  +*/
         2876  +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
         2877  +static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
         2878  +  sqlite3 *db;
         2879  +  Db *pDb;
         2880  +  if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
         2881  +    while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
         2882  +    if( pDb->bSyncSet==0 
         2883  +     && pDb->safety_level!=safety_level 
         2884  +     && pDb!=&db->aDb[1] 
         2885  +    ){
         2886  +      pDb->safety_level = safety_level;
         2887  +      sqlite3PagerSetFlags(pBt->pPager,
         2888  +          pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
         2889  +    }
         2890  +  }
         2891  +}
         2892  +#else
         2893  +# define setDefaultSyncFlag(pBt,safety_level)
         2894  +#endif
  2870   2895   
  2871   2896   /*
  2872   2897   ** Get a reference to pPage1 of the database file.  This will
  2873   2898   ** also acquire a readlock on that file.
  2874   2899   **
  2875   2900   ** SQLITE_OK is returned on success.  If the file is not a
  2876   2901   ** well-formed database file, then SQLITE_CORRUPT is returned.
................................................................................
  2936   2961       */
  2937   2962       if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
  2938   2963         int isOpen = 0;
  2939   2964         rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
  2940   2965         if( rc!=SQLITE_OK ){
  2941   2966           goto page1_init_failed;
  2942   2967         }else{
  2943         -#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
  2944         -        sqlite3 *db;
  2945         -        Db *pDb;
  2946         -        if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
  2947         -          while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
  2948         -          if( pDb->bSyncSet==0
  2949         -           && pDb->safety_level==SQLITE_DEFAULT_SYNCHRONOUS+1
  2950         -          ){
  2951         -            pDb->safety_level = SQLITE_DEFAULT_WAL_SYNCHRONOUS+1;
  2952         -            sqlite3PagerSetFlags(pBt->pPager,
  2953         -               pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
  2954         -          }
  2955         -        }
  2956         -#endif
         2968  +        setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
  2957   2969           if( isOpen==0 ){
  2958   2970             releasePage(pPage1);
  2959   2971             return SQLITE_OK;
  2960   2972           }
  2961   2973         }
  2962   2974         rc = SQLITE_NOTADB;
         2975  +    }else{
         2976  +      setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
  2963   2977       }
  2964   2978   #endif
  2965   2979   
  2966   2980       /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
  2967   2981       ** fractions and the leaf payload fraction values must be 64, 32, and 32.
  2968   2982       **
  2969   2983       ** The original design allowed these amounts to vary, but as of

Changes to src/ctime.c.

    62     62   #endif
    63     63   #if SQLITE_DEFAULT_LOCKING_MODE
    64     64     "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
    65     65   #endif
    66     66   #if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
    67     67     "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
    68     68   #endif
           69  +#if SQLITE_DEFAULT_SYNCHRONOUS
           70  +  "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
           71  +#endif
           72  +#if SQLITE_DEFAULT_WAL_SYNCHRONOUS
           73  +  "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
           74  +#endif
    69     75   #if SQLITE_DIRECT_OVERFLOW_READ
    70     76     "DIRECT_OVERFLOW_READ",
    71     77   #endif
    72     78   #if SQLITE_DISABLE_DIRSYNC
    73     79     "DISABLE_DIRSYNC",
    74     80   #endif
    75     81   #if SQLITE_DISABLE_LFS

Changes to src/func.c.

   200    200     if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
   201    201     nHaystack = sqlite3_value_bytes(argv[0]);
   202    202     nNeedle = sqlite3_value_bytes(argv[1]);
   203    203     if( nNeedle>0 ){
   204    204       if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
   205    205         zHaystack = sqlite3_value_blob(argv[0]);
   206    206         zNeedle = sqlite3_value_blob(argv[1]);
   207         -      assert( zNeedle!=0 );
   208         -      assert( zHaystack!=0 || nHaystack==0 );
   209    207         isText = 0;
   210    208       }else{
   211    209         zHaystack = sqlite3_value_text(argv[0]);
   212    210         zNeedle = sqlite3_value_text(argv[1]);
   213    211         isText = 1;
   214         -      if( zHaystack==0 || zNeedle==0 ) return;
   215    212       }
          213  +    if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
   216    214       while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
   217    215         N++;
   218    216         do{
   219    217           nHaystack--;
   220    218           zHaystack++;
   221    219         }while( isText && (zHaystack[0]&0xc0)==0x80 );
   222    220       }

Changes to src/insert.c.

  2223   2223         ** If any of the indexed columns use a collation sequence other than
  2224   2224         ** BINARY, this optimization is disabled. This is because the user 
  2225   2225         ** might change the definition of a collation sequence and then run
  2226   2226         ** a VACUUM command. In that case keys may not be written in strictly
  2227   2227         ** sorted order.  */
  2228   2228         for(i=0; i<pSrcIdx->nColumn; i++){
  2229   2229           const char *zColl = pSrcIdx->azColl[i];
  2230         -        assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0
  2231         -                    || sqlite3StrBINARY==zColl );
  2232   2230           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
  2233   2231         }
  2234   2232         if( i==pSrcIdx->nColumn ){
  2235   2233           idxInsFlags = OPFLAG_USESEEKRESULT;
  2236   2234           sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
  2237   2235         }
  2238   2236       }

Changes to src/loadext.c.

   417    417     sqlite3_status64,
   418    418     sqlite3_strlike,
   419    419     sqlite3_db_cacheflush,
   420    420     /* Version 3.12.0 and later */
   421    421     sqlite3_system_errno,
   422    422     /* Version 3.14.0 and later */
   423    423     sqlite3_trace_v2,
   424         -  sqlite3_expanded_sql
          424  +  sqlite3_expanded_sql,
          425  +  /* Version 3.18.0 and later */
          426  +  sqlite3_set_last_insert_rowid
   425    427   };
   426    428   
   427    429   /*
   428    430   ** Attempt to load an SQLite extension library contained in the file
   429    431   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   430    432   ** default entry point name (sqlite3_extension_init) is used.  Use
   431    433   ** of the default name is recommended.

Changes to src/mem1.c.

    53     53   
    54     54   /*
    55     55   ** Use the zone allocator available on apple products unless the
    56     56   ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
    57     57   */
    58     58   #include <sys/sysctl.h>
    59     59   #include <malloc/malloc.h>
           60  +#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
    60     61   #include <libkern/OSAtomic.h>
           62  +#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
    61     63   static malloc_zone_t* _sqliteZone_;
    62     64   #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
    63     65   #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
    64     66   #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
    65     67   #define SQLITE_MALLOCSIZE(x) \
    66     68           (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
    67     69   
................................................................................
   246    248     sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
   247    249     if( cpuCount>1 ){
   248    250       /* defer MT decisions to system malloc */
   249    251       _sqliteZone_ = malloc_default_zone();
   250    252     }else{
   251    253       /* only 1 core, use our own zone to contention over global locks, 
   252    254       ** e.g. we have our own dedicated locks */
   253         -    bool success;
   254         -    malloc_zone_t* newzone = malloc_create_zone(4096, 0);
   255         -    malloc_set_zone_name(newzone, "Sqlite_Heap");
   256         -    do{
   257         -      success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
   258         -                                 (void * volatile *)&_sqliteZone_);
   259         -    }while(!_sqliteZone_);
   260         -    if( !success ){
   261         -      /* somebody registered a zone first */
   262         -      malloc_destroy_zone(newzone);
   263         -    }
          255  +    _sqliteZone_ = malloc_create_zone(4096, 0);
          256  +    malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
   264    257     }
   265         -#endif
          258  +#endif /*  defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
   266    259     UNUSED_PARAMETER(NotUsed);
   267    260     return SQLITE_OK;
   268    261   }
   269    262   
   270    263   /*
   271    264   ** Deinitialize this module.
   272    265   */

Changes to src/pragma.c.

  1011   1011     case PragTyp_SYNCHRONOUS: {
  1012   1012       if( !zRight ){
  1013   1013         returnSingleInt(v, pDb->safety_level-1);
  1014   1014       }else{
  1015   1015         if( !db->autoCommit ){
  1016   1016           sqlite3ErrorMsg(pParse, 
  1017   1017               "Safety level may not be changed inside a transaction");
  1018         -      }else{
         1018  +      }else if( iDb!=1 ){
  1019   1019           int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
  1020   1020           if( iLevel==0 ) iLevel = 1;
  1021   1021           pDb->safety_level = iLevel;
  1022   1022           pDb->bSyncSet = 1;
  1023   1023           setAllPagerFlags(db);
  1024   1024         }
  1025   1025       }
................................................................................
  1501   1501           Table *pTab = sqliteHashData(x);
  1502   1502           Index *pIdx, *pPk;
  1503   1503           Index *pPrior = 0;
  1504   1504           int loopTop;
  1505   1505           int iDataCur, iIdxCur;
  1506   1506           int r1 = -1;
  1507   1507   
         1508  +        if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
  1508   1509           if( pTab->pCheck==0
  1509   1510            && (pTab->tabFlags & TF_HasNotNull)==0
  1510   1511            && (pTab->pIndex==0 || isQuick)
  1511   1512           ){
  1512   1513             continue;  /* No additional checks needed for this table */
  1513   1514           }
  1514   1515           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);

Changes to src/printf.c.

   396    396           if( flag_zeropad && precision<width-(prefix!=0) ){
   397    397             precision = width-(prefix!=0);
   398    398           }
   399    399           if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
   400    400             nOut = etBUFSIZE;
   401    401             zOut = buf;
   402    402           }else{
   403         -          nOut = precision + 10 + precision/3;
   404         -          zOut = zExtra = sqlite3Malloc( nOut );
          403  +          u64 n = (u64)precision + 10 + precision/3;
          404  +          zOut = zExtra = sqlite3Malloc( n );
   405    405             if( zOut==0 ){
   406    406               setStrAccumError(pAccum, STRACCUM_NOMEM);
   407    407               return;
   408    408             }
          409  +          nOut = (int)n;
   409    410           }
   410    411           bufpt = &zOut[nOut-1];
   411    412           if( xtype==etORDINAL ){
   412    413             static const char zOrd[] = "thstndrd";
   413    414             int x = (int)(longvalue % 10);
   414    415             if( x>=4 || (longvalue/10)%10==1 ){
   415    416               x = 0;

Changes to src/shell.c.

  2081   2081   /*
  2082   2082   ** This is the callback routine from sqlite3_exec() that appends all
  2083   2083   ** output onto the end of a ShellText object.
  2084   2084   */
  2085   2085   static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
  2086   2086     ShellText *p = (ShellText*)pArg;
  2087   2087     int i;
         2088  +  UNUSED_PARAMETER(az);
  2088   2089     if( p->n ) appendText(p, "|", 0);
  2089   2090     for(i=0; i<nArg; i++){
  2090   2091       if( i ) appendText(p, ",", 0);
  2091   2092       if( azArg[i] ) appendText(p, azArg[i], 0);
  2092   2093     }
  2093   2094     return 0;
  2094   2095   }
................................................................................
  2922   2923           if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
  2923   2924         }
  2924   2925         if( i>nCol ){
  2925   2926           /* At this point, we know that azRowid[j] is not the name of any
  2926   2927           ** ordinary column in the table.  Verify that azRowid[j] is a valid
  2927   2928           ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
  2928   2929           ** tables will fail this last check */
  2929         -        int rc;
  2930   2930           rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
  2931   2931           if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
  2932   2932           break;
  2933   2933         }
  2934   2934       }
  2935   2935     }
  2936   2936     return azCol;
................................................................................
  2955   2955   
  2956   2956   /*
  2957   2957   ** This is a different callback routine used for dumping the database.
  2958   2958   ** Each row received by this callback consists of a table name,
  2959   2959   ** the table type ("index" or "table") and SQL to create the table.
  2960   2960   ** This routine should print text sufficient to recreate the table.
  2961   2961   */
  2962         -static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
         2962  +static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
  2963   2963     int rc;
  2964   2964     const char *zTable;
  2965   2965     const char *zType;
  2966   2966     const char *zSql;
  2967   2967     ShellState *p = (ShellState *)pArg;
  2968   2968   
  2969         -  UNUSED_PARAMETER(azCol);
         2969  +  UNUSED_PARAMETER(azNotUsed);
  2970   2970     if( nArg!=3 ) return 1;
  2971   2971     zTable = azArg[0];
  2972   2972     zType = azArg[1];
  2973   2973     zSql = azArg[2];
  2974   2974   
  2975   2975     if( strcmp(zTable, "sqlite_sequence")==0 ){
  2976   2976       raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
................................................................................
  3165   3165     ".quit                  Exit this program\n"
  3166   3166     ".read FILENAME         Execute SQL in FILENAME\n"
  3167   3167     ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
  3168   3168     ".save FILE             Write in-memory database into FILE\n"
  3169   3169     ".scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off\n"
  3170   3170     ".schema ?PATTERN?      Show the CREATE statements matching PATTERN\n"
  3171   3171     "                          Add --indent for pretty-printing\n"
         3172  +  ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
  3172   3173     ".separator COL ?ROW?   Change the column separator and optionally the row\n"
  3173   3174     "                         separator for both the output mode and .import\n"
  3174   3175   #if defined(SQLITE_ENABLE_SESSION)
  3175   3176     ".session CMD ...       Create or control sessions\n"
  3176   3177   #endif
  3177   3178     ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
  3178   3179     ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
................................................................................
  4681   4682       int i;
  4682   4683       ShellClearFlag(p, SHFLG_PreserveRowid);
  4683   4684       for(i=1; i<nArg; i++){
  4684   4685         if( azArg[i][0]=='-' ){
  4685   4686           const char *z = azArg[i]+1;
  4686   4687           if( z[0]=='-' ) z++;
  4687   4688           if( strcmp(z,"preserve-rowids")==0 ){
         4689  +#ifdef SQLITE_OMIT_VIRTUALTABLE
         4690  +          raw_printf(stderr, "The --preserve-rowids option is not compatible"
         4691  +                             " with SQLITE_OMIT_VIRTUALTABLE\n");
         4692  +          rc = 1;
         4693  +          goto meta_command_exit;
         4694  +#else
  4688   4695             ShellSetFlag(p, SHFLG_PreserveRowid);
         4696  +#endif
  4689   4697           }else
  4690   4698           {
  4691   4699             raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
  4692   4700             rc = 1;
  4693   4701             goto meta_command_exit;
  4694   4702           }
  4695   4703         }else if( zLike ){

Changes to src/sqlite.h.in.

   110    110   **
   111    111   ** Since [version 3.6.18] ([dateof:3.6.18]), 
   112    112   ** SQLite source code has been stored in the
   113    113   ** <a href="http://www.fossil-scm.org/">Fossil configuration management
   114    114   ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   115    115   ** a string which identifies a particular check-in of SQLite
   116    116   ** within its configuration management system.  ^The SQLITE_SOURCE_ID
   117         -** string contains the date and time of the check-in (UTC) and an SHA1
   118         -** hash of the entire source tree.
          117  +** string contains the date and time of the check-in (UTC) and a SHA1
          118  +** or SHA3-256 hash of the entire source tree.
   119    119   **
   120    120   ** See also: [sqlite3_libversion()],
   121    121   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   122    122   ** [sqlite_version()] and [sqlite_source_id()].
   123    123   */
   124    124   #define SQLITE_VERSION        "--VERS--"
   125    125   #define SQLITE_VERSION_NUMBER --VERSION-NUMBER--
................................................................................
  3421   3421   ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  3422   3422   **
  3423   3423   ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  3424   3424   ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  3425   3425   **
  3426   3426   ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  3427   3427   ** <dd>The maximum number of instructions in a virtual machine program
  3428         -** used to implement an SQL statement.  This limit is not currently
  3429         -** enforced, though that might be added in some future release of
  3430         -** SQLite.</dd>)^
         3428  +** used to implement an SQL statement.  If [sqlite3_prepare_v2()] or
         3429  +** the equivalent tries to allocate space for more than this many opcodes
         3430  +** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
  3431   3431   **
  3432   3432   ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  3433   3433   ** <dd>The maximum number of arguments on a function.</dd>)^
  3434   3434   **
  3435   3435   ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  3436   3436   ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  3437   3437   **
................................................................................
  3460   3460   #define SQLITE_LIMIT_VDBE_OP                   5
  3461   3461   #define SQLITE_LIMIT_FUNCTION_ARG              6
  3462   3462   #define SQLITE_LIMIT_ATTACHED                  7
  3463   3463   #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
  3464   3464   #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3465   3465   #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3466   3466   #define SQLITE_LIMIT_WORKER_THREADS           11
         3467  +
  3467   3468   
  3468   3469   /*
  3469   3470   ** CAPI3REF: Compiling An SQL Statement
  3470   3471   ** KEYWORDS: {SQL statement compiler}
  3471   3472   ** METHOD: sqlite3
  3472   3473   ** CONSTRUCTOR: sqlite3_stmt
  3473   3474   **

Changes to src/sqlite3ext.h.

   278    278     int (*strlike)(const char*,const char*,unsigned int);
   279    279     int (*db_cacheflush)(sqlite3*);
   280    280     /* Version 3.12.0 and later */
   281    281     int (*system_errno)(sqlite3*);
   282    282     /* Version 3.14.0 and later */
   283    283     int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
   284    284     char *(*expanded_sql)(sqlite3_stmt*);
          285  +  /* Version 3.18.0 and later */
          286  +  void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
   285    287   };
   286    288   
   287    289   /*
   288    290   ** This is the function signature used for all extension entry points.  It
   289    291   ** is also defined in the file "loadext.c".
   290    292   */
   291    293   typedef int (*sqlite3_loadext_entry)(
................................................................................
   536    538   #define sqlite3_strlike                sqlite3_api->strlike
   537    539   #define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
   538    540   /* Version 3.12.0 and later */
   539    541   #define sqlite3_system_errno           sqlite3_api->system_errno
   540    542   /* Version 3.14.0 and later */
   541    543   #define sqlite3_trace_v2               sqlite3_api->trace_v2
   542    544   #define sqlite3_expanded_sql           sqlite3_api->expanded_sql
          545  +/* Version 3.18.0 and later */
          546  +#define sqlite3_set_last_insert_rowid  sqlite3_api->set_last_insert_rowid
   543    547   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
   544    548   
   545    549   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   546    550     /* This case when the file really is being compiled as a loadable 
   547    551     ** extension */
   548    552   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   549    553   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/sqliteInt.h.

  1123   1123   **   EXTRA         4                         3
  1124   1124   **
  1125   1125   ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
  1126   1126   ** In other words, the zero-based numbers are used for all external interfaces
  1127   1127   ** and the one-based values are used internally.
  1128   1128   */
  1129   1129   #ifndef SQLITE_DEFAULT_SYNCHRONOUS
  1130         -# define SQLITE_DEFAULT_SYNCHRONOUS (PAGER_SYNCHRONOUS_FULL-1)
         1130  +# define SQLITE_DEFAULT_SYNCHRONOUS 2
  1131   1131   #endif
  1132   1132   #ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
  1133   1133   # define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
  1134   1134   #endif
  1135   1135   
  1136   1136   /*
  1137   1137   ** Each database file to be accessed by the system is an instance

Changes to src/sqliteLimit.h.

    83     83   #endif
    84     84   
    85     85   /*
    86     86   ** The maximum number of opcodes in a VDBE program.
    87     87   ** Not currently enforced.
    88     88   */
    89     89   #ifndef SQLITE_MAX_VDBE_OP
    90         -# define SQLITE_MAX_VDBE_OP 25000
           90  +# define SQLITE_MAX_VDBE_OP 250000000
    91     91   #endif
    92     92   
    93     93   /*
    94     94   ** The maximum number of arguments to an SQL function.
    95     95   */
    96     96   #ifndef SQLITE_MAX_FUNCTION_ARG
    97     97   # define SQLITE_MAX_FUNCTION_ARG 127

Changes to src/test_config.c.

   766    766     LINKVAR( MAX_PAGE_SIZE );
   767    767     LINKVAR( MAX_PAGE_COUNT );
   768    768     LINKVAR( MAX_LIKE_PATTERN_LENGTH );
   769    769     LINKVAR( MAX_TRIGGER_DEPTH );
   770    770     LINKVAR( DEFAULT_CACHE_SIZE );
   771    771     LINKVAR( DEFAULT_PAGE_SIZE );
   772    772     LINKVAR( DEFAULT_FILE_FORMAT );
          773  +  LINKVAR( DEFAULT_SYNCHRONOUS );
          774  +  LINKVAR( DEFAULT_WAL_SYNCHRONOUS );
   773    775     LINKVAR( MAX_ATTACHED );
   774    776     LINKVAR( MAX_DEFAULT_PAGE_SIZE );
   775    777     LINKVAR( MAX_WORKER_THREADS );
   776    778   
   777    779     {
   778    780       static const int cv_TEMP_STORE = SQLITE_TEMP_STORE;
   779    781       Tcl_LinkVar(interp, "TEMP_STORE", (char *)&(cv_TEMP_STORE),

Changes to src/test_journal.c.

   553    553         ** it needs to fill in the non-locking-region part of the original
   554    554         ** pending-byte page.
   555    555         */
   556    556       }else{
   557    557         u32 pgno = (u32)(iOfst/p->nPagesize + 1);
   558    558         assert( (iAmt==1||iAmt==(int)p->nPagesize) &&
   559    559                 ((iOfst+iAmt)%p->nPagesize)==0 );
          560  +      /* The following assert() statements may fail if this layer is used
          561  +      ** with a connection in "PRAGMA synchronous=off" mode. If they
          562  +      ** fail with sync=normal or sync=full, this may indicate problem.  */
   560    563         assert( pgno<=p->nPage || p->nSync>0 );
   561    564         assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) );
   562    565       }
   563    566     }
   564    567   
   565    568     rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
   566    569     if( (p->flags&SQLITE_OPEN_MAIN_JOURNAL) && iAmt==12 ){

Changes to src/vdbe.c.

   399    399         assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
   400    400       }else if( f & MEM_Ephem ){
   401    401         c = 'e';
   402    402         assert( (f & (MEM_Static|MEM_Dyn))==0 );
   403    403       }else{
   404    404         c = 's';
   405    405       }
   406         -
   407         -    sqlite3_snprintf(100, zCsr, "%c", c);
   408         -    zCsr += sqlite3Strlen30(zCsr);
          406  +    *(zCsr++) = c;
   409    407       sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
   410    408       zCsr += sqlite3Strlen30(zCsr);
   411    409       for(i=0; i<16 && i<pMem->n; i++){
   412    410         sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
   413    411         zCsr += sqlite3Strlen30(zCsr);
   414    412       }
   415    413       for(i=0; i<16 && i<pMem->n; i++){
   416    414         char z = pMem->z[i];
   417    415         if( z<32 || z>126 ) *zCsr++ = '.';
   418    416         else *zCsr++ = z;
   419    417       }
   420         -
   421         -    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
   422         -    zCsr += sqlite3Strlen30(zCsr);
          418  +    *(zCsr++) = ']';
   423    419       if( f & MEM_Zero ){
   424    420         sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
   425    421         zCsr += sqlite3Strlen30(zCsr);
   426    422       }
   427    423       *zCsr = '\0';
   428    424     }else if( f & MEM_Str ){
   429    425       int j, k;
................................................................................
  2333   2329       pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
  2334   2330     }
  2335   2331     break;
  2336   2332   }
  2337   2333   
  2338   2334   /* Opcode: Once P1 P2 * * *
  2339   2335   **
  2340         -** If the P1 value is equal to the P1 value on the OP_Init opcode at
  2341         -** instruction 0, then jump to P2.  If the two P1 values differ, then
  2342         -** set the P1 value on this opcode to equal the P1 value on the OP_Init
  2343         -** and fall through.
         2336  +** Fall through to the next instruction the first time this opcode is
         2337  +** encountered on each invocation of the byte-code program.  Jump to P2
         2338  +** on the second and all subsequent encounters during the same invocation.
         2339  +**
         2340  +** Top-level programs determine first invocation by comparing the P1
         2341  +** operand against the P1 operand on the OP_Init opcode at the beginning
         2342  +** of the program.  If the P1 values differ, then fall through and make
         2343  +** the P1 of this opcode equal to the P1 of OP_Init.  If P1 values are
         2344  +** the same then take the jump.
         2345  +**
         2346  +** For subprograms, there is a bitmask in the VdbeFrame that determines
         2347  +** whether or not the jump should be taken.  The bitmask is necessary
         2348  +** because the self-altering code trick does not work for recursive
         2349  +** triggers.
  2344   2350   */
  2345   2351   case OP_Once: {             /* jump */
         2352  +  u32 iAddr;                /* Address of this instruction */
  2346   2353     assert( p->aOp[0].opcode==OP_Init );
  2347         -  VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2);
  2348         -  if( p->aOp[0].p1==pOp->p1 ){
  2349         -    goto jump_to_p2;
         2354  +  if( p->pFrame ){
         2355  +    iAddr = (int)(pOp - p->aOp);
         2356  +    if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
         2357  +      VdbeBranchTaken(1, 2);
         2358  +      goto jump_to_p2;
         2359  +    }
         2360  +    p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
  2350   2361     }else{
  2351         -    pOp->p1 = p->aOp[0].p1;
         2362  +    if( p->aOp[0].p1==pOp->p1 ){
         2363  +      VdbeBranchTaken(1, 2);
         2364  +      goto jump_to_p2;
         2365  +    }
  2352   2366     }
         2367  +  VdbeBranchTaken(0, 2);
         2368  +  pOp->p1 = p->aOp[0].p1;
  2353   2369     break;
  2354   2370   }
  2355   2371   
  2356   2372   /* Opcode: If P1 P2 P3 * *
  2357   2373   **
  2358   2374   ** Jump to P2 if the value in register P1 is true.  The value
  2359   2375   ** is considered true if it is numeric and non-zero.  If the value
................................................................................
  2658   2674        || (len = sqlite3VdbeSerialTypeLen(t))==0
  2659   2675       ){
  2660   2676         /* Content is irrelevant for
  2661   2677         **    1. the typeof() function,
  2662   2678         **    2. the length(X) function if X is a blob, and
  2663   2679         **    3. if the content length is zero.
  2664   2680         ** So we might as well use bogus content rather than reading
  2665         -      ** content from disk. */
  2666         -      static u8 aZero[8];  /* This is the bogus content */
         2681  +      ** content from disk. 
         2682  +      **
         2683  +      ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
         2684  +      ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
         2685  +      ** read up to 16. So 16 bytes of bogus content is supplied.
         2686  +      */
         2687  +      static u8 aZero[16];  /* This is the bogus content */
  2667   2688         sqlite3VdbeSerialGet(aZero, t, pDest);
  2668   2689       }else{
  2669   2690         rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
  2670   2691         if( rc!=SQLITE_OK ) goto abort_due_to_error;
  2671   2692         sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
  2672   2693         pDest->flags &= ~MEM_Ephem;
  2673   2694       }
................................................................................
  5866   5887       ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
  5867   5888       */
  5868   5889       nMem = pProgram->nMem + pProgram->nCsr;
  5869   5890       assert( nMem>0 );
  5870   5891       if( pProgram->nCsr==0 ) nMem++;
  5871   5892       nByte = ROUND8(sizeof(VdbeFrame))
  5872   5893                 + nMem * sizeof(Mem)
  5873         -              + pProgram->nCsr * sizeof(VdbeCursor *);
         5894  +              + pProgram->nCsr * sizeof(VdbeCursor*)
         5895  +              + (pProgram->nOp + 7)/8;
  5874   5896       pFrame = sqlite3DbMallocZero(db, nByte);
  5875   5897       if( !pFrame ){
  5876   5898         goto no_mem;
  5877   5899       }
  5878   5900       sqlite3VdbeMemRelease(pRt);
  5879   5901       pRt->flags = MEM_Frame;
  5880   5902       pRt->u.pFrame = pFrame;
................................................................................
  5917   5939     p->pAuxData = 0;
  5918   5940     p->nChange = 0;
  5919   5941     p->pFrame = pFrame;
  5920   5942     p->aMem = aMem = VdbeFrameMem(pFrame);
  5921   5943     p->nMem = pFrame->nChildMem;
  5922   5944     p->nCursor = (u16)pFrame->nChildCsr;
  5923   5945     p->apCsr = (VdbeCursor **)&aMem[p->nMem];
         5946  +  pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
         5947  +  memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
  5924   5948     p->aOp = aOp = pProgram->aOp;
  5925   5949     p->nOp = pProgram->nOp;
  5926   5950   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  5927   5951     p->anExec = 0;
  5928   5952   #endif
  5929   5953     pOp = &aOp[-1];
  5930   5954   
................................................................................
  6946   6970       if( db->mTrace & SQLITE_TRACE_LEGACY ){
  6947   6971         void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
  6948   6972         char *z = sqlite3VdbeExpandSql(p, zTrace);
  6949   6973         x(db->pTraceArg, z);
  6950   6974         sqlite3_free(z);
  6951   6975       }else
  6952   6976   #endif
  6953         -    {
         6977  +    if( db->nVdbeExec>1 ){
         6978  +      char *z = sqlite3MPrintf(db, "-- %s", zTrace);
         6979  +      (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
         6980  +      sqlite3DbFree(db, z);
         6981  +    }else{
  6954   6982         (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
  6955   6983       }
  6956   6984     }
  6957   6985   #ifdef SQLITE_USE_FCNTL_TRACE
  6958   6986     zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
  6959   6987     if( zTrace ){
  6960   6988       int j;

Changes to src/vdbe.h.

    83     83   ** A sub-routine used to implement a trigger program.
    84     84   */
    85     85   struct SubProgram {
    86     86     VdbeOp *aOp;                  /* Array of opcodes for sub-program */
    87     87     int nOp;                      /* Elements in aOp[] */
    88     88     int nMem;                     /* Number of memory cells required */
    89     89     int nCsr;                     /* Number of cursors required */
           90  +  u8 *aOnce;                    /* Array of OP_Once flags */
    90     91     void *token;                  /* id that may be used to recursive triggers */
    91     92     SubProgram *pNext;            /* Next sub-program already visited */
    92     93   };
    93     94   
    94     95   /*
    95     96   ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
    96     97   ** it takes up less space.

Changes to src/vdbeInt.h.

   160    160   struct VdbeFrame {
   161    161     Vdbe *v;                /* VM this frame belongs to */
   162    162     VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
   163    163     Op *aOp;                /* Program instructions for parent frame */
   164    164     i64 *anExec;            /* Event counters from parent frame */
   165    165     Mem *aMem;              /* Array of memory cells for parent frame */
   166    166     VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
          167  +  u8 *aOnce;              /* Bitmask used by OP_Once */
   167    168     void *token;            /* Copy of SubProgram.token */
   168    169     i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
   169    170     AuxData *pAuxData;      /* Linked list of auxdata allocations */
   170    171     int nCursor;            /* Number of entries in apCsr */
   171    172     int pc;                 /* Program Counter in parent (calling) frame */
   172    173     int nOp;                /* Size of aOp array */
   173    174     int nMem;               /* Number of entries in aMem */

Changes to src/vdbeaux.c.

   112    112     ** size of the op array or add 1KB of space, whichever is smaller. */
   113    113   #ifdef SQLITE_TEST_REALLOC_STRESS
   114    114     int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
   115    115   #else
   116    116     int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
   117    117     UNUSED_PARAMETER(nOp);
   118    118   #endif
          119  +
          120  +  /* Ensure that the size of a VDBE does not grow too large */
          121  +  if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
          122  +    sqlite3OomFault(p->db);
          123  +    return SQLITE_NOMEM;
          124  +  }
   119    125   
   120    126     assert( nOp<=(1024/sizeof(Op)) );
   121    127     assert( nNew>=(p->nOpAlloc+nOp) );
   122    128     pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
   123    129     if( pNew ){
   124    130       p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
   125    131       p->nOpAlloc = p->szOpAlloc/sizeof(Op);

Changes to test/autoanalyze1.test.

    25     25   set testdir [file dirname $argv0]
    26     26   source $testdir/tester.tcl
    27     27   
    28     28   # There is nothing to test if ANALYZE is disable for this build.
    29     29   # These tests also use "PRAGMA stats" which are only enabled for
    30     30   # debugging builds.
    31     31   #
    32         -ifcapable {!debug || !analyze} {
           32  +ifcapable {!debug || !analyze || !vtab} {
    33     33     finish_test
    34     34     return
    35     35   }
    36     36   
    37     37   do_execsql_test autoanalyze1-100 {
    38     38     -- Build up a test table with some indexes
    39     39     CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);

Changes to test/check.test.

   477    477   do_catchsql_test 9.2 {
   478    478     UPDATE t1 SET b=0 WHERE a=1;
   479    479   } {1 {CHECK constraint failed: b-check}}
   480    480   do_catchsql_test 9.3 {
   481    481     UPDATE t1 SET c=a*2 WHERE a=1;
   482    482   } {1 {CHECK constraint failed: c-check}}
   483    483   
   484         -
          484  +# Integrity check on a VIEW with columns.
          485  +#
          486  +db close
          487  +db2 close
          488  +forcedelete test.db
          489  +sqlite3 db test.db
          490  +do_execsql_test 10.1 {
          491  +  CREATE TABLE t1(x);
          492  +  CREATE VIEW v1(y) AS SELECT x FROM t1;
          493  +  PRAGMA integrity_check;
          494  +} {ok}
   485    495   
   486    496   finish_test

Changes to test/collateB.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   # Test cases for a crash bug.
    12     12   #
    13     13   
    14     14   set testdir [file dirname $argv0]
    15     15   source $testdir/tester.tcl
           16  +set testprefix collateB
    16     17   
    17     18   do_execsql_test collateB-1.1 {
    18     19     CREATE TABLE t1(a INTEGER PRIMARY KEY);
    19     20     CREATE TABLE t2(b INTEGER PRIMARY KEY, x1 INT COLLATE NOCASE);
    20     21     CREATE TABLE t3(x2 INT);
    21     22     SELECT * FROM t3, t2, t1 WHERE x2=b AND x1=a AND a=1;
    22     23   } {}
................................................................................
    55     56   } {11 1 1 11 |}
    56     57   do_execsql_test collateB-1.16 {
    57     58     SELECT *,'|' FROM t1, t2, t3 WHERE b=x2 AND a=x1 AND 1=a;
    58     59   } {1 11 1 11 |}
    59     60   do_execsql_test collateB-1.17 {
    60     61     SELECT *,'|' FROM t1, t2, t3 WHERE b=x2 AND a=x1 AND 1=a;
    61     62   } {1 11 1 11 |}
           63  +
           64  +#-------------------------------------------------------------------------
           65  +# Test an assert() failure that was occuring if an index were created
           66  +# on a column explicitly declared "COLLATE binary".
           67  +reset_db
           68  +do_execsql_test 2.1 {
           69  +  CREATE TABLE t4(a COLLATE binary);
           70  +  CREATE INDEX i4 ON t4(a);
           71  +  INSERT INTO t4 VALUES('one'), ('two'), ('three');
           72  +  VACUUM;
           73  +} 
           74  +
           75  +integrity_check 2.2
    62     76   
    63     77   finish_test

Changes to test/fuzzcheck.c.

   801    801   "  -q|--quiet           Reduced output\n"
   802    802   "  --limit-mem N        Limit memory used by test SQLite instance to N bytes\n"
   803    803   "  --limit-vdbe         Panic if any test runs for more than 100,000 cycles\n"
   804    804   "  --load-sql ARGS...   Load SQL scripts fro files into SOURCE-DB\n"
   805    805   "  --load-db ARGS...    Load template databases from files into SOURCE_DB\n"
   806    806   "  -m TEXT              Add a description to the database\n"
   807    807   "  --native-vfs         Use the native VFS for initially empty database files\n"
          808  +"  --native-malloc      Turn off MEMSYS3/5 and Lookaside\n"
   808    809   "  --oss-fuzz           Enable OSS-FUZZ testing\n"
   809    810   "  --prng-seed N        Seed value for the PRGN inside of SQLite\n"
   810    811   "  --rebuild            Rebuild and vacuum the database file\n"
   811    812   "  --result-trace       Show the results of each SQL command\n"
   812    813   "  --sqlid N            Use only SQL where sqlid=N\n"
   813    814   "  --timeout N          Abort if any single test needs more than N seconds\n"
   814    815   "  -v|--verbose         Increased output.  Repeat for more output.\n"
................................................................................
   847    848     int nMem = 0;                /* Memory limit */
   848    849     int nMemThisDb = 0;          /* Memory limit set by the CONFIG table */
   849    850     char *zExpDb = 0;            /* Write Databases to files in this directory */
   850    851     char *zExpSql = 0;           /* Write SQL to files in this directory */
   851    852     void *pHeap = 0;             /* Heap for use by SQLite */
   852    853     int ossFuzz = 0;             /* enable OSS-FUZZ testing */
   853    854     int ossFuzzThisDb = 0;       /* ossFuzz value for this particular database */
          855  +  int nativeMalloc = 0;        /* Turn off MEMSYS3/5 and lookaside if true */
   854    856     sqlite3_vfs *pDfltVfs;       /* The default VFS */
   855    857   
   856    858     iBegin = timeOfDay();
   857    859   #ifdef __unix__
   858    860     signal(SIGALRM, timeoutHandler);
   859    861   #endif
   860    862     g.zArgv0 = argv[0];
................................................................................
   906    908           zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))";
   907    909           iFirstInsArg = i+1;
   908    910           break;
   909    911         }else
   910    912         if( strcmp(z,"m")==0 ){
   911    913           if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
   912    914           zMsg = argv[++i];
          915  +      }else
          916  +      if( strcmp(z,"native-malloc")==0 ){
          917  +        nativeMalloc = 1;
   913    918         }else
   914    919         if( strcmp(z,"native-vfs")==0 ){
   915    920           nativeFlag = 1;
   916    921         }else
   917    922         if( strcmp(z,"oss-fuzz")==0 ){
   918    923           ossFuzz = 1;
   919    924         }else
................................................................................
   968    973         fatalError("cannot import into more than one database");
   969    974       }
   970    975     }
   971    976   
   972    977     /* Process each source database separately */
   973    978     for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){
   974    979       rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db,
   975         -                         SQLITE_OPEN_READONLY, pDfltVfs->zName);
          980  +                         SQLITE_OPEN_READWRITE, pDfltVfs->zName);
   976    981       if( rc ){
   977    982         fatalError("cannot open source database %s - %s",
   978    983         azSrcDb[iSrcDb], sqlite3_errmsg(db));
   979    984       }
   980    985       rc = sqlite3_exec(db,
   981    986          "CREATE TABLE IF NOT EXISTS db(\n"
   982    987          "  dbid INTEGER PRIMARY KEY, -- database id\n"
................................................................................
  1009   1014         while( SQLITE_ROW==sqlite3_step(pStmt) ){
  1010   1015           const char *zName = (const char *)sqlite3_column_text(pStmt,0);
  1011   1016           if( zName==0 ) continue;
  1012   1017           if( strcmp(zName, "oss-fuzz")==0 ){
  1013   1018             ossFuzzThisDb = sqlite3_column_int(pStmt,1);
  1014   1019             if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb);
  1015   1020           }
  1016         -        if( strcmp(zName, "limit-mem")==0 ){
         1021  +        if( strcmp(zName, "limit-mem")==0 && !nativeMalloc ){
  1017   1022   #if !defined(SQLITE_ENABLE_MEMSYS3) && !defined(SQLITE_ENABLE_MEMSYS5)
  1018   1023             fatalError("the limit-mem option requires -DSQLITE_ENABLE_MEMSYS5"
  1019   1024                        " or _MEMSYS3");
  1020   1025   #else
  1021   1026             nMemThisDb = sqlite3_column_int(pStmt,1);
  1022   1027             if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb);
  1023   1028   #endif
................................................................................
  1043   1048         sqlite3_finalize(pStmt);
  1044   1049         rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
  1045   1050         if( rc ) fatalError("cannot commit the transaction: %s", sqlite3_errmsg(db));
  1046   1051         rebuild_database(db);
  1047   1052         sqlite3_close(db);
  1048   1053         return 0;
  1049   1054       }
         1055  +    rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0);
         1056  +    if( rc ) fatalError("cannot set database to query-only");
  1050   1057       if( zExpDb!=0 || zExpSql!=0 ){
  1051   1058         sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
  1052   1059                                 writefileFunc, 0, 0);
  1053   1060         if( zExpDb!=0 ){
  1054   1061           const char *zExDb = 
  1055   1062             "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
  1056   1063             "       dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
................................................................................
  1137   1144       */
  1138   1145       sqlite3_close(db);
  1139   1146       if( sqlite3_memory_used()>0 ){
  1140   1147         fatalError("SQLite has memory in use before the start of testing");
  1141   1148       }
  1142   1149   
  1143   1150       /* Limit available memory, if requested */
  1144         -    if( nMemThisDb>0 ){
  1145         -      sqlite3_shutdown();
         1151  +    sqlite3_shutdown();
         1152  +    if( nMemThisDb>0 && !nativeMalloc ){
  1146   1153         pHeap = realloc(pHeap, nMemThisDb);
  1147   1154         if( pHeap==0 ){
  1148   1155           fatalError("failed to allocate %d bytes of heap memory", nMem);
  1149   1156         }
  1150   1157         sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128);
  1151   1158       }
         1159  +
         1160  +    /* Disable lookaside with the --native-malloc option */
         1161  +    if( nativeMalloc ){
         1162  +      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
         1163  +    }
  1152   1164     
  1153   1165       /* Reset the in-memory virtual filesystem */
  1154   1166       formatVfs();
  1155   1167       
  1156   1168       /* Run a test using each SQL script against each database.
  1157   1169       */
  1158   1170       if( !verboseFlag && !quietFlag ) printf("%s:", zDbName);
................................................................................
  1201   1213               sqlite3_progress_handler(db, 100000, progressHandler, &vdbeLimitFlag);
  1202   1214             }
  1203   1215   #endif
  1204   1216             do{
  1205   1217               runSql(db, (char*)pSql->a, runFlags);
  1206   1218             }while( timeoutTest );
  1207   1219             setAlarm(0);
         1220  +          sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0);
  1208   1221             sqlite3_close(db);
  1209   1222           }
  1210         -        if( sqlite3_memory_used()>0 ) fatalError("memory leak");
         1223  +        if( sqlite3_memory_used()>0 ){
         1224  +           fatalError("memory leak: %lld bytes outstanding",
         1225  +                      sqlite3_memory_used());
         1226  +        }
  1211   1227           reformatVfs();
  1212   1228           nTest++;
  1213   1229           g.zTestName[0] = 0;
  1214   1230   
  1215   1231           /* Simulate an error if the TEST_FAILURE environment variable is "5".
  1216   1232           ** This is used to verify that automated test script really do spot
  1217   1233           ** errors that occur in this test program.

Added test/json104.test.

            1  +# 2017-03-22
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements tests for json_patch(A,B) SQL function.
           12  +#
           13  +
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +
           17  +ifcapable !json1 {
           18  +  finish_test
           19  +  return
           20  +}
           21  +
           22  +# This is the example from pages 2 and 3 of RFC-7396
           23  +do_execsql_test json104-100 {
           24  +  SELECT json_patch('{
           25  +       "a": "b",
           26  +       "c": {
           27  +         "d": "e",
           28  +         "f": "g"
           29  +       }
           30  +     }','{
           31  +       "a":"z",
           32  +       "c": {
           33  +         "f": null
           34  +       }
           35  +     }');
           36  +} {{{"a":"z","c":{"d":"e"}}}}
           37  +
           38  +
           39  +# This is the example from pages 4 and 5 of RFC-7396 
           40  +do_execsql_test json104-110 {
           41  +  SELECT json_patch('{
           42  +       "title": "Goodbye!",
           43  +       "author" : {
           44  +         "givenName" : "John",
           45  +         "familyName" : "Doe"
           46  +       },
           47  +       "tags":[ "example", "sample" ],
           48  +       "content": "This will be unchanged"
           49  +     }','{
           50  +       "title": "Hello!",
           51  +       "phoneNumber": "+01-123-456-7890",
           52  +       "author": {
           53  +         "familyName": null
           54  +       },
           55  +       "tags": [ "example" ]
           56  +     }');
           57  +} {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
           58  +
           59  +do_execsql_test json104-200 {
           60  +  SELECT json_patch('[1,2,3]','{"x":null}');
           61  +} {{{}}}
           62  +do_execsql_test json104-210 {
           63  +  SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
           64  +} {{{"y":1}}}
           65  +do_execsql_test json104-220 {
           66  +  SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
           67  +} {{{"a":{"bb":{}}}}}
           68  +do_execsql_test json104-221 {
           69  +  SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}');
           70  +} {{{"a":{"bb":{"ccc":[1,null,3]}}}}}
           71  +do_execsql_test json104-222 {
           72  +  SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}');
           73  +} {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}}
           74  +
           75  +# Example test cases at the end of the RFC-7396 document
           76  +do_execsql_test json104-300 {
           77  +  SELECT json_patch('{"a":"b"}','{"a":"c"}');
           78  +} {{{"a":"c"}}}
           79  +do_execsql_test json104-300a {
           80  +  SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
           81  +} {{real-null}}
           82  +do_execsql_test json104-301 {
           83  +  SELECT json_patch('{"a":"b"}','{"b":"c"}');
           84  +} {{{"a":"b","b":"c"}}}
           85  +do_execsql_test json104-302 {
           86  +  SELECT json_patch('{"a":"b"}','{"a":null}');
           87  +} {{{}}}
           88  +do_execsql_test json104-303 {
           89  +  SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
           90  +} {{{"b":"c"}}}
           91  +do_execsql_test json104-304 {
           92  +  SELECT json_patch('{"a":["b"]}','{"a":"c"}');
           93  +} {{{"a":"c"}}}
           94  +do_execsql_test json104-305 {
           95  +  SELECT json_patch('{"a":"c"}','{"a":["b"]}');
           96  +} {{{"a":["b"]}}}
           97  +do_execsql_test json104-306 {
           98  +  SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}');
           99  +} {{{"a":{"b":"d"}}}}
          100  +do_execsql_test json104-307 {
          101  +  SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}');
          102  +} {{{"a":[1]}}}
          103  +do_execsql_test json104-308 {
          104  +  SELECT json_patch('["a","b"]','["c","d"]');
          105  +} {{["c","d"]}}
          106  +do_execsql_test json104-309 {
          107  +  SELECT json_patch('{"a":"b"}','["c"]');
          108  +} {{["c"]}}
          109  +do_execsql_test json104-310 {
          110  +  SELECT json_patch('{"a":"foo"}','null');
          111  +} {{null}}
          112  +do_execsql_test json104-310a {
          113  +  SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
          114  +} {{real-null}}
          115  +do_execsql_test json104-311 {
          116  +  SELECT json_patch('{"a":"foo"}','"bar"');
          117  +} {{"bar"}}
          118  +do_execsql_test json104-312 {
          119  +  SELECT json_patch('{"e":null}','{"a":1}');
          120  +} {{{"e":null,"a":1}}}
          121  +do_execsql_test json104-313 {
          122  +  SELECT json_patch('[1,2]','{"a":"b","c":null}');
          123  +} {{{"a":"b"}}}
          124  +do_execsql_test json104-314 {
          125  +  SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
          126  +} {{{"a":{"bb":{}}}}}
          127  +
          128  +
          129  +
          130  +finish_test

Changes to test/mallocM.test.

    17     17   set testprefix mallocM
    18     18   
    19     19   sqlite3_db_config_lookaside db 0 0 0
    20     20   
    21     21   do_execsql_test 1.0 {
    22     22     CREATE TABLE t1(x);
    23     23   }
    24         -do_faultsim_test 1 -faults oom-t* -body {
           24  +do_faultsim_test 1 -faults oom* -body {
    25     25     execsql {
    26     26       SELECT 'abc' FROM ( SELECT 'xyz' FROM t1 WHERE (SELECT 1) )
    27     27     }
    28     28   } -test {
    29     29     faultsim_test_result {0 {}}
    30     30   }
           31  +
           32  +do_execsql_test 2.0.1 { SELECT instr(x'', x'') }         {1}
           33  +do_execsql_test 2.0.2 { SELECT instr(x'12345678', x'') } {1}
           34  +do_execsql_test 2.0.3 { SELECT instr(x'', x'1234') }     {0}
           35  +
           36  +do_faultsim_test 2.1 -faults oom* -body {
           37  +  execsql { SELECT instr (x'00', zeroblob(1)) }
           38  +} -test {
           39  +  faultsim_test_result {0 1}
           40  +}
           41  +
           42  +do_faultsim_test 2.2 -faults oom* -body {
           43  +  execsql { SELECT instr (zeroblob(1), x'00') }
           44  +} -test {
           45  +  faultsim_test_result {0 1}
           46  +}
    31     47   
    32     48   finish_test

Changes to test/ossfuzz.c.

     1      1   /*
     2      2   ** This module interfaces SQLite to the Google OSS-Fuzz, fuzzer as a service.
     3      3   ** (https://github.com/google/oss-fuzz)
     4      4   */
     5      5   #include <stddef.h>
     6      6   #include <stdint.h>
            7  +#include <stdio.h>
            8  +#include <string.h>
     7      9   #include "sqlite3.h"
           10  +
           11  +/* Global debugging settings.  OSS-Fuzz will have all debugging turned
           12  +** off.  But if LLVMFuzzerTestOneInput() is called interactively from
           13  +** the ossshell utility program, then these flags might be set.
           14  +*/
           15  +static unsigned mDebug = 0;
           16  +#define FUZZ_SQL_TRACE       0x0001   /* Set an sqlite3_trace() callback */
           17  +#define FUZZ_SHOW_MAX_DELAY  0x0002   /* Show maximum progress callback delay */
           18  +#define FUZZ_SHOW_ERRORS     0x0004   /* Print error messages from SQLite */
           19  +
           20  +/* The ossshell utility program invokes this interface to see the
           21  +** debugging flags.  Unused by OSS-Fuzz.
           22  +*/
           23  +void ossfuzz_set_debug_flags(unsigned x){
           24  +  mDebug = x;
           25  +}
     8     26   
     9     27   /* Return the current real-world time in milliseconds since the
    10     28   ** Julian epoch (-4714-11-24).
    11     29   */
    12     30   static sqlite3_int64 timeOfDay(void){
    13     31     static sqlite3_vfs *clockVfs = 0;
    14     32     sqlite3_int64 t;
................................................................................
    18     36     }else{
    19     37       double r;
    20     38       clockVfs->xCurrentTime(clockVfs, &r);
    21     39       t = (sqlite3_int64)(r*86400000.0);
    22     40     }
    23     41     return t;
    24     42   }
           43  +
           44  +/* An instance of the following object is passed by pointer as the
           45  +** client data to various callbacks.
           46  +*/
           47  +typedef struct FuzzCtx {
           48  +  sqlite3 *db;               /* The database connection */
           49  +  sqlite3_int64 iCutoffTime; /* Stop processing at this time. */
           50  +  sqlite3_int64 iLastCb;     /* Time recorded for previous progress callback */
           51  +  sqlite3_int64 mxInterval;  /* Longest interval between two progress calls */
           52  +  unsigned nCb;              /* Number of progress callbacks */
           53  +} FuzzCtx;
    25     54   
    26     55   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
    27     56   /*
    28     57   ** Progress handler callback.
    29     58   **
    30     59   ** The argument is the cutoff-time after which all processing should
    31     60   ** stop.  So return non-zero if the cut-off time is exceeded.
    32     61   */
    33         -static int progress_handler(void *pReturn) {
    34         -  sqlite3_int64 iCutoffTime = *(sqlite3_int64*)pReturn;
    35         -  return timeOfDay()>=iCutoffTime;
           62  +static int progress_handler(void *pClientData) {
           63  +  FuzzCtx *p = (FuzzCtx*)pClientData;
           64  +  sqlite3_int64 iNow = timeOfDay();
           65  +  int rc = iNow>=p->iCutoffTime;
           66  +  sqlite3_int64 iDiff = iNow - p->iLastCb;
           67  +  if( iDiff > p->mxInterval ) p->mxInterval = iDiff;
           68  +  p->nCb++;
           69  +  return rc;
    36     70   }
    37     71   #endif
    38     72   
    39     73   /*
    40     74   ** Callback for sqlite3_exec().
    41     75   */
    42     76   static int exec_handler(void *pCnt, int argc, char **argv, char **namev){
................................................................................
    50     84   /*
    51     85   ** Main entry point.  The fuzzer invokes this function with each
    52     86   ** fuzzed input.
    53     87   */
    54     88   int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    55     89     int execCnt = 0;         /* Abort row callback when count reaches zero */
    56     90     char *zErrMsg = 0;       /* Error message returned by sqlite_exec() */
    57         -  sqlite3 *db;             /* The database connection */
    58     91     uint8_t uSelector;       /* First byte of input data[] */
    59     92     int rc;                  /* Return code from various interfaces */
    60     93     char *zSql;              /* Zero-terminated copy of data[] */
    61         -  sqlite3_int64 iCutoff;   /* Cutoff timer */
           94  +  FuzzCtx cx;              /* Fuzzing context */
    62     95   
           96  +  memset(&cx, 0, sizeof(cx));
    63     97     if( size<3 ) return 0;   /* Early out if unsufficient data */
    64     98   
    65     99     /* Extract the selector byte from the beginning of the input.  But only
    66    100     ** do this if the second byte is a \n.  If the second byte is not \n,
    67    101     ** then use a default selector */
    68    102     if( data[1]=='\n' ){
    69    103       uSelector = data[0];  data += 2; size -= 2;
    70    104     }else{
    71    105       uSelector = 0xfd;
    72    106     }
    73    107   
    74    108     /* Open the database connection.  Only use an in-memory database. */
    75         -  rc = sqlite3_open_v2("fuzz.db", &db,
          109  +  rc = sqlite3_open_v2("fuzz.db", &cx.db,
    76    110              SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 0);
    77    111     if( rc ) return 0;
    78    112   
    79    113   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
    80    114     /* Invoke the progress handler frequently to check to see if we
    81    115     ** are taking too long.  The progress handler will return true
    82    116     ** (which will block further processing) if more than 10 seconds have
    83    117     ** elapsed since the start of the test.
    84    118     */
    85         -  iCutoff = timeOfDay() + 10000;  /* Now + 10 seconds */
    86         -  sqlite3_progress_handler(db, 10, progress_handler, (void*)&iCutoff);
          119  +  cx.iLastCb = timeOfDay();
          120  +  cx.iCutoffTime = cx.iLastCb + 10000;  /* Now + 10 seconds */
          121  +  sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx);
    87    122   #endif
          123  +
          124  +  /* Set a limit on the maximum size of a prepared statement */
          125  +  sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, 25000);
    88    126   
    89    127     /* Bit 1 of the selector enables foreign key constraints */
    90         -  sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, uSelector&1, &rc);
          128  +  sqlite3_db_config(cx.db, SQLITE_DBCONFIG_ENABLE_FKEY, uSelector&1, &rc);
    91    129     uSelector >>= 1;
    92    130   
    93    131     /* Remaining bits of the selector determine a limit on the number of
    94    132     ** output rows */
    95    133     execCnt = uSelector + 1;
    96    134   
    97    135     /* Run the SQL.  The sqlite_exec() interface expects a zero-terminated
    98    136     ** string, so make a copy. */
    99    137     zSql = sqlite3_mprintf("%.*s", (int)size, data);
   100         -  sqlite3_exec(db, zSql, exec_handler, (void*)&execCnt, &zErrMsg);
          138  +  sqlite3_exec(cx.db, zSql, exec_handler, (void*)&execCnt, &zErrMsg);
          139  +
          140  +  /* Show any errors */
          141  +  if( (mDebug & FUZZ_SHOW_ERRORS)!=0 && zErrMsg ){
          142  +    printf("Error: %s\n", zErrMsg);
          143  +  }
   101    144   
   102    145     /* Cleanup and return */
   103    146     sqlite3_free(zErrMsg);
   104    147     sqlite3_free(zSql);
   105         -  sqlite3_close(db);
          148  +  sqlite3_exec(cx.db, "PRAGMA temp_store_directory=''", 0, 0, 0);
          149  +  sqlite3_close(cx.db);
          150  +
          151  +  if( mDebug & FUZZ_SHOW_MAX_DELAY ){
          152  +    printf("Progress callback count....... %d\n", cx.nCb);
          153  +    printf("Max time between callbacks.... %d ms\n", (int)cx.mxInterval);
          154  +  }
   106    155     return 0;
   107    156   }

Changes to test/ossshell.c.

     5      5   ** This program links against ossfuzz.c.  It reads files named on the
     6      6   ** command line and passes them one by one into ossfuzz.c.
     7      7   */
     8      8   #include <stddef.h>
     9      9   #include <stdint.h>
    10     10   #include <stdio.h>
    11     11   #include <stdlib.h>
           12  +#include <string.h>
    12     13   #include "sqlite3.h"
    13     14   
    14     15   /*
    15     16   ** The entry point in ossfuzz.c that this routine will be calling
    16     17   */
    17     18   int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
           19  +
           20  +/* Must match equivalent #defines in ossfuzz.c */
           21  +#define FUZZ_SQL_TRACE       0x0001   /* Set an sqlite3_trace() callback */
           22  +#define FUZZ_SHOW_MAX_DELAY  0x0002   /* Show maximum progress callback delay */
           23  +#define FUZZ_SHOW_ERRORS     0x0004   /* Show SQL errors */
           24  +extern void ossfuzz_set_debug_flags(unsigned);
           25  +
    18     26   
    19     27   
    20     28   /*
    21     29   ** Read files named on the command-line and invoke the fuzzer for
    22     30   ** each one.
    23     31   */
    24     32   int main(int argc, char **argv){
    25     33     FILE *in;
    26     34     int i;
    27     35     int nErr = 0;
    28     36     uint8_t *zBuf = 0;
    29     37     size_t sz;
           38  +  unsigned mDebug = 0;
    30     39   
    31     40     for(i=1; i<argc; i++){
    32     41       const char *zFilename = argv[i];
           42  +    if( zFilename[0]=='-' ){
           43  +      if( zFilename[1]=='-' ) zFilename++;
           44  +      if( strcmp(zFilename, "-show-errors")==0 ){
           45  +        mDebug |= FUZZ_SHOW_ERRORS;
           46  +        ossfuzz_set_debug_flags(mDebug);
           47  +      }else
           48  +      if( strcmp(zFilename, "-show-max-delay")==0 ){
           49  +        mDebug |= FUZZ_SHOW_MAX_DELAY;
           50  +        ossfuzz_set_debug_flags(mDebug);
           51  +      }else
           52  +      if( strcmp(zFilename, "-sql-trace")==0 ){
           53  +        mDebug |= FUZZ_SQL_TRACE;
           54  +        ossfuzz_set_debug_flags(mDebug);
           55  +      }else
           56  +      {
           57  +        printf("unknown option \"%s\"\n", argv[i]);
           58  +        printf("should be one of: --show-errors --show-max-delay"
           59  +               " --sql-trace\n");
           60  +        exit(1);
           61  +      }
           62  +      continue;
           63  +    }
    33     64       in = fopen(zFilename, "rb");
    34     65       if( in==0 ){
    35     66         fprintf(stderr, "cannot open \"%s\"\n", zFilename);
    36     67         nErr++;
    37     68         continue;
    38     69       }
    39     70       fseek(in, 0, SEEK_END);
................................................................................
    46     77       }
    47     78       if( fread(zBuf, sz, 1, in)!=1 ){
    48     79         fprintf(stderr, "cannot read %d bytes from \"%s\"\n",
    49     80                          (int)sz, zFilename);
    50     81         nErr++;
    51     82       }else{
    52     83         printf("%s... ", zFilename);
           84  +      if( mDebug ) printf("\n");
    53     85         fflush(stdout);
    54     86         (void)LLVMFuzzerTestOneInput(zBuf, sz);
           87  +      if( mDebug ) printf("%s: ", zFilename);
    55     88         printf("ok\n");
    56     89       }
    57     90       fclose(in);
    58     91     }
    59     92     free(zBuf);
    60     93     return nErr;
    61     94   }

Changes to test/permutations.test.

   243    243     notify2.test   thread001.test thread002.test thread003.test 
   244    244     thread004.test thread005.test walthread.test
   245    245   }
   246    246   
   247    247   test_suite "fts3" -prefix "" -description {
   248    248     All FTS3 tests except fts3rnd.test.
   249    249   } -files {
   250         -  fts3aa.test fts3ab.test fts3ac.test fts3ad.test fts3ae.test
   251         -  fts3af.test fts3ag.test fts3ah.test fts3ai.test fts3aj.test
   252         -  fts3ak.test fts3al.test fts3am.test fts3an.test fts3ao.test
   253         -  fts3atoken.test fts3b.test fts3c.test fts3cov.test fts3d.test
   254         -  fts3defer.test fts3defer2.test fts3e.test fts3expr.test fts3expr2.test 
   255         -  fts3expr3.test
   256         -  fts3near.test fts3query.test fts3shared.test fts3snippet.test 
   257         -  fts3sort.test
   258         -  fts3fault.test fts3malloc.test fts3matchinfo.test
   259         -  fts3aux1.test fts3comp1.test fts3auto.test
   260         -  fts4aa.test fts4content.test
   261         -  fts3conf.test fts3prefix.test fts3fault2.test fts3corrupt.test
   262         -  fts3corrupt2.test fts3first.test fts4langid.test fts4merge.test
   263         -  fts4check.test fts4unicode.test fts4noti.test
   264         -  fts3varint.test
   265         -  fts4growth.test fts4growth2.test
          250  +  fts3aa.test fts3ab.test fts3ac.test fts3ad.test
          251  +  fts3ae.test fts3af.test fts3ag.test fts3ah.test
          252  +  fts3ai.test fts3aj.test fts3ak.test fts3al.test
          253  +  fts3am.test fts3an.test fts3ao.test fts3atoken.test
          254  +  fts3auto.test fts3aux1.test fts3aux2.test fts3b.test
          255  +  fts3comp1.test fts3conf.test fts3corrupt2.test fts3corrupt.test
          256  +  fts3cov.test fts3c.test fts3defer2.test fts3defer3.test
          257  +  fts3defer.test fts3drop.test fts3d.test fts3e.test
          258  +  fts3expr2.test fts3expr3.test fts3expr4.test fts3expr5.test
          259  +  fts3expr.test fts3fault2.test fts3fault.test fts3first.test
          260  +  fts3join.test fts3malloc.test fts3matchinfo.test fts3near.test
          261  +  fts3offsets.test fts3prefix2.test fts3prefix.test fts3query.test
          262  +  fts3shared.test fts3snippet.test fts3sort.test fts3tok1.test
          263  +  fts3tok_err.test fts3varint.test fts4aa.test fts4check.test
          264  +  fts4content.test fts4docid.test fts4growth2.test fts4growth.test
          265  +  fts4incr.test fts4langid.test fts4lastrowid.test fts4merge2.test
          266  +  fts4merge4.test fts4merge.test fts4noti.test fts4onepass.test 
          267  +  fts4opt.test fts4unicode.test
   266    268   }
   267    269   
   268    270   test_suite "fts5" -prefix "" -description {
   269    271     All FTS5 tests.
   270    272   } -files [glob -nocomplain $::testdir/../ext/fts5/test/*.test]
   271    273   
   272    274   test_suite "fts5-light" -prefix "" -description {

Changes to test/shell1.test.

   751    751   INSERT INTO t3 VALUES(2,'');
   752    752   INSERT INTO t3 VALUES(3,1);
   753    753   INSERT INTO t3 VALUES(4,2.25);
   754    754   INSERT INTO t3 VALUES(5,'hello');
   755    755   INSERT INTO t3 VALUES(6,X'807f');
   756    756   COMMIT;}}
   757    757   
          758  +
          759  +ifcapable vtab {
          760  +
   758    761   # The --preserve-rowids option to .dump
   759    762   #
   760    763   do_test shell1-4.1.1 {
   761    764     catchcmd test.db {.dump --preserve-rowids}
   762    765   } {0 {PRAGMA foreign_keys=OFF;
   763    766   BEGIN TRANSACTION;
   764    767   CREATE TABLE t1(x);
................................................................................
   863    866   } {0 {PRAGMA foreign_keys=OFF;
   864    867   BEGIN TRANSACTION;
   865    868   CREATE TABLE t1(_ROWID_,rowid,oid);
   866    869   INSERT INTO t1 VALUES(1,NULL,'alpha');
   867    870   INSERT INTO t1 VALUES(12,'',99);
   868    871   INSERT INTO t1 VALUES(23,1,X'b0b1b2');
   869    872   COMMIT;}}
          873  +
          874  +} else {
          875  +
          876  +do_test shell1-4.1.6 {
          877  +  db close
          878  +  forcedelete test2.db
          879  +  sqlite3 db test2.db
          880  +  db eval {
          881  +    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
          882  +    INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
          883  +                         (4,2.25), (5,'hello'), (6,x'807f');
          884  +  }
          885  +  catchcmd test2.db {.dump --preserve-rowids}
          886  +} {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}
          887  +
          888  +}
          889  +
   870    890   
   871    891   # Test the output of ".mode insert"
   872    892   #
   873    893   do_test shell1-4.2.1 {
   874    894     catchcmd test.db ".mode insert t1\nselect * from t1;"
   875    895   } {0 {INSERT INTO t1 VALUES(NULL);
   876    896   INSERT INTO t1 VALUES('');

Added test/sync2.test.

            1  +# 2017 March 16
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# Specificly, it tests that "PRAGMA synchronous" appears to work.
           14  +#
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +set testprefix sync2
           19  +
           20  +#
           21  +# These tests are only applicable when pager pragma are
           22  +# enabled. Also, since every test uses an ATTACHed database, they
           23  +# are only run when ATTACH is enabled.
           24  +#
           25  +ifcapable !pager_pragmas||!attach {
           26  +  finish_test
           27  +  return
           28  +}
           29  +if {$::tcl_platform(platform)!="unix" 
           30  +  || [permutation] == "journaltest"
           31  +  || [permutation] == "inmemory_journal"
           32  +} {
           33  +  finish_test
           34  +  return
           35  +}
           36  +
           37  +proc execsql_sync {sql} {
           38  +  set s $::sqlite_sync_count
           39  +  set res [execsql $sql]
           40  +  concat [expr $::sqlite_sync_count-$s] $res
           41  +}
           42  +
           43  +proc do_execsql_sync_test {tn sql res} {
           44  +  uplevel [list do_test $tn [list execsql_sync $sql] [list {*}$res]]
           45  +}
           46  +
           47  +#-----------------------------------------------------------------------
           48  +# Tests for journal mode.
           49  +#
           50  +sqlite3 db test.db
           51  +do_execsql_test 1.0 {
           52  +  CREATE TABLE t1(a, b);
           53  +  INSERT INTO t1 VALUES(1, 2);
           54  +}
           55  +
           56  +do_execsql_sync_test 1.1 { INSERT INTO t1 VALUES(3, 4) } 4
           57  +
           58  +# synchronous=normal. So, 1 sync on the directory, 1 on the journal, 1 
           59  +# on the db file. 3 in total.
           60  +do_execsql_test      1.2.1 { PRAGMA main.synchronous = NORMAL }
           61  +do_execsql_test      1.2.2 { PRAGMA main.synchronous }     1
           62  +do_execsql_sync_test 1.2.3 { INSERT INTO t1 VALUES(5, 6) } 3
           63  +
           64  +# synchronous=off. No syncs.
           65  +do_execsql_test      1.3.1 { PRAGMA main.synchronous = OFF }
           66  +do_execsql_test      1.3.2 { PRAGMA main.synchronous }     0
           67  +do_execsql_sync_test 1.3.3 { INSERT INTO t1 VALUES(7, 8) } 0
           68  + 
           69  +# synchronous=full, journal_mode=delete. So, 1 sync on the directory,
           70  +# 2 on the journal, 1 on the db file. 4 in total.
           71  +do_execsql_test      1.4.1 { PRAGMA main.synchronous = FULL }
           72  +do_execsql_test      1.4.2 { PRAGMA main.synchronous }      2
           73  +do_execsql_sync_test 1.4.3 { INSERT INTO t1 VALUES(9, 10) } 4
           74  +
           75  +#-----------------------------------------------------------------------
           76  +# Tests for wal mode.
           77  +#
           78  +do_execsql_test      1.5 { PRAGMA journal_mode = wal } {wal}
           79  +
           80  +# sync=full, journal_mode=wal. One sync on the directory, two on the 
           81  +# wal file.
           82  +do_execsql_sync_test 1.6 { INSERT INTO t1 VALUES(11, 12) } 3
           83  +
           84  +# One sync on the wal file.
           85  +do_execsql_sync_test 1.7 { INSERT INTO t1 VALUES(13, 14) } 1
           86  +
           87  +# No syncs.
           88  +do_execsql_test      1.8.1 { PRAGMA main.synchronous = NORMAL }
           89  +do_execsql_test      1.8.2 { PRAGMA main.synchronous }          1
           90  +do_execsql_sync_test 1.8.3 { INSERT INTO t1 VALUES(15, 16) }    0
           91  +
           92  +# One sync on wal file, one on the db file.
           93  +do_execsql_sync_test 1.9   { PRAGMA wal_checkpoint }  {2 0 3 3}
           94  +
           95  +# No syncs.
           96  +do_execsql_test      1.10.1 { PRAGMA main.synchronous = OFF }
           97  +do_execsql_test      1.10.2 { PRAGMA main.synchronous }          0
           98  +do_execsql_sync_test 1.10.3 { INSERT INTO t1 VALUES(17, 18) }    0
           99  +
          100  +#-----------------------------------------------------------------------
          101  +# Tests for the compile time settings SQLITE_DEFAULT_SYNCHRONOUS and
          102  +# SQLITE_DEFAULT_WAL_SYNCHRONOUS. These tests only run if the former
          103  +# is set to "2" and the latter to "1". This is not the default, but
          104  +# it is currently the recommended configuration.
          105  +#
          106  +#   https://sqlite.org/compile.html#recommended_compile_time_options
          107  +#
          108  +if {$SQLITE_DEFAULT_SYNCHRONOUS==2 && $SQLITE_DEFAULT_WAL_SYNCHRONOUS==1} {
          109  +
          110  +  db close
          111  +  sqlite3 db test.db
          112  +
          113  +  # Wal mode, sync=normal. The first transaction does one sync on directory,
          114  +  # one on the wal file. The second does no syncs.
          115  +  do_execsql_sync_test 1.11.1 { INSERT INTO t1 VALUES(19, 20) } 2
          116  +  do_execsql_sync_test 1.11.2 { INSERT INTO t1 VALUES(21, 22) } 0
          117  +  do_execsql_test 1.11.3      { PRAGMA main.synchronous }       1
          118  +
          119  +  # One sync on wal file, one on the db file.
          120  +  do_execsql_sync_test 1.12   { PRAGMA wal_checkpoint }  {2 0 2 2}
          121  +
          122  +  # First transaction syncs the wal file once, the second not at all.
          123  +  # one on the wal file. The second does no syncs.
          124  +  do_execsql_sync_test 1.13.1 { INSERT INTO t1 VALUES(22, 23) } 1
          125  +  do_execsql_sync_test 1.13.2 { INSERT INTO t1 VALUES(24, 25) } 0
          126  +
          127  +  do_execsql_test 1.14 { PRAGMA journal_mode = delete } {delete}
          128  +  
          129  +  # Delete mode, sync=full. The first transaction does one sync on 
          130  +  # directory, two on the journal file, one on the db. The second does 
          131  +  # the same.
          132  +  do_execsql_sync_test 1.15.1 { INSERT INTO t1 VALUES(26, 27) } 4
          133  +  do_execsql_sync_test 1.15.2 { INSERT INTO t1 VALUES(28, 29) } 4
          134  +  do_execsql_test 1.15.3      { PRAGMA main.synchronous }       2
          135  +
          136  +  # Switch back to wal mode.
          137  +  do_execsql_test 1.16 { PRAGMA journal_mode = wal } {wal}
          138  +
          139  +  do_execsql_sync_test 1.17.1 { INSERT INTO t1 VALUES(30, 31) } 2
          140  +  do_execsql_sync_test 1.17.2 { INSERT INTO t1 VALUES(32, 33) } 0
          141  +  do_execsql_test 1.17.3      { PRAGMA main.synchronous }       1
          142  +
          143  +  # Now set synchronous=off, then switch back to delete mode. Check
          144  +  # that the db handle is still using synchronous=off.
          145  +  do_execsql_test 1.18.3      { PRAGMA main.synchronous=off }
          146  +  do_execsql_test 1.18 { PRAGMA journal_mode = delete } {delete}
          147  +
          148  +  do_execsql_sync_test 1.19.1 { INSERT INTO t1 VALUES(34, 35) } 0
          149  +  do_execsql_sync_test 1.19.2 { INSERT INTO t1 VALUES(36, 37) } 0
          150  +  do_execsql_test 1.19.3      { PRAGMA main.synchronous }       0
          151  +
          152  +  # Close and reopen the db. Back to synchronous=normal.
          153  +  db close
          154  +  sqlite3 db test.db
          155  +  do_execsql_sync_test 1.20.1 { INSERT INTO t1 VALUES(38, 39) } 4
          156  +  do_execsql_sync_test 1.20.2 { INSERT INTO t1 VALUES(40, 41) } 4
          157  +  do_execsql_test 1.20.3      { PRAGMA main.synchronous }       2
          158  +}
          159  +
          160  +finish_test

Added test/triggerG.test.

            1  +# 2017-03-24
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice', here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +set testprefix triggerG
           16  +ifcapable {!trigger} {
           17  +  finish_test
           18  +  return
           19  +}
           20  +
           21  +# Test cases for ticket 
           22  +# https://www.sqlite.org/src/tktview/06796225f59c057cd120f
           23  +#
           24  +# The OP_Once opcode was not working correctly for recursive triggers.
           25  +#
           26  +do_execsql_test 100 {
           27  +  PRAGMA recursive_triggers = 1;
           28  +  
           29  +  CREATE TABLE t1(a);
           30  +  CREATE INDEX i1 ON t1(a);
           31  +  INSERT INTO t1(a) VALUES(0),(2),(3),(8),(9);
           32  +  CREATE TABLE t2(b);
           33  +  CREATE TABLE t3(c);
           34  +  
           35  +  CREATE TRIGGER tr AFTER INSERT ON t3 BEGIN
           36  +    INSERT INTO t3 SELECT new.c+1 WHERE new.c<5;
           37  +    INSERT INTO t2 SELECT new.c*100+a FROM t1 WHERE a IN (1, 2, 3, 4);
           38  +  END;
           39  +  
           40  +  INSERT INTO t3 VALUES(2);
           41  +  SELECT c FROM t3 ORDER BY c;;
           42  +} {2 3 4 5}
           43  +do_execsql_test 110 {
           44  +  SELECT b FROM t2 ORDER BY b;
           45  +} {202 203 302 303 402 403 502 503}
           46  +
           47  +do_execsql_test 200 {
           48  +  DELETE FROM t1;
           49  +  INSERT INTO t1(a) VALUES(0),(2),(3),(8),(9);
           50  +  DELETE FROM t2;
           51  +  DELETE FROM t3;
           52  +  DROP TRIGGER tr;
           53  +  CREATE TRIGGER tr AFTER INSERT ON t3 BEGIN
           54  +    INSERT INTO t3 SELECT new.c+1 WHERE new.c<5;
           55  +    INSERT INTO t2 SELECT new.c*10000+xx.a*100+yy.a
           56  +                     FROM t1 AS xx, t1 AS yy
           57  +                    WHERE xx.a IN (1,2,3,4)
           58  +                      AND yy.a IN (2,3,4,5);
           59  +  END;
           60  +
           61  +  INSERT INTO t3 VALUES(2);
           62  +  SELECT b FROM t2 ORDER BY b;
           63  +} {20202 20203 20302 20303 30202 30203 30302 30303 40202 40203 40302 40303 50202 50203 50302 50303}
           64  +
           65  +finish_test

Changes to tool/showdb.c.

   531    531     if( nLocal>0 ){
   532    532       i = decodeVarint(x, &nHdr);
   533    533       printBytes(a, x, i);
   534    534       printf("record-header-size: %d\n", (int)nHdr);
   535    535       j = i;
   536    536       nCol = 0;
   537    537       k = nHdr;
   538         -    while( x+j<end && j<nHdr ){
          538  +    while( x+j<=end && j<nHdr ){
   539    539          const char *zTypeName;
   540    540          int sz = 0;
   541    541          char zNm[30];
   542    542          i = decodeVarint(x+j, &iType);
   543    543          printBytes(a, x+j, i);
   544    544          printf("typecode[%d]: %d - ", nCol, (int)iType);
   545    545          switch( iType ){