System.Data.SQLite
Check-in [4295b942f1]
Not logged in

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

Overview
Comment:Update SQLite core to the latest trunk code. Also, update Eagle script library in externals to the latest.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4295b942f10af63e3f06215c939f99ebefae906a
User & Date: mistachkin 2012-08-17 11:55:06
Context
2012-08-24
10:00
Add support for testing the sqlite3_win32_set_directory function. Also, add the ToFullPath connection string property. check-in: 7e3aa2f8bb user: mistachkin tags: trunk
2012-08-17
11:55
Update SQLite core to the latest trunk code. Also, update Eagle script library in externals to the latest. check-in: 4295b942f1 user: mistachkin tags: trunk
2012-07-27
00:52
Modify release archive verification tool to use environment variables that do not interfere with the operation of the called tools themselves. check-in: 4dedf50acc user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Externals/Eagle/lib/Test1.0/constraints.eagle.

    56     56           #
    57     57           foreach constraint $constraints {
    58     58             addConstraint $constraint
    59     59           }
    60     60         }
    61     61       }
    62     62     }
           63  +
           64  +  proc checkForVariable { channel name {notEmpty true} {constraint ""} } {
           65  +    tputs $channel [appendArgs "---- checking for variable \"" $name \
           66  +        "\"... "]
           67  +
           68  +    #
           69  +    # NOTE: First, normalize the variable name to be in the global scope.
           70  +    #
           71  +    set newName [appendArgs :: [string trimleft $name :]]
           72  +
           73  +    #
           74  +    # NOTE: Next, always check if it actually exists (as of right now).
           75  +    #
           76  +    if {[info exists $newName]} then {
           77  +      #
           78  +      # NOTE: Next, optionally check if it constains anything.
           79  +      #
           80  +      if {!$notEmpty || [string length [set $newName]] > 0} then {
           81  +        #
           82  +        # NOTE: The variable exists and it either contains something
           83  +        #       or we do not care about its contents.
           84  +        #
           85  +        if {[string length $constraint] > 0} then {
           86  +          addConstraint [appendArgs variable_ $constraint]
           87  +        } else {
           88  +          addConstraint [appendArgs variable_ [string trimleft $newName :]]
           89  +        }
           90  +
           91  +        #
           92  +        # NOTE: Show that we found the variable and whether it actually
           93  +        #       contained anything.
           94  +        #
           95  +        tputs $channel [appendArgs "yes (" \
           96  +            [expr {$notEmpty ? "exists, not empty" : "exists"}] ")\n"]
           97  +
           98  +        #
           99  +        # NOTE: We are done here, return now.
          100  +        #
          101  +        return
          102  +      }
          103  +    }
          104  +
          105  +    tputs $channel no\n
          106  +  }
    63    107   
    64    108     proc checkForTclOptions { channel } {
    65    109       tputs $channel "---- checking for Tcl options..."
    66    110   
    67    111       if {![isEagle]} then {
    68    112         set result [list]
    69    113   
................................................................................
   137    181   
   138    182           #
   139    183           # NOTE: We are done here, return now.
   140    184           #
   141    185           return
   142    186         }
   143    187       }
          188  +
          189  +    tputs $channel no\n
          190  +  }
          191  +
          192  +  proc checkForFossil { channel } {
          193  +    tputs $channel "---- checking for Fossil... "
          194  +
          195  +    if {[catch {set version [exec -- fossil version]}] == 0} then {
          196  +      set version [string trim $version]
          197  +      set pattern {^This is fossil version (.*) \[([0-9a-f]+)\]\
          198  +          \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} UTC$}
          199  +
          200  +      if {[regexp -- $pattern $version dummy version sourceId]} then {
          201  +        #
          202  +        # NOTE: Add a constraint to show that the Fossil executable
          203  +        #       itself is available.
          204  +        #
          205  +        addConstraint fossil_version
          206  +
          207  +        #
          208  +        # NOTE: Append the version of Fossil currently in use.
          209  +        #
          210  +        append result version " " $version " \[" $sourceId \]
          211  +
          212  +        if {[catch {set remote [exec -- fossil remote]}] == 0} then {
          213  +          set remote [string trim $remote]; set valid false
          214  +
          215  +          if {[isEagle]} then {
          216  +            #
          217  +            # NOTE: With Eagle, we can actually validate the URI.
          218  +            #
          219  +            if {[uri isvalid $remote]} then {
          220  +              set valid true
          221  +            }
          222  +          } else {
          223  +            #
          224  +            # HACK: Currently, there is no simple way to validate
          225  +            #       an arbitrary URI with Tcl (i.e. without using
          226  +            #       some hideously complex regular expression).
          227  +            #
          228  +            if {[string length $remote] > 0} then {
          229  +              set valid true
          230  +            }
          231  +          }
          232  +
          233  +          if {$valid} then {
          234  +            #
          235  +            # NOTE: Add a constraint to show that a valid Fossil
          236  +            #       repository URI appears to be available.
          237  +            #
          238  +            addConstraint fossil_repository
          239  +
          240  +            #
          241  +            # NOTE: If we are not prevented from doing so, save the
          242  +            #       test repository to the repository URI currently
          243  +            #       in use to a suitably named global variable.
          244  +            #
          245  +            if {![info exists ::no(setRepository)]} then {
          246  +              set ::test_repository $remote
          247  +            }
          248  +
          249  +            #
          250  +            # NOTE: Append the repository URI currently in use.
          251  +            #
          252  +            append result ", repository \"" $remote \"
          253  +
          254  +            #
          255  +            # NOTE: Show the result of the checking.
          256  +            #
          257  +            tputs $channel [appendArgs "yes (" $result ")\n"]
          258  +
          259  +            #
          260  +            # NOTE: We are done here, return now.
          261  +            #
          262  +            return
          263  +          }
          264  +        }
          265  +      }
          266  +    }
   144    267   
   145    268       tputs $channel no\n
   146    269     }
   147    270   
   148    271     proc checkForEagle { channel } {
   149    272       tputs $channel "---- checking for Eagle... "
   150    273   
................................................................................
  1995   2118       exportAndImportPackageCommands [namespace current] [list checkForPlatform \
  1996   2119           checkForEagle checkForGaruda checkForShell checkForDebug checkForTk \
  1997   2120           checkForVersion checkForCommand checkForFile checkForNativeCode \
  1998   2121           checkForTip127 checkForTip194 checkForTip241 checkForTip285 \
  1999   2122           checkForPerformance checkForTiming checkForInteractive checkForSymbols \
  2000   2123           checkForLogFile checkForNetwork checkForCompileOption \
  2001   2124           checkForWindowsCommandProcessor checkForUserInteraction \
  2002         -        checkForTclOptions checkForTestConfiguration] false false
         2125  +        checkForTclOptions checkForTestConfiguration checkForVariable \
         2126  +        checkForFossil] false false
  2003   2127   
  2004   2128       ###########################################################################
  2005   2129       ############################## END Tcl ONLY ###############################
  2006   2130       ###########################################################################
  2007   2131     }
  2008   2132   
  2009   2133     #
  2010   2134     # NOTE: Provide the Eagle test constraints package to the interpreter.
  2011   2135     #
  2012   2136     package provide Eagle.Test.Constraints \
  2013   2137       [expr {[isEagle] ? [info engine PatchLevel] : "1.0"}]
  2014   2138   }
  2015   2139   

Changes to Externals/Eagle/lib/Test1.0/prologue.eagle.

  1804   1804     if {![info exists no(tclOptions)]} then {
  1805   1805       checkForTclOptions $test_channel
  1806   1806     }
  1807   1807   
  1808   1808     if {![info exists no(windowsCommandProcessor)]} then {
  1809   1809       checkForWindowsCommandProcessor $test_channel cmd.exe
  1810   1810     }
         1811  +
         1812  +  if {![info exists no(fossil)]} then {
         1813  +    checkForFossil $test_channel
         1814  +  }
         1815  +
         1816  +  if {![info exists no(testRepository)]} then {
         1817  +    checkForVariable $test_channel test_repository
         1818  +  }
  1811   1819   
  1812   1820     if {![info exists no(version)]} then {
  1813   1821       checkForVersion $test_channel
  1814   1822     }
  1815   1823   
  1816   1824     if {![info exists no(eagle)]} then {
  1817   1825       checkForEagle $test_channel

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

   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    676   #define SQLITE_VERSION        "3.7.14"
   677    677   #define SQLITE_VERSION_NUMBER 3007014
   678         -#define SQLITE_SOURCE_ID      "2012-07-17 14:37:12 e4163596339c2166f9c4356ab824fff8bda8d0b0"
          678  +#define SQLITE_SOURCE_ID      "2012-08-17 11:47:32 61b1ae121782525b37882a559d0fbb7c85237fd5"
   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
................................................................................
  4719   4719   ** they return.  Hence, the calling function can deallocate or
  4720   4720   ** modify the text after they return without harm.
  4721   4721   ** ^The sqlite3_result_error_code() function changes the error code
  4722   4722   ** returned by SQLite as a result of an error in a function.  ^By default,
  4723   4723   ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
  4724   4724   ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  4725   4725   **
  4726         -** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
  4727         -** indicating that a string or BLOB is too long to represent.
         4726  +** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
         4727  +** error indicating that a string or BLOB is too long to represent.
  4728   4728   **
  4729         -** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
  4730         -** indicating that a memory allocation failed.
         4729  +** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
         4730  +** error indicating that a memory allocation failed.
  4731   4731   **
  4732   4732   ** ^The sqlite3_result_int() interface sets the return value
  4733   4733   ** of the application-defined function to be the 32-bit signed integer
  4734   4734   ** value given in the 2nd argument.
  4735   4735   ** ^The sqlite3_result_int64() interface sets the return value
  4736   4736   ** of the application-defined function to be the 64-bit signed integer
  4737   4737   ** value given in the 2nd argument.
................................................................................
  8395   8395   #define BTREE_FILE_FORMAT         2
  8396   8396   #define BTREE_DEFAULT_CACHE_SIZE  3
  8397   8397   #define BTREE_LARGEST_ROOT_PAGE   4
  8398   8398   #define BTREE_TEXT_ENCODING       5
  8399   8399   #define BTREE_USER_VERSION        6
  8400   8400   #define BTREE_INCR_VACUUM         7
  8401   8401   
         8402  +/*
         8403  +** Values that may be OR'd together to form the second argument of an
         8404  +** sqlite3BtreeCursorHints() call.
         8405  +*/
         8406  +#define BTREE_BULKLOAD 0x00000001
         8407  +
  8402   8408   SQLITE_PRIVATE int sqlite3BtreeCursor(
  8403   8409     Btree*,                              /* BTree containing table to open */
  8404   8410     int iTable,                          /* Index of root page */
  8405   8411     int wrFlag,                          /* 1 for writing.  0 for read-only */
  8406   8412     struct KeyInfo*,                     /* First argument to compare function */
  8407   8413     BtCursor *pCursor                    /* Space to write cursor structure */
  8408   8414   );
................................................................................
  8438   8444   
  8439   8445   SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  8440   8446   SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  8441   8447   
  8442   8448   SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  8443   8449   SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  8444   8450   SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  8445         -
  8446   8451   SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
         8452  +SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
  8447   8453   
  8448   8454   #ifndef NDEBUG
  8449   8455   SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  8450   8456   #endif
  8451   8457   
  8452   8458   #ifndef SQLITE_OMIT_BTREECOUNT
  8453   8459   SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
................................................................................
  9382   9388   #if defined(_WIN32_WCE)
  9383   9389   # define SQLITE_OS_WINCE 1
  9384   9390   #else
  9385   9391   # define SQLITE_OS_WINCE 0
  9386   9392   #endif
  9387   9393   
  9388   9394   /*
  9389         -** Determine if we are dealing with WindowsRT (Metro) as this has a different and
  9390         -** incompatible API from win32.
         9395  +** Determine if we are dealing with WinRT, which provides only a subset of
         9396  +** the full Win32 API.
  9391   9397   */
  9392   9398   #if !defined(SQLITE_OS_WINRT)
  9393   9399   # define SQLITE_OS_WINRT 0
  9394   9400   #endif
  9395   9401   
  9396   9402   /*
  9397   9403   ** When compiled for WinCE or WinRT, there is no concept of the current
................................................................................
 11083  11089   /*
 11084  11090   ** A structure used to customize the behavior of sqlite3Select(). See
 11085  11091   ** comments above sqlite3Select() for details.
 11086  11092   */
 11087  11093   typedef struct SelectDest SelectDest;
 11088  11094   struct SelectDest {
 11089  11095     u8 eDest;         /* How to dispose of the results */
 11090         -  u8 affinity;      /* Affinity used when eDest==SRT_Set */
 11091         -  int iParm;        /* A parameter used by the eDest disposal method */
 11092         -  int iMem;         /* Base register where results are written */
 11093         -  int nMem;         /* Number of registers allocated */
        11096  +  u8 affSdst;       /* Affinity used when eDest==SRT_Set */
        11097  +  int iSDParm;      /* A parameter used by the eDest disposal method */
        11098  +  int iSdst;        /* Base register where results are written */
        11099  +  int nSdst;        /* Number of registers allocated */
 11094  11100   };
 11095  11101   
 11096  11102   /*
 11097  11103   ** During code generation of statements that do inserts into AUTOINCREMENT 
 11098  11104   ** tables, the following information is attached to the Table.u.autoInc.p
 11099  11105   ** pointer of each autoincrement table to record some side information that
 11100  11106   ** the code generator needs.  We have to keep per-table autoincrement
................................................................................
 11282  11288   #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
 11283  11289   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 11284  11290   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 11285  11291   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 11286  11292   #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
 11287  11293   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 11288  11294   #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
        11295  +#define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
        11296  +#define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
 11289  11297   
 11290  11298   /*
 11291  11299    * Each trigger present in the database schema is stored as an instance of
 11292  11300    * struct Trigger. 
 11293  11301    *
 11294  11302    * Pointers to instances of struct Trigger are stored in two ways.
 11295  11303    * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
................................................................................
 13352  13360   # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
 13353  13361   # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
 13354  13362   # define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
 13355  13363   # define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
 13356  13364   #else
 13357  13365   SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
 13358  13366   SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 13359         -SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
 13360         -SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
 13361         -SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
 13362         -SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
 13363         -SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
        13367  +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
        13368  +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
        13369  +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
        13370  +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
        13371  +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int *);
 13364  13372   #endif
 13365  13373   
 13366  13374   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 13367  13375   SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
 13368  13376   SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
 13369  13377   #else
 13370  13378   # define sqlite3VdbeEnter(X)
................................................................................
 22183  22191       new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
 22184  22192     }
 22185  22193     if( new_size==pH->htsize ) return 0;
 22186  22194   #endif
 22187  22195   
 22188  22196     /* The inability to allocates space for a larger hash table is
 22189  22197     ** a performance hit but it is not a fatal error.  So mark the
 22190         -  ** allocation as a benign.
        22198  +  ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of 
        22199  +  ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
        22200  +  ** only zeroes the requested number of bytes whereas this module will
        22201  +  ** use the actual amount of space allocated for the hash table (which
        22202  +  ** may be larger than the requested amount).
 22191  22203     */
 22192  22204     sqlite3BeginBenignMalloc();
 22193  22205     new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
 22194  22206     sqlite3EndBenignMalloc();
 22195  22207   
 22196  22208     if( new_ht==0 ) return 0;
 22197  22209     sqlite3_free(pH->ht);
................................................................................
 29865  29877   # define FILE_FLAG_MASK          (0xFF3C0000)
 29866  29878   #endif
 29867  29879   
 29868  29880   #ifndef FILE_ATTRIBUTE_MASK
 29869  29881   # define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
 29870  29882   #endif
 29871  29883   
        29884  +#ifndef SQLITE_OMIT_WAL
 29872  29885   /* Forward references */
 29873  29886   typedef struct winShm winShm;           /* A connection to shared-memory */
 29874  29887   typedef struct winShmNode winShmNode;   /* A region of shared-memory */
        29888  +#endif
 29875  29889   
 29876  29890   /*
 29877  29891   ** WinCE lacks native support for file locking so we have to fake it
 29878  29892   ** with some code of our own.
 29879  29893   */
 29880  29894   #if SQLITE_OS_WINCE
 29881  29895   typedef struct winceLock {
................................................................................
 29895  29909     const sqlite3_io_methods *pMethod; /*** Must be first ***/
 29896  29910     sqlite3_vfs *pVfs;      /* The VFS used to open this file */
 29897  29911     HANDLE h;               /* Handle for accessing the file */
 29898  29912     u8 locktype;            /* Type of lock currently held on this file */
 29899  29913     short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
 29900  29914     u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
 29901  29915     DWORD lastErrno;        /* The Windows errno from the last I/O error */
        29916  +#ifndef SQLITE_OMIT_WAL
 29902  29917     winShm *pShm;           /* Instance of shared memory on this file */
        29918  +#endif
 29903  29919     const char *zPath;      /* Full pathname of this file */
 29904  29920     int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
 29905  29921   #if SQLITE_OS_WINCE
 29906  29922     LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
 29907  29923     HANDLE hMutex;          /* Mutex used to control access to shared lock */  
 29908  29924     HANDLE hShared;         /* Shared memory segment used for locking */
 29909  29925     winceLock local;        /* Locks obtained by this instance of winFile */
................................................................................
 30107  30123   #else
 30108  30124     { "CreateFileW",             (SYSCALL)0,                       0 },
 30109  30125   #endif
 30110  30126   
 30111  30127   #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
 30112  30128           LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
 30113  30129   
 30114         -#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
        30130  +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
        30131  +        !defined(SQLITE_OMIT_WAL))
 30115  30132     { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
 30116  30133   #else
 30117  30134     { "CreateFileMappingW",      (SYSCALL)0,                       0 },
 30118  30135   #endif
 30119  30136   
 30120  30137   #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
 30121  30138           DWORD,DWORD,DWORD,LPCWSTR))aSyscall[6].pCurrent)
................................................................................
 30419  30436   #endif
 30420  30437   
 30421  30438   #ifndef osLockFileEx
 30422  30439   #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
 30423  30440           LPOVERLAPPED))aSyscall[45].pCurrent)
 30424  30441   #endif
 30425  30442   
 30426         -#if !SQLITE_OS_WINRT
        30443  +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
 30427  30444     { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
 30428  30445   #else
 30429  30446     { "MapViewOfFile",           (SYSCALL)0,                       0 },
 30430  30447   #endif
 30431  30448   
 30432  30449   #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 30433  30450           SIZE_T))aSyscall[46].pCurrent)
................................................................................
 30489  30506   #else
 30490  30507     { "UnlockFileEx",            (SYSCALL)0,                       0 },
 30491  30508   #endif
 30492  30509   
 30493  30510   #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 30494  30511           LPOVERLAPPED))aSyscall[55].pCurrent)
 30495  30512   
        30513  +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
 30496  30514     { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
        30515  +#else
        30516  +  { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
        30517  +#endif
 30497  30518   
 30498  30519   #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[56].pCurrent)
 30499  30520   
 30500  30521     { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
 30501  30522   
 30502  30523   #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
 30503  30524           LPCSTR,LPBOOL))aSyscall[57].pCurrent)
................................................................................
 30521  30542   #else
 30522  30543     { "WaitForSingleObject",     (SYSCALL)0,                       0 },
 30523  30544   #endif
 30524  30545   
 30525  30546   #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
 30526  30547           DWORD))aSyscall[60].pCurrent)
 30527  30548   
 30528         -#if !SQLITE_OS_WINCE
        30549  +#if SQLITE_OS_WINRT
 30529  30550     { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
 30530  30551   #else
 30531  30552     { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
 30532  30553   #endif
 30533  30554   
 30534  30555   #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
 30535  30556           BOOL))aSyscall[61].pCurrent)
 30536  30557   
 30537         -#if !SQLITE_OS_WINCE
        30558  +#if SQLITE_OS_WINRT
 30538  30559     { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
 30539  30560   #else
 30540  30561     { "SetFilePointerEx",        (SYSCALL)0,                       0 },
 30541  30562   #endif
 30542  30563   
 30543  30564   #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
 30544  30565           PLARGE_INTEGER,DWORD))aSyscall[62].pCurrent)
................................................................................
 30548  30569   #else
 30549  30570     { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
 30550  30571   #endif
 30551  30572   
 30552  30573   #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
 30553  30574           FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[63].pCurrent)
 30554  30575   
 30555         -#if SQLITE_OS_WINRT
        30576  +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 30556  30577     { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
 30557  30578   #else
 30558  30579     { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
 30559  30580   #endif
 30560  30581   
 30561  30582   #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
 30562  30583           SIZE_T))aSyscall[64].pCurrent)
................................................................................
 30612  30633   
 30613  30634   #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[70].pCurrent)
 30614  30635   
 30615  30636     { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
 30616  30637   
 30617  30638   #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[71].pCurrent)
 30618  30639   
 30619         -#if SQLITE_OS_WINRT
        30640  +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 30620  30641     { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
 30621  30642   #else
 30622  30643     { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
 30623  30644   #endif
 30624  30645   
 30625  30646   #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
 30626  30647           LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[72].pCurrent)
................................................................................
 31744  31765   */
 31745  31766   #define MX_CLOSE_ATTEMPT 3
 31746  31767   static int winClose(sqlite3_file *id){
 31747  31768     int rc, cnt = 0;
 31748  31769     winFile *pFile = (winFile*)id;
 31749  31770   
 31750  31771     assert( id!=0 );
        31772  +#ifndef SQLITE_OMIT_WAL
 31751  31773     assert( pFile->pShm==0 );
        31774  +#endif
 31752  31775     OSTRACE(("CLOSE %d\n", pFile->h));
 31753  31776     do{
 31754  31777       rc = osCloseHandle(pFile->h);
 31755  31778       /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
 31756  31779     }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
 31757  31780   #if SQLITE_OS_WINCE
 31758  31781   #define WINCE_DELETION_ATTEMPTS 3
................................................................................
 33494  33517     }
 33495  33518   
 33496  33519     memset(pFile, 0, sizeof(*pFile));
 33497  33520     pFile->pMethod = &winIoMethod;
 33498  33521     pFile->h = h;
 33499  33522     pFile->lastErrno = NO_ERROR;
 33500  33523     pFile->pVfs = pVfs;
        33524  +#ifndef SQLITE_OMIT_WAL
 33501  33525     pFile->pShm = 0;
        33526  +#endif
 33502  33527     pFile->zPath = zName;
 33503  33528     if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 33504  33529       pFile->ctrlFlags |= WINFILE_PSOW;
 33505  33530     }
 33506  33531   
 33507  33532   #if SQLITE_OS_WINCE
 33508  33533     if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
................................................................................
 34470  34495     int rc = -1;
 34471  34496     int i, nx, pc, op;
 34472  34497     void *pTmpSpace;
 34473  34498   
 34474  34499     /* Allocate the Bitvec to be tested and a linear array of
 34475  34500     ** bits to act as the reference */
 34476  34501     pBitvec = sqlite3BitvecCreate( sz );
 34477         -  pV = sqlite3_malloc( (sz+7)/8 + 1 );
        34502  +  pV = sqlite3MallocZero( (sz+7)/8 + 1 );
 34478  34503     pTmpSpace = sqlite3_malloc(BITVEC_SZ);
 34479  34504     if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
 34480         -  memset(pV, 0, (sz+7)/8 + 1);
 34481  34505   
 34482  34506     /* NULL pBitvec tests */
 34483  34507     sqlite3BitvecSet(0, 1);
 34484  34508     sqlite3BitvecClear(0, 1, pTmpSpace);
 34485  34509   
 34486  34510     /* Run the program */
 34487  34511     pc = 0;
................................................................................
 35557  35581     nNew = p->nHash*2;
 35558  35582     if( nNew<256 ){
 35559  35583       nNew = 256;
 35560  35584     }
 35561  35585   
 35562  35586     pcache1LeaveMutex(p->pGroup);
 35563  35587     if( p->nHash ){ sqlite3BeginBenignMalloc(); }
 35564         -  apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
        35588  +  apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
 35565  35589     if( p->nHash ){ sqlite3EndBenignMalloc(); }
 35566  35590     pcache1EnterMutex(p->pGroup);
 35567  35591     if( apNew ){
 35568         -    memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
 35569  35592       for(i=0; i<p->nHash; i++){
 35570  35593         PgHdr1 *pPage;
 35571  35594         PgHdr1 *pNext = p->apHash[i];
 35572  35595         while( (pPage = pNext)!=0 ){
 35573  35596           unsigned int h = pPage->iKey % nNew;
 35574  35597           pNext = pPage->pNext;
 35575  35598           pPage->pNext = apNew[h];
................................................................................
 35745  35768     int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
 35746  35769   #endif
 35747  35770   
 35748  35771     assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
 35749  35772     assert( szExtra < 300 );
 35750  35773   
 35751  35774     sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
 35752         -  pCache = (PCache1 *)sqlite3_malloc(sz);
        35775  +  pCache = (PCache1 *)sqlite3MallocZero(sz);
 35753  35776     if( pCache ){
 35754         -    memset(pCache, 0, sz);
 35755  35777       if( separateCache ){
 35756  35778         pGroup = (PGroup*)&pCache[1];
 35757  35779         pGroup->mxPinned = 10;
 35758  35780       }else{
 35759  35781         pGroup = &pcache1.grp;
 35760  35782       }
 35761  35783       pCache->pGroup = pGroup;
................................................................................
 47388  47410     u8 wrFlag;                /* True if writable */
 47389  47411     u8 atLast;                /* Cursor pointing to the last entry */
 47390  47412     u8 validNKey;             /* True if info.nKey is valid */
 47391  47413     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 47392  47414   #ifndef SQLITE_OMIT_INCRBLOB
 47393  47415     u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
 47394  47416   #endif
        47417  +  u8 hints;                             /* As configured by CursorSetHints() */
 47395  47418     i16 iPage;                            /* Index of current page in apPage */
 47396  47419     u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
 47397  47420     MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
 47398  47421   };
 47399  47422   
 47400  47423   /*
 47401  47424   ** Potential values for BtCursor.eState.
................................................................................
 53737  53760   ** If aOvflSpace is set to a null pointer, this function returns 
 53738  53761   ** SQLITE_NOMEM.
 53739  53762   */
 53740  53763   static int balance_nonroot(
 53741  53764     MemPage *pParent,               /* Parent page of siblings being balanced */
 53742  53765     int iParentIdx,                 /* Index of "the page" in pParent */
 53743  53766     u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
 53744         -  int isRoot                      /* True if pParent is a root-page */
        53767  +  int isRoot,                     /* True if pParent is a root-page */
        53768  +  int bBulk                       /* True if this call is part of a bulk load */
 53745  53769   ){
 53746  53770     BtShared *pBt;               /* The whole database */
 53747  53771     int nCell = 0;               /* Number of cells in apCell[] */
 53748  53772     int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
 53749  53773     int nNew = 0;                /* Number of pages in apNew[] */
 53750  53774     int nOld;                    /* Number of pages in apOld[] */
 53751  53775     int i, j, k;                 /* Loop counters */
................................................................................
 53801  53825     ** way, the remainder of the function does not have to deal with any
 53802  53826     ** overflow cells in the parent page, since if any existed they will
 53803  53827     ** have already been removed.
 53804  53828     */
 53805  53829     i = pParent->nOverflow + pParent->nCell;
 53806  53830     if( i<2 ){
 53807  53831       nxDiv = 0;
 53808         -    nOld = i+1;
 53809  53832     }else{
 53810         -    nOld = 3;
        53833  +    assert( bBulk==0 || bBulk==1 );
 53811  53834       if( iParentIdx==0 ){                 
 53812  53835         nxDiv = 0;
 53813  53836       }else if( iParentIdx==i ){
 53814         -      nxDiv = i-2;
        53837  +      nxDiv = i-2+bBulk;
 53815  53838       }else{
        53839  +      assert( bBulk==0 );
 53816  53840         nxDiv = iParentIdx-1;
 53817  53841       }
 53818         -    i = 2;
        53842  +    i = 2-bBulk;
 53819  53843     }
        53844  +  nOld = i+1;
 53820  53845     if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
 53821  53846       pRight = &pParent->aData[pParent->hdrOffset+8];
 53822  53847     }else{
 53823  53848       pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
 53824  53849     }
 53825  53850     pgno = get4byte(pRight);
 53826  53851     while( 1 ){
................................................................................
 54021  54046       int r;              /* Index of right-most cell in left sibling */
 54022  54047       int d;              /* Index of first cell to the left of right sibling */
 54023  54048   
 54024  54049       r = cntNew[i-1] - 1;
 54025  54050       d = r + 1 - leafData;
 54026  54051       assert( d<nMaxCells );
 54027  54052       assert( r<nMaxCells );
 54028         -    while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
        54053  +    while( szRight==0 
        54054  +       || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
        54055  +    ){
 54029  54056         szRight += szCell[d] + 2;
 54030  54057         szLeft -= szCell[r] + 2;
 54031  54058         cntNew[i-1]--;
 54032  54059         r = cntNew[i-1] - 1;
 54033  54060         d = r + 1 - leafData;
 54034  54061       }
 54035  54062       szNew[i] = szRight;
................................................................................
 54068  54095         pNew = apNew[i] = apOld[i];
 54069  54096         apOld[i] = 0;
 54070  54097         rc = sqlite3PagerWrite(pNew->pDbPage);
 54071  54098         nNew++;
 54072  54099         if( rc ) goto balance_cleanup;
 54073  54100       }else{
 54074  54101         assert( i>0 );
 54075         -      rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
        54102  +      rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
 54076  54103         if( rc ) goto balance_cleanup;
 54077  54104         apNew[i] = pNew;
 54078  54105         nNew++;
 54079  54106   
 54080  54107         /* Set the pointer-map entry for the new sibling page. */
 54081  54108         if( ISAUTOVACUUM ){
 54082  54109           ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
................................................................................
 54280  54307       for(i=0; i<nCell; i++){
 54281  54308         int isDivider = 0;
 54282  54309         while( i==iNextOld ){
 54283  54310           /* Cell i is the cell immediately following the last cell on old
 54284  54311           ** sibling page j. If the siblings are not leaf pages of an
 54285  54312           ** intkey b-tree, then cell i was a divider cell. */
 54286  54313           assert( j+1 < ArraySize(apCopy) );
        54314  +        assert( j+1 < nOld );
 54287  54315           pOld = apCopy[++j];
 54288  54316           iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
 54289  54317           if( pOld->nOverflow ){
 54290  54318             nOverflow = pOld->nOverflow;
 54291  54319             iOverflow = i + !leafData + pOld->aiOvfl[0];
 54292  54320           }
 54293  54321           isDivider = !leafData;  
................................................................................
 54518  54546             ** different page). Once this subsequent call to balance_nonroot() 
 54519  54547             ** has completed, it is safe to release the pSpace buffer used by
 54520  54548             ** the previous call, as the overflow cell data will have been 
 54521  54549             ** copied either into the body of a database page or into the new
 54522  54550             ** pSpace buffer passed to the latter call to balance_nonroot().
 54523  54551             */
 54524  54552             u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
 54525         -          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
        54553  +          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
 54526  54554             if( pFree ){
 54527  54555               /* If pFree is not NULL, it points to the pSpace buffer used 
 54528  54556               ** by a previous call to balance_nonroot(). Its contents are
 54529  54557               ** now stored either on real database pages or within the 
 54530  54558               ** new pSpace buffer, so it may be safely freed here. */
 54531  54559               sqlite3PageFree(pFree);
 54532  54560             }
................................................................................
 56105  56133         }
 56106  56134       }
 56107  56135     }
 56108  56136   
 56109  56137     pBt->btsFlags &= ~BTS_NO_WAL;
 56110  56138     return rc;
 56111  56139   }
        56140  +
        56141  +/*
        56142  +** set the mask of hint flags for cursor pCsr. Currently the only valid
        56143  +** values are 0 and BTREE_BULKLOAD.
        56144  +*/
        56145  +SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
        56146  +  assert( mask==BTREE_BULKLOAD || mask==0 );
        56147  +  pCsr->hints = mask;
        56148  +}
 56112  56149   
 56113  56150   /************** End of btree.c ***********************************************/
 56114  56151   /************** Begin file backup.c ******************************************/
 56115  56152   /*
 56116  56153   ** 2009 January 28
 56117  56154   **
 56118  56155   ** The author disclaims copyright to this source code.  In place of
................................................................................
 56272  56309       );
 56273  56310       p = 0;
 56274  56311     }else {
 56275  56312       /* Allocate space for a new sqlite3_backup object...
 56276  56313       ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 56277  56314       ** call to sqlite3_backup_init() and is destroyed by a call to
 56278  56315       ** sqlite3_backup_finish(). */
 56279         -    p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
        56316  +    p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
 56280  56317       if( !p ){
 56281  56318         sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
 56282  56319       }
 56283  56320     }
 56284  56321   
 56285  56322     /* If the allocation succeeded, populate the new object. */
 56286  56323     if( p ){
 56287         -    memset(p, 0, sizeof(sqlite3_backup));
 56288  56324       p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
 56289  56325       p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
 56290  56326       p->pDestDb = pDestDb;
 56291  56327       p->pSrcDb = pSrcDb;
 56292  56328       p->iNext = 1;
 56293  56329       p->isAttached = 0;
 56294  56330   
................................................................................
 62704  62740   /*
 62705  62741   ** Allocate a new Explain object
 62706  62742   */
 62707  62743   SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
 62708  62744     if( pVdbe ){
 62709  62745       Explain *p;
 62710  62746       sqlite3BeginBenignMalloc();
 62711         -    p = sqlite3_malloc( sizeof(Explain) );
        62747  +    p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
 62712  62748       if( p ){
 62713         -      memset(p, 0, sizeof(*p));
 62714  62749         p->pVdbe = pVdbe;
 62715  62750         sqlite3_free(pVdbe->pExplain);
 62716  62751         pVdbe->pExplain = p;
 62717  62752         sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
 62718  62753                             SQLITE_MAX_LENGTH);
 62719  62754         p->str.useMalloc = 2;
 62720  62755       }else{
................................................................................
 66487  66522     int iDb;
 66488  66523     int wrFlag;
 66489  66524     Btree *pX;
 66490  66525     VdbeCursor *pCur;
 66491  66526     Db *pDb;
 66492  66527   #endif /* local variables moved into u.ax */
 66493  66528   
        66529  +  assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
        66530  +  assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
        66531  +
 66494  66532     if( p->expired ){
 66495  66533       rc = SQLITE_ABORT;
 66496  66534       break;
 66497  66535     }
 66498  66536   
 66499  66537     u.ax.nField = 0;
 66500  66538     u.ax.pKeyInfo = 0;
................................................................................
 66510  66548       assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
 66511  66549       if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
 66512  66550         p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
 66513  66551       }
 66514  66552     }else{
 66515  66553       u.ax.wrFlag = 0;
 66516  66554     }
 66517         -  if( pOp->p5 ){
        66555  +  if( pOp->p5 & OPFLAG_P2ISREG ){
 66518  66556       assert( u.ax.p2>0 );
 66519  66557       assert( u.ax.p2<=p->nMem );
 66520  66558       pIn2 = &aMem[u.ax.p2];
 66521  66559       assert( memIsValid(pIn2) );
 66522  66560       assert( (pIn2->flags & MEM_Int)!=0 );
 66523  66561       sqlite3VdbeMemIntegerify(pIn2);
 66524  66562       u.ax.p2 = (int)pIn2->u.i;
................................................................................
 66541  66579     assert( pOp->p1>=0 );
 66542  66580     u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
 66543  66581     if( u.ax.pCur==0 ) goto no_mem;
 66544  66582     u.ax.pCur->nullRow = 1;
 66545  66583     u.ax.pCur->isOrdered = 1;
 66546  66584     rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
 66547  66585     u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
        66586  +  assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
        66587  +  sqlite3BtreeCursorHints(u.ax.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
 66548  66588   
 66549  66589     /* Since it performs no memory allocation or IO, the only value that
 66550  66590     ** sqlite3BtreeCursor() may return is SQLITE_OK. */
 66551  66591     assert( rc==SQLITE_OK );
 66552  66592   
 66553  66593     /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
 66554  66594     ** SQLite used to check if the root-page flags were sane at this point
................................................................................
 70160  70200   */
 70161  70201   
 70162  70202   
 70163  70203   #ifndef SQLITE_OMIT_MERGE_SORT
 70164  70204   
 70165  70205   typedef struct VdbeSorterIter VdbeSorterIter;
 70166  70206   typedef struct SorterRecord SorterRecord;
        70207  +typedef struct FileWriter FileWriter;
 70167  70208   
 70168  70209   /*
 70169  70210   ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
 70170  70211   **
 70171  70212   ** As keys are added to the sorter, they are written to disk in a series
 70172  70213   ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
 70173  70214   ** the same as the cache-size allowed for temporary databases. In order
................................................................................
 70257  70298     i64 iReadOff;                   /* Current read offset */
 70258  70299     i64 iEof;                       /* 1 byte past EOF for this iterator */
 70259  70300     int nAlloc;                     /* Bytes of space at aAlloc */
 70260  70301     int nKey;                       /* Number of bytes in key */
 70261  70302     sqlite3_file *pFile;            /* File iterator is reading from */
 70262  70303     u8 *aAlloc;                     /* Allocated space */
 70263  70304     u8 *aKey;                       /* Pointer to current key */
        70305  +  u8 *aBuffer;                    /* Current read buffer */
        70306  +  int nBuffer;                    /* Size of read buffer in bytes */
        70307  +};
        70308  +
        70309  +/*
        70310  +** An instance of this structure is used to organize the stream of records
        70311  +** being written to files by the merge-sort code into aligned, page-sized
        70312  +** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
        70313  +** faster on many operating systems.
        70314  +*/
        70315  +struct FileWriter {
        70316  +  int eFWErr;                     /* Non-zero if in an error state */
        70317  +  u8 *aBuffer;                    /* Pointer to write buffer */
        70318  +  int nBuffer;                    /* Size of write buffer in bytes */
        70319  +  int iBufStart;                  /* First byte of buffer to write */
        70320  +  int iBufEnd;                    /* Last byte of buffer to write */
        70321  +  i64 iWriteOff;                  /* Offset of start of buffer in file */
        70322  +  sqlite3_file *pFile;            /* File to write to */
 70264  70323   };
 70265  70324   
 70266  70325   /*
 70267  70326   ** A structure to store a single record. All in-memory records are connected
 70268  70327   ** together into a linked list headed at VdbeSorter.pRecord using the 
 70269  70328   ** SorterRecord.pNext pointer.
 70270  70329   */
................................................................................
 70282  70341   
 70283  70342   /*
 70284  70343   ** Free all memory belonging to the VdbeSorterIter object passed as the second
 70285  70344   ** argument. All structure fields are set to zero before returning.
 70286  70345   */
 70287  70346   static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
 70288  70347     sqlite3DbFree(db, pIter->aAlloc);
        70348  +  sqlite3DbFree(db, pIter->aBuffer);
 70289  70349     memset(pIter, 0, sizeof(VdbeSorterIter));
 70290  70350   }
        70351  +
        70352  +/*
        70353  +** Read nByte bytes of data from the stream of data iterated by object p.
        70354  +** If successful, set *ppOut to point to a buffer containing the data
        70355  +** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
        70356  +** error code.
        70357  +**
        70358  +** The buffer indicated by *ppOut may only be considered valid until the
        70359  +** next call to this function.
        70360  +*/
        70361  +static int vdbeSorterIterRead(
        70362  +  sqlite3 *db,                    /* Database handle (for malloc) */
        70363  +  VdbeSorterIter *p,              /* Iterator */
        70364  +  int nByte,                      /* Bytes of data to read */
        70365  +  u8 **ppOut                      /* OUT: Pointer to buffer containing data */
        70366  +){
        70367  +  int iBuf;                       /* Offset within buffer to read from */
        70368  +  int nAvail;                     /* Bytes of data available in buffer */
        70369  +  assert( p->aBuffer );
        70370  +
        70371  +  /* If there is no more data to be read from the buffer, read the next 
        70372  +  ** p->nBuffer bytes of data from the file into it. Or, if there are less
        70373  +  ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
        70374  +  iBuf = p->iReadOff % p->nBuffer;
        70375  +  if( iBuf==0 ){
        70376  +    int nRead;                    /* Bytes to read from disk */
        70377  +    int rc;                       /* sqlite3OsRead() return code */
        70378  +
        70379  +    /* Determine how many bytes of data to read. */
        70380  +    nRead = (int)(p->iEof - p->iReadOff);
        70381  +    if( nRead>p->nBuffer ) nRead = p->nBuffer;
        70382  +    assert( nRead>0 );
        70383  +
        70384  +    /* Read data from the file. Return early if an error occurs. */
        70385  +    rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
        70386  +    assert( rc!=SQLITE_IOERR_SHORT_READ );
        70387  +    if( rc!=SQLITE_OK ) return rc;
        70388  +  }
        70389  +  nAvail = p->nBuffer - iBuf; 
        70390  +
        70391  +  if( nByte<=nAvail ){
        70392  +    /* The requested data is available in the in-memory buffer. In this
        70393  +    ** case there is no need to make a copy of the data, just return a 
        70394  +    ** pointer into the buffer to the caller.  */
        70395  +    *ppOut = &p->aBuffer[iBuf];
        70396  +    p->iReadOff += nByte;
        70397  +  }else{
        70398  +    /* The requested data is not all available in the in-memory buffer.
        70399  +    ** In this case, allocate space at p->aAlloc[] to copy the requested
        70400  +    ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
        70401  +    int nRem;                     /* Bytes remaining to copy */
        70402  +
        70403  +    /* Extend the p->aAlloc[] allocation if required. */
        70404  +    if( p->nAlloc<nByte ){
        70405  +      int nNew = p->nAlloc*2;
        70406  +      while( nByte>nNew ) nNew = nNew*2;
        70407  +      p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
        70408  +      if( !p->aAlloc ) return SQLITE_NOMEM;
        70409  +      p->nAlloc = nNew;
        70410  +    }
        70411  +
        70412  +    /* Copy as much data as is available in the buffer into the start of
        70413  +    ** p->aAlloc[].  */
        70414  +    memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
        70415  +    p->iReadOff += nAvail;
        70416  +    nRem = nByte - nAvail;
        70417  +
        70418  +    /* The following loop copies up to p->nBuffer bytes per iteration into
        70419  +    ** the p->aAlloc[] buffer.  */
        70420  +    while( nRem>0 ){
        70421  +      int rc;                     /* vdbeSorterIterRead() return code */
        70422  +      int nCopy;                  /* Number of bytes to copy */
        70423  +      u8 *aNext;                  /* Pointer to buffer to copy data from */
        70424  +
        70425  +      nCopy = nRem;
        70426  +      if( nRem>p->nBuffer ) nCopy = p->nBuffer;
        70427  +      rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
        70428  +      if( rc!=SQLITE_OK ) return rc;
        70429  +      assert( aNext!=p->aAlloc );
        70430  +      memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
        70431  +      nRem -= nCopy;
        70432  +    }
        70433  +
        70434  +    *ppOut = p->aAlloc;
        70435  +  }
        70436  +
        70437  +  return SQLITE_OK;
        70438  +}
        70439  +
        70440  +/*
        70441  +** Read a varint from the stream of data accessed by p. Set *pnOut to
        70442  +** the value read.
        70443  +*/
        70444  +static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
        70445  +  int iBuf;
        70446  +
        70447  +  iBuf = p->iReadOff % p->nBuffer;
        70448  +  if( iBuf && (p->nBuffer-iBuf)>=9 ){
        70449  +    p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
        70450  +  }else{
        70451  +    u8 aVarint[16], *a;
        70452  +    int i = 0, rc;
        70453  +    do{
        70454  +      rc = vdbeSorterIterRead(db, p, 1, &a);
        70455  +      if( rc ) return rc;
        70456  +      aVarint[(i++)&0xf] = a[0];
        70457  +    }while( (a[0]&0x80)!=0 );
        70458  +    sqlite3GetVarint(aVarint, pnOut);
        70459  +  }
        70460  +
        70461  +  return SQLITE_OK;
        70462  +}
        70463  +
 70291  70464   
 70292  70465   /*
 70293  70466   ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
 70294  70467   ** no error occurs, or an SQLite error code if one does.
 70295  70468   */
 70296  70469   static int vdbeSorterIterNext(
 70297  70470     sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
 70298  70471     VdbeSorterIter *pIter           /* Iterator to advance */
 70299  70472   ){
 70300  70473     int rc;                         /* Return Code */
 70301         -  int nRead;                      /* Number of bytes read */
 70302         -  int nRec = 0;                   /* Size of record in bytes */
 70303         -  int iOff = 0;                   /* Size of serialized size varint in bytes */
        70474  +  u64 nRec = 0;                   /* Size of record in bytes */
 70304  70475   
 70305         -  assert( pIter->iEof>=pIter->iReadOff );
 70306         -  if( pIter->iEof-pIter->iReadOff>5 ){
 70307         -    nRead = 5;
 70308         -  }else{
 70309         -    nRead = (int)(pIter->iEof - pIter->iReadOff);
 70310         -  }
 70311         -  if( nRead<=0 ){
        70476  +  if( pIter->iReadOff>=pIter->iEof ){
 70312  70477       /* This is an EOF condition */
 70313  70478       vdbeSorterIterZero(db, pIter);
 70314  70479       return SQLITE_OK;
 70315  70480     }
 70316  70481   
 70317         -  rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
        70482  +  rc = vdbeSorterIterVarint(db, pIter, &nRec);
 70318  70483     if( rc==SQLITE_OK ){
 70319         -    iOff = getVarint32(pIter->aAlloc, nRec);
 70320         -    if( (iOff+nRec)>nRead ){
 70321         -      int nRead2;                   /* Number of extra bytes to read */
 70322         -      if( (iOff+nRec)>pIter->nAlloc ){
 70323         -        int nNew = pIter->nAlloc*2;
 70324         -        while( (iOff+nRec)>nNew ) nNew = nNew*2;
 70325         -        pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
 70326         -        if( !pIter->aAlloc ) return SQLITE_NOMEM;
 70327         -        pIter->nAlloc = nNew;
 70328         -      }
 70329         -  
 70330         -      nRead2 = iOff + nRec - nRead;
 70331         -      rc = sqlite3OsRead(
 70332         -          pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
 70333         -      );
 70334         -    }
 70335         -  }
 70336         -
 70337         -  assert( rc!=SQLITE_OK || nRec>0 );
 70338         -  pIter->iReadOff += iOff+nRec;
 70339         -  pIter->nKey = nRec;
 70340         -  pIter->aKey = &pIter->aAlloc[iOff];
 70341         -  return rc;
 70342         -}
 70343         -
 70344         -/*
 70345         -** Write a single varint, value iVal, to file-descriptor pFile. Return
 70346         -** SQLITE_OK if successful, or an SQLite error code if some error occurs.
 70347         -**
 70348         -** The value of *piOffset when this function is called is used as the byte
 70349         -** offset in file pFile to write to. Before returning, *piOffset is 
 70350         -** incremented by the number of bytes written.
 70351         -*/
 70352         -static int vdbeSorterWriteVarint(
 70353         -  sqlite3_file *pFile,            /* File to write to */
 70354         -  i64 iVal,                       /* Value to write as a varint */
 70355         -  i64 *piOffset                   /* IN/OUT: Write offset in file pFile */
 70356         -){
 70357         -  u8 aVarint[9];                  /* Buffer large enough for a varint */
 70358         -  int nVarint;                    /* Number of used bytes in varint */
 70359         -  int rc;                         /* Result of write() call */
 70360         -
 70361         -  nVarint = sqlite3PutVarint(aVarint, iVal);
 70362         -  rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
 70363         -  *piOffset += nVarint;
 70364         -
 70365         -  return rc;
 70366         -}
 70367         -
 70368         -/*
 70369         -** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
 70370         -** successful, or an SQLite error code if some error occurs.
 70371         -**
 70372         -** The value of *piOffset when this function is called is used as the
 70373         -** byte offset in file pFile from whence to read the varint. If successful
 70374         -** (i.e. if no IO error occurs), then *piOffset is set to the offset of
 70375         -** the first byte past the end of the varint before returning. *piVal is
 70376         -** set to the integer value read. If an error occurs, the final values of
 70377         -** both *piOffset and *piVal are undefined.
 70378         -*/
 70379         -static int vdbeSorterReadVarint(
 70380         -  sqlite3_file *pFile,            /* File to read from */
 70381         -  i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
 70382         -  i64 *piVal                      /* OUT: Value read from file */
 70383         -){
 70384         -  u8 aVarint[9];                  /* Buffer large enough for a varint */
 70385         -  i64 iOff = *piOffset;           /* Offset in file to read from */
 70386         -  int rc;                         /* Return code */
 70387         -
 70388         -  rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
 70389         -  if( rc==SQLITE_OK ){
 70390         -    *piOffset += getVarint(aVarint, (u64 *)piVal);
        70484  +    pIter->nKey = (int)nRec;
        70485  +    rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
 70391  70486     }
 70392  70487   
 70393  70488     return rc;
 70394  70489   }
 70395  70490   
 70396  70491   /*
 70397  70492   ** Initialize iterator pIter to scan through the PMA stored in file pFile
 70398  70493   ** starting at offset iStart and ending at offset iEof-1. This function 
 70399  70494   ** leaves the iterator pointing to the first key in the PMA (or EOF if the 
 70400  70495   ** PMA is empty).
 70401  70496   */
 70402  70497   static int vdbeSorterIterInit(
 70403  70498     sqlite3 *db,                    /* Database handle */
 70404         -  VdbeSorter *pSorter,            /* Sorter object */
        70499  +  const VdbeSorter *pSorter,      /* Sorter object */
 70405  70500     i64 iStart,                     /* Start offset in pFile */
 70406  70501     VdbeSorterIter *pIter,          /* Iterator to populate */
 70407  70502     i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
 70408  70503   ){
 70409         -  int rc;
        70504  +  int rc = SQLITE_OK;
        70505  +  int nBuf;
        70506  +
        70507  +  nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 70410  70508   
 70411  70509     assert( pSorter->iWriteOff>iStart );
 70412  70510     assert( pIter->aAlloc==0 );
        70511  +  assert( pIter->aBuffer==0 );
 70413  70512     pIter->pFile = pSorter->pTemp1;
 70414  70513     pIter->iReadOff = iStart;
 70415  70514     pIter->nAlloc = 128;
 70416  70515     pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
 70417         -  if( !pIter->aAlloc ){
        70516  +  pIter->nBuffer = nBuf;
        70517  +  pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
        70518  +
        70519  +  if( !pIter->aBuffer ){
 70418  70520       rc = SQLITE_NOMEM;
 70419  70521     }else{
 70420         -    i64 nByte;                         /* Total size of PMA in bytes */
 70421         -    rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
 70422         -    *pnByte += nByte;
 70423         -    pIter->iEof = pIter->iReadOff + nByte;
        70522  +    int iBuf;
        70523  +
        70524  +    iBuf = iStart % nBuf;
        70525  +    if( iBuf ){
        70526  +      int nRead = nBuf - iBuf;
        70527  +      if( (iStart + nRead) > pSorter->iWriteOff ){
        70528  +        nRead = (int)(pSorter->iWriteOff - iStart);
        70529  +      }
        70530  +      rc = sqlite3OsRead(
        70531  +          pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
        70532  +      );
        70533  +      assert( rc!=SQLITE_IOERR_SHORT_READ );
        70534  +    }
        70535  +
        70536  +    if( rc==SQLITE_OK ){
        70537  +      u64 nByte;                       /* Size of PMA in bytes */
        70538  +      pIter->iEof = pSorter->iWriteOff;
        70539  +      rc = vdbeSorterIterVarint(db, pIter, &nByte);
        70540  +      pIter->iEof = pIter->iReadOff + nByte;
        70541  +      *pnByte += nByte;
        70542  +    }
 70424  70543     }
        70544  +
 70425  70545     if( rc==SQLITE_OK ){
 70426  70546       rc = vdbeSorterIterNext(db, pIter);
 70427  70547     }
 70428  70548     return rc;
 70429  70549   }
 70430  70550   
 70431  70551   
................................................................................
 70441  70561   ** is true and key1 contains even a single NULL value, it is considered to
 70442  70562   ** be less than key2. Even if key2 also contains NULL values.
 70443  70563   **
 70444  70564   ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
 70445  70565   ** has been allocated and contains an unpacked record that is used as key2.
 70446  70566   */
 70447  70567   static void vdbeSorterCompare(
 70448         -  VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
        70568  +  const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
 70449  70569     int bOmitRowid,                 /* Ignore rowid field at end of keys */
 70450         -  void *pKey1, int nKey1,         /* Left side of comparison */
 70451         -  void *pKey2, int nKey2,         /* Right side of comparison */
        70570  +  const void *pKey1, int nKey1,   /* Left side of comparison */
        70571  +  const void *pKey2, int nKey2,   /* Right side of comparison */
 70452  70572     int *pRes                       /* OUT: Result of comparison */
 70453  70573   ){
 70454  70574     KeyInfo *pKeyInfo = pCsr->pKeyInfo;
 70455  70575     VdbeSorter *pSorter = pCsr->pSorter;
 70456  70576     UnpackedRecord *r2 = pSorter->pUnpacked;
 70457  70577     int i;
 70458  70578   
................................................................................
 70476  70596   }
 70477  70597   
 70478  70598   /*
 70479  70599   ** This function is called to compare two iterator keys when merging 
 70480  70600   ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
 70481  70601   ** value to recalculate.
 70482  70602   */
 70483         -static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
        70603  +static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
 70484  70604     VdbeSorter *pSorter = pCsr->pSorter;
 70485  70605     int i1;
 70486  70606     int i2;
 70487  70607     int iRes;
 70488  70608     VdbeSorterIter *p1;
 70489  70609     VdbeSorterIter *p2;
 70490  70610   
................................................................................
 70602  70722   }
 70603  70723   
 70604  70724   /*
 70605  70725   ** Merge the two sorted lists p1 and p2 into a single list.
 70606  70726   ** Set *ppOut to the head of the new list.
 70607  70727   */
 70608  70728   static void vdbeSorterMerge(
 70609         -  VdbeCursor *pCsr,               /* For pKeyInfo */
        70729  +  const VdbeCursor *pCsr,         /* For pKeyInfo */
 70610  70730     SorterRecord *p1,               /* First list to merge */
 70611  70731     SorterRecord *p2,               /* Second list to merge */
 70612  70732     SorterRecord **ppOut            /* OUT: Head of merged list */
 70613  70733   ){
 70614  70734     SorterRecord *pFinal = 0;
 70615  70735     SorterRecord **pp = &pFinal;
 70616  70736     void *pVal2 = p2 ? p2->pVal : 0;
................................................................................
 70636  70756   }
 70637  70757   
 70638  70758   /*
 70639  70759   ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
 70640  70760   ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
 70641  70761   ** occurs.
 70642  70762   */
 70643         -static int vdbeSorterSort(VdbeCursor *pCsr){
        70763  +static int vdbeSorterSort(const VdbeCursor *pCsr){
 70644  70764     int i;
 70645  70765     SorterRecord **aSlot;
 70646  70766     SorterRecord *p;
 70647  70767     VdbeSorter *pSorter = pCsr->pSorter;
 70648  70768   
 70649  70769     aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
 70650  70770     if( !aSlot ){
................................................................................
 70669  70789     }
 70670  70790     pSorter->pRecord = p;
 70671  70791   
 70672  70792     sqlite3_free(aSlot);
 70673  70793     return SQLITE_OK;
 70674  70794   }
 70675  70795   
        70796  +/*
        70797  +** Initialize a file-writer object.
        70798  +*/
        70799  +static void fileWriterInit(
        70800  +  sqlite3 *db,                    /* Database (for malloc) */
        70801  +  sqlite3_file *pFile,            /* File to write to */
        70802  +  FileWriter *p,                  /* Object to populate */
        70803  +  i64 iStart                      /* Offset of pFile to begin writing at */
        70804  +){
        70805  +  int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
        70806  +
        70807  +  memset(p, 0, sizeof(FileWriter));
        70808  +  p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
        70809  +  if( !p->aBuffer ){
        70810  +    p->eFWErr = SQLITE_NOMEM;
        70811  +  }else{
        70812  +    p->iBufEnd = p->iBufStart = (iStart % nBuf);
        70813  +    p->iWriteOff = iStart - p->iBufStart;
        70814  +    p->nBuffer = nBuf;
        70815  +    p->pFile = pFile;
        70816  +  }
        70817  +}
        70818  +
        70819  +/*
        70820  +** Write nData bytes of data to the file-write object. Return SQLITE_OK
        70821  +** if successful, or an SQLite error code if an error occurs.
        70822  +*/
        70823  +static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
        70824  +  int nRem = nData;
        70825  +  while( nRem>0 && p->eFWErr==0 ){
        70826  +    int nCopy = nRem;
        70827  +    if( nCopy>(p->nBuffer - p->iBufEnd) ){
        70828  +      nCopy = p->nBuffer - p->iBufEnd;
        70829  +    }
        70830  +
        70831  +    memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
        70832  +    p->iBufEnd += nCopy;
        70833  +    if( p->iBufEnd==p->nBuffer ){
        70834  +      p->eFWErr = sqlite3OsWrite(p->pFile, 
        70835  +          &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
        70836  +          p->iWriteOff + p->iBufStart
        70837  +      );
        70838  +      p->iBufStart = p->iBufEnd = 0;
        70839  +      p->iWriteOff += p->nBuffer;
        70840  +    }
        70841  +    assert( p->iBufEnd<p->nBuffer );
        70842  +
        70843  +    nRem -= nCopy;
        70844  +  }
        70845  +}
        70846  +
        70847  +/*
        70848  +** Flush any buffered data to disk and clean up the file-writer object.
        70849  +** The results of using the file-writer after this call are undefined.
        70850  +** Return SQLITE_OK if flushing the buffered data succeeds or is not 
        70851  +** required. Otherwise, return an SQLite error code.
        70852  +**
        70853  +** Before returning, set *piEof to the offset immediately following the
        70854  +** last byte written to the file.
        70855  +*/
        70856  +static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
        70857  +  int rc;
        70858  +  if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
        70859  +    p->eFWErr = sqlite3OsWrite(p->pFile, 
        70860  +        &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
        70861  +        p->iWriteOff + p->iBufStart
        70862  +    );
        70863  +  }
        70864  +  *piEof = (p->iWriteOff + p->iBufEnd);
        70865  +  sqlite3DbFree(db, p->aBuffer);
        70866  +  rc = p->eFWErr;
        70867  +  memset(p, 0, sizeof(FileWriter));
        70868  +  return rc;
        70869  +}
        70870  +
        70871  +/*
        70872  +** Write value iVal encoded as a varint to the file-write object. Return 
        70873  +** SQLITE_OK if successful, or an SQLite error code if an error occurs.
        70874  +*/
        70875  +static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
        70876  +  int nByte; 
        70877  +  u8 aByte[10];
        70878  +  nByte = sqlite3PutVarint(aByte, iVal);
        70879  +  fileWriterWrite(p, aByte, nByte);
        70880  +}
 70676  70881   
 70677  70882   /*
 70678  70883   ** Write the current contents of the in-memory linked-list to a PMA. Return
 70679  70884   ** SQLITE_OK if successful, or an SQLite error code otherwise.
 70680  70885   **
 70681  70886   ** The format of a PMA is:
 70682  70887   **
................................................................................
 70683  70888   **     * A varint. This varint contains the total number of bytes of content
 70684  70889   **       in the PMA (not including the varint itself).
 70685  70890   **
 70686  70891   **     * One or more records packed end-to-end in order of ascending keys. 
 70687  70892   **       Each record consists of a varint followed by a blob of data (the 
 70688  70893   **       key). The varint is the number of bytes in the blob of data.
 70689  70894   */
 70690         -static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
        70895  +static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
 70691  70896     int rc = SQLITE_OK;             /* Return code */
 70692  70897     VdbeSorter *pSorter = pCsr->pSorter;
        70898  +  FileWriter writer;
        70899  +
        70900  +  memset(&writer, 0, sizeof(FileWriter));
 70693  70901   
 70694  70902     if( pSorter->nInMemory==0 ){
 70695  70903       assert( pSorter->pRecord==0 );
 70696  70904       return rc;
 70697  70905     }
 70698  70906   
 70699  70907     rc = vdbeSorterSort(pCsr);
................................................................................
 70703  70911       rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
 70704  70912       assert( rc!=SQLITE_OK || pSorter->pTemp1 );
 70705  70913       assert( pSorter->iWriteOff==0 );
 70706  70914       assert( pSorter->nPMA==0 );
 70707  70915     }
 70708  70916   
 70709  70917     if( rc==SQLITE_OK ){
 70710         -    i64 iOff = pSorter->iWriteOff;
 70711  70918       SorterRecord *p;
 70712  70919       SorterRecord *pNext = 0;
 70713         -    static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 70714  70920   
        70921  +    fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
 70715  70922       pSorter->nPMA++;
 70716         -    rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
 70717         -    for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
        70923  +    fileWriterWriteVarint(&writer, pSorter->nInMemory);
        70924  +    for(p=pSorter->pRecord; p; p=pNext){
 70718  70925         pNext = p->pNext;
 70719         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
 70720         -
 70721         -      if( rc==SQLITE_OK ){
 70722         -        rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
 70723         -        iOff += p->nVal;
 70724         -      }
 70725         -
        70926  +      fileWriterWriteVarint(&writer, p->nVal);
        70927  +      fileWriterWrite(&writer, p->pVal, p->nVal);
 70726  70928         sqlite3DbFree(db, p);
 70727  70929       }
 70728         -
 70729         -    /* This assert verifies that unless an error has occurred, the size of 
 70730         -    ** the PMA on disk is the same as the expected size stored in
 70731         -    ** pSorter->nInMemory. */ 
 70732         -    assert( rc!=SQLITE_OK || pSorter->nInMemory==(
 70733         -          iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
 70734         -    ));
 70735         -
 70736         -    pSorter->iWriteOff = iOff;
 70737         -    if( rc==SQLITE_OK ){
 70738         -      /* Terminate each file with 8 extra bytes so that from any offset
 70739         -      ** in the file we can always read 9 bytes without a SHORT_READ error */
 70740         -      rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
 70741         -    }
 70742  70930       pSorter->pRecord = p;
        70931  +    rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
 70743  70932     }
 70744  70933   
 70745  70934     return rc;
 70746  70935   }
 70747  70936   
 70748  70937   /*
 70749  70938   ** Add a record to the sorter.
 70750  70939   */
 70751  70940   SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
 70752  70941     sqlite3 *db,                    /* Database handle */
 70753         -  VdbeCursor *pCsr,               /* Sorter cursor */
        70942  +  const VdbeCursor *pCsr,               /* Sorter cursor */
 70754  70943     Mem *pVal                       /* Memory cell containing record */
 70755  70944   ){
 70756  70945     VdbeSorter *pSorter = pCsr->pSorter;
 70757  70946     int rc = SQLITE_OK;             /* Return Code */
 70758  70947     SorterRecord *pNew;             /* New list element */
 70759  70948   
 70760  70949     assert( pSorter );
................................................................................
 70780  70969     **   * The total memory allocated for the in-memory list is greater 
 70781  70970     **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
 70782  70971     */
 70783  70972     if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
 70784  70973           (pSorter->nInMemory>pSorter->mxPmaSize)
 70785  70974        || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
 70786  70975     )){
        70976  +#ifdef SQLITE_DEBUG
        70977  +    i64 nExpect = pSorter->iWriteOff
        70978  +                + sqlite3VarintLen(pSorter->nInMemory)
        70979  +                + pSorter->nInMemory;
        70980  +#endif
 70787  70981       rc = vdbeSorterListToPMA(db, pCsr);
 70788  70982       pSorter->nInMemory = 0;
        70983  +    assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
 70789  70984     }
 70790  70985   
 70791  70986     return rc;
 70792  70987   }
 70793  70988   
 70794  70989   /*
 70795  70990   ** Helper function for sqlite3VdbeSorterRewind(). 
 70796  70991   */
 70797  70992   static int vdbeSorterInitMerge(
 70798  70993     sqlite3 *db,                    /* Database handle */
 70799         -  VdbeCursor *pCsr,               /* Cursor handle for this sorter */
        70994  +  const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
 70800  70995     i64 *pnByte                     /* Sum of bytes in all opened PMAs */
 70801  70996   ){
 70802  70997     VdbeSorter *pSorter = pCsr->pSorter;
 70803  70998     int rc = SQLITE_OK;             /* Return code */
 70804  70999     int i;                          /* Used to iterator through aIter[] */
 70805  71000     i64 nByte = 0;                  /* Total bytes in all opened PMAs */
 70806  71001   
................................................................................
 70822  71017     return rc;
 70823  71018   }
 70824  71019   
 70825  71020   /*
 70826  71021   ** Once the sorter has been populated, this function is called to prepare
 70827  71022   ** for iterating through its contents in sorted order.
 70828  71023   */
 70829         -SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
        71024  +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 70830  71025     VdbeSorter *pSorter = pCsr->pSorter;
 70831  71026     int rc;                         /* Return code */
 70832  71027     sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
 70833  71028     i64 iWrite2 = 0;                /* Write offset for pTemp2 */
 70834  71029     int nIter;                      /* Number of iterators used */
 70835  71030     int nByte;                      /* Bytes of space required for aIter/aTree */
 70836  71031     int N = 2;                      /* Power of 2 >= nIter */
................................................................................
 70842  71037     ** from the in-memory list.  */
 70843  71038     if( pSorter->nPMA==0 ){
 70844  71039       *pbEof = !pSorter->pRecord;
 70845  71040       assert( pSorter->aTree==0 );
 70846  71041       return vdbeSorterSort(pCsr);
 70847  71042     }
 70848  71043   
 70849         -  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
        71044  +  /* Write the current in-memory list to a PMA. */
 70850  71045     rc = vdbeSorterListToPMA(db, pCsr);
 70851  71046     if( rc!=SQLITE_OK ) return rc;
 70852  71047   
 70853  71048     /* Allocate space for aIter[] and aTree[]. */
 70854  71049     nIter = pSorter->nPMA;
 70855  71050     if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
 70856  71051     assert( nIter>0 );
................................................................................
 70864  71059     do {
 70865  71060       int iNew;                     /* Index of new, merged, PMA */
 70866  71061   
 70867  71062       for(iNew=0; 
 70868  71063           rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
 70869  71064           iNew++
 70870  71065       ){
        71066  +      int rc2;                    /* Return code from fileWriterFinish() */
        71067  +      FileWriter writer;          /* Object used to write to disk */
 70871  71068         i64 nWrite;                 /* Number of bytes in new PMA */
        71069  +
        71070  +      memset(&writer, 0, sizeof(FileWriter));
 70872  71071   
 70873  71072         /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
 70874  71073         ** initialize an iterator for each of them and break out of the loop.
 70875  71074         ** These iterators will be incrementally merged as the VDBE layer calls
 70876  71075         ** sqlite3VdbeSorterNext().
 70877  71076         **
 70878  71077         ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
................................................................................
 70887  71086   
 70888  71087         /* Open the second temp file, if it is not already open. */
 70889  71088         if( pTemp2==0 ){
 70890  71089           assert( iWrite2==0 );
 70891  71090           rc = vdbeSorterOpenTempFile(db, &pTemp2);
 70892  71091         }
 70893  71092   
 70894         -      if( rc==SQLITE_OK ){
 70895         -        rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
 70896         -      }
 70897         -
 70898  71093         if( rc==SQLITE_OK ){
 70899  71094           int bEof = 0;
        71095  +        fileWriterInit(db, pTemp2, &writer, iWrite2);
        71096  +        fileWriterWriteVarint(&writer, nWrite);
 70900  71097           while( rc==SQLITE_OK && bEof==0 ){
 70901         -          int nToWrite;
 70902  71098             VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 70903  71099             assert( pIter->pFile );
 70904         -          nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
 70905         -          rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
 70906         -          iWrite2 += nToWrite;
 70907         -          if( rc==SQLITE_OK ){
 70908         -            rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
 70909         -          }
        71100  +
        71101  +          fileWriterWriteVarint(&writer, pIter->nKey);
        71102  +          fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
        71103  +          rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
 70910  71104           }
        71105  +        rc2 = fileWriterFinish(db, &writer, &iWrite2);
        71106  +        if( rc==SQLITE_OK ) rc = rc2;
 70911  71107         }
 70912  71108       }
 70913  71109   
 70914  71110       if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
 70915  71111         break;
 70916  71112       }else{
 70917  71113         sqlite3_file *pTmp = pSorter->pTemp1;
................................................................................
 70930  71126     *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 70931  71127     return rc;
 70932  71128   }
 70933  71129   
 70934  71130   /*
 70935  71131   ** Advance to the next element in the sorter.
 70936  71132   */
 70937         -SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
        71133  +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 70938  71134     VdbeSorter *pSorter = pCsr->pSorter;
 70939  71135     int rc;                         /* Return code */
 70940  71136   
 70941  71137     if( pSorter->aTree ){
 70942  71138       int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
 70943  71139       int i;                        /* Index of aTree[] to recalculate */
 70944  71140   
................................................................................
 70960  71156   }
 70961  71157   
 70962  71158   /*
 70963  71159   ** Return a pointer to a buffer owned by the sorter that contains the 
 70964  71160   ** current key.
 70965  71161   */
 70966  71162   static void *vdbeSorterRowkey(
 70967         -  VdbeSorter *pSorter,            /* Sorter object */
        71163  +  const VdbeSorter *pSorter,      /* Sorter object */
 70968  71164     int *pnKey                      /* OUT: Size of current key in bytes */
 70969  71165   ){
 70970  71166     void *pKey;
 70971  71167     if( pSorter->aTree ){
 70972  71168       VdbeSorterIter *pIter;
 70973  71169       pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 70974  71170       *pnKey = pIter->nKey;
................................................................................
 70979  71175     }
 70980  71176     return pKey;
 70981  71177   }
 70982  71178   
 70983  71179   /*
 70984  71180   ** Copy the current sorter key into the memory cell pOut.
 70985  71181   */
 70986         -SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
        71182  +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
 70987  71183     VdbeSorter *pSorter = pCsr->pSorter;
 70988  71184     void *pKey; int nKey;           /* Sorter key to copy into pOut */
 70989  71185   
 70990  71186     pKey = vdbeSorterRowkey(pSorter, &nKey);
 70991  71187     if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
 70992  71188       return SQLITE_NOMEM;
 70993  71189     }
................................................................................
 71005  71201   **
 71006  71202   ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
 71007  71203   ** Otherwise, set *pRes to a negative, zero or positive value if the
 71008  71204   ** key in pVal is smaller than, equal to or larger than the current sorter
 71009  71205   ** key.
 71010  71206   */
 71011  71207   SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
 71012         -  VdbeCursor *pCsr,               /* Sorter cursor */
        71208  +  const VdbeCursor *pCsr,         /* Sorter cursor */
 71013  71209     Mem *pVal,                      /* Value to compare to current sorter key */
 71014  71210     int *pRes                       /* OUT: Result of comparison */
 71015  71211   ){
 71016  71212     VdbeSorter *pSorter = pCsr->pSorter;
 71017  71213     void *pKey; int nKey;           /* Sorter key to compare pVal with */
 71018  71214   
 71019  71215     pKey = vdbeSorterRowkey(pSorter, &nKey);
................................................................................
 74592  74788           ** table allocated and opened above.
 74593  74789           */
 74594  74790           SelectDest dest;
 74595  74791           ExprList *pEList;
 74596  74792   
 74597  74793           assert( !isRowid );
 74598  74794           sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 74599         -        dest.affinity = (u8)affinity;
        74795  +        dest.affSdst = (u8)affinity;
 74600  74796           assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 74601  74797           pExpr->x.pSelect->iLimit = 0;
 74602  74798           if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
 74603  74799             return 0;
 74604  74800           }
 74605  74801           pEList = pExpr->x.pSelect->pEList;
 74606  74802           if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ 
................................................................................
 74685  74881         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 74686  74882   
 74687  74883         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 74688  74884         pSel = pExpr->x.pSelect;
 74689  74885         sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
 74690  74886         if( pExpr->op==TK_SELECT ){
 74691  74887           dest.eDest = SRT_Mem;
 74692         -        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
        74888  +        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
 74693  74889           VdbeComment((v, "Init subquery result"));
 74694  74890         }else{
 74695  74891           dest.eDest = SRT_Exists;
 74696         -        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
        74892  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 74697  74893           VdbeComment((v, "Init EXISTS result"));
 74698  74894         }
 74699  74895         sqlite3ExprDelete(pParse->db, pSel->pLimit);
 74700  74896         pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
 74701  74897                                     &sqlite3IntTokens[1]);
 74702  74898         pSel->iLimit = 0;
 74703  74899         if( sqlite3Select(pParse, pSel, &dest) ){
 74704  74900           return 0;
 74705  74901         }
 74706         -      rReg = dest.iParm;
        74902  +      rReg = dest.iSDParm;
 74707  74903         ExprSetIrreducible(pExpr);
 74708  74904         break;
 74709  74905       }
 74710  74906     }
 74711  74907   
 74712  74908     if( testAddr>=0 ){
 74713  74909       sqlite3VdbeJumpHere(v, testAddr);
................................................................................
 78013  78209         ** side-effect of the CREATE TABLE statement is to leave the rootpage 
 78014  78210         ** of the new table in register pParse->regRoot. This is important 
 78015  78211         ** because the OpenWrite opcode below will be needing it. */
 78016  78212         sqlite3NestedParse(pParse,
 78017  78213             "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
 78018  78214         );
 78019  78215         aRoot[i] = pParse->regRoot;
 78020         -      aCreateTbl[i] = 1;
        78216  +      aCreateTbl[i] = OPFLAG_P2ISREG;
 78021  78217       }else{
 78022  78218         /* The table already exists. If zWhere is not NULL, delete all entries 
 78023  78219         ** associated with the table zWhere. If zWhere is NULL, delete the
 78024  78220         ** entire contents of the table. */
 78025  78221         aRoot[i] = pStat->tnum;
 78026  78222         sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 78027  78223         if( zWhere ){
................................................................................
 78093  78289     int mxSample;
 78094  78290     int n;
 78095  78291   
 78096  78292     UNUSED_PARAMETER(argc);
 78097  78293     nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
 78098  78294     mxSample = sqlite3_value_int(argv[1]);
 78099  78295     n = sizeof(*p) + sizeof(p->a[0])*mxSample;
 78100         -  p = sqlite3_malloc( n );
        78296  +  p = sqlite3MallocZero( n );
 78101  78297     if( p==0 ){
 78102  78298       sqlite3_result_error_nomem(context);
 78103  78299       return;
 78104  78300     }
 78105         -  memset(p, 0, n);
 78106  78301     p->a = (struct Stat3Sample*)&p[1];
 78107  78302     p->nRow = nRow;
 78108  78303     p->mxSample = mxSample;
 78109  78304     p->nPSample = p->nRow/(mxSample/3+1) + 1;
 78110  78305     sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
 78111  78306     sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
 78112  78307   }
................................................................................
 81355  81550       */
 81356  81551       if( pSelect ){
 81357  81552         SelectDest dest;
 81358  81553         Table *pSelTab;
 81359  81554   
 81360  81555         assert(pParse->nTab==1);
 81361  81556         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
 81362         -      sqlite3VdbeChangeP5(v, 1);
        81557  +      sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
 81363  81558         pParse->nTab = 2;
 81364  81559         sqlite3SelectDestInit(&dest, SRT_Table, 1);
 81365  81560         sqlite3Select(pParse, pSelect, &dest);
 81366  81561         sqlite3VdbeAddOp1(v, OP_Close, 1);
 81367  81562         if( pParse->nErr==0 ){
 81368  81563           pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
 81369  81564           if( pSelTab==0 ) return;
................................................................................
 82171  82366     }else{
 82172  82367       tnum = pIndex->tnum;
 82173  82368       sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
 82174  82369     }
 82175  82370     pKey = sqlite3IndexKeyinfo(pParse, pIndex);
 82176  82371     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 82177  82372                       (char *)pKey, P4_KEYINFO_HANDOFF);
 82178         -  if( memRootPage>=0 ){
 82179         -    sqlite3VdbeChangeP5(v, 1);
 82180         -  }
        82373  +  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
 82181  82374   
 82182  82375   #ifndef SQLITE_OMIT_MERGE_SORT
 82183  82376     /* Open the sorter cursor if we are to use one. */
 82184  82377     iSorter = pParse->nTab++;
 82185  82378     sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
 82186  82379   #else
 82187  82380     iSorter = iTab;
................................................................................
 88192  88385       int rc, j1;
 88193  88386   
 88194  88387       regEof = ++pParse->nMem;
 88195  88388       sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
 88196  88389       VdbeComment((v, "SELECT eof flag"));
 88197  88390       sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
 88198  88391       addrSelect = sqlite3VdbeCurrentAddr(v)+2;
 88199         -    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
        88392  +    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iSDParm);
 88200  88393       j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 88201  88394       VdbeComment((v, "Jump over SELECT coroutine"));
 88202  88395   
 88203  88396       /* Resolve the expressions in the SELECT statement and execute it. */
 88204  88397       rc = sqlite3Select(pParse, pSelect, &dest);
 88205  88398       assert( pParse->nErr==0 || rc );
 88206  88399       if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
 88207  88400         goto insert_cleanup;
 88208  88401       }
 88209  88402       sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
 88210         -    sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
        88403  +    sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);   /* yield X */
 88211  88404       sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
 88212  88405       VdbeComment((v, "End of SELECT coroutine"));
 88213  88406       sqlite3VdbeJumpHere(v, j1);                          /* label B: */
 88214  88407   
 88215         -    regFromSelect = dest.iMem;
        88408  +    regFromSelect = dest.iSdst;
 88216  88409       assert( pSelect->pEList );
 88217  88410       nColumn = pSelect->pEList->nExpr;
 88218         -    assert( dest.nMem==nColumn );
        88411  +    assert( dest.nSdst==nColumn );
 88219  88412   
 88220  88413       /* Set useTempTable to TRUE if the result of the SELECT statement
 88221  88414       ** should be written into a temporary table (template 4).  Set to
 88222  88415       ** FALSE if each* row of the SELECT can be written directly into
 88223  88416       ** the destination table (template 3).
 88224  88417       **
 88225  88418       ** A temp table must be used if the table being updated is also one
................................................................................
 88247  88440         int addrTop;         /* Label "L" */
 88248  88441         int addrIf;          /* Address of jump to M */
 88249  88442   
 88250  88443         srcTab = pParse->nTab++;
 88251  88444         regRec = sqlite3GetTempReg(pParse);
 88252  88445         regTempRowid = sqlite3GetTempReg(pParse);
 88253  88446         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
 88254         -      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
        88447  +      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 88255  88448         addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
 88256  88449         sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
 88257  88450         sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
 88258  88451         sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
 88259  88452         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
 88260  88453         sqlite3VdbeJumpHere(v, addrIf);
 88261  88454         sqlite3ReleaseTempReg(pParse, regRec);
................................................................................
 88384  88577       **
 88385  88578       **      C: yield X
 88386  88579       **         if EOF goto D
 88387  88580       **         insert the select result into <table> from R..R+n
 88388  88581       **         goto C
 88389  88582       **      D: ...
 88390  88583       */
 88391         -    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
        88584  +    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 88392  88585       addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
 88393  88586     }
 88394  88587   
 88395  88588     /* Allocate registers for holding the rowid of the new row,
 88396  88589     ** the content of the new row, and the assemblied row record.
 88397  88590     */
 88398  88591     regRowid = regIns = pParse->nMem+1;
................................................................................
 93239  93432   }
 93240  93433   
 93241  93434   /*
 93242  93435   ** Initialize a SelectDest structure.
 93243  93436   */
 93244  93437   SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
 93245  93438     pDest->eDest = (u8)eDest;
 93246         -  pDest->iParm = iParm;
 93247         -  pDest->affinity = 0;
 93248         -  pDest->iMem = 0;
 93249         -  pDest->nMem = 0;
        93439  +  pDest->iSDParm = iParm;
        93440  +  pDest->affSdst = 0;
        93441  +  pDest->iSdst = 0;
        93442  +  pDest->nSdst = 0;
 93250  93443   }
 93251  93444   
 93252  93445   
 93253  93446   /*
 93254  93447   ** Allocate a new Select structure and return a pointer to that
 93255  93448   ** structure.
 93256  93449   */
................................................................................
 93754  93947     int iBreak              /* Jump here to break out of the inner loop */
 93755  93948   ){
 93756  93949     Vdbe *v = pParse->pVdbe;
 93757  93950     int i;
 93758  93951     int hasDistinct;        /* True if the DISTINCT keyword is present */
 93759  93952     int regResult;              /* Start of memory holding result set */
 93760  93953     int eDest = pDest->eDest;   /* How to dispose of results */
 93761         -  int iParm = pDest->iParm;   /* First argument to disposal method */
        93954  +  int iParm = pDest->iSDParm; /* First argument to disposal method */
 93762  93955     int nResultCol;             /* Number of result columns */
 93763  93956   
 93764  93957     assert( v );
 93765  93958     if( NEVER(v==0) ) return;
 93766  93959     assert( pEList!=0 );
 93767  93960     hasDistinct = distinct>=0;
 93768  93961     if( pOrderBy==0 && !hasDistinct ){
................................................................................
 93772  93965     /* Pull the requested columns.
 93773  93966     */
 93774  93967     if( nColumn>0 ){
 93775  93968       nResultCol = nColumn;
 93776  93969     }else{
 93777  93970       nResultCol = pEList->nExpr;
 93778  93971     }
 93779         -  if( pDest->iMem==0 ){
 93780         -    pDest->iMem = pParse->nMem+1;
 93781         -    pDest->nMem = nResultCol;
        93972  +  if( pDest->iSdst==0 ){
        93973  +    pDest->iSdst = pParse->nMem+1;
        93974  +    pDest->nSdst = nResultCol;
 93782  93975       pParse->nMem += nResultCol;
 93783  93976     }else{ 
 93784         -    assert( pDest->nMem==nResultCol );
        93977  +    assert( pDest->nSdst==nResultCol );
 93785  93978     }
 93786         -  regResult = pDest->iMem;
        93979  +  regResult = pDest->iSdst;
 93787  93980     if( nColumn>0 ){
 93788  93981       for(i=0; i<nColumn; i++){
 93789  93982         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
 93790  93983       }
 93791  93984     }else if( eDest!=SRT_Exists ){
 93792  93985       /* If the destination is an EXISTS(...) expression, the actual
 93793  93986       ** values returned by the SELECT are not required.
................................................................................
 93858  94051   #ifndef SQLITE_OMIT_SUBQUERY
 93859  94052       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 93860  94053       ** then there should be a single item on the stack.  Write this
 93861  94054       ** item into the set table with bogus data.
 93862  94055       */
 93863  94056       case SRT_Set: {
 93864  94057         assert( nColumn==1 );
 93865         -      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
        94058  +      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
 93866  94059         if( pOrderBy ){
 93867  94060           /* At first glance you would think we could optimize out the
 93868  94061           ** ORDER BY in this case since the order of entries in the set
 93869  94062           ** does not matter.  But there might be a LIMIT clause, in which
 93870  94063           ** case the order does matter */
 93871  94064           pushOntoSorter(pParse, pOrderBy, p, regResult);
 93872  94065         }else{
................................................................................
 93913  94106         testcase( eDest==SRT_Output );
 93914  94107         if( pOrderBy ){
 93915  94108           int r1 = sqlite3GetTempReg(pParse);
 93916  94109           sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
 93917  94110           pushOntoSorter(pParse, pOrderBy, p, r1);
 93918  94111           sqlite3ReleaseTempReg(pParse, r1);
 93919  94112         }else if( eDest==SRT_Coroutine ){
 93920         -        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
        94113  +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 93921  94114         }else{
 93922  94115           sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
 93923  94116           sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
 93924  94117         }
 93925  94118         break;
 93926  94119       }
 93927  94120   
................................................................................
 94093  94286     int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
 94094  94287     int addr;
 94095  94288     int iTab;
 94096  94289     int pseudoTab = 0;
 94097  94290     ExprList *pOrderBy = p->pOrderBy;
 94098  94291   
 94099  94292     int eDest = pDest->eDest;
 94100         -  int iParm = pDest->iParm;
        94293  +  int iParm = pDest->iSDParm;
 94101  94294   
 94102  94295     int regRow;
 94103  94296     int regRowid;
 94104  94297   
 94105  94298     iTab = pOrderBy->iECursor;
 94106  94299     regRow = sqlite3GetTempReg(pParse);
 94107  94300     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
................................................................................
 94152  94345   #endif
 94153  94346       default: {
 94154  94347         int i;
 94155  94348         assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
 94156  94349         testcase( eDest==SRT_Output );
 94157  94350         testcase( eDest==SRT_Coroutine );
 94158  94351         for(i=0; i<nColumn; i++){
 94159         -        assert( regRow!=pDest->iMem+i );
 94160         -        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
        94352  +        assert( regRow!=pDest->iSdst+i );
        94353  +        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
 94161  94354           if( i==0 ){
 94162  94355             sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 94163  94356           }
 94164  94357         }
 94165  94358         if( eDest==SRT_Output ){
 94166         -        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
 94167         -        sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
        94359  +        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
        94360  +        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
 94168  94361         }else{
 94169         -        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
        94362  +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 94170  94363         }
 94171  94364         break;
 94172  94365       }
 94173  94366     }
 94174  94367     sqlite3ReleaseTempReg(pParse, regRow);
 94175  94368     sqlite3ReleaseTempReg(pParse, regRowid);
 94176  94369   
................................................................................
 94813  95006     v = sqlite3GetVdbe(pParse);
 94814  95007     assert( v!=0 );  /* The VDBE already created by calling function */
 94815  95008   
 94816  95009     /* Create the destination temporary table if necessary
 94817  95010     */
 94818  95011     if( dest.eDest==SRT_EphemTab ){
 94819  95012       assert( p->pEList );
 94820         -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
        95013  +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
 94821  95014       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 94822  95015       dest.eDest = SRT_Table;
 94823  95016     }
 94824  95017   
 94825  95018     /* Make sure all SELECTs in the statement have the same number of elements
 94826  95019     ** in their result sets.
 94827  95020     */
................................................................................
 94899  95092           /* We can reuse a temporary table generated by a SELECT to our
 94900  95093           ** right.
 94901  95094           */
 94902  95095           assert( p->pRightmost!=p );  /* Can only happen for leftward elements
 94903  95096                                        ** of a 3-way or more compound */
 94904  95097           assert( p->pLimit==0 );      /* Not allowed on leftward elements */
 94905  95098           assert( p->pOffset==0 );     /* Not allowed on leftward elements */
 94906         -        unionTab = dest.iParm;
        95099  +        unionTab = dest.iSDParm;
 94907  95100         }else{
 94908  95101           /* We will need to create our own temporary table to hold the
 94909  95102           ** intermediate results.
 94910  95103           */
 94911  95104           unionTab = pParse->nTab++;
 94912  95105           assert( p->pOrderBy==0 );
 94913  95106           addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
................................................................................
 94956  95149         p->pOffset = pOffset;
 94957  95150         p->iLimit = 0;
 94958  95151         p->iOffset = 0;
 94959  95152   
 94960  95153         /* Convert the data in the temporary table into whatever form
 94961  95154         ** it is that we currently need.
 94962  95155         */
 94963         -      assert( unionTab==dest.iParm || dest.eDest!=priorOp );
        95156  +      assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
 94964  95157         if( dest.eDest!=priorOp ){
 94965  95158           int iCont, iBreak, iStart;
 94966  95159           assert( p->pEList );
 94967  95160           if( dest.eDest==SRT_Output ){
 94968  95161             Select *pFirst = p;
 94969  95162             while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 94970  95163             generateColumnNames(pParse, 0, pFirst->pEList);
................................................................................
 95020  95213         assert( p->addrOpenEphm[1] == -1 );
 95021  95214         p->addrOpenEphm[1] = addr;
 95022  95215         p->pPrior = 0;
 95023  95216         pLimit = p->pLimit;
 95024  95217         p->pLimit = 0;
 95025  95218         pOffset = p->pOffset;
 95026  95219         p->pOffset = 0;
 95027         -      intersectdest.iParm = tab2;
        95220  +      intersectdest.iSDParm = tab2;
 95028  95221         explainSetInteger(iSub2, pParse->iNextSelectId);
 95029  95222         rc = sqlite3Select(pParse, p, &intersectdest);
 95030  95223         testcase( rc!=SQLITE_OK );
 95031  95224         pDelete = p->pPrior;
 95032  95225         p->pPrior = pPrior;
 95033  95226         if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
 95034  95227         sqlite3ExprDelete(db, p->pLimit);
................................................................................
 95114  95307           pLoop->addrOpenEphm[i] = -1;
 95115  95308         }
 95116  95309       }
 95117  95310       sqlite3DbFree(db, pKeyInfo);
 95118  95311     }
 95119  95312   
 95120  95313   multi_select_end:
 95121         -  pDest->iMem = dest.iMem;
 95122         -  pDest->nMem = dest.nMem;
        95314  +  pDest->iSdst = dest.iSdst;
        95315  +  pDest->nSdst = dest.nSdst;
 95123  95316     sqlite3SelectDelete(db, pDelete);
 95124  95317     return rc;
 95125  95318   }
 95126  95319   #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 95127  95320   
 95128  95321   /*
 95129  95322   ** Code an output subroutine for a coroutine implementation of a
 95130  95323   ** SELECT statment.
 95131  95324   **
 95132         -** The data to be output is contained in pIn->iMem.  There are
 95133         -** pIn->nMem columns to be output.  pDest is where the output should
        95325  +** The data to be output is contained in pIn->iSdst.  There are
        95326  +** pIn->nSdst columns to be output.  pDest is where the output should
 95134  95327   ** be sent.
 95135  95328   **
 95136  95329   ** regReturn is the number of the register holding the subroutine
 95137  95330   ** return address.
 95138  95331   **
 95139  95332   ** If regPrev>0 then it is the first register in a vector that
 95140  95333   ** records the previous output.  mem[regPrev] is a flag that is false
................................................................................
 95164  95357     iContinue = sqlite3VdbeMakeLabel(v);
 95165  95358   
 95166  95359     /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
 95167  95360     */
 95168  95361     if( regPrev ){
 95169  95362       int j1, j2;
 95170  95363       j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
 95171         -    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
        95364  +    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
 95172  95365                                 (char*)pKeyInfo, p4type);
 95173  95366       sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
 95174  95367       sqlite3VdbeJumpHere(v, j1);
 95175         -    sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
        95368  +    sqlite3ExprCodeCopy(pParse, pIn->iSdst, regPrev+1, pIn->nSdst);
 95176  95369       sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
 95177  95370     }
 95178  95371     if( pParse->db->mallocFailed ) return 0;
 95179  95372   
 95180  95373     /* Suppress the the first OFFSET entries if there is an OFFSET clause
 95181  95374     */
 95182  95375     codeOffset(v, p, iContinue);
................................................................................
 95186  95379       */
 95187  95380       case SRT_Table:
 95188  95381       case SRT_EphemTab: {
 95189  95382         int r1 = sqlite3GetTempReg(pParse);
 95190  95383         int r2 = sqlite3GetTempReg(pParse);
 95191  95384         testcase( pDest->eDest==SRT_Table );
 95192  95385         testcase( pDest->eDest==SRT_EphemTab );
 95193         -      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
 95194         -      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
 95195         -      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
        95386  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
        95387  +      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
        95388  +      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
 95196  95389         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 95197  95390         sqlite3ReleaseTempReg(pParse, r2);
 95198  95391         sqlite3ReleaseTempReg(pParse, r1);
 95199  95392         break;
 95200  95393       }
 95201  95394   
 95202  95395   #ifndef SQLITE_OMIT_SUBQUERY
 95203  95396       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 95204  95397       ** then there should be a single item on the stack.  Write this
 95205  95398       ** item into the set table with bogus data.
 95206  95399       */
 95207  95400       case SRT_Set: {
 95208  95401         int r1;
 95209         -      assert( pIn->nMem==1 );
        95402  +      assert( pIn->nSdst==1 );
 95210  95403         p->affinity = 
 95211         -         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
        95404  +         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
 95212  95405         r1 = sqlite3GetTempReg(pParse);
 95213         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
 95214         -      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
 95215         -      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
        95406  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &p->affinity, 1);
        95407  +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
        95408  +      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
 95216  95409         sqlite3ReleaseTempReg(pParse, r1);
 95217  95410         break;
 95218  95411       }
 95219  95412   
 95220  95413   #if 0  /* Never occurs on an ORDER BY query */
 95221  95414       /* If any row exist in the result set, record that fact and abort.
 95222  95415       */
 95223  95416       case SRT_Exists: {
 95224         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
        95417  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
 95225  95418         /* The LIMIT clause will terminate the loop for us */
 95226  95419         break;
 95227  95420       }
 95228  95421   #endif
 95229  95422   
 95230  95423       /* If this is a scalar select that is part of an expression, then
 95231  95424       ** store the results in the appropriate memory cell and break out
 95232  95425       ** of the scan loop.
 95233  95426       */
 95234  95427       case SRT_Mem: {
 95235         -      assert( pIn->nMem==1 );
 95236         -      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
        95428  +      assert( pIn->nSdst==1 );
        95429  +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
 95237  95430         /* The LIMIT clause will jump out of the loop for us */
 95238  95431         break;
 95239  95432       }
 95240  95433   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 95241  95434   
 95242  95435       /* The results are stored in a sequence of registers
 95243         -    ** starting at pDest->iMem.  Then the co-routine yields.
        95436  +    ** starting at pDest->iSdst.  Then the co-routine yields.
 95244  95437       */
 95245  95438       case SRT_Coroutine: {
 95246         -      if( pDest->iMem==0 ){
 95247         -        pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
 95248         -        pDest->nMem = pIn->nMem;
        95439  +      if( pDest->iSdst==0 ){
        95440  +        pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
        95441  +        pDest->nSdst = pIn->nSdst;
 95249  95442         }
 95250         -      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
 95251         -      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
        95443  +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
        95444  +      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 95252  95445         break;
 95253  95446       }
 95254  95447   
 95255  95448       /* If none of the above, then the result destination must be
 95256  95449       ** SRT_Output.  This routine is never called with any other
 95257  95450       ** destination other than the ones handled above or SRT_Output.
 95258  95451       **
 95259  95452       ** For SRT_Output, results are stored in a sequence of registers.  
 95260  95453       ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
 95261  95454       ** return the next row of result.
 95262  95455       */
 95263  95456       default: {
 95264  95457         assert( pDest->eDest==SRT_Output );
 95265         -      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
 95266         -      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
        95458  +      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
        95459  +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
 95267  95460         break;
 95268  95461       }
 95269  95462     }
 95270  95463   
 95271  95464     /* Jump to the end of the loop if the LIMIT is reached.
 95272  95465     */
 95273  95466     if( p->iLimit ){
................................................................................
 95678  95871     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
 95679  95872     sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
 95680  95873   
 95681  95874     /* Implement the main merge loop
 95682  95875     */
 95683  95876     sqlite3VdbeResolveLabel(v, labelCmpr);
 95684  95877     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
 95685         -  sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
        95878  +  sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
 95686  95879                            (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
 95687  95880     sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
 95688  95881   
 95689  95882     /* Release temporary registers
 95690  95883     */
 95691  95884     if( regPrev ){
 95692  95885       sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
................................................................................
 96924  97117   **     pDest->eDest    Result
 96925  97118   **     ------------    -------------------------------------------
 96926  97119   **     SRT_Output      Generate a row of output (using the OP_ResultRow
 96927  97120   **                     opcode) for each row in the result set.
 96928  97121   **
 96929  97122   **     SRT_Mem         Only valid if the result is a single column.
 96930  97123   **                     Store the first column of the first result row
 96931         -**                     in register pDest->iParm then abandon the rest
        97124  +**                     in register pDest->iSDParm then abandon the rest
 96932  97125   **                     of the query.  This destination implies "LIMIT 1".
 96933  97126   **
 96934  97127   **     SRT_Set         The result must be a single column.  Store each
 96935         -**                     row of result as the key in table pDest->iParm. 
 96936         -**                     Apply the affinity pDest->affinity before storing
        97128  +**                     row of result as the key in table pDest->iSDParm. 
        97129  +**                     Apply the affinity pDest->affSdst before storing
 96937  97130   **                     results.  Used to implement "IN (SELECT ...)".
 96938  97131   **
 96939         -**     SRT_Union       Store results as a key in a temporary table pDest->iParm.
        97132  +**     SRT_Union       Store results as a key in a temporary table 
        97133  +**                     identified by pDest->iSDParm.
 96940  97134   **
 96941         -**     SRT_Except      Remove results from the temporary table pDest->iParm.
        97135  +**     SRT_Except      Remove results from the temporary table pDest->iSDParm.
 96942  97136   **
 96943         -**     SRT_Table       Store results in temporary table pDest->iParm.
        97137  +**     SRT_Table       Store results in temporary table pDest->iSDParm.
 96944  97138   **                     This is like SRT_EphemTab except that the table
 96945  97139   **                     is assumed to already be open.
 96946  97140   **
 96947         -**     SRT_EphemTab    Create an temporary table pDest->iParm and store
        97141  +**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
 96948  97142   **                     the result there. The cursor is left open after
 96949  97143   **                     returning.  This is like SRT_Table except that
 96950  97144   **                     this destination uses OP_OpenEphemeral to create
 96951  97145   **                     the table first.
 96952  97146   **
 96953  97147   **     SRT_Coroutine   Generate a co-routine that returns a new row of
 96954  97148   **                     results each time it is invoked.  The entry point
 96955         -**                     of the co-routine is stored in register pDest->iParm.
        97149  +**                     of the co-routine is stored in register pDest->iSDParm.
 96956  97150   **
 96957         -**     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
        97151  +**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
 96958  97152   **                     set is not empty.
 96959  97153   **
 96960  97154   **     SRT_Discard     Throw the results away.  This is used by SELECT
 96961  97155   **                     statements within triggers whose only purpose is
 96962  97156   **                     the side-effects of functions.
 96963  97157   **
 96964  97158   ** This routine returns the number of errors.  If any errors are
................................................................................
 97194  97388     }else{
 97195  97389       addrSortIndex = -1;
 97196  97390     }
 97197  97391   
 97198  97392     /* If the output is destined for a temporary table, open that table.
 97199  97393     */
 97200  97394     if( pDest->eDest==SRT_EphemTab ){
 97201         -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
        97395  +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
 97202  97396     }
 97203  97397   
 97204  97398     /* Set the limiter.
 97205  97399     */
 97206  97400     iEnd = sqlite3VdbeMakeLabel(v);
 97207  97401     p->nSelectRow = (double)LARGEST_INT64;
 97208  97402     computeLimitRegisters(pParse, p, iEnd);

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

   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    110   #define SQLITE_VERSION        "3.7.14"
   111    111   #define SQLITE_VERSION_NUMBER 3007014
   112         -#define SQLITE_SOURCE_ID      "2012-07-17 14:37:12 e4163596339c2166f9c4356ab824fff8bda8d0b0"
          112  +#define SQLITE_SOURCE_ID      "2012-08-17 11:47:32 61b1ae121782525b37882a559d0fbb7c85237fd5"
   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
................................................................................
  4153   4153   ** they return.  Hence, the calling function can deallocate or
  4154   4154   ** modify the text after they return without harm.
  4155   4155   ** ^The sqlite3_result_error_code() function changes the error code
  4156   4156   ** returned by SQLite as a result of an error in a function.  ^By default,
  4157   4157   ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
  4158   4158   ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  4159   4159   **
  4160         -** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
  4161         -** indicating that a string or BLOB is too long to represent.
         4160  +** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
         4161  +** error indicating that a string or BLOB is too long to represent.
  4162   4162   **
  4163         -** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
  4164         -** indicating that a memory allocation failed.
         4163  +** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
         4164  +** error indicating that a memory allocation failed.
  4165   4165   **
  4166   4166   ** ^The sqlite3_result_int() interface sets the return value
  4167   4167   ** of the application-defined function to be the 32-bit signed integer
  4168   4168   ** value given in the 2nd argument.
  4169   4169   ** ^The sqlite3_result_int64() interface sets the return value
  4170   4170   ** of the application-defined function to be the 64-bit signed integer
  4171   4171   ** value given in the 2nd argument.