System.Data.SQLite
Check-in [d145c7f890]
Not logged in

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

Overview
Comment:Update SQLite core library to include the RTree error message changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | tkt-d1fb769a8e
Files: files | file ages | folders
SHA1: d145c7f8900fd4b777c31ba61f95951e0f8b731e
User & Date: mistachkin 2013-01-17 04:59:04
Context
2013-01-17
04:59
Update SQLite core library to include the RTree error message changes. Closed-Leaf check-in: d145c7f890 user: mistachkin tags: tkt-d1fb769a8e
01:47
Add initial draft of test for ticket [d1fb769a8e]. check-in: 4e9be1fcdb user: mistachkin tags: tkt-d1fb769a8e
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/props/sqlite3.props.

     5      5    *
     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    11     11     <PropertyGroup Label="UserMacros">
    12         -    <SQLITE_MANIFEST_VERSION>3.7.15.2</SQLITE_MANIFEST_VERSION>
    13         -    <SQLITE_RC_VERSION>3,7,15,2</SQLITE_RC_VERSION>
           12  +    <SQLITE_MANIFEST_VERSION>3.7.16</SQLITE_MANIFEST_VERSION>
           13  +    <SQLITE_RC_VERSION>3,7,16</SQLITE_RC_VERSION>
    14     14       <SQLITE_COMMON_DEFINES>_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_CRT_NONSTDC_NO_WARNINGS;SQLITE_THREADSAFE=1;SQLITE_USE_URI=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT3=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1</SQLITE_COMMON_DEFINES>
    15     15       <SQLITE_EXTRA_DEFINES>SQLITE_HAS_CODEC=1</SQLITE_EXTRA_DEFINES>
    16     16       <SQLITE_WINCE_DEFINES>SQLITE_OMIT_WAL=1</SQLITE_WINCE_DEFINES>
    17     17       <SQLITE_DEBUG_DEFINES>SQLITE_DEBUG=1;SQLITE_MEMDEBUG=1;SQLITE_ENABLE_EXPENSIVE_ASSERT=1</SQLITE_DEBUG_DEFINES>
    18     18       <SQLITE_RELEASE_DEFINES>SQLITE_WIN32_MALLOC=1</SQLITE_RELEASE_DEFINES>
    19     19       <SQLITE_DISABLE_WARNINGS>4055;4100;4127;4146;4210;4232;4244;4245;4267;4306;4389;4701;4703;4706</SQLITE_DISABLE_WARNINGS>
    20     20       <SQLITE_DISABLE_X64_WARNINGS></SQLITE_DISABLE_X64_WARNINGS>

Changes to SQLite.Interop/props/sqlite3.vsprops.

    10     10   <VisualStudioPropertySheet
    11     11   	ProjectType="Visual C++"
    12     12   	Version="8.00"
    13     13   	Name="sqlite3"
    14     14   	>
    15     15   	<UserMacro
    16     16   		Name="SQLITE_MANIFEST_VERSION"
    17         -		Value="3.7.15.2"
           17  +		Value="3.7.16"
    18     18   		PerformEnvironmentSet="true"
    19     19   	/>
    20     20   	<UserMacro
    21     21   		Name="SQLITE_RC_VERSION"
    22         -		Value="3,7,15,2"
           22  +		Value="3,7,16"
    23     23   		PerformEnvironmentSet="true"
    24     24   	/>
    25     25   	<UserMacro
    26     26   		Name="SQLITE_COMMON_DEFINES"
    27     27   		Value="_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_CRT_NONSTDC_NO_WARNINGS;SQLITE_THREADSAFE=1;SQLITE_USE_URI=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT3=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1"
    28     28   		PerformEnvironmentSet="true"
    29     29   	/>

Changes to SQLite.Interop/src/core/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.7.15.2.  By combining all the individual C code files into this 
            3  +** version 3.7.16.  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
................................................................................
   669    669   ** string contains the date and time of the check-in (UTC) and an SHA1
   670    670   ** hash of the entire source tree.
   671    671   **
   672    672   ** See also: [sqlite3_libversion()],
   673    673   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   674    674   ** [sqlite_version()] and [sqlite_source_id()].
   675    675   */
   676         -#define SQLITE_VERSION        "3.7.15.2"
   677         -#define SQLITE_VERSION_NUMBER 3007015
   678         -#define SQLITE_SOURCE_ID      "2013-01-09 11:53:05 c0e09560d26f0a6456be9dd3447f5311eb4f238f"
          676  +#define SQLITE_VERSION        "3.7.16"
          677  +#define SQLITE_VERSION_NUMBER 3007016
          678  +#define SQLITE_SOURCE_ID      "2013-01-17 03:18:38 652233d646236d3fbca629813b20d075f00f3ed6"
   679    679   
   680    680   /*
   681    681   ** CAPI3REF: Run-Time Library Version Numbers
   682    682   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   683    683   **
   684    684   ** These interfaces provide the same information as the [SQLITE_VERSION],
   685    685   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
 10551  10551   ** must be unique and what to do if they are not.  When Index.onError=OE_None,
 10552  10552   ** it means this is not a unique index.  Otherwise it is a unique index
 10553  10553   ** and the value of Index.onError indicate the which conflict resolution 
 10554  10554   ** algorithm to employ whenever an attempt is made to insert a non-unique
 10555  10555   ** element.
 10556  10556   */
 10557  10557   struct Index {
 10558         -  char *zName;     /* Name of this index */
 10559         -  int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
 10560         -  tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
 10561         -  Table *pTable;   /* The SQL table being indexed */
 10562         -  char *zColAff;   /* String defining the affinity of each column */
 10563         -  Index *pNext;    /* The next index associated with the same table */
 10564         -  Schema *pSchema; /* Schema containing this index */
 10565         -  u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
 10566         -  char **azColl;   /* Array of collation sequence names for index */
 10567         -  int nColumn;     /* Number of columns in the table used by this index */
 10568         -  int tnum;        /* Page containing root of this index in database file */
 10569         -  u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 10570         -  u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
 10571         -  u8 bUnordered;   /* Use this index for == or IN queries only */
        10558  +  char *zName;             /* Name of this index */
        10559  +  int *aiColumn;           /* Which columns are used by this index.  1st is 0 */
        10560  +  tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
        10561  +  Table *pTable;           /* The SQL table being indexed */
        10562  +  char *zColAff;           /* String defining the affinity of each column */
        10563  +  Index *pNext;            /* The next index associated with the same table */
        10564  +  Schema *pSchema;         /* Schema containing this index */
        10565  +  u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
        10566  +  char **azColl;           /* Array of collation sequence names for index */
        10567  +  int tnum;                /* DB Page containing root of this index */
        10568  +  u16 nColumn;             /* Number of columns in table used by this index */
        10569  +  u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
        10570  +  unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
        10571  +  unsigned bUnordered:1;   /* Use this index for == or IN queries only */
 10572  10572   #ifdef SQLITE_ENABLE_STAT3
 10573  10573     int nSample;             /* Number of elements in aSample[] */
 10574  10574     tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
 10575  10575     IndexSample *aSample;    /* Samples of the left-most key */
 10576  10576   #endif
 10577  10577   };
 10578  10578   
................................................................................
 10838  10838   /*
 10839  10839   ** A list of expressions.  Each expression may optionally have a
 10840  10840   ** name.  An expr/name combination can be used in several ways, such
 10841  10841   ** as the list of "expr AS ID" fields following a "SELECT" or in the
 10842  10842   ** list of "ID = expr" items in an UPDATE.  A list of expressions can
 10843  10843   ** also be used as the argument to a function, in which case the a.zName
 10844  10844   ** field is not used.
        10845  +**
        10846  +** By default the Expr.zSpan field holds a human-readable description of
        10847  +** the expression that is used in the generation of error messages and
        10848  +** column labels.  In this case, Expr.zSpan is typically the text of a
        10849  +** column expression as it exists in a SELECT statement.  However, if
        10850  +** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
        10851  +** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
        10852  +** form is used for name resolution with nested FROM clauses.
 10845  10853   */
 10846  10854   struct ExprList {
 10847  10855     int nExpr;             /* Number of expressions on the list */
 10848  10856     int iECursor;          /* VDBE Cursor associated with this ExprList */
 10849  10857     struct ExprList_item { /* For each expression in the list */
 10850         -    Expr *pExpr;           /* The list of expressions */
 10851         -    char *zName;           /* Token associated with this expression */
 10852         -    char *zSpan;           /* Original text of the expression */
 10853         -    u8 sortOrder;          /* 1 for DESC or 0 for ASC */
 10854         -    u8 done;               /* A flag to indicate when processing is finished */
 10855         -    u16 iOrderByCol;       /* For ORDER BY, column number in result set */
 10856         -    u16 iAlias;            /* Index into Parse.aAlias[] for zName */
        10858  +    Expr *pExpr;            /* The list of expressions */
        10859  +    char *zName;            /* Token associated with this expression */
        10860  +    char *zSpan;            /* Original text of the expression */
        10861  +    u8 sortOrder;           /* 1 for DESC or 0 for ASC */
        10862  +    unsigned done :1;       /* A flag to indicate when processing is finished */
        10863  +    unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
        10864  +    u16 iOrderByCol;        /* For ORDER BY, column number in result set */
        10865  +    u16 iAlias;             /* Index into Parse.aAlias[] for zName */
 10857  10866     } *a;                  /* Alloc a power of two greater or equal to nExpr */
 10858  10867   };
 10859  10868   
 10860  10869   /*
 10861  10870   ** An instance of this structure is used by the parser to record both
 10862  10871   ** the parse tree for an expression and the span of input text for an
 10863  10872   ** expression.
................................................................................
 11169  11178   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
 11170  11179   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 11171  11180   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 11172  11181   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
 11173  11182   #define SF_UseSorter       0x0040  /* Sort using a sorter */
 11174  11183   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
 11175  11184   #define SF_Materialize     0x0100  /* Force materialization of views */
        11185  +#define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
 11176  11186   
 11177  11187   
 11178  11188   /*
 11179  11189   ** The results of a select can be distributed in several ways.  The
 11180  11190   ** "SRT" prefix means "SELECT Result Type".
 11181  11191   */
 11182  11192   #define SRT_Union        1  /* Store result as keys in an index */
................................................................................
 11881  11891   SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
 11882  11892   SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 11883  11893   SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 11884  11894                           Token*, int, int);
 11885  11895   SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 11886  11896   SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 11887  11897   SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 11888         -                         Expr*,ExprList*,int,Expr*,Expr*);
        11898  +                         Expr*,ExprList*,u16,Expr*,Expr*);
 11889  11899   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 11890  11900   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 11891  11901   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 11892  11902   SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 11893  11903   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 11894  11904   SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
 11895  11905   #endif
................................................................................
 12138  12148   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 12139  12149   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 12140  12150   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 12141  12151   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 12142  12152   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
 12143  12153   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
 12144  12154   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
        12155  +SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 12145  12156   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 12146  12157   SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 12147  12158   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 12148  12159   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 12149  12160   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 12150  12161   SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 12151  12162   SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
................................................................................
 12276  12287     #define sqlite3FkCheck(a,b,c,d)
 12277  12288     #define sqlite3FkDropTable(a,b,c)
 12278  12289     #define sqlite3FkOldmask(a,b)      0
 12279  12290     #define sqlite3FkRequired(a,b,c,d) 0
 12280  12291   #endif
 12281  12292   #ifndef SQLITE_OMIT_FOREIGN_KEY
 12282  12293   SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
        12294  +SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
 12283  12295   #else
 12284  12296     #define sqlite3FkDelete(a,b)
        12297  +  #define sqlite3FkLocateIndex(a,b,c,d,e)
 12285  12298   #endif
 12286  12299   
 12287  12300   
 12288  12301   /*
 12289  12302   ** Available fault injectors.  Should be numbered beginning with 0.
 12290  12303   */
 12291  12304   #define SQLITE_FAULTINJECTOR_MALLOC     0
................................................................................
 23284  23297     { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
 23285  23298   #else
 23286  23299     { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
 23287  23300   #endif
 23288  23301   #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
 23289  23302                       aSyscall[13].pCurrent)
 23290  23303   
 23291         -#if SQLITE_ENABLE_LOCKING_STYLE
 23292  23304     { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
 23293         -#else
 23294         -  { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
 23295         -#endif
 23296  23305   #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
 23297  23306   
 23298  23307   #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 23299  23308     { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
 23300  23309   #else
 23301  23310     { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
 23302  23311   #endif
................................................................................
 23313  23322   
 23314  23323     { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
 23315  23324   #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
 23316  23325   
 23317  23326     { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
 23318  23327   #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 23319  23328   
 23320         -  { "umask",        (sqlite3_syscall_ptr)umask,           0 },
 23321         -#define osUmask     ((mode_t(*)(mode_t))aSyscall[21].pCurrent)
 23322         -
 23323  23329   }; /* End of the overrideable system calls */
 23324  23330   
 23325  23331   /*
 23326  23332   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 23327  23333   ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
 23328  23334   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
 23329  23335   ** system call named zName.
................................................................................
 23420  23426   ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
 23421  23427   ** transaction crashes and leaves behind hot journals, then any
 23422  23428   ** process that is able to write to the database will also be able to
 23423  23429   ** recover the hot journals.
 23424  23430   */
 23425  23431   static int robust_open(const char *z, int f, mode_t m){
 23426  23432     int fd;
 23427         -  mode_t m2;
 23428         -  mode_t origM = 0;
 23429         -  if( m==0 ){
 23430         -    m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
 23431         -  }else{
 23432         -    m2 = m;
 23433         -    origM = osUmask(0);
 23434         -  }
        23433  +  mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
 23435  23434     do{
 23436  23435   #if defined(O_CLOEXEC)
 23437  23436       fd = osOpen(z,f|O_CLOEXEC,m2);
 23438  23437   #else
 23439  23438       fd = osOpen(z,f,m2);
 23440  23439   #endif
 23441  23440     }while( fd<0 && errno==EINTR );
 23442         -  if( m ){
 23443         -    osUmask(origM);
 23444         -  }
        23441  +  if( fd>=0 ){
        23442  +    if( m!=0 ){
        23443  +      struct stat statbuf;
        23444  +      if( osFstat(fd, &statbuf)==0 && (statbuf.st_mode&0777)!=m ){
        23445  +        osFchmod(fd, m);
        23446  +      }
        23447  +    }
 23445  23448   #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
 23446         -  if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
        23449  +    osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 23447  23450   #endif
        23451  +  }
 23448  23452     return fd;
 23449  23453   }
 23450  23454   
 23451  23455   /*
 23452  23456   ** Helper functions to obtain and relinquish the global mutex. The
 23453  23457   ** global mutex is used to protect the unixInodeInfo and
 23454  23458   ** vxworksFileId objects used by this file, all of which may be 
................................................................................
 29866  29870       UNIXVFS("unix-proxy",    proxyIoFinder ),
 29867  29871   #endif
 29868  29872     };
 29869  29873     unsigned int i;          /* Loop counter */
 29870  29874   
 29871  29875     /* Double-check that the aSyscall[] array has been constructed
 29872  29876     ** correctly.  See ticket [bb3a86e890c8e96ab] */
 29873         -  assert( ArraySize(aSyscall)==22 );
        29877  +  assert( ArraySize(aSyscall)==21 );
 29874  29878   
 29875  29879     /* Register all VFSes defined in the aVfs[] array */
 29876  29880     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 29877  29881       sqlite3_vfs_register(&aVfs[i], i==0);
 29878  29882     }
 29879  29883     return SQLITE_OK; 
 29880  29884   }
................................................................................
 56332  56336     if( !sCheck.aPgRef ){
 56333  56337       *pnErr = 1;
 56334  56338       sqlite3BtreeLeave(p);
 56335  56339       return 0;
 56336  56340     }
 56337  56341     i = PENDING_BYTE_PAGE(pBt);
 56338  56342     if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 56339         -  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
        56343  +  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
 56340  56344     sCheck.errMsg.useMalloc = 2;
 56341  56345   
 56342  56346     /* Check the integrity of the freelist
 56343  56347     */
 56344  56348     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
 56345  56349               get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
 56346  56350   
................................................................................
 56867  56871   }
 56868  56872   
 56869  56873   /*
 56870  56874   ** Parameter zSrcData points to a buffer containing the data for 
 56871  56875   ** page iSrcPg from the source database. Copy this data into the 
 56872  56876   ** destination database.
 56873  56877   */
 56874         -static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
        56878  +static int backupOnePage(
        56879  +  sqlite3_backup *p,              /* Backup handle */
        56880  +  Pgno iSrcPg,                    /* Source database page to backup */
        56881  +  const u8 *zSrcData,             /* Source database page data */
        56882  +  int bUpdate                     /* True for an update, false otherwise */
        56883  +){
 56875  56884     Pager * const pDestPager = sqlite3BtreePager(p->pDest);
 56876  56885     const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
 56877  56886     int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
 56878  56887     const int nCopy = MIN(nSrcPgsz, nDestPgsz);
 56879  56888     const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
 56880  56889   #ifdef SQLITE_HAS_CODEC
 56881  56890     /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
................................................................................
 56940  56949         ** and the pager code use this trick (clearing the first byte
 56941  56950         ** of the page 'extra' space to invalidate the Btree layers
 56942  56951         ** cached parse of the page). MemPage.isInit is marked 
 56943  56952         ** "MUST BE FIRST" for this purpose.
 56944  56953         */
 56945  56954         memcpy(zOut, zIn, nCopy);
 56946  56955         ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
        56956  +      if( iOff==0 && bUpdate==0 ){
        56957  +        sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
        56958  +      }
 56947  56959       }
 56948  56960       sqlite3PagerUnref(pDestPg);
 56949  56961     }
 56950  56962   
 56951  56963     return rc;
 56952  56964   }
 56953  56965   
................................................................................
 57046  57058       assert( nSrcPage>=0 );
 57047  57059       for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
 57048  57060         const Pgno iSrcPg = p->iNext;                 /* Source page number */
 57049  57061         if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
 57050  57062           DbPage *pSrcPg;                             /* Source page object */
 57051  57063           rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
 57052  57064           if( rc==SQLITE_OK ){
 57053         -          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
        57065  +          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
 57054  57066             sqlite3PagerUnref(pSrcPg);
 57055  57067           }
 57056  57068         }
 57057  57069         p->iNext++;
 57058  57070       }
 57059  57071       if( rc==SQLITE_OK ){
 57060  57072         p->nPagecount = nSrcPage;
................................................................................
 57294  57306         /* The backup process p has already copied page iPage. But now it
 57295  57307         ** has been modified by a transaction on the source pager. Copy
 57296  57308         ** the new data into the backup.
 57297  57309         */
 57298  57310         int rc;
 57299  57311         assert( p->pDestDb );
 57300  57312         sqlite3_mutex_enter(p->pDestDb->mutex);
 57301         -      rc = backupOnePage(p, iPage, aData);
        57313  +      rc = backupOnePage(p, iPage, aData, 1);
 57302  57314         sqlite3_mutex_leave(p->pDestDb->mutex);
 57303  57315         assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
 57304  57316         if( rc!=SQLITE_OK ){
 57305  57317           p->rc = rc;
 57306  57318         }
 57307  57319       }
 57308  57320     }
................................................................................
 71888  71900       rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
 71889  71901       if( rc==SQLITE_OK ){
 71890  71902         p->pReal = pReal;
 71891  71903         if( p->iSize>0 ){
 71892  71904           assert(p->iSize<=p->nBuf);
 71893  71905           rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
 71894  71906         }
        71907  +      if( rc!=SQLITE_OK ){
        71908  +        /* If an error occurred while writing to the file, close it before
        71909  +        ** returning. This way, SQLite uses the in-memory journal data to 
        71910  +        ** roll back changes made to the internal page-cache before this
        71911  +        ** function was called.  */
        71912  +        sqlite3OsClose(pReal);
        71913  +        p->pReal = 0;
        71914  +      }
 71895  71915       }
 71896  71916     }
 71897  71917     return rc;
 71898  71918   }
 71899  71919   
 71900  71920   /*
 71901  71921   ** Close the file.
................................................................................
 72634  72654       for(k=0; k<pUsing->nId; k++){
 72635  72655         if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
 72636  72656       }
 72637  72657     }
 72638  72658     return 0;
 72639  72659   }
 72640  72660   
        72661  +/*
        72662  +** Subqueries stores the original database, table and column names for their
        72663  +** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
        72664  +** Check to see if the zSpan given to this routine matches the zDb, zTab,
        72665  +** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
        72666  +** match anything.
        72667  +*/
        72668  +SQLITE_PRIVATE int sqlite3MatchSpanName(
        72669  +  const char *zSpan,
        72670  +  const char *zCol,
        72671  +  const char *zTab,
        72672  +  const char *zDb
        72673  +){
        72674  +  int n;
        72675  +  for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
        72676  +  if( zDb && sqlite3StrNICmp(zSpan, zDb, n)!=0 ){
        72677  +    return 0;
        72678  +  }
        72679  +  zSpan += n+1;
        72680  +  for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
        72681  +  if( zTab && sqlite3StrNICmp(zSpan, zTab, n)!=0 ){
        72682  +    return 0;
        72683  +  }
        72684  +  zSpan += n+1;
        72685  +  if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
        72686  +    return 0;
        72687  +  }
        72688  +  return 1;
        72689  +}
 72641  72690   
 72642  72691   /*
 72643  72692   ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
 72644  72693   ** that name in the set of source tables in pSrcList and make the pExpr 
 72645  72694   ** expression node refer back to that source column.  The following changes
 72646  72695   ** are made to pExpr:
 72647  72696   **
................................................................................
 72689  72738     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 72690  72739     assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 72691  72740   
 72692  72741     /* Initialize the node to no-match */
 72693  72742     pExpr->iTable = -1;
 72694  72743     pExpr->pTab = 0;
 72695  72744     ExprSetIrreducible(pExpr);
        72745  +
        72746  +  /* Translate the schema name in zDb into a pointer to the corresponding
        72747  +  ** schema.  If not found, pSchema will remain NULL and nothing will match
        72748  +  ** resulting in an appropriate error message toward the end of this routine
        72749  +  */
        72750  +  if( zDb ){
        72751  +    for(i=0; i<db->nDb; i++){
        72752  +      assert( db->aDb[i].zName );
        72753  +      if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
        72754  +        pSchema = db->aDb[i].pSchema;
        72755  +        break;
        72756  +      }
        72757  +    }
        72758  +  }
 72696  72759   
 72697  72760     /* Start at the inner-most context and move outward until a match is found */
 72698  72761     while( pNC && cnt==0 ){
 72699  72762       ExprList *pEList;
 72700  72763       SrcList *pSrcList = pNC->pSrcList;
 72701  72764   
 72702  72765       if( pSrcList ){
 72703  72766         for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
 72704  72767           Table *pTab;
 72705         -        int iDb;
 72706  72768           Column *pCol;
 72707  72769     
 72708  72770           pTab = pItem->pTab;
 72709  72771           assert( pTab!=0 && pTab->zName!=0 );
 72710         -        iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 72711  72772           assert( pTab->nCol>0 );
        72773  +        if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
        72774  +          ExprList *pEList = pItem->pSelect->pEList;
        72775  +          int hit = 0;
        72776  +          for(j=0; j<pEList->nExpr; j++){
        72777  +            if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
        72778  +              cnt++;
        72779  +              cntTab = 2;
        72780  +              pMatch = pItem;
        72781  +              pExpr->iColumn = j;
        72782  +              hit = 1;
        72783  +            }
        72784  +          }
        72785  +          if( hit || zTab==0 ) continue;
        72786  +        }
        72787  +        if( zDb && pTab->pSchema!=pSchema ){
        72788  +          continue;
        72789  +        }
 72712  72790           if( zTab ){
 72713         -          if( pItem->zAlias ){
 72714         -            char *zTabName = pItem->zAlias;
 72715         -            if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
 72716         -          }else{
 72717         -            char *zTabName = pTab->zName;
 72718         -            if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
 72719         -              continue;
 72720         -            }
 72721         -            if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
 72722         -              continue;
 72723         -            }
        72791  +          const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
        72792  +          assert( zTabName!=0 );
        72793  +          if( sqlite3StrICmp(zTabName, zTab)!=0 ){
        72794  +            continue;
 72724  72795             }
 72725  72796           }
 72726  72797           if( 0==(cntTab++) ){
 72727         -          pExpr->iTable = pItem->iCursor;
 72728         -          pExpr->pTab = pTab;
 72729         -          pSchema = pTab->pSchema;
 72730  72798             pMatch = pItem;
 72731  72799           }
 72732  72800           for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 72733  72801             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 72734  72802               /* If there has been exactly one prior match and this match
 72735  72803               ** is for the right-hand table of a NATURAL JOIN or is in a 
 72736  72804               ** USING clause, then skip this match.
 72737  72805               */
 72738  72806               if( cnt==1 ){
 72739  72807                 if( pItem->jointype & JT_NATURAL ) continue;
 72740  72808                 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
 72741  72809               }
 72742  72810               cnt++;
 72743         -            pExpr->iTable = pItem->iCursor;
 72744         -            pExpr->pTab = pTab;
 72745  72811               pMatch = pItem;
 72746         -            pSchema = pTab->pSchema;
 72747  72812               /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
 72748  72813               pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 72749  72814               break;
 72750  72815             }
 72751  72816           }
 72752  72817         }
 72753         -    }
        72818  +      if( pMatch ){
        72819  +        pExpr->iTable = pMatch->iCursor;
        72820  +        pExpr->pTab = pMatch->pTab;
        72821  +        pSchema = pExpr->pTab->pSchema;
        72822  +      }
        72823  +    } /* if( pSrcList ) */
 72754  72824   
 72755  72825   #ifndef SQLITE_OMIT_TRIGGER
 72756  72826       /* If we have not already resolved the name, then maybe 
 72757  72827       ** it is a new.* or old.* trigger argument reference
 72758  72828       */
 72759  72829       if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
 72760  72830         int op = pParse->eTriggerOp;
................................................................................
 73081  73151           }
 73082  73152         }
 73083  73153   #endif
 73084  73154         if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
 73085  73155           sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
 73086  73156           pNC->nErr++;
 73087  73157           is_agg = 0;
 73088         -      }else if( no_such_func ){
        73158  +      }else if( no_such_func && pParse->db->init.busy==0 ){
 73089  73159           sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
 73090  73160           pNC->nErr++;
 73091  73161         }else if( wrong_num_args ){
 73092  73162           sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 73093  73163                nId, zId);
 73094  73164           pNC->nErr++;
 73095  73165         }
................................................................................
 73517  73587       memset(&sNC, 0, sizeof(sNC));
 73518  73588       sNC.pParse = pParse;
 73519  73589       if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
 73520  73590           sqlite3ResolveExprNames(&sNC, p->pOffset) ){
 73521  73591         return WRC_Abort;
 73522  73592       }
 73523  73593     
 73524         -    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
 73525         -    ** resolve the result-set expression list.
 73526         -    */
 73527         -    sNC.ncFlags = NC_AllowAgg;
 73528         -    sNC.pSrcList = p->pSrc;
 73529         -    sNC.pNext = pOuterNC;
 73530         -  
 73531         -    /* Resolve names in the result set. */
 73532         -    pEList = p->pEList;
 73533         -    assert( pEList!=0 );
 73534         -    for(i=0; i<pEList->nExpr; i++){
 73535         -      Expr *pX = pEList->a[i].pExpr;
 73536         -      if( sqlite3ResolveExprNames(&sNC, pX) ){
 73537         -        return WRC_Abort;
 73538         -      }
 73539         -    }
 73540         -  
 73541  73594       /* Recursively resolve names in all subqueries
 73542  73595       */
 73543  73596       for(i=0; i<p->pSrc->nSrc; i++){
 73544  73597         struct SrcList_item *pItem = &p->pSrc->a[i];
 73545  73598         if( pItem->pSelect ){
 73546  73599           NameContext *pNC;         /* Used to iterate name contexts */
 73547  73600           int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
................................................................................
 73560  73613           if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
 73561  73614   
 73562  73615           for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
 73563  73616           assert( pItem->isCorrelated==0 && nRef<=0 );
 73564  73617           pItem->isCorrelated = (nRef!=0);
 73565  73618         }
 73566  73619       }
        73620  +  
        73621  +    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
        73622  +    ** resolve the result-set expression list.
        73623  +    */
        73624  +    sNC.ncFlags = NC_AllowAgg;
        73625  +    sNC.pSrcList = p->pSrc;
        73626  +    sNC.pNext = pOuterNC;
        73627  +  
        73628  +    /* Resolve names in the result set. */
        73629  +    pEList = p->pEList;
        73630  +    assert( pEList!=0 );
        73631  +    for(i=0; i<pEList->nExpr; i++){
        73632  +      Expr *pX = pEList->a[i].pExpr;
        73633  +      if( sqlite3ResolveExprNames(&sNC, pX) ){
        73634  +        return WRC_Abort;
        73635  +      }
        73636  +    }
 73567  73637     
 73568  73638       /* If there are no aggregate functions in the result-set, and no GROUP BY 
 73569  73639       ** expression, do not allow aggregates in any of the other expressions.
 73570  73640       */
 73571  73641       assert( (p->selFlags & SF_Aggregate)==0 );
 73572  73642       pGroupBy = p->pGroupBy;
 73573  73643       if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
................................................................................
 77044  77114     }else{
 77045  77115       sqlite3ExplainPush(pOut);
 77046  77116       for(i=0; i<pList->nExpr; i++){
 77047  77117         sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
 77048  77118         sqlite3ExplainPush(pOut);
 77049  77119         sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
 77050  77120         sqlite3ExplainPop(pOut);
        77121  +      if( pList->a[i].zName ){
        77122  +        sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
        77123  +      }
        77124  +      if( pList->a[i].bSpanIsTab ){
        77125  +        sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
        77126  +      }
 77051  77127         if( i<pList->nExpr-1 ){
 77052  77128           sqlite3ExplainNL(pOut);
 77053  77129         }
 77054  77130       }
 77055  77131       sqlite3ExplainPop(pOut);
 77056  77132     }
 77057  77133   }
................................................................................
 87521  87597   **   Register (x+3):      3.1  (type real)
 87522  87598   */
 87523  87599   
 87524  87600   /*
 87525  87601   ** A foreign key constraint requires that the key columns in the parent
 87526  87602   ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
 87527  87603   ** Given that pParent is the parent table for foreign key constraint pFKey, 
 87528         -** search the schema a unique index on the parent key columns. 
        87604  +** search the schema for a unique index on the parent key columns. 
 87529  87605   **
 87530  87606   ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
 87531  87607   ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
 87532  87608   ** is set to point to the unique index. 
 87533  87609   ** 
 87534  87610   ** If the parent key consists of a single column (the foreign key constraint
 87535  87611   ** is not a composite foreign key), output variable *paiCol is set to NULL.
................................................................................
 87557  87633   **      consists of a a different number of columns to the child key in 
 87558  87634   **      the child table.
 87559  87635   **
 87560  87636   ** then non-zero is returned, and a "foreign key mismatch" error loaded
 87561  87637   ** into pParse. If an OOM error occurs, non-zero is returned and the
 87562  87638   ** pParse->db->mallocFailed flag is set.
 87563  87639   */
 87564         -static int locateFkeyIndex(
        87640  +SQLITE_PRIVATE int sqlite3FkLocateIndex(
 87565  87641     Parse *pParse,                  /* Parse context to store any error in */
 87566  87642     Table *pParent,                 /* Parent table of FK constraint pFKey */
 87567  87643     FKey *pFKey,                    /* Foreign key to find index for */
 87568  87644     Index **ppIdx,                  /* OUT: Unique index on parent table */
 87569  87645     int **paiCol                    /* OUT: Map of index columns in pFKey */
 87570  87646   ){
 87571  87647     Index *pIdx = 0;                    /* Value to return via *ppIdx */
................................................................................
 87654  87730           if( i==nCol ) break;      /* pIdx is usable */
 87655  87731         }
 87656  87732       }
 87657  87733     }
 87658  87734   
 87659  87735     if( !pIdx ){
 87660  87736       if( !pParse->disableTriggers ){
 87661         -      sqlite3ErrorMsg(pParse, "foreign key mismatch");
        87737  +      sqlite3ErrorMsg(pParse,
        87738  +           "foreign key mismatch - \"%w\" referencing \"%w\"",
        87739  +           pFKey->pFrom->zName, pFKey->zTo);
 87662  87740       }
 87663  87741       sqlite3DbFree(pParse->db, aiCol);
 87664  87742       return 1;
 87665  87743     }
 87666  87744   
 87667  87745     *ppIdx = pIdx;
 87668  87746     return 0;
................................................................................
 88115  88193       ** schema items cannot be located, set an error in pParse and return 
 88116  88194       ** early.  */
 88117  88195       if( pParse->disableTriggers ){
 88118  88196         pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
 88119  88197       }else{
 88120  88198         pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
 88121  88199       }
 88122         -    if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
        88200  +    if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
 88123  88201         assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
 88124  88202         if( !isIgnoreErrors || db->mallocFailed ) return;
 88125  88203         if( pTo==0 ){
 88126  88204           /* If isIgnoreErrors is true, then a table is being dropped. In this
 88127  88205           ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
 88128  88206           ** before actually dropping it in order to check FK constraints.
 88129  88207           ** If the parent table of an FK constraint on the current table is
................................................................................
 88195  88273       if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
 88196  88274         assert( regOld==0 && regNew!=0 );
 88197  88275         /* Inserting a single row into a parent table cannot cause an immediate
 88198  88276         ** foreign key violation. So do nothing in this case.  */
 88199  88277         continue;
 88200  88278       }
 88201  88279   
 88202         -    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
        88280  +    if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
 88203  88281         if( !isIgnoreErrors || db->mallocFailed ) return;
 88204  88282         continue;
 88205  88283       }
 88206  88284       assert( aiCol || pFKey->nCol==1 );
 88207  88285   
 88208  88286       /* Create a SrcList structure containing a single table (the table 
 88209  88287       ** the foreign key that refers to this table is attached to). This
................................................................................
 88250  88328       FKey *p;
 88251  88329       int i;
 88252  88330       for(p=pTab->pFKey; p; p=p->pNextFrom){
 88253  88331         for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
 88254  88332       }
 88255  88333       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 88256  88334         Index *pIdx = 0;
 88257         -      locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
        88335  +      sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
 88258  88336         if( pIdx ){
 88259  88337           for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
 88260  88338         }
 88261  88339       }
 88262  88340     }
 88263  88341     return mask;
 88264  88342   }
................................................................................
 88376  88454       TriggerStep *pStep = 0;        /* First (only) step of trigger program */
 88377  88455       Expr *pWhere = 0;             /* WHERE clause of trigger step */
 88378  88456       ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
 88379  88457       Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
 88380  88458       int i;                        /* Iterator variable */
 88381  88459       Expr *pWhen = 0;              /* WHEN clause for the trigger */
 88382  88460   
 88383         -    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
        88461  +    if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
 88384  88462       assert( aiCol || pFKey->nCol==1 );
 88385  88463   
 88386  88464       for(i=0; i<pFKey->nCol; i++){
 88387  88465         Token tOld = { "old", 3 };  /* Literal "old" token */
 88388  88466         Token tNew = { "new", 3 };  /* Literal "new" token */
 88389  88467         Token tFromCol;             /* Name of column in child table */
 88390  88468         Token tToCol;               /* Name of column in parent table */
................................................................................
 92716  92794     ** dflt_value: The default value for the column, if any.
 92717  92795     */
 92718  92796     if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
 92719  92797       Table *pTab;
 92720  92798       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 92721  92799       pTab = sqlite3FindTable(db, zRight, zDb);
 92722  92800       if( pTab ){
 92723         -      int i;
        92801  +      int i, k;
 92724  92802         int nHidden = 0;
 92725  92803         Column *pCol;
        92804  +      Index *pPk;
        92805  +      for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){}
 92726  92806         sqlite3VdbeSetNumCols(v, 6);
 92727  92807         pParse->nMem = 6;
 92728  92808         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
 92729  92809         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 92730  92810         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
 92731  92811         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
 92732  92812         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
................................................................................
 92743  92823              pCol->zType ? pCol->zType : "", 0);
 92744  92824           sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
 92745  92825           if( pCol->zDflt ){
 92746  92826             sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
 92747  92827           }else{
 92748  92828             sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
 92749  92829           }
 92750         -        sqlite3VdbeAddOp2(v, OP_Integer,
 92751         -                            (pCol->colFlags&COLFLAG_PRIMKEY)!=0, 6);
        92830  +        if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
        92831  +          k = 0;
        92832  +        }else if( pPk==0 ){
        92833  +          k = 1;
        92834  +        }else{
        92835  +          for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
        92836  +        }
        92837  +        sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
 92752  92838           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
 92753  92839         }
 92754  92840       }
 92755  92841     }else
 92756  92842   
 92757  92843     if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
 92758  92844       Index *pIdx;
................................................................................
 92879  92965               sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
 92880  92966             }
 92881  92967             ++i;
 92882  92968             pFK = pFK->pNextFrom;
 92883  92969           }
 92884  92970         }
 92885  92971       }
        92972  +  }else
        92973  +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
        92974  +
        92975  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        92976  +  if( sqlite3StrICmp(zLeft, "foreign_key_check")==0 ){
        92977  +    FKey *pFK;             /* A foreign key constraint */
        92978  +    Table *pTab;           /* Child table contain "REFERENCES" keyword */
        92979  +    Table *pParent;        /* Parent table that child points to */
        92980  +    Index *pIdx;           /* Index in the parent table */
        92981  +    int i;                 /* Loop counter:  Foreign key number for pTab */
        92982  +    int j;                 /* Loop counter:  Field of the foreign key */
        92983  +    HashElem *k;           /* Loop counter:  Next table in schema */
        92984  +    int x;                 /* result variable */
        92985  +    int regResult;         /* 3 registers to hold a result row */
        92986  +    int regKey;            /* Register to hold key for checking the FK */
        92987  +    int regRow;            /* Registers to hold a row from pTab */
        92988  +    int addrTop;           /* Top of a loop checking foreign keys */
        92989  +    int addrOk;            /* Jump here if the key is OK */
        92990  +    int *aiCols;           /* child to parent column mapping */
        92991  +
        92992  +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
        92993  +    regResult = pParse->nMem+1;
        92994  +    pParse->nMem += 4;
        92995  +    regKey = ++pParse->nMem;
        92996  +    regRow = ++pParse->nMem;
        92997  +    v = sqlite3GetVdbe(pParse);
        92998  +    sqlite3VdbeSetNumCols(v, 4);
        92999  +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
        93000  +    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
        93001  +    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
        93002  +    sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
        93003  +    sqlite3CodeVerifySchema(pParse, iDb);
        93004  +    k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
        93005  +    while( k ){
        93006  +      if( zRight ){
        93007  +        pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
        93008  +        k = 0;
        93009  +      }else{
        93010  +        pTab = (Table*)sqliteHashData(k);
        93011  +        k = sqliteHashNext(k);
        93012  +      }
        93013  +      if( pTab==0 || pTab->pFKey==0 ) continue;
        93014  +      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
        93015  +      if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
        93016  +      sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
        93017  +      sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
        93018  +                        P4_TRANSIENT);
        93019  +      for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
        93020  +        pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
        93021  +        if( pParent==0 ) break;
        93022  +        pIdx = 0;
        93023  +        sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
        93024  +        x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
        93025  +        if( x==0 ){
        93026  +          if( pIdx==0 ){
        93027  +            sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
        93028  +          }else{
        93029  +            KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
        93030  +            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
        93031  +            sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
        93032  +          }
        93033  +        }else{
        93034  +          k = 0;
        93035  +          break;
        93036  +        }
        93037  +      }
        93038  +      if( pFK ) break;
        93039  +      if( pParse->nTab<i ) pParse->nTab = i;
        93040  +      addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
        93041  +      for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
        93042  +        pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
        93043  +        assert( pParent!=0 );
        93044  +        pIdx = 0;
        93045  +        aiCols = 0;
        93046  +        x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
        93047  +        assert( x==0 );
        93048  +        addrOk = sqlite3VdbeMakeLabel(v);
        93049  +        if( pIdx==0 ){
        93050  +          int iKey = pFK->aCol[0].iFrom;
        93051  +          assert( iKey>=0 && iKey<pTab->nCol );
        93052  +          if( iKey!=pTab->iPKey ){
        93053  +            sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
        93054  +            sqlite3ColumnDefault(v, pTab, iKey, regRow);
        93055  +            sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
        93056  +            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
        93057  +               sqlite3VdbeCurrentAddr(v)+3);
        93058  +          }else{
        93059  +            sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
        93060  +          }
        93061  +          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
        93062  +          sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
        93063  +          sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
        93064  +        }else{
        93065  +          for(j=0; j<pFK->nCol; j++){
        93066  +            sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
        93067  +                            aiCols ? aiCols[j] : pFK->aCol[0].iFrom, regRow+j);
        93068  +            sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
        93069  +          }
        93070  +          sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
        93071  +          sqlite3VdbeChangeP4(v, -1,
        93072  +                   sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
        93073  +          sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
        93074  +        }
        93075  +        sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
        93076  +        sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0, 
        93077  +                          pFK->zTo, P4_TRANSIENT);
        93078  +        sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
        93079  +        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
        93080  +        sqlite3VdbeResolveLabel(v, addrOk);
        93081  +        sqlite3DbFree(db, aiCols);
        93082  +      }
        93083  +      sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
        93084  +      sqlite3VdbeJumpHere(v, addrTop);
        93085  +    }
 92886  93086     }else
 92887  93087   #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 92888  93088   
 92889  93089   #ifndef NDEBUG
 92890  93090     if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
 92891  93091       if( zRight ){
 92892  93092         if( sqlite3GetBoolean(zRight, 0) ){
................................................................................
 93379  93579         sqlite3_key(db, zKey, i/2);
 93380  93580       }else{
 93381  93581         sqlite3_rekey(db, zKey, i/2);
 93382  93582       }
 93383  93583     }else
 93384  93584   #endif
 93385  93585   #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
 93386         -  if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
        93586  +  if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){
 93387  93587   #ifdef SQLITE_HAS_CODEC
 93388  93588       if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
 93389  93589         sqlite3_activate_see(&zRight[4]);
 93390  93590       }
 93391  93591   #endif
 93392  93592   #ifdef SQLITE_ENABLE_CEROD
 93393  93593       if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
................................................................................
 94338  94538     Parse *pParse,        /* Parsing context */
 94339  94539     ExprList *pEList,     /* which columns to include in the result */
 94340  94540     SrcList *pSrc,        /* the FROM clause -- which tables to scan */
 94341  94541     Expr *pWhere,         /* the WHERE clause */
 94342  94542     ExprList *pGroupBy,   /* the GROUP BY clause */
 94343  94543     Expr *pHaving,        /* the HAVING clause */
 94344  94544     ExprList *pOrderBy,   /* the ORDER BY clause */
 94345         -  int isDistinct,       /* true if the DISTINCT keyword is present */
        94545  +  u16 selFlags,         /* Flag parameters, such as SF_Distinct */
 94346  94546     Expr *pLimit,         /* LIMIT value.  NULL means not used */
 94347  94547     Expr *pOffset         /* OFFSET value.  NULL means no offset */
 94348  94548   ){
 94349  94549     Select *pNew;
 94350  94550     Select standin;
 94351  94551     sqlite3 *db = pParse->db;
 94352  94552     pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
................................................................................
 94362  94562     pNew->pEList = pEList;
 94363  94563     if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
 94364  94564     pNew->pSrc = pSrc;
 94365  94565     pNew->pWhere = pWhere;
 94366  94566     pNew->pGroupBy = pGroupBy;
 94367  94567     pNew->pHaving = pHaving;
 94368  94568     pNew->pOrderBy = pOrderBy;
 94369         -  pNew->selFlags = isDistinct ? SF_Distinct : 0;
        94569  +  pNew->selFlags = selFlags;
 94370  94570     pNew->op = TK_SELECT;
 94371  94571     pNew->pLimit = pLimit;
 94372  94572     pNew->pOffset = pOffset;
 94373  94573     assert( pOffset==0 || pLimit!=0 );
 94374  94574     pNew->addrOpenEphm[0] = -1;
 94375  94575     pNew->addrOpenEphm[1] = -1;
 94376  94576     pNew->addrOpenEphm[2] = -1;
................................................................................
 95619  95819     *pnCol = nCol;
 95620  95820     *paCol = aCol;
 95621  95821   
 95622  95822     for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 95623  95823       /* Get an appropriate name for the column
 95624  95824       */
 95625  95825       p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
 95626         -    assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
 95627         -               || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
 95628  95826       if( (zName = pEList->a[i].zName)!=0 ){
 95629  95827         /* If the column contains an "AS <name>" phrase, use <name> as the name */
 95630  95828         zName = sqlite3DbStrDup(db, zName);
 95631  95829       }else{
 95632  95830         Expr *pColExpr = p;  /* The expression that is the result column name */
 95633  95831         Table *pTab;         /* Table associated with this expression */
 95634  95832         while( pColExpr->op==TK_DOT ){
................................................................................
 95658  95856       /* Make sure the column name is unique.  If the name is not unique,
 95659  95857       ** append a integer to the name so that it becomes unique.
 95660  95858       */
 95661  95859       nName = sqlite3Strlen30(zName);
 95662  95860       for(j=cnt=0; j<i; j++){
 95663  95861         if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
 95664  95862           char *zNewName;
        95863  +        int k;
        95864  +        for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
        95865  +        if( zName[k]==':' ) nName = k;
 95665  95866           zName[nName] = 0;
 95666  95867           zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
 95667  95868           sqlite3DbFree(db, zName);
 95668  95869           zName = zNewName;
 95669  95870           j = -1;
 95670  95871           if( zName==0 ) break;
 95671  95872         }
................................................................................
 97443  97644     sqlite3SelectDelete(db, pSub1);
 97444  97645   
 97445  97646     return 1;
 97446  97647   }
 97447  97648   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 97448  97649   
 97449  97650   /*
 97450         -** Analyze the SELECT statement passed as an argument to see if it
 97451         -** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if 
 97452         -** it is, or 0 otherwise. At present, a query is considered to be
 97453         -** a min()/max() query if:
 97454         -**
 97455         -**   1. There is a single object in the FROM clause.
 97456         -**
 97457         -**   2. There is a single expression in the result set, and it is
 97458         -**      either min(x) or max(x), where x is a column reference.
 97459         -*/
 97460         -static u8 minMaxQuery(Select *p){
 97461         -  Expr *pExpr;
 97462         -  ExprList *pEList = p->pEList;
 97463         -
 97464         -  if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
 97465         -  pExpr = pEList->a[0].pExpr;
 97466         -  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
 97467         -  if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
 97468         -  pEList = pExpr->x.pList;
 97469         -  if( pEList==0 || pEList->nExpr!=1 ) return 0;
 97470         -  if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
 97471         -  assert( !ExprHasProperty(pExpr, EP_IntValue) );
 97472         -  if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
 97473         -    return WHERE_ORDERBY_MIN;
 97474         -  }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
 97475         -    return WHERE_ORDERBY_MAX;
 97476         -  }
 97477         -  return WHERE_ORDERBY_NORMAL;
        97651  +** Based on the contents of the AggInfo structure indicated by the first
        97652  +** argument, this function checks if the following are true:
        97653  +**
        97654  +**    * the query contains just a single aggregate function,
        97655  +**    * the aggregate function is either min() or max(), and
        97656  +**    * the argument to the aggregate function is a column value.
        97657  +**
        97658  +** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
        97659  +** is returned as appropriate. Also, *ppMinMax is set to point to the 
        97660  +** list of arguments passed to the aggregate before returning.
        97661  +**
        97662  +** Or, if the conditions above are not met, *ppMinMax is set to 0 and
        97663  +** WHERE_ORDERBY_NORMAL is returned.
        97664  +*/
        97665  +static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
        97666  +  int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
        97667  +
        97668  +  *ppMinMax = 0;
        97669  +  if( pAggInfo->nFunc==1 ){
        97670  +    Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
        97671  +    ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
        97672  +
        97673  +    assert( pExpr->op==TK_AGG_FUNCTION );
        97674  +    if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
        97675  +      const char *zFunc = pExpr->u.zToken;
        97676  +      if( sqlite3StrICmp(zFunc, "min")==0 ){
        97677  +        eRet = WHERE_ORDERBY_MIN;
        97678  +        *ppMinMax = pEList;
        97679  +      }else if( sqlite3StrICmp(zFunc, "max")==0 ){
        97680  +        eRet = WHERE_ORDERBY_MAX;
        97681  +        *ppMinMax = pEList;
        97682  +      }
        97683  +    }
        97684  +  }
        97685  +
        97686  +  assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
        97687  +  return eRet;
 97478  97688   }
 97479  97689   
 97480  97690   /*
 97481  97691   ** The select statement passed as the first argument is an aggregate query.
 97482  97692   ** The second argment is the associated aggregate-info object. This 
 97483  97693   ** function tests if the SELECT is of the form:
 97484  97694   **
................................................................................
 97565  97775   static int selectExpander(Walker *pWalker, Select *p){
 97566  97776     Parse *pParse = pWalker->pParse;
 97567  97777     int i, j, k;
 97568  97778     SrcList *pTabList;
 97569  97779     ExprList *pEList;
 97570  97780     struct SrcList_item *pFrom;
 97571  97781     sqlite3 *db = pParse->db;
        97782  +  Expr *pE, *pRight, *pExpr;
 97572  97783   
 97573  97784     if( db->mallocFailed  ){
 97574  97785       return WRC_Abort;
 97575  97786     }
 97576  97787     if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
 97577  97788       return WRC_Prune;
 97578  97789     }
................................................................................
 97650  97861     ** The following code just has to locate the TK_ALL expressions and expand
 97651  97862     ** each one to the list of all columns in all tables.
 97652  97863     **
 97653  97864     ** The first loop just checks to see if there are any "*" operators
 97654  97865     ** that need expanding.
 97655  97866     */
 97656  97867     for(k=0; k<pEList->nExpr; k++){
 97657         -    Expr *pE = pEList->a[k].pExpr;
        97868  +    pE = pEList->a[k].pExpr;
 97658  97869       if( pE->op==TK_ALL ) break;
 97659  97870       assert( pE->op!=TK_DOT || pE->pRight!=0 );
 97660  97871       assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
 97661  97872       if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
 97662  97873     }
 97663  97874     if( k<pEList->nExpr ){
 97664  97875       /*
................................................................................
 97668  97879       */
 97669  97880       struct ExprList_item *a = pEList->a;
 97670  97881       ExprList *pNew = 0;
 97671  97882       int flags = pParse->db->flags;
 97672  97883       int longNames = (flags & SQLITE_FullColNames)!=0
 97673  97884                         && (flags & SQLITE_ShortColNames)==0;
 97674  97885   
        97886  +    /* When processing FROM-clause subqueries, it is always the case
        97887  +    ** that full_column_names=OFF and short_column_names=ON.  The
        97888  +    ** sqlite3ResultSetOfSelect() routine makes it so. */
        97889  +    assert( (p->selFlags & SF_NestedFrom)==0
        97890  +          || ((flags & SQLITE_FullColNames)==0 &&
        97891  +              (flags & SQLITE_ShortColNames)!=0) );
        97892  +
 97675  97893       for(k=0; k<pEList->nExpr; k++){
 97676         -      Expr *pE = a[k].pExpr;
 97677         -      assert( pE->op!=TK_DOT || pE->pRight!=0 );
 97678         -      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
        97894  +      pE = a[k].pExpr;
        97895  +      pRight = pE->pRight;
        97896  +      assert( pE->op!=TK_DOT || pRight!=0 );
        97897  +      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
 97679  97898           /* This particular expression does not need to be expanded.
 97680  97899           */
 97681  97900           pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
 97682  97901           if( pNew ){
 97683  97902             pNew->a[pNew->nExpr-1].zName = a[k].zName;
 97684  97903             pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
 97685  97904             a[k].zName = 0;
................................................................................
 97686  97905             a[k].zSpan = 0;
 97687  97906           }
 97688  97907           a[k].pExpr = 0;
 97689  97908         }else{
 97690  97909           /* This expression is a "*" or a "TABLE.*" and needs to be
 97691  97910           ** expanded. */
 97692  97911           int tableSeen = 0;      /* Set to 1 when TABLE matches */
 97693         -        char *zTName;            /* text of name of TABLE */
        97912  +        char *zTName = 0;       /* text of name of TABLE */
 97694  97913           if( pE->op==TK_DOT ){
 97695  97914             assert( pE->pLeft!=0 );
 97696  97915             assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
 97697  97916             zTName = pE->pLeft->u.zToken;
 97698         -        }else{
 97699         -          zTName = 0;
 97700  97917           }
 97701  97918           for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 97702  97919             Table *pTab = pFrom->pTab;
        97920  +          Select *pSub = pFrom->pSelect;
 97703  97921             char *zTabName = pFrom->zAlias;
        97922  +          const char *zSchemaName = 0;
        97923  +          int iDb;
 97704  97924             if( zTabName==0 ){
 97705  97925               zTabName = pTab->zName;
 97706  97926             }
 97707  97927             if( db->mallocFailed ) break;
 97708         -          if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
 97709         -            continue;
        97928  +          if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
        97929  +            pSub = 0;
        97930  +            if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
        97931  +              continue;
        97932  +            }
        97933  +            iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
        97934  +            zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
 97710  97935             }
 97711         -          tableSeen = 1;
 97712  97936             for(j=0; j<pTab->nCol; j++){
 97713         -            Expr *pExpr, *pRight;
 97714  97937               char *zName = pTab->aCol[j].zName;
 97715  97938               char *zColname;  /* The computed column name */
 97716  97939               char *zToFree;   /* Malloced string that needs to be freed */
 97717  97940               Token sColname;  /* Computed column name as a token */
        97941  +
        97942  +            assert( zName );
        97943  +            if( zTName && pSub
        97944  +             && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
        97945  +            ){
        97946  +              continue;
        97947  +            }
 97718  97948   
 97719  97949               /* If a column is marked as 'hidden' (currently only possible
 97720  97950               ** for virtual tables), do not include it in the expanded
 97721  97951               ** result-set list.
 97722  97952               */
 97723  97953               if( IsHiddenColumn(&pTab->aCol[j]) ){
 97724  97954                 assert(IsVirtual(pTab));
 97725  97955                 continue;
 97726  97956               }
        97957  +            tableSeen = 1;
 97727  97958   
 97728  97959               if( i>0 && zTName==0 ){
 97729  97960                 if( (pFrom->jointype & JT_NATURAL)!=0
 97730  97961                   && tableAndColumnIndex(pTabList, i, zName, 0, 0)
 97731  97962                 ){
 97732  97963                   /* In a NATURAL join, omit the join columns from the 
 97733  97964                   ** table to the right of the join */
................................................................................
 97742  97973               pRight = sqlite3Expr(db, TK_ID, zName);
 97743  97974               zColname = zName;
 97744  97975               zToFree = 0;
 97745  97976               if( longNames || pTabList->nSrc>1 ){
 97746  97977                 Expr *pLeft;
 97747  97978                 pLeft = sqlite3Expr(db, TK_ID, zTabName);
 97748  97979                 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
        97980  +              if( zSchemaName ){
        97981  +                pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
        97982  +                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
        97983  +              }
 97749  97984                 if( longNames ){
 97750  97985                   zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
 97751  97986                   zToFree = zColname;
 97752  97987                 }
 97753  97988               }else{
 97754  97989                 pExpr = pRight;
 97755  97990               }
 97756  97991               pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
 97757  97992               sColname.z = zColname;
 97758  97993               sColname.n = sqlite3Strlen30(zColname);
 97759  97994               sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
        97995  +            if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
        97996  +              struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
        97997  +              if( pSub ){
        97998  +                pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
        97999  +                testcase( pX->zSpan==0 );
        98000  +              }else{
        98001  +                pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
        98002  +                                           zSchemaName, zTabName, zColname);
        98003  +                testcase( pX->zSpan==0 );
        98004  +              }
        98005  +              pX->bSpanIsTab = 1;
        98006  +            }
 97760  98007               sqlite3DbFree(db, zToFree);
 97761  98008             }
 97762  98009           }
 97763  98010           if( !tableSeen ){
 97764  98011             if( zTName ){
 97765  98012               sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
 97766  98013             }else{
................................................................................
 98810  99057           **
 98811  99058           **   + The optimizer code in where.c (the thing that decides which
 98812  99059           **     index or indices to use) should place a different priority on 
 98813  99060           **     satisfying the 'ORDER BY' clause than it does in other cases.
 98814  99061           **     Refer to code and comments in where.c for details.
 98815  99062           */
 98816  99063           ExprList *pMinMax = 0;
 98817         -        u8 flag = minMaxQuery(p);
        99064  +        u8 flag = WHERE_ORDERBY_NORMAL;
        99065  +        
        99066  +        assert( p->pGroupBy==0 );
        99067  +        assert( flag==0 );
        99068  +        if( p->pHaving==0 ){
        99069  +          flag = minMaxQuery(&sAggInfo, &pMinMax);
        99070  +        }
        99071  +        assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
        99072  +
 98818  99073           if( flag ){
 98819         -          assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
 98820         -          assert( p->pEList->a[0].pExpr->x.pList->nExpr==1 );
 98821         -          pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
        99074  +          pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
 98822  99075             pDel = pMinMax;
 98823  99076             if( pMinMax && !db->mallocFailed ){
 98824  99077               pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
 98825  99078               pMinMax->a[0].pExpr->op = TK_COLUMN;
 98826  99079             }
 98827  99080           }
 98828  99081     
................................................................................
102702 102955   #define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
102703 102956   #define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
102704 102957   #define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
102705 102958   #define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
102706 102959   #define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
102707 102960   #define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
102708 102961   #define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
102709         -#define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
       102962  +#define WHERE_IN_ABLE      0x080f1000  /* Able to support an IN operator */
102710 102963   #define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
102711 102964   #define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
102712 102965   #define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
102713 102966   #define WHERE_IDX_ONLY     0x00400000  /* Use index only - omit table */
102714 102967   #define WHERE_ORDERED      0x00800000  /* Output will appear in correct order */
102715 102968   #define WHERE_REVERSE      0x01000000  /* Scan in reverse order */
102716 102969   #define WHERE_UNIQUE       0x02000000  /* Selects no more than one row */
................................................................................
104506 104759     /* Count the number of possible WHERE clause constraints referring
104507 104760     ** to this virtual table */
104508 104761     for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
104509 104762       if( pTerm->leftCursor != pSrc->iCursor ) continue;
104510 104763       assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
104511 104764       testcase( pTerm->eOperator==WO_IN );
104512 104765       testcase( pTerm->eOperator==WO_ISNULL );
104513         -    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
       104766  +    if( pTerm->eOperator & (WO_ISNULL) ) continue;
104514 104767       if( pTerm->wtFlags & TERM_VNULL ) continue;
104515 104768       nTerm++;
104516 104769     }
104517 104770   
104518 104771     /* If the ORDER BY clause contains only columns in the current 
104519 104772     ** virtual table then allocate space for the aOrderBy part of
104520 104773     ** the sqlite3_index_info structure.
................................................................................
104554 104807     *(int*)&pIdxInfo->nOrderBy = nOrderBy;
104555 104808     *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
104556 104809     *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
104557 104810     *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
104558 104811                                                                      pUsage;
104559 104812   
104560 104813     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       104814  +    u8 op;
104561 104815       if( pTerm->leftCursor != pSrc->iCursor ) continue;
104562 104816       assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
104563 104817       testcase( pTerm->eOperator==WO_IN );
104564 104818       testcase( pTerm->eOperator==WO_ISNULL );
104565         -    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
       104819  +    if( pTerm->eOperator & (WO_ISNULL) ) continue;
104566 104820       if( pTerm->wtFlags & TERM_VNULL ) continue;
104567 104821       pIdxCons[j].iColumn = pTerm->u.leftColumn;
104568 104822       pIdxCons[j].iTermOffset = i;
104569         -    pIdxCons[j].op = (u8)pTerm->eOperator;
       104823  +    op = (u8)pTerm->eOperator;
       104824  +    if( op==WO_IN ) op = WO_EQ;
       104825  +    pIdxCons[j].op = op;
104570 104826       /* The direct assignment in the previous line is possible only because
104571 104827       ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
104572 104828       ** following asserts verify this fact. */
104573 104829       assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
104574 104830       assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
104575 104831       assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
104576 104832       assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
104577 104833       assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
104578 104834       assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
104579         -    assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
       104835  +    assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
104580 104836       j++;
104581 104837     }
104582 104838     for(i=0; i<nOrderBy; i++){
104583 104839       Expr *pExpr = pOrderBy->a[i].pExpr;
104584 104840       pIdxOrderBy[i].iColumn = pExpr->iColumn;
104585 104841       pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
104586 104842     }
................................................................................
104658 104914     Table *pTab = pSrc->pTab;
104659 104915     sqlite3_index_info *pIdxInfo;
104660 104916     struct sqlite3_index_constraint *pIdxCons;
104661 104917     struct sqlite3_index_constraint_usage *pUsage;
104662 104918     WhereTerm *pTerm;
104663 104919     int i, j;
104664 104920     int nOrderBy;
       104921  +  int bAllowIN;                   /* Allow IN optimizations */
104665 104922     double rCost;
104666 104923   
104667 104924     /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
104668 104925     ** malloc in allocateIndexInfo() fails and this function returns leaving
104669 104926     ** wsFlags in an uninitialized state, the caller may behave unpredictably.
104670 104927     */
104671 104928     memset(&p->cost, 0, sizeof(p->cost));
................................................................................
104692 104949     /* The module name must be defined. Also, by this point there must
104693 104950     ** be a pointer to an sqlite3_vtab structure. Otherwise
104694 104951     ** sqlite3ViewGetColumnNames() would have picked up the error. 
104695 104952     */
104696 104953     assert( pTab->azModuleArg && pTab->azModuleArg[0] );
104697 104954     assert( sqlite3GetVTable(pParse->db, pTab) );
104698 104955   
104699         -  /* Set the aConstraint[].usable fields and initialize all 
104700         -  ** output variables to zero.
104701         -  **
104702         -  ** aConstraint[].usable is true for constraints where the right-hand
104703         -  ** side contains only references to tables to the left of the current
104704         -  ** table.  In other words, if the constraint is of the form:
104705         -  **
104706         -  **           column = expr
104707         -  **
104708         -  ** and we are evaluating a join, then the constraint on column is 
104709         -  ** only valid if all tables referenced in expr occur to the left
104710         -  ** of the table containing column.
104711         -  **
104712         -  ** The aConstraints[] array contains entries for all constraints
104713         -  ** on the current table.  That way we only have to compute it once
104714         -  ** even though we might try to pick the best index multiple times.
104715         -  ** For each attempt at picking an index, the order of tables in the
104716         -  ** join might be different so we have to recompute the usable flag
104717         -  ** each time.
104718         -  */
104719         -  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104720         -  pUsage = pIdxInfo->aConstraintUsage;
104721         -  for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
104722         -    j = pIdxCons->iTermOffset;
104723         -    pTerm = &pWC->a[j];
104724         -    pIdxCons->usable = (pTerm->prereqRight&p->notReady) ? 0 : 1;
104725         -  }
104726         -  memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
104727         -  if( pIdxInfo->needToFreeIdxStr ){
104728         -    sqlite3_free(pIdxInfo->idxStr);
104729         -  }
104730         -  pIdxInfo->idxStr = 0;
104731         -  pIdxInfo->idxNum = 0;
104732         -  pIdxInfo->needToFreeIdxStr = 0;
104733         -  pIdxInfo->orderByConsumed = 0;
104734         -  /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
104735         -  pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
104736         -  nOrderBy = pIdxInfo->nOrderBy;
104737         -  if( !p->pOrderBy ){
104738         -    pIdxInfo->nOrderBy = 0;
104739         -  }
104740         -
104741         -  if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
104742         -    return;
104743         -  }
104744         -
104745         -  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104746         -  for(i=0; i<pIdxInfo->nConstraint; i++){
104747         -    if( pUsage[i].argvIndex>0 ){
104748         -      p->cost.used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
104749         -    }
104750         -  }
104751         -
       104956  +  /* Try once or twice.  On the first attempt, allow IN optimizations.
       104957  +  ** If an IN optimization is accepted by the virtual table xBestIndex
       104958  +  ** method, but the  pInfo->aConstrainUsage.omit flag is not set, then
       104959  +  ** the query will not work because it might allow duplicate rows in
       104960  +  ** output.  In that case, run the xBestIndex method a second time
       104961  +  ** without the IN constraints.  Usually this loop only runs once.
       104962  +  ** The loop will exit using a "break" statement.
       104963  +  */
       104964  +  for(bAllowIN=1; 1; bAllowIN--){
       104965  +    assert( bAllowIN==0 || bAllowIN==1 );
       104966  +
       104967  +    /* Set the aConstraint[].usable fields and initialize all 
       104968  +    ** output variables to zero.
       104969  +    **
       104970  +    ** aConstraint[].usable is true for constraints where the right-hand
       104971  +    ** side contains only references to tables to the left of the current
       104972  +    ** table.  In other words, if the constraint is of the form:
       104973  +    **
       104974  +    **           column = expr
       104975  +    **
       104976  +    ** and we are evaluating a join, then the constraint on column is 
       104977  +    ** only valid if all tables referenced in expr occur to the left
       104978  +    ** of the table containing column.
       104979  +    **
       104980  +    ** The aConstraints[] array contains entries for all constraints
       104981  +    ** on the current table.  That way we only have to compute it once
       104982  +    ** even though we might try to pick the best index multiple times.
       104983  +    ** For each attempt at picking an index, the order of tables in the
       104984  +    ** join might be different so we have to recompute the usable flag
       104985  +    ** each time.
       104986  +    */
       104987  +    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
       104988  +    pUsage = pIdxInfo->aConstraintUsage;
       104989  +    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
       104990  +      j = pIdxCons->iTermOffset;
       104991  +      pTerm = &pWC->a[j];
       104992  +      if( (pTerm->prereqRight&p->notReady)==0
       104993  +       && (bAllowIN || pTerm->eOperator!=WO_IN)
       104994  +      ){
       104995  +        pIdxCons->usable = 1;
       104996  +      }else{
       104997  +        pIdxCons->usable = 0;
       104998  +      }
       104999  +    }
       105000  +    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
       105001  +    if( pIdxInfo->needToFreeIdxStr ){
       105002  +      sqlite3_free(pIdxInfo->idxStr);
       105003  +    }
       105004  +    pIdxInfo->idxStr = 0;
       105005  +    pIdxInfo->idxNum = 0;
       105006  +    pIdxInfo->needToFreeIdxStr = 0;
       105007  +    pIdxInfo->orderByConsumed = 0;
       105008  +    /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
       105009  +    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
       105010  +    nOrderBy = pIdxInfo->nOrderBy;
       105011  +    if( !p->pOrderBy ){
       105012  +      pIdxInfo->nOrderBy = 0;
       105013  +    }
       105014  +  
       105015  +    if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
       105016  +      return;
       105017  +    }
       105018  +  
       105019  +    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
       105020  +    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
       105021  +      if( pUsage[i].argvIndex>0 ){
       105022  +        j = pIdxCons->iTermOffset;
       105023  +        pTerm = &pWC->a[j];
       105024  +        p->cost.used |= pTerm->prereqRight;
       105025  +        if( pTerm->eOperator==WO_IN && pUsage[i].omit==0 ){
       105026  +          /* Do not attempt to use an IN constraint if the virtual table
       105027  +          ** says that the equivalent EQ constraint cannot be safely omitted.
       105028  +          ** If we do attempt to use such a constraint, some rows might be
       105029  +          ** repeated in the output. */
       105030  +          break;
       105031  +        }
       105032  +      }
       105033  +    }
       105034  +    if( i>=pIdxInfo->nConstraint ) break;
       105035  +  }
       105036  +  
104752 105037     /* If there is an ORDER BY clause, and the selected virtual table index
104753 105038     ** does not satisfy it, increase the cost of the scan accordingly. This
104754 105039     ** matches the processing for non-virtual tables in bestBtreeIndex().
104755 105040     */
104756 105041     rCost = pIdxInfo->estimatedCost;
104757 105042     if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
104758 105043       rCost += estLog(rCost)*rCost;
................................................................................
105986 106271     assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
105987 106272     assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
105988 106273     assert( pSrc->pIndex==0 
105989 106274          || p->cost.plan.u.pIdx==0 
105990 106275          || p->cost.plan.u.pIdx==pSrc->pIndex 
105991 106276     );
105992 106277   
105993         -  WHERETRACE(("   best index is: %s\n",
105994         -         p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk"));
       106278  +  WHERETRACE(("   best index is %s cost=%.1f\n",
       106279  +         p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk",
       106280  +         p->cost.rCost));
105995 106281     
105996 106282     bestOrClauseIndex(p);
105997 106283     bestAutomaticIndex(p);
105998 106284     p->cost.plan.wsFlags |= eqTermMask;
105999 106285   }
106000 106286   
106001 106287   /*
................................................................................
106513 106799   
106514 106800   #ifndef SQLITE_OMIT_VIRTUALTABLE
106515 106801     if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
106516 106802       /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
106517 106803       **          to access the data.
106518 106804       */
106519 106805       int iReg;   /* P3 Value for OP_VFilter */
       106806  +    int addrNotFound;
106520 106807       sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
106521 106808       int nConstraint = pVtabIdx->nConstraint;
106522 106809       struct sqlite3_index_constraint_usage *aUsage =
106523 106810                                                   pVtabIdx->aConstraintUsage;
106524 106811       const struct sqlite3_index_constraint *aConstraint =
106525 106812                                                   pVtabIdx->aConstraint;
106526 106813   
106527 106814       sqlite3ExprCachePush(pParse);
106528 106815       iReg = sqlite3GetTempRange(pParse, nConstraint+2);
       106816  +    addrNotFound = pLevel->addrBrk;
106529 106817       for(j=1; j<=nConstraint; j++){
106530 106818         for(k=0; k<nConstraint; k++){
106531 106819           if( aUsage[k].argvIndex==j ){
106532         -          int iTerm = aConstraint[k].iTermOffset;
106533         -          sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
       106820  +          WhereTerm *pTerm = &pWC->a[aConstraint[k].iTermOffset];
       106821  +          int iTarget = iReg+j+1;
       106822  +          if( pTerm->eOperator & WO_IN ){
       106823  +            codeEqualityTerm(pParse, pTerm, pLevel, iTarget);
       106824  +            addrNotFound = pLevel->addrNxt;
       106825  +          }else{
       106826  +            sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
       106827  +          }
106534 106828             break;
106535 106829           }
106536 106830         }
106537 106831         if( k==nConstraint ) break;
106538 106832       }
106539 106833       sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
106540 106834       sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
106541         -    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
       106835  +    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, pVtabIdx->idxStr,
106542 106836                         pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
106543 106837       pVtabIdx->needToFreeIdxStr = 0;
106544 106838       for(j=0; j<nConstraint; j++){
106545 106839         if( aUsage[j].omit ){
106546 106840           int iTerm = aConstraint[j].iTermOffset;
106547 106841           disableTerm(pLevel, &pWC->a[iTerm]);
106548 106842         }
................................................................................
107480 107774     for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
107481 107775       WhereCost bestPlan;         /* Most efficient plan seen so far */
107482 107776       Index *pIdx;                /* Index for FROM table at pTabItem */
107483 107777       int j;                      /* For looping over FROM tables */
107484 107778       int bestJ = -1;             /* The value of j */
107485 107779       Bitmask m;                  /* Bitmask value for j or bestJ */
107486 107780       int isOptimal;              /* Iterator for optimal/non-optimal search */
       107781  +    int ckOptimal;              /* Do the optimal scan check */
107487 107782       int nUnconstrained;         /* Number tables without INDEXED BY */
107488 107783       Bitmask notIndexed;         /* Mask of tables that cannot use an index */
107489 107784   
107490 107785       memset(&bestPlan, 0, sizeof(bestPlan));
107491 107786       bestPlan.rCost = SQLITE_BIG_DBL;
107492 107787       WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
107493 107788   
................................................................................
107514 107809       ** that do not use indices.  But this nRow reduction only happens if the
107515 107810       ** table really is the innermost join.  
107516 107811       **
107517 107812       ** The second loop iteration is only performed if no optimal scan
107518 107813       ** strategies were found by the first iteration. This second iteration
107519 107814       ** is used to search for the lowest cost scan overall.
107520 107815       **
107521         -    ** Previous versions of SQLite performed only the second iteration -
107522         -    ** the next outermost loop was always that with the lowest overall
107523         -    ** cost. However, this meant that SQLite could select the wrong plan
107524         -    ** for scripts such as the following:
       107816  +    ** Without the optimal scan step (the first iteration) a suboptimal
       107817  +    ** plan might be chosen for queries like this:
107525 107818       **   
107526 107819       **   CREATE TABLE t1(a, b); 
107527 107820       **   CREATE TABLE t2(c, d);
107528 107821       **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
107529 107822       **
107530 107823       ** The best strategy is to iterate through table t1 first. However it
107531 107824       ** is not possible to determine this with a simple greedy algorithm.
................................................................................
107532 107825       ** Since the cost of a linear scan through table t2 is the same 
107533 107826       ** as the cost of a linear scan through table t1, a simple greedy 
107534 107827       ** algorithm may choose to use t2 for the outer loop, which is a much
107535 107828       ** costlier approach.
107536 107829       */
107537 107830       nUnconstrained = 0;
107538 107831       notIndexed = 0;
107539         -    for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
       107832  +
       107833  +    /* The optimal scan check only occurs if there are two or more tables
       107834  +    ** available to be reordered */
       107835  +    if( iFrom==nTabList-1 ){
       107836  +      ckOptimal = 0;  /* Common case of just one table in the FROM clause */
       107837  +    }else{
       107838  +      ckOptimal = -1;
107540 107839         for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
107541         -        int doNotReorder;    /* True if this table should not be reordered */
107542         -  
107543         -        doNotReorder =  (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0;
107544         -        if( j!=iFrom && doNotReorder ) break;
107545 107840           m = getMask(pMaskSet, sWBI.pSrc->iCursor);
107546 107841           if( (m & sWBI.notValid)==0 ){
107547 107842             if( j==iFrom ) iFrom++;
107548 107843             continue;
       107844  +        }
       107845  +        if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ) break;
       107846  +        if( ++ckOptimal ) break;
       107847  +        if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
       107848  +      }
       107849  +    }
       107850  +    assert( ckOptimal==0 || ckOptimal==1 );
       107851  +
       107852  +    for(isOptimal=ckOptimal; isOptimal>=0 && bestJ<0; isOptimal--){
       107853  +      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
       107854  +        if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ){
       107855  +          /* This break and one like it in the ckOptimal computation loop
       107856  +          ** above prevent table reordering across LEFT and CROSS JOINs.
       107857  +          ** The LEFT JOIN case is necessary for correctness.  The prohibition
       107858  +          ** against reordering across a CROSS JOIN is an SQLite feature that
       107859  +          ** allows the developer to control table reordering */
       107860  +          break;
       107861  +        }
       107862  +        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
       107863  +        if( (m & sWBI.notValid)==0 ){
       107864  +          assert( j>iFrom );
       107865  +          continue;
107549 107866           }
107550 107867           sWBI.notReady = (isOptimal ? m : sWBI.notValid);
107551 107868           if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
107552 107869     
107553 107870           WHERETRACE(("   === trying table %d (%s) with isOptimal=%d ===\n",
107554 107871                       j, sWBI.pSrc->pTab->zName, isOptimal));
107555 107872           assert( sWBI.pSrc->pTab );
................................................................................
107571 107888                     || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
107572 107889   
107573 107890           if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
107574 107891             notIndexed |= m;
107575 107892           }
107576 107893           if( isOptimal ){
107577 107894             pWInfo->a[j].rOptCost = sWBI.cost.rCost;
107578         -        }else if( iFrom<nTabList-1 ){
107579         -          /* If two or more tables have nearly the same outer loop cost,
       107895  +        }else if( ckOptimal ){
       107896  +          /* If two or more tables have nearly the same outer loop cost, but
107580 107897             ** very different inner loop (optimal) cost, we want to choose
107581 107898             ** for the outer loop that table which benefits the least from
107582 107899             ** being in the inner loop.  The following code scales the 
107583 107900             ** outer loop cost estimate to accomplish that. */
107584 107901             WHERETRACE(("   scaling cost from %.1f to %.1f\n",
107585 107902                         sWBI.cost.rCost,
107586 107903                         sWBI.cost.rCost/pWInfo->a[j].rOptCost));
................................................................................
107617 107934                         "       cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
107618 107935                         j, sWBI.pSrc->pTab->zName,
107619 107936                         sWBI.cost.rCost, sWBI.cost.plan.nRow,
107620 107937                         sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
107621 107938             bestPlan = sWBI.cost;
107622 107939             bestJ = j;
107623 107940           }
107624         -        if( doNotReorder ) break;
       107941  +
       107942  +        /* In a join like "w JOIN x LEFT JOIN y JOIN z"  make sure that
       107943  +        ** table y (and not table z) is always the next inner loop inside
       107944  +        ** of table x. */
       107945  +        if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
107625 107946         }
107626 107947       }
107627 107948       assert( bestJ>=0 );
107628 107949       assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
       107950  +    assert( bestJ==iFrom || (pTabList->a[iFrom].jointype & JT_LEFT)==0 );
       107951  +    testcase( bestJ>iFrom && (pTabList->a[iFrom].jointype & JT_CROSS)!=0 );
       107952  +    testcase( bestJ>iFrom && bestJ<nTabList-1
       107953  +                          && (pTabList->a[bestJ+1].jointype & JT_LEFT)!=0 );
107629 107954       WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
107630 107955                   "    cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
107631 107956                   bestJ, pTabList->a[bestJ].pTab->zName,
107632 107957                   pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
107633 107958                   bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
107634 107959       if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
107635 107960         assert( pWInfo->eDistinct==0 );
................................................................................
108181 108506     sqlite3ParserTOKENTYPE yy0;
108182 108507     struct LimitVal yy64;
108183 108508     Expr* yy122;
108184 108509     Select* yy159;
108185 108510     IdList* yy180;
108186 108511     struct {int value; int mask;} yy207;
108187 108512     u8 yy258;
       108513  +  u16 yy305;
108188 108514     struct LikeOp yy318;
108189 108515     TriggerStep* yy327;
108190 108516     ExprSpan yy342;
108191 108517     SrcList* yy347;
108192 108518     int yy392;
108193 108519     struct TrigEvent yy410;
108194 108520     ExprList* yy442;
................................................................................
110131 110457         case 31: /* temp ::= */ yytestcase(yyruleno==31);
110132 110458         case 69: /* autoinc ::= */ yytestcase(yyruleno==69);
110133 110459         case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==82);
110134 110460         case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
110135 110461         case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
110136 110462         case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
110137 110463         case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
110138         -      case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
110139         -      case 121: /* distinct ::= */ yytestcase(yyruleno==121);
110140 110464         case 221: /* between_op ::= BETWEEN */ yytestcase(yyruleno==221);
110141 110465         case 224: /* in_op ::= IN */ yytestcase(yyruleno==224);
110142 110466   {yygotominor.yy392 = 0;}
110143 110467           break;
110144 110468         case 29: /* ifnotexists ::= IF NOT EXISTS */
110145 110469         case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
110146 110470         case 70: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==70);
110147 110471         case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
110148 110472         case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
110149         -      case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
110150 110473         case 222: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==222);
110151 110474         case 225: /* in_op ::= NOT IN */ yytestcase(yyruleno==225);
110152 110475   {yygotominor.yy392 = 1;}
110153 110476           break;
110154 110477         case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
110155 110478   {
110156 110479     sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
................................................................................
110382 110705   }
110383 110706           break;
110384 110707         case 116: /* multiselect_op ::= UNION ALL */
110385 110708   {yygotominor.yy392 = TK_ALL;}
110386 110709           break;
110387 110710         case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
110388 110711   {
110389         -  yygotominor.yy159 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy442,yymsp[-5].minor.yy347,yymsp[-4].minor.yy122,yymsp[-3].minor.yy442,yymsp[-2].minor.yy122,yymsp[-1].minor.yy442,yymsp[-7].minor.yy392,yymsp[0].minor.yy64.pLimit,yymsp[0].minor.yy64.pOffset);
       110712  +  yygotominor.yy159 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy442,yymsp[-5].minor.yy347,yymsp[-4].minor.yy122,yymsp[-3].minor.yy442,yymsp[-2].minor.yy122,yymsp[-1].minor.yy442,yymsp[-7].minor.yy305,yymsp[0].minor.yy64.pLimit,yymsp[0].minor.yy64.pOffset);
110390 110713   }
       110714  +        break;
       110715  +      case 119: /* distinct ::= DISTINCT */
       110716  +{yygotominor.yy305 = SF_Distinct;}
       110717  +        break;
       110718  +      case 120: /* distinct ::= ALL */
       110719  +      case 121: /* distinct ::= */ yytestcase(yyruleno==121);
       110720  +{yygotominor.yy305 = 0;}
110391 110721           break;
110392 110722         case 122: /* sclp ::= selcollist COMMA */
110393 110723         case 246: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==246);
110394 110724   {yygotominor.yy442 = yymsp[-1].minor.yy442;}
110395 110725           break;
110396 110726         case 123: /* sclp ::= */
110397 110727         case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
................................................................................
110453 110783       yygotominor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy159,yymsp[-1].minor.yy122,yymsp[0].minor.yy180);
110454 110784     }
110455 110785           break;
110456 110786         case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
110457 110787   {
110458 110788       if( yymsp[-6].minor.yy347==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy122==0 && yymsp[0].minor.yy180==0 ){
110459 110789         yygotominor.yy347 = yymsp[-4].minor.yy347;
       110790  +    }else if( yymsp[-4].minor.yy347->nSrc==1 ){
       110791  +      yygotominor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy122,yymsp[0].minor.yy180);
       110792  +      if( yygotominor.yy347 ){
       110793  +        struct SrcList_item *pNew = &yygotominor.yy347->a[yygotominor.yy347->nSrc-1];
       110794  +        struct SrcList_item *pOld = yymsp[-4].minor.yy347->a;
       110795  +        pNew->zName = pOld->zName;
       110796  +        pNew->zDatabase = pOld->zDatabase;
       110797  +        pOld->zName = pOld->zDatabase = 0;
       110798  +      }
       110799  +      sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy347);
110460 110800       }else{
110461 110801         Select *pSubquery;
110462 110802         sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy347);
110463         -      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy347,0,0,0,0,0,0,0);
       110803  +      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy347,0,0,0,0,SF_NestedFrom,0,0);
110464 110804         yygotominor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy122,yymsp[0].minor.yy180);
110465 110805       }
110466 110806     }
110467 110807           break;
110468 110808         case 137: /* dbnm ::= */
110469 110809         case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
110470 110810   {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
................................................................................
110689 111029         case 196: /* expr ::= ID LP distinct exprlist RP */
110690 111030   {
110691 111031     if( yymsp[-1].minor.yy442 && yymsp[-1].minor.yy442->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
110692 111032       sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
110693 111033     }
110694 111034     yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy442, &yymsp[-4].minor.yy0);
110695 111035     spanSet(&yygotominor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
110696         -  if( yymsp[-2].minor.yy392 && yygotominor.yy342.pExpr ){
       111036  +  if( yymsp[-2].minor.yy305 && yygotominor.yy342.pExpr ){
110697 111037       yygotominor.yy342.pExpr->flags |= EP_Distinct;
110698 111038     }
110699 111039   }
110700 111040           break;
110701 111041         case 197: /* expr ::= ID LP STAR RP */
110702 111042   {
110703 111043     yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
................................................................................
136336 136676   ** This ensures that each node is stored on a single database page. If the 
136337 136677   ** database page-size is so large that more than RTREE_MAXCELLS entries 
136338 136678   ** would fit in a single node, use a smaller node-size.
136339 136679   */
136340 136680   static int getNodeSize(
136341 136681     sqlite3 *db,                    /* Database handle */
136342 136682     Rtree *pRtree,                  /* Rtree handle */
136343         -  int isCreate                    /* True for xCreate, false for xConnect */
       136683  +  int isCreate,                   /* True for xCreate, false for xConnect */
       136684  +  char **pzErr                    /* OUT: Error message, if any */
136344 136685   ){
136345 136686     int rc;
136346 136687     char *zSql;
136347 136688     if( isCreate ){
136348 136689       int iPageSize = 0;
136349 136690       zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
136350 136691       rc = getIntFromStmt(db, zSql, &iPageSize);
136351 136692       if( rc==SQLITE_OK ){
136352 136693         pRtree->iNodeSize = iPageSize-64;
136353 136694         if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
136354 136695           pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
136355 136696         }
       136697  +    }else{
       136698  +      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
136356 136699       }
136357 136700     }else{
136358 136701       zSql = sqlite3_mprintf(
136359 136702           "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
136360 136703           pRtree->zDb, pRtree->zName
136361 136704       );
136362 136705       rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
       136706  +    if( rc!=SQLITE_OK ){
       136707  +      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
       136708  +    }
136363 136709     }
136364 136710   
136365 136711     sqlite3_free(zSql);
136366 136712     return rc;
136367 136713   }
136368 136714   
136369 136715   /* 
................................................................................
136419 136765     pRtree->nDim = (argc-4)/2;
136420 136766     pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
136421 136767     pRtree->eCoordType = eCoordType;
136422 136768     memcpy(pRtree->zDb, argv[1], nDb);
136423 136769     memcpy(pRtree->zName, argv[2], nName);
136424 136770   
136425 136771     /* Figure out the node size to use. */
136426         -  rc = getNodeSize(db, pRtree, isCreate);
       136772  +  rc = getNodeSize(db, pRtree, isCreate, pzErr);
136427 136773   
136428 136774     /* Create/Connect to the underlying relational database schema. If
136429 136775     ** that is successful, call sqlite3_declare_vtab() to configure
136430 136776     ** the r-tree table schema.
136431 136777     */
136432 136778     if( rc==SQLITE_OK ){
136433 136779       if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){

Changes to SQLite.Interop/src/core/sqlite3.h.

   103    103   ** string contains the date and time of the check-in (UTC) and an SHA1
   104    104   ** hash of the entire source tree.
   105    105   **
   106    106   ** See also: [sqlite3_libversion()],
   107    107   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   108    108   ** [sqlite_version()] and [sqlite_source_id()].
   109    109   */
   110         -#define SQLITE_VERSION        "3.7.15.2"
   111         -#define SQLITE_VERSION_NUMBER 3007015
   112         -#define SQLITE_SOURCE_ID      "2013-01-09 11:53:05 c0e09560d26f0a6456be9dd3447f5311eb4f238f"
          110  +#define SQLITE_VERSION        "3.7.16"
          111  +#define SQLITE_VERSION_NUMBER 3007016
          112  +#define SQLITE_SOURCE_ID      "2013-01-17 03:18:38 652233d646236d3fbca629813b20d075f00f3ed6"
   113    113   
   114    114   /*
   115    115   ** CAPI3REF: Run-Time Library Version Numbers
   116    116   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   117    117   **
   118    118   ** These interfaces provide the same information as the [SQLITE_VERSION],
   119    119   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros