Fossil

Check-in Differences
Login

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

Difference From:

[9aa9ba8b] Version 2.7 (user: drh tags: trunk, release, version-2.7, date: 2018-09-22 17:40:11)

To:

[19eaa3ca] Next and Previous submenu buttons on the /wdiff page in order to step through all versions of a wiki page. (user: drh tags: trunk, date: 2018-12-12 20:43:14)

Changes to Makefile.classic.

    50     50   
    51     51   #### We sometimes add the -static option here so that we can build a
    52     52   #    static executable that will run in a chroot jail.
    53     53   #LIB = -static
    54     54   TCC += -DFOSSIL_DYNAMIC_BUILD=1
    55     55   
    56     56   TCCFLAGS = $(CFLAGS)
           57  +
           58  +# We don't attempt to use libedit or libreadline in this simplified
           59  +# build system (contrast auto.def and Makefile.in) so use the included
           60  +# copy of linenoise.  MinGW can't make use of this, but linenoise is
           61  +# ifdef'd out elsewhere for that platform.  Note that this is a make
           62  +# flag handled in src/main.mk, not a C preprocessor flag.
           63  +USE_LINENOISE := 1
    57     64   
    58     65   #### Extra arguments for linking the finished binary.  Fossil needs
    59     66   #    to link against the Z-Lib compression library unless the miniz
    60     67   #    library in the source tree is being used.  There are no other
    61     68   #    required dependencies.
    62     69   ZLIB_LIB.0 = -lz
    63     70   ZLIB_LIB.1 =
................................................................................
    64     71   ZLIB_LIB.  = $(ZLIB_LIB.0)
    65     72   
    66     73   # If using zlib:
    67     74   LIB += $(ZLIB_LIB.$(FOSSIL_ENABLE_MINIZ)) $(LDFLAGS)
    68     75   
    69     76   # If using HTTPS:
    70     77   LIB += -lcrypto -lssl
           78  +
           79  +# Many platforms put cos() needed by src/piechart.c in libm, rather than
           80  +# in libc.  We cannot enable this by default because libm doesn't exist
           81  +# everywhere.
           82  +#LIB += -lm
    71     83   
    72     84   #### Tcl shell for use in running the fossil testsuite.  If you do not
    73     85   #    care about testing the end result, this can be blank.
    74     86   #
    75     87   TCLSH = tclsh
    76     88   
    77     89   # You should not need to change anything below this line

Changes to auto.def.

   511    511   cc-check-function-in-lib gethostbyname nsl
   512    512   if {![cc-check-function-in-lib socket {socket network}]} {
   513    513       # Last resort, may be Windows
   514    514       if {[is_mingw]} {
   515    515           define-append LIBS -lwsock32
   516    516       }
   517    517   }
   518         -cc-check-function-in-lib ns_name_uncompress resolv
          518  +
          519  +# The SMTP module requires special libraries and headers for MX DNS
          520  +# record lookups and such.
          521  +cc-check-includes arpa/nameser.h
          522  +cc-include-needs bind/resolv.h netinet/in.h
          523  +cc-check-includes bind/resolv.h
          524  +cc-check-includes resolv.h
          525  +if {    !(([cc-check-function-in-lib dn_expand resolv] ||
          526  +           [cc-check-function-in-lib   ns_name_uncompress {bind resolv}] ||
          527  +           [cc-check-function-in-lib __ns_name_uncompress {bind resolv}]) &&
          528  +          ([cc-check-function-in-lib   ns_parserr {bind resolv}] ||
          529  +           [cc-check-function-in-lib __ns_parserr {bind resolv}]) &&
          530  +          ([cc-check-function-in-lib   res_query {bind resolv}] ||
          531  +           [cc-check-function-in-lib __res_query {bind resolv}]))} {
          532  +    msg-result "WARNING: SMTP feature will not be able to look up local MX."
          533  +}
          534  +cc-check-function-in-lib res_9_ns_initparse resolv
          535  +
          536  +# Other nonstandard function checks
   519    537   cc-check-functions utime
   520    538   cc-check-functions usleep
   521    539   cc-check-functions strchrnul
   522    540   cc-check-functions pledge
   523    541   cc-check-functions backtrace
   524    542   
   525    543   # Check for getloadavg(), and if it doesn't exist, define FOSSIL_OMIT_LOAD_AVERAGE

Changes to fossil.1.

     1         -.TH FOSSIL "1" "February 2015" "http://fossil-scm.org" "User Commands"
            1  +.TH FOSSIL "1" "September 2018" "http://fossil-scm.org" "User Commands"
     2      2   .SH NAME
     3      3   fossil \- Distributed Version Control System
     4      4   .SH SYNOPSIS
     5      5   .B fossil
     6      6   \fIhelp\fR
     7      7   .br
     8      8   .B fossil
     9      9   \fIhelp COMMAND\fR
    10     10   .br
    11     11   .B fossil
    12     12   \fICOMMAND [OPTIONS]\fR
    13     13   .SH DESCRIPTION
    14     14   Fossil is a distributed version control system (DVCS) with built-in
    15         -wiki, ticket tracker, CGI/http interface, and http server.
           15  +forum, wiki, ticket tracker, CGI/HTTP interface, and HTTP server.
    16     16   
    17     17   .SH Common COMMANDs:
    18     18   
    19         -add            changes        gdiff          publish        status
           19  +add            clean          help           push           timeline
           20  +.br
           21  +addremove      clone          import         rebuild        ui
           22  +.br
           23  +all            commit         info           remote-url     undo
           24  +.br
           25  +amend          delete         init           revert         unpublished
           26  +.br
           27  +annotate       diff           ls             rm             unversioned
           28  +.br
           29  +bisect         export         merge          settings       update
           30  +.br
           31  +blame          extras         mv             sql            version
    20     32   .br
    21         -addremove      clean          help           pull           sync
           33  +branch         finfo          open           stash
    22     34   .br
    23         -all            clone          import         push           tag
           35  +bundle         fusefs         praise         status
    24     36   .br
    25         -amend          commit         info           rebuild        timeline
    26         -.br
    27         -annotate       delete         init           remote-url     ui
           37  +cat            gdiff          publish        sync
    28     38   .br
    29         -bisect         diff           ls             revert         undo
    30         -.br
    31         -blame          export         merge          rm             unpublished
    32         -.br
    33         -branch         extras         mv             settings       unversioned
    34         -.br
    35         -bundle         finfo          open           sqlite3        update
    36         -.br
    37         -cat            fusefs         praise         stash          version
           39  +changes        grep           pull           tag
    38     40   
    39     41   .SH FEATURES
    40     42   
    41     43   Features as described on the fossil home page.
    42     44   
    43     45   .HP
    44     46   1.
    45         -.B Integrated Bug Tracking, Wiki, & Technotes
           47  +.B Integrated Bug Tracking, Wiki, Forum, and Technotes
    46     48   - In addition to doing distributed version control like Git and
    47         -Mercurial, Fossil also supports bug tracking, wiki, and technotes.
           49  +Mercurial, Fossil also supports bug tracking, wiki, forum, and
           50  +technotes.
    48     51   
    49     52   .HP
    50     53   2.
    51     54   .B Built-in Web Interface
    52     55   - Fossil has a built-in and intuitive web interface that promotes
    53     56   project situational awareness. Type "fossil ui" and Fossil automatically
    54     57   opens a web browser to a page that shows detailed graphical history and
................................................................................
    64     67   
    65     68   .HP
    66     69   4.
    67     70   .B Simple Networking
    68     71   - No custom protocols or TCP ports. Fossil uses plain old HTTP (or HTTPS
    69     72   or SSH) for all network communications, so it works fine from behind
    70     73   restrictive firewalls, including proxies. The protocol is bandwidth
    71         -efficient to the point that Fossil can be used comfortably over dial-up.
           74  +efficient to the point that Fossil can be used comfortably over dial-up
           75  +or over the exceedingly slow Wifi on airliners.
    72     76   
    73     77   .HP
    74     78   5.
    75     79   .B CGI/SCGI Enabled
    76     80   - No server is required, but if you want to set one up, Fossil supports
    77         -four simple server configurations.
           81  +four easy server configurations.
    78     82   
    79     83   .HP
    80     84   6.
    81     85   .B Autosync
    82     86   - Fossil supports "autosync" mode which helps to keep projects moving
    83     87   forward by reducing the amount of needless forking and merging often
    84     88   associated with distributed projects.
................................................................................
    85     89   
    86     90   .HP
    87     91   7.
    88     92   .B Robust & Reliable
    89     93   - Fossil stores content using an enduring file format in an SQLite
    90     94   database so that transactions are atomic even if interrupted by a
    91     95   power loss or system crash. Automatic self-checks verify that all
    92         -aspects of the repository are consistent prior to each commit. In
    93         -over seven years of operation, no work has ever been lost after
    94         -having been committed to a Fossil repository.
           96  +aspects of the repository are consistent prior to each commit.
           97  +
           98  +.HP
           99  +8.
          100  +.B Free and Open-Source
          101  +- Uses the 2-clause BSD license.
    95    102   
    96    103   .SH DOCUMENTATION
    97    104   http://www.fossil-scm.org/
    98    105   .br
    99    106   .B fossil
   100    107   \fIui\fR

Changes to skins/bootstrap/header.txt.

     1      1   <html lang="en">
     2      2   <head>
     3      3     <meta charset="utf-8">
     4      4     <base href="$baseurl/$current_page" />
     5      5     <title>$<project_name>: $<title></title>
     6      6       <meta name="viewport" content="width=device-width, initial-scale=1.0">
            7  +    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data:; script-src 'self' 'nonce-$<nonce>'; style-src 'self' 'unsafe-inline'"/>
     7      8       <link rel="alternate" type="application/rss+xml" title="RSS Feed" href="$home/timeline.rss" />
     8      9       <link rel="stylesheet" href="$home/style.css?default" type="text/css" media="screen" />
     9     10       <script>
    10     11       function gebi(x){
    11     12         if(/^#/.test(x)) x = x.substr(1);
    12     13         var e = document.getElementById(x);
    13     14         if(!e) throw new Error("Expecting element with ID "+x);
................................................................................
   112    113             </div><!--/.nav-collapse -->
   113    114           </div>
   114    115         </div>
   115    116         <div class="content">
   116    117           <th1>
   117    118             html "<div class='container'>"
   118    119               html "<ul class='breadcrumb'>"
   119         -              html "<li><a href='$index_page'>Home</a></li>"
   120         -              html "<li><a href='$home/$current_page'>$title</a></li>"
          120  +              html "<li><a href='$home$index_page'>Home</a></li>"
          121  +              html "<li><a href='$home/$current_page'>[htmlize $title]</a></li>"
   121    122                 html "</ul>"
   122    123               </th1>

Changes to skins/default/css.txt.

   143    143       overflow: auto;
   144    144       border: 1px solid #ccc;
   145    145       border-radius: 5px;
   146    146   }
   147    147   .content blockquote {
   148    148       padding: 0 15px;
   149    149   }
   150         -div.forumHierRoot blockquote, div.forumHier blockquote {
          150  +div.forumHierRoot blockquote, div.forumHier blockquote, div.forumEdit blockquote {
   151    151       background-color: rgba(65, 131, 196, 0.1);
   152    152       border-left: 3px solid #254769;
   153    153       padding: .1em 1em;
   154    154   }
   155    155   
   156    156   table.report {
   157    157       cursor: auto;

Changes to src/alerts.c.

   844    844         emailerError(p, "Failed to insert email message into output queue.\n"
   845    845                         "%s", sqlite3_errmsg(p->db));
   846    846       }
   847    847     }else if( p->zCmd ){
   848    848       FILE *out = popen(p->zCmd, "w");
   849    849       if( out ){
   850    850         fwrite(blob_buffer(&all), 1, blob_size(&all), out);
   851         -      fclose(out);
          851  +      pclose(out);
   852    852       }else{
   853    853         emailerError(p, "Could not open output pipe \"%s\"", p->zCmd);
   854    854       }
   855    855     }else if( p->zDir ){
   856    856       char *zFile = file_time_tempname(p->zDir, ".email");
   857    857       blob_write_to_file(&all, zFile);
   858    858       fossil_free(zFile);
................................................................................
   920    920   ** are sent when email-send-method is "relay".  There should be an
   921    921   ** SMTP server configured as a Mail Submission Agent listening on the
   922    922   ** designated host and port and all times.
   923    923   */
   924    924   
   925    925   
   926    926   /*
   927         -** COMMAND: alerts
          927  +** COMMAND: alerts*
   928    928   ** 
   929    929   ** Usage: %fossil alerts SUBCOMMAND ARGS...
   930    930   **
   931    931   ** Subcommands:
   932    932   **
   933    933   **    pending                 Show all pending alerts.  Useful for debugging.
   934    934   **
................................................................................
  2033   2033       p->type = db_column_text(&q, 3)[0];
  2034   2034       p->needMod = db_column_int(&q, 4);
  2035   2035       p->zFromName = 0;
  2036   2036       p->pNext = 0;
  2037   2037       switch( p->type ){
  2038   2038         case 'c':  zType = "Check-In";        break;
  2039   2039         case 'f':  zType = "Forum post";      break;
  2040         -      case 't':  zType = "Wiki Edit";       break;
  2041         -      case 'w':  zType = "Ticket Change";   break;
         2040  +      case 't':  zType = "Ticket Change";   break;
         2041  +      case 'w':  zType = "Wiki Edit";       break;
  2042   2042       }
  2043   2043       blob_init(&p->hdr, 0, 0);
  2044   2044       blob_init(&p->txt, 0, 0);
  2045   2045       blob_appendf(&p->txt,"== %s %s ==\n%s\n%s/info/%.20s\n",
  2046   2046         db_column_text(&q,1),
  2047   2047         zType,
  2048   2048         db_column_text(&q,2),
................................................................................
  2424   2424       const char *zEmail = db_column_text(&q, 1);
  2425   2425       const char *zCap = db_column_text(&q, 3);
  2426   2426       int nHit = 0;
  2427   2427       for(p=pEvents; p; p=p->pNext){
  2428   2428         if( strchr(zSub,p->type)==0 ) continue;
  2429   2429         if( p->needMod ){
  2430   2430           /* For events that require moderator approval, only send an alert
  2431         -        ** if the recipient is a moderator for that type of event */
         2431  +        ** if the recipient is a moderator for that type of event.  Setup
         2432  +        ** and Admin users always get notified. */
  2432   2433           char xType = '*';
  2433         -        switch( p->type ){
  2434         -          case 'f':  xType = '5';  break;
  2435         -          case 't':  xType = 'q';  break;
  2436         -          case 'w':  xType = 'l';  break;
         2434  +        if( strpbrk(zCap,"as")==0 ){
         2435  +          switch( p->type ){
         2436  +            case 'f':  xType = '5';  break;
         2437  +            case 't':  xType = 'q';  break;
         2438  +            case 'w':  xType = 'l';  break;
         2439  +          }
         2440  +          if( strchr(zCap,xType)==0 ) continue;
  2437   2441           }
  2438         -        if( strchr(zCap,xType)==0 ) continue;
  2439   2442         }else if( strchr(zCap,'s')!=0 || strchr(zCap,'a')!=0 ){
  2440   2443           /* Setup and admin users can get any notification that does not
  2441   2444           ** require moderation */
  2442   2445         }else{
  2443   2446           /* Other users only see the alert if they have sufficient
  2444   2447           ** privilege to view the event itself */
  2445   2448           char xType = '*';
................................................................................
  2696   2699       return;
  2697   2700     }
  2698   2701     if( fossil_strcmp(P("name"),"test1")==0 ){
  2699   2702       /* Visit the /announce/test1 page to see the CGI variables */
  2700   2703       @ <p style='border: 1px solid black; padding: 1ex;'>
  2701   2704       cgi_print_all(0, 0);
  2702   2705       @ </p>
  2703         -  }else
  2704         -  if( P("submit")!=0 && cgi_csrf_safe(1) ){
         2706  +  }else if( P("submit")!=0 && cgi_csrf_safe(1) ){
  2705   2707       char *zErr = alert_send_announcement();
  2706   2708       style_header("Announcement Sent");
  2707   2709       if( zErr ){
  2708   2710         @ <h1>Internal Error</h1>
  2709   2711         @ <p>The following error was reported by the system:
  2710   2712         @ <blockquote><pre>
  2711   2713         @ %h(zErr)
  2712   2714         @ </pre></blockquote>
  2713   2715       }else{
  2714   2716         @ <p>The announcement has been sent.</p>
  2715   2717       }
  2716   2718       style_footer();    
  2717   2719       return;
         2720  +  } else if( !alert_enabled() ){
         2721  +    style_header("Cannot Send Announcement");
         2722  +    @ <p>Either you have no subscribers yet, or email alerts are not yet
         2723  +    @ <a href="https://fossil-scm.org/fossil/doc/trunk/www/alerts.md">set up</a>
         2724  +    @ for this repository.</p>
         2725  +    return;
  2718   2726     }
         2727  +
  2719   2728     style_header("Send Announcement");
  2720   2729     @ <form method="POST">
  2721   2730     @ <table class="subscribe">
  2722   2731     if( g.perm.Admin ){
  2723   2732       int aa = PB("aa");
  2724   2733       int all = PB("all");
  2725   2734       const char *aack = aa ? "checked" : "";

Changes to src/backoffice.c.

   380    380     Lease x;
   381    381     sqlite3_uint64 tmNow;
   382    382   
   383    383     if( backofficeDb ) return;
   384    384     if( g.zRepositoryName==0 ) return;
   385    385     if( g.db==0 ) return;
   386    386     if( !db_table_exists("repository","config") ) return;
          387  +  if( db_get_boolean("backoffice-disable",0) ) return;
   387    388     tmNow = time(0);
   388    389     backofficeReadLease(&x);
   389    390     if( x.tmNext>=tmNow && backofficeProcessExists(x.idNext) ){
   390    391       /* Another backoffice process is already queued up to run.  This
   391    392       ** process does not need to do any backoffice work. */
   392    393       return;
   393    394     }else{
   394    395       /* We need to run backup to be (at a minimum) on-deck */
   395    396       backofficeDb = fossil_strdup(g.zRepositoryName);
   396    397     }
   397    398   }
          399  +
          400  +/*
          401  +** Call this routine to disable backoffice
          402  +*/
          403  +void backoffice_disable(void){
          404  +  backofficeDb = "x";
          405  +}
   398    406   
   399    407   /*
   400    408   ** Check for errors prior to running backoffice_thread() or backoffice_run().
   401    409   */
   402    410   static void backoffice_error_check_one(int *pOnce){
   403    411     if( *pOnce ){
   404    412       fossil_panic("multiple calls to backoffice()");
................................................................................
   429    437     Lease x;
   430    438     sqlite3_uint64 tmNow;
   431    439     sqlite3_uint64 idSelf;
   432    440     int lastWarning = 0;
   433    441     int warningDelay = 30;
   434    442     static int once = 0;
   435    443   
          444  +  if( sqlite3_db_readonly(g.db, 0) ) return;
   436    445     backoffice_error_check_one(&once);
   437    446     idSelf = backofficeProcessId();
   438    447     while(1){
   439    448       tmNow = time(0);
   440    449       db_begin_write();
   441    450       backofficeReadLease(&x);
   442    451       if( x.tmNext>=tmNow
................................................................................
   522    531   
   523    532     /* Here is where the actual work of the backoffice happens */
   524    533     alert_backoffice(0);
   525    534     smtp_cleanup();
   526    535   }
   527    536   
   528    537   /*
   529         -** COMMAND: backoffice
          538  +** COMMAND: backoffice*
   530    539   **
   531    540   ** Usage: backoffice [-R repository]
   532    541   **
   533    542   ** Run backoffice processing.  This might be done by a cron job or
   534    543   ** similar to make sure backoffice processing happens periodically.
   535    544   */
   536    545   void backoffice_command(void){

Changes to src/bisect.c.

   383    383       char *zDisplay = db_lget("bisect-display","chart");
   384    384       int m = (int)strlen(zDisplay);
   385    385       bisect_path();
   386    386       pMid = path_midpoint();
   387    387       if( pMid==0 ){
   388    388         fossil_print("bisect complete\n");
   389    389       }else{
          390  +      int nSpan = path_length();
          391  +      int nStep = path_search_depth();
   390    392         g.argv[1] = "update";
   391    393         g.argv[2] = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", pMid->rid);
   392    394         g.argc = 3;
   393    395         g.fNoSync = 1;
   394    396         update_cmd();
          397  +      fossil_print("span: %d  steps-remaining: %d\n", nSpan, nStep);
   395    398       }
   396    399   
   397    400       if( strncmp(zDisplay,"chart",m)==0 ){
   398    401         bisect_chart(1);
   399    402       }else if( strncmp(zDisplay, "log", m)==0 ){
   400    403         bisect_chart(0);
   401    404       }else if( strncmp(zDisplay, "status", m)==0 ){

Changes to src/branch.c.

    18     18   ** This file contains code used to create new branches within a repository.
    19     19   */
    20     20   #include "config.h"
    21     21   #include "branch.h"
    22     22   #include <assert.h>
    23     23   
    24     24   /*
    25         -**  fossil branch new    NAME BASIS ?OPTIONS?
           25  +**  fossil branch new    NAME  BASIS ?OPTIONS?
    26     26   **  argv0  argv1  argv2  argv3 argv4
    27     27   */
    28     28   void branch_new(void){
    29     29     int rootid;            /* RID of the root check-in - what we branch off of */
    30     30     int brid;              /* RID of the branch check-in */
    31     31     int noSign;            /* True if the branch is unsigned */
    32     32     int i;                 /* Loop counter */
................................................................................
   176    176   
   177    177     /* Commit */
   178    178     db_end_transaction(0);
   179    179   
   180    180     /* Do an autosync push, if requested */
   181    181     if( !isPrivate ) autosync_loop(SYNC_PUSH, db_get_int("autosync-tries",1),0);
   182    182   }
          183  +
          184  +/*
          185  +** Create a TEMP table named "tmp_brlist" with 7 columns:
          186  +**
          187  +**      name           Name of the branch
          188  +**      mtime          Time of last checkin on this branch
          189  +**      isclosed       True if the branch is closed
          190  +**      mergeto        Another branch this branch was merged into
          191  +**      nckin          Number of checkins on this branch
          192  +**      ckin           Hash of the last checkin on this branch
          193  +**      bgclr          Background color for this branch
          194  +*/
          195  +static const char createBrlistQuery[] =
          196  +@ CREATE TEMP TABLE IF NOT EXISTS tmp_brlist AS
          197  +@ SELECT
          198  +@   tagxref.value AS name,
          199  +@   max(event.mtime) AS mtime,
          200  +@   EXISTS(SELECT 1 FROM tagxref AS tx
          201  +@           WHERE tx.rid=tagxref.rid
          202  +@             AND tx.tagid=(SELECT tagid FROM tag WHERE tagname='closed')
          203  +@             AND tx.tagtype>0) AS isclosed,
          204  +@   (SELECT tagxref.value
          205  +@      FROM plink CROSS JOIN tagxref
          206  +@    WHERE plink.pid=event.objid
          207  +@       AND tagxref.rid=plink.cid
          208  +@      AND tagxref.tagid=(SELECT tagid FROM tag WHERE tagname='branch')
          209  +@      AND tagtype>0) AS mergeto,
          210  +@   count(*) AS nckin,
          211  +@   (SELECT uuid FROM blob WHERE rid=tagxref.rid) AS ckin,
          212  +@   event.bgcolor AS bgclr
          213  +@  FROM tagxref, tag, event
          214  +@ WHERE tagxref.tagid=tag.tagid
          215  +@   AND tagxref.tagtype>0
          216  +@   AND tag.tagname='branch'
          217  +@   AND event.objid=tagxref.rid
          218  +@ GROUP BY 1;
          219  +;
          220  +
          221  +/* Call this routine to create the TEMP table */
          222  +static void brlist_create_temp_table(void){
          223  +  db_multi_exec(createBrlistQuery/*works-like:""*/);
          224  +}
          225  +
   183    226   
   184    227   #if INTERFACE
   185    228   /*
   186    229   ** Allows bits in the mBplqFlags parameter to branch_prepare_list_query().
   187    230   */
   188    231   #define BRL_CLOSED_ONLY      0x001 /* Show only closed branches */
   189    232   #define BRL_OPEN_ONLY        0x002 /* Show only open branches */
   190    233   #define BRL_BOTH             0x003 /* Show both open and closed branches */
   191    234   #define BRL_OPEN_CLOSED_MASK 0x003
   192         -#define BRL_MTIME            0x004 /* Include lastest check-in time */
   193         -#define BRL_ORDERBY_MTIME    0x008 /* Sort by MTIME. (otherwise sort by name)*/
          235  +#define BRL_ORDERBY_MTIME    0x004 /* Sort by MTIME. (otherwise sort by name)*/
          236  +#define BRL_REVERSE          0x008 /* Reverse the sort order */
   194    237   
   195    238   #endif /* INTERFACE */
   196    239   
   197    240   /*
   198    241   ** Prepare a query that will list branches.
   199    242   **
   200    243   ** If (which<0) then the query pulls only closed branches. If
   201    244   ** (which>0) then the query pulls all (closed and opened)
   202    245   ** branches. Else the query pulls currently-opened branches.
   203    246   */
   204    247   void branch_prepare_list_query(Stmt *pQuery, int brFlags){
          248  +  Blob sql;
          249  +  blob_init(&sql, 0, 0);
          250  +  brlist_create_temp_table();
   205    251     switch( brFlags & BRL_OPEN_CLOSED_MASK ){
   206    252       case BRL_CLOSED_ONLY: {
   207         -      db_prepare(pQuery,
   208         -        "SELECT value FROM tagxref"
   209         -        " WHERE tagid=%d AND value NOT NULL "
   210         -        "EXCEPT "
   211         -        "SELECT value FROM tagxref"
   212         -        " WHERE tagid=%d"
   213         -        "   AND rid IN leaf"
   214         -        "   AND NOT %z"
   215         -        " ORDER BY value COLLATE nocase /*sort*/",
   216         -        TAG_BRANCH, TAG_BRANCH, leaf_is_closed_sql("tagxref.rid")
          253  +      blob_append_sql(&sql,
          254  +        "SELECT name FROM tmp_brlist WHERE isclosed"
   217    255         );
   218    256         break;
   219    257       }
   220    258       case BRL_BOTH: {
   221         -      db_prepare(pQuery,
   222         -        "SELECT DISTINCT value FROM tagxref"
   223         -        " WHERE tagid=%d AND value NOT NULL"
   224         -        "   AND rid IN leaf"
   225         -        " ORDER BY value COLLATE nocase /*sort*/",
   226         -        TAG_BRANCH
          259  +      blob_append_sql(&sql,
          260  +        "SELECT name FROM tmp_brlist"
   227    261         );
   228    262         break;
   229    263       }
   230    264       case BRL_OPEN_ONLY: {
   231         -      db_prepare(pQuery,
   232         -        "SELECT DISTINCT value FROM tagxref"
   233         -        " WHERE tagid=%d AND value NOT NULL"
   234         -        "   AND rid IN leaf"
   235         -        "   AND NOT %z"
   236         -        " ORDER BY value COLLATE nocase /*sort*/",
   237         -        TAG_BRANCH, leaf_is_closed_sql("tagxref.rid")
          265  +      blob_append_sql(&sql,
          266  +        "SELECT name FROM tmp_brlist WHERE NOT isclosed"
   238    267         );
   239    268         break;
   240    269       }
   241    270     }
          271  +  if( brFlags & BRL_ORDERBY_MTIME ){
          272  +    blob_append_sql(&sql, " ORDER BY -mtime");
          273  +  }else{
          274  +    blob_append_sql(&sql, " ORDER BY name COLLATE nocase");
          275  +  }
          276  +  if( brFlags & BRL_REVERSE ){
          277  +    blob_append_sql(&sql," DESC");
          278  +  }
          279  +  db_prepare_blob(pQuery, &sql);
          280  +  blob_reset(&sql);
   242    281   }
   243    282   
   244    283   /*
   245    284   ** If the branch named in the argument is open, return a RID for one of
   246    285   ** the open leaves of that branch.  If the branch does not exists or is
   247    286   ** closed, return 0.
   248    287   */
................................................................................
   274    313   **
   275    314   **        Print the name of the branch for the current check-out
   276    315   **
   277    316   **    fossil branch info BRANCH-NAME
   278    317   **
   279    318   **        Print information about a branch
   280    319   **
   281         -**    fossil branch list|ls ?-a|--all|-c|--closed?
          320  +**    fossil branch list|ls ?OPTIONS?
   282    321   **
   283         -**        List all branches.  Use -a or --all to list all branches and
   284         -**        -c or --closed to list all closed branches.  The default is to
   285         -**        show only open branches.
          322  +**        List all branches. Options:
          323  +**          -a|--all      List all branches.  Default show only open branches
          324  +**          -c|--closed   List closed branches.
          325  +**          -r            Reverse the sort order
          326  +**          -t            Show recently changed branches first
   286    327   **
   287    328   **    fossil branch new BRANCH-NAME BASIS ?OPTIONS?
   288    329   **
   289    330   **        Create a new branch BRANCH-NAME off of check-in BASIS.
   290    331   **        Supported options for this subcommand include:
   291    332   **        --private             branch is private (i.e., remains local)
   292    333   **        --bgcolor COLOR       use COLOR instead of automatic background
................................................................................
   343    384     }else if( (strncmp(zCmd,"list",n)==0)||(strncmp(zCmd, "ls", n)==0) ){
   344    385       Stmt q;
   345    386       int vid;
   346    387       char *zCurrent = 0;
   347    388       int brFlags = BRL_OPEN_ONLY;
   348    389       if( find_option("all","a",0)!=0 ) brFlags = BRL_BOTH;
   349    390       if( find_option("closed","c",0)!=0 ) brFlags = BRL_CLOSED_ONLY;
          391  +    if( find_option("t",0,0)!=0 ) brFlags |= BRL_ORDERBY_MTIME;
          392  +    if( find_option("r",0,0)!=0 ) brFlags |= BRL_REVERSE;
   350    393   
   351    394       if( g.localOpen ){
   352    395         vid = db_lget_int("checkout", 0);
   353    396         zCurrent = db_text(0, "SELECT value FROM tagxref"
   354    397                               " WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
   355    398       }
   356    399       branch_prepare_list_query(&q, brFlags);
................................................................................
   364    407       branch_new();
   365    408     }else{
   366    409       fossil_fatal("branch subcommand should be one of: "
   367    410                    "current info list ls new");
   368    411     }
   369    412   }
   370    413   
   371         -static const char brlistQuery[] =
   372         -@ SELECT
   373         -@   tagxref.value,
   374         -@   max(event.mtime),
   375         -@   EXISTS(SELECT 1 FROM tagxref AS tx
   376         -@           WHERE tx.rid=tagxref.rid
   377         -@             AND tx.tagid=(SELECT tagid FROM tag WHERE tagname='closed')
   378         -@             AND tx.tagtype>0),
   379         -@   (SELECT tagxref.value
   380         -@      FROM plink CROSS JOIN tagxref
   381         -@    WHERE plink.pid=event.objid
   382         -@       AND tagxref.rid=plink.cid
   383         -@      AND tagxref.tagid=(SELECT tagid FROM tag WHERE tagname='branch')
   384         -@      AND tagtype>0),
   385         -@   count(*),
   386         -@   (SELECT uuid FROM blob WHERE rid=tagxref.rid),
   387         -@   event.bgcolor
   388         -@  FROM tagxref, tag, event
   389         -@ WHERE tagxref.tagid=tag.tagid
   390         -@   AND tagxref.tagtype>0
   391         -@   AND tag.tagname='branch'
   392         -@   AND event.objid=tagxref.rid
   393         -@ GROUP BY 1
   394         -@ ORDER BY 2 DESC;
   395         -;
   396         -
   397    414   /*
   398    415   ** This is the new-style branch-list page that shows the branch names
   399    416   ** together with their ages (time of last check-in) and whether or not
   400    417   ** they are closed or merged to another branch.
   401    418   **
   402    419   ** Control jumps to this routine from brlist_page() (the /brlist handler)
   403    420   ** if there are no query parameters.
................................................................................
   409    426     login_check_credentials();
   410    427     if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
   411    428     style_header("Branches");
   412    429     style_adunit_config(ADUNIT_RIGHT_OK);
   413    430     style_submenu_checkbox("colors", "Use Branch Colors", 0, 0);
   414    431     login_anonymous_available();
   415    432   
   416         -  db_prepare(&q, brlistQuery/*works-like:""*/);
          433  +  brlist_create_temp_table();
          434  +  db_prepare(&q, "SELECT * FROM tmp_brlist ORDER BY mtime DESC");
   417    435     rNow = db_double(0.0, "SELECT julianday('now')");
   418    436     @ <div class="brlist">
   419    437     @ <table class='sortable' data-column-types='tkNtt' data-init-sort='2'>
   420    438     @ <thead><tr>
   421    439     @ <th>Branch Name</th>
   422         -  @ <th>Age</th>
          440  +  @ <th>Last Change</th>
   423    441     @ <th>Check-ins</th>
   424    442     @ <th>Status</th>
   425    443     @ <th>Resolution</th>
   426    444     @ </tr></thead><tbody>
   427    445     while( db_step(&q)==SQLITE_ROW ){
   428    446       const char *zBranch = db_column_text(&q, 0);
   429    447       double rMtime = db_column_double(&q, 1);
................................................................................
   444    462       }
   445    463       if( zBgClr && zBgClr[0] && show_colors ){
   446    464         @ <tr style="background-color:%s(zBgClr)">
   447    465       }else{
   448    466         @ <tr>
   449    467       }
   450    468       @ <td>%z(href("%R/timeline?n=100&r=%T",zBranch))%h(zBranch)</a></td>
   451         -    @ <td data-sortkey="%016llx(-iMtime)">%s(zAge)</td>
          469  +    @ <td data-sortkey="%016llx(iMtime)">%s(zAge)</td>
   452    470       @ <td>%d(nCkin)</td>
   453    471       fossil_free(zAge);
   454    472       @ <td>%s(isClosed?"closed":"")</td>
   455    473       if( zMergeTo ){
   456    474         @ <td>merged into
   457    475         @ %z(href("%R/timeline?f=%!S",zLastCkin))%h(zMergeTo)</a></td>
   458    476       }else{

Changes to src/browse.c.

   940    940     }else if( rAge*1440.0<90 ){
   941    941       return mprintf("%.1f minutes", rAge*1440.0);
   942    942     }else if( rAge*24.0<36 ){
   943    943       return mprintf("%.1f hours", rAge*24.0);
   944    944     }else if( rAge<365.0 ){
   945    945       return mprintf("%.1f days", rAge);
   946    946     }else{
   947         -    return mprintf("%.2f years", rAge/365.0);
          947  +    return mprintf("%.2f years", rAge/365.2425);
   948    948     }
   949    949   }
   950    950   
   951    951   /*
   952    952   ** COMMAND: test-fileage
   953    953   **
   954    954   ** Usage: %fossil test-fileage CHECKIN

Changes to src/cache.c.

   342    342                    " Should be one of: clear init list status", zCmd);
   343    343     }
   344    344   }
   345    345   
   346    346   /*
   347    347   ** WEBPAGE: cachestat
   348    348   **
   349         -** Show information about the webpage cache.  Requires Admin privilege.
          349  +** Show information about the webpage cache.  Requires Setup privilege.
   350    350   */
   351    351   void cache_page(void){
   352    352     sqlite3 *db;
   353    353     sqlite3_stmt *pStmt;
   354    354     char zBuf[100];
   355    355   
   356    356     login_check_credentials();

Changes to src/capabilities.c.

   226    226   
   227    227   /*
   228    228   ** The following structure holds descriptions of the various capabilities.
   229    229   */
   230    230   static struct Caps {
   231    231     char cCap;              /* The capability letter */
   232    232     unsigned short eClass;  /* The "class" for this capability */
          233  +  unsigned nUser;         /* Number of users with this capability */
   233    234     char *zAbbrev;          /* Abbreviated mnemonic name */
   234    235     char *zOneLiner;        /* One-line summary */
   235    236   } aCap[] = {
   236         -  { 'a', CAPCLASS_SUPER,
          237  +  { 'a', CAPCLASS_SUPER, 0,
   237    238       "Admin", "Create and delete users" },
   238         -  { 'b', CAPCLASS_WIKI|CAPCLASS_TKT,
          239  +  { 'b', CAPCLASS_WIKI|CAPCLASS_TKT, 0,
   239    240       "Attach", "Add attchments to wiki or tickets" },
   240         -  { 'c', CAPCLASS_TKT,
          241  +  { 'c', CAPCLASS_TKT, 0,
   241    242       "Append-Tkt", "Append to existing tickets" },
   242         -  { 'd', CAPCLASS_WIKI|CAPCLASS_TKT,
          243  +  { 'd', CAPCLASS_WIKI|CAPCLASS_TKT, 0,
   243    244       "Delete", "Delete wiki or tickets" },
   244         -  { 'e', CAPCLASS_DATA,
          245  +  { 'e', CAPCLASS_DATA, 0,
   245    246       "View-PII", "View sensitive info such as email addresses" },
   246         -  { 'f', CAPCLASS_WIKI,
          247  +  { 'f', CAPCLASS_WIKI, 0,
   247    248       "New-Wiki", "Create new wiki pages" },
   248         -  { 'g', CAPCLASS_DATA,
          249  +  { 'g', CAPCLASS_DATA, 0,
   249    250       "Clone", "Clone the repository" },
   250         -  { 'h', CAPCLASS_OTHER,
          251  +  { 'h', CAPCLASS_OTHER, 0,
   251    252       "Hyperlinks", "Show hyperlinks to detailed repository history" },
   252         -  { 'i', CAPCLASS_CODE,
          253  +  { 'i', CAPCLASS_CODE, 0,
   253    254       "Check-In", "Check-in code changes" },
   254         -  { 'j', CAPCLASS_WIKI,
          255  +  { 'j', CAPCLASS_WIKI, 0,
   255    256       "Read-Wiki", "View wiki pages" },
   256         -  { 'k', CAPCLASS_WIKI,
          257  +  { 'k', CAPCLASS_WIKI, 0,
   257    258       "Write-Wiki", "Edit wiki pages" },
   258         -  { 'l', CAPCLASS_WIKI|CAPCLASS_SUPER,
          259  +  { 'l', CAPCLASS_WIKI|CAPCLASS_SUPER, 0,
   259    260       "Mod-Wiki", "Moderator for wiki pages" },
   260         -  { 'm', CAPCLASS_WIKI,
          261  +  { 'm', CAPCLASS_WIKI, 0,
   261    262       "Append-Wiki", "Append to wiki pages" },
   262         -  { 'n', CAPCLASS_TKT,
          263  +  { 'n', CAPCLASS_TKT, 0,
   263    264       "New-Tkt", "Create new tickets" },
   264         -  { 'o', CAPCLASS_CODE,
          265  +  { 'o', CAPCLASS_CODE, 0,
   265    266       "Check-Out", "Check out code" },
   266         -  { 'p', CAPCLASS_OTHER,
          267  +  { 'p', CAPCLASS_OTHER, 0,
   267    268       "Password", "Change your own password" },
   268         -  { 'q', CAPCLASS_TKT|CAPCLASS_SUPER,
          269  +  { 'q', CAPCLASS_TKT|CAPCLASS_SUPER, 0,
   269    270       "Mod-Tkt", "Moderate tickets" },
   270         -  { 'r', CAPCLASS_TKT,
          271  +  { 'r', CAPCLASS_TKT, 0,
   271    272       "Read-Tkt", "View tickets" },
   272         -  { 's', CAPCLASS_SUPER,
          273  +  { 's', CAPCLASS_SUPER, 0,
   273    274       "Superuser", "Setup and configure the respository" },
   274         -  { 't', CAPCLASS_TKT,
          275  +  { 't', CAPCLASS_TKT, 0,
   275    276       "Reports", "Create new ticket report formats" },
   276         -  { 'u', CAPCLASS_OTHER,
          277  +  { 'u', CAPCLASS_OTHER, 0,
   277    278       "Reader", "Inherit all the capabilities of the \"reader\" user" },
   278         -  { 'v', CAPCLASS_OTHER,
          279  +  { 'v', CAPCLASS_OTHER, 0,
   279    280       "Developer", "Inherit all capabilities of the \"developer\" user" },
   280         -  { 'w', CAPCLASS_TKT,
          281  +  { 'w', CAPCLASS_TKT, 0,
   281    282       "Write-Tkt", "Edit tickets" },
   282         -  { 'x', CAPCLASS_DATA,
          283  +  { 'x', CAPCLASS_DATA, 0,
   283    284       "Private", "Push and/or pull private branches" },
   284         -  { 'y', CAPCLASS_SUPER,
          285  +  { 'y', CAPCLASS_SUPER, 0,
   285    286       "Write-UV", "Push unversioned content" },
   286         -  { 'z', CAPCLASS_CODE,
          287  +  { 'z', CAPCLASS_CODE, 0,
   287    288       "Zip-Download", "Download a ZIP archive, tarball, or SQL archive" },
   288         -  { '2', CAPCLASS_FORUM,
          289  +  { '2', CAPCLASS_FORUM, 0,
   289    290       "Forum-Read", "Read forum posts by others" },
   290         -  { '3', CAPCLASS_FORUM,
          291  +  { '3', CAPCLASS_FORUM, 0,
   291    292       "Forum-Write", "Create new forum messages" },
   292         -  { '4', CAPCLASS_FORUM,
          293  +  { '4', CAPCLASS_FORUM, 0,
   293    294       "Forum-Trusted", "Create forum messages that bypass moderation" },
   294         -  { '5', CAPCLASS_FORUM|CAPCLASS_SUPER,
          295  +  { '5', CAPCLASS_FORUM|CAPCLASS_SUPER, 0,
   295    296       "Forum-Mod", "Moderator for forum messages" },
   296         -  { '6', CAPCLASS_FORUM|CAPCLASS_SUPER,
          297  +  { '6', CAPCLASS_FORUM|CAPCLASS_SUPER, 0,
   297    298       "Forum-Admin", "Set or remove capability '4' from other users" },
   298         -  { '7', CAPCLASS_ALERT,
          299  +  { '7', CAPCLASS_ALERT, 0,
   299    300       "Alerts", "Sign up for email alerts" },
   300         -  { 'A', CAPCLASS_ALERT|CAPCLASS_SUPER,
          301  +  { 'A', CAPCLASS_ALERT|CAPCLASS_SUPER, 0,
   301    302       "Announce", "Send announcements to all subscribers" },
   302         -  { 'D', CAPCLASS_OTHER,
          303  +  { 'D', CAPCLASS_OTHER, 0,
   303    304       "Debug", "Enable debugging features" },
   304    305   };
          306  +
          307  +/*
          308  +** Populate the aCap[].nUser values based on the current content
          309  +** of the USER table.
          310  +*/
          311  +void capabilities_count(void){
          312  +  int i;
          313  +  static int done = 0;
          314  +  Stmt q;
          315  +  if( done ) return;
          316  +  db_prepare(&q, "SELECT fullcap(cap) FROM user");
          317  +  while( db_step(&q)==SQLITE_ROW ){
          318  +    const char *zCap = db_column_text(&q, 0);
          319  +    if( zCap==0 || zCap[0]==0 ) continue;
          320  +    for(i=0; i<sizeof(aCap)/sizeof(aCap[0]); i++){
          321  +      if( strchr(zCap, aCap[i].cCap) ) aCap[i].nUser++;
          322  +    }
          323  +  }
          324  +  db_finalize(&q);
          325  +  done = 1;
          326  +}
   305    327   
   306    328   
   307    329   /*
   308    330   ** Generate HTML that lists all of the capability letters together with
   309    331   ** a brief summary of what each letter means.
   310    332   */
   311    333   void capabilities_table(unsigned mClass){
   312    334     int i;
          335  +  if( g.perm.Admin ) capabilities_count();
   313    336     @ <table>
          337  +  @ <tbody>
   314    338     for(i=0; i<sizeof(aCap)/sizeof(aCap[0]); i++){
          339  +    int n;
   315    340       if( (aCap[i].eClass & mClass)==0 ) continue;
   316    341       @ <tr><th valign="top">%c(aCap[i].cCap)</th>
   317         -    @  <td><i>%h(aCap[i].zAbbrev):</i> %h(aCap[i].zOneLiner)</td></tr>
          342  +    @  <td>%h(aCap[i].zAbbrev)</td><td>%h(aCap[i].zOneLiner)</td>\
          343  +    n = aCap[i].nUser;
          344  +    if( n && g.perm.Admin ){
          345  +      @ <td><a href="%R/setup_ulist?with=%c(aCap[i].cCap)">\
          346  +      @ %d(n) user%s(n>1?"s":"")</a></td>\
          347  +    }
          348  +    @ </tr>
   318    349     }
          350  +  @ </tbody>
   319    351     @ </table>
   320    352   }
   321    353   
   322    354   /*
   323    355   ** Generate a "capability summary table" that shows the major capabilities
   324    356   ** against the various user categories.
   325    357   */

Changes to src/cgi.c.

  2065   2065   ** Return true if information is coming from the loopback network.
  2066   2066   */
  2067   2067   int cgi_is_loopback(const char *zIpAddr){
  2068   2068     return fossil_strcmp(zIpAddr, "127.0.0.1")==0 ||
  2069   2069            fossil_strcmp(zIpAddr, "::ffff:127.0.0.1")==0 ||
  2070   2070            fossil_strcmp(zIpAddr, "::1")==0;
  2071   2071   }
         2072  +
         2073  +/*
         2074  +** Return true if the HTTP request is likely to be from a small-screen
         2075  +** mobile device.
         2076  +**
         2077  +** The returned value is a guess.  Use it only for setting up defaults.
         2078  +*/
         2079  +int cgi_from_mobile(void){
         2080  +  const char *zAgent = P("HTTP_USER_AGENT");
         2081  +  if( zAgent==0 ) return 0;
         2082  +  if( sqlite3_strglob("*iPad*", zAgent)==0 ) return 0;
         2083  +  return sqlite3_strlike("%mobile%", zAgent, 0)==0;
         2084  +}

Changes to src/comformat.c.

     1      1   /*
     2      2   ** Copyright (c) 2007 D. Richard Hipp
     3      3   **
     4      4   ** This program is free software; you can redistribute it and/or
     5      5   ** modify it under the terms of the Simplified BSD License (also
     6      6   ** known as the "2-Clause License" or "FreeBSD License".)
     7         -
            7  +**
     8      8   ** This program is distributed in the hope that it will be useful,
     9      9   ** but without any warranty; without even the implied warranty of
    10     10   ** merchantability or fitness for a particular purpose.
    11     11   **
    12     12   ** Author contact information:
    13     13   **   drh@hwaci.com
    14     14   **   http://www.hwaci.com/drh/
................................................................................
    93     93     *pMaxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
    94     94     return -1;
    95     95   #endif
    96     96   }
    97     97   
    98     98   /*
    99     99   ** This function checks the current line being printed against the original
   100         -** comment text.  Upon matching, it emits a new line and updates the provided
   101         -** character and line counts, if applicable.
          100  +** comment text.  Upon matching, it updates the provided character and line
          101  +** counts, if applicable.  The caller needs to emit a new line, if desired.
   102    102   */
   103    103   static int comment_check_orig(
   104    104     const char *zOrigText, /* [in] Original comment text ONLY, may be NULL. */
   105    105     const char *zLine,     /* [in] The comment line to print. */
   106    106     int *pCharCnt,         /* [in/out] Pointer to the line character count. */
   107    107     int *pLineCnt          /* [in/out] Pointer to the total line count. */
   108    108   ){
   109    109     if( zOrigText && fossil_strcmp(zLine, zOrigText)==0 ){
   110         -    fossil_print("\n");
   111    110       if( pCharCnt ) *pCharCnt = 0;
   112    111       if( pLineCnt ) (*pLineCnt)++;
   113    112       return 1;
   114    113     }
   115    114     return 0;
   116    115   }
   117    116   
................................................................................
   119    118   ** This function scans the specified comment line starting just after the
   120    119   ** initial index and returns the index of the next spacing character -OR-
   121    120   ** zero if such a character cannot be found.  For the purposes of this
   122    121   ** algorithm, the NUL character is treated the same as a spacing character.
   123    122   */
   124    123   static int comment_next_space(
   125    124     const char *zLine, /* [in] The comment line being printed. */
   126         -  int index          /* [in] The current character index being handled. */
          125  +  int index,         /* [in] The current character index being handled. */
          126  +  int *distUTF8      /* [out] Distance to next space in UTF-8 sequences. */
   127    127   ){
   128    128     int nextIndex = index + 1;
          129  +  int fNonASCII=0;
   129    130     for(;;){
   130    131       char c = zLine[nextIndex];
          132  +    if( (c&0x80)==0x80 ) fNonASCII=1;
   131    133       if( c==0 || fossil_isspace(c) ){
          134  +      if( distUTF8 ){
          135  +        if( fNonASCII!=0 ){
          136  +          *distUTF8 = strlen_utf8(&zLine[index], nextIndex-index);
          137  +        }else{
          138  +          *distUTF8 = nextIndex-index;
          139  +        }
          140  +      }
   132    141         return nextIndex;
   133    142       }
   134    143       nextIndex++;
   135    144     }
   136    145     return 0; /* NOT REACHED */
   137    146   }
   138    147   
   139    148   /*
   140         -** This function is called when printing a logical comment line to perform
   141         -** the necessary indenting.
          149  +** Count the number of UTF-8 sequences in a string. Incomplete, ill-formed and
          150  +** overlong sequences are counted as one sequence. The invalid lead bytes 0xC0
          151  +** to 0xC1 and 0xF5 to 0xF7 are allowed to initiate (ill-formed) 2- and 4-byte
          152  +** sequences, respectively, the other invalid lead bytes 0xF8 to 0xFF are
          153  +** treated as invalid 1-byte sequences (as lone trail bytes).
          154  +** Combining characters and East Asian Wide and Fullwidth characters are counted
          155  +** as one, so this function does not calculate the effective "display width".
          156  +*/
          157  +int strlen_utf8(const char *zString, int lengthBytes){
          158  +  int i;          /* Counted bytes. */
          159  +  int lengthUTF8; /* Counted UTF-8 sequences. */
          160  +#if 0
          161  +  assert( lengthBytes>=0 );
          162  +#endif
          163  +  for(i=0, lengthUTF8=0; i<lengthBytes; i++, lengthUTF8++){
          164  +    char c = zString[i];
          165  +    int cchUTF8=1; /* Code units consumed. */
          166  +    int maxUTF8=1; /* Expected sequence length. */
          167  +    if( (c&0xe0)==0xc0 )maxUTF8=2;          /* UTF-8 lead byte 110vvvvv */
          168  +    else if( (c&0xf0)==0xe0 )maxUTF8=3;     /* UTF-8 lead byte 1110vvvv */
          169  +    else if( (c&0xf8)==0xf0 )maxUTF8=4;     /* UTF-8 lead byte 11110vvv */
          170  +    while( cchUTF8<maxUTF8 &&
          171  +            i<lengthBytes-1 &&
          172  +            (zString[i+1]&0xc0)==0x80 ){    /* UTF-8 trail byte 10vvvvvv */
          173  +      cchUTF8++;
          174  +      i++;
          175  +    }
          176  +  }
          177  +  return lengthUTF8;
          178  +}
          179  +
          180  +/*
          181  +** This function is called when printing a logical comment line to calculate
          182  +** the necessary indenting.  The caller needs to emit the indenting spaces.
   142    183   */
   143         -static void comment_print_indent(
          184  +static void comment_calc_indent(
   144    185     const char *zLine, /* [in] The comment line being printed. */
   145    186     int indent,        /* [in] Number of spaces to indent, zero for none. */
   146    187     int trimCrLf,      /* [in] Non-zero to trim leading/trailing CR/LF. */
   147    188     int trimSpace,     /* [in] Non-zero to trim leading/trailing spaces. */
   148    189     int *piIndex       /* [in/out] Pointer to first non-space character. */
   149    190   ){
   150         -  if( indent>0 ){
   151         -    fossil_print("%*s", indent, "");
   152         -  }
   153    191     if( zLine && piIndex ){
   154    192       int index = *piIndex;
   155    193       if( trimCrLf ){
   156    194         while( zLine[index]=='\r' || zLine[index]=='\n' ){ index++; }
   157    195       }
   158    196       if( trimSpace ){
   159    197         while( fossil_isspace(zLine[index]) ){ index++; }
................................................................................
   177    215     int trimCrLf,          /* [in] Non-zero to trim leading/trailing CR/LF. */
   178    216     int trimSpace,         /* [in] Non-zero to trim leading/trailing spaces. */
   179    217     int wordBreak,         /* [in] Non-zero to try breaking on word boundaries. */
   180    218     int origBreak,         /* [in] Non-zero to break before original comment. */
   181    219     int *pLineCnt,         /* [in/out] Pointer to the total line count. */
   182    220     const char **pzLine    /* [out] Pointer to the end of the logical line. */
   183    221   ){
   184         -  int index = 0, charCnt = 0, lineCnt = 0, maxChars;
          222  +  int index = 0, charCnt = 0, lineCnt = 0, maxChars, i;
          223  +  char zBuf[400]; int iBuf=0; /* Output buffer and counter. */
          224  +  int cchUTF8, maxUTF8;       /* Helper variables to count UTF-8 sequences. */
   185    225     if( !zLine ) return;
   186    226     if( lineChars<=0 ) return;
   187         -  comment_print_indent(zLine, indent, trimCrLf, trimSpace, &index);
          227  +#if 0
          228  +  assert( indent<sizeof(zBuf)-5 );       /* See following comments to explain */
          229  +  assert( origIndent<sizeof(zBuf)-5 );   /* these limits. */
          230  +#endif
          231  +  if( indent>sizeof(zBuf)-6 ){
          232  +    /* Limit initial indent to fit output buffer. */
          233  +    indent = sizeof(zBuf)-6;
          234  +  }
          235  +  comment_calc_indent(zLine, indent, trimCrLf, trimSpace, &index);
          236  +  if( indent>0 ){
          237  +    for(i=0; i<indent; i++){
          238  +      zBuf[iBuf++] = ' ';
          239  +    }
          240  +  }
          241  +  if( origIndent>sizeof(zBuf)-6 ){
          242  +    /* Limit line indent to fit output buffer. */
          243  +    origIndent = sizeof(zBuf)-6;
          244  +  }
   188    245     maxChars = lineChars;
   189    246     for(;;){
   190    247       int useChars = 1;
   191    248       char c = zLine[index];
          249  +    /* Flush the output buffer if there's no space left for at least one more
          250  +    ** (potentially 4-byte) UTF-8 sequence, one level of indentation spaces,
          251  +    ** a new line, and a terminating NULL. */
          252  +    if( iBuf>sizeof(zBuf)-origIndent-6 ){
          253  +      zBuf[iBuf]=0;
          254  +      iBuf=0;
          255  +      fossil_print("%s", zBuf);
          256  +    }
   192    257       if( c==0 ){
   193    258         break;
   194    259       }else{
   195    260         if( origBreak && index>0 ){
   196    261           const char *zCurrent = &zLine[index];
   197    262           if( comment_check_orig(zOrigText, zCurrent, &charCnt, &lineCnt) ){
   198         -          comment_print_indent(zCurrent, origIndent, trimCrLf, trimSpace,
   199         -                               &index);
          263  +          zBuf[iBuf++] = '\n';
          264  +          comment_calc_indent(zLine, origIndent, trimCrLf, trimSpace, &index);
          265  +          for( i=0; i<origIndent; i++ ){
          266  +            zBuf[iBuf++] = ' ';
          267  +          }
   200    268             maxChars = lineChars;
   201    269           }
   202    270         }
   203    271         index++;
   204    272       }
   205    273       if( c=='\n' ){
   206    274         lineCnt++;
   207    275         charCnt = 0;
   208    276         useChars = 0;
   209    277       }else if( c=='\t' ){
   210         -      int nextIndex = comment_next_space(zLine, index);
   211         -      if( nextIndex<=0 || (nextIndex-index)>maxChars ){
          278  +      int distUTF8;
          279  +      int nextIndex = comment_next_space(zLine, index, &distUTF8);
          280  +      if( nextIndex<=0 || distUTF8>maxChars ){
   212    281           break;
   213    282         }
   214    283         charCnt++;
   215    284         useChars = COMMENT_TAB_WIDTH;
   216    285         if( maxChars<useChars ){
   217         -        fossil_print(" ");
          286  +        zBuf[iBuf++] = ' ';
   218    287           break;
   219    288         }
   220    289       }else if( wordBreak && fossil_isspace(c) ){
   221         -      int nextIndex = comment_next_space(zLine, index);
   222         -      if( nextIndex<=0 || (nextIndex-index)>maxChars ){
          290  +      int distUTF8;
          291  +      int nextIndex = comment_next_space(zLine, index, &distUTF8);
          292  +      if( nextIndex<=0 || distUTF8>maxChars ){
   223    293           break;
   224    294         }
   225    295         charCnt++;
   226    296       }else{
   227    297         charCnt++;
   228    298       }
   229    299       assert( c!='\n' || charCnt==0 );
   230         -    fossil_print("%c", c);
   231         -    if( (c&0x80)==0 || (zLine[index+1]&0xc0)!=0xc0 ) maxChars -= useChars;
          300  +    zBuf[iBuf++] = c;
          301  +    /* Skip over UTF-8 sequences, see comment on strlen_utf8() for details. */
          302  +    cchUTF8=1; /* Code units consumed. */
          303  +    maxUTF8=1; /* Expected sequence length. */
          304  +    if( (c&0xe0)==0xc0 )maxUTF8=2;          /* UTF-8 lead byte 110vvvvv */
          305  +    else if( (c&0xf0)==0xe0 )maxUTF8=3;     /* UTF-8 lead byte 1110vvvv */
          306  +    else if( (c&0xf8)==0xf0 )maxUTF8=4;     /* UTF-8 lead byte 11110vvv */
          307  +    while( cchUTF8<maxUTF8 &&
          308  +            (zLine[index]&0xc0)==0x80 ){    /* UTF-8 trail byte 10vvvvvv */
          309  +      cchUTF8++;
          310  +      zBuf[iBuf++] = zLine[index++];
          311  +    }
          312  +    maxChars -= useChars;
   232    313       if( maxChars<=0 ) break;
   233    314       if( c=='\n' ) break;
   234    315     }
   235    316     if( charCnt>0 ){
   236         -    fossil_print("\n");
          317  +    zBuf[iBuf++] = '\n';
   237    318       lineCnt++;
          319  +  }
          320  +  /* Flush the remaining output buffer. */
          321  +  if( iBuf>0 ){
          322  +    zBuf[iBuf]=0;
          323  +    iBuf=0;
          324  +    fossil_print("%s", zBuf);
   238    325     }
   239    326     if( pLineCnt ){
   240    327       *pLineCnt += lineCnt;
   241    328     }
   242    329     if( pzLine ){
   243    330       *pzLine = zLine + index;
   244    331     }
................................................................................
   257    344   */
   258    345   static int comment_print_legacy(
   259    346     const char *zText, /* The comment text to be printed. */
   260    347     int indent,        /* Number of spaces to indent each non-initial line. */
   261    348     int width          /* Maximum number of characters per line. */
   262    349   ){
   263    350     int maxChars = width - indent;
   264         -  int si, sk, i, k;
          351  +  int si, sk, i, k, kc;
   265    352     int doIndent = 0;
   266    353     char *zBuf;
   267    354     char zBuffer[400];
   268    355     int lineCnt = 0;
          356  +  int cchUTF8, maxUTF8; /* Helper variables to count UTF-8 sequences. */
   269    357   
   270    358     if( width<0 ){
   271    359       comment_set_maxchars(indent, &maxChars);
   272    360     }
   273    361     if( zText==0 ) zText = "(NULL)";
   274    362     if( maxChars<=0 ){
   275    363       maxChars = strlen(zText);
   276    364     }
   277         -  if( maxChars >= (sizeof(zBuffer)) ){
   278         -    zBuf = fossil_malloc(maxChars+1);
          365  +  /* Ensure the buffer can hold the longest-possible UTF-8 sequences. */
          366  +  if( maxChars >= (sizeof(zBuffer)/4-1) ){
          367  +    zBuf = fossil_malloc(maxChars*4+1);
   279    368     }else{
   280    369       zBuf = zBuffer;
   281    370     }
   282    371     for(;;){
   283    372       while( fossil_isspace(zText[0]) ){ zText++; }
   284    373       if( zText[0]==0 ){
   285    374         if( doIndent==0 ){
   286    375           fossil_print("\n");
   287    376           lineCnt = 1;
   288    377         }
   289    378         if( zBuf!=zBuffer) fossil_free(zBuf);
   290    379         return lineCnt;
   291    380       }
   292         -    for(sk=si=i=k=0; zText[i] && k<maxChars; i++){
          381  +    for(sk=si=i=k=kc=0; zText[i] && kc<maxChars; i++){
   293    382         char c = zText[i];
   294         -      if( fossil_isspace(c) ){
          383  +      kc++; /* Count complete UTF-8 sequences. */
          384  +      /* Skip over UTF-8 sequences, see comment on strlen_utf8() for details. */
          385  +      cchUTF8=1; /* Code units consumed. */
          386  +      maxUTF8=1; /* Expected sequence length. */
          387  +      if( (c&0xe0)==0xc0 )maxUTF8=2;        /* UTF-8 lead byte 110vvvvv */
          388  +      else if( (c&0xf0)==0xe0 )maxUTF8=3;   /* UTF-8 lead byte 1110vvvv */
          389  +      else if( (c&0xf8)==0xf0 )maxUTF8=4;   /* UTF-8 lead byte 11110vvv */
          390  +      if( maxUTF8>1 ){
          391  +        zBuf[k++] = c;
          392  +        while( cchUTF8<maxUTF8 &&
          393  +                (zText[i+1]&0xc0)==0x80 ){  /* UTF-8 trail byte 10vvvvvv */
          394  +          cchUTF8++;
          395  +          zBuf[k++] = zText[++i];
          396  +        }
          397  +      }
          398  +      else if( fossil_isspace(c) ){
   295    399           si = i;
   296    400           sk = k;
   297    401           if( k==0 || zBuf[k-1]!=' ' ){
   298    402             zBuf[k++] = ' ';
   299    403           }
   300    404         }else{
   301    405           zBuf[k] = c;

Changes to src/db.c.

  3028   3028   */
  3029   3029   /*
  3030   3030   ** SETTING: backoffice-nodelay boolean default=off
  3031   3031   ** If backoffice-nodelay is true, then the backoffice processing
  3032   3032   ** will never invoke sleep().  If it has nothing useful to do,
  3033   3033   ** it simply exits.
  3034   3034   */
         3035  +/*
         3036  +** SETTING: backoffice-disable boolean default=off
         3037  +** If backoffice-disable is true, then the automatic backoffice
         3038  +** processing is disabled.  Automatic backoffice processing is the
         3039  +** backoffice work that normally runs after each web page is
         3040  +** rendered.  Backoffice processing that is triggered by the
         3041  +** "fossil backoffice" command is unaffected by this setting.
         3042  +**
         3043  +** Backoffice processing does things such as delivering
         3044  +** email notifications.  So if this setting is true, and if
         3045  +** there is no cron job periodically running "fossil backoffice",
         3046  +** email notifications and other work normally done by the
         3047  +** backoffice will not occur.
         3048  +*/
  3035   3049   /*
  3036   3050   ** SETTING: backoffice-logfile width=40
  3037   3051   ** If backoffice-logfile is not an empty string and is a valid
  3038   3052   ** filename, then a one-line message is appended to that file
  3039   3053   ** every time the backoffice runs.  This can be used for debugging,
  3040   3054   ** to ensure that backoffice is running appropriately.
  3041   3055   */
................................................................................
  3186   3200   /*
  3187   3201   ** SETTING: keep-glob        width=40 versionable block-text
  3188   3202   ** The value is a comma or newline-separated list of GLOB
  3189   3203   ** patterns specifying files that the "clean" command will keep.
  3190   3204   */
  3191   3205   /*
  3192   3206   ** SETTING: localauth        boolean default=off
  3193         -** If enabled, require that HTTP connections from
  3194         -** 127.0.0.1 be authenticated by password.  If
  3195         -** false, all HTTP requests from localhost have
  3196         -** unrestricted access to the repository.
         3207  +** If enabled, require that HTTP connections from the loopback
         3208  +** address (127.0.0.1) be authenticated by password.  If false,
         3209  +** some HTTP requests might be granted full "Setup" user
         3210  +** privileges without having to present login credentials.
         3211  +** This mechanism allows the "fossil ui" command to provide
         3212  +** full access to the repository without requiring the user to
         3213  +** log in first.
         3214  +**
         3215  +** In order for full "Setup" privilege to be granted without a
         3216  +** login, the following conditions must be met:
         3217  +**
         3218  +**   (1)  This setting ("localauth") must be off
         3219  +**   (2)  The HTTP request arrive over the loopback TCP/IP
         3220  +**        address (127.0.01) or else via SSH.
         3221  +**   (3)  The request must be HTTP, not HTTPS. (This
         3222  +**        restriction is designed to help prevent accidentally
         3223  +**        providing "Setup" privileges to requests arriving
         3224  +**        over a reverse proxy.)
         3225  +**   (4)  The command that launched the fossil server must be
         3226  +**        one of the following:
         3227  +**        (a) "fossil ui"
         3228  +**        (b) "fossil server" with the --localauth option
         3229  +**        (c) "fossil http" with the --localauth option
         3230  +**        (d) CGI with the "localauth" setting in the cgi script.
         3231  +**
         3232  +** For maximum security, set "localauth" to 1.  However, because
         3233  +** of the other restrictions (2) through (4), it should be safe
         3234  +** to leave "localauth" set to 0 in most installations, and 
         3235  +** especially on cloned repositories on workstations. Leaving
         3236  +** "localauth" at 0 makes the "fossil ui" command more convenient
         3237  +** to use.
  3197   3238   */
  3198   3239   /*
  3199   3240   ** SETTING: main-branch      width=40 default=trunk
  3200   3241   ** The value is the primary branch for the project.
  3201   3242   */
  3202   3243   /*
  3203   3244   ** SETTING: manifest         width=5 versionable

Changes to src/dispatch.c.

   263    263     int mask = CMDFLAG_1ST_TIER | CMDFLAG_2ND_TIER;
   264    264     int useHtml = find_option("html","h",0)!=0;
   265    265   
   266    266     if( find_option("www","w",0) ){
   267    267       mask = CMDFLAG_WEBPAGE;
   268    268     }
   269    269     if( find_option("everything","e",0) ){
   270         -    mask = CMDFLAG_1ST_TIER | CMDFLAG_2ND_TIER | CMDFLAG_WEBPAGE;
          270  +    mask = CMDFLAG_1ST_TIER | CMDFLAG_2ND_TIER | CMDFLAG_WEBPAGE |
          271  +              CMDFLAG_SETTING | CMDFLAG_TEST;
   271    272     }
   272    273     if( find_option("settings","s",0) ){
   273    274       mask = CMDFLAG_SETTING;
   274    275     }
   275    276     if( find_option("test","t",0) ){
   276    277       mask |= CMDFLAG_TEST;
   277    278     }
................................................................................
   363    364         const char *z = aCommand[i].zName;
   364    365         const char *zBoldOn  = aCommand[i].eCmdFlags&CMDFLAG_1ST_TIER?"<b>" :"";
   365    366         const char *zBoldOff = aCommand[i].eCmdFlags&CMDFLAG_1ST_TIER?"</b>":"";
   366    367         if( '/'==*z || strncmp(z,"test",4)==0 ) continue;
   367    368         if( (aCommand[i].eCmdFlags & CMDFLAG_SETTING)!=0 ) continue;
   368    369         @ <li><a href="%R/help?cmd=%s(z)">%s(zBoldOn)%s(z)%s(zBoldOff)</a></li>
   369    370       }
   370         -    @ </ui></div>
          371  +    @ </ul></div>
   371    372   
   372    373       @ <a name='webpages'></a>
   373    374       @ <h1>Available web UI pages:</h1>
   374    375       @ <div class="columns" style="column-width: 18ex;">
   375    376       @ <ul>
   376    377       for(i=0; i<MX_COMMAND; i++){
   377    378         const char *z = aCommand[i].zName;
................................................................................
   487    488       const char *z = aCommand[i].zName;
   488    489       if( (aCommand[i].eCmdFlags & cmdMask)==0 ) continue;
   489    490       if( zPrefix && memcmp(zPrefix, z, nPrefix)!=0 ) continue;
   490    491       aCmd[nCmd++] = aCommand[i].zName;
   491    492     }
   492    493     multi_column_list(aCmd, nCmd);
   493    494   }
          495  +
          496  +/*
          497  +** Documentation on universal command-line options.
          498  +*/
          499  +/* @-comment: # */
          500  +static const char zOptions[] =
          501  +@ Command-line options common to all commands:
          502  +@ 
          503  +@   --args FILENAME         Read additional arguments and options from FILENAME
          504  +@   --cgitrace              Active CGI tracing
          505  +@   --comfmtflags VALUE     Set comment formatting flags to VALUE
          506  +@   --errorlog FILENAME     Log errors to FILENAME 
          507  +@   --help                  Show help on the command rather than running it
          508  +@   --httptrace             Trace outbound HTTP requests
          509  +@   --localtime             Display times using the local timezone
          510  +@   --no-th-hook            Do not run TH1 hooks
          511  +@   --quiet                 Reduce the amount of output
          512  +@   --sqlstats              Show SQL usage statistics when done
          513  +@   --sqltrace              Trace all SQL commands
          514  +@   --sshtrace              Trace SSH activity
          515  +@   --ssl-identity NAME     Set the SSL identity to NAME
          516  +@   --systemtrace           Trace calls to system()
          517  +@   --user|-U USER          Make the default user be USER
          518  +@   --utc                   Display times using UTC
          519  +@   --vfs NAME              Cause SQLite to use the NAME VFS
          520  +;
   494    521   
   495    522   /*
   496    523   ** COMMAND: help
   497    524   **
   498    525   ** Usage: %fossil help TOPIC
   499    526   **    or: %fossil TOPIC --help
   500    527   **
   501    528   ** Display information on how to use TOPIC, which may be a command, webpage, or
   502    529   ** setting.  Webpage names begin with "/".  To display a list of available
   503    530   ** topics, use one of:
   504    531   **
   505    532   **    %fossil help                Show common commands
   506    533   **    %fossil help -a|--all       Show both common and auxiliary commands
          534  +**    %fossil help -o|--options   Show command-line options common to all cmds
   507    535   **    %fossil help -s|--setting   Show setting names
   508    536   **    %fossil help -t|--test      Show test commands only
   509    537   **    %fossil help -x|--aux       Show auxiliary commands only
   510    538   **    %fossil help -w|--www       Show list of webpages
   511    539   */
   512    540   void help_cmd(void){
   513    541     int rc;
................................................................................
   516    544     const char *zCmdOrPage;
   517    545     const char *zCmdOrPagePlural;
   518    546     const CmdOrPage *pCmd = 0;
   519    547     if( g.argc<3 ){
   520    548       z = g.argv[0];
   521    549       fossil_print(
   522    550         "Usage: %s help TOPIC\n"
   523         -      "Common commands:  (use \"%s help -a|--all\" for a complete list)\n",
          551  +      "Common commands:  (use \"%s help help\" for more options)\n",
   524    552         z, z);
   525    553       command_list(0, CMDFLAG_1ST_TIER);
   526    554       version_cmd();
   527    555       return;
          556  +  }
          557  +  if( find_option("options","o",0) ){
          558  +    fossil_print("%s", zOptions);
          559  +    return;
   528    560     }
   529    561     if( find_option("all","a",0) ){
   530    562       command_list(0, CMDFLAG_1ST_TIER | CMDFLAG_2ND_TIER);
   531    563       return;
   532    564     }
   533    565     else if( find_option("www","w",0) ){
   534    566       command_list(0, CMDFLAG_WEBPAGE);

Changes to src/forum.c.

   263    263     }else{
   264    264       @ <i>Deleted</i>
   265    265     }
   266    266     if( zClass ){
   267    267       @ </div>
   268    268     }
   269    269   }
          270  +
          271  +/*
          272  +** Generate the buttons in the display that allow a forum supervisor to
          273  +** mark a user as trusted.  Only do this if:
          274  +**
          275  +**   (1)  The poster is an individual, not a special user like "anonymous"
          276  +**   (2)  The current user has Forum Supervisor privilege
          277  +*/
          278  +static void generateTrustControls(Manifest *pPost){
          279  +  if( !g.perm.AdminForum ) return;
          280  +  if( login_is_special(pPost->zUser) ) return;
          281  +  @ <br>
          282  +  @ <label><input type="checkbox" name="trust">
          283  +  @ Trust user "%h(pPost->zUser)"
          284  +  @ so that future posts by "%h(pPost->zUser)" do not require moderation.
          285  +  @ </label>
          286  +  @ <input type="hidden" name="trustuser" value="%h(pPost->zUser)">
          287  +}
   270    288   
   271    289   /*
   272    290   ** Display all posts in a forum thread in chronological order
   273    291   */
   274    292   static void forum_display_chronological(int froot, int target){
   275    293     ForumThread *pThread = forumthread_create(froot, 0);
   276    294     ForumEntry *p;
................................................................................
   293    311       if( pPost->zThreadTitle ){
   294    312         @ <h1>%h(pPost->zThreadTitle)</h1>
   295    313       }
   296    314       zDate = db_text(0, "SELECT datetime(%.17g)", pPost->rDate);
   297    315       @ <p>By %h(pPost->zUser) on %h(zDate) (%d(p->fpid))
   298    316       fossil_free(zDate);
   299    317       if( p->pEdit ){
   300         -      @ edit of %z(href("%R/forumpost/%S?t",p->pEdit->zUuid))%d(p->fprev)</a>
          318  +      @ edit of %z(href("%R/forumpost/%S?t=c",p->pEdit->zUuid))%d(p->fprev)</a>
   301    319       }
   302    320       if( p->firt ){
   303    321         ForumEntry *pIrt = p->pPrev;
   304    322         while( pIrt && pIrt->fpid!=p->firt ) pIrt = pIrt->pPrev;
   305    323         if( pIrt ){
   306         -        @ reply to %z(href("%R/forumpost/%S?t",pIrt->zUuid))%d(p->firt)</a>
          324  +        @ reply to %z(href("%R/forumpost/%S?t=c",pIrt->zUuid))%d(p->firt)</a>
   307    325         }
   308    326       }
   309    327       if( p->pLeaf ){
   310         -      @ updated by %z(href("%R/forumpost/%S?t",p->pLeaf->zUuid))\
          328  +      @ updated by %z(href("%R/forumpost/%S?t=c",p->pLeaf->zUuid))\
   311    329         @ %d(p->pLeaf->fpid)</a>
   312    330       }
   313    331       if( g.perm.Debug ){
   314    332         @ <span class="debug">\
   315    333         @ <a href="%R/artifact/%h(p->zUuid)">artifact</a></span>
   316    334       }
   317    335       if( p->fpid!=target ){
   318         -      @ %z(href("%R/forumpost/%S?t",p->zUuid))[link]</a>
          336  +      @ %z(href("%R/forumpost/%S?t=c",p->zUuid))[link]</a>
   319    337       }
   320    338       isPrivate = content_is_private(p->fpid);
   321    339       sameUser = notAnon && fossil_strcmp(pPost->zUser, g.zLogin)==0;
   322    340       if( isPrivate && !g.perm.ModForum && !sameUser ){
   323    341         @ <p><span class="modpending">Awaiting Moderator Approval</span></p>
   324    342       }else{
   325    343         forum_render(0, pPost->zMimetype, pPost->zWiki, 0);
................................................................................
   338    356             @ <input type="submit" name="nullout" value="Delete">
   339    357           }
   340    358         }else if( g.perm.ModForum ){
   341    359           /* Provide moderators with moderation buttons for posts that
   342    360           ** are pending moderation */
   343    361           @ <input type="submit" name="approve" value="Approve">
   344    362           @ <input type="submit" name="reject" value="Reject">
          363  +        generateTrustControls(pPost);
   345    364         }else if( sameUser ){
   346    365           /* A post that is pending moderation can be deleted by the
   347    366           ** person who originally submitted the post */
   348    367           @ <input type="submit" name="reject" value="Delete">
   349    368         }
   350    369         @ </form></p>
   351    370       }
................................................................................
   444    463             @ <input type="submit" name="nullout" value="Delete">
   445    464           }
   446    465         }else if( g.perm.ModForum ){
   447    466           /* Provide moderators with moderation buttons for posts that
   448    467           ** are pending moderation */
   449    468           @ <input type="submit" name="approve" value="Approve">
   450    469           @ <input type="submit" name="reject" value="Reject">
          470  +        generateTrustControls(pPost);
   451    471         }else if( sameUser ){
   452    472           /* A post that is pending moderation can be deleted by the
   453    473           ** person who originally submitted the post */
   454    474           @ <input type="submit" name="reject" value="Delete">
   455    475         }
   456    476         @ </form></p>
   457    477       }
................................................................................
   469    489   ** it's entire thread.  The selected posting is enclosed within
   470    490   ** <div class='forumSel'>...</div>.  Javascript is used to move the
   471    491   ** selected posting into view after the page loads.
   472    492   **
   473    493   ** Query parameters:
   474    494   **
   475    495   **   name=X        REQUIRED.  The hash of the post to display
   476         -**   t             Show a chronologic listing instead of hierarchical
          496  +**   t=MODE        Display mode. MODE is 'c' for chronological or
          497  +**                   'h' for hierarchical, or 'a' for automatic.
   477    498   */
   478    499   void forumpost_page(void){
   479    500     forumthread_page();
   480    501   }
   481    502   
   482    503   /*
   483    504   ** WEBPAGE: forumthread
................................................................................
   485    506   ** Show all forum messages associated with a particular message thread.
   486    507   ** The result is basically the same as /forumpost except that none of
   487    508   ** the postings in the thread are selected.
   488    509   **
   489    510   ** Query parameters:
   490    511   **
   491    512   **   name=X        REQUIRED.  The hash of any post of the thread.
   492         -**   t             Show a chronologic listing instead of hierarchical
          513  +**   t=MODE        Display mode. MODE is 'c' for chronological or
          514  +**                   'h' for hierarchical, or 'a' for automatic.
   493    515   */
   494    516   void forumthread_page(void){
   495    517     int fpid;
   496    518     int froot;
   497    519     const char *zName = P("name");
          520  +  const char *zMode = PD("t","a");
   498    521     login_check_credentials();
   499    522     if( !g.perm.RdForum ){
   500    523       login_needed(g.anon.RdForum);
   501    524       return;
   502    525     }
   503    526     if( zName==0 ){
   504    527       webpage_error("Missing \"name=\" query parameter");
................................................................................
   509    532     }
   510    533     style_header("Forum");
   511    534     froot = db_int(0, "SELECT froot FROM forumpost WHERE fpid=%d", fpid);
   512    535     if( froot==0 ){
   513    536       webpage_error("Not a forum post: \"%s\"", zName);
   514    537     }
   515    538     if( fossil_strcmp(g.zPath,"forumthread")==0 ) fpid = 0;
   516         -  if( P("t") ){
   517         -    if( g.perm.Debug ){
   518         -      style_submenu_element("Hierarchical", "%R/%s/%s", g.zPath, zName);
   519         -    }                          
          539  +  if( zMode[0]=='a' ){
          540  +    if( cgi_from_mobile() ){
          541  +      zMode = "c";  /* Default to chronological on mobile */
          542  +    }else{
          543  +      zMode = "h";
          544  +    }
          545  +  }
          546  +  if( zMode[0]=='c' ){
          547  +    style_submenu_element("Hierarchical", "%R/%s/%s?t=h", g.zPath, zName);
   520    548       forum_display_chronological(froot, fpid);
   521    549     }else{
   522         -    if( g.perm.Debug ){
   523         -      style_submenu_element("Chronological", "%R/%s/%s?t", g.zPath, zName);
   524         -    }                          
          550  +    style_submenu_element("Chronological", "%R/%s/%s?t=c", g.zPath, zName);
   525    551       forum_display_hierarchical(froot, fpid);
   526    552     }
   527    553     style_load_js("forum.js");
   528    554     style_footer();
   529    555   }
   530    556   
   531    557   /*
................................................................................
   738    764     }
   739    765     if( P("preview") ){
   740    766       @ <h1>Preview:</h1>
   741    767       forum_render(zTitle, zMimetype, zContent, "forumEdit");
   742    768     }
   743    769     style_header("New Forum Thread");
   744    770     @ <form action="%R/forume1" method="POST">
   745         -  @ <h1>New Message:</h1>
          771  +  @ <h1>New Thread:</h1>
   746    772     forum_from_line();
   747    773     forum_entry_widget(zTitle, zMimetype, zContent);
   748    774     @ <input type="submit" name="preview" value="Preview">
   749    775     if( P("preview") ){
   750    776       @ <input type="submit" name="submit" value="Submit">
   751    777     }else{
   752    778       @ <input type="submit" name="submit" value="Submit" disabled>
................................................................................
   795    821     if( P("cancel") ){
   796    822       cgi_redirectf("%R/forumpost/%S",P("fpid"));
   797    823       return;
   798    824     }
   799    825     isCsrfSafe = cgi_csrf_safe(1);
   800    826     if( g.perm.ModForum && isCsrfSafe ){
   801    827       if( P("approve") ){
          828  +      const char *zUserToTrust;
   802    829         moderation_approve(fpid);
          830  +      if( g.perm.AdminForum
          831  +       && PB("trust")
          832  +       && (zUserToTrust = P("trustuser"))!=0
          833  +      ){
          834  +        db_multi_exec("UPDATE user SET cap=cap||'4' "
          835  +                      "WHERE login=%Q AND cap NOT GLOB '*4*'",
          836  +                      zUserToTrust);
          837  +      }
   803    838         cgi_redirectf("%R/forumpost/%S",P("fpid"));
   804    839         return;
   805    840       }
   806    841       if( P("reject") ){
   807    842         char *zParent = 
   808    843           db_text(0,
   809    844             "SELECT uuid FROM forumpost, blob"
................................................................................
   935    970     srchFlags = search_restrict(SRCH_FORUM);
   936    971     if( !g.perm.RdForum ){
   937    972       login_needed(g.anon.RdForum);
   938    973       return;
   939    974     }
   940    975     style_header("Forum");
   941    976     if( g.perm.WrForum ){
   942         -    style_submenu_element("New Message","%R/forumnew");
          977  +    style_submenu_element("New Thread","%R/forumnew");
          978  +  }else{
          979  +    /* Can't combine this with previous case using the ternary operator
          980  +     * because that causes an error yelling about "non-constant format"
          981  +     * with some compilers.  I can't see it, since both expressions have
          982  +     * the same format, but I'm no C spec lawyer. */
          983  +    style_submenu_element("New Thread","%R/login");
   943    984     }
   944    985     if( g.perm.ModForum && moderation_needed() ){
   945    986       style_submenu_element("Moderation Requests", "%R/modreq");
   946    987     }
   947    988     if( (srchFlags & SRCH_FORUM)!=0 ){
   948    989       if( search_screen(SRCH_FORUM, 0) ){
   949    990         style_submenu_element("Recent Threads","%R/forum");

Changes to src/http_socket.c.

   194    194     return rc;
   195    195   }
   196    196   
   197    197   /*
   198    198   ** Send content out over the open socket connection.
   199    199   */
   200    200   size_t socket_send(void *NotUsed, const void *pContent, size_t N){
   201         -  size_t sent;
          201  +  ssize_t sent;
   202    202     size_t total = 0;
   203    203     while( N>0 ){
   204    204       sent = send(iSocket, pContent, N, 0);
   205    205       if( sent<=0 ) break;
   206         -    total += sent;
   207         -    N -= sent;
          206  +    total += (size_t)sent;
          207  +    N -= (size_t)sent;
   208    208       pContent = (void*)&((char*)pContent)[sent];
   209    209     }
   210    210     return total;
   211    211   }
   212    212   
   213    213   /*
   214    214   ** Receive content back from the open socket connection.

Changes to src/info.c.

   889    889     int rid;
   890    890     Manifest *pWiki;
   891    891     char *zUuid;
   892    892     char *zDate;
   893    893     Blob wiki;
   894    894     int modPending;
   895    895     const char *zModAction;
          896  +  int tagid;
          897  +  int ridNext;
   896    898   
   897    899     login_check_credentials();
   898    900     if( !g.perm.RdWiki ){ login_needed(g.anon.RdWiki); return; }
   899    901     rid = name_to_rid_www("name");
   900    902     if( rid==0 || (pWiki = manifest_get(rid, CFTYPE_WIKI, 0))==0 ){
   901    903       style_header("Wiki Page Information Error");
   902    904       @ No such object: %h(P("name"))
................................................................................
   935    937     @ <tr><th>Artifact&nbsp;ID:</th>
   936    938     @ <td>%z(href("%R/artifact/%!S",zUuid))%s(zUuid)</a>
   937    939     if( g.perm.Setup ){
   938    940       @ (%d(rid))
   939    941     }
   940    942     modPending = moderation_pending_www(rid);
   941    943     @ </td></tr>
   942         -  @ <tr><th>Page&nbsp;Name:</th><td>%h(pWiki->zWikiTitle)</td></tr>
          944  +  @ <tr><th>Page&nbsp;Name:</th>\
          945  +  @ <td>%z(href("%R/whistory?name=%h",pWiki->zWikiTitle))\
          946  +  @ %h(pWiki->zWikiTitle)</a></td></tr>
   943    947     @ <tr><th>Date:</th><td>
   944    948     hyperlink_to_date(zDate, "</td></tr>");
   945    949     @ <tr><th>Original&nbsp;User:</th><td>
   946    950     hyperlink_to_user(pWiki->zUser, zDate, "</td></tr>");
   947    951     if( pWiki->zMimetype ){
   948    952       @ <tr><th>Mimetype:</th><td>%h(pWiki->zMimetype)</td></tr>
   949    953     }
   950    954     if( pWiki->nParent>0 ){
   951    955       int i;
   952    956       @ <tr><th>Parent%s(pWiki->nParent==1?"":"s"):</th><td>
   953    957       for(i=0; i<pWiki->nParent; i++){
   954    958         char *zParent = pWiki->azParent[i];
   955    959         @ %z(href("info/%!S",zParent))%s(zParent)</a>
          960  +      @ %z(href("%R/wdiff?id=%!S&pid=%!S",zUuid,zParent))(diff)</a>
   956    961       }
   957    962       @ </td></tr>
   958    963     }
          964  +  tagid = wiki_tagid(pWiki->zWikiTitle);
          965  +  if( tagid>0 && (ridNext = wiki_next(tagid, pWiki->rDate))>0 ){
          966  +    char *zId = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", ridNext);
          967  +    @ <tr><th>Next</th>
          968  +    @ <td>%z(href("%R/info/%!S",zId))%s(zId)</a></td>
          969  +  }
   959    970     @ </table>
   960    971   
   961    972     if( g.perm.ModWiki && modPending ){
   962    973       @ <div class="section">Moderation</div>
   963    974       @ <blockquote>
   964    975       @ <form method="POST" action="%R/winfo/%s(zUuid)">
   965    976       @ <label><input type="radio" name="modaction" value="delete">
................................................................................
  2127   2138       @ <hr />
  2128   2139       content_get(rid, &content);
  2129   2140       if( renderAsWiki ){
  2130   2141         wiki_render_by_mimetype(&content, zMime);
  2131   2142       }else if( renderAsHtml ){
  2132   2143         @ <iframe src="%R/raw/%T(blob_str(&downloadName))?name=%s(zUuid)"
  2133   2144         @ width="100%%" frameborder="0" marginwidth="0" marginheight="0"
  2134         -      @ sandbox="allow-same-origin"
  2135         -      @ onload="this.height=this.contentDocument.documentElement.scrollHeight;">
         2145  +      @ sandbox="allow-same-origin" id="ifm1">
  2136   2146         @ </iframe>
         2147  +      @ <script nonce="%h(style_nonce())">
         2148  +      @ document.getElementById("ifm1").addEventListener("load",
         2149  +      @   function(){
         2150  +      @     this.height=this.contentDocument.documentElement.scrollHeight + 75;
         2151  +      @   }
         2152  +      @ );
         2153  +      @ </script>
  2137   2154       }else{
  2138   2155         style_submenu_element("Hex", "%s/hexdump?name=%s", g.zTop, zUuid);
  2139   2156         blob_to_utf8_no_bom(&content, 0);
  2140   2157         zMime = mimetype_from_content(&content);
  2141   2158         @ <blockquote>
  2142   2159         if( zMime==0 ){
  2143   2160           const char *z;

Changes to src/main.c.

   970    970         fossil_fatal(
   971    971           "unrecognized command-line option, or missing argument: %s",
   972    972           g.argv[i]);
   973    973       }
   974    974     }
   975    975   }
   976    976   
   977         -/*
   978         -** Print a list of words in multiple columns.
   979         -*/
   980         -
   981    977   
   982    978   /*
   983    979   ** This function returns a human readable version string.
   984    980   */
   985    981   const char *get_version(){
   986    982     static const char version[] = RELEASE_VERSION " " MANIFEST_VERSION " "
   987    983                                   MANIFEST_DATE " UTC";
................................................................................
  1299   1295         db_open_repository(zRepo);
  1300   1296       }
  1301   1297     }
  1302   1298   #endif
  1303   1299     return zRepo;
  1304   1300   }
  1305   1301   
  1306         -/*
  1307         -** Generate a web-page that lists all repositories located under the
  1308         -** g.zRepositoryName directory and return non-zero.
  1309         -**
  1310         -** For the special case when g.zRepositoryName a non-chroot-jail "/",
  1311         -** compose the list using the "repo:" entries in the global_config
  1312         -** table of the configuration database.  These entries comprise all
  1313         -** of the repositories known to the "all" command.  The special case
  1314         -** processing is disallowed for chroot jails because g.zRepositoryName
  1315         -** is always "/" inside a chroot jail and so it cannot be used as a flag
  1316         -** to signal the special processing in that case.  The special case
  1317         -** processing is intended for the "fossil all ui" command which never
  1318         -** runs in a chroot jail anyhow.
  1319         -**
  1320         -** Or, if no repositories can be located beneath g.zRepositoryName,
  1321         -** return 0.
  1322         -*/
  1323         -static int repo_list_page(void){
  1324         -  Blob base;
  1325         -  int n = 0;
  1326         -  int allRepo;
  1327         -
  1328         -  assert( g.db==0 );
  1329         -  if( fossil_strcmp(g.zRepositoryName,"/")==0 && !g.fJail ){
  1330         -    /* For the special case of the "repository directory" being "/",
  1331         -    ** show all of the repositories named in the ~/.fossil database.
  1332         -    **
  1333         -    ** On unix systems, then entries are of the form "repo:/home/..."
  1334         -    ** and on Windows systems they are like on unix, starting with a "/"
  1335         -    ** or they can begin with a drive letter: "repo:C:/Users/...".  In either
  1336         -    ** case, we want returned path to omit any initial "/".
  1337         -    */
  1338         -    db_open_config(1, 0);
  1339         -    db_multi_exec(
  1340         -       "CREATE TEMP VIEW sfile AS"
  1341         -       "  SELECT ltrim(substr(name,6),'/') AS 'pathname' FROM global_config"
  1342         -       "   WHERE name GLOB 'repo:*'"
  1343         -    );
  1344         -    allRepo = 1;
  1345         -  }else{
  1346         -    /* The default case:  All repositories under the g.zRepositoryName
  1347         -    ** directory.
  1348         -    */
  1349         -    blob_init(&base, g.zRepositoryName, -1);
  1350         -    sqlite3_open(":memory:", &g.db);
  1351         -    db_multi_exec("CREATE TABLE sfile(pathname TEXT);");
  1352         -    db_multi_exec("CREATE TABLE vfile(pathname);");
  1353         -    vfile_scan(&base, blob_size(&base), 0, 0, 0);
  1354         -    db_multi_exec("DELETE FROM sfile WHERE pathname NOT GLOB '*[^/].fossil'");
  1355         -    allRepo = 0;
  1356         -  }
  1357         -  @ <html>
  1358         -  @ <head>
  1359         -  @ <base href="%s(g.zBaseURL)/" />
  1360         -  @ <meta name="viewport" content="width=device-width, initial-scale=1.0">
  1361         -  @ <title>Repository List</title>
  1362         -  @ </head>
  1363         -  @ <body>
  1364         -  n = db_int(0, "SELECT count(*) FROM sfile");
  1365         -  if( n>0 ){
  1366         -    Stmt q;
  1367         -    sqlite3_int64 iNow, iMTime;
  1368         -    @ <h1 align="center">Fossil Repositories</h1>
  1369         -    @ <table border="0" class="sortable" data-init-sort="1" \
  1370         -    @ data-column-types="tnk"><thead>
  1371         -    @ <tr><th>Filename<th width="20"><th>Last Modified</tr>
  1372         -    @ </thead><tbody>
  1373         -    db_prepare(&q, "SELECT pathname"
  1374         -                   " FROM sfile ORDER BY pathname COLLATE nocase;");
  1375         -    iNow = db_int64(0, "SELECT strftime('%%s','now')");
  1376         -    while( db_step(&q)==SQLITE_ROW ){
  1377         -      const char *zName = db_column_text(&q, 0);
  1378         -      int nName = (int)strlen(zName);
  1379         -      char *zUrl;
  1380         -      char *zAge;
  1381         -      char *zFull;
  1382         -      if( nName<7 ) continue;
  1383         -      zUrl = sqlite3_mprintf("%.*s", nName-7, zName);
  1384         -      if( zName[0]=='/'
  1385         -#ifdef _WIN32
  1386         -          || sqlite3_strglob("[a-zA-Z]:/*", zName)==0
  1387         -#endif
  1388         -      ){
  1389         -        zFull = mprintf("%s", zName);
  1390         -      }else if ( allRepo ){
  1391         -        zFull = mprintf("/%s", zName);
  1392         -      }else{
  1393         -        zFull = mprintf("%s/%s", g.zRepositoryName, zName);
  1394         -      }
  1395         -      iMTime = file_mtime(zFull, ExtFILE);
  1396         -      fossil_free(zFull);
  1397         -      if( iMTime<=0 ){
  1398         -        zAge = mprintf("...");
  1399         -      }else{
  1400         -        zAge = human_readable_age((iNow - iMTime)/86400.0);
  1401         -      }
  1402         -      if( sqlite3_strglob("*.fossil", zName)!=0 ){
  1403         -        /* The "fossil server DIRECTORY" and "fossil ui DIRECTORY" commands
  1404         -        ** do not work for repositories whose names do not end in ".fossil".
  1405         -        ** So do not hyperlink those cases. */
  1406         -        @ <tr><td>%h(zName)
  1407         -      } else if( sqlite3_strglob("*/.*", zName)==0 ){
  1408         -        /* Do not show hidden repos */
  1409         -        @ <tr><td>%h(zName) (hidden)
  1410         -      } else if( allRepo && sqlite3_strglob("[a-zA-Z]:/?*", zName)!=0 ){
  1411         -        @ <tr><td><a href="%R/%T(zUrl)/home" target="_blank">/%h(zName)</a>
  1412         -      }else{
  1413         -        @ <tr><td><a href="%R/%T(zUrl)/home" target="_blank">%h(zName)</a>
  1414         -      }
  1415         -      @ <td></td><td data-sortkey='%010llx(iNow - iMTime)'>%h(zAge)</tr>
  1416         -      fossil_free(zAge);
  1417         -      sqlite3_free(zUrl);
  1418         -    }
  1419         -    @ </tbody></table>
  1420         -  }else{
  1421         -    @ <h1>No Repositories Found</h1>
  1422         -  }
  1423         -  @ <script>%s(builtin_text("sorttable.js"))</script>
  1424         -  @ </body>
  1425         -  @ </html>
  1426         -  cgi_reply();
  1427         -  sqlite3_close(g.db);
  1428         -  g.db = 0;
  1429         -  return n;
  1430         -}
  1431         -
  1432         -/*
  1433         -** COMMAND: test-list-page
  1434         -**
  1435         -** Usage: %fossil test-list-page DIRECTORY
  1436         -**
  1437         -** Show all repositories underneath DIRECTORY.  Or if DIRECTORY is "/"
  1438         -** show all repositories in the ~/.fossil file.
  1439         -*/
  1440         -void test_list_page(void){
  1441         -  if( g.argc<3 ){
  1442         -    g.zRepositoryName = "/";
  1443         -  }else{
  1444         -    g.zRepositoryName = g.argv[2];
  1445         -  }
  1446         -  g.httpOut = stdout;
  1447         -  repo_list_page();
  1448         -}
  1449         -
  1450   1302   /*
  1451   1303   ** Called whenever a crash is encountered while processing a webpage.
  1452   1304   */
  1453   1305   void sigsegv_handler(int x){
  1454   1306   #if HAVE_BACKTRACE
  1455   1307     void *array[20];
  1456   1308     size_t size;
................................................................................
  2379   2231     noJail = find_option("nojail",0,0)!=0;
  2380   2232     allowRepoList = find_option("repolist",0,0)!=0;
  2381   2233     g.useLocalauth = find_option("localauth", 0, 0)!=0;
  2382   2234     g.sslNotAvailable = find_option("nossl", 0, 0)!=0;
  2383   2235     g.fNoHttpCompress = find_option("nocompress",0,0)!=0;
  2384   2236     zInFile = find_option("in",0,1);
  2385   2237     if( zInFile ){
         2238  +    backoffice_disable();
  2386   2239       g.httpIn = fossil_fopen(zInFile, "rb");
  2387   2240       if( g.httpIn==0 ) fossil_fatal("cannot open \"%s\" for reading", zInFile);
  2388   2241     }else{
  2389   2242       g.httpIn = stdin;
  2390   2243     }
  2391   2244     zOutFile = find_option("out",0,1);
  2392   2245     if( zOutFile ){
................................................................................
  2843   2696   **     case=6           Call webpage_assert()
  2844   2697   **     case=7           Call webpage_error()
  2845   2698   */
  2846   2699   void test_warning_page(void){
  2847   2700     int iCase = atoi(PD("case","0"));
  2848   2701     int i;
  2849   2702     login_check_credentials();
  2850         -  if( !g.perm.Setup && !g.perm.Admin ){
         2703  +  if( !g.perm.Admin ){
  2851   2704       login_needed(0);
  2852   2705       return;
  2853   2706     }
  2854   2707     style_header("Warning Test Page");
  2855   2708     style_submenu_element("Error Log","%R/errorlog");
  2856   2709     if( iCase<1 || iCase>4 ){
  2857   2710       @ <p>Generate a message to the <a href="%R/errorlog">error log</a>

Changes to src/main.mk.

   102    102     $(SRCDIR)/popen.c \
   103    103     $(SRCDIR)/pqueue.c \
   104    104     $(SRCDIR)/printf.c \
   105    105     $(SRCDIR)/publish.c \
   106    106     $(SRCDIR)/purge.c \
   107    107     $(SRCDIR)/rebuild.c \
   108    108     $(SRCDIR)/regexp.c \
          109  +  $(SRCDIR)/repolist.c \
   109    110     $(SRCDIR)/report.c \
   110    111     $(SRCDIR)/rss.c \
   111    112     $(SRCDIR)/schema.c \
   112    113     $(SRCDIR)/search.c \
   113    114     $(SRCDIR)/security_audit.c \
   114    115     $(SRCDIR)/setup.c \
   115    116     $(SRCDIR)/setupuser.c \
................................................................................
   312    313     $(OBJDIR)/popen_.c \
   313    314     $(OBJDIR)/pqueue_.c \
   314    315     $(OBJDIR)/printf_.c \
   315    316     $(OBJDIR)/publish_.c \
   316    317     $(OBJDIR)/purge_.c \
   317    318     $(OBJDIR)/rebuild_.c \
   318    319     $(OBJDIR)/regexp_.c \
          320  +  $(OBJDIR)/repolist_.c \
   319    321     $(OBJDIR)/report_.c \
   320    322     $(OBJDIR)/rss_.c \
   321    323     $(OBJDIR)/schema_.c \
   322    324     $(OBJDIR)/search_.c \
   323    325     $(OBJDIR)/security_audit_.c \
   324    326     $(OBJDIR)/setup_.c \
   325    327     $(OBJDIR)/setupuser_.c \
................................................................................
   449    451    $(OBJDIR)/popen.o \
   450    452    $(OBJDIR)/pqueue.o \
   451    453    $(OBJDIR)/printf.o \
   452    454    $(OBJDIR)/publish.o \
   453    455    $(OBJDIR)/purge.o \
   454    456    $(OBJDIR)/rebuild.o \
   455    457    $(OBJDIR)/regexp.o \
          458  + $(OBJDIR)/repolist.o \
   456    459    $(OBJDIR)/report.o \
   457    460    $(OBJDIR)/rss.o \
   458    461    $(OBJDIR)/schema.o \
   459    462    $(OBJDIR)/search.o \
   460    463    $(OBJDIR)/security_audit.o \
   461    464    $(OBJDIR)/setup.o \
   462    465    $(OBJDIR)/setupuser.o \
................................................................................
   572    575                    -DSQLITE_OMIT_LOAD_EXTENSION \
   573    576                    -DSQLITE_MAX_EXPR_DEPTH=0 \
   574    577                    -DSQLITE_USE_ALLOCA \
   575    578                    -DSQLITE_ENABLE_LOCKING_STYLE=0 \
   576    579                    -DSQLITE_DEFAULT_FILE_FORMAT=4 \
   577    580                    -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
   578    581                    -DSQLITE_ENABLE_FTS4 \
   579         -                 -DSQLITE_ENABLE_FTS3_PARENTHESIS \
   580    582                    -DSQLITE_ENABLE_DBSTAT_VTAB \
   581    583                    -DSQLITE_ENABLE_JSON1 \
   582    584                    -DSQLITE_ENABLE_FTS5 \
   583    585                    -DSQLITE_ENABLE_STMTVTAB \
   584    586                    -DSQLITE_HAVE_ZLIB \
   585    587                    -DSQLITE_INTROSPECTION_PRAGMAS \
   586    588                    -DSQLITE_ENABLE_DBPAGE_VTAB
................................................................................
   599    601                   -DSQLITE_OMIT_LOAD_EXTENSION \
   600    602                   -DSQLITE_MAX_EXPR_DEPTH=0 \
   601    603                   -DSQLITE_USE_ALLOCA \
   602    604                   -DSQLITE_ENABLE_LOCKING_STYLE=0 \
   603    605                   -DSQLITE_DEFAULT_FILE_FORMAT=4 \
   604    606                   -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
   605    607                   -DSQLITE_ENABLE_FTS4 \
   606         -                -DSQLITE_ENABLE_FTS3_PARENTHESIS \
   607    608                   -DSQLITE_ENABLE_DBSTAT_VTAB \
   608    609                   -DSQLITE_ENABLE_JSON1 \
   609    610                   -DSQLITE_ENABLE_FTS5 \
   610    611                   -DSQLITE_ENABLE_STMTVTAB \
   611    612                   -DSQLITE_HAVE_ZLIB \
   612    613                   -DSQLITE_INTROSPECTION_PRAGMAS \
   613    614                   -DSQLITE_ENABLE_DBPAGE_VTAB \
................................................................................
   784    785   	$(OBJDIR)/popen_.c:$(OBJDIR)/popen.h \
   785    786   	$(OBJDIR)/pqueue_.c:$(OBJDIR)/pqueue.h \
   786    787   	$(OBJDIR)/printf_.c:$(OBJDIR)/printf.h \
   787    788   	$(OBJDIR)/publish_.c:$(OBJDIR)/publish.h \
   788    789   	$(OBJDIR)/purge_.c:$(OBJDIR)/purge.h \
   789    790   	$(OBJDIR)/rebuild_.c:$(OBJDIR)/rebuild.h \
   790    791   	$(OBJDIR)/regexp_.c:$(OBJDIR)/regexp.h \
          792  +	$(OBJDIR)/repolist_.c:$(OBJDIR)/repolist.h \
   791    793   	$(OBJDIR)/report_.c:$(OBJDIR)/report.h \
   792    794   	$(OBJDIR)/rss_.c:$(OBJDIR)/rss.h \
   793    795   	$(OBJDIR)/schema_.c:$(OBJDIR)/schema.h \
   794    796   	$(OBJDIR)/search_.c:$(OBJDIR)/search.h \
   795    797   	$(OBJDIR)/security_audit_.c:$(OBJDIR)/security_audit.h \
   796    798   	$(OBJDIR)/setup_.c:$(OBJDIR)/setup.h \
   797    799   	$(OBJDIR)/setupuser_.c:$(OBJDIR)/setupuser.h \
................................................................................
  1555   1557   $(OBJDIR)/regexp_.c:	$(SRCDIR)/regexp.c $(OBJDIR)/translate
  1556   1558   	$(OBJDIR)/translate $(SRCDIR)/regexp.c >$@
  1557   1559   
  1558   1560   $(OBJDIR)/regexp.o:	$(OBJDIR)/regexp_.c $(OBJDIR)/regexp.h $(SRCDIR)/config.h
  1559   1561   	$(XTCC) -o $(OBJDIR)/regexp.o -c $(OBJDIR)/regexp_.c
  1560   1562   
  1561   1563   $(OBJDIR)/regexp.h:	$(OBJDIR)/headers
         1564  +
         1565  +$(OBJDIR)/repolist_.c:	$(SRCDIR)/repolist.c $(OBJDIR)/translate
         1566  +	$(OBJDIR)/translate $(SRCDIR)/repolist.c >$@
         1567  +
         1568  +$(OBJDIR)/repolist.o:	$(OBJDIR)/repolist_.c $(OBJDIR)/repolist.h $(SRCDIR)/config.h
         1569  +	$(XTCC) -o $(OBJDIR)/repolist.o -c $(OBJDIR)/repolist_.c
         1570  +
         1571  +$(OBJDIR)/repolist.h:	$(OBJDIR)/headers
  1562   1572   
  1563   1573   $(OBJDIR)/report_.c:	$(SRCDIR)/report.c $(OBJDIR)/translate
  1564   1574   	$(OBJDIR)/translate $(SRCDIR)/report.c >$@
  1565   1575   
  1566   1576   $(OBJDIR)/report.o:	$(OBJDIR)/report_.c $(OBJDIR)/report.h $(SRCDIR)/config.h
  1567   1577   	$(XTCC) -o $(OBJDIR)/report.o -c $(OBJDIR)/report_.c
  1568   1578   

Changes to src/makeheaders.c.

  1901   1901         return 0;
  1902   1902       }
  1903   1903       pFirst = pFirst->pNext;
  1904   1904     }
  1905   1905     return 1;
  1906   1906   }
  1907   1907   
         1908  +/*
         1909  +** Return TRUE if pFirst is the first token of a static assert.
         1910  +*/
         1911  +static int isStaticAssert(Token *pFirst){
         1912  +  if( (pFirst->nText==13 && strncmp(pFirst->zText, "static_assert", 13)==0)
         1913  +   || (pFirst->nText==14 && strncmp(pFirst->zText, "_Static_assert", 14)==0)
         1914  +  ){
         1915  +    return 1;
         1916  +  }else{
         1917  +    return 0;
         1918  +  }
         1919  +}
  1908   1920   
  1909   1921   /*
  1910   1922   ** This routine is called whenever we encounter a ";" or "=".  The stuff
  1911   1923   ** between pFirst and pLast constitutes either a typedef or a global
  1912   1924   ** variable definition.  Do the right thing.
  1913   1925   */
  1914   1926   static int ProcessDecl(Token *pFirst, Token *pEnd, int flags){
................................................................................
  1959   1971       }
  1960   1972       if( pFirst==0 || !isLocal ){
  1961   1973         return nErr;
  1962   1974       }
  1963   1975     }else if( flags & PS_Method ){
  1964   1976       /* Methods are declared by their class.  Don't declare separately. */
  1965   1977       return nErr;
         1978  +  }else if( isStaticAssert(pFirst) ){
         1979  +    return 0;
  1966   1980     }
  1967   1981     isVar =  (flags & (PS_Typedef|PS_Method))==0 && isVariableDef(pFirst,pEnd);
  1968   1982     if( isVar && (flags & (PS_Interface|PS_Export|PS_Local))!=0
  1969   1983     && (flags & PS_Extern)==0 ){
  1970   1984       fprintf(stderr,"%s:%d: Can't define a variable in this context\n",
  1971   1985         zFilename, pFirst->nLine);
  1972   1986       nErr++;

Changes to src/makemake.tcl.

   112    112     popen
   113    113     pqueue
   114    114     printf
   115    115     publish
   116    116     purge
   117    117     rebuild
   118    118     regexp
          119  +  repolist
   119    120     report
   120    121     rss
   121    122     schema
   122    123     search
   123    124     security_audit
   124    125     setup
   125    126     setupuser
................................................................................
   190    191     -DSQLITE_OMIT_LOAD_EXTENSION
   191    192     -DSQLITE_MAX_EXPR_DEPTH=0
   192    193     -DSQLITE_USE_ALLOCA
   193    194     -DSQLITE_ENABLE_LOCKING_STYLE=0
   194    195     -DSQLITE_DEFAULT_FILE_FORMAT=4
   195    196     -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   196    197     -DSQLITE_ENABLE_FTS4
   197         -  -DSQLITE_ENABLE_FTS3_PARENTHESIS
   198    198     -DSQLITE_ENABLE_DBSTAT_VTAB
   199    199     -DSQLITE_ENABLE_JSON1
   200    200     -DSQLITE_ENABLE_FTS5
   201    201     -DSQLITE_ENABLE_STMTVTAB
   202    202     -DSQLITE_HAVE_ZLIB
   203    203     -DSQLITE_INTROSPECTION_PRAGMAS
   204    204     -DSQLITE_ENABLE_DBPAGE_VTAB
................................................................................
   708    708   endif
   709    709   
   710    710   #### The directories where the OpenSSL include and library files are located.
   711    711   #    The recommended usage here is to use the Sysinternals junction tool
   712    712   #    to create a hard link between an "openssl-1.x" sub-directory of the
   713    713   #    Fossil source code directory and the target OpenSSL source directory.
   714    714   #
   715         -OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2p
          715  +OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2q
   716    716   OPENSSLINCDIR = $(OPENSSLDIR)/include
   717    717   OPENSSLLIBDIR = $(OPENSSLDIR)
   718    718   
   719    719   #### Either the directory where the Tcl library is installed or the Tcl
   720    720   #    source code directory resides (depending on the value of the macro
   721    721   #    FOSSIL_TCL_SOURCE).  If this points to the Tcl install directory,
   722    722   #    this directory must have "include" and "lib" sub-directories.  If
................................................................................
  1564   1564   
  1565   1565   # Enable support for the SQLite Encryption Extension?
  1566   1566   !ifndef USE_SEE
  1567   1567   USE_SEE = 0
  1568   1568   !endif
  1569   1569   
  1570   1570   !if $(FOSSIL_ENABLE_SSL)!=0
  1571         -SSLDIR    = $(B)\compat\openssl-1.0.2p
         1571  +SSLDIR    = $(B)\compat\openssl-1.0.2q
  1572   1572   SSLINCDIR = $(SSLDIR)\inc32
  1573   1573   !if $(FOSSIL_DYNAMIC_BUILD)!=0
  1574   1574   SSLLIBDIR = $(SSLDIR)\out32dll
  1575   1575   !else
  1576   1576   SSLLIBDIR = $(SSLDIR)\out32
  1577   1577   !endif
  1578   1578   SSLLFLAGS = /nologo /opt:ref /debug

Changes to src/manifest.c.

  2527   2527       char *zFType;
  2528   2528       char *zTitle;
  2529   2529       schema_forum();
  2530   2530       froot = p->zThreadRoot ? uuid_to_rid(p->zThreadRoot, 1) : rid;
  2531   2531       fprev = p->nParent ? uuid_to_rid(p->azParent[0],1) : 0;
  2532   2532       firt = p->zInReplyTo ? uuid_to_rid(p->zInReplyTo,1) : 0;
  2533   2533       db_multi_exec(
  2534         -      "INSERT INTO forumpost(fpid,froot,fprev,firt,fmtime)"
         2534  +      "REPLACE INTO forumpost(fpid,froot,fprev,firt,fmtime)"
  2535   2535         "VALUES(%d,%d,nullif(%d,0),nullif(%d,0),%.17g)",
  2536   2536         p->rid, froot, fprev, firt, p->rDate
  2537   2537       );
  2538   2538       if( firt==0 ){
  2539   2539         /* This is the start of a new thread, either the initial entry
  2540   2540         ** or an edit of the initial entry. */
  2541   2541         zTitle = p->zThreadTitle;

Changes to src/markdown.md.

    23     23   
    24     24   > 1.  **\[display text\]\(URL\)**
    25     25   > 2.  **\[display text\]\(URL "Title"\)**
    26     26   > 3.  **\[display text\]\(URL 'Title'\)**
    27     27   > 4.  **\<URL\>**
    28     28   > 5.  **\[display text\]\[label\]**
    29     29   > 6.  **\[display text\]\[\]**
           30  +> 7.  **\[display text\]**
    30     31   
    31         -> **URL** may optionally be written **\<URL\>**.  With link formats 5 and 6
           32  +> **URL** may optionally be written **\<URL\>**.  With link formats 5, 6, and 7
    32     33   > ("reference links"), the URL is supplied elsewhere in the document, as shown
    33         -> below.  Link format 6 reuses the display text as the label.  Labels are
           34  +> below.  Link formats 6 and 7 reuse the display text as the label.  Labels are
    34     35   > case-insensitive.  The title may be split onto the next line with optional
    35     36   > indenting.
    36     37   
    37     38   > * **\[label\]:&nbsp;URL**
    38     39   > * **\[label\]:&nbsp;URL&nbsp;"Title"**
    39     40   > * **\[label\]:&nbsp;URL&nbsp;'Title'**
    40     41   > * **\[label\]:&nbsp;URL&nbsp;(Title)**

Changes to src/path.c.

   190    190   PathNode *path_midpoint(void){
   191    191     PathNode *p;
   192    192     int i;
   193    193     if( path.nStep<2 ) return 0;
   194    194     for(p=path.pEnd, i=0; p && i<path.nStep/2; p=p->pFrom, i++){}
   195    195     return p;
   196    196   }
          197  +
          198  +/*
          199  +** Return an estimate of the number of comparisons remaining in order
          200  +** to bisect path.  This is based on the log2() of path.nStep.
          201  +*/
          202  +int path_search_depth(void){
          203  +  int i, j;
          204  +  for(i=0, j=1; j<path.nStep; i++, j+=j){}
          205  +  return i;
          206  +}
   197    207   
   198    208   /*
   199    209   ** Compute the shortest path between two check-ins and then transfer
   200    210   ** that path into the "ancestor" table.  This is a utility used by
   201    211   ** both /annotate and /finfo.  See also: compute_direct_ancestors().
   202    212   */
   203    213   void path_shortest_stored_in_ancestor_table(

Changes to src/printf.c.

  1000   1000     }else{
  1001   1001       out = fossil_fopen(g.zErrlog, "a");
  1002   1002       if( out==0 ) return;
  1003   1003     }
  1004   1004     now = time(0);
  1005   1005     pNow = gmtime(&now);
  1006   1006     fprintf(out, "------------- %04d-%02d-%02d %02d:%02d:%02d UTC ------------\n",
  1007         -          pNow->tm_year+1900, pNow->tm_mon+1, pNow->tm_mday+1,
         1007  +          pNow->tm_year+1900, pNow->tm_mon+1, pNow->tm_mday,
  1008   1008             pNow->tm_hour, pNow->tm_min, pNow->tm_sec);
  1009   1009     va_start(ap, zFormat);
  1010   1010     vfprintf(out, zFormat, ap);
  1011   1011     fprintf(out, "\n");
  1012   1012     va_end(ap);
  1013   1013     for(i=0; i<count(azEnv); i++){
  1014   1014       char *p;

Changes to src/regexp.c.

    14     14   **   http://www.hwaci.com/drh/
    15     15   **
    16     16   *******************************************************************************
    17     17   **
    18     18   ** This file was adapted from the test_regexp.c file in SQLite3.  That
    19     19   ** file is in the public domain.
    20     20   **
    21         -** The code in this file implements a compact but reasonably
    22         -** efficient regular-expression matcher for posix extended regular
    23         -** expressions against UTF8 text.  The following syntax is supported:
    24         -**
    25         -**     X*      zero or more occurrences of X
    26         -**     X+      one or more occurrences of X
    27         -**     X?      zero or one occurrences of X
    28         -**     X{p,q}  between p and q occurrences of X
    29         -**     (X)     match X
    30         -**     X|Y     X or Y
    31         -**     ^X      X occurring at the beginning of the string
    32         -**     X$      X occurring at the end of the string
    33         -**     .       Match any single character
    34         -**     \c      Character c where c is one of \{}()[]|*+?.
    35         -**     \c      C-language escapes for c in afnrtv.  ex: \t or \n
    36         -**     \uXXXX  Where XXXX is exactly 4 hex digits, unicode value XXXX
    37         -**     \xXX    Where XX is exactly 2 hex digits, unicode value XX
    38         -**     [abc]   Any single character from the set abc
    39         -**     [^abc]  Any single character not in the set abc
    40         -**     [a-z]   Any single character in the range a-z
    41         -**     [^a-z]  Any single character not in the range a-z
    42         -**     \b      Word boundary
    43         -**     \w      Word character.  [A-Za-z0-9_]
    44         -**     \W      Non-word character
    45         -**     \d      Digit
    46         -**     \D      Non-digit
    47         -**     \s      Whitespace character
    48         -**     \S      Non-whitespace character
    49         -**
    50         -** A nondeterministic finite automaton (NFA) is used for matching, so the
    51         -** performance is bounded by O(N*M) where N is the size of the regular
    52         -** expression and M is the size of the input string.  The matcher never
    53         -** exhibits exponential behavior.  Note that the X{p,q} operator expands
    54         -** to p copies of X following by q-p copies of X? and that the size of the
    55         -** regular expression in the O(N*M) performance bound is computed after
    56         -** this expansion.
           21  +** See ../www/grep.md for details of the algorithm and RE dialect.
    57     22   */
    58     23   #include "config.h"
    59     24   #include "regexp.h"
    60     25   
    61     26   /* The end-of-input character */
    62     27   #define RE_EOF            0    /* End of input */
    63     28   
................................................................................
   825    790   }
   826    791   
   827    792   /*
   828    793   ** COMMAND: grep
   829    794   **
   830    795   ** Usage: %fossil grep [OPTIONS] PATTERN FILENAME
   831    796   **
   832         -** Run grep over all historic version of FILENAME
          797  +** Attempt to match the given POSIX extended regular expression PATTERN
          798  +** over all historic versions of FILENAME.  For details of the supported
          799  +** RE dialect, see https://fossil-scm.org/fossil/doc/trunk/www/grep.md
   833    800   **
   834    801   ** Options:
   835    802   **
   836    803   **     -i|--ignore-case         Ignore case
   837         -**     -l|--files-with-matches  Print only filenames that match
          804  +**     -l|--files-with-matches  List only checkin ID for versions that match
   838    805   **     -v|--verbose             Show each file as it is analyzed
   839    806   */
   840    807   void re_grep_cmd(void){
   841    808     u32 flags = 0;
   842    809     int bVerbose = 0;
   843    810     ReCompiled *pRe;
   844    811     const char *zErr;
................................................................................
   846    813     Blob fullName;
   847    814   
   848    815     if( find_option("ignore-case","i",0)!=0 ) ignoreCase = 1;
   849    816     if( find_option("files-with-matches","l",0)!=0 ) flags |= GREP_EXISTS;
   850    817     if( find_option("verbose","v",0)!=0 ) bVerbose = 1;
   851    818     db_find_and_open_repository(0, 0);
   852    819     verify_all_options();
   853         -  if( g.argc<3 ){
          820  +  if( g.argc<4 ){
   854    821       usage("REGEXP FILENAME");
   855    822     }
   856    823     zErr = re_compile(&pRe, g.argv[2], ignoreCase);
   857    824     if( zErr ) fossil_fatal("%s", zErr);
   858    825   
   859    826     if( file_tree_name(g.argv[3], &fullName, 0, 0) ){
   860    827       int fnid = db_int(0, "SELECT fnid FROM filename WHERE name=%Q",

Added src/repolist.c.

            1  +/*
            2  +** Copyright (c) 2018 D. Richard Hipp
            3  +**
            4  +** This program is free software; you can redistribute it and/or
            5  +** modify it under the terms of the Simplified BSD License (also
            6  +** known as the "2-Clause License" or "FreeBSD License".)
            7  +**
            8  +** This program is distributed in the hope that it will be useful,
            9  +** but without any warranty; without even the implied warranty of
           10  +** merchantability or fitness for a particular purpose.
           11  +**
           12  +** Author contact information:
           13  +**   drh@hwaci.com
           14  +**   http://www.hwaci.com/drh/
           15  +**
           16  +*******************************************************************************
           17  +**
           18  +** This module contains code to implement the repository list page when
           19  +** "fossil server" or "fossil ui" is serving a directory full of repositories.
           20  +*/
           21  +#include "config.h"
           22  +#include "repolist.h"
           23  +
           24  +#if INTERFACE
           25  +/*
           26  +** Return value from the remote_repo_info() command.  zRepoName is the
           27  +** input.  All other fields are outputs.
           28  +*/
           29  +struct RepoInfo {
           30  +  char *zRepoName;      /* Name of the repository file */
           31  +  int isValid;          /* True if zRepoName is a valid Fossil repository */
           32  +  char *zProjName;      /* Project Name.  Memory from fossil_malloc() */
           33  +  double rMTime;        /* Last update.  Julian day number */
           34  +};
           35  +#endif
           36  +
           37  +/*
           38  +** Discover information about the repository given by
           39  +** pRepo->zRepoName.
           40  +*/
           41  +void remote_repo_info(RepoInfo *pRepo){
           42  +  sqlite3 *db;
           43  +  sqlite3_stmt *pStmt;
           44  +  int rc;
           45  +
           46  +  pRepo->isValid = 0;
           47  +  pRepo->zProjName = 0;
           48  +  pRepo->rMTime = 0.0;
           49  +
           50  +  rc = sqlite3_open(pRepo->zRepoName, &db);
           51  +  if( rc ) return;
           52  +  rc = sqlite3_prepare_v2(db, "SELECT value FROM config"
           53  +                              " WHERE name='project-name'",
           54  +                          -1, &pStmt, 0);
           55  +  if( rc ) return;
           56  +  if( sqlite3_step(pStmt)==SQLITE_ROW ){
           57  +    pRepo->zProjName = fossil_strdup((char*)sqlite3_column_text(pStmt,0));
           58  +  }
           59  +  sqlite3_finalize(pStmt);
           60  +  rc = sqlite3_prepare_v2(db, "SELECT max(mtime) FROM event", -1, &pStmt, 0);
           61  +  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
           62  +    pRepo->rMTime = sqlite3_column_double(pStmt,0);
           63  +  }
           64  +  sqlite3_finalize(pStmt);
           65  +  sqlite3_close(db);
           66  +  pRepo->isValid = 1;
           67  +}
           68  +
           69  +/*
           70  +** Generate a web-page that lists all repositories located under the
           71  +** g.zRepositoryName directory and return non-zero.
           72  +**
           73  +** For the special case when g.zRepositoryName is a non-chroot-jail "/",
           74  +** compose the list using the "repo:" entries in the global_config
           75  +** table of the configuration database.  These entries comprise all
           76  +** of the repositories known to the "all" command.  The special case
           77  +** processing is disallowed for chroot jails because g.zRepositoryName
           78  +** is always "/" inside a chroot jail and so it cannot be used as a flag
           79  +** to signal the special processing in that case.  The special case
           80  +** processing is intended for the "fossil all ui" command which never
           81  +** runs in a chroot jail anyhow.
           82  +**
           83  +** Or, if no repositories can be located beneath g.zRepositoryName,
           84  +** return 0.
           85  +*/
           86  +int repo_list_page(void){
           87  +  Blob base;
           88  +  int n = 0;
           89  +  int allRepo;
           90  +
           91  +  assert( g.db==0 );
           92  +  if( fossil_strcmp(g.zRepositoryName,"/")==0 && !g.fJail ){
           93  +    /* For the special case of the "repository directory" being "/",
           94  +    ** show all of the repositories named in the ~/.fossil database.
           95  +    **
           96  +    ** On unix systems, then entries are of the form "repo:/home/..."
           97  +    ** and on Windows systems they are like on unix, starting with a "/"
           98  +    ** or they can begin with a drive letter: "repo:C:/Users/...".  In either
           99  +    ** case, we want returned path to omit any initial "/".
          100  +    */
          101  +    db_open_config(1, 0);
          102  +    db_multi_exec(
          103  +       "CREATE TEMP VIEW sfile AS"
          104  +       "  SELECT ltrim(substr(name,6),'/') AS 'pathname' FROM global_config"
          105  +       "   WHERE name GLOB 'repo:*'"
          106  +    );
          107  +    allRepo = 1;
          108  +  }else{
          109  +    /* The default case:  All repositories under the g.zRepositoryName
          110  +    ** directory.
          111  +    */
          112  +    blob_init(&base, g.zRepositoryName, -1);
          113  +    sqlite3_open(":memory:", &g.db);
          114  +    db_multi_exec("CREATE TABLE sfile(pathname TEXT);");
          115  +    db_multi_exec("CREATE TABLE vfile(pathname);");
          116  +    vfile_scan(&base, blob_size(&base), 0, 0, 0);
          117  +    db_multi_exec("DELETE FROM sfile WHERE pathname NOT GLOB '*[^/].fossil'");
          118  +    allRepo = 0;
          119  +  }
          120  +  @ <html>
          121  +  @ <head>
          122  +  @ <base href="%s(g.zBaseURL)/" />
          123  +  @ <meta name="viewport" content="width=device-width, initial-scale=1.0">
          124  +  @ <title>Repository List</title>
          125  +  @ </head>
          126  +  @ <body>
          127  +  n = db_int(0, "SELECT count(*) FROM sfile");
          128  +  if( n>0 ){
          129  +    Stmt q;
          130  +    double rNow;
          131  +    @ <h1 align="center">Fossil Repositories</h1>
          132  +    @ <table border="0" class="sortable" data-init-sort="1" \
          133  +    @ data-column-types="tntnk"><thead>
          134  +    @ <tr><th>Filename<th width="20">\
          135  +    @ <th>Project Name<th width="20">\
          136  +    @ <th>Last Modified</tr>
          137  +    @ </thead><tbody>
          138  +    db_prepare(&q, "SELECT pathname"
          139  +                   " FROM sfile ORDER BY pathname COLLATE nocase;");
          140  +    rNow = db_double(0, "SELECT julianday('now')");
          141  +    while( db_step(&q)==SQLITE_ROW ){
          142  +      const char *zName = db_column_text(&q, 0);
          143  +      int nName = (int)strlen(zName);
          144  +      char *zUrl;
          145  +      char *zAge;
          146  +      char *zFull;
          147  +      RepoInfo x;
          148  +      int iAge;
          149  +      if( nName<7 ) continue;
          150  +      zUrl = sqlite3_mprintf("%.*s", nName-7, zName);
          151  +      if( zName[0]=='/'
          152  +#ifdef _WIN32
          153  +          || sqlite3_strglob("[a-zA-Z]:/*", zName)==0
          154  +#endif
          155  +      ){
          156  +        zFull = mprintf("%s", zName);
          157  +      }else if ( allRepo ){
          158  +        zFull = mprintf("/%s", zName);
          159  +      }else{
          160  +        zFull = mprintf("%s/%s", g.zRepositoryName, zName);
          161  +      }
          162  +      x.zRepoName = zFull;
          163  +      remote_repo_info(&x);
          164  +      fossil_free(zFull);
          165  +      if( !x.isValid ){
          166  +        zAge = mprintf("...");
          167  +        iAge = 0;
          168  +      }else{
          169  +        iAge = (rNow - x.rMTime)*86400;
          170  +        if( iAge<0 ) x.rMTime = rNow;
          171  +        zAge = human_readable_age(rNow - x.rMTime);
          172  +      }
          173  +      @ <tr><td valign="top">\
          174  +      if( sqlite3_strglob("*.fossil", zName)!=0 ){
          175  +        /* The "fossil server DIRECTORY" and "fossil ui DIRECTORY" commands
          176  +        ** do not work for repositories whose names do not end in ".fossil".
          177  +        ** So do not hyperlink those cases. */
          178  +        @ %h(zName)
          179  +      } else if( sqlite3_strglob("*/.*", zName)==0 ){
          180  +        /* Do not show hidden repos */
          181  +        @ %h(zName) (hidden)
          182  +      } else if( allRepo && sqlite3_strglob("[a-zA-Z]:/?*", zName)!=0 ){
          183  +        @ <a href="%R/%T(zUrl)/home" target="_blank">/%h(zName)</a>
          184  +      }else{
          185  +        @ <a href="%R/%T(zUrl)/home" target="_blank">%h(zName)</a>
          186  +      }
          187  +      if( x.zProjName ){
          188  +        @ <td></td><td>%h(x.zProjName)</td>
          189  +        fossil_free(x.zProjName);
          190  +      }else{
          191  +        @ <td></td><td></td>
          192  +      }
          193  +      @ <td></td><td data-sortkey='%08x(iAge)'>%h(zAge)</tr>
          194  +      fossil_free(zAge);
          195  +      sqlite3_free(zUrl);
          196  +    }
          197  +    @ </tbody></table>
          198  +  }else{
          199  +    @ <h1>No Repositories Found</h1>
          200  +  }
          201  +  @ <script>%s(builtin_text("sorttable.js"))</script>
          202  +  @ </body>
          203  +  @ </html>
          204  +  cgi_reply();
          205  +  sqlite3_close(g.db);
          206  +  g.db = 0;
          207  +  return n;
          208  +}
          209  +
          210  +/*
          211  +** COMMAND: test-list-page
          212  +**
          213  +** Usage: %fossil test-list-page DIRECTORY
          214  +**
          215  +** Show all repositories underneath DIRECTORY.  Or if DIRECTORY is "/"
          216  +** show all repositories in the ~/.fossil file.
          217  +*/
          218  +void test_list_page(void){
          219  +  if( g.argc<3 ){
          220  +    g.zRepositoryName = "/";
          221  +  }else{
          222  +    g.zRepositoryName = g.argv[2];
          223  +  }
          224  +  g.httpOut = stdout;
          225  +  repo_list_page();
          226  +}

Changes to src/security_audit.c.

    40     40   **
    41     41   ** Run a security audit of the current Fossil setup.
    42     42   ** This page requires administrator access
    43     43   */
    44     44   void secaudit0_page(void){
    45     45     const char *zAnonCap;      /* Capabilities of user "anonymous" and "nobody" */
    46     46     const char *zPubPages;     /* GLOB pattern for public pages */
           47  +  const char *zSelfCap;      /* Capabilities of self-registered users */
    47     48     char *z;
    48     49     int n;
    49     50   
    50     51     login_check_credentials();
    51         -  if( !g.perm.Setup && !g.perm.Admin ){
           52  +  if( !g.perm.Admin ){
    52     53       login_needed(0);
    53     54       return;
    54     55     }
    55     56     style_header("Security Audit");
    56     57     @ <ol>
    57     58   
    58     59     /* Step 1:  Determine if the repository is public or private.  "Public"
    59     60     ** means that any anonymous user on the internet can access all content.
    60     61     ** "Private" repos require (non-anonymous) login to access all content,
    61     62     ** though some content may be accessible anonymously.
    62     63     */
    63     64     zAnonCap = db_text("", "SELECT fullcap(NULL)");
    64     65     zPubPages = db_get("public-pages",0);
           66  +  if( db_get_boolean("self-register",0) ){
           67  +    CapabilityString *pCap;
           68  +    pCap = capability_add(0, db_get("default-perms",""));
           69  +    capability_expand(pCap);
           70  +    zSelfCap = capability_string(pCap);
           71  +    capability_free(pCap);
           72  +  }else{
           73  +    zSelfCap = fossil_strdup("");
           74  +  }
    65     75     if( hasAnyCap(zAnonCap,"as") ){
    66     76       @ <li><p>This repository is <big><b>Wildly INSECURE</b></big> because
    67     77       @ it grants administrator privileges to anonymous users.  You
    68     78       @ should <a href="takeitprivate">take this repository private</a>
    69     79       @ immediately!  Or, at least remove the Setup and Admin privileges
    70     80       @ for users "anonymous" and "login" on the
    71     81       @ <a href="setup_ulist">User Configuration</a> page.
           82  +  }else if( hasAnyCap(zSelfCap,"as") ){
           83  +    @ <li><p>This repository is <big><b>Wildly INSECURE</b></big> because
           84  +    @ it grants administrator privileges to self-registered users.  You
           85  +    @ should <a href="takeitprivate">take this repository private</a>
           86  +    @ and/or disable self-registration
           87  +    @ immediately!  Or, at least remove the Setup and Admin privileges
           88  +    @ from the default permissions for new users.
    72     89     }else if( hasAnyCap(zAnonCap,"y") ){
    73     90       @ <li><p>This repository is <big><b>INSECURE</b></big> because
    74     91       @ it allows anonymous users to push unversioned files.
    75     92       @ <p>Fix this by <a href="takeitprivate">taking the repository private</a>
    76     93       @ or by removing the "y" permission from users "anonymous" and
    77     94       @ "nobody" on the <a href="setup_ulist">User Configuration</a> page.
           95  +  }else if( hasAnyCap(zSelfCap,"y") ){
           96  +    @ <li><p>This repository is <big><b>INSECURE</b></big> because
           97  +    @ it allows self-registered users to push unversioned files.
           98  +    @ <p>Fix this by <a href="takeitprivate">taking the repository private</a>
           99  +    @ or by removing the "y" permission from the default permissions or
          100  +    @ by disabling self-registration.
    78    101     }else if( hasAnyCap(zAnonCap,"goz") ){
    79    102       @ <li><p>This repository is <big><b>PUBLIC</b></big>. All
    80    103       @ checked-in content can be accessed by anonymous users.
    81    104       @ <a href="takeitprivate">Take it private</a>.<p>
          105  +  }else if( hasAnyCap(zSelfCap,"goz") ){
          106  +    @ <li><p>This repository is <big><b>PUBLIC</b></big> because all
          107  +    @ checked-in content can be accessed by self-registered users.
          108  +    @ This repostory would be private if you disabled self-registration.</p>
    82    109     }else if( !hasAnyCap(zAnonCap, "jrwy234567")
          110  +         && !hasAnyCap(zSelfCap, "jrwy234567")
    83    111            && (zPubPages==0 || zPubPages[0]==0) ){
    84    112       @ <li><p>This repository is <big><b>Completely PRIVATE</b></big>.
    85    113       @ A valid login and password is required to access any content.
    86    114     }else{
    87    115       @ <li><p>This repository is <big><b>Mostly PRIVATE</b></big>.
    88    116       @ A valid login and password is usually required, however some
    89         -    @ content can be accessed anonymously:
          117  +    @ content can be accessed either anonymously or by self-registered
          118  +    @ users:
    90    119       @ <ul>
    91         -    if( hasAnyCap(zAnonCap,"j") ){
          120  +    if( hasAnyCap(zAnonCap,"j") || hasAnyCap(zSelfCap,"j") ){
    92    121         @ <li> Wiki pages
    93    122       }
    94         -    if( hasAnyCap(zAnonCap,"r") ){
          123  +    if( hasAnyCap(zAnonCap,"r") || hasAnyCap(zSelfCap,"r") ){
    95    124         @ <li> Tickets
    96    125       }
    97         -    if( hasAnyCap(zAnonCap,"234567") ){
          126  +    if( hasAnyCap(zAnonCap,"234567") || hasAnyCap(zSelfCap,"234567") ){
    98    127         @ <li> Forum posts
    99    128       }
   100    129       if( zPubPages && zPubPages[0] ){
   101    130         Glob *pGlob = glob_create(zPubPages);
   102    131         int i;
   103    132         @ <li> URLs that match any of these GLOB patterns:
   104    133         @ <ul>
................................................................................
   408    437   /*
   409    438   ** WEBPAGE: takeitprivate
   410    439   **
   411    440   ** Disable anonymous access to this website
   412    441   */
   413    442   void takeitprivate_page(void){
   414    443     login_check_credentials();
   415         -  if( !g.perm.Setup && !g.perm.Admin ){
          444  +  if( !g.perm.Admin ){
   416    445       login_needed(0);
   417    446       return;
   418    447     }
   419    448     if( P("cancel") ){
   420    449       /* User pressed the cancel button.  Go back */
   421    450       cgi_redirect("secaudit0");
   422    451     }
   423    452     if( P("apply") ){
   424    453       db_multi_exec(
   425    454         "UPDATE user SET cap=''"
   426    455         " WHERE login IN ('nobody','anonymous');"
   427    456         "DELETE FROM config WHERE name='public-pages';"
   428    457       );
          458  +    db_set("self-register","0",0);
   429    459       cgi_redirect("secaudit0");
   430    460     }
   431    461     style_header("Make This Website Private");
   432    462     @ <p>Click the "Make It Private" button below to disable all
   433    463     @ anonymous access to this repository.  A valid login and password
   434    464     @ will be required to access this repository after clicking that
   435    465     @ button.</p>
................................................................................
   456    486   ** this page.
   457    487   */
   458    488   void errorlog_page(void){
   459    489     i64 szFile;
   460    490     FILE *in;
   461    491     char z[10000];
   462    492     login_check_credentials();
   463         -  if( !g.perm.Setup && !g.perm.Admin ){
          493  +  if( !g.perm.Admin ){
   464    494       login_needed(0);
   465    495       return;
   466    496     }
   467    497     style_header("Server Error Log");
   468    498     style_submenu_element("Test", "%R/test-warning");
   469    499     style_submenu_element("Refresh", "%R/errorlog");
   470    500     if( g.zErrlog==0 || fossil_strcmp(g.zErrlog,"-")==0 ){

Changes to src/setup.c.

    57     57   }
    58     58   
    59     59   
    60     60   
    61     61   /*
    62     62   ** WEBPAGE: setup
    63     63   **
    64         -** Main menu for the administrative pages.  Requires Admin privileges.
           64  +** Main menu for the administrative pages.  Requires Admin or Setup
           65  +** privileges.  Links to sub-pages only usable by Setup users are
           66  +** shown only to Setup users.
    65     67   */
    66     68   void setup_page(void){
           69  +  int setup_user = 0;
    67     70     login_check_credentials();
    68         -  if( !g.perm.Setup ){
           71  +  if( !g.perm.Admin ){
    69     72       login_needed(0);
    70     73     }
           74  +  setup_user = g.perm.Setup;
    71     75   
    72     76     style_header("Server Administration");
    73     77   
    74     78     /* Make sure the header contains <base href="...">.   Issue a warning
    75     79     ** if it does not. */
    76     80     if( !cgi_header_contains("<base href=") ){
    77     81       @ <p class="generalError"><b>Configuration Error:</b> Please add
................................................................................
    93     97       @ by SQLite will be poorly seeded.</p>
    94     98     }
    95     99   #endif
    96    100   
    97    101     @ <table border="0" cellspacing="3">
    98    102     setup_menu_entry("Users", "setup_ulist",
    99    103       "Grant privileges to individual users.");
   100         -  setup_menu_entry("Access", "setup_access",
   101         -    "Control access settings.");
   102         -  setup_menu_entry("Configuration", "setup_config",
   103         -    "Configure the WWW components of the repository");
          104  +  if( setup_user ){
          105  +    setup_menu_entry("Access", "setup_access",
          106  +      "Control access settings.");
          107  +    setup_menu_entry("Configuration", "setup_config",
          108  +      "Configure the WWW components of the repository");
          109  +  }
   104    110     setup_menu_entry("Security-Audit", "secaudit0",
   105    111       "Analyze the current configuration for security problems");
   106         -  setup_menu_entry("Settings", "setup_settings",
   107         -    "Web interface to the \"fossil settings\" command");
          112  +  if( setup_user ){
          113  +    setup_menu_entry("Settings", "setup_settings",
          114  +      "Web interface to the \"fossil settings\" command");
          115  +  }
   108    116     setup_menu_entry("Timeline", "setup_timeline",
   109    117       "Timeline display preferences");
   110         -  setup_menu_entry("Login-Group", "setup_login_group",
   111         -    "Manage single sign-on between this repository and others"
   112         -    " on the same server");
   113         -  setup_menu_entry("Tickets", "tktsetup",
   114         -    "Configure the trouble-ticketing system for this repository");
          118  +  if( setup_user ){
          119  +    setup_menu_entry("Login-Group", "setup_login_group",
          120  +      "Manage single sign-on between this repository and others"
          121  +      " on the same server");
          122  +    setup_menu_entry("Tickets", "tktsetup",
          123  +      "Configure the trouble-ticketing system for this repository");
          124  +    setup_menu_entry("Wiki", "setup_wiki",
          125  +      "Configure the wiki for this repository");
          126  +  }
   115    127     setup_menu_entry("Search","srchsetup",
   116    128       "Configure the built-in search engine");
   117    129     setup_menu_entry("URL Aliases", "waliassetup",
   118    130       "Configure URL aliases");
   119         -  setup_menu_entry("Notification", "setup_notification",
   120         -    "Automatic notifications of changes via outbound email");
   121         -  setup_menu_entry("Email-Server", "setup_smtp",
   122         -    "Activate and configure the built-in email server");
   123         -  setup_menu_entry("Transfers", "xfersetup",
   124         -    "Configure the transfer system for this repository");
          131  +  if( setup_user ){
          132  +    setup_menu_entry("Notification", "setup_notification",
          133  +      "Automatic notifications of changes via outbound email");
          134  +    setup_menu_entry("Email-Server", "setup_smtp",
          135  +      "Activate and configure the built-in email server");
          136  +    setup_menu_entry("Transfers", "xfersetup",
          137  +      "Configure the transfer system for this repository");
          138  +  }
   125    139     setup_menu_entry("Skins", "setup_skin",
   126    140       "Select and/or modify the web interface \"skins\"");
   127    141     setup_menu_entry("Moderation", "setup_modreq",
   128    142       "Enable/Disable requiring moderator approval of Wiki and/or Ticket"
   129    143       " changes and attachments.");
   130    144     setup_menu_entry("Ad-Unit", "setup_adunit",
   131    145       "Edit HTML text for an ad unit inserted after the menu bar");
   132    146     setup_menu_entry("URLs & Checkouts", "urllist",
   133    147       "Show URLs used to access this repo and known check-outs");
   134         -  setup_menu_entry("Web-Cache", "cachestat",
   135         -    "View the status of the expensive-page cache");
          148  +  if( setup_user ){
          149  +    setup_menu_entry("Web-Cache", "cachestat",
          150  +      "View the status of the expensive-page cache");
          151  +  }
   136    152     setup_menu_entry("Logo", "setup_logo",
   137    153       "Change the logo and background images for the server");
   138    154     setup_menu_entry("Shunned", "shun",
   139    155       "Show artifacts that are shunned by this repository");
   140    156     setup_menu_entry("Artifact Receipts Log", "rcvfromlist",
   141    157       "A record of received artifacts and their sources");
   142    158     setup_menu_entry("User Log", "access_log",
................................................................................
   147    163       "View the Fossil server error log");
   148    164     setup_menu_entry("Unversioned Files", "uvlist?byage=1",
   149    165       "Show all unversioned files held");
   150    166     setup_menu_entry("Stats", "stat",
   151    167       "Repository Status Reports");
   152    168     setup_menu_entry("Sitemap", "sitemap",
   153    169       "Links to miscellaneous pages");
   154         -  setup_menu_entry("SQL", "admin_sql",
   155         -    "Enter raw SQL commands");
   156         -  setup_menu_entry("TH1", "admin_th1",
   157         -    "Enter raw TH1 commands");
          170  +  if( setup_user ){
          171  +    setup_menu_entry("SQL", "admin_sql",
          172  +      "Enter raw SQL commands");
          173  +    setup_menu_entry("TH1", "admin_th1",
          174  +      "Enter raw TH1 commands");
          175  +  }
   158    176     @ </table>
   159    177   
   160    178     style_footer();
   161    179   }
   162    180   
   163    181   /*
   164    182   ** Generate a checkbox for an attribute.
................................................................................
   289    307     @ </select> <b>%h(zLabel)</b>
   290    308   }
   291    309   
   292    310   
   293    311   /*
   294    312   ** WEBPAGE: setup_access
   295    313   **
   296         -** The access-control settings page.  Requires Admin privileges.
          314  +** The access-control settings page.  Requires Setup privileges.
   297    315   */
   298    316   void setup_access(void){
   299    317     login_check_credentials();
   300    318     if( !g.perm.Setup ){
   301    319       login_needed(0);
   302    320       return;
   303    321     }
................................................................................
   645    663         "0", "HH:MM",
   646    664         "1", "HH:MM:SS",
   647    665         "2", "YYYY-MM-DD HH:MM",
   648    666         "3", "YYMMDD HH:MM",
   649    667         "4", "(off)"
   650    668     };
   651    669     login_check_credentials();
   652         -  if( !g.perm.Setup ){
          670  +  if( !g.perm.Admin ){
   653    671       login_needed(0);
   654    672       return;
   655    673     }
   656    674   
   657    675     style_header("Timeline Display Preferences");
   658    676     db_begin_transaction();
   659    677     @ <form action="%s(g.zTop)/setup_timeline" method="post"><div>
................................................................................
   724    742     style_footer();
   725    743   }
   726    744   
   727    745   /*
   728    746   ** WEBPAGE: setup_settings
   729    747   **
   730    748   ** Change or view miscellaneous settings.  Part of the
   731         -** Admin pages requiring Admin privileges.
          749  +** /setup pages requiring Setup privileges.
   732    750   */
   733    751   void setup_settings(void){
   734    752     int nSetting;
   735    753     int i;
   736    754     Setting const *pSet;
   737    755     const Setting *aSetting = setting_info(&nSetting);
   738    756   
................................................................................
   811    829     db_end_transaction(0);
   812    830     style_footer();
   813    831   }
   814    832   
   815    833   /*
   816    834   ** WEBPAGE: setup_config
   817    835   **
   818         -** The "Admin/Configuration" page.  Requires Admin privilege.
          836  +** The "Admin/Configuration" page.  Requires Setup privilege.
   819    837   */
   820    838   void setup_config(void){
   821    839     login_check_credentials();
   822    840     if( !g.perm.Setup ){
   823    841       login_needed(0);
   824    842       return;
   825    843     }
................................................................................
   856    874     @ a convenient place for newbies
   857    875     @ to download a ZIP archive or a tarball of the project.  By default,
   858    876     @ the latest trunk check-in is downloaded.  Change this tag to something
   859    877     @ else (ex: release) to alter the behavior of the /download page.
   860    878     @ (Property: "download-tag")
   861    879     @ </p>
   862    880     @ <hr />
   863         -  onoff_attribute("Enable WYSIWYG Wiki Editing",
   864         -                  "wysiwyg-wiki", "wysiwyg-wiki", 0, 0);
   865         -  @ <p>Enable what-you-see-is-what-you-get (WYSIWYG) editing of wiki pages.
   866         -  @ The WYSIWYG editor generates HTML instead of markup, which makes
   867         -  @ subsequent manual editing more difficult.
   868         -  @ (Property: "wysiwyg-wiki")</p>
   869         -  @ <hr />
   870    881     entry_attribute("Index Page", 60, "index-page", "idxpg", "/home", 0);
   871    882     @ <p>Enter the pathname of the page to display when the "Home" menu
   872    883     @ option is selected and when no pathname is
   873    884     @ specified in the URL.  For example, if you visit the url:</p>
   874    885     @
   875    886     @ <blockquote><p>%h(g.zBaseURL)</p></blockquote>
   876    887     @
................................................................................
   904    915     @ (Property: sitemap-download)<br>
   905    916     entry_attribute("License", 40, "sitemap-license", "smlicense",
   906    917                     "", 0);
   907    918     @ (Property: sitemap-license)<br>
   908    919     entry_attribute("Contact", 40, "sitemap-contact", "smcontact",
   909    920                     "", 0);
   910    921     @ (Property: sitemap-contact)
          922  +  @ <hr />
          923  +  @ <p><input type="submit"  name="submit" value="Apply Changes" /></p>
          924  +  @ </div></form>
          925  +  db_end_transaction(0);
          926  +  style_footer();
          927  +}
          928  +
          929  +/*
          930  +** WEBPAGE: setup_wiki
          931  +**
          932  +** The "Admin/Wiki" page.  Requires Setup privilege.
          933  +*/
          934  +void setup_wiki(void){
          935  +  login_check_credentials();
          936  +  if( !g.perm.Setup ){
          937  +    login_needed(0);
          938  +    return;
          939  +  }
          940  +
          941  +  style_header("Wiki Configuration");
          942  +  db_begin_transaction();
          943  +  @ <form action="%s(g.zTop)/setup_config" method="post"><div>
          944  +  login_insert_csrf_secret();
          945  +  @ <input type="submit"  name="submit" value="Apply Changes" /></p>
          946  +  @ <hr />
          947  +  onoff_attribute("Enable WYSIWYG Wiki Editing",
          948  +                  "wysiwyg-wiki", "wysiwyg-wiki", 0, 0);
          949  +  @ <p>Enable what-you-see-is-what-you-get (WYSIWYG) editing of wiki pages.
          950  +  @ The WYSIWYG editor generates HTML instead of markup, which makes
          951  +  @ subsequent manual editing more difficult.
          952  +  @ (Property: "wysiwyg-wiki")</p>
   911    953     @ <hr />
   912    954     onoff_attribute("Use HTML as wiki markup language",
   913    955       "wiki-use-html", "wiki-use-html", 0, 0);
   914    956     @ <p>Use HTML as the wiki markup language. Wiki links will still be parsed
   915    957     @ but all other wiki formatting will be ignored. This option is helpful
   916    958     @ if you have chosen to use a rich HTML editor for wiki markup such as
   917    959     @ TinyMCE.</p>
................................................................................
   933    975   /*
   934    976   ** WEBPAGE: setup_modreq
   935    977   **
   936    978   ** Admin page for setting up moderation of tickets and wiki.
   937    979   */
   938    980   void setup_modreq(void){
   939    981     login_check_credentials();
   940         -  if( !g.perm.Setup ){
          982  +  if( !g.perm.Admin ){
   941    983       login_needed(0);
   942    984       return;
   943    985     }
   944    986   
   945    987     style_header("Moderator For Wiki And Tickets");
   946    988     db_begin_transaction();
   947    989     @ <form action="%R/setup_modreq" method="post"><div>
................................................................................
   981   1023   ** WEBPAGE: setup_adunit
   982   1024   **
   983   1025   ** Administrative page for configuring and controlling ad units
   984   1026   ** and how they are displayed.
   985   1027   */
   986   1028   void setup_adunit(void){
   987   1029     login_check_credentials();
   988         -  if( !g.perm.Setup ){
         1030  +  if( !g.perm.Admin ){
   989   1031       login_needed(0);
   990   1032       return;
   991   1033     }
   992   1034     db_begin_transaction();
   993   1035     if( P("clear")!=0 && cgi_csrf_safe(1) ){
   994   1036       db_multi_exec("DELETE FROM config WHERE name GLOB 'adunit*'");
   995   1037       cgi_replace_parameter("adunit","");
................................................................................
  1071   1113     if( szLogoImg>0 ){
  1072   1114       zLogoMime = PD("logoim:mimetype","image/gif");
  1073   1115     }
  1074   1116     if( szBgImg>0 ){
  1075   1117       zBgMime = PD("bgim:mimetype","image/gif");
  1076   1118     }
  1077   1119     login_check_credentials();
  1078         -  if( !g.perm.Setup ){
         1120  +  if( !g.perm.Admin ){
  1079   1121       login_needed(0);
  1080   1122       return;
  1081   1123     }
  1082   1124     db_begin_transaction();
  1083   1125     if( !cgi_csrf_safe(1) ){
  1084   1126       /* Allow no state changes if not safe from CSRF */
  1085   1127     }else if( P("setlogo")!=0 && zLogoMime && zLogoMime[0] && szLogoImg>0 ){
................................................................................
  1212   1254   }
  1213   1255   
  1214   1256   
  1215   1257   /*
  1216   1258   ** WEBPAGE: admin_sql
  1217   1259   **
  1218   1260   ** Run raw SQL commands against the database file using the web interface.
  1219         -** Requires Admin privileges.
         1261  +** Requires Setup privileges.
  1220   1262   */
  1221   1263   void sql_page(void){
  1222   1264     const char *zQ;
  1223   1265     int go = P("go")!=0;
  1224   1266     login_check_credentials();
  1225   1267     if( !g.perm.Setup ){
  1226   1268       login_needed(0);
................................................................................
  1403   1445   void page_admin_log(){
  1404   1446     Stmt stLog;
  1405   1447     int limit;                 /* How many entries to show */
  1406   1448     int ofst;                  /* Offset to the first entry */
  1407   1449     int fLogEnabled;
  1408   1450     int counter = 0;
  1409   1451     login_check_credentials();
  1410         -  if( !g.perm.Setup && !g.perm.Admin ){
         1452  +  if( !g.perm.Admin ){
  1411   1453       login_needed(0);
  1412   1454       return;
  1413   1455     }
  1414   1456     style_header("Admin Log");
  1415   1457     create_admin_log_table();
  1416   1458     limit = atoi(PD("n","200"));
  1417   1459     ofst = atoi(PD("x","0"));
................................................................................
  1463   1505   /*
  1464   1506   ** WEBPAGE: srchsetup
  1465   1507   **
  1466   1508   ** Configure the search engine.  Requires Admin privilege.
  1467   1509   */
  1468   1510   void page_srchsetup(){
  1469   1511     login_check_credentials();
  1470         -  if( !g.perm.Setup && !g.perm.Admin ){
         1512  +  if( !g.perm.Admin ){
  1471   1513       login_needed(0);
  1472   1514       return;
  1473   1515     }
  1474   1516     style_header("Search Configuration");
  1475   1517     @ <form action="%s(g.zTop)/srchsetup" method="post"><div>
  1476   1518     login_insert_csrf_secret();
  1477   1519     @ <div style="text-align:center;font-weight:bold;">
................................................................................
  1587   1629   ** Configure the URL aliases
  1588   1630   */
  1589   1631   void page_waliassetup(){
  1590   1632     Stmt q;
  1591   1633     int cnt = 0;
  1592   1634     Blob namelist;
  1593   1635     login_check_credentials();
  1594         -  if( !g.perm.Setup && !g.perm.Admin ){
         1636  +  if( !g.perm.Admin ){
  1595   1637       login_needed(0);
  1596   1638       return;
  1597   1639     }
  1598   1640     style_header("URL Alias Configuration");
  1599   1641     if( P("submit")!=0 ){
  1600   1642       Blob token;
  1601   1643       Blob sql;

Changes to src/setupuser.c.

    40     40   
    41     41     login_check_credentials();
    42     42     if( !g.perm.Admin ){
    43     43       login_needed(0);
    44     44       return;
    45     45     }
    46     46   
           47  +  style_submenu_element("Add", "setup_uedit");
           48  +  style_submenu_element("Log", "access_log");
           49  +  style_submenu_element("Help", "setup_ulist_notes");
           50  +  style_header("User List");
    47     51     if( zWith==0 || zWith[0]==0 ){
    48         -    style_submenu_element("Add", "setup_uedit");
    49         -    style_submenu_element("Log", "access_log");
    50         -    style_submenu_element("Help", "setup_ulist_notes");
    51         -    style_header("User List");
    52     52       @ <table border=1 cellpadding=2 cellspacing=0 class='userTable'>
    53     53       @ <thead><tr>
    54     54       @   <th>Category
    55     55       @   <th>Capabilities (<a href='%R/setup_ucap_list'>key</a>)
    56     56       @   <th>Info <th>Last Change</tr></thead>
    57     57       @ <tbody>
    58     58       db_prepare(&s,
................................................................................
    85     85           @ <td>%h(zDate)
    86     86         }else{
    87     87           @ <td>
    88     88         }
    89     89         @ </tr>
    90     90       }
    91     91       db_finalize(&s);
           92  +    @ </tbody></table>
           93  +    @ <div class='section'>Users</div>
    92     94     }else{
    93         -    style_header("Users With Capabilities \"%h\"", zWith);
           95  +    style_submenu_element("All Users", "setup_ulist");
           96  +    if( zWith[1]==0 ){
           97  +      @ <div class='section'>Users with capability "%h(zWith)"</div>
           98  +    }else{
           99  +      @ <div class='section'>Users with any capability in "%h(zWith)"</div>
          100  +    }
    94    101     }
    95         -  @ </tbody></table>
    96         -  @ <div class='section'>Users</div>
    97    102     @ <table border=1 cellpadding=2 cellspacing=0 class='userTable sortable' \
    98    103     @  data-column-types='ktxTTK' data-init-sort='2'>
    99    104     @ <thead><tr>
   100    105     @ <th>Login Name<th>Caps<th>Info<th>Date<th>Expire<th>Last Login</tr></thead>
   101    106     @ <tbody>
   102    107     db_multi_exec(
   103    108       "CREATE TEMP TABLE lastAccess(uname TEXT PRIMARY KEY, atime REAL)"

Changes to src/shell.c.

  1313   1313   **
  1314   1314   **    May you do good and not evil.
  1315   1315   **    May you find forgiveness for yourself and forgive others.
  1316   1316   **    May you share freely, never taking more than you give.
  1317   1317   **
  1318   1318   ******************************************************************************
  1319   1319   **
  1320         -** This SQLite extension implements a functions that compute SHA1 hashes.
         1320  +** This SQLite extension implements functions that compute SHA3 hashes.
  1321   1321   ** Two SQL functions are implemented:
  1322   1322   **
  1323   1323   **     sha3(X,SIZE)
  1324   1324   **     sha3_query(Y,SIZE)
  1325   1325   **
  1326   1326   ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
  1327   1327   ** X is NULL.
................................................................................
  2124   2124   #  define mkdir(path,mode) _mkdir(path)
  2125   2125   #  define lstat(path,buf) stat(path,buf)
  2126   2126   #endif
  2127   2127   #include <time.h>
  2128   2128   #include <errno.h>
  2129   2129   
  2130   2130   
         2131  +/*
         2132  +** Structure of the fsdir() table-valued function
         2133  +*/
         2134  +                 /*    0    1    2     3    4           5             */
  2131   2135   #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
         2136  +#define FSDIR_COLUMN_NAME     0     /* Name of the file */
         2137  +#define FSDIR_COLUMN_MODE     1     /* Access mode */
         2138  +#define FSDIR_COLUMN_MTIME    2     /* Last modification time */
         2139  +#define FSDIR_COLUMN_DATA     3     /* File content */
         2140  +#define FSDIR_COLUMN_PATH     4     /* Path to top of search */
         2141  +#define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
         2142  +
  2132   2143   
  2133   2144   /*
  2134   2145   ** Set the result stored by context ctx to a blob containing the 
  2135   2146   ** contents of file zName.
  2136   2147   */
  2137   2148   static void readFileContents(sqlite3_context *ctx, const char *zName){
  2138   2149     FILE *in;
................................................................................
  2713   2724   static int fsdirColumn(
  2714   2725     sqlite3_vtab_cursor *cur,   /* The cursor */
  2715   2726     sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
  2716   2727     int i                       /* Which column to return */
  2717   2728   ){
  2718   2729     fsdir_cursor *pCur = (fsdir_cursor*)cur;
  2719   2730     switch( i ){
  2720         -    case 0: { /* name */
         2731  +    case FSDIR_COLUMN_NAME: {
  2721   2732         sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
  2722   2733         break;
  2723   2734       }
  2724   2735   
  2725         -    case 1: /* mode */
         2736  +    case FSDIR_COLUMN_MODE:
  2726   2737         sqlite3_result_int64(ctx, pCur->sStat.st_mode);
  2727   2738         break;
  2728   2739   
  2729         -    case 2: /* mtime */
         2740  +    case FSDIR_COLUMN_MTIME:
  2730   2741         sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
  2731   2742         break;
  2732   2743   
  2733         -    case 3: { /* data */
         2744  +    case FSDIR_COLUMN_DATA: {
  2734   2745         mode_t m = pCur->sStat.st_mode;
  2735   2746         if( S_ISDIR(m) ){
  2736   2747           sqlite3_result_null(ctx);
  2737   2748   #if !defined(_WIN32) && !defined(WIN32)
  2738   2749         }else if( S_ISLNK(m) ){
  2739   2750           char aStatic[64];
  2740   2751           char *aBuf = aStatic;
................................................................................
  2756   2767           sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
  2757   2768           if( aBuf!=aStatic ) sqlite3_free(aBuf);
  2758   2769   #endif
  2759   2770         }else{
  2760   2771           readFileContents(ctx, pCur->zPath);
  2761   2772         }
  2762   2773       }
         2774  +    case FSDIR_COLUMN_PATH:
         2775  +    default: {
         2776  +      /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
         2777  +      ** always return their values as NULL */
         2778  +      break;
         2779  +    }
  2763   2780     }
  2764   2781     return SQLITE_OK;
  2765   2782   }
  2766   2783   
  2767   2784   /*
  2768   2785   ** Return the rowid for the current row. In this implementation, the
  2769   2786   ** first row returned is assigned rowid value 1, and each subsequent
................................................................................
  2782   2799   static int fsdirEof(sqlite3_vtab_cursor *cur){
  2783   2800     fsdir_cursor *pCur = (fsdir_cursor*)cur;
  2784   2801     return (pCur->zPath==0);
  2785   2802   }
  2786   2803   
  2787   2804   /*
  2788   2805   ** xFilter callback.
         2806  +**
         2807  +** idxNum==1   PATH parameter only
         2808  +** idxNum==2   Both PATH and DIR supplied
  2789   2809   */
  2790   2810   static int fsdirFilter(
  2791   2811     sqlite3_vtab_cursor *cur, 
  2792   2812     int idxNum, const char *idxStr,
  2793   2813     int argc, sqlite3_value **argv
  2794   2814   ){
  2795   2815     const char *zDir = 0;
................................................................................
  2834   2854   ** that uses the generate_series virtual table.  This routine needs to create
  2835   2855   ** a query plan for each invocation and compute an estimated cost for that
  2836   2856   ** plan.
  2837   2857   **
  2838   2858   ** In this implementation idxNum is used to represent the
  2839   2859   ** query plan.  idxStr is unused.
  2840   2860   **
  2841         -** The query plan is represented by bits in idxNum:
         2861  +** The query plan is represented by values of idxNum:
  2842   2862   **
  2843         -**  (1)  start = $value  -- constraint exists
  2844         -**  (2)  stop = $value   -- constraint exists
  2845         -**  (4)  step = $value   -- constraint exists
  2846         -**  (8)  output in descending order
         2863  +**  (1)  The path value is supplied by argv[0]
         2864  +**  (2)  Path is in argv[0] and dir is in argv[1]
  2847   2865   */
  2848   2866   static int fsdirBestIndex(
  2849   2867     sqlite3_vtab *tab,
  2850   2868     sqlite3_index_info *pIdxInfo
  2851   2869   ){
  2852   2870     int i;                 /* Loop over constraints */
  2853         -  int idx4 = -1;
  2854         -  int idx5 = -1;
         2871  +  int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
         2872  +  int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
         2873  +  int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
         2874  +  int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
  2855   2875     const struct sqlite3_index_constraint *pConstraint;
  2856   2876   
  2857   2877     (void)tab;
  2858   2878     pConstraint = pIdxInfo->aConstraint;
  2859   2879     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
  2860         -    if( pConstraint->usable==0 ) continue;
  2861   2880       if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
  2862         -    if( pConstraint->iColumn==4 ) idx4 = i;
  2863         -    if( pConstraint->iColumn==5 ) idx5 = i;
         2881  +    switch( pConstraint->iColumn ){
         2882  +      case FSDIR_COLUMN_PATH: {
         2883  +        if( pConstraint->usable ){
         2884  +          idxPath = i;
         2885  +          seenPath = 0;
         2886  +        }else if( idxPath<0 ){
         2887  +          seenPath = 1;
         2888  +        }
         2889  +        break;
         2890  +      }
         2891  +      case FSDIR_COLUMN_DIR: {
         2892  +        if( pConstraint->usable ){
         2893  +          idxDir = i;
         2894  +          seenDir = 0;
         2895  +        }else if( idxDir<0 ){
         2896  +          seenDir = 1;
         2897  +        }
         2898  +        break;
         2899  +      }
         2900  +    } 
         2901  +  }
         2902  +  if( seenPath || seenDir ){
         2903  +    /* If input parameters are unusable, disallow this plan */
         2904  +    return SQLITE_CONSTRAINT;
  2864   2905     }
  2865   2906   
  2866         -  if( idx4<0 ){
         2907  +  if( idxPath<0 ){
  2867   2908       pIdxInfo->idxNum = 0;
  2868         -    pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
         2909  +    /* The pIdxInfo->estimatedCost should have been initialized to a huge
         2910  +    ** number.  Leave it unchanged. */
         2911  +    pIdxInfo->estimatedRows = 0x7fffffff;
  2869   2912     }else{
  2870         -    pIdxInfo->aConstraintUsage[idx4].omit = 1;
  2871         -    pIdxInfo->aConstraintUsage[idx4].argvIndex = 1;
  2872         -    if( idx5>=0 ){
  2873         -      pIdxInfo->aConstraintUsage[idx5].omit = 1;
  2874         -      pIdxInfo->aConstraintUsage[idx5].argvIndex = 2;
         2913  +    pIdxInfo->aConstraintUsage[idxPath].omit = 1;
         2914  +    pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
         2915  +    if( idxDir>=0 ){
         2916  +      pIdxInfo->aConstraintUsage[idxDir].omit = 1;
         2917  +      pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
  2875   2918         pIdxInfo->idxNum = 2;
  2876   2919         pIdxInfo->estimatedCost = 10.0;
  2877   2920       }else{
  2878   2921         pIdxInfo->idxNum = 1;
  2879   2922         pIdxInfo->estimatedCost = 100.0;
  2880   2923       }
  2881   2924     }
................................................................................
  2906   2949       0,                         /* xSync */
  2907   2950       0,                         /* xCommit */
  2908   2951       0,                         /* xRollback */
  2909   2952       0,                         /* xFindMethod */
  2910   2953       0,                         /* xRename */
  2911   2954       0,                         /* xSavepoint */
  2912   2955       0,                         /* xRelease */
  2913         -    0                          /* xRollbackTo */
         2956  +    0,                         /* xRollbackTo */
         2957  +    0,                         /* xShadowName */
  2914   2958     };
  2915   2959   
  2916   2960     int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
  2917   2961     return rc;
  2918   2962   }
  2919   2963   #else         /* SQLITE_OMIT_VIRTUALTABLE */
  2920   2964   # define fsdirRegister(x) SQLITE_OK
................................................................................
  3414   3458     0,                         /* xSync */
  3415   3459     0,                         /* xCommit */
  3416   3460     0,                         /* xRollback */
  3417   3461     0,                         /* xFindMethod */
  3418   3462     0,                         /* xRename */
  3419   3463     0,                         /* xSavepoint */
  3420   3464     0,                         /* xRelease */
  3421         -  0                          /* xRollbackTo */
         3465  +  0,                         /* xRollbackTo */
         3466  +  0                          /* xShadowName */
  3422   3467   };
  3423   3468   
  3424   3469   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  3425   3470   
  3426   3471   int sqlite3CompletionVtabInit(sqlite3 *db){
  3427   3472     int rc = SQLITE_OK;
  3428   3473   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
  5311   5356   ** xBestIndex callback.
  5312   5357   */
  5313   5358   static int zipfileBestIndex(
  5314   5359     sqlite3_vtab *tab,
  5315   5360     sqlite3_index_info *pIdxInfo
  5316   5361   ){
  5317   5362     int i;
         5363  +  int idx = -1;
         5364  +  int unusable = 0;
  5318   5365   
  5319   5366     for(i=0; i<pIdxInfo->nConstraint; i++){
  5320   5367       const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
  5321         -    if( pCons->usable==0 ) continue;
  5322         -    if( pCons->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
  5323   5368       if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
  5324         -    break;
         5369  +    if( pCons->usable==0 ){
         5370  +      unusable = 1;
         5371  +    }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
         5372  +      idx = i;
         5373  +    }
  5325   5374     }
  5326         -
  5327         -  if( i<pIdxInfo->nConstraint ){
  5328         -    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
  5329         -    pIdxInfo->aConstraintUsage[i].omit = 1;
         5375  +  if( idx>=0 ){
         5376  +    pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
         5377  +    pIdxInfo->aConstraintUsage[idx].omit = 1;
  5330   5378       pIdxInfo->estimatedCost = 1000.0;
  5331   5379       pIdxInfo->idxNum = 1;
  5332         -  }else{
  5333         -    pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
  5334         -    pIdxInfo->idxNum = 0;
         5380  +  }else if( unusable ){
         5381  +    return SQLITE_CONSTRAINT;
  5335   5382     }
  5336         -
  5337   5383     return SQLITE_OK;
  5338   5384   }
  5339   5385   
  5340   5386   static ZipfileEntry *zipfileNewEntry(const char *zPath){
  5341   5387     ZipfileEntry *pNew;
  5342   5388     pNew = sqlite3_malloc(sizeof(ZipfileEntry));
  5343   5389     if( pNew ){
................................................................................
  7132   7178       0,                            /* xCommit - commit transaction */
  7133   7179       0,                            /* xRollback - rollback transaction */
  7134   7180       0,                            /* xFindFunction - function overloading */
  7135   7181       0,                            /* xRename - rename the table */
  7136   7182       0,                            /* xSavepoint */
  7137   7183       0,                            /* xRelease */
  7138   7184       0,                            /* xRollbackTo */
         7185  +    0,                            /* xShadowName */
  7139   7186     };
  7140   7187   
  7141   7188     return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
  7142   7189   }
  7143   7190   /*
  7144   7191   ** End of virtual table implementation.
  7145   7192   *************************************************************************/
................................................................................
  8556   8603   #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
  8557   8604   #define AUTOEQP_on       1           /* Automatic EQP is on */
  8558   8605   #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
  8559   8606   #define AUTOEQP_full     3           /* Show full EXPLAIN */
  8560   8607   
  8561   8608   /* Allowed values for ShellState.openMode
  8562   8609   */
  8563         -#define SHELL_OPEN_UNSPEC     0      /* No open-mode specified */
  8564         -#define SHELL_OPEN_NORMAL     1      /* Normal database file */
  8565         -#define SHELL_OPEN_APPENDVFS  2      /* Use appendvfs */
  8566         -#define SHELL_OPEN_ZIPFILE    3      /* Use the zipfile virtual table */
  8567         -#define SHELL_OPEN_READONLY   4      /* Open a normal database read-only */
         8610  +#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
         8611  +#define SHELL_OPEN_NORMAL      1      /* Normal database file */
         8612  +#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
         8613  +#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
         8614  +#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
         8615  +#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
  8568   8616   
  8569   8617   /*
  8570   8618   ** These are the allowed shellFlgs values
  8571   8619   */
  8572   8620   #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
  8573   8621   #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
  8574   8622   #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
................................................................................
  8770   8818     if( x!=sz ){
  8771   8819       sqlite3_result_error(context, "could not read back the whole file", -1);
  8772   8820       goto edit_func_end;
  8773   8821     }
  8774   8822     if( bBin ){
  8775   8823       sqlite3_result_blob64(context, p, sz, sqlite3_free);
  8776   8824     }else{
  8777         -    int i, j;
         8825  +    sqlite3_int64 i, j;
  8778   8826       if( hasCRNL ){
  8779   8827         /* If the original contains \r\n then do no conversions back to \n */
  8780   8828         j = sz;
  8781   8829       }else{
  8782   8830         /* If the file did not originally contain \r\n then convert any new
  8783   8831         ** \r\n back into \n */
  8784   8832         for(i=j=0; i<sz; i++){
................................................................................
 10832  10880       sqlite3_free(zErr);
 10833  10881       free(zQ2);
 10834  10882     }
 10835  10883     return rc;
 10836  10884   }
 10837  10885   
 10838  10886   /*
 10839         -** Text of a help message
        10887  +** Text of help messages.
        10888  +**
        10889  +** The help text for each individual command begins with a line that starts
        10890  +** with ".".  Subsequent lines are supplimental information.
        10891  +**
        10892  +** There must be two or more spaces between the end of the command and the
        10893  +** start of the description of what that command does.
 10840  10894   */
 10841         -static char zHelp[] =
        10895  +static const char *(azHelp[]) = {
 10842  10896   #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
 10843         -  ".archive ...           Manage SQL archives: \".archive --help\" for details\n"
        10897  +  ".archive ...             Manage SQL archives",
        10898  +  "   Each command must have exactly one of the following options:",
        10899  +  "     -c, --create               Create a new archive",
        10900  +  "     -u, --update               Update or add files to an existing archive",
        10901  +  "     -t, --list                 List contents of archive",
        10902  +  "     -x, --extract              Extract files from archive",
        10903  +  "   Optional arguments:",
        10904  +  "     -v, --verbose              Print each filename as it is processed",
        10905  +  "     -f FILE, --file FILE       Operate on archive FILE (default is current db)",
        10906  +  "     -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS",
        10907  +  "     -C DIR, --directory DIR    Change to directory DIR to read/extract files",
        10908  +  "     -n, --dryrun               Show the SQL that would have occurred",
        10909  +  "   Examples:",
        10910  +  "     .ar -cf archive.sar foo bar  # Create archive.sar from files foo and bar",
        10911  +  "     .ar -tf archive.sar          # List members of archive.sar",
        10912  +  "     .ar -xvf archive.sar         # Verbosely extract files from archive.sar",
        10913  +  "   See also:",
        10914  +  "      http://sqlite.org/cli.html#sqlar_archive_support",
 10844  10915   #endif
 10845  10916   #ifndef SQLITE_OMIT_AUTHORIZATION
 10846         -  ".auth ON|OFF           Show authorizer callbacks\n"
 10847         -#endif
 10848         -  ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
 10849         -  "                         Add \"--append\" to open using appendvfs.\n"
 10850         -  ".bail on|off           Stop after hitting an error.  Default OFF\n"
 10851         -  ".binary on|off         Turn binary output on or off.  Default OFF\n"
 10852         -  ".cd DIRECTORY          Change the working directory to DIRECTORY\n"
 10853         -  ".changes on|off        Show number of rows changed by SQL\n"
 10854         -  ".check GLOB            Fail if output since .testcase does not match\n"
 10855         -  ".clone NEWDB           Clone data into NEWDB from the existing database\n"
 10856         -  ".databases             List names and files of attached databases\n"
 10857         -  ".dbconfig ?op? ?val?   List or change sqlite3_db_config() options\n"
 10858         -  ".dbinfo ?DB?           Show status information about the database\n"
 10859         -  ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
 10860         -  "                         If TABLE specified, only dump tables matching\n"
 10861         -  "                         LIKE pattern TABLE.\n"
 10862         -  ".echo on|off           Turn command echo on or off\n"
 10863         -  ".eqp on|off|full       Enable or disable automatic EXPLAIN QUERY PLAN\n"
 10864         -  ".excel                 Display the output of next command in a spreadsheet\n"
 10865         -  ".exit                  Exit this program\n"
 10866         -  ".expert                EXPERIMENTAL. Suggest indexes for specified queries\n"
        10917  +  ".auth ON|OFF             Show authorizer callbacks",
        10918  +#endif
        10919  +  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
        10920  +  "       --append            Use the appendvfs",
        10921  +  ".bail on|off             Stop after hitting an error.  Default OFF",
        10922  +  ".binary on|off           Turn binary output on or off.  Default OFF",
        10923  +  ".cd DIRECTORY            Change the working directory to DIRECTORY",
        10924  +  ".changes on|off          Show number of rows changed by SQL",
        10925  +  ".check GLOB              Fail if output since .testcase does not match",
        10926  +  ".clone NEWDB             Clone data into NEWDB from the existing database",
        10927  +  ".databases               List names and files of attached databases",
        10928  +  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
        10929  +  ".dbinfo ?DB?             Show status information about the database",
        10930  +  ".dump ?TABLE? ...        Render all database content as SQL",
        10931  +  "   Options:",
        10932  +  "     --preserve-rowids      Include ROWID values in the output",
        10933  +  "     --newlines             Allow unescaped newline characters in output",
        10934  +  "   TABLE is LIKE pattern for the tables to dump",
        10935  +  ".echo on|off             Turn command echo on or off",
        10936  +  ".eqp on|off|full         Enable or disable automatic EXPLAIN QUERY PLAN",
        10937  +  ".excel                   Display the output of next command in a spreadsheet",
        10938  +  ".exit ?CODE?             Exit this program with return-code CODE",
        10939  +  ".expert                  EXPERIMENTAL. Suggest indexes for specified queries",
 10867  10940   /* Because explain mode comes on automatically now, the ".explain" mode
 10868  10941   ** is removed from the help screen.  It is still supported for legacy, however */
 10869         -/*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
 10870         -  ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
 10871         -  ".headers on|off        Turn display of headers on or off\n"
 10872         -  ".help                  Show this message\n"
 10873         -  ".import FILE TABLE     Import data from FILE into TABLE\n"
        10942  +/*".explain ?on|off|auto?   Turn EXPLAIN output mode on or off or to automatic",*/
        10943  +  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
        10944  +  ".headers on|off          Turn display of headers on or off",
        10945  +  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
        10946  +  ".import FILE TABLE       Import data from FILE into TABLE",
 10874  10947   #ifndef SQLITE_OMIT_TEST_CONTROL
 10875         -  ".imposter INDEX TABLE  Create imposter table TABLE on index INDEX\n"
        10948  +  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
 10876  10949   #endif
 10877         -  ".indexes ?TABLE?       Show names of all indexes\n"
 10878         -  "                         If TABLE specified, only show indexes for tables\n"
 10879         -  "                         matching LIKE pattern TABLE.\n"
        10950  +  ".indexes ?TABLE?         Show names of indexes",
        10951  +  "                           If TABLE is specified, only show indexes for",
        10952  +  "                           tables matching TABLE using the LIKE operator.",
 10880  10953   #ifdef SQLITE_ENABLE_IOTRACE
 10881         -  ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
        10954  +  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
 10882  10955   #endif
 10883         -  ".limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT\n"
 10884         -  ".lint OPTIONS          Report potential schema issues. Options:\n"
 10885         -  "                         fkey-indexes     Find missing foreign key indexes\n"
        10956  +  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
        10957  +  ".lint OPTIONS            Report potential schema issues.",
        10958  +  "     Options:",
        10959  +  "        fkey-indexes     Find missing foreign key indexes",
 10886  10960   #ifndef SQLITE_OMIT_LOAD_EXTENSION
 10887         -  ".load FILE ?ENTRY?     Load an extension library\n"
 10888         -#endif
 10889         -  ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
 10890         -  ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
 10891         -  "                         ascii    Columns/rows delimited by 0x1F and 0x1E\n"
 10892         -  "                         csv      Comma-separated values\n"
 10893         -  "                         column   Left-aligned columns.  (See .width)\n"
 10894         -  "                         html     HTML <table> code\n"
 10895         -  "                         insert   SQL insert statements for TABLE\n"
 10896         -  "                         line     One value per line\n"
 10897         -  "                         list     Values delimited by \"|\"\n"
 10898         -  "                         quote    Escape answers as for SQL\n"
 10899         -  "                         tabs     Tab-separated values\n"
 10900         -  "                         tcl      TCL list elements\n"
 10901         -  ".nullvalue STRING      Use STRING in place of NULL values\n"
 10902         -  ".once (-e|-x|FILE)     Output for the next SQL command only to FILE\n"
 10903         -  "                         or invoke system text editor (-e) or spreadsheet (-x)\n"
 10904         -  "                         on the output.\n"
 10905         -  ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
 10906         -  "                         The --new option starts with an empty file\n"
 10907         -  "                         Other options: --readonly --append --zip\n"
 10908         -  ".output ?FILE?         Send output to FILE or stdout\n"
 10909         -  ".print STRING...       Print literal STRING\n"
 10910         -  ".prompt MAIN CONTINUE  Replace the standard prompts\n"
 10911         -  ".quit                  Exit this program\n"
 10912         -  ".read FILENAME         Execute SQL in FILENAME\n"
 10913         -  ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
 10914         -  ".save FILE             Write in-memory database into FILE\n"
 10915         -  ".scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off\n"
 10916         -  ".schema ?PATTERN?      Show the CREATE statements matching PATTERN\n"
 10917         -  "                          Add --indent for pretty-printing\n"
 10918         -  ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
 10919         -  ".separator COL ?ROW?   Change the column separator and optionally the row\n"
 10920         -  "                         separator for both the output mode and .import\n"
        10961  +  ".load FILE ?ENTRY?       Load an extension library",
        10962  +#endif
        10963  +  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
        10964  +  ".mode MODE ?TABLE?       Set output mode",
        10965  +  "   MODE is one of:",
        10966  +  "     ascii    Columns/rows delimited by 0x1F and 0x1E",
        10967  +  "     csv      Comma-separated values",
        10968  +  "     column   Left-aligned columns.  (See .width)",
        10969  +  "     html     HTML <table> code",
        10970  +  "     insert   SQL insert statements for TABLE",
        10971  +  "     line     One value per line",
        10972  +  "     list     Values delimited by \"|\"",
        10973  +  "     quote    Escape answers as for SQL",
        10974  +  "     tabs     Tab-separated values",
        10975  +  "     tcl      TCL list elements",
        10976  +  ".nullvalue STRING        Use STRING in place of NULL values",
        10977  +  ".once (-e|-x|FILE)       Output for the next SQL command only to FILE",
        10978  +  "     If FILE begins with '|' then open as a pipe",
        10979  +  "     Other options:",
        10980  +  "       -e    Invoke system text editor",
        10981  +  "       -x    Open in a spreadsheet",
        10982  +  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
        10983  +  "     Options:",
        10984  +  "        --append        Use appendvfs to append database to the end of FILE",
        10985  +#ifdef SQLITE_ENABLE_DESERIALIZE
        10986  +  "        --deserialize   Load into memory useing sqlite3_deserialize()",
        10987  +#endif
        10988  +  "        --new           Initialize FILE to an empty database",
        10989  +  "        --readonly      Open FILE readonly",
        10990  +  "        --zip           FILE is a ZIP archive",
        10991  +  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
        10992  +  "     If FILE begins with '|' then open it as a pipe.",
        10993  +  ".print STRING...         Print literal STRING",
        10994  +  ".prompt MAIN CONTINUE    Replace the standard prompts",
        10995  +  ".quit                    Exit this program",
        10996  +  ".read FILE               Read input from FILE",
        10997  +  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
        10998  +  ".save FILE               Write in-memory database into FILE",
        10999  +  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
        11000  +  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
        11001  +  "     Options:",
        11002  +  "         --indent            Try to pretty-print the schema",
        11003  +  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
        11004  +  "    Options:",
        11005  +  "       --init               Create a new SELFTEST table",
        11006  +  "       -v                   Verbose output",
        11007  +  ".separator COL ?ROW?     Change the column and row separators",
 10921  11008   #if defined(SQLITE_ENABLE_SESSION)
 10922         -  ".session CMD ...       Create or control sessions\n"
        11009  +  ".session ?NAME? CMD ...  Create or control sessions",
        11010  +  "   Subcommands:",
        11011  +  "     attach TABLE             Attach TABLE",
        11012  +  "     changeset FILE           Write a changeset into FILE",
        11013  +  "     close                    Close one session",
        11014  +  "     enable ?BOOLEAN?         Set or query the enable bit",
        11015  +  "     filter GLOB...           Reject tables matching GLOBs",
        11016  +  "     indirect ?BOOLEAN?       Mark or query the indirect status",
        11017  +  "     isempty                  Query whether the session is empty",
        11018  +  "     list                     List currently open session names",
        11019  +  "     open DB NAME             Open a new session on DB",
        11020  +  "     patchset FILE            Write a patchset into FILE",
        11021  +  "   If ?NAME? is omitted, the first defined session is used.",
 10923  11022   #endif
 10924         -  ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
        11023  +  ".sha3sum ...             Compute a SHA3 hash of database content",
        11024  +  "    Options:",
        11025  +  "      --schema              Also hash the sqlite_master table",
        11026  +  "      --sha3-224            Use the sha3-224 algorithm",
        11027  +  "      --sha3-256            Use the sha3-256 algorithm.  This is the default.",
        11028  +  "      --sha3-384            Use the sha3-384 algorithm",
        11029  +  "      --sha3-512            Use the sha3-512 algorithm",
        11030  +  "    Any other argument is a LIKE pattern for tables to hash",
 10925  11031   #ifndef SQLITE_NOHAVE_SYSTEM
 10926         -  ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
        11032  +  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
 10927  11033   #endif
 10928         -  ".show                  Show the current values for various settings\n"
 10929         -  ".stats ?on|off?        Show stats or turn stats on or off\n"
        11034  +  ".show                    Show the current values for various settings",
        11035  +  ".stats ?on|off?          Show stats or turn stats on or off",
 10930  11036   #ifndef SQLITE_NOHAVE_SYSTEM
 10931         -  ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
 10932         -#endif
 10933         -  ".tables ?TABLE?        List names of tables\n"
 10934         -  "                         If TABLE specified, only list tables matching\n"
 10935         -  "                         LIKE pattern TABLE.\n"
 10936         -  ".testcase NAME         Begin redirecting output to 'testcase-out.txt'\n"
 10937         -  ".timeout MS            Try opening locked tables for MS milliseconds\n"
 10938         -  ".timer on|off          Turn SQL timer on or off\n"
 10939         -  ".trace FILE|off        Output each SQL statement as it is run\n"
 10940         -  ".vfsinfo ?AUX?         Information about the top-level VFS\n"
 10941         -  ".vfslist               List all available VFSes\n"
 10942         -  ".vfsname ?AUX?         Print the name of the VFS stack\n"
 10943         -  ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
 10944         -  "                         Negative values right-justify\n"
 10945         -;
 10946         -
 10947         -#if defined(SQLITE_ENABLE_SESSION)
 10948         -/*
 10949         -** Print help information for the ".sessions" command
 10950         -*/
 10951         -void session_help(ShellState *p){
 10952         -  raw_printf(p->out,
 10953         -    ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
 10954         -    "If ?NAME? is omitted, the first defined session is used.\n"
 10955         -    "Subcommands:\n"
 10956         -    "   attach TABLE             Attach TABLE\n"
 10957         -    "   changeset FILE           Write a changeset into FILE\n"
 10958         -    "   close                    Close one session\n"
 10959         -    "   enable ?BOOLEAN?         Set or query the enable bit\n"
 10960         -    "   filter GLOB...           Reject tables matching GLOBs\n"
 10961         -    "   indirect ?BOOLEAN?       Mark or query the indirect status\n"
 10962         -    "   isempty                  Query whether the session is empty\n"
 10963         -    "   list                     List currently open session names\n"
 10964         -    "   open DB NAME             Open a new session on DB\n"
 10965         -    "   patchset FILE            Write a patchset into FILE\n"
 10966         -  );
 10967         -}
 10968         -#endif
 10969         -
        11037  +  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
        11038  +#endif
        11039  +  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
        11040  +  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
        11041  +  ".timeout MS              Try opening locked tables for MS milliseconds",
        11042  +  ".timer on|off            Turn SQL timer on or off",
        11043  +  ".trace FILE|off          Output each SQL statement as it is run",
        11044  +  ".vfsinfo ?AUX?           Information about the top-level VFS",
        11045  +  ".vfslist                 List all available VFSes",
        11046  +  ".vfsname ?AUX?           Print the name of the VFS stack",
        11047  +  ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
        11048  +  "     Negative values right-justify",
        11049  +};
        11050  +
        11051  +/*
        11052  +** Output help text.
        11053  +**
        11054  +** zPattern describes the set of commands for which help text is provided.
        11055  +** If zPattern is NULL, then show all commands, but only give a one-line
        11056  +** description of each.
        11057  +**
        11058  +** Return the number of matches.
        11059  +*/
        11060  +static int showHelp(FILE *out, const char *zPattern){
        11061  +  int i = 0;
        11062  +  int j = 0;
        11063  +  int n = 0;
        11064  +  char *zPat;
        11065  +  if( zPattern==0
        11066  +   || zPattern[0]=='0'
        11067  +   || strcmp(zPattern,"-a")==0
        11068  +   || strcmp(zPattern,"-all")==0
        11069  +  ){
        11070  +    /* Show all commands, but only one line per command */
        11071  +    if( zPattern==0 ) zPattern = "";
        11072  +    for(i=0; i<ArraySize(azHelp); i++){
        11073  +      if( azHelp[i][0]=='.' || zPattern[0] ){
        11074  +        utf8_printf(out, "%s\n", azHelp[i]);
        11075  +        n++;
        11076  +      }
        11077  +    }
        11078  +  }else{
        11079  +    /* Look for commands that for which zPattern is an exact prefix */
        11080  +    zPat = sqlite3_mprintf(".%s*", zPattern);
        11081  +    for(i=0; i<ArraySize(azHelp); i++){
        11082  +      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
        11083  +        utf8_printf(out, "%s\n", azHelp[i]);
        11084  +        j = i+1;
        11085  +        n++;
        11086  +      }
        11087  +    }
        11088  +    sqlite3_free(zPat);
        11089  +    if( n ){
        11090  +      if( n==1 ){
        11091  +        /* when zPattern is a prefix of exactly one command, then include the
        11092  +        ** details of that command, which should begin at offset j */
        11093  +        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
        11094  +          utf8_printf(out, "%s\n", azHelp[j]);
        11095  +          j++;
        11096  +        }
        11097  +      }
        11098  +      return n;
        11099  +    }
        11100  +    /* Look for commands that contain zPattern anywhere.  Show the complete
        11101  +    ** text of all commands that match. */
        11102  +    zPat = sqlite3_mprintf("%%%s%%", zPattern);
        11103  +    for(i=0; i<ArraySize(azHelp); i++){
        11104  +      if( azHelp[i][0]=='.' ) j = i;
        11105  +      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
        11106  +        utf8_printf(out, "%s\n", azHelp[j]);
        11107  +        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
        11108  +          j++;
        11109  +          utf8_printf(out, "%s\n", azHelp[j]);
        11110  +        }
        11111  +        i = j;
        11112  +        n++;
        11113  +      }
        11114  +    }
        11115  +    sqlite3_free(zPat);
        11116  +  }
        11117  +  return n;
        11118  +}
 10970  11119   
 10971  11120   /* Forward reference */
 10972  11121   static int process_input(ShellState *p, FILE *in);
 10973  11122   
 10974  11123   /*
 10975  11124   ** Read the content of file zName into memory obtained from sqlite3_malloc64()
 10976  11125   ** and return a pointer to the buffer. The caller is responsible for freeing
................................................................................
 10992  11141     size_t nRead;
 10993  11142     char *pBuf;
 10994  11143     if( in==0 ) return 0;
 10995  11144     fseek(in, 0, SEEK_END);
 10996  11145     nIn = ftell(in);
 10997  11146     rewind(in);
 10998  11147     pBuf = sqlite3_malloc64( nIn+1 );
 10999         -  if( pBuf==0 ) return 0;
        11148  +  if( pBuf==0 ){ fclose(in); return 0; }
 11000  11149     nRead = fread(pBuf, nIn, 1, in);
 11001  11150     fclose(in);
 11002  11151     if( nRead!=1 ){
 11003  11152       sqlite3_free(pBuf);
 11004  11153       return 0;
 11005  11154     }
 11006  11155     pBuf[nIn] = 0;
................................................................................
 11072  11221     if( f==0 ){
 11073  11222       if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
 11074  11223          return SHELL_OPEN_ZIPFILE;
 11075  11224       }else{
 11076  11225          return SHELL_OPEN_NORMAL;
 11077  11226       }
 11078  11227     }
        11228  +  n = fread(zBuf, 16, 1, f);
        11229  +  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
        11230  +    fclose(f);
        11231  +    return SHELL_OPEN_NORMAL;
        11232  +  }
 11079  11233     fseek(f, -25, SEEK_END);
 11080  11234     n = fread(zBuf, 25, 1, f);
 11081  11235     if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
 11082  11236       rc = SHELL_OPEN_APPENDVFS;
 11083  11237     }else{
 11084  11238       fseek(f, -22, SEEK_END);
 11085  11239       n = fread(zBuf, 22, 1, f);
................................................................................
 11122  11276         }
 11123  11277       }
 11124  11278       switch( p->openMode ){
 11125  11279         case SHELL_OPEN_APPENDVFS: {
 11126  11280           sqlite3_open_v2(p->zDbFilename, &p->db, 
 11127  11281              SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
 11128  11282           break;
        11283  +      }
        11284  +      case SHELL_OPEN_DESERIALIZE: {
        11285  +        sqlite3_open(0, &p->db);
        11286  +        break;
 11129  11287         }
 11130  11288         case SHELL_OPEN_ZIPFILE: {
 11131  11289           sqlite3_open(":memory:", &p->db);
 11132  11290           break;
 11133  11291         }
 11134  11292         case SHELL_OPEN_READONLY: {
 11135  11293           sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
................................................................................
 11172  11330   #endif
 11173  11331       if( p->openMode==SHELL_OPEN_ZIPFILE ){
 11174  11332         char *zSql = sqlite3_mprintf(
 11175  11333            "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
 11176  11334         sqlite3_exec(p->db, zSql, 0, 0, 0);
 11177  11335         sqlite3_free(zSql);
 11178  11336       }
        11337  +#ifdef SQLITE_ENABLE_DESERIALIZE
        11338  +    else if( p->openMode==SHELL_OPEN_DESERIALIZE ){
        11339  +      int nData = 0;
        11340  +      unsigned char *aData = (unsigned char*)readFile(p->zDbFilename, &nData);
        11341  +      int rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
        11342  +                   SQLITE_DESERIALIZE_RESIZEABLE |
        11343  +                   SQLITE_DESERIALIZE_FREEONCLOSE);
        11344  +      if( rc ){
        11345  +        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
        11346  +      }
        11347  +    }
        11348  +#endif
 11179  11349     }
 11180  11350   }
 11181  11351   
 11182  11352   /*
 11183  11353   ** Attempt to close the databaes connection.  Report errors.
 11184  11354   */
 11185  11355   void close_db(sqlite3 *db){
................................................................................
 12376  12546   ){
 12377  12547     *ppStmt = 0;
 12378  12548     if( *pRc==SQLITE_OK ){
 12379  12549       va_list ap;
 12380  12550       char *z;
 12381  12551       va_start(ap, zFmt);
 12382  12552       z = sqlite3_vmprintf(zFmt, ap);
        12553  +    va_end(ap);
 12383  12554       if( z==0 ){
 12384  12555         *pRc = SQLITE_NOMEM;
 12385  12556       }else{
 12386  12557         shellPrepare(db, pRc, z, ppStmt);
 12387  12558         sqlite3_free(z);
 12388  12559       }
 12389  12560     }
................................................................................
 12438  12609     sqlite3 *db;                    /* Database containing the archive */
 12439  12610   };
 12440  12611   
 12441  12612   /*
 12442  12613   ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
 12443  12614   */
 12444  12615   static int arUsage(FILE *f){
 12445         -  raw_printf(f,
 12446         -"\n"
 12447         -"Usage: .ar [OPTION...] [FILE...]\n"
 12448         -"The .ar command manages sqlar archives.\n"
 12449         -"\n"
 12450         -"Examples:\n"
 12451         -"  .ar -cf archive.sar foo bar    # Create archive.sar from files foo and bar\n"
 12452         -"  .ar -tf archive.sar            # List members of archive.sar\n"
 12453         -"  .ar -xvf archive.sar           # Verbosely extract files from archive.sar\n"
 12454         -"\n"
 12455         -"Each command line must feature exactly one command option:\n"
 12456         -"  -c, --create               Create a new archive\n"
 12457         -"  -u, --update               Update or add files to an existing archive\n"
 12458         -"  -t, --list                 List contents of archive\n"
 12459         -"  -x, --extract              Extract files from archive\n"
 12460         -"\n"
 12461         -"And zero or more optional options:\n"
 12462         -"  -v, --verbose              Print each filename as it is processed\n"
 12463         -"  -f FILE, --file FILE       Operate on archive FILE (default is current db)\n"
 12464         -"  -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS\n"
 12465         -"  -C DIR, --directory DIR    Change to directory DIR to read/extract files\n"
 12466         -"  -n, --dryrun               Show the SQL that would have occurred\n"
 12467         -"\n"
 12468         -"See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
 12469         -"\n"
 12470         -);
        12616  +  showHelp(f,"archive");
 12471  12617     return SQLITE_ERROR;
 12472  12618   }
 12473  12619   
 12474  12620   /*
 12475  12621   ** Print an error message for the .ar command to stderr and return 
 12476  12622   ** SQLITE_ERROR.
 12477  12623   */
................................................................................
 12570  12716       { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
 12571  12717       { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
 12572  12718     };
 12573  12719     int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
 12574  12720     struct ArSwitch *pEnd = &aSwitch[nSwitch];
 12575  12721   
 12576  12722     if( nArg<=1 ){
        12723  +    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
 12577  12724       return arUsage(stderr);
 12578  12725     }else{
 12579  12726       char *z = azArg[1];
 12580  12727       if( z[0]!='-' ){
 12581  12728         /* Traditional style [tar] invocation */
 12582  12729         int i;
 12583  12730         int iArg = 2;
................................................................................
 13341  13488         utf8_printf(stderr,"Error: %s\n", zErrMsg);
 13342  13489         sqlite3_free(zErrMsg);
 13343  13490         rc = 1;
 13344  13491       }
 13345  13492     }else
 13346  13493   
 13347  13494     if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
 13348         -    static const struct DbConfigChoices {const char *zName; int op;} aDbConfig[] = {
        13495  +    static const struct DbConfigChoices {
        13496  +      const char *zName;
        13497  +      int op;
        13498  +    } aDbConfig[] = {
 13349  13499           { "enable_fkey",      SQLITE_DBCONFIG_ENABLE_FKEY            },
 13350  13500           { "enable_trigger",   SQLITE_DBCONFIG_ENABLE_TRIGGER         },
 13351  13501           { "fts3_tokenizer",   SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER  },
 13352  13502           { "load_extension",   SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION  },
 13353  13503           { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE       },
 13354  13504           { "enable_qpsg",      SQLITE_DBCONFIG_ENABLE_QPSG            },
 13355  13505           { "trigger_eqp",      SQLITE_DBCONFIG_TRIGGER_EQP            },
 13356  13506           { "reset_database",   SQLITE_DBCONFIG_RESET_DATABASE         },
        13507  +        { "defensive",        SQLITE_DBCONFIG_DEFENSIVE              },
 13357  13508       };
 13358  13509       int ii, v;
 13359  13510       open_db(p, 0);
 13360  13511       for(ii=0; ii<ArraySize(aDbConfig); ii++){
 13361  13512         if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
 13362  13513         if( nArg>=3 ){
 13363  13514           sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
................................................................................
 13588  13739       }else{
 13589  13740         raw_printf(stderr, "Usage: .headers on|off\n");
 13590  13741         rc = 1;
 13591  13742       }
 13592  13743     }else
 13593  13744   
 13594  13745     if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
 13595         -    utf8_printf(p->out, "%s", zHelp);
        13746  +    if( nArg>=2 ){
        13747  +      n = showHelp(p->out, azArg[1]);
        13748  +      if( n==0 ){
        13749  +        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
        13750  +      }
        13751  +    }else{
        13752  +      showHelp(p->out, 0);
        13753  +    }
 13596  13754     }else
 13597  13755   
 13598  13756     if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
 13599  13757       char *zTable;               /* Insert data into this table */
 13600  13758       char *zFile;                /* Name of file to extra content from */
 13601  13759       sqlite3_stmt *pStmt = NULL; /* A statement */
 13602  13760       int nCol;                   /* Number of columns in the table */
................................................................................
 14065  14223         }else if( optionMatch(z, "zip") ){
 14066  14224           p->openMode = SHELL_OPEN_ZIPFILE;
 14067  14225   #endif
 14068  14226         }else if( optionMatch(z, "append") ){
 14069  14227           p->openMode = SHELL_OPEN_APPENDVFS;
 14070  14228         }else if( optionMatch(z, "readonly") ){
 14071  14229           p->openMode = SHELL_OPEN_READONLY;
        14230  +#ifdef SQLITE_ENABLE_DESERIALIZE
        14231  +      }else if( optionMatch(z, "deserialize") ){
        14232  +        p->openMode = SHELL_OPEN_DESERIALIZE;
        14233  +#endif
 14072  14234         }else if( z[0]=='-' ){
 14073  14235           utf8_printf(stderr, "unknown option: %s\n", z);
 14074  14236           rc = 1;
 14075  14237           goto meta_command_exit;
 14076  14238         }
 14077  14239       }
 14078  14240       /* If a filename is specified, try to open it first */
................................................................................
 14592  14754         pSession->nFilter = 0;
 14593  14755         sqlite3session_table_filter(pSession->p, session_filter, pSession);
 14594  14756         p->nSession++;
 14595  14757         pSession->zName = sqlite3_mprintf("%s", zName);
 14596  14758       }else
 14597  14759       /* If no command name matches, show a syntax error */
 14598  14760       session_syntax_error:
 14599         -    session_help(p);
        14761  +    showHelp(p->out, "session");
 14600  14762     }else
 14601  14763   #endif
 14602  14764   
 14603  14765   #ifdef SQLITE_DEBUG
 14604  14766     /* Undocumented commands for internal testing.  Subject to change
 14605  14767     ** without notice. */
 14606  14768     if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
................................................................................
 15059  15221         { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
 15060  15222         { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
 15061  15223       /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
 15062  15224       /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
 15063  15225         { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
 15064  15226       /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
 15065  15227         { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
        15228  +      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"       },
 15066  15229         { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
 15067  15230         { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
 15068  15231         { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
 15069  15232   #ifdef YYCOVERAGE
 15070  15233         { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
 15071  15234   #endif
 15072  15235         { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
................................................................................
 15153  15316               isOk = 3;
 15154  15317             }
 15155  15318             break;
 15156  15319   
 15157  15320           /* sqlite3_test_control(int, int) */
 15158  15321           case SQLITE_TESTCTRL_ASSERT:
 15159  15322           case SQLITE_TESTCTRL_ALWAYS:
        15323  +        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
 15160  15324             if( nArg==3 ){
 15161  15325               int opt = booleanValue(azArg[2]);
 15162  15326               rc2 = sqlite3_test_control(testctrl, opt);
 15163  15327               isOk = 1;
 15164  15328             }
 15165  15329             break;
 15166  15330   
................................................................................
 15447  15611   /*
 15448  15612   ** We need a default sqlite3_complete() implementation to use in case
 15449  15613   ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
 15450  15614   ** any arbitrary text is a complete SQL statement.  This is not very
 15451  15615   ** user-friendly, but it does seem to work.
 15452  15616   */
 15453  15617   #ifdef SQLITE_OMIT_COMPLETE
 15454         -int sqlite3_complete(const char *zSql){ return 1; }
        15618  +#define sqlite3_complete(x) 1
 15455  15619   #endif
 15456  15620   
 15457  15621   /*
 15458  15622   ** Return true if zSql is a complete SQL statement.  Return false if it
 15459  15623   ** ends in the middle of a string literal or C-style comment.
 15460  15624   */
 15461  15625   static int line_is_complete(char *zSql, int nSql){
................................................................................
 16033  16197         zVfs = cmdline_option_value(argc, argv, ++i);
 16034  16198   #ifdef SQLITE_HAVE_ZLIB
 16035  16199       }else if( strcmp(z,"-zip")==0 ){
 16036  16200         data.openMode = SHELL_OPEN_ZIPFILE;
 16037  16201   #endif
 16038  16202       }else if( strcmp(z,"-append")==0 ){
 16039  16203         data.openMode = SHELL_OPEN_APPENDVFS;
        16204  +#ifdef SQLITE_ENABLE_DESERIALIZE
        16205  +    }else if( strcmp(z,"-deserialize")==0 ){
        16206  +      data.openMode = SHELL_OPEN_DESERIALIZE;
        16207  +#endif
 16040  16208       }else if( strcmp(z,"-readonly")==0 ){
 16041  16209         data.openMode = SHELL_OPEN_READONLY;
 16042  16210   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
 16043  16211       }else if( strncmp(z, "-A",2)==0 ){
 16044  16212         /* All remaining command-line arguments are passed to the ".archive"
 16045  16213         ** command, so ignore them */
 16046  16214         break;
................................................................................
 16128  16296         memcpy(data.colSeparator,",",2);
 16129  16297   #ifdef SQLITE_HAVE_ZLIB
 16130  16298       }else if( strcmp(z,"-zip")==0 ){
 16131  16299         data.openMode = SHELL_OPEN_ZIPFILE;
 16132  16300   #endif
 16133  16301       }else if( strcmp(z,"-append")==0 ){
 16134  16302         data.openMode = SHELL_OPEN_APPENDVFS;
        16303  +#ifdef SQLITE_ENABLE_DESERIALIZE
        16304  +    }else if( strcmp(z,"-deserialize")==0 ){
        16305  +      data.openMode = SHELL_OPEN_DESERIALIZE;
        16306  +#endif
 16135  16307       }else if( strcmp(z,"-readonly")==0 ){
 16136  16308         data.openMode = SHELL_OPEN_READONLY;
 16137  16309       }else if( strcmp(z,"-ascii")==0 ){
 16138  16310         data.mode = MODE_Ascii;
 16139  16311         sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
 16140  16312                          SEP_Unit);
 16141  16313         sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
................................................................................
 16272  16444       }
 16273  16445       free(azCmd);
 16274  16446     }else{
 16275  16447       /* Run commands received from standard input
 16276  16448       */
 16277  16449       if( stdin_is_interactive ){
 16278  16450         char *zHome;
 16279         -      char *zHistory = 0;
        16451  +      char *zHistory;
 16280  16452         int nHistory;
 16281  16453         printf(
 16282  16454           "SQLite version %s %.19s\n" /*extra-version-info*/
 16283  16455           "Enter \".help\" for usage hints.\n",
 16284  16456           sqlite3_libversion(), sqlite3_sourceid()
 16285  16457         );
 16286  16458         if( warnInmemoryDb ){
 16287  16459           printf("Connected to a ");
 16288  16460           printBold("transient in-memory database");
 16289  16461           printf(".\nUse \".open FILENAME\" to reopen on a "
 16290  16462                  "persistent database.\n");
 16291  16463         }
 16292         -      zHome = find_home_dir(0);
 16293         -      if( zHome ){
        16464  +      zHistory = getenv("SQLITE_HISTORY");
        16465  +      if( zHistory ){
        16466  +        zHistory = strdup(zHistory);
        16467  +      }else if( (zHome = find_home_dir(0))!=0 ){
 16294  16468           nHistory = strlen30(zHome) + 20;
 16295  16469           if( (zHistory = malloc(nHistory))!=0 ){
 16296  16470             sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
 16297  16471           }
 16298  16472         }
 16299  16473         if( zHistory ){ shell_read_history(zHistory); }
 16300  16474   #if HAVE_READLINE || HAVE_EDITLINE

Changes to src/skins.c.

   457    457     const char *zCurrent = 0;  /* Current skin */
   458    458     int i;                     /* Loop counter */
   459    459     Stmt q;
   460    460     int seenCurrent = 0;
   461    461     int once;
   462    462   
   463    463     login_check_credentials();
   464         -  if( !g.perm.Setup ){
          464  +  if( !g.perm.Admin ){
   465    465       login_needed(0);
   466    466       return;
   467    467     }
   468    468     db_begin_transaction();
   469    469     zCurrent = getSkin(0);
   470    470     for(i=0; i<count(aBuiltinSkin); i++){
   471    471       aBuiltinSkin[i].zSQL = getSkin(aBuiltinSkin[i].zLabel);
................................................................................
   674    674   static const char *skin_file_content(const char *zLabel, const char *zFile){
   675    675     const char *zResult;
   676    676     if( fossil_strcmp(zLabel, "current")==0 ){
   677    677       zResult = db_get(zFile, "");
   678    678     }else if( sqlite3_strglob("draft[1-9]", zLabel)==0 ){
   679    679       zResult = db_get_mprintf("", "%s-%s", zLabel, zFile);
   680    680     }else{
   681         -    while( 1 ){
          681  +    int i;
          682  +    for(i=0; i<2; i++){
   682    683         char *zKey = mprintf("skins/%s/%s.txt", zLabel, zFile);
   683    684         zResult = builtin_text(zKey);
   684    685         fossil_free(zKey);
   685         -      if( zResult!=0 || fossil_strcmp(zLabel,"default")==0 ) break;
          686  +      if( zResult!=0 ) break;
          687  +      zLabel = "default";
   686    688       }
   687    689     }
   688    690     return zResult;
   689    691   }
   690    692   
   691    693   
   692    694   /*
   693    695   ** WEBPAGE: setup_skinedit
   694    696   **
   695    697   ** Edit aspects of a skin determined by the w= query parameter.
   696         -** Requires Setup privileges.
          698  +** Requires Admin or Setup privileges.
   697    699   **
   698    700   **    w=NUM     -- 0=CSS, 1=footer, 2=header, 3=details, 4=js
   699    701   **    sk=NUM    -- the draft skin number
   700    702   */
   701    703   void setup_skinedit(void){
   702    704     static const struct sSkinAddr {
   703    705       const char *zFile;
................................................................................
   726    728     login_check_credentials();
   727    729   
   728    730     /* Figure out which skin we are editing */
   729    731     iSkin = atoi(PD("sk","1"));
   730    732     if( iSkin<1 || iSkin>9 ) iSkin = 1;
   731    733   
   732    734     /* Check that the user is authorized to edit this skin. */
   733         -  if( !g.perm.Setup ){
          735  +  if( !g.perm.Admin ){
   734    736       char *zAllowedEditors = "";
   735    737       Glob *pAllowedEditors;
   736    738       int isMatch = 0;
   737    739       if( login_is_individual() ){
   738    740         zAllowedEditors = db_get_mprintf("", "draft%d-users", iSkin);
   739    741       }
   740    742       if( zAllowedEditors[0] ){
................................................................................
   899    901     */
   900    902     login_check_credentials();
   901    903     if( !login_is_individual() ){
   902    904       login_needed(0);
   903    905       return;
   904    906     }
   905    907     zAllowedEditors = db_get_mprintf("", "draft%d-users", iSkin);
   906         -  if( g.perm.Setup ){
          908  +  if( g.perm.Admin ){
   907    909       isSetup = isEditor = 1;
   908    910     }else{
   909    911       Glob *pAllowedEditors;
   910    912       isSetup = isEditor = 0;
   911    913       if( zAllowedEditors[0] ){
   912    914         pAllowedEditors = glob_create(zAllowedEditors);
   913    915         isEditor = glob_match(pAllowedEditors, g.zLogin);
................................................................................
  1053   1055     @ <p>Repeat <a href='#step4'>step 4</a> and
  1054   1056     @ <a href='#step5'>step 5</a> as many times as necessary to create
  1055   1057     @ a production-ready skin.
  1056   1058     @
  1057   1059     @ <a name='step7'></a>
  1058   1060     @ <h1>Step 7: Publish</h1>
  1059   1061     @
  1060         -  if( !g.perm.Setup ){
         1062  +  if( !g.perm.Admin ){
  1061   1063       @ <p>Only administrators are allowed to publish draft skins.  Contact
  1062   1064       @ an administrator to get this "draft%d(iSkin)" skin published.</p>
  1063   1065     }else{
  1064   1066       @ <p>When the draft%d(iSkin) skin is ready for production use,
  1065   1067       @ make it the default scan by clicking the acknowledgements and
  1066   1068       @ pressing the button below:</p>
  1067   1069       @
................................................................................
  1078   1080       @ <p>You will probably need to press Reload on your browser after
  1079   1081       @ publishing the new skin.</p>
  1080   1082     }
  1081   1083     @
  1082   1084     @ <a name='step8'></a>
  1083   1085     @ <h1>Step 8: Cleanup and Undo Actions</h1>
  1084   1086     @
  1085         -  if( !g.perm.Setup ){
         1087  +  if( !g.perm.Admin ){
  1086   1088       @ <p>Administrators can optionally save or restore legacy skins, and/or
  1087   1089       @ undo a prior publish.
  1088   1090     }else{
  1089   1091       @ <p>Visit the <a href='%R/setup_skin_admin'>Skin Admin</a> page
  1090   1092       @ for cleanup and recovery actions.
  1091   1093     }
  1092   1094     style_load_one_js_file("skin.js");
  1093   1095     style_footer();
  1094   1096   }

Changes to src/smtp.c.

    17     17   **
    18     18   ** Implementation of SMTP (Simple Mail Transport Protocol) according
    19     19   ** to RFC 5321.
    20     20   */
    21     21   #include "config.h"
    22     22   #include "smtp.h"
    23     23   #include <assert.h>
    24         -#if defined(__linux__) && !defined(FOSSIL_OMIT_DNS)
           24  +#if (HAVE_DN_EXPAND || HAVE___NS_NAME_UNCOMPRESS || HAVE_NS_NAME_UNCOMPRESS) && \
           25  +    (HAVE_NS_PARSERR || HAVE___NS_PARSERR) && !defined(FOSSIL_OMIT_DNS)
    25     26   #  include <sys/types.h>
    26     27   #  include <netinet/in.h>
    27         -#  include <arpa/nameser.h>
    28         -#  include <resolv.h>
           28  +#  if defined(HAVE_BIND_RESOLV_H)
           29  +#    include <bind/resolv.h>
           30  +#    include <bind/arpa/nameser_compat.h>
           31  +#  else
           32  +#    include <arpa/nameser.h>
           33  +#    include <resolv.h>
           34  +#  endif
           35  +#  if defined(HAVENS_NAME_UNCOMPRESS) && !defined(dn_expand)
           36  +#    define dn_expand ns_name_uncompress
           37  +#  endif
           38  +#  if defined(HAVE__NS_NAME_UNCOMPRESS) && !defined(dn_expand)
           39  +#    define dn_expand __ns_name_uncompress
           40  +#  endif
    29     41   #  define FOSSIL_UNIX_STYLE_DNS 1
    30     42   #endif
    31     43   #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)
    32     44   #  include <windows.h>
    33     45   #  include <windns.h>
    34     46   #  define FOSSIL_WINDOWS_STYLE_DNS 1
    35     47   #endif
................................................................................
    75     87         if( priority<iBestPriority ){
    76     88           pBest = p;
    77     89           iBestPriority = priority;
    78     90         }
    79     91       }
    80     92     }
    81     93     if( pBest ){
    82         -    ns_name_uncompress(aDns, aDns+nDns, pBest+2,
    83         -                       zHostname, sizeof(zHostname));
           94  +    dn_expand(aDns, aDns+nDns, pBest+2, zHostname, sizeof(zHostname));
    84     95       return fossil_strdup(zHostname);
    85     96     }
    86     97     return 0;
    87     98   #elif defined(FOSSIL_WINDOWS_STYLE_DNS)
    88     99     DNS_STATUS status;           /* Return status */
    89    100     PDNS_RECORDA pDnsRecord, p;  /* Pointer to DNS_RECORD structure */
    90    101     int iBestPriority = 9999999; /* Best priority */
................................................................................
  1297   1308     if( nErr ){
  1298   1309       fossil_print("Number of incorrect emailblob.enref values: %d\n",nErr);
  1299   1310     }
  1300   1311   }
  1301   1312   
  1302   1313   
  1303   1314   /*
  1304         -** COMMAND: smtpd
         1315  +** COMMAND: smtpd*
  1305   1316   **
  1306   1317   ** Usage: %fossil smtpd [OPTIONS] REPOSITORY
  1307   1318   **
  1308   1319   ** Begin a SMTP conversation with a client using stdin/stdout.  The
  1309   1320   ** received email is stored in REPOSITORY.
  1310   1321   **
  1311   1322   ** Options:
................................................................................
  1444   1455   ** found in the user table's info field, in angle brackets.
  1445   1456   */
  1446   1457   static int pop3_login(const char *zUser, char *zPass){
  1447   1458     return login_search_uid(&zUser, zPass) != 0;
  1448   1459   }
  1449   1460   
  1450   1461   /*
  1451         -** COMMAND: pop3d
         1462  +** COMMAND: pop3d*
  1452   1463   **
  1453   1464   ** Usage: %fossil pop3d [OPTIONS] REPOSITORY
  1454   1465   **
  1455   1466   ** Begin a POP3 conversation with a client using stdin/stdout using
  1456   1467   ** the mailboxes stored in REPOSITORY.
  1457   1468   **
  1458   1469   ** If launched as root, the process first enters a chroot jail using

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.25.1.  By combining all the individual C code files into this
            3  +** version 3.26.0.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
   255    255     "ENABLE_FTS3_TOKENIZER",
   256    256   #endif
   257    257   #if SQLITE_ENABLE_FTS4
   258    258     "ENABLE_FTS4",
   259    259   #endif
   260    260   #if SQLITE_ENABLE_FTS5
   261    261     "ENABLE_FTS5",
          262  +#endif
          263  +#if SQLITE_ENABLE_GEOPOLY
          264  +  "ENABLE_GEOPOLY",
   262    265   #endif
   263    266   #if SQLITE_ENABLE_HIDDEN_COLUMNS
   264    267     "ENABLE_HIDDEN_COLUMNS",
   265    268   #endif
   266    269   #if SQLITE_ENABLE_ICU
   267    270     "ENABLE_ICU",
   268    271   #endif
................................................................................
   285    288     "ENABLE_MEMSYS3",
   286    289   #endif
   287    290   #if SQLITE_ENABLE_MEMSYS5
   288    291     "ENABLE_MEMSYS5",
   289    292   #endif
   290    293   #if SQLITE_ENABLE_MULTIPLEX
   291    294     "ENABLE_MULTIPLEX",
          295  +#endif
          296  +#if SQLITE_ENABLE_NORMALIZE
          297  +  "ENABLE_NORMALIZE",
   292    298   #endif
   293    299   #if SQLITE_ENABLE_NULL_TRIM
   294    300     "ENABLE_NULL_TRIM",
   295    301   #endif
   296    302   #if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
   297    303     "ENABLE_OVERSIZE_CELL_CHECK",
   298    304   #endif
................................................................................
  1152   1158   ** been edited in any way since it was last checked in, then the last
  1153   1159   ** four hexadecimal digits of the hash may be modified.
  1154   1160   **
  1155   1161   ** See also: [sqlite3_libversion()],
  1156   1162   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1157   1163   ** [sqlite_version()] and [sqlite_source_id()].
  1158   1164   */
  1159         -#define SQLITE_VERSION        "3.25.1"
  1160         -#define SQLITE_VERSION_NUMBER 3025001
  1161         -#define SQLITE_SOURCE_ID      "2018-09-18 20:20:44 2ac9003de44da7dafa3fbb1915ac5725a9275c86bf2f3b7aa19321bf1460b386"
         1165  +#define SQLITE_VERSION        "3.26.0"
         1166  +#define SQLITE_VERSION_NUMBER 3026000
         1167  +#define SQLITE_SOURCE_ID      "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9"
  1162   1168   
  1163   1169   /*
  1164   1170   ** CAPI3REF: Run-Time Library Version Numbers
  1165   1171   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1166   1172   **
  1167   1173   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1168   1174   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  3046   3052   ** Existing configuration options might be discontinued.  Applications
  3047   3053   ** should check the return code from [sqlite3_db_config()] to make sure that
  3048   3054   ** the call worked.  ^The [sqlite3_db_config()] interface will return a
  3049   3055   ** non-zero [error code] if a discontinued or unsupported configuration option
  3050   3056   ** is invoked.
  3051   3057   **
  3052   3058   ** <dl>
         3059  +** [[SQLITE_DBCONFIG_LOOKASIDE]]
  3053   3060   ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  3054   3061   ** <dd> ^This option takes three additional arguments that determine the 
  3055   3062   ** [lookaside memory allocator] configuration for the [database connection].
  3056   3063   ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  3057   3064   ** pointer to a memory buffer to use for lookaside memory.
  3058   3065   ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  3059   3066   ** may be NULL in which case SQLite will allocate the
................................................................................
  3068   3075   ** connection is not currently using lookaside memory, or in other words
  3069   3076   ** when the "current value" returned by
  3070   3077   ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  3071   3078   ** Any attempt to change the lookaside memory configuration when lookaside
  3072   3079   ** memory is in use leaves the configuration unchanged and returns 
  3073   3080   ** [SQLITE_BUSY].)^</dd>
  3074   3081   **
         3082  +** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
  3075   3083   ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  3076   3084   ** <dd> ^This option is used to enable or disable the enforcement of
  3077   3085   ** [foreign key constraints].  There should be two additional arguments.
  3078   3086   ** The first argument is an integer which is 0 to disable FK enforcement,
  3079   3087   ** positive to enable FK enforcement or negative to leave FK enforcement
  3080   3088   ** unchanged.  The second parameter is a pointer to an integer into which
  3081   3089   ** is written 0 or 1 to indicate whether FK enforcement is off or on
  3082   3090   ** following this call.  The second parameter may be a NULL pointer, in
  3083   3091   ** which case the FK enforcement setting is not reported back. </dd>
  3084   3092   **
         3093  +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
  3085   3094   ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  3086   3095   ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  3087   3096   ** There should be two additional arguments.
  3088   3097   ** The first argument is an integer which is 0 to disable triggers,
  3089   3098   ** positive to enable triggers or negative to leave the setting unchanged.
  3090   3099   ** The second parameter is a pointer to an integer into which
  3091   3100   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  3092   3101   ** following this call.  The second parameter may be a NULL pointer, in
  3093   3102   ** which case the trigger setting is not reported back. </dd>
  3094   3103   **
         3104  +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  3095   3105   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  3096   3106   ** <dd> ^This option is used to enable or disable the two-argument
  3097   3107   ** version of the [fts3_tokenizer()] function which is part of the
  3098   3108   ** [FTS3] full-text search engine extension.
  3099   3109   ** There should be two additional arguments.
  3100   3110   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  3101   3111   ** positive to enable fts3_tokenizer() or negative to leave the setting
  3102   3112   ** unchanged.
  3103   3113   ** The second parameter is a pointer to an integer into which
  3104   3114   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
  3105   3115   ** following this call.  The second parameter may be a NULL pointer, in
  3106   3116   ** which case the new setting is not reported back. </dd>
  3107   3117   **
         3118  +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
  3108   3119   ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
  3109   3120   ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
  3110   3121   ** interface independently of the [load_extension()] SQL function.
  3111   3122   ** The [sqlite3_enable_load_extension()] API enables or disables both the
  3112   3123   ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
  3113   3124   ** There should be two additional arguments.
  3114   3125   ** When the first argument to this interface is 1, then only the C-API is
................................................................................
  3118   3129   ** C-API or the SQL function.
  3119   3130   ** The second parameter is a pointer to an integer into which
  3120   3131   ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
  3121   3132   ** is disabled or enabled following this call.  The second parameter may
  3122   3133   ** be a NULL pointer, in which case the new setting is not reported back.
  3123   3134   ** </dd>
  3124   3135   **
  3125         -** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
         3136  +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
  3126   3137   ** <dd> ^This option is used to change the name of the "main" database
  3127   3138   ** schema.  ^The sole argument is a pointer to a constant UTF8 string
  3128   3139   ** which will become the new schema name in place of "main".  ^SQLite
  3129   3140   ** does not make a copy of the new main schema name string, so the application
  3130   3141   ** must ensure that the argument passed into this DBCONFIG option is unchanged
  3131   3142   ** until after the database connection closes.
  3132   3143   ** </dd>
  3133   3144   **
         3145  +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
  3134   3146   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  3135   3147   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  3136   3148   ** database handle, SQLite checks if this will mean that there are now no 
  3137   3149   ** connections at all to the database. If so, it performs a checkpoint 
  3138   3150   ** operation before closing the connection. This option may be used to
  3139   3151   ** override this behaviour. The first parameter passed to this operation
  3140   3152   ** is an integer - positive to disable checkpoints-on-close, or zero (the
  3141   3153   ** default) to enable them, and negative to leave the setting unchanged.
  3142   3154   ** The second parameter is a pointer to an integer
  3143   3155   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3144   3156   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3145   3157   ** </dd>
  3146   3158   **
  3147         -** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
         3159  +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3148   3160   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3149   3161   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3150   3162   ** a single SQL query statement will always use the same algorithm regardless
  3151   3163   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3152   3164   ** that look at the values of bound parameters, which can make some queries
  3153   3165   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3154   3166   ** the QPSG active, SQLite will always use the same query plan in the field as
................................................................................
  3156   3168   ** The first argument to this setting is an integer which is 0 to disable 
  3157   3169   ** the QPSG, positive to enable QPSG, or negative to leave the setting
  3158   3170   ** unchanged. The second parameter is a pointer to an integer into which
  3159   3171   ** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
  3160   3172   ** following this call.
  3161   3173   ** </dd>
  3162   3174   **
  3163         -** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
         3175  +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  3164   3176   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  3165   3177   ** include output for any operations performed by trigger programs. This
  3166   3178   ** option is used to set or clear (the default) a flag that governs this
  3167   3179   ** behavior. The first parameter passed to this operation is an integer -
  3168   3180   ** positive to enable output for trigger programs, or zero to disable it,
  3169   3181   ** or negative to leave the setting unchanged.
  3170   3182   ** The second parameter is a pointer to an integer into which is written 
  3171   3183   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3172   3184   ** it is not disabled, 1 if it is.  
  3173   3185   ** </dd>
  3174   3186   **
  3175         -** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
         3187  +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
  3176   3188   ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
  3177   3189   ** [VACUUM] in order to reset a database back to an empty database
  3178   3190   ** with no schema and no content. The following process works even for
  3179   3191   ** a badly corrupted database file:
  3180   3192   ** <ol>
  3181   3193   ** <li> If the database connection is newly opened, make sure it has read the
  3182   3194   **      database schema by preparing then discarding some query against the
................................................................................
  3187   3199   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
  3188   3200   ** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
  3189   3201   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
  3190   3202   ** </ol>
  3191   3203   ** Because resetting a database is destructive and irreversible, the
  3192   3204   ** process requires the use of this obscure API and multiple steps to help
  3193   3205   ** ensure that it does not happen by accident.
         3206  +**
         3207  +** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
         3208  +** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
         3209  +** "defensive" flag for a database connection.  When the defensive
         3210  +** flag is enabled, language features that allow ordinary SQL to 
         3211  +** deliberately corrupt the database file are disabled.  The disabled
         3212  +** features include but are not limited to the following:
         3213  +** <ul>
         3214  +** <li> The [PRAGMA writable_schema=ON] statement.
         3215  +** <li> Writes to the [sqlite_dbpage] virtual table.
         3216  +** <li> Direct writes to [shadow tables].
         3217  +** </ul>
  3194   3218   ** </dd>
  3195   3219   ** </dl>
  3196   3220   */
  3197   3221   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3198   3222   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3199   3223   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3200   3224   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3201   3225   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3202   3226   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3203   3227   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3204   3228   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3205   3229   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  3206   3230   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  3207         -#define SQLITE_DBCONFIG_MAX                   1009 /* Largest DBCONFIG */
         3231  +#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
         3232  +#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
  3208   3233   
  3209   3234   /*
  3210   3235   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3211   3236   ** METHOD: sqlite3
  3212   3237   **
  3213   3238   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3214   3239   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  4638   4663   ** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
  4639   4664   ** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
  4640   4665   ** be used just once or at most a few times and then destroyed using
  4641   4666   ** [sqlite3_finalize()] relatively soon. The current implementation acts
  4642   4667   ** on this hint by avoiding the use of [lookaside memory] so as not to
  4643   4668   ** deplete the limited store of lookaside memory. Future versions of
  4644   4669   ** SQLite may act on this hint differently.
         4670  +**
         4671  +** [[SQLITE_PREPARE_NORMALIZE]] ^(<dt>SQLITE_PREPARE_NORMALIZE</dt>
         4672  +** <dd>The SQLITE_PREPARE_NORMALIZE flag indicates that a normalized
         4673  +** representation of the SQL statement should be calculated and then
         4674  +** associated with the prepared statement, which can be obtained via
         4675  +** the [sqlite3_normalized_sql()] interface.)^  The semantics used to
         4676  +** normalize a SQL statement are unspecified and subject to change.
         4677  +** At a minimum, literal values will be replaced with suitable
         4678  +** placeholders.
  4645   4679   ** </dl>
  4646   4680   */
  4647   4681   #define SQLITE_PREPARE_PERSISTENT              0x01
         4682  +#define SQLITE_PREPARE_NORMALIZE               0x02
  4648   4683   
  4649   4684   /*
  4650   4685   ** CAPI3REF: Compiling An SQL Statement
  4651   4686   ** KEYWORDS: {SQL statement compiler}
  4652   4687   ** METHOD: sqlite3
  4653   4688   ** CONSTRUCTOR: sqlite3_stmt
  4654   4689   **
................................................................................
  4798   4833   ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
  4799   4834   ** SQL text used to create [prepared statement] P if P was
  4800   4835   ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
  4801   4836   ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
  4802   4837   ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
  4803   4838   ** string containing the SQL text of prepared statement P with
  4804   4839   ** [bound parameters] expanded.
         4840  +** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
         4841  +** string containing the normalized SQL text of prepared statement P.  The
         4842  +** semantics used to normalize a SQL statement are unspecified and subject
         4843  +** to change.  At a minimum, literal values will be replaced with suitable
         4844  +** placeholders.
  4805   4845   **
  4806   4846   ** ^(For example, if a prepared statement is created using the SQL
  4807   4847   ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
  4808   4848   ** and parameter :xyz is unbound, then sqlite3_sql() will return
  4809   4849   ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
  4810   4850   ** will return "SELECT 2345,NULL".)^
  4811   4851   **
................................................................................
  4813   4853   ** is available to hold the result, or if the result would exceed the
  4814   4854   ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
  4815   4855   **
  4816   4856   ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
  4817   4857   ** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
  4818   4858   ** option causes sqlite3_expanded_sql() to always return NULL.
  4819   4859   **
  4820         -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
  4821         -** automatically freed when the prepared statement is finalized.
         4860  +** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
         4861  +** are managed by SQLite and are automatically freed when the prepared
         4862  +** statement is finalized.
  4822   4863   ** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
  4823   4864   ** is obtained from [sqlite3_malloc()] and must be free by the application
  4824   4865   ** by passing it to [sqlite3_free()].
  4825   4866   */
  4826   4867   SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  4827   4868   SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
         4869  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
  4828   4870   
  4829   4871   /*
  4830   4872   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  4831   4873   ** METHOD: sqlite3_stmt
  4832   4874   **
  4833   4875   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  4834   4876   ** and only if the [prepared statement] X makes no direct changes to
................................................................................
  7310   7352                          void **ppArg);
  7311   7353     int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  7312   7354     /* The methods above are in version 1 of the sqlite_module object. Those 
  7313   7355     ** below are for version 2 and greater. */
  7314   7356     int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  7315   7357     int (*xRelease)(sqlite3_vtab *pVTab, int);
  7316   7358     int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
         7359  +  /* The methods above are in versions 1 and 2 of the sqlite_module object.
         7360  +  ** Those below are for version 3 and greater. */
         7361  +  int (*xShadowName)(const char*);
  7317   7362   };
  7318   7363   
  7319   7364   /*
  7320   7365   ** CAPI3REF: Virtual Table Indexing Information
  7321   7366   ** KEYWORDS: sqlite3_index_info
  7322   7367   **
  7323   7368   ** The sqlite3_index_info structure and its substructures is used as part
................................................................................
  8232   8277   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  8233   8278   #define SQLITE_TESTCTRL_ASSERT                  12
  8234   8279   #define SQLITE_TESTCTRL_ALWAYS                  13
  8235   8280   #define SQLITE_TESTCTRL_RESERVE                 14
  8236   8281   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  8237   8282   #define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
  8238   8283   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
         8284  +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
  8239   8285   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  8240   8286   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  8241   8287   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  8242   8288   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  8243   8289   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  8244   8290   #define SQLITE_TESTCTRL_BYTEORDER               22
  8245   8291   #define SQLITE_TESTCTRL_ISINIT                  23
................................................................................
  9644   9690   ** CAPI3REF: Virtual Table Configuration Options
  9645   9691   **
  9646   9692   ** These macros define the various options to the
  9647   9693   ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  9648   9694   ** can use to customize and optimize their behavior.
  9649   9695   **
  9650   9696   ** <dl>
         9697  +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
  9651   9698   ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  9652   9699   ** <dd>Calls of the form
  9653   9700   ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  9654   9701   ** where X is an integer.  If X is zero, then the [virtual table] whose
  9655   9702   ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  9656   9703   ** support constraints.  In this configuration (which is the default) if
  9657   9704   ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
................................................................................
 10413  10460     unsigned int *anQueue;            /* Number of pending entries in the queue */
 10414  10461     int nCoord;                       /* Number of coordinates */
 10415  10462     int iLevel;                       /* Level of current node or entry */
 10416  10463     int mxLevel;                      /* The largest iLevel value in the tree */
 10417  10464     sqlite3_int64 iRowid;             /* Rowid for current entry */
 10418  10465     sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
 10419  10466     int eParentWithin;                /* Visibility of parent node */
 10420         -  int eWithin;                      /* OUT: Visiblity */
        10467  +  int eWithin;                      /* OUT: Visibility */
 10421  10468     sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
 10422  10469     /* The following fields are only available in 3.8.11 and later */
 10423  10470     sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
 10424  10471   };
 10425  10472   
 10426  10473   /*
 10427  10474   ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
................................................................................
 10909  10956   ** [sqlite3changeset_invert()] functions, all changes within the changeset 
 10910  10957   ** that apply to a single table are grouped together. This means that when 
 10911  10958   ** an application iterates through a changeset using an iterator created by 
 10912  10959   ** this function, all changes that relate to a single table are visited 
 10913  10960   ** consecutively. There is no chance that the iterator will visit a change 
 10914  10961   ** the applies to table X, then one for table Y, and then later on visit 
 10915  10962   ** another change for table X.
        10963  +**
        10964  +** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
        10965  +** may be modified by passing a combination of
        10966  +** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
        10967  +**
        10968  +** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
        10969  +** and therefore subject to change.
 10916  10970   */
 10917  10971   SQLITE_API int sqlite3changeset_start(
 10918  10972     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
 10919  10973     int nChangeset,                 /* Size of changeset blob in bytes */
 10920  10974     void *pChangeset                /* Pointer to blob containing changeset */
 10921  10975   );
        10976  +SQLITE_API int sqlite3changeset_start_v2(
        10977  +  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
        10978  +  int nChangeset,                 /* Size of changeset blob in bytes */
        10979  +  void *pChangeset,               /* Pointer to blob containing changeset */
        10980  +  int flags                       /* SESSION_CHANGESETSTART_* flags */
        10981  +);
        10982  +
        10983  +/*
        10984  +** CAPI3REF: Flags for sqlite3changeset_start_v2
        10985  +**
        10986  +** The following flags may passed via the 4th parameter to
        10987  +** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
        10988  +**
        10989  +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
        10990  +**   Invert the changeset while iterating through it. This is equivalent to
        10991  +**   inverting a changeset using sqlite3changeset_invert() before applying it.
        10992  +**   It is an error to specify this flag with a patchset.
        10993  +*/
        10994  +#define SQLITE_CHANGESETSTART_INVERT        0x0002
 10922  10995   
 10923  10996   
 10924  10997   /*
 10925  10998   ** CAPI3REF: Advance A Changeset Iterator
 10926  10999   ** METHOD: sqlite3_changeset_iter
 10927  11000   **
 10928  11001   ** This function may only be used with iterators created by function
................................................................................
 11569  11642     int(*xConflict)(
 11570  11643       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11571  11644       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11572  11645       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11573  11646     ),
 11574  11647     void *pCtx,                     /* First argument passed to xConflict */
 11575  11648     void **ppRebase, int *pnRebase, /* OUT: Rebase data */
 11576         -  int flags                       /* Combination of SESSION_APPLY_* flags */
        11649  +  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
 11577  11650   );
 11578  11651   
 11579  11652   /*
 11580  11653   ** CAPI3REF: Flags for sqlite3changeset_apply_v2
 11581  11654   **
 11582  11655   ** The following flags may passed via the 9th parameter to
 11583  11656   ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
................................................................................
 11587  11660   **   Usually, the sessions module encloses all operations performed by
 11588  11661   **   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
 11589  11662   **   SAVEPOINT is committed if the changeset or patchset is successfully
 11590  11663   **   applied, or rolled back if an error occurs. Specifying this flag
 11591  11664   **   causes the sessions module to omit this savepoint. In this case, if the
 11592  11665   **   caller has an open transaction or savepoint when apply_v2() is called, 
 11593  11666   **   it may revert the partially applied changeset by rolling it back.
        11667  +**
        11668  +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
        11669  +**   Invert the changeset before applying it. This is equivalent to inverting
        11670  +**   a changeset using sqlite3changeset_invert() before applying it. It is
        11671  +**   an error to specify this flag with a patchset.
 11594  11672   */
 11595  11673   #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
        11674  +#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
 11596  11675   
 11597  11676   /* 
 11598  11677   ** CAPI3REF: Constants Passed To The Conflict Handler
 11599  11678   **
 11600  11679   ** Values that may be passed as the second argument to a conflict-handler.
 11601  11680   **
 11602  11681   ** <dl>
................................................................................
 11981  12060     int (*xOutput)(void *pOut, const void *pData, int nData),
 11982  12061     void *pOut
 11983  12062   );
 11984  12063   SQLITE_API int sqlite3changeset_start_strm(
 11985  12064     sqlite3_changeset_iter **pp,
 11986  12065     int (*xInput)(void *pIn, void *pData, int *pnData),
 11987  12066     void *pIn
        12067  +);
        12068  +SQLITE_API int sqlite3changeset_start_v2_strm(
        12069  +  sqlite3_changeset_iter **pp,
        12070  +  int (*xInput)(void *pIn, void *pData, int *pnData),
        12071  +  void *pIn,
        12072  +  int flags
 11988  12073   );
 11989  12074   SQLITE_API int sqlite3session_changeset_strm(
 11990  12075     sqlite3_session *pSession,
 11991  12076     int (*xOutput)(void *pOut, const void *pData, int nData),
 11992  12077     void *pOut
 11993  12078   );
 11994  12079   SQLITE_API int sqlite3session_patchset_strm(
................................................................................
 12008  12093     sqlite3_rebaser *pRebaser,
 12009  12094     int (*xInput)(void *pIn, void *pData, int *pnData),
 12010  12095     void *pIn,
 12011  12096     int (*xOutput)(void *pOut, const void *pData, int nData),
 12012  12097     void *pOut
 12013  12098   );
 12014  12099   
        12100  +/*
        12101  +** CAPI3REF: Configure global parameters
        12102  +**
        12103  +** The sqlite3session_config() interface is used to make global configuration
        12104  +** changes to the sessions module in order to tune it to the specific needs 
        12105  +** of the application.
        12106  +**
        12107  +** The sqlite3session_config() interface is not threadsafe. If it is invoked
        12108  +** while any other thread is inside any other sessions method then the
        12109  +** results are undefined. Furthermore, if it is invoked after any sessions
        12110  +** related objects have been created, the results are also undefined. 
        12111  +**
        12112  +** The first argument to the sqlite3session_config() function must be one
        12113  +** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The 
        12114  +** interpretation of the (void*) value passed as the second parameter and
        12115  +** the effect of calling this function depends on the value of the first
        12116  +** parameter.
        12117  +**
        12118  +** <dl>
        12119  +** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
        12120  +**    By default, the sessions module streaming interfaces attempt to input
        12121  +**    and output data in approximately 1 KiB chunks. This operand may be used
        12122  +**    to set and query the value of this configuration setting. The pointer
        12123  +**    passed as the second argument must point to a value of type (int).
        12124  +**    If this value is greater than 0, it is used as the new streaming data
        12125  +**    chunk size for both input and output. Before returning, the (int) value
        12126  +**    pointed to by pArg is set to the final value of the streaming interface
        12127  +**    chunk size.
        12128  +** </dl>
        12129  +**
        12130  +** This function returns SQLITE_OK if successful, or an SQLite error code
        12131  +** otherwise.
        12132  +*/
        12133  +SQLITE_API int sqlite3session_config(int op, void *pArg);
        12134  +
        12135  +/*
        12136  +** CAPI3REF: Values for sqlite3session_config().
        12137  +*/
        12138  +#define SQLITE_SESSION_CONFIG_STRMSIZE 1
 12015  12139   
 12016  12140   /*
 12017  12141   ** Make sure we can call this stuff from C++.
 12018  12142   */
 12019  12143   #if 0
 12020  12144   }
 12021  12145   #endif
................................................................................
 15273  15397   
 15274  15398   #ifndef SQLITE_OMIT_WAL
 15275  15399   SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
 15276  15400   SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
 15277  15401   SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
 15278  15402   SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
 15279  15403   SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
 15280         -# ifdef SQLITE_DIRECT_OVERFLOW_READ
 15281         -SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager, Pgno);
 15282         -# endif
 15283  15404   # ifdef SQLITE_ENABLE_SNAPSHOT
 15284  15405   SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
 15285  15406   SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
 15286  15407   SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
 15287  15408   SQLITE_PRIVATE   int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
 15288  15409   SQLITE_PRIVATE   void sqlite3PagerSnapshotUnlock(Pager *pPager);
 15289  15410   # endif
 15290         -#else
 15291         -# define sqlite3PagerUseWal(x,y) 0
        15411  +#endif
        15412  +
        15413  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        15414  +SQLITE_PRIVATE   int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
 15292  15415   #endif
 15293  15416   
 15294  15417   #ifdef SQLITE_ENABLE_ZIPVFS
 15295  15418   SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
 15296  15419   #endif
 15297  15420   
 15298  15421   /* Functions used to query pager state and configuration. */
................................................................................
 15528  15651   
 15529  15652   /* Return the header size */
 15530  15653   SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
 15531  15654   SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
 15532  15655   
 15533  15656   /* Number of dirty pages as a percentage of the configured cache size */
 15534  15657   SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
        15658  +
        15659  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        15660  +SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
        15661  +#endif
 15535  15662   
 15536  15663   #endif /* _PCACHE_H_ */
 15537  15664   
 15538  15665   /************** End of pcache.h **********************************************/
 15539  15666   /************** Continuing where we left off in sqliteInt.h ******************/
 15540  15667   /************** Include os.h in the middle of sqliteInt.h ********************/
 15541  15668   /************** Begin file os.h **********************************************/
................................................................................
 16034  16161   };
 16035  16162   
 16036  16163   /*
 16037  16164   ** A hash table for built-in function definitions.  (Application-defined
 16038  16165   ** functions use a regular table table from hash.h.)
 16039  16166   **
 16040  16167   ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
 16041         -** Collisions are on the FuncDef.u.pHash chain.
        16168  +** Collisions are on the FuncDef.u.pHash chain.  Use the SQLITE_FUNC_HASH()
        16169  +** macro to compute a hash on the function name.
 16042  16170   */
 16043  16171   #define SQLITE_FUNC_HASH_SZ 23
 16044  16172   struct FuncDefHash {
 16045  16173     FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
 16046  16174   };
        16175  +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
 16047  16176   
 16048  16177   #ifdef SQLITE_USER_AUTHENTICATION
 16049  16178   /*
 16050  16179   ** Information held in the "sqlite3" database connection object and used
 16051  16180   ** to manage user authentication.
 16052  16181   */
 16053  16182   typedef struct sqlite3_userauth sqlite3_userauth;
................................................................................
 16100  16229     sqlite3_vfs *pVfs;            /* OS Interface */
 16101  16230     struct Vdbe *pVdbe;           /* List of active virtual machines */
 16102  16231     CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
 16103  16232     sqlite3_mutex *mutex;         /* Connection mutex */
 16104  16233     Db *aDb;                      /* All backends */
 16105  16234     int nDb;                      /* Number of backends currently in use */
 16106  16235     u32 mDbFlags;                 /* flags recording internal state */
 16107         -  u32 flags;                    /* flags settable by pragmas. See below */
        16236  +  u64 flags;                    /* flags settable by pragmas. See below */
 16108  16237     i64 lastRowid;                /* ROWID of most recent insert (see above) */
 16109  16238     i64 szMmap;                   /* Default mmap_size setting */
 16110  16239     u32 nSchemaLock;              /* Do not reset the schema when non-zero */
 16111  16240     unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
 16112  16241     int errCode;                  /* Most recent error code (SQLITE_*) */
 16113  16242     int errMask;                  /* & result codes with this before returning */
 16114  16243     int iSysErrno;                /* Errno value from last system error */
................................................................................
 16265  16394   #define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
 16266  16395   #define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
 16267  16396   #define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
 16268  16397   #define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
 16269  16398   #define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
 16270  16399   #define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
 16271  16400   #define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
        16401  +#define SQLITE_LegacyAlter    0x04000000  /* Legacy ALTER TABLE behaviour */
        16402  +#define SQLITE_NoSchemaError  0x08000000  /* Do not report schema parse errors*/
        16403  +#define SQLITE_Defensive      0x10000000  /* Input SQL is likely hostile */
 16272  16404   
 16273  16405   /* Flags used only if debugging */
        16406  +#define HI(X)  ((u64)(X)<<32)
 16274  16407   #ifdef SQLITE_DEBUG
 16275         -#define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
 16276         -#define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
 16277         -#define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
 16278         -#define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
 16279         -#define SQLITE_VdbeEQP        0x80000000  /* Debug EXPLAIN QUERY PLAN */
        16408  +#define SQLITE_SqlTrace       HI(0x0001)  /* Debug print SQL as it executes */
        16409  +#define SQLITE_VdbeListing    HI(0x0002)  /* Debug listings of VDBE progs */
        16410  +#define SQLITE_VdbeTrace      HI(0x0004)  /* True to trace VDBE execution */
        16411  +#define SQLITE_VdbeAddopTrace HI(0x0008)  /* Trace sqlite3VdbeAddOp() calls */
        16412  +#define SQLITE_VdbeEQP        HI(0x0010)  /* Debug EXPLAIN QUERY PLAN */
 16280  16413   #endif
 16281  16414   
 16282  16415   /*
 16283  16416   ** Allowed values for sqlite3.mDbFlags
 16284  16417   */
 16285  16418   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 16286  16419   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
................................................................................
 16406  16539   #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
 16407  16540   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 16408  16541   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 16409  16542   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 16410  16543                                       ** single query - might change over time */
 16411  16544   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
 16412  16545   #define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
 16413         -#define SQLITE_FUNC_WINDOW  0x10000 /* Built-in window-only function */
 16414         -#define SQLITE_FUNC_WINDOW_SIZE  0x20000  /* Requires partition size as arg. */
        16546  +#define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
        16547  +#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
        16548  +#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
 16415  16549   
 16416  16550   /*
 16417  16551   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 16418  16552   ** used to create the initializers for the FuncDef structures.
 16419  16553   **
 16420  16554   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 16421  16555   **     Used to create a scalar function definition of a function zName
................................................................................
 16483  16617      (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
 16484  16618   #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue) \
 16485  16619     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
 16486  16620      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}}
 16487  16621   #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
 16488  16622     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
 16489  16623      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}}
 16490         -
 16491  16624   #define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
 16492  16625     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
 16493  16626      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
        16627  +#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
        16628  +  {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
        16629  +   0, 0, xFunc, 0, 0, 0, #zName, {0} }
        16630  +
 16494  16631   
 16495  16632   /*
 16496  16633   ** All current savepoints are stored in a linked list starting at
 16497  16634   ** sqlite3.pSavepoint. The first element in the list is the most recently
 16498  16635   ** opened savepoint. Savepoints are added to the list by the vdbe
 16499  16636   ** OP_Savepoint instruction.
 16500  16637   */
................................................................................
 16671  16808   /*
 16672  16809   ** The schema for each SQL table and view is represented in memory
 16673  16810   ** by an instance of the following structure.
 16674  16811   */
 16675  16812   struct Table {
 16676  16813     char *zName;         /* Name of the table or view */
 16677  16814     Column *aCol;        /* Information about each column */
        16815  +#ifdef SQLITE_ENABLE_NORMALIZE
        16816  +  Hash *pColHash;      /* All columns indexed by name */
        16817  +#endif
 16678  16818     Index *pIndex;       /* List of SQL indexes on this table. */
 16679  16819     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 16680  16820     FKey *pFKey;         /* Linked list of all foreign keys in this table */
 16681  16821     char *zColAff;       /* String defining the affinity of each column */
 16682  16822     ExprList *pCheck;    /* All CHECK constraints */
 16683  16823                          /*   ... also used as column name list in a VIEW */
 16684  16824     int tnum;            /* Root BTree page for this table */
................................................................................
 16721  16861   #define TF_HasStat1        0x0010    /* nRowLogEst set from sqlite_stat1 */
 16722  16862   #define TF_WithoutRowid    0x0020    /* No rowid.  PRIMARY KEY is the key */
 16723  16863   #define TF_NoVisibleRowid  0x0040    /* No user-visible "rowid" column */
 16724  16864   #define TF_OOOHidden       0x0080    /* Out-of-Order hidden columns */
 16725  16865   #define TF_StatsUsed       0x0100    /* Query planner decisions affected by
 16726  16866                                        ** Index.aiRowLogEst[] values */
 16727  16867   #define TF_HasNotNull      0x0200    /* Contains NOT NULL constraints */
        16868  +#define TF_Shadow          0x0400    /* True for a shadow table */
 16728  16869   
 16729  16870   /*
 16730  16871   ** Test to see whether or not a table is a virtual table.  This is
 16731  16872   ** done as a macro so that it will be optimized out when virtual
 16732  16873   ** table support is omitted from the build.
 16733  16874   */
 16734  16875   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 17007  17148     void *p;          /* Pointer to sampled record */
 17008  17149     int n;            /* Size of record in bytes */
 17009  17150     tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
 17010  17151     tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
 17011  17152     tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
 17012  17153   };
 17013  17154   
        17155  +/*
        17156  +** Possible values to use within the flags argument to sqlite3GetToken().
        17157  +*/
        17158  +#define SQLITE_TOKEN_QUOTED    0x1 /* Token is a quoted identifier. */
        17159  +#define SQLITE_TOKEN_KEYWORD   0x2 /* Token is a keyword. */
        17160  +
 17014  17161   /*
 17015  17162   ** Each token coming out of the lexer is an instance of
 17016  17163   ** this structure.  Tokens are also used as part of an expression.
 17017  17164   **
 17018  17165   ** The memory that "z" points to is owned by other objects.  Take care
 17019  17166   ** that the owner of the "z" string does not deallocate the string before
 17020  17167   ** the Token goes out of scope!  Very often, the "z" points to some place
................................................................................
 17188  17335                            ** TK_SELECT_COLUMN: column of the result vector */
 17189  17336     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 17190  17337     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 17191  17338     u8 op2;                /* TK_REGISTER: original value of Expr.op
 17192  17339                            ** TK_COLUMN: the value of p5 for OP_Column
 17193  17340                            ** TK_AGG_FUNCTION: nesting depth */
 17194  17341     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 17195         -  Table *pTab;           /* Table for TK_COLUMN expressions.  Can be NULL
 17196         -                         ** for a column of an index on an expression */
 17197         -#ifndef SQLITE_OMIT_WINDOWFUNC
 17198         -  Window *pWin;          /* Window definition for window functions */
 17199         -#endif
        17342  +  union {
        17343  +    Table *pTab;           /* TK_COLUMN: Table containing column. Can be NULL
        17344  +                           ** for a column of an index on an expression */
        17345  +    Window *pWin;          /* TK_FUNCTION: Window definition for the func */
        17346  +  } y;
 17200  17347   };
 17201  17348   
 17202  17349   /*
 17203  17350   ** The following are the meanings of bits in the Expr.flags field.
 17204  17351   */
 17205  17352   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 17206  17353   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
................................................................................
 17222  17369   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 17223  17370   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 17224  17371   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 17225  17372   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 17226  17373   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 17227  17374   #define EP_Alias     0x400000 /* Is an alias for a result set column */
 17228  17375   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
        17376  +#define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
 17229  17377   
 17230  17378   /*
 17231  17379   ** The EP_Propagate mask is a set of properties that automatically propagate
 17232  17380   ** upwards into parent nodes.
 17233  17381   */
 17234  17382   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
 17235  17383   
................................................................................
 17907  18055   **    OPFLAG_SEEKEQ       == BTREE_SEEK_EQ
 17908  18056   **    OPFLAG_FORDELETE    == BTREE_FORDELETE
 17909  18057   **    OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
 17910  18058   **    OPFLAG_AUXDELETE    == BTREE_AUXDELETE
 17911  18059   */
 17912  18060   #define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
 17913  18061                                        /* Also used in P2 (not P5) of OP_Delete */
        18062  +#define OPFLAG_NOCHNG        0x01    /* OP_VColumn nochange for UPDATE */
 17914  18063   #define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
 17915  18064   #define OPFLAG_LASTROWID     0x20    /* Set to update db->lastRowid */
 17916  18065   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 17917  18066   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 17918  18067   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 17919  18068   #define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
 17920  18069   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
................................................................................
 18125  18274     void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx);  /* Callback */
 18126  18275     void *pVdbeBranchArg;                                     /* 1st argument */
 18127  18276   #endif
 18128  18277   #ifndef SQLITE_UNTESTABLE
 18129  18278     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
 18130  18279   #endif
 18131  18280     int bLocaltimeFault;              /* True to fail localtime() calls */
        18281  +  int bInternalFunctions;           /* Internal SQL functions are visible */
 18132  18282     int iOnceResetThreshold;          /* When to reset OP_Once counters */
 18133  18283     u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
 18134  18284   };
 18135  18285   
 18136  18286   /*
 18137  18287   ** This macro is used inside of assert() statements to indicate that
 18138  18288   ** the assert is only valid on a well-formed database.  Instead of:
................................................................................
 18378  18528   SQLITE_PRIVATE int sqlite3IsIdChar(u8);
 18379  18529   
 18380  18530   /*
 18381  18531   ** Internal function prototypes
 18382  18532   */
 18383  18533   SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
 18384  18534   SQLITE_PRIVATE int sqlite3Strlen30(const char*);
        18535  +#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
 18385  18536   SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
 18386  18537   #define sqlite3StrNICmp sqlite3_strnicmp
 18387  18538   
 18388  18539   SQLITE_PRIVATE int sqlite3MallocInit(void);
 18389  18540   SQLITE_PRIVATE void sqlite3MallocEnd(void);
 18390  18541   SQLITE_PRIVATE void *sqlite3Malloc(u64);
 18391  18542   SQLITE_PRIVATE void *sqlite3MallocZero(u64);
................................................................................
 18494  18645   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 18495  18646   #endif
 18496  18647   
 18497  18648   #if defined(SQLITE_DEBUG)
 18498  18649   SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
 18499  18650   SQLITE_PRIVATE   void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
 18500  18651   SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
        18652  +SQLITE_PRIVATE   void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
 18501  18653   SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
 18502  18654   SQLITE_PRIVATE   void sqlite3TreeViewWith(TreeView*, const With*, u8);
 18503  18655   #ifndef SQLITE_OMIT_WINDOWFUNC
 18504  18656   SQLITE_PRIVATE   void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
 18505  18657   SQLITE_PRIVATE   void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
 18506  18658   #endif
 18507  18659   #endif
................................................................................
 18726  18878   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 18727  18879   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
 18728  18880   #endif
 18729  18881   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 18730  18882   SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 18731  18883   SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 18732  18884   SQLITE_PRIVATE int sqlite3IsRowid(const char*);
        18885  +#ifdef SQLITE_ENABLE_NORMALIZE
        18886  +SQLITE_PRIVATE int sqlite3IsRowidN(const char*, int);
        18887  +#endif
 18733  18888   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 18734  18889       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
 18735  18890   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
 18736  18891   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 18737  18892   SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
        18893  +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
 18738  18894   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 18739  18895                                        u8,u8,int,int*,int*,Upsert*);
 18740  18896   #ifdef SQLITE_ENABLE_NULL_TRIM
 18741  18897   SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
 18742  18898   #else
 18743  18899   # define sqlite3SetMakeRecordP5(A,B)
 18744  18900   #endif
................................................................................
 18751  18907   SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
 18752  18908   SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
 18753  18909   SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 18754  18910   SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 18755  18911   SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 18756  18912   SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 18757  18913   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
        18914  +#ifdef SQLITE_ENABLE_NORMALIZE
        18915  +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(int,const char*,int);
        18916  +#endif
 18758  18917   SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
 18759  18918   SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
 18760  18919   SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
 18761  18920   SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 18762  18921   SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
 18763  18922   SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 18764  18923   SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
................................................................................
 18908  19067   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 18909  19068   SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
 18910  19069   SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
 18911  19070   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
 18912  19071   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 18913  19072   SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 18914  19073   SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
        19074  +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
 18915  19075   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 18916  19076   SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 18917  19077   SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 18918  19078   SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
 18919  19079   SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
 18920  19080   SQLITE_PRIVATE int sqlite3AbsInt32(int);
 18921  19081   #ifdef SQLITE_ENABLE_8_3_NAMES
................................................................................
 18954  19114   #endif
 18955  19115   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 18956  19116   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 18957  19117   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 18958  19118   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 18959  19119   SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
 18960  19120   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
        19121  +#ifdef SQLITE_ENABLE_NORMALIZE
        19122  +SQLITE_PRIVATE int sqlite3GetTokenNormalized(const unsigned char *, int *, int *);
        19123  +#endif
 18961  19124   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 18962  19125   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
 18963  19126   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr *, int, int);
 18964  19127   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 18965  19128   SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
 18966  19129   SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 18967  19130   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
................................................................................
 19111  19274   SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 19112  19275   SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 19113  19276   SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 19114  19277   SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
 19115  19278   SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 19116  19279   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 19117  19280   SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
        19281  +#ifdef SQLITE_ENABLE_NORMALIZE
        19282  +SQLITE_PRIVATE void sqlite3Normalize(Vdbe*, const char*, int, u8);
        19283  +#endif
 19118  19284   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 19119  19285   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 19120  19286   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 19121  19287   SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 19122  19288   SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 19123  19289   #ifndef SQLITE_OMIT_WAL
 19124  19290   SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
................................................................................
 19572  19738      0,                         /* xVdbeBranch */
 19573  19739      0,                         /* pVbeBranchArg */
 19574  19740   #endif
 19575  19741   #ifndef SQLITE_UNTESTABLE
 19576  19742      0,                         /* xTestCallback */
 19577  19743   #endif
 19578  19744      0,                         /* bLocaltimeFault */
        19745  +   0,                         /* bInternalFunctions */
 19579  19746      0x7ffffffe,                /* iOnceResetThreshold */
 19580  19747      SQLITE_DEFAULT_SORTERREF_SIZE   /* szSorterRef */
 19581  19748   };
 19582  19749   
 19583  19750   /*
 19584  19751   ** Hash table for global functions - functions common to all
 19585  19752   ** database connections.  After initialization, this table is
................................................................................
 20063  20230     bft usesStmtJournal:1;  /* True if uses a statement journal */
 20064  20231     bft readOnly:1;         /* True for statements that do not write */
 20065  20232     bft bIsReader:1;        /* True for statements that read */
 20066  20233     yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
 20067  20234     yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 20068  20235     u32 aCounter[7];        /* Counters used by sqlite3_stmt_status() */
 20069  20236     char *zSql;             /* Text of the SQL statement that generated this */
        20237  +#ifdef SQLITE_ENABLE_NORMALIZE
        20238  +  char *zNormSql;         /* Normalization of the associated SQL statement */
        20239  +#endif
 20070  20240     void *pFree;            /* Free this when deleting the vdbe */
 20071  20241     VdbeFrame *pFrame;      /* Parent frame */
 20072  20242     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 20073  20243     int nFrame;             /* Number of frames in pFrame list */
 20074  20244     u32 expmask;            /* Binding to these vars invalidates VM */
 20075  20245     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 20076  20246     AuxData *pAuxData;      /* Linked list of auxdata allocations */
................................................................................
 20125  20295   SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 20126  20296   SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
 20127  20297   
 20128  20298   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 20129  20299   SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
 20130  20300   SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
 20131  20301   SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
        20302  +#ifndef SQLITE_OMIT_EXPLAIN
 20132  20303   SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
        20304  +#endif
 20133  20305   SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 20134  20306   SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 20135  20307   SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
 20136  20308   SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
 20137  20309   SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
 20138  20310   SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
 20139  20311   SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
................................................................................
 20164  20336   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
 20165  20337   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 20166  20338   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 20167  20339   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 20168  20340   #ifndef SQLITE_OMIT_WINDOWFUNC
 20169  20341   SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
 20170  20342   #endif
        20343  +#ifndef SQLITE_OMIT_EXPLAIN
 20171  20344   SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
        20345  +#endif
 20172  20346   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 20173  20347   SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
 20174  20348   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 20175  20349   #ifdef SQLITE_DEBUG
 20176  20350   SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
 20177  20351   #endif
 20178  20352   SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*);      /* Destructor on Mem */
................................................................................
 28291  28465         sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
 28292  28466         sqlite3TreeViewPop(pView);
 28293  28467       }
 28294  28468       sqlite3TreeViewPop(pView);
 28295  28469     }
 28296  28470   }
 28297  28471   
        28472  +/*
        28473  +** Generate a human-readable description of a SrcList object.
        28474  +*/
        28475  +SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
        28476  +  int i;
        28477  +  for(i=0; i<pSrc->nSrc; i++){
        28478  +    const struct SrcList_item *pItem = &pSrc->a[i];
        28479  +    StrAccum x;
        28480  +    char zLine[100];
        28481  +    sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
        28482  +    sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
        28483  +    if( pItem->zDatabase ){
        28484  +      sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
        28485  +    }else if( pItem->zName ){
        28486  +      sqlite3_str_appendf(&x, " %s", pItem->zName);
        28487  +    }
        28488  +    if( pItem->pTab ){
        28489  +      sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
        28490  +    }
        28491  +    if( pItem->zAlias ){
        28492  +      sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
        28493  +    }
        28494  +    if( pItem->fg.jointype & JT_LEFT ){
        28495  +      sqlite3_str_appendf(&x, " LEFT-JOIN");
        28496  +    }
        28497  +    sqlite3StrAccumFinish(&x);
        28498  +    sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); 
        28499  +    if( pItem->pSelect ){
        28500  +      sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
        28501  +    }
        28502  +    if( pItem->fg.isTabFunc ){
        28503  +      sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
        28504  +    }
        28505  +    sqlite3TreeViewPop(pView);
        28506  +  }
        28507  +}
 28298  28508   
 28299  28509   /*
 28300  28510   ** Generate a human-readable description of a Select object.
 28301  28511   */
 28302  28512   SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
 28303  28513     int n = 0;
 28304  28514     int cnt = 0;
................................................................................
 28345  28555         for(pX=p->pWin; pX; pX=pX->pNextWin){
 28346  28556           sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
 28347  28557         }
 28348  28558         sqlite3TreeViewPop(pView);
 28349  28559       }
 28350  28560   #endif
 28351  28561       if( p->pSrc && p->pSrc->nSrc ){
 28352         -      int i;
 28353  28562         pView = sqlite3TreeViewPush(pView, (n--)>0);
 28354  28563         sqlite3TreeViewLine(pView, "FROM");
 28355         -      for(i=0; i<p->pSrc->nSrc; i++){
 28356         -        struct SrcList_item *pItem = &p->pSrc->a[i];
 28357         -        StrAccum x;
 28358         -        char zLine[100];
 28359         -        sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
 28360         -        sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
 28361         -        if( pItem->zDatabase ){
 28362         -          sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
 28363         -        }else if( pItem->zName ){
 28364         -          sqlite3_str_appendf(&x, " %s", pItem->zName);
 28365         -        }
 28366         -        if( pItem->pTab ){
 28367         -          sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
 28368         -        }
 28369         -        if( pItem->zAlias ){
 28370         -          sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
 28371         -        }
 28372         -        if( pItem->fg.jointype & JT_LEFT ){
 28373         -          sqlite3_str_appendf(&x, " LEFT-JOIN");
 28374         -        }
 28375         -        sqlite3StrAccumFinish(&x);
 28376         -        sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
 28377         -        if( pItem->pSelect ){
 28378         -          sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
 28379         -        }
 28380         -        if( pItem->fg.isTabFunc ){
 28381         -          sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
 28382         -        }
 28383         -        sqlite3TreeViewPop(pView);
 28384         -      }
        28564  +      sqlite3TreeViewSrcList(pView, p->pSrc);
 28385  28565         sqlite3TreeViewPop(pView);
 28386  28566       }
 28387  28567       if( p->pWhere ){
 28388  28568         sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
 28389  28569         sqlite3TreeViewExpr(pView, p->pWhere, 0);
 28390  28570         sqlite3TreeViewPop(pView);
 28391  28571       }
................................................................................
 28667  28847         Window *pWin;
 28668  28848         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 28669  28849           pFarg = 0;
 28670  28850           pWin = 0;
 28671  28851         }else{
 28672  28852           pFarg = pExpr->x.pList;
 28673  28853   #ifndef SQLITE_OMIT_WINDOWFUNC
 28674         -        pWin = pExpr->pWin;
        28854  +        pWin = pExpr->y.pWin;
 28675  28855   #else
 28676  28856           pWin = 0;
 28677  28857   #endif 
 28678  28858         }
 28679  28859         if( pExpr->op==TK_AGG_FUNCTION ){
 28680  28860           sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
 28681  28861                                pExpr->op2, pExpr->u.zToken);
................................................................................
 31495  31675       ** 0x9e3779b1 is 2654435761 which is the closest prime number to
 31496  31676       ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
 31497  31677       h += sqlite3UpperToLower[c];
 31498  31678       h *= 0x9e3779b1;
 31499  31679     }
 31500  31680     return h;
 31501  31681   }
        31682  +#ifdef SQLITE_ENABLE_NORMALIZE
        31683  +static unsigned int strHashN(const char *z, int n){
        31684  +  unsigned int h = 0;
        31685  +  int i;
        31686  +  for(i=0; i<n; i++){
        31687  +    /* Knuth multiplicative hashing.  (Sorting & Searching, p. 510).
        31688  +    ** 0x9e3779b1 is 2654435761 which is the closest prime number to
        31689  +    ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
        31690  +    h += sqlite3UpperToLower[z[i]];
        31691  +    h *= 0x9e3779b1;
        31692  +  }
        31693  +  return h;
        31694  +}
        31695  +#endif /* SQLITE_ENABLE_NORMALIZE */
 31502  31696   
 31503  31697   
 31504  31698   /* Link pNew element into the hash table pH.  If pEntry!=0 then also
 31505  31699   ** insert pNew into the pEntry hash bucket.
 31506  31700   */
 31507  31701   static void insertElement(
 31508  31702     Hash *pH,              /* The complete hash table */
................................................................................
 31606  31800       if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ 
 31607  31801         return elem;
 31608  31802       }
 31609  31803       elem = elem->next;
 31610  31804     }
 31611  31805     return &nullElement;
 31612  31806   }
        31807  +#ifdef SQLITE_ENABLE_NORMALIZE
        31808  +static HashElem *findElementWithHashN(
        31809  +  const Hash *pH,     /* The pH to be searched */
        31810  +  const char *pKey,   /* The key we are searching for */
        31811  +  int nKey,           /* Number of key bytes to use */
        31812  +  unsigned int *pHash /* Write the hash value here */
        31813  +){
        31814  +  HashElem *elem;                /* Used to loop thru the element list */
        31815  +  int count;                     /* Number of elements left to test */
        31816  +  unsigned int h;                /* The computed hash */
        31817  +  static HashElem nullElement = { 0, 0, 0, 0 };
        31818  +
        31819  +  if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
        31820  +    struct _ht *pEntry;
        31821  +    h = strHashN(pKey, nKey) % pH->htsize;
        31822  +    pEntry = &pH->ht[h];
        31823  +    elem = pEntry->chain;
        31824  +    count = pEntry->count;
        31825  +  }else{
        31826  +    h = 0;
        31827  +    elem = pH->first;
        31828  +    count = pH->count;
        31829  +  }
        31830  +  if( pHash ) *pHash = h;
        31831  +  while( count-- ){
        31832  +    assert( elem!=0 );
        31833  +    if( sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
        31834  +      return elem;
        31835  +    }
        31836  +    elem = elem->next;
        31837  +  }
        31838  +  return &nullElement;
        31839  +}
        31840  +#endif /* SQLITE_ENABLE_NORMALIZE */
 31613  31841   
 31614  31842   /* Remove a single entry from the hash table given a pointer to that
 31615  31843   ** element and a hash on the element's key.
 31616  31844   */
 31617  31845   static void removeElementGivenHash(
 31618  31846     Hash *pH,         /* The pH containing "elem" */
 31619  31847     HashElem* elem,   /* The element to be removed from the pH */
................................................................................
 31650  31878   ** found, or NULL if there is no match.
 31651  31879   */
 31652  31880   SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
 31653  31881     assert( pH!=0 );
 31654  31882     assert( pKey!=0 );
 31655  31883     return findElementWithHash(pH, pKey, 0)->data;
 31656  31884   }
        31885  +#ifdef SQLITE_ENABLE_NORMALIZE
        31886  +SQLITE_PRIVATE void *sqlite3HashFindN(const Hash *pH, const char *pKey, int nKey){
        31887  +  assert( pH!=0 );
        31888  +  assert( pKey!=0 );
        31889  +  assert( nKey>=0 );
        31890  +  return findElementWithHashN(pH, pKey, nKey, 0)->data;
        31891  +}
        31892  +#endif /* SQLITE_ENABLE_NORMALIZE */
 31657  31893   
 31658  31894   /* Insert an element into the hash table pH.  The key is pKey
 31659  31895   ** and the data is "data".
 31660  31896   **
 31661  31897   ** If no element exists with a matching key, then a new
 31662  31898   ** element is created and NULL is returned.
 31663  31899   **
................................................................................
 32032  32268   
 32033  32269   /*
 32034  32270   ** Allowed values of unixFile.fsFlags
 32035  32271   */
 32036  32272   #define SQLITE_FSFLAGS_IS_MSDOS     0x1
 32037  32273   
 32038  32274   /*
 32039         -** If we are to be thread-safe, include the pthreads header and define
 32040         -** the SQLITE_UNIX_THREADS macro.
        32275  +** If we are to be thread-safe, include the pthreads header.
 32041  32276   */
 32042  32277   #if SQLITE_THREADSAFE
 32043  32278   /* # include <pthread.h> */
 32044         -# define SQLITE_UNIX_THREADS 1
 32045  32279   #endif
 32046  32280   
 32047  32281   /*
 32048  32282   ** Default permissions when creating a new file
 32049  32283   */
 32050  32284   #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 32051  32285   # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
................................................................................
 32615  32849     { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
 32616  32850   #else
 32617  32851     { "lstat",         (sqlite3_syscall_ptr)0,              0 },
 32618  32852   #endif
 32619  32853   #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
 32620  32854   
 32621  32855   #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
        32856  +# ifdef __ANDROID__
        32857  +  { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
        32858  +# else
 32622  32859     { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
        32860  +# endif
 32623  32861   #else
 32624  32862     { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
 32625  32863   #endif
 32626  32864   #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
 32627  32865   
 32628  32866   }; /* End of the overrideable system calls */
 32629  32867   
................................................................................
 33209  33447     /* WAS:  ino_t ino;   */
 33210  33448     u64 ino;                   /* Inode number */
 33211  33449   #endif
 33212  33450   };
 33213  33451   
 33214  33452   /*
 33215  33453   ** An instance of the following structure is allocated for each open
 33216         -** inode.  Or, on LinuxThreads, there is one of these structures for
 33217         -** each inode opened by each thread.
        33454  +** inode.
 33218  33455   **
 33219  33456   ** A single inode can have multiple file descriptors, so each unixFile
 33220  33457   ** structure contains a pointer to an instance of this object and this
 33221  33458   ** object keeps a count of the number of unixFile pointing to it.
 33222  33459   **
 33223  33460   ** Mutex rules:
 33224  33461   **
................................................................................
 33256  33493     sem_t *pSem;                    /* Named POSIX semaphore */
 33257  33494     char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
 33258  33495   #endif
 33259  33496   };
 33260  33497   
 33261  33498   /*
 33262  33499   ** A lists of all unixInodeInfo objects.
        33500  +**
        33501  +** Must hold unixBigLock in order to read or write this variable.
 33263  33502   */
 33264  33503   static unixInodeInfo *inodeList = 0;  /* All unixInodeInfo objects */
 33265  33504   
 33266  33505   #ifdef SQLITE_DEBUG
 33267  33506   /*
 33268         -** True if the inode mutex is held, or not.  Used only within assert()
 33269         -** to help verify correct mutex usage.
        33507  +** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
        33508  +** This routine is used only within assert() to help verify correct mutex
        33509  +** usage.
 33270  33510   */
 33271  33511   int unixFileMutexHeld(unixFile *pFile){
 33272  33512     assert( pFile->pInode );
 33273  33513     return sqlite3_mutex_held(pFile->pInode->pLockMutex);
 33274  33514   }
 33275  33515   int unixFileMutexNotheld(unixFile *pFile){
 33276  33516     assert( pFile->pInode );
................................................................................
 33390  33630     }
 33391  33631     pInode->pUnused = 0;
 33392  33632   }
 33393  33633   
 33394  33634   /*
 33395  33635   ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 33396  33636   **
 33397         -** The mutex entered using the unixEnterMutex() function must be held
 33398         -** when this function is called.
        33637  +** The global mutex must be held when this routine is called, but the mutex
        33638  +** on the inode being deleted must NOT be held.
 33399  33639   */
 33400  33640   static void releaseInodeInfo(unixFile *pFile){
 33401  33641     unixInodeInfo *pInode = pFile->pInode;
 33402  33642     assert( unixMutexHeld() );
 33403  33643     assert( unixFileMutexNotheld(pFile) );
 33404  33644     if( ALWAYS(pInode) ){
 33405  33645       pInode->nRef--;
................................................................................
 33426  33666   }
 33427  33667   
 33428  33668   /*
 33429  33669   ** Given a file descriptor, locate the unixInodeInfo object that
 33430  33670   ** describes that file descriptor.  Create a new one if necessary.  The
 33431  33671   ** return value might be uninitialized if an error occurs.
 33432  33672   **
 33433         -** The mutex entered using the unixEnterMutex() function must be held
 33434         -** when this function is called.
        33673  +** The global mutex must held when calling this routine.
 33435  33674   **
 33436  33675   ** Return an appropriate error code.
 33437  33676   */
 33438  33677   static int findInodeInfo(
 33439  33678     unixFile *pFile,               /* Unix file with file desc used in the key */
 33440  33679     unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
 33441  33680   ){
................................................................................
 33488  33727     memset(&fileId, 0, sizeof(fileId));
 33489  33728     fileId.dev = statbuf.st_dev;
 33490  33729   #if OS_VXWORKS
 33491  33730     fileId.pId = pFile->pId;
 33492  33731   #else
 33493  33732     fileId.ino = (u64)statbuf.st_ino;
 33494  33733   #endif
        33734  +  assert( unixMutexHeld() );
 33495  33735     pInode = inodeList;
 33496  33736     while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 33497  33737       pInode = pInode->pNext;
 33498  33738     }
 33499  33739     if( pInode==0 ){
 33500  33740       pInode = sqlite3_malloc64( sizeof(*pInode) );
 33501  33741       if( pInode==0 ){
................................................................................
 33507  33747         pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 33508  33748         if( pInode->pLockMutex==0 ){
 33509  33749           sqlite3_free(pInode);
 33510  33750           return SQLITE_NOMEM_BKPT;
 33511  33751         }
 33512  33752       }
 33513  33753       pInode->nRef = 1;
        33754  +    assert( unixMutexHeld() );
 33514  33755       pInode->pNext = inodeList;
 33515  33756       pInode->pPrev = 0;
 33516  33757       if( inodeList ) inodeList->pPrev = pInode;
 33517  33758       inodeList = pInode;
 33518  33759     }else{
 33519  33760       pInode->nRef++;
 33520  33761     }
................................................................................
 36304  36545   ** unixMutexHeld() must be true when creating or destroying
 36305  36546   ** this object or while reading or writing the following fields:
 36306  36547   **
 36307  36548   **      nRef
 36308  36549   **
 36309  36550   ** The following fields are read-only after the object is created:
 36310  36551   ** 
 36311         -**      fid
        36552  +**      hShm
 36312  36553   **      zFilename
 36313  36554   **
 36314         -** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
        36555  +** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
 36315  36556   ** unixMutexHeld() is true when reading or writing any other field
 36316  36557   ** in this structure.
 36317  36558   */
 36318  36559   struct unixShmNode {
 36319  36560     unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
 36320         -  sqlite3_mutex *mutex;      /* Mutex to access this object */
        36561  +  sqlite3_mutex *pShmMutex;  /* Mutex to access this object */
 36321  36562     char *zFilename;           /* Name of the mmapped file */
 36322         -  int h;                     /* Open file descriptor */
        36563  +  int hShm;                  /* Open file descriptor */
 36323  36564     int szRegion;              /* Size of shared-memory regions */
 36324  36565     u16 nRegion;               /* Size of array apRegion */
 36325  36566     u8 isReadonly;             /* True if read-only */
 36326  36567     u8 isUnlocked;             /* True if no DMS lock held */
 36327  36568     char **apRegion;           /* Array of mapped shared-memory regions */
 36328  36569     int nRef;                  /* Number of unixShm objects pointing to this */
 36329  36570     unixShm *pFirst;           /* All unixShm objects pointing to this */
................................................................................
 36337  36578   /*
 36338  36579   ** Structure used internally by this VFS to record the state of an
 36339  36580   ** open shared memory connection.
 36340  36581   **
 36341  36582   ** The following fields are initialized when this object is created and
 36342  36583   ** are read-only thereafter:
 36343  36584   **
 36344         -**    unixShm.pFile
        36585  +**    unixShm.pShmNode
 36345  36586   **    unixShm.id
 36346  36587   **
 36347         -** All other fields are read/write.  The unixShm.pFile->mutex must be held
 36348         -** while accessing any read/write fields.
        36588  +** All other fields are read/write.  The unixShm.pShmNode->pShmMutex must
        36589  +** be held while accessing any read/write fields.
 36349  36590   */
 36350  36591   struct unixShm {
 36351  36592     unixShmNode *pShmNode;     /* The underlying unixShmNode object */
 36352  36593     unixShm *pNext;            /* Next unixShm with the same unixShmNode */
 36353         -  u8 hasMutex;               /* True if holding the unixShmNode mutex */
        36594  +  u8 hasMutex;               /* True if holding the unixShmNode->pShmMutex */
 36354  36595     u8 id;                     /* Id of this connection within its unixShmNode */
 36355  36596     u16 sharedMask;            /* Mask of shared locks held */
 36356  36597     u16 exclMask;              /* Mask of exclusive locks held */
 36357  36598   };
 36358  36599   
 36359  36600   /*
 36360  36601   ** Constants used for locking
................................................................................
 36376  36617   ){
 36377  36618     unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
 36378  36619     struct flock f;        /* The posix advisory locking structure */
 36379  36620     int rc = SQLITE_OK;    /* Result code form fcntl() */
 36380  36621   
 36381  36622     /* Access to the unixShmNode object is serialized by the caller */
 36382  36623     pShmNode = pFile->pInode->pShmNode;
 36383         -  assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->mutex) );
        36624  +  assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
        36625  +  assert( pShmNode->nRef>0 || unixMutexHeld() );
 36384  36626   
 36385  36627     /* Shared locks never span more than one byte */
 36386  36628     assert( n==1 || lockType!=F_RDLCK );
 36387  36629   
 36388  36630     /* Locks are within range */
 36389  36631     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 36390  36632   
 36391         -  if( pShmNode->h>=0 ){
        36633  +  if( pShmNode->hShm>=0 ){
 36392  36634       /* Initialize the locking parameters */
 36393  36635       f.l_type = lockType;
 36394  36636       f.l_whence = SEEK_SET;
 36395  36637       f.l_start = ofst;
 36396  36638       f.l_len = n;
 36397         -    rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
        36639  +    rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
 36398  36640       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 36399  36641     }
 36400  36642   
 36401  36643     /* Update the global lock state and do debug tracing */
 36402  36644   #ifdef SQLITE_DEBUG
 36403  36645     { u16 mask;
 36404  36646     OSTRACE(("SHM-LOCK "));
................................................................................
 36462  36704   static void unixShmPurge(unixFile *pFd){
 36463  36705     unixShmNode *p = pFd->pInode->pShmNode;
 36464  36706     assert( unixMutexHeld() );
 36465  36707     if( p && ALWAYS(p->nRef==0) ){
 36466  36708       int nShmPerMap = unixShmRegionPerMap();
 36467  36709       int i;
 36468  36710       assert( p->pInode==pFd->pInode );
 36469         -    sqlite3_mutex_free(p->mutex);
        36711  +    sqlite3_mutex_free(p->pShmMutex);
 36470  36712       for(i=0; i<p->nRegion; i+=nShmPerMap){
 36471         -      if( p->h>=0 ){
        36713  +      if( p->hShm>=0 ){
 36472  36714           osMunmap(p->apRegion[i], p->szRegion);
 36473  36715         }else{
 36474  36716           sqlite3_free(p->apRegion[i]);
 36475  36717         }
 36476  36718       }
 36477  36719       sqlite3_free(p->apRegion);
 36478         -    if( p->h>=0 ){
 36479         -      robust_close(pFd, p->h, __LINE__);
 36480         -      p->h = -1;
        36720  +    if( p->hShm>=0 ){
        36721  +      robust_close(pFd, p->hShm, __LINE__);
        36722  +      p->hShm = -1;
 36481  36723       }
 36482  36724       p->pInode->pShmNode = 0;
 36483  36725       sqlite3_free(p);
 36484  36726     }
 36485  36727   }
 36486  36728   
 36487  36729   /*
................................................................................
 36515  36757     ** process might open and use the *-shm file without truncating it.
 36516  36758     ** And if the *-shm file has been corrupted by a power failure or
 36517  36759     ** system crash, the database itself may also become corrupt.  */
 36518  36760     lock.l_whence = SEEK_SET;
 36519  36761     lock.l_start = UNIX_SHM_DMS;
 36520  36762     lock.l_len = 1;
 36521  36763     lock.l_type = F_WRLCK;
 36522         -  if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) {
        36764  +  if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
 36523  36765       rc = SQLITE_IOERR_LOCK;
 36524  36766     }else if( lock.l_type==F_UNLCK ){
 36525  36767       if( pShmNode->isReadonly ){
 36526  36768         pShmNode->isUnlocked = 1;
 36527  36769         rc = SQLITE_READONLY_CANTINIT;
 36528  36770       }else{
 36529  36771         rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
 36530         -      if( rc==SQLITE_OK && robust_ftruncate(pShmNode->h, 0) ){
        36772  +      /* The first connection to attach must truncate the -shm file.  We
        36773  +      ** truncate to 3 bytes (an arbitrary small number, less than the
        36774  +      ** -shm header size) rather than 0 as a system debugging aid, to
        36775  +      ** help detect if a -shm file truncation is legitimate or is the work
        36776  +      ** or a rogue process. */
        36777  +      if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
 36531  36778           rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
 36532  36779         }
 36533  36780       }
 36534  36781     }else if( lock.l_type==F_WRLCK ){
 36535  36782       rc = SQLITE_BUSY;
 36536  36783     }
 36537  36784   
................................................................................
 36629  36876       sqlite3_snprintf(nShmFilename, zShm, 
 36630  36877                        SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
 36631  36878                        (u32)sStat.st_ino, (u32)sStat.st_dev);
 36632  36879   #else
 36633  36880       sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
 36634  36881       sqlite3FileSuffix3(pDbFd->zPath, zShm);
 36635  36882   #endif
 36636         -    pShmNode->h = -1;
        36883  +    pShmNode->hShm = -1;
 36637  36884       pDbFd->pInode->pShmNode = pShmNode;
 36638  36885       pShmNode->pInode = pDbFd->pInode;
 36639  36886       if( sqlite3GlobalConfig.bCoreMutex ){
 36640         -      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 36641         -      if( pShmNode->mutex==0 ){
        36887  +      pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
        36888  +      if( pShmNode->pShmMutex==0 ){
 36642  36889           rc = SQLITE_NOMEM_BKPT;
 36643  36890           goto shm_open_err;
 36644  36891         }
 36645  36892       }
 36646  36893   
 36647  36894       if( pInode->bProcessLock==0 ){
 36648  36895         if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 36649         -        pShmNode->h = robust_open(zShm, O_RDWR|O_CREAT, (sStat.st_mode&0777));
        36896  +        pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT,(sStat.st_mode&0777));
 36650  36897         }
 36651         -      if( pShmNode->h<0 ){
 36652         -        pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
 36653         -        if( pShmNode->h<0 ){
        36898  +      if( pShmNode->hShm<0 ){
        36899  +        pShmNode->hShm = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
        36900  +        if( pShmNode->hShm<0 ){
 36654  36901             rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
 36655  36902             goto shm_open_err;
 36656  36903           }
 36657  36904           pShmNode->isReadonly = 1;
 36658  36905         }
 36659  36906   
 36660  36907         /* If this process is running as root, make sure that the SHM file
 36661  36908         ** is owned by the same user that owns the original database.  Otherwise,
 36662  36909         ** the original owner will not be able to connect.
 36663  36910         */
 36664         -      robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
        36911  +      robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
 36665  36912   
 36666  36913         rc = unixLockSharedMemory(pDbFd, pShmNode);
 36667  36914         if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
 36668  36915       }
 36669  36916     }
 36670  36917   
 36671  36918     /* Make the new connection a child of the unixShmNode */
................................................................................
 36677  36924     pDbFd->pShm = p;
 36678  36925     unixLeaveMutex();
 36679  36926   
 36680  36927     /* The reference count on pShmNode has already been incremented under
 36681  36928     ** the cover of the unixEnterMutex() mutex and the pointer from the
 36682  36929     ** new (struct unixShm) object to the pShmNode has been set. All that is
 36683  36930     ** left to do is to link the new object into the linked list starting
 36684         -  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 36685         -  ** mutex.
        36931  +  ** at pShmNode->pFirst. This must be done while holding the
        36932  +  ** pShmNode->pShmMutex.
 36686  36933     */
 36687         -  sqlite3_mutex_enter(pShmNode->mutex);
        36934  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36688  36935     p->pNext = pShmNode->pFirst;
 36689  36936     pShmNode->pFirst = p;
 36690         -  sqlite3_mutex_leave(pShmNode->mutex);
        36937  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36691  36938     return rc;
 36692  36939   
 36693  36940     /* Jump here on any error */
 36694  36941   shm_open_err:
 36695  36942     unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
 36696  36943     sqlite3_free(p);
 36697  36944     unixLeaveMutex();
................................................................................
 36735  36982     if( pDbFd->pShm==0 ){
 36736  36983       rc = unixOpenSharedMemory(pDbFd);
 36737  36984       if( rc!=SQLITE_OK ) return rc;
 36738  36985     }
 36739  36986   
 36740  36987     p = pDbFd->pShm;
 36741  36988     pShmNode = p->pShmNode;
 36742         -  sqlite3_mutex_enter(pShmNode->mutex);
        36989  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36743  36990     if( pShmNode->isUnlocked ){
 36744  36991       rc = unixLockSharedMemory(pDbFd, pShmNode);
 36745  36992       if( rc!=SQLITE_OK ) goto shmpage_out;
 36746  36993       pShmNode->isUnlocked = 0;
 36747  36994     }
 36748  36995     assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 36749  36996     assert( pShmNode->pInode==pDbFd->pInode );
 36750         -  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 36751         -  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
        36997  +  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
        36998  +  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
 36752  36999   
 36753  37000     /* Minimum number of regions required to be mapped. */
 36754  37001     nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
 36755  37002   
 36756  37003     if( pShmNode->nRegion<nReqRegion ){
 36757  37004       char **apNew;                      /* New apRegion[] array */
 36758  37005       int nByte = nReqRegion*szRegion;   /* Minimum required file size */
 36759  37006       struct stat sStat;                 /* Used by fstat() */
 36760  37007   
 36761  37008       pShmNode->szRegion = szRegion;
 36762  37009   
 36763         -    if( pShmNode->h>=0 ){
        37010  +    if( pShmNode->hShm>=0 ){
 36764  37011         /* The requested region is not mapped into this processes address space.
 36765  37012         ** Check to see if it has been allocated (i.e. if the wal-index file is
 36766  37013         ** large enough to contain the requested region).
 36767  37014         */
 36768         -      if( osFstat(pShmNode->h, &sStat) ){
        37015  +      if( osFstat(pShmNode->hShm, &sStat) ){
 36769  37016           rc = SQLITE_IOERR_SHMSIZE;
 36770  37017           goto shmpage_out;
 36771  37018         }
 36772  37019     
 36773  37020         if( sStat.st_size<nByte ){
 36774  37021           /* The requested memory region does not exist. If bExtend is set to
 36775  37022           ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
................................................................................
 36789  37036             static const int pgsz = 4096;
 36790  37037             int iPg;
 36791  37038   
 36792  37039             /* Write to the last byte of each newly allocated or extended page */
 36793  37040             assert( (nByte % pgsz)==0 );
 36794  37041             for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
 36795  37042               int x = 0;
 36796         -            if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
        37043  +            if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
 36797  37044                 const char *zFile = pShmNode->zFilename;
 36798  37045                 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
 36799  37046                 goto shmpage_out;
 36800  37047               }
 36801  37048             }
 36802  37049           }
 36803  37050         }
................................................................................
 36812  37059         goto shmpage_out;
 36813  37060       }
 36814  37061       pShmNode->apRegion = apNew;
 36815  37062       while( pShmNode->nRegion<nReqRegion ){
 36816  37063         int nMap = szRegion*nShmPerMap;
 36817  37064         int i;
 36818  37065         void *pMem;
 36819         -      if( pShmNode->h>=0 ){
        37066  +      if( pShmNode->hShm>=0 ){
 36820  37067           pMem = osMmap(0, nMap,
 36821  37068               pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
 36822         -            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
        37069  +            MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
 36823  37070           );
 36824  37071           if( pMem==MAP_FAILED ){
 36825  37072             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 36826  37073             goto shmpage_out;
 36827  37074           }
 36828  37075         }else{
 36829         -        pMem = sqlite3_malloc64(szRegion);
        37076  +        pMem = sqlite3_malloc64(nMap);
 36830  37077           if( pMem==0 ){
 36831  37078             rc = SQLITE_NOMEM_BKPT;
 36832  37079             goto shmpage_out;
 36833  37080           }
 36834         -        memset(pMem, 0, szRegion);
        37081  +        memset(pMem, 0, nMap);
 36835  37082         }
 36836  37083   
 36837  37084         for(i=0; i<nShmPerMap; i++){
 36838  37085           pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
 36839  37086         }
 36840  37087         pShmNode->nRegion += nShmPerMap;
 36841  37088       }
................................................................................
 36844  37091   shmpage_out:
 36845  37092     if( pShmNode->nRegion>iRegion ){
 36846  37093       *pp = pShmNode->apRegion[iRegion];
 36847  37094     }else{
 36848  37095       *pp = 0;
 36849  37096     }
 36850  37097     if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 36851         -  sqlite3_mutex_leave(pShmNode->mutex);
        37098  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36852  37099     return rc;
 36853  37100   }
 36854  37101   
 36855  37102   /*
 36856  37103   ** Change the lock state for a shared-memory segment.
 36857  37104   **
 36858  37105   ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
................................................................................
 36878  37125     assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 36879  37126     assert( n>=1 );
 36880  37127     assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 36881  37128          || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 36882  37129          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 36883  37130          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 36884  37131     assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 36885         -  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 36886         -  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
        37132  +  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
        37133  +  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
 36887  37134   
 36888  37135     mask = (1<<(ofst+n)) - (1<<ofst);
 36889  37136     assert( n>1 || mask==(1<<ofst) );
 36890         -  sqlite3_mutex_enter(pShmNode->mutex);
        37137  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36891  37138     if( flags & SQLITE_SHM_UNLOCK ){
 36892  37139       u16 allMask = 0; /* Mask of locks held by siblings */
 36893  37140   
 36894  37141       /* See if any siblings hold this same lock */
 36895  37142       for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 36896  37143         if( pX==p ) continue;
 36897  37144         assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
................................................................................
 36956  37203         rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
 36957  37204         if( rc==SQLITE_OK ){
 36958  37205           assert( (p->sharedMask & mask)==0 );
 36959  37206           p->exclMask |= mask;
 36960  37207         }
 36961  37208       }
 36962  37209     }
 36963         -  sqlite3_mutex_leave(pShmNode->mutex);
        37210  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36964  37211     OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
 36965  37212              p->id, osGetpid(0), p->sharedMask, p->exclMask));
 36966  37213     return rc;
 36967  37214   }
 36968  37215   
 36969  37216   /*
 36970  37217   ** Implement a memory barrier or memory fence on shared memory.  
................................................................................
 37006  37253     pShmNode = p->pShmNode;
 37007  37254   
 37008  37255     assert( pShmNode==pDbFd->pInode->pShmNode );
 37009  37256     assert( pShmNode->pInode==pDbFd->pInode );
 37010  37257   
 37011  37258     /* Remove connection p from the set of connections associated
 37012  37259     ** with pShmNode */
 37013         -  sqlite3_mutex_enter(pShmNode->mutex);
        37260  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 37014  37261     for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 37015  37262     *pp = p->pNext;
 37016  37263   
 37017  37264     /* Free the connection p */
 37018  37265     sqlite3_free(p);
 37019  37266     pDbFd->pShm = 0;
 37020         -  sqlite3_mutex_leave(pShmNode->mutex);
        37267  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 37021  37268   
 37022  37269     /* If pShmNode->nRef has reached 0, then close the underlying
 37023  37270     ** shared-memory file, too */
 37024  37271     assert( unixFileMutexNotheld(pDbFd) );
 37025  37272     unixEnterMutex();
 37026  37273     assert( pShmNode->nRef>0 );
 37027  37274     pShmNode->nRef--;
 37028  37275     if( pShmNode->nRef==0 ){
 37029         -    if( deleteFlag && pShmNode->h>=0 ){
        37276  +    if( deleteFlag && pShmNode->hShm>=0 ){
 37030  37277         osUnlink(pShmNode->zFilename);
 37031  37278       }
 37032  37279       unixShmPurge(pDbFd);
 37033  37280     }
 37034  37281     unixLeaveMutex();
 37035  37282   
 37036  37283     return SQLITE_OK;
................................................................................
 40440  40687     winceLock local;        /* Locks obtained by this instance of winFile */
 40441  40688     winceLock *shared;      /* Global shared lock memory for the file  */
 40442  40689   #endif
 40443  40690   #if SQLITE_MAX_MMAP_SIZE>0
 40444  40691     int nFetchOut;                /* Number of outstanding xFetch references */
 40445  40692     HANDLE hMap;                  /* Handle for accessing memory mapping */
 40446  40693     void *pMapRegion;             /* Area memory mapped */
 40447         -  sqlite3_int64 mmapSize;       /* Usable size of mapped region */
 40448         -  sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
        40694  +  sqlite3_int64 mmapSize;       /* Size of mapped region */
 40449  40695     sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
 40450  40696   #endif
 40451  40697   };
 40452  40698   
 40453  40699   /*
 40454  40700   ** The winVfsAppData structure is used for the pAppData member for all of the
 40455  40701   ** Win32 VFS variants.
................................................................................
 43062  43308   */
 43063  43309   static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
 43064  43310     winFile *pFile = (winFile*)id;  /* File handle object */
 43065  43311     int rc = SQLITE_OK;             /* Return code for this function */
 43066  43312     DWORD lastErrno;
 43067  43313   #if SQLITE_MAX_MMAP_SIZE>0
 43068  43314     sqlite3_int64 oldMmapSize;
        43315  +  if( pFile->nFetchOut>0 ){
        43316  +    /* File truncation is a no-op if there are outstanding memory mapped
        43317  +    ** pages.  This is because truncating the file means temporarily unmapping
        43318  +    ** the file, and that might delete memory out from under existing cursors.
        43319  +    **
        43320  +    ** This can result in incremental vacuum not truncating the file,
        43321  +    ** if there is an active read cursor when the incremental vacuum occurs.
        43322  +    ** No real harm comes of this - the database file is not corrupted,
        43323  +    ** though some folks might complain that the file is bigger than it
        43324  +    ** needs to be.
        43325  +    **
        43326  +    ** The only feasible work-around is to defer the truncation until after
        43327  +    ** all references to memory-mapped content are closed.  That is doable,
        43328  +    ** but involves adding a few branches in the common write code path which
        43329  +    ** could slow down normal operations slightly.  Hence, we have decided for
        43330  +    ** now to simply make trancations a no-op if there are pending reads.  We
        43331  +    ** can maybe revisit this decision in the future.
        43332  +    */
        43333  +    return SQLITE_OK;
        43334  +  }
 43069  43335   #endif
 43070  43336   
 43071  43337     assert( pFile );
 43072  43338     SimulateIOError(return SQLITE_IOERR_TRUNCATE);
 43073  43339     OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
 43074  43340              osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
 43075  43341   
................................................................................
 44490  44756   /*
 44491  44757   ** Cleans up the mapped region of the specified file, if any.
 44492  44758   */
 44493  44759   #if SQLITE_MAX_MMAP_SIZE>0
 44494  44760   static int winUnmapfile(winFile *pFile){
 44495  44761     assert( pFile!=0 );
 44496  44762     OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
 44497         -           "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
        44763  +           "mmapSize=%lld, mmapSizeMax=%lld\n",
 44498  44764              osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
 44499         -           pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
        44765  +           pFile->mmapSize, pFile->mmapSizeMax));
 44500  44766     if( pFile->pMapRegion ){
 44501  44767       if( !osUnmapViewOfFile(pFile->pMapRegion) ){
 44502  44768         pFile->lastErrno = osGetLastError();
 44503  44769         OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
 44504  44770                  "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
 44505  44771                  pFile->pMapRegion));
 44506  44772         return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
 44507  44773                            "winUnmapfile1", pFile->zPath);
 44508  44774       }
 44509  44775       pFile->pMapRegion = 0;
 44510  44776       pFile->mmapSize = 0;
 44511         -    pFile->mmapSizeActual = 0;
 44512  44777     }
 44513  44778     if( pFile->hMap!=NULL ){
 44514  44779       if( !osCloseHandle(pFile->hMap) ){
 44515  44780         pFile->lastErrno = osGetLastError();
 44516  44781         OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
 44517  44782                  osGetCurrentProcessId(), pFile, pFile->hMap));
 44518  44783         return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
................................................................................
 44615  44880         /* Log the error, but continue normal operation using xRead/xWrite */
 44616  44881         OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
 44617  44882                  osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
 44618  44883         return SQLITE_OK;
 44619  44884       }
 44620  44885       pFd->pMapRegion = pNew;
 44621  44886       pFd->mmapSize = nMap;
 44622         -    pFd->mmapSizeActual = nMap;
 44623  44887     }
 44624  44888   
 44625  44889     OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
 44626  44890              osGetCurrentProcessId(), pFd));
 44627  44891     return SQLITE_OK;
 44628  44892   }
 44629  44893   #endif /* SQLITE_MAX_MMAP_SIZE>0 */
................................................................................
 45417  45681     }
 45418  45682     pFile->lastErrno = NO_ERROR;
 45419  45683     pFile->zPath = zName;
 45420  45684   #if SQLITE_MAX_MMAP_SIZE>0
 45421  45685     pFile->hMap = NULL;
 45422  45686     pFile->pMapRegion = 0;
 45423  45687     pFile->mmapSize = 0;
 45424         -  pFile->mmapSizeActual = 0;
 45425  45688     pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
 45426  45689   #endif
 45427  45690   
 45428  45691     OpenCounter(+1);
 45429  45692     return rc;
 45430  45693   }
 45431  45694   
................................................................................
 47314  47577   **   such that p was added to the list more recently than p->pDirtyNext.
 47315  47578   **   PCache.pDirty points to the first (newest) element in the list and
 47316  47579   **   pDirtyTail to the last (oldest).
 47317  47580   **
 47318  47581   **   The PCache.pSynced variable is used to optimize searching for a dirty
 47319  47582   **   page to eject from the cache mid-transaction. It is better to eject
 47320  47583   **   a page that does not require a journal sync than one that does. 
 47321         -**   Therefore, pSynced is maintained to that it *almost* always points
        47584  +**   Therefore, pSynced is maintained so that it *almost* always points
 47322  47585   **   to either the oldest page in the pDirty/pDirtyTail list that has a
 47323  47586   **   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
 47324  47587   **   (so that the right page to eject can be found by following pDirtyPrev
 47325  47588   **   pointers).
 47326  47589   */
 47327  47590   struct PCache {
 47328  47591     PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
................................................................................
 48137  48400   SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
 48138  48401     PgHdr *pDirty;
 48139  48402     int nDirty = 0;
 48140  48403     int nCache = numberOfCachePages(pCache);
 48141  48404     for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
 48142  48405     return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
 48143  48406   }
        48407  +
        48408  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        48409  +/* 
        48410  +** Return true if there are one or more dirty pages in the cache. Else false.
        48411  +*/
        48412  +SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
        48413  +  return (pCache->pDirty!=0);
        48414  +}
        48415  +#endif
 48144  48416   
 48145  48417   #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
 48146  48418   /*
 48147  48419   ** For all dirty pages currently in the cache, invoke the specified
 48148  48420   ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
 48149  48421   ** defined.
 48150  48422   */
................................................................................
 48261  48533     PgHdr1 *pNext;                 /* Next in hash table chain */
 48262  48534     PCache1 *pCache;               /* Cache that currently owns this page */
 48263  48535     PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 48264  48536     PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 48265  48537   };
 48266  48538   
 48267  48539   /*
 48268         -** A page is pinned if it is no on the LRU list
        48540  +** A page is pinned if it is not on the LRU list.  To be "pinned" means
        48541  +** that the page is in active use and must not be deallocated.
 48269  48542   */
 48270  48543   #define PAGE_IS_PINNED(p)    ((p)->pLruNext==0)
 48271  48544   #define PAGE_IS_UNPINNED(p)  ((p)->pLruNext!=0)
 48272  48545   
 48273  48546   /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
 48274  48547   ** of one or more PCaches that are able to recycle each other's unpinned
 48275  48548   ** pages when they are under memory pressure.  A PGroup is an instance of
................................................................................
 50901  51174   **
 50902  51175   ** instead of
 50903  51176   **
 50904  51177   **   if( pPager->jfd->pMethods ){ ...
 50905  51178   */
 50906  51179   #define isOpen(pFd) ((pFd)->pMethods!=0)
 50907  51180   
        51181  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
 50908  51182   /*
 50909         -** Return true if this pager uses a write-ahead log to read page pgno.
 50910         -** Return false if the pager reads pgno directly from the database.
        51183  +** Return true if page pgno can be read directly from the database file
        51184  +** by the b-tree layer. This is the case if:
        51185  +**
        51186  +**   * the database file is open,
        51187  +**   * there are no dirty pages in the cache, and
        51188  +**   * the desired page is not currently in the wal file.
 50911  51189   */
 50912         -#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
 50913         -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
 50914         -  u32 iRead = 0;
 50915         -  int rc;
 50916         -  if( pPager->pWal==0 ) return 0;
 50917         -  rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
 50918         -  return rc || iRead;
        51190  +SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
        51191  +  if( pPager->fd->pMethods==0 ) return 0;
        51192  +  if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
        51193  +#ifndef SQLITE_OMIT_WAL
        51194  +  if( pPager->pWal ){
        51195  +    u32 iRead = 0;
        51196  +    int rc;
        51197  +    rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
        51198  +    return (rc==SQLITE_OK && iRead==0);
        51199  +  }
        51200  +#endif
        51201  +  return 1;
 50919  51202   }
 50920  51203   #endif
        51204  +
 50921  51205   #ifndef SQLITE_OMIT_WAL
 50922  51206   # define pagerUseWal(x) ((x)->pWal!=0)
 50923  51207   #else
 50924  51208   # define pagerUseWal(x) 0
 50925  51209   # define pagerRollbackWal(x) 0
 50926  51210   # define pagerWalFrames(v,w,x,y) 0
 50927  51211   # define pagerOpenWalIfPresent(z) SQLITE_OK
................................................................................
 57097  57381   SQLITE_PRIVATE void sqlite3PagerSetCodec(
 57098  57382     Pager *pPager,
 57099  57383     void *(*xCodec)(void*,void*,Pgno,int),
 57100  57384     void (*xCodecSizeChng)(void*,int,int),
 57101  57385     void (*xCodecFree)(void*),
 57102  57386     void *pCodec
 57103  57387   ){
 57104         -  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
        57388  +  if( pPager->xCodecFree ){
        57389  +    pPager->xCodecFree(pPager->pCodec);
        57390  +  }else{
        57391  +    pager_reset(pPager);
        57392  +  }
 57105  57393     pPager->xCodec = pPager->memDb ? 0 : xCodec;
 57106  57394     pPager->xCodecSizeChng = xCodecSizeChng;
 57107  57395     pPager->xCodecFree = xCodecFree;
 57108  57396     pPager->pCodec = pCodec;
 57109  57397     setGetterMethod(pPager);
 57110  57398     pagerReportSize(pPager);
 57111  57399   }
................................................................................
 65757  66045         pBt->usableSize = usableSize;
 65758  66046         pBt->pageSize = pageSize;
 65759  66047         freeTempSpace(pBt);
 65760  66048         rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 65761  66049                                      pageSize-usableSize);
 65762  66050         return rc;
 65763  66051       }
 65764         -    if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
        66052  +    if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
 65765  66053         rc = SQLITE_CORRUPT_BKPT;
 65766  66054         goto page1_init_failed;
 65767  66055       }
 65768  66056       /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
 65769  66057       ** be less than 480. In other words, if the page size is 512, then the
 65770  66058       ** reserved space size cannot exceed 32. */
 65771  66059       if( usableSize<480 ){
................................................................................
 66231  66519     Pager *pPager = pBt->pPager;
 66232  66520     int rc;
 66233  66521   
 66234  66522     assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
 66235  66523         eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
 66236  66524     assert( sqlite3_mutex_held(pBt->mutex) );
 66237  66525     assert( pDbPage->pBt==pBt );
        66526  +  if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
 66238  66527   
 66239  66528     /* Move page iDbPage from its current location to page number iFreePage */
 66240  66529     TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
 66241  66530         iDbPage, iFreePage, iPtrPage, eType));
 66242  66531     rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
 66243  66532     if( rc!=SQLITE_OK ){
 66244  66533       return rc;
................................................................................
 67402  67691             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 67403  67692           }
 67404  67693           offset -= ovflSize;
 67405  67694         }else{
 67406  67695           /* Need to read this page properly. It contains some of the
 67407  67696           ** range of data that is being read (eOp==0) or written (eOp!=0).
 67408  67697           */
 67409         -#ifdef SQLITE_DIRECT_OVERFLOW_READ
 67410         -        sqlite3_file *fd;      /* File from which to do direct overflow read */
 67411         -#endif
 67412  67698           int a = amt;
 67413  67699           if( a + offset > ovflSize ){
 67414  67700             a = ovflSize - offset;
 67415  67701           }
 67416  67702   
 67417  67703   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 67418  67704           /* If all the following are true:
 67419  67705           **
 67420  67706           **   1) this is a read operation, and 
 67421  67707           **   2) data is required from the start of this overflow page, and
 67422         -        **   3) there is no open write-transaction, and
        67708  +        **   3) there are no dirty pages in the page-cache
 67423  67709           **   4) the database is file-backed, and
 67424  67710           **   5) the page is not in the WAL file
 67425  67711           **   6) at least 4 bytes have already been read into the output buffer 
 67426  67712           **
 67427  67713           ** then data can be read directly from the database file into the
 67428  67714           ** output buffer, bypassing the page-cache altogether. This speeds
 67429  67715           ** up loading large records that span many overflow pages.
 67430  67716           */
 67431  67717           if( eOp==0                                             /* (1) */
 67432  67718            && offset==0                                          /* (2) */
 67433         -         && pBt->inTransaction==TRANS_READ                     /* (3) */
 67434         -         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (4) */
 67435         -         && 0==sqlite3PagerUseWal(pBt->pPager, nextPage)       /* (5) */
        67719  +         && sqlite3PagerDirectReadOk(pBt->pPager, nextPage)    /* (3,4,5) */
 67436  67720            && &pBuf[-4]>=pBufStart                               /* (6) */
 67437  67721           ){
        67722  +          sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
 67438  67723             u8 aSave[4];
 67439  67724             u8 *aWrite = &pBuf[-4];
 67440  67725             assert( aWrite>=pBufStart );                         /* due to (6) */
 67441  67726             memcpy(aSave, aWrite, 4);
 67442  67727             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 67443  67728             nextPage = get4byte(aWrite);
 67444  67729             memcpy(aWrite, aSave, 4);
................................................................................
 74026  74311     */
 74027  74312     if( fg & MEM_Int ){
 74028  74313       sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
 74029  74314     }else{
 74030  74315       assert( fg & MEM_Real );
 74031  74316       sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
 74032  74317     }
 74033         -  pMem->n = sqlite3Strlen30(pMem->z);
        74318  +  assert( pMem->z!=0 );
        74319  +  pMem->n = sqlite3Strlen30NN(pMem->z);
 74034  74320     pMem->enc = SQLITE_UTF8;
 74035  74321     pMem->flags |= MEM_Str|MEM_Term;
 74036  74322     if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
 74037  74323     sqlite3VdbeChangeEncoding(pMem, enc);
 74038  74324     return SQLITE_OK;
 74039  74325   }
 74040  74326   
................................................................................
 75601  75887     if( p==0 ) return;
 75602  75888     p->prepFlags = prepFlags;
 75603  75889     if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
 75604  75890       p->expmask = 0;
 75605  75891     }
 75606  75892     assert( p->zSql==0 );
 75607  75893     p->zSql = sqlite3DbStrNDup(p->db, z, n);
        75894  +#ifdef SQLITE_ENABLE_NORMALIZE
        75895  +  assert( p->zNormSql==0 );
        75896  +  if( p->zSql && (prepFlags & SQLITE_PREPARE_NORMALIZE)!=0 ){
        75897  +    sqlite3Normalize(p, p->zSql, n, prepFlags);
        75898  +    assert( p->zNormSql!=0 || p->db->mallocFailed );
        75899  +  }
        75900  +#endif
 75608  75901   }
 75609  75902   
 75610  75903   /*
 75611  75904   ** Swap all content between two VDBE structures.
 75612  75905   */
 75613  75906   SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
 75614  75907     Vdbe tmp, *pTmp;
................................................................................
 75622  75915     pB->pNext = pTmp;
 75623  75916     pTmp = pA->pPrev;
 75624  75917     pA->pPrev = pB->pPrev;
 75625  75918     pB->pPrev = pTmp;
 75626  75919     zTmp = pA->zSql;
 75627  75920     pA->zSql = pB->zSql;
 75628  75921     pB->zSql = zTmp;
        75922  +#ifdef SQLITE_ENABLE_NORMALIZE
        75923  +  zTmp = pA->zNormSql;
        75924  +  pA->zNormSql = pB->zNormSql;
        75925  +  pB->zNormSql = zTmp;
        75926  +#endif
 75629  75927     pB->expmask = pA->expmask;
 75630  75928     pB->prepFlags = pA->prepFlags;
 75631  75929     memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
 75632  75930     pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
 75633  75931   }
 75634  75932   
 75635  75933   /*
................................................................................
 78693  78991       releaseMemArray(p->aVar, p->nVar);
 78694  78992       sqlite3DbFree(db, p->pVList);
 78695  78993       sqlite3DbFree(db, p->pFree);
 78696  78994     }
 78697  78995     vdbeFreeOpArray(db, p->aOp, p->nOp);
 78698  78996     sqlite3DbFree(db, p->aColName);
 78699  78997     sqlite3DbFree(db, p->zSql);
        78998  +#ifdef SQLITE_ENABLE_NORMALIZE
        78999  +  sqlite3DbFree(db, p->zNormSql);
        79000  +#endif
 78700  79001   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 78701  79002     {
 78702  79003       int i;
 78703  79004       for(i=0; i<p->nScan; i++){
 78704  79005         sqlite3DbFree(db, p->aScan[i].zName);
 78705  79006       }
 78706  79007       sqlite3DbFree(db, p->aScan);
................................................................................
 80094  80395       return rc;
 80095  80396     }
 80096  80397   
 80097  80398     /* The index entry must begin with a header size */
 80098  80399     (void)getVarint32((u8*)m.z, szHdr);
 80099  80400     testcase( szHdr==3 );
 80100  80401     testcase( szHdr==m.n );
 80101         -  if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
        80402  +  testcase( szHdr>0x7fffffff );
        80403  +  assert( m.n>=0 );
        80404  +  if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
 80102  80405       goto idx_rowid_corruption;
 80103  80406     }
 80104  80407   
 80105  80408     /* The last field of the index should be an integer - the ROWID.
 80106  80409     ** Verify that the last entry really is an integer. */
 80107  80410     (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
 80108  80411     testcase( typeRowid==1 );
................................................................................
 82105  82408       z = sqlite3VdbeExpandSql(p, zSql);
 82106  82409       sqlite3_mutex_leave(p->db->mutex);
 82107  82410     }
 82108  82411     return z;
 82109  82412   #endif
 82110  82413   }
 82111  82414   
        82415  +#ifdef SQLITE_ENABLE_NORMALIZE
        82416  +/*
        82417  +** Return the normalized SQL associated with a prepared statement.
        82418  +*/
        82419  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
        82420  +  Vdbe *p = (Vdbe *)pStmt;
        82421  +  return p ? p->zNormSql : 0;
        82422  +}
        82423  +#endif /* SQLITE_ENABLE_NORMALIZE */
        82424  +
 82112  82425   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 82113  82426   /*
 82114  82427   ** Allocate and populate an UnpackedRecord structure based on the serialized
 82115  82428   ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
 82116  82429   ** if successful, or a NULL pointer if an OOM error is encountered.
 82117  82430   */
 82118  82431   static UnpackedRecord *vdbeUnpackRecord(
................................................................................
 85544  85857     }else{
 85545  85858       /* Rare case of a really large header */
 85546  85859       nVarint = sqlite3VarintLen(nHdr);
 85547  85860       nHdr += nVarint;
 85548  85861       if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
 85549  85862     }
 85550  85863     nByte = nHdr+nData;
 85551         -  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 85552         -    goto too_big;
 85553         -  }
 85554  85864   
 85555  85865     /* Make sure the output register has a buffer large enough to store 
 85556  85866     ** the new record. The output register (pOp->p3) is not allowed to
 85557  85867     ** be one of the input registers (because the following call to
 85558  85868     ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
 85559  85869     */
 85560         -  if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
 85561         -    goto no_mem;
        85870  +  if( nByte+nZero<=pOut->szMalloc ){
        85871  +    /* The output register is already large enough to hold the record.
        85872  +    ** No error checks or buffer enlargement is required */
        85873  +    pOut->z = pOut->zMalloc;
        85874  +  }else{
        85875  +    /* Need to make sure that the output is not too big and then enlarge
        85876  +    ** the output register to hold the full result */
        85877  +    if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
        85878  +      goto too_big;
        85879  +    }
        85880  +    if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
        85881  +      goto no_mem;
        85882  +    }
 85562  85883     }
 85563  85884     zNewRecord = (u8 *)pOut->z;
 85564  85885   
 85565  85886     /* Write the record */
 85566  85887     i = putVarint32(zNewRecord, nHdr);
 85567  85888     j = nHdr;
 85568  85889     assert( pData0<=pLast );
................................................................................
 88410  88731       db->mDbFlags |= DBFLAG_SchemaChange;
 88411  88732       p->expired = 0;
 88412  88733     }else
 88413  88734   #endif
 88414  88735     {
 88415  88736       zMaster = MASTER_NAME;
 88416  88737       initData.db = db;
 88417         -    initData.iDb = pOp->p1;
        88738  +    initData.iDb = iDb;
 88418  88739       initData.pzErrMsg = &p->zErrMsg;
 88419  88740       initData.mInitFlags = 0;
 88420  88741       zSql = sqlite3MPrintf(db,
 88421  88742          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 88422  88743          db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
 88423  88744       if( zSql==0 ){
 88424  88745         rc = SQLITE_NOMEM_BKPT;
................................................................................
 89607  89928   ** Synopsis: r[P3]=vcolumn(P2)
 89608  89929   **
 89609  89930   ** Store in register P3 the value of the P2-th column of
 89610  89931   ** the current row of the virtual-table of cursor P1.
 89611  89932   **
 89612  89933   ** If the VColumn opcode is being used to fetch the value of
 89613  89934   ** an unchanging column during an UPDATE operation, then the P5
 89614         -** value is 1.  Otherwise, P5 is 0.  The P5 value is returned
 89615         -** by sqlite3_vtab_nochange() routine and can be used
 89616         -** by virtual table implementations to return special "no-change"
 89617         -** marks which can be more efficient, depending on the virtual table.
        89935  +** value is OPFLAG_NOCHNG.  This will cause the sqlite3_vtab_nochange()
        89936  +** function to return true inside the xColumn method of the virtual
        89937  +** table implementation.  The P5 column might also contain other
        89938  +** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
        89939  +** unused by OP_VColumn.
 89618  89940   */
 89619  89941   case OP_VColumn: {
 89620  89942     sqlite3_vtab *pVtab;
 89621  89943     const sqlite3_module *pModule;
 89622  89944     Mem *pDest;
 89623  89945     sqlite3_context sContext;
 89624  89946   
................................................................................
 89632  89954       break;
 89633  89955     }
 89634  89956     pVtab = pCur->uc.pVCur->pVtab;
 89635  89957     pModule = pVtab->pModule;
 89636  89958     assert( pModule->xColumn );
 89637  89959     memset(&sContext, 0, sizeof(sContext));
 89638  89960     sContext.pOut = pDest;
 89639         -  if( pOp->p5 ){
        89961  +  testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
        89962  +  if( pOp->p5 & OPFLAG_NOCHNG ){
 89640  89963       sqlite3VdbeMemSetNull(pDest);
 89641  89964       pDest->flags = MEM_Null|MEM_Zero;
 89642  89965       pDest->u.nZero = 0;
 89643  89966     }else{
 89644  89967       MemSetTypeFlag(pDest, MEM_Null);
 89645  89968     }
 89646  89969     rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
................................................................................
 89709  90032   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 89710  90033   ** This opcode invokes the corresponding xRename method. The value
 89711  90034   ** in register P1 is passed as the zName argument to the xRename method.
 89712  90035   */
 89713  90036   case OP_VRename: {
 89714  90037     sqlite3_vtab *pVtab;
 89715  90038     Mem *pName;
 89716         -
        90039  +  int isLegacy;
        90040  +  
        90041  +  isLegacy = (db->flags & SQLITE_LegacyAlter);
        90042  +  db->flags |= SQLITE_LegacyAlter;
 89717  90043     pVtab = pOp->p4.pVtab->pVtab;
 89718  90044     pName = &aMem[pOp->p1];
 89719  90045     assert( pVtab->pModule->xRename );
 89720  90046     assert( memIsValid(pName) );
 89721  90047     assert( p->readOnly==0 );
 89722  90048     REGISTER_TRACE(pOp->p1, pName);
 89723  90049     assert( pName->flags & MEM_Str );
 89724  90050     testcase( pName->enc==SQLITE_UTF8 );
 89725  90051     testcase( pName->enc==SQLITE_UTF16BE );
 89726  90052     testcase( pName->enc==SQLITE_UTF16LE );
 89727  90053     rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
 89728  90054     if( rc ) goto abort_due_to_error;
 89729  90055     rc = pVtab->pModule->xRename(pVtab, pName->z);
        90056  +  if( isLegacy==0 ) db->flags &= ~SQLITE_LegacyAlter;
 89730  90057     sqlite3VtabImportErrmsg(p, pVtab);
 89731  90058     p->expired = 0;
 89732  90059     if( rc ) goto abort_due_to_error;
 89733  90060     break;
 89734  90061   }
 89735  90062   #endif
 89736  90063   
................................................................................
 93987  94314           continue;
 93988  94315         }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 93989  94316           if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 93990  94317         }else if( pExpr->x.pList ){
 93991  94318           if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 93992  94319         }
 93993  94320   #ifndef SQLITE_OMIT_WINDOWFUNC
 93994         -      if( !ExprHasProperty(pExpr, EP_Reduced) && pExpr->pWin ){
 93995         -        Window *pWin = pExpr->pWin;
        94321  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
        94322  +        Window *pWin = pExpr->y.pWin;
 93996  94323           if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
 93997  94324           if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
 93998  94325           if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
 93999  94326         }
 94000  94327   #endif
 94001  94328       }
 94002  94329       break;
................................................................................
 94261  94588   ** expression node refer back to that source column.  The following changes
 94262  94589   ** are made to pExpr:
 94263  94590   **
 94264  94591   **    pExpr->iDb           Set the index in db->aDb[] of the database X
 94265  94592   **                         (even if X is implied).
 94266  94593   **    pExpr->iTable        Set to the cursor number for the table obtained
 94267  94594   **                         from pSrcList.
 94268         -**    pExpr->pTab          Points to the Table structure of X.Y (even if
        94595  +**    pExpr->y.pTab        Points to the Table structure of X.Y (even if
 94269  94596   **                         X and/or Y are implied.)
 94270  94597   **    pExpr->iColumn       Set to the column number within the table.
 94271  94598   **    pExpr->op            Set to TK_COLUMN.
 94272  94599   **    pExpr->pLeft         Any expression this points to is deleted
 94273  94600   **    pExpr->pRight        Any expression this points to is deleted.
 94274  94601   **
 94275  94602   ** The zDb variable is the name of the database (the "X").  This value may be
................................................................................
 94305  94632   
 94306  94633     assert( pNC );     /* the name context cannot be NULL. */
 94307  94634     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 94308  94635     assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 94309  94636   
 94310  94637     /* Initialize the node to no-match */
 94311  94638     pExpr->iTable = -1;
 94312         -  pExpr->pTab = 0;
 94313  94639     ExprSetVVAProperty(pExpr, EP_NoReduce);
 94314  94640   
 94315  94641     /* Translate the schema name in zDb into a pointer to the corresponding
 94316  94642     ** schema.  If not found, pSchema will remain NULL and nothing will match
 94317  94643     ** resulting in an appropriate error message toward the end of this routine
 94318  94644     */
 94319  94645     if( zDb ){
................................................................................
 94367  94693           if( zTab ){
 94368  94694             const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
 94369  94695             assert( zTabName!=0 );
 94370  94696             if( sqlite3StrICmp(zTabName, zTab)!=0 ){
 94371  94697               continue;
 94372  94698             }
 94373  94699             if( IN_RENAME_OBJECT && pItem->zAlias ){
 94374         -            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->pTab);
        94700  +            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
 94375  94701             }
 94376  94702           }
 94377  94703           if( 0==(cntTab++) ){
 94378  94704             pMatch = pItem;
 94379  94705           }
 94380  94706           for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 94381  94707             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
................................................................................
 94393  94719               pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 94394  94720               break;
 94395  94721             }
 94396  94722           }
 94397  94723         }
 94398  94724         if( pMatch ){
 94399  94725           pExpr->iTable = pMatch->iCursor;
 94400         -        pExpr->pTab = pMatch->pTab;
        94726  +        pExpr->y.pTab = pMatch->pTab;
 94401  94727           /* RIGHT JOIN not (yet) supported */
 94402  94728           assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
 94403  94729           if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
 94404  94730             ExprSetProperty(pExpr, EP_CanBeNull);
 94405  94731           }
 94406         -        pSchema = pExpr->pTab->pSchema;
        94732  +        pSchema = pExpr->y.pTab->pSchema;
 94407  94733         }
 94408  94734       } /* if( pSrcList ) */
 94409  94735   
 94410  94736   #if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
 94411  94737       /* If we have not already resolved the name, then maybe 
 94412  94738       ** it is a new.* or old.* trigger argument reference.  Or
 94413  94739       ** maybe it is an excluded.* from an upsert.
................................................................................
 94456  94782           if( iCol<pTab->nCol ){
 94457  94783             cnt++;
 94458  94784   #ifndef SQLITE_OMIT_UPSERT
 94459  94785             if( pExpr->iTable==2 ){
 94460  94786               testcase( iCol==(-1) );
 94461  94787               if( IN_RENAME_OBJECT ){
 94462  94788                 pExpr->iColumn = iCol;
 94463         -              pExpr->pTab = pTab;
        94789  +              pExpr->y.pTab = pTab;
 94464  94790                 eNewExprOp = TK_COLUMN;
 94465  94791               }else{
 94466  94792                 pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
 94467  94793                 eNewExprOp = TK_REGISTER;
 94468  94794                 ExprSetProperty(pExpr, EP_Alias);
 94469  94795               }
 94470  94796             }else
................................................................................
 94478  94804                 testcase( iCol==32 );
 94479  94805                 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 94480  94806               }else{
 94481  94807                 testcase( iCol==31 );
 94482  94808                 testcase( iCol==32 );
 94483  94809                 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 94484  94810               }
 94485         -            pExpr->pTab = pTab;
        94811  +            pExpr->y.pTab = pTab;
 94486  94812               pExpr->iColumn = (i16)iCol;
 94487  94813               eNewExprOp = TK_TRIGGER;
 94488  94814   #endif /* SQLITE_OMIT_TRIGGER */
 94489  94815             }
 94490  94816           }
 94491  94817         }
 94492  94818       }
................................................................................
 94578  94904     ** Because no reference was made to outer contexts, the pNC->nRef
 94579  94905     ** fields are not changed in any context.
 94580  94906     */
 94581  94907     if( cnt==0 && zTab==0 ){
 94582  94908       assert( pExpr->op==TK_ID );
 94583  94909       if( ExprHasProperty(pExpr,EP_DblQuoted) ){
 94584  94910         pExpr->op = TK_STRING;
 94585         -      pExpr->pTab = 0;
        94911  +      pExpr->y.pTab = 0;
 94586  94912         return WRC_Prune;
 94587  94913       }
 94588  94914       if( sqlite3ExprIdToTrueFalse(pExpr) ){
 94589  94915         return WRC_Prune;
 94590  94916       }
 94591  94917     }
 94592  94918   
................................................................................
 94656  94982   ** Allocate and return a pointer to an expression to load the column iCol
 94657  94983   ** from datasource iSrc in SrcList pSrc.
 94658  94984   */
 94659  94985   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
 94660  94986     Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
 94661  94987     if( p ){
 94662  94988       struct SrcList_item *pItem = &pSrc->a[iSrc];
 94663         -    p->pTab = pItem->pTab;
        94989  +    p->y.pTab = pItem->pTab;
 94664  94990       p->iTable = pItem->iCursor;
 94665         -    if( p->pTab->iPKey==iCol ){
        94991  +    if( p->y.pTab->iPKey==iCol ){
 94666  94992         p->iColumn = -1;
 94667  94993       }else{
 94668  94994         p->iColumn = (ynVar)iCol;
 94669  94995         testcase( iCol==BMS );
 94670  94996         testcase( iCol==BMS-1 );
 94671  94997         pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
 94672  94998       }
................................................................................
 94748  95074       case TK_ROW: {
 94749  95075         SrcList *pSrcList = pNC->pSrcList;
 94750  95076         struct SrcList_item *pItem;
 94751  95077         assert( pSrcList && pSrcList->nSrc==1 );
 94752  95078         pItem = pSrcList->a;
 94753  95079         assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
 94754  95080         pExpr->op = TK_COLUMN;
 94755         -      pExpr->pTab = pItem->pTab;
        95081  +      pExpr->y.pTab = pItem->pTab;
 94756  95082         pExpr->iTable = pItem->iCursor;
 94757  95083         pExpr->iColumn = -1;
 94758  95084         pExpr->affinity = SQLITE_AFF_INTEGER;
 94759  95085         break;
 94760  95086       }
 94761  95087   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
 94762  95088             && !defined(SQLITE_OMIT_SUBQUERY) */
................................................................................
 94792  95118             pLeft = pRight->pLeft;
 94793  95119             pRight = pRight->pRight;
 94794  95120           }
 94795  95121           zTable = pLeft->u.zToken;
 94796  95122           zColumn = pRight->u.zToken;
 94797  95123           if( IN_RENAME_OBJECT ){
 94798  95124             sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
 94799         -        }
 94800         -        if( IN_RENAME_OBJECT ){
 94801         -          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->pTab, (void*)pLeft);
        95125  +          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
 94802  95126           }
 94803  95127         }
 94804  95128         return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 94805  95129       }
 94806  95130   
 94807  95131       /* Resolve function names
 94808  95132       */
................................................................................
 94876  95200           if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
 94877  95201             /* Date/time functions that use 'now', and other functions like
 94878  95202             ** sqlite_version() that might change over time cannot be used
 94879  95203             ** in an index. */
 94880  95204             notValid(pParse, pNC, "non-deterministic functions",
 94881  95205                      NC_IdxExpr|NC_PartIdx);
 94882  95206           }
        95207  +        if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
        95208  +         && pParse->nested==0
        95209  +         && sqlite3Config.bInternalFunctions==0
        95210  +        ){
        95211  +          /* Internal-use-only functions are disallowed unless the
        95212  +          ** SQL is being compiled using sqlite3NestedParse() */
        95213  +          no_such_func = 1;
        95214  +          pDef = 0;
        95215  +        }
 94883  95216         }
 94884  95217   
 94885  95218         if( 0==IN_RENAME_OBJECT ){
 94886  95219   #ifndef SQLITE_OMIT_WINDOWFUNC
 94887  95220           assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
 94888  95221             || (pDef->xValue==0 && pDef->xInverse==0)
 94889  95222             || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
 94890  95223           );
 94891         -        if( pDef && pDef->xValue==0 && pExpr->pWin ){
        95224  +        if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
 94892  95225             sqlite3ErrorMsg(pParse, 
 94893  95226                 "%.*s() may not be used as a window function", nId, zId
 94894  95227             );
 94895  95228             pNC->nErr++;
 94896  95229           }else if( 
 94897  95230                 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
 94898         -           || (is_agg && (pDef->funcFlags & SQLITE_FUNC_WINDOW) && !pExpr->pWin)
 94899         -           || (is_agg && pExpr->pWin && (pNC->ncFlags & NC_AllowWin)==0)
        95231  +           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin)
        95232  +           || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0)
 94900  95233           ){
 94901  95234             const char *zType;
 94902         -          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->pWin ){
        95235  +          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){
 94903  95236               zType = "window";
 94904  95237             }else{
 94905  95238               zType = "aggregate";
 94906  95239             }
 94907  95240             sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
 94908  95241             pNC->nErr++;
 94909  95242             is_agg = 0;
................................................................................
 94925  95258           }else if( wrong_num_args ){
 94926  95259             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 94927  95260                  nId, zId);
 94928  95261             pNC->nErr++;
 94929  95262           }
 94930  95263           if( is_agg ){
 94931  95264   #ifndef SQLITE_OMIT_WINDOWFUNC
 94932         -          pNC->ncFlags &= ~(pExpr->pWin ? NC_AllowWin : NC_AllowAgg);
        95265  +          pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
 94933  95266   #else
 94934  95267             pNC->ncFlags &= ~NC_AllowAgg;
 94935  95268   #endif
 94936  95269           }
 94937  95270         }
 94938  95271         sqlite3WalkExprList(pWalker, pList);
 94939  95272         if( is_agg ){
 94940  95273   #ifndef SQLITE_OMIT_WINDOWFUNC
 94941         -        if( pExpr->pWin ){
        95274  +        if( pExpr->y.pWin ){
 94942  95275             Select *pSel = pNC->pWinSelect;
 94943         -          sqlite3WalkExprList(pWalker, pExpr->pWin->pPartition);
 94944         -          sqlite3WalkExprList(pWalker, pExpr->pWin->pOrderBy);
 94945         -          sqlite3WalkExpr(pWalker, pExpr->pWin->pFilter);
 94946         -          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->pWin, pDef);
        95276  +          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
        95277  +          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
        95278  +          sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
        95279  +          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
 94947  95280             if( 0==pSel->pWin 
 94948         -           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->pWin) 
        95281  +           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
 94949  95282             ){
 94950         -            pExpr->pWin->pNextWin = pSel->pWin;
 94951         -            pSel->pWin = pExpr->pWin;
        95283  +            pExpr->y.pWin->pNextWin = pSel->pWin;
        95284  +            pSel->pWin = pExpr->y.pWin;
 94952  95285             }
 94953  95286             pNC->ncFlags |= NC_AllowWin;
 94954  95287           }else
 94955  95288   #endif /* SQLITE_OMIT_WINDOWFUNC */
 94956  95289           {
 94957  95290             NameContext *pNC2 = pNC;
 94958  95291             pExpr->op = TK_AGG_FUNCTION;
................................................................................
 95367  95700       pItem->u.x.iOrderByCol = 0;
 95368  95701       if( sqlite3ResolveExprNames(pNC, pE) ){
 95369  95702         return 1;
 95370  95703       }
 95371  95704       for(j=0; j<pSelect->pEList->nExpr; j++){
 95372  95705         if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
 95373  95706   #ifndef SQLITE_OMIT_WINDOWFUNC
 95374         -        if( pE->pWin ){
        95707  +        if( ExprHasProperty(pE, EP_WinFunc) ){
 95375  95708             /* Since this window function is being changed into a reference
 95376  95709             ** to the same window function the result set, remove the instance
 95377  95710             ** of this window function from the Select.pWin list. */
 95378  95711             Window **pp;
 95379  95712             for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
 95380         -            if( *pp==pE->pWin ){
        95713  +            if( *pp==pE->y.pWin ){
 95381  95714                 *pp = (*pp)->pNextWin;
 95382  95715               }    
 95383  95716             }
 95384  95717           }
 95385  95718   #endif
 95386  95719           pItem->u.x.iOrderByCol = j+1;
 95387  95720         }
................................................................................
 95836  96169     if( op==TK_REGISTER ) op = pExpr->op2;
 95837  96170   #ifndef SQLITE_OMIT_CAST
 95838  96171     if( op==TK_CAST ){
 95839  96172       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 95840  96173       return sqlite3AffinityType(pExpr->u.zToken, 0);
 95841  96174     }
 95842  96175   #endif
 95843         -  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
 95844         -    return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        96176  +  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
        96177  +    return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 95845  96178     }
 95846  96179     if( op==TK_SELECT_COLUMN ){
 95847  96180       assert( pExpr->pLeft->flags&EP_xIsSelect );
 95848  96181       return sqlite3ExprAffinity(
 95849  96182           pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
 95850  96183       );
 95851  96184     }
................................................................................
 95921  96254     CollSeq *pColl = 0;
 95922  96255     Expr *p = pExpr;
 95923  96256     while( p ){
 95924  96257       int op = p->op;
 95925  96258       if( p->flags & EP_Generic ) break;
 95926  96259       if( (op==TK_AGG_COLUMN || op==TK_COLUMN
 95927  96260             || op==TK_REGISTER || op==TK_TRIGGER)
 95928         -     && p->pTab!=0
        96261  +     && p->y.pTab!=0
 95929  96262       ){
 95930         -      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
        96263  +      /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
 95931  96264         ** a TK_COLUMN but was previously evaluated and cached in a register */
 95932  96265         int j = p->iColumn;
 95933  96266         if( j>=0 ){
 95934         -        const char *zColl = p->pTab->aCol[j].zColl;
        96267  +        const char *zColl = p->y.pTab->aCol[j].zColl;
 95935  96268           pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 95936  96269         }
 95937  96270         break;
 95938  96271       }
 95939  96272       if( op==TK_CAST || op==TK_UPLUS ){
 95940  96273         p = p->pLeft;
 95941  96274         continue;
................................................................................
 96830  97163   /*
 96831  97164   ** Recursively delete an expression tree.
 96832  97165   */
 96833  97166   static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
 96834  97167     assert( p!=0 );
 96835  97168     /* Sanity check: Assert that the IntValue is non-negative if it exists */
 96836  97169     assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
        97170  +
        97171  +  assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
        97172  +  assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
        97173  +          || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
 96837  97174   #ifdef SQLITE_DEBUG
 96838  97175     if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
 96839  97176       assert( p->pLeft==0 );
 96840  97177       assert( p->pRight==0 );
 96841  97178       assert( p->x.pSelect==0 );
 96842  97179     }
 96843  97180   #endif
................................................................................
 96848  97185       if( p->pRight ){
 96849  97186         sqlite3ExprDeleteNN(db, p->pRight);
 96850  97187       }else if( ExprHasProperty(p, EP_xIsSelect) ){
 96851  97188         sqlite3SelectDelete(db, p->x.pSelect);
 96852  97189       }else{
 96853  97190         sqlite3ExprListDelete(db, p->x.pList);
 96854  97191       }
 96855         -    if( !ExprHasProperty(p, EP_Reduced) ){
 96856         -      sqlite3WindowDelete(db, p->pWin);
        97192  +    if( ExprHasProperty(p, EP_WinFunc) ){
        97193  +      assert( p->op==TK_FUNCTION );
        97194  +      sqlite3WindowDelete(db, p->y.pWin);
 96857  97195       }
 96858  97196     }
 96859  97197     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
 96860  97198     if( !ExprHasProperty(p, EP_Static) ){
 96861  97199       sqlite3DbFreeNN(db, p);
 96862  97200     }
 96863  97201   }
................................................................................
 96913  97251   static int dupedExprStructSize(Expr *p, int flags){
 96914  97252     int nSize;
 96915  97253     assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
 96916  97254     assert( EXPR_FULLSIZE<=0xfff );
 96917  97255     assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
 96918  97256     if( 0==flags || p->op==TK_SELECT_COLUMN 
 96919  97257   #ifndef SQLITE_OMIT_WINDOWFUNC
 96920         -   || p->pWin 
        97258  +   || ExprHasProperty(p, EP_WinFunc)
 96921  97259   #endif
 96922  97260     ){
 96923  97261       nSize = EXPR_FULLSIZE;
 96924  97262     }else{
 96925  97263       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
 96926  97264       assert( !ExprHasProperty(p, EP_FromJoin) ); 
 96927  97265       assert( !ExprHasProperty(p, EP_MemToken) );
................................................................................
 96940  97278   ** This function returns the space in bytes required to store the copy 
 96941  97279   ** of the Expr structure and a copy of the Expr.u.zToken string (if that
 96942  97280   ** string is defined.)
 96943  97281   */
 96944  97282   static int dupedExprNodeSize(Expr *p, int flags){
 96945  97283     int nByte = dupedExprStructSize(p, flags) & 0xfff;
 96946  97284     if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 96947         -    nByte += sqlite3Strlen30(p->u.zToken)+1;
        97285  +    nByte += sqlite3Strlen30NN(p->u.zToken)+1;
 96948  97286     }
 96949  97287     return ROUND8(nByte);
 96950  97288   }
 96951  97289   
 96952  97290   /*
 96953  97291   ** Return the number of bytes required to create a duplicate of the 
 96954  97292   ** expression passed as the first argument. The second argument is a
................................................................................
 97043  97381           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
 97044  97382         }else{
 97045  97383           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
 97046  97384         }
 97047  97385       }
 97048  97386   
 97049  97387       /* Fill in pNew->pLeft and pNew->pRight. */
 97050         -    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
        97388  +    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
 97051  97389         zAlloc += dupedExprNodeSize(p, dupFlags);
 97052  97390         if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
 97053  97391           pNew->pLeft = p->pLeft ?
 97054  97392                         exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
 97055  97393           pNew->pRight = p->pRight ?
 97056  97394                          exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
 97057  97395         }
        97396  +#ifndef SQLITE_OMIT_WINDOWFUNC
        97397  +      if( ExprHasProperty(p, EP_WinFunc) ){
        97398  +        pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
        97399  +        assert( ExprHasProperty(pNew, EP_WinFunc) );
        97400  +      }
        97401  +#endif /* SQLITE_OMIT_WINDOWFUNC */
 97058  97402         if( pzBuffer ){
 97059  97403           *pzBuffer = zAlloc;
 97060  97404         }
 97061  97405       }else{
 97062         -#ifndef SQLITE_OMIT_WINDOWFUNC
 97063         -      if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
 97064         -        pNew->pWin = 0;
 97065         -      }else{
 97066         -        pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
 97067         -      }
 97068         -#endif /* SQLITE_OMIT_WINDOWFUNC */
 97069  97406         if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
 97070  97407           if( pNew->op==TK_SELECT_COLUMN ){
 97071  97408             pNew->pLeft = p->pLeft;
 97072  97409             assert( p->iColumn==0 || p->pRight==0 );
 97073  97410             assert( p->pRight==0  || p->pRight==p->pLeft );
 97074  97411           }else{
 97075  97412             pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
................................................................................
 97867  98204       case TK_INTEGER:
 97868  98205       case TK_STRING:
 97869  98206       case TK_FLOAT:
 97870  98207       case TK_BLOB:
 97871  98208         return 0;
 97872  98209       case TK_COLUMN:
 97873  98210         return ExprHasProperty(p, EP_CanBeNull) ||
 97874         -             p->pTab==0 ||  /* Reference to column of index on expression */
 97875         -             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
        98211  +             p->y.pTab==0 ||  /* Reference to column of index on expression */
        98212  +             (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0);
 97876  98213       default:
 97877  98214         return 1;
 97878  98215     }
 97879  98216   }
 97880  98217   
 97881  98218   /*
 97882  98219   ** Return TRUE if the given expression is a constant which would be
................................................................................
 97923  98260   */
 97924  98261   SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
 97925  98262     if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
 97926  98263     if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
 97927  98264     if( sqlite3StrICmp(z, "OID")==0 ) return 1;
 97928  98265     return 0;
 97929  98266   }
        98267  +#ifdef SQLITE_ENABLE_NORMALIZE
        98268  +SQLITE_PRIVATE int sqlite3IsRowidN(const char *z, int n){
        98269  +  if( sqlite3StrNICmp(z, "_ROWID_", n)==0 ) return 1;
        98270  +  if( sqlite3StrNICmp(z, "ROWID", n)==0 ) return 1;
        98271  +  if( sqlite3StrNICmp(z, "OID", n)==0 ) return 1;
        98272  +  return 0;
        98273  +}
        98274  +#endif
 97930  98275   
 97931  98276   /*
 97932  98277   ** pX is the RHS of an IN operator.  If pX is a SELECT statement 
 97933  98278   ** that can be simplified to a direct table access, then return
 97934  98279   ** a pointer to the SELECT statement.  If pX is not a SELECT statement,
 97935  98280   ** or if the SELECT statement needs to be manifested into a transient
 97936  98281   ** table, then return NULL.
................................................................................
 99156  99501           /* This COLUMN expression is really a constant due to WHERE clause
 99157  99502           ** constraints, and that constant is coded by the pExpr->pLeft
 99158  99503           ** expresssion.  However, make sure the constant has the correct
 99159  99504           ** datatype by applying the Affinity of the table column to the
 99160  99505           ** constant.
 99161  99506           */
 99162  99507           int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
 99163         -        int aff = sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        99508  +        int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 99164  99509           if( aff!=SQLITE_AFF_BLOB ){
 99165  99510             static const char zAff[] = "B\000C\000D\000E";
 99166  99511             assert( SQLITE_AFF_BLOB=='A' );
 99167  99512             assert( SQLITE_AFF_TEXT=='B' );
 99168  99513             if( iReg!=target ){
 99169  99514               sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
 99170  99515               iReg = target;
................................................................................
 99180  99525             return pExpr->iColumn - pParse->iSelfTab;
 99181  99526           }else{
 99182  99527             /* Coding an expression that is part of an index where column names
 99183  99528             ** in the index refer to the table to which the index belongs */
 99184  99529             iTab = pParse->iSelfTab - 1;
 99185  99530           }
 99186  99531         }
 99187         -      return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
        99532  +      return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
 99188  99533                                  pExpr->iColumn, iTab, target,
 99189  99534                                  pExpr->op2);
 99190  99535       }
 99191  99536       case TK_INTEGER: {
 99192  99537         codeInteger(pParse, pExpr, 0, target);
 99193  99538         return target;
 99194  99539       }
................................................................................
 99394  99739         u32 constMask = 0;     /* Mask of function arguments that are constant */
 99395  99740         int i;                 /* Loop counter */
 99396  99741         sqlite3 *db = pParse->db;  /* The database connection */
 99397  99742         u8 enc = ENC(db);      /* The text encoding used by this database */
 99398  99743         CollSeq *pColl = 0;    /* A collating sequence */
 99399  99744   
 99400  99745   #ifndef SQLITE_OMIT_WINDOWFUNC
 99401         -      if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){
 99402         -        return pExpr->pWin->regResult;
        99746  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
        99747  +        return pExpr->y.pWin->regResult;
 99403  99748         }
 99404  99749   #endif
 99405  99750   
 99406  99751         if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
 99407  99752           /* SQL functions can be expensive. So try to move constant functions
 99408  99753           ** out of the inner loop, even if that means an extra OP_Copy. */
 99409  99754           return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
................................................................................
 99638  99983         **
 99639  99984         ** Then p1 is interpreted as follows:
 99640  99985         **
 99641  99986         **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
 99642  99987         **   p1==1   ->    old.a         p1==4   ->    new.a
 99643  99988         **   p1==2   ->    old.b         p1==5   ->    new.b       
 99644  99989         */
 99645         -      Table *pTab = pExpr->pTab;
        99990  +      Table *pTab = pExpr->y.pTab;
 99646  99991         int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
 99647  99992   
 99648  99993         assert( pExpr->iTable==0 || pExpr->iTable==1 );
 99649  99994         assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 99650  99995         assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 99651  99996         assert( p1>=0 && p1<(pTab->nCol*2+2) );
 99652  99997   
 99653  99998         sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 99654  99999         VdbeComment((v, "r[%d]=%s.%s", target,
 99655 100000           (pExpr->iTable ? "new" : "old"),
 99656         -        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
       100001  +        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
 99657 100002         ));
 99658 100003   
 99659 100004   #ifndef SQLITE_OMIT_FLOATING_POINT
 99660 100005         /* If the column has REAL affinity, it may currently be stored as an
 99661 100006         ** integer. Use OP_RealAffinity to make sure it is really real.
 99662 100007         **
 99663 100008         ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
................................................................................
100500 100845         return 1;
100501 100846       }
100502 100847       return 2;
100503 100848     }
100504 100849     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
100505 100850       if( pA->op==TK_FUNCTION ){
100506 100851         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
       100852  +#ifndef SQLITE_OMIT_WINDOWFUNC
       100853  +      /* Justification for the assert():
       100854  +      ** window functions have p->op==TK_FUNCTION but aggregate functions
       100855  +      ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
       100856  +      ** function and a window function should have failed before reaching
       100857  +      ** this point.  And, it is not possible to have a window function and
       100858  +      ** a scalar function with the same name and number of arguments.  So
       100859  +      ** if we reach this point, either A and B both window functions or
       100860  +      ** neither are a window functions. */
       100861  +      assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) );
       100862  +      if( ExprHasProperty(pA,EP_WinFunc) ){
       100863  +        if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
       100864  +      }
       100865  +#endif
100507 100866       }else if( pA->op==TK_COLLATE ){
100508 100867         if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
100509 100868       }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
100510 100869         return 2;
100511 100870       }
100512 100871     }
100513 100872     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
................................................................................
100519 100878       if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
100520 100879       assert( (combinedFlags & EP_Reduced)==0 );
100521 100880       if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
100522 100881         if( pA->iColumn!=pB->iColumn ) return 2;
100523 100882         if( pA->iTable!=pB->iTable 
100524 100883          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
100525 100884       }
100526         -#ifndef SQLITE_OMIT_WINDOWFUNC
100527         -    /* Justification for the assert():
100528         -    ** window functions have p->op==TK_FUNCTION but aggregate functions
100529         -    ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
100530         -    ** function and a window function should have failed before reaching
100531         -    ** this point.  And, it is not possible to have a window function and
100532         -    ** a scalar function with the same name and number of arguments.  So
100533         -    ** if we reach this point, either A and B both window functions or
100534         -    ** neither are a window functions. */
100535         -    assert( (pA->pWin==0)==(pB->pWin==0) );
100536         -
100537         -    if( pA->pWin!=0 ){
100538         -      if( sqlite3WindowCompare(pParse,pA->pWin,pB->pWin)!=0 ) return 2;
100539         -    }
100540         -#endif
100541 100885     }
100542 100886     return 0;
100543 100887   }
100544 100888   
100545 100889   /*
100546 100890   ** Compare two ExprList objects.  Return 0 if they are identical and 
100547 100891   ** non-zero if they differ in any way.
................................................................................
100624 100968     }
100625 100969     return 0;
100626 100970   }
100627 100971   
100628 100972   /*
100629 100973   ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
100630 100974   ** If the expression node requires that the table at pWalker->iCur
100631         -** have a non-NULL column, then set pWalker->eCode to 1 and abort.
       100975  +** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
       100976  +**
       100977  +** This routine controls an optimization.  False positives (setting
       100978  +** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
       100979  +** (never setting pWalker->eCode) is a harmless missed optimization.
100632 100980   */
100633 100981   static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
100634         -  /* This routine is only called for WHERE clause expressions and so it
100635         -  ** cannot have any TK_AGG_COLUMN entries because those are only found
100636         -  ** in HAVING clauses.  We can get a TK_AGG_FUNCTION in a WHERE clause,
100637         -  ** but that is an illegal construct and the query will be rejected at
100638         -  ** a later stage of processing, so the TK_AGG_FUNCTION case does not
100639         -  ** need to be considered here. */
100640         -  assert( pExpr->op!=TK_AGG_COLUMN );
       100982  +  testcase( pExpr->op==TK_AGG_COLUMN );
100641 100983     testcase( pExpr->op==TK_AGG_FUNCTION );
100642         -
100643 100984     if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
100644 100985     switch( pExpr->op ){
100645 100986       case TK_ISNOT:
100646 100987       case TK_NOT:
100647 100988       case TK_ISNULL:
100648 100989       case TK_IS:
100649 100990       case TK_OR:
................................................................................
100677 101018       case TK_GE:
100678 101019         testcase( pExpr->op==TK_EQ );
100679 101020         testcase( pExpr->op==TK_NE );
100680 101021         testcase( pExpr->op==TK_LT );
100681 101022         testcase( pExpr->op==TK_LE );
100682 101023         testcase( pExpr->op==TK_GT );
100683 101024         testcase( pExpr->op==TK_GE );
100684         -      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab))
100685         -       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab))
       101025  +      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
       101026  +       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
100686 101027         ){
100687 101028          return WRC_Prune;
100688 101029         }
100689 101030       default:
100690 101031         return WRC_Continue;
100691 101032     }
100692 101033   }
................................................................................
100909 101250                   break;
100910 101251                 }
100911 101252               }
100912 101253               if( (k>=pAggInfo->nColumn)
100913 101254                && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
100914 101255               ){
100915 101256                 pCol = &pAggInfo->aCol[k];
100916         -              pCol->pTab = pExpr->pTab;
       101257  +              pCol->pTab = pExpr->y.pTab;
100917 101258                 pCol->iTable = pExpr->iTable;
100918 101259                 pCol->iColumn = pExpr->iColumn;
100919 101260                 pCol->iMem = ++pParse->nMem;
100920 101261                 pCol->iSorterColumn = -1;
100921 101262                 pCol->pExpr = pExpr;
100922 101263                 if( pAggInfo->pGroupBy ){
100923 101264                   int j, n;
................................................................................
101294 101635     ** table.
101295 101636     */
101296 101637     v = sqlite3GetVdbe(pParse);
101297 101638     if( v==0 ){
101298 101639       goto exit_rename_table;
101299 101640     }
101300 101641   
101301         -  /* If this is a virtual table, invoke the xRename() function if
101302         -  ** one is defined. The xRename() callback will modify the names
101303         -  ** of any resources used by the v-table implementation (including other
101304         -  ** SQLite tables) that are identified by the name of the virtual table.
101305         -  */
101306         -#ifndef SQLITE_OMIT_VIRTUALTABLE
101307         -  if( pVTab ){
101308         -    int i = ++pParse->nMem;
101309         -    sqlite3VdbeLoadString(v, i, zName);
101310         -    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
101311         -    sqlite3MayAbort(pParse);
101312         -  }
101313         -#endif
101314         -
101315 101642     /* figure out how many UTF-8 characters are in zName */
101316 101643     zTabName = pTab->zName;
101317 101644     nTabName = sqlite3Utf8CharLen(zTabName, -1);
101318 101645   
101319 101646     /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
101320 101647     ** the schema to use the new table name.  */
101321 101648     sqlite3NestedParse(pParse, 
................................................................................
101364 101691               "tbl_name = "
101365 101692                 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
101366 101693                 "          sqlite_rename_test(%Q, sql, type, name, 1) "
101367 101694                 "THEN %Q ELSE tbl_name END "
101368 101695               "WHERE type IN ('view', 'trigger')"
101369 101696           , zDb, zTabName, zName, zTabName, zDb, zName);
101370 101697     }
       101698  +
       101699  +  /* If this is a virtual table, invoke the xRename() function if
       101700  +  ** one is defined. The xRename() callback will modify the names
       101701  +  ** of any resources used by the v-table implementation (including other
       101702  +  ** SQLite tables) that are identified by the name of the virtual table.
       101703  +  */
       101704  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       101705  +  if( pVTab ){
       101706  +    int i = ++pParse->nMem;
       101707  +    sqlite3VdbeLoadString(v, i, zName);
       101708  +    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
       101709  +    sqlite3MayAbort(pParse);
       101710  +  }
       101711  +#endif
101371 101712   
101372 101713     renameReloadSchema(pParse, iDb);
101373 101714     renameTestSchema(pParse, zDb, iDb==1);
101374 101715   
101375 101716   exit_rename_table:
101376 101717     sqlite3SrcListDelete(db, pSrc);
101377 101718     sqlite3DbFree(db, zName);
................................................................................
101792 102133     }
101793 102134   }
101794 102135   #else
101795 102136   # define renameTokenCheckAll(x,y)
101796 102137   #endif
101797 102138   
101798 102139   /*
101799         -** Add a new RenameToken object mapping parse tree element pPtr into
101800         -** token *pToken to the Parse object currently under construction.
       102140  +** Remember that the parser tree element pPtr was created using
       102141  +** the token pToken.
101801 102142   **
101802         -** Return a copy of pPtr.
       102143  +** In other words, construct a new RenameToken object and add it
       102144  +** to the list of RenameToken objects currently being built up
       102145  +** in pParse->pRename.
       102146  +**
       102147  +** The pPtr argument is returned so that this routine can be used
       102148  +** with tail recursion in tokenExpr() routine, for a small performance
       102149  +** improvement.
101803 102150   */
101804 102151   SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
101805 102152     RenameToken *pNew;
101806 102153     assert( pPtr || pParse->db->mallocFailed );
101807 102154     renameTokenCheckAll(pParse, pPtr);
101808 102155     pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
101809 102156     if( pNew ){
................................................................................
101928 102275     if( pExpr->op==TK_TRIGGER 
101929 102276      && pExpr->iColumn==p->iCol 
101930 102277      && pWalker->pParse->pTriggerTab==p->pTab
101931 102278     ){
101932 102279       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101933 102280     }else if( pExpr->op==TK_COLUMN 
101934 102281      && pExpr->iColumn==p->iCol 
101935         -   && p->pTab==pExpr->pTab
       102282  +   && p->pTab==pExpr->y.pTab
101936 102283     ){
101937 102284       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101938 102285     }
101939 102286     return WRC_Continue;
101940 102287   }
101941 102288   
101942 102289   /*
................................................................................
102186 102533     memset(&sNC, 0, sizeof(sNC));
102187 102534     sNC.pParse = pParse;
102188 102535     assert( pNew->pTabSchema );
102189 102536     pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, 
102190 102537         db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
102191 102538     );
102192 102539     pParse->eTriggerOp = pNew->op;
       102540  +  /* ALWAYS() because if the table of the trigger does not exist, the
       102541  +  ** error would have been hit before this point */
       102542  +  if( ALWAYS(pParse->pTriggerTab) ){
       102543  +    rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
       102544  +  }
102193 102545   
102194 102546     /* Resolve symbols in WHEN clause */
102195         -  if( pNew->pWhen ){
       102547  +  if( rc==SQLITE_OK && pNew->pWhen ){
102196 102548       rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
102197 102549     }
102198 102550   
102199 102551     for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
102200 102552       if( pStep->pSelect ){
102201 102553         sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
102202 102554         if( pParse->nErr ) rc = pParse->rc;
................................................................................
102302 102654   **   8. bTemp:    True if zSql comes from temp schema
102303 102655   **
102304 102656   ** Do a column rename operation on the CREATE statement given in zSql.
102305 102657   ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
102306 102658   ** into zNew.  The name should be quoted if bQuote is true.
102307 102659   **
102308 102660   ** This function is used internally by the ALTER TABLE RENAME COLUMN command.
102309         -** Though accessible to application code, it is not intended for use by
102310         -** applications.  The existance of this function, and the way it works,
102311         -** is subject to change without notice.
102312         -**
102313         -** If any of the parameters are out-of-bounds, then simply return NULL.
102314         -** An out-of-bounds parameter can only occur when the application calls
102315         -** this function directly.  The parameters will always be well-formed when
102316         -** this routine is invoked by the bytecode for a legitimate ALTER TABLE
102317         -** statement.
       102661  +** It is only accessible to SQL created using sqlite3NestedParse().  It is
       102662  +** not reachable from ordinary SQL passed into sqlite3_prepare().
102318 102663   */
102319 102664   static void renameColumnFunc(
102320 102665     sqlite3_context *context,
102321 102666     int NotUsed,
102322 102667     sqlite3_value **argv
102323 102668   ){
102324 102669     sqlite3 *db = sqlite3_context_db_handle(context);
................................................................................
102466 102811   }
102467 102812   
102468 102813   /*
102469 102814   ** Walker expression callback used by "RENAME TABLE". 
102470 102815   */
102471 102816   static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
102472 102817     RenameCtx *p = pWalker->u.pRename;
102473         -  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
102474         -    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
       102818  +  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
       102819  +    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
102475 102820     }
102476 102821     return WRC_Continue;
102477 102822   }
102478 102823   
102479 102824   /*
102480 102825   ** Walker select callback used by "RENAME TABLE". 
102481 102826   */
................................................................................
102547 102892       sWalker.xExprCallback = renameTableExprCb;
102548 102893       sWalker.xSelectCallback = renameTableSelectCb;
102549 102894       sWalker.u.pRename = &sCtx;
102550 102895   
102551 102896       rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
102552 102897   
102553 102898       if( rc==SQLITE_OK ){
       102899  +      int isLegacy = (db->flags & SQLITE_LegacyAlter);
102554 102900         if( sParse.pNewTable ){
102555 102901           Table *pTab = sParse.pNewTable;
102556 102902   
102557 102903           if( pTab->pSelect ){
102558         -          NameContext sNC;
102559         -          memset(&sNC, 0, sizeof(sNC));
102560         -          sNC.pParse = &sParse;
       102904  +          if( isLegacy==0 ){
       102905  +            NameContext sNC;
       102906  +            memset(&sNC, 0, sizeof(sNC));
       102907  +            sNC.pParse = &sParse;
102561 102908   
102562         -          sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
102563         -          if( sParse.nErr ) rc = sParse.rc;
102564         -          sqlite3WalkSelect(&sWalker, pTab->pSelect);
       102909  +            sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
       102910  +            if( sParse.nErr ) rc = sParse.rc;
       102911  +            sqlite3WalkSelect(&sWalker, pTab->pSelect);
       102912  +          }
102565 102913           }else{
102566 102914             /* Modify any FK definitions to point to the new table. */
102567 102915   #ifndef SQLITE_OMIT_FOREIGN_KEY
102568         -          if( db->flags & SQLITE_ForeignKeys ){
       102916  +          if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
102569 102917               FKey *pFKey;
102570 102918               for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
102571 102919                 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
102572 102920                   renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
102573 102921                 }
102574 102922               }
102575 102923             }
................................................................................
102576 102924   #endif
102577 102925   
102578 102926             /* If this is the table being altered, fix any table refs in CHECK
102579 102927             ** expressions. Also update the name that appears right after the
102580 102928             ** "CREATE [VIRTUAL] TABLE" bit. */
102581 102929             if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
102582 102930               sCtx.pTab = pTab;
102583         -            sqlite3WalkExprList(&sWalker, pTab->pCheck);
       102931  +            if( isLegacy==0 ){
       102932  +              sqlite3WalkExprList(&sWalker, pTab->pCheck);
       102933  +            }
102584 102934               renameTokenFind(&sParse, &sCtx, pTab->zName);
102585 102935             }
102586 102936           }
102587 102937         }
102588 102938   
102589 102939         else if( sParse.pNewIndex ){
102590 102940           renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
102591         -        sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
       102941  +        if( isLegacy==0 ){
       102942  +          sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
       102943  +        }
102592 102944         }
102593 102945   
102594 102946   #ifndef SQLITE_OMIT_TRIGGER
102595 102947         else{
102596 102948           Trigger *pTrigger = sParse.pNewTrigger;
102597 102949           TriggerStep *pStep;
102598 102950           if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 
102599 102951               && sCtx.pTab->pSchema==pTrigger->pTabSchema
102600 102952             ){
102601 102953             renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
102602 102954           }
102603 102955   
102604         -        rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
102605         -        if( rc==SQLITE_OK ){
102606         -          renameWalkTrigger(&sWalker, pTrigger);
102607         -          for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
102608         -            if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
102609         -              renameTokenFind(&sParse, &sCtx, pStep->zTarget);
       102956  +        if( isLegacy==0 ){
       102957  +          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
       102958  +          if( rc==SQLITE_OK ){
       102959  +            renameWalkTrigger(&sWalker, pTrigger);
       102960  +            for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
       102961  +              if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
       102962  +                renameTokenFind(&sParse, &sCtx, pStep->zTarget);
       102963  +              }
102610 102964               }
102611 102965             }
102612 102966           }
102613 102967         }
102614 102968   #endif
102615 102969       }
102616 102970   
................................................................................
102660 103014     int NotUsed,
102661 103015     sqlite3_value **argv
102662 103016   ){
102663 103017     sqlite3 *db = sqlite3_context_db_handle(context);
102664 103018     char const *zDb = (const char*)sqlite3_value_text(argv[0]);
102665 103019     char const *zInput = (const char*)sqlite3_value_text(argv[1]);
102666 103020     int bTemp = sqlite3_value_int(argv[4]);
       103021  +  int isLegacy = (db->flags & SQLITE_LegacyAlter);
102667 103022   
102668 103023   #ifndef SQLITE_OMIT_AUTHORIZATION
102669 103024     sqlite3_xauth xAuth = db->xAuth;
102670 103025     db->xAuth = 0;
102671 103026   #endif
102672 103027   
102673 103028     UNUSED_PARAMETER(NotUsed);
102674 103029     if( zDb && zInput ){
102675 103030       int rc;
102676 103031       Parse sParse;
102677 103032       rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
102678 103033       if( rc==SQLITE_OK ){
102679         -      if( sParse.pNewTable && sParse.pNewTable->pSelect ){
       103034  +      if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
102680 103035           NameContext sNC;
102681 103036           memset(&sNC, 0, sizeof(sNC));
102682 103037           sNC.pParse = &sParse;
102683 103038           sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
102684 103039           if( sParse.nErr ) rc = sParse.rc;
102685 103040         }
102686 103041   
102687 103042         else if( sParse.pNewTrigger ){
102688         -        rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
       103043  +        if( isLegacy==0 ){
       103044  +          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
       103045  +        }
102689 103046           if( rc==SQLITE_OK ){
102690 103047             int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
102691 103048             int i2 = sqlite3FindDbName(db, zDb);
102692 103049             if( i1==i2 ) sqlite3_result_int(context, 1);
102693 103050           }
102694 103051         }
102695 103052       }
................................................................................
102706 103063   }
102707 103064   
102708 103065   /*
102709 103066   ** Register built-in functions used to help implement ALTER TABLE
102710 103067   */
102711 103068   SQLITE_PRIVATE void sqlite3AlterFunctions(void){
102712 103069     static FuncDef aAlterTableFuncs[] = {
102713         -    FUNCTION(sqlite_rename_column,  9, 0, 0, renameColumnFunc),
102714         -    FUNCTION(sqlite_rename_table,  7, 0, 0, renameTableFunc),
102715         -    FUNCTION(sqlite_rename_test,  5, 0, 0, renameTableTest),
       103070  +    INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
       103071  +    INTERNAL_FUNCTION(sqlite_rename_table,  7, renameTableFunc),
       103072  +    INTERNAL_FUNCTION(sqlite_rename_test,   5, renameTableTest),
102716 103073     };
102717 103074     sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
102718 103075   }
102719 103076   #endif  /* SQLITE_ALTER_TABLE */
102720 103077   
102721 103078   /************** End of alter.c ***********************************************/
102722 103079   /************** Begin file analyze.c *****************************************/
................................................................................
104757 105114       ** reopen it as a MemDB */
104758 105115       pVfs = sqlite3_vfs_find("memdb");
104759 105116       if( pVfs==0 ) return;
104760 105117       pNew = &db->aDb[db->init.iDb];
104761 105118       if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
104762 105119       pNew->pBt = 0;
104763 105120       pNew->pSchema = 0;
104764         -    rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
       105121  +    rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
104765 105122     }else{
104766 105123       /* This is a real ATTACH
104767 105124       **
104768 105125       ** Check for the following errors:
104769 105126       **
104770 105127       **     * Too many attached databases,
104771 105128       **     * Transaction currently open
................................................................................
105437 105794     Table *pTab = 0;      /* The table being read */
105438 105795     const char *zCol;     /* Name of the column of the table */
105439 105796     int iSrc;             /* Index in pTabList->a[] of table being read */
105440 105797     int iDb;              /* The index of the database the expression refers to */
105441 105798     int iCol;             /* Index of column in table */
105442 105799   
105443 105800     assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
       105801  +  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105444 105802     if( db->xAuth==0 ) return;
105445 105803     iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
105446 105804     if( iDb<0 ){
105447 105805       /* An attempt to read a column out of a subquery or other
105448 105806       ** temporary table. */
105449 105807       return;
105450 105808     }
................................................................................
105493 105851   ){
105494 105852     sqlite3 *db = pParse->db;
105495 105853     int rc;
105496 105854   
105497 105855     /* Don't do any authorization checks if the database is initialising
105498 105856     ** or if the parser is being invoked from within sqlite3_declare_vtab.
105499 105857     */
       105858  +  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105500 105859     if( db->init.busy || IN_SPECIAL_PARSE ){
105501 105860       return SQLITE_OK;
105502 105861     }
105503 105862   
105504 105863     if( db->xAuth==0 ){
105505 105864       return SQLITE_OK;
105506 105865     }
................................................................................
105916 106275       return 0;
105917 106276     }
105918 106277   
105919 106278     p = sqlite3FindTable(db, zName, zDbase);
105920 106279     if( p==0 ){
105921 106280       const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
105922 106281   #ifndef SQLITE_OMIT_VIRTUALTABLE
105923         -    if( sqlite3FindDbName(db, zDbase)<1 ){
105924         -      /* If zName is the not the name of a table in the schema created using
105925         -      ** CREATE, then check to see if it is the name of an virtual table that
105926         -      ** can be an eponymous virtual table. */
105927         -      Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
105928         -      if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
105929         -        pMod = sqlite3PragmaVtabRegister(db, zName);
105930         -      }
105931         -      if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
105932         -        return pMod->pEpoTab;
105933         -      }
       106282  +    /* If zName is the not the name of a table in the schema created using
       106283  +    ** CREATE, then check to see if it is the name of an virtual table that
       106284  +    ** can be an eponymous virtual table. */
       106285  +    Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
       106286  +    if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
       106287  +      pMod = sqlite3PragmaVtabRegister(db, zName);
       106288  +    }
       106289  +    if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
       106290  +      return pMod->pEpoTab;
105934 106291       }
105935 106292   #endif
105936 106293       if( (flags & LOCATE_NOERR)==0 ){
105937 106294         if( zDbase ){
105938 106295           sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
105939 106296         }else{
105940 106297           sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
................................................................................
106106 106463   /*
106107 106464   ** Erase all schema information from all attached databases (including
106108 106465   ** "main" and "temp") for a single database connection.
106109 106466   */
106110 106467   SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
106111 106468     int i;
106112 106469     sqlite3BtreeEnterAll(db);
106113         -  assert( db->nSchemaLock==0 );
106114 106470     for(i=0; i<db->nDb; i++){
106115 106471       Db *pDb = &db->aDb[i];
106116 106472       if( pDb->pSchema ){
106117         -      sqlite3SchemaClear(pDb->pSchema);
       106473  +      if( db->nSchemaLock==0 ){
       106474  +        sqlite3SchemaClear(pDb->pSchema);
       106475  +      }else{
       106476  +        DbSetProperty(db, i, DB_ResetWanted);
       106477  +      }
106118 106478       }
106119 106479     }
106120 106480     db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
106121 106481     sqlite3VtabUnlockList(db);
106122 106482     sqlite3BtreeLeaveAll(db);
106123         -  sqlite3CollapseDatabaseArray(db);
       106483  +  if( db->nSchemaLock==0 ){
       106484  +    sqlite3CollapseDatabaseArray(db);
       106485  +  }
106124 106486   }
106125 106487   
106126 106488   /*
106127 106489   ** This routine is called when a commit occurs.
106128 106490   */
106129 106491   SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
106130 106492     db->mDbFlags &= ~DBFLAG_SchemaChange;
................................................................................
106193 106555     }
106194 106556   
106195 106557     /* Delete any foreign keys attached to this table. */
106196 106558     sqlite3FkDelete(db, pTable);
106197 106559   
106198 106560     /* Delete the Table structure itself.
106199 106561     */
       106562  +#ifdef SQLITE_ENABLE_NORMALIZE
       106563  +  if( pTable->pColHash ){
       106564  +    sqlite3HashClear(pTable->pColHash);
       106565  +    sqlite3_free(pTable->pColHash);
       106566  +  }
       106567  +#endif
106200 106568     sqlite3DeleteColumnNames(db, pTable);
106201 106569     sqlite3DbFree(db, pTable->zName);
106202 106570     sqlite3DbFree(db, pTable->zColAff);
106203 106571     sqlite3SelectDelete(db, pTable->pSelect);
106204 106572     sqlite3ExprListDelete(db, pTable->pCheck);
106205 106573   #ifndef SQLITE_OMIT_VIRTUALTABLE
106206 106574     sqlite3VtabClear(db, pTable);
................................................................................
106350 106718       assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
106351 106719                || (db->mDbFlags & DBFLAG_Vacuum)!=0);
106352 106720       iDb = db->init.iDb;
106353 106721       *pUnqual = pName1;
106354 106722     }
106355 106723     return iDb;
106356 106724   }
       106725  +
       106726  +/*
       106727  +** True if PRAGMA writable_schema is ON
       106728  +*/
       106729  +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
       106730  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
       106731  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106732  +               SQLITE_WriteSchema );
       106733  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106734  +               SQLITE_Defensive );
       106735  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106736  +               (SQLITE_WriteSchema|SQLITE_Defensive) );
       106737  +  return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
       106738  +}
106357 106739   
106358 106740   /*
106359 106741   ** This routine is used to check if the UTF-8 string zName is a legal
106360 106742   ** unqualified name for a new schema object (table, index, view or
106361 106743   ** trigger). All names are legal except those that begin with the string
106362 106744   ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
106363 106745   ** is reserved for internal use.
106364 106746   */
106365 106747   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
106366 106748     if( !pParse->db->init.busy && pParse->nested==0 
106367         -          && (pParse->db->flags & SQLITE_WriteSchema)==0
       106749  +          && sqlite3WritableSchema(pParse->db)==0
106368 106750             && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
106369 106751       sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
106370 106752       return SQLITE_ERROR;
106371 106753     }
106372 106754     return SQLITE_OK;
106373 106755   }
106374 106756   
................................................................................
107331 107713       for(i=0; i<pTab->nCol; i++){
107332 107714         if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
107333 107715           pTab->aCol[i].notNull = OE_Abort;
107334 107716         }
107335 107717       }
107336 107718     }
107337 107719   
107338         -  /* The remaining transformations only apply to b-tree tables, not to
107339         -  ** virtual tables */
107340         -  if( IN_DECLARE_VTAB ) return;
107341         -
107342 107720     /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
107343 107721     ** into BTREE_BLOBKEY.
107344 107722     */
107345 107723     if( pParse->addrCrTab ){
107346 107724       assert( v );
107347 107725       sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
107348 107726     }
................................................................................
107440 107818       assert( pTab->nCol==j );
107441 107819     }else{
107442 107820       pPk->nColumn = pTab->nCol;
107443 107821     }
107444 107822     recomputeColumnsNotIndexed(pPk);
107445 107823   }
107446 107824   
       107825  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       107826  +/*
       107827  +** Return true if zName is a shadow table name in the current database
       107828  +** connection.
       107829  +**
       107830  +** zName is temporarily modified while this routine is running, but is
       107831  +** restored to its original value prior to this routine returning.
       107832  +*/
       107833  +static int isShadowTableName(sqlite3 *db, char *zName){
       107834  +  char *zTail;                  /* Pointer to the last "_" in zName */
       107835  +  Table *pTab;                  /* Table that zName is a shadow of */
       107836  +  Module *pMod;                 /* Module for the virtual table */
       107837  +
       107838  +  zTail = strrchr(zName, '_');
       107839  +  if( zTail==0 ) return 0;
       107840  +  *zTail = 0;
       107841  +  pTab = sqlite3FindTable(db, zName, 0);
       107842  +  *zTail = '_';
       107843  +  if( pTab==0 ) return 0;
       107844  +  if( !IsVirtual(pTab) ) return 0;
       107845  +  pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
       107846  +  if( pMod==0 ) return 0;
       107847  +  if( pMod->pModule->iVersion<3 ) return 0;
       107848  +  if( pMod->pModule->xShadowName==0 ) return 0;
       107849  +  return pMod->pModule->xShadowName(zTail+1);
       107850  +}
       107851  +#else
       107852  +# define isShadowTableName(x,y) 0
       107853  +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
       107854  +
107447 107855   /*
107448 107856   ** This routine is called to report the final ")" that terminates
107449 107857   ** a CREATE TABLE statement.
107450 107858   **
107451 107859   ** The table structure that other action routines have been building
107452 107860   ** is added to the internal hash tables, assuming no errors have
107453 107861   ** occurred.
................................................................................
107478 107886   
107479 107887     if( pEnd==0 && pSelect==0 ){
107480 107888       return;
107481 107889     }
107482 107890     assert( !db->mallocFailed );
107483 107891     p = pParse->pNewTable;
107484 107892     if( p==0 ) return;
       107893  +
       107894  +  if( pSelect==0 && isShadowTableName(db, p->zName) ){
       107895  +    p->tabFlags |= TF_Shadow;
       107896  +  }
107485 107897   
107486 107898     /* If the db->init.busy is 1 it means we are reading the SQL off the
107487 107899     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
107488 107900     ** So do not write to the disk again.  Extract the root page number
107489 107901     ** for the table from the db->init.newTnum field.  (The page number
107490 107902     ** should have been put there by the sqliteOpenCb routine.)
107491 107903     **
................................................................................
107986 108398   ** Also write code to modify the sqlite_master table and internal schema
107987 108399   ** if a root-page of another table is moved by the btree-layer whilst
107988 108400   ** erasing iTable (this can happen with an auto-vacuum database).
107989 108401   */ 
107990 108402   static void destroyRootPage(Parse *pParse, int iTable, int iDb){
107991 108403     Vdbe *v = sqlite3GetVdbe(pParse);
107992 108404     int r1 = sqlite3GetTempReg(pParse);
107993         -  assert( iTable>1 );
       108405  +  if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
107994 108406     sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
107995 108407     sqlite3MayAbort(pParse);
107996 108408   #ifndef SQLITE_OMIT_AUTOVACUUM
107997 108409     /* OP_Destroy stores an in integer r1. If this integer
107998 108410     ** is non-zero, then it is the root page number of a table moved to
107999 108411     ** location iTable. The following code modifies the sqlite_master table to
108000 108412     ** reflect this.
................................................................................
110441 110853     for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
110442 110854       if( sqlite3StrICmp(p->zName, zFunc)==0 ){
110443 110855         return p;
110444 110856       }
110445 110857     }
110446 110858     return 0;
110447 110859   }
       110860  +#ifdef SQLITE_ENABLE_NORMALIZE
       110861  +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(
       110862  +  int h,               /* Hash of the name */
       110863  +  const char *zFunc,   /* Name of function */
       110864  +  int nFunc            /* Length of the name */
       110865  +){
       110866  +  FuncDef *p;
       110867  +  for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
       110868  +    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 ){
       110869  +      return p;
       110870  +    }
       110871  +  }
       110872  +  return 0;
       110873  +}
       110874  +#endif /* SQLITE_ENABLE_NORMALIZE */
110448 110875   
110449 110876   /*
110450 110877   ** Insert a new FuncDef into a FuncDefHash hash table.
110451 110878   */
110452 110879   SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
110453 110880     FuncDef *aDef,      /* List of global functions to be inserted */
110454 110881     int nDef            /* Length of the apDef[] list */
110455 110882   ){
110456 110883     int i;
110457 110884     for(i=0; i<nDef; i++){
110458 110885       FuncDef *pOther;
110459 110886       const char *zName = aDef[i].zName;
110460 110887       int nName = sqlite3Strlen30(zName);
110461         -    int h = (zName[0] + nName) % SQLITE_FUNC_HASH_SZ;
       110888  +    int h = SQLITE_FUNC_HASH(zName[0], nName);
110462 110889       assert( zName[0]>='a' && zName[0]<='z' );
110463 110890       pOther = functionSearch(h, zName);
110464 110891       if( pOther ){
110465 110892         assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
110466 110893         aDef[i].pNext = pOther->pNext;
110467 110894         pOther->pNext = &aDef[i];
110468 110895       }else{
................................................................................
110533 110960     ** install a new function.  Whatever FuncDef structure is returned it will
110534 110961     ** have fields overwritten with new information appropriate for the
110535 110962     ** new function.  But the FuncDefs for built-in functions are read-only.
110536 110963     ** So we must not search for built-ins when creating a new function.
110537 110964     */ 
110538 110965     if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
110539 110966       bestScore = 0;
110540         -    h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
       110967  +    h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
110541 110968       p = functionSearch(h, zName);
110542 110969       while( p ){
110543 110970         int score = matchQuality(p, nArg, enc);
110544 110971         if( score>bestScore ){
110545 110972           pBest = p;
110546 110973           bestScore = score;
110547 110974         }
................................................................................
110680 111107       pTab->nTabRef++;
110681 111108     }
110682 111109     if( sqlite3IndexedByLookup(pParse, pItem) ){
110683 111110       pTab = 0;
110684 111111     }
110685 111112     return pTab;
110686 111113   }
       111114  +
       111115  +/* Return true if table pTab is read-only.
       111116  +**
       111117  +** A table is read-only if any of the following are true:
       111118  +**
       111119  +**   1) It is a virtual table and no implementation of the xUpdate method
       111120  +**      has been provided
       111121  +**
       111122  +**   2) It is a system table (i.e. sqlite_master), this call is not
       111123  +**      part of a nested parse and writable_schema pragma has not 
       111124  +**      been specified
       111125  +**
       111126  +**   3) The table is a shadow table, the database connection is in
       111127  +**      defensive mode, and the current sqlite3_prepare()
       111128  +**      is for a top-level SQL statement.
       111129  +*/
       111130  +static int tabIsReadOnly(Parse *pParse, Table *pTab){
       111131  +  sqlite3 *db;
       111132  +  if( IsVirtual(pTab) ){
       111133  +    return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
       111134  +  }
       111135  +  if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
       111136  +  db = pParse->db;
       111137  +  if( (pTab->tabFlags & TF_Readonly)!=0 ){
       111138  +    return sqlite3WritableSchema(db)==0 && pParse->nested==0;
       111139  +  }
       111140  +  assert( pTab->tabFlags & TF_Shadow );
       111141  +  return (db->flags & SQLITE_Defensive)!=0 
       111142  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       111143  +          && db->pVtabCtx==0
       111144  +#endif
       111145  +          && db->nVdbeExec==0;
       111146  +}
110687 111147   
110688 111148   /*
110689 111149   ** Check to make sure the given table is writable.  If it is not
110690 111150   ** writable, generate an error message and return 1.  If it is
110691 111151   ** writable return 0;
110692 111152   */
110693 111153   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
110694         -  /* A table is not writable under the following circumstances:
110695         -  **
110696         -  **   1) It is a virtual table and no implementation of the xUpdate method
110697         -  **      has been provided, or
110698         -  **   2) It is a system table (i.e. sqlite_master), this call is not
110699         -  **      part of a nested parse and writable_schema pragma has not 
110700         -  **      been specified.
110701         -  **
110702         -  ** In either case leave an error message in pParse and return non-zero.
110703         -  */
110704         -  if( ( IsVirtual(pTab) 
110705         -     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
110706         -   || ( (pTab->tabFlags & TF_Readonly)!=0
110707         -     && (pParse->db->flags & SQLITE_WriteSchema)==0
110708         -     && pParse->nested==0 )
110709         -  ){
       111154  +  if( tabIsReadOnly(pParse, pTab) ){
110710 111155       sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
110711 111156       return 1;
110712 111157     }
110713         -
110714 111158   #ifndef SQLITE_OMIT_VIEW
110715 111159     if( !viewOk && pTab->pSelect ){
110716 111160       sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
110717 111161       return 1;
110718 111162     }
110719 111163   #endif
110720 111164     return 0;
................................................................................
114110 114554     sqlite3 *db,      /* The database connection */
114111 114555     Table *pTab,      /* The table whose column is desired */
114112 114556     int iCursor,      /* The open cursor on the table */
114113 114557     i16 iCol          /* The column that is wanted */
114114 114558   ){
114115 114559     Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
114116 114560     if( pExpr ){
114117         -    pExpr->pTab = pTab;
       114561  +    pExpr->y.pTab = pTab;
114118 114562       pExpr->iTable = iCursor;
114119 114563       pExpr->iColumn = iCol;
114120 114564     }
114121 114565     return pExpr;
114122 114566   }
114123 114567   
114124 114568   /*
................................................................................
115186 115630         zColAff[i] = pTab->aCol[i].affinity;
115187 115631       }
115188 115632       do{
115189 115633         zColAff[i--] = 0;
115190 115634       }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
115191 115635       pTab->zColAff = zColAff;
115192 115636     }
115193         -  i = sqlite3Strlen30(zColAff);
       115637  +  assert( zColAff!=0 );
       115638  +  i = sqlite3Strlen30NN(zColAff);
115194 115639     if( i ){
115195 115640       if( iReg ){
115196 115641         sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
115197 115642       }else{
115198 115643         sqlite3VdbeChangeP4(v, -1, zColAff, i);
115199 115644       }
115200 115645     }
................................................................................
116166 116611    #undef pTrigger
116167 116612   #endif
116168 116613   #ifdef tmask
116169 116614    #undef tmask
116170 116615   #endif
116171 116616   
116172 116617   /*
116173         -** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
       116618  +** Meanings of bits in of pWalker->eCode for 
       116619  +** sqlite3ExprReferencesUpdatedColumn()
116174 116620   */
116175 116621   #define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
116176 116622   #define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
116177 116623   
116178         -/* This is the Walker callback from checkConstraintUnchanged().  Set
116179         -** bit 0x01 of pWalker->eCode if
116180         -** pWalker->eCode to 0 if this expression node references any of the
       116624  +/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
       116625  +*  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
       116626  +** expression node references any of the
116181 116627   ** columns that are being modifed by an UPDATE statement.
116182 116628   */
116183 116629   static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
116184 116630     if( pExpr->op==TK_COLUMN ){
116185 116631       assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
116186 116632       if( pExpr->iColumn>=0 ){
116187 116633         if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
................................................................................
116195 116641   }
116196 116642   
116197 116643   /*
116198 116644   ** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
116199 116645   ** only columns that are modified by the UPDATE are those for which
116200 116646   ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
116201 116647   **
116202         -** Return true if CHECK constraint pExpr does not use any of the
       116648  +** Return true if CHECK constraint pExpr uses any of the
116203 116649   ** changing columns (or the rowid if it is changing).  In other words,
116204         -** return true if this CHECK constraint can be skipped when validating
       116650  +** return true if this CHECK constraint must be validated for
116205 116651   ** the new row in the UPDATE statement.
       116652  +**
       116653  +** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
       116654  +** The operation of this routine is the same - return true if an only if
       116655  +** the expression uses one or more of columns identified by the second and
       116656  +** third arguments.
116206 116657   */
116207         -static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
       116658  +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
       116659  +  Expr *pExpr,    /* The expression to be checked */
       116660  +  int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
       116661  +  int chngRowid   /* True if UPDATE changes the rowid */
       116662  +){
116208 116663     Walker w;
116209 116664     memset(&w, 0, sizeof(w));
116210 116665     w.eCode = 0;
116211 116666     w.xExprCallback = checkConstraintExprNode;
116212 116667     w.u.aiCol = aiChng;
116213 116668     sqlite3WalkExpr(&w, pExpr);
116214 116669     if( !chngRowid ){
................................................................................
116215 116670       testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
116216 116671       w.eCode &= ~CKCNSTRNT_ROWID;
116217 116672     }
116218 116673     testcase( w.eCode==0 );
116219 116674     testcase( w.eCode==CKCNSTRNT_COLUMN );
116220 116675     testcase( w.eCode==CKCNSTRNT_ROWID );
116221 116676     testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
116222         -  return !w.eCode;
       116677  +  return w.eCode!=0;
116223 116678   }
116224 116679   
116225 116680   /*
116226 116681   ** Generate code to do constraint checks prior to an INSERT or an UPDATE
116227 116682   ** on table pTab.
116228 116683   **
116229 116684   ** The regNewData parameter is the first register in a range that contains
................................................................................
116421 116876     if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
116422 116877       ExprList *pCheck = pTab->pCheck;
116423 116878       pParse->iSelfTab = -(regNewData+1);
116424 116879       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
116425 116880       for(i=0; i<pCheck->nExpr; i++){
116426 116881         int allOk;
116427 116882         Expr *pExpr = pCheck->a[i].pExpr;
116428         -      if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
       116883  +      if( aiChng
       116884  +       && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
       116885  +      ){
       116886  +        /* The check constraints do not reference any of the columns being
       116887  +        ** updated so there is no point it verifying the check constraint */
       116888  +        continue;
       116889  +      }
116429 116890         allOk = sqlite3VdbeMakeLabel(v);
116430 116891         sqlite3VdbeVerifyAbortable(v, onError);
116431 116892         sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
116432 116893         if( onError==OE_Ignore ){
116433 116894           sqlite3VdbeGoto(v, ignoreDest);
116434 116895         }else{
116435 116896           char *zName = pCheck->a[i].zName;
................................................................................
117922 118383     void (*str_append)(sqlite3_str*, const char *zIn, int N);
117923 118384     void (*str_appendall)(sqlite3_str*, const char *zIn);
117924 118385     void (*str_appendchar)(sqlite3_str*, int N, char C);
117925 118386     void (*str_reset)(sqlite3_str*);
117926 118387     int (*str_errcode)(sqlite3_str*);
117927 118388     int (*str_length)(sqlite3_str*);
117928 118389     char *(*str_value)(sqlite3_str*);
       118390  +  /* Version 3.25.0 and later */
117929 118391     int (*create_window_function)(sqlite3*,const char*,int,int,void*,
117930 118392                               void (*xStep)(sqlite3_context*,int,sqlite3_value**),
117931 118393                               void (*xFinal)(sqlite3_context*),
117932 118394                               void (*xValue)(sqlite3_context*),
117933 118395                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
117934 118396                               void(*xDestroy)(void*));
       118397  +  /* Version 3.26.0 and later */
       118398  +  const char *(*normalized_sql)(sqlite3_stmt*);
117935 118399   };
117936 118400   
117937 118401   /*
117938 118402   ** This is the function signature used for all extension entry points.  It
117939 118403   ** is also defined in the file "loadext.c".
117940 118404   */
117941 118405   typedef int (*sqlite3_loadext_entry)(
................................................................................
118215 118679   #define sqlite3_str_appendchar         sqlite3_api->str_appendchar
118216 118680   #define sqlite3_str_reset              sqlite3_api->str_reset
118217 118681   #define sqlite3_str_errcode            sqlite3_api->str_errcode
118218 118682   #define sqlite3_str_length             sqlite3_api->str_length
118219 118683   #define sqlite3_str_value              sqlite3_api->str_value
118220 118684   /* Version 3.25.0 and later */
118221 118685   #define sqlite3_create_window_function sqlite3_api->create_window_function
       118686  +/* Version 3.26.0 and later */
       118687  +#define sqlite3_normalized_sql         sqlite3_api->normalized_sql
118222 118688   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
118223 118689   
118224 118690   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
118225 118691     /* This case when the file really is being compiled as a loadable 
118226 118692     ** extension */
118227 118693   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
118228 118694   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
118303 118769   
118304 118770   #ifdef SQLITE_OMIT_VIRTUALTABLE
118305 118771   # define sqlite3_create_module 0
118306 118772   # define sqlite3_create_module_v2 0
118307 118773   # define sqlite3_declare_vtab 0
118308 118774   # define sqlite3_vtab_config 0
118309 118775   # define sqlite3_vtab_on_conflict 0
       118776  +# define sqlite3_vtab_collation 0
118310 118777   #endif
118311 118778   
118312 118779   #ifdef SQLITE_OMIT_SHARED_CACHE
118313 118780   # define sqlite3_enable_shared_cache 0
118314 118781   #endif
118315 118782   
118316 118783   #if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
................................................................................
118670 119137     sqlite3_str_appendall,
118671 119138     sqlite3_str_appendchar,
118672 119139     sqlite3_str_reset,
118673 119140     sqlite3_str_errcode,
118674 119141     sqlite3_str_length,
118675 119142     sqlite3_str_value,
118676 119143     /* Version 3.25.0 and later */
118677         -  sqlite3_create_window_function
       119144  +  sqlite3_create_window_function,
       119145  +  /* Version 3.26.0 and later */
       119146  +#ifdef SQLITE_ENABLE_NORMALIZE
       119147  +  sqlite3_normalized_sql
       119148  +#else
       119149  +  0
       119150  +#endif
118678 119151   };
118679 119152   
118680 119153   /*
118681 119154   ** Attempt to load an SQLite extension library contained in the file
118682 119155   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
118683 119156   ** default entry point name (sqlite3_extension_init) is used.  Use
118684 119157   ** of the default name is recommended.
................................................................................
119120 119593   #define PragTyp_TEMP_STORE_DIRECTORY          36
119121 119594   #define PragTyp_THREADS                       37
119122 119595   #define PragTyp_WAL_AUTOCHECKPOINT            38
119123 119596   #define PragTyp_WAL_CHECKPOINT                39
119124 119597   #define PragTyp_ACTIVATE_EXTENSIONS           40
119125 119598   #define PragTyp_HEXKEY                        41
119126 119599   #define PragTyp_KEY                           42
119127         -#define PragTyp_REKEY                         43
119128         -#define PragTyp_LOCK_STATUS                   44
119129         -#define PragTyp_PARSER_TRACE                  45
119130         -#define PragTyp_STATS                         46
       119600  +#define PragTyp_LOCK_STATUS                   43
       119601  +#define PragTyp_PARSER_TRACE                  44
       119602  +#define PragTyp_STATS                         45
119131 119603   
119132 119604   /* Property flags associated with various pragma. */
119133 119605   #define PragFlg_NeedSchema 0x01 /* Force schema load before running */
119134 119606   #define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
119135 119607   #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
119136 119608   #define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
119137 119609   #define PragFlg_Result0    0x10 /* Acts as query when no argument */
................................................................................
119140 119612   #define PragFlg_SchemaReq  0x80 /* Schema required - "main" is default */
119141 119613   
119142 119614   /* Names of columns for pragmas that return multi-column result
119143 119615   ** or that return single-column results where the name of the
119144 119616   ** result column is different from the name of the pragma
119145 119617   */
119146 119618   static const char *const pragCName[] = {
119147         -  /*   0 */ "cache_size",  /* Used by: default_cache_size */
119148         -  /*   1 */ "cid",         /* Used by: table_info */
119149         -  /*   2 */ "name",       
119150         -  /*   3 */ "type",       
119151         -  /*   4 */ "notnull",    
119152         -  /*   5 */ "dflt_value", 
119153         -  /*   6 */ "pk",         
119154         -  /*   7 */ "tbl",         /* Used by: stats */
119155         -  /*   8 */ "idx",        
119156         -  /*   9 */ "wdth",       
119157         -  /*  10 */ "hght",       
119158         -  /*  11 */ "flgs",       
119159         -  /*  12 */ "seqno",       /* Used by: index_info */
119160         -  /*  13 */ "cid",        
119161         -  /*  14 */ "name",       
       119619  +  /*   0 */ "id",          /* Used by: foreign_key_list */
       119620  +  /*   1 */ "seq",        
       119621  +  /*   2 */ "table",      
       119622  +  /*   3 */ "from",       
       119623  +  /*   4 */ "to",         
       119624  +  /*   5 */ "on_update",  
       119625  +  /*   6 */ "on_delete",  
       119626  +  /*   7 */ "match",      
       119627  +  /*   8 */ "cid",         /* Used by: table_xinfo */
       119628  +  /*   9 */ "name",       
       119629  +  /*  10 */ "type",       
       119630  +  /*  11 */ "notnull",    
       119631  +  /*  12 */ "dflt_value", 
       119632  +  /*  13 */ "pk",         
       119633  +  /*  14 */ "hidden",     
       119634  +                           /* table_info reuses 8 */
119162 119635     /*  15 */ "seqno",       /* Used by: index_xinfo */
119163 119636     /*  16 */ "cid",        
119164 119637     /*  17 */ "name",       
119165 119638     /*  18 */ "desc",       
119166 119639     /*  19 */ "coll",       
119167 119640     /*  20 */ "key",        
119168         -  /*  21 */ "seq",         /* Used by: index_list */
119169         -  /*  22 */ "name",       
119170         -  /*  23 */ "unique",     
119171         -  /*  24 */ "origin",     
119172         -  /*  25 */ "partial",    
119173         -  /*  26 */ "seq",         /* Used by: database_list */
       119641  +  /*  21 */ "tbl",         /* Used by: stats */
       119642  +  /*  22 */ "idx",        
       119643  +  /*  23 */ "wdth",       
       119644  +  /*  24 */ "hght",       
       119645  +  /*  25 */ "flgs",       
       119646  +  /*  26 */ "seq",         /* Used by: index_list */
119174 119647     /*  27 */ "name",       
119175         -  /*  28 */ "file",       
119176         -  /*  29 */ "name",        /* Used by: function_list */
119177         -  /*  30 */ "builtin",    
119178         -  /*  31 */ "name",        /* Used by: module_list pragma_list */
119179         -  /*  32 */ "seq",         /* Used by: collation_list */
119180         -  /*  33 */ "name",       
119181         -  /*  34 */ "id",          /* Used by: foreign_key_list */
119182         -  /*  35 */ "seq",        
119183         -  /*  36 */ "table",      
119184         -  /*  37 */ "from",       
119185         -  /*  38 */ "to",         
119186         -  /*  39 */ "on_update",  
119187         -  /*  40 */ "on_delete",  
119188         -  /*  41 */ "match",      
119189         -  /*  42 */ "table",       /* Used by: foreign_key_check */
119190         -  /*  43 */ "rowid",      
119191         -  /*  44 */ "parent",     
119192         -  /*  45 */ "fkid",       
119193         -  /*  46 */ "busy",        /* Used by: wal_checkpoint */
119194         -  /*  47 */ "log",        
119195         -  /*  48 */ "checkpointed",
119196         -  /*  49 */ "timeout",     /* Used by: busy_timeout */
119197         -  /*  50 */ "database",    /* Used by: lock_status */
119198         -  /*  51 */ "status",     
       119648  +  /*  28 */ "unique",     
       119649  +  /*  29 */ "origin",     
       119650  +  /*  30 */ "partial",    
       119651  +  /*  31 */ "table",       /* Used by: foreign_key_check */
       119652  +  /*  32 */ "rowid",      
       119653  +  /*  33 */ "parent",     
       119654  +  /*  34 */ "fkid",       
       119655  +                           /* index_info reuses 15 */
       119656  +  /*  35 */ "seq",         /* Used by: database_list */
       119657  +  /*  36 */ "name",       
       119658  +  /*  37 */ "file",       
       119659  +  /*  38 */ "busy",        /* Used by: wal_checkpoint */
       119660  +  /*  39 */ "log",        
       119661  +  /*  40 */ "checkpointed",
       119662  +  /*  41 */ "name",        /* Used by: function_list */
       119663  +  /*  42 */ "builtin",    
       119664  +                           /* collation_list reuses 26 */
       119665  +  /*  43 */ "database",    /* Used by: lock_status */
       119666  +  /*  44 */ "status",     
       119667  +  /*  45 */ "cache_size",  /* Used by: default_cache_size */
       119668  +                           /* module_list pragma_list reuses 9 */
       119669  +  /*  46 */ "timeout",     /* Used by: busy_timeout */
119199 119670   };
119200 119671   
119201 119672   /* Definitions of all built-in pragmas */
119202 119673   typedef struct PragmaName {
119203 119674     const char *const zName; /* Name of pragma */
119204 119675     u8 ePragTyp;             /* PragTyp_XXX value */
119205 119676     u8 mPragFlg;             /* Zero or more PragFlg_XXX values */
119206 119677     u8 iPragCName;           /* Start of column names in pragCName[] */
119207 119678     u8 nPragCName;           /* Num of col names. 0 means use pragma name */
119208         -  u32 iArg;                /* Extra argument */
       119679  +  u64 iArg;                /* Extra argument */
119209 119680   } PragmaName;
119210 119681   static const PragmaName aPragmaName[] = {
119211 119682   #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
119212 119683    {/* zName:     */ "activate_extensions",
119213 119684     /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
119214 119685     /* ePragFlg:  */ 0,
119215 119686     /* ColNames:  */ 0, 0,
................................................................................
119237 119708     /* ColNames:  */ 0, 0,
119238 119709     /* iArg:      */ SQLITE_AutoIndex },
119239 119710   #endif
119240 119711   #endif
119241 119712    {/* zName:     */ "busy_timeout",
119242 119713     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
119243 119714     /* ePragFlg:  */ PragFlg_Result0,
119244         -  /* ColNames:  */ 49, 1,
       119715  +  /* ColNames:  */ 46, 1,
119245 119716     /* iArg:      */ 0 },
119246 119717   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119247 119718    {/* zName:     */ "cache_size",
119248 119719     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
119249 119720     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119250 119721     /* ColNames:  */ 0, 0,
119251 119722     /* iArg:      */ 0 },
................................................................................
119274 119745     /* ColNames:  */ 0, 0,
119275 119746     /* iArg:      */ SQLITE_CkptFullFSync },
119276 119747   #endif
119277 119748   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119278 119749    {/* zName:     */ "collation_list",
119279 119750     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
119280 119751     /* ePragFlg:  */ PragFlg_Result0,
119281         -  /* ColNames:  */ 32, 2,
       119752  +  /* ColNames:  */ 26, 2,
119282 119753     /* iArg:      */ 0 },
119283 119754   #endif
119284 119755   #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
119285 119756    {/* zName:     */ "compile_options",
119286 119757     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
119287 119758     /* ePragFlg:  */ PragFlg_Result0,
119288 119759     /* ColNames:  */ 0, 0,
................................................................................
119309 119780     /* ColNames:  */ 0, 0,
119310 119781     /* iArg:      */ BTREE_DATA_VERSION },
119311 119782   #endif
119312 119783   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119313 119784    {/* zName:     */ "database_list",
119314 119785     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
119315 119786     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
119316         -  /* ColNames:  */ 26, 3,
       119787  +  /* ColNames:  */ 35, 3,
119317 119788     /* iArg:      */ 0 },
119318 119789   #endif
119319 119790   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
119320 119791    {/* zName:     */ "default_cache_size",
119321 119792     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
119322 119793     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119323         -  /* ColNames:  */ 0, 1,
       119794  +  /* ColNames:  */ 45, 1,
119324 119795     /* iArg:      */ 0 },
119325 119796   #endif
119326 119797   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119327 119798   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119328 119799    {/* zName:     */ "defer_foreign_keys",
119329 119800     /* ePragTyp:  */ PragTyp_FLAG,
119330 119801     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
119346 119817     /* ColNames:  */ 0, 0,
119347 119818     /* iArg:      */ 0 },
119348 119819   #endif
119349 119820   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119350 119821    {/* zName:     */ "foreign_key_check",
119351 119822     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
119352 119823     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
119353         -  /* ColNames:  */ 42, 4,
       119824  +  /* ColNames:  */ 31, 4,
119354 119825     /* iArg:      */ 0 },
119355 119826   #endif
119356 119827   #if !defined(SQLITE_OMIT_FOREIGN_KEY)
119357 119828    {/* zName:     */ "foreign_key_list",
119358 119829     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
119359 119830     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119360         -  /* ColNames:  */ 34, 8,
       119831  +  /* ColNames:  */ 0, 8,
119361 119832     /* iArg:      */ 0 },
119362 119833   #endif
119363 119834   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119364 119835   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119365 119836    {/* zName:     */ "foreign_keys",
119366 119837     /* ePragTyp:  */ PragTyp_FLAG,
119367 119838     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
119389 119860     /* iArg:      */ SQLITE_FullFSync },
119390 119861   #endif
119391 119862   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119392 119863   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119393 119864    {/* zName:     */ "function_list",
119394 119865     /* ePragTyp:  */ PragTyp_FUNCTION_LIST,
119395 119866     /* ePragFlg:  */ PragFlg_Result0,
119396         -  /* ColNames:  */ 29, 2,
       119867  +  /* ColNames:  */ 41, 2,
119397 119868     /* iArg:      */ 0 },
119398 119869   #endif
119399 119870   #endif
119400 119871   #if defined(SQLITE_HAS_CODEC)
119401 119872    {/* zName:     */ "hexkey",
119402 119873     /* ePragTyp:  */ PragTyp_HEXKEY,
119403 119874     /* ePragFlg:  */ 0,
119404 119875     /* ColNames:  */ 0, 0,
119405         -  /* iArg:      */ 0 },
       119876  +  /* iArg:      */ 2 },
119406 119877    {/* zName:     */ "hexrekey",
119407 119878     /* ePragTyp:  */ PragTyp_HEXKEY,
119408 119879     /* ePragFlg:  */ 0,
119409 119880     /* ColNames:  */ 0, 0,
119410         -  /* iArg:      */ 0 },
       119881  +  /* iArg:      */ 3 },
119411 119882   #endif
119412 119883   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119413 119884   #if !defined(SQLITE_OMIT_CHECK)
119414 119885    {/* zName:     */ "ignore_check_constraints",
119415 119886     /* ePragTyp:  */ PragTyp_FLAG,
119416 119887     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119417 119888     /* ColNames:  */ 0, 0,
................................................................................
119425 119896     /* ColNames:  */ 0, 0,
119426 119897     /* iArg:      */ 0 },
119427 119898   #endif
119428 119899   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119429 119900    {/* zName:     */ "index_info",
119430 119901     /* ePragTyp:  */ PragTyp_INDEX_INFO,
119431 119902     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119432         -  /* ColNames:  */ 12, 3,
       119903  +  /* ColNames:  */ 15, 3,
119433 119904     /* iArg:      */ 0 },
119434 119905    {/* zName:     */ "index_list",
119435 119906     /* ePragTyp:  */ PragTyp_INDEX_LIST,
119436 119907     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119437         -  /* ColNames:  */ 21, 5,
       119908  +  /* ColNames:  */ 26, 5,
119438 119909     /* iArg:      */ 0 },
119439 119910    {/* zName:     */ "index_xinfo",
119440 119911     /* ePragTyp:  */ PragTyp_INDEX_INFO,
119441 119912     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119442 119913     /* ColNames:  */ 15, 6,
119443 119914     /* iArg:      */ 1 },
119444 119915   #endif
................................................................................
119465 119936    {/* zName:     */ "key",
119466 119937     /* ePragTyp:  */ PragTyp_KEY,
119467 119938     /* ePragFlg:  */ 0,
119468 119939     /* ColNames:  */ 0, 0,
119469 119940     /* iArg:      */ 0 },
119470 119941   #endif
119471 119942   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
       119943  + {/* zName:     */ "legacy_alter_table",
       119944  +  /* ePragTyp:  */ PragTyp_FLAG,
       119945  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
       119946  +  /* ColNames:  */ 0, 0,
       119947  +  /* iArg:      */ SQLITE_LegacyAlter },
119472 119948    {/* zName:     */ "legacy_file_format",
119473 119949     /* ePragTyp:  */ PragTyp_FLAG,
119474 119950     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119475 119951     /* ColNames:  */ 0, 0,
119476 119952     /* iArg:      */ SQLITE_LegacyFileFmt },
119477 119953   #endif
119478 119954   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
................................................................................
119482 119958     /* ColNames:  */ 0, 0,
119483 119959     /* iArg:      */ 0 },
119484 119960   #endif
119485 119961   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
119486 119962    {/* zName:     */ "lock_status",
119487 119963     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
119488 119964     /* ePragFlg:  */ PragFlg_Result0,
119489         -  /* ColNames:  */ 50, 2,
       119965  +  /* ColNames:  */ 43, 2,
119490 119966     /* iArg:      */ 0 },
119491 119967   #endif
119492 119968   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119493 119969    {/* zName:     */ "locking_mode",
119494 119970     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
119495 119971     /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
119496 119972     /* ColNames:  */ 0, 0,
................................................................................
119508 119984   #endif
119509 119985   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119510 119986   #if !defined(SQLITE_OMIT_VIRTUALTABLE)
119511 119987   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119512 119988    {/* zName:     */ "module_list",
119513 119989     /* ePragTyp:  */ PragTyp_MODULE_LIST,
119514 119990     /* ePragFlg:  */ PragFlg_Result0,
119515         -  /* ColNames:  */ 31, 1,
       119991  +  /* ColNames:  */ 9, 1,
119516 119992     /* iArg:      */ 0 },
119517 119993   #endif
119518 119994   #endif
119519 119995   #endif
119520 119996    {/* zName:     */ "optimize",
119521 119997     /* ePragTyp:  */ PragTyp_OPTIMIZE,
119522 119998     /* ePragFlg:  */ PragFlg_Result1|PragFlg_NeedSchema,
................................................................................
119541 120017     /* ColNames:  */ 0, 0,
119542 120018     /* iArg:      */ 0 },
119543 120019   #endif
119544 120020   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119545 120021    {/* zName:     */ "pragma_list",
119546 120022     /* ePragTyp:  */ PragTyp_PRAGMA_LIST,
119547 120023     /* ePragFlg:  */ PragFlg_Result0,
119548         -  /* ColNames:  */ 31, 1,
       120024  +  /* ColNames:  */ 9, 1,
119549 120025     /* iArg:      */ 0 },
119550 120026   #endif
119551 120027   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119552 120028    {/* zName:     */ "query_only",
119553 120029     /* ePragTyp:  */ PragTyp_FLAG,
119554 120030     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119555 120031     /* ColNames:  */ 0, 0,
................................................................................
119572 120048     /* ePragTyp:  */ PragTyp_FLAG,
119573 120049     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119574 120050     /* ColNames:  */ 0, 0,
119575 120051     /* iArg:      */ SQLITE_RecTriggers },
119576 120052   #endif
119577 120053   #if defined(SQLITE_HAS_CODEC)
119578 120054    {/* zName:     */ "rekey",
119579         -  /* ePragTyp:  */ PragTyp_REKEY,
       120055  +  /* ePragTyp:  */ PragTyp_KEY,
119580 120056     /* ePragFlg:  */ 0,
119581 120057     /* ColNames:  */ 0, 0,
119582         -  /* iArg:      */ 0 },
       120058  +  /* iArg:      */ 1 },
119583 120059   #endif
119584 120060   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119585 120061    {/* zName:     */ "reverse_unordered_selects",
119586 120062     /* ePragTyp:  */ PragTyp_FLAG,
119587 120063     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119588 120064     /* ColNames:  */ 0, 0,
119589 120065     /* iArg:      */ SQLITE_ReverseOrder },
................................................................................
119628 120104     /* iArg:      */ SQLITE_SqlTrace },
119629 120105   #endif
119630 120106   #endif
119631 120107   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
119632 120108    {/* zName:     */ "stats",
119633 120109     /* ePragTyp:  */ PragTyp_STATS,
119634 120110     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119635         -  /* ColNames:  */ 7, 5,
       120111  +  /* ColNames:  */ 21, 5,
119636 120112     /* iArg:      */ 0 },
119637 120113   #endif
119638 120114   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119639 120115    {/* zName:     */ "synchronous",
119640 120116     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
119641 120117     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119642 120118     /* ColNames:  */ 0, 0,
119643 120119     /* iArg:      */ 0 },
119644 120120   #endif
119645 120121   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119646 120122    {/* zName:     */ "table_info",
119647 120123     /* ePragTyp:  */ PragTyp_TABLE_INFO,
119648 120124     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119649         -  /* ColNames:  */ 1, 6,
       120125  +  /* ColNames:  */ 8, 6,
119650 120126     /* iArg:      */ 0 },
       120127  + {/* zName:     */ "table_xinfo",
       120128  +  /* ePragTyp:  */ PragTyp_TABLE_INFO,
       120129  +  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
       120130  +  /* ColNames:  */ 8, 7,
       120131  +  /* iArg:      */ 1 },
119651 120132   #endif
119652 120133   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119653 120134    {/* zName:     */ "temp_store",
119654 120135     /* ePragTyp:  */ PragTyp_TEMP_STORE,
119655 120136     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119656 120137     /* ColNames:  */ 0, 0,
119657 120138     /* iArg:      */ 0 },
119658 120139    {/* zName:     */ "temp_store_directory",
119659 120140     /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
119660 120141     /* ePragFlg:  */ PragFlg_NoColumns1,
119661 120142     /* ColNames:  */ 0, 0,
119662 120143     /* iArg:      */ 0 },
       120144  +#endif
       120145  +#if defined(SQLITE_HAS_CODEC)
       120146  + {/* zName:     */ "textkey",
       120147  +  /* ePragTyp:  */ PragTyp_KEY,
       120148  +  /* ePragFlg:  */ 0,
       120149  +  /* ColNames:  */ 0, 0,
       120150  +  /* iArg:      */ 4 },
       120151  + {/* zName:     */ "textrekey",
       120152  +  /* ePragTyp:  */ PragTyp_KEY,
       120153  +  /* ePragFlg:  */ 0,
       120154  +  /* ColNames:  */ 0, 0,
       120155  +  /* iArg:      */ 5 },
119663 120156   #endif
119664 120157    {/* zName:     */ "threads",
119665 120158     /* ePragTyp:  */ PragTyp_THREADS,
119666 120159     /* ePragFlg:  */ PragFlg_Result0,
119667 120160     /* ColNames:  */ 0, 0,
119668 120161     /* iArg:      */ 0 },
119669 120162   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
................................................................................
119707 120200     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
119708 120201     /* ePragFlg:  */ 0,
119709 120202     /* ColNames:  */ 0, 0,
119710 120203     /* iArg:      */ 0 },
119711 120204    {/* zName:     */ "wal_checkpoint",
119712 120205     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
119713 120206     /* ePragFlg:  */ PragFlg_NeedSchema,
119714         -  /* ColNames:  */ 46, 3,
       120207  +  /* ColNames:  */ 38, 3,
119715 120208     /* iArg:      */ 0 },
119716 120209   #endif
119717 120210   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119718 120211    {/* zName:     */ "writable_schema",
119719 120212     /* ePragTyp:  */ PragTyp_FLAG,
119720 120213     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119721 120214     /* ColNames:  */ 0, 0,
119722         -  /* iArg:      */ SQLITE_WriteSchema },
       120215  +  /* iArg:      */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
119723 120216   #endif
119724 120217   };
119725         -/* Number of pragmas: 60 on by default, 77 total. */
       120218  +/* Number of pragmas: 62 on by default, 81 total. */
119726 120219   
119727 120220   /************** End of pragma.h **********************************************/
119728 120221   /************** Continuing where we left off in pragma.c *********************/
119729 120222   
119730 120223   /*
119731 120224   ** Interpret the given string as a safety level.  Return 0 for OFF,
119732 120225   ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or 
................................................................................
120730 121223   
120731 121224   #ifndef SQLITE_OMIT_FLAG_PRAGMAS
120732 121225     case PragTyp_FLAG: {
120733 121226       if( zRight==0 ){
120734 121227         setPragmaResultColumnNames(v, pPragma);
120735 121228         returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
120736 121229       }else{
120737         -      int mask = pPragma->iArg;    /* Mask of bits to set or clear. */
       121230  +      u64 mask = pPragma->iArg;    /* Mask of bits to set or clear. */
120738 121231         if( db->autoCommit==0 ){
120739 121232           /* Foreign key support may not be enabled or disabled while not
120740 121233           ** in auto-commit mode.  */
120741 121234           mask &= ~(SQLITE_ForeignKeys);
120742 121235         }
120743 121236   #if SQLITE_USER_AUTHENTICATION
120744 121237         if( db->auth.authLevel==UAUTH_User ){
................................................................................
120779 121272     ** dflt_value: The default value for the column, if any.
120780 121273     ** pk:         Non-zero for PK fields.
120781 121274     */
120782 121275     case PragTyp_TABLE_INFO: if( zRight ){
120783 121276       Table *pTab;
120784 121277       pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
120785 121278       if( pTab ){
       121279  +      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120786 121280         int i, k;
120787 121281         int nHidden = 0;
120788 121282         Column *pCol;
120789 121283         Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120790         -      pParse->nMem = 6;
120791         -      sqlite3CodeVerifySchema(pParse, iDb);
       121284  +      pParse->nMem = 7;
       121285  +      sqlite3CodeVerifySchema(pParse, iTabDb);
120792 121286         sqlite3ViewGetColumnNames(pParse, pTab);
120793 121287         for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
120794         -        if( IsHiddenColumn(pCol) ){
       121288  +        int isHidden = IsHiddenColumn(pCol);
       121289  +        if( isHidden && pPragma->iArg==0 ){
120795 121290             nHidden++;
120796 121291             continue;
120797 121292           }
120798 121293           if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
120799 121294             k = 0;
120800 121295           }else if( pPk==0 ){
120801 121296             k = 1;
120802 121297           }else{
120803 121298             for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
120804 121299           }
120805 121300           assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
120806         -        sqlite3VdbeMultiLoad(v, 1, "issisi",
       121301  +        sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
120807 121302                  i-nHidden,
120808 121303                  pCol->zName,
120809 121304                  sqlite3ColumnType(pCol,""),
120810 121305                  pCol->notNull ? 1 : 0,
120811 121306                  pCol->pDflt ? pCol->pDflt->u.zToken : 0,
120812         -               k);
       121307  +               k,
       121308  +               isHidden);
120813 121309         }
120814 121310       }
120815 121311     }
120816 121312     break;
120817 121313   
120818 121314   #ifdef SQLITE_DEBUG
120819 121315     case PragTyp_STATS: {
................................................................................
120843 121339   #endif
120844 121340   
120845 121341     case PragTyp_INDEX_INFO: if( zRight ){
120846 121342       Index *pIdx;
120847 121343       Table *pTab;
120848 121344       pIdx = sqlite3FindIndex(db, zRight, zDb);
120849 121345       if( pIdx ){
       121346  +      int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
120850 121347         int i;
120851 121348         int mx;
120852 121349         if( pPragma->iArg ){
120853 121350           /* PRAGMA index_xinfo (newer version with more rows and columns) */
120854 121351           mx = pIdx->nColumn;
120855 121352           pParse->nMem = 6;
120856 121353         }else{
120857 121354           /* PRAGMA index_info (legacy version) */
120858 121355           mx = pIdx->nKeyCol;
120859 121356           pParse->nMem = 3;
120860 121357         }
120861 121358         pTab = pIdx->pTable;
120862         -      sqlite3CodeVerifySchema(pParse, iDb);
       121359  +      sqlite3CodeVerifySchema(pParse, iIdxDb);
120863 121360         assert( pParse->nMem<=pPragma->nPragCName );
120864 121361         for(i=0; i<mx; i++){
120865 121362           i16 cnum = pIdx->aiColumn[i];
120866 121363           sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
120867 121364                                cnum<0 ? 0 : pTab->aCol[cnum].zName);
120868 121365           if( pPragma->iArg ){
120869 121366             sqlite3VdbeMultiLoad(v, 4, "isiX",
................................................................................
120879 121376   
120880 121377     case PragTyp_INDEX_LIST: if( zRight ){
120881 121378       Index *pIdx;
120882 121379       Table *pTab;
120883 121380       int i;
120884 121381       pTab = sqlite3FindTable(db, zRight, zDb);
120885 121382       if( pTab ){
       121383  +      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120886 121384         pParse->nMem = 5;
120887         -      sqlite3CodeVerifySchema(pParse, iDb);
       121385  +      sqlite3CodeVerifySchema(pParse, iTabDb);
120888 121386         for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
120889 121387           const char *azOrigin[] = { "c", "u", "pk" };
120890 121388           sqlite3VdbeMultiLoad(v, 1, "isisi",
120891 121389              i,
120892 121390              pIdx->zName,
120893 121391              IsUniqueIndex(pIdx),
120894 121392              azOrigin[pIdx->idxType],
................................................................................
120927 121425     case PragTyp_FUNCTION_LIST: {
120928 121426       int i;
120929 121427       HashElem *j;
120930 121428       FuncDef *p;
120931 121429       pParse->nMem = 2;
120932 121430       for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
120933 121431         for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
       121432  +        if( p->funcFlags & SQLITE_FUNC_INTERNAL ) continue;
120934 121433           sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
120935 121434         }
120936 121435       }
120937 121436       for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
120938 121437         p = (FuncDef*)sqliteHashData(j);
120939 121438         sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
120940 121439       }
................................................................................
120968 121467     case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
120969 121468       FKey *pFK;
120970 121469       Table *pTab;
120971 121470       pTab = sqlite3FindTable(db, zRight, zDb);
120972 121471       if( pTab ){
120973 121472         pFK = pTab->pFKey;
120974 121473         if( pFK ){
       121474  +        int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120975 121475           int i = 0; 
120976 121476           pParse->nMem = 8;
120977         -        sqlite3CodeVerifySchema(pParse, iDb);
       121477  +        sqlite3CodeVerifySchema(pParse, iTabDb);
120978 121478           while(pFK){
120979 121479             int j;
120980 121480             for(j=0; j<pFK->nCol; j++){
120981 121481               sqlite3VdbeMultiLoad(v, 1, "iissssss",
120982 121482                      i,
120983 121483                      j,
120984 121484                      pFK->zTo,
................................................................................
121015 121515       int addrOk;            /* Jump here if the key is OK */
121016 121516       int *aiCols;           /* child to parent column mapping */
121017 121517   
121018 121518       regResult = pParse->nMem+1;
121019 121519       pParse->nMem += 4;
121020 121520       regKey = ++pParse->nMem;
121021 121521       regRow = ++pParse->nMem;
121022         -    sqlite3CodeVerifySchema(pParse, iDb);
121023 121522       k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
121024 121523       while( k ){
       121524  +      int iTabDb;
121025 121525         if( zRight ){
121026 121526           pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
121027 121527           k = 0;
121028 121528         }else{
121029 121529           pTab = (Table*)sqliteHashData(k);
121030 121530           k = sqliteHashNext(k);
121031 121531         }
121032 121532         if( pTab==0 || pTab->pFKey==0 ) continue;
121033         -      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       121533  +      iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       121534  +      sqlite3CodeVerifySchema(pParse, iTabDb);
       121535  +      sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
121034 121536         if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
121035         -      sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
       121537  +      sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
121036 121538         sqlite3VdbeLoadString(v, regResult, pTab->zName);
121037 121539         for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
121038 121540           pParent = sqlite3FindTable(db, pFK->zTo, zDb);
121039 121541           if( pParent==0 ) continue;
121040 121542           pIdx = 0;
121041         -        sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
       121543  +        sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
121042 121544           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
121043 121545           if( x==0 ){
121044 121546             if( pIdx==0 ){
121045         -            sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
       121547  +            sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
121046 121548             }else{
121047         -            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
       121549  +            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb);
121048 121550               sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
121049 121551             }
121050 121552           }else{
121051 121553             k = 0;
121052 121554             break;
121053 121555           }
121054 121556         }
................................................................................
121809 122311         sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
121810 122312       }
121811 122313       break;
121812 122314     }
121813 122315   #endif
121814 122316   
121815 122317   #ifdef SQLITE_HAS_CODEC
       122318  +  /* Pragma        iArg
       122319  +  ** ----------   ------
       122320  +  **  key           0
       122321  +  **  rekey         1
       122322  +  **  hexkey        2
       122323  +  **  hexrekey      3
       122324  +  **  textkey       4
       122325  +  **  textrekey     5
       122326  +  */
121816 122327     case PragTyp_KEY: {
121817         -    if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
121818         -    break;
121819         -  }
121820         -  case PragTyp_REKEY: {
121821         -    if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
       122328  +    if( zRight ){
       122329  +      int n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
       122330  +      if( (pPragma->iArg & 1)==0 ){
       122331  +        sqlite3_key_v2(db, zDb, zRight, n);
       122332  +      }else{
       122333  +        sqlite3_rekey_v2(db, zDb, zRight, n);
       122334  +      }
       122335  +    }
121822 122336       break;
121823 122337     }
121824 122338     case PragTyp_HEXKEY: {
121825 122339       if( zRight ){
121826 122340         u8 iByte;
121827 122341         int i;
121828 122342         char zKey[40];
121829 122343         for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
121830 122344           iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
121831 122345           if( (i&1)!=0 ) zKey[i/2] = iByte;
121832 122346         }
121833         -      if( (zLeft[3] & 0xf)==0xb ){
       122347  +      if( (pPragma->iArg & 1)==0 ){
121834 122348           sqlite3_key_v2(db, zDb, zKey, i/2);
121835 122349         }else{
121836 122350           sqlite3_rekey_v2(db, zDb, zKey, i/2);
121837 122351         }
121838 122352       }
121839 122353       break;
121840 122354     }
................................................................................
122156 122670     0,                           /* xSync - sync transaction */
122157 122671     0,                           /* xCommit - commit transaction */
122158 122672     0,                           /* xRollback - rollback transaction */
122159 122673     0,                           /* xFindFunction - function overloading */
122160 122674     0,                           /* xRename - rename the table */
122161 122675     0,                           /* xSavepoint */
122162 122676     0,                           /* xRelease */
122163         -  0                            /* xRollbackTo */
       122677  +  0,                           /* xRollbackTo */
       122678  +  0                            /* xShadowName */
122164 122679   };
122165 122680   
122166 122681   /*
122167 122682   ** Check to see if zTabName is really the name of a pragma.  If it is,
122168 122683   ** then register an eponymous virtual table for that pragma and return
122169 122684   ** a pointer to the Module object for the new virtual table.
122170 122685   */
................................................................................
122509 123024       }
122510 123025   #endif
122511 123026     }
122512 123027     if( db->mallocFailed ){
122513 123028       rc = SQLITE_NOMEM_BKPT;
122514 123029       sqlite3ResetAllSchemasOfConnection(db);
122515 123030     }
122516         -  if( rc==SQLITE_OK || (db->flags&SQLITE_WriteSchema)){
122517         -    /* Black magic: If the SQLITE_WriteSchema flag is set, then consider
       123031  +  if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
       123032  +    /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
122518 123033       ** the schema loaded, even if errors occurred. In this situation the 
122519 123034       ** current sqlite3_prepare() operation will fail, but the following one
122520 123035       ** will attempt to compile the supplied statement against whatever subset
122521 123036       ** of the schema was loaded before the error occurred. The primary
122522 123037       ** purpose of this is to allow access to the sqlite_master table
122523 123038       ** even when its contents have been corrupted.
122524 123039       */
................................................................................
122891 123406     sqlite3BtreeLeaveAll(db);
122892 123407     rc = sqlite3ApiExit(db, rc);
122893 123408     assert( (rc&db->errMask)==rc );
122894 123409     sqlite3_mutex_leave(db->mutex);
122895 123410     return rc;
122896 123411   }
122897 123412   
       123413  +#ifdef SQLITE_ENABLE_NORMALIZE
       123414  +/*
       123415  +** Checks if the specified token is a table, column, or function name,
       123416  +** based on the databases associated with the statement being prepared.
       123417  +** If the function fails, zero is returned and pRc is filled with the
       123418  +** error code.
       123419  +*/
       123420  +static int shouldTreatAsIdentifier(
       123421  +  sqlite3 *db,        /* Database handle. */
       123422  +  const char *zToken, /* Pointer to start of token to be checked */
       123423  +  int nToken,         /* Length of token to be checked */
       123424  +  int *pRc            /* Pointer to error code upon failure */
       123425  +){
       123426  +  int bFound = 0;     /* Non-zero if token is an identifier name. */
       123427  +  int i, j;           /* Database and column loop indexes. */
       123428  +  Schema *pSchema;    /* Schema for current database. */
       123429  +  Hash *pHash;        /* Hash table of tables for current database. */
       123430  +  HashElem *e;        /* Hash element for hash table iteration. */
       123431  +  Table *pTab;        /* Database table for columns being checked. */
       123432  +
       123433  +  if( sqlite3IsRowidN(zToken, nToken) ){
       123434  +    return 1;
       123435  +  }
       123436  +  if( nToken>0 ){
       123437  +    int hash = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zToken[0]], nToken);
       123438  +    if( sqlite3FunctionSearchN(hash, zToken, nToken) ) return 1;
       123439  +  }
       123440  +  assert( db!=0 );
       123441  +  sqlite3_mutex_enter(db->mutex);
       123442  +  sqlite3BtreeEnterAll(db);
       123443  +  for(i=0; i<db->nDb; i++){
       123444  +    pHash = &db->aFunc;
       123445  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
       123446  +      bFound = 1;
       123447  +      break;
       123448  +    }
       123449  +    pSchema = db->aDb[i].pSchema;
       123450  +    if( pSchema==0 ) continue;
       123451  +    pHash = &pSchema->tblHash;
       123452  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
       123453  +      bFound = 1;
       123454  +      break;
       123455  +    }
       123456  +    for(e=sqliteHashFirst(pHash); e; e=sqliteHashNext(e)){
       123457  +      pTab = sqliteHashData(e);
       123458  +      if( pTab==0 ) continue;
       123459  +      pHash = pTab->pColHash;
       123460  +      if( pHash==0 ){
       123461  +        pTab->pColHash = pHash = sqlite3_malloc(sizeof(Hash));
       123462  +        if( pHash ){
       123463  +          sqlite3HashInit(pHash);
       123464  +          for(j=0; j<pTab->nCol; j++){
       123465  +            Column *pCol = &pTab->aCol[j];
       123466  +            sqlite3HashInsert(pHash, pCol->zName, pCol);
       123467  +          }
       123468  +        }else{
       123469  +          *pRc = SQLITE_NOMEM_BKPT;
       123470  +          bFound = 0;
       123471  +          goto done;
       123472  +        }
       123473  +      }
       123474  +      if( pHash && sqlite3HashFindN(pHash, zToken, nToken) ){
       123475  +        bFound = 1;
       123476  +        goto done;
       123477  +      }
       123478  +    }
       123479  +  }
       123480  +done:
       123481  +  sqlite3BtreeLeaveAll(db);
       123482  +  sqlite3_mutex_leave(db->mutex);
       123483  +  return bFound;
       123484  +}
       123485  +
       123486  +/*
       123487  +** Attempt to estimate the final output buffer size needed for the fully
       123488  +** normalized version of the specified SQL string.  This should take into
       123489  +** account any potential expansion that could occur (e.g. via IN clauses
       123490  +** being expanded, etc).  This size returned is the total number of bytes
       123491  +** including the NUL terminator.
       123492  +*/
       123493  +static int estimateNormalizedSize(
       123494  +  const char *zSql, /* The original SQL string */
       123495  +  int nSql,         /* Length of original SQL string */
       123496  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
       123497  +){
       123498  +  int nOut = nSql + 4;
       123499  +  const char *z = zSql;
       123500  +  while( nOut<nSql*5 ){
       123501  +    while( z[0]!=0 && z[0]!='I' && z[0]!='i' ){ z++; }
       123502  +    if( z[0]==0 ) break;
       123503  +    z++;
       123504  +    if( z[0]!='N' && z[0]!='n' ) break;
       123505  +    z++;
       123506  +    while( sqlite3Isspace(z[0]) ){ z++; }
       123507  +    if( z[0]!='(' ) break;
       123508  +    z++;
       123509  +    nOut += 5; /* ?,?,? */
       123510  +  }
       123511  +  return nOut;
       123512  +}
       123513  +
       123514  +/*
       123515  +** Copy the current token into the output buffer while dealing with quoted
       123516  +** identifiers.  By default, all letters will be converted into lowercase.
       123517  +** If the bUpper flag is set, uppercase will be used.  The piOut argument
       123518  +** will be used to update the target index into the output string.
       123519  +*/
       123520  +static void copyNormalizedToken(
       123521  +  const char *zSql, /* The original SQL string */
       123522  +  int iIn,          /* Current index into the original SQL string */
       123523  +  int nToken,       /* Number of bytes in the current token */
       123524  +  int tokenFlags,   /* Flags returned by the tokenizer */
       123525  +  char *zOut,       /* The output string */
       123526  +  int *piOut        /* Pointer to target index into the output string */
       123527  +){
       123528  +  int bQuoted = tokenFlags & SQLITE_TOKEN_QUOTED;
       123529  +  int bKeyword = tokenFlags & SQLITE_TOKEN_KEYWORD;
       123530  +  int j = *piOut, k = 0;
       123531  +  for(; k<nToken; k++){
       123532  +    if( bQuoted ){
       123533  +      if( k==0 && iIn>0 ){
       123534  +        zOut[j++] = '"';
       123535  +        continue;
       123536  +      }else if( k==nToken-1 ){
       123537  +        zOut[j++] = '"';
       123538  +        continue;
       123539  +      }
       123540  +    }
       123541  +    if( bKeyword ){
       123542  +      zOut[j++] = sqlite3Toupper(zSql[iIn+k]);
       123543  +    }else{
       123544  +      zOut[j++] = sqlite3Tolower(zSql[iIn+k]);
       123545  +    }
       123546  +  }
       123547  +  *piOut = j;
       123548  +}
       123549  +
       123550  +/*
       123551  +** Perform normalization of the SQL contained in the prepared statement and
       123552  +** store the result in the zNormSql field.  The schema for the associated
       123553  +** databases are consulted while performing the normalization in order to
       123554  +** determine if a token appears to be an identifier.  All identifiers are
       123555  +** left intact in the normalized SQL and all literals are replaced with a
       123556  +** single '?'.
       123557  +*/
       123558  +SQLITE_PRIVATE void sqlite3Normalize(
       123559  +  Vdbe *pVdbe,      /* VM being reprepared */
       123560  +  const char *zSql, /* The original SQL string */
       123561  +  int nSql,         /* Size of the input string in bytes */
       123562  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
       123563  +){
       123564  +  sqlite3 *db;           /* Database handle. */
       123565  +  char *z;               /* The output string */
       123566  +  int nZ;                /* Size of the output string in bytes */
       123567  +  int i;                 /* Next character to read from zSql[] */
       123568  +  int j;                 /* Next character to fill in on z[] */
       123569  +  int tokenType = 0;     /* Type of the next token */
       123570  +  int prevTokenType = 0; /* Type of the previous token, except spaces */
       123571  +  int n;                 /* Size of the next token */
       123572  +  int nParen = 0;        /* Nesting level of parenthesis */
       123573  +  Hash inHash;           /* Table of parenthesis levels to output index. */
       123574  +
       123575  +  db = sqlite3VdbeDb(pVdbe);
       123576  +  assert( db!=0 );
       123577  +  assert( pVdbe->zNormSql==0 );
       123578  +  if( zSql==0 ) return;
       123579  +  nZ = estimateNormalizedSize(zSql, nSql, prepFlags);
       123580  +  z = sqlite3DbMallocRawNN(db, nZ);
       123581  +  if( z==0 ) return;
       123582  +  sqlite3HashInit(&inHash);
       123583  +  for(i=j=0; i<nSql && zSql[i]; i+=n){
       123584  +    int flags = 0;
       123585  +    if( tokenType!=TK_SPACE ) prevTokenType = tokenType;
       123586  +    n = sqlite3GetTokenNormalized((unsigned char*)zSql+i, &tokenType, &flags);
       123587  +    switch( tokenType ){
       123588  +      case TK_SPACE: {
       123589  +        break;
       123590  +      }
       123591  +      case TK_ILLEGAL: {
       123592  +        sqlite3DbFree(db, z);
       123593  +        sqlite3HashClear(&inHash);
       123594  +        return;
       123595  +      }
       123596  +      case TK_STRING:
       123597  +      case TK_INTEGER:
       123598  +      case TK_FLOAT:
       123599  +      case TK_VARIABLE:
       123600  +      case TK_BLOB: {
       123601  +        z[j++] = '?';
       123602  +        break;
       123603  +      }
       123604  +      case TK_LP:
       123605  +      case TK_RP: {
       123606  +        if( tokenType==TK_LP ){
       123607  +          nParen++;
       123608  +          if( prevTokenType==TK_IN ){
       123609  +            assert( nParen<nSql );
       123610  +            sqlite3HashInsert(&inHash, zSql+nParen, SQLITE_INT_TO_PTR(j));
       123611  +          }
       123612  +        }else{
       123613  +          int jj;
       123614  +          assert( nParen<nSql );
       123615  +          jj = SQLITE_PTR_TO_INT(sqlite3HashFind(&inHash, zSql+nParen));
       123616  +          if( jj>0 ){
       123617  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
       123618  +            assert( jj+6<nZ );
       123619  +            memcpy(z+jj+1, "?,?,?", 5);
       123620  +            j = jj+6;
       123621  +            assert( nZ-1-j>=0 );
       123622  +            assert( nZ-1-j<nZ );
       123623  +            memset(z+j, 0, nZ-1-j);
       123624  +          }
       123625  +          nParen--;
       123626  +        }
       123627  +        assert( nParen>=0 );
       123628  +        /* Fall through */
       123629  +      }
       123630  +      case TK_MINUS:
       123631  +      case TK_SEMI:
       123632  +      case TK_PLUS:
       123633  +      case TK_STAR:
       123634  +      case TK_SLASH:
       123635  +      case TK_REM:
       123636  +      case TK_EQ:
       123637  +      case TK_LE:
       123638  +      case TK_NE:
       123639  +      case TK_LSHIFT:
       123640  +      case TK_LT:
       123641  +      case TK_RSHIFT:
       123642  +      case TK_GT:
       123643  +      case TK_GE:
       123644  +      case TK_BITOR:
       123645  +      case TK_CONCAT:
       123646  +      case TK_COMMA:
       123647  +      case TK_BITAND:
       123648  +      case TK_BITNOT:
       123649  +      case TK_DOT:
       123650  +      case TK_IN:
       123651  +      case TK_IS:
       123652  +      case TK_NOT:
       123653  +      case TK_NULL:
       123654  +      case TK_ID: {
       123655  +        if( tokenType==TK_NULL ){
       123656  +          if( prevTokenType==TK_IS || prevTokenType==TK_NOT ){
       123657  +            /* NULL is a keyword in this case, not a literal value */
       123658  +          }else{
       123659  +            /* Here the NULL is a literal value */
       123660  +            z[j++] = '?';
       123661  +            break;
       123662  +          }
       123663  +        }
       123664  +        if( j>0 && sqlite3IsIdChar(z[j-1]) && sqlite3IsIdChar(zSql[i]) ){
       123665  +          z[j++] = ' ';
       123666  +        }
       123667  +        if( tokenType==TK_ID ){
       123668  +          int i2 = i, n2 = n, rc = SQLITE_OK;
       123669  +          if( nParen>0 ){
       123670  +            assert( nParen<nSql );
       123671  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
       123672  +          }
       123673  +          if( flags&SQLITE_TOKEN_QUOTED ){ i2++; n2-=2; }
       123674  +          if( shouldTreatAsIdentifier(db, zSql+i2, n2, &rc)==0 ){
       123675  +            if( rc!=SQLITE_OK ){
       123676  +              sqlite3DbFree(db, z);
       123677  +              sqlite3HashClear(&inHash);
       123678  +              return;
       123679  +            }
       123680  +            if( sqlite3_keyword_check(zSql+i2, n2)==0 ){
       123681  +              z[j++] = '?';
       123682  +              break;
       123683  +            }
       123684  +          }
       123685  +        }
       123686  +        copyNormalizedToken(zSql, i, n, flags, z, &j);
       123687  +        break;
       123688  +      }
       123689  +    }
       123690  +  }
       123691  +  assert( j<nZ && "one" );
       123692  +  while( j>0 && z[j-1]==' ' ){ j--; }
       123693  +  if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; }
       123694  +  z[j] = 0;
       123695  +  assert( j<nZ && "two" );
       123696  +  pVdbe->zNormSql = z;
       123697  +  sqlite3HashClear(&inHash);
       123698  +}
       123699  +#endif /* SQLITE_ENABLE_NORMALIZE */
       123700  +
122898 123701   /*
122899 123702   ** Rerun the compilation of a statement after a schema change.
122900 123703   **
122901 123704   ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
122902 123705   ** if the statement cannot be recompiled because another connection has
122903 123706   ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
122904 123707   ** occurs, return SQLITE_SCHEMA.
................................................................................
123903 124706     int i;
123904 124707     int nDefer = 0;
123905 124708     ExprList *pExtra = 0;
123906 124709     for(i=0; i<pEList->nExpr; i++){
123907 124710       struct ExprList_item *pItem = &pEList->a[i];
123908 124711       if( pItem->u.x.iOrderByCol==0 ){
123909 124712         Expr *pExpr = pItem->pExpr;
123910         -      Table *pTab = pExpr->pTab;
       124713  +      Table *pTab = pExpr->y.pTab;
123911 124714         if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
123912 124715          && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
123913 124716         ){
123914 124717           int j;
123915 124718           for(j=0; j<nDefer; j++){
123916 124719             if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
123917 124720           }
................................................................................
123926 124729                 pPk = sqlite3PrimaryKeyIndex(pTab);
123927 124730                 nKey = pPk->nKeyCol;
123928 124731               }
123929 124732               for(k=0; k<nKey; k++){
123930 124733                 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
123931 124734                 if( pNew ){
123932 124735                   pNew->iTable = pExpr->iTable;
123933         -                pNew->pTab = pExpr->pTab;
       124736  +                pNew->y.pTab = pExpr->y.pTab;
123934 124737                   pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
123935 124738                   pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
123936 124739                 }
123937 124740               }
123938         -            pSort->aDefer[nDefer].pTab = pExpr->pTab;
       124741  +            pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
123939 124742               pSort->aDefer[nDefer].iCsr = pExpr->iTable;
123940 124743               pSort->aDefer[nDefer].nKey = nKey;
123941 124744               nDefer++;
123942 124745             }
123943 124746           }
123944 124747           pItem->bSorterRef = 1;
123945 124748         }
................................................................................
124780 125583           ** This is not a problem, as the column type of "t1.col" is never
124781 125584           ** used. When columnType() is called on the expression 
124782 125585           ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
124783 125586           ** branch below.  */
124784 125587           break;
124785 125588         }
124786 125589   
124787         -      assert( pTab && pExpr->pTab==pTab );
       125590  +      assert( pTab && pExpr->y.pTab==pTab );
124788 125591         if( pS ){
124789 125592           /* The "table" is actually a sub-select or a view in the FROM clause
124790 125593           ** of the SELECT statement. Return the declaration type and origin
124791 125594           ** data for the result-set column of the sub-select.
124792 125595           */
124793 125596           if( iCol>=0 && iCol<pS->pEList->nExpr ){
124794 125597             /* If iCol is less than zero, then the expression requests the
................................................................................
124965 125768     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
124966 125769     sqlite3VdbeSetNumCols(v, pEList->nExpr);
124967 125770     for(i=0; i<pEList->nExpr; i++){
124968 125771       Expr *p = pEList->a[i].pExpr;
124969 125772   
124970 125773       assert( p!=0 );
124971 125774       assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
124972         -    assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
       125775  +    assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
124973 125776       if( pEList->a[i].zName ){
124974 125777         /* An AS clause always takes first priority */
124975 125778         char *zName = pEList->a[i].zName;
124976 125779         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
124977 125780       }else if( srcName && p->op==TK_COLUMN ){
124978 125781         char *zCol;
124979 125782         int iCol = p->iColumn;
124980         -      pTab = p->pTab;
       125783  +      pTab = p->y.pTab;
124981 125784         assert( pTab!=0 );
124982 125785         if( iCol<0 ) iCol = pTab->iPKey;
124983 125786         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
124984 125787         if( iCol<0 ){
124985 125788           zCol = "rowid";
124986 125789         }else{
124987 125790           zCol = pTab->aCol[iCol].zName;
................................................................................
125064 125867           pColExpr = pColExpr->pRight;
125065 125868           assert( pColExpr!=0 );
125066 125869         }
125067 125870         assert( pColExpr->op!=TK_AGG_COLUMN );
125068 125871         if( pColExpr->op==TK_COLUMN ){
125069 125872           /* For columns use the column name name */
125070 125873           int iCol = pColExpr->iColumn;
125071         -        Table *pTab = pColExpr->pTab;
       125874  +        Table *pTab = pColExpr->y.pTab;
125072 125875           assert( pTab!=0 );
125073 125876           if( iCol<0 ) iCol = pTab->iPKey;
125074 125877           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
125075 125878         }else if( pColExpr->op==TK_ID ){
125076 125879           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
125077 125880           zName = pColExpr->u.zToken;
125078 125881         }else{
................................................................................
125417 126220     int eDest = SRT_Fifo;         /* How to write to Queue */
125418 126221     SelectDest destQueue;         /* SelectDest targetting the Queue table */
125419 126222     int i;                        /* Loop counter */
125420 126223     int rc;                       /* Result code */
125421 126224     ExprList *pOrderBy;           /* The ORDER BY clause */
125422 126225     Expr *pLimit;                 /* Saved LIMIT and OFFSET */
125423 126226     int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
       126227  +
       126228  +#ifndef SQLITE_OMIT_WINDOWFUNC
       126229  +  if( p->pWin ){
       126230  +    sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
       126231  +    return;
       126232  +  }
       126233  +#endif
125424 126234   
125425 126235     /* Obtain authorization to do a recursive query */
125426 126236     if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
125427 126237   
125428 126238     /* Process the LIMIT and OFFSET clauses, if they exist */
125429 126239     addrBreak = sqlite3VdbeMakeLabel(v);
125430 126240     p->nSelectRow = 320;  /* 4 billion rows */
................................................................................
127167 127977   #endif
127168 127978   
127169 127979     return 1;
127170 127980   }
127171 127981   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
127172 127982   
127173 127983   /*
127174         -** A structure to keep track of all of the column values that fixed to
       127984  +** A structure to keep track of all of the column values that are fixed to
127175 127985   ** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
127176 127986   */
127177 127987   typedef struct WhereConst WhereConst;
127178 127988   struct WhereConst {
127179 127989     Parse *pParse;   /* Parsing context */
127180 127990     int nConst;      /* Number for COLUMN=CONSTANT terms */
127181 127991     int nChng;       /* Number of times a constant is propagated */
127182 127992     Expr **apExpr;   /* [i*2] is COLUMN and [i*2+1] is VALUE */
127183 127993   };
127184 127994   
127185 127995   /*
127186         -** Add a new entry to the pConst object
       127996  +** Add a new entry to the pConst object.  Except, do not add duplicate
       127997  +** pColumn entires.
127187 127998   */
127188 127999   static void constInsert(
127189         -  WhereConst *pConst,
127190         -  Expr *pColumn,
127191         -  Expr *pValue
       128000  +  WhereConst *pConst,      /* The WhereConst into which we are inserting */
       128001  +  Expr *pColumn,           /* The COLUMN part of the constraint */
       128002  +  Expr *pValue             /* The VALUE part of the constraint */
127192 128003   ){
       128004  +  int i;
       128005  +  assert( pColumn->op==TK_COLUMN );
       128006  +
       128007  +  /* 2018-10-25 ticket [cf5ed20f]
       128008  +  ** Make sure the same pColumn is not inserted more than once */
       128009  +  for(i=0; i<pConst->nConst; i++){
       128010  +    const Expr *pExpr = pConst->apExpr[i*2];
       128011  +    assert( pExpr->op==TK_COLUMN );
       128012  +    if( pExpr->iTable==pColumn->iTable
       128013  +     && pExpr->iColumn==pColumn->iColumn
       128014  +    ){
       128015  +      return;  /* Already present.  Return without doing anything. */
       128016  +    }
       128017  +  }
127193 128018   
127194 128019     pConst->nConst++;
127195 128020     pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
127196 128021                            pConst->nConst*2*sizeof(Expr*));
127197 128022     if( pConst->apExpr==0 ){
127198 128023       pConst->nConst = 0;
127199 128024     }else{
................................................................................
131166 131991     }
131167 131992   #ifndef SQLITE_OMIT_FLOATING_POINT
131168 131993     if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
131169 131994       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
131170 131995     }
131171 131996   #endif
131172 131997   }
       131998  +
       131999  +/*
       132000  +** Check to see if column iCol of index pIdx references any of the
       132001  +** columns defined by aXRef and chngRowid.  Return true if it does
       132002  +** and false if not.  This is an optimization.  False-positives are a
       132003  +** performance degradation, but false-negatives can result in a corrupt
       132004  +** index and incorrect answers.
       132005  +**
       132006  +** aXRef[j] will be non-negative if column j of the original table is
       132007  +** being updated.  chngRowid will be true if the rowid of the table is
       132008  +** being updated.
       132009  +*/
       132010  +static int indexColumnIsBeingUpdated(
       132011  +  Index *pIdx,      /* The index to check */
       132012  +  int iCol,         /* Which column of the index to check */
       132013  +  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
       132014  +  int chngRowid     /* true if the rowid is being updated */
       132015  +){
       132016  +  i16 iIdxCol = pIdx->aiColumn[iCol];
       132017  +  assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
       132018  +  if( iIdxCol>=0 ){
       132019  +    return aXRef[iIdxCol]>=0;
       132020  +  }
       132021  +  assert( iIdxCol==XN_EXPR );
       132022  +  assert( pIdx->aColExpr!=0 );
       132023  +  assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
       132024  +  return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
       132025  +                                            aXRef,chngRowid);
       132026  +}
       132027  +
       132028  +/*
       132029  +** Check to see if index pIdx is a partial index whose conditional
       132030  +** expression might change values due to an UPDATE.  Return true if
       132031  +** the index is subject to change and false if the index is guaranteed
       132032  +** to be unchanged.  This is an optimization.  False-positives are a
       132033  +** performance degradation, but false-negatives can result in a corrupt
       132034  +** index and incorrect answers.
       132035  +**
       132036  +** aXRef[j] will be non-negative if column j of the original table is
       132037  +** being updated.  chngRowid will be true if the rowid of the table is
       132038  +** being updated.
       132039  +*/
       132040  +static int indexWhereClauseMightChange(
       132041  +  Index *pIdx,      /* The index to check */
       132042  +  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
       132043  +  int chngRowid     /* true if the rowid is being updated */
       132044  +){
       132045  +  if( pIdx->pPartIdxWhere==0 ) return 0;
       132046  +  return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
       132047  +                                            aXRef, chngRowid);
       132048  +}
131173 132049   
131174 132050   /*
131175 132051   ** Process an UPDATE statement.
131176 132052   **
131177 132053   **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
131178 132054   **          \_______/ \________/     \______/       \________________/
131179 132055   *            onError   pTabList      pChanges             pWhere
................................................................................
131390 132266     pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
131391 132267   
131392 132268     hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
131393 132269   
131394 132270     /* There is one entry in the aRegIdx[] array for each index on the table
131395 132271     ** being updated.  Fill in aRegIdx[] with a register number that will hold
131396 132272     ** the key for accessing each index.
131397         -  **
131398         -  ** FIXME:  Be smarter about omitting indexes that use expressions.
131399 132273     */
131400 132274     for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
131401 132275       int reg;
131402         -    if( chngKey || hasFK>1 || pIdx->pPartIdxWhere || pIdx==pPk ){
       132276  +    if( chngKey || hasFK>1 || pIdx==pPk
       132277  +     || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
       132278  +    ){
131403 132279         reg = ++pParse->nMem;
131404 132280         pParse->nMem += pIdx->nColumn;
131405 132281       }else{
131406 132282         reg = 0;
131407 132283         for(i=0; i<pIdx->nKeyCol; i++){
131408         -        i16 iIdxCol = pIdx->aiColumn[i];
131409         -        if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
       132284  +        if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
131410 132285             reg = ++pParse->nMem;
131411 132286             pParse->nMem += pIdx->nColumn;
131412 132287             if( (onError==OE_Replace)
131413 132288              || (onError==OE_Default && pIdx->onError==OE_Replace) 
131414 132289             ){
131415 132290               bReplace = 1;
131416 132291             }
................................................................................
131951 132826   
131952 132827     /* Populate the argument registers. */
131953 132828     for(i=0; i<pTab->nCol; i++){
131954 132829       if( aXRef[i]>=0 ){
131955 132830         sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
131956 132831       }else{
131957 132832         sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
131958         -      sqlite3VdbeChangeP5(v, 1); /* Enable sqlite3_vtab_nochange() */
       132833  +      sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* Enable sqlite3_vtab_nochange() */
131959 132834       }
131960 132835     }
131961 132836     if( HasRowid(pTab) ){
131962 132837       sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
131963 132838       if( pRowid ){
131964 132839         sqlite3ExprCode(pParse, pRowid, regArg+1);
131965 132840       }else{
................................................................................
132452 133327     saved_flags = db->flags;
132453 133328     saved_mDbFlags = db->mDbFlags;
132454 133329     saved_nChange = db->nChange;
132455 133330     saved_nTotalChange = db->nTotalChange;
132456 133331     saved_mTrace = db->mTrace;
132457 133332     db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
132458 133333     db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
132459         -  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
       133334  +  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder
       133335  +                   | SQLITE_Defensive | SQLITE_CountRows);
132460 133336     db->mTrace = 0;
132461 133337   
132462 133338     zDbMain = db->aDb[iDb].zDbSName;
132463 133339     pMain = db->aDb[iDb].pBt;
132464 133340     isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
132465 133341   
132466 133342     /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
................................................................................
132994 133870   SQLITE_PRIVATE void sqlite3VtabBeginParse(
132995 133871     Parse *pParse,        /* Parsing context */
132996 133872     Token *pName1,        /* Name of new table, or database name */
132997 133873     Token *pName2,        /* Name of new table or NULL */
132998 133874     Token *pModuleName,   /* Name of the module for the virtual table */
132999 133875     int ifNotExists       /* No error if the table already exists */
133000 133876   ){
133001         -  int iDb;              /* The database the table is being created in */
133002 133877     Table *pTable;        /* The new virtual table */
133003 133878     sqlite3 *db;          /* Database connection */
133004 133879   
133005 133880     sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
133006 133881     pTable = pParse->pNewTable;
133007 133882     if( pTable==0 ) return;
133008 133883     assert( 0==pTable->pIndex );
133009 133884   
133010 133885     db = pParse->db;
133011         -  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
133012         -  assert( iDb>=0 );
133013 133886   
133014 133887     assert( pTable->nModuleArg==0 );
133015 133888     addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
133016 133889     addModuleArgument(db, pTable, 0);
133017 133890     addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
133018 133891     assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
133019 133892          || (pParse->sNameToken.z==pName1->z && pName2->z==0)
................................................................................
133025 133898   #ifndef SQLITE_OMIT_AUTHORIZATION
133026 133899     /* Creating a virtual table invokes the authorization callback twice.
133027 133900     ** The first invocation, to obtain permission to INSERT a row into the
133028 133901     ** sqlite_master table, has already been made by sqlite3StartTable().
133029 133902     ** The second call, to obtain permission to create the table, is made now.
133030 133903     */
133031 133904     if( pTable->azModuleArg ){
       133905  +    int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
       133906  +    assert( iDb>=0 ); /* The database the table is being created in */
133032 133907       sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
133033 133908               pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
133034 133909     }
133035 133910   #endif
133036 133911   }
133037 133912   
133038 133913   /*
................................................................................
133719 134594     void *pArg = 0;
133720 134595     FuncDef *pNew;
133721 134596     int rc = 0;
133722 134597   
133723 134598     /* Check to see the left operand is a column in a virtual table */
133724 134599     if( NEVER(pExpr==0) ) return pDef;
133725 134600     if( pExpr->op!=TK_COLUMN ) return pDef;
133726         -  pTab = pExpr->pTab;
       134601  +  pTab = pExpr->y.pTab;
133727 134602     if( pTab==0 ) return pDef;
133728 134603     if( !IsVirtual(pTab) ) return pDef;
133729 134604     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
133730 134605     assert( pVtab!=0 );
133731 134606     assert( pVtab->pModule!=0 );
133732 134607     pMod = (sqlite3_module *)pVtab->pModule;
133733 134608     if( pMod->xFindFunction==0 ) return pDef;
................................................................................
134339 135214     WhereLoop *pNew;          /* Template WhereLoop */
134340 135215     WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
134341 135216   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
134342 135217     UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
134343 135218     int nRecValid;            /* Number of valid fields currently in pRec */
134344 135219   #endif
134345 135220     unsigned int bldFlags;    /* SQLITE_BLDF_* flags */
       135221  +  unsigned int iPlanLimit;  /* Search limiter */
134346 135222   };
134347 135223   
134348 135224   /* Allowed values for WhereLoopBuider.bldFlags */
134349 135225   #define SQLITE_BLDF_INDEXED  0x0001   /* An index is used */
134350 135226   #define SQLITE_BLDF_UNIQUE   0x0002   /* All keys of a UNIQUE index used */
       135227  +
       135228  +/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
       135229  +** index+constraint combinations the query planner will consider for a
       135230  +** particular query.  If this parameter is unlimited, then certain
       135231  +** pathological queries can spend excess time in the sqlite3WhereBegin()
       135232  +** routine.  The limit is high enough that is should not impact real-world
       135233  +** queries.
       135234  +**
       135235  +** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit.  The limit is
       135236  +** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
       135237  +** clause is processed, so that every table in a join is guaranteed to be
       135238  +** able to propose a some index+constraint combinations even if the initial
       135239  +** baseline limit was exhausted by prior tables of the join.
       135240  +*/
       135241  +#ifndef SQLITE_QUERY_PLANNER_LIMIT
       135242  +# define SQLITE_QUERY_PLANNER_LIMIT 20000
       135243  +#endif
       135244  +#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
       135245  +# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
       135246  +#endif
134351 135247   
134352 135248   /*
134353 135249   ** The WHERE clause processing routine has two halves.  The
134354 135250   ** first part does the start of the WHERE loop and the second
134355 135251   ** half does the tail of the WHERE loop.  An instance of
134356 135252   ** this structure is returned by the first half and passed
134357 135253   ** into the second half to give some continuity.
................................................................................
134906 135802       ExprList *pLhs = 0;         /* New LHS after mods */
134907 135803       int i;                      /* Loop counter */
134908 135804       Select *pSelect;            /* Pointer to the SELECT on the RHS */
134909 135805   
134910 135806       for(i=iEq; i<pLoop->nLTerm; i++){
134911 135807         if( pLoop->aLTerm[i]->pExpr==pX ){
134912 135808           int iField = pLoop->aLTerm[i]->iField - 1;
134913         -        assert( pOrigRhs->a[iField].pExpr!=0 );
       135809  +        if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
134914 135810           pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
134915 135811           pOrigRhs->a[iField].pExpr = 0;
134916 135812           assert( pOrigLhs->a[iField].pExpr!=0 );
134917 135813           pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
134918 135814           pOrigLhs->a[iField].pExpr = 0;
134919 135815         }
134920 135816       }
................................................................................
135598 136494   */
135599 136495   static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
135600 136496     IdxExprTrans *pX = p->u.pIdxTrans;
135601 136497     if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
135602 136498       pExpr->op = TK_COLUMN;
135603 136499       pExpr->iTable = pX->iIdxCur;
135604 136500       pExpr->iColumn = pX->iIdxCol;
135605         -    pExpr->pTab = 0;
       136501  +    pExpr->y.pTab = 0;
135606 136502       return WRC_Prune;
135607 136503     }else{
135608 136504       return WRC_Continue;
135609 136505     }
135610 136506   }
135611 136507   
135612 136508   /*
................................................................................
136998 137894           */
136999 137895           if( sqlite3Isdigit(zNew[0])
137000 137896            || zNew[0]=='-'
137001 137897            || (zNew[0]+1=='0' && iTo==1)
137002 137898           ){
137003 137899             if( pLeft->op!=TK_COLUMN 
137004 137900              || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
137005         -           || IsVirtual(pLeft->pTab)  /* Value might be numeric */
       137901  +           || IsVirtual(pLeft->y.pTab)  /* Value might be numeric */
137006 137902             ){
137007 137903               sqlite3ExprDelete(db, pPrefix);
137008 137904               sqlite3ValueFree(pVal);
137009 137905               return 0;
137010 137906             }
137011 137907           }
137012 137908         }
................................................................................
137099 137995       ** virtual table on their second argument, which is the same as
137100 137996       ** the left-hand side operand in their in-fix form.
137101 137997       **
137102 137998       **       vtab_column MATCH expression
137103 137999       **       MATCH(expression,vtab_column)
137104 138000       */
137105 138001       pCol = pList->a[1].pExpr;
137106         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
       138002  +    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
137107 138003         for(i=0; i<ArraySize(aOp); i++){
137108 138004           if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
137109 138005             *peOp2 = aOp[i].eOp2;
137110 138006             *ppRight = pList->a[0].pExpr;
137111 138007             *ppLeft = pCol;
137112 138008             return 1;
137113 138009           }
................................................................................
137121 138017       **      OVERLOADED(vtab_column,expression)
137122 138018       **
137123 138019       ** Historically, xFindFunction expected to see lower-case function
137124 138020       ** names.  But for this use case, xFindFunction is expected to deal
137125 138021       ** with function names in an arbitrary case.
137126 138022       */
137127 138023       pCol = pList->a[0].pExpr;
137128         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
       138024  +    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
137129 138025         sqlite3_vtab *pVtab;
137130 138026         sqlite3_module *pMod;
137131 138027         void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
137132 138028         void *pNotUsed;
137133         -      pVtab = sqlite3GetVTable(db, pCol->pTab)->pVtab;
       138029  +      pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
137134 138030         assert( pVtab!=0 );
137135 138031         assert( pVtab->pModule!=0 );
137136 138032         pMod = (sqlite3_module *)pVtab->pModule;
137137 138033         if( pMod->xFindFunction!=0 ){
137138 138034           i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
137139 138035           if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
137140 138036             *peOp2 = i;
................................................................................
137144 138040           }
137145 138041         }
137146 138042       }
137147 138043     }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
137148 138044       int res = 0;
137149 138045       Expr *pLeft = pExpr->pLeft;
137150 138046       Expr *pRight = pExpr->pRight;
137151         -    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
       138047  +    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->y.pTab) ){
137152 138048         res++;
137153 138049       }
137154         -    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
       138050  +    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->y.pTab) ){
137155 138051         res++;
137156 138052         SWAP(Expr*, pLeft, pRight);
137157 138053       }
137158 138054       *ppLeft = pLeft;
137159 138055       *ppRight = pRight;
137160 138056       if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
137161 138057       if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
................................................................................
138099 138995     ** virtual term of that form.
138100 138996     **
138101 138997     ** Note that the virtual term must be tagged with TERM_VNULL.
138102 138998     */
138103 138999     if( pExpr->op==TK_NOTNULL
138104 139000      && pExpr->pLeft->op==TK_COLUMN
138105 139001      && pExpr->pLeft->iColumn>=0
       139002  +   && !ExprHasProperty(pExpr, EP_FromJoin)
138106 139003      && OptimizationEnabled(db, SQLITE_Stat34)
138107 139004     ){
138108 139005       Expr *pNewExpr;
138109 139006       Expr *pLeft = pExpr->pLeft;
138110 139007       int idxNew;
138111 139008       WhereTerm *pNewTerm;
138112 139009   
................................................................................
138290 139187     Expr *pTerm;
138291 139188     if( pItem->fg.isTabFunc==0 ) return;
138292 139189     pTab = pItem->pTab;
138293 139190     assert( pTab!=0 );
138294 139191     pArgs = pItem->u1.pFuncArg;
138295 139192     if( pArgs==0 ) return;
138296 139193     for(j=k=0; j<pArgs->nExpr; j++){
       139194  +    Expr *pRhs;
138297 139195       while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
138298 139196       if( k>=pTab->nCol ){
138299 139197         sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
138300 139198                         pTab->zName, j);
138301 139199         return;
138302 139200       }
138303 139201       pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
138304 139202       if( pColRef==0 ) return;
138305 139203       pColRef->iTable = pItem->iCursor;
138306 139204       pColRef->iColumn = k++;
138307         -    pColRef->pTab = pTab;
138308         -    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
138309         -                         sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
       139205  +    pColRef->y.pTab = pTab;
       139206  +    pRhs = sqlite3PExpr(pParse, TK_UPLUS, 
       139207  +        sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
       139208  +    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
138310 139209       whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
138311 139210     }
138312 139211   }
138313 139212   
138314 139213   /************** End of whereexpr.c *******************************************/
138315 139214   /************** Begin file where.c *******************************************/
138316 139215   /*
................................................................................
139165 140064     if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
139166 140065     if( pTabItem->fg.viaCoroutine ){
139167 140066       sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
139168 140067       testcase( pParse->db->mallocFailed );
139169 140068       translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
139170 140069                             pTabItem->regResult, 1);
139171 140070       sqlite3VdbeGoto(v, addrTop);
139172         -    pTabItem->fg.viaCoroutine = 0;
139173 140071     }else{
139174 140072       sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
139175 140073     }
139176 140074     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
139177 140075     sqlite3VdbeJumpHere(v, addrTop);
139178 140076     sqlite3ReleaseTempReg(pParse, regRecord);
139179 140077     
................................................................................
139343 140241   
139344 140242   /*
139345 140243   ** The table object reference passed as the second argument to this function
139346 140244   ** must represent a virtual table. This function invokes the xBestIndex()
139347 140245   ** method of the virtual table with the sqlite3_index_info object that
139348 140246   ** comes in as the 3rd argument to this function.
139349 140247   **
139350         -** If an error occurs, pParse is populated with an error message and a
139351         -** non-zero value is returned. Otherwise, 0 is returned and the output
139352         -** part of the sqlite3_index_info structure is left populated.
       140248  +** If an error occurs, pParse is populated with an error message and an
       140249  +** appropriate error code is returned.  A return of SQLITE_CONSTRAINT from
       140250  +** xBestIndex is not considered an error.  SQLITE_CONSTRAINT indicates that
       140251  +** the current configuration of "unusable" flags in sqlite3_index_info can
       140252  +** not result in a valid plan.
139353 140253   **
139354 140254   ** Whether or not an error is returned, it is the responsibility of the
139355 140255   ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
139356 140256   ** that this is required.
139357 140257   */
139358 140258   static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
139359 140259     sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
139360 140260     int rc;
139361 140261   
139362 140262     TRACE_IDX_INPUTS(p);
139363 140263     rc = pVtab->pModule->xBestIndex(pVtab, p);
139364 140264     TRACE_IDX_OUTPUTS(p);
139365 140265   
139366         -  if( rc!=SQLITE_OK ){
       140266  +  if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
139367 140267       if( rc==SQLITE_NOMEM ){
139368 140268         sqlite3OomFault(pParse->db);
139369 140269       }else if( !pVtab->zErrMsg ){
139370 140270         sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
139371 140271       }else{
139372 140272         sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
139373 140273       }
139374 140274     }
139375 140275     sqlite3_free(pVtab->zErrMsg);
139376 140276     pVtab->zErrMsg = 0;
139377         -
139378         -#if 0
139379         -  /* This error is now caught by the caller.
139380         -  ** Search for "xBestIndex malfunction" below */
139381         -  for(i=0; i<p->nConstraint; i++){
139382         -    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
139383         -      sqlite3ErrorMsg(pParse, 
139384         -          "table %s: xBestIndex returned an invalid plan", pTab->zName);
139385         -    }
139386         -  }
139387         -#endif
139388         -
139389         -  return pParse->nErr;
       140277  +  return rc;
139390 140278   }
139391 140279   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
139392 140280   
139393 140281   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139394 140282   /*
139395 140283   ** Estimate the location of a particular key among all keys in an
139396 140284   ** index.  Store the results in aStat as follows:
................................................................................
140436 141324   **    (4)  The template has the same or lower cost than the current loop
140437 141325   */
140438 141326   static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
140439 141327     WhereLoop **ppPrev, *p;
140440 141328     WhereInfo *pWInfo = pBuilder->pWInfo;
140441 141329     sqlite3 *db = pWInfo->pParse->db;
140442 141330     int rc;
       141331  +
       141332  +  /* Stop the search once we hit the query planner search limit */
       141333  +  if( pBuilder->iPlanLimit==0 ){
       141334  +    WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
       141335  +    if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
       141336  +    return SQLITE_DONE;
       141337  +  }
       141338  +  pBuilder->iPlanLimit--;
140443 141339   
140444 141340     /* If pBuilder->pOrSet is defined, then only keep track of the costs
140445 141341     ** and prereqs.
140446 141342     */
140447 141343     if( pBuilder->pOrSet!=0 ){
140448 141344       if( pTemplate->nLTerm ){
140449 141345   #if WHERETRACE_ENABLED
................................................................................
141447 142343     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
141448 142344     pIdxInfo->estimatedRows = 25;
141449 142345     pIdxInfo->idxFlags = 0;
141450 142346     pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
141451 142347   
141452 142348     /* Invoke the virtual table xBestIndex() method */
141453 142349     rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
141454         -  if( rc ) return rc;
       142350  +  if( rc ){
       142351  +    if( rc==SQLITE_CONSTRAINT ){
       142352  +      /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
       142353  +      ** that the particular combination of parameters provided is unusable.
       142354  +      ** Make no entries in the loop table.
       142355  +      */
       142356  +      WHERETRACE(0xffff, ("  ^^^^--- non-viable plan rejected!\n"));
       142357  +      return SQLITE_OK;
       142358  +    }
       142359  +    return rc;
       142360  +  }
141455 142361   
141456 142362     mxTerm = -1;
141457 142363     assert( pNew->nLSlot>=nConstraint );
141458 142364     for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
141459 142365     pNew->u.vtab.omitMask = 0;
141460 142366     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
141461 142367     for(i=0; i<nConstraint; i++, pIdxCons++){
................................................................................
141843 142749     int rc = SQLITE_OK;
141844 142750     WhereLoop *pNew;
141845 142751     u8 priorJointype = 0;
141846 142752   
141847 142753     /* Loop over the tables in the join, from left to right */
141848 142754     pNew = pBuilder->pNew;
141849 142755     whereLoopInit(pNew);
       142756  +  pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
141850 142757     for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
141851 142758       Bitmask mUnusable = 0;
141852 142759       pNew->iTab = iTab;
       142760  +    pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
141853 142761       pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
141854 142762       if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
141855 142763         /* This condition is true when pItem is the FROM clause term on the
141856 142764         ** right-hand-side of a LEFT or CROSS JOIN.  */
141857 142765         mPrereq = mPrior;
141858 142766       }
141859 142767       priorJointype = pItem->fg.jointype;
................................................................................
141871 142779       {
141872 142780         rc = whereLoopAddBtree(pBuilder, mPrereq);
141873 142781       }
141874 142782       if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
141875 142783         rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
141876 142784       }
141877 142785       mPrior |= pNew->maskSelf;
141878         -    if( rc || db->mallocFailed ) break;
       142786  +    if( rc || db->mallocFailed ){
       142787  +      if( rc==SQLITE_DONE ){
       142788  +        /* We hit the query planner search limit set by iPlanLimit */
       142789  +        sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search");
       142790  +        rc = SQLITE_OK;
       142791  +      }else{
       142792  +        break;
       142793  +      }
       142794  +    }
141879 142795     }
141880 142796   
141881 142797     whereLoopClear(db, pNew);
141882 142798     return rc;
141883 142799   }
141884 142800   
141885 142801   /*
................................................................................
144253 145169         if( i==nSrc ) return WRC_Continue;
144254 145170       }
144255 145171     }
144256 145172   
144257 145173     switch( pExpr->op ){
144258 145174   
144259 145175       case TK_FUNCTION:
144260         -      if( pExpr->pWin==0 ){
       145176  +      if( !ExprHasProperty(pExpr, EP_WinFunc) ){
144261 145177           break;
144262 145178         }else{
144263 145179           Window *pWin;
144264 145180           for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
144265         -          if( pExpr->pWin==pWin ){
       145181  +          if( pExpr->y.pWin==pWin ){
144266 145182               assert( pWin->pOwner==pExpr );
144267 145183               return WRC_Prune;
144268 145184             }
144269 145185           }
144270 145186         }
144271 145187         /* Fall through.  */
144272 145188   
................................................................................
144375 145291   ** any SQL window functions, this function is a no-op. Otherwise, it 
144376 145292   ** rewrites the SELECT statement so that window function xStep functions
144377 145293   ** are invoked in the correct order as described under "SELECT REWRITING"
144378 145294   ** at the top of this file.
144379 145295   */
144380 145296   SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
144381 145297     int rc = SQLITE_OK;
144382         -  if( p->pWin ){
       145298  +  if( p->pWin && p->pPrior==0 ){
144383 145299       Vdbe *v = sqlite3GetVdbe(pParse);
144384 145300       sqlite3 *db = pParse->db;
144385 145301       Select *pSub = 0;             /* The subquery */
144386 145302       SrcList *pSrc = p->pSrc;
144387 145303       Expr *pWhere = p->pWhere;
144388 145304       ExprList *pGroupBy = p->pGroupBy;
144389 145305       Expr *pHaving = p->pHaving;
................................................................................
144588 145504   }
144589 145505   
144590 145506   /*
144591 145507   ** Attach window object pWin to expression p.
144592 145508   */
144593 145509   SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
144594 145510     if( p ){
       145511  +    assert( p->op==TK_FUNCTION );
144595 145512       /* This routine is only called for the parser.  If pWin was not
144596 145513       ** allocated due to an OOM, then the parser would fail before ever
144597 145514       ** invoking this routine */
144598 145515       if( ALWAYS(pWin) ){
144599         -      p->pWin = pWin;
       145516  +      p->y.pWin = pWin;
       145517  +      ExprSetProperty(p, EP_WinFunc);
144600 145518         pWin->pOwner = p;
144601 145519         if( p->flags & EP_Distinct ){
144602 145520           sqlite3ErrorMsg(pParse,
144603 145521              "DISTINCT is not supported for window functions");
144604 145522         }
144605 145523       }
144606 145524     }else{
................................................................................
145755 146673   /*
145756 146674   ** Allocate and return a duplicate of the Window object indicated by the
145757 146675   ** third argument. Set the Window.pOwner field of the new object to
145758 146676   ** pOwner.
145759 146677   */
145760 146678   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
145761 146679     Window *pNew = 0;
145762         -  if( p ){
       146680  +  if( ALWAYS(p) ){
145763 146681       pNew = sqlite3DbMallocZero(db, sizeof(Window));
145764 146682       if( pNew ){
145765 146683         pNew->zName = sqlite3DbStrDup(db, p->zName);
145766 146684         pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
145767 146685         pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
145768 146686         pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
145769 146687         pNew->eType = p->eType;
................................................................................
145907 146825   ** of this template is copied straight through into the generate parser
145908 146826   ** source file.
145909 146827   **
145910 146828   ** The following is the concatenation of all %include directives from the
145911 146829   ** input grammar file:
145912 146830   */
145913 146831   /* #include <stdio.h> */
       146832  +/* #include <assert.h> */
145914 146833   /************ Begin %include sections from the grammar ************************/
145915 146834   
145916 146835   /* #include "sqliteInt.h" */
145917 146836   
145918 146837   /*
145919 146838   ** Disable all error recovery processing in the parser push-down
145920 146839   ** automaton.
................................................................................
146008 146927         p->op = (u8)op;
146009 146928         p->affinity = 0;
146010 146929         p->flags = EP_Leaf;
146011 146930         p->iAgg = -1;
146012 146931         p->pLeft = p->pRight = 0;
146013 146932         p->x.pList = 0;
146014 146933         p->pAggInfo = 0;
146015         -      p->pTab = 0;
       146934  +      p->y.pTab = 0;
146016 146935         p->op2 = 0;
146017 146936         p->iTable = 0;
146018 146937         p->iColumn = 0;
146019         -#ifndef SQLITE_OMIT_WINDOWFUNC
146020         -      p->pWin = 0;
146021         -#endif
146022 146938         p->u.zToken = (char*)&p[1];
146023 146939         memcpy(p->u.zToken, t.z, t.n);
146024 146940         p->u.zToken[t.n] = 0;
146025 146941         if( sqlite3Isquote(p->u.zToken[0]) ){
146026 146942           if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
146027 146943           sqlite3Dequote(p->u.zToken);
146028 146944         }
................................................................................
150206 151122                yyTracePrompt,yyTokenName[yymajor]);
150207 151123           }
150208 151124   #endif
150209 151125           yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
150210 151126           yymajor = YYNOCODE;
150211 151127         }else{
150212 151128           while( yypParser->yytos >= yypParser->yystack
150213         -            && yymx != YYERRORSYMBOL
150214 151129               && (yyact = yy_find_reduce_action(
150215 151130                           yypParser->yytos->stateno,
150216         -                        YYERRORSYMBOL)) >= YY_MIN_REDUCE
       151131  +                        YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE
150217 151132           ){
150218 151133             yy_pop_parser_stack(yypParser);
150219 151134           }
150220 151135           if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
150221 151136             yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
150222 151137             yy_parse_failed(yypParser);
150223 151138   #ifndef YYNOERRORRECOVERY
................................................................................
151176 152091       }
151177 152092     }
151178 152093     while( IdChar(z[i]) ){ i++; }
151179 152094     *tokenType = TK_ID;
151180 152095     return i;
151181 152096   }
151182 152097   
       152098  +#ifdef SQLITE_ENABLE_NORMALIZE
       152099  +/*
       152100  +** Return the length (in bytes) of the token that begins at z[0].
       152101  +** Store the token type in *tokenType before returning.  If flags has
       152102  +** SQLITE_TOKEN_NORMALIZE flag enabled, use the identifier token type
       152103  +** for keywords.  Add SQLITE_TOKEN_QUOTED to flags if the token was
       152104  +** actually a quoted identifier.  Add SQLITE_TOKEN_KEYWORD to flags
       152105  +** if the token was recognized as a keyword; this is useful when the
       152106  +** SQLITE_TOKEN_NORMALIZE flag is used, because it enables the caller
       152107  +** to differentiate between a keyword being treated as an identifier
       152108  +** (for normalization purposes) and an actual identifier.
       152109  +*/
       152110  +SQLITE_PRIVATE int sqlite3GetTokenNormalized(
       152111  +  const unsigned char *z,
       152112  +  int *tokenType,
       152113  +  int *flags
       152114  +){
       152115  +  int n;
       152116  +  unsigned char iClass = aiClass[*z];
       152117  +  if( iClass==CC_KYWD ){
       152118  +    int i;
       152119  +    for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
       152120  +    if( IdChar(z[i]) ){
       152121  +      /* This token started out using characters that can appear in keywords,
       152122  +      ** but z[i] is a character not allowed within keywords, so this must
       152123  +      ** be an identifier instead */
       152124  +      i++;
       152125  +      while( IdChar(z[i]) ){ i++; }
       152126  +      *tokenType = TK_ID;
       152127  +      return i;
       152128  +    }
       152129  +    *tokenType = TK_ID;
       152130  +    n = keywordCode((char*)z, i, tokenType);
       152131  +    /* If the token is no longer considered to be an identifier, then it is a
       152132  +    ** keyword of some kind.  Make the token back into an identifier and then
       152133  +    ** set the SQLITE_TOKEN_KEYWORD flag.  Several non-identifier tokens are
       152134  +    ** used verbatim, including IN, IS, NOT, and NULL. */
       152135  +    switch( *tokenType ){
       152136  +      case TK_ID: {
       152137  +        /* do nothing, handled by caller */
       152138  +        break;
       152139  +      }
       152140  +      case TK_IN:
       152141  +      case TK_IS:
       152142  +      case TK_NOT:
       152143  +      case TK_NULL: {
       152144  +        *flags |= SQLITE_TOKEN_KEYWORD;
       152145  +        break;
       152146  +      }
       152147  +      default: {
       152148  +        *tokenType = TK_ID;
       152149  +        *flags |= SQLITE_TOKEN_KEYWORD;
       152150  +        break;
       152151  +      }
       152152  +    }
       152153  +  }else{
       152154  +    n = sqlite3GetToken(z, tokenType);
       152155  +    /* If the token is considered to be an identifier and the character class
       152156  +    ** of the first character is a quote, set the SQLITE_TOKEN_QUOTED flag. */
       152157  +    if( *tokenType==TK_ID && (iClass==CC_QUOTE || iClass==CC_QUOTE2) ){
       152158  +      *flags |= SQLITE_TOKEN_QUOTED;
       152159  +    }
       152160  +  }
       152161  +  return n;
       152162  +}
       152163  +#endif /* SQLITE_ENABLE_NORMALIZE */
       152164  +
151183 152165   /*
151184 152166   ** Run the parser on the given SQL string.  The parser structure is
151185 152167   ** passed in.  An SQLITE_ status code is returned.  If an error occurs
151186 152168   ** then an and attempt is made to write an error message into 
151187 152169   ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
151188 152170   ** error message.
151189 152171   */
................................................................................
152573 153555           { SQLITE_DBCONFIG_ENABLE_TRIGGER,        SQLITE_EnableTrigger  },
152574 153556           { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer  },
152575 153557           { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension  },
152576 153558           { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,      SQLITE_NoCkptOnClose  },
152577 153559           { SQLITE_DBCONFIG_ENABLE_QPSG,           SQLITE_EnableQPSG     },
152578 153560           { SQLITE_DBCONFIG_TRIGGER_EQP,           SQLITE_TriggerEQP     },
152579 153561           { SQLITE_DBCONFIG_RESET_DATABASE,        SQLITE_ResetDatabase  },
       153562  +        { SQLITE_DBCONFIG_DEFENSIVE,             SQLITE_Defensive      },
152580 153563         };
152581 153564         unsigned int i;
152582 153565         rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
152583 153566         for(i=0; i<ArraySize(aFlagOp); i++){
152584 153567           if( aFlagOp[i].op==op ){
152585 153568             int onoff = va_arg(ap, int);
152586 153569             int *pRes = va_arg(ap, int*);
................................................................................
154759 155742       }
154760 155743     }
154761 155744     sqlite3_mutex_enter(db->mutex);
154762 155745     db->errMask = 0xff;
154763 155746     db->nDb = 2;
154764 155747     db->magic = SQLITE_MAGIC_BUSY;
154765 155748     db->aDb = db->aDbStatic;
       155749  +  db->lookaside.bDisable = 1;
154766 155750   
154767 155751     assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
154768 155752     memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
154769 155753     db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
154770 155754     db->autoCommit = 1;
154771 155755     db->nextAutovac = -1;
154772 155756     db->szMmap = sqlite3GlobalConfig.szMmap;
................................................................................
154798 155782                    | SQLITE_CellSizeCk
154799 155783   #endif
154800 155784   #if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
154801 155785                    | SQLITE_Fts3Tokenizer
154802 155786   #endif
154803 155787   #if defined(SQLITE_ENABLE_QPSG)
154804 155788                    | SQLITE_EnableQPSG
       155789  +#endif
       155790  +#if defined(SQLITE_DEFAULT_DEFENSIVE)
       155791  +                 | SQLITE_Defensive
154805 155792   #endif
154806 155793         ;
154807 155794     sqlite3HashInit(&db->aCollSeq);
154808 155795   #ifndef SQLITE_OMIT_VIRTUALTABLE
154809 155796     sqlite3HashInit(&db->aModule);
154810 155797   #endif
154811 155798   
................................................................................
155686 156673         sqlite3 *db = va_arg(ap, sqlite3*);
155687 156674         db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
155688 156675         break;
155689 156676       }
155690 156677   
155691 156678       /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
155692 156679       **
155693         -    ** If parameter onoff is non-zero, configure the wrappers so that all
155694         -    ** subsequent calls to localtime() and variants fail. If onoff is zero,
155695         -    ** undo this setting.
       156680  +    ** If parameter onoff is non-zero, subsequent calls to localtime()
       156681  +    ** and its variants fail. If onoff is zero, undo this setting.
155696 156682       */
155697 156683       case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
155698 156684         sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
155699 156685         break;
155700 156686       }
       156687  +
       156688  +    /*   sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCS, int onoff);
       156689  +    **
       156690  +    ** If parameter onoff is non-zero, internal-use-only SQL functions
       156691  +    ** are visible to ordinary SQL.  This is useful for testing but is
       156692  +    ** unsafe because invalid parameters to those internal-use-only functions
       156693  +    ** can result in crashes or segfaults.
       156694  +    */
       156695  +    case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
       156696  +      sqlite3GlobalConfig.bInternalFunctions = va_arg(ap, int);
       156697  +      break;
       156698  +    }
155701 156699   
155702 156700       /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
155703 156701       **
155704 156702       ** Set or clear a flag that indicates that the database file is always well-
155705 156703       ** formed and never corrupt.  This flag is clear by default, indicating that
155706 156704       ** database files might have arbitrary corruption.  Setting the flag during
155707 156705       ** testing causes certain assert() statements in the code to be activated
................................................................................
159137 160135     sqlite3_int64 *piFirst,         /* OUT: Selected child node */
159138 160136     sqlite3_int64 *piLast           /* OUT: Selected child node */
159139 160137   ){
159140 160138     int rc = SQLITE_OK;             /* Return code */
159141 160139     const char *zCsr = zNode;       /* Cursor to iterate through node */
159142 160140     const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
159143 160141     char *zBuffer = 0;              /* Buffer to load terms into */
159144         -  int nAlloc = 0;                 /* Size of allocated buffer */
       160142  +  i64 nAlloc = 0;                 /* Size of allocated buffer */
159145 160143     int isFirstTerm = 1;            /* True when processing first term on page */
159146 160144     sqlite3_int64 iChild;           /* Block id of child node to descend to */
159147 160145   
159148 160146     /* Skip over the 'height' varint that occurs at the start of every 
159149 160147     ** interior node. Then load the blockid of the left-child of the b-tree
159150 160148     ** node into variable iChild.  
159151 160149     **
................................................................................
159175 160173       if( !isFirstTerm ){
159176 160174         zCsr += fts3GetVarint32(zCsr, &nPrefix);
159177 160175       }
159178 160176       isFirstTerm = 0;
159179 160177       zCsr += fts3GetVarint32(zCsr, &nSuffix);
159180 160178       
159181 160179       assert( nPrefix>=0 && nSuffix>=0 );
159182         -    if( &zCsr[nSuffix]>zEnd ){
       160180  +    if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr ){
159183 160181         rc = FTS_CORRUPT_VTAB;
159184 160182         goto finish_scan;
159185 160183       }
159186         -    if( nPrefix+nSuffix>nAlloc ){
       160184  +    if( (i64)nPrefix+nSuffix>nAlloc ){
159187 160185         char *zNew;
159188         -      nAlloc = (nPrefix+nSuffix) * 2;
159189         -      zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
       160186  +      nAlloc = ((i64)nPrefix+nSuffix) * 2;
       160187  +      zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
159190 160188         if( !zNew ){
159191 160189           rc = SQLITE_NOMEM;
159192 160190           goto finish_scan;
159193 160191         }
159194 160192         zBuffer = zNew;
159195 160193       }
159196 160194       assert( zBuffer );
................................................................................
161161 162159     UNUSED_PARAMETER(iSavepoint);
161162 162160     assert( p->inTransaction );
161163 162161     assert( p->mxSavepoint >= iSavepoint );
161164 162162     TESTONLY( p->mxSavepoint = iSavepoint );
161165 162163     sqlite3Fts3PendingTermsClear(p);
161166 162164     return SQLITE_OK;
161167 162165   }
       162166  +
       162167  +/*
       162168  +** Return true if zName is the extension on one of the shadow tables used
       162169  +** by this module.
       162170  +*/
       162171  +static int fts3ShadowName(const char *zName){
       162172  +  static const char *azName[] = {
       162173  +    "content", "docsize", "segdir", "segments", "stat", 
       162174  +  };
       162175  +  unsigned int i;
       162176  +  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
       162177  +    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
       162178  +  }
       162179  +  return 0;
       162180  +}
161168 162181   
161169 162182   static const sqlite3_module fts3Module = {
161170         -  /* iVersion      */ 2,
       162183  +  /* iVersion      */ 3,
161171 162184     /* xCreate       */ fts3CreateMethod,
161172 162185     /* xConnect      */ fts3ConnectMethod,
161173 162186     /* xBestIndex    */ fts3BestIndexMethod,
161174 162187     /* xDisconnect   */ fts3DisconnectMethod,
161175 162188     /* xDestroy      */ fts3DestroyMethod,
161176 162189     /* xOpen         */ fts3OpenMethod,
161177 162190     /* xClose        */ fts3CloseMethod,
................................................................................
161186 162199     /* xCommit       */ fts3CommitMethod,
161187 162200     /* xRollback     */ fts3RollbackMethod,
161188 162201     /* xFindFunction */ fts3FindFunctionMethod,
161189 162202     /* xRename */       fts3RenameMethod,
161190 162203     /* xSavepoint    */ fts3SavepointMethod,
161191 162204     /* xRelease      */ fts3ReleaseMethod,
161192 162205     /* xRollbackTo   */ fts3RollbackToMethod,
       162206  +  /* xShadowName   */ fts3ShadowName,
161193 162207   };
161194 162208   
161195 162209   /*
161196 162210   ** This function is registered as the module destructor (called when an
161197 162211   ** FTS3 enabled database connection is closed). It frees the memory
161198 162212   ** allocated for the tokenizer hash table.
161199 162213   */
................................................................................
161466 162480       }
161467 162481       assert( pToken->pSegcsr==0 );
161468 162482     }
161469 162483   
161470 162484     return rc;
161471 162485   }
161472 162486   
       162487  +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
161473 162488   /*
161474 162489   ** This function is called on each phrase after the position lists for
161475 162490   ** any deferred tokens have been loaded into memory. It updates the phrases
161476 162491   ** current position list to include only those positions that are really
161477 162492   ** instances of the phrase (after considering deferred tokens). If this
161478 162493   ** means that the phrase does not appear in the current row, doclist.pList
161479 162494   ** and doclist.nList are both zeroed.
................................................................................
161569 162584         }
161570 162585         sqlite3_free(aPoslist);
161571 162586       }
161572 162587     }
161573 162588   
161574 162589     return SQLITE_OK;
161575 162590   }
       162591  +#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
161576 162592   
161577 162593   /*
161578 162594   ** Maximum number of tokens a phrase may have to be considered for the
161579 162595   ** incremental doclists strategy.
161580 162596   */
161581 162597   #define MAX_INCR_PHRASE_TOKENS 4
161582 162598   
................................................................................
163817 164833        0,                           /* xSync         */
163818 164834        0,                           /* xCommit       */
163819 164835        0,                           /* xRollback     */
163820 164836        0,                           /* xFindFunction */
163821 164837        0,                           /* xRename       */
163822 164838        0,                           /* xSavepoint    */
163823 164839        0,                           /* xRelease      */
163824         -     0                            /* xRollbackTo   */
       164840  +     0,                           /* xRollbackTo   */
       164841  +     0                            /* xShadowName   */
163825 164842     };
163826 164843     int rc;                         /* Return code */
163827 164844   
163828 164845     rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
163829 164846     return rc;
163830 164847   }
163831 164848   
................................................................................
167376 168393        0,                           /* xSync         */
167377 168394        0,                           /* xCommit       */
167378 168395        0,                           /* xRollback     */
167379 168396        0,                           /* xFindFunction */
167380 168397        0,                           /* xRename       */
167381 168398        0,                           /* xSavepoint    */
167382 168399        0,                           /* xRelease      */
167383         -     0                            /* xRollbackTo   */
       168400  +     0,                           /* xRollbackTo   */
       168401  +     0                            /* xShadowName   */
167384 168402     };
167385 168403     int rc;                         /* Return code */
167386 168404   
167387 168405     rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
167388 168406     return rc;
167389 168407   }
167390 168408   
................................................................................
168764 169782     rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
168765 169783     if( rc!=SQLITE_OK ) return rc;
168766 169784     
168767 169785     /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
168768 169786     ** safe (no risk of overread) even if the node data is corrupted. */
168769 169787     pNext += fts3GetVarint32(pNext, &nPrefix);
168770 169788     pNext += fts3GetVarint32(pNext, &nSuffix);
168771         -  if( nPrefix<0 || nSuffix<=0 
168772         -   || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
       169789  +  if( nSuffix<=0 
       169790  +   || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
       169791  +   || nPrefix>pReader->nTermAlloc
168773 169792     ){
168774 169793       return FTS_CORRUPT_VTAB;