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

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

Overview
Comment:Bump all versions to 1.0.92.0. Update SQLite core library to the latest trunk code. Verify all version numbers in the 'testlinq' EF6 configuration files.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a1cfefb6f2fe2982129d970702939d0773100598
User & Date: mistachkin 2014-03-06 02:29:33
Context
2014-03-06
06:49
For the NuGet packages supporting Entity Framework 6, only list the EF6 DbProviderFactory in the config file. check-in: bb6a9292c1 user: mistachkin tags: trunk
02:29
Bump all versions to 1.0.92.0. Update SQLite core library to the latest trunk code. Verify all version numbers in the 'testlinq' EF6 configuration files. check-in: a1cfefb6f2 user: mistachkin tags: trunk
2014-03-02
12:33
Update 32-bit only Eagle shell executable. check-in: d6cb0e33bf user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Doc/Extra/Provider/dbfactorysupport.html.

    81     81   <configuration>
    82     82     <system.data>
    83     83       <DbProviderFactories>
    84     84         <remove invariant="System.Data.SQLite"/>
    85     85         <add name="SQLite Data Provider" invariant="System.Data.SQLite" 
    86     86              description=".Net Framework Data Provider for SQLite"
    87     87              type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite,
    88         -                 Version=1.0.91.0, Culture=neutral,
           88  +                 Version=1.0.92.0, Culture=neutral,
    89     89                    PublicKeyToken=db937bc2d44ff139"/>
    90     90       </DbProviderFactories>
    91     91     </system.data>
    92     92   </configuration>
    93     93   </pre>
    94     94         </div>
    95     95         <p>

Changes to Doc/Extra/Provider/version.html.

    39     39             </td>
    40     40           </tr>
    41     41         </table>
    42     42       </div>
    43     43       <div id="mainSection">
    44     44       <div id="mainBody">
    45     45       <h1 class="heading">Version History</h1>
           46  +    <p><b>1.0.92.0 - March XX, 2014 <font color="red">(release scheduled)</font></b></p>
           47  +    <ul>
           48  +      <li>Updated to <a href="http://www.sqlite.org/src/info/trunk">SQLite 3.8.4</a>.</li>
           49  +      <li>When the TraceWarning connection flag is set, issue warnings about possibly malformed UNC paths. Pursuant to <a href="http://system.data.sqlite.org/index.html/info/283344397b">[283344397b]</a>.</li>
           50  +      <li>Enhancements to the NuGet packages, including the new &quot;modular&quot; packages.</li>
           51  +    </ul>
    46     52       <p><b>1.0.91.0 - February 12, 2014</b></p>
    47     53       <ul>
    48     54         <li>Updated to <a href="http://www.sqlite.org/releaselog/3_8_3_1.html">SQLite 3.8.3.1</a>.</li>
    49     55         <li>Refresh all included SQLite core library documentation (e.g. SQL syntax).</li>
    50     56         <li>Add support for <a href="http://entityframework.codeplex.com/">Entity Framework 6</a>.</li>
    51     57         <li>Add support for per-connection mappings between type names and DbType values. Pursuant to <a href="http://system.data.sqlite.org/index.html/info/e87af1d06a">[e87af1d06a]</a>.</li>
    52     58         <li>Modify the namespace used for all internal classes in the System.Data.SQLite.Linq assembly.&nbsp;<b>** Potentially Incompatible Change **</b></li>

Changes to Doc/Extra/Provider/welcome.html.

   156    156           <font color="red">
   157    157             Itanium processor support not currently included.
   158    158           </font>
   159    159         </p>
   160    160         <h1 class="heading">Distributing the Binaries (Compact Framework)</h1>
   161    161         <p>Both the <b>System.Data.SQLite.DLL </b>and <b>SQLite.Interop.XXX.DLL</b> files
   162    162           must be deployed on the Compact Framework.&nbsp; The XXX is the build number of
   163         -        the System.Data.SQLite library (e.g. &quot;091&quot;).&nbsp; The
          163  +        the System.Data.SQLite library (e.g. &quot;092&quot;).&nbsp; The
   164    164           <b>SQLite.Interop.XXX.DLL</b> file is a fully native assembly compiled for
   165    165           the ARM processor, and System.Data.SQLite is the fully-managed Compact
   166    166           Framework assembly.</p>
   167    167         <hr />
   168    168         <div id="footer">
   169    169           <p>
   170    170             <a href="mailto:sqlite-users@sqlite.org?subject=SQLite.NET%20Class%20Library%20Documentation%20Feedback:%20Welcome">
   171    171             Send comments on this topic.<!--[if gte IE 5]><tool:tip element="seeAlsoToolTip" avoidmouse="false" /><tool:tip element="languageFilterToolTip" avoidmouse="false" /><![endif]-->    </div>
   172    172       </div>
   173    173     </body>
   174    174   </html>

Changes to NuGet/SQLite.Beta.nuspec.

     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite.Beta</id>
    13     13       <title>System.Data.SQLite (x86/x64) Beta</title>
    14         -    <version>1.0.91.3</version>
           14  +    <version>1.0.92.0</version>
    15     15       <authors>SQLite Development Team</authors>
    16     16       <description>The official SQLite database engine for both x86 and x64 along with the ADO.NET provider.</description>
    17     17       <language>en-US</language>
    18     18       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    19     19       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    20     20       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    21     21       <tags>sqlite database ado.net provider interop</tags>

Changes to NuGet/SQLite.Core.nuspec.

     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite.Core</id>
    13     13       <title>System.Data.SQLite Core (x86/x64)</title>
    14         -    <version>1.0.91.3</version>
           14  +    <version>1.0.92.0</version>
    15     15       <authors>SQLite Development Team</authors>
    16     16       <description>The official SQLite database engine for both x86 and x64 along with the ADO.NET provider.</description>
    17     17       <language>en-US</language>
    18     18       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    19     19       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    20     20       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    21     21       <tags>sqlite database ado.net provider interop</tags>

Changes to NuGet/SQLite.EF6.nuspec.

     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite.EF6</id>
    13     13       <title>System.Data.SQLite EF6 (x86/x64)</title>
    14         -    <version>1.0.91.3</version>
           14  +    <version>1.0.92.0</version>
    15     15       <authors>SQLite Development Team</authors>
    16     16       <description>Support for Entity Framework 6 using System.Data.SQLite.</description>
    17     17       <language>en-US</language>
    18     18       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    19     19       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    20     20       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    21     21       <tags>sqlite database ado.net provider interop</tags>
    22     22       <copyright>Public Domain</copyright>
    23     23       <dependencies>
    24     24         <group targetFramework="net40">
    25         -        <dependency id="System.Data.SQLite.Core" version="1.0.91.3" />
           25  +        <dependency id="System.Data.SQLite.Core" version="1.0.92.0" />
    26     26         </group>
    27     27         <group targetFramework="net45">
    28         -        <dependency id="System.Data.SQLite.Core" version="1.0.91.3" />
           28  +        <dependency id="System.Data.SQLite.Core" version="1.0.92.0" />
    29     29         </group>
    30     30         <group targetFramework="net451">
    31         -        <dependency id="System.Data.SQLite.Core" version="1.0.91.3" />
           31  +        <dependency id="System.Data.SQLite.Core" version="1.0.92.0" />
    32     32         </group>
    33     33         <group targetFramework="net40">
    34     34           <dependency id="EntityFramework" version="6.0.0.0" />
    35     35         </group>
    36     36         <group targetFramework="net45">
    37     37           <dependency id="EntityFramework" version="6.0.0.0" />
    38     38         </group>

Changes to NuGet/SQLite.Linq.nuspec.

     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite.Linq</id>
    13     13       <title>System.Data.SQLite LINQ (x86/x64)</title>
    14         -    <version>1.0.91.3</version>
           14  +    <version>1.0.92.0</version>
    15     15       <authors>SQLite Development Team</authors>
    16     16       <description>Support for LINQ using System.Data.SQLite.</description>
    17     17       <language>en-US</language>
    18     18       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    19     19       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    20     20       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    21     21       <tags>sqlite database ado.net provider interop</tags>
    22     22       <copyright>Public Domain</copyright>
    23     23       <dependencies>
    24     24         <group targetFramework="net20">
    25         -        <dependency id="System.Data.SQLite.Core" version="1.0.91.3" />
           25  +        <dependency id="System.Data.SQLite.Core" version="1.0.92.0" />
    26     26         </group>
    27     27         <group targetFramework="net40">
    28         -        <dependency id="System.Data.SQLite.Core" version="1.0.91.3" />
           28  +        <dependency id="System.Data.SQLite.Core" version="1.0.92.0" />
    29     29         </group>
    30     30         <group targetFramework="net45">
    31         -        <dependency id="System.Data.SQLite.Core" version="1.0.91.3" />
           31  +        <dependency id="System.Data.SQLite.Core" version="1.0.92.0" />
    32     32         </group>
    33     33         <group targetFramework="net451">
    34         -        <dependency id="System.Data.SQLite.Core" version="1.0.91.3" />
           34  +        <dependency id="System.Data.SQLite.Core" version="1.0.92.0" />
    35     35         </group>
    36     36       </dependencies>
    37     37     </metadata>
    38     38     <files>
    39     39       <file src="..\bin\2008\Release\bin\System.Data.SQLite.Linq.dll" target="lib\net20" />
    40     40       <file src="..\bin\2010\Release\bin\System.Data.SQLite.Linq.dll" target="lib\net40" />
    41     41       <file src="..\bin\2012\Release\bin\System.Data.SQLite.Linq.dll" target="lib\net45" />

Changes to NuGet/SQLite.MSIL.nuspec.

     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite.MSIL</id>
    13         -    <version>1.0.91.3</version>
           13  +    <version>1.0.92.0</version>
    14     14       <authors>SQLite Development Team</authors>
    15     15       <description>An ADO.NET provider for SQLite (managed-only).</description>
    16     16       <language>en-US</language>
    17     17       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    18     18       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    19     19       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    20     20       <tags>sqlite database ado.net provider interop</tags>

Changes to NuGet/SQLite.nuspec.

     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite</id>
    13     13       <title>System.Data.SQLite (x86/x64)</title>
    14         -    <version>1.0.91.3</version>
           14  +    <version>1.0.92.0</version>
    15     15       <authors>SQLite Development Team</authors>
    16     16       <description>The official SQLite database engine for both x86 and x64 along with the ADO.NET provider.  This package includes support for LINQ and Entity Framework 6.</description>
    17     17       <language>en-US</language>
    18     18       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    19     19       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    20     20       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    21     21       <tags>sqlite database ado.net provider interop</tags>

Changes to NuGet/SQLite.x64.nuspec.

     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite.x64</id>
    13         -    <version>1.0.91.3</version>
           13  +    <version>1.0.92.0</version>
    14     14       <authors>SQLite Development Team</authors>
    15     15       <description>This is a legacy package; if possible, please use either the &quot;System.Data.SQLite&quot; or &quot;System.Data.SQLite.Core&quot; package instead.  The official SQLite database engine combined with a complete ADO.NET provider all rolled into a single mixed-mode assembly for x64.</description>
    16     16       <language>en-US</language>
    17     17       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    18     18       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    19     19       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    20     20       <tags>sqlite database ado.net provider interop</tags>

Changes to NuGet/SQLite.x86.nuspec.

     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <package>
    11     11     <metadata>
    12     12       <id>System.Data.SQLite.x86</id>
    13         -    <version>1.0.91.3</version>
           13  +    <version>1.0.92.0</version>
    14     14       <authors>SQLite Development Team</authors>
    15     15       <description>This is a legacy package; if possible, please use either the &quot;System.Data.SQLite&quot; or &quot;System.Data.SQLite.Core&quot; package instead.  The official SQLite database engine combined with a complete ADO.NET provider all rolled into a single mixed-mode assembly for x86.</description>
    16     16       <language>en-US</language>
    17     17       <projectUrl>http://system.data.sqlite.org/</projectUrl>
    18     18       <iconUrl>http://system.data.sqlite.org/images/sqlite32.png</iconUrl>
    19     19       <licenseUrl>http://www.sqlite.org/copyright.html</licenseUrl>
    20     20       <tags>sqlite database ado.net provider interop</tags>

Changes to SQLite.Designer/AssemblyInfo.cs.

    39     39   //      Major Version
    40     40   //      Minor Version 
    41     41   //      Build Number
    42     42   //      Revision
    43     43   //
    44     44   // You can specify all the values or you can default the Revision and Build Numbers 
    45     45   // by using the '*' as shown below:
    46         -[assembly: AssemblyVersion("1.0.91.0")]
    47         -[assembly: AssemblyFileVersion("1.0.91.0")]
           46  +[assembly: AssemblyVersion("1.0.92.0")]
           47  +[assembly: AssemblyFileVersion("1.0.92.0")]

Changes to SQLite.Designer/source.extension.vsixmanifest.

     1      1   <?xml version="1.0" encoding="utf-8"?>
     2      2   <Vsix Version="1.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010">
     3      3     <Identifier Id="67b5f3a9-cde1-430f-a12b-af95bb064851">
     4      4       <Name>System.Data.SQLite Designer</Name>
     5      5       <Author>http://system.data.sqlite.org/</Author>
     6         -    <Version>1.0.91.0</Version>
            6  +    <Version>1.0.92.0</Version>
     7      7       <Description>ADO.NET Data Designer for SQLite</Description>
     8      8       <Locale>1033</Locale>
     9      9       <InstalledByMsi>false</InstalledByMsi>
    10     10       <SupportedProducts>
    11     11         <VisualStudio Version="10.0">
    12     12           <Edition>Pro</Edition>
    13     13         </VisualStudio>

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

    15     15   	<UserMacro
    16     16   		Name="ConfigurationYear"
    17     17   		Value="2005"
    18     18   		PerformEnvironmentSet="true"
    19     19   	/>
    20     20   	<UserMacro
    21     21   		Name="INTEROP_BUILD_NUMBER"
    22         -		Value="091"
           22  +		Value="092"
    23     23   		PerformEnvironmentSet="true"
    24     24   	/>
    25     25   	<UserMacro
    26     26   		Name="INTEROP_LINKER_VERSION"
    27     27   		Value="1.0"
    28     28   		PerformEnvironmentSet="true"
    29     29   	/>
    30     30   	<UserMacro
    31     31   		Name="INTEROP_MANIFEST_VERSION"
    32         -		Value="1.0.91.0"
           32  +		Value="1.0.92.0"
    33     33   		PerformEnvironmentSet="true"
    34     34   	/>
    35     35   	<UserMacro
    36     36   		Name="INTEROP_RC_VERSION"
    37         -		Value="1,0,91,0"
           37  +		Value="1,0,92,0"
    38     38   		PerformEnvironmentSet="true"
    39     39   	/>
    40     40   	<UserMacro
    41     41   		Name="INTEROP_INCLUDE_DIRECTORIES"
    42     42   		Value="src\core"
    43     43   		PerformEnvironmentSet="true"
    44     44   	/>

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

    15     15   	<UserMacro
    16     16   		Name="ConfigurationYear"
    17     17   		Value="2008"
    18     18   		PerformEnvironmentSet="true"
    19     19   	/>
    20     20   	<UserMacro
    21     21   		Name="INTEROP_BUILD_NUMBER"
    22         -		Value="091"
           22  +		Value="092"
    23     23   		PerformEnvironmentSet="true"
    24     24   	/>
    25     25   	<UserMacro
    26     26   		Name="INTEROP_LINKER_VERSION"
    27     27   		Value="1.0"
    28     28   		PerformEnvironmentSet="true"
    29     29   	/>
    30     30   	<UserMacro
    31     31   		Name="INTEROP_MANIFEST_VERSION"
    32         -		Value="1.0.91.0"
           32  +		Value="1.0.92.0"
    33     33   		PerformEnvironmentSet="true"
    34     34   	/>
    35     35   	<UserMacro
    36     36   		Name="INTEROP_RC_VERSION"
    37         -		Value="1,0,91,0"
           37  +		Value="1,0,92,0"
    38     38   		PerformEnvironmentSet="true"
    39     39   	/>
    40     40   	<UserMacro
    41     41   		Name="INTEROP_INCLUDE_DIRECTORIES"
    42     42   		Value="src\core"
    43     43   		PerformEnvironmentSet="true"
    44     44   	/>

Changes to SQLite.Interop/props/SQLite.Interop.2010.props.

     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    11     11     <PropertyGroup Label="UserMacros">
    12     12       <ConfigurationYear>2010</ConfigurationYear>
    13         -    <INTEROP_BUILD_NUMBER>091</INTEROP_BUILD_NUMBER>
           13  +    <INTEROP_BUILD_NUMBER>092</INTEROP_BUILD_NUMBER>
    14     14       <INTEROP_LINKER_VERSION>1.0</INTEROP_LINKER_VERSION>
    15         -    <INTEROP_MANIFEST_VERSION>1.0.91.0</INTEROP_MANIFEST_VERSION>
    16         -    <INTEROP_RC_VERSION>1,0,91,0</INTEROP_RC_VERSION>
           15  +    <INTEROP_MANIFEST_VERSION>1.0.92.0</INTEROP_MANIFEST_VERSION>
           16  +    <INTEROP_RC_VERSION>1,0,92,0</INTEROP_RC_VERSION>
    17     17       <INTEROP_INCLUDE_DIRECTORIES>src\core</INTEROP_INCLUDE_DIRECTORIES>
    18     18       <INTEROP_LIBRARY_DIRECTORIES></INTEROP_LIBRARY_DIRECTORIES>
    19     19       <INTEROP_LIBRARY_DEPENDENCIES></INTEROP_LIBRARY_DEPENDENCIES>
    20     20       <INTEROP_DEBUG_DEFINES>INTEROP_DEBUG=0x31F;INTEROP_LOG=1;INTEROP_TEST_EXTENSION=1</INTEROP_DEBUG_DEFINES>
    21     21       <INTEROP_EXTRA_DEFINES>INTEROP_EXTENSION_FUNCTIONS=1;INTEROP_CODEC=1;INTEROP_VIRTUAL_TABLE=1;INTEROP_PERCENTILE_EXTENSION=1;INTEROP_TOTYPE_EXTENSION=1;INTEROP_REGEXP_EXTENSION=1</INTEROP_EXTRA_DEFINES>
    22     22       <INTEROP_ASSEMBLY_RESOURCES>/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteCommand.bmp,System.Data.SQLite.SQLiteCommand.bmp /ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteConnection.bmp,System.Data.SQLite.SQLiteConnection.bmp /ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteDataAdapter.bmp,System.Data.SQLite.SQLiteDataAdapter.bmp</INTEROP_ASSEMBLY_RESOURCES>
    23     23       <INTEROP_KEY_FILE>$(ProjectDir)..\System.Data.SQLite\System.Data.SQLite.snk</INTEROP_KEY_FILE>

Changes to SQLite.Interop/props/SQLite.Interop.2012.props.

     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    11     11     <PropertyGroup Label="UserMacros">
    12     12       <ConfigurationYear>2012</ConfigurationYear>
    13         -    <INTEROP_BUILD_NUMBER>091</INTEROP_BUILD_NUMBER>
           13  +    <INTEROP_BUILD_NUMBER>092</INTEROP_BUILD_NUMBER>
    14     14       <INTEROP_LINKER_VERSION>1.0</INTEROP_LINKER_VERSION>
    15         -    <INTEROP_MANIFEST_VERSION>1.0.91.0</INTEROP_MANIFEST_VERSION>
    16         -    <INTEROP_RC_VERSION>1,0,91,0</INTEROP_RC_VERSION>
           15  +    <INTEROP_MANIFEST_VERSION>1.0.92.0</INTEROP_MANIFEST_VERSION>
           16  +    <INTEROP_RC_VERSION>1,0,92,0</INTEROP_RC_VERSION>
    17     17       <INTEROP_INCLUDE_DIRECTORIES>src\core</INTEROP_INCLUDE_DIRECTORIES>
    18     18       <INTEROP_LIBRARY_DIRECTORIES></INTEROP_LIBRARY_DIRECTORIES>
    19     19       <INTEROP_LIBRARY_DEPENDENCIES></INTEROP_LIBRARY_DEPENDENCIES>
    20     20       <INTEROP_DEBUG_DEFINES>INTEROP_DEBUG=0x31F;INTEROP_LOG=1;INTEROP_TEST_EXTENSION=1</INTEROP_DEBUG_DEFINES>
    21     21       <INTEROP_EXTRA_DEFINES>INTEROP_EXTENSION_FUNCTIONS=1;INTEROP_CODEC=1;INTEROP_VIRTUAL_TABLE=1;INTEROP_PERCENTILE_EXTENSION=1;INTEROP_TOTYPE_EXTENSION=1;INTEROP_REGEXP_EXTENSION=1</INTEROP_EXTRA_DEFINES>
    22     22       <INTEROP_ASSEMBLY_RESOURCES>/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteCommand.bmp,System.Data.SQLite.SQLiteCommand.bmp /ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteConnection.bmp,System.Data.SQLite.SQLiteConnection.bmp /ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteDataAdapter.bmp,System.Data.SQLite.SQLiteDataAdapter.bmp</INTEROP_ASSEMBLY_RESOURCES>
    23     23       <INTEROP_KEY_FILE>$(ProjectDir)..\System.Data.SQLite\System.Data.SQLite.snk</INTEROP_KEY_FILE>

Changes to SQLite.Interop/props/SQLite.Interop.2013.props.

     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0">
    11     11     <PropertyGroup Label="UserMacros">
    12     12       <ConfigurationYear>2013</ConfigurationYear>
    13         -    <INTEROP_BUILD_NUMBER>091</INTEROP_BUILD_NUMBER>
           13  +    <INTEROP_BUILD_NUMBER>092</INTEROP_BUILD_NUMBER>
    14     14       <INTEROP_LINKER_VERSION>1.0</INTEROP_LINKER_VERSION>
    15         -    <INTEROP_MANIFEST_VERSION>1.0.91.0</INTEROP_MANIFEST_VERSION>
    16         -    <INTEROP_RC_VERSION>1,0,91,0</INTEROP_RC_VERSION>
           15  +    <INTEROP_MANIFEST_VERSION>1.0.92.0</INTEROP_MANIFEST_VERSION>
           16  +    <INTEROP_RC_VERSION>1,0,92,0</INTEROP_RC_VERSION>
    17     17       <INTEROP_INCLUDE_DIRECTORIES>src\core</INTEROP_INCLUDE_DIRECTORIES>
    18     18       <INTEROP_LIBRARY_DIRECTORIES></INTEROP_LIBRARY_DIRECTORIES>
    19     19       <INTEROP_LIBRARY_DEPENDENCIES></INTEROP_LIBRARY_DEPENDENCIES>
    20     20       <INTEROP_DEBUG_DEFINES>INTEROP_DEBUG=0x31F;INTEROP_LOG=1;INTEROP_TEST_EXTENSION=1</INTEROP_DEBUG_DEFINES>
    21     21       <INTEROP_EXTRA_DEFINES>INTEROP_EXTENSION_FUNCTIONS=1;INTEROP_CODEC=1;INTEROP_VIRTUAL_TABLE=1;INTEROP_PERCENTILE_EXTENSION=1;INTEROP_TOTYPE_EXTENSION=1;INTEROP_REGEXP_EXTENSION=1</INTEROP_EXTRA_DEFINES>
    22     22       <INTEROP_ASSEMBLY_RESOURCES>/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteCommand.bmp,System.Data.SQLite.SQLiteCommand.bmp /ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteConnection.bmp,System.Data.SQLite.SQLiteConnection.bmp /ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteDataAdapter.bmp,System.Data.SQLite.SQLiteDataAdapter.bmp</INTEROP_ASSEMBLY_RESOURCES>
    23     23       <INTEROP_KEY_FILE>$(ProjectDir)..\System.Data.SQLite\System.Data.SQLite.snk</INTEROP_KEY_FILE>

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

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

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

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

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

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.8.3.1.  By combining all the individual C code files into this 
            3  +** version 3.8.4.  By combining all the individual C code files into this 
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
    21     21   #define SQLITE_AMALGAMATION 1
    22     22   #ifndef SQLITE_PRIVATE
    23     23   # define SQLITE_PRIVATE static
    24     24   #endif
    25     25   #ifndef SQLITE_API
    26     26   # define SQLITE_API
    27     27   #endif
           28  +/************** Begin file sqliteInt.h ***************************************/
           29  +/*
           30  +** 2001 September 15
           31  +**
           32  +** The author disclaims copyright to this source code.  In place of
           33  +** a legal notice, here is a blessing:
           34  +**
           35  +**    May you do good and not evil.
           36  +**    May you find forgiveness for yourself and forgive others.
           37  +**    May you share freely, never taking more than you give.
           38  +**
           39  +*************************************************************************
           40  +** Internal interface definitions for SQLite.
           41  +**
           42  +*/
           43  +#ifndef _SQLITEINT_H_
           44  +#define _SQLITEINT_H_
           45  +
           46  +/*
           47  +** These #defines should enable >2GB file support on POSIX if the
           48  +** underlying operating system supports it.  If the OS lacks
           49  +** large file support, or if the OS is windows, these should be no-ops.
           50  +**
           51  +** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
           52  +** system #includes.  Hence, this block of code must be the very first
           53  +** code in all source files.
           54  +**
           55  +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
           56  +** on the compiler command line.  This is necessary if you are compiling
           57  +** on a recent machine (ex: Red Hat 7.2) but you want your code to work
           58  +** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
           59  +** without this option, LFS is enable.  But LFS does not exist in the kernel
           60  +** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
           61  +** portability you should omit LFS.
           62  +**
           63  +** The previous paragraph was written in 2005.  (This paragraph is written
           64  +** on 2008-11-28.) These days, all Linux kernels support large files, so
           65  +** you should probably leave LFS enabled.  But some embedded platforms might
           66  +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
           67  +**
           68  +** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
           69  +*/
           70  +#ifndef SQLITE_DISABLE_LFS
           71  +# define _LARGE_FILE       1
           72  +# ifndef _FILE_OFFSET_BITS
           73  +#   define _FILE_OFFSET_BITS 64
           74  +# endif
           75  +# define _LARGEFILE_SOURCE 1
           76  +#endif
           77  +
           78  +/*
           79  +** For MinGW, check to see if we can include the header file containing its
           80  +** version information, among other things.  Normally, this internal MinGW
           81  +** header file would [only] be included automatically by other MinGW header
           82  +** files; however, the contained version information is now required by this
           83  +** header file to work around binary compatibility issues (see below) and
           84  +** this is the only known way to reliably obtain it.  This entire #if block
           85  +** would be completely unnecessary if there was any other way of detecting
           86  +** MinGW via their preprocessor (e.g. if they customized their GCC to define
           87  +** some MinGW-specific macros).  When compiling for MinGW, either the
           88  +** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
           89  +** defined; otherwise, detection of conditions specific to MinGW will be
           90  +** disabled.
           91  +*/
           92  +#if defined(_HAVE_MINGW_H)
           93  +# include "mingw.h"
           94  +#elif defined(_HAVE__MINGW_H)
           95  +# include "_mingw.h"
           96  +#endif
           97  +
           98  +/*
           99  +** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
          100  +** define is required to maintain binary compatibility with the MSVC runtime
          101  +** library in use (e.g. for Windows XP).
          102  +*/
          103  +#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
          104  +    defined(_WIN32) && !defined(_WIN64) && \
          105  +    defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
          106  +    defined(__MSVCRT__)
          107  +# define _USE_32BIT_TIME_T
          108  +#endif
          109  +
          110  +/* The public SQLite interface.  The _FILE_OFFSET_BITS macro must appear
          111  +** first in QNX.  Also, the _USE_32BIT_TIME_T macro must appear first for
          112  +** MinGW.
          113  +*/
          114  +/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
    28    115   /************** Begin file sqlite3.h *****************************************/
    29    116   /*
    30    117   ** 2001 September 15
    31    118   **
    32    119   ** The author disclaims copyright to this source code.  In place of
    33    120   ** a legal notice, here is a blessing:
    34    121   **
................................................................................
   131    218   ** string contains the date and time of the check-in (UTC) and an SHA1
   132    219   ** hash of the entire source tree.
   133    220   **
   134    221   ** See also: [sqlite3_libversion()],
   135    222   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   136    223   ** [sqlite_version()] and [sqlite_source_id()].
   137    224   */
   138         -#define SQLITE_VERSION        "3.8.3.1"
   139         -#define SQLITE_VERSION_NUMBER 3008003
   140         -#define SQLITE_SOURCE_ID      "2014-02-11 14:52:19 ea3317a4803d71d88183b29f1d3086f46d68a00e"
          225  +#define SQLITE_VERSION        "3.8.4"
          226  +#define SQLITE_VERSION_NUMBER 3008004
          227  +#define SQLITE_SOURCE_ID      "2014-03-06 00:30:27 29b0a4f158785449b6f3da6fcceeb63442c9711c"
   141    228   
   142    229   /*
   143    230   ** CAPI3REF: Run-Time Library Version Numbers
   144    231   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   145    232   **
   146    233   ** These interfaces provide the same information as the [SQLITE_VERSION],
   147    234   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  6146   6233   #define SQLITE_TESTCTRL_RESERVE                 14
  6147   6234   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6148   6235   #define SQLITE_TESTCTRL_ISKEYWORD               16
  6149   6236   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6150   6237   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6151   6238   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
  6152   6239   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6153         -#define SQLITE_TESTCTRL_LAST                    20
         6240  +#define SQLITE_TESTCTRL_VDBE_COVERAGE           21
         6241  +#define SQLITE_TESTCTRL_LAST                    21
  6154   6242   
  6155   6243   /*
  6156   6244   ** CAPI3REF: SQLite Runtime Status
  6157   6245   **
  6158   6246   ** ^This interface is used to retrieve runtime status information
  6159   6247   ** about the performance of SQLite, and optionally to reset various
  6160   6248   ** highwater marks.  ^The first argument is an integer code for
................................................................................
  7409   7497   }  /* end of the 'extern "C"' block */
  7410   7498   #endif
  7411   7499   
  7412   7500   #endif  /* ifndef _SQLITE3RTREE_H_ */
  7413   7501   
  7414   7502   
  7415   7503   /************** End of sqlite3.h *********************************************/
  7416         -/************** Begin file sqliteInt.h ***************************************/
  7417         -/*
  7418         -** 2001 September 15
  7419         -**
  7420         -** The author disclaims copyright to this source code.  In place of
  7421         -** a legal notice, here is a blessing:
  7422         -**
  7423         -**    May you do good and not evil.
  7424         -**    May you find forgiveness for yourself and forgive others.
  7425         -**    May you share freely, never taking more than you give.
  7426         -**
  7427         -*************************************************************************
  7428         -** Internal interface definitions for SQLite.
  7429         -**
  7430         -*/
  7431         -#ifndef _SQLITEINT_H_
  7432         -#define _SQLITEINT_H_
  7433         -
  7434         -/*
  7435         -** These #defines should enable >2GB file support on POSIX if the
  7436         -** underlying operating system supports it.  If the OS lacks
  7437         -** large file support, or if the OS is windows, these should be no-ops.
  7438         -**
  7439         -** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
  7440         -** system #includes.  Hence, this block of code must be the very first
  7441         -** code in all source files.
  7442         -**
  7443         -** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
  7444         -** on the compiler command line.  This is necessary if you are compiling
  7445         -** on a recent machine (ex: Red Hat 7.2) but you want your code to work
  7446         -** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
  7447         -** without this option, LFS is enable.  But LFS does not exist in the kernel
  7448         -** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
  7449         -** portability you should omit LFS.
  7450         -**
  7451         -** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
  7452         -*/
  7453         -#ifndef SQLITE_DISABLE_LFS
  7454         -# define _LARGE_FILE       1
  7455         -# ifndef _FILE_OFFSET_BITS
  7456         -#   define _FILE_OFFSET_BITS 64
  7457         -# endif
  7458         -# define _LARGEFILE_SOURCE 1
  7459         -#endif
         7504  +/************** Continuing where we left off in sqliteInt.h ******************/
  7460   7505   
  7461   7506   /*
  7462   7507   ** Include the configuration header output by 'configure' if we're using the
  7463   7508   ** autoconf-based build
  7464   7509   */
  7465   7510   #ifdef _HAVE_SQLITE_CONFIG_H
  7466   7511   #include "config.h"
................................................................................
  8839   8884   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
  8840   8885   SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
  8841   8886   SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
  8842   8887   SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
  8843   8888   SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
  8844   8889   SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  8845   8890   SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  8846         -SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  8847         -SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  8848   8891   
  8849   8892   SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  8850   8893   SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  8851   8894   
  8852   8895   SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  8853   8896   SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  8854   8897   SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
................................................................................
  8980   9023       SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
  8981   9024       int (*xAdvance)(BtCursor *, int *);
  8982   9025     } p4;
  8983   9026   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
  8984   9027     char *zComment;          /* Comment to improve readability */
  8985   9028   #endif
  8986   9029   #ifdef VDBE_PROFILE
  8987         -  int cnt;                 /* Number of times this instruction was executed */
         9030  +  u32 cnt;                 /* Number of times this instruction was executed */
  8988   9031     u64 cycles;              /* Total time spent executing this instruction */
  8989   9032   #endif
         9033  +#ifdef SQLITE_VDBE_COVERAGE
         9034  +  int iSrcLine;            /* Source-code line that generated this opcode */
         9035  +#endif
  8990   9036   };
  8991   9037   typedef struct VdbeOp VdbeOp;
  8992   9038   
  8993   9039   
  8994   9040   /*
  8995   9041   ** A sub-routine used to implement a trigger program.
  8996   9042   */
................................................................................
  9092   9138   #define OP_Vacuum         13
  9093   9139   #define OP_VFilter        14 /* synopsis: iPlan=r[P3] zPlan='P4'           */
  9094   9140   #define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
  9095   9141   #define OP_Goto           16
  9096   9142   #define OP_Gosub          17
  9097   9143   #define OP_Return         18
  9098   9144   #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
  9099         -#define OP_Yield          20
  9100         -#define OP_HaltIfNull     21 /* synopsis: if r[P3] null then halt          */
  9101         -#define OP_Halt           22
  9102         -#define OP_Integer        23 /* synopsis: r[P2]=P1                         */
  9103         -#define OP_Int64          24 /* synopsis: r[P2]=P4                         */
  9104         -#define OP_String         25 /* synopsis: r[P2]='P4' (len=P1)              */
  9105         -#define OP_Null           26 /* synopsis: r[P2..P3]=NULL                   */
  9106         -#define OP_Blob           27 /* synopsis: r[P2]=P4 (len=P1)                */
  9107         -#define OP_Variable       28 /* synopsis: r[P2]=parameter(P1,P4)           */
  9108         -#define OP_Move           29 /* synopsis: r[P2@P3]=r[P1@P3]                */
  9109         -#define OP_Copy           30 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  9110         -#define OP_SCopy          31 /* synopsis: r[P2]=r[P1]                      */
  9111         -#define OP_ResultRow      32 /* synopsis: output=r[P1@P2]                  */
  9112         -#define OP_CollSeq        33
  9113         -#define OP_AddImm         34 /* synopsis: r[P1]=r[P1]+P2                   */
  9114         -#define OP_MustBeInt      35
  9115         -#define OP_RealAffinity   36
  9116         -#define OP_Permutation    37
  9117         -#define OP_Compare        38
  9118         -#define OP_Jump           39
  9119         -#define OP_Once           40
  9120         -#define OP_If             41
  9121         -#define OP_IfNot          42
  9122         -#define OP_Column         43 /* synopsis: r[P3]=PX                         */
  9123         -#define OP_Affinity       44 /* synopsis: affinity(r[P1@P2])               */
  9124         -#define OP_MakeRecord     45 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  9125         -#define OP_Count          46 /* synopsis: r[P2]=count()                    */
  9126         -#define OP_ReadCookie     47
  9127         -#define OP_SetCookie      48
  9128         -#define OP_VerifyCookie   49
  9129         -#define OP_OpenRead       50 /* synopsis: root=P2 iDb=P3                   */
  9130         -#define OP_OpenWrite      51 /* synopsis: root=P2 iDb=P3                   */
  9131         -#define OP_OpenAutoindex  52 /* synopsis: nColumn=P2                       */
  9132         -#define OP_OpenEphemeral  53 /* synopsis: nColumn=P2                       */
  9133         -#define OP_SorterOpen     54
  9134         -#define OP_OpenPseudo     55 /* synopsis: content in r[P2@P3]              */
  9135         -#define OP_Close          56
  9136         -#define OP_SeekLt         57 /* synopsis: key=r[P3@P4]                     */
  9137         -#define OP_SeekLe         58 /* synopsis: key=r[P3@P4]                     */
  9138         -#define OP_SeekGe         59 /* synopsis: key=r[P3@P4]                     */
  9139         -#define OP_SeekGt         60 /* synopsis: key=r[P3@P4]                     */
  9140         -#define OP_Seek           61 /* synopsis: intkey=r[P2]                     */
  9141         -#define OP_NoConflict     62 /* synopsis: key=r[P3@P4]                     */
  9142         -#define OP_NotFound       63 /* synopsis: key=r[P3@P4]                     */
  9143         -#define OP_Found          64 /* synopsis: key=r[P3@P4]                     */
  9144         -#define OP_NotExists      65 /* synopsis: intkey=r[P3]                     */
  9145         -#define OP_Sequence       66 /* synopsis: r[P2]=rowid                      */
  9146         -#define OP_NewRowid       67 /* synopsis: r[P2]=rowid                      */
  9147         -#define OP_Insert         68 /* synopsis: intkey=r[P3] data=r[P2]          */
  9148         -#define OP_InsertInt      69 /* synopsis: intkey=P3 data=r[P2]             */
  9149         -#define OP_Delete         70
         9145  +#define OP_InitCoroutine  20
         9146  +#define OP_EndCoroutine   21
         9147  +#define OP_Yield          22
         9148  +#define OP_HaltIfNull     23 /* synopsis: if r[P3]=null halt               */
         9149  +#define OP_Halt           24
         9150  +#define OP_Integer        25 /* synopsis: r[P2]=P1                         */
         9151  +#define OP_Int64          26 /* synopsis: r[P2]=P4                         */
         9152  +#define OP_String         27 /* synopsis: r[P2]='P4' (len=P1)              */
         9153  +#define OP_Null           28 /* synopsis: r[P2..P3]=NULL                   */
         9154  +#define OP_SoftNull       29 /* synopsis: r[P1]=NULL                       */
         9155  +#define OP_Blob           30 /* synopsis: r[P2]=P4 (len=P1)                */
         9156  +#define OP_Variable       31 /* synopsis: r[P2]=parameter(P1,P4)           */
         9157  +#define OP_Move           32 /* synopsis: r[P2@P3]=r[P1@P3]                */
         9158  +#define OP_Copy           33 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
         9159  +#define OP_SCopy          34 /* synopsis: r[P2]=r[P1]                      */
         9160  +#define OP_ResultRow      35 /* synopsis: output=r[P1@P2]                  */
         9161  +#define OP_CollSeq        36
         9162  +#define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
         9163  +#define OP_MustBeInt      38
         9164  +#define OP_RealAffinity   39
         9165  +#define OP_Permutation    40
         9166  +#define OP_Compare        41
         9167  +#define OP_Jump           42
         9168  +#define OP_Once           43
         9169  +#define OP_If             44
         9170  +#define OP_IfNot          45
         9171  +#define OP_Column         46 /* synopsis: r[P3]=PX                         */
         9172  +#define OP_Affinity       47 /* synopsis: affinity(r[P1@P2])               */
         9173  +#define OP_MakeRecord     48 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
         9174  +#define OP_Count          49 /* synopsis: r[P2]=count()                    */
         9175  +#define OP_ReadCookie     50
         9176  +#define OP_SetCookie      51
         9177  +#define OP_OpenRead       52 /* synopsis: root=P2 iDb=P3                   */
         9178  +#define OP_OpenWrite      53 /* synopsis: root=P2 iDb=P3                   */
         9179  +#define OP_OpenAutoindex  54 /* synopsis: nColumn=P2                       */
         9180  +#define OP_OpenEphemeral  55 /* synopsis: nColumn=P2                       */
         9181  +#define OP_SorterOpen     56
         9182  +#define OP_OpenPseudo     57 /* synopsis: P3 columns in r[P2]              */
         9183  +#define OP_Close          58
         9184  +#define OP_SeekLT         59
         9185  +#define OP_SeekLE         60
         9186  +#define OP_SeekGE         61
         9187  +#define OP_SeekGT         62
         9188  +#define OP_Seek           63 /* synopsis: intkey=r[P2]                     */
         9189  +#define OP_NoConflict     64 /* synopsis: key=r[P3@P4]                     */
         9190  +#define OP_NotFound       65 /* synopsis: key=r[P3@P4]                     */
         9191  +#define OP_Found          66 /* synopsis: key=r[P3@P4]                     */
         9192  +#define OP_NotExists      67 /* synopsis: intkey=r[P3]                     */
         9193  +#define OP_Sequence       68 /* synopsis: r[P2]=rowid                      */
         9194  +#define OP_NewRowid       69 /* synopsis: r[P2]=rowid                      */
         9195  +#define OP_Insert         70 /* synopsis: intkey=r[P3] data=r[P2]          */
  9150   9196   #define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  9151   9197   #define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  9152         -#define OP_ResetCount     73
  9153         -#define OP_SorterCompare  74 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
  9154         -#define OP_SorterData     75 /* synopsis: r[P2]=data                       */
         9198  +#define OP_InsertInt      73 /* synopsis: intkey=P3 data=r[P2]             */
         9199  +#define OP_Delete         74
         9200  +#define OP_ResetCount     75
  9155   9201   #define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  9156   9202   #define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  9157   9203   #define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  9158   9204   #define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  9159   9205   #define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  9160   9206   #define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  9161   9207   #define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  9162   9208   #define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  9163         -#define OP_RowKey         84 /* synopsis: r[P2]=key                        */
         9209  +#define OP_SorterCompare  84 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
  9164   9210   #define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  9165   9211   #define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  9166   9212   #define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  9167   9213   #define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  9168   9214   #define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  9169   9215   #define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  9170   9216   #define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  9171   9217   #define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  9172   9218   #define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  9173   9219   #define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  9174         -#define OP_RowData        95 /* synopsis: r[P2]=data                       */
         9220  +#define OP_SorterData     95 /* synopsis: r[P2]=data                       */
  9175   9221   #define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  9176   9222   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  9177         -#define OP_Rowid          98 /* synopsis: r[P2]=rowid                      */
  9178         -#define OP_NullRow        99
  9179         -#define OP_Last          100
  9180         -#define OP_SorterSort    101
  9181         -#define OP_Sort          102
  9182         -#define OP_Rewind        103
  9183         -#define OP_SorterInsert  104
  9184         -#define OP_IdxInsert     105 /* synopsis: key=r[P2]                        */
  9185         -#define OP_IdxDelete     106 /* synopsis: key=r[P2@P3]                     */
  9186         -#define OP_IdxRowid      107 /* synopsis: r[P2]=rowid                      */
  9187         -#define OP_IdxLT         108 /* synopsis: key=r[P3@P4]                     */
  9188         -#define OP_IdxGE         109 /* synopsis: key=r[P3@P4]                     */
  9189         -#define OP_Destroy       110
  9190         -#define OP_Clear         111
  9191         -#define OP_CreateIndex   112 /* synopsis: r[P2]=root iDb=P1                */
  9192         -#define OP_CreateTable   113 /* synopsis: r[P2]=root iDb=P1                */
  9193         -#define OP_ParseSchema   114
  9194         -#define OP_LoadAnalysis  115
  9195         -#define OP_DropTable     116
  9196         -#define OP_DropIndex     117
  9197         -#define OP_DropTrigger   118
  9198         -#define OP_IntegrityCk   119
  9199         -#define OP_RowSetAdd     120 /* synopsis: rowset(P1)=r[P2]                 */
  9200         -#define OP_RowSetRead    121 /* synopsis: r[P3]=rowset(P1)                 */
  9201         -#define OP_RowSetTest    122 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  9202         -#define OP_Program       123
  9203         -#define OP_Param         124
  9204         -#define OP_FkCounter     125 /* synopsis: fkctr[P1]+=P2                    */
  9205         -#define OP_FkIfZero      126 /* synopsis: if fkctr[P1]==0 goto P2          */
  9206         -#define OP_MemMax        127 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  9207         -#define OP_IfPos         128 /* synopsis: if r[P1]>0 goto P2               */
  9208         -#define OP_IfNeg         129 /* synopsis: if r[P1]<0 goto P2               */
  9209         -#define OP_IfZero        130 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
  9210         -#define OP_AggFinal      131 /* synopsis: accum=r[P1] N=P2                 */
  9211         -#define OP_IncrVacuum    132
         9223  +#define OP_RowKey         98 /* synopsis: r[P2]=key                        */
         9224  +#define OP_RowData        99 /* synopsis: r[P2]=data                       */
         9225  +#define OP_Rowid         100 /* synopsis: r[P2]=rowid                      */
         9226  +#define OP_NullRow       101
         9227  +#define OP_Last          102
         9228  +#define OP_SorterSort    103
         9229  +#define OP_Sort          104
         9230  +#define OP_Rewind        105
         9231  +#define OP_SorterInsert  106
         9232  +#define OP_IdxInsert     107 /* synopsis: key=r[P2]                        */
         9233  +#define OP_IdxDelete     108 /* synopsis: key=r[P2@P3]                     */
         9234  +#define OP_IdxRowid      109 /* synopsis: r[P2]=rowid                      */
         9235  +#define OP_IdxLE         110 /* synopsis: key=r[P3@P4]                     */
         9236  +#define OP_IdxGT         111 /* synopsis: key=r[P3@P4]                     */
         9237  +#define OP_IdxLT         112 /* synopsis: key=r[P3@P4]                     */
         9238  +#define OP_IdxGE         113 /* synopsis: key=r[P3@P4]                     */
         9239  +#define OP_Destroy       114
         9240  +#define OP_Clear         115
         9241  +#define OP_CreateIndex   116 /* synopsis: r[P2]=root iDb=P1                */
         9242  +#define OP_CreateTable   117 /* synopsis: r[P2]=root iDb=P1                */
         9243  +#define OP_ParseSchema   118
         9244  +#define OP_LoadAnalysis  119
         9245  +#define OP_DropTable     120
         9246  +#define OP_DropIndex     121
         9247  +#define OP_DropTrigger   122
         9248  +#define OP_IntegrityCk   123
         9249  +#define OP_RowSetAdd     124 /* synopsis: rowset(P1)=r[P2]                 */
         9250  +#define OP_RowSetRead    125 /* synopsis: r[P3]=rowset(P1)                 */
         9251  +#define OP_RowSetTest    126 /* synopsis: if r[P3] in rowset(P1) goto P2   */
         9252  +#define OP_Program       127
         9253  +#define OP_Param         128
         9254  +#define OP_FkCounter     129 /* synopsis: fkctr[P1]+=P2                    */
         9255  +#define OP_FkIfZero      130 /* synopsis: if fkctr[P1]==0 goto P2          */
         9256  +#define OP_MemMax        131 /* synopsis: r[P1]=max(r[P1],r[P2])           */
         9257  +#define OP_IfPos         132 /* synopsis: if r[P1]>0 goto P2               */
  9212   9258   #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  9213         -#define OP_Expire        134
  9214         -#define OP_TableLock     135 /* synopsis: iDb=P1 root=P2 write=P3          */
  9215         -#define OP_VBegin        136
  9216         -#define OP_VCreate       137
  9217         -#define OP_VDestroy      138
  9218         -#define OP_VOpen         139
  9219         -#define OP_VColumn       140 /* synopsis: r[P3]=vcolumn(P2)                */
  9220         -#define OP_VNext         141
  9221         -#define OP_VRename       142
         9259  +#define OP_IfNeg         134 /* synopsis: if r[P1]<0 goto P2               */
         9260  +#define OP_IfZero        135 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
         9261  +#define OP_AggFinal      136 /* synopsis: accum=r[P1] N=P2                 */
         9262  +#define OP_IncrVacuum    137
         9263  +#define OP_Expire        138
         9264  +#define OP_TableLock     139 /* synopsis: iDb=P1 root=P2 write=P3          */
         9265  +#define OP_VBegin        140
         9266  +#define OP_VCreate       141
         9267  +#define OP_VDestroy      142
  9222   9268   #define OP_ToText        143 /* same as TK_TO_TEXT                         */
  9223   9269   #define OP_ToBlob        144 /* same as TK_TO_BLOB                         */
  9224   9270   #define OP_ToNumeric     145 /* same as TK_TO_NUMERIC                      */
  9225   9271   #define OP_ToInt         146 /* same as TK_TO_INT                          */
  9226   9272   #define OP_ToReal        147 /* same as TK_TO_REAL                         */
  9227         -#define OP_Pagecount     148
  9228         -#define OP_MaxPgcnt      149
  9229         -#define OP_Trace         150
  9230         -#define OP_Noop          151
  9231         -#define OP_Explain       152
         9273  +#define OP_VOpen         148
         9274  +#define OP_VColumn       149 /* synopsis: r[P3]=vcolumn(P2)                */
         9275  +#define OP_VNext         150
         9276  +#define OP_VRename       151
         9277  +#define OP_Pagecount     152
         9278  +#define OP_MaxPgcnt      153
         9279  +#define OP_Init          154 /* synopsis: Start at P2                      */
         9280  +#define OP_Noop          155
         9281  +#define OP_Explain       156
  9232   9282   
  9233   9283   
  9234   9284   /* Properties such as "out2" or "jump" that are specified in
  9235   9285   ** comments following the "case" for each opcode in the vdbe.c
  9236   9286   ** are encoded into bitvectors as follows:
  9237   9287   */
  9238   9288   #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
................................................................................
  9241   9291   #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
  9242   9292   #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
  9243   9293   #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
  9244   9294   #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
  9245   9295   #define OPFLG_INITIALIZER {\
  9246   9296   /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
  9247   9297   /*   8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\
  9248         -/*  16 */ 0x01, 0x01, 0x04, 0x24, 0x04, 0x10, 0x00, 0x02,\
  9249         -/*  24 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x20,\
  9250         -/*  32 */ 0x00, 0x00, 0x04, 0x05, 0x04, 0x00, 0x00, 0x01,\
  9251         -/*  40 */ 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x02,\
  9252         -/*  48 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  9253         -/*  56 */ 0x00, 0x11, 0x11, 0x11, 0x11, 0x08, 0x11, 0x11,\
  9254         -/*  64 */ 0x11, 0x11, 0x02, 0x02, 0x00, 0x00, 0x00, 0x4c,\
         9298  +/*  16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
         9299  +/*  24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
         9300  +/*  32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
         9301  +/*  40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
         9302  +/*  48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
         9303  +/*  56 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\
         9304  +/*  64 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c,\
  9255   9305   /*  72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
  9256   9306   /*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
  9257   9307   /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
  9258         -/*  96 */ 0x24, 0x02, 0x02, 0x00, 0x01, 0x01, 0x01, 0x01,\
  9259         -/* 104 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
  9260         -/* 112 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  9261         -/* 120 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
  9262         -/* 128 */ 0x05, 0x05, 0x05, 0x00, 0x01, 0x02, 0x00, 0x00,\
  9263         -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04,\
  9264         -/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x02, 0x02, 0x00, 0x00,\
  9265         -/* 152 */ 0x00,}
         9308  +/*  96 */ 0x24, 0x02, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\
         9309  +/* 104 */ 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01, 0x01,\
         9310  +/* 112 */ 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00,\
         9311  +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01,\
         9312  +/* 128 */ 0x02, 0x00, 0x01, 0x08, 0x05, 0x02, 0x05, 0x05,\
         9313  +/* 136 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,\
         9314  +/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x01, 0x00,\
         9315  +/* 152 */ 0x02, 0x02, 0x01, 0x00, 0x00,}
  9266   9316   
  9267   9317   /************** End of opcodes.h *********************************************/
  9268   9318   /************** Continuing where we left off in vdbe.h ***********************/
  9269   9319   
  9270   9320   /*
  9271   9321   ** Prototypes for the VDBE interface.  See comments on the implementation
  9272   9322   ** for a description of what each of these routines does.
................................................................................
  9274   9324   SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
  9275   9325   SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  9276   9326   SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  9277   9327   SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  9278   9328   SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  9279   9329   SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  9280   9330   SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  9281         -SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
         9331  +SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
  9282   9332   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  9283   9333   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  9284   9334   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  9285   9335   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
  9286   9336   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  9287   9337   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  9288   9338   SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
................................................................................
  9315   9365   SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
  9316   9366   SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  9317   9367   #ifndef SQLITE_OMIT_TRACE
  9318   9368   SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  9319   9369   #endif
  9320   9370   
  9321   9371   SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  9322         -SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
         9372  +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int);
  9323   9373   SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
         9374  +
         9375  +typedef int (*RecordCompare)(int,const void*,const UnpackedRecord*,int);
         9376  +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
  9324   9377   
  9325   9378   #ifndef SQLITE_OMIT_TRIGGER
  9326   9379   SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  9327   9380   #endif
  9328   9381   
  9329   9382   /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
  9330   9383   ** each VDBE opcode.
................................................................................
  9345   9398   # endif
  9346   9399   #else
  9347   9400   # define VdbeComment(X)
  9348   9401   # define VdbeNoopComment(X)
  9349   9402   # define VdbeModuleComment(X)
  9350   9403   #endif
  9351   9404   
         9405  +/*
         9406  +** The VdbeCoverage macros are used to set a coverage testing point
         9407  +** for VDBE branch instructions.  The coverage testing points are line
         9408  +** numbers in the sqlite3.c source file.  VDBE branch coverage testing
         9409  +** only works with an amalagmation build.  That's ok since a VDBE branch
         9410  +** coverage build designed for testing the test suite only.  No application
         9411  +** should ever ship with VDBE branch coverage measuring turned on.
         9412  +**
         9413  +**    VdbeCoverage(v)                  // Mark the previously coded instruction
         9414  +**                                     // as a branch
         9415  +**
         9416  +**    VdbeCoverageIf(v, conditional)   // Mark previous if conditional true
         9417  +**
         9418  +**    VdbeCoverageAlwaysTaken(v)       // Previous branch is always taken
         9419  +**
         9420  +**    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
         9421  +**
         9422  +** Every VDBE branch operation must be tagged with one of the macros above.
         9423  +** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
         9424  +** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
         9425  +** routine in vdbe.c, alerting the developer to the missed tag.
         9426  +*/
         9427  +#ifdef SQLITE_VDBE_COVERAGE
         9428  +SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
         9429  +# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
         9430  +# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
         9431  +# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
         9432  +# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
         9433  +#else
         9434  +# define VdbeCoverage(v)
         9435  +# define VdbeCoverageIf(v,x)
         9436  +# define VdbeCoverageAlwaysTaken(v)
         9437  +# define VdbeCoverageNeverTaken(v)
         9438  +#endif
         9439  +
  9352   9440   #endif
  9353   9441   
  9354   9442   /************** End of vdbe.h ************************************************/
  9355   9443   /************** Continuing where we left off in sqliteInt.h ******************/
  9356   9444   /************** Include pager.h in the middle of sqliteInt.h *****************/
  9357   9445   /************** Begin file pager.h *******************************************/
  9358   9446   /*
................................................................................
 10402  10490   #define OptimizationEnabled(db, mask)   1
 10403  10491   #endif
 10404  10492   
 10405  10493   /*
 10406  10494   ** Return true if it OK to factor constant expressions into the initialization
 10407  10495   ** code. The argument is a Parse object for the code generator.
 10408  10496   */
 10409         -#define ConstFactorOk(P) \
 10410         -  ((P)->cookieGoto>0 && OptimizationEnabled((P)->db,SQLITE_FactorOutConst))
        10497  +#define ConstFactorOk(P) ((P)->okConstFactor)
 10411  10498   
 10412  10499   /*
 10413  10500   ** Possible values for the sqlite.magic field.
 10414  10501   ** The numbers are obtained at random and have no special meaning, other
 10415  10502   ** than being distinct from one another.
 10416  10503   */
 10417  10504   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
................................................................................
 10629  10716   ** affinity value. 
 10630  10717   */
 10631  10718   #define SQLITE_AFF_MASK     0x67
 10632  10719   
 10633  10720   /*
 10634  10721   ** Additional bit values that can be ORed with an affinity without
 10635  10722   ** changing the affinity.
        10723  +**
        10724  +** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
        10725  +** It causes an assert() to fire if either operand to a comparison
        10726  +** operator is NULL.  It is added to certain comparison operators to
        10727  +** prove that the operands are always NOT NULL.
 10636  10728   */
 10637  10729   #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
 10638  10730   #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
 10639  10731   #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
        10732  +#define SQLITE_NOTNULL      0x88  /* Assert that operands are never NULL */
 10640  10733   
 10641  10734   /*
 10642  10735   ** An object of this type is created for each virtual table present in
 10643  10736   ** the database schema. 
 10644  10737   **
 10645  10738   ** If the database schema is shared, then there is one instance of this
 10646  10739   ** structure for each database connection (sqlite3*) that uses the shared
................................................................................
 10891  10984   ** Records are used to store the content of a table row and to store
 10892  10985   ** the key of an index.  A blob encoding of a record is created by
 10893  10986   ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
 10894  10987   ** OP_Column opcode.
 10895  10988   **
 10896  10989   ** This structure holds a record that has already been disassembled
 10897  10990   ** into its constituent fields.
        10991  +**
        10992  +** The r1 and r2 member variables are only used by the optimized comparison
        10993  +** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
 10898  10994   */
 10899  10995   struct UnpackedRecord {
 10900  10996     KeyInfo *pKeyInfo;  /* Collation and sort-order information */
 10901  10997     u16 nField;         /* Number of entries in apMem[] */
 10902         -  u8 flags;           /* Boolean settings.  UNPACKED_... below */
        10998  +  i8 default_rc;      /* Comparison result if keys are equal */
 10903  10999     Mem *aMem;          /* Values */
        11000  +  int r1;             /* Value to return if (lhs > rhs) */
        11001  +  int r2;             /* Value to return if (rhs < lhs) */
 10904  11002   };
 10905  11003   
 10906         -/*
 10907         -** Allowed values of UnpackedRecord.flags
 10908         -*/
 10909         -#define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
 10910         -#define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
 10911  11004   
 10912  11005   /*
 10913  11006   ** Each SQL index is represented in memory by an
 10914  11007   ** instance of the following structure.
 10915  11008   **
 10916  11009   ** The columns of the table that are to be indexed are described
 10917  11010   ** by the aiColumn[] field of this structure.  For example, suppose
................................................................................
 11323  11416   ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
 11324  11417   ** jointype expresses the join between the table and the previous table.
 11325  11418   **
 11326  11419   ** In the colUsed field, the high-order bit (bit 63) is set if the table
 11327  11420   ** contains more than 63 columns and the 64-th or later column is used.
 11328  11421   */
 11329  11422   struct SrcList {
 11330         -  u8 nSrc;        /* Number of tables or subqueries in the FROM clause */
 11331         -  u8 nAlloc;      /* Number of entries allocated in a[] below */
        11423  +  int nSrc;        /* Number of tables or subqueries in the FROM clause */
        11424  +  u32 nAlloc;      /* Number of entries allocated in a[] below */
 11332  11425     struct SrcList_item {
 11333  11426       Schema *pSchema;  /* Schema to which this item is fixed */
 11334  11427       char *zDatabase;  /* Name of database holding this table */
 11335  11428       char *zName;      /* Name of the table */
 11336  11429       char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
 11337  11430       Table *pTab;      /* An SQL table corresponding to zName */
 11338  11431       Select *pSelect;  /* A SELECT statement used in place of a table name */
 11339  11432       int addrFillSub;  /* Address of subroutine to manifest a subquery */
 11340  11433       int regReturn;    /* Register holding return address of addrFillSub */
        11434  +    int regResult;    /* Registers holding results of a co-routine */
 11341  11435       u8 jointype;      /* Type of join between this able and the previous */
 11342  11436       unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
 11343  11437       unsigned isCorrelated :1;  /* True if sub-query is correlated */
 11344  11438       unsigned viaCoroutine :1;  /* Implemented as a co-routine */
 11345  11439       unsigned isRecursive :1;   /* True for recursive reference in WITH */
 11346  11440   #ifndef SQLITE_OMIT_EXPLAIN
 11347  11441       u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
................................................................................
 11462  11556     SrcList *pSrc;         /* The FROM clause */
 11463  11557     Expr *pWhere;          /* The WHERE clause */
 11464  11558     ExprList *pGroupBy;    /* The GROUP BY clause */
 11465  11559     Expr *pHaving;         /* The HAVING clause */
 11466  11560     ExprList *pOrderBy;    /* The ORDER BY clause */
 11467  11561     Select *pPrior;        /* Prior select in a compound select statement */
 11468  11562     Select *pNext;         /* Next select to the left in a compound */
 11469         -  Select *pRightmost;    /* Right-most select in a compound select statement */
 11470  11563     Expr *pLimit;          /* LIMIT expression. NULL means not used. */
 11471  11564     Expr *pOffset;         /* OFFSET expression. NULL means not used. */
 11472  11565     With *pWith;           /* WITH clause attached to this select. Or NULL. */
 11473  11566   };
 11474  11567   
 11475  11568   /*
 11476  11569   ** Allowed values for Select.selFlags.  The "SF" prefix stands for
................................................................................
 11480  11573   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
 11481  11574   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
 11482  11575   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 11483  11576   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 11484  11577   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
 11485  11578   #define SF_UseSorter       0x0040  /* Sort using a sorter */
 11486  11579   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
 11487         -#define SF_Materialize     0x0100  /* Force materialization of views */
        11580  +#define SF_Materialize     0x0100  /* NOT USED */
 11488  11581   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
 11489  11582   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
 11490  11583   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
        11584  +#define SF_Compound        0x1000  /* Part of a compound query */
 11491  11585   
 11492  11586   
 11493  11587   /*
 11494  11588   ** The results of a SELECT can be distributed in several ways, as defined
 11495  11589   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
 11496  11590   ** Type".
 11497  11591   **
................................................................................
 11662  11756     char *zErrMsg;       /* An error message */
 11663  11757     Vdbe *pVdbe;         /* An engine for executing database bytecode */
 11664  11758     int rc;              /* Return code from execution */
 11665  11759     u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
 11666  11760     u8 checkSchema;      /* Causes schema cookie check after an error */
 11667  11761     u8 nested;           /* Number of nested calls to the parser/code generator */
 11668  11762     u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
 11669         -  u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
 11670  11763     u8 nColCache;        /* Number of entries in aColCache[] */
 11671  11764     u8 iColCache;        /* Next entry in aColCache[] to replace */
 11672  11765     u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
 11673  11766     u8 mayAbort;         /* True if statement may throw an ABORT exception */
 11674  11767     u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
        11768  +  u8 okConstFactor;    /* OK to factor out constants */
 11675  11769     int aTempReg[8];     /* Holding area for temporary registers */
 11676  11770     int nRangeReg;       /* Size of the temporary register block */
 11677  11771     int iRangeReg;       /* First register in temporary register block */
 11678  11772     int nErr;            /* Number of errors seen */
 11679  11773     int nTab;            /* Number of previously allocated VDBE cursors */
 11680  11774     int nMem;            /* Number of memory cells used so far */
 11681  11775     int nSet;            /* Number of sets used so far */
 11682  11776     int nOnce;           /* Number of OP_Once instructions so far */
 11683  11777     int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
 11684         -  int nLabel;          /* Number of labels used */
 11685         -  int *aLabel;         /* Space to hold the labels */
 11686  11778     int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
 11687  11779     int ckBase;          /* Base register of data during check constraints */
 11688  11780     int iPartIdxTab;     /* Table corresponding to a partial index */
 11689  11781     int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
 11690  11782     int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
        11783  +  int nLabel;          /* Number of labels used */
        11784  +  int *aLabel;         /* Space to hold the labels */
 11691  11785     struct yColCache {
 11692  11786       int iTable;           /* Table cursor number */
 11693         -    int iColumn;          /* Table column number */
        11787  +    i16 iColumn;          /* Table column number */
 11694  11788       u8 tempReg;           /* iReg is a temp register that needs to be freed */
 11695  11789       int iLevel;           /* Nesting level */
 11696  11790       int iReg;             /* Reg with value of this column. 0 means none. */
 11697  11791       int lru;              /* Least recently used entry has the smallest value */
 11698  11792     } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
 11699  11793     ExprList *pConstExpr;/* Constant expressions */
        11794  +  Token constraintName;/* Name of the constraint currently being parsed */
 11700  11795     yDbMask writeMask;   /* Start a write transaction on these databases */
 11701  11796     yDbMask cookieMask;  /* Bitmask of schema verified databases */
 11702         -  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
 11703  11797     int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
 11704  11798     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 11705  11799     int regRoot;         /* Register holding root page number for new objects */
 11706  11800     int nMaxArg;         /* Max args passed to user function by sub-program */
 11707         -  Token constraintName;/* Name of the constraint currently being parsed */
 11708  11801   #ifndef SQLITE_OMIT_SHARED_CACHE
 11709  11802     int nTableLock;        /* Number of locks in aTableLock */
 11710  11803     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 11711  11804   #endif
 11712  11805     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 11713  11806   
 11714  11807     /* Information used while coding trigger programs. */
................................................................................
 11719  11812     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 11720  11813     u32 oldmask;         /* Mask of old.* columns referenced */
 11721  11814     u32 newmask;         /* Mask of new.* columns referenced */
 11722  11815     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 11723  11816     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 11724  11817     u8 disableTriggers;  /* True to disable triggers */
 11725  11818   
 11726         -  /* Above is constant between recursions.  Below is reset before and after
 11727         -  ** each recursion */
        11819  +  /************************************************************************
        11820  +  ** Above is constant between recursions.  Below is reset before and after
        11821  +  ** each recursion.  The boundary between these two regions is determined
        11822  +  ** using offsetof(Parse,nVar) so the nVar field must be the first field
        11823  +  ** in the recursive region.
        11824  +  ************************************************************************/
 11728  11825   
 11729  11826     int nVar;                 /* Number of '?' variables seen in the SQL so far */
 11730  11827     int nzVar;                /* Number of available slots in azVar[] */
 11731  11828     u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
        11829  +  u8 bFreeWith;             /* True if pWith should be freed with parser */
 11732  11830     u8 explain;               /* True if the EXPLAIN flag is found on the query */
 11733  11831   #ifndef SQLITE_OMIT_VIRTUALTABLE
 11734  11832     u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
 11735  11833     int nVtabLock;            /* Number of virtual tables to lock */
 11736  11834   #endif
 11737  11835     int nAlias;               /* Number of aliased result set columns */
 11738  11836     int nHeight;              /* Expression tree height of current sub-select */
................................................................................
 11751  11849   #ifndef SQLITE_OMIT_VIRTUALTABLE
 11752  11850     Token sArg;               /* Complete text of a module argument */
 11753  11851     Table **apVtabLock;       /* Pointer to virtual tables needing locking */
 11754  11852   #endif
 11755  11853     Table *pZombieTab;        /* List of Table objects to delete after code gen */
 11756  11854     TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
 11757  11855     With *pWith;              /* Current WITH clause, or NULL */
 11758         -  u8 bFreeWith;             /* True if pWith should be freed with parser */
 11759  11856   };
 11760  11857   
 11761  11858   /*
 11762  11859   ** Return true if currently inside an sqlite3_declare_vtab() call.
 11763  11860   */
 11764  11861   #ifdef SQLITE_OMIT_VIRTUALTABLE
 11765  11862     #define IN_DECLARE_VTAB 0
................................................................................
 11967  12064     void (*xLog)(void*,int,const char*); /* Function for logging */
 11968  12065     void *pLogArg;                       /* First argument to xLog() */
 11969  12066     int bLocaltimeFault;              /* True to fail localtime() calls */
 11970  12067   #ifdef SQLITE_ENABLE_SQLLOG
 11971  12068     void(*xSqllog)(void*,sqlite3*,const char*, int);
 11972  12069     void *pSqllogArg;
 11973  12070   #endif
        12071  +#ifdef SQLITE_VDBE_COVERAGE
        12072  +  /* The following callback (if not NULL) is invoked on every VDBE branch
        12073  +  ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
        12074  +  */
        12075  +  void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
        12076  +  void *pVdbeBranchArg;                                     /* 1st argument */
        12077  +#endif
 11974  12078   };
 11975  12079   
 11976  12080   /*
 11977  12081   ** This macro is used inside of assert() statements to indicate that
 11978  12082   ** the assert is only valid on a well-formed database.  Instead of:
 11979  12083   **
 11980  12084   **     assert( X );
................................................................................
 12300  12404   #ifndef SQLITE_OMIT_AUTOINCREMENT
 12301  12405   SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
 12302  12406   SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
 12303  12407   #else
 12304  12408   # define sqlite3AutoincrementBegin(X)
 12305  12409   # define sqlite3AutoincrementEnd(X)
 12306  12410   #endif
 12307         -SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
 12308  12411   SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
 12309  12412   SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
 12310  12413   SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
 12311  12414   SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
 12312  12415   SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
 12313  12416   SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
 12314  12417   SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
................................................................................
 12348  12451   SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 12349  12452   SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
 12350  12453   SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
 12351  12454   SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
 12352  12455   SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
 12353  12456   SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
 12354  12457   SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
 12355         -SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
        12458  +SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
        12459  +SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
 12356  12460   SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
 12357  12461   SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
 12358  12462   SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
 12359         -SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
        12463  +SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
 12360  12464   SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
 12361  12465   #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
 12362  12466   #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
 12363  12467   SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
 12364  12468   SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
 12365  12469   SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
 12366  12470   SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
................................................................................
 12390  12494   SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 12391  12495   SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
 12392  12496   SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 12393  12497   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 12394  12498   SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
 12395  12499   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 12396  12500   SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 12397         -SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
 12398  12501   SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 12399  12502   SQLITE_PRIVATE int sqlite3IsRowid(const char*);
 12400  12503   SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
 12401  12504   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
 12402  12505   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 12403  12506   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 12404  12507                                        u8,u8,int,int*);
................................................................................
 12534  12637     (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
 12535  12638     sqlite3PutVarint32((A),(B)))
 12536  12639   #define getVarint    sqlite3GetVarint
 12537  12640   #define putVarint    sqlite3PutVarint
 12538  12641   
 12539  12642   
 12540  12643   SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
 12541         -SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
        12644  +SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
 12542  12645   SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
 12543  12646   SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
 12544  12647   SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
 12545  12648   SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
 12546  12649   SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
 12547  12650   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 12548  12651   SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
................................................................................
 13346  13449     "OMIT_CHECK",
 13347  13450   #endif
 13348  13451   #ifdef SQLITE_OMIT_COMPLETE
 13349  13452     "OMIT_COMPLETE",
 13350  13453   #endif
 13351  13454   #ifdef SQLITE_OMIT_COMPOUND_SELECT
 13352  13455     "OMIT_COMPOUND_SELECT",
        13456  +#endif
        13457  +#ifdef SQLITE_OMIT_CTE
        13458  +  "OMIT_CTE",
 13353  13459   #endif
 13354  13460   #ifdef SQLITE_OMIT_DATETIME_FUNCS
 13355  13461     "OMIT_DATETIME_FUNCS",
 13356  13462   #endif
 13357  13463   #ifdef SQLITE_OMIT_DECLTYPE
 13358  13464     "OMIT_DECLTYPE",
 13359  13465   #endif
................................................................................
 13638  13744     i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
 13639  13745     u8 nullRow;           /* True if pointing to a row with no data */
 13640  13746     u8 rowidIsValid;      /* True if lastRowid is valid */
 13641  13747     u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
 13642  13748     Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
 13643  13749     Bool isTable:1;       /* True if a table requiring integer keys */
 13644  13750     Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
 13645         -  Bool multiPseudo:1;   /* Multi-register pseudo-cursor */
 13646  13751     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
 13647  13752     i64 seqCount;         /* Sequence counter */
 13648  13753     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
 13649  13754     i64 lastRowid;        /* Rowid being deleted by OP_Delete */
 13650  13755     VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
 13651  13756   
 13652  13757     /* Cached information about the header for the data record that the
................................................................................
 13732  13837       int nZero;          /* Used when bit MEM_Zero is set in flags */
 13733  13838       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
 13734  13839       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
 13735  13840       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
 13736  13841     } u;
 13737  13842     int n;              /* Number of characters in string value, excluding '\0' */
 13738  13843     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
 13739         -  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
 13740  13844     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
 13741  13845   #ifdef SQLITE_DEBUG
 13742  13846     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
 13743  13847     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
 13744  13848   #endif
 13745  13849     void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
 13746  13850     char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
................................................................................
 13759  13863   ** flags may coexist with the MEM_Str flag.
 13760  13864   */
 13761  13865   #define MEM_Null      0x0001   /* Value is NULL */
 13762  13866   #define MEM_Str       0x0002   /* Value is a string */
 13763  13867   #define MEM_Int       0x0004   /* Value is an integer */
 13764  13868   #define MEM_Real      0x0008   /* Value is a real number */
 13765  13869   #define MEM_Blob      0x0010   /* Value is a BLOB */
        13870  +#define MEM_AffMask   0x001f   /* Mask of affinity bits */
 13766  13871   #define MEM_RowSet    0x0020   /* Value is a RowSet object */
 13767  13872   #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
 13768         -#define MEM_Invalid   0x0080   /* Value is undefined */
        13873  +#define MEM_Undefined 0x0080   /* Value is undefined */
 13769  13874   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
 13770  13875   #define MEM_TypeMask  0x01ff   /* Mask of type bits */
 13771  13876   
 13772  13877   
 13773  13878   /* Whenever Mem contains a valid string or blob representation, one of
 13774  13879   ** the following flags must be set to determine the memory management
 13775  13880   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
 13776  13881   ** string is \000 or \u0000 terminated
 13777  13882   */
 13778  13883   #define MEM_Term      0x0200   /* String rep is nul terminated */
 13779         -#define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
        13884  +#define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
 13780  13885   #define MEM_Static    0x0800   /* Mem.z points to a static string */
 13781  13886   #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
 13782  13887   #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
 13783  13888   #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
 13784  13889   #ifdef SQLITE_OMIT_INCRBLOB
 13785  13890     #undef MEM_Zero
 13786  13891     #define MEM_Zero 0x0000
................................................................................
 13793  13898      ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
 13794  13899   
 13795  13900   /*
 13796  13901   ** Return true if a memory cell is not marked as invalid.  This macro
 13797  13902   ** is for use inside assert() statements only.
 13798  13903   */
 13799  13904   #ifdef SQLITE_DEBUG
 13800         -#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
        13905  +#define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
 13801  13906   #endif
 13802  13907   
 13803  13908   /*
 13804  13909   ** Each auxilliary data pointer stored by a user defined function 
 13805  13910   ** implementation calling sqlite3_set_auxdata() is stored in an instance
 13806  13911   ** of this structure. All such structures associated with a single VM
 13807  13912   ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
................................................................................
 13955  14060   SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
 13956  14061   SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
 13957  14062   SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
 13958  14063   SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 13959  14064   SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
 13960  14065   
 13961  14066   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 13962         -SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
        14067  +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*);
 13963  14068   SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
 13964  14069   SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
 13965  14070   SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
 13966  14071   SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
 13967  14072   SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 13968  14073   SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 13969  14074   SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
................................................................................
 13988  14093   SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
 13989  14094   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 13990  14095   SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 13991  14096   SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 13992  14097   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
 13993  14098   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 13994  14099   SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
        14100  +#define VdbeMemDynamic(X)  \
        14101  +  (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
 13995  14102   #define VdbeMemRelease(X)  \
 13996         -  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
 13997         -    sqlite3VdbeMemReleaseExternal(X);
        14103  +  if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
 13998  14104   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 13999  14105   SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
 14000  14106   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 14001  14107   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 14002  14108   SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 14003  14109   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 14004         -SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
 14005  14110   SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
 14006  14111   
 14007  14112   SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
 14008  14113   SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 14009  14114   SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
 14010  14115   SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
 14011  14116   SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
................................................................................
 14018  14123   #else
 14019  14124   # define sqlite3VdbeEnter(X)
 14020  14125   # define sqlite3VdbeLeave(X)
 14021  14126   #endif
 14022  14127   
 14023  14128   #ifdef SQLITE_DEBUG
 14024  14129   SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
        14130  +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
 14025  14131   #endif
 14026  14132   
 14027  14133   #ifndef SQLITE_OMIT_FOREIGN_KEY
 14028  14134   SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
 14029  14135   #else
 14030  14136   # define sqlite3VdbeCheckFk(p,i) 0
 14031  14137   #endif
................................................................................
 17765  17871     mem5.nAlloc++;
 17766  17872     mem5.totalAlloc += iFullSz;
 17767  17873     mem5.totalExcess += iFullSz - nByte;
 17768  17874     mem5.currentCount++;
 17769  17875     mem5.currentOut += iFullSz;
 17770  17876     if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
 17771  17877     if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
        17878  +
        17879  +#ifdef SQLITE_DEBUG
        17880  +  /* Make sure the allocated memory does not assume that it is set to zero
        17881  +  ** or retains a value from a previous allocation */
        17882  +  memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
        17883  +#endif
 17772  17884   
 17773  17885     /* Return a pointer to the allocated memory. */
 17774  17886     return (void*)&mem5.zPool[i*mem5.szAtom];
 17775  17887   }
 17776  17888   
 17777  17889   /*
 17778  17890   ** Free an outstanding memory allocation.
................................................................................
 17823  17935         iBlock = iBuddy;
 17824  17936       }else{
 17825  17937         mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
 17826  17938         mem5.aCtrl[iBuddy] = 0;
 17827  17939       }
 17828  17940       size *= 2;
 17829  17941     }
        17942  +
        17943  +#ifdef SQLITE_DEBUG
        17944  +  /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
        17945  +  ** not used after being freed */
        17946  +  memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
        17947  +#endif
        17948  +
 17830  17949     memsys5Link(iBlock, iLogsize);
 17831  17950   }
 17832  17951   
 17833  17952   /*
 17834  17953   ** Allocate nBytes of memory.
 17835  17954   */
 17836  17955   static void *memsys5Malloc(int nBytes){
................................................................................
 21388  21507     }
 21389  21508     *z = 0;
 21390  21509     assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
 21391  21510   
 21392  21511     sqlite3VdbeMemRelease(pMem);
 21393  21512     pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
 21394  21513     pMem->enc = desiredEnc;
 21395         -  pMem->flags |= (MEM_Term|MEM_Dyn);
        21514  +  pMem->flags |= (MEM_Term);
 21396  21515     pMem->z = (char*)zOut;
 21397  21516     pMem->zMalloc = pMem->z;
 21398  21517   
 21399  21518   translate_out:
 21400  21519   #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
 21401  21520     {
 21402  21521       char zBuf[100];
................................................................................
 21516  21635     sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
 21517  21636     if( db->mallocFailed ){
 21518  21637       sqlite3VdbeMemRelease(&m);
 21519  21638       m.z = 0;
 21520  21639     }
 21521  21640     assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
 21522  21641     assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
 21523         -  assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
 21524  21642     assert( m.z || db->mallocFailed );
 21525  21643     return m.z;
 21526  21644   }
 21527  21645   
 21528  21646   /*
 21529  21647   ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
 21530  21648   ** Return the number of bytes in the first nChar unicode characters
................................................................................
 22726  22844     i64 iA = *pA;
 22727  22845     testcase( iA==0 ); testcase( iA==1 );
 22728  22846     testcase( iB==-1 ); testcase( iB==0 );
 22729  22847     if( iB>=0 ){
 22730  22848       testcase( iA>0 && LARGEST_INT64 - iA == iB );
 22731  22849       testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
 22732  22850       if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
 22733         -    *pA += iB;
 22734  22851     }else{
 22735  22852       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
 22736  22853       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
 22737  22854       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 22738         -    *pA += iB;
 22739  22855     }
        22856  +  *pA += iB;
 22740  22857     return 0; 
 22741  22858   }
 22742  22859   SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 22743  22860     testcase( iB==SMALLEST_INT64+1 );
 22744  22861     if( iB==SMALLEST_INT64 ){
 22745  22862       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 22746  22863       if( (*pA)>=0 ) return 1;
................................................................................
 22756  22873     i64 iA = *pA;
 22757  22874     i64 iA1, iA0, iB1, iB0, r;
 22758  22875   
 22759  22876     iA1 = iA/TWOPOWER32;
 22760  22877     iA0 = iA % TWOPOWER32;
 22761  22878     iB1 = iB/TWOPOWER32;
 22762  22879     iB0 = iB % TWOPOWER32;
 22763         -  if( iA1*iB1 != 0 ) return 1;
 22764         -  assert( iA1*iB0==0 || iA0*iB1==0 );
 22765         -  r = iA1*iB0 + iA0*iB1;
        22880  +  if( iA1==0 ){
        22881  +    if( iB1==0 ){
        22882  +      *pA *= iB;
        22883  +      return 0;
        22884  +    }
        22885  +    r = iA0*iB1;
        22886  +  }else if( iB1==0 ){
        22887  +    r = iA1*iB0;
        22888  +  }else{
        22889  +    /* If both iA1 and iB1 are non-zero, overflow will result */
        22890  +    return 1;
        22891  +  }
 22766  22892     testcase( r==(-TWOPOWER31)-1 );
 22767  22893     testcase( r==(-TWOPOWER31) );
 22768  22894     testcase( r==TWOPOWER31 );
 22769  22895     testcase( r==TWOPOWER31-1 );
 22770  22896     if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
 22771  22897     r *= TWOPOWER32;
 22772  22898     if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
................................................................................
 23204  23330        /*  13 */ "Vacuum"           OpHelp(""),
 23205  23331        /*  14 */ "VFilter"          OpHelp("iPlan=r[P3] zPlan='P4'"),
 23206  23332        /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
 23207  23333        /*  16 */ "Goto"             OpHelp(""),
 23208  23334        /*  17 */ "Gosub"            OpHelp(""),
 23209  23335        /*  18 */ "Return"           OpHelp(""),
 23210  23336        /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
 23211         -     /*  20 */ "Yield"            OpHelp(""),
 23212         -     /*  21 */ "HaltIfNull"       OpHelp("if r[P3] null then halt"),
 23213         -     /*  22 */ "Halt"             OpHelp(""),
 23214         -     /*  23 */ "Integer"          OpHelp("r[P2]=P1"),
 23215         -     /*  24 */ "Int64"            OpHelp("r[P2]=P4"),
 23216         -     /*  25 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
 23217         -     /*  26 */ "Null"             OpHelp("r[P2..P3]=NULL"),
 23218         -     /*  27 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
 23219         -     /*  28 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
 23220         -     /*  29 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
 23221         -     /*  30 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
 23222         -     /*  31 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
 23223         -     /*  32 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 23224         -     /*  33 */ "CollSeq"          OpHelp(""),
 23225         -     /*  34 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 23226         -     /*  35 */ "MustBeInt"        OpHelp(""),
 23227         -     /*  36 */ "RealAffinity"     OpHelp(""),
 23228         -     /*  37 */ "Permutation"      OpHelp(""),
 23229         -     /*  38 */ "Compare"          OpHelp(""),
 23230         -     /*  39 */ "Jump"             OpHelp(""),
 23231         -     /*  40 */ "Once"             OpHelp(""),
 23232         -     /*  41 */ "If"               OpHelp(""),
 23233         -     /*  42 */ "IfNot"            OpHelp(""),
 23234         -     /*  43 */ "Column"           OpHelp("r[P3]=PX"),
 23235         -     /*  44 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 23236         -     /*  45 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 23237         -     /*  46 */ "Count"            OpHelp("r[P2]=count()"),
 23238         -     /*  47 */ "ReadCookie"       OpHelp(""),
 23239         -     /*  48 */ "SetCookie"        OpHelp(""),
 23240         -     /*  49 */ "VerifyCookie"     OpHelp(""),
 23241         -     /*  50 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 23242         -     /*  51 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 23243         -     /*  52 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 23244         -     /*  53 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 23245         -     /*  54 */ "SorterOpen"       OpHelp(""),
 23246         -     /*  55 */ "OpenPseudo"       OpHelp("content in r[P2@P3]"),
 23247         -     /*  56 */ "Close"            OpHelp(""),
 23248         -     /*  57 */ "SeekLt"           OpHelp("key=r[P3@P4]"),
 23249         -     /*  58 */ "SeekLe"           OpHelp("key=r[P3@P4]"),
 23250         -     /*  59 */ "SeekGe"           OpHelp("key=r[P3@P4]"),
 23251         -     /*  60 */ "SeekGt"           OpHelp("key=r[P3@P4]"),
 23252         -     /*  61 */ "Seek"             OpHelp("intkey=r[P2]"),
 23253         -     /*  62 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
 23254         -     /*  63 */ "NotFound"         OpHelp("key=r[P3@P4]"),
 23255         -     /*  64 */ "Found"            OpHelp("key=r[P3@P4]"),
 23256         -     /*  65 */ "NotExists"        OpHelp("intkey=r[P3]"),
 23257         -     /*  66 */ "Sequence"         OpHelp("r[P2]=rowid"),
 23258         -     /*  67 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 23259         -     /*  68 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 23260         -     /*  69 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 23261         -     /*  70 */ "Delete"           OpHelp(""),
        23337  +     /*  20 */ "InitCoroutine"    OpHelp(""),
        23338  +     /*  21 */ "EndCoroutine"     OpHelp(""),
        23339  +     /*  22 */ "Yield"            OpHelp(""),
        23340  +     /*  23 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
        23341  +     /*  24 */ "Halt"             OpHelp(""),
        23342  +     /*  25 */ "Integer"          OpHelp("r[P2]=P1"),
        23343  +     /*  26 */ "Int64"            OpHelp("r[P2]=P4"),
        23344  +     /*  27 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
        23345  +     /*  28 */ "Null"             OpHelp("r[P2..P3]=NULL"),
        23346  +     /*  29 */ "SoftNull"         OpHelp("r[P1]=NULL"),
        23347  +     /*  30 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
        23348  +     /*  31 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
        23349  +     /*  32 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
        23350  +     /*  33 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
        23351  +     /*  34 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
        23352  +     /*  35 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
        23353  +     /*  36 */ "CollSeq"          OpHelp(""),
        23354  +     /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
        23355  +     /*  38 */ "MustBeInt"        OpHelp(""),
        23356  +     /*  39 */ "RealAffinity"     OpHelp(""),
        23357  +     /*  40 */ "Permutation"      OpHelp(""),
        23358  +     /*  41 */ "Compare"          OpHelp(""),
        23359  +     /*  42 */ "Jump"             OpHelp(""),
        23360  +     /*  43 */ "Once"             OpHelp(""),
        23361  +     /*  44 */ "If"               OpHelp(""),
        23362  +     /*  45 */ "IfNot"            OpHelp(""),
        23363  +     /*  46 */ "Column"           OpHelp("r[P3]=PX"),
        23364  +     /*  47 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        23365  +     /*  48 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        23366  +     /*  49 */ "Count"            OpHelp("r[P2]=count()"),
        23367  +     /*  50 */ "ReadCookie"       OpHelp(""),
        23368  +     /*  51 */ "SetCookie"        OpHelp(""),
        23369  +     /*  52 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        23370  +     /*  53 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        23371  +     /*  54 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        23372  +     /*  55 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        23373  +     /*  56 */ "SorterOpen"       OpHelp(""),
        23374  +     /*  57 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        23375  +     /*  58 */ "Close"            OpHelp(""),
        23376  +     /*  59 */ "SeekLT"           OpHelp(""),
        23377  +     /*  60 */ "SeekLE"           OpHelp(""),
        23378  +     /*  61 */ "SeekGE"           OpHelp(""),
        23379  +     /*  62 */ "SeekGT"           OpHelp(""),
        23380  +     /*  63 */ "Seek"             OpHelp("intkey=r[P2]"),
        23381  +     /*  64 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
        23382  +     /*  65 */ "NotFound"         OpHelp("key=r[P3@P4]"),
        23383  +     /*  66 */ "Found"            OpHelp("key=r[P3@P4]"),
        23384  +     /*  67 */ "NotExists"        OpHelp("intkey=r[P3]"),
        23385  +     /*  68 */ "Sequence"         OpHelp("r[P2]=rowid"),
        23386  +     /*  69 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        23387  +     /*  70 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 23262  23388        /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
 23263  23389        /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
 23264         -     /*  73 */ "ResetCount"       OpHelp(""),
 23265         -     /*  74 */ "SorterCompare"    OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
 23266         -     /*  75 */ "SorterData"       OpHelp("r[P2]=data"),
        23390  +     /*  73 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        23391  +     /*  74 */ "Delete"           OpHelp(""),
        23392  +     /*  75 */ "ResetCount"       OpHelp(""),
 23267  23393        /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
 23268  23394        /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
 23269  23395        /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
 23270  23396        /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
 23271  23397        /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
 23272  23398        /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
 23273  23399        /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
 23274  23400        /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
 23275         -     /*  84 */ "RowKey"           OpHelp("r[P2]=key"),
        23401  +     /*  84 */ "SorterCompare"    OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
 23276  23402        /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 23277  23403        /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 23278  23404        /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 23279  23405        /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 23280  23406        /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 23281  23407        /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 23282  23408        /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 23283  23409        /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 23284  23410        /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 23285  23411        /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 23286         -     /*  95 */ "RowData"          OpHelp("r[P2]=data"),
        23412  +     /*  95 */ "SorterData"       OpHelp("r[P2]=data"),
 23287  23413        /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 23288  23414        /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 23289         -     /*  98 */ "Rowid"            OpHelp("r[P2]=rowid"),
 23290         -     /*  99 */ "NullRow"          OpHelp(""),
 23291         -     /* 100 */ "Last"             OpHelp(""),
 23292         -     /* 101 */ "SorterSort"       OpHelp(""),
 23293         -     /* 102 */ "Sort"             OpHelp(""),
 23294         -     /* 103 */ "Rewind"           OpHelp(""),
 23295         -     /* 104 */ "SorterInsert"     OpHelp(""),
 23296         -     /* 105 */ "IdxInsert"        OpHelp("key=r[P2]"),
 23297         -     /* 106 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 23298         -     /* 107 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 23299         -     /* 108 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 23300         -     /* 109 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 23301         -     /* 110 */ "Destroy"          OpHelp(""),
 23302         -     /* 111 */ "Clear"            OpHelp(""),
 23303         -     /* 112 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 23304         -     /* 113 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
 23305         -     /* 114 */ "ParseSchema"      OpHelp(""),
 23306         -     /* 115 */ "LoadAnalysis"     OpHelp(""),
 23307         -     /* 116 */ "DropTable"        OpHelp(""),
 23308         -     /* 117 */ "DropIndex"        OpHelp(""),
 23309         -     /* 118 */ "DropTrigger"      OpHelp(""),
 23310         -     /* 119 */ "IntegrityCk"      OpHelp(""),
 23311         -     /* 120 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 23312         -     /* 121 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 23313         -     /* 122 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 23314         -     /* 123 */ "Program"          OpHelp(""),
 23315         -     /* 124 */ "Param"            OpHelp(""),
 23316         -     /* 125 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 23317         -     /* 126 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 23318         -     /* 127 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 23319         -     /* 128 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
 23320         -     /* 129 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
 23321         -     /* 130 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
 23322         -     /* 131 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 23323         -     /* 132 */ "IncrVacuum"       OpHelp(""),
        23415  +     /*  98 */ "RowKey"           OpHelp("r[P2]=key"),
        23416  +     /*  99 */ "RowData"          OpHelp("r[P2]=data"),
        23417  +     /* 100 */ "Rowid"            OpHelp("r[P2]=rowid"),
        23418  +     /* 101 */ "NullRow"          OpHelp(""),
        23419  +     /* 102 */ "Last"             OpHelp(""),
        23420  +     /* 103 */ "SorterSort"       OpHelp(""),
        23421  +     /* 104 */ "Sort"             OpHelp(""),
        23422  +     /* 105 */ "Rewind"           OpHelp(""),
        23423  +     /* 106 */ "SorterInsert"     OpHelp(""),
        23424  +     /* 107 */ "IdxInsert"        OpHelp("key=r[P2]"),
        23425  +     /* 108 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        23426  +     /* 109 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        23427  +     /* 110 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
        23428  +     /* 111 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
        23429  +     /* 112 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
        23430  +     /* 113 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
        23431  +     /* 114 */ "Destroy"          OpHelp(""),
        23432  +     /* 115 */ "Clear"            OpHelp(""),
        23433  +     /* 116 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
        23434  +     /* 117 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        23435  +     /* 118 */ "ParseSchema"      OpHelp(""),
        23436  +     /* 119 */ "LoadAnalysis"     OpHelp(""),
        23437  +     /* 120 */ "DropTable"        OpHelp(""),
        23438  +     /* 121 */ "DropIndex"        OpHelp(""),
        23439  +     /* 122 */ "DropTrigger"      OpHelp(""),
        23440  +     /* 123 */ "IntegrityCk"      OpHelp(""),
        23441  +     /* 124 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        23442  +     /* 125 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
        23443  +     /* 126 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
        23444  +     /* 127 */ "Program"          OpHelp(""),
        23445  +     /* 128 */ "Param"            OpHelp(""),
        23446  +     /* 129 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        23447  +     /* 130 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
        23448  +     /* 131 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        23449  +     /* 132 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
 23324  23450        /* 133 */ "Real"             OpHelp("r[P2]=P4"),
 23325         -     /* 134 */ "Expire"           OpHelp(""),
 23326         -     /* 135 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 23327         -     /* 136 */ "VBegin"           OpHelp(""),
 23328         -     /* 137 */ "VCreate"          OpHelp(""),
 23329         -     /* 138 */ "VDestroy"         OpHelp(""),
 23330         -     /* 139 */ "VOpen"            OpHelp(""),
 23331         -     /* 140 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 23332         -     /* 141 */ "VNext"            OpHelp(""),
 23333         -     /* 142 */ "VRename"          OpHelp(""),
        23451  +     /* 134 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
        23452  +     /* 135 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
        23453  +     /* 136 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        23454  +     /* 137 */ "IncrVacuum"       OpHelp(""),
        23455  +     /* 138 */ "Expire"           OpHelp(""),
        23456  +     /* 139 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        23457  +     /* 140 */ "VBegin"           OpHelp(""),
        23458  +     /* 141 */ "VCreate"          OpHelp(""),
        23459  +     /* 142 */ "VDestroy"         OpHelp(""),
 23334  23460        /* 143 */ "ToText"           OpHelp(""),
 23335  23461        /* 144 */ "ToBlob"           OpHelp(""),
 23336  23462        /* 145 */ "ToNumeric"        OpHelp(""),
 23337  23463        /* 146 */ "ToInt"            OpHelp(""),
 23338  23464        /* 147 */ "ToReal"           OpHelp(""),
 23339         -     /* 148 */ "Pagecount"        OpHelp(""),
 23340         -     /* 149 */ "MaxPgcnt"         OpHelp(""),
 23341         -     /* 150 */ "Trace"            OpHelp(""),
 23342         -     /* 151 */ "Noop"             OpHelp(""),
 23343         -     /* 152 */ "Explain"          OpHelp(""),
        23465  +     /* 148 */ "VOpen"            OpHelp(""),
        23466  +     /* 149 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        23467  +     /* 150 */ "VNext"            OpHelp(""),
        23468  +     /* 151 */ "VRename"          OpHelp(""),
        23469  +     /* 152 */ "Pagecount"        OpHelp(""),
        23470  +     /* 153 */ "MaxPgcnt"         OpHelp(""),
        23471  +     /* 154 */ "Init"             OpHelp("Start at P2"),
        23472  +     /* 155 */ "Noop"             OpHelp(""),
        23473  +     /* 156 */ "Explain"          OpHelp(""),
 23344  23474     };
 23345  23475     return azName[i];
 23346  23476   }
 23347  23477   #endif
 23348  23478   
 23349  23479   /************** End of opcodes.c *********************************************/
 23350  23480   /************** Begin file os_unix.c *****************************************/
................................................................................
 23428  23558   #  if defined(__RTP__) || defined(_WRS_KERNEL)
 23429  23559   #    define OS_VXWORKS 1
 23430  23560   #  else
 23431  23561   #    define OS_VXWORKS 0
 23432  23562   #  endif
 23433  23563   #endif
 23434  23564   
 23435         -/*
 23436         -** These #defines should enable >2GB file support on Posix if the
 23437         -** underlying operating system supports it.  If the OS lacks
 23438         -** large file support, these should be no-ops.
 23439         -**
 23440         -** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
 23441         -** on the compiler command line.  This is necessary if you are compiling
 23442         -** on a recent machine (ex: RedHat 7.2) but you want your code to work
 23443         -** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
 23444         -** without this option, LFS is enable.  But LFS does not exist in the kernel
 23445         -** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
 23446         -** portability you should omit LFS.
 23447         -**
 23448         -** The previous paragraph was written in 2005.  (This paragraph is written
 23449         -** on 2008-11-28.) These days, all Linux kernels support large files, so
 23450         -** you should probably leave LFS enabled.  But some embedded platforms might
 23451         -** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
 23452         -*/
 23453         -#ifndef SQLITE_DISABLE_LFS
 23454         -# define _LARGE_FILE       1
 23455         -# ifndef _FILE_OFFSET_BITS
 23456         -#   define _FILE_OFFSET_BITS 64
 23457         -# endif
 23458         -# define _LARGEFILE_SOURCE 1
 23459         -#endif
 23460         -
 23461  23565   /*
 23462  23566   ** standard include files.
 23463  23567   */
 23464  23568   #include <sys/types.h>
 23465  23569   #include <sys/stat.h>
 23466  23570   #include <fcntl.h>
 23467  23571   #include <unistd.h>
................................................................................
 34442  34546   
 34443  34547   /* 
 34444  34548   ** Windows will only let you create file view mappings
 34445  34549   ** on allocation size granularity boundaries.
 34446  34550   ** During sqlite3_os_init() we do a GetSystemInfo()
 34447  34551   ** to get the granularity size.
 34448  34552   */
 34449         -SYSTEM_INFO winSysInfo;
        34553  +static SYSTEM_INFO winSysInfo;
 34450  34554   
 34451  34555   #ifndef SQLITE_OMIT_WAL
 34452  34556   
 34453  34557   /*
 34454  34558   ** Helper functions to obtain and relinquish the global mutex. The
 34455  34559   ** global mutex is used to protect the winLockInfo objects used by 
 34456  34560   ** this file, all of which may be shared by multiple threads.
................................................................................
 36376  36480   }
 36377  36481   
 36378  36482   #ifndef SQLITE_OMIT_LOAD_EXTENSION
 36379  36483   /*
 36380  36484   ** Interfaces for opening a shared library, finding entry points
 36381  36485   ** within the shared library, and closing the shared library.
 36382  36486   */
 36383         -/*
 36384         -** Interfaces for opening a shared library, finding entry points
 36385         -** within the shared library, and closing the shared library.
 36386         -*/
 36387  36487   static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
 36388  36488     HANDLE h;
        36489  +#if defined(__CYGWIN__)
        36490  +  int nFull = pVfs->mxPathname+1;
        36491  +  char *zFull = sqlite3MallocZero( nFull );
        36492  +  void *zConverted = 0;
        36493  +  if( zFull==0 ){
        36494  +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
        36495  +    return 0;
        36496  +  }
        36497  +  if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
        36498  +    sqlite3_free(zFull);
        36499  +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
        36500  +    return 0;
        36501  +  }
        36502  +  zConverted = winConvertFromUtf8Filename(zFull);
        36503  +  sqlite3_free(zFull);
        36504  +#else
 36389  36505     void *zConverted = winConvertFromUtf8Filename(zFilename);
 36390  36506     UNUSED_PARAMETER(pVfs);
        36507  +#endif
 36391  36508     if( zConverted==0 ){
        36509  +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
 36392  36510       return 0;
 36393  36511     }
 36394  36512     if( osIsNT() ){
 36395  36513   #if SQLITE_OS_WINRT
 36396  36514       h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
 36397  36515   #else
 36398  36516       h = osLoadLibraryW((LPCWSTR)zConverted);
................................................................................
 36399  36517   #endif
 36400  36518     }
 36401  36519   #ifdef SQLITE_WIN32_HAS_ANSI
 36402  36520     else{
 36403  36521       h = osLoadLibraryA((char*)zConverted);
 36404  36522     }
 36405  36523   #endif
        36524  +  OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
 36406  36525     sqlite3_free(zConverted);
 36407  36526     return (void*)h;
 36408  36527   }
 36409  36528   static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
 36410  36529     UNUSED_PARAMETER(pVfs);
 36411  36530     winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
 36412  36531   }
 36413  36532   static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
        36533  +  FARPROC proc;
 36414  36534     UNUSED_PARAMETER(pVfs);
 36415         -  return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
        36535  +  proc = osGetProcAddressA((HANDLE)pH, zSym);
        36536  +  OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
        36537  +           (void*)pH, zSym, (void*)proc));
        36538  +  return (void(*)(void))proc;
 36416  36539   }
 36417  36540   static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
 36418  36541     UNUSED_PARAMETER(pVfs);
 36419  36542     osFreeLibrary((HANDLE)pHandle);
        36543  +  OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
 36420  36544   }
 36421  36545   #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 36422  36546     #define winDlOpen  0
 36423  36547     #define winDlError 0
 36424  36548     #define winDlSym   0
 36425  36549     #define winDlClose 0
 36426  36550   #endif
................................................................................
 37108  37232   struct PCache {
 37109  37233     PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
 37110  37234     PgHdr *pSynced;                     /* Last synced page in dirty page list */
 37111  37235     int nRef;                           /* Number of referenced pages */
 37112  37236     int szCache;                        /* Configured cache size */
 37113  37237     int szPage;                         /* Size of every page in this cache */
 37114  37238     int szExtra;                        /* Size of extra space for each page */
 37115         -  int bPurgeable;                     /* True if pages are on backing store */
        37239  +  u8 bPurgeable;                      /* True if pages are on backing store */
        37240  +  u8 eCreate;                         /* eCreate value for for xFetch() */
 37116  37241     int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
 37117  37242     void *pStress;                      /* Argument to xStress */
 37118  37243     sqlite3_pcache *pCache;             /* Pluggable cache module */
 37119  37244     PgHdr *pPage1;                      /* Reference to page 1 */
 37120  37245   };
 37121  37246   
 37122  37247   /*
................................................................................
 37175  37300       p->pDirtyTail = pPage->pDirtyPrev;
 37176  37301     }
 37177  37302     if( pPage->pDirtyPrev ){
 37178  37303       pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
 37179  37304     }else{
 37180  37305       assert( pPage==p->pDirty );
 37181  37306       p->pDirty = pPage->pDirtyNext;
        37307  +    if( p->pDirty==0 && p->bPurgeable ){
        37308  +      assert( p->eCreate==1 );
        37309  +      p->eCreate = 2;
        37310  +    }
 37182  37311     }
 37183  37312     pPage->pDirtyNext = 0;
 37184  37313     pPage->pDirtyPrev = 0;
 37185  37314   
 37186  37315     expensive_assert( pcacheCheckSynced(p) );
 37187  37316   }
 37188  37317   
................................................................................
 37195  37324   
 37196  37325     assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
 37197  37326   
 37198  37327     pPage->pDirtyNext = p->pDirty;
 37199  37328     if( pPage->pDirtyNext ){
 37200  37329       assert( pPage->pDirtyNext->pDirtyPrev==0 );
 37201  37330       pPage->pDirtyNext->pDirtyPrev = pPage;
        37331  +  }else if( p->bPurgeable ){
        37332  +    assert( p->eCreate==2 );
        37333  +    p->eCreate = 1;
 37202  37334     }
 37203  37335     p->pDirty = pPage;
 37204  37336     if( !p->pDirtyTail ){
 37205  37337       p->pDirtyTail = pPage;
 37206  37338     }
 37207  37339     if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
 37208  37340       p->pSynced = pPage;
................................................................................
 37264  37396     void *pStress,               /* Argument to xStress */
 37265  37397     PCache *p                    /* Preallocated space for the PCache */
 37266  37398   ){
 37267  37399     memset(p, 0, sizeof(PCache));
 37268  37400     p->szPage = szPage;
 37269  37401     p->szExtra = szExtra;
 37270  37402     p->bPurgeable = bPurgeable;
        37403  +  p->eCreate = 2;
 37271  37404     p->xStress = xStress;
 37272  37405     p->pStress = pStress;
 37273  37406     p->szCache = 100;
 37274  37407   }
 37275  37408   
 37276  37409   /*
 37277  37410   ** Change the page size for PCache object. The caller must ensure that there
................................................................................
 37303  37436   */
 37304  37437   SQLITE_PRIVATE int sqlite3PcacheFetch(
 37305  37438     PCache *pCache,       /* Obtain the page from this cache */
 37306  37439     Pgno pgno,            /* Page number to obtain */
 37307  37440     int createFlag,       /* If true, create page if it does not exist already */
 37308  37441     PgHdr **ppPage        /* Write the page here */
 37309  37442   ){
 37310         -  sqlite3_pcache_page *pPage = 0;
        37443  +  sqlite3_pcache_page *pPage;
 37311  37444     PgHdr *pPgHdr = 0;
 37312  37445     int eCreate;
 37313  37446   
 37314  37447     assert( pCache!=0 );
 37315  37448     assert( createFlag==1 || createFlag==0 );
 37316  37449     assert( pgno>0 );
 37317  37450   
 37318  37451     /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
 37319  37452     ** allocate it now.
 37320  37453     */
 37321         -  if( !pCache->pCache && createFlag ){
        37454  +  if( !pCache->pCache ){
 37322  37455       sqlite3_pcache *p;
        37456  +    if( !createFlag ){
        37457  +      *ppPage = 0;
        37458  +      return SQLITE_OK;
        37459  +    }
 37323  37460       p = sqlite3GlobalConfig.pcache2.xCreate(
 37324  37461           pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
 37325  37462       );
 37326  37463       if( !p ){
 37327  37464         return SQLITE_NOMEM;
 37328  37465       }
 37329  37466       sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
 37330  37467       pCache->pCache = p;
 37331  37468     }
 37332  37469   
 37333         -  eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
 37334         -  if( pCache->pCache ){
 37335         -    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
 37336         -  }
 37337         -
        37470  +  /* eCreate defines what to do if the page does not exist.
        37471  +  **    0     Do not allocate a new page.  (createFlag==0)
        37472  +  **    1     Allocate a new page if doing so is inexpensive.
        37473  +  **          (createFlag==1 AND bPurgeable AND pDirty)
        37474  +  **    2     Allocate a new page even it doing so is difficult.
        37475  +  **          (createFlag==1 AND !(bPurgeable AND pDirty)
        37476  +  */
        37477  +  eCreate = createFlag==0 ? 0 : pCache->eCreate;
        37478  +  assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
        37479  +  pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
 37338  37480     if( !pPage && eCreate==1 ){
 37339  37481       PgHdr *pPg;
 37340  37482   
 37341  37483       /* Find a dirty page to write-out and recycle. First try to find a 
 37342  37484       ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
 37343  37485       ** cleared), but if that is not possible settle for any other 
 37344  37486       ** unreferenced dirty page.
................................................................................
 47918  48060     }
 47919  48061   
 47920  48062     if( rc!=SQLITE_OK ){
 47921  48063       walIndexClose(pRet, 0);
 47922  48064       sqlite3OsClose(pRet->pWalFd);
 47923  48065       sqlite3_free(pRet);
 47924  48066     }else{
 47925         -    int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
        48067  +    int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
 47926  48068       if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
 47927  48069       if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
 47928  48070         pRet->padToSectorBoundary = 0;
 47929  48071       }
 47930  48072       *ppWal = pRet;
 47931  48073       WALTRACE(("WAL%d: opened\n", pRet));
 47932  48074     }
................................................................................
 49289  49431       int iFirstAmt = (int)(p->iSyncPoint - iOffset);
 49290  49432       rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
 49291  49433       if( rc ) return rc;
 49292  49434       iOffset += iFirstAmt;
 49293  49435       iAmt -= iFirstAmt;
 49294  49436       pContent = (void*)(iFirstAmt + (char*)pContent);
 49295  49437       assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
 49296         -    rc = sqlite3OsSync(p->pFd, p->syncFlags);
        49438  +    rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
 49297  49439       if( iAmt==0 || rc ) return rc;
 49298  49440     }
 49299  49441     rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
 49300  49442     return rc;
 49301  49443   }
 49302  49444   
 49303  49445   /*
................................................................................
 50227  50369     BtShared *pBt;            /* The BtShared this cursor points to */
 50228  50370     BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
 50229  50371     struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
 50230  50372   #ifndef SQLITE_OMIT_INCRBLOB
 50231  50373     Pgno *aOverflow;          /* Cache of overflow page locations */
 50232  50374   #endif
 50233  50375     Pgno pgnoRoot;            /* The root page of this tree */
 50234         -  sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
 50235  50376     CellInfo info;            /* A parse of the cell we are pointing at */
 50236  50377     i64 nKey;        /* Size of pKey, or last integer key */
 50237  50378     void *pKey;      /* Saved key that was cursor's last known position */
 50238  50379     int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
 50239  50380     u8 wrFlag;                /* True if writable */
 50240  50381     u8 atLast;                /* Cursor pointing to the last entry */
 50241  50382     u8 validNKey;             /* True if info.nKey is valid */
................................................................................
 52211  52352     assert( sqlite3PagerGetData(pPage->pDbPage) == data );
 52212  52353     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 52213  52354     assert( sqlite3_mutex_held(pBt->mutex) );
 52214  52355     if( pBt->btsFlags & BTS_SECURE_DELETE ){
 52215  52356       memset(&data[hdr], 0, pBt->usableSize - hdr);
 52216  52357     }
 52217  52358     data[hdr] = (char)flags;
 52218         -  first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
        52359  +  first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
 52219  52360     memset(&data[hdr+1], 0, 4);
 52220  52361     data[hdr+7] = 0;
 52221  52362     put2byte(&data[hdr+5], pBt->usableSize);
 52222  52363     pPage->nFree = (u16)(pBt->usableSize - first);
 52223  52364     decodeFlags(pPage, flags);
 52224         -  pPage->hdrOffset = hdr;
 52225  52365     pPage->cellOffset = first;
 52226  52366     pPage->aDataEnd = &data[pBt->usableSize];
 52227  52367     pPage->aCellIdx = &data[first];
 52228  52368     pPage->nOverflow = 0;
 52229  52369     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 52230  52370     pPage->maskPage = (u16)(pBt->pageSize - 1);
 52231  52371     pPage->nCell = 0;
................................................................................
 54301  54441     pCur->wrFlag = (u8)wrFlag;
 54302  54442     pCur->pNext = pBt->pCursor;
 54303  54443     if( pCur->pNext ){
 54304  54444       pCur->pNext->pPrev = pCur;
 54305  54445     }
 54306  54446     pBt->pCursor = pCur;
 54307  54447     pCur->eState = CURSOR_INVALID;
 54308         -  pCur->cachedRowid = 0;
 54309  54448     return SQLITE_OK;
 54310  54449   }
 54311  54450   SQLITE_PRIVATE int sqlite3BtreeCursor(
 54312  54451     Btree *p,                                   /* The btree */
 54313  54452     int iTable,                                 /* Root page of table to open */
 54314  54453     int wrFlag,                                 /* 1 to write. 0 read-only */
 54315  54454     struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
................................................................................
 54342  54481   ** do not need to be zeroed and they are large, so we can save a lot
 54343  54482   ** of run-time by skipping the initialization of those elements.
 54344  54483   */
 54345  54484   SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
 54346  54485     memset(p, 0, offsetof(BtCursor, iPage));
 54347  54486   }
 54348  54487   
 54349         -/*
 54350         -** Set the cached rowid value of every cursor in the same database file
 54351         -** as pCur and having the same root page number as pCur.  The value is
 54352         -** set to iRowid.
 54353         -**
 54354         -** Only positive rowid values are considered valid for this cache.
 54355         -** The cache is initialized to zero, indicating an invalid cache.
 54356         -** A btree will work fine with zero or negative rowids.  We just cannot
 54357         -** cache zero or negative rowids, which means tables that use zero or
 54358         -** negative rowids might run a little slower.  But in practice, zero
 54359         -** or negative rowids are very uncommon so this should not be a problem.
 54360         -*/
 54361         -SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
 54362         -  BtCursor *p;
 54363         -  for(p=pCur->pBt->pCursor; p; p=p->pNext){
 54364         -    if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
 54365         -  }
 54366         -  assert( pCur->cachedRowid==iRowid );
 54367         -}
 54368         -
 54369         -/*
 54370         -** Return the cached rowid for the given cursor.  A negative or zero
 54371         -** return value indicates that the rowid cache is invalid and should be
 54372         -** ignored.  If the rowid cache has never before been set, then a
 54373         -** zero is returned.
 54374         -*/
 54375         -SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
 54376         -  return pCur->cachedRowid;
 54377         -}
 54378         -
 54379  54488   /*
 54380  54489   ** Close a cursor.  The read lock on the database file is released
 54381  54490   ** when the last cursor is closed.
 54382  54491   */
 54383  54492   SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
 54384  54493     Btree *pBtree = pCur->pBtree;
 54385  54494     if( pBtree ){
................................................................................
 55248  55357     BtCursor *pCur,          /* The cursor to be moved */
 55249  55358     UnpackedRecord *pIdxKey, /* Unpacked index key */
 55250  55359     i64 intKey,              /* The table key */
 55251  55360     int biasRight,           /* If true, bias the search to the high end */
 55252  55361     int *pRes                /* Write search results here */
 55253  55362   ){
 55254  55363     int rc;
        55364  +  RecordCompare xRecordCompare;
 55255  55365   
 55256  55366     assert( cursorHoldsMutex(pCur) );
 55257  55367     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55258  55368     assert( pRes );
 55259  55369     assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
 55260  55370   
 55261  55371     /* If the cursor is already positioned at the point we are trying
................................................................................
 55268  55378         return SQLITE_OK;
 55269  55379       }
 55270  55380       if( pCur->atLast && pCur->info.nKey<intKey ){
 55271  55381         *pRes = -1;
 55272  55382         return SQLITE_OK;
 55273  55383       }
 55274  55384     }
        55385  +
        55386  +  if( pIdxKey ){
        55387  +    xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
        55388  +    assert( pIdxKey->default_rc==1 
        55389  +         || pIdxKey->default_rc==0 
        55390  +         || pIdxKey->default_rc==-1
        55391  +    );
        55392  +  }else{
        55393  +    xRecordCompare = 0; /* Not actually used.  Avoids a compiler warning. */
        55394  +  }
 55275  55395   
 55276  55396     rc = moveToRoot(pCur);
 55277  55397     if( rc ){
 55278  55398       return rc;
 55279  55399     }
 55280  55400     assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
 55281  55401     assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
................................................................................
 55301  55421       assert( pPage->nCell>0 );
 55302  55422       assert( pPage->intKey==(pIdxKey==0) );
 55303  55423       lwr = 0;
 55304  55424       upr = pPage->nCell-1;
 55305  55425       assert( biasRight==0 || biasRight==1 );
 55306  55426       idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
 55307  55427       pCur->aiIdx[pCur->iPage] = (u16)idx;
 55308         -    if( pPage->intKey ){
        55428  +    if( xRecordCompare==0 ){
 55309  55429         for(;;){
 55310  55430           i64 nCellKey;
 55311  55431           pCell = findCell(pPage, idx) + pPage->childPtrSize;
 55312  55432           if( pPage->hasData ){
 55313  55433             while( 0x80 <= *(pCell++) ){
 55314  55434               if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
 55315  55435             }
................................................................................
 55353  55473           */
 55354  55474           nCell = pCell[0];
 55355  55475           if( nCell<=pPage->max1bytePayload ){
 55356  55476             /* This branch runs if the record-size field of the cell is a
 55357  55477             ** single byte varint and the record fits entirely on the main
 55358  55478             ** b-tree page.  */
 55359  55479             testcase( pCell+nCell+1==pPage->aDataEnd );
 55360         -          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
        55480  +          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
 55361  55481           }else if( !(pCell[1] & 0x80) 
 55362  55482             && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
 55363  55483           ){
 55364  55484             /* The record-size field is a 2 byte varint and the record 
 55365  55485             ** fits entirely on the main b-tree page.  */
 55366  55486             testcase( pCell+nCell+2==pPage->aDataEnd );
 55367         -          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
        55487  +          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
 55368  55488           }else{
 55369  55489             /* The record flows over onto one or more overflow pages. In
 55370  55490             ** this case the whole cell needs to be parsed, a buffer allocated
 55371  55491             ** and accessPayload() used to retrieve the record into the
 55372  55492             ** buffer before VdbeRecordCompare() can be called. */
 55373  55493             void *pCellKey;
 55374  55494             u8 * const pCellBody = pCell - pPage->childPtrSize;
................................................................................
 55381  55501             }
 55382  55502             pCur->aiIdx[pCur->iPage] = (u16)idx;
 55383  55503             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
 55384  55504             if( rc ){
 55385  55505               sqlite3_free(pCellKey);
 55386  55506               goto moveto_finish;
 55387  55507             }
 55388         -          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
        55508  +          c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
 55389  55509             sqlite3_free(pCellKey);
 55390  55510           }
 55391  55511           if( c<0 ){
 55392  55512             lwr = idx+1;
 55393  55513           }else if( c>0 ){
 55394  55514             upr = idx-1;
 55395  55515           }else{
................................................................................
 55446  55566   }
 55447  55567   
 55448  55568   /*
 55449  55569   ** Advance the cursor to the next entry in the database.  If
 55450  55570   ** successful then set *pRes=0.  If the cursor
 55451  55571   ** was already pointing to the last entry in the database before
 55452  55572   ** this routine was called, then set *pRes=1.
        55573  +**
        55574  +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
        55575  +** will be 1 if the cursor being stepped corresponds to an SQL index and
        55576  +** if this routine could have been skipped if that SQL index had been
        55577  +** a unique index.  Otherwise the caller will have set *pRes to zero.
        55578  +** Zero is the common case. The btree implementation is free to use the
        55579  +** initial *pRes value as a hint to improve performance, but the current
        55580  +** SQLite btree implementation does not. (Note that the comdb2 btree
        55581  +** implementation does use this hint, however.)
 55453  55582   */
 55454  55583   SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
 55455  55584     int rc;
 55456  55585     int idx;
 55457  55586     MemPage *pPage;
 55458  55587   
 55459  55588     assert( cursorHoldsMutex(pCur) );
 55460  55589     assert( pRes!=0 );
        55590  +  assert( *pRes==0 || *pRes==1 );
 55461  55591     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 55462  55592     if( pCur->eState!=CURSOR_VALID ){
 55463  55593       rc = restoreCursorPosition(pCur);
 55464  55594       if( rc!=SQLITE_OK ){
 55465  55595         *pRes = 0;
 55466  55596         return rc;
 55467  55597       }
................................................................................
 55532  55662   
 55533  55663   
 55534  55664   /*
 55535  55665   ** Step the cursor to the back to the previous entry in the database.  If
 55536  55666   ** successful then set *pRes=0.  If the cursor
 55537  55667   ** was already pointing to the first entry in the database before
 55538  55668   ** this routine was called, then set *pRes=1.
        55669  +**
        55670  +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
        55671  +** will be 1 if the cursor being stepped corresponds to an SQL index and
        55672  +** if this routine could have been skipped if that SQL index had been
        55673  +** a unique index.  Otherwise the caller will have set *pRes to zero.
        55674  +** Zero is the common case. The btree implementation is free to use the
        55675  +** initial *pRes value as a hint to improve performance, but the current
        55676  +** SQLite btree implementation does not. (Note that the comdb2 btree
        55677  +** implementation does use this hint, however.)
 55539  55678   */
 55540  55679   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
 55541  55680     int rc;
 55542  55681     MemPage *pPage;
 55543  55682   
 55544  55683     assert( cursorHoldsMutex(pCur) );
 55545  55684     assert( pRes!=0 );
        55685  +  assert( *pRes==0 || *pRes==1 );
 55546  55686     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 55547  55687     pCur->atLast = 0;
 55548  55688     if( pCur->eState!=CURSOR_VALID ){
 55549  55689       if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
 55550  55690         rc = btreeRestoreCursorPosition(pCur);
 55551  55691         if( rc!=SQLITE_OK ){
 55552  55692           *pRes = 0;
................................................................................
 57635  57775     ** that the cursor is already where it needs to be and returns without
 57636  57776     ** doing any work. To avoid thwarting these optimizations, it is important
 57637  57777     ** not to clear the cursor here.
 57638  57778     */
 57639  57779     rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 57640  57780     if( rc ) return rc;
 57641  57781   
 57642         -  /* If this is an insert into a table b-tree, invalidate any incrblob 
 57643         -  ** cursors open on the row being replaced (assuming this is a replace
 57644         -  ** operation - if it is not, the following is a no-op).  */
 57645  57782     if( pCur->pKeyInfo==0 ){
        57783  +    /* If this is an insert into a table b-tree, invalidate any incrblob 
        57784  +    ** cursors open on the row being replaced */
 57646  57785       invalidateIncrblobCursors(p, nKey, 0);
        57786  +
        57787  +    /* If the cursor is currently on the last row and we are appending a
        57788  +    ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
        57789  +    ** call */
        57790  +    if( pCur->validNKey && nKey>0 && pCur->info.nKey==nKey-1 ){
        57791  +      loc = -1;
        57792  +    }
 57647  57793     }
 57648  57794   
 57649  57795     if( !loc ){
 57650  57796       rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
 57651  57797       if( rc ) return rc;
 57652  57798     }
 57653  57799     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
................................................................................
 57709  57855     ** is advantageous to leave the cursor pointing to the last entry in
 57710  57856     ** the b-tree if possible. If the cursor is left pointing to the last
 57711  57857     ** entry in the table, and the next row inserted has an integer key
 57712  57858     ** larger than the largest existing key, it is possible to insert the
 57713  57859     ** row without seeking the cursor. This can be a big performance boost.
 57714  57860     */
 57715  57861     pCur->info.nSize = 0;
 57716         -  pCur->validNKey = 0;
 57717  57862     if( rc==SQLITE_OK && pPage->nOverflow ){
        57863  +    pCur->validNKey = 0;
 57718  57864       rc = balance(pCur);
 57719  57865   
 57720  57866       /* Must make sure nOverflow is reset to zero even if the balance()
 57721  57867       ** fails. Internal data structure corruption will result otherwise. 
 57722  57868       ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 57723  57869       ** from trying to save the current position of the cursor.  */
 57724  57870       pCur->apPage[pCur->iPage]->nOverflow = 0;
................................................................................
 57765  57911     ** the cursor to the largest entry in the tree that is smaller than
 57766  57912     ** the entry being deleted. This cell will replace the cell being deleted
 57767  57913     ** from the internal node. The 'previous' entry is used for this instead
 57768  57914     ** of the 'next' entry, as the previous entry is always a part of the
 57769  57915     ** sub-tree headed by the child page of the cell being deleted. This makes
 57770  57916     ** balancing the tree following the delete operation easier.  */
 57771  57917     if( !pPage->leaf ){
 57772         -    int notUsed;
        57918  +    int notUsed = 0;
 57773  57919       rc = sqlite3BtreePrevious(pCur, &notUsed);
 57774  57920       if( rc ) return rc;
 57775  57921     }
 57776  57922   
 57777  57923     /* Save the positions of any other cursors open on this table before
 57778  57924     ** making any modifications. Make the page containing the entry to be 
 57779  57925     ** deleted writable. Then free any overflow pages associated with the 
................................................................................
 59918  60064   *************************************************************************
 59919  60065   **
 59920  60066   ** This file contains code use to manipulate "Mem" structure.  A "Mem"
 59921  60067   ** stores a single value in the VDBE.  Mem is an opaque structure visible
 59922  60068   ** only within the VDBE.  Interface routines refer to a Mem using the
 59923  60069   ** name sqlite_value
 59924  60070   */
        60071  +
        60072  +#ifdef SQLITE_DEBUG
        60073  +/*
        60074  +** Check invariants on a Mem object.
        60075  +**
        60076  +** This routine is intended for use inside of assert() statements, like
        60077  +** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
        60078  +*/
        60079  +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
        60080  +  /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor
        60081  +  ** function for Mem.z 
        60082  +  */
        60083  +  assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
        60084  +  assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 );
        60085  +
        60086  +  /* If p holds a string or blob, the Mem.z must point to exactly
        60087  +  ** one of the following:
        60088  +  **
        60089  +  **   (1) Memory in Mem.zMalloc and managed by the Mem object
        60090  +  **   (2) Memory to be freed using Mem.xDel
        60091  +  **   (3) An ephermal string or blob
        60092  +  **   (4) A static string or blob
        60093  +  */
        60094  +  if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){
        60095  +    assert( 
        60096  +      ((p->z==p->zMalloc)? 1 : 0) +
        60097  +      ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
        60098  +      ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
        60099  +      ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
        60100  +    );
        60101  +  }
        60102  +
        60103  +  return 1;
        60104  +}
        60105  +#endif
        60106  +
 59925  60107   
 59926  60108   /*
 59927  60109   ** If pMem is an object with a valid string representation, this routine
 59928  60110   ** ensures the internal encoding for the string representation is
 59929  60111   ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
 59930  60112   **
 59931  60113   ** If pMem is not a string object, or the encoding of the string
................................................................................
 59968  60150   **
 59969  60151   ** If the bPreserve argument is true, then copy of the content of
 59970  60152   ** pMem->z into the new allocation.  pMem must be either a string or
 59971  60153   ** blob if bPreserve is true.  If bPreserve is false, any prior content
 59972  60154   ** in pMem->z is discarded.
 59973  60155   */
 59974  60156   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
 59975         -  assert( 1 >=
 59976         -    ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
 59977         -    (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
 59978         -    ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
 59979         -    ((pMem->flags&MEM_Static) ? 1 : 0)
 59980         -  );
        60157  +  assert( sqlite3VdbeCheckMemInvariants(pMem) );
 59981  60158     assert( (pMem->flags&MEM_RowSet)==0 );
 59982  60159   
 59983  60160     /* If the bPreserve flag is set to true, then the memory cell must already
 59984  60161     ** contain a valid string or blob value.  */
 59985  60162     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 59986  60163     testcase( bPreserve && pMem->z==0 );
 59987  60164   
................................................................................
 59991  60168         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 59992  60169         bPreserve = 0;
 59993  60170       }else{
 59994  60171         sqlite3DbFree(pMem->db, pMem->zMalloc);
 59995  60172         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 59996  60173       }
 59997  60174       if( pMem->zMalloc==0 ){
 59998         -      sqlite3VdbeMemRelease(pMem);
        60175  +      VdbeMemRelease(pMem);
        60176  +      pMem->z = 0;
 59999  60177         pMem->flags = MEM_Null;  
 60000  60178         return SQLITE_NOMEM;
 60001  60179       }
 60002  60180     }
 60003  60181   
 60004  60182     if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
 60005  60183       memcpy(pMem->zMalloc, pMem->z, pMem->n);
 60006  60184     }
 60007         -  if( (pMem->flags&MEM_Dyn)!=0 && pMem->xDel ){
 60008         -    assert( pMem->xDel!=SQLITE_DYNAMIC );
        60185  +  if( (pMem->flags&MEM_Dyn)!=0 ){
        60186  +    assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
 60009  60187       pMem->xDel((void *)(pMem->z));
 60010  60188     }
 60011  60189   
 60012  60190     pMem->z = pMem->zMalloc;
 60013         -  pMem->flags &= ~(MEM_Ephem|MEM_Static);
        60191  +  pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
 60014  60192     pMem->xDel = 0;
 60015  60193     return SQLITE_OK;
 60016  60194   }
 60017  60195   
 60018  60196   /*
 60019  60197   ** Make the given Mem object MEM_Dyn.  In other words, make it so
 60020  60198   ** that any TEXT or BLOB content is stored in memory obtained from
................................................................................
 60175  60353   */
 60176  60354   SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
 60177  60355     assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
 60178  60356     if( p->flags&MEM_Agg ){
 60179  60357       sqlite3VdbeMemFinalize(p, p->u.pDef);
 60180  60358       assert( (p->flags & MEM_Agg)==0 );
 60181  60359       sqlite3VdbeMemRelease(p);
 60182         -  }else if( p->flags&MEM_Dyn && p->xDel ){
        60360  +  }else if( p->flags&MEM_Dyn ){
 60183  60361       assert( (p->flags&MEM_RowSet)==0 );
 60184         -    assert( p->xDel!=SQLITE_DYNAMIC );
        60362  +    assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
 60185  60363       p->xDel((void *)p->z);
 60186  60364       p->xDel = 0;
 60187  60365     }else if( p->flags&MEM_RowSet ){
 60188  60366       sqlite3RowSetClear(p->u.pRowSet);
 60189  60367     }else if( p->flags&MEM_Frame ){
 60190  60368       sqlite3VdbeMemSetNull(p);
 60191  60369     }
 60192  60370   }
 60193  60371   
 60194  60372   /*
 60195  60373   ** Release any memory held by the Mem. This may leave the Mem in an
 60196  60374   ** inconsistent state, for example with (Mem.z==0) and
 60197         -** (Mem.type==SQLITE_TEXT).
        60375  +** (Mem.flags==MEM_Str).
 60198  60376   */
 60199  60377   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
        60378  +  assert( sqlite3VdbeCheckMemInvariants(p) );
 60200  60379     VdbeMemRelease(p);
 60201  60380     if( p->zMalloc ){
 60202  60381       sqlite3DbFree(p->db, p->zMalloc);
 60203  60382       p->zMalloc = 0;
 60204  60383     }
 60205  60384     p->z = 0;
 60206  60385     assert( p->xDel==0 );  /* Zeroed by VdbeMemRelease() above */
................................................................................
 60381  60560       pFrame->pParent = pFrame->v->pDelFrame;
 60382  60561       pFrame->v->pDelFrame = pFrame;
 60383  60562     }
 60384  60563     if( pMem->flags & MEM_RowSet ){
 60385  60564       sqlite3RowSetClear(pMem->u.pRowSet);
 60386  60565     }
 60387  60566     MemSetTypeFlag(pMem, MEM_Null);
 60388         -  pMem->type = SQLITE_NULL;
 60389  60567   }
 60390  60568   SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
 60391  60569     sqlite3VdbeMemSetNull((Mem*)p); 
 60392  60570   }
 60393  60571   
 60394  60572   /*
 60395  60573   ** Delete any previous value and set the value to be a BLOB of length
 60396  60574   ** n containing all zeros.
 60397  60575   */
 60398  60576   SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
 60399  60577     sqlite3VdbeMemRelease(pMem);
 60400  60578     pMem->flags = MEM_Blob|MEM_Zero;
 60401         -  pMem->type = SQLITE_BLOB;
 60402  60579     pMem->n = 0;
 60403  60580     if( n<0 ) n = 0;
 60404  60581     pMem->u.nZero = n;
 60405  60582     pMem->enc = SQLITE_UTF8;
 60406  60583   
 60407  60584   #ifdef SQLITE_OMIT_INCRBLOB
 60408  60585     sqlite3VdbeMemGrow(pMem, n, 0);
................................................................................
 60417  60594   ** Delete any previous value and set the value stored in *pMem to val,
 60418  60595   ** manifest type INTEGER.
 60419  60596   */
 60420  60597   SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
 60421  60598     sqlite3VdbeMemRelease(pMem);
 60422  60599     pMem->u.i = val;
 60423  60600     pMem->flags = MEM_Int;
 60424         -  pMem->type = SQLITE_INTEGER;
 60425  60601   }
 60426  60602   
 60427  60603   #ifndef SQLITE_OMIT_FLOATING_POINT
 60428  60604   /*
 60429  60605   ** Delete any previous value and set the value stored in *pMem to val,
 60430  60606   ** manifest type REAL.
 60431  60607   */
................................................................................
 60432  60608   SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
 60433  60609     if( sqlite3IsNaN(val) ){
 60434  60610       sqlite3VdbeMemSetNull(pMem);
 60435  60611     }else{
 60436  60612       sqlite3VdbeMemRelease(pMem);
 60437  60613       pMem->r = val;
 60438  60614       pMem->flags = MEM_Real;
 60439         -    pMem->type = SQLITE_FLOAT;
 60440  60615     }
 60441  60616   }
 60442  60617   #endif
 60443  60618   
 60444  60619   /*
 60445  60620   ** Delete any previous value and set the value of pMem to be an
 60446  60621   ** empty boolean index.
................................................................................
 60488  60663   ** copies are not misused.
 60489  60664   */
 60490  60665   SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
 60491  60666     int i;
 60492  60667     Mem *pX;
 60493  60668     for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
 60494  60669       if( pX->pScopyFrom==pMem ){
 60495         -      pX->flags |= MEM_Invalid;
        60670  +      pX->flags |= MEM_Undefined;
 60496  60671         pX->pScopyFrom = 0;
 60497  60672       }
 60498  60673     }
 60499  60674     pMem->pScopyFrom = 0;
 60500  60675   }
 60501  60676   #endif /* SQLITE_DEBUG */
 60502  60677   
................................................................................
 60530  60705   SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
 60531  60706     int rc = SQLITE_OK;
 60532  60707   
 60533  60708     assert( (pFrom->flags & MEM_RowSet)==0 );
 60534  60709     VdbeMemRelease(pTo);
 60535  60710     memcpy(pTo, pFrom, MEMCELLSIZE);
 60536  60711     pTo->flags &= ~MEM_Dyn;
        60712  +  pTo->xDel = 0;
 60537  60713   
 60538  60714     if( pTo->flags&(MEM_Str|MEM_Blob) ){
 60539  60715       if( 0==(pFrom->flags&MEM_Static) ){
 60540  60716         pTo->flags |= MEM_Ephem;
 60541  60717         rc = sqlite3VdbeMemMakeWriteable(pTo);
 60542  60718       }
 60543  60719     }
................................................................................
 60640  60816       pMem->xDel = xDel;
 60641  60817       flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
 60642  60818     }
 60643  60819   
 60644  60820     pMem->n = nByte;
 60645  60821     pMem->flags = flags;
 60646  60822     pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
 60647         -  pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
 60648  60823   
 60649  60824   #ifndef SQLITE_OMIT_UTF16
 60650  60825     if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
 60651  60826       return SQLITE_NOMEM;
 60652  60827     }
 60653  60828   #endif
 60654  60829   
................................................................................
 60655  60830     if( nByte>iLimit ){
 60656  60831       return SQLITE_TOOBIG;
 60657  60832     }
 60658  60833   
 60659  60834     return SQLITE_OK;
 60660  60835   }
 60661  60836   
 60662         -/*
 60663         -** Compare the values contained by the two memory cells, returning
 60664         -** negative, zero or positive if pMem1 is less than, equal to, or greater
 60665         -** than pMem2. Sorting order is NULL's first, followed by numbers (integers
 60666         -** and reals) sorted numerically, followed by text ordered by the collating
 60667         -** sequence pColl and finally blob's ordered by memcmp().
 60668         -**
 60669         -** Two NULL values are considered equal by this function.
 60670         -*/
 60671         -SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
 60672         -  int rc;
 60673         -  int f1, f2;
 60674         -  int combined_flags;
 60675         -
 60676         -  f1 = pMem1->flags;
 60677         -  f2 = pMem2->flags;
 60678         -  combined_flags = f1|f2;
 60679         -  assert( (combined_flags & MEM_RowSet)==0 );
 60680         - 
 60681         -  /* If one value is NULL, it is less than the other. If both values
 60682         -  ** are NULL, return 0.
 60683         -  */
 60684         -  if( combined_flags&MEM_Null ){
 60685         -    return (f2&MEM_Null) - (f1&MEM_Null);
 60686         -  }
 60687         -
 60688         -  /* If one value is a number and the other is not, the number is less.
 60689         -  ** If both are numbers, compare as reals if one is a real, or as integers
 60690         -  ** if both values are integers.
 60691         -  */
 60692         -  if( combined_flags&(MEM_Int|MEM_Real) ){
 60693         -    double r1, r2;
 60694         -    if( (f1 & f2 & MEM_Int)!=0 ){
 60695         -      if( pMem1->u.i < pMem2->u.i ) return -1;
 60696         -      if( pMem1->u.i > pMem2->u.i ) return 1;
 60697         -      return 0;
 60698         -    }
 60699         -    if( (f1&MEM_Real)!=0 ){
 60700         -      r1 = pMem1->r;
 60701         -    }else if( (f1&MEM_Int)!=0 ){
 60702         -      r1 = (double)pMem1->u.i;
 60703         -    }else{
 60704         -      return 1;
 60705         -    }
 60706         -    if( (f2&MEM_Real)!=0 ){
 60707         -      r2 = pMem2->r;
 60708         -    }else if( (f2&MEM_Int)!=0 ){
 60709         -      r2 = (double)pMem2->u.i;
 60710         -    }else{
 60711         -      return -1;
 60712         -    }
 60713         -    if( r1<r2 ) return -1;
 60714         -    if( r1>r2 ) return 1;
 60715         -    return 0;
 60716         -  }
 60717         -
 60718         -  /* If one value is a string and the other is a blob, the string is less.
 60719         -  ** If both are strings, compare using the collating functions.
 60720         -  */
 60721         -  if( combined_flags&MEM_Str ){
 60722         -    if( (f1 & MEM_Str)==0 ){
 60723         -      return 1;
 60724         -    }
 60725         -    if( (f2 & MEM_Str)==0 ){
 60726         -      return -1;
 60727         -    }
 60728         -
 60729         -    assert( pMem1->enc==pMem2->enc );
 60730         -    assert( pMem1->enc==SQLITE_UTF8 || 
 60731         -            pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
 60732         -
 60733         -    /* The collation sequence must be defined at this point, even if
 60734         -    ** the user deletes the collation sequence after the vdbe program is
 60735         -    ** compiled (this was not always the case).
 60736         -    */
 60737         -    assert( !pColl || pColl->xCmp );
 60738         -
 60739         -    if( pColl ){
 60740         -      if( pMem1->enc==pColl->enc ){
 60741         -        /* The strings are already in the correct encoding.  Call the
 60742         -        ** comparison function directly */
 60743         -        return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
 60744         -      }else{
 60745         -        const void *v1, *v2;
 60746         -        int n1, n2;
 60747         -        Mem c1;
 60748         -        Mem c2;
 60749         -        memset(&c1, 0, sizeof(c1));
 60750         -        memset(&c2, 0, sizeof(c2));
 60751         -        sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
 60752         -        sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
 60753         -        v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
 60754         -        n1 = v1==0 ? 0 : c1.n;
 60755         -        v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
 60756         -        n2 = v2==0 ? 0 : c2.n;
 60757         -        rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
 60758         -        sqlite3VdbeMemRelease(&c1);
 60759         -        sqlite3VdbeMemRelease(&c2);
 60760         -        return rc;
 60761         -      }
 60762         -    }
 60763         -    /* If a NULL pointer was passed as the collate function, fall through
 60764         -    ** to the blob case and use memcmp().  */
 60765         -  }
 60766         - 
 60767         -  /* Both values must be blobs.  Compare using memcmp().  */
 60768         -  rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
 60769         -  if( rc==0 ){
 60770         -    rc = pMem1->n - pMem2->n;
 60771         -  }
 60772         -  return rc;
 60773         -}
 60774         -
 60775  60837   /*
 60776  60838   ** Move data out of a btree key or data field and into a Mem structure.
 60777  60839   ** The data or key is taken from the entry that pCur is currently pointing
 60778  60840   ** to.  offset and amt determine what portion of the data or key to retrieve.
 60779  60841   ** key is true to get the key or false to get data.  The result is written
 60780  60842   ** into the pMem element.
 60781  60843   **
................................................................................
 60808  60870     }
 60809  60871     assert( zData!=0 );
 60810  60872   
 60811  60873     if( offset+amt<=available ){
 60812  60874       sqlite3VdbeMemRelease(pMem);
 60813  60875       pMem->z = &zData[offset];
 60814  60876       pMem->flags = MEM_Blob|MEM_Ephem;
        60877  +    pMem->n = (int)amt;
 60815  60878     }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
 60816         -    pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
 60817         -    pMem->enc = 0;
 60818         -    pMem->type = SQLITE_BLOB;
 60819  60879       if( key ){
 60820  60880         rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
 60821  60881       }else{
 60822  60882         rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
 60823  60883       }
 60824         -    pMem->z[amt] = 0;
 60825         -    pMem->z[amt+1] = 0;
 60826         -    if( rc!=SQLITE_OK ){
        60884  +    if( rc==SQLITE_OK ){
        60885  +      pMem->z[amt] = 0;
        60886  +      pMem->z[amt+1] = 0;
        60887  +      pMem->flags = MEM_Blob|MEM_Term;
        60888  +      pMem->n = (int)amt;
        60889  +    }else{
 60827  60890         sqlite3VdbeMemRelease(pMem);
 60828  60891       }
 60829  60892     }
 60830         -  pMem->n = (int)amt;
 60831  60893   
 60832  60894     return rc;
 60833  60895   }
 60834  60896   
 60835  60897   /* This function is only available internally, it is not part of the
 60836  60898   ** external API. It works in a similar way to sqlite3_value_text(),
 60837  60899   ** except the data returned is in the encoding specified by the second
................................................................................
 60881  60943   /*
 60882  60944   ** Create a new sqlite3_value object.
 60883  60945   */
 60884  60946   SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
 60885  60947     Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
 60886  60948     if( p ){
 60887  60949       p->flags = MEM_Null;
 60888         -    p->type = SQLITE_NULL;
 60889  60950       p->db = db;
 60890  60951     }
 60891  60952     return p;
 60892  60953   }
 60893  60954   
 60894  60955   /*
 60895  60956   ** Context object passed by sqlite3Stat4ProbeSetValue() through to 
................................................................................
 60927  60988         nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
 60928  60989         pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
 60929  60990         if( pRec ){
 60930  60991           pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
 60931  60992           if( pRec->pKeyInfo ){
 60932  60993             assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
 60933  60994             assert( pRec->pKeyInfo->enc==ENC(db) );
 60934         -          pRec->flags = UNPACKED_PREFIX_MATCH;
 60935  60995             pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
 60936  60996             for(i=0; i<nCol; i++){
 60937  60997               pRec->aMem[i].flags = MEM_Null;
 60938         -            pRec->aMem[i].type = SQLITE_NULL;
 60939  60998               pRec->aMem[i].db = db;
 60940  60999             }
 60941  61000           }else{
 60942  61001             sqlite3DbFree(db, pRec);
 60943  61002             pRec = 0;
 60944  61003           }
 60945  61004         }
................................................................................
 61004  61063       if( pVal==0 ) goto no_mem;
 61005  61064       if( ExprHasProperty(pExpr, EP_IntValue) ){
 61006  61065         sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
 61007  61066       }else{
 61008  61067         zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
 61009  61068         if( zVal==0 ) goto no_mem;
 61010  61069         sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
 61011         -      if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
 61012  61070       }
 61013  61071       if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
 61014  61072         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
 61015  61073       }else{
 61016  61074         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
 61017  61075       }
 61018  61076       if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
................................................................................
 61022  61080     }else if( op==TK_UMINUS ) {
 61023  61081       /* This branch happens for multiple negative signs.  Ex: -(-5) */
 61024  61082       if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) 
 61025  61083        && pVal!=0
 61026  61084       ){
 61027  61085         sqlite3VdbeMemNumerify(pVal);
 61028  61086         if( pVal->u.i==SMALLEST_INT64 ){
 61029         -        pVal->flags &= MEM_Int;
        61087  +        pVal->flags &= ~MEM_Int;
 61030  61088           pVal->flags |= MEM_Real;
 61031         -        pVal->r = (double)LARGEST_INT64;
        61089  +        pVal->r = (double)SMALLEST_INT64;
 61032  61090         }else{
 61033  61091           pVal->u.i = -pVal->u.i;
 61034  61092         }
 61035  61093         pVal->r = -pVal->r;
 61036  61094         sqlite3ValueApplyAffinity(pVal, affinity, enc);
 61037  61095       }
 61038  61096     }else if( op==TK_NULL ){
................................................................................
 61050  61108       nVal = sqlite3Strlen30(zVal)-1;
 61051  61109       assert( zVal[nVal]=='\'' );
 61052  61110       sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
 61053  61111                            0, SQLITE_DYNAMIC);
 61054  61112     }
 61055  61113   #endif
 61056  61114   
 61057         -  if( pVal ){
 61058         -    sqlite3VdbeMemStoreType(pVal);
 61059         -  }
 61060  61115     *ppVal = pVal;
 61061  61116     return rc;
 61062  61117   
 61063  61118   no_mem:
 61064  61119     db->mallocFailed = 1;
 61065  61120     sqlite3DbFree(db, zVal);
 61066  61121     assert( *ppVal==0 );
................................................................................
 61216  61271         pVal = valueNew(db, &alloc);
 61217  61272         if( pVal ){
 61218  61273           rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
 61219  61274           if( rc==SQLITE_OK ){
 61220  61275             sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
 61221  61276           }
 61222  61277           pVal->db = pParse->db;
 61223         -        sqlite3VdbeMemStoreType((Mem*)pVal);
 61224  61278         }
 61225  61279       }
 61226  61280     }else{
 61227  61281       rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
 61228  61282     }
 61229  61283     *pbOk = (pVal!=0);
 61230  61284   
................................................................................
 61461  61515       sqlite3VdbePrintOp(0, i, &p->aOp[i]);
 61462  61516       test_addop_breakpoint();
 61463  61517     }
 61464  61518   #endif
 61465  61519   #ifdef VDBE_PROFILE
 61466  61520     pOp->cycles = 0;
 61467  61521     pOp->cnt = 0;
        61522  +#endif
        61523  +#ifdef SQLITE_VDBE_COVERAGE
        61524  +  pOp->iSrcLine = 0;
 61468  61525   #endif
 61469  61526     return i;
 61470  61527   }
 61471  61528   SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
 61472  61529     return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
 61473  61530   }
 61474  61531   SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
................................................................................
 61823  61880     return aOp;
 61824  61881   }
 61825  61882   
 61826  61883   /*
 61827  61884   ** Add a whole list of operations to the operation stack.  Return the
 61828  61885   ** address of the first operation added.
 61829  61886   */
 61830         -SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
        61887  +SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
 61831  61888     int addr;
 61832  61889     assert( p->magic==VDBE_MAGIC_INIT );
 61833  61890     if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
 61834  61891       return 0;
 61835  61892     }
 61836  61893     addr = p->nOp;
 61837  61894     if( ALWAYS(nOp>0) ){
................................................................................
 61850  61907         }
 61851  61908         pOut->p3 = pIn->p3;
 61852  61909         pOut->p4type = P4_NOTUSED;
 61853  61910         pOut->p4.p = 0;
 61854  61911         pOut->p5 = 0;
 61855  61912   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 61856  61913         pOut->zComment = 0;
        61914  +#endif
        61915  +#ifdef SQLITE_VDBE_COVERAGE
        61916  +      pOut->iSrcLine = iLineno+i;
        61917  +#else
        61918  +      (void)iLineno;
 61857  61919   #endif
 61858  61920   #ifdef SQLITE_DEBUG
 61859  61921         if( p->db->flags & SQLITE_VdbeAddopTrace ){
 61860  61922           sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
 61861  61923         }
 61862  61924   #endif
 61863  61925       }
................................................................................
 62139  62201       va_start(ap, zFormat);
 62140  62202       vdbeVComment(p, zFormat, ap);
 62141  62203       va_end(ap);
 62142  62204     }
 62143  62205   }
 62144  62206   #endif  /* NDEBUG */
 62145  62207   
        62208  +#ifdef SQLITE_VDBE_COVERAGE
        62209  +/*
        62210  +** Set the value if the iSrcLine field for the previously coded instruction.
        62211  +*/
        62212  +SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
        62213  +  sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
        62214  +}
        62215  +#endif /* SQLITE_VDBE_COVERAGE */
        62216  +
 62146  62217   /*
 62147  62218   ** Return the opcode for a given address.  If the address is -1, then
 62148  62219   ** return the most recently inserted opcode.
 62149  62220   **
 62150  62221   ** If a memory allocation error has occurred prior to the calling of this
 62151  62222   ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
 62152  62223   ** is readable but not writable, though it is cast to a writable value.
 62153  62224   ** The return of a dummy opcode allows the call to continue functioning
 62154  62225   ** after a OOM fault without having to check to see if the return from 
 62155  62226   ** this routine is a valid pointer.  But because the dummy.opcode is 0,
 62156  62227   ** dummy will never be written to.  This is verified by code inspection and
 62157  62228   ** by running with Valgrind.
 62158         -**
 62159         -** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
 62160         -** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
 62161         -** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
 62162         -** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
 62163         -** having to double-check to make sure that the result is non-negative. But
 62164         -** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
 62165         -** check the value of p->nOp-1 before continuing.
 62166  62229   */
 62167  62230   SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
 62168  62231     /* C89 specifies that the constant "dummy" will be initialized to all
 62169  62232     ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
 62170  62233     static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
 62171  62234     assert( p->magic==VDBE_MAGIC_INIT );
 62172  62235     if( addr<0 ){
 62173         -#ifdef SQLITE_OMIT_TRACE
 62174         -    if( p->nOp==0 ) return (VdbeOp*)&dummy;
 62175         -#endif
 62176  62236       addr = p->nOp - 1;
 62177  62237     }
 62178  62238     assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
 62179  62239     if( p->db->mallocFailed ){
 62180  62240       return (VdbeOp*)&dummy;
 62181  62241     }else{
 62182  62242       return &p->aOp[addr];
................................................................................
 62473  62533     char zCom[100];
 62474  62534     static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
 62475  62535     if( pOut==0 ) pOut = stdout;
 62476  62536     zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
 62477  62537   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 62478  62538     displayComment(pOp, zP4, zCom, sizeof(zCom));
 62479  62539   #else
 62480         -  zCom[0] = 0
        62540  +  zCom[0] = 0;
 62481  62541   #endif
 62482  62542     /* NB:  The sqlite3OpcodeName() function is implemented by code created
 62483  62543     ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
 62484  62544     ** information from the vdbe.c source text */
 62485  62545     fprintf(pOut, zFormat1, pc, 
 62486  62546         sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
 62487  62547         zCom
................................................................................
 62502  62562         for(pEnd=&p[N]; p<pEnd; p++){
 62503  62563           sqlite3DbFree(db, p->zMalloc);
 62504  62564         }
 62505  62565         return;
 62506  62566       }
 62507  62567       for(pEnd=&p[N]; p<pEnd; p++){
 62508  62568         assert( (&p[1])==pEnd || p[0].db==p[1].db );
        62569  +      assert( sqlite3VdbeCheckMemInvariants(p) );
 62509  62570   
 62510  62571         /* This block is really an inlined version of sqlite3VdbeMemRelease()
 62511  62572         ** that takes advantage of the fact that the memory cell value is 
 62512  62573         ** being set to NULL after releasing any dynamic resources.
 62513  62574         **
 62514  62575         ** The justification for duplicating code is that according to 
 62515  62576         ** callgrind, this causes a certain test case to hit the CPU 4.7 
................................................................................
 62522  62583         if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
 62523  62584           sqlite3VdbeMemRelease(p);
 62524  62585         }else if( p->zMalloc ){
 62525  62586           sqlite3DbFree(db, p->zMalloc);
 62526  62587           p->zMalloc = 0;
 62527  62588         }
 62528  62589   
 62529         -      p->flags = MEM_Invalid;
        62590  +      p->flags = MEM_Undefined;
 62530  62591       }
 62531  62592       db->mallocFailed = malloc_failed;
 62532  62593     }
 62533  62594   }
 62534  62595   
 62535  62596   /*
 62536  62597   ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
................................................................................
 62644  62705         for(j=0; i>=apSub[j]->nOp; j++){
 62645  62706           i -= apSub[j]->nOp;
 62646  62707         }
 62647  62708         pOp = &apSub[j]->aOp[i];
 62648  62709       }
 62649  62710       if( p->explain==1 ){
 62650  62711         pMem->flags = MEM_Int;
 62651         -      pMem->type = SQLITE_INTEGER;
 62652  62712         pMem->u.i = i;                                /* Program counter */
 62653  62713         pMem++;
 62654  62714     
 62655  62715         pMem->flags = MEM_Static|MEM_Str|MEM_Term;
 62656  62716         pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
 62657  62717         assert( pMem->z!=0 );
 62658  62718         pMem->n = sqlite3Strlen30(pMem->z);
 62659         -      pMem->type = SQLITE_TEXT;
 62660  62719         pMem->enc = SQLITE_UTF8;
 62661  62720         pMem++;
 62662  62721   
 62663  62722         /* When an OP_Program opcode is encounter (the only opcode that has
 62664  62723         ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
 62665  62724         ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
 62666  62725         ** has not already been seen.
................................................................................
 62678  62737             pSub->n = nSub*sizeof(SubProgram*);
 62679  62738           }
 62680  62739         }
 62681  62740       }
 62682  62741   
 62683  62742       pMem->flags = MEM_Int;
 62684  62743       pMem->u.i = pOp->p1;                          /* P1 */
 62685         -    pMem->type = SQLITE_INTEGER;
 62686  62744       pMem++;
 62687  62745   
 62688  62746       pMem->flags = MEM_Int;
 62689  62747       pMem->u.i = pOp->p2;                          /* P2 */
 62690         -    pMem->type = SQLITE_INTEGER;
 62691  62748       pMem++;
 62692  62749   
 62693  62750       pMem->flags = MEM_Int;
 62694  62751       pMem->u.i = pOp->p3;                          /* P3 */
 62695         -    pMem->type = SQLITE_INTEGER;
 62696  62752       pMem++;
 62697  62753   
 62698  62754       if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
 62699  62755         assert( p->db->mallocFailed );
 62700  62756         return SQLITE_ERROR;
 62701  62757       }
 62702         -    pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
        62758  +    pMem->flags = MEM_Str|MEM_Term;
 62703  62759       zP4 = displayP4(pOp, pMem->z, 32);
 62704  62760       if( zP4!=pMem->z ){
 62705  62761         sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
 62706  62762       }else{
 62707  62763         assert( pMem->z!=0 );
 62708  62764         pMem->n = sqlite3Strlen30(pMem->z);
 62709  62765         pMem->enc = SQLITE_UTF8;
 62710  62766       }
 62711         -    pMem->type = SQLITE_TEXT;
 62712  62767       pMem++;
 62713  62768   
 62714  62769       if( p->explain==1 ){
 62715  62770         if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
 62716  62771           assert( p->db->mallocFailed );
 62717  62772           return SQLITE_ERROR;
 62718  62773         }
 62719         -      pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
        62774  +      pMem->flags = MEM_Str|MEM_Term;
 62720  62775         pMem->n = 2;
 62721  62776         sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
 62722         -      pMem->type = SQLITE_TEXT;
 62723  62777         pMem->enc = SQLITE_UTF8;
 62724  62778         pMem++;
 62725  62779     
 62726  62780   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 62727  62781         if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
 62728  62782           assert( p->db->mallocFailed );
 62729  62783           return SQLITE_ERROR;
 62730  62784         }
 62731         -      pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
        62785  +      pMem->flags = MEM_Str|MEM_Term;
 62732  62786         pMem->n = displayComment(pOp, zP4, pMem->z, 500);
 62733         -      pMem->type = SQLITE_TEXT;
 62734  62787         pMem->enc = SQLITE_UTF8;
 62735  62788   #else
 62736  62789         pMem->flags = MEM_Null;                       /* Comment */
 62737         -      pMem->type = SQLITE_NULL;
 62738  62790   #endif
 62739  62791       }
 62740  62792   
 62741  62793       p->nResColumn = 8 - 4*(p->explain-1);
 62742  62794       p->pResultSet = &p->aMem[1];
 62743  62795       p->rc = SQLITE_OK;
 62744  62796       rc = SQLITE_ROW;
................................................................................
 62753  62805   */
 62754  62806   SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
 62755  62807     const char *z = 0;
 62756  62808     if( p->zSql ){
 62757  62809       z = p->zSql;
 62758  62810     }else if( p->nOp>=1 ){
 62759  62811       const VdbeOp *pOp = &p->aOp[0];
 62760         -    if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
        62812  +    if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
 62761  62813         z = pOp->p4.z;
 62762  62814         while( sqlite3Isspace(*z) ) z++;
 62763  62815       }
 62764  62816     }
 62765  62817     if( z ) printf("SQL: [%s]\n", z);
 62766  62818   }
 62767  62819   #endif
................................................................................
 62772  62824   */
 62773  62825   SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
 62774  62826     int nOp = p->nOp;
 62775  62827     VdbeOp *pOp;
 62776  62828     if( sqlite3IoTrace==0 ) return;
 62777  62829     if( nOp<1 ) return;
 62778  62830     pOp = &p->aOp[0];
 62779         -  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
        62831  +  if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
 62780  62832       int i, j;
 62781  62833       char z[1000];
 62782  62834       sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
 62783  62835       for(i=0; sqlite3Isspace(z[i]); i++){}
 62784  62836       for(j=0; z[i]; i++){
 62785  62837         if( sqlite3Isspace(z[i]) ){
 62786  62838           if( z[i-1]!=' ' ){
................................................................................
 62990  63042       memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
 62991  63043       memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
 62992  63044     }
 62993  63045     if( p->aMem ){
 62994  63046       p->aMem--;                      /* aMem[] goes from 1..nMem */
 62995  63047       p->nMem = nMem;                 /*       not from 0..nMem-1 */
 62996  63048       for(n=1; n<=nMem; n++){
 62997         -      p->aMem[n].flags = MEM_Invalid;
        63049  +      p->aMem[n].flags = MEM_Undefined;
 62998  63050         p->aMem[n].db = db;
 62999  63051       }
 63000  63052     }
 63001  63053     p->explain = pParse->explain;
 63002  63054     sqlite3VdbeRewind(p);
 63003  63055   }
 63004  63056   
................................................................................
 63102  63154   
 63103  63155   #ifdef SQLITE_DEBUG
 63104  63156     /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
 63105  63157     ** Vdbe.aMem[] arrays have already been cleaned up.  */
 63106  63158     int i;
 63107  63159     if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
 63108  63160     if( p->aMem ){
 63109         -    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
        63161  +    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
 63110  63162     }
 63111  63163   #endif
 63112  63164   
 63113  63165     sqlite3DbFree(db, p->zErrMsg);
 63114  63166     p->zErrMsg = 0;
 63115  63167     p->pResultSet = 0;
 63116  63168   }
................................................................................
 63851  63903       if( out ){
 63852  63904         int i;
 63853  63905         fprintf(out, "---- ");
 63854  63906         for(i=0; i<p->nOp; i++){
 63855  63907           fprintf(out, "%02x", p->aOp[i].opcode);
 63856  63908         }
 63857  63909         fprintf(out, "\n");
        63910  +      if( p->zSql ){
        63911  +        char c, pc = 0;
        63912  +        fprintf(out, "-- ");
        63913  +        for(i=0; (c = p->zSql[i])!=0; i++){
        63914  +          if( pc=='\n' ) fprintf(out, "-- ");
        63915  +          putc(c, out);
        63916  +          pc = c;
        63917  +        }
        63918  +        if( pc!='\n' ) fprintf(out, "\n");
        63919  +      }
 63858  63920         for(i=0; i<p->nOp; i++){
 63859         -        fprintf(out, "%6d %10lld %8lld ",
        63921  +        char zHdr[100];
        63922  +        sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
 63860  63923              p->aOp[i].cnt,
 63861  63924              p->aOp[i].cycles,
 63862  63925              p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
 63863  63926           );
        63927  +        fprintf(out, "%s", zHdr);
 63864  63928           sqlite3VdbePrintOp(out, i, &p->aOp[i]);
 63865  63929         }
 63866  63930         fclose(out);
 63867  63931       }
 63868  63932     }
 63869  63933   #endif
 63870  63934     p->iCurrentTime = 0;
................................................................................
 64211  64275       return len;
 64212  64276     }
 64213  64277   
 64214  64278     /* NULL or constants 0 or 1 */
 64215  64279     return 0;
 64216  64280   }
 64217  64281   
        64282  +/* Input "x" is a sequence of unsigned characters that represent a
        64283  +** big-endian integer.  Return the equivalent native integer
        64284  +*/
        64285  +#define ONE_BYTE_INT(x)    ((i8)(x)[0])
        64286  +#define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
        64287  +#define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
        64288  +#define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
        64289  +
 64218  64290   /*
 64219  64291   ** Deserialize the data blob pointed to by buf as serial type serial_type
 64220  64292   ** and store the result in pMem.  Return the number of bytes read.
 64221  64293   */ 
 64222  64294   SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
 64223  64295     const unsigned char *buf,     /* Buffer to deserialize from */
 64224  64296     u32 serial_type,              /* Serial type to deserialize */
 64225  64297     Mem *pMem                     /* Memory cell to write value into */
 64226  64298   ){
 64227  64299     u64 x;
 64228  64300     u32 y;
 64229         -  int i;
 64230  64301     switch( serial_type ){
 64231  64302       case 10:   /* Reserved for future use */
 64232  64303       case 11:   /* Reserved for future use */
 64233  64304       case 0: {  /* NULL */
 64234  64305         pMem->flags = MEM_Null;
 64235  64306         break;
 64236  64307       }
 64237  64308       case 1: { /* 1-byte signed integer */
 64238         -      pMem->u.i = (signed char)buf[0];
        64309  +      pMem->u.i = ONE_BYTE_INT(buf);
 64239  64310         pMem->flags = MEM_Int;
 64240  64311         return 1;
 64241  64312       }
 64242  64313       case 2: { /* 2-byte signed integer */
 64243         -      i = 256*(signed char)buf[0] | buf[1];
 64244         -      pMem->u.i = (i64)i;
        64314  +      pMem->u.i = TWO_BYTE_INT(buf);
 64245  64315         pMem->flags = MEM_Int;
 64246  64316         return 2;
 64247  64317       }
 64248  64318       case 3: { /* 3-byte signed integer */
 64249         -      i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2];
 64250         -      pMem->u.i = (i64)i;
        64319  +      pMem->u.i = THREE_BYTE_INT(buf);
 64251  64320         pMem->flags = MEM_Int;
 64252  64321         return 3;
 64253  64322       }
 64254  64323       case 4: { /* 4-byte signed integer */
 64255         -      y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
        64324  +      y = FOUR_BYTE_UINT(buf);
 64256  64325         pMem->u.i = (i64)*(int*)&y;
 64257  64326         pMem->flags = MEM_Int;
 64258  64327         return 4;
 64259  64328       }
 64260  64329       case 5: { /* 6-byte signed integer */
 64261         -      x = 256*(signed char)buf[0] + buf[1];
 64262         -      y = ((unsigned)buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
 64263         -      x = (x<<32) | y;
 64264         -      pMem->u.i = *(i64*)&x;
        64330  +      pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
 64265  64331         pMem->flags = MEM_Int;
 64266  64332         return 6;
 64267  64333       }
 64268  64334       case 6:   /* 8-byte signed integer */
 64269  64335       case 7: { /* IEEE floating point */
 64270  64336   #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
 64271  64337         /* Verify that integers and floating point values use the same
................................................................................
 64275  64341         */
 64276  64342         static const u64 t1 = ((u64)0x3ff00000)<<32;
 64277  64343         static const double r1 = 1.0;
 64278  64344         u64 t2 = t1;
 64279  64345         swapMixedEndianFloat(t2);
 64280  64346         assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
 64281  64347   #endif
 64282         -      x = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
 64283         -      y = ((unsigned)buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
        64348  +      x = FOUR_BYTE_UINT(buf);
        64349  +      y = FOUR_BYTE_UINT(buf+4);
 64284  64350         x = (x<<32) | y;
 64285  64351         if( serial_type==6 ){
 64286  64352           pMem->u.i = *(i64*)&x;
 64287  64353           pMem->flags = MEM_Int;
 64288  64354         }else{
 64289  64355           assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
 64290  64356           swapMixedEndianFloat(x);
................................................................................
 64372  64438     const unsigned char *aKey = (const unsigned char *)pKey;
 64373  64439     int d; 
 64374  64440     u32 idx;                        /* Offset in aKey[] to read from */
 64375  64441     u16 u;                          /* Unsigned loop counter */
 64376  64442     u32 szHdr;
 64377  64443     Mem *pMem = p->aMem;
 64378  64444   
 64379         -  p->flags = 0;
        64445  +  p->default_rc = 0;
 64380  64446     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 64381  64447     idx = getVarint32(aKey, szHdr);
 64382  64448     d = szHdr;
 64383  64449     u = 0;
 64384  64450     while( idx<szHdr && u<p->nField && d<=nKey ){
 64385  64451       u32 serial_type;
 64386  64452   
................................................................................
 64393  64459       pMem++;
 64394  64460       u++;
 64395  64461     }
 64396  64462     assert( u<=pKeyInfo->nField + 1 );
 64397  64463     p->nField = u;
 64398  64464   }
 64399  64465   
        64466  +#if SQLITE_DEBUG
 64400  64467   /*
 64401         -** This function compares the two table rows or index records
 64402         -** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
 64403         -** or positive integer if key1 is less than, equal to or 
 64404         -** greater than key2.  The {nKey1, pKey1} key must be a blob
 64405         -** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
 64406         -** key must be a parsed key such as obtained from
 64407         -** sqlite3VdbeParseRecord.
 64408         -**
 64409         -** Key1 and Key2 do not have to contain the same number of fields.
 64410         -** The key with fewer fields is usually compares less than the 
 64411         -** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
 64412         -** and the common prefixes are equal, then key1 is less than key2.
 64413         -** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
 64414         -** equal, then the keys are considered to be equal and
 64415         -** the parts beyond the common prefix are ignored.
        64468  +** This function compares two index or table record keys in the same way
        64469  +** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
        64470  +** this function deserializes and compares values using the
        64471  +** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
        64472  +** in assert() statements to ensure that the optimized code in
        64473  +** sqlite3VdbeRecordCompare() returns results with these two primitives.
 64416  64474   */
 64417         -SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
        64475  +static int vdbeRecordCompareDebug(
 64418  64476     int nKey1, const void *pKey1, /* Left key */
 64419         -  UnpackedRecord *pPKey2        /* Right key */
        64477  +  const UnpackedRecord *pPKey2  /* Right key */
 64420  64478   ){
 64421  64479     u32 d1;            /* Offset into aKey[] of next data element */
 64422  64480     u32 idx1;          /* Offset into aKey[] of next header element */
 64423  64481     u32 szHdr1;        /* Number of bytes in header */
 64424  64482     int i = 0;
 64425  64483     int rc = 0;
 64426  64484     const unsigned char *aKey1 = (const unsigned char *)pKey1;
................................................................................
 64486  64544     /* No memory allocation is ever used on mem1.  Prove this using
 64487  64545     ** the following assert().  If the assert() fails, it indicates a
 64488  64546     ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
 64489  64547     */
 64490  64548     assert( mem1.zMalloc==0 );
 64491  64549   
 64492  64550     /* rc==0 here means that one of the keys ran out of fields and
 64493         -  ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
 64494         -  ** flag is set, then break the tie by treating key2 as larger.
 64495         -  ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
 64496         -  ** are considered to be equal.  Otherwise, the longer key is the 
 64497         -  ** larger.  As it happens, the pPKey2 will always be the longer
 64498         -  ** if there is a difference.
 64499         -  */
 64500         -  assert( rc==0 );
 64501         -  if( pPKey2->flags & UNPACKED_INCRKEY ){
 64502         -    rc = -1;
 64503         -  }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
 64504         -    /* Leave rc==0 */
 64505         -  }else if( idx1<szHdr1 ){
 64506         -    rc = 1;
 64507         -  }
 64508         -  return rc;
 64509         -}
 64510         - 
        64551  +  ** all the fields up to that point were equal. Return the the default_rc
        64552  +  ** value.  */
        64553  +  return pPKey2->default_rc;
        64554  +}
        64555  +#endif
        64556  +
        64557  +/*
        64558  +** Both *pMem1 and *pMem2 contain string values. Compare the two values
        64559  +** using the collation sequence pColl. As usual, return a negative , zero
        64560  +** or positive value if *pMem1 is less than, equal to or greater than 
        64561  +** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
        64562  +*/
        64563  +static int vdbeCompareMemString(
        64564  +  const Mem *pMem1,
        64565  +  const Mem *pMem2,
        64566  +  const CollSeq *pColl
        64567  +){
        64568  +  if( pMem1->enc==pColl->enc ){
        64569  +    /* The strings are already in the correct encoding.  Call the
        64570  +     ** comparison function directly */
        64571  +    return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
        64572  +  }else{
        64573  +    int rc;
        64574  +    const void *v1, *v2;
        64575  +    int n1, n2;
        64576  +    Mem c1;
        64577  +    Mem c2;
        64578  +    memset(&c1, 0, sizeof(c1));
        64579  +    memset(&c2, 0, sizeof(c2));
        64580  +    sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
        64581  +    sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
        64582  +    v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
        64583  +    n1 = v1==0 ? 0 : c1.n;
        64584  +    v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
        64585  +    n2 = v2==0 ? 0 : c2.n;
        64586  +    rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
        64587  +    sqlite3VdbeMemRelease(&c1);
        64588  +    sqlite3VdbeMemRelease(&c2);
        64589  +    return rc;
        64590  +  }
        64591  +}
        64592  +
        64593  +/*
        64594  +** Compare the values contained by the two memory cells, returning
        64595  +** negative, zero or positive if pMem1 is less than, equal to, or greater
        64596  +** than pMem2. Sorting order is NULL's first, followed by numbers (integers
        64597  +** and reals) sorted numerically, followed by text ordered by the collating
        64598  +** sequence pColl and finally blob's ordered by memcmp().
        64599  +**
        64600  +** Two NULL values are considered equal by this function.
        64601  +*/
        64602  +SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
        64603  +  int rc;
        64604  +  int f1, f2;
        64605  +  int combined_flags;
        64606  +
        64607  +  f1 = pMem1->flags;
        64608  +  f2 = pMem2->flags;
        64609  +  combined_flags = f1|f2;
        64610  +  assert( (combined_flags & MEM_RowSet)==0 );
        64611  + 
        64612  +  /* If one value is NULL, it is less than the other. If both values
        64613  +  ** are NULL, return 0.
        64614  +  */
        64615  +  if( combined_flags&MEM_Null ){
        64616  +    return (f2&MEM_Null) - (f1&MEM_Null);
        64617  +  }
        64618  +
        64619  +  /* If one value is a number and the other is not, the number is less.
        64620  +  ** If both are numbers, compare as reals if one is a real, or as integers
        64621  +  ** if both values are integers.
        64622  +  */
        64623  +  if( combined_flags&(MEM_Int|MEM_Real) ){
        64624  +    double r1, r2;
        64625  +    if( (f1 & f2 & MEM_Int)!=0 ){
        64626  +      if( pMem1->u.i < pMem2->u.i ) return -1;
        64627  +      if( pMem1->u.i > pMem2->u.i ) return 1;
        64628  +      return 0;
        64629  +    }
        64630  +    if( (f1&MEM_Real)!=0 ){
        64631  +      r1 = pMem1->r;
        64632  +    }else if( (f1&MEM_Int)!=0 ){
        64633  +      r1 = (double)pMem1->u.i;
        64634  +    }else{
        64635  +      return 1;
        64636  +    }
        64637  +    if( (f2&MEM_Real)!=0 ){
        64638  +      r2 = pMem2->r;
        64639  +    }else if( (f2&MEM_Int)!=0 ){
        64640  +      r2 = (double)pMem2->u.i;
        64641  +    }else{
        64642  +      return -1;
        64643  +    }
        64644  +    if( r1<r2 ) return -1;
        64645  +    if( r1>r2 ) return 1;
        64646  +    return 0;
        64647  +  }
        64648  +
        64649  +  /* If one value is a string and the other is a blob, the string is less.
        64650  +  ** If both are strings, compare using the collating functions.
        64651  +  */
        64652  +  if( combined_flags&MEM_Str ){
        64653  +    if( (f1 & MEM_Str)==0 ){
        64654  +      return 1;
        64655  +    }
        64656  +    if( (f2 & MEM_Str)==0 ){
        64657  +      return -1;
        64658  +    }
        64659  +
        64660  +    assert( pMem1->enc==pMem2->enc );
        64661  +    assert( pMem1->enc==SQLITE_UTF8 || 
        64662  +            pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
        64663  +
        64664  +    /* The collation sequence must be defined at this point, even if
        64665  +    ** the user deletes the collation sequence after the vdbe program is
        64666  +    ** compiled (this was not always the case).
        64667  +    */
        64668  +    assert( !pColl || pColl->xCmp );
        64669  +
        64670  +    if( pColl ){
        64671  +      return vdbeCompareMemString(pMem1, pMem2, pColl);
        64672  +    }
        64673  +    /* If a NULL pointer was passed as the collate function, fall through
        64674  +    ** to the blob case and use memcmp().  */
        64675  +  }
        64676  + 
        64677  +  /* Both values must be blobs.  Compare using memcmp().  */
        64678  +  rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
        64679  +  if( rc==0 ){
        64680  +    rc = pMem1->n - pMem2->n;
        64681  +  }
        64682  +  return rc;
        64683  +}
        64684  +
        64685  +
        64686  +/*
        64687  +** The first argument passed to this function is a serial-type that
        64688  +** corresponds to an integer - all values between 1 and 9 inclusive 
        64689  +** except 7. The second points to a buffer containing an integer value
        64690  +** serialized according to serial_type. This function deserializes
        64691  +** and returns the value.
        64692  +*/
        64693  +static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
        64694  +  u32 y;
        64695  +  assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
        64696  +  switch( serial_type ){
        64697  +    case 0:
        64698  +    case 1:
        64699  +      return ONE_BYTE_INT(aKey);
        64700  +    case 2:
        64701  +      return TWO_BYTE_INT(aKey);
        64702  +    case 3:
        64703  +      return THREE_BYTE_INT(aKey);
        64704  +    case 4: {
        64705  +      y = FOUR_BYTE_UINT(aKey);
        64706  +      return (i64)*(int*)&y;
        64707  +    }
        64708  +    case 5: {
        64709  +      return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
        64710  +    }
        64711  +    case 6: {
        64712  +      u64 x = FOUR_BYTE_UINT(aKey);
        64713  +      x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
        64714  +      return (i64)*(i64*)&x;
        64715  +    }
        64716  +  }
        64717  +
        64718  +  return (serial_type - 8);
        64719  +}
        64720  +
        64721  +/*
        64722  +** This function compares the two table rows or index records
        64723  +** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
        64724  +** or positive integer if key1 is less than, equal to or 
        64725  +** greater than key2.  The {nKey1, pKey1} key must be a blob
        64726  +** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
        64727  +** key must be a parsed key such as obtained from
        64728  +** sqlite3VdbeParseRecord.
        64729  +**
        64730  +** If argument bSkip is non-zero, it is assumed that the caller has already
        64731  +** determined that the first fields of the keys are equal.
        64732  +**
        64733  +** Key1 and Key2 do not have to contain the same number of fields. If all 
        64734  +** fields that appear in both keys are equal, then pPKey2->default_rc is 
        64735  +** returned.
        64736  +*/
        64737  +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
        64738  +  int nKey1, const void *pKey1,   /* Left key */
        64739  +  const UnpackedRecord *pPKey2,   /* Right key */
        64740  +  int bSkip                       /* If true, skip the first field */
        64741  +){
        64742  +  u32 d1;                         /* Offset into aKey[] of next data element */
        64743  +  int i;                          /* Index of next field to compare */
        64744  +  u32 szHdr1;                     /* Size of record header in bytes */
        64745  +  u32 idx1;                       /* Offset of first type in header */
        64746  +  int rc = 0;                     /* Return value */
        64747  +  Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
        64748  +  KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
        64749  +  const unsigned char *aKey1 = (const unsigned char *)pKey1;
        64750  +  Mem mem1;
        64751  +
        64752  +  /* If bSkip is true, then the caller has already determined that the first
        64753  +  ** two elements in the keys are equal. Fix the various stack variables so
        64754  +  ** that this routine begins comparing at the second field. */
        64755  +  if( bSkip ){
        64756  +    u32 s1;
        64757  +    idx1 = 1 + getVarint32(&aKey1[1], s1);
        64758  +    szHdr1 = aKey1[0];
        64759  +    d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
        64760  +    i = 1;
        64761  +    pRhs++;
        64762  +  }else{
        64763  +    idx1 = getVarint32(aKey1, szHdr1);
        64764  +    d1 = szHdr1;
        64765  +    i = 0;
        64766  +  }
        64767  +
        64768  +  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
        64769  +  assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
        64770  +       || CORRUPT_DB );
        64771  +  assert( pPKey2->pKeyInfo->aSortOrder!=0 );
        64772  +  assert( pPKey2->pKeyInfo->nField>0 );
        64773  +  assert( idx1<=szHdr1 || CORRUPT_DB );
        64774  +  do{
        64775  +    u32 serial_type;
        64776  +
        64777  +    /* RHS is an integer */
        64778  +    if( pRhs->flags & MEM_Int ){
        64779  +      serial_type = aKey1[idx1];
        64780  +      if( serial_type>=12 ){
        64781  +        rc = +1;
        64782  +      }else if( serial_type==0 ){
        64783  +        rc = -1;
        64784  +      }else if( serial_type==7 ){
        64785  +        double rhs = (double)pRhs->u.i;
        64786  +        sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
        64787  +        if( mem1.r<rhs ){
        64788  +          rc = -1;
        64789  +        }else if( mem1.r>rhs ){
        64790  +          rc = +1;
        64791  +        }
        64792  +      }else{
        64793  +        i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
        64794  +        i64 rhs = pRhs->u.i;
        64795  +        if( lhs<rhs ){
        64796  +          rc = -1;
        64797  +        }else if( lhs>rhs ){
        64798  +          rc = +1;
        64799  +        }
        64800  +      }
        64801  +    }
        64802  +
        64803  +    /* RHS is real */
        64804  +    else if( pRhs->flags & MEM_Real ){
        64805  +      serial_type = aKey1[idx1];
        64806  +      if( serial_type>=12 ){
        64807  +        rc = +1;
        64808  +      }else if( serial_type==0 ){
        64809  +        rc = -1;
        64810  +      }else{
        64811  +        double rhs = pRhs->r;
        64812  +        double lhs;
        64813  +        sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
        64814  +        if( serial_type==7 ){
        64815  +          lhs = mem1.r;
        64816  +        }else{
        64817  +          lhs = (double)mem1.u.i;
        64818  +        }
        64819  +        if( lhs<rhs ){
        64820  +          rc = -1;
        64821  +        }else if( lhs>rhs ){
        64822  +          rc = +1;
        64823  +        }
        64824  +      }
        64825  +    }
        64826  +
        64827  +    /* RHS is a string */
        64828  +    else if( pRhs->flags & MEM_Str ){
        64829  +      getVarint32(&aKey1[idx1], serial_type);
        64830  +      if( serial_type<12 ){
        64831  +        rc = -1;
        64832  +      }else if( !(serial_type & 0x01) ){
        64833  +        rc = +1;
        64834  +      }else{
        64835  +        mem1.n = (serial_type - 12) / 2;
        64836  +        if( (d1+mem1.n) > (unsigned)nKey1 ){
        64837  +          rc = 1;                /* Corruption */
        64838  +        }else if( pKeyInfo->aColl[i] ){
        64839  +          mem1.enc = pKeyInfo->enc;
        64840  +          mem1.db = pKeyInfo->db;
        64841  +          mem1.flags = MEM_Str;
        64842  +          mem1.z = (char*)&aKey1[d1];
        64843  +          rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
        64844  +        }else{
        64845  +          int nCmp = MIN(mem1.n, pRhs->n);
        64846  +          rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
        64847  +          if( rc==0 ) rc = mem1.n - pRhs->n; 
        64848  +        }
        64849  +      }
        64850  +    }
        64851  +
        64852  +    /* RHS is a blob */
        64853  +    else if( pRhs->flags & MEM_Blob ){
        64854  +      getVarint32(&aKey1[idx1], serial_type);
        64855  +      if( serial_type<12 || (serial_type & 0x01) ){
        64856  +        rc = -1;
        64857  +      }else{
        64858  +        int nStr = (serial_type - 12) / 2;
        64859  +        if( (d1+nStr) > (unsigned)nKey1 ){
        64860  +          rc = 1;                /* Corruption */
        64861  +        }else{
        64862  +          int nCmp = MIN(nStr, pRhs->n);
        64863  +          rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
        64864  +          if( rc==0 ) rc = nStr - pRhs->n;
        64865  +        }
        64866  +      }
        64867  +    }
        64868  +
        64869  +    /* RHS is null */
        64870  +    else{
        64871  +      serial_type = aKey1[idx1];
        64872  +      rc = (serial_type!=0);
        64873  +    }
        64874  +
        64875  +    if( rc!=0 ){
        64876  +      if( pKeyInfo->aSortOrder[i] ){
        64877  +        rc = -rc;
        64878  +      }
        64879  +      assert( CORRUPT_DB
        64880  +          || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
        64881  +          || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
        64882  +          || pKeyInfo->db->mallocFailed
        64883  +      );
        64884  +      assert( mem1.zMalloc==0 );  /* See comment below */
        64885  +      return rc;
        64886  +    }
        64887  +
        64888  +    i++;
        64889  +    pRhs++;
        64890  +    d1 += sqlite3VdbeSerialTypeLen(serial_type);
        64891  +    idx1 += sqlite3VarintLen(serial_type);
        64892  +  }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
        64893  +
        64894  +  /* No memory allocation is ever used on mem1.  Prove this using
        64895  +  ** the following assert().  If the assert() fails, it indicates a
        64896  +  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
        64897  +  assert( mem1.zMalloc==0 );
        64898  +
        64899  +  /* rc==0 here means that one or both of the keys ran out of fields and
        64900  +  ** all the fields up to that point were equal. Return the the default_rc
        64901  +  ** value.  */
        64902  +  assert( CORRUPT_DB 
        64903  +       || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2) 
        64904  +  );
        64905  +  return pPKey2->default_rc;
        64906  +}
        64907  +
        64908  +/*
        64909  +** This function is an optimized version of sqlite3VdbeRecordCompare() 
        64910  +** that (a) the first field of pPKey2 is an integer, and (b) the 
        64911  +** size-of-header varint at the start of (pKey1/nKey1) fits in a single
        64912  +** byte (i.e. is less than 128).
        64913  +*/
        64914  +static int vdbeRecordCompareInt(
        64915  +  int nKey1, const void *pKey1, /* Left key */
        64916  +  const UnpackedRecord *pPKey2, /* Right key */
        64917  +  int bSkip                     /* Ignored */
        64918  +){
        64919  +  const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
        64920  +  int serial_type = ((const u8*)pKey1)[1];
        64921  +  int res;
        64922  +  u32 y;
        64923  +  u64 x;
        64924  +  i64 v = pPKey2->aMem[0].u.i;
        64925  +  i64 lhs;
        64926  +  UNUSED_PARAMETER(bSkip);
        64927  +
        64928  +  assert( bSkip==0 );
        64929  +  switch( serial_type ){
        64930  +    case 1: { /* 1-byte signed integer */
        64931  +      lhs = ONE_BYTE_INT(aKey);
        64932  +      break;
        64933  +    }
        64934  +    case 2: { /* 2-byte signed integer */
        64935  +      lhs = TWO_BYTE_INT(aKey);
        64936  +      break;
        64937  +    }
        64938  +    case 3: { /* 3-byte signed integer */
        64939  +      lhs = THREE_BYTE_INT(aKey);
        64940  +      break;
        64941  +    }
        64942  +    case 4: { /* 4-byte signed integer */
        64943  +      y = FOUR_BYTE_UINT(aKey);
        64944  +      lhs = (i64)*(int*)&y;
        64945  +      break;
        64946  +    }
        64947  +    case 5: { /* 6-byte signed integer */
        64948  +      lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
        64949  +      break;
        64950  +    }
        64951  +    case 6: { /* 8-byte signed integer */
        64952  +      x = FOUR_BYTE_UINT(aKey);
        64953  +      x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
        64954  +      lhs = *(i64*)&x;
        64955  +      break;
        64956  +    }
        64957  +    case 8: 
        64958  +      lhs = 0;
        64959  +      break;
        64960  +    case 9:
        64961  +      lhs = 1;
        64962  +      break;
        64963  +
        64964  +    /* This case could be removed without changing the results of running
        64965  +    ** this code. Including it causes gcc to generate a faster switch 
        64966  +    ** statement (since the range of switch targets now starts at zero and
        64967  +    ** is contiguous) but does not cause any duplicate code to be generated
        64968  +    ** (as gcc is clever enough to combine the two like cases). Other 
        64969  +    ** compilers might be similar.  */ 
        64970  +    case 0: case 7:
        64971  +      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
        64972  +
        64973  +    default:
        64974  +      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
        64975  +  }
        64976  +
        64977  +  if( v>lhs ){
        64978  +    res = pPKey2->r1;
        64979  +  }else if( v<lhs ){
        64980  +    res = pPKey2->r2;
        64981  +  }else if( pPKey2->nField>1 ){
        64982  +    /* The first fields of the two keys are equal. Compare the trailing 
        64983  +    ** fields.  */
        64984  +    res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
        64985  +  }else{
        64986  +    /* The first fields of the two keys are equal and there are no trailing
        64987  +    ** fields. Return pPKey2->default_rc in this case. */
        64988  +    res = pPKey2->default_rc;
        64989  +  }
        64990  +
        64991  +  assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
        64992  +       || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
        64993  +       || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
        64994  +       || CORRUPT_DB
        64995  +  );
        64996  +  return res;
        64997  +}
        64998  +
        64999  +/*
        65000  +** This function is an optimized version of sqlite3VdbeRecordCompare() 
        65001  +** that (a) the first field of pPKey2 is a string, that (b) the first field
        65002  +** uses the collation sequence BINARY and (c) that the size-of-header varint 
        65003  +** at the start of (pKey1/nKey1) fits in a single byte.
        65004  +*/
        65005  +static int vdbeRecordCompareString(
        65006  +  int nKey1, const void *pKey1, /* Left key */
        65007  +  const UnpackedRecord *pPKey2, /* Right key */
        65008  +  int bSkip
        65009  +){
        65010  +  const u8 *aKey1 = (const u8*)pKey1;
        65011  +  int serial_type;
        65012  +  int res;
        65013  +  UNUSED_PARAMETER(bSkip);
        65014  +
        65015  +  assert( bSkip==0 );
        65016  +  getVarint32(&aKey1[1], serial_type);
        65017  +
        65018  +  if( serial_type<12 ){
        65019  +    res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
        65020  +  }else if( !(serial_type & 0x01) ){ 
        65021  +    res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
        65022  +  }else{
        65023  +    int nCmp;
        65024  +    int nStr;
        65025  +    int szHdr = aKey1[0];
        65026  +
        65027  +    nStr = (serial_type-12) / 2;
        65028  +    if( (szHdr + nStr) > nKey1 ) return 0;    /* Corruption */
        65029  +    nCmp = MIN( pPKey2->aMem[0].n, nStr );
        65030  +    res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
        65031  +
        65032  +    if( res==0 ){
        65033  +      res = nStr - pPKey2->aMem[0].n;
        65034  +      if( res==0 ){
        65035  +        if( pPKey2->nField>1 ){
        65036  +          res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
        65037  +        }else{
        65038  +          res = pPKey2->default_rc;
        65039  +        }
        65040  +      }else if( res>0 ){
        65041  +        res = pPKey2->r2;
        65042  +      }else{
        65043  +        res = pPKey2->r1;
        65044  +      }
        65045  +    }else if( res>0 ){
        65046  +      res = pPKey2->r2;
        65047  +    }else{
        65048  +      res = pPKey2->r1;
        65049  +    }
        65050  +  }
        65051  +
        65052  +  assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
        65053  +       || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
        65054  +       || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
        65055  +       || CORRUPT_DB
        65056  +  );
        65057  +  return res;
        65058  +}
        65059  +
        65060  +/*
        65061  +** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
        65062  +** suitable for comparing serialized records to the unpacked record passed
        65063  +** as the only argument.
        65064  +*/
        65065  +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
        65066  +  /* varintRecordCompareInt() and varintRecordCompareString() both assume
        65067  +  ** that the size-of-header varint that occurs at the start of each record
        65068  +  ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
        65069  +  ** also assumes that it is safe to overread a buffer by at least the 
        65070  +  ** maximum possible legal header size plus 8 bytes. Because there is
        65071  +  ** guaranteed to be at least 74 (but not 136) bytes of padding following each
        65072  +  ** buffer passed to varintRecordCompareInt() this makes it convenient to
        65073  +  ** limit the size of the header to 64 bytes in cases where the first field
        65074  +  ** is an integer.
        65075  +  **
        65076  +  ** The easiest way to enforce this limit is to consider only records with
        65077  +  ** 13 fields or less. If the first field is an integer, the maximum legal
        65078  +  ** header size is (12*5 + 1 + 1) bytes.  */
        65079  +  if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
        65080  +    int flags = p->aMem[0].flags;
        65081  +    if( p->pKeyInfo->aSortOrder[0] ){
        65082  +      p->r1 = 1;
        65083  +      p->r2 = -1;
        65084  +    }else{
        65085  +      p->r1 = -1;
        65086  +      p->r2 = 1;
        65087  +    }
        65088  +    if( (flags & MEM_Int) ){
        65089  +      return vdbeRecordCompareInt;
        65090  +    }
        65091  +    if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0 
        65092  +        && p->pKeyInfo->aColl[0]==0 
        65093  +    ){
        65094  +      return vdbeRecordCompareString;
        65095  +    }
        65096  +  }
        65097  +
        65098  +  return sqlite3VdbeRecordCompare;
        65099  +}
 64511  65100   
 64512  65101   /*
 64513  65102   ** pCur points at an index entry created using the OP_MakeRecord opcode.
 64514  65103   ** Read the rowid (the last field in the record) and store it in *rowid.
 64515  65104   ** Return SQLITE_OK if everything works, or an error code otherwise.
 64516  65105   **
 64517  65106   ** pCur might be pointing to text obtained from a corrupt database file.
................................................................................
 64594  65183   **
 64595  65184   ** pUnpacked is either created without a rowid or is truncated so that it
 64596  65185   ** omits the rowid at the end.  The rowid at the end of the index entry
 64597  65186   ** is ignored as well.  Hence, this routine only compares the prefixes 
 64598  65187   ** of the keys prior to the final rowid, not the entire key.
 64599  65188   */
 64600  65189   SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
 64601         -  VdbeCursor *pC,             /* The cursor to compare against */
 64602         -  UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
 64603         -  int *res                    /* Write the comparison result here */
        65190  +  VdbeCursor *pC,                  /* The cursor to compare against */
        65191  +  const UnpackedRecord *pUnpacked, /* Unpacked version of key */
        65192  +  int *res                         /* Write the comparison result here */
 64604  65193   ){
 64605  65194     i64 nCellKey = 0;
 64606  65195     int rc;
 64607  65196     BtCursor *pCur = pC->pCursor;
 64608  65197     Mem m;
 64609  65198   
 64610  65199     assert( sqlite3BtreeCursorIsValid(pCur) );
 64611  65200     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 64612  65201     assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
 64613         -  /* nCellKey will always be between 0 and 0xffffffff because of the say
        65202  +  /* nCellKey will always be between 0 and 0xffffffff because of the way
 64614  65203     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
 64615  65204     if( nCellKey<=0 || nCellKey>0x7fffffff ){
 64616  65205       *res = 0;
 64617  65206       return SQLITE_CORRUPT_BKPT;
 64618  65207     }
 64619  65208     memset(&m, 0, sizeof(m));
 64620  65209     rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
 64621  65210     if( rc ){
 64622  65211       return rc;
 64623  65212     }
 64624         -  assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
 64625         -  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
        65213  +  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
 64626  65214     sqlite3VdbeMemRelease(&m);
 64627  65215     return SQLITE_OK;
 64628  65216   }
 64629  65217   
 64630  65218   /*
 64631  65219   ** This routine sets the value to be returned by subsequent calls to
 64632  65220   ** sqlite3_changes() on the database handle 'db'. 
................................................................................
 64682  65270     if( v ){
 64683  65271       Mem *pMem = &v->aVar[iVar-1];
 64684  65272       if( 0==(pMem->flags & MEM_Null) ){
 64685  65273         sqlite3_value *pRet = sqlite3ValueNew(v->db);
 64686  65274         if( pRet ){
 64687  65275           sqlite3VdbeMemCopy((Mem *)pRet, pMem);
 64688  65276           sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
 64689         -        sqlite3VdbeMemStoreType((Mem *)pRet);
 64690  65277         }
 64691  65278         return pRet;
 64692  65279       }
 64693  65280     }
 64694  65281     return 0;
 64695  65282   }
 64696  65283   
................................................................................
 64856  65443   ** The following routines extract information from a Mem or sqlite3_value
 64857  65444   ** structure.
 64858  65445   */
 64859  65446   SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
 64860  65447     Mem *p = (Mem*)pVal;
 64861  65448     if( p->flags & (MEM_Blob|MEM_Str) ){
 64862  65449       sqlite3VdbeMemExpandBlob(p);
 64863         -    p->flags &= ~MEM_Str;
 64864  65450       p->flags |= MEM_Blob;
 64865  65451       return p->n ? p->z : 0;
 64866  65452     }else{
 64867  65453       return sqlite3_value_text(pVal);
 64868  65454     }
 64869  65455   }
 64870  65456   SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
................................................................................
 64893  65479     return sqlite3ValueText(pVal, SQLITE_UTF16BE);
 64894  65480   }
 64895  65481   SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
 64896  65482     return sqlite3ValueText(pVal, SQLITE_UTF16LE);
 64897  65483   }
 64898  65484   #endif /* SQLITE_OMIT_UTF16 */
 64899  65485   SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
 64900         -  return pVal->type;
        65486  +  static const u8 aType[] = {
        65487  +     SQLITE_BLOB,     /* 0x00 */
        65488  +     SQLITE_NULL,     /* 0x01 */
        65489  +     SQLITE_TEXT,     /* 0x02 */
        65490  +     SQLITE_NULL,     /* 0x03 */
        65491  +     SQLITE_INTEGER,  /* 0x04 */
        65492  +     SQLITE_NULL,     /* 0x05 */
        65493  +     SQLITE_INTEGER,  /* 0x06 */
        65494  +     SQLITE_NULL,     /* 0x07 */
        65495  +     SQLITE_FLOAT,    /* 0x08 */
        65496  +     SQLITE_NULL,     /* 0x09 */
        65497  +     SQLITE_FLOAT,    /* 0x0a */
        65498  +     SQLITE_NULL,     /* 0x0b */
        65499  +     SQLITE_INTEGER,  /* 0x0c */
        65500  +     SQLITE_NULL,     /* 0x0d */
        65501  +     SQLITE_INTEGER,  /* 0x0e */
        65502  +     SQLITE_NULL,     /* 0x0f */
        65503  +     SQLITE_BLOB,     /* 0x10 */
        65504  +     SQLITE_NULL,     /* 0x11 */
        65505  +     SQLITE_TEXT,     /* 0x12 */
        65506  +     SQLITE_NULL,     /* 0x13 */
        65507  +     SQLITE_INTEGER,  /* 0x14 */
        65508  +     SQLITE_NULL,     /* 0x15 */
        65509  +     SQLITE_INTEGER,  /* 0x16 */
        65510  +     SQLITE_NULL,     /* 0x17 */
        65511  +     SQLITE_FLOAT,    /* 0x18 */
        65512  +     SQLITE_NULL,     /* 0x19 */
        65513  +     SQLITE_FLOAT,    /* 0x1a */
        65514  +     SQLITE_NULL,     /* 0x1b */
        65515  +     SQLITE_INTEGER,  /* 0x1c */
        65516  +     SQLITE_NULL,     /* 0x1d */
        65517  +     SQLITE_INTEGER,  /* 0x1e */
        65518  +     SQLITE_NULL,     /* 0x1f */
        65519  +  };
        65520  +  return aType[pVal->flags&MEM_AffMask];
 64901  65521   }
 64902  65522   
 64903  65523   /**************************** sqlite3_result_  *******************************
 64904  65524   ** The following routines are used by user-defined functions to specify
 64905  65525   ** the function result.
 64906  65526   **
 64907  65527   ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
................................................................................
 65414  66034   */
 65415  66035   SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
 65416  66036     Vdbe *pVm = (Vdbe *)pStmt;
 65417  66037     if( pVm==0 || pVm->pResultSet==0 ) return 0;
 65418  66038     return pVm->nResColumn;
 65419  66039   }
 65420  66040   
        66041  +/*
        66042  +** Return a pointer to static memory containing an SQL NULL value.
        66043  +*/
        66044  +static const Mem *columnNullValue(void){
        66045  +  /* Even though the Mem structure contains an element
        66046  +  ** of type i64, on certain architectures (x86) with certain compiler
        66047  +  ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
        66048  +  ** instead of an 8-byte one. This all works fine, except that when
        66049  +  ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
        66050  +  ** that a Mem structure is located on an 8-byte boundary. To prevent
        66051  +  ** these assert()s from failing, when building with SQLITE_DEBUG defined
        66052  +  ** using gcc, we force nullMem to be 8-byte aligned using the magical
        66053  +  ** __attribute__((aligned(8))) macro.  */
        66054  +  static const Mem nullMem 
        66055  +#if defined(SQLITE_DEBUG) && defined(__GNUC__)
        66056  +    __attribute__((aligned(8))) 
        66057  +#endif
        66058  +    = {0, "", (double)0, {0}, 0, MEM_Null, 0,
        66059  +#ifdef SQLITE_DEBUG
        66060  +       0, 0,  /* pScopyFrom, pFiller */
        66061  +#endif
        66062  +       0, 0 };
        66063  +  return &nullMem;
        66064  +}
 65421  66065   
 65422  66066   /*
 65423  66067   ** Check to see if column iCol of the given statement is valid.  If
 65424  66068   ** it is, return a pointer to the Mem for the value of that column.
 65425  66069   ** If iCol is not valid, return a pointer to a Mem which has a value
 65426  66070   ** of NULL.
 65427  66071   */
................................................................................
 65430  66074     Mem *pOut;
 65431  66075   
 65432  66076     pVm = (Vdbe *)pStmt;
 65433  66077     if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
 65434  66078       sqlite3_mutex_enter(pVm->db->mutex);
 65435  66079       pOut = &pVm->pResultSet[i];
 65436  66080     }else{
 65437         -    /* If the value passed as the second argument is out of range, return
 65438         -    ** a pointer to the following static Mem object which contains the
 65439         -    ** value SQL NULL. Even though the Mem structure contains an element
 65440         -    ** of type i64, on certain architectures (x86) with certain compiler
 65441         -    ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
 65442         -    ** instead of an 8-byte one. This all works fine, except that when
 65443         -    ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
 65444         -    ** that a Mem structure is located on an 8-byte boundary. To prevent
 65445         -    ** these assert()s from failing, when building with SQLITE_DEBUG defined
 65446         -    ** using gcc, we force nullMem to be 8-byte aligned using the magical
 65447         -    ** __attribute__((aligned(8))) macro.  */
 65448         -    static const Mem nullMem 
 65449         -#if defined(SQLITE_DEBUG) && defined(__GNUC__)
 65450         -      __attribute__((aligned(8))) 
 65451         -#endif
 65452         -      = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
 65453         -#ifdef SQLITE_DEBUG
 65454         -         0, 0,  /* pScopyFrom, pFiller */
 65455         -#endif
 65456         -         0, 0 };
 65457         -
 65458  66081       if( pVm && ALWAYS(pVm->db) ){
 65459  66082         sqlite3_mutex_enter(pVm->db->mutex);
 65460  66083         sqlite3Error(pVm->db, SQLITE_RANGE, 0);
 65461  66084       }
 65462         -    pOut = (Mem*)&nullMem;
        66085  +    pOut = (Mem*)columnNullValue();
 65463  66086     }
 65464  66087     return pOut;
 65465  66088   }
 65466  66089   
 65467  66090   /*
 65468  66091   ** This function is called after invoking an sqlite3_value_XXX function on a 
 65469  66092   ** column value (i.e. a value returned by evaluating an SQL expression in the
................................................................................
 65852  66475     void (*xDel)(void*)
 65853  66476   ){
 65854  66477     return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
 65855  66478   }
 65856  66479   #endif /* SQLITE_OMIT_UTF16 */
 65857  66480   SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
 65858  66481     int rc;
 65859         -  switch( pValue->type ){
        66482  +  switch( sqlite3_value_type((sqlite3_value*)pValue) ){
 65860  66483       case SQLITE_INTEGER: {
 65861  66484         rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
 65862  66485         break;
 65863  66486       }
 65864  66487       case SQLITE_FLOAT: {
 65865  66488         rc = sqlite3_bind_double(pStmt, i, pValue->r);
 65866  66489         break;
................................................................................
 66353  66976   ** a legal notice, here is a blessing:
 66354  66977   **
 66355  66978   **    May you do good and not evil.
 66356  66979   **    May you find forgiveness for yourself and forgive others.
 66357  66980   **    May you share freely, never taking more than you give.
 66358  66981   **
 66359  66982   *************************************************************************
 66360         -** The code in this file implements execution method of the 
 66361         -** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
 66362         -** handles housekeeping details such as creating and deleting
 66363         -** VDBE instances.  This file is solely interested in executing
 66364         -** the VDBE program.
 66365         -**
 66366         -** In the external interface, an "sqlite3_stmt*" is an opaque pointer
 66367         -** to a VDBE.
 66368         -**
 66369         -** The SQL parser generates a program which is then executed by
 66370         -** the VDBE to do the work of the SQL statement.  VDBE programs are 
 66371         -** similar in form to assembly language.  The program consists of
 66372         -** a linear sequence of operations.  Each operation has an opcode 
 66373         -** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4 
 66374         -** is a null-terminated string.  Operand P5 is an unsigned character.
 66375         -** Few opcodes use all 5 operands.
 66376         -**
 66377         -** Computation results are stored on a set of registers numbered beginning
 66378         -** with 1 and going up to Vdbe.nMem.  Each register can store
 66379         -** either an integer, a null-terminated string, a floating point
 66380         -** number, or the SQL "NULL" value.  An implicit conversion from one
 66381         -** type to the other occurs as necessary.
 66382         -** 
 66383         -** Most of the code in this file is taken up by the sqlite3VdbeExec()
 66384         -** function which does the work of interpreting a VDBE program.
 66385         -** But other routines are also provided to help in building up
 66386         -** a program instruction by instruction.
        66983  +** The code in this file implements the function that runs the
        66984  +** bytecode of a prepared statement.
 66387  66985   **
 66388  66986   ** Various scripts scan this source file in order to generate HTML
 66389  66987   ** documentation, headers files, or other derived files.  The formatting
 66390  66988   ** of the code in this file is, therefore, important.  See other comments
 66391  66989   ** in this file for details.  If in doubt, do not deviate from existing
 66392  66990   ** commenting and indentation practices when changing or adding code.
 66393  66991   */
 66394  66992   
 66395  66993   /*
 66396  66994   ** Invoke this macro on memory cells just prior to changing the
 66397  66995   ** value of the cell.  This macro verifies that shallow copies are
 66398         -** not misused.
        66996  +** not misused.  A shallow copy of a string or blob just copies a
        66997  +** pointer to the string or blob, not the content.  If the original
        66998  +** is changed while the copy is still in use, the string or blob might
        66999  +** be changed out from under the copy.  This macro verifies that nothing
        67000  +** like that every happens.
 66399  67001   */
 66400  67002   #ifdef SQLITE_DEBUG
 66401  67003   # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
 66402  67004   #else
 66403  67005   # define memAboutToChange(P,M)
 66404  67006   #endif
 66405  67007   
................................................................................
 66450  67052     if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
 66451  67053       sqlite3_max_blobsize = p->n;
 66452  67054     }
 66453  67055   }
 66454  67056   #endif
 66455  67057   
 66456  67058   /*
 66457         -** The next global variable is incremented each type the OP_Found opcode
        67059  +** The next global variable is incremented each time the OP_Found opcode
 66458  67060   ** is executed. This is used to test whether or not the foreign key
 66459  67061   ** operation implemented using OP_FkIsZero is working. This variable
 66460  67062   ** has no function other than to help verify the correct operation of the
 66461  67063   ** library.
 66462  67064   */
 66463  67065   #ifdef SQLITE_TEST
 66464  67066   SQLITE_API int sqlite3_found_count = 0;
................................................................................
 66470  67072   */
 66471  67073   #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
 66472  67074   # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
 66473  67075   #else
 66474  67076   # define UPDATE_MAX_BLOBSIZE(P)
 66475  67077   #endif
 66476  67078   
        67079  +/*
        67080  +** Invoke the VDBE coverage callback, if that callback is defined.  This
        67081  +** feature is used for test suite validation only and does not appear an
        67082  +** production builds.
        67083  +**
        67084  +** M is an integer, 2 or 3, that indices how many different ways the
        67085  +** branch can go.  It is usually 2.  "I" is the direction the branch
        67086  +** goes.  0 means falls through.  1 means branch is taken.  2 means the
        67087  +** second alternative branch is taken.
        67088  +*/
        67089  +#if !defined(SQLITE_VDBE_COVERAGE)
        67090  +# define VdbeBranchTaken(I,M)
        67091  +#else
        67092  +# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
        67093  +  static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
        67094  +    if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
        67095  +      M = iSrcLine;
        67096  +      /* Assert the truth of VdbeCoverageAlwaysTaken() and 
        67097  +      ** VdbeCoverageNeverTaken() */
        67098  +      assert( (M & I)==I );
        67099  +    }else{
        67100  +      if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
        67101  +      sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
        67102  +                                      iSrcLine,I,M);
        67103  +    }
        67104  +  }
        67105  +#endif
        67106  +
 66477  67107   /*
 66478  67108   ** Convert the given register into a string if it isn't one
 66479  67109   ** already. Return non-zero if a malloc() fails.
 66480  67110   */
 66481  67111   #define Stringify(P, enc) \
 66482  67112      if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
 66483  67113        { goto no_mem; }
................................................................................
 66487  67117   ** a pointer to a dynamically allocated string where some other entity
 66488  67118   ** is responsible for deallocating that string.  Because the register
 66489  67119   ** does not control the string, it might be deleted without the register
 66490  67120   ** knowing it.
 66491  67121   **
 66492  67122   ** This routine converts an ephemeral string into a dynamically allocated
 66493  67123   ** string that the register itself controls.  In other words, it
 66494         -** converts an MEM_Ephem string into an MEM_Dyn string.
        67124  +** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
 66495  67125   */
 66496  67126   #define Deephemeralize(P) \
 66497  67127      if( ((P)->flags&MEM_Ephem)!=0 \
 66498  67128          && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
 66499  67129   
 66500  67130   /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
 66501         -# define isSorter(x) ((x)->pSorter!=0)
 66502         -
 66503         -/*
 66504         -** Argument pMem points at a register that will be passed to a
 66505         -** user-defined function or returned to the user as the result of a query.
 66506         -** This routine sets the pMem->type variable used by the sqlite3_value_*() 
 66507         -** routines.
 66508         -*/
 66509         -SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
 66510         -  int flags = pMem->flags;
 66511         -  if( flags & MEM_Null ){
 66512         -    pMem->type = SQLITE_NULL;
 66513         -  }
 66514         -  else if( flags & MEM_Int ){
 66515         -    pMem->type = SQLITE_INTEGER;
 66516         -  }
 66517         -  else if( flags & MEM_Real ){
 66518         -    pMem->type = SQLITE_FLOAT;
 66519         -  }
 66520         -  else if( flags & MEM_Str ){
 66521         -    pMem->type = SQLITE_TEXT;
 66522         -  }else{
 66523         -    pMem->type = SQLITE_BLOB;
 66524         -  }
 66525         -}
        67131  +#define isSorter(x) ((x)->pSorter!=0)
 66526  67132   
 66527  67133   /*
 66528  67134   ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
 66529  67135   ** if we run out of memory.
 66530  67136   */
 66531  67137   static VdbeCursor *allocateCursor(
 66532  67138     Vdbe *p,              /* The virtual machine */
................................................................................
 66648  67254   /*
 66649  67255   ** Try to convert the type of a function argument or a result column
 66650  67256   ** into a numeric representation.  Use either INTEGER or REAL whichever
 66651  67257   ** is appropriate.  But only do the conversion if it is possible without
 66652  67258   ** loss of information and return the revised type of the argument.
 66653  67259   */
 66654  67260   SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
 66655         -  Mem *pMem = (Mem*)pVal;
 66656         -  if( pMem->type==SQLITE_TEXT ){
        67261  +  int eType = sqlite3_value_type(pVal);
        67262  +  if( eType==SQLITE_TEXT ){
        67263  +    Mem *pMem = (Mem*)pVal;
 66657  67264       applyNumericAffinity(pMem);
 66658         -    sqlite3VdbeMemStoreType(pMem);
        67265  +    eType = sqlite3_value_type(pVal);
 66659  67266     }
 66660         -  return pMem->type;
        67267  +  return eType;
 66661  67268   }
 66662  67269   
 66663  67270   /*
 66664  67271   ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
 66665  67272   ** not the internal Mem* type.
 66666  67273   */
 66667  67274   SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
................................................................................
 66756  67363   #endif
 66757  67364   
 66758  67365   #ifdef SQLITE_DEBUG
 66759  67366   /*
 66760  67367   ** Print the value of a register for tracing purposes:
 66761  67368   */
 66762  67369   static void memTracePrint(Mem *p){
 66763         -  if( p->flags & MEM_Invalid ){
        67370  +  if( p->flags & MEM_Undefined ){
 66764  67371       printf(" undefined");
 66765  67372     }else if( p->flags & MEM_Null ){
 66766  67373       printf(" NULL");
 66767  67374     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
 66768  67375       printf(" si:%lld", p->u.i);
 66769  67376     }else if( p->flags & MEM_Int ){
 66770  67377       printf(" i:%lld", p->u.i);
................................................................................
 66888  67495   
 66889  67496   #endif /* !defined(_HWTIME_H_) */
 66890  67497   
 66891  67498   /************** End of hwtime.h **********************************************/
 66892  67499   /************** Continuing where we left off in vdbe.c ***********************/
 66893  67500   
 66894  67501   #endif
 66895         -
 66896         -/*
 66897         -** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
 66898         -** sqlite3_interrupt() routine has been called.  If it has been, then
 66899         -** processing of the VDBE program is interrupted.
 66900         -**
 66901         -** This macro added to every instruction that does a jump in order to
 66902         -** implement a loop.  This test used to be on every single instruction,
 66903         -** but that meant we more testing than we needed.  By only testing the
 66904         -** flag on jump instructions, we get a (small) speed improvement.
 66905         -*/
 66906         -#define CHECK_FOR_INTERRUPT \
 66907         -   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 66908         -
 66909  67502   
 66910  67503   #ifndef NDEBUG
 66911  67504   /*
 66912  67505   ** This function is only called from within an assert() expression. It
 66913  67506   ** checks that the sqlite3.nTransaction variable is correctly set to
 66914  67507   ** the number of non-transaction savepoints currently in the 
 66915  67508   ** linked list starting at sqlite3.pSavepoint.
................................................................................
 66925  67518     assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
 66926  67519     return 1;
 66927  67520   }
 66928  67521   #endif
 66929  67522   
 66930  67523   
 66931  67524   /*
 66932         -** Execute as much of a VDBE program as we can then return.
 66933         -**
 66934         -** sqlite3VdbeMakeReady() must be called before this routine in order to
 66935         -** close the program with a final OP_Halt and to set up the callbacks
 66936         -** and the error message pointer.
 66937         -**
 66938         -** Whenever a row or result data is available, this routine will either
 66939         -** invoke the result callback (if there is one) or return with
 66940         -** SQLITE_ROW.
 66941         -**
 66942         -** If an attempt is made to open a locked database, then this routine
 66943         -** will either invoke the busy callback (if there is one) or it will
 66944         -** return SQLITE_BUSY.
 66945         -**
 66946         -** If an error occurs, an error message is written to memory obtained
 66947         -** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
 66948         -** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
 66949         -**
 66950         -** If the callback ever returns non-zero, then the program exits
 66951         -** immediately.  There will be no error message but the p->rc field is
 66952         -** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
 66953         -**
 66954         -** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
 66955         -** routine to return SQLITE_ERROR.
 66956         -**
 66957         -** Other fatal errors return SQLITE_ERROR.
 66958         -**
 66959         -** After this routine has finished, sqlite3VdbeFinalize() should be
 66960         -** used to clean up the mess that was left behind.
        67525  +** Execute as much of a VDBE program as we can.
        67526  +** This is the core of sqlite3_step().  
 66961  67527   */
 66962  67528   SQLITE_PRIVATE int sqlite3VdbeExec(
 66963  67529     Vdbe *p                    /* The VDBE */
 66964  67530   ){
 66965  67531     int pc=0;                  /* The program counter */
 66966  67532     Op *aOp = p->aOp;          /* Copy of p->aOp */
 66967  67533     Op *pOp;                   /* Current operation */
................................................................................
 66979  67545     Mem *pIn2 = 0;             /* 2nd input operand */
 66980  67546     Mem *pIn3 = 0;             /* 3rd input operand */
 66981  67547     Mem *pOut = 0;             /* Output operand */
 66982  67548     int *aPermute = 0;         /* Permutation of columns for OP_Compare */
 66983  67549     i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
 66984  67550   #ifdef VDBE_PROFILE
 66985  67551     u64 start;                 /* CPU clock count at start of opcode */
 66986         -  int origPc;                /* Program counter at start of opcode */
 66987  67552   #endif
 66988  67553     /*** INSERT STACK UNION HERE ***/
 66989  67554   
 66990  67555     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 66991  67556     sqlite3VdbeEnter(p);
 66992  67557     if( p->rc==SQLITE_NOMEM ){
 66993  67558       /* This happens if a malloc() inside a call to sqlite3_column_text() or
................................................................................
 66997  67562     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
 66998  67563     assert( p->bIsReader || p->readOnly!=0 );
 66999  67564     p->rc = SQLITE_OK;
 67000  67565     p->iCurrentTime = 0;
 67001  67566     assert( p->explain==0 );
 67002  67567     p->pResultSet = 0;
 67003  67568     db->busyHandler.nBusy = 0;
 67004         -  CHECK_FOR_INTERRUPT;
        67569  +  if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 67005  67570     sqlite3VdbeIOTraceSql(p);
 67006  67571   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 67007  67572     if( db->xProgress ){
 67008  67573       assert( 0 < db->nProgressOps );
 67009  67574       nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
 67010  67575       if( nProgressLimit==0 ){
 67011  67576         nProgressLimit = db->nProgressOps;
................................................................................
 67041  67606     }
 67042  67607     sqlite3EndBenignMalloc();
 67043  67608   #endif
 67044  67609     for(pc=p->pc; rc==SQLITE_OK; pc++){
 67045  67610       assert( pc>=0 && pc<p->nOp );
 67046  67611       if( db->mallocFailed ) goto no_mem;
 67047  67612   #ifdef VDBE_PROFILE
 67048         -    origPc = pc;
 67049  67613       start = sqlite3Hwtime();
 67050  67614   #endif
 67051  67615       nVmStep++;
 67052  67616       pOp = &aOp[pc];
 67053  67617   
 67054  67618       /* Only allow tracing if SQLITE_DEBUG is defined.
 67055  67619       */
................................................................................
 67089  67653   
 67090  67654       /* Sanity checking on other operands */
 67091  67655   #ifdef SQLITE_DEBUG
 67092  67656       if( (pOp->opflags & OPFLG_IN1)!=0 ){
 67093  67657         assert( pOp->p1>0 );
 67094  67658         assert( pOp->p1<=(p->nMem-p->nCursor) );
 67095  67659         assert( memIsValid(&aMem[pOp->p1]) );
        67660  +      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
 67096  67661         REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
 67097  67662       }
 67098  67663       if( (pOp->opflags & OPFLG_IN2)!=0 ){
 67099  67664         assert( pOp->p2>0 );
 67100  67665         assert( pOp->p2<=(p->nMem-p->nCursor) );
 67101  67666         assert( memIsValid(&aMem[pOp->p2]) );
        67667  +      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
 67102  67668         REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
 67103  67669       }
 67104  67670       if( (pOp->opflags & OPFLG_IN3)!=0 ){
 67105  67671         assert( pOp->p3>0 );
 67106  67672         assert( pOp->p3<=(p->nMem-p->nCursor) );
 67107  67673         assert( memIsValid(&aMem[pOp->p3]) );
        67674  +      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
 67108  67675         REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
 67109  67676       }
 67110  67677       if( (pOp->opflags & OPFLG_OUT2)!=0 ){
 67111  67678         assert( pOp->p2>0 );
 67112  67679         assert( pOp->p2<=(p->nMem-p->nCursor) );
 67113  67680         memAboutToChange(p, &aMem[pOp->p2]);
 67114  67681       }
................................................................................
 67173  67740     **
 67174  67741     ** This code uses unstructured "goto" statements and does not look clean.
 67175  67742     ** But that is not due to sloppy coding habits. The code is written this
 67176  67743     ** way for performance, to avoid having to run the interrupt and progress
 67177  67744     ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
 67178  67745     ** faster according to "valgrind --tool=cachegrind" */
 67179  67746   check_for_interrupt:
 67180         -  CHECK_FOR_INTERRUPT;
        67747  +  if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 67181  67748   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 67182  67749     /* Call the progress callback if it is configured and the required number
 67183  67750     ** of VDBE ops have been executed (either since this invocation of
 67184  67751     ** sqlite3VdbeExec() or since last time the progress callback was called).
 67185  67752     ** If the progress callback returns non-zero, exit the virtual machine with
 67186  67753     ** a return code SQLITE_ABORT.
 67187  67754     */
................................................................................
 67202  67769   **
 67203  67770   ** Write the current address onto register P1
 67204  67771   ** and then jump to address P2.
 67205  67772   */
 67206  67773   case OP_Gosub: {            /* jump */
 67207  67774     assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
 67208  67775     pIn1 = &aMem[pOp->p1];
 67209         -  assert( (pIn1->flags & MEM_Dyn)==0 );
        67776  +  assert( VdbeMemDynamic(pIn1)==0 );
 67210  67777     memAboutToChange(p, pIn1);
 67211  67778     pIn1->flags = MEM_Int;
 67212  67779     pIn1->u.i = pc;
 67213  67780     REGISTER_TRACE(pOp->p1, pIn1);
 67214  67781     pc = pOp->p2 - 1;
 67215  67782     break;
 67216  67783   }
 67217  67784   
 67218  67785   /* Opcode:  Return P1 * * * *
 67219  67786   **
 67220         -** Jump to the next instruction after the address in register P1.
        67787  +** Jump to the next instruction after the address in register P1.  After
        67788  +** the jump, register P1 becomes undefined.
 67221  67789   */
 67222  67790   case OP_Return: {           /* in1 */
 67223  67791     pIn1 = &aMem[pOp->p1];
 67224         -  assert( pIn1->flags & MEM_Int );
        67792  +  assert( pIn1->flags==MEM_Int );
 67225  67793     pc = (int)pIn1->u.i;
        67794  +  pIn1->flags = MEM_Undefined;
 67226  67795     break;
 67227  67796   }
 67228  67797   
 67229         -/* Opcode:  Yield P1 * * * *
        67798  +/* Opcode: InitCoroutine P1 P2 P3 * *
        67799  +**
        67800  +** Set up register P1 so that it will OP_Yield to the co-routine
        67801  +** located at address P3.
        67802  +**
        67803  +** If P2!=0 then the co-routine implementation immediately follows
        67804  +** this opcode.  So jump over the co-routine implementation to
        67805  +** address P2.
        67806  +*/
        67807  +case OP_InitCoroutine: {     /* jump */
        67808  +  assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
        67809  +  assert( pOp->p2>=0 && pOp->p2<p->nOp );
        67810  +  assert( pOp->p3>=0 && pOp->p3<p->nOp );
        67811  +  pOut = &aMem[pOp->p1];
        67812  +  assert( !VdbeMemDynamic(pOut) );
        67813  +  pOut->u.i = pOp->p3 - 1;
        67814  +  pOut->flags = MEM_Int;
        67815  +  if( pOp->p2 ) pc = pOp->p2 - 1;
        67816  +  break;
        67817  +}
        67818  +
        67819  +/* Opcode:  EndCoroutine P1 * * * *
        67820  +**
        67821  +** The instruction at the address in register P1 is an OP_Yield.
        67822  +** Jump to the P2 parameter of that OP_Yield.
        67823  +** After the jump, register P1 becomes undefined.
        67824  +*/
        67825  +case OP_EndCoroutine: {           /* in1 */
        67826  +  VdbeOp *pCaller;
        67827  +  pIn1 = &aMem[pOp->p1];
        67828  +  assert( pIn1->flags==MEM_Int );
        67829  +  assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
        67830  +  pCaller = &aOp[pIn1->u.i];
        67831  +  assert( pCaller->opcode==OP_Yield );
        67832  +  assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
        67833  +  pc = pCaller->p2 - 1;
        67834  +  pIn1->flags = MEM_Undefined;
        67835  +  break;
        67836  +}
        67837  +
        67838  +/* Opcode:  Yield P1 P2 * * *
 67230  67839   **
 67231  67840   ** Swap the program counter with the value in register P1.
        67841  +**
        67842  +** If the co-routine ends with OP_Yield or OP_Return then continue
        67843  +** to the next instruction.  But if the co-routine ends with
        67844  +** OP_EndCoroutine, jump immediately to P2.
 67232  67845   */
 67233         -case OP_Yield: {            /* in1 */
        67846  +case OP_Yield: {            /* in1, jump */
 67234  67847     int pcDest;
 67235  67848     pIn1 = &aMem[pOp->p1];
 67236         -  assert( (pIn1->flags & MEM_Dyn)==0 );
        67849  +  assert( VdbeMemDynamic(pIn1)==0 );
 67237  67850     pIn1->flags = MEM_Int;
 67238  67851     pcDest = (int)pIn1->u.i;
 67239  67852     pIn1->u.i = pc;
 67240  67853     REGISTER_TRACE(pOp->p1, pIn1);
 67241  67854     pc = pcDest;
 67242  67855     break;
 67243  67856   }
 67244  67857   
 67245  67858   /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
 67246         -** Synopsis:  if r[P3] null then halt
        67859  +** Synopsis:  if r[P3]=null halt
 67247  67860   **
 67248  67861   ** Check the value in register P3.  If it is NULL then Halt using
 67249  67862   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
 67250  67863   ** value in register P3 is not NULL, then this routine is a no-op.
 67251  67864   ** The P5 parameter should be 1.
 67252  67865   */
 67253  67866   case OP_HaltIfNull: {      /* in3 */
................................................................................
 67387  68000   }
 67388  68001   #endif
 67389  68002   
 67390  68003   /* Opcode: String8 * P2 * P4 *
 67391  68004   ** Synopsis: r[P2]='P4'
 67392  68005   **
 67393  68006   ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
 67394         -** into an OP_String before it is executed for the first time.
        68007  +** into an OP_String before it is executed for the first time.  During
        68008  +** this transformation, the length of string P4 is computed and stored
        68009  +** as the P1 parameter.
 67395  68010   */
 67396  68011   case OP_String8: {         /* same as TK_STRING, out2-prerelease */
 67397  68012     assert( pOp->p4.z!=0 );
 67398  68013     pOp->opcode = OP_String;
 67399  68014     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 67400  68015   
 67401  68016   #ifndef SQLITE_OMIT_UTF16
 67402  68017     if( encoding!=SQLITE_UTF8 ){
 67403  68018       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 67404  68019       if( rc==SQLITE_TOOBIG ) goto too_big;
 67405  68020       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
 67406  68021       assert( pOut->zMalloc==pOut->z );
 67407         -    assert( pOut->flags & MEM_Dyn );
        68022  +    assert( VdbeMemDynamic(pOut)==0 );
 67408  68023       pOut->zMalloc = 0;
 67409  68024       pOut->flags |= MEM_Static;
 67410         -    pOut->flags &= ~MEM_Dyn;
 67411  68025       if( pOp->p4type==P4_DYNAMIC ){
 67412  68026         sqlite3DbFree(db, pOp->p4.z);
 67413  68027       }
 67414  68028       pOp->p4type = P4_DYNAMIC;
 67415  68029       pOp->p4.z = pOut->z;
 67416  68030       pOp->p1 = pOut->n;
 67417  68031     }
................................................................................
 67461  68075       VdbeMemRelease(pOut);
 67462  68076       pOut->flags = nullFlag;
 67463  68077       cnt--;
 67464  68078     }
 67465  68079     break;
 67466  68080   }
 67467  68081   
        68082  +/* Opcode: SoftNull P1 * * * *
        68083  +** Synopsis:  r[P1]=NULL
        68084  +**
        68085  +** Set register P1 to have the value NULL as seen by the OP_MakeRecord
        68086  +** instruction, but do not free any string or blob memory associated with
        68087  +** the register, so that if the value was a string or blob that was
        68088  +** previously copied using OP_SCopy, the copies will continue to be valid.
        68089  +*/
        68090  +case OP_SoftNull: {
        68091  +  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
        68092  +  pOut = &aMem[pOp->p1];
        68093  +  pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
        68094  +  break;
        68095  +}
 67468  68096   
 67469         -/* Opcode: Blob P1 P2 * P4
        68097  +/* Opcode: Blob P1 P2 * P4 *
 67470  68098   ** Synopsis: r[P2]=P4 (len=P1)
 67471  68099   **
 67472  68100   ** P4 points to a blob of data P1 bytes long.  Store this
 67473  68101   ** blob in register P2.
 67474  68102   */
 67475  68103   case OP_Blob: {                /* out2-prerelease */
 67476  68104     assert( pOp->p1 <= SQLITE_MAX_LENGTH );
................................................................................
 67481  68109   }
 67482  68110   
 67483  68111   /* Opcode: Variable P1 P2 * P4 *
 67484  68112   ** Synopsis: r[P2]=parameter(P1,P4)
 67485  68113   **
 67486  68114   ** Transfer the values of bound parameter P1 into register P2
 67487  68115   **
 67488         -** If the parameter is named, then its name appears in P4 and P3==1.
        68116  +** If the parameter is named, then its name appears in P4.
 67489  68117   ** The P4 value is used by sqlite3_bind_parameter_name().
 67490  68118   */
 67491  68119   case OP_Variable: {            /* out2-prerelease */
 67492  68120     Mem *pVar;       /* Value being transferred */
 67493  68121   
 67494  68122     assert( pOp->p1>0 && pOp->p1<=p->nVar );
 67495  68123     assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
................................................................................
 67525  68153     pIn1 = &aMem[p1];
 67526  68154     pOut = &aMem[p2];
 67527  68155     do{
 67528  68156       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
 67529  68157       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
 67530  68158       assert( memIsValid(pIn1) );
 67531  68159       memAboutToChange(p, pOut);
        68160  +    VdbeMemRelease(pOut);
 67532  68161       zMalloc = pOut->zMalloc;
 67533         -    pOut->zMalloc = 0;
 67534         -    sqlite3VdbeMemMove(pOut, pIn1);
        68162  +    memcpy(pOut, pIn1, sizeof(Mem));
 67535  68163   #ifdef SQLITE_DEBUG
 67536  68164       if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
 67537  68165         pOut->pScopyFrom += p1 - pOp->p2;
 67538  68166       }
 67539  68167   #endif
        68168  +    pIn1->flags = MEM_Undefined;
        68169  +    pIn1->xDel = 0;
 67540  68170       pIn1->zMalloc = zMalloc;
 67541  68171       REGISTER_TRACE(p2++, pOut);
 67542  68172       pIn1++;
 67543  68173       pOut++;
 67544  68174     }while( n-- );
 67545  68175     break;
 67546  68176   }
................................................................................
 67600  68230   
 67601  68231   /* Opcode: ResultRow P1 P2 * * *
 67602  68232   ** Synopsis:  output=r[P1@P2]
 67603  68233   **
 67604  68234   ** The registers P1 through P1+P2-1 contain a single row of
 67605  68235   ** results. This opcode causes the sqlite3_step() call to terminate
 67606  68236   ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
 67607         -** structure to provide access to the top P1 values as the result
 67608         -** row.
        68237  +** structure to provide access to the r(P1)..r(P1+P2-1) values as
        68238  +** the result row.
 67609  68239   */
 67610  68240   case OP_ResultRow: {
 67611  68241     Mem *pMem;
 67612  68242     int i;
 67613  68243     assert( p->nResColumn==pOp->p2 );
 67614  68244     assert( pOp->p1>0 );
 67615  68245     assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
................................................................................
 67666  68296     pMem = p->pResultSet = &aMem[pOp->p1];
 67667  68297     for(i=0; i<pOp->p2; i++){
 67668  68298       assert( memIsValid(&pMem[i]) );
 67669  68299       Deephemeralize(&pMem[i]);
 67670  68300       assert( (pMem[i].flags & MEM_Ephem)==0
 67671  68301               || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
 67672  68302       sqlite3VdbeMemNulTerminate(&pMem[i]);
 67673         -    sqlite3VdbeMemStoreType(&pMem[i]);
 67674  68303       REGISTER_TRACE(pOp->p1+i, &pMem[i]);
 67675  68304     }
 67676  68305     if( db->mallocFailed ) goto no_mem;
 67677  68306   
 67678  68307     /* Return SQLITE_ROW
 67679  68308     */
 67680  68309     p->pc = pc + 1;
................................................................................
 67709  68338     if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
 67710  68339     Stringify(pIn1, encoding);
 67711  68340     Stringify(pIn2, encoding);
 67712  68341     nByte = pIn1->n + pIn2->n;
 67713  68342     if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 67714  68343       goto too_big;
 67715  68344     }
 67716         -  MemSetTypeFlag(pOut, MEM_Str);
 67717  68345     if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
 67718  68346       goto no_mem;
 67719  68347     }
        68348  +  MemSetTypeFlag(pOut, MEM_Str);
 67720  68349     if( pOut!=pIn2 ){
 67721  68350       memcpy(pOut->z, pIn2->z, pIn2->n);
 67722  68351     }
 67723  68352     memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
 67724  68353     pOut->z[nByte]=0;
 67725  68354     pOut->z[nByte+1] = 0;
 67726  68355     pOut->flags |= MEM_Term;
................................................................................
 67912  68541     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
 67913  68542     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 67914  68543     pArg = &aMem[pOp->p2];
 67915  68544     for(i=0; i<n; i++, pArg++){
 67916  68545       assert( memIsValid(pArg) );
 67917  68546       apVal[i] = pArg;
 67918  68547       Deephemeralize(pArg);
 67919         -    sqlite3VdbeMemStoreType(pArg);
 67920  68548       REGISTER_TRACE(pOp->p2+i, pArg);
 67921  68549     }
 67922  68550   
 67923  68551     assert( pOp->p4type==P4_FUNCDEF );
 67924  68552     ctx.pFunc = pOp->p4.pFunc;
 67925  68553     ctx.iOp = pc;
 67926  68554     ctx.pVdbe = p;
................................................................................
 68091  68719   ** without data loss, then jump immediately to P2, or if P2==0
 68092  68720   ** raise an SQLITE_MISMATCH exception.
 68093  68721   */
 68094  68722   case OP_MustBeInt: {            /* jump, in1 */
 68095  68723     pIn1 = &aMem[pOp->p1];
 68096  68724     if( (pIn1->flags & MEM_Int)==0 ){
 68097  68725       applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
        68726  +    VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
 68098  68727       if( (pIn1->flags & MEM_Int)==0 ){
 68099  68728         if( pOp->p2==0 ){
 68100  68729           rc = SQLITE_MISMATCH;
 68101  68730           goto abort_due_to_error;
 68102  68731         }else{
 68103  68732           pc = pOp->p2 - 1;
 68104  68733           break;
................................................................................
 68129  68758   #endif
 68130  68759   
 68131  68760   #ifndef SQLITE_OMIT_CAST
 68132  68761   /* Opcode: ToText P1 * * * *
 68133  68762   **
 68134  68763   ** Force the value in register P1 to be text.
 68135  68764   ** If the value is numeric, convert it to a string using the
 68136         -** equivalent of printf().  Blob values are unchanged and
        68765  +** equivalent of sprintf().  Blob values are unchanged and
 68137  68766   ** are afterwards simply interpreted as text.
 68138  68767   **
 68139  68768   ** A NULL value is not changed by this routine.  It remains NULL.
 68140  68769   */
 68141  68770   case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
 68142  68771     pIn1 = &aMem[pOp->p1];
 68143  68772     memAboutToChange(p, pIn1);
................................................................................
 68331  68960       if( pOp->p5 & SQLITE_NULLEQ ){
 68332  68961         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
 68333  68962         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
 68334  68963         ** or not both operands are null.
 68335  68964         */
 68336  68965         assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 68337  68966         assert( (flags1 & MEM_Cleared)==0 );
        68967  +      assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
 68338  68968         if( (flags1&MEM_Null)!=0
 68339  68969          && (flags3&MEM_Null)!=0
 68340  68970          && (flags3&MEM_Cleared)==0
 68341  68971         ){
 68342  68972           res = 0;  /* Results are equal */
 68343  68973         }else{
 68344  68974           res = 1;  /* Results are not equal */
 68345  68975         }
 68346  68976       }else{
 68347  68977         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 68348  68978         ** then the result is always NULL.
 68349  68979         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 68350  68980         */
 68351         -      if( pOp->p5 & SQLITE_JUMPIFNULL ){
 68352         -        pc = pOp->p2-1;
 68353         -      }else if( pOp->p5 & SQLITE_STOREP2 ){
        68981  +      if( pOp->p5 & SQLITE_STOREP2 ){
 68354  68982           pOut = &aMem[pOp->p2];
 68355  68983           MemSetTypeFlag(pOut, MEM_Null);
 68356  68984           REGISTER_TRACE(pOp->p2, pOut);
        68985  +      }else{
        68986  +        VdbeBranchTaken(2,3);
        68987  +        if( pOp->p5 & SQLITE_JUMPIFNULL ){
        68988  +          pc = pOp->p2-1;
        68989  +        }
 68357  68990         }
 68358  68991         break;
 68359  68992       }
 68360  68993     }else{
 68361  68994       /* Neither operand is NULL.  Do a comparison. */
 68362  68995       affinity = pOp->p5 & SQLITE_AFF_MASK;
 68363  68996       if( affinity ){
................................................................................
 68382  69015   
 68383  69016     if( pOp->p5 & SQLITE_STOREP2 ){
 68384  69017       pOut = &aMem[pOp->p2];
 68385  69018       memAboutToChange(p, pOut);
 68386  69019       MemSetTypeFlag(pOut, MEM_Int);
 68387  69020       pOut->u.i = res;
 68388  69021       REGISTER_TRACE(pOp->p2, pOut);
 68389         -  }else if( res ){
 68390         -    pc = pOp->p2-1;
        69022  +  }else{
        69023  +    VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
        69024  +    if( res ){
        69025  +      pc = pOp->p2-1;
        69026  +    }
 68391  69027     }
 68392         -
 68393  69028     /* Undo any changes made by applyAffinity() to the input registers. */
 68394  69029     pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
 68395  69030     pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
 68396  69031     break;
 68397  69032   }
 68398  69033   
 68399  69034   /* Opcode: Permutation * * * P4 *
................................................................................
 68482  69117   **
 68483  69118   ** Jump to the instruction at address P1, P2, or P3 depending on whether
 68484  69119   ** in the most recent OP_Compare instruction the P1 vector was less than
 68485  69120   ** equal to, or greater than the P2 vector, respectively.
 68486  69121   */
 68487  69122   case OP_Jump: {             /* jump */
 68488  69123     if( iCompare<0 ){
 68489         -    pc = pOp->p1 - 1;
        69124  +    pc = pOp->p1 - 1;  VdbeBranchTaken(0,3);
 68490  69125     }else if( iCompare==0 ){
 68491         -    pc = pOp->p2 - 1;
        69126  +    pc = pOp->p2 - 1;  VdbeBranchTaken(1,3);
 68492  69127     }else{
 68493         -    pc = pOp->p3 - 1;
        69128  +    pc = pOp->p3 - 1;  VdbeBranchTaken(2,3);
 68494  69129     }
 68495  69130     break;
 68496  69131   }
 68497  69132   
 68498  69133   /* Opcode: And P1 P2 P3 * *
 68499  69134   ** Synopsis: r[P3]=(r[P1] && r[P2])
 68500  69135   **
................................................................................
 68584  69219     }
 68585  69220     break;
 68586  69221   }
 68587  69222   
 68588  69223   /* Opcode: Once P1 P2 * * *
 68589  69224   **
 68590  69225   ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
 68591         -** set the flag and fall through to the next instruction.
        69226  +** set the flag and fall through to the next instruction.  In other words,
        69227  +** this opcode causes all following up codes up through P2 (but not including
        69228  +** P2) to run just once and skipped on subsequent times through the loop.
 68592  69229   */
 68593  69230   case OP_Once: {             /* jump */
 68594  69231     assert( pOp->p1<p->nOnceFlag );
        69232  +  VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
 68595  69233     if( p->aOnceFlag[pOp->p1] ){
 68596  69234       pc = pOp->p2-1;
 68597  69235     }else{
 68598  69236       p->aOnceFlag[pOp->p1] = 1;
 68599  69237     }
 68600  69238     break;
 68601  69239   }
................................................................................
 68622  69260   #ifdef SQLITE_OMIT_FLOATING_POINT
 68623  69261       c = sqlite3VdbeIntValue(pIn1)!=0;
 68624  69262   #else
 68625  69263       c = sqlite3VdbeRealValue(pIn1)!=0.0;
 68626  69264   #endif
 68627  69265       if( pOp->opcode==OP_IfNot ) c = !c;
 68628  69266     }
        69267  +  VdbeBranchTaken(c!=0, 2);
 68629  69268     if( c ){
 68630  69269       pc = pOp->p2-1;
 68631  69270     }
 68632  69271     break;
 68633  69272   }
 68634  69273   
 68635  69274   /* Opcode: IsNull P1 P2 * * *
 68636  69275   ** Synopsis:  if r[P1]==NULL goto P2
 68637  69276   **
 68638  69277   ** Jump to P2 if the value in register P1 is NULL.
 68639  69278   */
 68640  69279   case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
 68641  69280     pIn1 = &aMem[pOp->p1];
        69281  +  VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
 68642  69282     if( (pIn1->flags & MEM_Null)!=0 ){
 68643  69283       pc = pOp->p2 - 1;
 68644  69284     }
 68645  69285     break;
 68646  69286   }
 68647  69287   
 68648  69288   /* Opcode: NotNull P1 P2 * * *
 68649  69289   ** Synopsis: if r[P1]!=NULL goto P2
 68650  69290   **
 68651  69291   ** Jump to P2 if the value in register P1 is not NULL.  
 68652  69292   */
 68653  69293   case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
 68654  69294     pIn1 = &aMem[pOp->p1];
        69295  +  VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
 68655  69296     if( (pIn1->flags & MEM_Null)==0 ){
 68656  69297       pc = pOp->p2 - 1;
 68657  69298     }
 68658  69299     break;
 68659  69300   }
 68660  69301   
 68661  69302   /* Opcode: Column P1 P2 P3 P4 P5
................................................................................
 68724  69365     rc = sqlite3VdbeCursorMoveto(pC);
 68725  69366     if( rc ) goto abort_due_to_error;
 68726  69367     if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
 68727  69368       if( pC->nullRow ){
 68728  69369         if( pCrsr==0 ){
 68729  69370           assert( pC->pseudoTableReg>0 );
 68730  69371           pReg = &aMem[pC->pseudoTableReg];
 68731         -        if( pC->multiPseudo ){
 68732         -          sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
 68733         -          Deephemeralize(pDest);
 68734         -          goto op_column_out;
 68735         -        }
 68736  69372           assert( pReg->flags & MEM_Blob );
 68737  69373           assert( memIsValid(pReg) );
 68738  69374           pC->payloadSize = pC->szRow = avail = pReg->n;
 68739  69375           pC->aRow = (u8*)pReg->z;
 68740  69376         }else{
 68741  69377           MemSetTypeFlag(pDest, MEM_Null);
 68742  69378           goto op_column_out;
................................................................................
 68879  69515   
 68880  69516     /* Extract the content for the p2+1-th column.  Control can only
 68881  69517     ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
 68882  69518     ** all valid.
 68883  69519     */
 68884  69520     assert( p2<pC->nHdrParsed );
 68885  69521     assert( rc==SQLITE_OK );
        69522  +  assert( sqlite3VdbeCheckMemInvariants(pDest) );
 68886  69523     if( pC->szRow>=aOffset[p2+1] ){
 68887  69524       /* This is the common case where the desired content fits on the original
 68888  69525       ** page - where the content is not on an overflow page */
 68889  69526       VdbeMemRelease(pDest);
 68890  69527       sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
 68891  69528     }else{
 68892  69529       /* This branch happens only when content is on overflow pages */
................................................................................
 68916  69553       sqlite3VdbeSerialGet(zData, t, pDest);
 68917  69554       /* If we dynamically allocated space to hold the data (in the
 68918  69555       ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
 68919  69556       ** dynamically allocated space over to the pDest structure.
 68920  69557       ** This prevents a memory copy. */
 68921  69558       if( sMem.zMalloc ){
 68922  69559         assert( sMem.z==sMem.zMalloc );
 68923         -      assert( !(pDest->flags & MEM_Dyn) );
 68924         -      assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
        69560  +      assert( VdbeMemDynamic(pDest)==0 );
        69561  +      assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
 68925  69562         pDest->flags &= ~(MEM_Ephem|MEM_Static);
 68926  69563         pDest->flags |= MEM_Term;
 68927  69564         pDest->z = sMem.z;
 68928  69565         pDest->zMalloc = sMem.zMalloc;
 68929  69566       }
 68930  69567     }
 68931  69568     pDest->enc = encoding;
................................................................................
 68954  69591     zAffinity = pOp->p4.z;
 68955  69592     assert( zAffinity!=0 );
 68956  69593     assert( zAffinity[pOp->p2]==0 );
 68957  69594     pIn1 = &aMem[pOp->p1];
 68958  69595     while( (cAff = *(zAffinity++))!=0 ){
 68959  69596       assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
 68960  69597       assert( memIsValid(pIn1) );
 68961         -    ExpandBlob(pIn1);
 68962  69598       applyAffinity(pIn1, cAff, encoding);
 68963  69599       pIn1++;
 68964  69600     }
 68965  69601     break;
 68966  69602   }
 68967  69603   
 68968  69604   /* Opcode: MakeRecord P1 P2 P3 P4 *
................................................................................
 69032  69668   
 69033  69669     /* Apply the requested affinity to all inputs
 69034  69670     */
 69035  69671     assert( pData0<=pLast );
 69036  69672     if( zAffinity ){
 69037  69673       pRec = pData0;
 69038  69674       do{
 69039         -      applyAffinity(pRec, *(zAffinity++), encoding);
 69040         -    }while( (++pRec)<=pLast );
        69675  +      applyAffinity(pRec++, *(zAffinity++), encoding);
        69676  +      assert( zAffinity[0]==0 || pRec<=pLast );
        69677  +    }while( zAffinity[0] );
 69041  69678     }
 69042  69679   
 69043  69680     /* Loop through the elements that will make up the record to figure
 69044  69681     ** out how much space is required for the new record.
 69045  69682     */
 69046  69683     pRec = pLast;
 69047  69684     do{
................................................................................
 69100  69737       j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
 69101  69738     }while( (++pRec)<=pLast );
 69102  69739     assert( i==nHdr );
 69103  69740     assert( j==nByte );
 69104  69741   
 69105  69742     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 69106  69743     pOut->n = (int)nByte;
 69107         -  pOut->flags = MEM_Blob | MEM_Dyn;
        69744  +  pOut->flags = MEM_Blob;
 69108  69745     pOut->xDel = 0;
 69109  69746     if( nZero ){
 69110  69747       pOut->u.nZero = nZero;
 69111  69748       pOut->flags |= MEM_Zero;
 69112  69749     }
 69113  69750     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
 69114  69751     REGISTER_TRACE(pOp->p3, pOut);
................................................................................
 69377  70014                      "cannot commit - no transaction is active"));
 69378  70015            
 69379  70016       rc = SQLITE_ERROR;
 69380  70017     }
 69381  70018     break;
 69382  70019   }
 69383  70020   
 69384         -/* Opcode: Transaction P1 P2 * * *
        70021  +/* Opcode: Transaction P1 P2 P3 P4 P5
 69385  70022   **
 69386         -** Begin a transaction.  The transaction ends when a Commit or Rollback
 69387         -** opcode is encountered.  Depending on the ON CONFLICT setting, the
 69388         -** transaction might also be rolled back if an error is encountered.
        70023  +** Begin a transaction on database P1 if a transaction is not already
        70024  +** active.
        70025  +** If P2 is non-zero, then a write-transaction is started, or if a 
        70026  +** read-transaction is already active, it is upgraded to a write-transaction.
        70027  +** If P2 is zero, then a read-transaction is started.
 69389  70028   **
 69390  70029   ** P1 is the index of the database file on which the transaction is
 69391  70030   ** started.  Index 0 is the main database file and index 1 is the
 69392  70031   ** file used for temporary tables.  Indices of 2 or more are used for
 69393  70032   ** attached databases.
 69394  70033   **
 69395         -** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
 69396         -** obtained on the database file when a write-transaction is started.  No
 69397         -** other process can start another write transaction while this transaction is
 69398         -** underway.  Starting a write transaction also creates a rollback journal. A
 69399         -** write transaction must be started before any changes can be made to the
 69400         -** database.  If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
 69401         -** also obtained on the file.
 69402         -**
 69403  70034   ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
 69404  70035   ** true (this flag is set if the Vdbe may modify more than one row and may
 69405  70036   ** throw an ABORT exception), a statement transaction may also be opened.
 69406  70037   ** More specifically, a statement transaction is opened iff the database
 69407  70038   ** connection is currently not in autocommit mode, or if there are other
 69408  70039   ** active statements. A statement transaction allows the changes made by this
 69409  70040   ** VDBE to be rolled back after an error without having to roll back the
 69410  70041   ** entire transaction. If no error is encountered, the statement transaction
 69411  70042   ** will automatically commit when the VDBE halts.
 69412  70043   **
 69413         -** If P2 is zero, then a read-lock is obtained on the database file.
        70044  +** If P5!=0 then this opcode also checks the schema cookie against P3
        70045  +** and the schema generation counter against P4.
        70046  +** The cookie changes its value whenever the database schema changes.
        70047  +** This operation is used to detect when that the cookie has changed
        70048  +** and that the current process needs to reread the schema.  If the schema
        70049  +** cookie in P3 differs from the schema cookie in the database header or
        70050  +** if the schema generation counter in P4 differs from the current
        70051  +** generation counter, then an SQLITE_SCHEMA error is raised and execution
        70052  +** halts.  The sqlite3_step() wrapper function might then reprepare the
        70053  +** statement and rerun it from the beginning.
 69414  70054   */
 69415  70055   case OP_Transaction: {
 69416  70056     Btree *pBt;
        70057  +  int iMeta;
        70058  +  int iGen;
 69417  70059   
 69418  70060     assert( p->bIsReader );
 69419  70061     assert( p->readOnly==0 || pOp->p2==0 );
 69420  70062     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 69421  70063     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 69422  70064     if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
 69423  70065       rc = SQLITE_READONLY;
................................................................................
 69453  70095   
 69454  70096         /* Store the current value of the database handles deferred constraint
 69455  70097         ** counter. If the statement transaction needs to be rolled back,
 69456  70098         ** the value of this counter needs to be restored too.  */
 69457  70099         p->nStmtDefCons = db->nDeferredCons;
 69458  70100         p->nStmtDefImmCons = db->nDeferredImmCons;
 69459  70101       }
        70102  +
        70103  +    /* Gather the schema version number for checking */
        70104  +    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
        70105  +    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
        70106  +  }else{
        70107  +    iGen = iMeta = 0;
        70108  +  }
        70109  +  assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
        70110  +  if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
        70111  +    sqlite3DbFree(db, p->zErrMsg);
        70112  +    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
        70113  +    /* If the schema-cookie from the database file matches the cookie 
        70114  +    ** stored with the in-memory representation of the schema, do
        70115  +    ** not reload the schema from the database file.
        70116  +    **
        70117  +    ** If virtual-tables are in use, this is not just an optimization.
        70118  +    ** Often, v-tables store their data in other SQLite tables, which
        70119  +    ** are queried from within xNext() and other v-table methods using
        70120  +    ** prepared queries. If such a query is out-of-date, we do not want to
        70121  +    ** discard the database schema, as the user code implementing the
        70122  +    ** v-table would have to be ready for the sqlite3_vtab structure itself
        70123  +    ** to be invalidated whenever sqlite3_step() is called from within 
        70124  +    ** a v-table method.
        70125  +    */
        70126  +    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
        70127  +      sqlite3ResetOneSchema(db, pOp->p1);
        70128  +    }
        70129  +    p->expired = 1;
        70130  +    rc = SQLITE_SCHEMA;
 69460  70131     }
 69461  70132     break;
 69462  70133   }
 69463  70134   
 69464  70135   /* Opcode: ReadCookie P1 P2 P3 * *
 69465  70136   **
 69466  70137   ** Read cookie number P3 from database P1 and write it into register P2.
................................................................................
 69524  70195     }
 69525  70196     if( pOp->p1==1 ){
 69526  70197       /* Invalidate all prepared statements whenever the TEMP database
 69527  70198       ** schema is changed.  Ticket #1644 */
 69528  70199       sqlite3ExpirePreparedStatements(db);
 69529  70200       p->expired = 0;
 69530  70201     }
 69531         -  break;
 69532         -}
 69533         -
 69534         -/* Opcode: VerifyCookie P1 P2 P3 * *
 69535         -**
 69536         -** Check the value of global database parameter number 0 (the
 69537         -** schema version) and make sure it is equal to P2 and that the
 69538         -** generation counter on the local schema parse equals P3.
 69539         -**
 69540         -** P1 is the database number which is 0 for the main database file
 69541         -** and 1 for the file holding temporary tables and some higher number
 69542         -** for auxiliary databases.
 69543         -**
 69544         -** The cookie changes its value whenever the database schema changes.
 69545         -** This operation is used to detect when that the cookie has changed
 69546         -** and that the current process needs to reread the schema.
 69547         -**
 69548         -** Either a transaction needs to have been started or an OP_Open needs
 69549         -** to be executed (to establish a read lock) before this opcode is
 69550         -** invoked.
 69551         -*/
 69552         -case OP_VerifyCookie: {
 69553         -  int iMeta;
 69554         -  int iGen;
 69555         -  Btree *pBt;
 69556         -
 69557         -  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 69558         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 69559         -  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 69560         -  assert( p->bIsReader );
 69561         -  pBt = db->aDb[pOp->p1].pBt;
 69562         -  if( pBt ){
 69563         -    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
 69564         -    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
 69565         -  }else{
 69566         -    iGen = iMeta = 0;
 69567         -  }
 69568         -  if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
 69569         -    sqlite3DbFree(db, p->zErrMsg);
 69570         -    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
 69571         -    /* If the schema-cookie from the database file matches the cookie 
 69572         -    ** stored with the in-memory representation of the schema, do
 69573         -    ** not reload the schema from the database file.
 69574         -    **
 69575         -    ** If virtual-tables are in use, this is not just an optimization.
 69576         -    ** Often, v-tables store their data in other SQLite tables, which
 69577         -    ** are queried from within xNext() and other v-table methods using
 69578         -    ** prepared queries. If such a query is out-of-date, we do not want to
 69579         -    ** discard the database schema, as the user code implementing the
 69580         -    ** v-table would have to be ready for the sqlite3_vtab structure itself
 69581         -    ** to be invalidated whenever sqlite3_step() is called from within 
 69582         -    ** a v-table method.
 69583         -    */
 69584         -    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
 69585         -      sqlite3ResetOneSchema(db, pOp->p1);
 69586         -    }
 69587         -
 69588         -    p->expired = 1;
 69589         -    rc = SQLITE_SCHEMA;
 69590         -  }
 69591  70202     break;
 69592  70203   }
 69593  70204   
 69594  70205   /* Opcode: OpenRead P1 P2 P3 P4 P5
 69595  70206   ** Synopsis: root=P2 iDb=P3
 69596  70207   **
 69597  70208   ** Open a read-only cursor for the database table whose root page is
................................................................................
 69800  70411         pCx->isTable = 1;
 69801  70412       }
 69802  70413     }
 69803  70414     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
 69804  70415     break;
 69805  70416   }
 69806  70417   
 69807         -/* Opcode: SorterOpen P1 * * P4 *
        70418  +/* Opcode: SorterOpen P1 P2 * P4 *
 69808  70419   **
 69809  70420   ** This opcode works like OP_OpenEphemeral except that it opens
 69810  70421   ** a transient index that is specifically designed to sort large
 69811  70422   ** tables using an external merge-sort algorithm.
 69812  70423   */
 69813  70424   case OP_SorterOpen: {
 69814  70425     VdbeCursor *pCx;
................................................................................
 69820  70431     pCx->pKeyInfo = pOp->p4.pKeyInfo;
 69821  70432     assert( pCx->pKeyInfo->db==db );
 69822  70433     assert( pCx->pKeyInfo->enc==ENC(db) );
 69823  70434     rc = sqlite3VdbeSorterInit(db, pCx);
 69824  70435     break;
 69825  70436   }
 69826  70437   
 69827         -/* Opcode: OpenPseudo P1 P2 P3 * P5
 69828         -** Synopsis: content in r[P2@P3]
        70438  +/* Opcode: OpenPseudo P1 P2 P3 * *
        70439  +** Synopsis: P3 columns in r[P2]
 69829  70440   **
 69830  70441   ** Open a new cursor that points to a fake table that contains a single
 69831         -** row of data.  The content of that one row in the content of memory
 69832         -** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the 
 69833         -** MEM_Blob content contained in register P2.  When P5==1, then the
 69834         -** row is represented by P3 consecutive registers beginning with P2.
        70442  +** row of data.  The content of that one row is the content of memory
        70443  +** register P2.  In other words, cursor P1 becomes an alias for the 
        70444  +** MEM_Blob content contained in register P2.
 69835  70445   **
 69836  70446   ** A pseudo-table created by this opcode is used to hold a single
 69837  70447   ** row output from the sorter so that the row can be decomposed into
 69838  70448   ** individual columns using the OP_Column opcode.  The OP_Column opcode
 69839  70449   ** is the only cursor opcode that works with a pseudo-table.
 69840  70450   **
 69841  70451   ** P3 is the number of fields in the records that will be stored by
................................................................................
 69847  70457     assert( pOp->p1>=0 );
 69848  70458     assert( pOp->p3>=0 );
 69849  70459     pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
 69850  70460     if( pCx==0 ) goto no_mem;
 69851  70461     pCx->nullRow = 1;
 69852  70462     pCx->pseudoTableReg = pOp->p2;
 69853  70463     pCx->isTable = 1;
 69854         -  pCx->multiPseudo = pOp->p5;
        70464  +  assert( pOp->p5==0 );
 69855  70465     break;
 69856  70466   }
 69857  70467   
 69858  70468   /* Opcode: Close P1 * * * *
 69859  70469   **
 69860  70470   ** Close a cursor previously opened as P1.  If P1 is not
 69861  70471   ** currently open, this instruction is a no-op.
................................................................................
 69919  70529   **
 69920  70530   ** Reposition cursor P1 so that it points to the largest entry that 
 69921  70531   ** is less than or equal to the key value. If there are no records 
 69922  70532   ** less than or equal to the key and P2 is not zero, then jump to P2.
 69923  70533   **
 69924  70534   ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
 69925  70535   */
 69926         -case OP_SeekLt:         /* jump, in3 */
 69927         -case OP_SeekLe:         /* jump, in3 */
 69928         -case OP_SeekGe:         /* jump, in3 */
 69929         -case OP_SeekGt: {       /* jump, in3 */
        70536  +case OP_SeekLT:         /* jump, in3 */
        70537  +case OP_SeekLE:         /* jump, in3 */
        70538  +case OP_SeekGE:         /* jump, in3 */
        70539  +case OP_SeekGT: {       /* jump, in3 */
 69930  70540     int res;
 69931  70541     int oc;
 69932  70542     VdbeCursor *pC;
 69933  70543     UnpackedRecord r;
 69934  70544     int nField;
 69935  70545     i64 iKey;      /* The rowid we are to seek to */
 69936  70546   
 69937  70547     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 69938  70548     assert( pOp->p2!=0 );
 69939  70549     pC = p->apCsr[pOp->p1];
 69940  70550     assert( pC!=0 );
 69941  70551     assert( pC->pseudoTableReg==0 );
 69942         -  assert( OP_SeekLe == OP_SeekLt+1 );
 69943         -  assert( OP_SeekGe == OP_SeekLt+2 );
 69944         -  assert( OP_SeekGt == OP_SeekLt+3 );
        70552  +  assert( OP_SeekLE == OP_SeekLT+1 );
        70553  +  assert( OP_SeekGE == OP_SeekLT+2 );
        70554  +  assert( OP_SeekGT == OP_SeekLT+3 );
 69945  70555     assert( pC->isOrdered );
 69946  70556     assert( pC->pCursor!=0 );
 69947  70557     oc = pOp->opcode;
 69948  70558     pC->nullRow = 0;
 69949  70559     if( pC->isTable ){
 69950  70560       /* The input value in P3 might be of any type: integer, real, string,
 69951  70561       ** blob, or NULL.  But it needs to be an integer before we can do
................................................................................
 69957  70567   
 69958  70568       /* If the P3 value could not be converted into an integer without
 69959  70569       ** loss of information, then special processing is required... */
 69960  70570       if( (pIn3->flags & MEM_Int)==0 ){
 69961  70571         if( (pIn3->flags & MEM_Real)==0 ){
 69962  70572           /* If the P3 value cannot be converted into any kind of a number,
 69963  70573           ** then the seek is not possible, so jump to P2 */
 69964         -        pc = pOp->p2 - 1;
        70574  +        pc = pOp->p2 - 1;  VdbeBranchTaken(1,2);
 69965  70575           break;
 69966  70576         }
 69967  70577   
 69968  70578         /* If the approximation iKey is larger than the actual real search
 69969  70579         ** term, substitute >= for > and < for <=. e.g. if the search term
 69970  70580         ** is 4.9 and the integer approximation 5:
 69971  70581         **
 69972  70582         **        (x >  4.9)    ->     (x >= 5)
 69973  70583         **        (x <= 4.9)    ->     (x <  5)
 69974  70584         */
 69975  70585         if( pIn3->r<(double)iKey ){
 69976         -        assert( OP_SeekGe==(OP_SeekGt-1) );
 69977         -        assert( OP_SeekLt==(OP_SeekLe-1) );
 69978         -        assert( (OP_SeekLe & 0x0001)==(OP_SeekGt & 0x0001) );
 69979         -        if( (oc & 0x0001)==(OP_SeekGt & 0x0001) ) oc--;
        70586  +        assert( OP_SeekGE==(OP_SeekGT-1) );
        70587  +        assert( OP_SeekLT==(OP_SeekLE-1) );
        70588  +        assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
        70589  +        if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
 69980  70590         }
 69981  70591   
 69982  70592         /* If the approximation iKey is smaller than the actual real search
 69983  70593         ** term, substitute <= for < and > for >=.  */
 69984  70594         else if( pIn3->r>(double)iKey ){
 69985         -        assert( OP_SeekLe==(OP_SeekLt+1) );
 69986         -        assert( OP_SeekGt==(OP_SeekGe+1) );
 69987         -        assert( (OP_SeekLt & 0x0001)==(OP_SeekGe & 0x0001) );
 69988         -        if( (oc & 0x0001)==(OP_SeekLt & 0x0001) ) oc++;
        70595  +        assert( OP_SeekLE==(OP_SeekLT+1) );
        70596  +        assert( OP_SeekGT==(OP_SeekGE+1) );
        70597  +        assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
        70598  +        if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
 69989  70599         }
 69990  70600       } 
 69991  70601       rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
 69992  70602       if( rc!=SQLITE_OK ){
 69993  70603         goto abort_due_to_error;
 69994  70604       }
 69995  70605       if( res==0 ){
................................................................................
 70000  70610       nField = pOp->p4.i;
 70001  70611       assert( pOp->p4type==P4_INT32 );
 70002  70612       assert( nField>0 );
 70003  70613       r.pKeyInfo = pC->pKeyInfo;
 70004  70614       r.nField = (u16)nField;
 70005  70615   
 70006  70616       /* The next line of code computes as follows, only faster:
 70007         -    **   if( oc==OP_SeekGt || oc==OP_SeekLe ){
 70008         -    **     r.flags = UNPACKED_INCRKEY;
        70617  +    **   if( oc==OP_SeekGT || oc==OP_SeekLE ){
        70618  +    **     r.default_rc = -1;
 70009  70619       **   }else{
 70010         -    **     r.flags = 0;
        70620  +    **     r.default_rc = +1;
 70011  70621       **   }
 70012  70622       */
 70013         -    r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
 70014         -    assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
 70015         -    assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
 70016         -    assert( oc!=OP_SeekGe || r.flags==0 );
 70017         -    assert( oc!=OP_SeekLt || r.flags==0 );
        70623  +    r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
        70624  +    assert( oc!=OP_SeekGT || r.default_rc==-1 );
        70625  +    assert( oc!=OP_SeekLE || r.default_rc==-1 );
        70626  +    assert( oc!=OP_SeekGE || r.default_rc==+1 );
        70627  +    assert( oc!=OP_SeekLT || r.default_rc==+1 );
 70018  70628   
 70019  70629       r.aMem = &aMem[pOp->p3];
 70020  70630   #ifdef SQLITE_DEBUG
 70021  70631       { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 70022  70632   #endif
 70023  70633       ExpandBlob(r.aMem);
 70024  70634       rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
................................................................................
 70028  70638       pC->rowidIsValid = 0;
 70029  70639     }
 70030  70640     pC->deferredMoveto = 0;
 70031  70641     pC->cacheStatus = CACHE_STALE;
 70032  70642   #ifdef SQLITE_TEST
 70033  70643     sqlite3_search_count++;
 70034  70644   #endif
 70035         -  if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
 70036         -    if( res<0 || (res==0 && oc==OP_SeekGt) ){
        70645  +  if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
        70646  +    if( res<0 || (res==0 && oc==OP_SeekGT) ){
        70647  +      res = 0;
 70037  70648         rc = sqlite3BtreeNext(pC->pCursor, &res);
 70038  70649         if( rc!=SQLITE_OK ) goto abort_due_to_error;
 70039  70650         pC->rowidIsValid = 0;
 70040  70651       }else{
 70041  70652         res = 0;
 70042  70653       }
 70043  70654     }else{
 70044         -    assert( oc==OP_SeekLt || oc==OP_SeekLe );
 70045         -    if( res>0 || (res==0 && oc==OP_SeekLt) ){
        70655  +    assert( oc==OP_SeekLT || oc==OP_SeekLE );
        70656  +    if( res>0 || (res==0 && oc==OP_SeekLT) ){
        70657  +      res = 0;
 70046  70658         rc = sqlite3BtreePrevious(pC->pCursor, &res);
 70047  70659         if( rc!=SQLITE_OK ) goto abort_due_to_error;
 70048  70660         pC->rowidIsValid = 0;
 70049  70661       }else{
 70050  70662         /* res might be negative because the table is empty.  Check to
 70051  70663         ** see if this is the case.
 70052  70664         */
 70053  70665         res = sqlite3BtreeEof(pC->pCursor);
 70054  70666       }
 70055  70667     }
 70056  70668     assert( pOp->p2>0 );
        70669  +  VdbeBranchTaken(res!=0,2);
 70057  70670     if( res ){
 70058  70671       pc = pOp->p2 - 1;
 70059  70672     }
 70060  70673     break;
 70061  70674   }
 70062  70675   
 70063  70676   /* Opcode: Seek P1 P2 * * *
................................................................................
 70158  70771     assert( pC->pCursor!=0 );
 70159  70772     assert( pC->isTable==0 );
 70160  70773     pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
 70161  70774     if( pOp->p4.i>0 ){
 70162  70775       r.pKeyInfo = pC->pKeyInfo;
 70163  70776       r.nField = (u16)pOp->p4.i;
 70164  70777       r.aMem = pIn3;
        70778  +    for(ii=0; ii<r.nField; ii++){
        70779  +      assert( memIsValid(&r.aMem[ii]) );
        70780  +      ExpandBlob(&r.aMem[ii]);
 70165  70781   #ifdef SQLITE_DEBUG
 70166         -    {
 70167         -      int i;
 70168         -      for(i=0; i<r.nField; i++){
 70169         -        assert( memIsValid(&r.aMem[i]) );
 70170         -        if( i ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
 70171         -      }
        70782  +      if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
        70783  +#endif
 70172  70784       }
 70173         -#endif
 70174         -    r.flags = UNPACKED_PREFIX_MATCH;
 70175  70785       pIdxKey = &r;
 70176  70786     }else{
 70177  70787       pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 70178  70788           pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
 70179  70789       ); 
 70180  70790       if( pIdxKey==0 ) goto no_mem;
 70181  70791       assert( pIn3->flags & MEM_Blob );
 70182  70792       assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
 70183  70793       sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
 70184         -    pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
 70185  70794     }
        70795  +  pIdxKey->default_rc = 0;
 70186  70796     if( pOp->opcode==OP_NoConflict ){
 70187  70797       /* For the OP_NoConflict opcode, take the jump if any of the
 70188  70798       ** input fields are NULL, since any key with a NULL will not
 70189  70799       ** conflict */
 70190  70800       for(ii=0; ii<r.nField; ii++){
 70191  70801         if( r.aMem[ii].flags & MEM_Null ){
 70192         -        pc = pOp->p2 - 1;
        70802  +        pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
 70193  70803           break;
 70194  70804         }
 70195  70805       }
 70196  70806     }
 70197  70807     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
 70198  70808     if( pOp->p4.i==0 ){
 70199  70809       sqlite3DbFree(db, pFree);
................................................................................
 70203  70813     }
 70204  70814     pC->seekResult = res;
 70205  70815     alreadyExists = (res==0);
 70206  70816     pC->nullRow = 1-alreadyExists;
 70207  70817     pC->deferredMoveto = 0;
 70208  70818     pC->cacheStatus = CACHE_STALE;
 70209  70819     if( pOp->opcode==OP_Found ){
        70820  +    VdbeBranchTaken(alreadyExists!=0,2);
 70210  70821       if( alreadyExists ) pc = pOp->p2 - 1;
 70211  70822     }else{
        70823  +    VdbeBranchTaken(alreadyExists==0,2);
 70212  70824       if( !alreadyExists ) pc = pOp->p2 - 1;
 70213  70825     }
 70214  70826     break;
 70215  70827   }
 70216  70828   
 70217  70829   /* Opcode: NotExists P1 P2 P3 * *
 70218  70830   ** Synopsis: intkey=r[P3]
................................................................................
 70247  70859     iKey = pIn3->u.i;
 70248  70860     rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
 70249  70861     pC->lastRowid = pIn3->u.i;
 70250  70862     pC->rowidIsValid = res==0 ?1:0;
 70251  70863     pC->nullRow = 0;
 70252  70864     pC->cacheStatus = CACHE_STALE;
 70253  70865     pC->deferredMoveto = 0;
        70866  +  VdbeBranchTaken(res!=0,2);
 70254  70867     if( res!=0 ){
 70255  70868       pc = pOp->p2 - 1;
 70256  70869       assert( pC->rowidIsValid==0 );
 70257  70870     }
 70258  70871     pC->seekResult = res;
 70259  70872     break;
 70260  70873   }
................................................................................
 70328  70941       ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
 70329  70942       ** to provide the constant while making all compilers happy.
 70330  70943       */
 70331  70944   #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
 70332  70945   #endif
 70333  70946   
 70334  70947       if( !pC->useRandomRowid ){
 70335         -      v = sqlite3BtreeGetCachedRowid(pC->pCursor);
 70336         -      if( v==0 ){
 70337         -        rc = sqlite3BtreeLast(pC->pCursor, &res);
 70338         -        if( rc!=SQLITE_OK ){
 70339         -          goto abort_due_to_error;
 70340         -        }
 70341         -        if( res ){
 70342         -          v = 1;   /* IMP: R-61914-48074 */
        70948  +      rc = sqlite3BtreeLast(pC->pCursor, &res);
        70949  +      if( rc!=SQLITE_OK ){
        70950  +        goto abort_due_to_error;
        70951  +      }
        70952  +      if( res ){
        70953  +        v = 1;   /* IMP: R-61914-48074 */
        70954  +      }else{
        70955  +        assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
        70956  +        rc = sqlite3BtreeKeySize(pC->pCursor, &v);
        70957  +        assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
        70958  +        if( v>=MAX_ROWID ){
        70959  +          pC->useRandomRowid = 1;
 70343  70960           }else{
 70344         -          assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
 70345         -          rc = sqlite3BtreeKeySize(pC->pCursor, &v);
 70346         -          assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
 70347         -          if( v>=MAX_ROWID ){
 70348         -            pC->useRandomRowid = 1;
 70349         -          }else{
 70350         -            v++;   /* IMP: R-29538-34987 */
 70351         -          }
        70961  +          v++;   /* IMP: R-29538-34987 */
 70352  70962           }
 70353  70963         }
        70964  +    }
 70354  70965   
 70355  70966   #ifndef SQLITE_OMIT_AUTOINCREMENT
 70356         -      if( pOp->p3 ){
        70967  +    if( pOp->p3 ){
        70968  +      /* Assert that P3 is a valid memory cell. */
        70969  +      assert( pOp->p3>0 );
        70970  +      if( p->pFrame ){
        70971  +        for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
        70972  +        /* Assert that P3 is a valid memory cell. */
        70973  +        assert( pOp->p3<=pFrame->nMem );
        70974  +        pMem = &pFrame->aMem[pOp->p3];
        70975  +      }else{
 70357  70976           /* Assert that P3 is a valid memory cell. */
 70358         -        assert( pOp->p3>0 );
 70359         -        if( p->pFrame ){
 70360         -          for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 70361         -          /* Assert that P3 is a valid memory cell. */
 70362         -          assert( pOp->p3<=pFrame->nMem );
 70363         -          pMem = &pFrame->aMem[pOp->p3];
 70364         -        }else{
 70365         -          /* Assert that P3 is a valid memory cell. */
 70366         -          assert( pOp->p3<=(p->nMem-p->nCursor) );
 70367         -          pMem = &aMem[pOp->p3];
 70368         -          memAboutToChange(p, pMem);
 70369         -        }
 70370         -        assert( memIsValid(pMem) );
 70371         -
 70372         -        REGISTER_TRACE(pOp->p3, pMem);
 70373         -        sqlite3VdbeMemIntegerify(pMem);
 70374         -        assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
 70375         -        if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
 70376         -          rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
 70377         -          goto abort_due_to_error;
 70378         -        }
 70379         -        if( v<pMem->u.i+1 ){
 70380         -          v = pMem->u.i + 1;
 70381         -        }
 70382         -        pMem->u.i = v;
 70383         -      }
 70384         -#endif
 70385         -
 70386         -      sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
 70387         -    }
        70977  +        assert( pOp->p3<=(p->nMem-p->nCursor) );
        70978  +        pMem = &aMem[pOp->p3];
        70979  +        memAboutToChange(p, pMem);
        70980  +      }
        70981  +      assert( memIsValid(pMem) );
        70982  +
        70983  +      REGISTER_TRACE(pOp->p3, pMem);
        70984  +      sqlite3VdbeMemIntegerify(pMem);
        70985  +      assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
        70986  +      if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
        70987  +        rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
        70988  +        goto abort_due_to_error;
        70989  +      }
        70990  +      if( v<pMem->u.i+1 ){
        70991  +        v = pMem->u.i + 1;
        70992  +      }
        70993  +      pMem->u.i = v;
        70994  +    }
        70995  +#endif
 70388  70996       if( pC->useRandomRowid ){
 70389  70997         /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
 70390  70998         ** largest possible integer (9223372036854775807) then the database
 70391  70999         ** engine starts picking positive candidate ROWIDs at random until
 70392  71000         ** it finds one that is not previously used. */
 70393  71001         assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
 70394  71002                                ** an AUTOINCREMENT table. */
................................................................................
 70514  71122     }
 70515  71123     seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
 70516  71124     if( pData->flags & MEM_Zero ){
 70517  71125       nZero = pData->u.nZero;
 70518  71126     }else{
 70519  71127       nZero = 0;
 70520  71128     }
 70521         -  sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
 70522  71129     rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
 70523  71130                             pData->z, pData->n, nZero,
 70524  71131                             (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
 70525  71132     );
 70526  71133     pC->rowidIsValid = 0;
 70527  71134     pC->deferredMoveto = 0;
 70528  71135     pC->cacheStatus = CACHE_STALE;
................................................................................
 70576  71183     ** below is always a no-op and cannot fail.  We will run it anyhow, though,
 70577  71184     ** to guard against future changes to the code generator.
 70578  71185     **/
 70579  71186     assert( pC->deferredMoveto==0 );
 70580  71187     rc = sqlite3VdbeCursorMoveto(pC);
 70581  71188     if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 70582  71189   
 70583         -  sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
 70584  71190     rc = sqlite3BtreeDelete(pC->pCursor);
 70585  71191     pC->cacheStatus = CACHE_STALE;
 70586  71192   
 70587  71193     /* Invoke the update-hook if required. */
 70588  71194     if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
 70589  71195       db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
 70590  71196                           db->aDb[pC->iDb].zName, pOp->p4.z, iKey);
................................................................................
 70628  71234   
 70629  71235     pC = p->apCsr[pOp->p1];
 70630  71236     assert( isSorter(pC) );
 70631  71237     assert( pOp->p4type==P4_INT32 );
 70632  71238     pIn3 = &aMem[pOp->p3];
 70633  71239     nIgnore = pOp->p4.i;
 70634  71240     rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
        71241  +  VdbeBranchTaken(res!=0,2);
 70635  71242     if( res ){
 70636  71243       pc = pOp->p2-1;
 70637  71244     }
 70638  71245     break;
 70639  71246   };
 70640  71247   
 70641  71248   /* Opcode: SorterData P1 P2 * * *
................................................................................
 70665  71272   ** of a real table, not a pseudo-table.
 70666  71273   */
 70667  71274   /* Opcode: RowKey P1 P2 * * *
 70668  71275   ** Synopsis: r[P2]=key
 70669  71276   **
 70670  71277   ** Write into register P2 the complete row key for cursor P1.
 70671  71278   ** There is no interpretation of the data.  
 70672         -** The key is copied onto the P3 register exactly as 
        71279  +** The key is copied onto the P2 register exactly as 
 70673  71280   ** it is found in the database file.
 70674  71281   **
 70675  71282   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 70676  71283   ** of a real table, not a pseudo-table.
 70677  71284   */
 70678  71285   case OP_RowKey:
 70679  71286   case OP_RowData: {
................................................................................
 70827  71434     res = 0;
 70828  71435     assert( pCrsr!=0 );
 70829  71436     rc = sqlite3BtreeLast(pCrsr, &res);
 70830  71437     pC->nullRow = (u8)res;
 70831  71438     pC->deferredMoveto = 0;
 70832  71439     pC->rowidIsValid = 0;
 70833  71440     pC->cacheStatus = CACHE_STALE;
 70834         -  if( pOp->p2>0 && res ){
 70835         -    pc = pOp->p2 - 1;
        71441  +  if( pOp->p2>0 ){
        71442  +    VdbeBranchTaken(res!=0,2);
        71443  +    if( res ) pc = pOp->p2 - 1;
 70836  71444     }
 70837  71445     break;
 70838  71446   }
 70839  71447   
 70840  71448   
 70841  71449   /* Opcode: Sort P1 P2 * * *
 70842  71450   **
................................................................................
 70885  71493       rc = sqlite3BtreeFirst(pCrsr, &res);
 70886  71494       pC->deferredMoveto = 0;
 70887  71495       pC->cacheStatus = CACHE_STALE;
 70888  71496       pC->rowidIsValid = 0;
 70889  71497     }
 70890  71498     pC->nullRow = (u8)res;
 70891  71499     assert( pOp->p2>0 && pOp->p2<p->nOp );
        71500  +  VdbeBranchTaken(res!=0,2);
 70892  71501     if( res ){
 70893  71502       pc = pOp->p2 - 1;
 70894  71503     }
 70895  71504     break;
 70896  71505   }
 70897  71506   
 70898         -/* Opcode: Next P1 P2 * * P5
        71507  +/* Opcode: Next P1 P2 P3 P4 P5
 70899  71508   **
 70900  71509   ** Advance cursor P1 so that it points to the next key/data pair in its
 70901  71510   ** table or index.  If there are no more key/value pairs then fall through
 70902  71511   ** to the following instruction.  But if the cursor advance was successful,
 70903  71512   ** jump immediately to P2.
 70904  71513   **
 70905  71514   ** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
 70906  71515   ** been opened prior to this opcode or the program will segfault.
        71516  +**
        71517  +** The P3 value is a hint to the btree implementation. If P3==1, that
        71518  +** means P1 is an SQL index and that this instruction could have been
        71519  +** omitted if that index had been unique.  P3 is usually 0.  P3 is
        71520  +** always either 0 or 1.
 70907  71521   **
 70908  71522   ** P4 is always of type P4_ADVANCE. The function pointer points to
 70909  71523   ** sqlite3BtreeNext().
 70910  71524   **
 70911  71525   ** If P5 is positive and the jump is taken, then event counter
 70912  71526   ** number P5-1 in the prepared statement is incremented.
 70913  71527   **
 70914  71528   ** See also: Prev, NextIfOpen
 70915  71529   */
 70916         -/* Opcode: NextIfOpen P1 P2 * * P5
        71530  +/* Opcode: NextIfOpen P1 P2 P3 P4 P5
 70917  71531   **
 70918  71532   ** This opcode works just like OP_Next except that if cursor P1 is not
 70919  71533   ** open it behaves a no-op.
 70920  71534   */
 70921         -/* Opcode: Prev P1 P2 * * P5
        71535  +/* Opcode: Prev P1 P2 P3 P4 P5
 70922  71536   **
 70923  71537   ** Back up cursor P1 so that it points to the previous key/data pair in its
 70924  71538   ** table or index.  If there is no previous key/value pairs then fall through
 70925  71539   ** to the following instruction.  But if the cursor backup was successful,
 70926  71540   ** jump immediately to P2.
 70927  71541   **
 70928  71542   ** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
 70929  71543   ** not open then the behavior is undefined.
        71544  +**
        71545  +** The P3 value is a hint to the btree implementation. If P3==1, that
        71546  +** means P1 is an SQL index and that this instruction could have been
        71547  +** omitted if that index had been unique.  P3 is usually 0.  P3 is
        71548  +** always either 0 or 1.
 70930  71549   **
 70931  71550   ** P4 is always of type P4_ADVANCE. The function pointer points to
 70932  71551   ** sqlite3BtreePrevious().
 70933  71552   **
 70934  71553   ** If P5 is positive and the jump is taken, then event counter
 70935  71554   ** number P5-1 in the prepared statement is incremented.
 70936  71555   */
 70937         -/* Opcode: PrevIfOpen P1 P2 * * P5
        71556  +/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
 70938  71557   **
 70939  71558   ** This opcode works just like OP_Prev except that if cursor P1 is not
 70940  71559   ** open it behaves a no-op.
 70941  71560   */
 70942  71561   case OP_SorterNext: {  /* jump */
 70943  71562     VdbeCursor *pC;
 70944  71563     int res;
................................................................................
 70952  71571     if( p->apCsr[pOp->p1]==0 ) break;
 70953  71572     /* Fall through */
 70954  71573   case OP_Prev:          /* jump */
 70955  71574   case OP_Next:          /* jump */
 70956  71575     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70957  71576     assert( pOp->p5<ArraySize(p->aCounter) );
 70958  71577     pC = p->apCsr[pOp->p1];
        71578  +  res = pOp->p3;
 70959  71579     assert( pC!=0 );
 70960  71580     assert( pC->deferredMoveto==0 );
 70961  71581     assert( pC->pCursor );
        71582  +  assert( res==0 || (res==1 && pC->isTable==0) );
        71583  +  testcase( res==1 );
 70962  71584     assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
 70963  71585     assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
 70964  71586     assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
 70965  71587     assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
 70966  71588     rc = pOp->p4.xAdvance(pC->pCursor, &res);
 70967  71589   next_tail:
 70968  71590     pC->cacheStatus = CACHE_STALE;
        71591  +  VdbeBranchTaken(res==0,2);
 70969  71592     if( res==0 ){
 70970  71593       pC->nullRow = 0;
 70971  71594       pc = pOp->p2 - 1;
 70972  71595       p->aCounter[pOp->p5]++;
 70973  71596   #ifdef SQLITE_TEST
 70974  71597       sqlite3_search_count++;
 70975  71598   #endif
................................................................................
 70985  71608   **
 70986  71609   ** Register P2 holds an SQL index key made using the
 70987  71610   ** MakeRecord instructions.  This opcode writes that key
 70988  71611   ** into the index P1.  Data for the entry is nil.
 70989  71612   **
 70990  71613   ** P3 is a flag that provides a hint to the b-tree layer that this
 70991  71614   ** insert is likely to be an append.
        71615  +**
        71616  +** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
        71617  +** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
        71618  +** then the change counter is unchanged.
        71619  +**
        71620  +** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
        71621  +** just done a seek to the spot where the new entry is to be inserted.
        71622  +** This flag avoids doing an extra seek.
 70992  71623   **
 70993  71624   ** This instruction only works for indices.  The equivalent instruction
 70994  71625   ** for tables is OP_Insert.
 70995  71626   */
 70996  71627   case OP_SorterInsert:       /* in2 */
 70997  71628   case OP_IdxInsert: {        /* in2 */
 70998  71629     VdbeCursor *pC;
................................................................................
 71046  71677     pC = p->apCsr[pOp->p1];
 71047  71678     assert( pC!=0 );
 71048  71679     pCrsr = pC->pCursor;
 71049  71680     assert( pCrsr!=0 );
 71050  71681     assert( pOp->p5==0 );
 71051  71682     r.pKeyInfo = pC->pKeyInfo;
 71052  71683     r.nField = (u16)pOp->p3;
 71053         -  r.flags = UNPACKED_PREFIX_MATCH;
        71684  +  r.default_rc = 0;
 71054  71685     r.aMem = &aMem[pOp->p2];
 71055  71686   #ifdef SQLITE_DEBUG
 71056  71687     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 71057  71688   #endif
 71058  71689     rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
 71059  71690     if( rc==SQLITE_OK && res==0 ){
 71060  71691       rc = sqlite3BtreeDelete(pCrsr);
................................................................................
 71100  71731     break;
 71101  71732   }
 71102  71733   
 71103  71734   /* Opcode: IdxGE P1 P2 P3 P4 P5
 71104  71735   ** Synopsis: key=r[P3@P4]
 71105  71736   **
 71106  71737   ** The P4 register values beginning with P3 form an unpacked index 
 71107         -** key that omits the ROWID.  Compare this key value against the index 
 71108         -** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
        71738  +** key that omits the PRIMARY KEY.  Compare this key value against the index 
        71739  +** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 
        71740  +** fields at the end.
 71109  71741   **
 71110  71742   ** If the P1 index entry is greater than or equal to the key value
 71111  71743   ** then jump to P2.  Otherwise fall through to the next instruction.
        71744  +*/
        71745  +/* Opcode: IdxGT P1 P2 P3 P4 P5
        71746  +** Synopsis: key=r[P3@P4]
 71112  71747   **
 71113         -** If P5 is non-zero then the key value is increased by an epsilon 
 71114         -** prior to the comparison.  This make the opcode work like IdxGT except
 71115         -** that if the key from register P3 is a prefix of the key in the cursor,
 71116         -** the result is false whereas it would be true with IdxGT.
        71748  +** The P4 register values beginning with P3 form an unpacked index 
        71749  +** key that omits the PRIMARY KEY.  Compare this key value against the index 
        71750  +** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 
        71751  +** fields at the end.
        71752  +**
        71753  +** If the P1 index entry is greater than the key value
        71754  +** then jump to P2.  Otherwise fall through to the next instruction.
 71117  71755   */
 71118  71756   /* Opcode: IdxLT P1 P2 P3 P4 P5
 71119  71757   ** Synopsis: key=r[P3@P4]
 71120  71758   **
 71121  71759   ** The P4 register values beginning with P3 form an unpacked index 
 71122         -** key that omits the ROWID.  Compare this key value against the index 
 71123         -** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
        71760  +** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
        71761  +** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
        71762  +** ROWID on the P1 index.
 71124  71763   **
 71125  71764   ** If the P1 index entry is less than the key value then jump to P2.
 71126  71765   ** Otherwise fall through to the next instruction.
        71766  +*/
        71767  +/* Opcode: IdxLE P1 P2 P3 P4 P5
        71768  +** Synopsis: key=r[P3@P4]
 71127  71769   **
 71128         -** If P5 is non-zero then the key value is increased by an epsilon prior 
 71129         -** to the comparison.  This makes the opcode work like IdxLE.
        71770  +** The P4 register values beginning with P3 form an unpacked index 
        71771  +** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
        71772  +** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
        71773  +** ROWID on the P1 index.
        71774  +**
        71775  +** If the P1 index entry is less than or equal to the key value then jump
        71776  +** to P2. Otherwise fall through to the next instruction.
 71130  71777   */
        71778  +case OP_IdxLE:          /* jump */
        71779  +case OP_IdxGT:          /* jump */
 71131  71780   case OP_IdxLT:          /* jump */
 71132         -case OP_IdxGE: {        /* jump */
        71781  +case OP_IdxGE:  {       /* jump */
 71133  71782     VdbeCursor *pC;
 71134  71783     int res;
 71135  71784     UnpackedRecord r;
 71136  71785   
 71137  71786     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71138  71787     pC = p->apCsr[pOp->p1];
 71139  71788     assert( pC!=0 );
................................................................................
 71140  71789     assert( pC->isOrdered );
 71141  71790     assert( pC->pCursor!=0);
 71142  71791     assert( pC->deferredMoveto==0 );
 71143  71792     assert( pOp->p5==0 || pOp->p5==1 );
 71144  71793     assert( pOp->p4type==P4_INT32 );
 71145  71794     r.pKeyInfo = pC->pKeyInfo;
 71146  71795     r.nField = (u16)pOp->p4.i;
 71147         -  if( pOp->p5 ){
 71148         -    r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
        71796  +  if( pOp->opcode<OP_IdxLT ){
        71797  +    assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
        71798  +    r.default_rc = -1;
 71149  71799     }else{
 71150         -    r.flags = UNPACKED_PREFIX_MATCH;
        71800  +    assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
        71801  +    r.default_rc = 0;
 71151  71802     }
 71152  71803     r.aMem = &aMem[pOp->p3];
 71153  71804   #ifdef SQLITE_DEBUG
 71154  71805     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 71155  71806   #endif
 71156  71807     res = 0;  /* Not needed.  Only used to silence a warning. */
 71157  71808     rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
 71158         -  if( pOp->opcode==OP_IdxLT ){
        71809  +  assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
        71810  +  if( (pOp->opcode&1)==(OP_IdxLT&1) ){
        71811  +    assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
 71159  71812       res = -res;
 71160  71813     }else{
 71161         -    assert( pOp->opcode==OP_IdxGE );
        71814  +    assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
 71162  71815       res++;
 71163  71816     }
        71817  +  VdbeBranchTaken(res>0,2);
 71164  71818     if( res>0 ){
 71165  71819       pc = pOp->p2 - 1 ;
 71166  71820     }
 71167  71821     break;
 71168  71822   }
 71169  71823   
 71170  71824   /* Opcode: Destroy P1 P2 P3 * *
................................................................................
 71249  71903   ** See also: Destroy
 71250  71904   */
 71251  71905   case OP_Clear: {
 71252  71906     int nChange;
 71253  71907    
 71254  71908     nChange = 0;
 71255  71909     assert( p->readOnly==0 );
 71256         -  assert( pOp->p1!=1 );
 71257  71910     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
 71258  71911     rc = sqlite3BtreeClearTable(
 71259  71912         db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
 71260  71913     );
 71261  71914     if( pOp->p3 ){
 71262  71915       p->nChange += nChange;
 71263  71916       if( pOp->p3>0 ){
................................................................................
 71518  72171     pIn1 = &aMem[pOp->p1];
 71519  72172     if( (pIn1->flags & MEM_RowSet)==0 
 71520  72173      || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
 71521  72174     ){
 71522  72175       /* The boolean index is empty */
 71523  72176       sqlite3VdbeMemSetNull(pIn1);
 71524  72177       pc = pOp->p2 - 1;
        72178  +    VdbeBranchTaken(1,2);
 71525  72179     }else{
 71526  72180       /* A value was pulled from the index */
 71527  72181       sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
        72182  +    VdbeBranchTaken(0,2);
 71528  72183     }
 71529  72184     goto check_for_interrupt;
 71530  72185   }
 71531  72186   
 71532  72187   /* Opcode: RowSetTest P1 P2 P3 P4
 71533  72188   ** Synopsis: if r[P3] in rowset(P1) goto P2
 71534  72189   **
................................................................................
 71572  72227   
 71573  72228     assert( pOp->p4type==P4_INT32 );
 71574  72229     assert( iSet==-1 || iSet>=0 );
 71575  72230     if( iSet ){
 71576  72231       exists = sqlite3RowSetTest(pIn1->u.pRowSet, 
 71577  72232                                  (u8)(iSet>=0 ? iSet & 0xf : 0xff),
 71578  72233                                  pIn3->u.i);
        72234  +    VdbeBranchTaken(exists!=0,2);
 71579  72235       if( exists ){
 71580  72236         pc = pOp->p2 - 1;
 71581  72237         break;
 71582  72238       }
 71583  72239     }
 71584  72240     if( iSet>=0 ){
 71585  72241       sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
................................................................................
 71586  72242     }
 71587  72243     break;
 71588  72244   }
 71589  72245   
 71590  72246   
 71591  72247   #ifndef SQLITE_OMIT_TRIGGER
 71592  72248   
 71593         -/* Opcode: Program P1 P2 P3 P4 *
        72249  +/* Opcode: Program P1 P2 P3 P4 P5
 71594  72250   **
 71595  72251   ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
 71596  72252   **
 71597  72253   ** P1 contains the address of the memory cell that contains the first memory 
 71598  72254   ** cell in an array of values used as arguments to the sub-program. P2 
 71599  72255   ** contains the address to jump to if the sub-program throws an IGNORE 
 71600  72256   ** exception using the RAISE() function. Register P3 contains the address 
 71601  72257   ** of a memory cell in this (the parent) VM that is used to allocate the 
 71602  72258   ** memory required by the sub-vdbe at runtime.
 71603  72259   **
 71604  72260   ** P4 is a pointer to the VM containing the trigger program.
        72261  +**
        72262  +** If P5 is non-zero, then recursive program invocation is enabled.
 71605  72263   */
 71606  72264   case OP_Program: {        /* jump */
 71607  72265     int nMem;               /* Number of memory registers for sub-program */
 71608  72266     int nByte;              /* Bytes of runtime space required for sub-program */
 71609  72267     Mem *pRt;               /* Register to allocate runtime space */
 71610  72268     Mem *pMem;              /* Used to iterate through memory cells */
 71611  72269     Mem *pEnd;              /* Last memory cell in new array */
................................................................................
 71675  72333       pFrame->nOp = p->nOp;
 71676  72334       pFrame->token = pProgram->token;
 71677  72335       pFrame->aOnceFlag = p->aOnceFlag;
 71678  72336       pFrame->nOnceFlag = p->nOnceFlag;
 71679  72337   
 71680  72338       pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
 71681  72339       for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
 71682         -      pMem->flags = MEM_Invalid;
        72340  +      pMem->flags = MEM_Undefined;
 71683  72341         pMem->db = db;
 71684  72342       }
 71685  72343     }else{
 71686  72344       pFrame = pRt->u.pFrame;
 71687  72345       assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
 71688  72346       assert( pProgram->nCsr==pFrame->nChildCsr );
 71689  72347       assert( pc==pFrame->pc );
................................................................................
 71762  72420   ** If P1 is non-zero, then the jump is taken if the database constraint-counter
 71763  72421   ** is zero (the one that counts deferred constraint violations). If P1 is
 71764  72422   ** zero, the jump is taken if the statement constraint-counter is zero
 71765  72423   ** (immediate foreign key constraint violations).
 71766  72424   */
 71767  72425   case OP_FkIfZero: {         /* jump */
 71768  72426     if( pOp->p1 ){
        72427  +    VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
 71769  72428       if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
 71770  72429     }else{
        72430  +    VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
 71771  72431       if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
 71772  72432     }
 71773  72433     break;
 71774  72434   }
 71775  72435   #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
 71776  72436   
 71777  72437   #ifndef SQLITE_OMIT_AUTOINCREMENT
................................................................................
 71812  72472   **
 71813  72473   ** It is illegal to use this instruction on a register that does
 71814  72474   ** not contain an integer.  An assertion fault will result if you try.
 71815  72475   */
 71816  72476   case OP_IfPos: {        /* jump, in1 */
 71817  72477     pIn1 = &aMem[pOp->p1];
 71818  72478     assert( pIn1->flags&MEM_Int );
        72479  +  VdbeBranchTaken( pIn1->u.i>0, 2);
 71819  72480     if( pIn1->u.i>0 ){
 71820  72481        pc = pOp->p2 - 1;
 71821  72482     }
 71822  72483     break;
 71823  72484   }
 71824  72485   
 71825  72486   /* Opcode: IfNeg P1 P2 * * *
................................................................................
 71829  72490   **
 71830  72491   ** It is illegal to use this instruction on a register that does
 71831  72492   ** not contain an integer.  An assertion fault will result if you try.
 71832  72493   */
 71833  72494   case OP_IfNeg: {        /* jump, in1 */
 71834  72495     pIn1 = &aMem[pOp->p1];
 71835  72496     assert( pIn1->flags&MEM_Int );
        72497  +  VdbeBranchTaken(pIn1->u.i<0, 2);
 71836  72498     if( pIn1->u.i<0 ){
 71837  72499        pc = pOp->p2 - 1;
 71838  72500     }
 71839  72501     break;
 71840  72502   }
 71841  72503   
 71842  72504   /* Opcode: IfZero P1 P2 P3 * *
................................................................................
 71848  72510   ** It is illegal to use this instruction on a register that does
 71849  72511   ** not contain an integer.  An assertion fault will result if you try.
 71850  72512   */
 71851  72513   case OP_IfZero: {        /* jump, in1 */
 71852  72514     pIn1 = &aMem[pOp->p1];
 71853  72515     assert( pIn1->flags&MEM_Int );
 71854  72516     pIn1->u.i += pOp->p3;
        72517  +  VdbeBranchTaken(pIn1->u.i==0, 2);
 71855  72518     if( pIn1->u.i==0 ){
 71856  72519        pc = pOp->p2 - 1;
 71857  72520     }
 71858  72521     break;
 71859  72522   }
 71860  72523   
 71861  72524   /* Opcode: AggStep * P2 P3 P4 P5
................................................................................
 71882  72545     pRec = &aMem[pOp->p2];
 71883  72546     apVal = p->apArg;
 71884  72547     assert( apVal || n==0 );
 71885  72548     for(i=0; i<n; i++, pRec++){
 71886  72549       assert( memIsValid(pRec) );
 71887  72550       apVal[i] = pRec;
 71888  72551       memAboutToChange(p, pRec);
 71889         -    sqlite3VdbeMemStoreType(pRec);
 71890  72552     }
 71891  72553     ctx.pFunc = pOp->p4.pFunc;
 71892  72554     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 71893  72555     ctx.pMem = pMem = &aMem[pOp->p3];
 71894  72556     pMem->n++;
 71895  72557     ctx.s.flags = MEM_Null;
 71896  72558     ctx.s.z = 0;
................................................................................
 71985  72647       sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
 71986  72648     }    
 71987  72649     break;
 71988  72650   };  
 71989  72651   #endif
 71990  72652   
 71991  72653   #ifndef SQLITE_OMIT_PRAGMA
 71992         -/* Opcode: JournalMode P1 P2 P3 * P5
        72654  +/* Opcode: JournalMode P1 P2 P3 * *
 71993  72655   **
 71994  72656   ** Change the journal mode of database P1 to P3. P3 must be one of the
 71995  72657   ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
 71996  72658   ** modes (delete, truncate, persist, off and memory), this is a simple
 71997  72659   ** operation. No IO is required.
 71998  72660   **
 71999  72661   ** If changing into or out of WAL mode the procedure is more complicated.
................................................................................
 72119  72781     Btree *pBt;
 72120  72782   
 72121  72783     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 72122  72784     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 72123  72785     assert( p->readOnly==0 );
 72124  72786     pBt = db->aDb[pOp->p1].pBt;
 72125  72787     rc = sqlite3BtreeIncrVacuum(pBt);
        72788  +  VdbeBranchTaken(rc==SQLITE_DONE,2);
 72126  72789     if( rc==SQLITE_DONE ){
 72127  72790       pc = pOp->p2 - 1;
 72128  72791       rc = SQLITE_OK;
 72129  72792     }
 72130  72793     break;
 72131  72794   }
 72132  72795   #endif
................................................................................
 72315  72978   
 72316  72979     /* Invoke the xFilter method */
 72317  72980     {
 72318  72981       res = 0;
 72319  72982       apArg = p->apArg;
 72320  72983       for(i = 0; i<nArg; i++){
 72321  72984         apArg[i] = &pArgc[i+1];
 72322         -      sqlite3VdbeMemStoreType(apArg[i]);
 72323  72985       }
 72324  72986   
 72325  72987       p->inVtabMethod = 1;
 72326  72988       rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
 72327  72989       p->inVtabMethod = 0;
 72328  72990       sqlite3VtabImportErrmsg(p, pVtab);
 72329  72991       if( rc==SQLITE_OK ){
 72330  72992         res = pModule->xEof(pVtabCursor);
 72331  72993       }
 72332         -
        72994  +    VdbeBranchTaken(res!=0,2);
 72333  72995       if( res ){
 72334  72996         pc = pOp->p2 - 1;
 72335  72997       }
 72336  72998     }
 72337  72999     pCur->nullRow = 0;
 72338  73000   
 72339  73001     break;
................................................................................
 72430  73092     p->inVtabMethod = 1;
 72431  73093     rc = pModule->xNext(pCur->pVtabCursor);
 72432  73094     p->inVtabMethod = 0;
 72433  73095     sqlite3VtabImportErrmsg(p, pVtab);
 72434  73096     if( rc==SQLITE_OK ){
 72435  73097       res = pModule->xEof(pCur->pVtabCursor);
 72436  73098     }
 72437         -
        73099  +  VdbeBranchTaken(!res,2);
 72438  73100     if( !res ){
 72439  73101       /* If there is data, jump to P2 */
 72440  73102       pc = pOp->p2 - 1;
 72441  73103     }
 72442  73104     goto check_for_interrupt;
 72443  73105   }
 72444  73106   #endif /* SQLITE_OMIT_VIRTUALTABLE */
................................................................................
 72471  73133       p->expired = 0;
 72472  73134     }
 72473  73135     break;
 72474  73136   }
 72475  73137   #endif
 72476  73138   
 72477  73139   #ifndef SQLITE_OMIT_VIRTUALTABLE
 72478         -/* Opcode: VUpdate P1 P2 P3 P4 *
        73140  +/* Opcode: VUpdate P1 P2 P3 P4 P5
 72479  73141   ** Synopsis: data=r[P3@P2]
 72480  73142   **
 72481  73143   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 72482  73144   ** This opcode invokes the corresponding xUpdate method. P2 values
 72483  73145   ** are contiguous memory cells starting at P3 to pass to the xUpdate 
 72484  73146   ** invocation. The value in register (P3+P2-1) corresponds to the 
 72485  73147   ** p2th element of the argv array passed to xUpdate.
................................................................................
 72494  73156   **
 72495  73157   ** If P2==1 then no insert is performed.  argv[0] is the rowid of
 72496  73158   ** a row to delete.
 72497  73159   **
 72498  73160   ** P1 is a boolean flag. If it is set to true and the xUpdate call
 72499  73161   ** is successful, then the value returned by sqlite3_last_insert_rowid() 
 72500  73162   ** is set to the value of the rowid for the row just inserted.
        73163  +**
        73164  +** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
        73165  +** apply in the case of a constraint failure on an insert or update.
 72501  73166   */
 72502  73167   case OP_VUpdate: {
 72503  73168     sqlite3_vtab *pVtab;
 72504  73169     sqlite3_module *pModule;
 72505  73170     int nArg;
 72506  73171     int i;
 72507  73172     sqlite_int64 rowid;
................................................................................
 72519  73184     if( ALWAYS(pModule->xUpdate) ){
 72520  73185       u8 vtabOnConflict = db->vtabOnConflict;
 72521  73186       apArg = p->apArg;
 72522  73187       pX = &aMem[pOp->p3];
 72523  73188       for(i=0; i<nArg; i++){
 72524  73189         assert( memIsValid(pX) );
 72525  73190         memAboutToChange(p, pX);
 72526         -      sqlite3VdbeMemStoreType(pX);
 72527  73191         apArg[i] = pX;
 72528  73192         pX++;
 72529  73193       }
 72530  73194       db->vtabOnConflict = pOp->p5;
 72531  73195       rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
 72532  73196       db->vtabOnConflict = vtabOnConflict;
 72533  73197       sqlite3VtabImportErrmsg(p, pVtab);
................................................................................
 72582  73246     }
 72583  73247     pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
 72584  73248     break;
 72585  73249   }
 72586  73250   #endif
 72587  73251   
 72588  73252   
 72589         -#ifndef SQLITE_OMIT_TRACE
 72590         -/* Opcode: Trace * * * P4 *
        73253  +/* Opcode: Init * P2 * P4 *
        73254  +** Synopsis:  Start at P2
        73255  +**
        73256  +** Programs contain a single instance of this opcode as the very first
        73257  +** opcode.
 72591  73258   **
 72592  73259   ** If tracing is enabled (by the sqlite3_trace()) interface, then
 72593  73260   ** the UTF-8 string contained in P4 is emitted on the trace callback.
        73261  +** Or if P4 is blank, use the string returned by sqlite3_sql().
        73262  +**
        73263  +** If P2 is not zero, jump to instruction P2.
 72594  73264   */
 72595         -case OP_Trace: {
        73265  +case OP_Init: {          /* jump */
 72596  73266     char *zTrace;
 72597  73267     char *z;
 72598  73268   
        73269  +  if( pOp->p2 ){
        73270  +    pc = pOp->p2 - 1;
        73271  +  }
        73272  +#ifndef SQLITE_OMIT_TRACE
 72599  73273     if( db->xTrace
 72600  73274      && !p->doingRerun
 72601  73275      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 72602  73276     ){
 72603  73277       z = sqlite3VdbeExpandSql(p, zTrace);
 72604  73278       db->xTrace(db->pTraceArg, z);
 72605  73279       sqlite3DbFree(db, z);
................................................................................
 72617  73291   #ifdef SQLITE_DEBUG
 72618  73292     if( (db->flags & SQLITE_SqlTrace)!=0
 72619  73293      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 72620  73294     ){
 72621  73295       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
 72622  73296     }
 72623  73297   #endif /* SQLITE_DEBUG */
        73298  +#endif /* SQLITE_OMIT_TRACE */
 72624  73299     break;
 72625  73300   }
 72626         -#endif
 72627  73301   
 72628  73302   
 72629  73303   /* Opcode: Noop * * * * *
 72630  73304   **
 72631  73305   ** Do nothing.  This instruction is often useful as a jump
 72632  73306   ** destination.
 72633  73307   */
................................................................................
 72651  73325       }
 72652  73326   
 72653  73327   #ifdef VDBE_PROFILE
 72654  73328       {
 72655  73329         u64 elapsed = sqlite3Hwtime() - start;
 72656  73330         pOp->cycles += elapsed;
 72657  73331         pOp->cnt++;
 72658         -#if 0
 72659         -        fprintf(stdout, "%10llu ", elapsed);
 72660         -        sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
 72661         -#endif
 72662  73332       }
 72663  73333   #endif
 72664  73334   
 72665  73335       /* The following code adds nothing to the actual functionality
 72666  73336       ** of the program.  It is only here for testing and debugging.
 72667  73337       ** On the other hand, it does burn CPU cycles every time through
 72668  73338       ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
................................................................................
 72880  73550     ** uses it to implement the blob_read(), blob_write() and 
 72881  73551     ** blob_bytes() functions.
 72882  73552     **
 72883  73553     ** The sqlite3_blob_close() function finalizes the vdbe program,
 72884  73554     ** which closes the b-tree cursor and (possibly) commits the 
 72885  73555     ** transaction.
 72886  73556     */
        73557  +  static const int iLn = __LINE__+4;
 72887  73558     static const VdbeOpList openBlob[] = {
 72888         -    {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
 72889         -    {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
 72890         -    {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
 72891         -
        73559  +    /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
        73560  +    {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
 72892  73561       /* One of the following two instructions is replaced by an OP_Noop. */
 72893         -    {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
 72894         -    {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
 72895         -
 72896         -    {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
 72897         -    {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
 72898         -    {OP_Column, 0, 0, 1},          /* 7  */
 72899         -    {OP_ResultRow, 1, 0, 0},       /* 8  */
 72900         -    {OP_Goto, 0, 5, 0},            /* 9  */
 72901         -    {OP_Close, 0, 0, 0},           /* 10 */
 72902         -    {OP_Halt, 0, 0, 0},            /* 11 */
        73562  +    {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
        73563  +    {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
        73564  +    {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
        73565  +    {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
        73566  +    {OP_Column, 0, 0, 1},          /* 6  */
        73567  +    {OP_ResultRow, 1, 0, 0},       /* 7  */
        73568  +    {OP_Goto, 0, 4, 0},            /* 8  */
        73569  +    {OP_Close, 0, 0, 0},           /* 9  */
        73570  +    {OP_Halt, 0, 0, 0},            /* 10 */
 72903  73571     };
 72904  73572   
 72905  73573     int rc = SQLITE_OK;
 72906  73574     char *zErr = 0;
 72907  73575     Table *pTab;
 72908  73576     Parse *pParse = 0;
 72909  73577     Incrblob *pBlob = 0;
................................................................................
 73008  73676   
 73009  73677       pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
 73010  73678       assert( pBlob->pStmt || db->mallocFailed );
 73011  73679       if( pBlob->pStmt ){
 73012  73680         Vdbe *v = (Vdbe *)pBlob->pStmt;
 73013  73681         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 73014  73682   
 73015         -      sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
 73016  73683   
 73017         -
 73018         -      /* Configure the OP_Transaction */
 73019         -      sqlite3VdbeChangeP1(v, 0, iDb);
 73020         -      sqlite3VdbeChangeP2(v, 0, flags);
 73021         -
 73022         -      /* Configure the OP_VerifyCookie */
 73023         -      sqlite3VdbeChangeP1(v, 1, iDb);
 73024         -      sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
 73025         -      sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
        73684  +      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
        73685  +                           pTab->pSchema->schema_cookie,
        73686  +                           pTab->pSchema->iGeneration);
        73687  +      sqlite3VdbeChangeP5(v, 1);     
        73688  +      sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
 73026  73689   
 73027  73690         /* Make sure a mutex is held on the table to be accessed */
 73028  73691         sqlite3VdbeUsesBtree(v, iDb); 
 73029  73692   
 73030  73693         /* Configure the OP_TableLock instruction */
 73031  73694   #ifdef SQLITE_OMIT_SHARED_CACHE
 73032         -      sqlite3VdbeChangeToNoop(v, 2);
        73695  +      sqlite3VdbeChangeToNoop(v, 1);
 73033  73696   #else
 73034         -      sqlite3VdbeChangeP1(v, 2, iDb);
 73035         -      sqlite3VdbeChangeP2(v, 2, pTab->tnum);
 73036         -      sqlite3VdbeChangeP3(v, 2, flags);
 73037         -      sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
        73697  +      sqlite3VdbeChangeP1(v, 1, iDb);
        73698  +      sqlite3VdbeChangeP2(v, 1, pTab->tnum);
        73699  +      sqlite3VdbeChangeP3(v, 1, flags);
        73700  +      sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
 73038  73701   #endif
 73039  73702   
 73040  73703         /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 73041  73704         ** parameter of the other to pTab->tnum.  */
 73042         -      sqlite3VdbeChangeToNoop(v, 4 - flags);
 73043         -      sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
 73044         -      sqlite3VdbeChangeP3(v, 3 + flags, iDb);
        73705  +      sqlite3VdbeChangeToNoop(v, 3 - flags);
        73706  +      sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
        73707  +      sqlite3VdbeChangeP3(v, 2 + flags, iDb);
 73045  73708   
 73046  73709         /* Configure the number of columns. Configure the cursor to
 73047  73710         ** think that the table has one more column than it really
 73048  73711         ** does. An OP_Column to retrieve this imaginary column will
 73049  73712         ** always return an SQL NULL. This is useful because it means
 73050  73713         ** we can invoke OP_Column to fill in the vdbe cursors type 
 73051  73714         ** and offset cache without causing any IO.
 73052  73715         */
 73053         -      sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
 73054         -      sqlite3VdbeChangeP2(v, 7, pTab->nCol);
        73716  +      sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
        73717  +      sqlite3VdbeChangeP2(v, 6, pTab->nCol);
 73055  73718         if( !db->mallocFailed ){
 73056  73719           pParse->nVar = 1;
 73057  73720           pParse->nMem = 1;
 73058  73721           pParse->nTab = 1;
 73059  73722           sqlite3VdbeMakeReady(v, pParse);
 73060  73723         }
 73061  73724       }
................................................................................
 73632  74295       assert( r2->nField>0 );
 73633  74296       for(i=0; i<r2->nField; i++){
 73634  74297         if( r2->aMem[i].flags & MEM_Null ){
 73635  74298           *pRes = -1;
 73636  74299           return;
 73637  74300         }
 73638  74301       }
 73639         -    r2->flags |= UNPACKED_PREFIX_MATCH;
        74302  +    assert( r2->default_rc==0 );
 73640  74303     }
 73641  74304   
 73642         -  *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
        74305  +  *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
 73643  74306   }
 73644  74307   
 73645  74308   /*
 73646  74309   ** This function is called to compare two iterator keys when merging 
 73647  74310   ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
 73648  74311   ** value to recalculate.
 73649  74312   */
................................................................................
 75272  75935         assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
 75273  75936         if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
 75274  75937           pExpr->iTable = 1;
 75275  75938           pTab = pParse->pTriggerTab;
 75276  75939         }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
 75277  75940           pExpr->iTable = 0;
 75278  75941           pTab = pParse->pTriggerTab;
        75942  +      }else{
        75943  +        pTab = 0;
 75279  75944         }
 75280  75945   
 75281  75946         if( pTab ){ 
 75282  75947           int iCol;
 75283  75948           pSchema = pTab->pSchema;
 75284  75949           cntTab++;
 75285  75950           for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
................................................................................
 75315  75980         }
 75316  75981       }
 75317  75982   #endif /* !defined(SQLITE_OMIT_TRIGGER) */
 75318  75983   
 75319  75984       /*
 75320  75985       ** Perhaps the name is a reference to the ROWID
 75321  75986       */
 75322         -    assert( pTab!=0 || cntTab==0 );
 75323         -    if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
        75987  +    if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
        75988  +     && HasRowid(pMatch->pTab) ){
 75324  75989         cnt = 1;
 75325  75990         pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
 75326  75991         pExpr->affinity = SQLITE_AFF_INTEGER;
 75327  75992       }
 75328  75993   
 75329  75994       /*
 75330  75995       ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
................................................................................
 77447  78112     if( pPrior ) pPrior->pNext = pNew;
 77448  78113     pNew->pNext = 0;
 77449  78114     pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 77450  78115     pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 77451  78116     pNew->iLimit = 0;
 77452  78117     pNew->iOffset = 0;
 77453  78118     pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 77454         -  pNew->pRightmost = 0;
 77455  78119     pNew->addrOpenEphm[0] = -1;
 77456  78120     pNew->addrOpenEphm[1] = -1;
 77457  78121     pNew->addrOpenEphm[2] = -1;
 77458  78122     pNew->nSelectRow = p->nSelectRow;
 77459  78123     pNew->pWith = withDup(db, p->pWith);
 77460  78124     return pNew;
 77461  78125   }
................................................................................
 77757  78421       case TK_BLOB:
 77758  78422         return 0;
 77759  78423       default:
 77760  78424         return 1;
 77761  78425     }
 77762  78426   }
 77763  78427   
 77764         -/*
 77765         -** Generate an OP_IsNull instruction that tests register iReg and jumps
 77766         -** to location iDest if the value in iReg is NULL.  The value in iReg 
 77767         -** was computed by pExpr.  If we can look at pExpr at compile-time and
 77768         -** determine that it can never generate a NULL, then the OP_IsNull operation
 77769         -** can be omitted.
 77770         -*/
 77771         -SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
 77772         -  Vdbe *v,            /* The VDBE under construction */
 77773         -  const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
 77774         -  int iReg,           /* Test the value in this register for NULL */
 77775         -  int iDest           /* Jump here if the value is null */
 77776         -){
 77777         -  if( sqlite3ExprCanBeNull(pExpr) ){
 77778         -    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
 77779         -  }
 77780         -}
 77781         -
 77782  78428   /*
 77783  78429   ** Return TRUE if the given expression is a constant which would be
 77784  78430   ** unchanged by OP_Affinity with the affinity given in the second
 77785  78431   ** argument.
 77786  78432   **
 77787  78433   ** This routine is used to determine if the OP_Affinity operation
 77788  78434   ** can be omitted.  When in doubt return FALSE.  A false negative
................................................................................
 77971  78617       assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
 77972  78618       assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
 77973  78619       assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
 77974  78620       pTab = p->pSrc->a[0].pTab;
 77975  78621       pExpr = p->pEList->a[0].pExpr;
 77976  78622       iCol = (i16)pExpr->iColumn;
 77977  78623      
 77978         -    /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
        78624  +    /* Code an OP_Transaction and OP_TableLock for <table>. */
 77979  78625       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 77980  78626       sqlite3CodeVerifySchema(pParse, iDb);
 77981  78627       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 77982  78628   
 77983  78629       /* This function is only called from two places. In both cases the vdbe
 77984  78630       ** has already been allocated. So assume sqlite3GetVdbe() is always
 77985  78631       ** successful here.
 77986  78632       */
 77987  78633       assert(v);
 77988  78634       if( iCol<0 ){
 77989         -      int iAddr;
 77990         -
 77991         -      iAddr = sqlite3CodeOnce(pParse);
        78635  +      int iAddr = sqlite3CodeOnce(pParse);
        78636  +      VdbeCoverage(v);
 77992  78637   
 77993  78638         sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 77994  78639         eType = IN_INDEX_ROWID;
 77995  78640   
 77996  78641         sqlite3VdbeJumpHere(v, iAddr);
 77997  78642       }else{
 77998  78643         Index *pIdx;                         /* Iterator variable */
................................................................................
 78009  78654         int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 78010  78655   
 78011  78656         for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
 78012  78657           if( (pIdx->aiColumn[0]==iCol)
 78013  78658            && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
 78014  78659            && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
 78015  78660           ){
 78016         -          int iAddr = sqlite3CodeOnce(pParse);
        78661  +          int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
 78017  78662             sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
 78018  78663             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 78019  78664             VdbeComment((v, "%s", pIdx->zName));
 78020  78665             assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
 78021  78666             eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
 78022  78667   
 78023         -          sqlite3VdbeJumpHere(v, iAddr);
 78024  78668             if( prNotFound && !pTab->aCol[iCol].notNull ){
 78025  78669               *prNotFound = ++pParse->nMem;
 78026  78670               sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 78027  78671             }
        78672  +          sqlite3VdbeJumpHere(v, iAddr);
 78028  78673           }
 78029  78674         }
 78030  78675       }
 78031  78676     }
 78032  78677   
 78033  78678     if( eType==0 ){
 78034  78679       /* Could not found an existing table or index to use as the RHS b-tree.
................................................................................
 78109  78754     **    *  The right-hand side is an expression list containing variables
 78110  78755     **    *  We are inside a trigger
 78111  78756     **
 78112  78757     ** If all of the above are false, then we can run this code just once
 78113  78758     ** save the results, and reuse the same result on subsequent invocations.
 78114  78759     */
 78115  78760     if( !ExprHasProperty(pExpr, EP_VarSelect) ){
 78116         -    testAddr = sqlite3CodeOnce(pParse);
        78761  +    testAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
 78117  78762     }
 78118  78763   
 78119  78764   #ifndef SQLITE_OMIT_EXPLAIN
 78120  78765     if( pParse->explain==2 ){
 78121  78766       char *zMsg = sqlite3MPrintf(
 78122  78767           pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
 78123  78768           pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
................................................................................
 78150  78795         ** SELECT... statement are columns, then numeric affinity is used
 78151  78796         ** if either column has NUMERIC or INTEGER affinity. If neither
 78152  78797         ** 'x' nor the SELECT... statement are columns, then numeric affinity
 78153  78798         ** is used.
 78154  78799         */
 78155  78800         pExpr->iTable = pParse->nTab++;
 78156  78801         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
 78157         -      if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 78158  78802         pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
 78159  78803   
 78160  78804         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 78161  78805           /* Case 1:     expr IN (SELECT ...)
 78162  78806           **
 78163  78807           ** Generate code to write the results of the select into the temporary
 78164  78808           ** table allocated and opened above.
................................................................................
 78226  78870             if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
 78227  78871               sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
 78228  78872             }else{
 78229  78873               r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
 78230  78874               if( isRowid ){
 78231  78875                 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
 78232  78876                                   sqlite3VdbeCurrentAddr(v)+2);
        78877  +              VdbeCoverage(v);
 78233  78878                 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
 78234  78879               }else{
 78235  78880                 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
 78236  78881                 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
 78237  78882                 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
 78238  78883               }
 78239  78884             }
................................................................................
 78349  78994   
 78350  78995     /* If the LHS is NULL, then the result is either false or NULL depending
 78351  78996     ** on whether the RHS is empty or not, respectively.
 78352  78997     */
 78353  78998     if( destIfNull==destIfFalse ){
 78354  78999       /* Shortcut for the common case where the false and NULL outcomes are
 78355  79000       ** the same. */
 78356         -    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
        79001  +    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
 78357  79002     }else{
 78358         -    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
        79003  +    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
 78359  79004       sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
        79005  +    VdbeCoverage(v);
 78360  79006       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
 78361  79007       sqlite3VdbeJumpHere(v, addr1);
 78362  79008     }
 78363  79009   
 78364  79010     if( eType==IN_INDEX_ROWID ){
 78365  79011       /* In this case, the RHS is the ROWID of table b-tree
 78366  79012       */
 78367         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
        79013  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
 78368  79014       sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
        79015  +    VdbeCoverage(v);
 78369  79016     }else{
 78370  79017       /* In this case, the RHS is an index b-tree.
 78371  79018       */
 78372  79019       sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
 78373  79020   
 78374  79021       /* If the set membership test fails, then the result of the 
 78375  79022       ** "x IN (...)" expression must be either 0 or NULL. If the set
................................................................................
 78382  79029         ** cannot contain NULL values. This happens as the result
 78383  79030         ** of a "NOT NULL" constraint in the database schema.
 78384  79031         **
 78385  79032         ** Also run this branch if NULL is equivalent to FALSE
 78386  79033         ** for this particular IN operator.
 78387  79034         */
 78388  79035         sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
 78389         -
        79036  +      VdbeCoverage(v);
 78390  79037       }else{
 78391  79038         /* In this branch, the RHS of the IN might contain a NULL and
 78392  79039         ** the presence of a NULL on the RHS makes a difference in the
 78393  79040         ** outcome.
 78394  79041         */
 78395         -      int j1, j2, j3;
        79042  +      int j1, j2;
 78396  79043   
 78397  79044         /* First check to see if the LHS is contained in the RHS.  If so,
 78398  79045         ** then the presence of NULLs in the RHS does not matter, so jump
 78399  79046         ** over all of the code that follows.
 78400  79047         */
 78401  79048         j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
        79049  +      VdbeCoverage(v);
 78402  79050   
 78403  79051         /* Here we begin generating code that runs if the LHS is not
 78404  79052         ** contained within the RHS.  Generate additional code that
 78405  79053         ** tests the RHS for NULLs.  If the RHS contains a NULL then
 78406  79054         ** jump to destIfNull.  If there are no NULLs in the RHS then
 78407  79055         ** jump to destIfFalse.
 78408  79056         */
 78409         -      j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
 78410         -      j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
 78411         -      sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
 78412         -      sqlite3VdbeJumpHere(v, j3);
 78413         -      sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
        79057  +      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
        79058  +      sqlite3VdbeAddOp2(v, OP_IfNot, rRhsHasNull, destIfFalse); VdbeCoverage(v);
        79059  +      j2 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
        79060  +      VdbeCoverage(v);
        79061  +      sqlite3VdbeAddOp2(v, OP_Integer, 0, rRhsHasNull);
        79062  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
 78414  79063         sqlite3VdbeJumpHere(v, j2);
 78415         -
 78416         -      /* Jump to the appropriate target depending on whether or not
 78417         -      ** the RHS contains a NULL
 78418         -      */
 78419         -      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
 78420         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
        79064  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, rRhsHasNull);
        79065  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
 78421  79066   
 78422  79067         /* The OP_Found at the top of this branch jumps here when true, 
 78423  79068         ** causing the overall IN expression evaluation to fall through.
 78424  79069         */
 78425  79070         sqlite3VdbeJumpHere(v, j1);
 78426  79071       }
 78427  79072     }
................................................................................
 78934  79579   #endif /* SQLITE_OMIT_CAST */
 78935  79580       case TK_LT:
 78936  79581       case TK_LE:
 78937  79582       case TK_GT:
 78938  79583       case TK_GE:
 78939  79584       case TK_NE:
 78940  79585       case TK_EQ: {
 78941         -      assert( TK_LT==OP_Lt );
 78942         -      assert( TK_LE==OP_Le );
 78943         -      assert( TK_GT==OP_Gt );
 78944         -      assert( TK_GE==OP_Ge );
 78945         -      assert( TK_EQ==OP_Eq );
 78946         -      assert( TK_NE==OP_Ne );
 78947         -      testcase( op==TK_LT );
 78948         -      testcase( op==TK_LE );
 78949         -      testcase( op==TK_GT );
 78950         -      testcase( op==TK_GE );
 78951         -      testcase( op==TK_EQ );
 78952         -      testcase( op==TK_NE );
 78953  79586         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 78954  79587         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 78955  79588         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 78956  79589                     r1, r2, inReg, SQLITE_STOREP2);
        79590  +      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
        79591  +      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
        79592  +      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
        79593  +      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
        79594  +      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
        79595  +      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 78957  79596         testcase( regFree1==0 );
 78958  79597         testcase( regFree2==0 );
 78959  79598         break;
 78960  79599       }
 78961  79600       case TK_IS:
 78962  79601       case TK_ISNOT: {
 78963  79602         testcase( op==TK_IS );
 78964  79603         testcase( op==TK_ISNOT );
 78965  79604         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 78966  79605         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 78967  79606         op = (op==TK_IS) ? TK_EQ : TK_NE;
 78968  79607         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 78969  79608                     r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
        79609  +      VdbeCoverageIf(v, op==TK_EQ);
        79610  +      VdbeCoverageIf(v, op==TK_NE);
 78970  79611         testcase( regFree1==0 );
 78971  79612         testcase( regFree2==0 );
 78972  79613         break;
 78973  79614       }
 78974  79615       case TK_AND:
 78975  79616       case TK_OR:
 78976  79617       case TK_PLUS:
................................................................................
 78979  79620       case TK_REM:
 78980  79621       case TK_BITAND:
 78981  79622       case TK_BITOR:
 78982  79623       case TK_SLASH:
 78983  79624       case TK_LSHIFT:
 78984  79625       case TK_RSHIFT: 
 78985  79626       case TK_CONCAT: {
 78986         -      assert( TK_AND==OP_And );
 78987         -      assert( TK_OR==OP_Or );
 78988         -      assert( TK_PLUS==OP_Add );
 78989         -      assert( TK_MINUS==OP_Subtract );
 78990         -      assert( TK_REM==OP_Remainder );
 78991         -      assert( TK_BITAND==OP_BitAnd );
 78992         -      assert( TK_BITOR==OP_BitOr );
 78993         -      assert( TK_SLASH==OP_Divide );
 78994         -      assert( TK_LSHIFT==OP_ShiftLeft );
 78995         -      assert( TK_RSHIFT==OP_ShiftRight );
 78996         -      assert( TK_CONCAT==OP_Concat );
 78997         -      testcase( op==TK_AND );
 78998         -      testcase( op==TK_OR );
 78999         -      testcase( op==TK_PLUS );
 79000         -      testcase( op==TK_MINUS );
 79001         -      testcase( op==TK_REM );
 79002         -      testcase( op==TK_BITAND );
 79003         -      testcase( op==TK_BITOR );
 79004         -      testcase( op==TK_SLASH );
 79005         -      testcase( op==TK_LSHIFT );
 79006         -      testcase( op==TK_RSHIFT );
 79007         -      testcase( op==TK_CONCAT );
        79627  +      assert( TK_AND==OP_And );            testcase( op==TK_AND );
        79628  +      assert( TK_OR==OP_Or );              testcase( op==TK_OR );
        79629  +      assert( TK_PLUS==OP_Add );           testcase( op==TK_PLUS );
        79630  +      assert( TK_MINUS==OP_Subtract );     testcase( op==TK_MINUS );
        79631  +      assert( TK_REM==OP_Remainder );      testcase( op==TK_REM );
        79632  +      assert( TK_BITAND==OP_BitAnd );      testcase( op==TK_BITAND );
        79633  +      assert( TK_BITOR==OP_BitOr );        testcase( op==TK_BITOR );
        79634  +      assert( TK_SLASH==OP_Divide );       testcase( op==TK_SLASH );
        79635  +      assert( TK_LSHIFT==OP_ShiftLeft );   testcase( op==TK_LSHIFT );
        79636  +      assert( TK_RSHIFT==OP_ShiftRight );  testcase( op==TK_RSHIFT );
        79637  +      assert( TK_CONCAT==OP_Concat );      testcase( op==TK_CONCAT );
 79008  79638         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79009  79639         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 79010  79640         sqlite3VdbeAddOp3(v, op, r2, r1, target);
 79011  79641         testcase( regFree1==0 );
 79012  79642         testcase( regFree2==0 );
 79013  79643         break;
 79014  79644       }
................................................................................
 79032  79662           testcase( regFree2==0 );
 79033  79663         }
 79034  79664         inReg = target;
 79035  79665         break;
 79036  79666       }
 79037  79667       case TK_BITNOT:
 79038  79668       case TK_NOT: {
 79039         -      assert( TK_BITNOT==OP_BitNot );
 79040         -      assert( TK_NOT==OP_Not );
 79041         -      testcase( op==TK_BITNOT );
 79042         -      testcase( op==TK_NOT );
        79669  +      assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
        79670  +      assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
 79043  79671         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79044  79672         testcase( regFree1==0 );
 79045  79673         inReg = target;
 79046  79674         sqlite3VdbeAddOp2(v, op, r1, inReg);
 79047  79675         break;
 79048  79676       }
 79049  79677       case TK_ISNULL:
 79050  79678       case TK_NOTNULL: {
 79051  79679         int addr;
 79052         -      assert( TK_ISNULL==OP_IsNull );
 79053         -      assert( TK_NOTNULL==OP_NotNull );
 79054         -      testcase( op==TK_ISNULL );
 79055         -      testcase( op==TK_NOTNULL );
        79680  +      assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
        79681  +      assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
 79056  79682         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 79057  79683         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79058  79684         testcase( regFree1==0 );
 79059  79685         addr = sqlite3VdbeAddOp1(v, op, r1);
        79686  +      VdbeCoverageIf(v, op==TK_ISNULL);
        79687  +      VdbeCoverageIf(v, op==TK_NOTNULL);
 79060  79688         sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
 79061  79689         sqlite3VdbeJumpHere(v, addr);
 79062  79690         break;
 79063  79691       }
 79064  79692       case TK_AGG_FUNCTION: {
 79065  79693         AggInfo *pInfo = pExpr->pAggInfo;
 79066  79694         if( pInfo==0 ){
................................................................................
 79104  79732         */
 79105  79733         if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
 79106  79734           int endCoalesce = sqlite3VdbeMakeLabel(v);
 79107  79735           assert( nFarg>=2 );
 79108  79736           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
 79109  79737           for(i=1; i<nFarg; i++){
 79110  79738             sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
        79739  +          VdbeCoverage(v);
 79111  79740             sqlite3ExprCacheRemove(pParse, target, 1);
 79112  79741             sqlite3ExprCachePush(pParse);
 79113  79742             sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
 79114  79743             sqlite3ExprCachePop(pParse, 1);
 79115  79744           }
 79116  79745           sqlite3VdbeResolveLabel(v, endCoalesce);
 79117  79746           break;
................................................................................
 79241  79870         r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
 79242  79871         r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 79243  79872         testcase( regFree1==0 );
 79244  79873         testcase( regFree2==0 );
 79245  79874         r3 = sqlite3GetTempReg(pParse);
 79246  79875         r4 = sqlite3GetTempReg(pParse);
 79247  79876         codeCompare(pParse, pLeft, pRight, OP_Ge,
 79248         -                  r1, r2, r3, SQLITE_STOREP2);
        79877  +                  r1, r2, r3, SQLITE_STOREP2);  VdbeCoverage(v);
 79249  79878         pLItem++;
 79250  79879         pRight = pLItem->pExpr;
 79251  79880         sqlite3ReleaseTempReg(pParse, regFree2);
 79252  79881         r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 79253  79882         testcase( regFree2==0 );
 79254  79883         codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
        79884  +      VdbeCoverage(v);
 79255  79885         sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
 79256  79886         sqlite3ReleaseTempReg(pParse, r3);
 79257  79887         sqlite3ReleaseTempReg(pParse, r4);
 79258  79888         break;
 79259  79889       }
 79260  79890       case TK_COLLATE: 
 79261  79891       case TK_UPLUS: {
................................................................................
 79414  80044         if( pExpr->affinity==OE_Abort ){
 79415  80045           sqlite3MayAbort(pParse);
 79416  80046         }
 79417  80047         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79418  80048         if( pExpr->affinity==OE_Ignore ){
 79419  80049           sqlite3VdbeAddOp4(
 79420  80050               v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
        80051  +        VdbeCoverage(v);
 79421  80052         }else{
 79422  80053           sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
 79423  80054                                 pExpr->affinity, pExpr->u.zToken, 0, 0);
 79424  80055         }
 79425  80056   
 79426  80057         break;
 79427  80058       }
................................................................................
 79501  80132   }
 79502  80133   
 79503  80134   /*
 79504  80135   ** Generate code that will evaluate expression pExpr and store the
 79505  80136   ** results in register target.  The results are guaranteed to appear
 79506  80137   ** in register target.
 79507  80138   */
 79508         -SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
        80139  +SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
 79509  80140     int inReg;
 79510  80141   
 79511  80142     assert( target>0 && target<=pParse->nMem );
 79512  80143     if( pExpr && pExpr->op==TK_REGISTER ){
 79513  80144       sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
 79514  80145     }else{
 79515  80146       inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
 79516  80147       assert( pParse->pVdbe || pParse->db->mallocFailed );
 79517  80148       if( inReg!=target && pParse->pVdbe ){
 79518  80149         sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
 79519  80150       }
 79520  80151     }
 79521         -  return target;
        80152  +}
        80153  +
        80154  +/*
        80155  +** Generate code that will evaluate expression pExpr and store the
        80156  +** results in register target.  The results are guaranteed to appear
        80157  +** in register target.  If the expression is constant, then this routine
        80158  +** might choose to code the expression at initialization time.
        80159  +*/
        80160  +SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
        80161  +  if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
        80162  +    sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
        80163  +  }else{
        80164  +    sqlite3ExprCode(pParse, pExpr, target);
        80165  +  }
 79522  80166   }
 79523  80167   
 79524  80168   /*
 79525  80169   ** Generate code that evalutes the given expression and puts the result
 79526  80170   ** in register target.
 79527  80171   **
 79528  80172   ** Also make a copy of the expression results into another "cache" register
................................................................................
 79529  80173   ** and modify the expression so that the next time it is evaluated,
 79530  80174   ** the result is a copy of the cache register.
 79531  80175   **
 79532  80176   ** This routine is used for expressions that are used multiple 
 79533  80177   ** times.  They are evaluated once and the results of the expression
 79534  80178   ** are reused.
 79535  80179   */
 79536         -SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
        80180  +SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
 79537  80181     Vdbe *v = pParse->pVdbe;
 79538         -  int inReg;
 79539         -  inReg = sqlite3ExprCode(pParse, pExpr, target);
        80182  +  int iMem;
        80183  +
 79540  80184     assert( target>0 );
 79541         -  /* The only place, other than this routine, where expressions can be
 79542         -  ** converted to TK_REGISTER is internal subexpressions in BETWEEN and
 79543         -  ** CASE operators.  Neither ever calls this routine.  And this routine
 79544         -  ** is never called twice on the same expression.  Hence it is impossible
 79545         -  ** for the input to this routine to already be a register.  Nevertheless,
 79546         -  ** it seems prudent to keep the ALWAYS() in case the conditions above
 79547         -  ** change with future modifications or enhancements. */
 79548         -  if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
 79549         -    int iMem;
 79550         -    iMem = ++pParse->nMem;
 79551         -    sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
 79552         -    exprToRegister(pExpr, iMem);
 79553         -  }
 79554         -  return inReg;
        80185  +  assert( pExpr->op!=TK_REGISTER );
        80186  +  sqlite3ExprCode(pParse, pExpr, target);
        80187  +  iMem = ++pParse->nMem;
        80188  +  sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
        80189  +  exprToRegister(pExpr, iMem);
 79555  80190   }
 79556  80191   
 79557  80192   #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 79558  80193   /*
 79559  80194   ** Generate a human-readable explanation of an expression tree.
 79560  80195   */
 79561  80196   SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
................................................................................
 79982  80617       }
 79983  80618       case TK_LT:
 79984  80619       case TK_LE:
 79985  80620       case TK_GT:
 79986  80621       case TK_GE:
 79987  80622       case TK_NE:
 79988  80623       case TK_EQ: {
 79989         -      assert( TK_LT==OP_Lt );
 79990         -      assert( TK_LE==OP_Le );
 79991         -      assert( TK_GT==OP_Gt );
 79992         -      assert( TK_GE==OP_Ge );
 79993         -      assert( TK_EQ==OP_Eq );
 79994         -      assert( TK_NE==OP_Ne );
 79995         -      testcase( op==TK_LT );
 79996         -      testcase( op==TK_LE );
 79997         -      testcase( op==TK_GT );
 79998         -      testcase( op==TK_GE );
 79999         -      testcase( op==TK_EQ );
 80000         -      testcase( op==TK_NE );
 80001  80624         testcase( jumpIfNull==0 );
 80002  80625         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80003  80626         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80004  80627         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80005  80628                     r1, r2, dest, jumpIfNull);
        80629  +      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
        80630  +      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
        80631  +      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
        80632  +      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
        80633  +      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
        80634  +      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 80006  80635         testcase( regFree1==0 );
 80007  80636         testcase( regFree2==0 );
 80008  80637         break;
 80009  80638       }
 80010  80639       case TK_IS:
 80011  80640       case TK_ISNOT: {
 80012  80641         testcase( op==TK_IS );
 80013  80642         testcase( op==TK_ISNOT );
 80014  80643         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80015  80644         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80016  80645         op = (op==TK_IS) ? TK_EQ : TK_NE;
 80017  80646         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80018  80647                     r1, r2, dest, SQLITE_NULLEQ);
        80648  +      VdbeCoverageIf(v, op==TK_EQ);
        80649  +      VdbeCoverageIf(v, op==TK_NE);
 80019  80650         testcase( regFree1==0 );
 80020  80651         testcase( regFree2==0 );
 80021  80652         break;
 80022  80653       }
 80023  80654       case TK_ISNULL:
 80024  80655       case TK_NOTNULL: {
 80025         -      assert( TK_ISNULL==OP_IsNull );
 80026         -      assert( TK_NOTNULL==OP_NotNull );
 80027         -      testcase( op==TK_ISNULL );
 80028         -      testcase( op==TK_NOTNULL );
        80656  +      assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
        80657  +      assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
 80029  80658         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80030  80659         sqlite3VdbeAddOp2(v, op, r1, dest);
        80660  +      VdbeCoverageIf(v, op==TK_ISNULL);
        80661  +      VdbeCoverageIf(v, op==TK_NOTNULL);
 80031  80662         testcase( regFree1==0 );
 80032  80663         break;
 80033  80664       }
 80034  80665       case TK_BETWEEN: {
 80035  80666         testcase( jumpIfNull==0 );
 80036  80667         exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
 80037  80668         break;
................................................................................
 80050  80681         if( exprAlwaysTrue(pExpr) ){
 80051  80682           sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 80052  80683         }else if( exprAlwaysFalse(pExpr) ){
 80053  80684           /* No-op */
 80054  80685         }else{
 80055  80686           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 80056  80687           sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
        80688  +        VdbeCoverage(v);
 80057  80689           testcase( regFree1==0 );
 80058  80690           testcase( jumpIfNull==0 );
 80059  80691         }
 80060  80692         break;
 80061  80693       }
 80062  80694     }
 80063  80695     sqlite3ReleaseTempReg(pParse, regFree1);
................................................................................
 80141  80773       }
 80142  80774       case TK_LT:
 80143  80775       case TK_LE:
 80144  80776       case TK_GT:
 80145  80777       case TK_GE:
 80146  80778       case TK_NE:
 80147  80779       case TK_EQ: {
 80148         -      testcase( op==TK_LT );
 80149         -      testcase( op==TK_LE );
 80150         -      testcase( op==TK_GT );
 80151         -      testcase( op==TK_GE );
 80152         -      testcase( op==TK_EQ );
 80153         -      testcase( op==TK_NE );
 80154  80780         testcase( jumpIfNull==0 );
 80155  80781         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80156  80782         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80157  80783         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80158  80784                     r1, r2, dest, jumpIfNull);
        80785  +      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
        80786  +      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
        80787  +      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
        80788  +      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
        80789  +      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
        80790  +      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 80159  80791         testcase( regFree1==0 );
 80160  80792         testcase( regFree2==0 );
 80161  80793         break;
 80162  80794       }
 80163  80795       case TK_IS:
 80164  80796       case TK_ISNOT: {
 80165  80797         testcase( pExpr->op==TK_IS );
 80166  80798         testcase( pExpr->op==TK_ISNOT );
 80167  80799         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80168  80800         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80169  80801         op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
 80170  80802         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80171  80803                     r1, r2, dest, SQLITE_NULLEQ);
        80804  +      VdbeCoverageIf(v, op==TK_EQ);
        80805  +      VdbeCoverageIf(v, op==TK_NE);
 80172  80806         testcase( regFree1==0 );
 80173  80807         testcase( regFree2==0 );
 80174  80808         break;
 80175  80809       }
 80176  80810       case TK_ISNULL:
 80177  80811       case TK_NOTNULL: {
 80178         -      testcase( op==TK_ISNULL );
 80179         -      testcase( op==TK_NOTNULL );
 80180  80812         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80181  80813         sqlite3VdbeAddOp2(v, op, r1, dest);
        80814  +      testcase( op==TK_ISNULL );   VdbeCoverageIf(v, op==TK_ISNULL);
        80815  +      testcase( op==TK_NOTNULL );  VdbeCoverageIf(v, op==TK_NOTNULL);
 80182  80816         testcase( regFree1==0 );
 80183  80817         break;
 80184  80818       }
 80185  80819       case TK_BETWEEN: {
 80186  80820         testcase( jumpIfNull==0 );
 80187  80821         exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
 80188  80822         break;
................................................................................
 80203  80837         if( exprAlwaysFalse(pExpr) ){
 80204  80838           sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 80205  80839         }else if( exprAlwaysTrue(pExpr) ){
 80206  80840           /* no-op */
 80207  80841         }else{
 80208  80842           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 80209  80843           sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
        80844  +        VdbeCoverage(v);
 80210  80845           testcase( regFree1==0 );
 80211  80846           testcase( jumpIfNull==0 );
 80212  80847         }
 80213  80848         break;
 80214  80849       }
 80215  80850     }
 80216  80851     sqlite3ReleaseTempReg(pParse, regFree1);
................................................................................
 80749  81384         do {
 80750  81385           zCsr += len;
 80751  81386           len = sqlite3GetToken(zCsr, &token);
 80752  81387         } while( token==TK_SPACE );
 80753  81388         assert( len>0 );
 80754  81389       } while( token!=TK_LP && token!=TK_USING );
 80755  81390   
 80756         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
 80757         -       zTableName, tname.z+tname.n);
        81391  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
        81392  +       zSql, zTableName, tname.z+tname.n);
 80758  81393       sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 80759  81394     }
 80760  81395   }
 80761  81396   
 80762  81397   /*
 80763  81398   ** This C function implements an SQL user function that is used by SQL code
 80764  81399   ** generated by the ALTER TABLE ... RENAME command to modify the definition
................................................................................
 80802  81437         }while( token==TK_SPACE );
 80803  81438   
 80804  81439         zParent = sqlite3DbStrNDup(db, (const char *)z, n);
 80805  81440         if( zParent==0 ) break;
 80806  81441         sqlite3Dequote(zParent);
 80807  81442         if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
 80808  81443           char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
 80809         -            (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
        81444  +            (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
 80810  81445           );
 80811  81446           sqlite3DbFree(db, zOutput);
 80812  81447           zOutput = zOut;
 80813  81448           zInput = &z[n];
 80814  81449         }
 80815  81450         sqlite3DbFree(db, zParent);
 80816  81451       }
................................................................................
 80888  81523           dist = 0;
 80889  81524         }
 80890  81525       } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
 80891  81526   
 80892  81527       /* Variable tname now contains the token that is the old table-name
 80893  81528       ** in the CREATE TRIGGER statement.
 80894  81529       */
 80895         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
 80896         -       zTableName, tname.z+tname.n);
        81530  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
        81531  +       zSql, zTableName, tname.z+tname.n);
 80897  81532       sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 80898  81533     }
 80899  81534   }
 80900  81535   #endif   /* !SQLITE_OMIT_TRIGGER */
 80901  81536   
 80902  81537   /*
 80903  81538   ** Register built-in functions used to help implement ALTER TABLE
................................................................................
 81141  81776       pVTab = sqlite3GetVTable(db, pTab);
 81142  81777       if( pVTab->pVtab->pModule->xRename==0 ){
 81143  81778         pVTab = 0;
 81144  81779       }
 81145  81780     }
 81146  81781   #endif
 81147  81782   
 81148         -  /* Begin a transaction and code the VerifyCookie for database iDb. 
        81783  +  /* Begin a transaction for database iDb. 
 81149  81784     ** Then modify the schema cookie (since the ALTER TABLE modifies the
 81150  81785     ** schema). Open a statement transaction if the table is a virtual
 81151  81786     ** table.
 81152  81787     */
 81153  81788     v = sqlite3GetVdbe(pParse);
 81154  81789     if( v==0 ){
 81155  81790       goto exit_rename_table;
................................................................................
 81277  81912       int r1 = sqlite3GetTempReg(pParse);
 81278  81913       int r2 = sqlite3GetTempReg(pParse);
 81279  81914       int j1;
 81280  81915       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
 81281  81916       sqlite3VdbeUsesBtree(v, iDb);
 81282  81917       sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
 81283  81918       j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
        81919  +    sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
 81284  81920       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
 81285  81921       sqlite3VdbeJumpHere(v, j1);
 81286  81922       sqlite3ReleaseTempReg(pParse, r1);
 81287  81923       sqlite3ReleaseTempReg(pParse, r2);
 81288  81924     }
 81289  81925   }
 81290  81926   
................................................................................
 82577  83213       **   Rewind csr
 82578  83214       **   if eof(csr) goto end_of_scan;
 82579  83215       **   regChng = 0
 82580  83216       **   goto next_push_0;
 82581  83217       **
 82582  83218       */
 82583  83219       addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
        83220  +    VdbeCoverage(v);
 82584  83221       sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
 82585  83222       addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
 82586  83223   
 82587  83224       /*
 82588  83225       **  next_row:
 82589  83226       **   regChng = 0
 82590  83227       **   if( idx(0) != regPrev(0) ) goto chng_addr_0
................................................................................
 82598  83235       for(i=0; i<nCol; i++){
 82599  83236         char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
 82600  83237         sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
 82601  83238         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
 82602  83239         aGotoChng[i] = 
 82603  83240         sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
 82604  83241         sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
        83242  +      VdbeCoverage(v);
 82605  83243       }
 82606  83244       sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
 82607  83245       aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
 82608  83246   
 82609  83247       /*
 82610  83248       **  chng_addr_0:
 82611  83249       **   regPrev(0) = idx(0)
................................................................................
 82644  83282         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
 82645  83283       }
 82646  83284   #endif
 82647  83285       assert( regChng==(regStat4+1) );
 82648  83286       sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
 82649  83287       sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
 82650  83288       sqlite3VdbeChangeP5(v, 2+IsStat34);
 82651         -    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
        83289  +    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
 82652  83290   
 82653  83291       /* Add the entry to the stat1 table. */
 82654  83292       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
 82655  83293       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
 82656  83294       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 82657  83295       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
 82658  83296       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
................................................................................
 82671  83309         u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
 82672  83310   
 82673  83311         pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
 82674  83312   
 82675  83313         addrNext = sqlite3VdbeCurrentAddr(v);
 82676  83314         callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
 82677  83315         addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
        83316  +      VdbeCoverage(v);
 82678  83317         callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
 82679  83318         callStatGet(v, regStat4, STAT_GET_NLT, regLt);
 82680  83319         callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
 82681  83320         sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
        83321  +      VdbeCoverage(v);
 82682  83322   #ifdef SQLITE_ENABLE_STAT3
 82683  83323         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, 
 82684  83324                                         pIdx->aiColumn[0], regSample);
 82685  83325   #else
 82686  83326         for(i=0; i<nCol; i++){
 82687  83327           i16 iCol = pIdx->aiColumn[i];
 82688  83328           sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
 82689  83329         }
 82690  83330         sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
 82691  83331   #endif
 82692         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
        83332  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
 82693  83333         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
 82694  83334         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
 82695  83335         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
 82696  83336         sqlite3VdbeJumpHere(v, addrIsNull);
 82697  83337       }
 82698  83338   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 82699  83339   
................................................................................
 82705  83345   
 82706  83346     /* Create a single sqlite_stat1 entry containing NULL as the index
 82707  83347     ** name and the row count as the content.
 82708  83348     */
 82709  83349     if( pOnlyIdx==0 && needTableCnt ){
 82710  83350       VdbeComment((v, "%s", pTab->zName));
 82711  83351       sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
 82712         -    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
        83352  +    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
 82713  83353       sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
 82714  83354       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
 82715  83355       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 82716  83356       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
 82717  83357       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 82718  83358       sqlite3VdbeJumpHere(v, jZeroRows);
 82719  83359     }
................................................................................
 84243  84883   
 84244  84884       /* The cookie mask contains one bit for each database file open.
 84245  84885       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
 84246  84886       ** set for each database that is used.  Generate code to start a
 84247  84887       ** transaction on each used database and to verify the schema cookie
 84248  84888       ** on each used database.
 84249  84889       */
 84250         -    if( pParse->cookieGoto>0 ){
        84890  +    if( db->mallocFailed==0 && (pParse->cookieMask || pParse->pConstExpr) ){
 84251  84891         yDbMask mask;
 84252         -      int iDb, i, addr;
 84253         -      sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
        84892  +      int iDb, i;
        84893  +      assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
        84894  +      sqlite3VdbeJumpHere(v, 0);
 84254  84895         for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
 84255  84896           if( (mask & pParse->cookieMask)==0 ) continue;
 84256  84897           sqlite3VdbeUsesBtree(v, iDb);
 84257         -        sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
 84258         -        if( db->init.busy==0 ){
 84259         -          assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 84260         -          sqlite3VdbeAddOp3(v, OP_VerifyCookie,
 84261         -                            iDb, pParse->cookieValue[iDb],
 84262         -                            db->aDb[iDb].pSchema->iGeneration);
 84263         -        }
        84898  +        sqlite3VdbeAddOp4Int(v,
        84899  +          OP_Transaction,                    /* Opcode */
        84900  +          iDb,                               /* P1 */
        84901  +          (mask & pParse->writeMask)!=0,     /* P2 */
        84902  +          pParse->cookieValue[iDb],          /* P3 */
        84903  +          db->aDb[iDb].pSchema->iGeneration  /* P4 */
        84904  +        );
        84905  +        if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
 84264  84906         }
 84265  84907   #ifndef SQLITE_OMIT_VIRTUALTABLE
 84266  84908         for(i=0; i<pParse->nVtabLock; i++){
 84267  84909           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
 84268  84910           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
 84269  84911         }
 84270  84912         pParse->nVtabLock = 0;
................................................................................
 84277  84919         codeTableLocks(pParse);
 84278  84920   
 84279  84921         /* Initialize any AUTOINCREMENT data structures required.
 84280  84922         */
 84281  84923         sqlite3AutoincrementBegin(pParse);
 84282  84924   
 84283  84925         /* Code constant expressions that where factored out of inner loops */
 84284         -      addr = pParse->cookieGoto;
 84285  84926         if( pParse->pConstExpr ){
 84286  84927           ExprList *pEL = pParse->pConstExpr;
 84287         -        pParse->cookieGoto = 0;
        84928  +        pParse->okConstFactor = 0;
 84288  84929           for(i=0; i<pEL->nExpr; i++){
 84289  84930             sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
 84290  84931           }
 84291  84932         }
 84292  84933   
 84293  84934         /* Finally, jump back to the beginning of the executable code. */
 84294         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
        84935  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
 84295  84936       }
 84296  84937     }
 84297  84938   
 84298  84939   
 84299  84940     /* Get the VDBE program ready for execution
 84300  84941     */
 84301  84942     if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
................................................................................
 84310  84951       pParse->rc = SQLITE_ERROR;
 84311  84952     }
 84312  84953     pParse->nTab = 0;
 84313  84954     pParse->nMem = 0;
 84314  84955     pParse->nSet = 0;
 84315  84956     pParse->nVar = 0;
 84316  84957     pParse->cookieMask = 0;
 84317         -  pParse->cookieGoto = 0;
 84318  84958   }
 84319  84959   
 84320  84960   /*
 84321  84961   ** Run the parser and code generator recursively in order to generate
 84322  84962   ** code for the SQL statement given onto the end of the pParse context
 84323  84963   ** currently under construction.  When the parser is run recursively
 84324  84964   ** this way, the final OP_Halt is not appended and other initialization
................................................................................
 85042  85682       ** set them now.
 85043  85683       */
 85044  85684       reg1 = pParse->regRowid = ++pParse->nMem;
 85045  85685       reg2 = pParse->regRoot = ++pParse->nMem;
 85046  85686       reg3 = ++pParse->nMem;
 85047  85687       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
 85048  85688       sqlite3VdbeUsesBtree(v, iDb);
 85049         -    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
        85689  +    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
 85050  85690       fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
 85051  85691                     1 : SQLITE_MAX_FILE_FORMAT;
 85052  85692       sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
 85053  85693       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
 85054  85694       sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
 85055  85695       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
 85056  85696       sqlite3VdbeJumpHere(v, j1);
................................................................................
 86769  87409     iSorter = pParse->nTab++;
 86770  87410     sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)
 86771  87411                       sqlite3KeyInfoRef(pKey), P4_KEYINFO);
 86772  87412   
 86773  87413     /* Open the table. Loop through all rows of the table, inserting index
 86774  87414     ** records into the sorter. */
 86775  87415     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 86776         -  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
        87416  +  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
 86777  87417     regRecord = sqlite3GetTempReg(pParse);
 86778  87418   
 86779  87419     sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
 86780  87420     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
 86781  87421     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
 86782         -  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
        87422  +  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
 86783  87423     sqlite3VdbeJumpHere(v, addr1);
 86784  87424     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
 86785  87425     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 86786  87426                       (char *)pKey, P4_KEYINFO);
 86787  87427     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
 86788  87428   
 86789         -  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
        87429  +  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
 86790  87430     assert( pKey!=0 || db->mallocFailed || pParse->nErr );
 86791  87431     if( pIndex->onError!=OE_None && pKey!=0 ){
 86792  87432       int j2 = sqlite3VdbeCurrentAddr(v) + 3;
 86793  87433       sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
 86794  87434       addr2 = sqlite3VdbeCurrentAddr(v);
 86795  87435       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
 86796         -                         pKey->nField - pIndex->nKeyCol);
        87436  +                         pKey->nField - pIndex->nKeyCol); VdbeCoverage(v);
 86797  87437       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
 86798  87438     }else{
 86799  87439       addr2 = sqlite3VdbeCurrentAddr(v);
 86800  87440     }
 86801  87441     sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
 86802  87442     sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
 86803  87443     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 86804  87444     sqlite3ReleaseTempReg(pParse, regRecord);
 86805         -  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
        87445  +  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
 86806  87446     sqlite3VdbeJumpHere(v, addr1);
 86807  87447   
 86808  87448     sqlite3VdbeAddOp1(v, OP_Close, iTab);
 86809  87449     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
 86810  87450     sqlite3VdbeAddOp1(v, OP_Close, iSorter);
 86811  87451   }
 86812  87452   
................................................................................
 87567  88207     /* Sanity checking on calling parameters */
 87568  88208     assert( iStart>=0 );
 87569  88209     assert( nExtra>=1 );
 87570  88210     assert( pSrc!=0 );
 87571  88211     assert( iStart<=pSrc->nSrc );
 87572  88212   
 87573  88213     /* Allocate additional space if needed */
 87574         -  if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
        88214  +  if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
 87575  88215       SrcList *pNew;
 87576  88216       int nAlloc = pSrc->nSrc+nExtra;
 87577  88217       int nGot;
 87578  88218       pNew = sqlite3DbRealloc(db, pSrc,
 87579  88219                  sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
 87580  88220       if( pNew==0 ){
 87581  88221         assert( db->mallocFailed );
 87582  88222         return pSrc;
 87583  88223       }
 87584  88224       pSrc = pNew;
 87585  88225       nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
 87586         -    pSrc->nAlloc = (u8)nGot;
        88226  +    pSrc->nAlloc = nGot;
 87587  88227     }
 87588  88228   
 87589  88229     /* Move existing slots that come after the newly inserted slots
 87590  88230     ** out of the way */
 87591  88231     for(i=pSrc->nSrc-1; i>=iStart; i--){
 87592  88232       pSrc->a[i+nExtra] = pSrc->a[i];
 87593  88233     }
 87594         -  pSrc->nSrc += (i8)nExtra;
        88234  +  pSrc->nSrc += nExtra;
 87595  88235   
 87596  88236     /* Zero the newly allocated slots */
 87597  88237     memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
 87598  88238     for(i=iStart; i<iStart+nExtra; i++){
 87599  88239       pSrc->a[i].iCursor = -1;
 87600  88240     }
 87601  88241   
................................................................................
 87919  88559         return 1;
 87920  88560       }
 87921  88561     }
 87922  88562     return 0;
 87923  88563   }
 87924  88564   
 87925  88565   /*
 87926         -** Generate VDBE code that will verify the schema cookie and start
 87927         -** a read-transaction for all named database files.
 87928         -**
 87929         -** It is important that all schema cookies be verified and all
 87930         -** read transactions be started before anything else happens in
 87931         -** the VDBE program.  But this routine can be called after much other
 87932         -** code has been generated.  So here is what we do:
 87933         -**
 87934         -** The first time this routine is called, we code an OP_Goto that
 87935         -** will jump to a subroutine at the end of the program.  Then we
 87936         -** record every database that needs its schema verified in the
 87937         -** pParse->cookieMask field.  Later, after all other code has been
 87938         -** generated, the subroutine that does the cookie verifications and
 87939         -** starts the transactions will be coded and the OP_Goto P2 value
 87940         -** will be made to point to that subroutine.  The generation of the
 87941         -** cookie verification subroutine code happens in sqlite3FinishCoding().
 87942         -**
 87943         -** If iDb<0 then code the OP_Goto only - don't set flag to verify the
 87944         -** schema on any databases.  This can be used to position the OP_Goto
 87945         -** early in the code, before we know if any database tables will be used.
        88566  +** Record the fact that the schema cookie will need to be verified
        88567  +** for database iDb.  The code to actually verify the schema cookie
        88568  +** will occur at the end of the top-level VDBE and will be generated
        88569  +** later, by sqlite3FinishCoding().
 87946  88570   */
 87947  88571   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
 87948  88572     Parse *pToplevel = sqlite3ParseToplevel(pParse);
        88573  +  sqlite3 *db = pToplevel->db;
        88574  +  yDbMask mask;
 87949  88575   
 87950         -#ifndef SQLITE_OMIT_TRIGGER
 87951         -  if( pToplevel!=pParse ){
 87952         -    /* This branch is taken if a trigger is currently being coded. In this
 87953         -    ** case, set cookieGoto to a non-zero value to show that this function
 87954         -    ** has been called. This is used by the sqlite3ExprCodeConstants()
 87955         -    ** function. */
 87956         -    pParse->cookieGoto = -1;
 87957         -  }
 87958         -#endif
 87959         -  if( pToplevel->cookieGoto==0 ){
 87960         -    Vdbe *v = sqlite3GetVdbe(pToplevel);
 87961         -    if( v==0 ) return;  /* This only happens if there was a prior error */
 87962         -    pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
 87963         -  }
 87964         -  if( iDb>=0 ){
 87965         -    sqlite3 *db = pToplevel->db;
 87966         -    yDbMask mask;
 87967         -
 87968         -    assert( iDb<db->nDb );
 87969         -    assert( db->aDb[iDb].pBt!=0 || iDb==1 );
 87970         -    assert( iDb<SQLITE_MAX_ATTACHED+2 );
 87971         -    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 87972         -    mask = ((yDbMask)1)<<iDb;
 87973         -    if( (pToplevel->cookieMask & mask)==0 ){
 87974         -      pToplevel->cookieMask |= mask;
 87975         -      pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
 87976         -      if( !OMIT_TEMPDB && iDb==1 ){
 87977         -        sqlite3OpenTempDatabase(pToplevel);
 87978         -      }
        88576  +  assert( iDb>=0 && iDb<db->nDb );
        88577  +  assert( db->aDb[iDb].pBt!=0 || iDb==1 );
        88578  +  assert( iDb<SQLITE_MAX_ATTACHED+2 );
        88579  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
        88580  +  mask = ((yDbMask)1)<<iDb;
        88581  +  if( (pToplevel->cookieMask & mask)==0 ){
        88582  +    pToplevel->cookieMask |= mask;
        88583  +    pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
        88584  +    if( !OMIT_TEMPDB && iDb==1 ){
        88585  +      sqlite3OpenTempDatabase(pToplevel);
 87979  88586       }
 87980  88587     }
 87981  88588   }
 87982  88589   
 87983  88590   /*
 87984  88591   ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
 87985  88592   ** attached database. Otherwise, invoke it for the database named zDb only.
................................................................................
 88942  89549     int iCur             /* Cursor number for ephemerial table */
 88943  89550   ){
 88944  89551     SelectDest dest;
 88945  89552     Select *pSel;
 88946  89553     SrcList *pFrom;
 88947  89554     sqlite3 *db = pParse->db;
 88948  89555     int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
 88949         -
 88950  89556     pWhere = sqlite3ExprDup(db, pWhere, 0);
 88951  89557     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
 88952         -
 88953  89558     if( pFrom ){
 88954  89559       assert( pFrom->nSrc==1 );
 88955  89560       pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
 88956  89561       pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
 88957  89562       assert( pFrom->a[0].pOn==0 );
 88958  89563       assert( pFrom->a[0].pUsing==0 );
 88959  89564     }
 88960         -
 88961  89565     pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
 88962         -  if( pSel ) pSel->selFlags |= SF_Materialize;
 88963         -
 88964  89566     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
 88965  89567     sqlite3Select(pParse, pSel, &dest);
 88966  89568     sqlite3SelectDelete(db, pSel);
 88967  89569   }
 88968  89570   #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
 88969  89571   
 88970  89572   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
................................................................................
 89293  89895         if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
 89294  89896         addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
 89295  89897       }else if( pPk ){
 89296  89898         /* Construct a composite key for the row to be deleted and remember it */
 89297  89899         iKey = ++pParse->nMem;
 89298  89900         nKey = 0;   /* Zero tells OP_Found to use a composite key */
 89299  89901         sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
 89300         -                        sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
        89902  +                        sqlite3IndexAffinityStr(v, pPk), nPk);
 89301  89903         sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
 89302  89904       }else{
 89303  89905         /* Get the rowid of the row to be deleted and remember it in the RowSet */
 89304  89906         nKey = 1;  /* OP_Seek always uses a single rowid */
 89305  89907         sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
 89306  89908       }
 89307  89909     
................................................................................
 89331  89933       */
 89332  89934       if( okOnePass ){
 89333  89935         /* Just one row.  Hence the top-of-loop is a no-op */
 89334  89936         assert( nKey==nPk ); /* OP_Found will use an unpacked key */
 89335  89937         if( aToOpen[iDataCur-iTabCur] ){
 89336  89938           assert( pPk!=0 );
 89337  89939           sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
        89940  +        VdbeCoverage(v);
 89338  89941         }
 89339  89942       }else if( pPk ){
 89340         -      addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur);
        89943  +      addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
 89341  89944         sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
 89342  89945         assert( nKey==0 );  /* OP_Found will use a composite key */
 89343  89946       }else{
 89344  89947         addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
        89948  +      VdbeCoverage(v);
 89345  89949         assert( nKey==1 );
 89346  89950       }  
 89347  89951     
 89348  89952       /* Delete the row */
 89349  89953   #ifndef SQLITE_OMIT_VIRTUALTABLE
 89350  89954       if( IsVirtual(pTab) ){
 89351  89955         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
................................................................................
 89361  89965                                  iKey, nKey, count, OE_Default, okOnePass);
 89362  89966       }
 89363  89967     
 89364  89968       /* End of the loop over all rowids/primary-keys. */
 89365  89969       if( okOnePass ){
 89366  89970         sqlite3VdbeResolveLabel(v, addrBypass);
 89367  89971       }else if( pPk ){
 89368         -      sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1);
        89972  +      sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
 89369  89973         sqlite3VdbeJumpHere(v, addrLoop);
 89370  89974       }else{
 89371  89975         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
 89372  89976         sqlite3VdbeJumpHere(v, addrLoop);
 89373  89977       }     
 89374  89978     
 89375  89979       /* Close the cursors open on the table and its indexes. */
................................................................................
 89459  90063                            iDataCur, iIdxCur, iPk, (int)nPk));
 89460  90064   
 89461  90065     /* Seek cursor iCur to the row to delete. If this row no longer exists 
 89462  90066     ** (this can happen if a trigger program has already deleted it), do
 89463  90067     ** not attempt to delete it or fire any DELETE triggers.  */
 89464  90068     iLabel = sqlite3VdbeMakeLabel(v);
 89465  90069     opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
 89466         -  if( !bNoSeek ) sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
        90070  +  if( !bNoSeek ){
        90071  +    sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
        90072  +    VdbeCoverageIf(v, opSeek==OP_NotExists);
        90073  +    VdbeCoverageIf(v, opSeek==OP_NotFound);
        90074  +  }
 89467  90075    
 89468  90076     /* If there are any triggers to fire, allocate a range of registers to
 89469  90077     ** use for the old.* references in the triggers.  */
 89470  90078     if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
 89471  90079       u32 mask;                     /* Mask of OLD.* columns in use */
 89472  90080       int iCol;                     /* Iterator used while populating OLD.* */
 89473  90081       int addrStart;                /* Start of BEFORE trigger programs */
................................................................................
 89501  90109       /* If any BEFORE triggers were coded, then seek the cursor to the 
 89502  90110       ** row to be deleted again. It may be that the BEFORE triggers moved
 89503  90111       ** the cursor or of already deleted the row that the cursor was
 89504  90112       ** pointing to.
 89505  90113       */
 89506  90114       if( addrStart<sqlite3VdbeCurrentAddr(v) ){
 89507  90115         sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
        90116  +      VdbeCoverageIf(v, opSeek==OP_NotExists);
        90117  +      VdbeCoverageIf(v, opSeek==OP_NotFound);
 89508  90118       }
 89509  90119   
 89510  90120       /* Do FK processing. This call checks that any FK constraints that
 89511  90121       ** refer to this table (i.e. constraints attached to other tables) 
 89512  90122       ** are not violated by deleting this row.  */
 89513  90123       sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
 89514  90124     }
................................................................................
 91758  92368     ** to check if deleting this row resolves any outstanding violations.
 91759  92369     **
 91760  92370     ** Check if any of the key columns in the child table row are NULL. If 
 91761  92371     ** any are, then the constraint is considered satisfied. No need to 
 91762  92372     ** search for a matching row in the parent table.  */
 91763  92373     if( nIncr<0 ){
 91764  92374       sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
        92375  +    VdbeCoverage(v);
 91765  92376     }
 91766  92377     for(i=0; i<pFKey->nCol; i++){
 91767  92378       int iReg = aiCol[i] + regData + 1;
 91768         -    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
        92379  +    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
 91769  92380     }
 91770  92381   
 91771  92382     if( isIgnore==0 ){
 91772  92383       if( pIdx==0 ){
 91773  92384         /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
 91774  92385         ** column of the parent table (table pTab).  */
 91775  92386         int iMustBeInt;               /* Address of MustBeInt instruction */
................................................................................
 91778  92389         /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
 91779  92390         ** apply the affinity of the parent key). If this fails, then there
 91780  92391         ** is no matching parent key. Before using MustBeInt, make a copy of
 91781  92392         ** the value. Otherwise, the value inserted into the child key column
 91782  92393         ** will have INTEGER affinity applied to it, which may not be correct.  */
 91783  92394         sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
 91784  92395         iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
        92396  +      VdbeCoverage(v);
 91785  92397     
 91786  92398         /* If the parent table is the same as the child table, and we are about
 91787  92399         ** to increment the constraint-counter (i.e. this is an INSERT operation),
 91788  92400         ** then check if the row being inserted matches itself. If so, do not
 91789  92401         ** increment the constraint-counter.  */
 91790  92402         if( pTab==pFKey->pFrom && nIncr==1 ){
 91791         -        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
        92403  +        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
        92404  +        sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
 91792  92405         }
 91793  92406     
 91794  92407         sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
 91795         -      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
        92408  +      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
 91796  92409         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 91797  92410         sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
 91798  92411         sqlite3VdbeJumpHere(v, iMustBeInt);
 91799  92412         sqlite3ReleaseTempReg(pParse, regTemp);
 91800  92413       }else{
 91801  92414         int nCol = pFKey->nCol;
 91802  92415         int regTemp = sqlite3GetTempRange(pParse, nCol);
................................................................................
 91824  92437             int iChild = aiCol[i]+1+regData;
 91825  92438             int iParent = pIdx->aiColumn[i]+1+regData;
 91826  92439             assert( aiCol[i]!=pTab->iPKey );
 91827  92440             if( pIdx->aiColumn[i]==pTab->iPKey ){
 91828  92441               /* The parent key is a composite key that includes the IPK column */
 91829  92442               iParent = regData;
 91830  92443             }
 91831         -          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
        92444  +          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
 91832  92445             sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 91833  92446           }
 91834  92447           sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 91835  92448         }
 91836  92449     
 91837         -      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
 91838         -      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
 91839         -      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
        92450  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
        92451  +                        sqlite3IndexAffinityStr(v,pIdx), nCol);
        92452  +      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
 91840  92453     
 91841  92454         sqlite3ReleaseTempReg(pParse, regRec);
 91842  92455         sqlite3ReleaseTempRange(pParse, regTemp, nCol);
 91843  92456       }
 91844  92457     }
 91845  92458   
 91846  92459     if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
................................................................................
 91970  92583     assert( pIdx==0 || pIdx->pTable==pTab );
 91971  92584     assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
 91972  92585     assert( pIdx!=0 || pFKey->nCol==1 );
 91973  92586     assert( pIdx!=0 || HasRowid(pTab) );
 91974  92587   
 91975  92588     if( nIncr<0 ){
 91976  92589       iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
        92590  +    VdbeCoverage(v);
 91977  92591     }
 91978  92592   
 91979  92593     /* Create an Expr object representing an SQL expression like:
 91980  92594     **
 91981  92595     **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
 91982  92596     **
 91983  92597     ** The collation sequence used for the comparison should be that of
................................................................................
 92132  92746         ** when this statement is run.  */
 92133  92747         FKey *p;
 92134  92748         for(p=pTab->pFKey; p; p=p->pNextFrom){
 92135  92749           if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
 92136  92750         }
 92137  92751         if( !p ) return;
 92138  92752         iSkip = sqlite3VdbeMakeLabel(v);
 92139         -      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
        92753  +      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
 92140  92754       }
 92141  92755   
 92142  92756       pParse->disableTriggers = 1;
 92143  92757       sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
 92144  92758       pParse->disableTriggers = 0;
 92145  92759   
 92146  92760       /* If the DELETE has generated immediate foreign key constraint 
................................................................................
 92150  92764       **
 92151  92765       ** If the SQLITE_DeferFKs flag is set, then this is not required, as
 92152  92766       ** the statement transaction will not be rolled back even if FK
 92153  92767       ** constraints are violated.
 92154  92768       */
 92155  92769       if( (db->flags & SQLITE_DeferFKs)==0 ){
 92156  92770         sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
        92771  +      VdbeCoverage(v);
 92157  92772         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
 92158  92773             OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
 92159  92774       }
 92160  92775   
 92161  92776       if( iSkip ){
 92162  92777         sqlite3VdbeResolveLabel(v, iSkip);
 92163  92778       }
................................................................................
 92309  92924           ** missing, behave as if it is empty. i.e. decrement the relevant
 92310  92925           ** FK counter for each row of the current table with non-NULL keys.
 92311  92926           */
 92312  92927           Vdbe *v = sqlite3GetVdbe(pParse);
 92313  92928           int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
 92314  92929           for(i=0; i<pFKey->nCol; i++){
 92315  92930             int iReg = pFKey->aCol[i].iFrom + regOld + 1;
 92316         -          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
        92931  +          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
 92317  92932           }
 92318  92933           sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
 92319  92934         }
 92320  92935         continue;
 92321  92936       }
 92322  92937       assert( pFKey->nCol==1 || (aiFree && pIdx) );
 92323  92938   
................................................................................
 92876  93491       pIdx->zColAff[n] = 0;
 92877  93492     }
 92878  93493    
 92879  93494     return pIdx->zColAff;
 92880  93495   }
 92881  93496   
 92882  93497   /*
 92883         -** Set P4 of the most recently inserted opcode to a column affinity
 92884         -** string for table pTab. A column affinity string has one character
 92885         -** for each column indexed by the index, according to the affinity of the
 92886         -** column:
        93498  +** Compute the affinity string for table pTab, if it has not already been
        93499  +** computed.  As an optimization, omit trailing SQLITE_AFF_NONE affinities.
        93500  +**
        93501  +** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values and
        93502  +** if iReg>0 then code an OP_Affinity opcode that will set the affinities
        93503  +** for register iReg and following.  Or if affinities exists and iReg==0,
        93504  +** then just set the P4 operand of the previous opcode (which should  be
        93505  +** an OP_MakeRecord) to the affinity string.
        93506  +**
        93507  +** A column affinity string has one character column:
 92887  93508   **
 92888  93509   **  Character      Column affinity
 92889  93510   **  ------------------------------
 92890  93511   **  'a'            TEXT
 92891  93512   **  'b'            NONE
 92892  93513   **  'c'            NUMERIC
 92893  93514   **  'd'            INTEGER
 92894  93515   **  'e'            REAL
 92895  93516   */
 92896         -SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
 92897         -  /* The first time a column affinity string for a particular table
 92898         -  ** is required, it is allocated and populated here. It is then 
 92899         -  ** stored as a member of the Table structure for subsequent use.
 92900         -  **
 92901         -  ** The column affinity string will eventually be deleted by
 92902         -  ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
 92903         -  */
 92904         -  if( !pTab->zColAff ){
 92905         -    char *zColAff;
 92906         -    int i;
        93517  +SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
        93518  +  int i;
        93519  +  char *zColAff = pTab->zColAff;
        93520  +  if( zColAff==0 ){
 92907  93521       sqlite3 *db = sqlite3VdbeDb(v);
 92908         -
 92909  93522       zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
 92910  93523       if( !zColAff ){
 92911  93524         db->mallocFailed = 1;
 92912  93525         return;
 92913  93526       }
 92914  93527   
 92915  93528       for(i=0; i<pTab->nCol; i++){
 92916  93529         zColAff[i] = pTab->aCol[i].affinity;
 92917  93530       }
 92918         -    zColAff[pTab->nCol] = '\0';
 92919         -
        93531  +    do{
        93532  +      zColAff[i--] = 0;
        93533  +    }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
 92920  93534       pTab->zColAff = zColAff;
 92921  93535     }
 92922         -
 92923         -  sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
        93536  +  i = sqlite3Strlen30(zColAff);
        93537  +  if( i ){
        93538  +    if( iReg ){
        93539  +      sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
        93540  +    }else{
        93541  +      sqlite3VdbeChangeP4(v, -1, zColAff, i);
        93542  +    }
        93543  +  }
 92924  93544   }
 92925  93545   
 92926  93546   /*
 92927  93547   ** Return non-zero if the table pTab in database iDb or any of its indices
 92928  93548   ** have been opened at any point in the VDBE program beginning at location
 92929  93549   ** iStartAddr throught the end of the program.  This is used to see if 
 92930  93550   ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
 92931  93551   ** run without using temporary table for the results of the SELECT. 
 92932  93552   */
 92933         -static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
        93553  +static int readsTable(Parse *p, int iDb, Table *pTab){
 92934  93554     Vdbe *v = sqlite3GetVdbe(p);
 92935  93555     int i;
 92936  93556     int iEnd = sqlite3VdbeCurrentAddr(v);
 92937  93557   #ifndef SQLITE_OMIT_VIRTUALTABLE
 92938  93558     VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
 92939  93559   #endif
 92940  93560   
 92941         -  for(i=iStartAddr; i<iEnd; i++){
        93561  +  for(i=1; i<iEnd; i++){
 92942  93562       VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
 92943  93563       assert( pOp!=0 );
 92944  93564       if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
 92945  93565         Index *pIndex;
 92946  93566         int tnum = pOp->p2;
 92947  93567         if( tnum==pTab->tnum ){
 92948  93568           return 1;
................................................................................
 93035  93655       pDb = &db->aDb[p->iDb];
 93036  93656       memId = p->regCtr;
 93037  93657       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 93038  93658       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
 93039  93659       sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
 93040  93660       addr = sqlite3VdbeCurrentAddr(v);
 93041  93661       sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
 93042         -    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
        93662  +    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
 93043  93663       sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
 93044         -    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
        93664  +    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
 93045  93665       sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 93046  93666       sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
 93047  93667       sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
 93048  93668       sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
 93049         -    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
        93669  +    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
 93050  93670       sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
 93051  93671       sqlite3VdbeAddOp0(v, OP_Close);
 93052  93672     }
 93053  93673   }
 93054  93674   
 93055  93675   /*
 93056  93676   ** Update the maximum rowid for an autoincrement calculation.
................................................................................
 93077  93697     AutoincInfo *p;
 93078  93698     Vdbe *v = pParse->pVdbe;
 93079  93699     sqlite3 *db = pParse->db;
 93080  93700   
 93081  93701     assert( v );
 93082  93702     for(p = pParse->pAinc; p; p = p->pNext){
 93083  93703       Db *pDb = &db->aDb[p->iDb];
 93084         -    int j1, j2, j3, j4, j5;
        93704  +    int j1;
 93085  93705       int iRec;
 93086  93706       int memId = p->regCtr;
 93087  93707   
 93088  93708       iRec = sqlite3GetTempReg(pParse);
 93089  93709       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 93090  93710       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
 93091         -    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
 93092         -    j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
 93093         -    j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
 93094         -    j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
 93095         -    sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
 93096         -    sqlite3VdbeJumpHere(v, j2);
        93711  +    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
 93097  93712       sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
 93098         -    j5 = sqlite3VdbeAddOp0(v, OP_Goto);
 93099         -    sqlite3VdbeJumpHere(v, j4);
 93100         -    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
 93101  93713       sqlite3VdbeJumpHere(v, j1);
 93102         -    sqlite3VdbeJumpHere(v, j5);
 93103  93714       sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
 93104  93715       sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
 93105  93716       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 93106  93717       sqlite3VdbeAddOp0(v, OP_Close);
 93107  93718       sqlite3ReleaseTempReg(pParse, iRec);
 93108  93719     }
 93109  93720   }
................................................................................
 93111  93722   /*
 93112  93723   ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
 93113  93724   ** above are all no-ops
 93114  93725   */
 93115  93726   # define autoIncBegin(A,B,C) (0)
 93116  93727   # define autoIncStep(A,B,C)
 93117  93728   #endif /* SQLITE_OMIT_AUTOINCREMENT */
 93118         -
 93119         -
 93120         -/*
 93121         -** Generate code for a co-routine that will evaluate a subquery one
 93122         -** row at a time.
 93123         -**
 93124         -** The pSelect parameter is the subquery that the co-routine will evaluation.
 93125         -** Information about the location of co-routine and the registers it will use
 93126         -** is returned by filling in the pDest object.
 93127         -**
 93128         -** Registers are allocated as follows:
 93129         -**
 93130         -**   pDest->iSDParm      The register holding the next entry-point of the
 93131         -**                       co-routine.  Run the co-routine to its next breakpoint
 93132         -**                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
 93133         -**
 93134         -**   pDest->iSDParm+1    The register holding the "completed" flag for the
 93135         -**                       co-routine. This register is 0 if the previous Yield
 93136         -**                       generated a new result row, or 1 if the subquery
 93137         -**                       has completed.  If the Yield is called again
 93138         -**                       after this register becomes 1, then the VDBE will
 93139         -**                       halt with an SQLITE_INTERNAL error.
 93140         -**
 93141         -**   pDest->iSdst        First result register.
 93142         -**
 93143         -**   pDest->nSdst        Number of result registers.
 93144         -**
 93145         -** This routine handles all of the register allocation and fills in the
 93146         -** pDest structure appropriately.
 93147         -**
 93148         -** Here is a schematic of the generated code assuming that X is the 
 93149         -** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
 93150         -** completed flag reg[pDest->iSDParm+1], and R and S are the range of
 93151         -** registers that hold the result set, reg[pDest->iSdst] through
 93152         -** reg[pDest->iSdst+pDest->nSdst-1]:
 93153         -**
 93154         -**         X <- A
 93155         -**         EOF <- 0
 93156         -**         goto B
 93157         -**      A: setup for the SELECT
 93158         -**         loop rows in the SELECT
 93159         -**           load results into registers R..S
 93160         -**           yield X
 93161         -**         end loop
 93162         -**         cleanup after the SELECT
 93163         -**         EOF <- 1
 93164         -**         yield X
 93165         -**         halt-error
 93166         -**      B:
 93167         -**
 93168         -** To use this subroutine, the caller generates code as follows:
 93169         -**
 93170         -**         [ Co-routine generated by this subroutine, shown above ]
 93171         -**      S: yield X
 93172         -**         if EOF goto E
 93173         -**         if skip this row, goto C
 93174         -**         if terminate loop, goto E
 93175         -**         deal with this row
 93176         -**      C: goto S
 93177         -**      E:
 93178         -*/
 93179         -SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
 93180         -  int regYield;       /* Register holding co-routine entry-point */
 93181         -  int regEof;         /* Register holding co-routine completion flag */
 93182         -  int addrTop;        /* Top of the co-routine */
 93183         -  int j1;             /* Jump instruction */
 93184         -  int rc;             /* Result code */
 93185         -  Vdbe *v;            /* VDBE under construction */
 93186         -
 93187         -  regYield = ++pParse->nMem;
 93188         -  regEof = ++pParse->nMem;
 93189         -  v = sqlite3GetVdbe(pParse);
 93190         -  addrTop = sqlite3VdbeCurrentAddr(v);
 93191         -  sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
 93192         -  VdbeComment((v, "Co-routine entry point"));
 93193         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
 93194         -  VdbeComment((v, "Co-routine completion flag"));
 93195         -  sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
 93196         -  j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 93197         -  rc = sqlite3Select(pParse, pSelect, pDest);
 93198         -  assert( pParse->nErr==0 || rc );
 93199         -  if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
 93200         -  if( rc ) return rc;
 93201         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
 93202         -  sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
 93203         -  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
 93204         -  VdbeComment((v, "End of coroutine"));
 93205         -  sqlite3VdbeJumpHere(v, j1);                             /* label B: */
 93206         -  return rc;
 93207         -}
 93208         -
 93209  93729   
 93210  93730   
 93211  93731   /* Forward declaration */
 93212  93732   static int xferOptimization(
 93213  93733     Parse *pParse,        /* Parser context */
 93214  93734     Table *pDest,         /* The table we are inserting into */
 93215  93735     Select *pSelect,      /* A SELECT statement to use as the data source */
................................................................................
 93266  93786   **           close cursors
 93267  93787   **         end foreach
 93268  93788   **
 93269  93789   ** The 3rd template is for when the second template does not apply
 93270  93790   ** and the SELECT clause does not read from <table> at any time.
 93271  93791   ** The generated code follows this template:
 93272  93792   **
 93273         -**         EOF <- 0
 93274  93793   **         X <- A
 93275  93794   **         goto B
 93276  93795   **      A: setup for the SELECT
 93277  93796   **         loop over the rows in the SELECT
 93278  93797   **           load values into registers R..R+n
 93279  93798   **           yield X
 93280  93799   **         end loop
 93281  93800   **         cleanup after the SELECT
 93282         -**         EOF <- 1
 93283         -**         yield X
 93284         -**         goto A
        93801  +**         end-coroutine X
 93285  93802   **      B: open write cursor to <table> and its indices
 93286         -**      C: yield X
 93287         -**         if EOF goto D
        93803  +**      C: yield X, at EOF goto D
 93288  93804   **         insert the select result into <table> from R..R+n
 93289  93805   **         goto C
 93290  93806   **      D: cleanup
 93291  93807   **
 93292  93808   ** The 4th template is used if the insert statement takes its
 93293  93809   ** values from a SELECT but the data is being inserted into a table
 93294  93810   ** that is also read as part of the SELECT.  In the third form,
 93295  93811   ** we have to use a intermediate table to store the results of
 93296  93812   ** the select.  The template is like this:
 93297  93813   **
 93298         -**         EOF <- 0
 93299  93814   **         X <- A
 93300  93815   **         goto B
 93301  93816   **      A: setup for the SELECT
 93302  93817   **         loop over the tables in the SELECT
 93303  93818   **           load value into register R..R+n
 93304  93819   **           yield X
 93305  93820   **         end loop
 93306  93821   **         cleanup after the SELECT
 93307         -**         EOF <- 1
 93308         -**         yield X
 93309         -**         halt-error
        93822  +**         end co-routine R
 93310  93823   **      B: open temp table
 93311         -**      L: yield X
 93312         -**         if EOF goto M
        93824  +**      L: yield X, at EOF goto M
 93313  93825   **         insert row from R..R+n into temp table
 93314  93826   **         goto L
 93315  93827   **      M: open write cursor to <table> and its indices
 93316  93828   **         rewind temp table
 93317  93829   **      C: loop over rows of intermediate table
 93318  93830   **           transfer values form intermediate table into <table>
 93319  93831   **         end loop
................................................................................
 93335  93847     Index *pIdx;          /* For looping over indices of the table */
 93336  93848     int nColumn;          /* Number of columns in the data */
 93337  93849     int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
 93338  93850     int iDataCur = 0;     /* VDBE cursor that is the main data repository */
 93339  93851     int iIdxCur = 0;      /* First index cursor */
 93340  93852     int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
 93341  93853     int endOfLoop;        /* Label for the end of the insertion loop */
 93342         -  int useTempTable = 0; /* Store SELECT results in intermediate table */
 93343  93854     int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
 93344  93855     int addrInsTop = 0;   /* Jump to label "D" */
 93345  93856     int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
 93346         -  int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
 93347  93857     SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
 93348  93858     int iDb;              /* Index of database holding TABLE */
 93349  93859     Db *pDb;              /* The database containing table being inserted into */
 93350         -  int appendFlag = 0;   /* True if the insert is likely to be an append */
 93351         -  int withoutRowid;     /* 0 for normal table.  1 for WITHOUT ROWID table */
        93860  +  u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
        93861  +  u8 appendFlag = 0;    /* True if the insert is likely to be an append */
        93862  +  u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
        93863  +  u8 bIdListInOrder = 1; /* True if IDLIST is in table order */
 93352  93864     ExprList *pList = 0;  /* List of VALUES() to be inserted  */
 93353  93865   
 93354  93866     /* Register allocations */
 93355  93867     int regFromSelect = 0;/* Base register for data coming from SELECT */
 93356  93868     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
 93357  93869     int regRowCount = 0;  /* Memory cell used for the row counter */
 93358  93870     int regIns;           /* Block of regs holding rowid+data being inserted */
 93359  93871     int regRowid;         /* registers holding insert rowid */
 93360  93872     int regData;          /* register holding first column to insert */
 93361         -  int regEof = 0;       /* Register recording end of SELECT data */
 93362  93873     int *aRegIdx = 0;     /* One register allocated to each index */
 93363  93874   
 93364  93875   #ifndef SQLITE_OMIT_TRIGGER
 93365  93876     int isView;                 /* True if attempting to insert into a view */
 93366  93877     Trigger *pTrigger;          /* List of triggers on pTab, if required */
 93367  93878     int tmask;                  /* Mask of trigger times */
 93368  93879   #endif
................................................................................
 93456  93967     }
 93457  93968   #endif /* SQLITE_OMIT_XFER_OPT */
 93458  93969   
 93459  93970     /* If this is an AUTOINCREMENT table, look up the sequence number in the
 93460  93971     ** sqlite_sequence table and store it in memory cell regAutoinc.
 93461  93972     */
 93462  93973     regAutoinc = autoIncBegin(pParse, iDb, pTab);
        93974  +
        93975  +  /* Allocate registers for holding the rowid of the new row,
        93976  +  ** the content of the new row, and the assemblied row record.
        93977  +  */
        93978  +  regRowid = regIns = pParse->nMem+1;
        93979  +  pParse->nMem += pTab->nCol + 1;
        93980  +  if( IsVirtual(pTab) ){
        93981  +    regRowid++;
        93982  +    pParse->nMem++;
        93983  +  }
        93984  +  regData = regRowid+1;
        93985  +
        93986  +  /* If the INSERT statement included an IDLIST term, then make sure
        93987  +  ** all elements of the IDLIST really are columns of the table and 
        93988  +  ** remember the column indices.
        93989  +  **
        93990  +  ** If the table has an INTEGER PRIMARY KEY column and that column
        93991  +  ** is named in the IDLIST, then record in the ipkColumn variable
        93992  +  ** the index into IDLIST of the primary key column.  ipkColumn is
        93993  +  ** the index of the primary key as it appears in IDLIST, not as
        93994  +  ** is appears in the original table.  (The index of the INTEGER
        93995  +  ** PRIMARY KEY in the original table is pTab->iPKey.)
        93996  +  */
        93997  +  if( pColumn ){
        93998  +    for(i=0; i<pColumn->nId; i++){
        93999  +      pColumn->a[i].idx = -1;
        94000  +    }
        94001  +    for(i=0; i<pColumn->nId; i++){
        94002  +      for(j=0; j<pTab->nCol; j++){
        94003  +        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
        94004  +          pColumn->a[i].idx = j;
        94005  +          if( i!=j ) bIdListInOrder = 0;
        94006  +          if( j==pTab->iPKey ){
        94007  +            ipkColumn = i;  assert( !withoutRowid );
        94008  +          }
        94009  +          break;
        94010  +        }
        94011  +      }
        94012  +      if( j>=pTab->nCol ){
        94013  +        if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
        94014  +          ipkColumn = i;
        94015  +        }else{
        94016  +          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
        94017  +              pTabList, 0, pColumn->a[i].zName);
        94018  +          pParse->checkSchema = 1;
        94019  +          goto insert_cleanup;
        94020  +        }
        94021  +      }
        94022  +    }
        94023  +  }
 93463  94024   
 93464  94025     /* Figure out how many columns of data are supplied.  If the data
 93465  94026     ** is coming from a SELECT statement, then generate a co-routine that
 93466  94027     ** produces a single row of the SELECT on each invocation.  The
 93467  94028     ** co-routine is the common header to the 3rd and 4th templates.
 93468  94029     */
 93469  94030     if( pSelect ){
 93470  94031       /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
 93471         -    int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
 93472         -    if( rc ) goto insert_cleanup;
        94032  +    int regYield;       /* Register holding co-routine entry-point */
        94033  +    int addrTop;        /* Top of the co-routine */
        94034  +    int rc;             /* Result code */
 93473  94035   
 93474         -    regEof = dest.iSDParm + 1;
        94036  +    regYield = ++pParse->nMem;
        94037  +    addrTop = sqlite3VdbeCurrentAddr(v) + 1;
        94038  +    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
        94039  +    sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
        94040  +    dest.iSdst = bIdListInOrder ? regData : 0;
        94041  +    dest.nSdst = pTab->nCol;
        94042  +    rc = sqlite3Select(pParse, pSelect, &dest);
 93475  94043       regFromSelect = dest.iSdst;
        94044  +    assert( pParse->nErr==0 || rc );
        94045  +    if( rc || db->mallocFailed ) goto insert_cleanup;
        94046  +    sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
        94047  +    sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
 93476  94048       assert( pSelect->pEList );
 93477  94049       nColumn = pSelect->pEList->nExpr;
 93478         -    assert( dest.nSdst==nColumn );
 93479  94050   
 93480  94051       /* Set useTempTable to TRUE if the result of the SELECT statement
 93481  94052       ** should be written into a temporary table (template 4).  Set to
 93482  94053       ** FALSE if each output row of the SELECT can be written directly into
 93483  94054       ** the destination table (template 3).
 93484  94055       **
 93485  94056       ** A temp table must be used if the table being updated is also one
 93486  94057       ** of the tables being read by the SELECT statement.  Also use a 
 93487  94058       ** temp table in the case of row triggers.
 93488  94059       */
 93489         -    if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
        94060  +    if( pTrigger || readsTable(pParse, iDb, pTab) ){
 93490  94061         useTempTable = 1;
 93491  94062       }
 93492  94063   
 93493  94064       if( useTempTable ){
 93494  94065         /* Invoke the coroutine to extract information from the SELECT
 93495  94066         ** and add it to a transient table srcTab.  The code generated
 93496  94067         ** here is from the 4th template:
 93497  94068         **
 93498  94069         **      B: open temp table
 93499         -      **      L: yield X
 93500         -      **         if EOF goto M
        94070  +      **      L: yield X, goto M at EOF
 93501  94071         **         insert row from R..R+n into temp table
 93502  94072         **         goto L
 93503  94073         **      M: ...
 93504  94074         */
 93505  94075         int regRec;          /* Register to hold packed record */
 93506  94076         int regTempRowid;    /* Register to hold temp table ROWID */
 93507         -      int addrTop;         /* Label "L" */
 93508         -      int addrIf;          /* Address of jump to M */
        94077  +      int addrL;           /* Label "L" */
 93509  94078   
 93510  94079         srcTab = pParse->nTab++;
 93511  94080         regRec = sqlite3GetTempReg(pParse);
 93512  94081         regTempRowid = sqlite3GetTempReg(pParse);
 93513  94082         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
 93514         -      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 93515         -      addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
        94083  +      addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
 93516  94084         sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
 93517  94085         sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
 93518  94086         sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
 93519         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
 93520         -      sqlite3VdbeJumpHere(v, addrIf);
        94087  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrL);
        94088  +      sqlite3VdbeJumpHere(v, addrL);
 93521  94089         sqlite3ReleaseTempReg(pParse, regRec);
 93522  94090         sqlite3ReleaseTempReg(pParse, regTempRowid);
 93523  94091       }
 93524  94092     }else{
 93525  94093       /* This is the case if the data for the INSERT is coming from a VALUES
 93526  94094       ** clause
 93527  94095       */
................................................................................
 93533  94101       nColumn = pList ? pList->nExpr : 0;
 93534  94102       for(i=0; i<nColumn; i++){
 93535  94103         if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
 93536  94104           goto insert_cleanup;
 93537  94105         }
 93538  94106       }
 93539  94107     }
        94108  +
        94109  +  /* If there is no IDLIST term but the table has an integer primary
        94110  +  ** key, the set the ipkColumn variable to the integer primary key 
        94111  +  ** column index in the original table definition.
        94112  +  */
        94113  +  if( pColumn==0 && nColumn>0 ){
        94114  +    ipkColumn = pTab->iPKey;
        94115  +  }
 93540  94116   
 93541  94117     /* Make sure the number of columns in the source data matches the number
 93542  94118     ** of columns to be inserted into the table.
 93543  94119     */
 93544  94120     if( IsVirtual(pTab) ){
 93545  94121       for(i=0; i<pTab->nCol; i++){
 93546  94122         nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
................................................................................
 93552  94128          pTabList, 0, pTab->nCol-nHidden, nColumn);
 93553  94129       goto insert_cleanup;
 93554  94130     }
 93555  94131     if( pColumn!=0 && nColumn!=pColumn->nId ){
 93556  94132       sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
 93557  94133       goto insert_cleanup;
 93558  94134     }
 93559         -
 93560         -  /* If the INSERT statement included an IDLIST term, then make sure
 93561         -  ** all elements of the IDLIST really are columns of the table and 
 93562         -  ** remember the column indices.
 93563         -  **
 93564         -  ** If the table has an INTEGER PRIMARY KEY column and that column
 93565         -  ** is named in the IDLIST, then record in the ipkColumn variable
 93566         -  ** the index into IDLIST of the primary key column.  ipkColumn is
 93567         -  ** the index of the primary key as it appears in IDLIST, not as
 93568         -  ** is appears in the original table.  (The index of the INTEGER
 93569         -  ** PRIMARY KEY in the original table is pTab->iPKey.)
 93570         -  */
 93571         -  if( pColumn ){
 93572         -    for(i=0; i<pColumn->nId; i++){
 93573         -      pColumn->a[i].idx = -1;
 93574         -    }
 93575         -    for(i=0; i<pColumn->nId; i++){
 93576         -      for(j=0; j<pTab->nCol; j++){
 93577         -        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
 93578         -          pColumn->a[i].idx = j;
 93579         -          if( j==pTab->iPKey ){
 93580         -            ipkColumn = i;  assert( !withoutRowid );
 93581         -          }
 93582         -          break;
 93583         -        }
 93584         -      }
 93585         -      if( j>=pTab->nCol ){
 93586         -        if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
 93587         -          ipkColumn = i;
 93588         -        }else{
 93589         -          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
 93590         -              pTabList, 0, pColumn->a[i].zName);
 93591         -          pParse->checkSchema = 1;
 93592         -          goto insert_cleanup;
 93593         -        }
 93594         -      }
 93595         -    }
 93596         -  }
 93597         -
 93598         -  /* If there is no IDLIST term but the table has an integer primary
 93599         -  ** key, the set the ipkColumn variable to the integer primary key 
 93600         -  ** column index in the original table definition.
 93601         -  */
 93602         -  if( pColumn==0 && nColumn>0 ){
 93603         -    ipkColumn = pTab->iPKey;
 93604         -  }
 93605  94135       
 93606  94136     /* Initialize the count of rows to be inserted
 93607  94137     */
 93608  94138     if( db->flags & SQLITE_CountRows ){
 93609  94139       regRowCount = ++pParse->nMem;
 93610  94140       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
 93611  94141     }
................................................................................
 93625  94155     }
 93626  94156   
 93627  94157     /* This is the top of the main insertion loop */
 93628  94158     if( useTempTable ){
 93629  94159       /* This block codes the top of loop only.  The complete loop is the
 93630  94160       ** following pseudocode (template 4):
 93631  94161       **
 93632         -    **         rewind temp table
        94162  +    **         rewind temp table, if empty goto D
 93633  94163       **      C: loop over rows of intermediate table
 93634  94164       **           transfer values form intermediate table into <table>
 93635  94165       **         end loop
 93636  94166       **      D: ...
 93637  94167       */
 93638         -    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
        94168  +    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
 93639  94169       addrCont = sqlite3VdbeCurrentAddr(v);
 93640  94170     }else if( pSelect ){
 93641  94171       /* This block codes the top of loop only.  The complete loop is the
 93642  94172       ** following pseudocode (template 3):
 93643  94173       **
 93644         -    **      C: yield X
 93645         -    **         if EOF goto D
        94174  +    **      C: yield X, at EOF goto D
 93646  94175       **         insert the select result into <table> from R..R+n
 93647  94176       **         goto C
 93648  94177       **      D: ...
 93649  94178       */
 93650         -    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 93651         -    addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
        94179  +    addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
        94180  +    VdbeCoverage(v);
 93652  94181     }
 93653  94182   
 93654         -  /* Allocate registers for holding the rowid of the new row,
 93655         -  ** the content of the new row, and the assemblied row record.
 93656         -  */
 93657         -  regRowid = regIns = pParse->nMem+1;
 93658         -  pParse->nMem += pTab->nCol + 1;
 93659         -  if( IsVirtual(pTab) ){
 93660         -    regRowid++;
 93661         -    pParse->nMem++;
 93662         -  }
 93663         -  regData = regRowid+1;
 93664         -
 93665  94183     /* Run the BEFORE and INSTEAD OF triggers, if there are any
 93666  94184     */
 93667  94185     endOfLoop = sqlite3VdbeMakeLabel(v);
 93668  94186     if( tmask & TRIGGER_BEFORE ){
 93669  94187       int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
 93670  94188   
 93671  94189       /* build the NEW.* reference row.  Note that if there is an INTEGER
................................................................................
 93681  94199         assert( !withoutRowid );
 93682  94200         if( useTempTable ){
 93683  94201           sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
 93684  94202         }else{
 93685  94203           assert( pSelect==0 );  /* Otherwise useTempTable is true */
 93686  94204           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
 93687  94205         }
 93688         -      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
        94206  +      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
 93689  94207         sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
 93690  94208         sqlite3VdbeJumpHere(v, j1);
 93691         -      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
        94209  +      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
 93692  94210       }
 93693  94211   
 93694  94212       /* Cannot have triggers on a virtual table. If it were possible,
 93695  94213       ** this block would have to account for hidden column.
 93696  94214       */
 93697  94215       assert( !IsVirtual(pTab) );
 93698  94216   
................................................................................
 93718  94236   
 93719  94237       /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
 93720  94238       ** do not attempt any conversions before assembling the record.
 93721  94239       ** If this is a real table, attempt conversions as required by the
 93722  94240       ** table column affinities.
 93723  94241       */
 93724  94242       if( !isView ){
 93725         -      sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
 93726         -      sqlite3TableAffinityStr(v, pTab);
        94243  +      sqlite3TableAffinity(v, pTab, regCols+1);
 93727  94244       }
 93728  94245   
 93729  94246       /* Fire BEFORE or INSTEAD OF triggers */
 93730  94247       sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
 93731  94248           pTab, regCols-pTab->nCol-1, onError, endOfLoop);
 93732  94249   
 93733  94250       sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
................................................................................
 93741  94258         /* The row that the VUpdate opcode will delete: none */
 93742  94259         sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
 93743  94260       }
 93744  94261       if( ipkColumn>=0 ){
 93745  94262         if( useTempTable ){
 93746  94263           sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
 93747  94264         }else if( pSelect ){
 93748         -        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+ipkColumn, regRowid);
        94265  +        sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
 93749  94266         }else{
 93750  94267           VdbeOp *pOp;
 93751  94268           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
 93752  94269           pOp = sqlite3VdbeGetOp(v, -1);
 93753  94270           if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
 93754  94271             appendFlag = 1;
 93755  94272             pOp->opcode = OP_NewRowid;
................................................................................
 93760  94277         }
 93761  94278         /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
 93762  94279         ** to generate a unique primary key value.
 93763  94280         */
 93764  94281         if( !appendFlag ){
 93765  94282           int j1;
 93766  94283           if( !IsVirtual(pTab) ){
 93767         -          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
        94284  +          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
 93768  94285             sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
 93769  94286             sqlite3VdbeJumpHere(v, j1);
 93770  94287           }else{
 93771  94288             j1 = sqlite3VdbeCurrentAddr(v);
 93772         -          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
        94289  +          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); VdbeCoverage(v);
 93773  94290           }
 93774         -        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
        94291  +        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
 93775  94292         }
 93776  94293       }else if( IsVirtual(pTab) || withoutRowid ){
 93777  94294         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
 93778  94295       }else{
 93779  94296         sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
 93780  94297         appendFlag = 1;
 93781  94298       }
................................................................................
 93787  94304       nHidden = 0;
 93788  94305       for(i=0; i<pTab->nCol; i++){
 93789  94306         int iRegStore = regRowid+1+i;
 93790  94307         if( i==pTab->iPKey ){
 93791  94308           /* The value of the INTEGER PRIMARY KEY column is always a NULL.
 93792  94309           ** Whenever this column is read, the rowid will be substituted
 93793  94310           ** in its place.  Hence, fill this column with a NULL to avoid
 93794         -        ** taking up data space with information that will never be used. */
 93795         -        sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
        94311  +        ** taking up data space with information that will never be used.
        94312  +        ** As there may be shallow copies of this value, make it a soft-NULL */
        94313  +        sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
 93796  94314           continue;
 93797  94315         }
 93798  94316         if( pColumn==0 ){
 93799  94317           if( IsHiddenColumn(&pTab->aCol[i]) ){
 93800  94318             assert( IsVirtual(pTab) );
 93801  94319             j = -1;
 93802  94320             nHidden++;
................................................................................
 93805  94323           }
 93806  94324         }else{
 93807  94325           for(j=0; j<pColumn->nId; j++){
 93808  94326             if( pColumn->a[j].idx==i ) break;
 93809  94327           }
 93810  94328         }
 93811  94329         if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
 93812         -        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
        94330  +        sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
 93813  94331         }else if( useTempTable ){
 93814  94332           sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
 93815  94333         }else if( pSelect ){
 93816         -        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
        94334  +        if( regFromSelect!=regData ){
        94335  +          sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
        94336  +        }
 93817  94337         }else{
 93818  94338           sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
 93819  94339         }
 93820  94340       }
 93821  94341   
 93822  94342       /* Generate code to check constraints and generate index keys and
 93823  94343       ** do the insertion.
................................................................................
 93855  94375     }
 93856  94376   
 93857  94377     /* The bottom of the main insertion loop, if the data source
 93858  94378     ** is a SELECT statement.
 93859  94379     */
 93860  94380     sqlite3VdbeResolveLabel(v, endOfLoop);
 93861  94381     if( useTempTable ){
 93862         -    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
        94382  +    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
 93863  94383       sqlite3VdbeJumpHere(v, addrInsTop);
 93864  94384       sqlite3VdbeAddOp1(v, OP_Close, srcTab);
 93865  94385     }else if( pSelect ){
 93866  94386       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
 93867  94387       sqlite3VdbeJumpHere(v, addrInsTop);
 93868  94388     }
 93869  94389   
................................................................................
 94022  94542     int onError;         /* Conflict resolution strategy */
 94023  94543     int j1;              /* Addresss of jump instruction */
 94024  94544     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
 94025  94545     int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
 94026  94546     int ipkTop = 0;      /* Top of the rowid change constraint check */
 94027  94547     int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
 94028  94548     u8 isUpdate;         /* True if this is an UPDATE operation */
        94549  +  u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
 94029  94550     int regRowid = -1;   /* Register holding ROWID value */
 94030  94551   
 94031  94552     isUpdate = regOldData!=0;
 94032  94553     db = pParse->db;
 94033  94554     v = sqlite3GetVdbe(pParse);
 94034  94555     assert( v!=0 );
 94035  94556     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
................................................................................
 94076  94597         case OE_Rollback:
 94077  94598         case OE_Fail: {
 94078  94599           char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
 94079  94600                                       pTab->aCol[i].zName);
 94080  94601           sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
 94081  94602                             regNewData+1+i, zMsg, P4_DYNAMIC);
 94082  94603           sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
        94604  +        VdbeCoverage(v);
 94083  94605           break;
 94084  94606         }
 94085  94607         case OE_Ignore: {
 94086  94608           sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
        94609  +        VdbeCoverage(v);
 94087  94610           break;
 94088  94611         }
 94089  94612         default: {
 94090  94613           assert( onError==OE_Replace );
 94091         -        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
        94614  +        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); VdbeCoverage(v);
 94092  94615           sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
 94093  94616           sqlite3VdbeJumpHere(v, j1);
 94094  94617           break;
 94095  94618         }
 94096  94619       }
 94097  94620     }
 94098  94621   
................................................................................
 94136  94659       }
 94137  94660   
 94138  94661       if( isUpdate ){
 94139  94662         /* pkChng!=0 does not mean that the rowid has change, only that
 94140  94663         ** it might have changed.  Skip the conflict logic below if the rowid
 94141  94664         ** is unchanged. */
 94142  94665         sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
        94666  +      sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
        94667  +      VdbeCoverage(v);
 94143  94668       }
 94144  94669   
 94145  94670       /* If the response to a rowid conflict is REPLACE but the response
 94146  94671       ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
 94147  94672       ** to defer the running of the rowid conflict checking until after
 94148  94673       ** the UNIQUE constraints have run.
 94149  94674       */
................................................................................
 94155  94680           }
 94156  94681         }
 94157  94682       }
 94158  94683   
 94159  94684       /* Check to see if the new rowid already exists in the table.  Skip
 94160  94685       ** the following conflict logic if it does not. */
 94161  94686       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
        94687  +    VdbeCoverage(v);
 94162  94688   
 94163  94689       /* Generate code that deals with a rowid collision */
 94164  94690       switch( onError ){
 94165  94691         default: {
 94166  94692           onError = OE_Abort;
 94167  94693           /* Fall thru into the next case */
 94168  94694         }
................................................................................
 94233  94759     for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
 94234  94760       int regIdx;          /* Range of registers hold conent for pIdx */
 94235  94761       int regR;            /* Range of registers holding conflicting PK */
 94236  94762       int iThisCur;        /* Cursor for this UNIQUE index */
 94237  94763       int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
 94238  94764   
 94239  94765       if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
        94766  +    if( bAffinityDone==0 ){
        94767  +      sqlite3TableAffinity(v, pTab, regNewData+1);
        94768  +      bAffinityDone = 1;
        94769  +    }
 94240  94770       iThisCur = iIdxCur+ix;
 94241  94771       addrUniqueOk = sqlite3VdbeMakeLabel(v);
 94242  94772   
 94243  94773       /* Skip partial indices for which the WHERE clause is not true */
 94244  94774       if( pIdx->pPartIdxWhere ){
 94245  94775         sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
 94246  94776         pParse->ckBase = regNewData+1;
................................................................................
 94263  94793         }else{
 94264  94794           x = iField + regNewData + 1;
 94265  94795         }
 94266  94796         sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
 94267  94797         VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
 94268  94798       }
 94269  94799       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
 94270         -    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
 94271  94800       VdbeComment((v, "for %s", pIdx->zName));
 94272  94801       sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
 94273  94802   
 94274  94803       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
 94275  94804       ** of a WITHOUT ROWID table and there has been no change the
 94276  94805       ** primary key, then no collision is possible.  The collision detection
 94277  94806       ** logic below can all be skipped. */
................................................................................
 94291  94820         onError = overrideError;
 94292  94821       }else if( onError==OE_Default ){
 94293  94822         onError = OE_Abort;
 94294  94823       }
 94295  94824       
 94296  94825       /* Check to see if the new index entry will be unique */
 94297  94826       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
 94298         -                         regIdx, pIdx->nKeyCol);
        94827  +                         regIdx, pIdx->nKeyCol); VdbeCoverage(v);
 94299  94828   
 94300  94829       /* Generate code to handle collisions */
 94301  94830       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
 94302  94831       if( isUpdate || onError==OE_Replace ){
 94303  94832         if( HasRowid(pTab) ){
 94304  94833           sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
 94305  94834           /* Conflict only if the rowid of the existing index entry
 94306  94835           ** is different from old-rowid */
 94307  94836           if( isUpdate ){
 94308  94837             sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
        94838  +          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
        94839  +          VdbeCoverage(v);
 94309  94840           }
 94310  94841         }else{
 94311  94842           int x;
 94312  94843           /* Extract the PRIMARY KEY from the end of the index entry and
 94313  94844           ** store it in registers regR..regR+nPk-1 */
 94314  94845           if( pIdx!=pPk ){
 94315  94846             for(i=0; i<pPk->nKeyCol; i++){
................................................................................
 94337  94868               if( i==(pPk->nKeyCol-1) ){
 94338  94869                 addrJump = addrUniqueOk;
 94339  94870                 op = OP_Eq;
 94340  94871               }
 94341  94872               sqlite3VdbeAddOp4(v, op, 
 94342  94873                   regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
 94343  94874               );
        94875  +            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
        94876  +            VdbeCoverageIf(v, op==OP_Eq);
        94877  +            VdbeCoverageIf(v, op==OP_Ne);
 94344  94878             }
 94345  94879           }
 94346  94880         }
 94347  94881       }
 94348  94882   
 94349  94883       /* Generate code that executes if the new index entry is not unique */
 94350  94884       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
................................................................................
 94408  94942   ){
 94409  94943     Vdbe *v;            /* Prepared statements under construction */
 94410  94944     Index *pIdx;        /* An index being inserted or updated */
 94411  94945     u8 pik_flags;       /* flag values passed to the btree insert */
 94412  94946     int regData;        /* Content registers (after the rowid) */
 94413  94947     int regRec;         /* Register holding assemblied record for the table */
 94414  94948     int i;              /* Loop counter */
        94949  +  u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
 94415  94950   
 94416  94951     v = sqlite3GetVdbe(pParse);
 94417  94952     assert( v!=0 );
 94418  94953     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 94419  94954     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 94420  94955       if( aRegIdx[i]==0 ) continue;
        94956  +    bAffinityDone = 1;
 94421  94957       if( pIdx->pPartIdxWhere ){
 94422  94958         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
        94959  +      VdbeCoverage(v);
 94423  94960       }
 94424  94961       sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
 94425  94962       pik_flags = 0;
 94426  94963       if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
 94427  94964       if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
 94428  94965         assert( pParse->nested==0 );
 94429  94966         pik_flags |= OPFLAG_NCHANGE;
................................................................................
 94430  94967       }
 94431  94968       if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
 94432  94969     }
 94433  94970     if( !HasRowid(pTab) ) return;
 94434  94971     regData = regNewData + 1;
 94435  94972     regRec = sqlite3GetTempReg(pParse);
 94436  94973     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
 94437         -  sqlite3TableAffinityStr(v, pTab);
        94974  +  if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
 94438  94975     sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
 94439  94976     if( pParse->nested ){
 94440  94977       pik_flags = 0;
 94441  94978     }else{
 94442  94979       pik_flags = OPFLAG_NCHANGE;
 94443  94980       pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
 94444  94981     }
................................................................................
 94799  95336       **     of index entries might need to change.)
 94800  95337       **
 94801  95338       ** (2) The destination has a unique index.  (The xfer optimization 
 94802  95339       **     is unable to test uniqueness.)
 94803  95340       **
 94804  95341       ** (3) onError is something other than OE_Abort and OE_Rollback.
 94805  95342       */
 94806         -    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
        95343  +    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
 94807  95344       emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 94808  95345       sqlite3VdbeJumpHere(v, addr1);
 94809  95346     }
 94810  95347     if( HasRowid(pSrc) ){
 94811  95348       sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
 94812         -    emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
        95349  +    emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
 94813  95350       if( pDest->iPKey>=0 ){
 94814  95351         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 94815  95352         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
        95353  +      VdbeCoverage(v);
 94816  95354         sqlite3RowidConstraint(pParse, onError, pDest);
 94817  95355         sqlite3VdbeJumpHere(v, addr2);
 94818  95356         autoIncStep(pParse, regAutoinc, regRowid);
 94819  95357       }else if( pDest->pIndex==0 ){
 94820  95358         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
 94821  95359       }else{
 94822  95360         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 94823  95361         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
 94824  95362       }
 94825  95363       sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
 94826  95364       sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
 94827  95365       sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
 94828  95366       sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
 94829         -    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
        95367  +    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
 94830  95368       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 94831  95369       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 94832  95370     }else{
 94833  95371       sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
 94834  95372       sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
 94835  95373     }
 94836  95374     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
................................................................................
 94841  95379       sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
 94842  95380       sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
 94843  95381       VdbeComment((v, "%s", pSrcIdx->zName));
 94844  95382       sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
 94845  95383       sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
 94846  95384       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
 94847  95385       VdbeComment((v, "%s", pDestIdx->zName));
 94848         -    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
        95386  +    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
 94849  95387       sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
 94850  95388       sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
 94851         -    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
        95389  +    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
 94852  95390       sqlite3VdbeJumpHere(v, addr1);
 94853  95391       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 94854  95392       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 94855  95393     }
 94856         -  sqlite3VdbeJumpHere(v, emptySrcTest);
        95394  +  if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
 94857  95395     sqlite3ReleaseTempReg(pParse, regRowid);
 94858  95396     sqlite3ReleaseTempReg(pParse, regData);
 94859  95397     if( emptyDestTest ){
 94860  95398       sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
 94861  95399       sqlite3VdbeJumpHere(v, emptyDestTest);
 94862  95400       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 94863  95401       return 0;
................................................................................
 97083  97621     ** Older versions of SQLite would set the default cache size to a
 97084  97622     ** negative number to indicate synchronous=OFF.  These days, synchronous
 97085  97623     ** is always on by default regardless of the sign of the default cache
 97086  97624     ** size.  But continue to take the absolute value of the default cache
 97087  97625     ** size of historical compatibility.
 97088  97626     */
 97089  97627     case PragTyp_DEFAULT_CACHE_SIZE: {
        97628  +    static const int iLn = __LINE__+2;
 97090  97629       static const VdbeOpList getCacheSize[] = {
 97091  97630         { OP_Transaction, 0, 0,        0},                         /* 0 */
 97092  97631         { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
 97093  97632         { OP_IfPos,       1, 8,        0},
 97094  97633         { OP_Integer,     0, 2,        0},
 97095  97634         { OP_Subtract,    1, 2,        1},
 97096  97635         { OP_IfPos,       1, 8,        0},
................................................................................
 97100  97639       };
 97101  97640       int addr;
 97102  97641       sqlite3VdbeUsesBtree(v, iDb);
 97103  97642       if( !zRight ){
 97104  97643         sqlite3VdbeSetNumCols(v, 1);
 97105  97644         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
 97106  97645         pParse->nMem += 2;
 97107         -      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
        97646  +      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
 97108  97647         sqlite3VdbeChangeP1(v, addr, iDb);
 97109  97648         sqlite3VdbeChangeP1(v, addr+1, iDb);
 97110  97649         sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
 97111  97650       }else{
 97112  97651         int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
 97113  97652         sqlite3BeginWriteOperation(pParse, 0, iDb);
 97114  97653         sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
................................................................................
 97345  97884         rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
 97346  97885         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
 97347  97886           /* When setting the auto_vacuum mode to either "full" or 
 97348  97887           ** "incremental", write the value of meta[6] in the database
 97349  97888           ** file. Before writing to meta[6], check that meta[3] indicates
 97350  97889           ** that this really is an auto-vacuum capable database.
 97351  97890           */
        97891  +        static const int iLn = __LINE__+2;
 97352  97892           static const VdbeOpList setMeta6[] = {
 97353  97893             { OP_Transaction,    0,         1,                 0},    /* 0 */
 97354  97894             { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
 97355  97895             { OP_If,             1,         0,                 0},    /* 2 */
 97356  97896             { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
 97357  97897             { OP_Integer,        0,         1,                 0},    /* 4 */
 97358  97898             { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
 97359  97899           };
 97360  97900           int iAddr;
 97361         -        iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
        97901  +        iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
 97362  97902           sqlite3VdbeChangeP1(v, iAddr, iDb);
 97363  97903           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
 97364  97904           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
 97365  97905           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
 97366  97906           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
 97367  97907           sqlite3VdbeUsesBtree(v, iDb);
 97368  97908         }
................................................................................
 97380  97920     case PragTyp_INCREMENTAL_VACUUM: {
 97381  97921       int iLimit, addr;
 97382  97922       if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
 97383  97923         iLimit = 0x7fffffff;
 97384  97924       }
 97385  97925       sqlite3BeginWriteOperation(pParse, 0, iDb);
 97386  97926       sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
 97387         -    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
        97927  +    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
 97388  97928       sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
 97389  97929       sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
 97390         -    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
        97930  +    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
 97391  97931       sqlite3VdbeJumpHere(v, addr);
 97392  97932       break;
 97393  97933     }
 97394  97934   #endif
 97395  97935   
 97396  97936   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 97397  97937     /*
................................................................................
 97954  98494             k = 0;
 97955  98495             break;
 97956  98496           }
 97957  98497         }
 97958  98498         assert( pParse->nErr>0 || pFK==0 );
 97959  98499         if( pFK ) break;
 97960  98500         if( pParse->nTab<i ) pParse->nTab = i;
 97961         -      addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
        98501  +      addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
 97962  98502         for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
 97963  98503           pParent = sqlite3FindTable(db, pFK->zTo, zDb);
 97964  98504           pIdx = 0;
 97965  98505           aiCols = 0;
 97966  98506           if( pParent ){
 97967  98507             x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
 97968  98508             assert( x==0 );
................................................................................
 97970  98510           addrOk = sqlite3VdbeMakeLabel(v);
 97971  98511           if( pParent && pIdx==0 ){
 97972  98512             int iKey = pFK->aCol[0].iFrom;
 97973  98513             assert( iKey>=0 && iKey<pTab->nCol );
 97974  98514             if( iKey!=pTab->iPKey ){
 97975  98515               sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
 97976  98516               sqlite3ColumnDefault(v, pTab, iKey, regRow);
 97977         -            sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
 97978         -            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
 97979         -               sqlite3VdbeCurrentAddr(v)+3);
        98517  +            sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
        98518  +            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, 
        98519  +               sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
 97980  98520             }else{
 97981  98521               sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
 97982  98522             }
 97983         -          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
        98523  +          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
 97984  98524             sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
 97985  98525             sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
 97986  98526           }else{
 97987  98527             for(j=0; j<pFK->nCol; j++){
 97988  98528               sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
 97989  98529                               aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
 97990         -            sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
        98530  +            sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
 97991  98531             }
 97992  98532             if( pParent ){
 97993         -            sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
 97994         -            sqlite3VdbeChangeP4(v, -1,
 97995         -                     sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
        98533  +            sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
        98534  +                              sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
 97996  98535               sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
        98536  +            VdbeCoverage(v);
 97997  98537             }
 97998  98538           }
 97999  98539           sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
 98000  98540           sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0, 
 98001  98541                             pFK->zTo, P4_TRANSIENT);
 98002  98542           sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
 98003  98543           sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
 98004  98544           sqlite3VdbeResolveLabel(v, addrOk);
 98005  98545           sqlite3DbFree(db, aiCols);
 98006  98546         }
 98007         -      sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
        98547  +      sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
 98008  98548         sqlite3VdbeJumpHere(v, addrTop);
 98009  98549       }
 98010  98550     }
 98011  98551     break;
 98012  98552   #endif /* !defined(SQLITE_OMIT_TRIGGER) */
 98013  98553   #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 98014  98554   
................................................................................
 98047  98587     case PragTyp_INTEGRITY_CHECK: {
 98048  98588       int i, j, addr, mxErr;
 98049  98589   
 98050  98590       /* Code that appears at the end of the integrity check.  If no error
 98051  98591       ** messages have been generated, output OK.  Otherwise output the
 98052  98592       ** error message
 98053  98593       */
        98594  +    static const int iLn = __LINE__+2;
 98054  98595       static const VdbeOpList endCode[] = {
 98055  98596         { OP_AddImm,      1, 0,        0},    /* 0 */
 98056  98597         { OP_IfNeg,       1, 0,        0},    /* 1 */
 98057  98598         { OP_String8,     0, 3,        0},    /* 2 */
 98058  98599         { OP_ResultRow,   3, 1,        0},
 98059  98600       };
 98060  98601   
................................................................................
 98095  98636         int cnt = 0;
 98096  98637   
 98097  98638         if( OMIT_TEMPDB && i==1 ) continue;
 98098  98639         if( iDb>=0 && i!=iDb ) continue;
 98099  98640   
 98100  98641         sqlite3CodeVerifySchema(pParse, i);
 98101  98642         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
        98643  +      VdbeCoverage(v);
 98102  98644         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 98103  98645         sqlite3VdbeJumpHere(v, addr);
 98104  98646   
 98105  98647         /* Do an integrity check of the B-Tree
 98106  98648         **
 98107  98649         ** Begin by filling registers 2, 3, ... with the root pages numbers
 98108  98650         ** for all tables and indices in the database.
................................................................................
 98126  98668   
 98127  98669         /* Make sure sufficient number of registers have been allocated */
 98128  98670         pParse->nMem = MAX( pParse->nMem, cnt+8 );
 98129  98671   
 98130  98672         /* Do the b-tree integrity checks */
 98131  98673         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
 98132  98674         sqlite3VdbeChangeP5(v, (u8)i);
 98133         -      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
        98675  +      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
 98134  98676         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 98135  98677            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
 98136  98678            P4_DYNAMIC);
 98137  98679         sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
 98138  98680         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
 98139  98681         sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
 98140  98682         sqlite3VdbeJumpHere(v, addr);
................................................................................
 98148  98690           int loopTop;
 98149  98691           int iDataCur, iIdxCur;
 98150  98692           int r1 = -1;
 98151  98693   
 98152  98694           if( pTab->pIndex==0 ) continue;
 98153  98695           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
 98154  98696           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
        98697  +        VdbeCoverage(v);
 98155  98698           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 98156  98699           sqlite3VdbeJumpHere(v, addr);
 98157  98700           sqlite3ExprCacheClear(pParse);
 98158  98701           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
 98159  98702                                      1, 0, &iDataCur, &iIdxCur);
 98160  98703           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
 98161  98704           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 98162  98705             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
 98163  98706           }
 98164  98707           pParse->nMem = MAX(pParse->nMem, 8+j);
 98165         -        sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
        98708  +        sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
 98166  98709           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
 98167  98710           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 98168  98711             int jmp2, jmp3, jmp4;
 98169  98712             if( pPk==pIdx ) continue;
 98170  98713             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
 98171  98714                                          pPrior, r1);
 98172  98715             pPrior = pIdx;
 98173  98716             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
 98174  98717             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
 98175         -                                      pIdx->nColumn);
        98718  +                                      pIdx->nColumn); VdbeCoverage(v);
 98176  98719             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
 98177  98720             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
 98178  98721             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
 98179  98722             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
 98180  98723                               P4_STATIC);
 98181  98724             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
 98182  98725             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
 98183  98726             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
 98184  98727             sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
 98185         -          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
        98728  +          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
 98186  98729             sqlite3VdbeAddOp0(v, OP_Halt);
 98187  98730             sqlite3VdbeJumpHere(v, jmp4);
 98188  98731             sqlite3VdbeJumpHere(v, jmp2);
 98189  98732             sqlite3VdbeResolveLabel(v, jmp3);
 98190  98733           }
 98191         -        sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop);
        98734  +        sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
 98192  98735           sqlite3VdbeJumpHere(v, loopTop-1);
 98193  98736   #ifndef SQLITE_OMIT_BTREECOUNT
 98194  98737           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
 98195  98738                        "wrong # of entries in index ", P4_STATIC);
 98196  98739           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 98197  98740             if( pPk==pIdx ) continue;
 98198  98741             addr = sqlite3VdbeCurrentAddr(v);
 98199         -          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
        98742  +          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
 98200  98743             sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 98201  98744             sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
 98202         -          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3);
        98745  +          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
        98746  +          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
 98203  98747             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
 98204  98748             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
 98205  98749             sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
 98206  98750             sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
 98207  98751           }
 98208  98752   #endif /* SQLITE_OMIT_BTREECOUNT */
 98209  98753         } 
 98210  98754       }
 98211         -    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
        98755  +    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
 98212  98756       sqlite3VdbeChangeP2(v, addr, -mxErr);
 98213  98757       sqlite3VdbeJumpHere(v, addr+1);
 98214  98758       sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
 98215  98759     }
 98216  98760     break;
 98217  98761   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 98218  98762   
................................................................................
 98342  98886       if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
 98343  98887         /* Write the specified cookie value */
 98344  98888         static const VdbeOpList setCookie[] = {
 98345  98889           { OP_Transaction,    0,  1,  0},    /* 0 */
 98346  98890           { OP_Integer,        0,  1,  0},    /* 1 */
 98347  98891           { OP_SetCookie,      0,  0,  1},    /* 2 */
 98348  98892         };
 98349         -      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
        98893  +      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
 98350  98894         sqlite3VdbeChangeP1(v, addr, iDb);
 98351  98895         sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
 98352  98896         sqlite3VdbeChangeP1(v, addr+2, iDb);
 98353  98897         sqlite3VdbeChangeP2(v, addr+2, iCookie);
 98354  98898       }else{
 98355  98899         /* Read the specified cookie value */
 98356  98900         static const VdbeOpList readCookie[] = {
 98357  98901           { OP_Transaction,     0,  0,  0},    /* 0 */
 98358  98902           { OP_ReadCookie,      0,  1,  0},    /* 1 */
 98359  98903           { OP_ResultRow,       1,  1,  0}
 98360  98904         };
 98361         -      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
        98905  +      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
 98362  98906         sqlite3VdbeChangeP1(v, addr, iDb);
 98363  98907         sqlite3VdbeChangeP1(v, addr+1, iDb);
 98364  98908         sqlite3VdbeChangeP3(v, addr+1, iCookie);
 98365  98909         sqlite3VdbeSetNumCols(v, 1);
 98366  98910         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
 98367  98911       }
 98368  98912     }
................................................................................
 99560 100104   */
 99561 100105   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
 99562 100106     if( p ){
 99563 100107       clearSelect(db, p);
 99564 100108       sqlite3DbFree(db, p);
 99565 100109     }
 99566 100110   }
       100111  +
       100112  +/*
       100113  +** Return a pointer to the right-most SELECT statement in a compound.
       100114  +*/
       100115  +static Select *findRightmost(Select *p){
       100116  +  while( p->pNext ) p = p->pNext;
       100117  +  return p;
       100118  +}
 99567 100119   
 99568 100120   /*
 99569 100121   ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
 99570 100122   ** type of join.  Return an integer constant that expresses that type
 99571 100123   ** in terms of the following bit values:
 99572 100124   **
 99573 100125   **     JT_INNER
................................................................................
 99899 100451       int addr1, addr2;
 99900 100452       int iLimit;
 99901 100453       if( pSelect->iOffset ){
 99902 100454         iLimit = pSelect->iOffset+1;
 99903 100455       }else{
 99904 100456         iLimit = pSelect->iLimit;
 99905 100457       }
 99906         -    addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
       100458  +    addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); VdbeCoverage(v);
 99907 100459       sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
 99908 100460       addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
 99909 100461       sqlite3VdbeJumpHere(v, addr1);
 99910 100462       sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
 99911 100463       sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
 99912 100464       sqlite3VdbeJumpHere(v, addr2);
 99913 100465     }
................................................................................
 99920 100472     Vdbe *v,          /* Generate code into this VM */
 99921 100473     int iOffset,      /* Register holding the offset counter */
 99922 100474     int iContinue     /* Jump here to skip the current record */
 99923 100475   ){
 99924 100476     if( iOffset>0 && iContinue!=0 ){
 99925 100477       int addr;
 99926 100478       sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
 99927         -    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset);
       100479  +    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
 99928 100480       sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
 99929 100481       VdbeComment((v, "skip OFFSET records"));
 99930 100482       sqlite3VdbeJumpHere(v, addr);
 99931 100483     }
 99932 100484   }
 99933 100485   
 99934 100486   /*
................................................................................
 99948 100500     int iMem           /* First element */
 99949 100501   ){
 99950 100502     Vdbe *v;
 99951 100503     int r1;
 99952 100504   
 99953 100505     v = pParse->pVdbe;
 99954 100506     r1 = sqlite3GetTempReg(pParse);
 99955         -  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
       100507  +  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
 99956 100508     sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
 99957 100509     sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
 99958 100510     sqlite3ReleaseTempReg(pParse, r1);
 99959 100511   }
 99960 100512   
 99961 100513   #ifndef SQLITE_OMIT_SUBQUERY
 99962 100514   /*
................................................................................
100029 100581     if( pOrderBy==0 && !hasDistinct ){
100030 100582       codeOffset(v, p->iOffset, iContinue);
100031 100583     }
100032 100584   
100033 100585     /* Pull the requested columns.
100034 100586     */
100035 100587     nResultCol = pEList->nExpr;
       100588  +
100036 100589     if( pDest->iSdst==0 ){
100037 100590       pDest->iSdst = pParse->nMem+1;
100038         -    pDest->nSdst = nResultCol;
       100591  +    pParse->nMem += nResultCol;
       100592  +  }else if( pDest->iSdst+nResultCol > pParse->nMem ){
       100593  +    /* This is an error condition that can result, for example, when a SELECT
       100594  +    ** on the right-hand side of an INSERT contains more result columns than
       100595  +    ** there are columns in the table on the left.  The error will be caught
       100596  +    ** and reported later.  But we need to make sure enough memory is allocated
       100597  +    ** to avoid other spurious errors in the meantime. */
100039 100598       pParse->nMem += nResultCol;
100040         -  }else{ 
100041         -    assert( pDest->nSdst==nResultCol );
100042 100599     }
       100600  +  pDest->nSdst = nResultCol;
100043 100601     regResult = pDest->iSdst;
100044 100602     if( srcTab>=0 ){
100045 100603       for(i=0; i<nResultCol; i++){
100046 100604         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
100047 100605         VdbeComment((v, "%s", pEList->a[i].zName));
100048 100606       }
100049 100607     }else if( eDest!=SRT_Exists ){
100050 100608       /* If the destination is an EXISTS(...) expression, the actual
100051 100609       ** values returned by the SELECT are not required.
100052 100610       */
100053 100611       sqlite3ExprCodeExprList(pParse, pEList, regResult,
100054         -                            (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
       100612  +                  (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
100055 100613     }
100056 100614   
100057 100615     /* If the DISTINCT keyword was present on the SELECT statement
100058 100616     ** and this row has been seen before, then do not make this row
100059 100617     ** part of the result.
100060 100618     */
100061 100619     if( hasDistinct ){
................................................................................
100082 100640           pOp->p2 = regPrev;
100083 100641   
100084 100642           iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
100085 100643           for(i=0; i<nResultCol; i++){
100086 100644             CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
100087 100645             if( i<nResultCol-1 ){
100088 100646               sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
       100647  +            VdbeCoverage(v);
100089 100648             }else{
100090 100649               sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
100091         -          }
       100650  +            VdbeCoverage(v);
       100651  +           }
100092 100652             sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
100093 100653             sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
100094 100654           }
100095 100655           assert( sqlite3VdbeCurrentAddr(v)==iJump );
100096 100656           sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
100097 100657           break;
100098 100658         }
................................................................................
100150 100710         if( eDest==SRT_DistTable ){
100151 100711           /* If the destination is DistTable, then cursor (iParm+1) is open
100152 100712           ** on an ephemeral index. If the current row is already present
100153 100713           ** in the index, do not write it to the output. If not, add the
100154 100714           ** current row to the index and proceed with writing it to the
100155 100715           ** output table as well.  */
100156 100716           int addr = sqlite3VdbeCurrentAddr(v) + 4;
100157         -        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
       100717  +        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
100158 100718           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
100159 100719           assert( pOrderBy==0 );
100160 100720         }
100161 100721   #endif
100162 100722         if( pOrderBy ){
100163 100723           pushOntoSorter(pParse, pOrderBy, p, r1);
100164 100724         }else{
................................................................................
100217 100777           sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
100218 100778           /* The LIMIT clause will jump out of the loop for us */
100219 100779         }
100220 100780         break;
100221 100781       }
100222 100782   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
100223 100783   
100224         -    /* Send the data to the callback function or to a subroutine.  In the
100225         -    ** case of a subroutine, the subroutine itself is responsible for
100226         -    ** popping the data from the stack.
100227         -    */
100228         -    case SRT_Coroutine:
100229         -    case SRT_Output: {
       100784  +    case SRT_Coroutine:       /* Send data to a co-routine */
       100785  +    case SRT_Output: {        /* Return the results */
100230 100786         testcase( eDest==SRT_Coroutine );
100231 100787         testcase( eDest==SRT_Output );
100232 100788         if( pOrderBy ){
100233 100789           int r1 = sqlite3GetTempReg(pParse);
100234 100790           sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100235 100791           pushOntoSorter(pParse, pOrderBy, p, r1);
100236 100792           sqlite3ReleaseTempReg(pParse, r1);
................................................................................
100258 100814         ExprList *pSO;
100259 100815         pSO = pDest->pOrderBy;
100260 100816         assert( pSO );
100261 100817         nKey = pSO->nExpr;
100262 100818         r1 = sqlite3GetTempReg(pParse);
100263 100819         r2 = sqlite3GetTempRange(pParse, nKey+2);
100264 100820         r3 = r2+nKey+1;
100265         -      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
100266 100821         if( eDest==SRT_DistQueue ){
100267 100822           /* If the destination is DistQueue, then cursor (iParm+1) is open
100268 100823           ** on a second ephemeral index that holds all values every previously
100269         -        ** added to the queue.  Only add this new value if it has never before
100270         -        ** been added */
100271         -        addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, r3, 0);
       100824  +        ** added to the queue. */
       100825  +        addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, 
       100826  +                                        regResult, nResultCol);
       100827  +        VdbeCoverage(v);
       100828  +      }
       100829  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
       100830  +      if( eDest==SRT_DistQueue ){
100272 100831           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
100273 100832           sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
100274 100833         }
100275 100834         for(i=0; i<nKey; i++){
100276 100835           sqlite3VdbeAddOp2(v, OP_SCopy,
100277 100836                             regResult + pSO->a[i].u.x.iOrderByCol - 1,
100278 100837                             r2+i);
................................................................................
100303 100862     }
100304 100863   
100305 100864     /* Jump to the end of the loop if the LIMIT is reached.  Except, if
100306 100865     ** there is a sorter, in which case the sorter has already limited
100307 100866     ** the output for us.
100308 100867     */
100309 100868     if( pOrderBy==0 && p->iLimit ){
100310         -    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
       100869  +    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
100311 100870     }
100312 100871   }
100313 100872   
100314 100873   /*
100315 100874   ** Allocate a KeyInfo object sufficient for an index of N key columns and
100316 100875   ** X extra columns.
100317 100876   */
................................................................................
100522 101081       regRowid = sqlite3GetTempReg(pParse);
100523 101082     }
100524 101083     if( p->selFlags & SF_UseSorter ){
100525 101084       int regSortOut = ++pParse->nMem;
100526 101085       int ptab2 = pParse->nTab++;
100527 101086       sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
100528 101087       addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
       101088  +    VdbeCoverage(v);
100529 101089       codeOffset(v, p->iOffset, addrContinue);
100530 101090       sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
100531 101091       sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
100532 101092       sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
100533 101093     }else{
100534         -    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
       101094  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
100535 101095       codeOffset(v, p->iOffset, addrContinue);
100536 101096       sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
100537 101097     }
100538 101098     switch( eDest ){
100539 101099       case SRT_Table:
100540 101100       case SRT_EphemTab: {
100541 101101         testcase( eDest==SRT_Table );
................................................................................
100585 101145     sqlite3ReleaseTempReg(pParse, regRow);
100586 101146     sqlite3ReleaseTempReg(pParse, regRowid);
100587 101147   
100588 101148     /* The bottom of the loop
100589 101149     */
100590 101150     sqlite3VdbeResolveLabel(v, addrContinue);
100591 101151     if( p->selFlags & SF_UseSorter ){
100592         -    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
       101152  +    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
100593 101153     }else{
100594         -    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
       101154  +    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
100595 101155     }
100596 101156     sqlite3VdbeResolveLabel(v, addrBreak);
100597 101157     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
100598 101158       sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
100599 101159     }
100600 101160   }
100601 101161   
................................................................................
100958 101518       */
100959 101519       nName = sqlite3Strlen30(zName);
100960 101520       for(j=cnt=0; j<i; j++){
100961 101521         if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
100962 101522           char *zNewName;
100963 101523           int k;
100964 101524           for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
100965         -        if( zName[k]==':' ) nName = k;
       101525  +        if( k>=0 && zName[k]==':' ) nName = k;
100966 101526           zName[nName] = 0;
100967 101527           zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
100968 101528           sqlite3DbFree(db, zName);
100969 101529           zName = zNewName;
100970 101530           j = -1;
100971 101531           if( zName==0 ) break;
100972 101532         }
................................................................................
101071 101631   ** Get a VDBE for the given parser context.  Create a new one if necessary.
101072 101632   ** If an error occurs, return NULL and leave a message in pParse.
101073 101633   */
101074 101634   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
101075 101635     Vdbe *v = pParse->pVdbe;
101076 101636     if( v==0 ){
101077 101637       v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
101078         -#ifndef SQLITE_OMIT_TRACE
101079         -    if( v ){
101080         -      sqlite3VdbeAddOp0(v, OP_Trace);
       101638  +    if( v ) sqlite3VdbeAddOp0(v, OP_Init);
       101639  +    if( pParse->pToplevel==0
       101640  +     && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
       101641  +    ){
       101642  +      pParse->okConstFactor = 1;
101081 101643       }
101082         -#endif
       101644  +
101083 101645     }
101084 101646     return v;
101085 101647   }
101086 101648   
101087 101649   
101088 101650   /*
101089 101651   ** Compute the iLimit and iOffset fields of the SELECT based on the
................................................................................
101133 101695         if( n==0 ){
101134 101696           sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
101135 101697         }else if( n>=0 && p->nSelectRow>(u64)n ){
101136 101698           p->nSelectRow = n;
101137 101699         }
101138 101700       }else{
101139 101701         sqlite3ExprCode(pParse, p->pLimit, iLimit);
101140         -      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
       101702  +      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
101141 101703         VdbeComment((v, "LIMIT counter"));
101142         -      sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
       101704  +      sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak); VdbeCoverage(v);
101143 101705       }
101144 101706       if( p->pOffset ){
101145 101707         p->iOffset = iOffset = ++pParse->nMem;
101146 101708         pParse->nMem++;   /* Allocate an extra register for limit+offset */
101147 101709         sqlite3ExprCode(pParse, p->pOffset, iOffset);
101148         -      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
       101710  +      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
101149 101711         VdbeComment((v, "OFFSET counter"));
101150         -      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
       101712  +      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); VdbeCoverage(v);
101151 101713         sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
101152 101714         sqlite3VdbeJumpHere(v, addr1);
101153 101715         sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
101154 101716         VdbeComment((v, "LIMIT+OFFSET"));
101155         -      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
       101717  +      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); VdbeCoverage(v);
101156 101718         sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
101157 101719         sqlite3VdbeJumpHere(v, addr1);
101158 101720       }
101159 101721     }
101160 101722   }
101161 101723   
101162 101724   #ifndef SQLITE_OMIT_COMPOUND_SELECT
................................................................................
101331 101893       p->selFlags |= SF_UsesEphemeral;
101332 101894     }
101333 101895   
101334 101896     /* Detach the ORDER BY clause from the compound SELECT */
101335 101897     p->pOrderBy = 0;
101336 101898   
101337 101899     /* Store the results of the setup-query in Queue. */
       101900  +  pSetup->pNext = 0;
101338 101901     rc = sqlite3Select(pParse, pSetup, &destQueue);
       101902  +  pSetup->pNext = p;
101339 101903     if( rc ) goto end_of_recursive_query;
101340 101904   
101341 101905     /* Find the next row in the Queue and output that row */
101342         -  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak);
       101906  +  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
101343 101907   
101344 101908     /* Transfer the next row in Queue over to Current */
101345 101909     sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
101346 101910     if( pOrderBy ){
101347 101911       sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
101348 101912     }else{
101349 101913       sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
................................................................................
101351 101915     sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
101352 101916   
101353 101917     /* Output the single row in Current */
101354 101918     addrCont = sqlite3VdbeMakeLabel(v);
101355 101919     codeOffset(v, regOffset, addrCont);
101356 101920     selectInnerLoop(pParse, p, p->pEList, iCurrent,
101357 101921         0, 0, pDest, addrCont, addrBreak);
101358         -  if( regLimit ) sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
       101922  +  if( regLimit ){
       101923  +    sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
       101924  +    VdbeCoverage(v);
       101925  +  }
101359 101926     sqlite3VdbeResolveLabel(v, addrCont);
101360 101927   
101361 101928     /* Execute the recursive SELECT taking the single row in Current as
101362 101929     ** the value for the recursive-table. Store the results in the Queue.
101363 101930     */
101364 101931     p->pPrior = 0;
101365 101932     sqlite3Select(pParse, p, &destQueue);
................................................................................
101436 102003     /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
101437 102004     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
101438 102005     */
101439 102006     assert( p && p->pPrior );  /* Calling function guarantees this much */
101440 102007     assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
101441 102008     db = pParse->db;
101442 102009     pPrior = p->pPrior;
101443         -  assert( pPrior->pRightmost!=pPrior );
101444         -  assert( pPrior->pRightmost==p->pRightmost );
101445 102010     dest = *pDest;
101446 102011     if( pPrior->pOrderBy ){
101447 102012       sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
101448 102013         selectOpName(p->op));
101449 102014       rc = 1;
101450 102015       goto multi_select_end;
101451 102016     }
................................................................................
101513 102078         if( rc ){
101514 102079           goto multi_select_end;
101515 102080         }
101516 102081         p->pPrior = 0;
101517 102082         p->iLimit = pPrior->iLimit;
101518 102083         p->iOffset = pPrior->iOffset;
101519 102084         if( p->iLimit ){
101520         -        addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
       102085  +        addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit); VdbeCoverage(v);
101521 102086           VdbeComment((v, "Jump ahead if LIMIT reached"));
101522 102087         }
101523 102088         explainSetInteger(iSub2, pParse->iNextSelectId);
101524 102089         rc = sqlite3Select(pParse, p, &dest);
101525 102090         testcase( rc!=SQLITE_OK );
101526 102091         pDelete = p->pPrior;
101527 102092         p->pPrior = pPrior;
................................................................................
101545 102110         Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
101546 102111         int addr;
101547 102112         SelectDest uniondest;
101548 102113   
101549 102114         testcase( p->op==TK_EXCEPT );
101550 102115         testcase( p->op==TK_UNION );
101551 102116         priorOp = SRT_Union;
101552         -      if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
       102117  +      if( dest.eDest==priorOp ){
101553 102118           /* We can reuse a temporary table generated by a SELECT to our
101554 102119           ** right.
101555 102120           */
101556         -        assert( p->pRightmost!=p );  /* Can only happen for leftward elements
101557         -                                     ** of a 3-way or more compound */
101558 102121           assert( p->pLimit==0 );      /* Not allowed on leftward elements */
101559 102122           assert( p->pOffset==0 );     /* Not allowed on leftward elements */
101560 102123           unionTab = dest.iSDParm;
101561 102124         }else{
101562 102125           /* We will need to create our own temporary table to hold the
101563 102126           ** intermediate results.
101564 102127           */
101565 102128           unionTab = pParse->nTab++;
101566 102129           assert( p->pOrderBy==0 );
101567 102130           addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
101568 102131           assert( p->addrOpenEphm[0] == -1 );
101569 102132           p->addrOpenEphm[0] = addr;
101570         -        p->pRightmost->selFlags |= SF_UsesEphemeral;
       102133  +        findRightmost(p)->selFlags |= SF_UsesEphemeral;
101571 102134           assert( p->pEList );
101572 102135         }
101573 102136   
101574 102137         /* Code the SELECT statements to our left
101575 102138         */
101576 102139         assert( !pPrior->pOrderBy );
101577 102140         sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
................................................................................
101622 102185             Select *pFirst = p;
101623 102186             while( pFirst->pPrior ) pFirst = pFirst->pPrior;
101624 102187             generateColumnNames(pParse, 0, pFirst->pEList);
101625 102188           }
101626 102189           iBreak = sqlite3VdbeMakeLabel(v);
101627 102190           iCont = sqlite3VdbeMakeLabel(v);
101628 102191           computeLimitRegisters(pParse, p, iBreak);
101629         -        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
       102192  +        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
101630 102193           iStart = sqlite3VdbeCurrentAddr(v);
101631 102194           selectInnerLoop(pParse, p, p->pEList, unionTab,
101632 102195                           0, 0, &dest, iCont, iBreak);
101633 102196           sqlite3VdbeResolveLabel(v, iCont);
101634         -        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
       102197  +        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
101635 102198           sqlite3VdbeResolveLabel(v, iBreak);
101636 102199           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
101637 102200         }
101638 102201         break;
101639 102202       }
101640 102203       default: assert( p->op==TK_INTERSECT ); {
101641 102204         int tab1, tab2;
................................................................................
101652 102215         tab1 = pParse->nTab++;
101653 102216         tab2 = pParse->nTab++;
101654 102217         assert( p->pOrderBy==0 );
101655 102218   
101656 102219         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
101657 102220         assert( p->addrOpenEphm[0] == -1 );
101658 102221         p->addrOpenEphm[0] = addr;
101659         -      p->pRightmost->selFlags |= SF_UsesEphemeral;
       102222  +      findRightmost(p)->selFlags |= SF_UsesEphemeral;
101660 102223         assert( p->pEList );
101661 102224   
101662 102225         /* Code the SELECTs to our left into temporary table "tab1".
101663 102226         */
101664 102227         sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
101665 102228         explainSetInteger(iSub1, pParse->iNextSelectId);
101666 102229         rc = sqlite3Select(pParse, pPrior, &intersectdest);
................................................................................
101697 102260           Select *pFirst = p;
101698 102261           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
101699 102262           generateColumnNames(pParse, 0, pFirst->pEList);
101700 102263         }
101701 102264         iBreak = sqlite3VdbeMakeLabel(v);
101702 102265         iCont = sqlite3VdbeMakeLabel(v);
101703 102266         computeLimitRegisters(pParse, p, iBreak);
101704         -      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
       102267  +      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
101705 102268         r1 = sqlite3GetTempReg(pParse);
101706 102269         iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
101707         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
       102270  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
101708 102271         sqlite3ReleaseTempReg(pParse, r1);
101709 102272         selectInnerLoop(pParse, p, p->pEList, tab1,
101710 102273                         0, 0, &dest, iCont, iBreak);
101711 102274         sqlite3VdbeResolveLabel(v, iCont);
101712         -      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
       102275  +      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
101713 102276         sqlite3VdbeResolveLabel(v, iBreak);
101714 102277         sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
101715 102278         sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
101716 102279         break;
101717 102280       }
101718 102281     }
101719 102282   
................................................................................
101731 102294     if( p->selFlags & SF_UsesEphemeral ){
101732 102295       int i;                        /* Loop counter */
101733 102296       KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
101734 102297       Select *pLoop;                /* For looping through SELECT statements */
101735 102298       CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
101736 102299       int nCol;                     /* Number of columns in result set */
101737 102300   
101738         -    assert( p->pRightmost==p );
       102301  +    assert( p->pNext==0 );
101739 102302       nCol = p->pEList->nExpr;
101740 102303       pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
101741 102304       if( !pKeyInfo ){
101742 102305         rc = SQLITE_NOMEM;
101743 102306         goto multi_select_end;
101744 102307       }
101745 102308       for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
................................................................................
101812 102375     addr = sqlite3VdbeCurrentAddr(v);
101813 102376     iContinue = sqlite3VdbeMakeLabel(v);
101814 102377   
101815 102378     /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
101816 102379     */
101817 102380     if( regPrev ){
101818 102381       int j1, j2;
101819         -    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
       102382  +    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
101820 102383       j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
101821 102384                                 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
101822         -    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
       102385  +    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2); VdbeCoverage(v);
101823 102386       sqlite3VdbeJumpHere(v, j1);
101824 102387       sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
101825 102388       sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
101826 102389     }
101827 102390     if( pParse->db->mallocFailed ) return 0;
101828 102391   
101829 102392     /* Suppress the first OFFSET entries if there is an OFFSET clause
................................................................................
101916 102479         break;
101917 102480       }
101918 102481     }
101919 102482   
101920 102483     /* Jump to the end of the loop if the LIMIT is reached.
101921 102484     */
101922 102485     if( p->iLimit ){
101923         -    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
       102486  +    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
101924 102487     }
101925 102488   
101926 102489     /* Generate the subroutine return
101927 102490     */
101928 102491     sqlite3VdbeResolveLabel(v, iContinue);
101929 102492     sqlite3VdbeAddOp1(v, OP_Return, regReturn);
101930 102493   
................................................................................
102024 102587   ){
102025 102588     int i, j;             /* Loop counters */
102026 102589     Select *pPrior;       /* Another SELECT immediately to our left */
102027 102590     Vdbe *v;              /* Generate code to this VDBE */
102028 102591     SelectDest destA;     /* Destination for coroutine A */
102029 102592     SelectDest destB;     /* Destination for coroutine B */
102030 102593     int regAddrA;         /* Address register for select-A coroutine */
102031         -  int regEofA;          /* Flag to indicate when select-A is complete */
102032 102594     int regAddrB;         /* Address register for select-B coroutine */
102033         -  int regEofB;          /* Flag to indicate when select-B is complete */
102034 102595     int addrSelectA;      /* Address of the select-A coroutine */
102035 102596     int addrSelectB;      /* Address of the select-B coroutine */
102036 102597     int regOutA;          /* Address register for the output-A subroutine */
102037 102598     int regOutB;          /* Address register for the output-B subroutine */
102038 102599     int addrOutA;         /* Address of the output-A subroutine */
102039 102600     int addrOutB = 0;     /* Address of the output-B subroutine */
102040 102601     int addrEofA;         /* Address of the select-A-exhausted subroutine */
       102602  +  int addrEofA_noB;     /* Alternate addrEofA if B is uninitialized */
102041 102603     int addrEofB;         /* Address of the select-B-exhausted subroutine */
102042 102604     int addrAltB;         /* Address of the A<B subroutine */
102043 102605     int addrAeqB;         /* Address of the A==B subroutine */
102044 102606     int addrAgtB;         /* Address of the A>B subroutine */
102045 102607     int regLimitA;        /* Limit register for select-A */
102046 102608     int regLimitB;        /* Limit register for select-A */
102047 102609     int regPrev;          /* A range of registers to hold previous output */
................................................................................
102148 102710         }
102149 102711       }
102150 102712     }
102151 102713    
102152 102714     /* Separate the left and the right query from one another
102153 102715     */
102154 102716     p->pPrior = 0;
       102717  +  pPrior->pNext = 0;
102155 102718     sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
102156 102719     if( pPrior->pPrior==0 ){
102157 102720       sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
102158 102721     }
102159 102722   
102160 102723     /* Compute the limit registers */
102161 102724     computeLimitRegisters(pParse, p, labelEnd);
................................................................................
102170 102733     }
102171 102734     sqlite3ExprDelete(db, p->pLimit);
102172 102735     p->pLimit = 0;
102173 102736     sqlite3ExprDelete(db, p->pOffset);
102174 102737     p->pOffset = 0;
102175 102738   
102176 102739     regAddrA = ++pParse->nMem;
102177         -  regEofA = ++pParse->nMem;
102178 102740     regAddrB = ++pParse->nMem;
102179         -  regEofB = ++pParse->nMem;
102180 102741     regOutA = ++pParse->nMem;
102181 102742     regOutB = ++pParse->nMem;
102182 102743     sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
102183 102744     sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
102184         -
102185         -  /* Jump past the various subroutines and coroutines to the main
102186         -  ** merge loop
102187         -  */
102188         -  j1 = sqlite3VdbeAddOp0(v, OP_Goto);
102189         -  addrSelectA = sqlite3VdbeCurrentAddr(v);
102190         -
102191 102745   
102192 102746     /* Generate a coroutine to evaluate the SELECT statement to the
102193 102747     ** left of the compound operator - the "A" select.
102194 102748     */
102195         -  VdbeNoopComment((v, "Begin coroutine for left SELECT"));
       102749  +  addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
       102750  +  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
       102751  +  VdbeComment((v, "left SELECT"));
102196 102752     pPrior->iLimit = regLimitA;
102197 102753     explainSetInteger(iSub1, pParse->iNextSelectId);
102198 102754     sqlite3Select(pParse, pPrior, &destA);
102199         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
102200         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
102201         -  VdbeNoopComment((v, "End coroutine for left SELECT"));
       102755  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
       102756  +  sqlite3VdbeJumpHere(v, j1);
102202 102757   
102203 102758     /* Generate a coroutine to evaluate the SELECT statement on 
102204 102759     ** the right - the "B" select
102205 102760     */
102206         -  addrSelectB = sqlite3VdbeCurrentAddr(v);
102207         -  VdbeNoopComment((v, "Begin coroutine for right SELECT"));
       102761  +  addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
       102762  +  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
       102763  +  VdbeComment((v, "right SELECT"));
102208 102764     savedLimit = p->iLimit;
102209 102765     savedOffset = p->iOffset;
102210 102766     p->iLimit = regLimitB;
102211 102767     p->iOffset = 0;  
102212 102768     explainSetInteger(iSub2, pParse->iNextSelectId);
102213 102769     sqlite3Select(pParse, p, &destB);
102214 102770     p->iLimit = savedLimit;
102215 102771     p->iOffset = savedOffset;
102216         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
102217         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
102218         -  VdbeNoopComment((v, "End coroutine for right SELECT"));
       102772  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
102219 102773   
102220 102774     /* Generate a subroutine that outputs the current row of the A
102221 102775     ** select as the next output row of the compound select.
102222 102776     */
102223 102777     VdbeNoopComment((v, "Output routine for A"));
102224 102778     addrOutA = generateOutputSubroutine(pParse,
102225 102779                    p, &destA, pDest, regOutA,
................................................................................
102235 102789                    regPrev, pKeyDup, labelEnd);
102236 102790     }
102237 102791     sqlite3KeyInfoUnref(pKeyDup);
102238 102792   
102239 102793     /* Generate a subroutine to run when the results from select A
102240 102794     ** are exhausted and only data in select B remains.
102241 102795     */
102242         -  VdbeNoopComment((v, "eof-A subroutine"));
102243 102796     if( op==TK_EXCEPT || op==TK_INTERSECT ){
102244         -    addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
       102797  +    addrEofA_noB = addrEofA = labelEnd;
102245 102798     }else{  
102246         -    addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
102247         -    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
102248         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
       102799  +    VdbeNoopComment((v, "eof-A subroutine"));
       102800  +    addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
       102801  +    addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
       102802  +                                     VdbeCoverage(v);
102249 102803       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
102250 102804       p->nSelectRow += pPrior->nSelectRow;
102251 102805     }
102252 102806   
102253 102807     /* Generate a subroutine to run when the results from select B
102254 102808     ** are exhausted and only data in select A remains.
102255 102809     */
102256 102810     if( op==TK_INTERSECT ){
102257 102811       addrEofB = addrEofA;
102258 102812       if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
102259 102813     }else{  
102260 102814       VdbeNoopComment((v, "eof-B subroutine"));
102261         -    addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
102262         -    sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
102263         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
       102815  +    addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
       102816  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
102264 102817       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
102265 102818     }
102266 102819   
102267 102820     /* Generate code to handle the case of A<B
102268 102821     */
102269 102822     VdbeNoopComment((v, "A-lt-B subroutine"));
102270 102823     addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
102271         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
102272         -  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
       102824  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
102273 102825     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102274 102826   
102275 102827     /* Generate code to handle the case of A==B
102276 102828     */
102277 102829     if( op==TK_ALL ){
102278 102830       addrAeqB = addrAltB;
102279 102831     }else if( op==TK_INTERSECT ){
102280 102832       addrAeqB = addrAltB;
102281 102833       addrAltB++;
102282 102834     }else{
102283 102835       VdbeNoopComment((v, "A-eq-B subroutine"));
102284 102836       addrAeqB =
102285         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
102286         -    sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
       102837  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
102287 102838       sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102288 102839     }
102289 102840   
102290 102841     /* Generate code to handle the case of A>B
102291 102842     */
102292 102843     VdbeNoopComment((v, "A-gt-B subroutine"));
102293 102844     addrAgtB = sqlite3VdbeCurrentAddr(v);
102294 102845     if( op==TK_ALL || op==TK_UNION ){
102295 102846       sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
102296 102847     }
102297         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
102298         -  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
       102848  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
102299 102849     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102300 102850   
102301 102851     /* This code runs once to initialize everything.
102302 102852     */
102303 102853     sqlite3VdbeJumpHere(v, j1);
102304         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
102305         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
102306         -  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
102307         -  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
102308         -  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
102309         -  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
       102854  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
       102855  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
102310 102856   
102311 102857     /* Implement the main merge loop
102312 102858     */
102313 102859     sqlite3VdbeResolveLabel(v, labelCmpr);
102314 102860     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
102315 102861     sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
102316 102862                            (char*)pKeyMerge, P4_KEYINFO);
102317 102863     sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
102318         -  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
       102864  +  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
102319 102865   
102320 102866     /* Jump to the this point in order to terminate the query.
102321 102867     */
102322 102868     sqlite3VdbeResolveLabel(v, labelEnd);
102323 102869   
102324 102870     /* Set the number of output columns
102325 102871     */
................................................................................
102331 102877   
102332 102878     /* Reassembly the compound query so that it will be freed correctly
102333 102879     ** by the calling function */
102334 102880     if( p->pPrior ){
102335 102881       sqlite3SelectDelete(db, p->pPrior);
102336 102882     }
102337 102883     p->pPrior = pPrior;
       102884  +  pPrior->pNext = p;
102338 102885   
102339 102886     /*** TBD:  Insert subroutine calls to close cursors on incomplete
102340 102887     **** subqueries ****/
102341 102888     explainComposite(pParse, p->op, iSub1, iSub2, 0);
102342 102889     return SQLITE_OK;
102343 102890   }
102344 102891   #endif
................................................................................
102596 103143     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
102597 103144     ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
102598 103145     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
102599 103146     ** became arbitrary expressions, we were forced to add restrictions (13)
102600 103147     ** and (14). */
102601 103148     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
102602 103149     if( pSub->pOffset ) return 0;                          /* Restriction (14) */
102603         -  if( p->pRightmost && pSub->pLimit ){
       103150  +  if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
102604 103151       return 0;                                            /* Restriction (15) */
102605 103152     }
102606 103153     if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
102607 103154     if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
102608 103155     if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
102609 103156        return 0;         /* Restrictions (8)(9) */
102610 103157     }
................................................................................
102747 103294       p->pOffset = 0;
102748 103295       pNew = sqlite3SelectDup(db, p, 0);
102749 103296       p->pOffset = pOffset;
102750 103297       p->pLimit = pLimit;
102751 103298       p->pOrderBy = pOrderBy;
102752 103299       p->pSrc = pSrc;
102753 103300       p->op = TK_ALL;
102754         -    p->pRightmost = 0;
102755 103301       if( pNew==0 ){
102756         -      pNew = pPrior;
       103302  +      p->pPrior = pPrior;
102757 103303       }else{
102758 103304         pNew->pPrior = pPrior;
102759         -      pNew->pRightmost = 0;
       103305  +      if( pPrior ) pPrior->pNext = pNew;
       103306  +      pNew->pNext = p;
       103307  +      p->pPrior = pNew;
102760 103308       }
102761         -    p->pPrior = pNew;
102762 103309       if( db->mallocFailed ) return 1;
102763 103310     }
102764 103311   
102765 103312     /* Begin flattening the iFrom-th entry of the FROM clause 
102766 103313     ** in the outer query.
102767 103314     */
102768 103315     pSub = pSub1 = pSubitem->pSelect;
................................................................................
103093 103640     p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
103094 103641     p->op = TK_SELECT;
103095 103642     p->pWhere = 0;
103096 103643     pNew->pGroupBy = 0;
103097 103644     pNew->pHaving = 0;
103098 103645     pNew->pOrderBy = 0;
103099 103646     p->pPrior = 0;
       103647  +  p->pNext = 0;
       103648  +  p->selFlags &= ~SF_Compound;
       103649  +  assert( pNew->pPrior!=0 );
       103650  +  pNew->pPrior->pNext = pNew;
103100 103651     pNew->pLimit = 0;
103101 103652     pNew->pOffset = 0;
103102 103653     return WRC_Continue;
103103 103654   }
103104 103655   
103105 103656   #ifndef SQLITE_OMIT_CTE
103106 103657   /*
................................................................................
103280 103831   **
103281 103832   ** This function is used as the xSelectCallback2() callback by
103282 103833   ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
103283 103834   ** names and other FROM clause elements. 
103284 103835   */
103285 103836   static void selectPopWith(Walker *pWalker, Select *p){
103286 103837     Parse *pParse = pWalker->pParse;
103287         -  if( p->pWith ){
103288         -    assert( pParse->pWith==p->pWith );
103289         -    pParse->pWith = p->pWith->pOuter;
       103838  +  With *pWith = findRightmost(p)->pWith;
       103839  +  if( pWith!=0 ){
       103840  +    assert( pParse->pWith==pWith );
       103841  +    pParse->pWith = pWith->pOuter;
103290 103842     }
103291 103843   }
103292 103844   #else
103293 103845   #define selectPopWith 0
103294 103846   #endif
103295 103847   
103296 103848   /*
................................................................................
103332 103884       return WRC_Abort;
103333 103885     }
103334 103886     if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
103335 103887       return WRC_Prune;
103336 103888     }
103337 103889     pTabList = p->pSrc;
103338 103890     pEList = p->pEList;
103339         -  sqlite3WithPush(pParse, p->pWith, 0);
       103891  +  sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
103340 103892   
103341 103893     /* Make sure cursor numbers have been assigned to all entries in
103342 103894     ** the FROM clause of the SELECT statement.
103343 103895     */
103344 103896     sqlite3SrcListAssignCursors(pParse, pTabList);
103345 103897   
103346 103898     /* Look up every table named in the FROM clause of the select.  If
................................................................................
103845 104397     ** may have been used, invalidating the underlying buffer holding the
103846 104398     ** text or blob value. See ticket [883034dcb5].
103847 104399     **
103848 104400     ** Another solution would be to change the OP_SCopy used to copy cached
103849 104401     ** values to an OP_Copy.
103850 104402     */
103851 104403     if( regHit ){
103852         -    addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit);
       104404  +    addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
103853 104405     }
103854 104406     sqlite3ExprCacheClear(pParse);
103855 104407     for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
103856 104408       sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
103857 104409     }
103858 104410     pAggInfo->directMode = 0;
103859 104411     sqlite3ExprCacheClear(pParse);
................................................................................
104004 104556       if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
104005 104557         /* This subquery can be absorbed into its parent. */
104006 104558         if( isAggSub ){
104007 104559           isAgg = 1;
104008 104560           p->selFlags |= SF_Aggregate;
104009 104561         }
104010 104562         i = -1;
104011         -    }else if( pTabList->nSrc==1 && (p->selFlags & SF_Materialize)==0
104012         -      && OptimizationEnabled(db, SQLITE_SubqCoroutine)
       104563  +    }else if( pTabList->nSrc==1
       104564  +           && OptimizationEnabled(db, SQLITE_SubqCoroutine)
104013 104565       ){
104014 104566         /* Implement a co-routine that will return a single row of the result
104015 104567         ** set on each invocation.
104016 104568         */
104017         -      int addrTop;
104018         -      int addrEof;
       104569  +      int addrTop = sqlite3VdbeCurrentAddr(v)+1;
104019 104570         pItem->regReturn = ++pParse->nMem;
104020         -      addrEof = ++pParse->nMem;
104021         -      /* Before coding the OP_Goto to jump to the start of the main routine,
104022         -      ** ensure that the jump to the verify-schema routine has already
104023         -      ** been coded. Otherwise, the verify-schema would likely be coded as 
104024         -      ** part of the co-routine. If the main routine then accessed the 
104025         -      ** database before invoking the co-routine for the first time (for 
104026         -      ** example to initialize a LIMIT register from a sub-select), it would 
104027         -      ** be doing so without having verified the schema version and obtained 
104028         -      ** the required db locks. See ticket d6b36be38.  */
104029         -      sqlite3CodeVerifySchema(pParse, -1);
104030         -      sqlite3VdbeAddOp0(v, OP_Goto);
104031         -      addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
104032         -      sqlite3VdbeChangeP5(v, 1);
104033         -      VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
       104571  +      sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
       104572  +      VdbeComment((v, "%s", pItem->pTab->zName));
104034 104573         pItem->addrFillSub = addrTop;
104035         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
104036         -      sqlite3VdbeChangeP5(v, 1);
104037 104574         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
104038 104575         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104039 104576         sqlite3Select(pParse, pSub, &dest);
104040 104577         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104041 104578         pItem->viaCoroutine = 1;
104042         -      sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
104043         -      sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
104044         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
104045         -      sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
104046         -      VdbeComment((v, "end %s", pItem->pTab->zName));
       104579  +      pItem->regResult = dest.iSdst;
       104580  +      sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
104047 104581         sqlite3VdbeJumpHere(v, addrTop-1);
104048 104582         sqlite3ClearTempRegCache(pParse);
104049 104583       }else{
104050 104584         /* Generate a subroutine that will fill an ephemeral table with
104051 104585         ** the content of this subquery.  pItem->addrFillSub will point
104052 104586         ** to the address of the generated subroutine.  pItem->regReturn
104053 104587         ** is a register allocated to hold the subroutine return address
................................................................................
104055 104589         int topAddr;
104056 104590         int onceAddr = 0;
104057 104591         int retAddr;
104058 104592         assert( pItem->addrFillSub==0 );
104059 104593         pItem->regReturn = ++pParse->nMem;
104060 104594         topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
104061 104595         pItem->addrFillSub = topAddr+1;
104062         -      VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
104063 104596         if( pItem->isCorrelated==0 ){
104064 104597           /* If the subquery is not correlated and if we are not inside of
104065 104598           ** a trigger, then we only need to compute the value of the subquery
104066 104599           ** once. */
104067         -        onceAddr = sqlite3CodeOnce(pParse);
       104600  +        onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
       104601  +        VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
       104602  +      }else{
       104603  +        VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
104068 104604         }
104069 104605         sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
104070 104606         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104071 104607         sqlite3Select(pParse, pSub, &dest);
104072 104608         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104073 104609         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
104074 104610         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
................................................................................
104092 104628     pHaving = p->pHaving;
104093 104629     sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
104094 104630   
104095 104631   #ifndef SQLITE_OMIT_COMPOUND_SELECT
104096 104632     /* If there is are a sequence of queries, do the earlier ones first.
104097 104633     */
104098 104634     if( p->pPrior ){
104099         -    if( p->pRightmost==0 ){
104100         -      Select *pLoop, *pRight = 0;
104101         -      int cnt = 0;
104102         -      int mxSelect;
104103         -      for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
104104         -        pLoop->pRightmost = p;
104105         -        pLoop->pNext = pRight;
104106         -        pRight = pLoop;
104107         -      }
104108         -      mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
104109         -      if( mxSelect && cnt>mxSelect ){
104110         -        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
104111         -        goto select_end;
104112         -      }
104113         -    }
104114 104635       rc = multiSelect(pParse, p, pDest);
104115 104636       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
104116 104637       return rc;
104117 104638     }
104118 104639   #endif
104119 104640   
104120 104641     /* If there is both a GROUP BY and an ORDER BY clause and they are
................................................................................
104410 104931           sqlite3ReleaseTempReg(pParse, regRecord);
104411 104932           sqlite3ReleaseTempRange(pParse, regBase, nCol);
104412 104933           sqlite3WhereEnd(pWInfo);
104413 104934           sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
104414 104935           sortOut = sqlite3GetTempReg(pParse);
104415 104936           sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
104416 104937           sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
104417         -        VdbeComment((v, "GROUP BY sort"));
       104938  +        VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
104418 104939           sAggInfo.useSortingIdx = 1;
104419 104940           sqlite3ExprCacheClear(pParse);
104420 104941         }
104421 104942   
104422 104943         /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
104423 104944         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
104424 104945         ** Then compare the current GROUP BY terms against the GROUP BY terms
................................................................................
104437 104958             sAggInfo.directMode = 1;
104438 104959             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
104439 104960           }
104440 104961         }
104441 104962         sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
104442 104963                             (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
104443 104964         j1 = sqlite3VdbeCurrentAddr(v);
104444         -      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
       104965  +      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1); VdbeCoverage(v);
104445 104966   
104446 104967         /* Generate code that runs whenever the GROUP BY changes.
104447 104968         ** Changes in the GROUP BY are detected by the previous code
104448 104969         ** block.  If there were no changes, this block is skipped.
104449 104970         **
104450 104971         ** This code copies current group by terms in b0,b1,b2,...
104451 104972         ** over to a0,a1,a2.  It then calls the output subroutine
104452 104973         ** and resets the aggreg