System.Data.SQLite
Changes On Branch vs2010
Not logged in

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

Changes In Branch vs2010 Excluding Merge-Ins

This is equivalent to a diff from 7f9e634bf5 to 8a92ed133d

2011-01-04
21:55
Merge the VS2010 updates into trunk. check-in: b6e6cd73f6 user: drh tags: trunk
20:46
Update internal SQLite engine to 3.7.4. Update SDS version to 1.0.67.0. Closed-Leaf check-in: 8a92ed133d user: shaneh tags: vs2010
2010-12-20
18:34
Another missing project file. check-in: eb30ee2240 user: shaneh tags: vs2010
2010-12-16
17:49
Changes to get base portions compiling under Visual Studio 2010. Still much to be done. Need to sort out packaging, installation, wince, etc. check-in: 644aeca13c user: shaneh tags: vs2010
2010-11-24
14:29
Remove a duplicate DLL whose name differs only in case. check-in: 7f9e634bf5 user: drh tags: trunk
2010-08-15
22:10
Fix guid type Closed-Leaf check-in: 555d0d530d user: rmsimpson tags: sourceforge

Changes to Doc/Extra/dbfactorysupport.html.

    93     93   <configuration>
    94     94     <system.data>
    95     95       <DbProviderFactories>
    96     96         <remove invariant="System.Data.SQLite"/>
    97     97         <add name="SQLite Data Provider" invariant="System.Data.SQLite" 
    98     98              description=".Net Framework Data Provider for SQLite"
    99     99              type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite,
   100         -                 Version=1.0.66.0, Culture=neutral,
          100  +                 Version=1.0.67.0, Culture=neutral,
   101    101                    PublicKeyToken=db937bc2d44ff139"/>
   102    102       </DbProviderFactories>
   103    103     </system.data>
   104    104   </configuration>
   105    105   </pre>
   106    106         </div>
   107    107         <p>

Changes to Doc/Extra/version.html.

    51     51             </td>
    52     52           </tr>
    53     53         </table>
    54     54       </div>
    55     55       <div id="mainSection">
    56     56       <div id="mainBody">
    57     57       <h1 class="heading">Version History</h1>
           58  +    <p><b>1.0.67.0 - January 3, 2011</b></p>
           59  +    <ul>
           60  +      <li>Code merge with SQLite 3.7.4</li>
           61  +      <li>Continuing work on supporting Visual Studio 2010</li>
           62  +    </ul>
           63  +    <p><b>1.0.66.1 - August 1, 2010</b></p>
           64  +    <ul>
           65  +      <li>Code merge with SQLite 3.7.0.1</li>
           66  +      <li>Re-enabled VS2005 designer support, broken in previous versions during the 2008 transition</li>
           67  +      <li>Implemented new forms of Take/Skip in the EF framework courtesy jlsantiago</li>
           68  +      <li>Added "Foreign Keys" to the connection string parameters</li>
           69  +      <li>Added the Truncate option to the Journal Modes enumeration</li>
           70  +    </ul>
    58     71       <p><b>1.0.66.0 - April 18, 2010</b></p>
    59     72       <ul>
    60     73         <li>Code merge with SQLite 3.6.23.1</li>
    61     74         <li>Fixed a bug in the installer that accidentally modified the machine.config on .NET versions prior to 2.0, invaliding the config file.</li>
    62     75         <li>Fixed INTERSECT and EXCEPT union query generation in EF</li>
    63     76         <li>Fixed an out of memory error in the trigger designer in cases where a WHEN clause is used in the trigger</li>
    64     77       </ul>

Changes to SQLite.Designer/AssemblyInfo.cs.

     5      5   using System.Runtime.ConstrainedExecution;
     6      6   using System.Resources;
     7      7   
     8      8   // General Information about an assembly is controlled through the following 
     9      9   // set of attributes. Change these attribute values to modify the information
    10     10   // associated with an assembly.
    11     11   [assembly: AssemblyTitle("SQLite.Designer")]
    12         -[assembly: AssemblyDescription("ADO.NET 2.0 Data Designer for SQLite")]
           12  +[assembly: AssemblyDescription("ADO.NET 4.0 Data Designer for SQLite")]
    13     13   [assembly: AssemblyConfiguration("")]
    14     14   [assembly: AssemblyCompany("http://sqlite.phxsoftware.com")]
    15     15   [assembly: AssemblyProduct("SQLite Designer")]
    16     16   [assembly: AssemblyCopyright("Public Domain")]
    17     17   [assembly: AssemblyTrademark("")]
    18     18   [assembly: AssemblyCulture("")]
    19     19   
................................................................................
    30     30   //      Major Version
    31     31   //      Minor Version 
    32     32   //      Build Number
    33     33   //      Revision
    34     34   //
    35     35   // You can specify all the values or you can default the Revision and Build Numbers 
    36     36   // by using the '*' as shown below:
    37         -[assembly: AssemblyVersion("1.0.37.0")]
    38         -[assembly: AssemblyFileVersion("1.0.37.0")]
           37  +[assembly: AssemblyVersion("1.0.38.1")]
           38  +[assembly: AssemblyFileVersion("1.0.38.1")]
    39     39   [assembly: AssemblyDelaySignAttribute(false)]
    40     40   [assembly: AssemblyKeyFileAttribute("..\\System.Data.SQLite\\System.Data.SQLite.snk")]
    41     41   [assembly: AssemblyKeyNameAttribute("")]

Changes to SQLite.Designer/SQLite.Designer.csproj.

     1         -<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
            1  +<?xml version="1.0" encoding="utf-8"?>
            2  +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
     2      3     <PropertyGroup>
     3      4       <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     4      5       <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
     5      6       <ProductVersion>9.0.30729</ProductVersion>
     6      7       <SchemaVersion>2.0</SchemaVersion>
     7      8       <ProjectGuid>{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}</ProjectGuid>
     8      9       <OutputType>Library</OutputType>
................................................................................
    11     12       <AssemblyName>SQLite.Designer</AssemblyName>
    12     13       <SignAssembly>false</SignAssembly>
    13     14       <RegisterOutputPackage>false</RegisterOutputPackage>
    14     15       <RegisterWithCodebase>false</RegisterWithCodebase>
    15     16       <GeneratePkgDefFile>false</GeneratePkgDefFile>
    16     17       <FileUpgradeFlags>
    17     18       </FileUpgradeFlags>
    18         -    <OldToolsVersion>2.0</OldToolsVersion>
           19  +    <OldToolsVersion>3.5</OldToolsVersion>
    19     20       <UpgradeBackupLocation>
    20     21       </UpgradeBackupLocation>
           22  +    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
           23  +    <PublishUrl>publish\</PublishUrl>
           24  +    <Install>true</Install>
           25  +    <InstallFrom>Disk</InstallFrom>
           26  +    <UpdateEnabled>false</UpdateEnabled>
           27  +    <UpdateMode>Foreground</UpdateMode>
           28  +    <UpdateInterval>7</UpdateInterval>
           29  +    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
           30  +    <UpdatePeriodically>false</UpdatePeriodically>
           31  +    <UpdateRequired>false</UpdateRequired>
           32  +    <MapFileExtensions>true</MapFileExtensions>
           33  +    <ApplicationRevision>0</ApplicationRevision>
           34  +    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
           35  +    <IsWebBootstrapper>false</IsWebBootstrapper>
           36  +    <UseApplicationTrust>false</UseApplicationTrust>
           37  +    <BootstrapperEnabled>true</BootstrapperEnabled>
           38  +    <TargetFrameworkProfile />
    21     39     </PropertyGroup>
    22     40     <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    23     41       <DebugSymbols>true</DebugSymbols>
    24     42       <DebugType>full</DebugType>
    25     43       <Optimize>false</Optimize>
    26     44       <OutputPath>..\bin\designer\</OutputPath>
    27     45       <DefineConstants>DEBUG;TRACE</DefineConstants>
................................................................................
    30     48       <NoWarn>1701;1702;1699;3001</NoWarn>
    31     49       <FileAlignment>512</FileAlignment>
    32     50       <PlatformTarget>x86</PlatformTarget>
    33     51       <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
    34     52       <RegisterOutputPackage>false</RegisterOutputPackage>
    35     53       <GeneratePkgDefFile>false</GeneratePkgDefFile>
    36     54       <RegisterWithCodebase>false</RegisterWithCodebase>
           55  +    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
    37     56     </PropertyGroup>
    38     57     <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    39     58       <DebugType>none</DebugType>
    40     59       <Optimize>true</Optimize>
    41     60       <OutputPath>..\bin\Designer\</OutputPath>
    42     61       <DefineConstants>
    43     62       </DefineConstants>
................................................................................
    47     66       <FileAlignment>512</FileAlignment>
    48     67       <PlatformTarget>x86</PlatformTarget>
    49     68       <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
    50     69       <RegisterOutputPackage>false</RegisterOutputPackage>
    51     70       <RegisterWithCodebase>false</RegisterWithCodebase>
    52     71       <GeneratePkgDefFile>false</GeneratePkgDefFile>
    53     72       <UseVSHostingProcess>false</UseVSHostingProcess>
           73  +    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
           74  +  </PropertyGroup>
           75  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
           76  +    <DebugSymbols>true</DebugSymbols>
           77  +    <OutputPath>bin\x86\Debug\</OutputPath>
           78  +    <DefineConstants>DEBUG;TRACE</DefineConstants>
           79  +    <NoWarn>1701;1702;1699;3001</NoWarn>
           80  +    <DebugType>full</DebugType>
           81  +    <PlatformTarget>x86</PlatformTarget>
           82  +    <CodeAnalysisLogFile>..\bin\designer\SQLite.Designer.dll.CodeAnalysisLog.xml</CodeAnalysisLogFile>
           83  +    <CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
           84  +    <CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
           85  +    <ErrorReport>prompt</ErrorReport>
           86  +    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
           87  +    <CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
           88  +    <CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
           89  +  </PropertyGroup>
           90  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
           91  +    <OutputPath>..\bin\designer\</OutputPath>
           92  +    <Optimize>true</Optimize>
           93  +    <NoWarn>1701;1702;1699;3001</NoWarn>
           94  +    <PlatformTarget>x86</PlatformTarget>
           95  +    <CodeAnalysisLogFile>..\bin\Designer\SQLite.Designer.dll.CodeAnalysisLog.xml</CodeAnalysisLogFile>
           96  +    <CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
           97  +    <CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
           98  +    <UseVSHostingProcess>false</UseVSHostingProcess>
           99  +    <ErrorReport>prompt</ErrorReport>
          100  +    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
          101  +    <CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
          102  +    <CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
    54    103     </PropertyGroup>
    55    104     <ItemGroup>
    56    105       <Reference Include="EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    57    106         <SpecificVersion>False</SpecificVersion>
    58    107         <HintPath>..\..\..\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\EnvDTE.dll</HintPath>
    59    108       </Reference>
    60    109       <Reference Include="Microsoft.Data.ConnectionUI, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
    61    110         <SpecificVersion>False</SpecificVersion>
    62         -      <HintPath>..\..\Microsoft.Data.ConnectionUI.dll</HintPath>
          111  +      <HintPath>..\..\..\Program Files (x86)\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Common\Assemblies\2005\Microsoft.Data.ConnectionUI.dll</HintPath>
    63    112         <Private>False</Private>
    64    113       </Reference>
    65    114       <Reference Include="Microsoft.VisualStudio.CommandBars, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    66    115         <SpecificVersion>False</SpecificVersion>
    67    116         <HintPath>..\..\..\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\Microsoft.VisualStudio.CommandBars.dll</HintPath>
    68    117       </Reference>
    69    118       <Reference Include="Microsoft.VisualStudio.Data, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
    70         -      <HintPath>..\..\Microsoft.VisualStudio.Data.dll</HintPath>
    71    119         <SpecificVersion>False</SpecificVersion>
          120  +      <HintPath>..\..\..\Program Files (x86)\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Common\Assemblies\2005\Microsoft.VisualStudio.Data.dll</HintPath>
    72    121         <Private>False</Private>
    73    122       </Reference>
          123  +    <Reference Include="Microsoft.VisualStudio.Data.Services, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
    74    124       <Reference Include="Microsoft.VisualStudio.OLE.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    75    125         <SpecificVersion>False</SpecificVersion>
    76    126       </Reference>
    77    127       <Reference Include="Microsoft.VisualStudio.Shell, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
    78    128         <SpecificVersion>False</SpecificVersion>
    79    129       </Reference>
    80    130       <Reference Include="Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
................................................................................
   165    215     <ItemGroup>
   166    216       <VSCTCompile Include="PkgCmd.vsct">
   167    217         <ResourceName>1000</ResourceName>
   168    218       </VSCTCompile>
   169    219     </ItemGroup>
   170    220     <ItemGroup>
   171    221       <EmbeddedResource Include="SQLiteDataViewSupport2008.xml" />
          222  +    <EmbeddedResource Include="SQLiteDataViewSupport2010.xml" />
   172    223       <EmbeddedResource Include="VSPackage.resx">
   173    224         <Generator>ResXFileCodeGenerator</Generator>
   174    225         <LastGenOutput>VSPackage.Designer.cs</LastGenOutput>
   175    226         <MergeWithCTO>true</MergeWithCTO>
   176    227         <SubType>Designer</SubType>
   177    228       </EmbeddedResource>
   178    229     </ItemGroup>
................................................................................
   208    259     </ItemGroup>
   209    260     <ItemGroup>
   210    261       <None Include="Resources\ToolboxItems.txt" />
   211    262     </ItemGroup>
   212    263     <ItemGroup>
   213    264       <Folder Include="Properties\" />
   214    265     </ItemGroup>
          266  +  <ItemGroup>
          267  +    <None Include="source.extension.vsixmanifest" />
          268  +  </ItemGroup>
          269  +  <ItemGroup>
          270  +    <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
          271  +      <Visible>False</Visible>
          272  +      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
          273  +      <Install>false</Install>
          274  +    </BootstrapperPackage>
          275  +    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
          276  +      <Visible>False</Visible>
          277  +      <ProductName>.NET Framework 3.5 SP1</ProductName>
          278  +      <Install>true</Install>
          279  +    </BootstrapperPackage>
          280  +    <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
          281  +      <Visible>False</Visible>
          282  +      <ProductName>Windows Installer 3.1</ProductName>
          283  +      <Install>true</Install>
          284  +    </BootstrapperPackage>
          285  +  </ItemGroup>
   215    286     <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   216         -  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\VSSDK\Microsoft.VsSDK.targets" />
          287  +  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\VSSDK\Microsoft.VsSDK.targets" />
   217    288     <PropertyGroup>
   218    289       <PostBuildEvent>
   219    290       </PostBuildEvent>
   220    291     </PropertyGroup>
   221    292   </Project>

Changes to SQLite.Designer/SQLite.Designer.sln.

     1      1   
     2         -Microsoft Visual Studio Solution File, Format Version 10.00
     3         -# Visual Studio 2008
            2  +Microsoft Visual Studio Solution File, Format Version 11.00
            3  +# Visual Studio 2010
     4      4   Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLite.Designer", "SQLite.Designer.csproj", "{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}"
     5      5   EndProject
     6      6   Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "install", "..\tools\install\install.csproj", "{71EED886-B5BF-488E-A4AA-1403E393D224}"
     7      7   	ProjectSection(ProjectDependencies) = postProject
     8         -		{AC139962-261A-4463-B6FA-AEBC25283A66} = {AC139962-261A-4463-B6FA-AEBC25283A66}
     9      8   		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198} = {9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}
    10      9   	EndProjectSection
    11     10   EndProject
    12         -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Data.SQLite - CFDT", "..\System.Data.SQLite\System.Data.SQLite - CFDT.csproj", "{AC139962-261A-4463-B6FA-AEBC25283A66}"
    13         -EndProject
    14     11   Global
    15     12   	GlobalSection(SolutionConfigurationPlatforms) = preSolution
    16     13   		Debug|Any CPU = Debug|Any CPU
           14  +		Debug|x86 = Debug|x86
    17     15   		Release|Any CPU = Release|Any CPU
           16  +		Release|x86 = Release|x86
    18     17   	EndGlobalSection
    19     18   	GlobalSection(ProjectConfigurationPlatforms) = postSolution
    20     19   		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    21     20   		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Debug|Any CPU.Build.0 = Debug|Any CPU
    22         -		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Release|Any CPU.ActiveCfg = Release|Any CPU
    23         -		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Release|Any CPU.Build.0 = Release|Any CPU
           21  +		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Debug|x86.ActiveCfg = Debug|x86
           22  +		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Debug|x86.Build.0 = Debug|x86
           23  +		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Release|Any CPU.ActiveCfg = Release|x86
           24  +		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Release|Any CPU.Build.0 = Release|x86
           25  +		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Release|x86.ActiveCfg = Release|x86
           26  +		{9B4A5CF6-5BE5-4926-ACC7-B729A8C05198}.Release|x86.Build.0 = Release|x86
    24     27   		{71EED886-B5BF-488E-A4AA-1403E393D224}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    25     28   		{71EED886-B5BF-488E-A4AA-1403E393D224}.Debug|Any CPU.Build.0 = Debug|Any CPU
           29  +		{71EED886-B5BF-488E-A4AA-1403E393D224}.Debug|x86.ActiveCfg = Debug|x86
           30  +		{71EED886-B5BF-488E-A4AA-1403E393D224}.Debug|x86.Build.0 = Debug|x86
    26     31   		{71EED886-B5BF-488E-A4AA-1403E393D224}.Release|Any CPU.ActiveCfg = Release|Any CPU
    27     32   		{71EED886-B5BF-488E-A4AA-1403E393D224}.Release|Any CPU.Build.0 = Release|Any CPU
    28         -		{AC139962-261A-4463-B6FA-AEBC25283A66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    29         -		{AC139962-261A-4463-B6FA-AEBC25283A66}.Debug|Any CPU.Build.0 = Debug|Any CPU
    30         -		{AC139962-261A-4463-B6FA-AEBC25283A66}.Release|Any CPU.ActiveCfg = Release|Any CPU
    31         -		{AC139962-261A-4463-B6FA-AEBC25283A66}.Release|Any CPU.Build.0 = Release|Any CPU
           33  +		{71EED886-B5BF-488E-A4AA-1403E393D224}.Release|x86.ActiveCfg = Release|x86
           34  +		{71EED886-B5BF-488E-A4AA-1403E393D224}.Release|x86.Build.0 = Release|x86
    32     35   	EndGlobalSection
    33     36   	GlobalSection(SolutionProperties) = preSolution
    34     37   		HideSolutionNode = FALSE
    35     38   	EndGlobalSection
    36     39   EndGlobal

Changes to SQLite.Designer/SQLiteDataViewSupport.cs.

    27     27   
    28     28       private static string GetVSVersion()
    29     29       {
    30     30         switch (System.Diagnostics.FileVersionInfo.GetVersionInfo(Environment.GetCommandLineArgs()[0]).FileMajorPart)
    31     31         {
    32     32           case 8:
    33     33             return "2005";
    34         -        case 9:
    35         -          return "2008";
    36     34   		case 10:
    37     35   		  return "2010";
           36  +        default:
           37  +          return "2008";
    38     38         }
    39     39       }
    40     40     }
    41     41   }

Changes to SQLite.Designer/VSPackage.Designer.cs.

     1      1   //------------------------------------------------------------------------------
     2      2   // <auto-generated>
     3      3   //     This code was generated by a tool.
     4         -//     Runtime Version:2.0.50727.3031
            4  +//     Runtime Version:4.0.30319.1
     5      5   //
     6      6   //     Changes to this file may cause incorrect behavior and will be lost if
     7      7   //     the code is regenerated.
     8      8   // </auto-generated>
     9      9   //------------------------------------------------------------------------------
    10     10   
    11     11   namespace SQLite.Designer {
................................................................................
    15     15       /// <summary>
    16     16       ///   A strongly-typed resource class, for looking up localized strings, etc.
    17     17       /// </summary>
    18     18       // This class was auto-generated by the StronglyTypedResourceBuilder
    19     19       // class via a tool like ResGen or Visual Studio.
    20     20       // To add or remove a member, edit your .ResX file then rerun ResGen
    21     21       // with the /str option, or rebuild your VS project.
    22         -    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")]
           22  +    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    23     23       [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    24     24       [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    25     25       internal class VSPackage {
    26     26           
    27     27           private static global::System.Resources.ResourceManager resourceMan;
    28     28           
    29     29           private static global::System.Globalization.CultureInfo resourceCulture;
................................................................................
   100    100           internal static string ReEncrypt {
   101    101               get {
   102    102                   return ResourceManager.GetString("ReEncrypt", resourceCulture);
   103    103               }
   104    104           }
   105    105           
   106    106           /// <summary>
   107         -        ///   Looks up a localized string similar to .
          107  +        ///   Looks up a localized string similar to [SQLite]
          108  +        ///System.Data.SQLite.SQLiteConnection, System.Data.SQLite
          109  +        ///System.Data.SQLite.SQLiteDataAdapter, System.Data.SQLite
          110  +        ///System.Data.SQLite.SQLiteCommand, System.Data.SQLite
          111  +        ///.
   108    112           /// </summary>
   109    113           internal static string ToolboxItems {
   110    114               get {
   111    115                   return ResourceManager.GetString("ToolboxItems", resourceCulture);
   112    116               }
   113    117           }
   114    118       }
   115    119   }

Changes to SQLite.Interop/FTS2/fts2.c.

     1      1   /* fts2 has a design flaw which can lead to database corruption (see
     2      2   ** below).  It is recommended not to use it any longer, instead use
     3      3   ** fts3 (or higher).  If you believe that your use of fts2 is safe,
     4      4   ** add -DSQLITE_ENABLE_BROKEN_FTS2=1 to your CFLAGS.
     5      5   */
     6         -#ifndef SQLITE_ENABLE_BROKEN_FTS2
            6  +#if (!defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2)) \
            7  +        && !defined(SQLITE_ENABLE_BROKEN_FTS2)
     7      8   #error fts2 has a design flaw and has been deprecated.
     8      9   #endif
     9     10   /* The flaw is that fts2 uses the content table's unaliased rowid as
    10     11   ** the unique docid.  fts2 embeds the rowid in the index it builds,
    11     12   ** and expects the rowid to not change.  The SQLite VACUUM operation
    12     13   ** will renumber such rowids, thereby breaking fts2.  If you are using
    13     14   ** fts2 in a system which has disabled VACUUM, then you can continue
................................................................................
   269    270   **
   270    271   ** This appears to have only a moderate impact on queries for very
   271    272   ** frequent terms (which are somewhat dominated by segment merge
   272    273   ** costs), and infrequent and non-existent terms still seem to be fast
   273    274   ** even with many segments.
   274    275   **
   275    276   ** TODO(shess) That said, it would be nice to have a better query-side
   276         -** argument for MERGE_COUNT of 16.  Also, it's possible/likely that
          277  +** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
   277    278   ** optimizations to things like doclist merging will swing the sweet
   278    279   ** spot around.
   279    280   **
   280    281   **
   281    282   **
   282    283   **** Handling of deletions and updates ****
   283    284   ** Since we're using a segmented structure, with no docid-oriented
................................................................................
   301    302   # define SQLITE_CORE 1
   302    303   #endif
   303    304   
   304    305   #include <assert.h>
   305    306   #include <stdlib.h>
   306    307   #include <stdio.h>
   307    308   #include <string.h>
   308         -#include <ctype.h>
   309         -
   310    309   #include "fts2.h"
   311    310   #include "fts2_hash.h"
   312    311   #include "fts2_tokenizer.h"
   313    312   #include "sqlite3.h"
   314    313   #include "sqlite3ext.h"
   315    314   SQLITE_EXTENSION_INIT1
   316    315   
................................................................................
   340    339   */
   341    340   /* TODO(shess) The snippet-generation code should be using the
   342    341   ** tokenizer-generated tokens rather than doing its own local
   343    342   ** tokenization.
   344    343   */
   345    344   /* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */
   346    345   static int safe_isspace(char c){
   347         -  return (c&0x80)==0 ? isspace(c) : 0;
          346  +  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
   348    347   }
   349    348   static int safe_tolower(char c){
   350         -  return (c&0x80)==0 ? tolower(c) : c;
          349  +  return (c>='A' && c<='Z') ? (c - 'A' + 'a') : c;
   351    350   }
   352    351   static int safe_isalnum(char c){
   353         -  return (c&0x80)==0 ? isalnum(c) : 0;
          352  +  return (c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z');
   354    353   }
   355    354   
   356    355   typedef enum DocListType {
   357    356     DL_DOCIDS,              /* docids only */
   358    357     DL_POSITIONS,           /* docids + positions */
   359    358     DL_POSITIONS_OFFSETS    /* docids + positions + offsets */
   360    359   } DocListType;
................................................................................
   450    449   /* DataBuffer is used to collect data into a buffer in piecemeal
   451    450   ** fashion.  It implements the usual distinction between amount of
   452    451   ** data currently stored (nData) and buffer capacity (nCapacity).
   453    452   **
   454    453   ** dataBufferInit - create a buffer with given initial capacity.
   455    454   ** dataBufferReset - forget buffer's data, retaining capacity.
   456    455   ** dataBufferDestroy - free buffer's data.
          456  +** dataBufferSwap - swap contents of two buffers.
   457    457   ** dataBufferExpand - expand capacity without adding data.
   458    458   ** dataBufferAppend - append data.
   459    459   ** dataBufferAppend2 - append two pieces of data at once.
   460    460   ** dataBufferReplace - replace buffer's data.
   461    461   */
   462    462   typedef struct DataBuffer {
   463    463     char *pData;          /* Pointer to malloc'ed buffer. */
................................................................................
   465    465     int nData;            /* End of data loaded into pData. */
   466    466   } DataBuffer;
   467    467   
   468    468   static void dataBufferInit(DataBuffer *pBuffer, int nCapacity){
   469    469     assert( nCapacity>=0 );
   470    470     pBuffer->nData = 0;
   471    471     pBuffer->nCapacity = nCapacity;
   472         -  pBuffer->pData = nCapacity==0 ? NULL : malloc(nCapacity);
          472  +  pBuffer->pData = nCapacity==0 ? NULL : sqlite3_malloc(nCapacity);
   473    473   }
   474    474   static void dataBufferReset(DataBuffer *pBuffer){
   475    475     pBuffer->nData = 0;
   476    476   }
   477    477   static void dataBufferDestroy(DataBuffer *pBuffer){
   478         -  if( pBuffer->pData!=NULL ) free(pBuffer->pData);
          478  +  if( pBuffer->pData!=NULL ) sqlite3_free(pBuffer->pData);
   479    479     SCRAMBLE(pBuffer);
          480  +}
          481  +static void dataBufferSwap(DataBuffer *pBuffer1, DataBuffer *pBuffer2){
          482  +  DataBuffer tmp = *pBuffer1;
          483  +  *pBuffer1 = *pBuffer2;
          484  +  *pBuffer2 = tmp;
   480    485   }
   481    486   static void dataBufferExpand(DataBuffer *pBuffer, int nAddCapacity){
   482    487     assert( nAddCapacity>0 );
   483    488     /* TODO(shess) Consider expanding more aggressively.  Note that the
   484    489     ** underlying malloc implementation may take care of such things for
   485    490     ** us already.
   486    491     */
   487    492     if( pBuffer->nData+nAddCapacity>pBuffer->nCapacity ){
   488    493       pBuffer->nCapacity = pBuffer->nData+nAddCapacity;
   489         -    pBuffer->pData = realloc(pBuffer->pData, pBuffer->nCapacity);
          494  +    pBuffer->pData = sqlite3_realloc(pBuffer->pData, pBuffer->nCapacity);
   490    495     }
   491    496   }
   492    497   static void dataBufferAppend(DataBuffer *pBuffer,
   493    498                                const char *pSource, int nSource){
   494    499     assert( nSource>0 && pSource!=NULL );
   495    500     dataBufferExpand(pBuffer, nSource);
   496    501     memcpy(pBuffer->pData+pBuffer->nData, pSource, nSource);
................................................................................
   689    694   }
   690    695   static void dlrDestroy(DLReader *pReader){
   691    696     SCRAMBLE(pReader);
   692    697   }
   693    698   
   694    699   #ifndef NDEBUG
   695    700   /* Verify that the doclist can be validly decoded.  Also returns the
   696         -** last docid found because it's convenient in other assertions for
          701  +** last docid found because it is convenient in other assertions for
   697    702   ** DLWriter.
   698    703   */
   699    704   static void docListValidate(DocListType iType, const char *pData, int nData,
   700    705                               sqlite_int64 *pLastDocid){
   701    706     sqlite_int64 iPrevDocid = 0;
   702    707     assert( nData>0 );
   703    708     assert( pData!=0 );
................................................................................
  1052   1057     DLWriter dlw;
  1053   1058     PLWriter plw;
  1054   1059   } DLCollector;
  1055   1060   
  1056   1061   /* TODO(shess) This could also be done by calling plwTerminate() and
  1057   1062   ** dataBufferAppend().  I tried that, expecting nominal performance
  1058   1063   ** differences, but it seemed to pretty reliably be worth 1% to code
  1059         -** it this way.  I suspect it's the incremental malloc overhead (some
         1064  +** it this way.  I suspect it is the incremental malloc overhead (some
  1060   1065   ** percentage of the plwTerminate() calls will cause a realloc), so
  1061   1066   ** this might be worth revisiting if the DataBuffer implementation
  1062   1067   ** changes.
  1063   1068   */
  1064   1069   static void dlcAddDoclist(DLCollector *pCollector, DataBuffer *b){
  1065   1070     if( pCollector->dlw.iType>DL_DOCIDS ){
  1066   1071       char c[VARINT_MAX];
................................................................................
  1077   1082   }
  1078   1083   static void dlcAddPos(DLCollector *pCollector, int iColumn, int iPos,
  1079   1084                         int iStartOffset, int iEndOffset){
  1080   1085     plwAdd(&pCollector->plw, iColumn, iPos, iStartOffset, iEndOffset);
  1081   1086   }
  1082   1087   
  1083   1088   static DLCollector *dlcNew(sqlite_int64 iDocid, DocListType iType){
  1084         -  DLCollector *pCollector = malloc(sizeof(DLCollector));
         1089  +  DLCollector *pCollector = sqlite3_malloc(sizeof(DLCollector));
  1085   1090     dataBufferInit(&pCollector->b, 0);
  1086   1091     dlwInit(&pCollector->dlw, iType, &pCollector->b);
  1087   1092     plwInit(&pCollector->plw, &pCollector->dlw, iDocid);
  1088   1093     return pCollector;
  1089   1094   }
  1090   1095   static void dlcDelete(DLCollector *pCollector){
  1091   1096     plwDestroy(&pCollector->plw);
  1092   1097     dlwDestroy(&pCollector->dlw);
  1093   1098     dataBufferDestroy(&pCollector->b);
  1094   1099     SCRAMBLE(pCollector);
  1095         -  free(pCollector);
         1100  +  sqlite3_free(pCollector);
  1096   1101   }
  1097   1102   
  1098   1103   
  1099   1104   /* Copy the doclist data of iType in pData/nData into *out, trimming
  1100   1105   ** unnecessary data as we go.  Only columns matching iColumn are
  1101   1106   ** copied, all columns copied if iColumn is -1.  Elements with no
  1102   1107   ** matching columns are dropped.  The output is an iOutType doclist.
................................................................................
  1347   1352     const char *pRight, int nRight,
  1348   1353     DataBuffer *pOut      /* Write the combined doclist here */
  1349   1354   ){
  1350   1355     DLReader left, right;
  1351   1356     DLWriter writer;
  1352   1357   
  1353   1358     if( nLeft==0 ){
  1354         -    dataBufferAppend(pOut, pRight, nRight);
         1359  +    if( nRight!=0) dataBufferAppend(pOut, pRight, nRight);
  1355   1360       return;
  1356   1361     }
  1357   1362     if( nRight==0 ){
  1358   1363       dataBufferAppend(pOut, pLeft, nLeft);
  1359   1364       return;
  1360   1365     }
  1361   1366   
................................................................................
  1528   1533     const char *pRight, int nRight,
  1529   1534     DataBuffer *pOut      /* Write the combined doclist here */
  1530   1535   ){
  1531   1536     DLReader left, right;
  1532   1537     DLWriter writer;
  1533   1538   
  1534   1539     if( nLeft==0 ){
  1535         -    dataBufferAppend(pOut, pRight, nRight);
         1540  +    if( nRight!=0 ) dataBufferAppend(pOut, pRight, nRight);
  1536   1541       return;
  1537   1542     }
  1538   1543     if( nRight==0 ){
  1539   1544       dataBufferAppend(pOut, pLeft, nLeft);
  1540   1545       return;
  1541   1546     }
  1542   1547   
................................................................................
  1603   1608   
  1604   1609     dlrDestroy(&left);
  1605   1610     dlrDestroy(&right);
  1606   1611     dlwDestroy(&writer);
  1607   1612   }
  1608   1613   
  1609   1614   static char *string_dup_n(const char *s, int n){
  1610         -  char *str = malloc(n + 1);
         1615  +  char *str = sqlite3_malloc(n + 1);
  1611   1616     memcpy(str, s, n);
  1612   1617     str[n] = '\0';
  1613   1618     return str;
  1614   1619   }
  1615   1620   
  1616   1621   /* Duplicate a string; the caller must free() the returned string.
  1617         - * (We don't use strdup() since it's not part of the standard C library and
         1622  + * (We don't use strdup() since it is not part of the standard C library and
  1618   1623    * may not be available everywhere.) */
  1619   1624   static char *string_dup(const char *s){
  1620   1625     return string_dup_n(s, strlen(s));
  1621   1626   }
  1622   1627   
  1623   1628   /* Format a string, replacing each occurrence of the % character with
  1624   1629    * zDb.zName.  This may be more convenient than sqlite_mprintf()
................................................................................
  1636   1641   
  1637   1642     /* first compute length needed */
  1638   1643     for(p = zFormat ; *p ; ++p){
  1639   1644       len += (*p=='%' ? nFullTableName : 1);
  1640   1645     }
  1641   1646     len += 1;  /* for null terminator */
  1642   1647   
  1643         -  r = result = malloc(len);
         1648  +  r = result = sqlite3_malloc(len);
  1644   1649     for(p = zFormat; *p; ++p){
  1645   1650       if( *p=='%' ){
  1646   1651         memcpy(r, zDb, nDb);
  1647   1652         r += nDb;
  1648   1653         *r++ = '.';
  1649   1654         memcpy(r, zName, nName);
  1650   1655         r += nName;
................................................................................
  1659   1664   
  1660   1665   static int sql_exec(sqlite3 *db, const char *zDb, const char *zName,
  1661   1666                       const char *zFormat){
  1662   1667     char *zCommand = string_format(zFormat, zDb, zName);
  1663   1668     int rc;
  1664   1669     TRACE(("FTS2 sql: %s\n", zCommand));
  1665   1670     rc = sqlite3_exec(db, zCommand, NULL, 0, NULL);
  1666         -  free(zCommand);
         1671  +  sqlite3_free(zCommand);
  1667   1672     return rc;
  1668   1673   }
  1669   1674   
  1670   1675   static int sql_prepare(sqlite3 *db, const char *zDb, const char *zName,
  1671   1676                          sqlite3_stmt **ppStmt, const char *zFormat){
  1672   1677     char *zCommand = string_format(zFormat, zDb, zName);
  1673   1678     int rc;
  1674   1679     TRACE(("FTS2 prepare: %s\n", zCommand));
  1675   1680     rc = sqlite3_prepare_v2(db, zCommand, -1, ppStmt, NULL);
  1676         -  free(zCommand);
         1681  +  sqlite3_free(zCommand);
  1677   1682     return rc;
  1678   1683   }
  1679   1684   
  1680   1685   /* end utility functions */
  1681   1686   
  1682   1687   /* Forward reference */
  1683   1688   typedef struct fulltext_vtab fulltext_vtab;
................................................................................
  1763   1768   } QueryType;
  1764   1769   
  1765   1770   typedef enum fulltext_statement {
  1766   1771     CONTENT_INSERT_STMT,
  1767   1772     CONTENT_SELECT_STMT,
  1768   1773     CONTENT_UPDATE_STMT,
  1769   1774     CONTENT_DELETE_STMT,
         1775  +  CONTENT_EXISTS_STMT,
  1770   1776   
  1771   1777     BLOCK_INSERT_STMT,
  1772   1778     BLOCK_SELECT_STMT,
  1773   1779     BLOCK_DELETE_STMT,
         1780  +  BLOCK_DELETE_ALL_STMT,
  1774   1781   
  1775   1782     SEGDIR_MAX_INDEX_STMT,
  1776   1783     SEGDIR_SET_STMT,
  1777         -  SEGDIR_SELECT_STMT,
         1784  +  SEGDIR_SELECT_LEVEL_STMT,
  1778   1785     SEGDIR_SPAN_STMT,
  1779   1786     SEGDIR_DELETE_STMT,
         1787  +  SEGDIR_SELECT_SEGMENT_STMT,
  1780   1788     SEGDIR_SELECT_ALL_STMT,
         1789  +  SEGDIR_DELETE_ALL_STMT,
         1790  +  SEGDIR_COUNT_STMT,
  1781   1791   
  1782   1792     MAX_STMT                     /* Always at end! */
  1783   1793   } fulltext_statement;
  1784   1794   
  1785   1795   /* These must exactly match the enum above. */
  1786   1796   /* TODO(shess): Is there some risk that a statement will be used in two
  1787   1797   ** cursors at once, e.g.  if a query joins a virtual table to itself?
................................................................................
  1788   1798   ** If so perhaps we should move some of these to the cursor object.
  1789   1799   */
  1790   1800   static const char *const fulltext_zStatement[MAX_STMT] = {
  1791   1801     /* CONTENT_INSERT */ NULL,  /* generated in contentInsertStatement() */
  1792   1802     /* CONTENT_SELECT */ "select * from %_content where rowid = ?",
  1793   1803     /* CONTENT_UPDATE */ NULL,  /* generated in contentUpdateStatement() */
  1794   1804     /* CONTENT_DELETE */ "delete from %_content where rowid = ?",
         1805  +  /* CONTENT_EXISTS */ "select rowid from %_content limit 1",
  1795   1806   
  1796   1807     /* BLOCK_INSERT */ "insert into %_segments values (?)",
  1797   1808     /* BLOCK_SELECT */ "select block from %_segments where rowid = ?",
  1798   1809     /* BLOCK_DELETE */ "delete from %_segments where rowid between ? and ?",
         1810  +  /* BLOCK_DELETE_ALL */ "delete from %_segments",
  1799   1811   
  1800   1812     /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?",
  1801   1813     /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)",
  1802         -  /* SEGDIR_SELECT */
         1814  +  /* SEGDIR_SELECT_LEVEL */
  1803   1815     "select start_block, leaves_end_block, root from %_segdir "
  1804   1816     " where level = ? order by idx",
  1805   1817     /* SEGDIR_SPAN */
  1806   1818     "select min(start_block), max(end_block) from %_segdir "
  1807   1819     " where level = ? and start_block <> 0",
  1808   1820     /* SEGDIR_DELETE */ "delete from %_segdir where level = ?",
         1821  +
         1822  +  /* NOTE(shess): The first three results of the following two
         1823  +  ** statements must match.
         1824  +  */
         1825  +  /* SEGDIR_SELECT_SEGMENT */
         1826  +  "select start_block, leaves_end_block, root from %_segdir "
         1827  +  " where level = ? and idx = ?",
  1809   1828     /* SEGDIR_SELECT_ALL */
  1810         -  "select root, leaves_end_block from %_segdir order by level desc, idx",
         1829  +  "select start_block, leaves_end_block, root from %_segdir "
         1830  +  " order by level desc, idx asc",
         1831  +  /* SEGDIR_DELETE_ALL */ "delete from %_segdir",
         1832  +  /* SEGDIR_COUNT */ "select count(*), ifnull(max(level),0) from %_segdir",
  1811   1833   };
  1812   1834   
  1813   1835   /*
  1814   1836   ** A connection to a fulltext index is an instance of the following
  1815   1837   ** structure.  The xCreate and xConnect methods create an instance
  1816   1838   ** of this structure and xDestroy and xDisconnect free that instance.
  1817   1839   ** All other methods receive a pointer to the structure as one of their
................................................................................
  1933   1955         case CONTENT_UPDATE_STMT:
  1934   1956           zStmt = contentUpdateStatement(v); break;
  1935   1957         default:
  1936   1958           zStmt = fulltext_zStatement[iStmt];
  1937   1959       }
  1938   1960       rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt],
  1939   1961                            zStmt);
  1940         -    if( zStmt != fulltext_zStatement[iStmt]) free((void *) zStmt);
         1962  +    if( zStmt != fulltext_zStatement[iStmt]) sqlite3_free((void *) zStmt);
  1941   1963       if( rc!=SQLITE_OK ) return rc;
  1942   1964     } else {
  1943   1965       int rc = sqlite3_reset(v->pFulltextStatements[iStmt]);
  1944   1966       if( rc!=SQLITE_OK ) return rc;
  1945   1967     }
  1946   1968   
  1947   1969     *ppStmt = v->pFulltextStatements[iStmt];
................................................................................
  1954   1976   */
  1955   1977   static int sql_single_step(sqlite3_stmt *s){
  1956   1978     int rc = sqlite3_step(s);
  1957   1979     return (rc==SQLITE_DONE) ? SQLITE_OK : rc;
  1958   1980   }
  1959   1981   
  1960   1982   /* Like sql_get_statement(), but for special replicated LEAF_SELECT
  1961         -** statements.
         1983  +** statements.  idx -1 is a special case for an uncached version of
         1984  +** the statement (used in the optimize implementation).
  1962   1985   */
  1963   1986   /* TODO(shess) Write version for generic statements and then share
  1964   1987   ** that between the cached-statement functions.
  1965   1988   */
  1966   1989   static int sql_get_leaf_statement(fulltext_vtab *v, int idx,
  1967   1990                                     sqlite3_stmt **ppStmt){
  1968         -  assert( idx>=0 && idx<MERGE_COUNT );
  1969         -  if( v->pLeafSelectStmts[idx]==NULL ){
         1991  +  assert( idx>=-1 && idx<MERGE_COUNT );
         1992  +  if( idx==-1 ){
         1993  +    return sql_prepare(v->db, v->zDb, v->zName, ppStmt, LEAF_SELECT);
         1994  +  }else if( v->pLeafSelectStmts[idx]==NULL ){
  1970   1995       int rc = sql_prepare(v->db, v->zDb, v->zName, &v->pLeafSelectStmts[idx],
  1971   1996                            LEAF_SELECT);
  1972   1997       if( rc!=SQLITE_OK ) return rc;
  1973   1998     }else{
  1974   1999       int rc = sqlite3_reset(v->pLeafSelectStmts[idx]);
  1975   2000       if( rc!=SQLITE_OK ) return rc;
  1976   2001     }
................................................................................
  2018   2043     return sql_single_step(s);
  2019   2044   }
  2020   2045   
  2021   2046   static void freeStringArray(int nString, const char **pString){
  2022   2047     int i;
  2023   2048   
  2024   2049     for (i=0 ; i < nString ; ++i) {
  2025         -    if( pString[i]!=NULL ) free((void *) pString[i]);
         2050  +    if( pString[i]!=NULL ) sqlite3_free((void *) pString[i]);
  2026   2051     }
  2027         -  free((void *) pString);
         2052  +  sqlite3_free((void *) pString);
  2028   2053   }
  2029   2054   
  2030   2055   /* select * from %_content where rowid = [iRow]
  2031   2056    * The caller must delete the returned array and all strings in it.
  2032   2057    * null fields will be NULL in the returned array.
  2033   2058    *
  2034   2059    * TODO: Perhaps we should return pointer/length strings here for consistency
................................................................................
  2047   2072   
  2048   2073     rc = sqlite3_bind_int64(s, 1, iRow);
  2049   2074     if( rc!=SQLITE_OK ) return rc;
  2050   2075   
  2051   2076     rc = sqlite3_step(s);
  2052   2077     if( rc!=SQLITE_ROW ) return rc;
  2053   2078   
  2054         -  values = (const char **) malloc(v->nColumn * sizeof(const char *));
         2079  +  values = (const char **) sqlite3_malloc(v->nColumn * sizeof(const char *));
  2055   2080     for(i=0; i<v->nColumn; ++i){
  2056   2081       if( sqlite3_column_type(s, i)==SQLITE_NULL ){
  2057   2082         values[i] = NULL;
  2058   2083       }else{
  2059   2084         values[i] = string_dup((char*)sqlite3_column_text(s, i));
  2060   2085       }
  2061   2086     }
................................................................................
  2079   2104     if( rc!=SQLITE_OK ) return rc;
  2080   2105   
  2081   2106     rc = sqlite3_bind_int64(s, 1, iRow);
  2082   2107     if( rc!=SQLITE_OK ) return rc;
  2083   2108   
  2084   2109     return sql_single_step(s);
  2085   2110   }
         2111  +
         2112  +/* Returns SQLITE_ROW if any rows exist in %_content, SQLITE_DONE if
         2113  +** no rows exist, and any error in case of failure.
         2114  +*/
         2115  +static int content_exists(fulltext_vtab *v){
         2116  +  sqlite3_stmt *s;
         2117  +  int rc = sql_get_statement(v, CONTENT_EXISTS_STMT, &s);
         2118  +  if( rc!=SQLITE_OK ) return rc;
         2119  +
         2120  +  rc = sqlite3_step(s);
         2121  +  if( rc!=SQLITE_ROW ) return rc;
         2122  +
         2123  +  /* We expect only one row.  We must execute another sqlite3_step()
         2124  +   * to complete the iteration; otherwise the table will remain locked. */
         2125  +  rc = sqlite3_step(s);
         2126  +  if( rc==SQLITE_DONE ) return SQLITE_ROW;
         2127  +  if( rc==SQLITE_ROW ) return SQLITE_ERROR;
         2128  +  return rc;
         2129  +}
  2086   2130   
  2087   2131   /* insert into %_segments values ([pData])
  2088   2132   **   returns assigned rowid in *piBlockid
  2089   2133   */
  2090   2134   static int block_insert(fulltext_vtab *v, const char *pData, int nData,
  2091   2135                           sqlite_int64 *piBlockid){
  2092   2136     sqlite3_stmt *s;
................................................................................
  2252   2296     if( rc!=SQLITE_OK ) return rc;
  2253   2297   
  2254   2298     rc = sqlite3_bind_int64(s, 1, iLevel);
  2255   2299     if( rc!=SQLITE_OK ) return rc;
  2256   2300   
  2257   2301     return sql_single_step(s);
  2258   2302   }
         2303  +
         2304  +/* Delete entire fts index, SQLITE_OK on success, relevant error on
         2305  +** failure.
         2306  +*/
         2307  +static int segdir_delete_all(fulltext_vtab *v){
         2308  +  sqlite3_stmt *s;
         2309  +  int rc = sql_get_statement(v, SEGDIR_DELETE_ALL_STMT, &s);
         2310  +  if( rc!=SQLITE_OK ) return rc;
         2311  +
         2312  +  rc = sql_single_step(s);
         2313  +  if( rc!=SQLITE_OK ) return rc;
         2314  +
         2315  +  rc = sql_get_statement(v, BLOCK_DELETE_ALL_STMT, &s);
         2316  +  if( rc!=SQLITE_OK ) return rc;
         2317  +
         2318  +  return sql_single_step(s);
         2319  +}
         2320  +
         2321  +/* Returns SQLITE_OK with *pnSegments set to the number of entries in
         2322  +** %_segdir and *piMaxLevel set to the highest level which has a
         2323  +** segment.  Otherwise returns the SQLite error which caused failure.
         2324  +*/
         2325  +static int segdir_count(fulltext_vtab *v, int *pnSegments, int *piMaxLevel){
         2326  +  sqlite3_stmt *s;
         2327  +  int rc = sql_get_statement(v, SEGDIR_COUNT_STMT, &s);
         2328  +  if( rc!=SQLITE_OK ) return rc;
         2329  +
         2330  +  rc = sqlite3_step(s);
         2331  +  /* TODO(shess): This case should not be possible?  Should stronger
         2332  +  ** measures be taken if it happens?
         2333  +  */
         2334  +  if( rc==SQLITE_DONE ){
         2335  +    *pnSegments = 0;
         2336  +    *piMaxLevel = 0;
         2337  +    return SQLITE_OK;
         2338  +  }
         2339  +  if( rc!=SQLITE_ROW ) return rc;
         2340  +
         2341  +  *pnSegments = sqlite3_column_int(s, 0);
         2342  +  *piMaxLevel = sqlite3_column_int(s, 1);
         2343  +
         2344  +  /* We expect only one row.  We must execute another sqlite3_step()
         2345  +   * to complete the iteration; otherwise the table will remain locked. */
         2346  +  rc = sqlite3_step(s);
         2347  +  if( rc==SQLITE_DONE ) return SQLITE_OK;
         2348  +  if( rc==SQLITE_ROW ) return SQLITE_ERROR;
         2349  +  return rc;
         2350  +}
  2259   2351   
  2260   2352   /* TODO(shess) clearPendingTerms() is far down the file because
  2261   2353   ** writeZeroSegment() is far down the file because LeafWriter is far
  2262   2354   ** down the file.  Consider refactoring the code to move the non-vtab
  2263   2355   ** code above the vtab code so that we don't need this forward
  2264   2356   ** reference.
  2265   2357   */
................................................................................
  2289   2381     if( v->pTokenizer!=NULL ){
  2290   2382       v->pTokenizer->pModule->xDestroy(v->pTokenizer);
  2291   2383       v->pTokenizer = NULL;
  2292   2384     }
  2293   2385   
  2294   2386     clearPendingTerms(v);
  2295   2387   
  2296         -  free(v->azColumn);
         2388  +  sqlite3_free(v->azColumn);
  2297   2389     for(i = 0; i < v->nColumn; ++i) {
  2298   2390       sqlite3_free(v->azContentColumn[i]);
  2299   2391     }
  2300         -  free(v->azContentColumn);
  2301         -  free(v);
         2392  +  sqlite3_free(v->azContentColumn);
         2393  +  sqlite3_free(v);
  2302   2394   }
  2303   2395   
  2304   2396   /*
  2305   2397   ** Token types for parsing the arguments to xConnect or xCreate.
  2306   2398   */
  2307   2399   #define TOKEN_EOF         0    /* End of file */
  2308   2400   #define TOKEN_SPACE       1    /* Any kind of whitespace */
................................................................................
  2405   2497   ** Space to hold the returned array is obtained from a single
  2406   2498   ** malloc and should be freed by passing the return value to free().
  2407   2499   ** The individual strings within the token list are all a part of
  2408   2500   ** the single memory allocation and will all be freed at once.
  2409   2501   */
  2410   2502   static char **tokenizeString(const char *z, int *pnToken){
  2411   2503     int nToken = 0;
  2412         -  Token *aToken = malloc( strlen(z) * sizeof(aToken[0]) );
         2504  +  Token *aToken = sqlite3_malloc( strlen(z) * sizeof(aToken[0]) );
  2413   2505     int n = 1;
  2414   2506     int e, i;
  2415   2507     int totalSize = 0;
  2416   2508     char **azToken;
  2417   2509     char *zCopy;
  2418   2510     while( n>0 ){
  2419   2511       n = getToken(z, &e);
................................................................................
  2421   2513         aToken[nToken].z = z;
  2422   2514         aToken[nToken].n = n;
  2423   2515         nToken++;
  2424   2516         totalSize += n+1;
  2425   2517       }
  2426   2518       z += n;
  2427   2519     }
  2428         -  azToken = (char**)malloc( nToken*sizeof(char*) + totalSize );
         2520  +  azToken = (char**)sqlite3_malloc( nToken*sizeof(char*) + totalSize );
  2429   2521     zCopy = (char*)&azToken[nToken];
  2430   2522     nToken--;
  2431   2523     for(i=0; i<nToken; i++){
  2432   2524       azToken[i] = zCopy;
  2433   2525       n = aToken[i].n;
  2434   2526       memcpy(zCopy, aToken[i].z, n);
  2435   2527       zCopy[n] = 0;
  2436   2528       zCopy += n+1;
  2437   2529     }
  2438   2530     azToken[nToken] = 0;
  2439         -  free(aToken);
         2531  +  sqlite3_free(aToken);
  2440   2532     *pnToken = nToken;
  2441   2533     return azToken;
  2442   2534   }
  2443   2535   
  2444   2536   /*
  2445   2537   ** Convert an SQL-style quoted string into a normal string by removing
  2446   2538   ** the quote characters.  The conversion is done in-place.  If the
................................................................................
  2570   2662     char **azTokenizer;      /* Name of tokenizer and its arguments */
  2571   2663   } TableSpec;
  2572   2664   
  2573   2665   /*
  2574   2666   ** Reclaim all of the memory used by a TableSpec
  2575   2667   */
  2576   2668   static void clearTableSpec(TableSpec *p) {
  2577         -  free(p->azColumn);
  2578         -  free(p->azContentColumn);
  2579         -  free(p->azTokenizer);
         2669  +  sqlite3_free(p->azColumn);
         2670  +  sqlite3_free(p->azContentColumn);
         2671  +  sqlite3_free(p->azTokenizer);
  2580   2672   }
  2581   2673   
  2582   2674   /* Parse a CREATE VIRTUAL TABLE statement, which looks like this:
  2583   2675    *
  2584   2676    * CREATE VIRTUAL TABLE email
  2585   2677    *        USING fts2(subject, body, tokenize mytokenizer(myarg))
  2586   2678    *
................................................................................
  2607   2699     ** The argv[][] array is read-only and transient.  We can write to the
  2608   2700     ** copy in order to modify things and the copy is persistent.
  2609   2701     */
  2610   2702     CLEAR(pSpec);
  2611   2703     for(i=n=0; i<argc; i++){
  2612   2704       n += strlen(argv[i]) + 1;
  2613   2705     }
  2614         -  azArg = malloc( sizeof(char*)*argc + n );
         2706  +  azArg = sqlite3_malloc( sizeof(char*)*argc + n );
  2615   2707     if( azArg==0 ){
  2616   2708       return SQLITE_NOMEM;
  2617   2709     }
  2618   2710     z = (char*)&azArg[argc];
  2619   2711     for(i=0; i<argc; i++){
  2620   2712       azArg[i] = z;
  2621   2713       strcpy(z, argv[i]);
................................................................................
  2652   2744     ** converted to "_".  The cNN prefix guarantees that all column
  2653   2745     ** names are unique.
  2654   2746     **
  2655   2747     ** The AAAA suffix is not strictly necessary.  It is included
  2656   2748     ** for the convenience of people who might examine the generated
  2657   2749     ** %_content table and wonder what the columns are used for.
  2658   2750     */
  2659         -  pSpec->azContentColumn = malloc( pSpec->nColumn * sizeof(char *) );
         2751  +  pSpec->azContentColumn = sqlite3_malloc( pSpec->nColumn * sizeof(char *) );
  2660   2752     if( pSpec->azContentColumn==0 ){
  2661   2753       clearTableSpec(pSpec);
  2662   2754       return SQLITE_NOMEM;
  2663   2755     }
  2664   2756     for(i=0; i<pSpec->nColumn; i++){
  2665   2757       char *p;
  2666   2758       pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]);
................................................................................
  2721   2813     fulltext_vtab *v = 0;
  2722   2814     const sqlite3_tokenizer_module *m = NULL;
  2723   2815     char *schema;
  2724   2816   
  2725   2817     char const *zTok;         /* Name of tokenizer to use for this fts table */
  2726   2818     int nTok;                 /* Length of zTok, including nul terminator */
  2727   2819   
  2728         -  v = (fulltext_vtab *) malloc(sizeof(fulltext_vtab));
         2820  +  v = (fulltext_vtab *) sqlite3_malloc(sizeof(fulltext_vtab));
  2729   2821     if( v==0 ) return SQLITE_NOMEM;
  2730   2822     CLEAR(v);
  2731   2823     /* sqlite will initialize v->base */
  2732   2824     v->db = db;
  2733   2825     v->zDb = spec->zDb;       /* Freed when azColumn is freed */
  2734   2826     v->zName = spec->zName;   /* Freed when azColumn is freed */
  2735   2827     v->nColumn = spec->nColumn;
................................................................................
  2908   3000     fulltext_vtab_destroy((fulltext_vtab *)pVTab);
  2909   3001     return SQLITE_OK;
  2910   3002   }
  2911   3003   
  2912   3004   static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
  2913   3005     fulltext_cursor *c;
  2914   3006   
  2915         -  c = (fulltext_cursor *) calloc(sizeof(fulltext_cursor), 1);
  2916         -  /* sqlite will initialize c->base */
  2917         -  *ppCursor = &c->base;
  2918         -  TRACE(("FTS2 Open %p: %p\n", pVTab, c));
  2919         -
  2920         -  return SQLITE_OK;
         3007  +  c = (fulltext_cursor *) sqlite3_malloc(sizeof(fulltext_cursor));
         3008  +  if( c ){
         3009  +    memset(c, 0, sizeof(fulltext_cursor));
         3010  +    /* sqlite will initialize c->base */
         3011  +    *ppCursor = &c->base;
         3012  +    TRACE(("FTS2 Open %p: %p\n", pVTab, c));
         3013  +    return SQLITE_OK;
         3014  +  }else{
         3015  +    return SQLITE_NOMEM;
         3016  +  }
  2921   3017   }
  2922   3018   
  2923   3019   
  2924   3020   /* Free all of the dynamically allocated memory held by *q
  2925   3021   */
  2926   3022   static void queryClear(Query *q){
  2927   3023     int i;
  2928   3024     for(i = 0; i < q->nTerms; ++i){
  2929         -    free(q->pTerms[i].pTerm);
         3025  +    sqlite3_free(q->pTerms[i].pTerm);
  2930   3026     }
  2931         -  free(q->pTerms);
         3027  +  sqlite3_free(q->pTerms);
  2932   3028     CLEAR(q);
  2933   3029   }
  2934   3030   
  2935   3031   /* Free all of the dynamically allocated memory held by the
  2936   3032   ** Snippet
  2937   3033   */
  2938   3034   static void snippetClear(Snippet *p){
  2939         -  free(p->aMatch);
  2940         -  free(p->zOffset);
  2941         -  free(p->zSnippet);
         3035  +  sqlite3_free(p->aMatch);
         3036  +  sqlite3_free(p->zOffset);
         3037  +  sqlite3_free(p->zSnippet);
  2942   3038     CLEAR(p);
  2943   3039   }
  2944   3040   /*
  2945   3041   ** Append a single entry to the p->aMatch[] log.
  2946   3042   */
  2947   3043   static void snippetAppendMatch(
  2948   3044     Snippet *p,               /* Append the entry to this snippet */
................................................................................
  2949   3045     int iCol, int iTerm,      /* The column and query term */
  2950   3046     int iStart, int nByte     /* Offset and size of the match */
  2951   3047   ){
  2952   3048     int i;
  2953   3049     struct snippetMatch *pMatch;
  2954   3050     if( p->nMatch+1>=p->nAlloc ){
  2955   3051       p->nAlloc = p->nAlloc*2 + 10;
  2956         -    p->aMatch = realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) );
         3052  +    p->aMatch = sqlite3_realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) );
  2957   3053       if( p->aMatch==0 ){
  2958   3054         p->nMatch = 0;
  2959   3055         p->nAlloc = 0;
  2960   3056         return;
  2961   3057       }
  2962   3058     }
  2963   3059     i = p->nMatch++;
................................................................................
  3090   3186     StringBuffer sb;
  3091   3187     char zBuf[200];
  3092   3188     if( p->zOffset ) return;
  3093   3189     initStringBuffer(&sb);
  3094   3190     for(i=0; i<p->nMatch; i++){
  3095   3191       struct snippetMatch *pMatch = &p->aMatch[i];
  3096   3192       zBuf[0] = ' ';
  3097         -    sprintf(&zBuf[cnt>0], "%d %d %d %d", pMatch->iCol,
  3098         -        pMatch->iTerm, pMatch->iStart, pMatch->nByte);
         3193  +    sqlite3_snprintf(sizeof(zBuf)-1, &zBuf[cnt>0], "%d %d %d %d",
         3194  +        pMatch->iCol, pMatch->iTerm, pMatch->iStart, pMatch->nByte);
  3099   3195       append(&sb, zBuf);
  3100   3196       cnt++;
  3101   3197     }
  3102   3198     p->zOffset = stringBufferData(&sb);
  3103   3199     p->nOffset = stringBufferLength(&sb);
  3104   3200   }
  3105   3201   
................................................................................
  3176   3272     int nDoc;
  3177   3273     const char *zDoc;
  3178   3274     int iStart, iEnd;
  3179   3275     int tailEllipsis = 0;
  3180   3276     int iMatch;
  3181   3277     
  3182   3278   
  3183         -  free(pCursor->snippet.zSnippet);
         3279  +  sqlite3_free(pCursor->snippet.zSnippet);
  3184   3280     pCursor->snippet.zSnippet = 0;
  3185   3281     aMatch = pCursor->snippet.aMatch;
  3186   3282     nMatch = pCursor->snippet.nMatch;
  3187   3283     initStringBuffer(&sb);
  3188   3284   
  3189   3285     for(i=0; i<nMatch; i++){
  3190   3286       aMatch[i].snStatus = SNIPPET_IGNORE;
................................................................................
  3278   3374     fulltext_cursor *c = (fulltext_cursor *) pCursor;
  3279   3375     TRACE(("FTS2 Close %p\n", c));
  3280   3376     sqlite3_finalize(c->pStmt);
  3281   3377     queryClear(&c->q);
  3282   3378     snippetClear(&c->snippet);
  3283   3379     if( c->result.nData!=0 ) dlrDestroy(&c->reader);
  3284   3380     dataBufferDestroy(&c->result);
  3285         -  free(c);
         3381  +  sqlite3_free(c);
  3286   3382     return SQLITE_OK;
  3287   3383   }
  3288   3384   
  3289   3385   static int fulltextNext(sqlite3_vtab_cursor *pCursor){
  3290   3386     fulltext_cursor *c = (fulltext_cursor *) pCursor;
  3291   3387     int rc;
  3292   3388   
................................................................................
  3383   3479   }
  3384   3480   
  3385   3481   /* Add a new term pTerm[0..nTerm-1] to the query *q.
  3386   3482   */
  3387   3483   static void queryAdd(Query *q, const char *pTerm, int nTerm){
  3388   3484     QueryTerm *t;
  3389   3485     ++q->nTerms;
  3390         -  q->pTerms = realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0]));
         3486  +  q->pTerms = sqlite3_realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0]));
  3391   3487     if( q->pTerms==0 ){
  3392   3488       q->nTerms = 0;
  3393   3489       return;
  3394   3490     }
  3395   3491     t = &q->pTerms[q->nTerms - 1];
  3396   3492     CLEAR(t);
  3397         -  t->pTerm = malloc(nTerm+1);
         3493  +  t->pTerm = sqlite3_malloc(nTerm+1);
  3398   3494     memcpy(t->pTerm, pTerm, nTerm);
  3399   3495     t->pTerm[nTerm] = 0;
  3400   3496     t->nTerm = nTerm;
  3401   3497     t->isOr = q->nextIsOr;
  3402   3498     t->isPrefix = 0;
  3403   3499     q->nextIsOr = 0;
  3404   3500     t->iColumn = q->nextColumn;
................................................................................
  3675   3771     sqlite3_vtab_cursor *pCursor,     /* The cursor used for this query */
  3676   3772     int idxNum, const char *idxStr,   /* Which indexing scheme to use */
  3677   3773     int argc, sqlite3_value **argv    /* Arguments for the indexing scheme */
  3678   3774   ){
  3679   3775     fulltext_cursor *c = (fulltext_cursor *) pCursor;
  3680   3776     fulltext_vtab *v = cursor_vtab(c);
  3681   3777     int rc;
  3682         -  char *zSql;
  3683   3778   
  3684   3779     TRACE(("FTS2 Filter %p\n",pCursor));
  3685   3780   
  3686         -  zSql = sqlite3_mprintf("select rowid, * from %%_content %s",
  3687         -                          idxNum==QUERY_GENERIC ? "" : "where rowid=?");
  3688         -  sqlite3_finalize(c->pStmt);
  3689         -  rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql);
  3690         -  sqlite3_free(zSql);
  3691         -  if( rc!=SQLITE_OK ) return rc;
         3781  +  /* If the cursor has a statement that was not prepared according to
         3782  +  ** idxNum, clear it.  I believe all calls to fulltextFilter with a
         3783  +  ** given cursor will have the same idxNum , but in this case it's
         3784  +  ** easy to be safe.
         3785  +  */
         3786  +  if( c->pStmt && c->iCursorType!=idxNum ){
         3787  +    sqlite3_finalize(c->pStmt);
         3788  +    c->pStmt = NULL;
         3789  +  }
  3692   3790   
  3693         -  c->iCursorType = idxNum;
         3791  +  /* Get a fresh statement appropriate to idxNum. */
         3792  +  /* TODO(shess): Add a prepared-statement cache in the vt structure.
         3793  +  ** The cache must handle multiple open cursors.  Easier to cache the
         3794  +  ** statement variants at the vt to reduce malloc/realloc/free here.
         3795  +  ** Or we could have a StringBuffer variant which allowed stack
         3796  +  ** construction for small values.
         3797  +  */
         3798  +  if( !c->pStmt ){
         3799  +    char *zSql = sqlite3_mprintf("select rowid, * from %%_content %s",
         3800  +                                 idxNum==QUERY_GENERIC ? "" : "where rowid=?");
         3801  +    rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql);
         3802  +    sqlite3_free(zSql);
         3803  +    if( rc!=SQLITE_OK ) return rc;
         3804  +    c->iCursorType = idxNum;
         3805  +  }else{
         3806  +    sqlite3_reset(c->pStmt);
         3807  +    assert( c->iCursorType==idxNum );
         3808  +  }
         3809  +
  3694   3810     switch( idxNum ){
  3695   3811       case QUERY_GENERIC:
  3696   3812         break;
  3697   3813   
  3698   3814       case QUERY_ROWID:
  3699   3815         rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0]));
  3700   3816         if( rc!=SQLITE_OK ) return rc;
................................................................................
  3781   3897     int iStartOffset, iEndOffset, iPosition;
  3782   3898     int rc;
  3783   3899   
  3784   3900     rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor);
  3785   3901     if( rc!=SQLITE_OK ) return rc;
  3786   3902   
  3787   3903     pCursor->pTokenizer = pTokenizer;
  3788         -  while( SQLITE_OK==pTokenizer->pModule->xNext(pCursor,
  3789         -                                               &pToken, &nTokenBytes,
  3790         -                                               &iStartOffset, &iEndOffset,
  3791         -                                               &iPosition) ){
         3904  +  while( SQLITE_OK==(rc=pTokenizer->pModule->xNext(pCursor,
         3905  +                                                   &pToken, &nTokenBytes,
         3906  +                                                   &iStartOffset, &iEndOffset,
         3907  +                                                   &iPosition)) ){
  3792   3908       DLCollector *p;
  3793   3909       int nData;                   /* Size of doclist before our update. */
  3794   3910   
  3795         -    /* Positions can't be negative; we use -1 as a terminator internally. */
  3796         -    if( iPosition<0 ){
  3797         -      pTokenizer->pModule->xClose(pCursor);
  3798         -      return SQLITE_ERROR;
         3911  +    /* Positions can't be negative; we use -1 as a terminator
         3912  +     * internally.  Token can't be NULL or empty. */
         3913  +    if( iPosition<0 || pToken == NULL || nTokenBytes == 0 ){
         3914  +      rc = SQLITE_ERROR;
         3915  +      break;
  3799   3916       }
  3800   3917   
  3801   3918       p = fts2HashFind(&v->pendingTerms, pToken, nTokenBytes);
  3802   3919       if( p==NULL ){
  3803   3920         nData = 0;
  3804   3921         p = dlcNew(iDocid, DL_DEFAULT);
  3805   3922         fts2HashInsert(&v->pendingTerms, pToken, nTokenBytes, p);
................................................................................
  3820   3937   
  3821   3938     /* TODO(shess) Check return?  Should this be able to cause errors at
  3822   3939     ** this point?  Actually, same question about sqlite3_finalize(),
  3823   3940     ** though one could argue that failure there means that the data is
  3824   3941     ** not durable.  *ponder*
  3825   3942     */
  3826   3943     pTokenizer->pModule->xClose(pCursor);
         3944  +  if( SQLITE_DONE == rc ) return SQLITE_OK;
  3827   3945     return rc;
  3828   3946   }
  3829   3947   
  3830   3948   /* Add doclists for all terms in [pValues] to pendingTerms table. */
  3831   3949   static int insertTerms(fulltext_vtab *v, sqlite_int64 iRowid,
  3832   3950                          sqlite3_value **pValues){
  3833   3951     int i;
................................................................................
  3954   4072     DataBuffer term;           /* Leftmost term in block's subtree. */
  3955   4073     DataBuffer data;           /* Accumulated data for the block. */
  3956   4074     struct InteriorBlock *next;
  3957   4075   } InteriorBlock;
  3958   4076   
  3959   4077   static InteriorBlock *interiorBlockNew(int iHeight, sqlite_int64 iChildBlock,
  3960   4078                                          const char *pTerm, int nTerm){
  3961         -  InteriorBlock *block = calloc(1, sizeof(InteriorBlock));
         4079  +  InteriorBlock *block = sqlite3_malloc(sizeof(InteriorBlock));
  3962   4080     char c[VARINT_MAX+VARINT_MAX];
  3963   4081     int n;
  3964   4082   
  3965         -  dataBufferInit(&block->term, 0);
  3966         -  dataBufferReplace(&block->term, pTerm, nTerm);
         4083  +  if( block ){
         4084  +    memset(block, 0, sizeof(*block));
         4085  +    dataBufferInit(&block->term, 0);
         4086  +    dataBufferReplace(&block->term, pTerm, nTerm);
  3967   4087   
  3968         -  n = putVarint(c, iHeight);
  3969         -  n += putVarint(c+n, iChildBlock);
  3970         -  dataBufferInit(&block->data, INTERIOR_MAX);
  3971         -  dataBufferReplace(&block->data, c, n);
  3972         -
         4088  +    n = putVarint(c, iHeight);
         4089  +    n += putVarint(c+n, iChildBlock);
         4090  +    dataBufferInit(&block->data, INTERIOR_MAX);
         4091  +    dataBufferReplace(&block->data, c, n);
         4092  +  }
  3973   4093     return block;
  3974   4094   }
  3975   4095   
  3976   4096   #ifndef NDEBUG
  3977   4097   /* Verify that the data is readable as an interior node. */
  3978   4098   static void interiorBlockValidate(InteriorBlock *pBlock){
  3979   4099     const char *pData = pBlock->data.pData;
................................................................................
  4130   4250     InteriorBlock *block = pWriter->first;
  4131   4251   
  4132   4252     while( block!=NULL ){
  4133   4253       InteriorBlock *b = block;
  4134   4254       block = block->next;
  4135   4255       dataBufferDestroy(&b->term);
  4136   4256       dataBufferDestroy(&b->data);
  4137         -    free(b);
         4257  +    sqlite3_free(b);
  4138   4258     }
  4139   4259     if( pWriter->parentWriter!=NULL ){
  4140   4260       interiorWriterDestroy(pWriter->parentWriter);
  4141         -    free(pWriter->parentWriter);
         4261  +    sqlite3_free(pWriter->parentWriter);
  4142   4262     }
  4143   4263     dataBufferDestroy(&pWriter->term);
  4144   4264     SCRAMBLE(pWriter);
  4145   4265     return SQLITE_OK;
  4146   4266   }
  4147   4267   
  4148   4268   /* If pWriter can fit entirely in ROOT_MAX, return it as the root info
................................................................................
  4168   4288     ** interior node.
  4169   4289     */
  4170   4290     ASSERT_VALID_INTERIOR_BLOCK(block);
  4171   4291     rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid);
  4172   4292     if( rc!=SQLITE_OK ) return rc;
  4173   4293     *piEndBlockid = iBlockid;
  4174   4294   
  4175         -  pWriter->parentWriter = malloc(sizeof(*pWriter->parentWriter));
         4295  +  pWriter->parentWriter = sqlite3_malloc(sizeof(*pWriter->parentWriter));
  4176   4296     interiorWriterInit(pWriter->iHeight+1,
  4177   4297                        block->term.pData, block->term.nData,
  4178   4298                        iBlockid, pWriter->parentWriter);
  4179   4299   
  4180   4300     /* Flush additional blocks and append to the higher interior
  4181   4301     ** node.
  4182   4302     */
................................................................................
  4936   5056   ** this case.  Probably a brittle assumption.
  4937   5057   */
  4938   5058   static int leavesReaderReset(LeavesReader *pReader){
  4939   5059     return sqlite3_reset(pReader->pStmt);
  4940   5060   }
  4941   5061   
  4942   5062   static void leavesReaderDestroy(LeavesReader *pReader){
         5063  +  /* If idx is -1, that means we're using a non-cached statement
         5064  +  ** handle in the optimize() case, so we need to release it.
         5065  +  */
         5066  +  if( pReader->pStmt!=NULL && pReader->idx==-1 ){
         5067  +    sqlite3_finalize(pReader->pStmt);
         5068  +  }
  4943   5069     leafReaderDestroy(&pReader->leafReader);
  4944   5070     dataBufferDestroy(&pReader->rootData);
  4945   5071     SCRAMBLE(pReader);
  4946   5072   }
  4947   5073   
  4948   5074   /* Initialize pReader with the given root data (if iStartBlockid==0
  4949   5075   ** the leaf data was entirely contained in the root), or from the
................................................................................
  5056   5182   /* Initializes pReaders with the segments from level iLevel, returning
  5057   5183   ** the number of segments in *piReaders.  Leaves pReaders in sorted
  5058   5184   ** order.
  5059   5185   */
  5060   5186   static int leavesReadersInit(fulltext_vtab *v, int iLevel,
  5061   5187                                LeavesReader *pReaders, int *piReaders){
  5062   5188     sqlite3_stmt *s;
  5063         -  int i, rc = sql_get_statement(v, SEGDIR_SELECT_STMT, &s);
         5189  +  int i, rc = sql_get_statement(v, SEGDIR_SELECT_LEVEL_STMT, &s);
  5064   5190     if( rc!=SQLITE_OK ) return rc;
  5065   5191   
  5066   5192     rc = sqlite3_bind_int(s, 1, iLevel);
  5067   5193     if( rc!=SQLITE_OK ) return rc;
  5068   5194   
  5069   5195     i = 0;
  5070   5196     while( (rc = sqlite3_step(s))==SQLITE_ROW ){
................................................................................
  5207   5333    err:
  5208   5334     for(i=0; i<MERGE_COUNT; i++){
  5209   5335       leavesReaderDestroy(&lrs[i]);
  5210   5336     }
  5211   5337     leafWriterDestroy(&writer);
  5212   5338     return rc;
  5213   5339   }
         5340  +
         5341  +/* Accumulate the union of *acc and *pData into *acc. */
         5342  +static void docListAccumulateUnion(DataBuffer *acc,
         5343  +                                   const char *pData, int nData) {
         5344  +  DataBuffer tmp = *acc;
         5345  +  dataBufferInit(acc, tmp.nData+nData);
         5346  +  docListUnion(tmp.pData, tmp.nData, pData, nData, acc);
         5347  +  dataBufferDestroy(&tmp);
         5348  +}
         5349  +
         5350  +/* TODO(shess) It might be interesting to explore different merge
         5351  +** strategies, here.  For instance, since this is a sorted merge, we
         5352  +** could easily merge many doclists in parallel.  With some
         5353  +** comprehension of the storage format, we could merge all of the
         5354  +** doclists within a leaf node directly from the leaf node's storage.
         5355  +** It may be worthwhile to merge smaller doclists before larger
         5356  +** doclists, since they can be traversed more quickly - but the
         5357  +** results may have less overlap, making them more expensive in a
         5358  +** different way.
         5359  +*/
  5214   5360   
  5215   5361   /* Scan pReader for pTerm/nTerm, and merge the term's doclist over
  5216   5362   ** *out (any doclists with duplicate docids overwrite those in *out).
  5217   5363   ** Internal function for loadSegmentLeaf().
  5218   5364   */
  5219   5365   static int loadSegmentLeavesInt(fulltext_vtab *v, LeavesReader *pReader,
  5220   5366                                   const char *pTerm, int nTerm, int isPrefix,
  5221   5367                                   DataBuffer *out){
         5368  +  /* doclist data is accumulated into pBuffers similar to how one does
         5369  +  ** increment in binary arithmetic.  If index 0 is empty, the data is
         5370  +  ** stored there.  If there is data there, it is merged and the
         5371  +  ** results carried into position 1, with further merge-and-carry
         5372  +  ** until an empty position is found.
         5373  +  */
         5374  +  DataBuffer *pBuffers = NULL;
         5375  +  int nBuffers = 0, nMaxBuffers = 0, rc;
         5376  +
  5222   5377     assert( nTerm>0 );
  5223   5378   
  5224         -  /* Process while the prefix matches. */
  5225         -  while( !leavesReaderAtEnd(pReader) ){
         5379  +  for(rc=SQLITE_OK; rc==SQLITE_OK && !leavesReaderAtEnd(pReader);
         5380  +      rc=leavesReaderStep(v, pReader)){
  5226   5381       /* TODO(shess) Really want leavesReaderTermCmp(), but that name is
  5227   5382       ** already taken to compare the terms of two LeavesReaders.  Think
  5228   5383       ** on a better name.  [Meanwhile, break encapsulation rather than
  5229   5384       ** use a confusing name.]
  5230   5385       */
  5231         -    int rc;
  5232   5386       int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix);
         5387  +    if( c>0 ) break;      /* Past any possible matches. */
  5233   5388       if( c==0 ){
  5234   5389         const char *pData = leavesReaderData(pReader);
  5235         -      int nData = leavesReaderDataBytes(pReader);
  5236         -      if( out->nData==0 ){
  5237         -        dataBufferReplace(out, pData, nData);
         5390  +      int iBuffer, nData = leavesReaderDataBytes(pReader);
         5391  +
         5392  +      /* Find the first empty buffer. */
         5393  +      for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){
         5394  +        if( 0==pBuffers[iBuffer].nData ) break;
         5395  +      }
         5396  +
         5397  +      /* Out of buffers, add an empty one. */
         5398  +      if( iBuffer==nBuffers ){
         5399  +        if( nBuffers==nMaxBuffers ){
         5400  +          DataBuffer *p;
         5401  +          nMaxBuffers += 20;
         5402  +
         5403  +          /* Manual realloc so we can handle NULL appropriately. */
         5404  +          p = sqlite3_malloc(nMaxBuffers*sizeof(*pBuffers));
         5405  +          if( p==NULL ){
         5406  +            rc = SQLITE_NOMEM;
         5407  +            break;
         5408  +          }
         5409  +
         5410  +          if( nBuffers>0 ){
         5411  +            assert(pBuffers!=NULL);
         5412  +            memcpy(p, pBuffers, nBuffers*sizeof(*pBuffers));
         5413  +            sqlite3_free(pBuffers);
         5414  +          }
         5415  +          pBuffers = p;
         5416  +        }
         5417  +        dataBufferInit(&(pBuffers[nBuffers]), 0);
         5418  +        nBuffers++;
         5419  +      }
         5420  +
         5421  +      /* At this point, must have an empty at iBuffer. */
         5422  +      assert(iBuffer<nBuffers && pBuffers[iBuffer].nData==0);
         5423  +
         5424  +      /* If empty was first buffer, no need for merge logic. */
         5425  +      if( iBuffer==0 ){
         5426  +        dataBufferReplace(&(pBuffers[0]), pData, nData);
  5238   5427         }else{
  5239         -        DataBuffer result;
  5240         -        dataBufferInit(&result, out->nData+nData);
  5241         -        docListUnion(out->pData, out->nData, pData, nData, &result);
  5242         -        dataBufferDestroy(out);
  5243         -        *out = result;
  5244         -        /* TODO(shess) Rather than destroy out, we could retain it for
  5245         -        ** later reuse.
         5428  +        /* pAcc is the empty buffer the merged data will end up in. */
         5429  +        DataBuffer *pAcc = &(pBuffers[iBuffer]);
         5430  +        DataBuffer *p = &(pBuffers[0]);
         5431  +
         5432  +        /* Handle position 0 specially to avoid need to prime pAcc
         5433  +        ** with pData/nData.
  5246   5434           */
         5435  +        dataBufferSwap(p, pAcc);
         5436  +        docListAccumulateUnion(pAcc, pData, nData);
         5437  +
         5438  +        /* Accumulate remaining doclists into pAcc. */
         5439  +        for(++p; p<pAcc; ++p){
         5440  +          docListAccumulateUnion(pAcc, p->pData, p->nData);
         5441  +
         5442  +          /* dataBufferReset() could allow a large doclist to blow up
         5443  +          ** our memory requirements.
         5444  +          */
         5445  +          if( p->nCapacity<1024 ){
         5446  +            dataBufferReset(p);
         5447  +          }else{
         5448  +            dataBufferDestroy(p);
         5449  +            dataBufferInit(p, 0);
         5450  +          }
         5451  +        }
         5452  +      }
         5453  +    }
         5454  +  }
         5455  +
         5456  +  /* Union all the doclists together into *out. */
         5457  +  /* TODO(shess) What if *out is big?  Sigh. */
         5458  +  if( rc==SQLITE_OK && nBuffers>0 ){
         5459  +    int iBuffer;
         5460  +    for(iBuffer=0; iBuffer<nBuffers; ++iBuffer){
         5461  +      if( pBuffers[iBuffer].nData>0 ){
         5462  +        if( out->nData==0 ){
         5463  +          dataBufferSwap(out, &(pBuffers[iBuffer]));
         5464  +        }else{
         5465  +          docListAccumulateUnion(out, pBuffers[iBuffer].pData,
         5466  +                                 pBuffers[iBuffer].nData);
         5467  +        }
  5247   5468         }
  5248   5469       }
  5249         -    if( c>0 ) break;      /* Past any possible matches. */
         5470  +  }
  5250   5471   
  5251         -    rc = leavesReaderStep(v, pReader);
  5252         -    if( rc!=SQLITE_OK ) return rc;
         5472  +  while( nBuffers-- ){
         5473  +    dataBufferDestroy(&(pBuffers[nBuffers]));
  5253   5474     }
  5254         -  return SQLITE_OK;
         5475  +  if( pBuffers!=NULL ) sqlite3_free(pBuffers);
         5476  +
         5477  +  return rc;
  5255   5478   }
  5256   5479   
  5257   5480   /* Call loadSegmentLeavesInt() with pData/nData as input. */
  5258   5481   static int loadSegmentLeaf(fulltext_vtab *v, const char *pData, int nData,
  5259   5482                              const char *pTerm, int nTerm, int isPrefix,
  5260   5483                              DataBuffer *out){
  5261   5484     LeavesReader reader;
................................................................................
  5298   5521   ** interior node terms logically come between the blocks, so there is
  5299   5522   ** one more blockid than there are terms (that block contains terms >=
  5300   5523   ** the last interior-node term).
  5301   5524   */
  5302   5525   /* TODO(shess) The calling code may already know that the end child is
  5303   5526   ** not worth calculating, because the end may be in a later sibling
  5304   5527   ** node.  Consider whether breaking symmetry is worthwhile.  I suspect
  5305         -** it's not worthwhile.
         5528  +** it is not worthwhile.
  5306   5529   */
  5307   5530   static void getChildrenContaining(const char *pData, int nData,
  5308   5531                                     const char *pTerm, int nTerm, int isPrefix,
  5309   5532                                     sqlite_int64 *piStartChild,
  5310   5533                                     sqlite_int64 *piEndChild){
  5311   5534     InteriorReader reader;
  5312   5535   
................................................................................
  5497   5720   
  5498   5721     dataBufferInit(&doclist, 0);
  5499   5722   
  5500   5723     /* Traverse the segments from oldest to newest so that newer doclist
  5501   5724     ** elements for given docids overwrite older elements.
  5502   5725     */
  5503   5726     while( (rc = sqlite3_step(s))==SQLITE_ROW ){
  5504         -    const char *pData = sqlite3_column_blob(s, 0);
  5505         -    const int nData = sqlite3_column_bytes(s, 0);
         5727  +    const char *pData = sqlite3_column_blob(s, 2);
         5728  +    const int nData = sqlite3_column_bytes(s, 2);
  5506   5729       const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1);
  5507   5730       rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix,
  5508   5731                        &doclist);
  5509   5732       if( rc!=SQLITE_OK ) goto err;
  5510   5733     }
  5511   5734     if( rc==SQLITE_DONE ){
  5512   5735       if( doclist.nData!=0 ){
................................................................................
  5558   5781     DataBuffer dl;
  5559   5782   
  5560   5783     /* Determine the next index at level 0, merging as necessary. */
  5561   5784     rc = segdirNextIndex(v, 0, &idx);
  5562   5785     if( rc!=SQLITE_OK ) return rc;
  5563   5786   
  5564   5787     n = fts2HashCount(pTerms);
  5565         -  pData = malloc(n*sizeof(TermData));
         5788  +  pData = sqlite3_malloc(n*sizeof(TermData));
  5566   5789   
  5567   5790     for(i = 0, e = fts2HashFirst(pTerms); e; i++, e = fts2HashNext(e)){
  5568   5791       assert( i<n );
  5569   5792       pData[i].pTerm = fts2HashKey(e);
  5570   5793       pData[i].nTerm = fts2HashKeysize(e);
  5571   5794       pData[i].pCollector = fts2HashData(e);
  5572   5795     }
................................................................................
  5589   5812                           pData[i].pTerm, pData[i].nTerm, dl.pData, dl.nData);
  5590   5813       if( rc!=SQLITE_OK ) goto err;
  5591   5814     }
  5592   5815     rc = leafWriterFinalize(v, &writer);
  5593   5816   
  5594   5817    err:
  5595   5818     dataBufferDestroy(&dl);
  5596         -  free(pData);
         5819  +  sqlite3_free(pData);
  5597   5820     leafWriterDestroy(&writer);
  5598   5821     return rc;
  5599   5822   }
  5600   5823   
  5601   5824   /* If pendingTerms has data, free it. */
  5602   5825   static int clearPendingTerms(fulltext_vtab *v){
  5603   5826     if( v->nPendingData>=0 ){
................................................................................
  5641   5864       fts2HashInit(&v->pendingTerms, FTS2_HASH_STRING, 1);
  5642   5865       v->nPendingData = 0;
  5643   5866     }
  5644   5867     v->iPrevDocid = iDocid;
  5645   5868     return SQLITE_OK;
  5646   5869   }
  5647   5870   
  5648         -/* This function implements the xUpdate callback; it's the top-level entry
         5871  +/* This function implements the xUpdate callback; it is the top-level entry
  5649   5872    * point for inserting, deleting or updating a row in a full-text table. */
  5650   5873   static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg,
  5651   5874                      sqlite_int64 *pRowid){
  5652   5875     fulltext_vtab *v = (fulltext_vtab *) pVtab;
  5653   5876     int rc;
  5654   5877   
  5655   5878     TRACE(("FTS2 Update %p\n", pVtab));
  5656   5879   
  5657   5880     if( nArg<2 ){
  5658   5881       rc = index_delete(v, sqlite3_value_int64(ppArg[0]));
         5882  +    if( rc==SQLITE_OK ){
         5883  +      /* If we just deleted the last row in the table, clear out the
         5884  +      ** index data.
         5885  +      */
         5886  +      rc = content_exists(v);
         5887  +      if( rc==SQLITE_ROW ){
         5888  +        rc = SQLITE_OK;
         5889  +      }else if( rc==SQLITE_DONE ){
         5890  +        /* Clear the pending terms so we don't flush a useless level-0
         5891  +        ** segment when the transaction closes.
         5892  +        */
         5893  +        rc = clearPendingTerms(v);
         5894  +        if( rc==SQLITE_OK ){
         5895  +          rc = segdir_delete_all(v);
         5896  +        }
         5897  +      }
         5898  +    }
  5659   5899     } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){
  5660   5900       /* An update:
  5661   5901        * ppArg[0] = old rowid
  5662   5902        * ppArg[1] = new rowid
  5663   5903        * ppArg[2..2+v->nColumn-1] = values
  5664   5904        * ppArg[2+v->nColumn] = value for magic column (we ignore this)
  5665   5905        */
................................................................................
  5767   6007       snippetOffsetText(&pCursor->snippet);
  5768   6008       sqlite3_result_text(pContext,
  5769   6009                           pCursor->snippet.zOffset, pCursor->snippet.nOffset,
  5770   6010                           SQLITE_STATIC);
  5771   6011     }
  5772   6012   }
  5773   6013   
         6014  +/* OptLeavesReader is nearly identical to LeavesReader, except that
         6015  +** where LeavesReader is geared towards the merging of complete
         6016  +** segment levels (with exactly MERGE_COUNT segments), OptLeavesReader
         6017  +** is geared towards implementation of the optimize() function, and
         6018  +** can merge all segments simultaneously.  This version may be
         6019  +** somewhat less efficient than LeavesReader because it merges into an
         6020  +** accumulator rather than doing an N-way merge, but since segment
         6021  +** size grows exponentially (so segment count logrithmically) this is
         6022  +** probably not an immediate problem.
         6023  +*/
         6024  +/* TODO(shess): Prove that assertion, or extend the merge code to
         6025  +** merge tree fashion (like the prefix-searching code does).
         6026  +*/
         6027  +/* TODO(shess): OptLeavesReader and LeavesReader could probably be
         6028  +** merged with little or no loss of performance for LeavesReader.  The
         6029  +** merged code would need to handle >MERGE_COUNT segments, and would
         6030  +** also need to be able to optionally optimize away deletes.
         6031  +*/
         6032  +typedef struct OptLeavesReader {
         6033  +  /* Segment number, to order readers by age. */
         6034  +  int segment;
         6035  +  LeavesReader reader;
         6036  +} OptLeavesReader;
         6037  +
         6038  +static int optLeavesReaderAtEnd(OptLeavesReader *pReader){
         6039  +  return leavesReaderAtEnd(&pReader->reader);
         6040  +}
         6041  +static int optLeavesReaderTermBytes(OptLeavesReader *pReader){
         6042  +  return leavesReaderTermBytes(&pReader->reader);
         6043  +}
         6044  +static const char *optLeavesReaderData(OptLeavesReader *pReader){
         6045  +  return leavesReaderData(&pReader->reader);
         6046  +}
         6047  +static int optLeavesReaderDataBytes(OptLeavesReader *pReader){
         6048  +  return leavesReaderDataBytes(&pReader->reader);
         6049  +}
         6050  +static const char *optLeavesReaderTerm(OptLeavesReader *pReader){
         6051  +  return leavesReaderTerm(&pReader->reader);
         6052  +}
         6053  +static int optLeavesReaderStep(fulltext_vtab *v, OptLeavesReader *pReader){
         6054  +  return leavesReaderStep(v, &pReader->reader);
         6055  +}
         6056  +static int optLeavesReaderTermCmp(OptLeavesReader *lr1, OptLeavesReader *lr2){
         6057  +  return leavesReaderTermCmp(&lr1->reader, &lr2->reader);
         6058  +}
         6059  +/* Order by term ascending, segment ascending (oldest to newest), with
         6060  +** exhausted readers to the end.
         6061  +*/
         6062  +static int optLeavesReaderCmp(OptLeavesReader *lr1, OptLeavesReader *lr2){
         6063  +  int c = optLeavesReaderTermCmp(lr1, lr2);
         6064  +  if( c!=0 ) return c;
         6065  +  return lr1->segment-lr2->segment;
         6066  +}
         6067  +/* Bubble pLr[0] to appropriate place in pLr[1..nLr-1].  Assumes that
         6068  +** pLr[1..nLr-1] is already sorted.
         6069  +*/
         6070  +static void optLeavesReaderReorder(OptLeavesReader *pLr, int nLr){
         6071  +  while( nLr>1 && optLeavesReaderCmp(pLr, pLr+1)>0 ){
         6072  +    OptLeavesReader tmp = pLr[0];
         6073  +    pLr[0] = pLr[1];
         6074  +    pLr[1] = tmp;
         6075  +    nLr--;
         6076  +    pLr++;
         6077  +  }
         6078  +}
         6079  +
         6080  +/* optimize() helper function.  Put the readers in order and iterate
         6081  +** through them, merging doclists for matching terms into pWriter.
         6082  +** Returns SQLITE_OK on success, or the SQLite error code which
         6083  +** prevented success.
         6084  +*/
         6085  +static int optimizeInternal(fulltext_vtab *v,
         6086  +                            OptLeavesReader *readers, int nReaders,
         6087  +                            LeafWriter *pWriter){
         6088  +  int i, rc = SQLITE_OK;
         6089  +  DataBuffer doclist, merged, tmp;
         6090  +
         6091  +  /* Order the readers. */
         6092  +  i = nReaders;
         6093  +  while( i-- > 0 ){
         6094  +    optLeavesReaderReorder(&readers[i], nReaders-i);
         6095  +  }
         6096  +
         6097  +  dataBufferInit(&doclist, LEAF_MAX);
         6098  +  dataBufferInit(&merged, LEAF_MAX);
         6099  +
         6100  +  /* Exhausted readers bubble to the end, so when the first reader is
         6101  +  ** at eof, all are at eof.
         6102  +  */
         6103  +  while( !optLeavesReaderAtEnd(&readers[0]) ){
         6104  +
         6105  +    /* Figure out how many readers share the next term. */
         6106  +    for(i=1; i<nReaders && !optLeavesReaderAtEnd(&readers[i]); i++){
         6107  +      if( 0!=optLeavesReaderTermCmp(&readers[0], &readers[i]) ) break;
         6108  +    }
         6109  +
         6110  +    /* Special-case for no merge. */
         6111  +    if( i==1 ){
         6112  +      /* Trim deletions from the doclist. */
         6113  +      dataBufferReset(&merged);
         6114  +      docListTrim(DL_DEFAULT,
         6115  +                  optLeavesReaderData(&readers[0]),
         6116  +                  optLeavesReaderDataBytes(&readers[0]),
         6117  +                  -1, DL_DEFAULT, &merged);
         6118  +    }else{
         6119  +      DLReader dlReaders[MERGE_COUNT];
         6120  +      int iReader, nReaders;
         6121  +
         6122  +      /* Prime the pipeline with the first reader's doclist.  After
         6123  +      ** one pass index 0 will reference the accumulated doclist.
         6124  +      */
         6125  +      dlrInit(&dlReaders[0], DL_DEFAULT,
         6126  +              optLeavesReaderData(&readers[0]),
         6127  +              optLeavesReaderDataBytes(&readers[0]));
         6128  +      iReader = 1;
         6129  +
         6130  +      assert( iReader<i );  /* Must execute the loop at least once. */
         6131  +      while( iReader<i ){
         6132  +        /* Merge 16 inputs per pass. */
         6133  +        for( nReaders=1; iReader<i && nReaders<MERGE_COUNT;
         6134  +             iReader++, nReaders++ ){
         6135  +          dlrInit(&dlReaders[nReaders], DL_DEFAULT,
         6136  +                  optLeavesReaderData(&readers[iReader]),
         6137  +                  optLeavesReaderDataBytes(&readers[iReader]));
         6138  +        }
         6139  +
         6140  +        /* Merge doclists and swap result into accumulator. */
         6141  +        dataBufferReset(&merged);
         6142  +        docListMerge(&merged, dlReaders, nReaders);
         6143  +        tmp = merged;
         6144  +        merged = doclist;
         6145  +        doclist = tmp;
         6146  +
         6147  +        while( nReaders-- > 0 ){
         6148  +          dlrDestroy(&dlReaders[nReaders]);
         6149  +        }
         6150  +
         6151  +        /* Accumulated doclist to reader 0 for next pass. */
         6152  +        dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData);
         6153  +      }
         6154  +
         6155  +      /* Destroy reader that was left in the pipeline. */
         6156  +      dlrDestroy(&dlReaders[0]);
         6157  +
         6158  +      /* Trim deletions from the doclist. */
         6159  +      dataBufferReset(&merged);
         6160  +      docListTrim(DL_DEFAULT, doclist.pData, doclist.nData,
         6161  +                  -1, DL_DEFAULT, &merged);
         6162  +    }
         6163  +
         6164  +    /* Only pass doclists with hits (skip if all hits deleted). */
         6165  +    if( merged.nData>0 ){
         6166  +      rc = leafWriterStep(v, pWriter,
         6167  +                          optLeavesReaderTerm(&readers[0]),
         6168  +                          optLeavesReaderTermBytes(&readers[0]),
         6169  +                          merged.pData, merged.nData);
         6170  +      if( rc!=SQLITE_OK ) goto err;
         6171  +    }
         6172  +
         6173  +    /* Step merged readers to next term and reorder. */
         6174  +    while( i-- > 0 ){
         6175  +      rc = optLeavesReaderStep(v, &readers[i]);
         6176  +      if( rc!=SQLITE_OK ) goto err;
         6177  +
         6178  +      optLeavesReaderReorder(&readers[i], nReaders-i);
         6179  +    }
         6180  +  }
         6181  +
         6182  + err:
         6183  +  dataBufferDestroy(&doclist);
         6184  +  dataBufferDestroy(&merged);
         6185  +  return rc;
         6186  +}
         6187  +
         6188  +/* Implement optimize() function for FTS3.  optimize(t) merges all
         6189  +** segments in the fts index into a single segment.  't' is the magic
         6190  +** table-named column.
         6191  +*/
         6192  +static void optimizeFunc(sqlite3_context *pContext,
         6193  +                         int argc, sqlite3_value **argv){
         6194  +  fulltext_cursor *pCursor;
         6195  +  if( argc>1 ){
         6196  +    sqlite3_result_error(pContext, "excess arguments to optimize()",-1);
         6197  +  }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
         6198  +            sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
         6199  +    sqlite3_result_error(pContext, "illegal first argument to optimize",-1);
         6200  +  }else{
         6201  +    fulltext_vtab *v;
         6202  +    int i, rc, iMaxLevel;
         6203  +    OptLeavesReader *readers;
         6204  +    int nReaders;
         6205  +    LeafWriter writer;
         6206  +    sqlite3_stmt *s;
         6207  +
         6208  +    memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
         6209  +    v = cursor_vtab(pCursor);
         6210  +
         6211  +    /* Flush any buffered updates before optimizing. */
         6212  +    rc = flushPendingTerms(v);
         6213  +    if( rc!=SQLITE_OK ) goto err;
         6214  +
         6215  +    rc = segdir_count(v, &nReaders, &iMaxLevel);
         6216  +    if( rc!=SQLITE_OK ) goto err;
         6217  +    if( nReaders==0 || nReaders==1 ){
         6218  +      sqlite3_result_text(pContext, "Index already optimal", -1,
         6219  +                          SQLITE_STATIC);
         6220  +      return;
         6221  +    }
         6222  +
         6223  +    rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s);
         6224  +    if( rc!=SQLITE_OK ) goto err;
         6225  +
         6226  +    readers = sqlite3_malloc(nReaders*sizeof(readers[0]));
         6227  +    if( readers==NULL ) goto err;
         6228  +
         6229  +    /* Note that there will already be a segment at this position
         6230  +    ** until we call segdir_delete() on iMaxLevel.
         6231  +    */
         6232  +    leafWriterInit(iMaxLevel, 0, &writer);
         6233  +
         6234  +    i = 0;
         6235  +    while( (rc = sqlite3_step(s))==SQLITE_ROW ){
         6236  +      sqlite_int64 iStart = sqlite3_column_int64(s, 0);
         6237  +      sqlite_int64 iEnd = sqlite3_column_int64(s, 1);
         6238  +      const char *pRootData = sqlite3_column_blob(s, 2);
         6239  +      int nRootData = sqlite3_column_bytes(s, 2);
         6240  +
         6241  +      assert( i<nReaders );
         6242  +      rc = leavesReaderInit(v, -1, iStart, iEnd, pRootData, nRootData,
         6243  +                            &readers[i].reader);
         6244  +      if( rc!=SQLITE_OK ) break;
         6245  +
         6246  +      readers[i].segment = i;
         6247  +      i++;
         6248  +    }
         6249  +
         6250  +    /* If we managed to successfully read them all, optimize them. */
         6251  +    if( rc==SQLITE_DONE ){
         6252  +      assert( i==nReaders );
         6253  +      rc = optimizeInternal(v, readers, nReaders, &writer);
         6254  +    }
         6255  +
         6256  +    while( i-- > 0 ){
         6257  +      leavesReaderDestroy(&readers[i].reader);
         6258  +    }
         6259  +    sqlite3_free(readers);
         6260  +
         6261  +    /* If we've successfully gotten to here, delete the old segments
         6262  +    ** and flush the interior structure of the new segment.
         6263  +    */
         6264  +    if( rc==SQLITE_OK ){
         6265  +      for( i=0; i<=iMaxLevel; i++ ){
         6266  +        rc = segdir_delete(v, i);
         6267  +        if( rc!=SQLITE_OK ) break;
         6268  +      }
         6269  +
         6270  +      if( rc==SQLITE_OK ) rc = leafWriterFinalize(v, &writer);
         6271  +    }
         6272  +
         6273  +    leafWriterDestroy(&writer);
         6274  +
         6275  +    if( rc!=SQLITE_OK ) goto err;
         6276  +
         6277  +    sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
         6278  +    return;
         6279  +
         6280  +    /* TODO(shess): Error-handling needs to be improved along the
         6281  +    ** lines of the dump_ functions.
         6282  +    */
         6283  + err:
         6284  +    {
         6285  +      char buf[512];
         6286  +      sqlite3_snprintf(sizeof(buf), buf, "Error in optimize: %s",
         6287  +                       sqlite3_errmsg(sqlite3_context_db_handle(pContext)));
         6288  +      sqlite3_result_error(pContext, buf, -1);
         6289  +    }
         6290  +  }
         6291  +}
         6292  +
         6293  +#ifdef SQLITE_TEST
         6294  +/* Generate an error of the form "<prefix>: <msg>".  If msg is NULL,
         6295  +** pull the error from the context's db handle.
         6296  +*/
         6297  +static void generateError(sqlite3_context *pContext,
         6298  +                          const char *prefix, const char *msg){
         6299  +  char buf[512];
         6300  +  if( msg==NULL ) msg = sqlite3_errmsg(sqlite3_context_db_handle(pContext));
         6301  +  sqlite3_snprintf(sizeof(buf), buf, "%s: %s", prefix, msg);
         6302  +  sqlite3_result_error(pContext, buf, -1);
         6303  +}
         6304  +
         6305  +/* Helper function to collect the set of terms in the segment into
         6306  +** pTerms.  The segment is defined by the leaf nodes between
         6307  +** iStartBlockid and iEndBlockid, inclusive, or by the contents of
         6308  +** pRootData if iStartBlockid is 0 (in which case the entire segment
         6309  +** fit in a leaf).
         6310  +*/
         6311  +static int collectSegmentTerms(fulltext_vtab *v, sqlite3_stmt *s,
         6312  +                               fts2Hash *pTerms){
         6313  +  const sqlite_int64 iStartBlockid = sqlite3_column_int64(s, 0);
         6314  +  const sqlite_int64 iEndBlockid = sqlite3_column_int64(s, 1);
         6315  +  const char *pRootData = sqlite3_column_blob(s, 2);
         6316  +  const int nRootData = sqlite3_column_bytes(s, 2);
         6317  +  LeavesReader reader;
         6318  +  int rc = leavesReaderInit(v, 0, iStartBlockid, iEndBlockid,
         6319  +                            pRootData, nRootData, &reader);
         6320  +  if( rc!=SQLITE_OK ) return rc;
         6321  +
         6322  +  while( rc==SQLITE_OK && !leavesReaderAtEnd(&reader) ){
         6323  +    const char *pTerm = leavesReaderTerm(&reader);
         6324  +    const int nTerm = leavesReaderTermBytes(&reader);
         6325  +    void *oldValue = sqlite3Fts2HashFind(pTerms, pTerm, nTerm);
         6326  +    void *newValue = (void *)((char *)oldValue+1);
         6327  +
         6328  +    /* From the comment before sqlite3Fts2HashInsert in fts2_hash.c,
         6329  +    ** the data value passed is returned in case of malloc failure.
         6330  +    */
         6331  +    if( newValue==sqlite3Fts2HashInsert(pTerms, pTerm, nTerm, newValue) ){
         6332  +      rc = SQLITE_NOMEM;
         6333  +    }else{
         6334  +      rc = leavesReaderStep(v, &reader);
         6335  +    }
         6336  +  }
         6337  +
         6338  +  leavesReaderDestroy(&reader);
         6339  +  return rc;
         6340  +}
         6341  +
         6342  +/* Helper function to build the result string for dump_terms(). */
         6343  +static int generateTermsResult(sqlite3_context *pContext, fts2Hash *pTerms){
         6344  +  int iTerm, nTerms, nResultBytes, iByte;
         6345  +  char *result;
         6346  +  TermData *pData;
         6347  +  fts2HashElem *e;
         6348  +
         6349  +  /* Iterate pTerms to generate an array of terms in pData for
         6350  +  ** sorting.
         6351  +  */
         6352  +  nTerms = fts2HashCount(pTerms);
         6353  +  assert( nTerms>0 );
         6354  +  pData = sqlite3_malloc(nTerms*sizeof(TermData));
         6355  +  if( pData==NULL ) return SQLITE_NOMEM;
         6356  +
         6357  +  nResultBytes = 0;
         6358  +  for(iTerm = 0, e = fts2HashFirst(pTerms); e; iTerm++, e = fts2HashNext(e)){
         6359  +    nResultBytes += fts2HashKeysize(e)+1;   /* Term plus trailing space */
         6360  +    assert( iTerm<nTerms );
         6361  +    pData[iTerm].pTerm = fts2HashKey(e);
         6362  +    pData[iTerm].nTerm = fts2HashKeysize(e);
         6363  +    pData[iTerm].pCollector = fts2HashData(e);  /* unused */
         6364  +  }
         6365  +  assert( iTerm==nTerms );
         6366  +
         6367  +  assert( nResultBytes>0 );   /* nTerms>0, nResultsBytes must be, too. */
         6368  +  result = sqlite3_malloc(nResultBytes);
         6369  +  if( result==NULL ){
         6370  +    sqlite3_free(pData);
         6371  +    return SQLITE_NOMEM;
         6372  +  }
         6373  +
         6374  +  if( nTerms>1 ) qsort(pData, nTerms, sizeof(*pData), termDataCmp);
         6375  +
         6376  +  /* Read the terms in order to build the result. */
         6377  +  iByte = 0;
         6378  +  for(iTerm=0; iTerm<nTerms; ++iTerm){
         6379  +    memcpy(result+iByte, pData[iTerm].pTerm, pData[iTerm].nTerm);
         6380  +    iByte += pData[iTerm].nTerm;
         6381  +    result[iByte++] = ' ';
         6382  +  }
         6383  +  assert( iByte==nResultBytes );
         6384  +  assert( result[nResultBytes-1]==' ' );
         6385  +  result[nResultBytes-1] = '\0';
         6386  +
         6387  +  /* Passes away ownership of result. */
         6388  +  sqlite3_result_text(pContext, result, nResultBytes-1, sqlite3_free);
         6389  +  sqlite3_free(pData);
         6390  +  return SQLITE_OK;
         6391  +}
         6392  +
         6393  +/* Implements dump_terms() for use in inspecting the fts2 index from
         6394  +** tests.  TEXT result containing the ordered list of terms joined by
         6395  +** spaces.  dump_terms(t, level, idx) dumps the terms for the segment
         6396  +** specified by level, idx (in %_segdir), while dump_terms(t) dumps
         6397  +** all terms in the index.  In both cases t is the fts table's magic
         6398  +** table-named column.
         6399  +*/
         6400  +static void dumpTermsFunc(
         6401  +  sqlite3_context *pContext,
         6402  +  int argc, sqlite3_value **argv
         6403  +){
         6404  +  fulltext_cursor *pCursor;
         6405  +  if( argc!=3 && argc!=1 ){
         6406  +    generateError(pContext, "dump_terms", "incorrect arguments");
         6407  +  }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
         6408  +            sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
         6409  +    generateError(pContext, "dump_terms", "illegal first argument");
         6410  +  }else{
         6411  +    fulltext_vtab *v;
         6412  +    fts2Hash terms;
         6413  +    sqlite3_stmt *s = NULL;
         6414  +    int rc;
         6415  +
         6416  +    memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
         6417  +    v = cursor_vtab(pCursor);
         6418  +
         6419  +    /* If passed only the cursor column, get all segments.  Otherwise
         6420  +    ** get the segment described by the following two arguments.
         6421  +    */
         6422  +    if( argc==1 ){
         6423  +      rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s);
         6424  +    }else{
         6425  +      rc = sql_get_statement(v, SEGDIR_SELECT_SEGMENT_STMT, &s);
         6426  +      if( rc==SQLITE_OK ){
         6427  +        rc = sqlite3_bind_int(s, 1, sqlite3_value_int(argv[1]));
         6428  +        if( rc==SQLITE_OK ){
         6429  +          rc = sqlite3_bind_int(s, 2, sqlite3_value_int(argv[2]));
         6430  +        }
         6431  +      }
         6432  +    }
         6433  +
         6434  +    if( rc!=SQLITE_OK ){
         6435  +      generateError(pContext, "dump_terms", NULL);
         6436  +      return;
         6437  +    }
         6438  +
         6439  +    /* Collect the terms for each segment. */
         6440  +    sqlite3Fts2HashInit(&terms, FTS2_HASH_STRING, 1);
         6441  +    while( (rc = sqlite3_step(s))==SQLITE_ROW ){
         6442  +      rc = collectSegmentTerms(v, s, &terms);
         6443  +      if( rc!=SQLITE_OK ) break;
         6444  +    }
         6445  +
         6446  +    if( rc!=SQLITE_DONE ){
         6447  +      sqlite3_reset(s);
         6448  +      generateError(pContext, "dump_terms", NULL);
         6449  +    }else{
         6450  +      const int nTerms = fts2HashCount(&terms);
         6451  +      if( nTerms>0 ){
         6452  +        rc = generateTermsResult(pContext, &terms);
         6453  +        if( rc==SQLITE_NOMEM ){
         6454  +          generateError(pContext, "dump_terms", "out of memory");
         6455  +        }else{
         6456  +          assert( rc==SQLITE_OK );
         6457  +        }
         6458  +      }else if( argc==3 ){
         6459  +        /* The specific segment asked for could not be found. */
         6460  +        generateError(pContext, "dump_terms", "segment not found");
         6461  +      }else{
         6462  +        /* No segments found. */
         6463  +        /* TODO(shess): It should be impossible to reach this.  This
         6464  +        ** case can only happen for an empty table, in which case
         6465  +        ** SQLite has no rows to call this function on.
         6466  +        */
         6467  +        sqlite3_result_null(pContext);
         6468  +      }
         6469  +    }
         6470  +    sqlite3Fts2HashClear(&terms);
         6471  +  }
         6472  +}
         6473  +
         6474  +/* Expand the DL_DEFAULT doclist in pData into a text result in
         6475  +** pContext.
         6476  +*/
         6477  +static void createDoclistResult(sqlite3_context *pContext,
         6478  +                                const char *pData, int nData){
         6479  +  DataBuffer dump;
         6480  +  DLReader dlReader;
         6481  +
         6482  +  assert( pData!=NULL && nData>0 );
         6483  +
         6484  +  dataBufferInit(&dump, 0);
         6485  +  dlrInit(&dlReader, DL_DEFAULT, pData, nData);
         6486  +  for( ; !dlrAtEnd(&dlReader); dlrStep(&dlReader) ){
         6487  +    char buf[256];
         6488  +    PLReader plReader;
         6489  +
         6490  +    plrInit(&plReader, &dlReader);
         6491  +    if( DL_DEFAULT==DL_DOCIDS || plrAtEnd(&plReader) ){
         6492  +      sqlite3_snprintf(sizeof(buf), buf, "[%lld] ", dlrDocid(&dlReader));
         6493  +      dataBufferAppend(&dump, buf, strlen(buf));
         6494  +    }else{
         6495  +      int iColumn = plrColumn(&plReader);
         6496  +
         6497  +      sqlite3_snprintf(sizeof(buf), buf, "[%lld %d[",
         6498  +                       dlrDocid(&dlReader), iColumn);
         6499  +      dataBufferAppend(&dump, buf, strlen(buf));
         6500  +
         6501  +      for( ; !plrAtEnd(&plReader); plrStep(&plReader) ){
         6502  +        if( plrColumn(&plReader)!=iColumn ){
         6503  +          iColumn = plrColumn(&plReader);
         6504  +          sqlite3_snprintf(sizeof(buf), buf, "] %d[", iColumn);
         6505  +          assert( dump.nData>0 );
         6506  +          dump.nData--;                     /* Overwrite trailing space. */
         6507  +          assert( dump.pData[dump.nData]==' ');
         6508  +          dataBufferAppend(&dump, buf, strlen(buf));
         6509  +        }
         6510  +        if( DL_DEFAULT==DL_POSITIONS_OFFSETS ){
         6511  +          sqlite3_snprintf(sizeof(buf), buf, "%d,%d,%d ",
         6512  +                           plrPosition(&plReader),
         6513  +                           plrStartOffset(&plReader), plrEndOffset(&plReader));
         6514  +        }else if( DL_DEFAULT==DL_POSITIONS ){
         6515  +          sqlite3_snprintf(sizeof(buf), buf, "%d ", plrPosition(&plReader));
         6516  +        }else{
         6517  +          assert( NULL=="Unhandled DL_DEFAULT value");
         6518  +        }
         6519  +        dataBufferAppend(&dump, buf, strlen(buf));
         6520  +      }
         6521  +      plrDestroy(&plReader);
         6522  +
         6523  +      assert( dump.nData>0 );
         6524  +      dump.nData--;                     /* Overwrite trailing space. */
         6525  +      assert( dump.pData[dump.nData]==' ');
         6526  +      dataBufferAppend(&dump, "]] ", 3);
         6527  +    }
         6528  +  }
         6529  +  dlrDestroy(&dlReader);
         6530  +
         6531  +  assert( dump.nData>0 );
         6532  +  dump.nData--;                     /* Overwrite trailing space. */
         6533  +  assert( dump.pData[dump.nData]==' ');
         6534  +  dump.pData[dump.nData] = '\0';
         6535  +  assert( dump.nData>0 );
         6536  +
         6537  +  /* Passes ownership of dump's buffer to pContext. */
         6538  +  sqlite3_result_text(pContext, dump.pData, dump.nData, sqlite3_free);
         6539  +  dump.pData = NULL;
         6540  +  dump.nData = dump.nCapacity = 0;
         6541  +}
         6542  +
         6543  +/* Implements dump_doclist() for use in inspecting the fts2 index from
         6544  +** tests.  TEXT result containing a string representation of the
         6545  +** doclist for the indicated term.  dump_doclist(t, term, level, idx)
         6546  +** dumps the doclist for term from the segment specified by level, idx
         6547  +** (in %_segdir), while dump_doclist(t, term) dumps the logical
         6548  +** doclist for the term across all segments.  The per-segment doclist
         6549  +** can contain deletions, while the full-index doclist will not
         6550  +** (deletions are omitted).
         6551  +**
         6552  +** Result formats differ with the setting of DL_DEFAULTS.  Examples:
         6553  +**
         6554  +** DL_DOCIDS: [1] [3] [7]
         6555  +** DL_POSITIONS: [1 0[0 4] 1[17]] [3 1[5]]
         6556  +** DL_POSITIONS_OFFSETS: [1 0[0,0,3 4,23,26] 1[17,102,105]] [3 1[5,20,23]]
         6557  +**
         6558  +** In each case the number after the outer '[' is the docid.  In the
         6559  +** latter two cases, the number before the inner '[' is the column
         6560  +** associated with the values within.  For DL_POSITIONS the numbers
         6561  +** within are the positions, for DL_POSITIONS_OFFSETS they are the
         6562  +** position, the start offset, and the end offset.
         6563  +*/
         6564  +static void dumpDoclistFunc(
         6565  +  sqlite3_context *pContext,
         6566  +  int argc, sqlite3_value **argv
         6567  +){
         6568  +  fulltext_cursor *pCursor;
         6569  +  if( argc!=2 && argc!=4 ){
         6570  +    generateError(pContext, "dump_doclist", "incorrect arguments");
         6571  +  }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ||
         6572  +            sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){
         6573  +    generateError(pContext, "dump_doclist", "illegal first argument");
         6574  +  }else if( sqlite3_value_text(argv[1])==NULL ||
         6575  +            sqlite3_value_text(argv[1])[0]=='\0' ){
         6576  +    generateError(pContext, "dump_doclist", "empty second argument");
         6577  +  }else{
         6578  +    const char *pTerm = (const char *)sqlite3_value_text(argv[1]);
         6579  +    const int nTerm = strlen(pTerm);
         6580  +    fulltext_vtab *v;
         6581  +    int rc;
         6582  +    DataBuffer doclist;
         6583  +
         6584  +    memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor));
         6585  +    v = cursor_vtab(pCursor);
         6586  +
         6587  +    dataBufferInit(&doclist, 0);
         6588  +
         6589  +    /* termSelect() yields the same logical doclist that queries are
         6590  +    ** run against.
         6591  +    */
         6592  +    if( argc==2 ){
         6593  +      rc = termSelect(v, v->nColumn, pTerm, nTerm, 0, DL_DEFAULT, &doclist);
         6594  +    }else{
         6595  +      sqlite3_stmt *s = NULL;
         6596  +
         6597  +      /* Get our specific segment's information. */
         6598  +      rc = sql_get_statement(v, SEGDIR_SELECT_SEGMENT_STMT, &s);
         6599  +      if( rc==SQLITE_OK ){
         6600  +        rc = sqlite3_bind_int(s, 1, sqlite3_value_int(argv[2]));
         6601  +        if( rc==SQLITE_OK ){
         6602  +          rc = sqlite3_bind_int(s, 2, sqlite3_value_int(argv[3]));
         6603  +        }
         6604  +      }
         6605  +
         6606  +      if( rc==SQLITE_OK ){
         6607  +        rc = sqlite3_step(s);
         6608  +
         6609  +        if( rc==SQLITE_DONE ){
         6610  +          dataBufferDestroy(&doclist);
         6611  +          generateError(pContext, "dump_doclist", "segment not found");
         6612  +          return;
         6613  +        }
         6614  +
         6615  +        /* Found a segment, load it into doclist. */
         6616  +        if( rc==SQLITE_ROW ){
         6617  +          const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1);
         6618  +          const char *pData = sqlite3_column_blob(s, 2);
         6619  +          const int nData = sqlite3_column_bytes(s, 2);
         6620  +
         6621  +          /* loadSegment() is used by termSelect() to load each
         6622  +          ** segment's data.
         6623  +          */
         6624  +          rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, 0,
         6625  +                           &doclist);
         6626  +          if( rc==SQLITE_OK ){
         6627  +            rc = sqlite3_step(s);
         6628  +
         6629  +            /* Should not have more than one matching segment. */
         6630  +            if( rc!=SQLITE_DONE ){
         6631  +              sqlite3_reset(s);
         6632  +              dataBufferDestroy(&doclist);
         6633  +              generateError(pContext, "dump_doclist", "invalid segdir");
         6634  +              return;
         6635  +            }
         6636  +            rc = SQLITE_OK;
         6637  +          }
         6638  +        }
         6639  +      }
         6640  +
         6641  +      sqlite3_reset(s);
         6642  +    }
         6643  +
         6644  +    if( rc==SQLITE_OK ){
         6645  +      if( doclist.nData>0 ){
         6646  +        createDoclistResult(pContext, doclist.pData, doclist.nData);
         6647  +      }else{
         6648  +        /* TODO(shess): This can happen if the term is not present, or
         6649  +        ** if all instances of the term have been deleted and this is
         6650  +        ** an all-index dump.  It may be interesting to distinguish
         6651  +        ** these cases.
         6652  +        */
         6653  +        sqlite3_result_text(pContext, "", 0, SQLITE_STATIC);
         6654  +      }
         6655  +    }else if( rc==SQLITE_NOMEM ){
         6656  +      /* Handle out-of-memory cases specially because if they are
         6657  +      ** generated in fts2 code they may not be reflected in the db
         6658  +      ** handle.
         6659  +      */
         6660  +      /* TODO(shess): Handle this more comprehensively.
         6661  +      ** sqlite3ErrStr() has what I need, but is internal.
         6662  +      */
         6663  +      generateError(pContext, "dump_doclist", "out of memory");
         6664  +    }else{
         6665  +      generateError(pContext, "dump_doclist", NULL);
         6666  +    }
         6667  +
         6668  +    dataBufferDestroy(&doclist);
         6669  +  }
         6670  +}
         6671  +#endif
         6672  +
  5774   6673   /*
  5775   6674   ** This routine implements the xFindFunction method for the FTS2
  5776   6675   ** virtual table.
  5777   6676   */
  5778   6677   static int fulltextFindFunction(
  5779   6678     sqlite3_vtab *pVtab,
  5780   6679     int nArg,
................................................................................
  5784   6683   ){
  5785   6684     if( strcmp(zName,"snippet")==0 ){
  5786   6685       *pxFunc = snippetFunc;
  5787   6686       return 1;
  5788   6687     }else if( strcmp(zName,"offsets")==0 ){
  5789   6688       *pxFunc = snippetOffsetsFunc;
  5790   6689       return 1;
         6690  +  }else if( strcmp(zName,"optimize")==0 ){
         6691  +    *pxFunc = optimizeFunc;
         6692  +    return 1;
         6693  +#ifdef SQLITE_TEST
         6694  +    /* NOTE(shess): These functions are present only for testing
         6695  +    ** purposes.  No particular effort is made to optimize their
         6696  +    ** execution or how they build their results.
         6697  +    */
         6698  +  }else if( strcmp(zName,"dump_terms")==0 ){
         6699  +    /* fprintf(stderr, "Found dump_terms\n"); */
         6700  +    *pxFunc = dumpTermsFunc;
         6701  +    return 1;
         6702  +  }else if( strcmp(zName,"dump_doclist")==0 ){
         6703  +    /* fprintf(stderr, "Found dump_doclist\n"); */
         6704  +    *pxFunc = dumpDoclistFunc;
         6705  +    return 1;
         6706  +#endif
  5791   6707     }
  5792   6708     return 0;
  5793   6709   }
  5794   6710   
  5795   6711   /*
  5796   6712   ** Rename an fts2 table.
  5797   6713   */
................................................................................
  5903   6819     ** the two scalar functions. If this is successful, register the
  5904   6820     ** module with sqlite.
  5905   6821     */
  5906   6822     if( SQLITE_OK==rc 
  5907   6823      && SQLITE_OK==(rc = sqlite3Fts2InitHashTable(db, pHash, "fts2_tokenizer"))
  5908   6824      && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
  5909   6825      && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1))
         6826  +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", -1))
         6827  +#ifdef SQLITE_TEST
         6828  +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_terms", -1))
         6829  +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_doclist", -1))
         6830  +#endif
  5910   6831     ){
  5911   6832       return sqlite3_create_module_v2(
  5912   6833           db, "fts2", &fts2Module, (void *)pHash, hashDestroy
  5913   6834       );
  5914   6835     }
  5915   6836   
  5916         -  /* An error has occured. Delete the hash table and return the error code. */
         6837  +  /* An error has occurred. Delete the hash table and return the error code. */
  5917   6838     assert( rc!=SQLITE_OK );
  5918   6839     if( pHash ){
  5919   6840       sqlite3Fts2HashClear(pHash);
  5920   6841       sqlite3_free(pHash);
  5921   6842     }
  5922   6843     return rc;
  5923   6844   }

Changes to SQLite.Interop/FTS2/fts2_hash.c.

    25     25   */
    26     26   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2)
    27     27   
    28     28   #include <assert.h>
    29     29   #include <stdlib.h>
    30     30   #include <string.h>
    31     31   
           32  +#include "sqlite3.h"
    32     33   #include "fts2_hash.h"
    33     34   
    34         -static void *malloc_and_zero(int n){
    35         -  void *p = malloc(n);
           35  +/*
           36  +** Malloc and Free functions
           37  +*/
           38  +static void *fts2HashMalloc(int n){
           39  +  void *p = sqlite3_malloc(n);
    36     40     if( p ){
    37     41       memset(p, 0, n);
    38     42     }
    39     43     return p;
           44  +}
           45  +static void fts2HashFree(void *p){
           46  +  sqlite3_free(p);
    40     47   }
    41     48   
    42     49   /* Turn bulk memory into a hash table object by initializing the
    43     50   ** fields of the Hash structure.
    44     51   **
    45     52   ** "pNew" is a pointer to the hash table that is to be initialized.
    46     53   ** keyClass is one of the constants 
................................................................................
    54     61     assert( keyClass>=FTS2_HASH_STRING && keyClass<=FTS2_HASH_BINARY );
    55     62     pNew->keyClass = keyClass;
    56     63     pNew->copyKey = copyKey;
    57     64     pNew->first = 0;
    58     65     pNew->count = 0;
    59     66     pNew->htsize = 0;
    60     67     pNew->ht = 0;
    61         -  pNew->xMalloc = malloc_and_zero;
    62         -  pNew->xFree = free;
    63     68   }
    64     69   
    65     70   /* Remove all entries from a hash table.  Reclaim all memory.
    66     71   ** Call this routine to delete a hash table or to reset a hash table
    67     72   ** to the empty state.
    68     73   */
    69     74   void sqlite3Fts2HashClear(fts2Hash *pH){
    70     75     fts2HashElem *elem;         /* For looping over all elements of the table */
    71     76   
    72     77     assert( pH!=0 );
    73     78     elem = pH->first;
    74     79     pH->first = 0;
    75         -  if( pH->ht ) pH->xFree(pH->ht);
           80  +  fts2HashFree(pH->ht);
    76     81     pH->ht = 0;
    77     82     pH->htsize = 0;
    78     83     while( elem ){
    79     84       fts2HashElem *next_elem = elem->next;
    80     85       if( pH->copyKey && elem->pKey ){
    81         -      pH->xFree(elem->pKey);
           86  +      fts2HashFree(elem->pKey);
    82     87       }
    83         -    pH->xFree(elem);
           88  +    fts2HashFree(elem);
    84     89       elem = next_elem;
    85     90     }
    86     91     pH->count = 0;
    87     92   }
    88     93   
    89     94   /*
    90     95   ** Hash and comparison functions when the mode is FTS2_HASH_STRING
................................................................................
   188    193   */
   189    194   static void rehash(fts2Hash *pH, int new_size){
   190    195     struct _fts2ht *new_ht;          /* The new hash table */
   191    196     fts2HashElem *elem, *next_elem;  /* For looping over existing elements */
   192    197     int (*xHash)(const void*,int);   /* The hash function */
   193    198   
   194    199     assert( (new_size & (new_size-1))==0 );
   195         -  new_ht = (struct _fts2ht *)pH->xMalloc( new_size*sizeof(struct _fts2ht) );
          200  +  new_ht = (struct _fts2ht *)fts2HashMalloc( new_size*sizeof(struct _fts2ht) );
   196    201     if( new_ht==0 ) return;
   197         -  if( pH->ht ) pH->xFree(pH->ht);
          202  +  fts2HashFree(pH->ht);
   198    203     pH->ht = new_ht;
   199    204     pH->htsize = new_size;
   200    205     xHash = hashFunction(pH->keyClass);
   201    206     for(elem=pH->first, pH->first=0; elem; elem = next_elem){
   202    207       int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
   203    208       next_elem = elem->next;
   204    209       insertElement(pH, &new_ht[h], elem);
................................................................................
   256    261       pEntry->chain = elem->next;
   257    262     }
   258    263     pEntry->count--;
   259    264     if( pEntry->count<=0 ){
   260    265       pEntry->chain = 0;
   261    266     }
   262    267     if( pH->copyKey && elem->pKey ){
   263         -    pH->xFree(elem->pKey);
          268  +    fts2HashFree(elem->pKey);
   264    269     }
   265         -  pH->xFree( elem );
          270  +  fts2HashFree( elem );
   266    271     pH->count--;
   267    272     if( pH->count<=0 ){
   268    273       assert( pH->first==0 );
   269    274       assert( pH->count==0 );
   270    275       fts2HashClear(pH);
   271    276     }
   272    277   }
................................................................................
   329    334         removeElementGivenHash(pH,elem,h);
   330    335       }else{
   331    336         elem->data = data;
   332    337       }
   333    338       return old_data;
   334    339     }
   335    340     if( data==0 ) return 0;
   336         -  new_elem = (fts2HashElem*)pH->xMalloc( sizeof(fts2HashElem) );
          341  +  new_elem = (fts2HashElem*)fts2HashMalloc( sizeof(fts2HashElem) );
   337    342     if( new_elem==0 ) return data;
   338    343     if( pH->copyKey && pKey!=0 ){
   339         -    new_elem->pKey = pH->xMalloc( nKey );
          344  +    new_elem->pKey = fts2HashMalloc( nKey );
   340    345       if( new_elem->pKey==0 ){
   341         -      pH->xFree(new_elem);
          346  +      fts2HashFree(new_elem);
   342    347         return data;
   343    348       }
   344    349       memcpy((void*)new_elem->pKey, pKey, nKey);
   345    350     }else{
   346    351       new_elem->pKey = (void*)pKey;
   347    352     }
   348    353     new_elem->nKey = nKey;
   349    354     pH->count++;
   350    355     if( pH->htsize==0 ){
   351    356       rehash(pH,8);
   352    357       if( pH->htsize==0 ){
   353    358         pH->count = 0;
   354         -      pH->xFree(new_elem);
          359  +      fts2HashFree(new_elem);
   355    360         return data;
   356    361       }
   357    362     }
   358    363     if( pH->count > pH->htsize ){
   359    364       rehash(pH,pH->htsize*2);
   360    365     }
   361    366     assert( pH->htsize>0 );

Changes to SQLite.Interop/FTS2/fts2_hash.h.

    30     30   ** this structure opaque.
    31     31   */
    32     32   struct fts2Hash {
    33     33     char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
    34     34     char copyKey;           /* True if copy of key made on insert */
    35     35     int count;              /* Number of entries in this table */
    36     36     fts2HashElem *first;    /* The first element of the array */
    37         -  void *(*xMalloc)(int);  /* malloc() function to use */
    38         -  void (*xFree)(void *);  /* free() function to use */
    39     37     int htsize;             /* Number of buckets in the hash table */
    40     38     struct _fts2ht {        /* the hash table */
    41     39       int count;               /* Number of entries with this hash */
    42     40       fts2HashElem *chain;     /* Pointer to first entry with this hash */
    43     41     } *ht;
    44     42   };
    45     43   

Changes to SQLite.Interop/FTS2/fts2_porter.c.

    25     25   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2)
    26     26   
    27     27   
    28     28   #include <assert.h>
    29     29   #include <stdlib.h>
    30     30   #include <stdio.h>
    31     31   #include <string.h>
    32         -#include <ctype.h>
    33     32   
    34     33   #include "fts2_tokenizer.h"
    35     34   
    36     35   /*
    37     36   ** Class derived from sqlite3_tokenizer
    38     37   */
    39     38   typedef struct porter_tokenizer {
................................................................................
    62     61   ** Create a new tokenizer instance.
    63     62   */
    64     63   static int porterCreate(
    65     64     int argc, const char * const *argv,
    66     65     sqlite3_tokenizer **ppTokenizer
    67     66   ){
    68     67     porter_tokenizer *t;
    69         -  t = (porter_tokenizer *) calloc(sizeof(*t), 1);
           68  +  t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
    70     69     if( t==NULL ) return SQLITE_NOMEM;
    71         -
           70  +  memset(t, 0, sizeof(*t));
    72     71     *ppTokenizer = &t->base;
    73     72     return SQLITE_OK;
    74     73   }
    75     74   
    76     75   /*
    77     76   ** Destroy a tokenizer
    78     77   */
    79     78   static int porterDestroy(sqlite3_tokenizer *pTokenizer){
    80         -  free(pTokenizer);
           79  +  sqlite3_free(pTokenizer);
    81     80     return SQLITE_OK;
    82     81   }
    83     82   
    84     83   /*
    85     84   ** Prepare to begin tokenizing a particular string.  The input
    86     85   ** string to be tokenized is zInput[0..nInput-1].  A cursor
    87     86   ** used to incrementally tokenize this string is returned in 
................................................................................
    90     89   static int porterOpen(
    91     90     sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
    92     91     const char *zInput, int nInput,        /* String to be tokenized */
    93     92     sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
    94     93   ){
    95     94     porter_tokenizer_cursor *c;
    96     95   
    97         -  c = (porter_tokenizer_cursor *) malloc(sizeof(*c));
           96  +  c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
    98     97     if( c==NULL ) return SQLITE_NOMEM;
    99     98   
   100     99     c->zInput = zInput;
   101    100     if( zInput==0 ){
   102    101       c->nInput = 0;
   103    102     }else if( nInput<0 ){
   104    103       c->nInput = (int)strlen(zInput);
................................................................................
   116    115   
   117    116   /*
   118    117   ** Close a tokenization cursor previously opened by a call to
   119    118   ** porterOpen() above.
   120    119   */
   121    120   static int porterClose(sqlite3_tokenizer_cursor *pCursor){
   122    121     porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
   123         -  free(c->zToken);
   124         -  free(c);
          122  +  sqlite3_free(c->zToken);
          123  +  sqlite3_free(c);
   125    124     return SQLITE_OK;
   126    125   }
   127    126   /*
   128    127   ** Vowel or consonant
   129    128   */
   130    129   static const char cType[] = {
   131    130      0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
................................................................................
   599    598         c->iOffset++;
   600    599       }
   601    600   
   602    601       if( c->iOffset>iStartOffset ){
   603    602         int n = c->iOffset-iStartOffset;
   604    603         if( n>c->nAllocated ){
   605    604           c->nAllocated = n+20;
   606         -        c->zToken = realloc(c->zToken, c->nAllocated);
          605  +        c->zToken = sqlite3_realloc(c->zToken, c->nAllocated);
   607    606           if( c->zToken==NULL ) return SQLITE_NOMEM;
   608    607         }
   609    608         porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
   610    609         *pzToken = c->zToken;
   611    610         *piStartOffset = iStartOffset;
   612    611         *piEndOffset = c->iOffset;
   613    612         *piPosition = c->iToken++;

Changes to SQLite.Interop/FTS2/fts2_tokenizer.c.

   235    235     sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
   236    236     sqlite3_step(pStmt);
   237    237   
   238    238     return sqlite3_finalize(pStmt);
   239    239   }
   240    240   
   241    241   static
   242         -int queryTokenizer(
          242  +int queryFts2Tokenizer(
   243    243     sqlite3 *db, 
   244    244     char *zName,  
   245    245     const sqlite3_tokenizer_module **pp
   246    246   ){
   247    247     int rc;
   248    248     sqlite3_stmt *pStmt;
   249    249     const char zSql[] = "SELECT fts2_tokenizer(?)";
................................................................................
   268    268   
   269    269   /*
   270    270   ** Implementation of the scalar function fts2_tokenizer_internal_test().
   271    271   ** This function is used for testing only, it is not included in the
   272    272   ** build unless SQLITE_TEST is defined.
   273    273   **
   274    274   ** The purpose of this is to test that the fts2_tokenizer() function
   275         -** can be used as designed by the C-code in the queryTokenizer and
          275  +** can be used as designed by the C-code in the queryFts2Tokenizer and
   276    276   ** registerTokenizer() functions above. These two functions are repeated
   277    277   ** in the README.tokenizer file as an example, so it is important to
   278    278   ** test them.
   279    279   **
   280    280   ** To run the tests, evaluate the fts2_tokenizer_internal_test() scalar
   281    281   ** function with no arguments. An assert() will fail if a problem is
   282    282   ** detected. i.e.:
................................................................................
   292    292     int rc;
   293    293     const sqlite3_tokenizer_module *p1;
   294    294     const sqlite3_tokenizer_module *p2;
   295    295     sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
   296    296   
   297    297     /* Test the query function */
   298    298     sqlite3Fts2SimpleTokenizerModule(&p1);
   299         -  rc = queryTokenizer(db, "simple", &p2);
          299  +  rc = queryFts2Tokenizer(db, "simple", &p2);
   300    300     assert( rc==SQLITE_OK );
   301    301     assert( p1==p2 );
   302         -  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
          302  +  rc = queryFts2Tokenizer(db, "nosuchtokenizer", &p2);
   303    303     assert( rc==SQLITE_ERROR );
   304    304     assert( p2==0 );
   305    305     assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
   306    306   
   307    307     /* Test the storage function */
   308    308     rc = registerTokenizer(db, "nosuchtokenizer", p1);
   309    309     assert( rc==SQLITE_OK );
   310         -  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
          310  +  rc = queryFts2Tokenizer(db, "nosuchtokenizer", &p2);
   311    311     assert( rc==SQLITE_OK );
   312    312     assert( p2==p1 );
   313    313   
   314    314     sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
   315    315   }
   316    316   
   317    317   #endif

Changes to SQLite.Interop/FTS2/fts2_tokenizer1.c.

    25     25   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2)
    26     26   
    27     27   
    28     28   #include <assert.h>
    29     29   #include <stdlib.h>
    30     30   #include <stdio.h>
    31     31   #include <string.h>
    32         -#include <ctype.h>
    33     32   
    34     33   #include "fts2_tokenizer.h"
    35     34   
    36     35   typedef struct simple_tokenizer {
    37     36     sqlite3_tokenizer base;
    38     37     char delim[128];             /* flag ASCII delimiters */
    39     38   } simple_tokenizer;
................................................................................
    61     60   */
    62     61   static int simpleCreate(
    63     62     int argc, const char * const *argv,
    64     63     sqlite3_tokenizer **ppTokenizer
    65     64   ){
    66     65     simple_tokenizer *t;
    67     66   
    68         -  t = (simple_tokenizer *) calloc(sizeof(*t), 1);
           67  +  t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
    69     68     if( t==NULL ) return SQLITE_NOMEM;
           69  +  memset(t, 0, sizeof(*t));
    70     70   
    71     71     /* TODO(shess) Delimiters need to remain the same from run to run,
    72     72     ** else we need to reindex.  One solution would be a meta-table to
    73     73     ** track such information in the database, then we'd only want this
    74     74     ** information on the initial create.
    75     75     */
    76     76     if( argc>1 ){
    77     77       int i, n = strlen(argv[1]);
    78     78       for(i=0; i<n; i++){
    79     79         unsigned char ch = argv[1][i];
    80     80         /* We explicitly don't support UTF-8 delimiters for now. */
    81     81         if( ch>=0x80 ){
    82         -        free(t);
           82  +        sqlite3_free(t);
    83     83           return SQLITE_ERROR;
    84     84         }
    85     85         t->delim[ch] = 1;
    86     86       }
    87     87     } else {
    88     88       /* Mark non-alphanumeric ASCII characters as delimiters */
    89     89       int i;
    90     90       for(i=1; i<0x80; i++){
    91         -      t->delim[i] = !isalnum(i);
           91  +      t->delim[i] = !((i>='0' && i<='9') || (i>='A' && i<='Z') ||
           92  +                      (i>='a' && i<='z'));
    92     93       }
    93     94     }
    94     95   
    95     96     *ppTokenizer = &t->base;
    96     97     return SQLITE_OK;
    97     98   }
    98     99   
    99    100   /*
   100    101   ** Destroy a tokenizer
   101    102   */
   102    103   static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
   103         -  free(pTokenizer);
          104  +  sqlite3_free(pTokenizer);
   104    105     return SQLITE_OK;
   105    106   }
   106    107   
   107    108   /*
   108    109   ** Prepare to begin tokenizing a particular string.  The input
   109    110   ** string to be tokenized is pInput[0..nBytes-1].  A cursor
   110    111   ** used to incrementally tokenize this string is returned in 
................................................................................
   113    114   static int simpleOpen(
   114    115     sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
   115    116     const char *pInput, int nBytes,        /* String to be tokenized */
   116    117     sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
   117    118   ){
   118    119     simple_tokenizer_cursor *c;
   119    120   
   120         -  c = (simple_tokenizer_cursor *) malloc(sizeof(*c));
          121  +  c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
   121    122     if( c==NULL ) return SQLITE_NOMEM;
   122    123   
   123    124     c->pInput = pInput;
   124    125     if( pInput==0 ){
   125    126       c->nBytes = 0;
   126    127     }else if( nBytes<0 ){
   127    128       c->nBytes = (int)strlen(pInput);
................................................................................
   139    140   
   140    141   /*
   141    142   ** Close a tokenization cursor previously opened by a call to
   142    143   ** simpleOpen() above.
   143    144   */
   144    145   static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
   145    146     simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
   146         -  free(c->pToken);
   147         -  free(c);
          147  +  sqlite3_free(c->pToken);
          148  +  sqlite3_free(c);
   148    149     return SQLITE_OK;
   149    150   }
   150    151   
   151    152   /*
   152    153   ** Extract the next token from a tokenization cursor.  The cursor must
   153    154   ** have been opened by a prior call to simpleOpen().
   154    155   */
................................................................................
   178    179         c->iOffset++;
   179    180       }
   180    181   
   181    182       if( c->iOffset>iStartOffset ){
   182    183         int i, n = c->iOffset-iStartOffset;
   183    184         if( n>c->nTokenAllocated ){
   184    185           c->nTokenAllocated = n+20;
   185         -        c->pToken = realloc(c->pToken, c->nTokenAllocated);
          186  +        c->pToken = sqlite3_realloc(c->pToken, c->nTokenAllocated);
   186    187           if( c->pToken==NULL ) return SQLITE_NOMEM;
   187    188         }
   188    189         for(i=0; i<n; i++){
   189    190           /* TODO(shess) This needs expansion to handle UTF-8
   190    191           ** case-insensitivity.
   191    192           */
   192    193           unsigned char ch = p[iStartOffset+i];
   193         -        c->pToken[i] = ch<0x80 ? tolower(ch) : ch;
          194  +        c->pToken[i] = (ch>='A' && ch<='Z') ? (ch - 'A' + 'a') : ch;
   194    195         }
   195    196         *ppToken = c->pToken;
   196    197         *pnBytes = n;
   197    198         *piStartOffset = iStartOffset;
   198    199         *piEndOffset = c->iOffset;
   199    200         *piPosition = c->iToken++;
   200    201   

Changes to SQLite.Interop/SQLite.Interop.rc.

     9      9   //
    10     10   #include "afxres.h"
    11     11   
    12     12   /////////////////////////////////////////////////////////////////////////////
    13     13   #undef APSTUDIO_READONLY_SYMBOLS
    14     14   
    15     15   /////////////////////////////////////////////////////////////////////////////
    16         -// English (U.S.) resources
           16  +// English (United States) resources
    17     17   
    18     18   #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
    19         -#ifdef _WIN32
    20     19   LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
    21     20   #pragma code_page(1252)
    22         -#endif //_WIN32
    23     21   
    24     22   #ifdef APSTUDIO_INVOKED
    25     23   /////////////////////////////////////////////////////////////////////////////
    26     24   //
    27     25   // TEXTINCLUDE
    28     26   //
    29     27   
................................................................................
    70     68           BLOCK "040904b0"
    71     69           BEGIN
    72     70               VALUE "Comments", "http://sqlite.phxsoftware.com"
    73     71               VALUE "FileDescription", "System.Data.SQLite Interop Library"
    74     72               VALUE "FileVersion", "1.0.67.0"
    75     73               VALUE "InternalName", "SQLite.Interop.DLL"
    76     74               VALUE "LegalCopyright", "Released to the public domain"
    77         -            VALUE "OriginalFilename", "SQLite3.DLL 3.7.0.1"
           75  +            VALUE "OriginalFilename", "SQLite3.DLL 3.7.4"
    78     76               VALUE "ProductName", "System.Data.SQLite"
    79     77               VALUE "ProductVersion", "1.0"
    80     78           END
    81     79       END
    82     80       BLOCK "VarFileInfo"
    83     81       BEGIN
    84     82           VALUE "Translation", 0x409, 1200
    85     83       END
    86     84   END
    87     85   
    88         -#endif    // English (U.S.) resources
           86  +#endif    // English (United States) resources
    89     87   /////////////////////////////////////////////////////////////////////////////
    90     88   
    91     89   
    92     90   
    93     91   #ifndef APSTUDIO_INVOKED
    94     92   /////////////////////////////////////////////////////////////////////////////
    95     93   //

Changes to SQLite.Interop/SQLite.Interop.vcproj.

     8      8   	Keyword="Win32Proj"
     9      9   	TargetFrameworkVersion="131072"
    10     10   	>
    11     11   	<Platforms>
    12     12   		<Platform
    13     13   			Name="Win32"
    14     14   		/>
    15         -		<Platform
    16         -			Name="Pocket PC 2003 (ARMV4)"
    17         -		/>
    18         -		<Platform
    19         -			Name="Itanium"
    20         -		/>
    21     15   		<Platform
    22     16   			Name="x64"
    23     17   		/>
    24     18   	</Platforms>
    25     19   	<ToolFiles>
    26     20   	</ToolFiles>
    27     21   	<Configurations>
................................................................................
    79     73   				ModuleDefinitionFile="src\sqlite3.def"
    80     74   				EmbedManagedResourceFile=""
    81     75   				DelayLoadDLLs="advapi32.dll"
    82     76   				RandomizedBaseAddress="1"
    83     77   				DataExecutionPrevention="0"
    84     78   				ImportLibrary=""
    85     79   				TargetMachine="1"
    86         -				KeyFile="..\System.Data.SQLite\System.Data.SQLite.snk"
    87         -				CLRUnmanagedCodeCheck="true"
    88         -			/>
    89         -			<Tool
    90         -				Name="VCALinkTool"
    91         -			/>
    92         -			<Tool
    93         -				Name="VCManifestTool"
    94         -				EmbedManifest="true"
    95         -			/>
    96         -			<Tool
    97         -				Name="VCXDCMakeTool"
    98         -			/>
    99         -			<Tool
   100         -				Name="VCBscMakeTool"
   101         -			/>
   102         -			<Tool
   103         -				Name="VCFxCopTool"
   104         -			/>
   105         -			<Tool
   106         -				Name="VCAppVerifierTool"
   107         -			/>
   108         -			<Tool
   109         -				Name="VCPostBuildEventTool"
   110         -				CommandLine=""
   111         -			/>
   112         -		</Configuration>
   113         -		<Configuration
   114         -			Name="Release|Pocket PC 2003 (ARMV4)"
   115         -			OutputDirectory="Pocket PC 2003 (ARMV4)\$(ConfigurationName)"
   116         -			IntermediateDirectory="Pocket PC 2003 (ARMV4)\$(ConfigurationName)"
   117         -			ConfigurationType="2"
   118         -			CharacterSet="1"
   119         -			WholeProgramOptimization="0"
   120         -			>
   121         -			<Tool
   122         -				Name="VCPreBuildEventTool"
   123         -				CommandLine=""
   124         -			/>
   125         -			<Tool
   126         -				Name="VCCustomBuildTool"
   127         -			/>
   128         -			<Tool
   129         -				Name="VCXMLDataGeneratorTool"
   130         -			/>
   131         -			<Tool
   132         -				Name="VCWebServiceProxyGeneratorTool"
   133         -			/>
   134         -			<Tool
   135         -				Name="VCMIDLTool"
   136         -			/>
   137         -			<Tool
   138         -				Name="VCCLCompilerTool"
   139         -				ExecutionBucket="7"
   140         -				Optimization="1"
   141         -				FavorSizeOrSpeed="2"
   142         -				PreprocessorDefinitions="_WIN32_WCE=$(CEVER);UNDER_CE=$(CEVER);WINCE;$(PLATFORMDEFINES);NDEBUG;_WINDOWS;_USRDLL;CPPSMART_EXPORTS;$(ARCHFAM);$(_ARCHFAM_);UNICODE;_UNICODE;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;INVALID_FILE_ATTRIBUTES=((DWORD)-1);SQLITE_OMIT_WAL"
   143         -				StringPooling="true"
   144         -				ExceptionHandling="0"
   145         -				BufferSecurityCheck="false"
   146         -				CompileForArchitecture="0"
   147         -				InterworkCalls="false"
   148         -			/>
   149         -			<Tool
   150         -				Name="VCManagedResourceCompilerTool"
   151         -				ResourceFileName="$(IntDir)\System.Data.SQLite.$(InputName).resources"
   152         -			/>
   153         -			<Tool
   154         -				Name="VCResourceCompilerTool"
   155         -				PreprocessorDefinitions="NDEBUG;_UNICODE;UNICODE;_WIN32_WCE;UNDER_CE"
   156         -				Culture="1033"
   157         -				AdditionalIncludeDirectories="$(IntDir)"
   158         -			/>
   159         -			<Tool
   160         -				Name="VCPreLinkEventTool"
   161         -				CommandLine=""
   162         -			/>
   163         -			<Tool
   164         -				Name="VCLinkerTool"
   165         -				IgnoreImportLibrary="true"
   166         -				AdditionalOptions=" /subsystem:windowsce,4.20 /ARMPADCODE"
   167         -				OutputFile="../bin/CompactFramework/$(InputName).067.DLL"
   168         -				AdditionalLibraryDirectories=""
   169         -				IgnoreDefaultLibraryNames=""
   170         -				ModuleDefinitionFile="src\sqlite3.def"
   171         -				AddModuleNamesToAssembly=""
   172         -				EmbedManagedResourceFile=""
   173         -				EntryPointSymbol=""
   174         -				MergeSections=""
   175         -				TargetMachine="3"
   176         -			/>
   177         -			<Tool
   178         -				Name="VCALinkTool"
   179         -			/>
   180         -			<Tool
   181         -				Name="VCXDCMakeTool"
   182         -			/>
   183         -			<Tool
   184         -				Name="VCBscMakeTool"
   185         -			/>
   186         -			<Tool
   187         -				Name="VCFxCopTool"
   188         -			/>
   189         -			<Tool
   190         -				Name="VCCodeSignTool"
   191         -			/>
   192         -			<Tool
   193         -				Name="VCPostBuildEventTool"
   194         -				CommandLine=""
   195         -			/>
   196         -			<DeploymentTool
   197         -				ForceDirty="-1"
   198         -				RemoteDirectory="\Program Files\testce"
   199         -				RegisterOutput="0"
   200         -				AdditionalFiles=""
   201         -			/>
   202         -			<DebuggerTool
   203         -			/>
   204         -		</Configuration>
   205         -		<Configuration
   206         -			Name="Release|Itanium"
   207         -			OutputDirectory="ia64\$(ConfigurationName)"
   208         -			IntermediateDirectory="ia64\$(ConfigurationName)"
   209         -			ConfigurationType="2"
   210         -			CharacterSet="2"
   211         -			ManagedExtensions="0"
   212         -			WholeProgramOptimization="1"
   213         -			>
   214         -			<Tool
   215         -				Name="VCPreBuildEventTool"
   216         -				CommandLine=""
   217         -			/>
   218         -			<Tool
   219         -				Name="VCCustomBuildTool"
   220         -			/>
   221         -			<Tool
   222         -				Name="VCXMLDataGeneratorTool"
   223         -			/>
   224         -			<Tool
   225         -				Name="VCWebServiceProxyGeneratorTool"
   226         -			/>
   227         -			<Tool
   228         -				Name="VCMIDLTool"
   229         -				TargetEnvironment="2"
   230         -			/>
   231         -			<Tool
   232         -				Name="VCCLCompilerTool"
   233         -				AdditionalOptions="/GS-"
   234         -				Optimization="2"
   235         -				FavorSizeOrSpeed="1"
   236         -				PreprocessorDefinitions="NDEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;WIN64;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX"
   237         -				StringPooling="true"
   238         -				ExceptionHandling="0"
   239         -				BufferSecurityCheck="false"
   240         -				EnableFunctionLevelLinking="true"
   241         -				RuntimeTypeInfo="false"
   242         -			/>
   243         -			<Tool
   244         -				Name="VCManagedResourceCompilerTool"
   245         -				ResourceFileName="$(IntDir)\System.Data.SQLite.$(InputName).resources"
   246         -			/>
   247         -			<Tool
   248         -				Name="VCResourceCompilerTool"
   249         -			/>
   250         -			<Tool
   251         -				Name="VCPreLinkEventTool"
   252         -			/>
   253         -			<Tool
   254         -				Name="VCLinkerTool"
   255         -				AdditionalOptions="/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteCommand.bmp,System.Data.SQLite.SQLiteCommand.bmp&#x0D;&#x0A;/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteConnection.bmp,System.Data.SQLite.SQLiteConnection.bmp&#x0D;&#x0A;/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteDataAdapter.bmp,System.Data.SQLite.SQLiteDataAdapter.bmp"
   256         -				AdditionalDependencies="..\System.Data.SQLite\bin\System.Data.SQLite.netmodule"
   257         -				OutputFile="../bin/Itanium/System.Data.SQLite.DLL"
   258         -				ModuleDefinitionFile="src\sqlite3.def"
   259         -				EmbedManagedResourceFile=""
   260         -				DelayLoadDLLs="advapi32.dll"
   261         -				RandomizedBaseAddress="1"
   262         -				DataExecutionPrevention="0"
   263         -				ImportLibrary=""
   264         -				TargetMachine="5"
   265     80   				KeyFile="..\System.Data.SQLite\System.Data.SQLite.snk"
   266     81   				CLRUnmanagedCodeCheck="true"
   267     82   			/>
   268     83   			<Tool
   269     84   				Name="VCALinkTool"
   270     85   			/>
   271     86   			<Tool
................................................................................
   449    264   			<Tool
   450    265   				Name="VCAppVerifierTool"
   451    266   			/>
   452    267   			<Tool
   453    268   				Name="VCPostBuildEventTool"
   454    269   				CommandLine=""
   455    270   			/>
   456         -		</Configuration>
   457         -		<Configuration
   458         -			Name="Debug|Pocket PC 2003 (ARMV4)"
   459         -			OutputDirectory="Pocket PC 2003 (ARMV4)\$(ConfigurationName)"
   460         -			IntermediateDirectory="Pocket PC 2003 (ARMV4)\$(ConfigurationName)"
   461         -			ConfigurationType="2"
   462         -			CharacterSet="1"
   463         -			WholeProgramOptimization="0"
   464         -			>
   465         -			<Tool
   466         -				Name="VCPreBuildEventTool"
   467         -				CommandLine=""
   468         -			/>
   469         -			<Tool
   470         -				Name="VCCustomBuildTool"
   471         -			/>
   472         -			<Tool
   473         -				Name="VCXMLDataGeneratorTool"
   474         -			/>
   475         -			<Tool
   476         -				Name="VCWebServiceProxyGeneratorTool"
   477         -			/>
   478         -			<Tool
   479         -				Name="VCMIDLTool"
   480         -				TargetEnvironment="1"
   481         -			/>
   482         -			<Tool
   483         -				Name="VCCLCompilerTool"
   484         -				ExecutionBucket="7"
   485         -				AdditionalOptions="/GS-"
   486         -				Optimization="0"
   487         -				PreprocessorDefinitions="_WIN32_WCE=$(CEVER);UNDER_CE=$(CEVER);WINCE;$(PLATFORMDEFINES);_DEBUG;_WINDOWS;_USRDLL;CPPSMART_EXPORTS;$(ARCHFAM);$(_ARCHFAM_);UNICODE;_UNICODE;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;INVALID_FILE_ATTRIBUTES=((DWORD)-1)"
   488         -				DebugInformationFormat="3"
   489         -				InterworkCalls="false"
   490         -			/>
   491         -			<Tool
   492         -				Name="VCManagedResourceCompilerTool"
   493         -			/>
   494         -			<Tool
   495         -				Name="VCResourceCompilerTool"
   496         -				PreprocessorDefinitions="NDEBUG;_UNICODE;UNICODE;_WIN32_WCE;UNDER_CE"
   497         -				Culture="1033"
   498         -				AdditionalIncludeDirectories="$(IntDir)"
   499         -			/>
   500         -			<Tool
   501         -				Name="VCPreLinkEventTool"
   502         -			/>
   503         -			<Tool
   504         -				Name="VCLinkerTool"
   505         -				IgnoreImportLibrary="true"
   506         -				AdditionalOptions=" /subsystem:windowsce,4.20 /ARMPADCODE"
   507         -				OutputFile="../bin/CompactFramework/$(InputName).065.DLL"
   508         -				AdditionalLibraryDirectories=""
   509         -				IgnoreDefaultLibraryNames=""
   510         -				ModuleDefinitionFile="src\sqlite3.def"
   511         -				AddModuleNamesToAssembly=""
   512         -				EmbedManagedResourceFile=""
   513         -				GenerateDebugInformation="true"
   514         -				SubSystem="8"
   515         -				TargetMachine="3"
   516         -			/>
   517         -			<Tool
   518         -				Name="VCALinkTool"
   519         -			/>
   520         -			<Tool
   521         -				Name="VCXDCMakeTool"
   522         -			/>
   523         -			<Tool
   524         -				Name="VCBscMakeTool"
   525         -			/>
   526         -			<Tool
   527         -				Name="VCFxCopTool"
   528         -			/>
   529         -			<Tool
   530         -				Name="VCCodeSignTool"
   531         -			/>
   532         -			<Tool
   533         -				Name="VCPostBuildEventTool"
   534         -				CommandLine=""
   535         -			/>
   536         -			<DeploymentTool
   537         -				ForceDirty="-1"
   538         -				RemoteDirectory="\Program Files\testce"
   539         -				RegisterOutput="0"
   540         -				AdditionalFiles=""
   541         -			/>
   542         -			<DebuggerTool
   543         -			/>
   544         -		</Configuration>
   545         -		<Configuration
   546         -			Name="Debug|Itanium"
   547         -			OutputDirectory="$(PlatformName)\$(ConfigurationName)"
   548         -			IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
   549         -			ConfigurationType="2"
   550         -			CharacterSet="2"
   551         -			ManagedExtensions="0"
   552         -			WholeProgramOptimization="0"
   553         -			>
   554         -			<Tool
   555         -				Name="VCPreBuildEventTool"
   556         -				CommandLine=""
   557         -			/>
   558         -			<Tool
   559         -				Name="VCCustomBuildTool"
   560         -			/>
   561         -			<Tool
   562         -				Name="VCXMLDataGeneratorTool"
   563         -			/>
   564         -			<Tool
   565         -				Name="VCWebServiceProxyGeneratorTool"
   566         -			/>
   567         -			<Tool
   568         -				Name="VCMIDLTool"
   569         -				TargetEnvironment="2"
   570         -			/>
   571         -			<Tool
   572         -				Name="VCCLCompilerTool"
   573         -				AdditionalOptions="/GS-"
   574         -				Optimization="0"
   575         -				PreprocessorDefinitions="SQLITE_DEBUG;_DEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;WIN64;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX"
   576         -				RuntimeLibrary="3"
   577         -				RuntimeTypeInfo="false"
   578         -				DebugInformationFormat="3"
   579         -			/>
   580         -			<Tool
   581         -				Name="VCManagedResourceCompilerTool"
   582         -			/>
   583         -			<Tool
   584         -				Name="VCResourceCompilerTool"
   585         -			/>
   586         -			<Tool
   587         -				Name="VCPreLinkEventTool"
   588         -			/>
   589         -			<Tool
   590         -				Name="VCLinkerTool"
   591         -				OutputFile="../bin/Itanium/$(InputName).DLL"
   592         -				ModuleDefinitionFile="src\sqlite3.def"
   593         -				EmbedManagedResourceFile=""
   594         -				DelayLoadDLLs="advapi32.dll"
   595         -				GenerateDebugInformation="true"
   596         -				RandomizedBaseAddress="1"
   597         -				DataExecutionPrevention="0"
   598         -				ImportLibrary=""
   599         -				TargetMachine="5"
   600         -				KeyFile="..\System.Data.SQLite\System.Data.SQLite.snk"
   601         -			/>
   602         -			<Tool
   603         -				Name="VCALinkTool"
   604         -			/>
   605         -			<Tool
   606         -				Name="VCManifestTool"
   607         -				EmbedManifest="true"
   608         -			/>
   609         -			<Tool
   610         -				Name="VCXDCMakeTool"
   611         -			/>
   612         -			<Tool
   613         -				Name="VCBscMakeTool"
   614         -			/>
   615         -			<Tool
   616         -				Name="VCFxCopTool"
   617         -			/>
   618         -			<Tool
   619         -				Name="VCAppVerifierTool"
   620         -			/>
   621         -			<Tool
   622         -				Name="VCPostBuildEventTool"
   623         -			/>
   624    271   		</Configuration>
   625    272   		<Configuration
   626    273   			Name="Debug|x64"
   627    274   			OutputDirectory="$(PlatformName)\$(ConfigurationName)"
   628    275   			IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
   629    276   			ConfigurationType="2"
   630    277   			CharacterSet="2"
................................................................................
   778    425   			<Tool
   779    426   				Name="VCAppVerifierTool"
   780    427   			/>
   781    428   			<Tool
   782    429   				Name="VCPostBuildEventTool"
   783    430   				CommandLine=""
   784    431   			/>
   785         -		</Configuration>
   786         -		<Configuration
   787         -			Name="StockDebug|Pocket PC 2003 (ARMV4)"
   788         -			OutputDirectory="Pocket PC 2003 (ARMV4)\$(ConfigurationName)"
   789         -			IntermediateDirectory="Pocket PC 2003 (ARMV4)\$(ConfigurationName)"
   790         -			ConfigurationType="2"
   791         -			CharacterSet="1"
   792         -			WholeProgramOptimization="0"
   793         -			>
   794         -			<Tool
   795         -				Name="VCPreBuildEventTool"
   796         -				CommandLine=""
   797         -			/>
   798         -			<Tool
   799         -				Name="VCCustomBuildTool"
   800         -			/>
   801         -			<Tool
   802         -				Name="VCXMLDataGeneratorTool"
   803         -			/>
   804         -			<Tool
   805         -				Name="VCWebServiceProxyGeneratorTool"
   806         -			/>
   807         -			<Tool
   808         -				Name="VCMIDLTool"
   809         -				TargetEnvironment="1"
   810         -			/>
   811         -			<Tool
   812         -				Name="VCCLCompilerTool"
   813         -				ExecutionBucket="7"
   814         -				AdditionalOptions="/GS-"
   815         -				Optimization="0"
   816         -				PreprocessorDefinitions="SQLITE_DEBUG;_WIN32_WCE=$(CEVER);UNDER_CE=$(CEVER);WINCE;$(PLATFORMDEFINES);_DEBUG;_WINDOWS;_USRDLL;CPPSMART_EXPORTS;$(ARCHFAM);$(_ARCHFAM_);UNICODE;_UNICODE;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;SQLITE_CORE;INVALID_FILE_ATTRIBUTES=((DWORD)-1)"
   817         -				DebugInformationFormat="3"
   818         -				InterworkCalls="true"
   819         -			/>
   820         -			<Tool
   821         -				Name="VCManagedResourceCompilerTool"
   822         -			/>
   823         -			<Tool
   824         -				Name="VCResourceCompilerTool"
   825         -				PreprocessorDefinitions="NDEBUG;_UNICODE;UNICODE;_WIN32_WCE;UNDER_CE"
   826         -				Culture="1033"
   827         -				AdditionalIncludeDirectories="$(IntDir)"
   828         -			/>
   829         -			<Tool
   830         -				Name="VCPreLinkEventTool"
   831         -			/>
   832         -			<Tool
   833         -				Name="VCLinkerTool"
   834         -				IgnoreImportLibrary="true"
   835         -				AdditionalOptions=" /subsystem:windowsce,4.20 /machine:ARM /ARMPADCODE"
   836         -				OutputFile="../bin/CompactFramework/sqlite3.dll"
   837         -				AdditionalLibraryDirectories=""
   838         -				IgnoreDefaultLibraryNames=""
   839         -				ModuleDefinitionFile="src\sqlite3.def"
   840         -				AddModuleNamesToAssembly=""
   841         -				EmbedManagedResourceFile=""
   842         -				GenerateDebugInformation="true"
   843         -				TargetMachine="0"
   844         -			/>
   845         -			<Tool
   846         -				Name="VCALinkTool"
   847         -			/>
   848         -			<Tool
   849         -				Name="VCXDCMakeTool"
   850         -			/>
   851         -			<Tool
   852         -				Name="VCBscMakeTool"
   853         -			/>
   854         -			<Tool
   855         -				Name="VCFxCopTool"
   856         -			/>
   857         -			<Tool
   858         -				Name="VCCodeSignTool"
   859         -			/>
   860         -			<Tool
   861         -				Name="VCPostBuildEventTool"
   862         -				CommandLine=""
   863         -			/>
   864         -			<DeploymentTool
   865         -				ForceDirty="-1"
   866         -				RemoteDirectory="\Program Files\testce"
   867         -				RegisterOutput="0"
   868         -				AdditionalFiles=""
   869         -			/>
   870         -			<DebuggerTool
   871         -			/>
   872         -		</Configuration>
   873         -		<Configuration
   874         -			Name="StockDebug|Itanium"
   875         -			OutputDirectory="$(PlatformName)\$(ConfigurationName)"
   876         -			IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
   877         -			ConfigurationType="2"
   878         -			CharacterSet="2"
   879         -			ManagedExtensions="0"
   880         -			WholeProgramOptimization="0"
   881         -			>
   882         -			<Tool
   883         -				Name="VCPreBuildEventTool"
   884         -				CommandLine=""
   885         -			/>
   886         -			<Tool
   887         -				Name="VCCustomBuildTool"
   888         -			/>
   889         -			<Tool
   890         -				Name="VCXMLDataGeneratorTool"
   891         -			/>
   892         -			<Tool
   893         -				Name="VCWebServiceProxyGeneratorTool"
   894         -			/>
   895         -			<Tool
   896         -				Name="VCMIDLTool"
   897         -				TargetEnvironment="2"
   898         -			/>
   899         -			<Tool
   900         -				Name="VCCLCompilerTool"
   901         -				AdditionalOptions="/GS-"
   902         -				Optimization="0"
   903         -				PreprocessorDefinitions="SQLITE_DEBUG;_DEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;WIN64;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX"
   904         -				RuntimeLibrary="3"
   905         -				RuntimeTypeInfo="false"
   906         -				DebugInformationFormat="3"
   907         -			/>
   908         -			<Tool
   909         -				Name="VCManagedResourceCompilerTool"
   910         -			/>
   911         -			<Tool
   912         -				Name="VCResourceCompilerTool"
   913         -			/>
   914         -			<Tool
   915         -				Name="VCPreLinkEventTool"
   916         -			/>
   917         -			<Tool
   918         -				Name="VCLinkerTool"
   919         -				OutputFile="../bin/Itanium/$(InputName).DLL"
   920         -				ModuleDefinitionFile="src\sqlite3.def"
   921         -				EmbedManagedResourceFile=""
   922         -				DelayLoadDLLs="advapi32.dll"
   923         -				GenerateDebugInformation="true"
   924         -				RandomizedBaseAddress="1"
   925         -				DataExecutionPrevention="0"
   926         -				ImportLibrary=""
   927         -				TargetMachine="5"
   928         -				KeyFile="..\System.Data.SQLite\System.Data.SQLite.snk"
   929         -			/>
   930         -			<Tool
   931         -				Name="VCALinkTool"
   932         -			/>
   933         -			<Tool
   934         -				Name="VCManifestTool"
   935         -				EmbedManifest="true"
   936         -			/>
   937         -			<Tool
   938         -				Name="VCXDCMakeTool"
   939         -			/>
   940         -			<Tool
   941         -				Name="VCBscMakeTool"
   942         -			/>
   943         -			<Tool
   944         -				Name="VCFxCopTool"
   945         -			/>
   946         -			<Tool
   947         -				Name="VCAppVerifierTool"
   948         -			/>
   949         -			<Tool
   950         -				Name="VCPostBuildEventTool"
   951         -			/>
   952    432   		</Configuration>
   953    433   		<Configuration
   954    434   			Name="StockDebug|x64"
   955    435   			OutputDirectory="$(PlatformName)\$(ConfigurationName)"
   956    436   			IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
   957    437   			ConfigurationType="2"
   958    438   			CharacterSet="2"
................................................................................
  1065    545   				<FileConfiguration
  1066    546   					Name="Release|Win32"
  1067    547   					>
  1068    548   					<Tool
  1069    549   						Name="VCManagedResourceCompilerTool"
  1070    550   						ResourceFileName="$(IntDir)\System.Data.SQLite.$(InputName).resources"
  1071    551   					/>
  1072         -				</FileConfiguration>
  1073         -				<FileConfiguration
  1074         -					Name="Release|Pocket PC 2003 (ARMV4)"
  1075         -					ExcludedFromBuild="true"
  1076         -					>
  1077         -					<Tool
  1078         -						Name="VCManagedResourceCompilerTool"
  1079         -						ResourceFileName="$(IntDir)\System.Data.SQLite.$(InputName).resources"
  1080         -					/>
  1081         -				</FileConfiguration>
  1082         -				<FileConfiguration
  1083         -					Name="Release|Itanium"
  1084         -					>
  1085         -					<Tool
  1086         -						Name="VCManagedResourceCompilerTool"
  1087         -						ResourceFileName="$(IntDir)\System.Data.SQLite.$(InputName).resources"
  1088         -					/>
  1089    552   				</FileConfiguration>
  1090    553   				<FileConfiguration
  1091    554   					Name="Release|x64"
  1092    555   					>
  1093    556   					<Tool
  1094    557   						Name="VCManagedResourceCompilerTool"
  1095    558   						ResourceFileName="$(IntDir)\System.Data.SQLite.$(InputName).resources"
................................................................................
  1098    561   				<FileConfiguration
  1099    562   					Name="Debug|Win32"
  1100    563   					ExcludedFromBuild="true"
  1101    564   					>
  1102    565   					<Tool
  1103    566   						Name="VCManagedResourceCompilerTool"
  1104    567   					/>
  1105         -				</FileConfiguration>
  1106         -				<FileConfiguration
  1107         -					Name="Debug|Pocket PC 2003 (ARMV4)"
  1108         -					ExcludedFromBuild="true"
  1109         -					>
  1110         -					<Tool
  1111         -						Name="VCManagedResourceCompilerTool"
  1112         -					/>
  1113         -				</FileConfiguration>
  1114         -				<FileConfiguration
  1115         -					Name="Debug|Itanium"
  1116         -					ExcludedFromBuild="true"
  1117         -					>
  1118         -					<Tool
  1119         -						Name="VCManagedResourceCompilerTool"
  1120         -					/>
  1121    568   				</FileConfiguration>
  1122    569   				<FileConfiguration
  1123    570   					Name="Debug|x64"
  1124    571   					ExcludedFromBuild="true"
  1125    572   					>
  1126    573   					<Tool
  1127    574   						Name="VCManagedResourceCompilerTool"
  1128    575   					/>
  1129    576   				</FileConfiguration>
  1130    577   				<FileConfiguration
  1131    578   					Name="StockDebug|Win32"
  1132    579   					ExcludedFromBuild="true"
  1133    580   					>
  1134         -					<Tool
  1135         -						Name="VCManagedResourceCompilerTool"
  1136         -					/>
  1137         -				</FileConfiguration>
  1138         -				<FileConfiguration
  1139         -					Name="StockDebug|Pocket PC 2003 (ARMV4)"
  1140         -					ExcludedFromBuild="true"
  1141         -					>
  1142         -					<Tool
  1143         -						Name="VCManagedResourceCompilerTool"
  1144         -					/>
  1145         -				</FileConfiguration>
  1146         -				<FileConfiguration
  1147         -					Name="StockDebug|Itanium"
  1148         -					ExcludedFromBuild="true"
  1149         -					>
  1150    581   					<Tool
  1151    582   						Name="VCManagedResourceCompilerTool"
  1152    583   					/>
  1153    584   				</FileConfiguration>
  1154    585   				<FileConfiguration
  1155    586   					Name="StockDebug|x64"
  1156    587   					ExcludedFromBuild="true"

Added SQLite.Interop/SQLite.Interop.vcxproj.

            1  +<?xml version="1.0" encoding="utf-8"?>
            2  +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
            3  +  <ItemGroup Label="ProjectConfigurations">
            4  +    <ProjectConfiguration Include="Debug|Win32">
            5  +      <Configuration>Debug</Configuration>
            6  +      <Platform>Win32</Platform>
            7  +    </ProjectConfiguration>
            8  +    <ProjectConfiguration Include="Debug|x64">
            9  +      <Configuration>Debug</Configuration>
           10  +      <Platform>x64</Platform>
           11  +    </ProjectConfiguration>
           12  +    <ProjectConfiguration Include="Release|Win32">
           13  +      <Configuration>Release</Configuration>
           14  +      <Platform>Win32</Platform>
           15  +    </ProjectConfiguration>
           16  +    <ProjectConfiguration Include="Release|x64">
           17  +      <Configuration>Release</Configuration>
           18  +      <Platform>x64</Platform>
           19  +    </ProjectConfiguration>
           20  +    <ProjectConfiguration Include="StockDebug|Win32">
           21  +      <Configuration>StockDebug</Configuration>
           22  +      <Platform>Win32</Platform>
           23  +    </ProjectConfiguration>
           24  +    <ProjectConfiguration Include="StockDebug|x64">
           25  +      <Configuration>StockDebug</Configuration>
           26  +      <Platform>x64</Platform>
           27  +    </ProjectConfiguration>
           28  +  </ItemGroup>
           29  +  <PropertyGroup Label="Globals">
           30  +    <ProjectGuid>{10B51CE8-A838-44DE-BD82-B658F0296F80}</ProjectGuid>
           31  +    <RootNamespace>SQLite.Interop</RootNamespace>
           32  +    <Keyword>Win32Proj</Keyword>
           33  +  </PropertyGroup>
           34  +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
           35  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'" Label="Configuration">
           36  +    <ConfigurationType>DynamicLibrary</ConfigurationType>
           37  +    <CharacterSet>MultiByte</CharacterSet>
           38  +    <CLRSupport>false</CLRSupport>
           39  +    <WholeProgramOptimization>false</WholeProgramOptimization>
           40  +  </PropertyGroup>
           41  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
           42  +    <ConfigurationType>DynamicLibrary</ConfigurationType>
           43  +    <CharacterSet>MultiByte</CharacterSet>
           44  +    <CLRSupport>false</CLRSupport>
           45  +    <WholeProgramOptimization>false</WholeProgramOptimization>
           46  +  </PropertyGroup>
           47  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
           48  +    <ConfigurationType>DynamicLibrary</ConfigurationType>
           49  +    <CharacterSet>MultiByte</CharacterSet>
           50  +    <CLRSupport>false</CLRSupport>
           51  +    <WholeProgramOptimization>true</WholeProgramOptimization>
           52  +  </PropertyGroup>
           53  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'" Label="Configuration">
           54  +    <ConfigurationType>DynamicLibrary</ConfigurationType>
           55  +    <CharacterSet>MultiByte</CharacterSet>
           56  +    <CLRSupport>false</CLRSupport>
           57  +    <WholeProgramOptimization>false</WholeProgramOptimization>
           58  +  </PropertyGroup>
           59  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
           60  +    <ConfigurationType>DynamicLibrary</ConfigurationType>
           61  +    <CharacterSet>MultiByte</CharacterSet>
           62  +    <CLRSupport>false</CLRSupport>
           63  +    <WholeProgramOptimization>false</WholeProgramOptimization>
           64  +  </PropertyGroup>
           65  +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
           66  +    <ConfigurationType>DynamicLibrary</ConfigurationType>
           67  +    <CharacterSet>MultiByte</CharacterSet>
           68  +    <CLRSupport>false</CLRSupport>
           69  +    <WholeProgramOptimization>true</WholeProgramOptimization>
           70  +  </PropertyGroup>
           71  +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
           72  +  <ImportGroup Label="ExtensionSettings">
           73  +  </ImportGroup>
           74  +  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'" Label="PropertySheets">
           75  +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
           76  +  </ImportGroup>
           77  +  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
           78  +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
           79  +  </ImportGroup>
           80  +  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
           81  +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
           82  +  </ImportGroup>
           83  +  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'" Label="PropertySheets">
           84  +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
           85  +  </ImportGroup>
           86  +  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
           87  +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
           88  +  </ImportGroup>
           89  +  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
           90  +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
           91  +  </ImportGroup>
           92  +  <PropertyGroup Label="UserMacros" />
           93  +  <PropertyGroup>
           94  +    <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
           95  +    <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)bin\</OutDir>
           96  +    <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Configuration)\</IntDir>
           97  +    <GenerateManifest Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</GenerateManifest>
           98  +    <EmbedManifest Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</EmbedManifest>
           99  +    <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">..\bin\x64\</OutDir>
          100  +    <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">x64\$(Configuration)\</IntDir>
          101  +    <EmbedManifest Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</EmbedManifest>
          102  +    <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)bin\</OutDir>
          103  +    <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</IntDir>
          104  +    <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</LinkIncremental>
          105  +    <EmbedManifest Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</EmbedManifest>
          106  +    <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(Platform)\$(Configuration)\</OutDir>
          107  +    <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(Platform)\$(Configuration)\</IntDir>
          108  +    <EmbedManifest Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</EmbedManifest>
          109  +    <OutDir Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">$(SolutionDir)bin\</OutDir>
          110  +    <IntDir Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">$(Configuration)\</IntDir>
          111  +    <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">false</LinkIncremental>
          112  +    <EmbedManifest Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">true</EmbedManifest>
          113  +    <OutDir Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'">$(Platform)\$(Configuration)\</OutDir>
          114  +    <IntDir Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'">$(Platform)\$(Configuration)\</IntDir>
          115  +    <EmbedManifest Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'">true</EmbedManifest>
          116  +    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
          117  +    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
          118  +    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
          119  +    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">AllRules.ruleset</CodeAnalysisRuleSet>
          120  +    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" />
          121  +    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" />
          122  +    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
          123  +    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
          124  +    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
          125  +    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Release|x64'">AllRules.ruleset</CodeAnalysisRuleSet>
          126  +    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
          127  +    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
          128  +    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
          129  +    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'" />
          130  +    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'" />
          131  +    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'">AllRules.ruleset</CodeAnalysisRuleSet>
          132  +    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'" />
          133  +    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'" />
          134  +    <TargetName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">System.Data.SQLite</TargetName>
          135  +    <TargetName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">System.Data.SQLite</TargetName>
          136  +    <PostBuildEventUseInBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</PostBuildEventUseInBuild>
          137  +    <PostBuildEventUseInBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</PostBuildEventUseInBuild>
          138  +    <PostBuildEventUseInBuild Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">false</PostBuildEventUseInBuild>
          139  +  </PropertyGroup>
          140  +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
          141  +    <PreBuildEvent>
          142  +      <Command>
          143  +      </Command>
          144  +    </PreBuildEvent>
          145  +    <ClCompile>
          146  +      <AdditionalOptions>/GS- %(AdditionalOptions)</AdditionalOptions>
          147  +      <Optimization>MaxSpeed</Optimization>
          148  +      <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
          149  +      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          150  +      <StringPooling>true</StringPooling>
          151  +      <ExceptionHandling>
          152  +      </ExceptionHandling>
          153  +    </ClCompile>
          154  +    <EmbeddedResource>
          155  +      <LogicalName>$(IntDir)System.Data.SQLite.%(Filename).resources</LogicalName>
          156  +    </EmbeddedResource>
          157  +    <Link>
          158  +      <AdditionalOptions>/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteCommand.bmp,System.Data.SQLite.SQLiteCommand.bmp
          159  +/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteConnection.bmp,System.Data.SQLite.SQLiteConnection.bmp
          160  +/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteDataAdapter.bmp,System.Data.SQLite.SQLiteDataAdapter.bmp %(AdditionalOptions)</AdditionalOptions>
          161  +      <AdditionalDependencies>..\System.Data.SQLite\bin\System.Data.SQLite.netmodule;%(AdditionalDependencies)</AdditionalDependencies>
          162  +      <OutputFile>../bin/System.Data.SQLite.DLL</OutputFile>
          163  +      <IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
          164  +      <ModuleDefinitionFile>src\sqlite3.def</ModuleDefinitionFile>
          165  +      <EmbedManagedResourceFile>%(EmbedManagedResourceFile)</EmbedManagedResourceFile>
          166  +      <DelayLoadDLLs>advapi32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
          167  +      <RandomizedBaseAddress>false</RandomizedBaseAddress>
          168  +      <DataExecutionPrevention>
          169  +      </DataExecutionPrevention>
          170  +      <ImportLibrary>
          171  +      </ImportLibrary>
          172  +      <TargetMachine>MachineX86</TargetMachine>
          173  +      <KeyFile>..\System.Data.SQLite\System.Data.SQLite.snk</KeyFile>
          174  +      <CLRUnmanagedCodeCheck>true</CLRUnmanagedCodeCheck>
          175  +      <DelaySign>true</DelaySign>
          176  +    </Link>
          177  +    <PostBuildEvent>
          178  +      <Command>"$(FrameworkSDKDir)Bin\sn.exe" -R "$(TargetPath)" "$(SolutionDir)System.Data.SQLite\System.Data.SQLite.snk"</Command>
          179  +    </PostBuildEvent>
          180  +  </ItemDefinitionGroup>
          181  +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
          182  +    <PreBuildEvent>
          183  +      <Command>
          184  +      </Command>
          185  +    </PreBuildEvent>
          186  +    <Midl>
          187  +      <TargetEnvironment>X64</TargetEnvironment>
          188  +    </Midl>
          189  +    <ClCompile>
          190  +      <AdditionalOptions>/GS- %(AdditionalOptions)</AdditionalOptions>
          191  +      <Optimization>MaxSpeed</Optimization>
          192  +      <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
          193  +      <PreprocessorDefinitions>NDEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;WIN64;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          194  +      <StringPooling>true</StringPooling>
          195  +      <ExceptionHandling>
          196  +      </ExceptionHandling>
          197  +      <BufferSecurityCheck>false</BufferSecurityCheck>
          198  +      <FunctionLevelLinking>true</FunctionLevelLinking>
          199  +      <RuntimeTypeInfo>false</RuntimeTypeInfo>
          200  +    </ClCompile>
          201  +    <EmbeddedResource>
          202  +      <LogicalName>$(IntDir)System.Data.SQLite.%(Filename).resources</LogicalName>
          203  +    </EmbeddedResource>
          204  +    <Link>
          205  +      <AdditionalOptions>/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteCommand.bmp,System.Data.SQLite.SQLiteCommand.bmp
          206  +/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteConnection.bmp,System.Data.SQLite.SQLiteConnection.bmp
          207  +/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteDataAdapter.bmp,System.Data.SQLite.SQLiteDataAdapter.bmp %(AdditionalOptions)</AdditionalOptions>
          208  +      <AdditionalDependencies>..\System.Data.SQLite\bin\System.Data.SQLite.netmodule;%(AdditionalDependencies)</AdditionalDependencies>
          209  +      <OutputFile>../bin/x64/System.Data.SQLite.DLL</OutputFile>
          210  +      <ModuleDefinitionFile>src\sqlite3.def</ModuleDefinitionFile>
          211  +      <EmbedManagedResourceFile>%(EmbedManagedResourceFile)</EmbedManagedResourceFile>
          212  +      <DelayLoadDLLs>advapi32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
          213  +      <RandomizedBaseAddress>false</RandomizedBaseAddress>
          214  +      <DataExecutionPrevention>
          215  +      </DataExecutionPrevention>
          216  +      <ImportLibrary>
          217  +      </ImportLibrary>
          218  +      <TargetMachine>MachineX64</TargetMachine>
          219  +      <KeyFile>..\System.Data.SQLite\System.Data.SQLite.snk</KeyFile>
          220  +      <CLRUnmanagedCodeCheck>true</CLRUnmanagedCodeCheck>
          221  +      <DelaySign>true</DelaySign>
          222  +    </Link>
          223  +    <PostBuildEvent>
          224  +      <Command>"$(FrameworkSDKDir).\Bin\sn.exe" -R "$(TargetPath)" "..\System.Data.SQLite\System.Data.SQLite.snk"</Command>
          225  +    </PostBuildEvent>
          226  +  </ItemDefinitionGroup>
          227  +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
          228  +    <PreBuildEvent>
          229  +      <Command>
          230  +      </Command>
          231  +    </PreBuildEvent>
          232  +    <ClCompile>
          233  +      <AdditionalOptions>/GS- %(AdditionalOptions)</AdditionalOptions>
          234  +      <Optimization>Disabled</Optimization>
          235  +      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          236  +      <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
          237  +      <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
          238  +      <RuntimeTypeInfo>false</RuntimeTypeInfo>
          239  +      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
          240  +    </ClCompile>
          241  +    <Link>
          242  +      <ModuleDefinitionFile>src\sqlite3.def</ModuleDefinitionFile>
          243  +      <EmbedManagedResourceFile>%(EmbedManagedResourceFile)</EmbedManagedResourceFile>
          244  +      <DelayLoadDLLs>advapi32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
          245  +      <GenerateDebugInformation>true</GenerateDebugInformation>
          246  +      <ImportLibrary>
          247  +      </ImportLibrary>
          248  +    </Link>
          249  +    <PostBuildEvent>
          250  +      <Command>"$(FrameworkSDKDir)Bin\sn.exe" -R "$(TargetPath)" "$(SolutionDir)System.Data.SQLite\System.Data.SQLite.snk"</Command>
          251  +    </PostBuildEvent>
          252  +  </ItemDefinitionGroup>
          253  +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
          254  +    <PreBuildEvent>
          255  +      <Command>
          256  +      </Command>
          257  +    </PreBuildEvent>
          258  +    <Midl>
          259  +      <TargetEnvironment>X64</TargetEnvironment>
          260  +    </Midl>
          261  +    <ClCompile>
          262  +      <AdditionalOptions>/GS- %(AdditionalOptions)</AdditionalOptions>
          263  +      <Optimization>Disabled</Optimization>
          264  +      <PreprocessorDefinitions>SQLITE_DEBUG;_DEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;WIN64;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          265  +      <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
          266  +      <RuntimeTypeInfo>false</RuntimeTypeInfo>
          267  +      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
          268  +    </ClCompile>
          269  +    <Link>
          270  +      <OutputFile>../bin/x64/$(ProjectName).DLL</OutputFile>
          271  +      <ModuleDefinitionFile>src\sqlite3.def</ModuleDefinitionFile>
          272  +      <EmbedManagedResourceFile>%(EmbedManagedResourceFile)</EmbedManagedResourceFile>
          273  +      <DelayLoadDLLs>advapi32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
          274  +      <GenerateDebugInformation>true</GenerateDebugInformation>
          275  +      <RandomizedBaseAddress>false</RandomizedBaseAddress>
          276  +      <DataExecutionPrevention>
          277  +      </DataExecutionPrevention>
          278  +      <ImportLibrary>
          279  +      </ImportLibrary>
          280  +      <TargetMachine>MachineX64</TargetMachine>
          281  +      <KeyFile>..\System.Data.SQLite\System.Data.SQLite.snk</KeyFile>
          282  +    </Link>
          283  +    <PostBuildEvent>
          284  +      <Command>"$(FrameworkSDKDir).\Bin\sn.exe" -R "$(TargetPath)" "..\System.Data.SQLite\System.Data.SQLite.snk"</Command>
          285  +    </PostBuildEvent>
          286  +  </ItemDefinitionGroup>
          287  +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">
          288  +    <PreBuildEvent>
          289  +      <Command>
          290  +      </Command>
          291  +    </PreBuildEvent>
          292  +    <ClCompile>
          293  +      <AdditionalOptions>/GS- %(AdditionalOptions)</AdditionalOptions>
          294  +      <Optimization>Disabled</Optimization>
          295  +      <PreprocessorDefinitions>WIN32;SQLITE_DEBUG;_DEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;SQLITE_CORE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          296  +      <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
          297  +      <RuntimeTypeInfo>false</RuntimeTypeInfo>
          298  +      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
          299  +    </ClCompile>
          300  +    <Link>
          301  +      <OutputFile>../bin/sqlite3.dll</OutputFile>
          302  +      <ModuleDefinitionFile>src\sqlite3.def</ModuleDefinitionFile>
          303  +      <EmbedManagedResourceFile>%(EmbedManagedResourceFile)</EmbedManagedResourceFile>
          304  +      <DelayLoadDLLs>advapi32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
          305  +      <GenerateDebugInformation>true</GenerateDebugInformation>
          306  +      <RandomizedBaseAddress>false</RandomizedBaseAddress>
          307  +      <DataExecutionPrevention>
          308  +      </DataExecutionPrevention>
          309  +      <ImportLibrary>
          310  +      </ImportLibrary>
          311  +      <TargetMachine>MachineX86</TargetMachine>
          312  +      <KeyFile>..\System.Data.SQLite\System.Data.SQLite.snk</KeyFile>
          313  +      <DelaySign>true</DelaySign>
          314  +    </Link>
          315  +    <PostBuildEvent>
          316  +      <Command>"$(FrameworkSDKDir)Bin\sn.exe" -R "$(TargetPath)" "$(SolutionDir)System.Data.SQLite\System.Data.SQLite.snk"</Command>
          317  +    </PostBuildEvent>
          318  +  </ItemDefinitionGroup>
          319  +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'">
          320  +    <PreBuildEvent>
          321  +      <Command>
          322  +      </Command>
          323  +    </PreBuildEvent>
          324  +    <Midl>
          325  +      <TargetEnvironment>X64</TargetEnvironment>
          326  +    </Midl>
          327  +    <ClCompile>
          328  +      <AdditionalOptions>/GS- %(AdditionalOptions)</AdditionalOptions>
          329  +      <Optimization>Disabled</Optimization>
          330  +      <PreprocessorDefinitions>SQLITE_DEBUG;_DEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_DEPRECATE;NO_TCL;THREADSAFE=1;WIN64;SQLITE_HAS_CODEC;SQLITE_ENABLE_COLUMN_METADATA;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_RTREE;SQLITE_ENABLE_LOAD_EXTENSION;SQLITE_SOUNDEX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          331  +      <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
          332  +      <RuntimeTypeInfo>false</RuntimeTypeInfo>
          333  +      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
          334  +    </ClCompile>
          335  +    <Link>
          336  +      <OutputFile>../bin/x64/$(ProjectName).DLL</OutputFile>
          337  +      <ModuleDefinitionFile>src\sqlite3.def</ModuleDefinitionFile>
          338  +      <EmbedManagedResourceFile>%(EmbedManagedResourceFile)</EmbedManagedResourceFile>
          339  +      <DelayLoadDLLs>advapi32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
          340  +      <GenerateDebugInformation>true</GenerateDebugInformation>
          341  +      <RandomizedBaseAddress>false</RandomizedBaseAddress>
          342  +      <DataExecutionPrevention>
          343  +      </DataExecutionPrevention>
          344  +      <ImportLibrary>
          345  +      </ImportLibrary>
          346  +      <TargetMachine>MachineX64</TargetMachine>
          347  +      <KeyFile>..\System.Data.SQLite\System.Data.SQLite.snk</KeyFile>
          348  +      <DelaySign>true</DelaySign>
          349  +    </Link>
          350  +    <PostBuildEvent>
          351  +      <Command>"$(FrameworkSDKDir).\Bin\sn.exe" -R "$(TargetPath)" "..\System.Data.SQLite\System.Data.SQLite.snk"</Command>
          352  +    </PostBuildEvent>
          353  +  </ItemDefinitionGroup>
          354  +  <ItemGroup>
          355  +    <ClInclude Include="resource.h" />
          356  +    <ClInclude Include="src\sqlite3.h" />
          357  +  </ItemGroup>
          358  +  <ItemGroup>
          359  +    <ClCompile Include="interop.c" />
          360  +  </ItemGroup>
          361  +  <ItemGroup>
          362  +    <EmbeddedResource Include="..\System.Data.SQLite\SR.resx">
          363  +      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
          364  +      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
          365  +      <LogicalName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">System.Data.SQLite.%(Filename).resources</LogicalName>
          366  +      <LogicalName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">System.Data.SQLite.%(Filename).resources</LogicalName>
          367  +      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='StockDebug|Win32'">true</ExcludedFromBuild>
          368  +      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='StockDebug|x64'">true</ExcludedFromBuild>
          369  +    </EmbeddedResource>
          370  +  </ItemGroup>
          371  +  <ItemGroup>
          372  +    <ResourceCompile Include="SQLite.Interop.rc" />
          373  +  </ItemGroup>
          374  +  <ItemGroup>
          375  +    <ProjectReference Include="..\System.Data.SQLite\System.Data.SQLite - ManagedOnly.csproj">
          376  +      <Project>{ac139952-261a-4463-b6fa-aebc25283a66}</Project>
          377  +      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
          378  +    </ProjectReference>
          379  +    <ProjectReference Include="..\System.Data.SQLite\System.Data.SQLite - Netmodule.csproj">
          380  +      <Project>{ac139952-261a-4463-b6fa-aebc25284a66}</Project>
          381  +      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
          382  +    </ProjectReference>
          383  +  </ItemGroup>
          384  +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
          385  +  <ImportGroup Label="ExtensionTargets">
          386  +  </ImportGroup>
          387  +</Project>

Changes to SQLite.Interop/crypt.c.

   346    346     {
   347    347       // Rewrite all the pages in the database using the new encryption key
   348    348       Pgno nPage;
   349    349       Pgno nSkip = PAGER_MJ_PGNO(p);
   350    350       DbPage *pPage;
   351    351       Pgno n;
   352    352   
   353         -    rc = sqlite3PagerPagecount(p, &nPage);
          353  +    sqlite3PagerPagecount(p, &nPage);
   354    354   
   355         -    for(n = 1; rc == SQLITE_OK && n <= nPage; n ++)
          355  +    for(n = 1; n <= nPage; n ++)
   356    356       {
   357    357         if (n == nSkip) continue;
   358    358         rc = sqlite3PagerGet(p, n, &pPage);
   359    359         if(!rc)
   360    360         {
   361    361           rc = sqlite3PagerWrite(pPage);
   362    362           sqlite3PagerUnref(pPage);

Changes to SQLite.Interop/splitsource/alter.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that used to generate VDBE code
    13     13   ** that implements the ALTER TABLE command.
    14         -**
    15         -** $Id: alter.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
    16     14   */
    17     15   #include "sqliteInt.h"
    18         -#include <ctype.h>
    19     16   
    20     17   /*
    21     18   ** The code in this file only exists if we are not omitting the
    22     19   ** ALTER TABLE logic from the build.
    23     20   */
    24     21   #ifndef SQLITE_OMIT_ALTERTABLE
    25     22   
................................................................................
    35     32   **     -> 'CREATE TABLE def(a, b, c)'
    36     33   **
    37     34   ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
    38     35   **     -> 'CREATE INDEX i ON def(a, b, c)'
    39     36   */
    40     37   static void renameTableFunc(
    41     38     sqlite3_context *context,
    42         -  int argc,
           39  +  int NotUsed,
    43     40     sqlite3_value **argv
    44     41   ){
    45     42     unsigned char const *zSql = sqlite3_value_text(argv[0]);
    46     43     unsigned char const *zTableName = sqlite3_value_text(argv[1]);
    47     44   
    48     45     int token;
    49     46     Token tname;
    50     47     unsigned char const *zCsr = zSql;
    51     48     int len = 0;
    52     49     char *zRet;
    53     50   
    54     51     sqlite3 *db = sqlite3_context_db_handle(context);
           52  +
           53  +  UNUSED_PARAMETER(NotUsed);
    55     54   
    56     55     /* The principle used to locate the table name in the CREATE TABLE 
    57     56     ** statement is that the table name is the first non-space token that
    58     57     ** is immediately followed by a TK_LP or TK_USING token.
    59     58     */
    60     59     if( zSql ){
    61     60       do {
    62     61         if( !*zCsr ){
    63     62           /* Ran out of input before finding an opening bracket. Return NULL. */
    64     63           return;
    65     64         }
    66     65   
    67     66         /* Store the token that zCsr points to in tname. */
    68         -      tname.z = zCsr;
           67  +      tname.z = (char*)zCsr;
    69     68         tname.n = len;
    70     69   
    71     70         /* Advance zCsr to the next token. Store that token type in 'token',
    72     71         ** and its length in 'len' (to be used next iteration of this loop).
    73     72         */
    74     73         do {
    75     74           zCsr += len;
    76     75           len = sqlite3GetToken(zCsr, &token);
    77         -      } while( token==TK_SPACE || token==TK_COMMENT );
           76  +      } while( token==TK_SPACE );
    78     77         assert( len>0 );
    79     78       } while( token!=TK_LP && token!=TK_USING );
    80     79   
    81         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
           80  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
    82     81          zTableName, tname.z+tname.n);
    83     82       sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
    84     83     }
    85     84   }
    86     85   
           86  +/*
           87  +** This C function implements an SQL user function that is used by SQL code
           88  +** generated by the ALTER TABLE ... RENAME command to modify the definition
           89  +** of any foreign key constraints that use the table being renamed as the 
           90  +** parent table. It is passed three arguments:
           91  +**
           92  +**   1) The complete text of the CREATE TABLE statement being modified,
           93  +**   2) The old name of the table being renamed, and
           94  +**   3) The new name of the table being renamed.
           95  +**
           96  +** It returns the new CREATE TABLE statement. For example:
           97  +**
           98  +**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
           99  +**       -> 'CREATE TABLE t1(a REFERENCES t3)'
          100  +*/
          101  +#ifndef SQLITE_OMIT_FOREIGN_KEY
          102  +static void renameParentFunc(
          103  +  sqlite3_context *context,
          104  +  int NotUsed,
          105  +  sqlite3_value **argv
          106  +){
          107  +  sqlite3 *db = sqlite3_context_db_handle(context);
          108  +  char *zOutput = 0;
          109  +  char *zResult;
          110  +  unsigned char const *zInput = sqlite3_value_text(argv[0]);
          111  +  unsigned char const *zOld = sqlite3_value_text(argv[1]);
          112  +  unsigned char const *zNew = sqlite3_value_text(argv[2]);
          113  +
          114  +  unsigned const char *z;         /* Pointer to token */
          115  +  int n;                          /* Length of token z */
          116  +  int token;                      /* Type of token */
          117  +
          118  +  UNUSED_PARAMETER(NotUsed);
          119  +  for(z=zInput; *z; z=z+n){
          120  +    n = sqlite3GetToken(z, &token);
          121  +    if( token==TK_REFERENCES ){
          122  +      char *zParent;
          123  +      do {
          124  +        z += n;
          125  +        n = sqlite3GetToken(z, &token);
          126  +      }while( token==TK_SPACE );
          127  +
          128  +      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
          129  +      if( zParent==0 ) break;
          130  +      sqlite3Dequote(zParent);
          131  +      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
          132  +        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
          133  +            (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
          134  +        );
          135  +        sqlite3DbFree(db, zOutput);
          136  +        zOutput = zOut;
          137  +        zInput = &z[n];
          138  +      }
          139  +      sqlite3DbFree(db, zParent);
          140  +    }
          141  +  }
          142  +
          143  +  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
          144  +  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
          145  +  sqlite3DbFree(db, zOutput);
          146  +}
          147  +#endif
          148  +
    87    149   #ifndef SQLITE_OMIT_TRIGGER
    88    150   /* This function is used by SQL generated to implement the
    89    151   ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
    90    152   ** statement. The second is a table name. The table name in the CREATE 
    91    153   ** TRIGGER statement is replaced with the third argument and the result 
    92    154   ** returned. This is analagous to renameTableFunc() above, except for CREATE
    93    155   ** TRIGGER, not CREATE INDEX and CREATE TABLE.
    94    156   */
    95    157   static void renameTriggerFunc(
    96    158     sqlite3_context *context,
    97         -  int argc,
          159  +  int NotUsed,
    98    160     sqlite3_value **argv
    99    161   ){
   100    162     unsigned char const *zSql = sqlite3_value_text(argv[0]);
   101    163     unsigned char const *zTableName = sqlite3_value_text(argv[1]);
   102    164   
   103    165     int token;
   104    166     Token tname;
   105    167     int dist = 3;
   106    168     unsigned char const *zCsr = zSql;
   107    169     int len = 0;
   108    170     char *zRet;
   109         -
   110    171     sqlite3 *db = sqlite3_context_db_handle(context);
          172  +
          173  +  UNUSED_PARAMETER(NotUsed);
   111    174   
   112    175     /* The principle used to locate the table name in the CREATE TRIGGER 
   113    176     ** statement is that the table name is the first token that is immediatedly
   114    177     ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
   115    178     ** of TK_WHEN, TK_BEGIN or TK_FOR.
   116    179     */
   117    180     if( zSql ){
................................................................................
   119    182   
   120    183         if( !*zCsr ){
   121    184           /* Ran out of input before finding the table name. Return NULL. */
   122    185           return;
   123    186         }
   124    187   
   125    188         /* Store the token that zCsr points to in tname. */
   126         -      tname.z = zCsr;
          189  +      tname.z = (char*)zCsr;
   127    190         tname.n = len;
   128    191   
   129    192         /* Advance zCsr to the next token. Store that token type in 'token',
   130    193         ** and its length in 'len' (to be used next iteration of this loop).
   131    194         */
   132    195         do {
   133    196           zCsr += len;
................................................................................
   149    212           dist = 0;
   150    213         }
   151    214       } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
   152    215   
   153    216       /* Variable tname now contains the token that is the old table-name
   154    217       ** in the CREATE TRIGGER statement.
   155    218       */
   156         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
          219  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
   157    220          zTableName, tname.z+tname.n);
   158    221       sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
   159    222     }
   160    223   }
   161    224   #endif   /* !SQLITE_OMIT_TRIGGER */
   162    225   
   163    226   /*
   164    227   ** Register built-in functions used to help implement ALTER TABLE
   165    228   */
   166         -void sqlite3AlterFunctions(sqlite3 *db){
   167         -  sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
   168         -                         renameTableFunc, 0, 0);
          229  +void sqlite3AlterFunctions(void){
          230  +  static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
          231  +    FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
   169    232   #ifndef SQLITE_OMIT_TRIGGER
   170         -  sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
   171         -                         renameTriggerFunc, 0, 0);
          233  +    FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
   172    234   #endif
          235  +#ifndef SQLITE_OMIT_FOREIGN_KEY
          236  +    FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
          237  +#endif
          238  +  };
          239  +  int i;
          240  +  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
          241  +  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
          242  +
          243  +  for(i=0; i<ArraySize(aAlterTableFuncs); i++){
          244  +    sqlite3FuncDefInsert(pHash, &aFunc[i]);
          245  +  }
          246  +}
          247  +
          248  +/*
          249  +** This function is used to create the text of expressions of the form:
          250  +**
          251  +**   name=<constant1> OR name=<constant2> OR ...
          252  +**
          253  +** If argument zWhere is NULL, then a pointer string containing the text 
          254  +** "name=<constant>" is returned, where <constant> is the quoted version
          255  +** of the string passed as argument zConstant. The returned buffer is
          256  +** allocated using sqlite3DbMalloc(). It is the responsibility of the
          257  +** caller to ensure that it is eventually freed.
          258  +**
          259  +** If argument zWhere is not NULL, then the string returned is 
          260  +** "<where> OR name=<constant>", where <where> is the contents of zWhere.
          261  +** In this case zWhere is passed to sqlite3DbFree() before returning.
          262  +** 
          263  +*/
          264  +static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
          265  +  char *zNew;
          266  +  if( !zWhere ){
          267  +    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
          268  +  }else{
          269  +    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
          270  +    sqlite3DbFree(db, zWhere);
          271  +  }
          272  +  return zNew;
          273  +}
          274  +
          275  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
          276  +/*
          277  +** Generate the text of a WHERE expression which can be used to select all
          278  +** tables that have foreign key constraints that refer to table pTab (i.e.
          279  +** constraints for which pTab is the parent table) from the sqlite_master
          280  +** table.
          281  +*/
          282  +static char *whereForeignKeys(Parse *pParse, Table *pTab){
          283  +  FKey *p;
          284  +  char *zWhere = 0;
          285  +  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
          286  +    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
          287  +  }
          288  +  return zWhere;
   173    289   }
          290  +#endif
   174    291   
   175    292   /*
   176    293   ** Generate the text of a WHERE expression which can be used to select all
   177    294   ** temporary triggers on table pTab from the sqlite_temp_master table. If
   178    295   ** table pTab has no temporary triggers, or is itself stored in the 
   179    296   ** temporary database, NULL is returned.
   180    297   */
   181    298   static char *whereTempTriggers(Parse *pParse, Table *pTab){
   182    299     Trigger *pTrig;
   183    300     char *zWhere = 0;
   184         -  char *tmp = 0;
   185    301     const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
   186    302   
   187    303     /* If the table is not located in the temp-db (in which case NULL is 
   188    304     ** returned, loop through the tables list of triggers. For each trigger
   189    305     ** that is not part of the temp-db schema, add a clause to the WHERE 
   190    306     ** expression being built up in zWhere.
   191    307     */
   192    308     if( pTab->pSchema!=pTempSchema ){
   193    309       sqlite3 *db = pParse->db;
   194         -    for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
          310  +    for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
   195    311         if( pTrig->pSchema==pTempSchema ){
   196         -        if( !zWhere ){
   197         -          zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
   198         -        }else{
   199         -          tmp = zWhere;
   200         -          zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
   201         -          sqlite3DbFree(db, tmp);
   202         -        }
          312  +        zWhere = whereOrName(db, zWhere, pTrig->zName);
   203    313         }
   204    314       }
   205    315     }
          316  +  if( zWhere ){
          317  +    char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
          318  +    sqlite3DbFree(pParse->db, zWhere);
          319  +    zWhere = zNew;
          320  +  }
   206    321     return zWhere;
   207    322   }
   208    323   
   209    324   /*
   210    325   ** Generate code to drop and reload the internal representation of table
   211    326   ** pTab from the database, including triggers and temporary triggers.
   212    327   ** Argument zName is the name of the table in the database schema at
................................................................................
   219    334     char *zWhere;
   220    335     int iDb;                   /* Index of database containing pTab */
   221    336   #ifndef SQLITE_OMIT_TRIGGER
   222    337     Trigger *pTrig;
   223    338   #endif
   224    339   
   225    340     v = sqlite3GetVdbe(pParse);
   226         -  if( !v ) return;
          341  +  if( NEVER(v==0) ) return;
   227    342     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   228    343     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   229    344     assert( iDb>=0 );
   230    345   
   231    346   #ifndef SQLITE_OMIT_TRIGGER
   232    347     /* Drop any table triggers from the internal schema. */
   233         -  for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
          348  +  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
   234    349       int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
   235    350       assert( iTrigDb==iDb || iTrigDb==1 );
   236         -    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->name, 0);
          351  +    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
   237    352     }
   238    353   #endif
   239    354   
   240         -  /* Drop the table and index from the internal schema */
          355  +  /* Drop the table and index from the internal schema.  */
   241    356     sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
   242    357   
   243    358     /* Reload the table, index and permanent trigger schemas. */
   244    359     zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
   245    360     if( !zWhere ) return;
   246    361     sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
   247    362   
................................................................................
   271    386     sqlite3 *db = pParse->db; /* Database connection */
   272    387     int nTabName;             /* Number of UTF-8 characters in zTabName */
   273    388     const char *zTabName;     /* Original name of the table */
   274    389     Vdbe *v;
   275    390   #ifndef SQLITE_OMIT_TRIGGER
   276    391     char *zWhere = 0;         /* Where clause to locate temp triggers */
   277    392   #endif
   278         -  int isVirtualRename = 0;  /* True if this is a v-table with an xRename() */
   279         -  
   280         -  if( db->mallocFailed ) goto exit_rename_table;
          393  +  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
          394  +  int savedDbFlags;         /* Saved value of db->flags */
          395  +
          396  +  savedDbFlags = db->flags;  
          397  +  if( NEVER(db->mallocFailed) ) goto exit_rename_table;
   281    398     assert( pSrc->nSrc==1 );
   282    399     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   283    400   
   284    401     pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   285    402     if( !pTab ) goto exit_rename_table;
   286    403     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   287    404     zDb = db->aDb[iDb].zName;
          405  +  db->flags |= SQLITE_PreferBuiltin;
   288    406   
   289    407     /* Get a NULL terminated version of the new table name. */
   290    408     zName = sqlite3NameFromToken(db, pName);
   291    409     if( !zName ) goto exit_rename_table;
   292    410   
   293    411     /* Check that a table or index named 'zName' does not already exist
   294    412     ** in database iDb. If so, this is an error.
................................................................................
   298    416           "there is already another table or index with this name: %s", zName);
   299    417       goto exit_rename_table;
   300    418     }
   301    419   
   302    420     /* Make sure it is not a system table being altered, or a reserved name
   303    421     ** that the table is being renamed to.
   304    422     */
   305         -  if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
          423  +  if( sqlite3Strlen30(pTab->zName)>6 
          424  +   && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
          425  +  ){
   306    426       sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
   307    427       goto exit_rename_table;
   308    428     }
   309    429     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   310    430       goto exit_rename_table;
   311    431     }
   312    432   
................................................................................
   324    444     }
   325    445   #endif
   326    446   
   327    447   #ifndef SQLITE_OMIT_VIRTUALTABLE
   328    448     if( sqlite3ViewGetColumnNames(pParse, pTab) ){
   329    449       goto exit_rename_table;
   330    450     }
   331         -  if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
   332         -    isVirtualRename = 1;
          451  +  if( IsVirtual(pTab) ){
          452  +    pVTab = sqlite3GetVTable(db, pTab);
          453  +    if( pVTab->pVtab->pModule->xRename==0 ){
          454  +      pVTab = 0;
          455  +    }
   333    456     }
   334    457   #endif
   335    458   
   336    459     /* Begin a transaction and code the VerifyCookie for database iDb. 
   337    460     ** Then modify the schema cookie (since the ALTER TABLE modifies the
   338    461     ** schema). Open a statement transaction if the table is a virtual
   339    462     ** table.
   340    463     */
   341    464     v = sqlite3GetVdbe(pParse);
   342    465     if( v==0 ){
   343    466       goto exit_rename_table;
   344    467     }
   345         -  sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
          468  +  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
   346    469     sqlite3ChangeCookie(pParse, iDb);
   347    470   
   348    471     /* If this is a virtual table, invoke the xRename() function if
   349    472     ** one is defined. The xRename() callback will modify the names
   350    473     ** of any resources used by the v-table implementation (including other
   351    474     ** SQLite tables) that are identified by the name of the virtual table.
   352    475     */
   353    476   #ifndef SQLITE_OMIT_VIRTUALTABLE
   354         -  if( isVirtualRename ){
          477  +  if( pVTab ){
   355    478       int i = ++pParse->nMem;
   356    479       sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
   357         -    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pTab->pVtab, P4_VTAB);
          480  +    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
          481  +    sqlite3MayAbort(pParse);
   358    482     }
   359    483   #endif
   360    484   
   361    485     /* figure out how many UTF-8 characters are in zName */
   362    486     zTabName = pTab->zName;
   363    487     nTabName = sqlite3Utf8CharLen(zTabName, -1);
          488  +
          489  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
          490  +  if( db->flags&SQLITE_ForeignKeys ){
          491  +    /* If foreign-key support is enabled, rewrite the CREATE TABLE 
          492  +    ** statements corresponding to all child tables of foreign key constraints
          493  +    ** for which the renamed table is the parent table.  */
          494  +    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
          495  +      sqlite3NestedParse(pParse, 
          496  +          "UPDATE \"%w\".%s SET "
          497  +              "sql = sqlite_rename_parent(sql, %Q, %Q) "
          498  +              "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
          499  +      sqlite3DbFree(db, zWhere);
          500  +    }
          501  +  }
          502  +#endif
   364    503   
   365    504     /* Modify the sqlite_master table to use the new table name. */
   366    505     sqlite3NestedParse(pParse,
   367    506         "UPDATE %Q.%s SET "
   368    507   #ifdef SQLITE_OMIT_TRIGGER
   369    508             "sql = sqlite_rename_table(sql, %Q), "
   370    509   #else
................................................................................
   408    547           "UPDATE sqlite_temp_master SET "
   409    548               "sql = sqlite_rename_trigger(sql, %Q), "
   410    549               "tbl_name = %Q "
   411    550               "WHERE %s;", zName, zName, zWhere);
   412    551       sqlite3DbFree(db, zWhere);
   413    552     }
   414    553   #endif
          554  +
          555  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
          556  +  if( db->flags&SQLITE_ForeignKeys ){
          557  +    FKey *p;
          558  +    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
          559  +      Table *pFrom = p->pFrom;
          560  +      if( pFrom!=pTab ){
          561  +        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
          562  +      }
          563  +    }
          564  +  }
          565  +#endif
   415    566   
   416    567     /* Drop and reload the internal table schema. */
   417    568     reloadTableSchema(pParse, pTab, zName);
   418    569   
   419    570   exit_rename_table:
   420    571     sqlite3SrcListDelete(db, pSrc);
   421    572     sqlite3DbFree(db, zName);
          573  +  db->flags = savedDbFlags;
   422    574   }
   423    575   
          576  +
          577  +/*
          578  +** Generate code to make sure the file format number is at least minFormat.
          579  +** The generated code will increase the file format number if necessary.
          580  +*/
          581  +void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
          582  +  Vdbe *v;
          583  +  v = sqlite3GetVdbe(pParse);
          584  +  /* The VDBE should have been allocated before this routine is called.
          585  +  ** If that allocation failed, we would have quit before reaching this
          586  +  ** point */
          587  +  if( ALWAYS(v) ){
          588  +    int r1 = sqlite3GetTempReg(pParse);
          589  +    int r2 = sqlite3GetTempReg(pParse);
          590  +    int j1;
          591  +    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
          592  +    sqlite3VdbeUsesBtree(v, iDb);
          593  +    sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
          594  +    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
          595  +    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
          596  +    sqlite3VdbeJumpHere(v, j1);
          597  +    sqlite3ReleaseTempReg(pParse, r1);
          598  +    sqlite3ReleaseTempReg(pParse, r2);
          599  +  }
          600  +}
   424    601   
   425    602   /*
   426    603   ** This function is called after an "ALTER TABLE ... ADD" statement
   427    604   ** has been parsed. Argument pColDef contains the text of the new
   428    605   ** column definition.
   429    606   **
   430    607   ** The Table structure pParse->pNewTable was extended to include
................................................................................
   437    614     const char *zDb;          /* Database name */
   438    615     const char *zTab;         /* Table name */
   439    616     char *zCol;               /* Null-terminated column definition */
   440    617     Column *pCol;             /* The new column */
   441    618     Expr *pDflt;              /* Default value for the new column */
   442    619     sqlite3 *db;              /* The database connection; */
   443    620   
   444         -  if( pParse->nErr ) return;
          621  +  db = pParse->db;
          622  +  if( pParse->nErr || db->mallocFailed ) return;
   445    623     pNew = pParse->pNewTable;
   446    624     assert( pNew );
   447    625   
   448         -  db = pParse->db;
   449    626     assert( sqlite3BtreeHoldsAllMutexes(db) );
   450    627     iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
   451    628     zDb = db->aDb[iDb].zName;
   452         -  zTab = pNew->zName;
          629  +  zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
   453    630     pCol = &pNew->aCol[pNew->nCol-1];
   454    631     pDflt = pCol->pDflt;
   455    632     pTab = sqlite3FindTable(db, zTab, zDb);
   456    633     assert( pTab );
   457    634   
   458    635   #ifndef SQLITE_OMIT_AUTHORIZATION
   459    636     /* Invoke the authorization callback. */
................................................................................
   477    654     if( pCol->isPrimKey ){
   478    655       sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
   479    656       return;
   480    657     }
   481    658     if( pNew->pIndex ){
   482    659       sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
   483    660       return;
          661  +  }
          662  +  if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
          663  +    sqlite3ErrorMsg(pParse, 
          664  +        "Cannot add a REFERENCES column with non-NULL default value");
          665  +    return;
   484    666     }
   485    667     if( pCol->notNull && !pDflt ){
   486    668       sqlite3ErrorMsg(pParse, 
   487    669           "Cannot add a NOT NULL column with default value NULL");
   488    670       return;
   489    671     }
   490    672   
................................................................................
   504    686       sqlite3ValueFree(pVal);
   505    687     }
   506    688   
   507    689     /* Modify the CREATE TABLE statement. */
   508    690     zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
   509    691     if( zCol ){
   510    692       char *zEnd = &zCol[pColDef->n-1];
   511         -    while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
          693  +    int savedDbFlags = db->flags;
          694  +    while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
   512    695         *zEnd-- = '\0';
   513    696       }
          697  +    db->flags |= SQLITE_PreferBuiltin;
   514    698       sqlite3NestedParse(pParse, 
   515    699           "UPDATE \"%w\".%s SET "
   516    700             "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
   517    701           "WHERE type = 'table' AND name = %Q", 
   518    702         zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
   519    703         zTab
   520    704       );
   521    705       sqlite3DbFree(db, zCol);
          706  +    db->flags = savedDbFlags;
   522    707     }
   523    708   
   524    709     /* If the default value of the new column is NULL, then set the file
   525    710     ** format to 2. If the default value of the new column is not NULL,
   526    711     ** the file format becomes 3.
   527    712     */
   528    713     sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
................................................................................
   575    760       goto exit_begin_add_column;
   576    761     }
   577    762   
   578    763     assert( pTab->addColOffset>0 );
   579    764     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   580    765   
   581    766     /* Put a copy of the Table struct in Parse.pNewTable for the
   582         -  ** sqlite3AddColumn() function and friends to modify.
          767  +  ** sqlite3AddColumn() function and friends to modify.  But modify
          768  +  ** the name by adding an "sqlite_altertab_" prefix.  By adding this
          769  +  ** prefix, we insure that the name will not collide with an existing
          770  +  ** table because user table are not allowed to have the "sqlite_"
          771  +  ** prefix on their name.
   583    772     */
   584    773     pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
   585    774     if( !pNew ) goto exit_begin_add_column;
   586    775     pParse->pNewTable = pNew;
   587    776     pNew->nRef = 1;
   588         -  pNew->db = db;
   589    777     pNew->nCol = pTab->nCol;
   590    778     assert( pNew->nCol>0 );
   591    779     nAlloc = (((pNew->nCol-1)/8)*8)+8;
   592    780     assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
   593    781     pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
   594         -  pNew->zName = sqlite3DbStrDup(db, pTab->zName);
          782  +  pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
   595    783     if( !pNew->aCol || !pNew->zName ){
   596    784       db->mallocFailed = 1;
   597    785       goto exit_begin_add_column;
   598    786     }
   599    787     memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
   600    788     for(i=0; i<pNew->nCol; i++){
   601    789       Column *pCol = &pNew->aCol[i];
   602    790       pCol->zName = sqlite3DbStrDup(db, pCol->zName);
   603    791       pCol->zColl = 0;
   604    792       pCol->zType = 0;
   605    793       pCol->pDflt = 0;
          794  +    pCol->zDflt = 0;
   606    795     }
   607    796     pNew->pSchema = db->aDb[iDb].pSchema;
   608    797     pNew->addColOffset = pTab->addColOffset;
   609    798     pNew->nRef = 1;
   610    799   
   611    800     /* Begin a transaction and increment the schema cookie.  */
   612    801     sqlite3BeginWriteOperation(pParse, 0, iDb);

Changes to SQLite.Interop/splitsource/analyze.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code associated with the ANALYZE command.
    13         -**
    14         -** @(#) $Id: analyze.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
    15     13   */
    16     14   #ifndef SQLITE_OMIT_ANALYZE
    17     15   #include "sqliteInt.h"
    18     16   
    19     17   /*
    20         -** This routine generates code that opens the sqlite_stat1 table on cursor
    21         -** iStatCur.
           18  +** This routine generates code that opens the sqlite_stat1 table for
           19  +** writing with cursor iStatCur. If the library was built with the
           20  +** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
           21  +** opened for writing using cursor (iStatCur+1)
    22     22   **
    23     23   ** If the sqlite_stat1 tables does not previously exist, it is created.
    24         -** If it does previously exist, all entires associated with table zWhere
    25         -** are removed.  If zWhere==0 then all entries are removed.
           24  +** Similarly, if the sqlite_stat2 table does not exist and the library
           25  +** is compiled with SQLITE_ENABLE_STAT2 defined, it is created. 
           26  +**
           27  +** Argument zWhere may be a pointer to a buffer containing a table name,
           28  +** or it may be a NULL pointer. If it is not NULL, then all entries in
           29  +** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
           30  +** with the named table are deleted. If zWhere==0, then code is generated
           31  +** to delete all stat table entries.
    26     32   */
    27     33   static void openStatTable(
    28     34     Parse *pParse,          /* Parsing context */
    29     35     int iDb,                /* The database we are looking in */
    30     36     int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
    31     37     const char *zWhere      /* Delete entries associated with this table */
    32     38   ){
           39  +  static const struct {
           40  +    const char *zName;
           41  +    const char *zCols;
           42  +  } aTable[] = {
           43  +    { "sqlite_stat1", "tbl,idx,stat" },
           44  +#ifdef SQLITE_ENABLE_STAT2
           45  +    { "sqlite_stat2", "tbl,idx,sampleno,sample" },
           46  +#endif
           47  +  };
           48  +
           49  +  int aRoot[] = {0, 0};
           50  +  u8 aCreateTbl[] = {0, 0};
           51  +
           52  +  int i;
    33     53     sqlite3 *db = pParse->db;
    34     54     Db *pDb;
    35         -  int iRootPage;
    36         -  int createStat1 = 0;
    37         -  Table *pStat;
    38     55     Vdbe *v = sqlite3GetVdbe(pParse);
    39         -
    40     56     if( v==0 ) return;
    41     57     assert( sqlite3BtreeHoldsAllMutexes(db) );
    42     58     assert( sqlite3VdbeDb(v)==db );
    43     59     pDb = &db->aDb[iDb];
    44         -  if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){
    45         -    /* The sqlite_stat1 tables does not exist.  Create it.  
    46         -    ** Note that a side-effect of the CREATE TABLE statement is to leave
    47         -    ** the rootpage of the new table in register pParse->regRoot.  This is
    48         -    ** important because the OpenWrite opcode below will be needing it. */
    49         -    sqlite3NestedParse(pParse,
    50         -      "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)",
    51         -      pDb->zName
    52         -    );
    53         -    iRootPage = pParse->regRoot;
    54         -    createStat1 = 1;  /* Cause rootpage to be taken from top of stack */
    55         -  }else if( zWhere ){
    56         -    /* The sqlite_stat1 table exists.  Delete all entries associated with
    57         -    ** the table zWhere. */
    58         -    sqlite3NestedParse(pParse,
    59         -       "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q",
    60         -       pDb->zName, zWhere
    61         -    );
    62         -    iRootPage = pStat->tnum;
    63         -  }else{
    64         -    /* The sqlite_stat1 table already exists.  Delete all rows. */
    65         -    iRootPage = pStat->tnum;
    66         -    sqlite3VdbeAddOp2(v, OP_Clear, pStat->tnum, iDb);
    67         -  }
    68         -
    69         -  /* Open the sqlite_stat1 table for writing. Unless it was created
    70         -  ** by this vdbe program, lock it for writing at the shared-cache level. 
    71         -  ** If this vdbe did create the sqlite_stat1 table, then it must have 
    72         -  ** already obtained a schema-lock, making the write-lock redundant.
    73         -  */
    74         -  if( !createStat1 ){
    75         -    sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1");
    76         -  }
    77         -  sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, 3);
    78         -  sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur, iRootPage, iDb);
    79         -  sqlite3VdbeChangeP5(v, createStat1);
           60  +
           61  +  for(i=0; i<ArraySize(aTable); i++){
           62  +    const char *zTab = aTable[i].zName;
           63  +    Table *pStat;
           64  +    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
           65  +      /* The sqlite_stat[12] table does not exist. Create it. Note that a 
           66  +      ** side-effect of the CREATE TABLE statement is to leave the rootpage 
           67  +      ** of the new table in register pParse->regRoot. This is important 
           68  +      ** because the OpenWrite opcode below will be needing it. */
           69  +      sqlite3NestedParse(pParse,
           70  +          "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
           71  +      );
           72  +      aRoot[i] = pParse->regRoot;
           73  +      aCreateTbl[i] = 1;
           74  +    }else{
           75  +      /* The table already exists. If zWhere is not NULL, delete all entries 
           76  +      ** associated with the table zWhere. If zWhere is NULL, delete the
           77  +      ** entire contents of the table. */
           78  +      aRoot[i] = pStat->tnum;
           79  +      sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
           80  +      if( zWhere ){
           81  +        sqlite3NestedParse(pParse,
           82  +           "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere
           83  +        );
           84  +      }else{
           85  +        /* The sqlite_stat[12] table already exists.  Delete all rows. */
           86  +        sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
           87  +      }
           88  +    }
           89  +  }
           90  +
           91  +  /* Open the sqlite_stat[12] tables for writing. */
           92  +  for(i=0; i<ArraySize(aTable); i++){
           93  +    sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
           94  +    sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
           95  +    sqlite3VdbeChangeP5(v, aCreateTbl[i]);
           96  +  }
    80     97   }
    81     98   
    82     99   /*
    83    100   ** Generate code to do an analysis of all indices associated with
    84    101   ** a single table.
    85    102   */
    86    103   static void analyzeOneTable(
    87    104     Parse *pParse,   /* Parser context */
    88    105     Table *pTab,     /* Table whose indices are to be analyzed */
    89         -  int iStatCur,    /* Cursor that writes to the sqlite_stat1 table */
          106  +  int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
    90    107     int iMem         /* Available memory locations begin here */
    91    108   ){
    92         -  Index *pIdx;     /* An index to being analyzed */
    93         -  int iIdxCur;     /* Cursor number for index being analyzed */
    94         -  int nCol;        /* Number of columns in the index */
    95         -  Vdbe *v;         /* The virtual machine being built up */
    96         -  int i;           /* Loop counter */
    97         -  int topOfLoop;   /* The top of the loop */
    98         -  int endOfLoop;   /* The end of the loop */
    99         -  int addr;        /* The address of an instruction */
   100         -  int iDb;         /* Index of database containing pTab */
          109  +  sqlite3 *db = pParse->db;    /* Database handle */
          110  +  Index *pIdx;                 /* An index to being analyzed */
          111  +  int iIdxCur;                 /* Cursor open on index being analyzed */
          112  +  Vdbe *v;                     /* The virtual machine being built up */
          113  +  int i;                       /* Loop counter */
          114  +  int topOfLoop;               /* The top of the loop */
          115  +  int endOfLoop;               /* The end of the loop */
          116  +  int addr = 0;                /* The address of an instruction */
          117  +  int jZeroRows = 0;           /* Jump from here if number of rows is zero */
          118  +  int iDb;                     /* Index of database containing pTab */
          119  +  int regTabname = iMem++;     /* Register containing table name */
          120  +  int regIdxname = iMem++;     /* Register containing index name */
          121  +  int regSampleno = iMem++;    /* Register containing next sample number */
          122  +  int regCol = iMem++;         /* Content of a column analyzed table */
          123  +  int regRec = iMem++;         /* Register holding completed record */
          124  +  int regTemp = iMem++;        /* Temporary use register */
          125  +  int regRowid = iMem++;       /* Rowid for the inserted record */
          126  +
          127  +#ifdef SQLITE_ENABLE_STAT2
          128  +  int regTemp2 = iMem++;       /* Temporary use register */
          129  +  int regSamplerecno = iMem++; /* Index of next sample to record */
          130  +  int regRecno = iMem++;       /* Current sample index */
          131  +  int regLast = iMem++;        /* Index of last sample to record */
          132  +  int regFirst = iMem++;       /* Index of first sample to record */
          133  +#endif
   101    134   
   102    135     v = sqlite3GetVdbe(pParse);
   103         -  if( v==0 || pTab==0 || pTab->pIndex==0 ){
   104         -    /* Do no analysis for tables that have no indices */
          136  +  if( v==0 || NEVER(pTab==0) ){
          137  +    return;
          138  +  }
          139  +  if( pTab->tnum==0 ){
          140  +    /* Do not gather statistics on views or virtual tables */
          141  +    return;
          142  +  }
          143  +  if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
          144  +    /* Do not gather statistics on system tables */
   105    145       return;
   106    146     }
   107         -  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   108         -  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
          147  +  assert( sqlite3BtreeHoldsAllMutexes(db) );
          148  +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   109    149     assert( iDb>=0 );
   110    150   #ifndef SQLITE_OMIT_AUTHORIZATION
   111    151     if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
   112         -      pParse->db->aDb[iDb].zName ) ){
          152  +      db->aDb[iDb].zName ) ){
   113    153       return;
   114    154     }
   115    155   #endif
   116    156   
   117    157     /* Establish a read-lock on the table at the shared-cache level. */
   118    158     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
   119    159   
   120         -  iIdxCur = pParse->nTab;
          160  +  iIdxCur = pParse->nTab++;
          161  +  sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
   121    162     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
          163  +    int nCol = pIdx->nColumn;
   122    164       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   123         -    int regFields;    /* Register block for building records */
   124         -    int regRec;       /* Register holding completed record */
   125         -    int regTemp;      /* Temporary use register */
   126         -    int regCol;       /* Content of a column from the table being analyzed */
   127         -    int regRowid;     /* Rowid for the inserted record */
   128         -    int regF2;
   129    165   
   130         -    /* Open a cursor to the index to be analyzed
   131         -    */
   132         -    assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
   133         -    nCol = pIdx->nColumn;
   134         -    sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, nCol+1);
          166  +    if( iMem+1+(nCol*2)>pParse->nMem ){
          167  +      pParse->nMem = iMem+1+(nCol*2);
          168  +    }
          169  +
          170  +    /* Open a cursor to the index to be analyzed. */
          171  +    assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
   135    172       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
   136    173           (char *)pKey, P4_KEYINFO_HANDOFF);
   137    174       VdbeComment((v, "%s", pIdx->zName));
   138         -    regFields = iMem+nCol*2;
   139         -    regTemp = regRowid = regCol = regFields+3;
   140         -    regRec = regCol+1;
   141         -    if( regRec>pParse->nMem ){
   142         -      pParse->nMem = regRec;
          175  +
          176  +    /* Populate the register containing the index name. */
          177  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
          178  +
          179  +#ifdef SQLITE_ENABLE_STAT2
          180  +
          181  +    /* If this iteration of the loop is generating code to analyze the
          182  +    ** first index in the pTab->pIndex list, then register regLast has
          183  +    ** not been populated. In this case populate it now.  */
          184  +    if( pTab->pIndex==pIdx ){
          185  +      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
          186  +      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
          187  +      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
          188  +
          189  +      sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
          190  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
          191  +      addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
          192  +      sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
          193  +      sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
          194  +      sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
          195  +      sqlite3VdbeAddOp3(v, OP_Divide,  regTemp2, regLast, regLast);
          196  +      sqlite3VdbeJumpHere(v, addr);
   143    197       }
   144    198   
   145         -    /* Memory cells are used as follows:
          199  +    /* Zero the regSampleno and regRecno registers. */
          200  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
          201  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
          202  +    sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
          203  +#endif
          204  +
          205  +    /* The block of memory cells initialized here is used as follows.
          206  +    **
          207  +    **    iMem:                
          208  +    **        The total number of rows in the table.
          209  +    **
          210  +    **    iMem+1 .. iMem+nCol: 
          211  +    **        Number of distinct entries in index considering the 
          212  +    **        left-most N columns only, where N is between 1 and nCol, 
          213  +    **        inclusive.
   146    214       **
   147         -    **    mem[iMem]:             The total number of rows in the table.
   148         -    **    mem[iMem+1]:           Number of distinct values in column 1
   149         -    **    ...
   150         -    **    mem[iMem+nCol]:        Number of distinct values in column N
   151         -    **    mem[iMem+nCol+1]       Last observed value of column 1
   152         -    **    ...
   153         -    **    mem[iMem+nCol+nCol]:   Last observed value of column N
          215  +    **    iMem+nCol+1 .. Mem+2*nCol:  
          216  +    **        Previous value of indexed columns, from left to right.
   154    217       **
   155         -    ** Cells iMem through iMem+nCol are initialized to 0.  The others
   156         -    ** are initialized to NULL.
          218  +    ** Cells iMem through iMem+nCol are initialized to 0. The others are 
          219  +    ** initialized to contain an SQL NULL.
   157    220       */
   158    221       for(i=0; i<=nCol; i++){
   159    222         sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
   160    223       }
   161    224       for(i=0; i<nCol; i++){
   162    225         sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
   163    226       }
   164    227   
   165         -    /* Do the analysis.
   166         -    */
          228  +    /* Start the analysis loop. This loop runs through all the entries in
          229  +    ** the index b-tree.  */
   167    230       endOfLoop = sqlite3VdbeMakeLabel(v);
   168    231       sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
   169    232       topOfLoop = sqlite3VdbeCurrentAddr(v);
   170    233       sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
          234  +
   171    235       for(i=0; i<nCol; i++){
   172    236         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
          237  +#ifdef SQLITE_ENABLE_STAT2
          238  +      if( i==0 ){
          239  +        /* Check if the record that cursor iIdxCur points to contains a
          240  +        ** value that should be stored in the sqlite_stat2 table. If so,
          241  +        ** store it.  */
          242  +        int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
          243  +        assert( regTabname+1==regIdxname 
          244  +             && regTabname+2==regSampleno
          245  +             && regTabname+3==regCol
          246  +        );
          247  +        sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
          248  +        sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
          249  +        sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
          250  +        sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
          251  +
          252  +        /* Calculate new values for regSamplerecno and regSampleno.
          253  +        **
          254  +        **   sampleno = sampleno + 1
          255  +        **   samplerecno = samplerecno+(remaining records)/(remaining samples)
          256  +        */
          257  +        sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
          258  +        sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
          259  +        sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
          260  +        sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
          261  +        sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
          262  +        sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
          263  +        sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
          264  +
          265  +        sqlite3VdbeJumpHere(v, ne);
          266  +        sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
          267  +      }
          268  +#endif
          269  +
   173    270         sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
   174    271         /**** TODO:  add collating sequence *****/
   175    272         sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
          273  +    }
          274  +    if( db->mallocFailed ){
          275  +      /* If a malloc failure has occurred, then the result of the expression 
          276  +      ** passed as the second argument to the call to sqlite3VdbeJumpHere() 
          277  +      ** below may be negative. Which causes an assert() to fail (or an
          278  +      ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
          279  +      return;
   176    280       }
   177    281       sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
   178    282       for(i=0; i<nCol; i++){
   179         -      sqlite3VdbeJumpHere(v, topOfLoop + 2*(i + 1));
          283  +      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-(nCol*2));
   180    284         sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
   181    285         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
   182    286       }
          287  +
          288  +    /* End of the analysis loop. */
   183    289       sqlite3VdbeResolveLabel(v, endOfLoop);
   184    290       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
   185    291       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
   186    292   
   187         -    /* Store the results.  
          293  +    /* Store the results in sqlite_stat1.
   188    294       **
   189    295       ** The result is a single row of the sqlite_stat1 table.  The first
   190    296       ** two columns are the names of the table and index.  The third column
   191    297       ** is a string composed of a list of integer statistics about the
   192         -    ** index.  The first integer in the list is the total number of entires
          298  +    ** index.  The first integer in the list is the total number of entries
   193    299       ** in the index.  There is one additional integer in the list for each
   194    300       ** column of the table.  This additional integer is a guess of how many
   195    301       ** rows of the table the index will select.  If D is the count of distinct
   196    302       ** values and K is the total number of rows, then the integer is computed
   197    303       ** as:
   198    304       **
   199    305       **        I = (K+D-1)/D
   200    306       **
   201    307       ** If K==0 then no entry is made into the sqlite_stat1 table.  
   202    308       ** If K>0 then it is always the case the D>0 so division by zero
   203    309       ** is never possible.
   204    310       */
   205         -    addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
   206         -    sqlite3VdbeAddOp4(v, OP_String8, 0, regFields, 0, pTab->zName, 0);
   207         -    sqlite3VdbeAddOp4(v, OP_String8, 0, regFields+1, 0, pIdx->zName, 0);
   208         -    regF2 = regFields+2;
   209         -    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regF2);
          311  +    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
          312  +    if( jZeroRows==0 ){
          313  +      jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
          314  +    }
   210    315       for(i=0; i<nCol; i++){
   211    316         sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
   212         -      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
          317  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
   213    318         sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
   214    319         sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
   215    320         sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
   216    321         sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
   217         -      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
          322  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
   218    323       }
   219         -    sqlite3VdbeAddOp4(v, OP_MakeRecord, regFields, 3, regRec, "aaa", 0);
          324  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
   220    325       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
   221    326       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
   222    327       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
          328  +  }
          329  +
          330  +  /* If the table has no indices, create a single sqlite_stat1 entry
          331  +  ** containing NULL as the index name and the row count as the content.
          332  +  */
          333  +  if( pTab->pIndex==0 ){
          334  +    sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
          335  +    VdbeComment((v, "%s", pTab->zName));
          336  +    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
          337  +    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
          338  +  }else{
          339  +    assert( jZeroRows>0 );
          340  +    addr = sqlite3VdbeAddOp0(v, OP_Goto);
          341  +    sqlite3VdbeJumpHere(v, jZeroRows);
          342  +  }
          343  +  sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
          344  +  sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
          345  +  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
          346  +  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
          347  +  sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
          348  +  if( pParse->nMem<regRec ) pParse->nMem = regRec;
          349  +  if( jZeroRows ){
   223    350       sqlite3VdbeJumpHere(v, addr);
   224    351     }
   225    352   }
   226    353   
   227    354   /*
   228    355   ** Generate code that will cause the most recent index analysis to
   229         -** be laoded into internal hash tables where is can be used.
          356  +** be loaded into internal hash tables where is can be used.
   230    357   */
   231    358   static void loadAnalysis(Parse *pParse, int iDb){
   232    359     Vdbe *v = sqlite3GetVdbe(pParse);
   233    360     if( v ){
   234    361       sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
   235    362     }
   236    363   }
................................................................................
   242    369     sqlite3 *db = pParse->db;
   243    370     Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
   244    371     HashElem *k;
   245    372     int iStatCur;
   246    373     int iMem;
   247    374   
   248    375     sqlite3BeginWriteOperation(pParse, 0, iDb);
   249         -  iStatCur = pParse->nTab++;
          376  +  iStatCur = pParse->nTab;
          377  +  pParse->nTab += 2;
   250    378     openStatTable(pParse, iDb, iStatCur, 0);
   251    379     iMem = pParse->nMem+1;
   252    380     for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
   253    381       Table *pTab = (Table*)sqliteHashData(k);
   254    382       analyzeOneTable(pParse, pTab, iStatCur, iMem);
   255    383     }
   256    384     loadAnalysis(pParse, iDb);
................................................................................
   264    392     int iDb;
   265    393     int iStatCur;
   266    394   
   267    395     assert( pTab!=0 );
   268    396     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   269    397     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   270    398     sqlite3BeginWriteOperation(pParse, 0, iDb);
   271         -  iStatCur = pParse->nTab++;
          399  +  iStatCur = pParse->nTab;
          400  +  pParse->nTab += 2;
   272    401     openStatTable(pParse, iDb, iStatCur, pTab->zName);
   273    402     analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
   274    403     loadAnalysis(pParse, iDb);
   275    404   }
   276    405   
   277    406   /*
   278    407   ** Generate code for the ANALYZE command.  The parser calls this routine
................................................................................
   297    426     /* Read the database schema. If an error occurs, leave an error message
   298    427     ** and code in pParse and return NULL. */
   299    428     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   300    429     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   301    430       return;
   302    431     }
   303    432   
          433  +  assert( pName2!=0 || pName1==0 );
   304    434     if( pName1==0 ){
   305    435       /* Form 1:  Analyze everything */
   306    436       for(i=0; i<db->nDb; i++){
   307    437         if( i==1 ) continue;  /* Do not analyze the TEMP database */
   308    438         analyzeDatabase(pParse, i);
   309    439       }
   310         -  }else if( pName2==0 || pName2->n==0 ){
          440  +  }else if( pName2->n==0 ){
   311    441       /* Form 2:  Analyze the database or table named */
   312    442       iDb = sqlite3FindDb(db, pName1);
   313    443       if( iDb>=0 ){
   314    444         analyzeDatabase(pParse, iDb);
   315    445       }else{
   316    446         z = sqlite3NameFromToken(db, pName1);
   317    447         if( z ){
................................................................................
   349    479     const char *zDatabase;
   350    480   };
   351    481   
   352    482   /*
   353    483   ** This callback is invoked once for each index when reading the
   354    484   ** sqlite_stat1 table.  
   355    485   **
   356         -**     argv[0] = name of the index
   357         -**     argv[1] = results of analysis - on integer for each column
          486  +**     argv[0] = name of the table
          487  +**     argv[1] = name of the index (might be NULL)
          488  +**     argv[2] = results of analysis - on integer for each column
          489  +**
          490  +** Entries for which argv[1]==NULL simply record the number of rows in
          491  +** the table.
   358    492   */
   359         -static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){
          493  +static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
   360    494     analysisInfo *pInfo = (analysisInfo*)pData;
   361    495     Index *pIndex;
   362         -  int i, c;
          496  +  Table *pTable;
          497  +  int i, c, n;
   363    498     unsigned int v;
   364    499     const char *z;
   365    500   
   366         -  assert( argc==2 );
   367         -  if( argv==0 || argv[0]==0 || argv[1]==0 ){
          501  +  assert( argc==3 );
          502  +  UNUSED_PARAMETER2(NotUsed, argc);
          503  +
          504  +  if( argv==0 || argv[0]==0 || argv[2]==0 ){
   368    505       return 0;
   369    506     }
   370         -  pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
   371         -  if( pIndex==0 ){
          507  +  pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
          508  +  if( pTable==0 ){
   372    509       return 0;
   373    510     }
   374         -  z = argv[1];
   375         -  for(i=0; *z && i<=pIndex->nColumn; i++){
          511  +  if( argv[1] ){
          512  +    pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
          513  +  }else{
          514  +    pIndex = 0;
          515  +  }
          516  +  n = pIndex ? pIndex->nColumn : 0;
          517  +  z = argv[2];
          518  +  for(i=0; *z && i<=n; i++){
   376    519       v = 0;
   377    520       while( (c=z[0])>='0' && c<='9' ){
   378    521         v = v*10 + c - '0';
   379    522         z++;
   380    523       }
          524  +    if( i==0 ) pTable->nRowEst = v;
          525  +    if( pIndex==0 ) break;
   381    526       pIndex->aiRowEst[i] = v;
   382    527       if( *z==' ' ) z++;
   383    528     }
   384    529     return 0;
   385    530   }
   386    531   
   387    532   /*
   388         -** Load the content of the sqlite_stat1 table into the index hash tables.
          533  +** If the Index.aSample variable is not NULL, delete the aSample[] array
          534  +** and its contents.
          535  +*/
          536  +void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
          537  +#ifdef SQLITE_ENABLE_STAT2
          538  +  if( pIdx->aSample ){
          539  +    int j;
          540  +    for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
          541  +      IndexSample *p = &pIdx->aSample[j];
          542  +      if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
          543  +        sqlite3DbFree(db, p->u.z);
          544  +      }
          545  +    }
          546  +    sqlite3DbFree(db, pIdx->aSample);
          547  +  }
          548  +#else
          549  +  UNUSED_PARAMETER(db);
          550  +  UNUSED_PARAMETER(pIdx);
          551  +#endif
          552  +}
          553  +
          554  +/*
          555  +** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
          556  +** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
          557  +** arrays. The contents of sqlite_stat2 are used to populate the
          558  +** Index.aSample[] arrays.
          559  +**
          560  +** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
          561  +** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined 
          562  +** during compilation and the sqlite_stat2 table is present, no data is 
          563  +** read from it.
          564  +**
          565  +** If SQLITE_ENABLE_STAT2 was defined during compilation and the 
          566  +** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
          567  +** returned. However, in this case, data is read from the sqlite_stat1
          568  +** table (if it is present) before returning.
          569  +**
          570  +** If an OOM error occurs, this function always sets db->mallocFailed.
          571  +** This means if the caller does not care about other errors, the return
          572  +** code may be ignored.
   389    573   */
   390    574   int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
   391    575     analysisInfo sInfo;
   392    576     HashElem *i;
   393    577     char *zSql;
   394    578     int rc;
   395    579   
................................................................................
   397    581     assert( db->aDb[iDb].pBt!=0 );
   398    582     assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
   399    583   
   400    584     /* Clear any prior statistics */
   401    585     for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
   402    586       Index *pIdx = sqliteHashData(i);
   403    587       sqlite3DefaultRowEst(pIdx);
          588  +    sqlite3DeleteIndexSamples(db, pIdx);
          589  +    pIdx->aSample = 0;
   404    590     }
   405    591   
   406         -  /* Check to make sure the sqlite_stat1 table existss */
          592  +  /* Check to make sure the sqlite_stat1 table exists */
   407    593     sInfo.db = db;
   408    594     sInfo.zDatabase = db->aDb[iDb].zName;
   409    595     if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
   410         -     return SQLITE_ERROR;
          596  +    return SQLITE_ERROR;
   411    597     }
   412         -
   413    598   
   414    599     /* Load new statistics out of the sqlite_stat1 table */
   415         -  zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1",
   416         -                        sInfo.zDatabase);
   417         -  (void)sqlite3SafetyOff(db);
   418         -  rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
   419         -  (void)sqlite3SafetyOn(db);
   420         -  sqlite3DbFree(db, zSql);
          600  +  zSql = sqlite3MPrintf(db, 
          601  +      "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
          602  +  if( zSql==0 ){
          603  +    rc = SQLITE_NOMEM;
          604  +  }else{
          605  +    rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
          606  +    sqlite3DbFree(db, zSql);
          607  +  }
          608  +
          609  +
          610  +  /* Load the statistics from the sqlite_stat2 table. */
          611  +#ifdef SQLITE_ENABLE_STAT2
          612  +  if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
          613  +    rc = SQLITE_ERROR;
          614  +  }
          615  +  if( rc==SQLITE_OK ){
          616  +    sqlite3_stmt *pStmt = 0;
          617  +
          618  +    zSql = sqlite3MPrintf(db, 
          619  +        "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
          620  +    if( !zSql ){
          621  +      rc = SQLITE_NOMEM;
          622  +    }else{
          623  +      rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
          624  +      sqlite3DbFree(db, zSql);
          625  +    }
          626  +
          627  +    if( rc==SQLITE_OK ){
          628  +      while( sqlite3_step(pStmt)==SQLITE_ROW ){
          629  +        char *zIndex = (char *)sqlite3_column_text(pStmt, 0);
          630  +        Index *pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase);
          631  +        if( pIdx ){
          632  +          int iSample = sqlite3_column_int(pStmt, 1);
          633  +          if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
          634  +            int eType = sqlite3_column_type(pStmt, 2);
          635  +
          636  +            if( pIdx->aSample==0 ){
          637  +              static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
          638  +              pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
          639  +              if( pIdx->aSample==0 ){
          640  +                db->mallocFailed = 1;
          641  +                break;
          642  +              }
          643  +	      memset(pIdx->aSample, 0, sz);
          644  +            }
          645  +
          646  +            assert( pIdx->aSample );
          647  +            {
          648  +              IndexSample *pSample = &pIdx->aSample[iSample];
          649  +              pSample->eType = (u8)eType;
          650  +              if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
          651  +                pSample->u.r = sqlite3_column_double(pStmt, 2);
          652  +              }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
          653  +                const char *z = (const char *)(
          654  +                    (eType==SQLITE_BLOB) ?
          655  +                    sqlite3_column_blob(pStmt, 2):
          656  +                    sqlite3_column_text(pStmt, 2)
          657  +                );
          658  +                int n = sqlite3_column_bytes(pStmt, 2);
          659  +                if( n>24 ){
          660  +                  n = 24;
          661  +                }
          662  +                pSample->nByte = (u8)n;
          663  +                if( n < 1){
          664  +                  pSample->u.z = 0;
          665  +                }else{
          666  +                  pSample->u.z = sqlite3DbStrNDup(0, z, n);
          667  +                  if( pSample->u.z==0 ){
          668  +                    db->mallocFailed = 1;
          669  +                    break;
          670  +                  }
          671  +                }
          672  +              }
          673  +            }
          674  +          }
          675  +        }
          676  +      }
          677  +      rc = sqlite3_finalize(pStmt);
          678  +    }
          679  +  }
          680  +#endif
          681  +
          682  +  if( rc==SQLITE_NOMEM ){
          683  +    db->mallocFailed = 1;
          684  +  }
   421    685     return rc;
   422    686   }
   423    687   
   424    688   
   425    689   #endif /* SQLITE_OMIT_ANALYZE */

Changes to SQLite.Interop/splitsource/attach.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the ATTACH and DETACH commands.
    13         -**
    14         -** $Id: attach.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
    15     13   */
    16     14   #include "sqliteInt.h"
    17     15   
    18     16   #ifndef SQLITE_OMIT_ATTACH
    19     17   /*
    20     18   ** Resolve an expression that was part of an ATTACH or DETACH statement. This
    21     19   ** is slightly different from resolving a normal SQL expression, because simple
................................................................................
    35     33   ** will fail because neither abc or def can be resolved.
    36     34   */
    37     35   static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
    38     36   {
    39     37     int rc = SQLITE_OK;
    40     38     if( pExpr ){
    41     39       if( pExpr->op!=TK_ID ){
    42         -      rc = sqlite3ExprResolveNames(pName, pExpr);
           40  +      rc = sqlite3ResolveExprNames(pName, pExpr);
    43     41         if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
    44         -        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span);
           42  +        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
    45     43           return SQLITE_ERROR;
    46     44         }
    47     45       }else{
    48     46         pExpr->op = TK_STRING;
    49     47       }
    50     48     }
    51     49     return rc;
................................................................................
    60     58   **     SELECT sqlite_attach(x, y, z)
    61     59   **
    62     60   ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
    63     61   ** third argument.
    64     62   */
    65     63   static void attachFunc(
    66     64     sqlite3_context *context,
    67         -  int argc,
           65  +  int NotUsed,
    68     66     sqlite3_value **argv
    69     67   ){
    70     68     int i;
    71     69     int rc = 0;
    72     70     sqlite3 *db = sqlite3_context_db_handle(context);
    73     71     const char *zName;
    74     72     const char *zFile;
    75     73     Db *aNew;
    76     74     char *zErrDyn = 0;
    77         -  char zErr[128];
           75  +
           76  +  UNUSED_PARAMETER(NotUsed);
    78     77   
    79     78     zFile = (const char *)sqlite3_value_text(argv[0]);
    80     79     zName = (const char *)sqlite3_value_text(argv[1]);
    81     80     if( zFile==0 ) zFile = "";
    82     81     if( zName==0 ) zName = "";
    83     82   
    84     83     /* Check for the following errors:
    85     84     **
    86     85     **     * Too many attached databases,
    87     86     **     * Transaction currently open
    88     87     **     * Specified database name already being used.
    89     88     */
    90     89     if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
    91         -    sqlite3_snprintf(
    92         -      sizeof(zErr), zErr, "too many attached databases - max %d", 
           90  +    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
    93     91         db->aLimit[SQLITE_LIMIT_ATTACHED]
    94     92       );
    95     93       goto attach_error;
    96     94     }
    97     95     if( !db->autoCommit ){
    98         -    sqlite3_snprintf(sizeof(zErr), zErr,
    99         -                     "cannot ATTACH database within transaction");
           96  +    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
   100     97       goto attach_error;
   101     98     }
   102     99     for(i=0; i<db->nDb; i++){
   103    100       char *z = db->aDb[i].zName;
   104         -    if( z && zName && sqlite3StrICmp(z, zName)==0 ){
   105         -      sqlite3_snprintf(sizeof(zErr), zErr, 
   106         -                       "database %s is already in use", zName);
          101  +    assert( z && zName );
          102  +    if( sqlite3StrICmp(z, zName)==0 ){
          103  +      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
   107    104         goto attach_error;
   108    105       }
   109    106     }
   110    107   
   111    108     /* Allocate the new entry in the db->aDb[] array and initialise the schema
   112    109     ** hash tables.
   113    110     */
................................................................................
   116    113       if( aNew==0 ) return;
   117    114       memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
   118    115     }else{
   119    116       aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
   120    117       if( aNew==0 ) return;
   121    118     }
   122    119     db->aDb = aNew;
   123         -  aNew = &db->aDb[db->nDb++];
          120  +  aNew = &db->aDb[db->nDb];
   124    121     memset(aNew, 0, sizeof(*aNew));
   125    122   
   126    123     /* Open the database file. If the btree is successfully opened, use
   127    124     ** it to obtain the database schema. At this point the schema may
   128    125     ** or may not be initialised.
   129    126     */
   130         -  rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE,
   131         -                           db->openFlags | SQLITE_OPEN_MAIN_DB,
   132         -                           &aNew->pBt);
   133         -  if( rc==SQLITE_OK ){
          127  +  rc = sqlite3BtreeOpen(zFile, db, &aNew->pBt, 0,
          128  +                        db->openFlags | SQLITE_OPEN_MAIN_DB);
          129  +  db->nDb++;
          130  +  if( rc==SQLITE_CONSTRAINT ){
          131  +    rc = SQLITE_ERROR;
          132  +    zErrDyn = sqlite3MPrintf(db, "database is already attached");
          133  +  }else if( rc==SQLITE_OK ){
   134    134       Pager *pPager;
   135    135       aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
   136    136       if( !aNew->pSchema ){
   137    137         rc = SQLITE_NOMEM;
   138    138       }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
   139         -      sqlite3_snprintf(sizeof(zErr), zErr, 
          139  +      zErrDyn = sqlite3MPrintf(db, 
   140    140           "attached databases must use the same text encoding as main database");
   141         -      goto attach_error;
          141  +      rc = SQLITE_ERROR;
   142    142       }
   143    143       pPager = sqlite3BtreePager(aNew->pBt);
   144    144       sqlite3PagerLockingMode(pPager, db->dfltLockMode);
   145         -    sqlite3PagerJournalMode(pPager, db->dfltJournalMode);
          145  +    sqlite3BtreeSecureDelete(aNew->pBt,
          146  +                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
   146    147     }
   147         -  aNew->zName = sqlite3DbStrDup(db, zName);
   148    148     aNew->safety_level = 3;
          149  +  aNew->zName = sqlite3DbStrDup(db, zName);
          150  +  if( rc==SQLITE_OK && aNew->zName==0 ){
          151  +    rc = SQLITE_NOMEM;
          152  +  }
   149    153   
   150         -#if SQLITE_HAS_CODEC
   151         -  {
          154  +
          155  +#ifdef SQLITE_HAS_CODEC
          156  +  if( rc==SQLITE_OK ){
   152    157       extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
   153    158       extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
   154    159       int nKey;
   155    160       char *zKey;
   156    161       int t = sqlite3_value_type(argv[2]);
   157    162       switch( t ){
   158    163         case SQLITE_INTEGER:
................................................................................
   161    166           rc = SQLITE_ERROR;
   162    167           break;
   163    168           
   164    169         case SQLITE_TEXT:
   165    170         case SQLITE_BLOB:
   166    171           nKey = sqlite3_value_bytes(argv[2]);
   167    172           zKey = (char *)sqlite3_value_blob(argv[2]);
   168         -        sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
          173  +        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
   169    174           break;
   170    175   
   171    176         case SQLITE_NULL:
   172    177           /* No key specified.  Use the key from the main database */
   173    178           sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
   174         -        sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
          179  +        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
   175    180           break;
   176    181       }
   177    182     }
   178    183   #endif
   179    184   
   180    185     /* If the file was opened successfully, read the schema for the new database.
   181    186     ** If this fails, or if opening the file failed, then close the file and 
   182    187     ** remove the entry from the db->aDb[] array. i.e. put everything back the way
   183    188     ** we found it.
   184    189     */
   185    190     if( rc==SQLITE_OK ){
   186         -    (void)sqlite3SafetyOn(db);
   187    191       sqlite3BtreeEnterAll(db);
   188    192       rc = sqlite3Init(db, &zErrDyn);
   189    193       sqlite3BtreeLeaveAll(db);
   190         -    (void)sqlite3SafetyOff(db);
   191    194     }
   192    195     if( rc ){
   193    196       int iDb = db->nDb - 1;
   194    197       assert( iDb>=2 );
   195    198       if( db->aDb[iDb].pBt ){
   196    199         sqlite3BtreeClose(db->aDb[iDb].pBt);
   197    200         db->aDb[iDb].pBt = 0;
   198    201         db->aDb[iDb].pSchema = 0;
   199    202       }
   200    203       sqlite3ResetInternalSchema(db, 0);
   201    204       db->nDb = iDb;
   202    205       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
   203    206         db->mallocFailed = 1;
   204         -      sqlite3_snprintf(sizeof(zErr),zErr, "out of memory");
   205         -    }else{
   206         -      sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile);
          207  +      sqlite3DbFree(db, zErrDyn);
          208  +      zErrDyn = sqlite3MPrintf(db, "out of memory");
          209  +    }else if( zErrDyn==0 ){
          210  +      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
   207    211       }
   208    212       goto attach_error;
   209    213     }
   210    214     
   211    215     return;
   212    216   
   213    217   attach_error:
   214    218     /* Return an error if we get here */
   215    219     if( zErrDyn ){
   216    220       sqlite3_result_error(context, zErrDyn, -1);
   217    221       sqlite3DbFree(db, zErrDyn);
   218         -  }else{
   219         -    zErr[sizeof(zErr)-1] = 0;
   220         -    sqlite3_result_error(context, zErr, -1);
   221    222     }
   222    223     if( rc ) sqlite3_result_error_code(context, rc);
   223    224   }
   224    225   
   225    226   /*
   226    227   ** An SQL user-function registered to do the work of an DETACH statement. The
   227    228   ** three arguments to the function come directly from a detach statement:
................................................................................
   228    229   **
   229    230   **     DETACH DATABASE x
   230    231   **
   231    232   **     SELECT sqlite_detach(x)
   232    233   */
   233    234   static void detachFunc(
   234    235     sqlite3_context *context,
   235         -  int argc,
          236  +  int NotUsed,
   236    237     sqlite3_value **argv
   237    238   ){
   238    239     const char *zName = (const char *)sqlite3_value_text(argv[0]);
   239    240     sqlite3 *db = sqlite3_context_db_handle(context);
   240    241     int i;
   241    242     Db *pDb = 0;
   242    243     char zErr[128];
          244  +
          245  +  UNUSED_PARAMETER(NotUsed);
   243    246   
   244    247     if( zName==0 ) zName = "";
   245    248     for(i=0; i<db->nDb; i++){
   246    249       pDb = &db->aDb[i];
   247    250       if( pDb->pBt==0 ) continue;
   248    251       if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
   249    252     }
................................................................................
   257    260       goto detach_error;
   258    261     }
   259    262     if( !db->autoCommit ){
   260    263       sqlite3_snprintf(sizeof(zErr), zErr,
   261    264                        "cannot DETACH database within transaction");
   262    265       goto detach_error;
   263    266     }
   264         -  if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){
          267  +  if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
   265    268       sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
   266    269       goto detach_error;
   267    270     }
   268    271   
   269    272     sqlite3BtreeClose(pDb->pBt);
   270    273     pDb->pBt = 0;
   271    274     pDb->pSchema = 0;
................................................................................
   279    282   /*
   280    283   ** This procedure generates VDBE code for a single invocation of either the
   281    284   ** sqlite_detach() or sqlite_attach() SQL user functions.
   282    285   */
   283    286   static void codeAttach(
   284    287     Parse *pParse,       /* The parser context */
   285    288     int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
   286         -  const char *zFunc,   /* Either "sqlite_attach" or "sqlite_detach */
   287         -  int nFunc,           /* Number of args to pass to zFunc */
          289  +  FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
   288    290     Expr *pAuthArg,      /* Expression to pass to authorization callback */
   289    291     Expr *pFilename,     /* Name of database file */
   290    292     Expr *pDbname,       /* Name of the database to use internally */
   291    293     Expr *pKey           /* Database key for encryption extension */
   292    294   ){
   293    295     int rc;
   294    296     NameContext sName;
   295    297     Vdbe *v;
   296         -  FuncDef *pFunc;
   297    298     sqlite3* db = pParse->db;
   298    299     int regArgs;
   299    300   
   300         -#ifndef SQLITE_OMIT_AUTHORIZATION
   301         -  assert( db->mallocFailed || pAuthArg );
   302         -  if( pAuthArg ){
   303         -    char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span);
   304         -    if( !zAuthArg ){
   305         -      goto attach_end;
   306         -    }
   307         -    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
   308         -    sqlite3DbFree(db, zAuthArg);
   309         -    if(rc!=SQLITE_OK ){
   310         -      goto attach_end;
   311         -    }
   312         -  }
   313         -#endif /* SQLITE_OMIT_AUTHORIZATION */
   314         -
   315    301     memset(&sName, 0, sizeof(NameContext));
   316    302     sName.pParse = pParse;
   317    303   
   318    304     if( 
   319    305         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
   320    306         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
   321    307         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
   322    308     ){
   323    309       pParse->nErr++;
   324    310       goto attach_end;
   325    311     }
          312  +
          313  +#ifndef SQLITE_OMIT_AUTHORIZATION
          314  +  if( pAuthArg ){
          315  +    char *zAuthArg = pAuthArg->u.zToken;
          316  +    if( NEVER(zAuthArg==0) ){
          317  +      goto attach_end;
          318  +    }
          319  +    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
          320  +    if(rc!=SQLITE_OK ){
          321  +      goto attach_end;
          322  +    }
          323  +  }
          324  +#endif /* SQLITE_OMIT_AUTHORIZATION */
          325  +
   326    326   
   327    327     v = sqlite3GetVdbe(pParse);
   328    328     regArgs = sqlite3GetTempRange(pParse, 4);
   329    329     sqlite3ExprCode(pParse, pFilename, regArgs);
   330    330     sqlite3ExprCode(pParse, pDbname, regArgs+1);
   331    331     sqlite3ExprCode(pParse, pKey, regArgs+2);
   332    332   
   333    333     assert( v || db->mallocFailed );
   334    334     if( v ){
   335         -    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-nFunc, regArgs+3);
   336         -    sqlite3VdbeChangeP5(v, nFunc);
   337         -    pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
          335  +    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
          336  +    assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
          337  +    sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
   338    338       sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
   339    339   
   340    340       /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
   341    341       ** statement only). For DETACH, set it to false (expire all existing
   342    342       ** statements).
   343    343       */
   344    344       sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
................................................................................
   352    352   
   353    353   /*
   354    354   ** Called by the parser to compile a DETACH statement.
   355    355   **
   356    356   **     DETACH pDbname
   357    357   */
   358    358   void sqlite3Detach(Parse *pParse, Expr *pDbname){
   359         -  codeAttach(pParse, SQLITE_DETACH, "sqlite_detach", 1, pDbname, 0, 0, pDbname);
          359  +  static const FuncDef detach_func = {
          360  +    1,                /* nArg */
          361  +    SQLITE_UTF8,      /* iPrefEnc */
          362  +    0,                /* flags */
          363  +    0,                /* pUserData */
          364  +    0,                /* pNext */
          365  +    detachFunc,       /* xFunc */
          366  +    0,                /* xStep */
          367  +    0,                /* xFinalize */
          368  +    "sqlite_detach",  /* zName */
          369  +    0,                /* pHash */
          370  +    0                 /* pDestructor */
          371  +  };
          372  +  codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
   360    373   }
   361    374   
   362    375   /*
   363    376   ** Called by the parser to compile an ATTACH statement.
   364    377   **
   365    378   **     ATTACH p AS pDbname KEY pKey
   366    379   */
   367    380   void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
   368         -  codeAttach(pParse, SQLITE_ATTACH, "sqlite_attach", 3, p, p, pDbname, pKey);
          381  +  static const FuncDef attach_func = {
          382  +    3,                /* nArg */
          383  +    SQLITE_UTF8,      /* iPrefEnc */
          384  +    0,                /* flags */
          385  +    0,                /* pUserData */
          386  +    0,                /* pNext */
          387  +    attachFunc,       /* xFunc */
          388  +    0,                /* xStep */
          389  +    0,                /* xFinalize */
          390  +    "sqlite_attach",  /* zName */
          391  +    0,                /* pHash */
          392  +    0                 /* pDestructor */
          393  +  };
          394  +  codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
   369    395   }
   370    396   #endif /* SQLITE_OMIT_ATTACH */
   371    397   
   372         -/*
   373         -** Register the functions sqlite_attach and sqlite_detach.
   374         -*/
   375         -void sqlite3AttachFunctions(sqlite3 *db){
   376         -#ifndef SQLITE_OMIT_ATTACH
   377         -  static const int enc = SQLITE_UTF8;
   378         -  sqlite3CreateFunc(db, "sqlite_attach", 3, enc, 0, attachFunc, 0, 0);
   379         -  sqlite3CreateFunc(db, "sqlite_detach", 1, enc, 0, detachFunc, 0, 0);
   380         -#endif
   381         -}
   382         -
   383    398   /*
   384    399   ** Initialize a DbFixer structure.  This routine must be called prior
   385    400   ** to passing the structure to one of the sqliteFixAAAA() routines below.
   386    401   **
   387    402   ** The return value indicates whether or not fixation is required.  TRUE
   388    403   ** means we do need to fix the database references, FALSE means we do not.
   389    404   */
................................................................................
   392    407     Parse *pParse,      /* Error messages will be written here */
   393    408     int iDb,            /* This is the database that must be used */
   394    409     const char *zType,  /* "view", "trigger", or "index" */
   395    410     const Token *pName  /* Name of the view, trigger, or index */
   396    411   ){
   397    412     sqlite3 *db;
   398    413   
   399         -  if( iDb<0 || iDb==1 ) return 0;
          414  +  if( NEVER(iDb<0) || iDb==1 ) return 0;
   400    415     db = pParse->db;
   401    416     assert( db->nDb>iDb );
   402    417     pFix->pParse = pParse;
   403    418     pFix->zDb = db->aDb[iDb].zName;
   404    419     pFix->zType = zType;
   405    420     pFix->pName = pName;
   406    421     return 1;
................................................................................
   424    439     DbFixer *pFix,       /* Context of the fixation */
   425    440     SrcList *pList       /* The Source list to check and modify */
   426    441   ){
   427    442     int i;
   428    443     const char *zDb;
   429    444     struct SrcList_item *pItem;
   430    445   
   431         -  if( pList==0 ) return 0;
          446  +  if( NEVER(pList==0) ) return 0;
   432    447     zDb = pFix->zDb;
   433    448     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
   434    449       if( pItem->zDatabase==0 ){
   435    450         pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
   436    451       }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
   437    452         sqlite3ErrorMsg(pFix->pParse,
   438    453            "%s %T cannot reference objects in database %s",
................................................................................
   469    484     return 0;
   470    485   }
   471    486   int sqlite3FixExpr(
   472    487     DbFixer *pFix,     /* Context of the fixation */
   473    488     Expr *pExpr        /* The expression to be fixed to one database */
   474    489   ){
   475    490     while( pExpr ){
   476         -    if( sqlite3FixSelect(pFix, pExpr->pSelect) ){
   477         -      return 1;
   478         -    }
   479         -    if( sqlite3FixExprList(pFix, pExpr->pList) ){
   480         -      return 1;
          491  +    if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
          492  +    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
          493  +      if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
          494  +    }else{
          495  +      if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
   481    496       }
   482    497       if( sqlite3FixExpr(pFix, pExpr->pRight) ){
   483    498         return 1;
   484    499       }
   485    500       pExpr = pExpr->pLeft;
   486    501     }
   487    502     return 0;

Changes to SQLite.Interop/splitsource/auth.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the sqlite3_set_authorizer()
    13     13   ** API.  This facility is an optional feature of the library.  Embedded
    14     14   ** systems that do not need this facility may omit it by recompiling
    15     15   ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
    16         -**
    17         -** $Id: auth.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
    18     16   */
    19     17   #include "sqliteInt.h"
    20     18   
    21     19   /*
    22     20   ** All of the code in this file may be omitted by defining a single
    23     21   ** macro.
    24     22   */
................................................................................
    82     80     return SQLITE_OK;
    83     81   }
    84     82   
    85     83   /*
    86     84   ** Write an error message into pParse->zErrMsg that explains that the
    87     85   ** user-supplied authorization function returned an illegal value.
    88     86   */
    89         -static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
    90         -  sqlite3ErrorMsg(pParse, "illegal return value (%d) from the "
    91         -    "authorization function - should be SQLITE_OK, SQLITE_IGNORE, "
    92         -    "or SQLITE_DENY", rc);
           87  +static void sqliteAuthBadReturnCode(Parse *pParse){
           88  +  sqlite3ErrorMsg(pParse, "authorizer malfunction");
    93     89     pParse->rc = SQLITE_ERROR;
    94     90   }
           91  +
           92  +/*
           93  +** Invoke the authorization callback for permission to read column zCol from
           94  +** table zTab in database zDb. This function assumes that an authorization
           95  +** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
           96  +**
           97  +** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
           98  +** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
           99  +** is treated as SQLITE_DENY. In this case an error is left in pParse.
          100  +*/
          101  +int sqlite3AuthReadCol(
          102  +  Parse *pParse,                  /* The parser context */
          103  +  const char *zTab,               /* Table name */
          104  +  const char *zCol,               /* Column name */
          105  +  int iDb                         /* Index of containing database. */
          106  +){
          107  +  sqlite3 *db = pParse->db;       /* Database handle */
          108  +  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
          109  +  int rc;                         /* Auth callback return code */
          110  +
          111  +  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
          112  +  if( rc==SQLITE_DENY ){
          113  +    if( db->nDb>2 || iDb!=0 ){
          114  +      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
          115  +    }else{
          116  +      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
          117  +    }
          118  +    pParse->rc = SQLITE_AUTH;
          119  +  }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
          120  +    sqliteAuthBadReturnCode(pParse);
          121  +  }
          122  +  return rc;
          123  +}
    95    124   
    96    125   /*
    97    126   ** The pExpr should be a TK_COLUMN expression.  The table referred to
    98    127   ** is in pTabList or else it is the NEW or OLD table of a trigger.  
    99    128   ** Check to see if it is OK to read this particular column.
   100    129   **
   101    130   ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
................................................................................
   105    134   void sqlite3AuthRead(
   106    135     Parse *pParse,        /* The parser context */
   107    136     Expr *pExpr,          /* The expression to check authorization on */
   108    137     Schema *pSchema,      /* The schema of the expression */
   109    138     SrcList *pTabList     /* All table that pExpr might refer to */
   110    139   ){
   111    140     sqlite3 *db = pParse->db;
   112         -  int rc;
   113    141     Table *pTab = 0;      /* The table being read */
   114    142     const char *zCol;     /* Name of the column of the table */
   115    143     int iSrc;             /* Index in pTabList->a[] of table being read */
   116         -  const char *zDBase;   /* Name of database being accessed */
   117         -  TriggerStack *pStack; /* The stack of current triggers */
   118    144     int iDb;              /* The index of the database the expression refers to */
          145  +  int iCol;             /* Index of column in table */
   119    146   
   120    147     if( db->xAuth==0 ) return;
   121         -  if( pExpr->op!=TK_COLUMN ) return;
   122    148     iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
   123    149     if( iDb<0 ){
   124    150       /* An attempt to read a column out of a subquery or other
   125    151       ** temporary table. */
   126    152       return;
   127    153     }
   128         -  for(iSrc=0; pTabList && iSrc<pTabList->nSrc; iSrc++){
   129         -    if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
   130         -  }
   131         -  if( iSrc>=0 && pTabList && iSrc<pTabList->nSrc ){
   132         -    pTab = pTabList->a[iSrc].pTab;
   133         -  }else if( (pStack = pParse->trigStack)!=0 ){
   134         -    /* This must be an attempt to read the NEW or OLD pseudo-tables
   135         -    ** of a trigger.
   136         -    */
   137         -    assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
   138         -    pTab = pStack->pTab;
          154  +
          155  +  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
          156  +  if( pExpr->op==TK_TRIGGER ){
          157  +    pTab = pParse->pTriggerTab;
          158  +  }else{
          159  +    assert( pTabList );
          160  +    for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
          161  +      if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
          162  +        pTab = pTabList->a[iSrc].pTab;
          163  +        break;
          164  +      }
          165  +    }
   139    166     }
   140         -  if( pTab==0 ) return;
   141         -  if( pExpr->iColumn>=0 ){
   142         -    assert( pExpr->iColumn<pTab->nCol );
   143         -    zCol = pTab->aCol[pExpr->iColumn].zName;
          167  +  iCol = pExpr->iColumn;
          168  +  if( NEVER(pTab==0) ) return;
          169  +
          170  +  if( iCol>=0 ){
          171  +    assert( iCol<pTab->nCol );
          172  +    zCol = pTab->aCol[iCol].zName;
   144    173     }else if( pTab->iPKey>=0 ){
   145    174       assert( pTab->iPKey<pTab->nCol );
   146    175       zCol = pTab->aCol[pTab->iPKey].zName;
   147    176     }else{
   148    177       zCol = "ROWID";
   149    178     }
   150    179     assert( iDb>=0 && iDb<db->nDb );
   151         -  zDBase = db->aDb[iDb].zName;
   152         -  rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, 
   153         -                 pParse->zAuthContext);
   154         -  if( rc==SQLITE_IGNORE ){
          180  +  if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
   155    181       pExpr->op = TK_NULL;
   156         -  }else if( rc==SQLITE_DENY ){
   157         -    if( db->nDb>2 || iDb!=0 ){
   158         -      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited", 
   159         -         zDBase, pTab->zName, zCol);
   160         -    }else{
   161         -      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited",pTab->zName,zCol);
   162         -    }
   163         -    pParse->rc = SQLITE_AUTH;
   164         -  }else if( rc!=SQLITE_OK ){
   165         -    sqliteAuthBadReturnCode(pParse, rc);
   166    182     }
   167    183   }
   168    184   
   169    185   /*
   170    186   ** Do an authorization check using the code and arguments given.  Return
   171    187   ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
   172    188   ** is returned, then the error count and error message in pParse are
................................................................................
   194    210     }
   195    211     rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
   196    212     if( rc==SQLITE_DENY ){
   197    213       sqlite3ErrorMsg(pParse, "not authorized");
   198    214       pParse->rc = SQLITE_AUTH;
   199    215     }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
   200    216       rc = SQLITE_DENY;
   201         -    sqliteAuthBadReturnCode(pParse, rc);
          217  +    sqliteAuthBadReturnCode(pParse);
   202    218     }
   203    219     return rc;
   204    220   }
   205    221   
   206    222   /*
   207    223   ** Push an authorization context.  After this routine is called, the
   208    224   ** zArg3 argument to authorization callbacks will be zContext until
................................................................................
   209    225   ** popped.  Or if pParse==0, this routine is a no-op.
   210    226   */
   211    227   void sqlite3AuthContextPush(
   212    228     Parse *pParse,
   213    229     AuthContext *pContext, 
   214    230     const char *zContext
   215    231   ){
          232  +  assert( pParse );
   216    233     pContext->pParse = pParse;
   217         -  if( pParse ){
   218         -    pContext->zAuthContext = pParse->zAuthContext;
   219         -    pParse->zAuthContext = zContext;
   220         -  }
          234  +  pContext->zAuthContext = pParse->zAuthContext;
          235  +  pParse->zAuthContext = zContext;
   221    236   }
   222    237   
   223    238   /*
   224    239   ** Pop an authorization context that was previously pushed
   225    240   ** by sqlite3AuthContextPush
   226    241   */
   227    242   void sqlite3AuthContextPop(AuthContext *pContext){

Changes to SQLite.Interop/splitsource/bitvec.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file implements an object that represents a fixed-length
    13     13   ** bitmap.  Bits are numbered starting with 1.
    14     14   **
    15         -** A bitmap is used to record what pages a database file have been
    16         -** journalled during a transaction.  Usually only a few pages are
    17         -** journalled.  So the bitmap is usually sparse and has low cardinality.
           15  +** A bitmap is used to record which pages of a database file have been
           16  +** journalled during a transaction, or which pages have the "dont-write"
           17  +** property.  Usually only a few pages are meet either condition.
           18  +** So the bitmap is usually sparse and has low cardinality.
    18     19   ** But sometimes (for example when during a DROP of a large table) most
    19         -** or all of the pages get journalled.  In those cases, the bitmap becomes
    20         -** dense.  The algorithm needs to handle both cases well.
           20  +** or all of the pages in a database can get journalled.  In those cases, 
           21  +** the bitmap becomes dense with high cardinality.  The algorithm needs 
           22  +** to handle both cases well.
    21     23   **
    22     24   ** The size of the bitmap is fixed when the object is created.
    23     25   **
    24     26   ** All bits are clear when the bitmap is created.  Individual bits
    25     27   ** may be set or cleared one at a time.
    26     28   **
    27     29   ** Test operations are about 100 times more common that set operations.
    28     30   ** Clear operations are exceedingly rare.  There are usually between
    29     31   ** 5 and 500 set operations per Bitvec object, though the number of sets can
    30     32   ** sometimes grow into tens of thousands or larger.  The size of the
    31     33   ** Bitvec object is the number of pages in the database file at the
    32     34   ** start of a transaction, and is thus usually less than a few thousand,
    33     35   ** but can be as large as 2 billion for a really big database.
    34         -**
    35         -** @(#) $Id: bitvec.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
    36     36   */
    37     37   #include "sqliteInt.h"
    38     38   
           39  +/* Size of the Bitvec structure in bytes. */
    39     40   #define BITVEC_SZ        512
           41  +
    40     42   /* Round the union size down to the nearest pointer boundary, since that's how 
    41     43   ** it will be aligned within the Bitvec struct. */
    42         -#define BITVEC_USIZE     (((BITVEC_SZ-12)/sizeof(Bitvec*))*sizeof(Bitvec*))
    43         -#define BITVEC_NCHAR     BITVEC_USIZE
    44         -#define BITVEC_NBIT      (BITVEC_NCHAR*8)
    45         -#define BITVEC_NINT      (BITVEC_USIZE/4)
           44  +#define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
           45  +
           46  +/* Type of the array "element" for the bitmap representation. 
           47  +** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
           48  +** Setting this to the "natural word" size of your CPU may improve
           49  +** performance. */
           50  +#define BITVEC_TELEM     u8
           51  +/* Size, in bits, of the bitmap element. */
           52  +#define BITVEC_SZELEM    8
           53  +/* Number of elements in a bitmap array. */
           54  +#define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
           55  +/* Number of bits in the bitmap array. */
           56  +#define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
           57  +
           58  +/* Number of u32 values in hash table. */
           59  +#define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
           60  +/* Maximum number of entries in hash table before 
           61  +** sub-dividing and re-hashing. */
    46     62   #define BITVEC_MXHASH    (BITVEC_NINT/2)
           63  +/* Hashing function for the aHash representation.
           64  +** Empirical testing showed that the *37 multiplier 
           65  +** (an arbitrary prime)in the hash function provided 
           66  +** no fewer collisions than the no-op *1. */
           67  +#define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
           68  +
    47     69   #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
    48     70   
    49         -#define BITVEC_HASH(X)   (((X)*37)%BITVEC_NINT)
    50     71   
    51     72   /*
    52     73   ** A bitmap is an instance of the following structure.
    53     74   **
    54     75   ** This bitmap records the existance of zero or more bits
    55     76   ** with values between 1 and iSize, inclusive.
    56     77   **
................................................................................
    66     87   ** handles up to iDivisor separate values of i.  apSub[0] holds
    67     88   ** values between 1 and iDivisor.  apSub[1] holds values between
    68     89   ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
    69     90   ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
    70     91   ** to hold deal with values between 1 and iDivisor.
    71     92   */
    72     93   struct Bitvec {
    73         -  u32 iSize;      /* Maximum bit index */
    74         -  u32 nSet;       /* Number of bits that are set */
    75         -  u32 iDivisor;   /* Number of bits handled by each apSub[] entry */
           94  +  u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
           95  +  u32 nSet;       /* Number of bits that are set - only valid for aHash
           96  +                  ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
           97  +                  ** this would be 125. */
           98  +  u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
           99  +                  /* Should >=0 for apSub element. */
          100  +                  /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
          101  +                  /* For a BITVEC_SZ of 512, this would be 34,359,739. */
    76    102     union {
    77         -    u8 aBitmap[BITVEC_NCHAR];    /* Bitmap representation */
          103  +    BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
    78    104       u32 aHash[BITVEC_NINT];      /* Hash table representation */
    79    105       Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
    80    106     } u;
    81    107   };
    82    108   
    83    109   /*
    84    110   ** Create a new bitmap object able to handle bits between 0 and iSize,
................................................................................
    99    125   ** Check to see if the i-th bit is set.  Return true or false.
   100    126   ** If p is NULL (if the bitmap has not been created) or if
   101    127   ** i is out of range, then return false.
   102    128   */
   103    129   int sqlite3BitvecTest(Bitvec *p, u32 i){
   104    130     if( p==0 ) return 0;
   105    131     if( i>p->iSize || i==0 ) return 0;
   106         -  if( p->iSize<=BITVEC_NBIT ){
   107         -    i--;
   108         -    return (p->u.aBitmap[i/8] & (1<<(i&7)))!=0;
          132  +  i--;
          133  +  while( p->iDivisor ){
          134  +    u32 bin = i/p->iDivisor;
          135  +    i = i%p->iDivisor;
          136  +    p = p->u.apSub[bin];
          137  +    if (!p) {
          138  +      return 0;
          139  +    }
   109    140     }
   110         -  if( p->iDivisor>0 ){
   111         -    u32 bin = (i-1)/p->iDivisor;
   112         -    i = (i-1)%p->iDivisor + 1;
   113         -    return sqlite3BitvecTest(p->u.apSub[bin], i);
   114         -  }else{
   115         -    u32 h = BITVEC_HASH(i);
          141  +  if( p->iSize<=BITVEC_NBIT ){
          142  +    return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
          143  +  } else{
          144  +    u32 h = BITVEC_HASH(i++);
   116    145       while( p->u.aHash[h] ){
   117    146         if( p->u.aHash[h]==i ) return 1;
   118         -      h++;
   119         -      if( h>=BITVEC_NINT ) h = 0;
          147  +      h = (h+1) % BITVEC_NINT;
   120    148       }
   121    149       return 0;
   122    150     }
   123    151   }
   124    152   
   125    153   /*
   126    154   ** Set the i-th bit.  Return 0 on success and an error code if
   127    155   ** anything goes wrong.
          156  +**
          157  +** This routine might cause sub-bitmaps to be allocated.  Failing
          158  +** to get the memory needed to hold the sub-bitmap is the only
          159  +** that can go wrong with an insert, assuming p and i are valid.
          160  +**
          161  +** The calling function must ensure that p is a valid Bitvec object
          162  +** and that the value for "i" is within range of the Bitvec object.
          163  +** Otherwise the behavior is undefined.
   128    164   */
   129    165   int sqlite3BitvecSet(Bitvec *p, u32 i){
   130    166     u32 h;
   131         -  assert( p!=0 );
          167  +  if( p==0 ) return SQLITE_OK;
   132    168     assert( i>0 );
   133    169     assert( i<=p->iSize );
   134         -  if( p->iSize<=BITVEC_NBIT ){
   135         -    i--;
   136         -    p->u.aBitmap[i/8] |= 1 << (i&7);
   137         -    return SQLITE_OK;
   138         -  }
   139         -  if( p->iDivisor ){
   140         -    u32 bin = (i-1)/p->iDivisor;
   141         -    i = (i-1)%p->iDivisor + 1;
          170  +  i--;
          171  +  while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
          172  +    u32 bin = i/p->iDivisor;
          173  +    i = i%p->iDivisor;
   142    174       if( p->u.apSub[bin]==0 ){
   143         -      sqlite3BeginBenignMalloc();
   144    175         p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
   145         -      sqlite3EndBenignMalloc();
   146    176         if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
   147    177       }
   148         -    return sqlite3BitvecSet(p->u.apSub[bin], i);
          178  +    p = p->u.apSub[bin];
          179  +  }
          180  +  if( p->iSize<=BITVEC_NBIT ){
          181  +    p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
          182  +    return SQLITE_OK;
          183  +  }
          184  +  h = BITVEC_HASH(i++);
          185  +  /* if there wasn't a hash collision, and this doesn't */
          186  +  /* completely fill the hash, then just add it without */
          187  +  /* worring about sub-dividing and re-hashing. */
          188  +  if( !p->u.aHash[h] ){
          189  +    if (p->nSet<(BITVEC_NINT-1)) {
          190  +      goto bitvec_set_end;
          191  +    } else {
          192  +      goto bitvec_set_rehash;
          193  +    }
   149    194     }
   150         -  h = BITVEC_HASH(i);
   151         -  while( p->u.aHash[h] ){
          195  +  /* there was a collision, check to see if it's already */
          196  +  /* in hash, if not, try to find a spot for it */
          197  +  do {
   152    198       if( p->u.aHash[h]==i ) return SQLITE_OK;
   153    199       h++;
   154         -    if( h==BITVEC_NINT ) h = 0;
   155         -  }
   156         -  p->nSet++;
          200  +    if( h>=BITVEC_NINT ) h = 0;
          201  +  } while( p->u.aHash[h] );
          202  +  /* we didn't find it in the hash.  h points to the first */
          203  +  /* available free spot. check to see if this is going to */
          204  +  /* make our hash too "full".  */
          205  +bitvec_set_rehash:
   157    206     if( p->nSet>=BITVEC_MXHASH ){
   158         -    int j, rc;
   159         -    u32 aiValues[BITVEC_NINT];
   160         -    memcpy(aiValues, p->u.aHash, sizeof(aiValues));
   161         -    memset(p->u.apSub, 0, sizeof(p->u.apSub[0])*BITVEC_NPTR);
   162         -    p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
   163         -    rc = sqlite3BitvecSet(p, i);
   164         -    for(j=0; j<BITVEC_NINT; j++){
   165         -      if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
          207  +    unsigned int j;
          208  +    int rc;
          209  +    u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
          210  +    if( aiValues==0 ){
          211  +      return SQLITE_NOMEM;
          212  +    }else{
          213  +      memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
          214  +      memset(p->u.apSub, 0, sizeof(p->u.apSub));
          215  +      p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
          216  +      rc = sqlite3BitvecSet(p, i);
          217  +      for(j=0; j<BITVEC_NINT; j++){
          218  +        if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
          219  +      }
          220  +      sqlite3StackFree(0, aiValues);
          221  +      return rc;
   166    222       }
   167         -    return rc;
   168    223     }
          224  +bitvec_set_end:
          225  +  p->nSet++;
   169    226     p->u.aHash[h] = i;
   170    227     return SQLITE_OK;
   171    228   }
   172    229   
   173    230   /*
   174         -** Clear the i-th bit.  Return 0 on success and an error code if
   175         -** anything goes wrong.
          231  +** Clear the i-th bit.
          232  +**
          233  +** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
          234  +** that BitvecClear can use to rebuilt its hash table.
   176    235   */
   177         -void sqlite3BitvecClear(Bitvec *p, u32 i){
   178         -  assert( p!=0 );
          236  +void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
          237  +  if( p==0 ) return;
   179    238     assert( i>0 );
   180         -  if( p->iSize<=BITVEC_NBIT ){
   181         -    i--;
   182         -    p->u.aBitmap[i/8] &= ~(1 << (i&7));
   183         -  }else if( p->iDivisor ){
   184         -    u32 bin = (i-1)/p->iDivisor;
   185         -    i = (i-1)%p->iDivisor + 1;
   186         -    if( p->u.apSub[bin] ){
   187         -      sqlite3BitvecClear(p->u.apSub[bin], i);
          239  +  i--;
          240  +  while( p->iDivisor ){
          241  +    u32 bin = i/p->iDivisor;
          242  +    i = i%p->iDivisor;
          243  +    p = p->u.apSub[bin];
          244  +    if (!p) {
          245  +      return;
   188    246       }
          247  +  }
          248  +  if( p->iSize<=BITVEC_NBIT ){
          249  +    p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
   189    250     }else{
   190         -    int j;
   191         -    u32 aiValues[BITVEC_NINT];
   192         -    memcpy(aiValues, p->u.aHash, sizeof(aiValues));
   193         -    memset(p->u.aHash, 0, sizeof(p->u.aHash[0])*BITVEC_NINT);
          251  +    unsigned int j;
          252  +    u32 *aiValues = pBuf;
          253  +    memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
          254  +    memset(p->u.aHash, 0, sizeof(p->u.aHash));
   194    255       p->nSet = 0;
   195    256       for(j=0; j<BITVEC_NINT; j++){
   196         -      if( aiValues[j] && aiValues[j]!=i ){
   197         -        sqlite3BitvecSet(p, aiValues[j]);
          257  +      if( aiValues[j] && aiValues[j]!=(i+1) ){
          258  +        u32 h = BITVEC_HASH(aiValues[j]-1);
          259  +        p->nSet++;
          260  +        while( p->u.aHash[h] ){
          261  +          h++;
          262  +          if( h>=BITVEC_NINT ) h = 0;
          263  +        }
          264  +        p->u.aHash[h] = aiValues[j];
   198    265         }
   199    266       }
   200    267     }
   201    268   }
   202    269   
   203    270   /*
   204    271   ** Destroy a bitmap object.  Reclaim all memory used.
   205    272   */
   206    273   void sqlite3BitvecDestroy(Bitvec *p){
   207    274     if( p==0 ) return;
   208    275     if( p->iDivisor ){
   209         -    int i;
          276  +    unsigned int i;
   210    277       for(i=0; i<BITVEC_NPTR; i++){
   211    278         sqlite3BitvecDestroy(p->u.apSub[i]);
   212    279       }
   213    280     }
   214    281     sqlite3_free(p);
   215    282   }
          283  +
          284  +/*
          285  +** Return the value of the iSize parameter specified when Bitvec *p
          286  +** was created.
          287  +*/
          288  +u32 sqlite3BitvecSize(Bitvec *p){
          289  +  return p->iSize;
          290  +}
   216    291   
   217    292   #ifndef SQLITE_OMIT_BUILTIN_TEST
   218    293   /*
   219    294   ** Let V[] be an array of unsigned characters sufficient to hold
   220    295   ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
   221    296   ** Then the following macros can be used to set, clear, or test
   222    297   ** individual bits within V.
................................................................................
   256    331   ** If a memory allocation error occurs, return -1.
   257    332   */
   258    333   int sqlite3BitvecBuiltinTest(int sz, int *aOp){
   259    334     Bitvec *pBitvec = 0;
   260    335     unsigned char *pV = 0;
   261    336     int rc = -1;
   262    337     int i, nx, pc, op;
          338  +  void *pTmpSpace;
   263    339   
   264    340     /* Allocate the Bitvec to be tested and a linear array of
   265    341     ** bits to act as the reference */
   266    342     pBitvec = sqlite3BitvecCreate( sz );
   267    343     pV = sqlite3_malloc( (sz+7)/8 + 1 );
   268         -  if( pBitvec==0 || pV==0 ) goto bitvec_end;
          344  +  pTmpSpace = sqlite3_malloc(BITVEC_SZ);
          345  +  if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
   269    346     memset(pV, 0, (sz+7)/8 + 1);
   270    347   
          348  +  /* NULL pBitvec tests */
          349  +  sqlite3BitvecSet(0, 1);
          350  +  sqlite3BitvecClear(0, 1, pTmpSpace);
          351  +
   271    352     /* Run the program */
   272    353     pc = 0;
   273    354     while( (op = aOp[pc])!=0 ){
   274    355       switch( op ){
   275    356         case 1:
   276    357         case 2:
   277    358         case 5: {
................................................................................
   294    375       if( (op & 1)!=0 ){
   295    376         SETBIT(pV, (i+1));
   296    377         if( op!=5 ){
   297    378           if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
   298    379         }
   299    380       }else{
   300    381         CLEARBIT(pV, (i+1));
   301         -      sqlite3BitvecClear(pBitvec, i+1);
          382  +      sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
   302    383       }
   303    384     }
   304    385   
   305    386     /* Test to make sure the linear array exactly matches the
   306    387     ** Bitvec object.  Start with the assumption that they do
   307    388     ** match (rc==0).  Change rc to non-zero if a discrepancy
   308    389     ** is found.
   309    390     */
   310    391     rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
   311         -          + sqlite3BitvecTest(pBitvec, 0);
          392  +          + sqlite3BitvecTest(pBitvec, 0)
          393  +          + (sqlite3BitvecSize(pBitvec) - sz);
   312    394     for(i=1; i<=sz; i++){
   313    395       if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
   314    396         rc = i;
   315    397         break;
   316    398       }
   317    399     }
   318    400   
   319    401     /* Free allocated structure */
   320    402   bitvec_end:
          403  +  sqlite3_free(pTmpSpace);
   321    404     sqlite3_free(pV);
   322    405     sqlite3BitvecDestroy(pBitvec);
   323    406     return rc;
   324    407   }
   325    408   #endif /* SQLITE_OMIT_BUILTIN_TEST */

Changes to SQLite.Interop/splitsource/btmutex.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13         -** $Id: btmutex.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
    14         -**
    15     13   ** This file contains code used to implement mutexes on Btree objects.
    16     14   ** This code really belongs in btree.c.  But btree.c is getting too
    17     15   ** big and we want to break it down some.  This packaged seemed like
    18     16   ** a good breakout.
    19     17   */
    20     18   #include "btreeInt.h"
    21         -#if SQLITE_THREADSAFE && !defined(SQLITE_OMIT_SHARED_CACHE)
           19  +#ifndef SQLITE_OMIT_SHARED_CACHE
           20  +#if SQLITE_THREADSAFE
    22     21   
           22  +/*
           23  +** Obtain the BtShared mutex associated with B-Tree handle p. Also,
           24  +** set BtShared.db to the database handle associated with p and the
           25  +** p->locked boolean to true.
           26  +*/
           27  +static void lockBtreeMutex(Btree *p){
           28  +  assert( p->locked==0 );
           29  +  assert( sqlite3_mutex_notheld(p->pBt->mutex) );
           30  +  assert( sqlite3_mutex_held(p->db->mutex) );
           31  +
           32  +  sqlite3_mutex_enter(p->pBt->mutex);
           33  +  p->pBt->db = p->db;
           34  +  p->locked = 1;
           35  +}
           36  +
           37  +/*
           38  +** Release the BtShared mutex associated with B-Tree handle p and
           39  +** clear the p->locked boolean.
           40  +*/
           41  +static void unlockBtreeMutex(Btree *p){
           42  +  assert( p->locked==1 );
           43  +  assert( sqlite3_mutex_held(p->pBt->mutex) );
           44  +  assert( sqlite3_mutex_held(p->db->mutex) );
           45  +  assert( p->db==p->pBt->db );
           46  +
           47  +  sqlite3_mutex_leave(p->pBt->mutex);
           48  +  p->locked = 0;
           49  +}
    23     50   
    24     51   /*
    25     52   ** Enter a mutex on the given BTree object.
    26     53   **
    27     54   ** If the object is not sharable, then no mutex is ever required
    28     55   ** and this routine is a no-op.  The underlying mutex is non-recursive.
    29     56   ** But we keep a reference count in Btree.wantToLock so the behavior
................................................................................
    53     80     /* Check for locking consistency */
    54     81     assert( !p->locked || p->wantToLock>0 );
    55     82     assert( p->sharable || p->wantToLock==0 );
    56     83   
    57     84     /* We should already hold a lock on the database connection */
    58     85     assert( sqlite3_mutex_held(p->db->mutex) );
    59     86   
           87  +  /* Unless the database is sharable and unlocked, then BtShared.db
           88  +  ** should already be set correctly. */
           89  +  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
           90  +
    60     91     if( !p->sharable ) return;
    61     92     p->wantToLock++;
    62     93     if( p->locked ) return;
    63     94   
    64         -#ifndef SQLITE_MUTEX_NOOP
    65     95     /* In most cases, we should be able to acquire the lock we
    66     96     ** want without having to go throught the ascending lock
    67     97     ** procedure that follows.  Just be sure not to block.
    68     98     */
    69     99     if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
          100  +    p->pBt->db = p->db;
    70    101       p->locked = 1;
    71    102       return;
    72    103     }
    73    104   
    74    105     /* To avoid deadlock, first release all locks with a larger
    75    106     ** BtShared address.  Then acquire our lock.  Then reacquire
    76    107     ** the other BtShared locks that we used to hold in ascending
................................................................................
    77    108     ** order.
    78    109     */
    79    110     for(pLater=p->pNext; pLater; pLater=pLater->pNext){
    80    111       assert( pLater->sharable );
    81    112       assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
    82    113       assert( !pLater->locked || pLater->wantToLock>0 );
    83    114       if( pLater->locked ){
    84         -      sqlite3_mutex_leave(pLater->pBt->mutex);
    85         -      pLater->locked = 0;
          115  +      unlockBtreeMutex(pLater);
    86    116       }
    87    117     }
    88         -  sqlite3_mutex_enter(p->pBt->mutex);
    89         -  p->locked = 1;
          118  +  lockBtreeMutex(p);
    90    119     for(pLater=p->pNext; pLater; pLater=pLater->pNext){
    91    120       if( pLater->wantToLock ){
    92         -      sqlite3_mutex_enter(pLater->pBt->mutex);
    93         -      pLater->locked = 1;
          121  +      lockBtreeMutex(pLater);
    94    122       }
    95    123     }
    96         -#endif /* SQLITE_MUTEX_NOOP */
    97    124   }
    98    125   
    99    126   /*
   100    127   ** Exit the recursive mutex on a Btree.
   101    128   */
   102    129   void sqlite3BtreeLeave(Btree *p){
   103    130     if( p->sharable ){
   104    131       assert( p->wantToLock>0 );
   105    132       p->wantToLock--;
   106    133       if( p->wantToLock==0 ){
   107         -      assert( p->locked );
   108         -      sqlite3_mutex_leave(p->pBt->mutex);
   109         -      p->locked = 0;
          134  +      unlockBtreeMutex(p);
   110    135       }
   111    136     }
   112    137   }
   113    138   
   114    139   #ifndef NDEBUG
   115    140   /*
   116         -** Return true if the BtShared mutex is held on the btree.  
   117         -**
   118         -** This routine makes no determination one why or another if the
   119         -** database connection mutex is held.
          141  +** Return true if the BtShared mutex is held on the btree, or if the
          142  +** B-Tree is not marked as sharable.
   120    143   **
   121    144   ** This routine is used only from within assert() statements.
   122    145   */
   123    146   int sqlite3BtreeHoldsMutex(Btree *p){
   124         -  return (p->sharable==0 ||
   125         -             (p->locked && p->wantToLock && sqlite3_mutex_held(p->pBt->mutex)));
          147  +  assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
          148  +  assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
          149  +  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
          150  +  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
          151  +
          152  +  return (p->sharable==0 || p->locked);
   126    153   }
   127    154   #endif
   128    155   
   129    156   
   130    157   #ifndef SQLITE_OMIT_INCRBLOB
   131    158   /*
   132    159   ** Enter and leave a mutex on a Btree given a cursor owned by that
................................................................................
   158    185   */
   159    186   void sqlite3BtreeEnterAll(sqlite3 *db){
   160    187     int i;
   161    188     Btree *p, *pLater;
   162    189     assert( sqlite3_mutex_held(db->mutex) );
   163    190     for(i=0; i<db->nDb; i++){
   164    191       p = db->aDb[i].pBt;
          192  +    assert( !p || (p->locked==0 && p->sharable) || p->pBt->db==p->db );
   165    193       if( p && p->sharable ){
   166    194         p->wantToLock++;
   167    195         if( !p->locked ){
   168    196           assert( p->wantToLock==1 );
   169    197           while( p->pPrev ) p = p->pPrev;
   170         -        while( p->locked && p->pNext ) p = p->pNext;
          198  +        /* Reason for ALWAYS:  There must be at least on unlocked Btree in
          199  +        ** the chain.  Otherwise the !p->locked test above would have failed */
          200  +        while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
   171    201           for(pLater = p->pNext; pLater; pLater=pLater->pNext){
   172    202             if( pLater->locked ){
   173         -            sqlite3_mutex_leave(pLater->pBt->mutex);
   174         -            pLater->locked = 0;
          203  +            unlockBtreeMutex(pLater);
   175    204             }
   176    205           }
   177    206           while( p ){
   178         -          sqlite3_mutex_enter(p->pBt->mutex);
   179         -          p->locked++;
          207  +          lockBtreeMutex(p);
   180    208             p = p->pNext;
   181    209           }
   182    210         }
   183    211       }
   184    212     }
   185    213   }
   186    214   void sqlite3BtreeLeaveAll(sqlite3 *db){
................................................................................
   189    217     assert( sqlite3_mutex_held(db->mutex) );
   190    218     for(i=0; i<db->nDb; i++){
   191    219       p = db->aDb[i].pBt;
   192    220       if( p && p->sharable ){
   193    221         assert( p->wantToLock>0 );
   194    222         p->wantToLock--;
   195    223         if( p->wantToLock==0 ){
   196         -        assert( p->locked );
   197         -        sqlite3_mutex_leave(p->pBt->mutex);
   198         -        p->locked = 0;
          224  +        unlockBtreeMutex(p);
   199    225         }
   200    226       }
   201    227     }
   202    228   }
   203    229   
   204    230   #ifndef NDEBUG
   205    231   /*
................................................................................
   246    272     {
   247    273       for(i=0; i<pArray->nMutex; i++){
   248    274         assert( pArray->aBtree[i]!=pBtree );
   249    275       }
   250    276     }
   251    277   #endif
   252    278     assert( pArray->nMutex>=0 );
   253         -  assert( pArray->nMutex<sizeof(pArray->aBtree)/sizeof(pArray->aBtree[0])-1 );
          279  +  assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
   254    280     pBt = pBtree->pBt;
   255    281     for(i=0; i<pArray->nMutex; i++){
   256    282       assert( pArray->aBtree[i]!=pBtree );
   257    283       if( pArray->aBtree[i]->pBt>pBt ){
   258    284         for(j=pArray->nMutex; j>i; j--){
   259    285           pArray->aBtree[j] = pArray->aBtree[j-1];
   260    286         }
................................................................................
   278    304       /* Some basic sanity checking */
   279    305       assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
   280    306       assert( !p->locked || p->wantToLock>0 );
   281    307   
   282    308       /* We should already hold a lock on the database connection */
   283    309       assert( sqlite3_mutex_held(p->db->mutex) );
   284    310   
          311  +    /* The Btree is sharable because only sharable Btrees are entered
          312  +    ** into the array in the first place. */
          313  +    assert( p->sharable );
          314  +
   285    315       p->wantToLock++;
   286         -    if( !p->locked && p->sharable ){
   287         -      sqlite3_mutex_enter(p->pBt->mutex);
   288         -      p->locked = 1;
          316  +    if( !p->locked ){
          317  +      lockBtreeMutex(p);
   289    318       }
   290    319     }
   291    320   }
   292    321   
   293    322   /*
   294    323   ** Leave the mutex of every btree in the group.
   295    324   */
   296    325   void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
   297    326     int i;
   298    327     for(i=0; i<pArray->nMutex; i++){
   299    328       Btree *p = pArray->aBtree[i];
   300    329       /* Some basic sanity checking */
   301    330       assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
   302         -    assert( p->locked || !p->sharable );
          331  +    assert( p->locked );
   303    332       assert( p->wantToLock>0 );
   304    333   
   305    334       /* We should already hold a lock on the database connection */
   306    335       assert( sqlite3_mutex_held(p->db->mutex) );
   307    336   
   308    337       p->wantToLock--;
   309         -    if( p->wantToLock==0 && p->locked ){
   310         -      sqlite3_mutex_leave(p->pBt->mutex);
   311         -      p->locked = 0;
          338  +    if( p->wantToLock==0 ){
          339  +      unlockBtreeMutex(p);
   312    340       }
   313    341     }
   314    342   }
   315    343   
   316         -
   317         -#endif  /* SQLITE_THREADSAFE && !SQLITE_OMIT_SHARED_CACHE */
          344  +#else
          345  +void sqlite3BtreeEnter(Btree *p){
          346  +  p->pBt->db = p->db;
          347  +}
          348  +void sqlite3BtreeEnterAll(sqlite3 *db){
          349  +  int i;
          350  +  for(i=0; i<db->nDb; i++){
          351  +    Btree *p = db->aDb[i].pBt;
          352  +    if( p ){
          353  +      p->pBt->db = p->db;
          354  +    }
          355  +  }
          356  +}
          357  +#endif /* if SQLITE_THREADSAFE */
          358  +#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */

Changes to SQLite.Interop/splitsource/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
    13         -**
    14     12   ** This file implements a external (disk-based) database using BTrees.
    15     13   ** See the header comment on "btreeInt.h" for additional information.
    16     14   ** Including a description of file format and an overview of operation.
    17     15   */
    18     16   #include "btreeInt.h"
    19     17   
    20     18   /*
................................................................................
    24     22   static const char zMagicHeader[] = SQLITE_FILE_HEADER;
    25     23   
    26     24   /*
    27     25   ** Set this global variable to 1 to enable tracing using the TRACE
    28     26   ** macro.
    29     27   */
    30     28   #if 0
    31         -int sqlite3BtreeTrace=0;  /* True to enable tracing */
           29  +int sqlite3BtreeTrace=1;  /* True to enable tracing */
    32     30   # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
    33     31   #else
    34     32   # define TRACE(X)
    35     33   #endif
    36     34   
    37         -
           35  +/*
           36  +** Extract a 2-byte big-endian integer from an array of unsigned bytes.
           37  +** But if the value is zero, make it 65536.
           38  +**
           39  +** This routine is used to extract the "offset to cell content area" value
           40  +** from the header of a btree page.  If the page size is 65536 and the page
           41  +** is empty, the offset should be 65536, but the 2-byte value stores zero.
           42  +** This routine makes the necessary adjustment to 65536.
           43  +*/
           44  +#define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
    38     45   
    39     46   #ifndef SQLITE_OMIT_SHARED_CACHE
    40     47   /*
    41         -** A flag to indicate whether or not shared cache is enabled.  Also,
    42         -** a list of BtShared objects that are eligible for participation
    43         -** in shared cache.  The variables have file scope during normal builds,
    44         -** but the test harness needs to access these variables so we make them
    45         -** global for test builds.
           48  +** A list of BtShared objects that are eligible for participation
           49  +** in shared cache.  This variable has file scope during normal builds,
           50  +** but the test harness needs to access it so we make it global for 
           51  +** test builds.
           52  +**
           53  +** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
    46     54   */
    47     55   #ifdef SQLITE_TEST
    48         -BtShared *sqlite3SharedCacheList = 0;
    49         -int sqlite3SharedCacheEnabled = 0;
           56  +BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
    50     57   #else
    51         -static BtShared *sqlite3SharedCacheList = 0;
    52         -static int sqlite3SharedCacheEnabled = 0;
           58  +static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
    53     59   #endif
    54     60   #endif /* SQLITE_OMIT_SHARED_CACHE */
    55     61   
    56     62   #ifndef SQLITE_OMIT_SHARED_CACHE
    57     63   /*
    58     64   ** Enable or disable the shared pager and schema features.
    59     65   **
    60     66   ** This routine has no effect on existing database connections.
    61     67   ** The shared cache setting effects only future calls to
    62     68   ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
    63     69   */
    64     70   int sqlite3_enable_shared_cache(int enable){
    65         -  sqlite3SharedCacheEnabled = enable;
           71  +  sqlite3GlobalConfig.sharedCacheEnabled = enable;
    66     72     return SQLITE_OK;
    67     73   }
    68     74   #endif
    69     75   
    70     76   
    71         -/*
    72         -** Forward declaration
    73         -*/
    74         -static int checkReadLocks(Btree*, Pgno, BtCursor*, i64);
    75         -
    76     77   
    77     78   #ifdef SQLITE_OMIT_SHARED_CACHE
    78     79     /*
    79         -  ** The functions queryTableLock(), lockTable() and unlockAllTables()
           80  +  ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
           81  +  ** and clearAllSharedCacheTableLocks()
    80     82     ** manipulate entries in the BtShared.pLock linked list used to store
    81     83     ** shared-cache table level locks. If the library is compiled with the
    82     84     ** shared-cache feature disabled, then there is only ever one user
    83     85     ** of each BtShared structure and so this locking is not necessary. 
    84     86     ** So define the lock related functions as no-ops.
    85     87     */
    86         -  #define queryTableLock(a,b,c) SQLITE_OK
    87         -  #define lockTable(a,b,c) SQLITE_OK
    88         -  #define unlockAllTables(a)
           88  +  #define querySharedCacheTableLock(a,b,c) SQLITE_OK
           89  +  #define setSharedCacheTableLock(a,b,c) SQLITE_OK
           90  +  #define clearAllSharedCacheTableLocks(a)
           91  +  #define downgradeAllSharedCacheTableLocks(a)
           92  +  #define hasSharedCacheTableLock(a,b,c,d) 1
           93  +  #define hasReadConflicts(a, b) 0
    89     94   #endif
    90     95   
    91     96   #ifndef SQLITE_OMIT_SHARED_CACHE
           97  +
           98  +#ifdef SQLITE_DEBUG
    92     99   /*
    93         -** Query to see if btree handle p may obtain a lock of type eLock 
          100  +**** This function is only used as part of an assert() statement. ***
          101  +**
          102  +** Check to see if pBtree holds the required locks to read or write to the 
          103  +** table with root page iRoot.   Return 1 if it does and 0 if not.
          104  +**
          105  +** For example, when writing to a table with root-page iRoot via 
          106  +** Btree connection pBtree:
          107  +**
          108  +**    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
          109  +**
          110  +** When writing to an index that resides in a sharable database, the 
          111  +** caller should have first obtained a lock specifying the root page of
          112  +** the corresponding table. This makes things a bit more complicated,
          113  +** as this module treats each table as a separate structure. To determine
          114  +** the table corresponding to the index being written, this
          115  +** function has to search through the database schema.
          116  +**
          117  +** Instead of a lock on the table/index rooted at page iRoot, the caller may
          118  +** hold a write-lock on the schema table (root page 1). This is also
          119  +** acceptable.
          120  +*/
          121  +static int hasSharedCacheTableLock(
          122  +  Btree *pBtree,         /* Handle that must hold lock */
          123  +  Pgno iRoot,            /* Root page of b-tree */
          124  +  int isIndex,           /* True if iRoot is the root of an index b-tree */
          125  +  int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
          126  +){
          127  +  Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
          128  +  Pgno iTab = 0;
          129  +  BtLock *pLock;
          130  +
          131  +  /* If this database is not shareable, or if the client is reading
          132  +  ** and has the read-uncommitted flag set, then no lock is required. 
          133  +  ** Return true immediately.
          134  +  */
          135  +  if( (pBtree->sharable==0)
          136  +   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
          137  +  ){
          138  +    return 1;
          139  +  }
          140  +
          141  +  /* If the client is reading  or writing an index and the schema is
          142  +  ** not loaded, then it is too difficult to actually check to see if
          143  +  ** the correct locks are held.  So do not bother - just return true.
          144  +  ** This case does not come up very often anyhow.
          145  +  */
          146  +  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
          147  +    return 1;
          148  +  }
          149  +
          150  +  /* Figure out the root-page that the lock should be held on. For table
          151  +  ** b-trees, this is just the root page of the b-tree being read or
          152  +  ** written. For index b-trees, it is the root page of the associated
          153  +  ** table.  */
          154  +  if( isIndex ){
          155  +    HashElem *p;
          156  +    for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
          157  +      Index *pIdx = (Index *)sqliteHashData(p);
          158  +      if( pIdx->tnum==(int)iRoot ){
          159  +        iTab = pIdx->pTable->tnum;
          160  +      }
          161  +    }
          162  +  }else{
          163  +    iTab = iRoot;
          164  +  }
          165  +
          166  +  /* Search for the required lock. Either a write-lock on root-page iTab, a 
          167  +  ** write-lock on the schema table, or (if the client is reading) a
          168  +  ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
          169  +  for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
          170  +    if( pLock->pBtree==pBtree 
          171  +     && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
          172  +     && pLock->eLock>=eLockType 
          173  +    ){
          174  +      return 1;
          175  +    }
          176  +  }
          177  +
          178  +  /* Failed to find the required lock. */
          179  +  return 0;
          180  +}
          181  +#endif /* SQLITE_DEBUG */
          182  +
          183  +#ifdef SQLITE_DEBUG
          184  +/*
          185  +**** This function may be used as part of assert() statements only. ****
          186  +**
          187  +** Return true if it would be illegal for pBtree to write into the
          188  +** table or index rooted at iRoot because other shared connections are
          189  +** simultaneously reading that same table or index.
          190  +**
          191  +** It is illegal for pBtree to write if some other Btree object that
          192  +** shares the same BtShared object is currently reading or writing
          193  +** the iRoot table.  Except, if the other Btree object has the
          194  +** read-uncommitted flag set, then it is OK for the other object to
          195  +** have a read cursor.
          196  +**
          197  +** For example, before writing to any part of the table or index
          198  +** rooted at page iRoot, one should call:
          199  +**
          200  +**    assert( !hasReadConflicts(pBtree, iRoot) );
          201  +*/
          202  +static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
          203  +  BtCursor *p;
          204  +  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
          205  +    if( p->pgnoRoot==iRoot 
          206  +     && p->pBtree!=pBtree
          207  +     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
          208  +    ){
          209  +      return 1;
          210  +    }
          211  +  }
          212  +  return 0;
          213  +}
          214  +#endif    /* #ifdef SQLITE_DEBUG */
          215  +
          216  +/*
          217  +** Query to see if Btree handle p may obtain a lock of type eLock 
    94    218   ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
    95         -** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
    96         -** SQLITE_LOCKED if not.
          219  +** SQLITE_OK if the lock may be obtained (by calling
          220  +** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
    97    221   */
    98         -static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
          222  +static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
    99    223     BtShared *pBt = p->pBt;
   100    224     BtLock *pIter;
   101    225   
   102    226     assert( sqlite3BtreeHoldsMutex(p) );
   103    227     assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
   104    228     assert( p->db!=0 );
          229  +  assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
   105    230     
   106         -  /* This is a no-op if the shared-cache is not enabled */
          231  +  /* If requesting a write-lock, then the Btree must have an open write
          232  +  ** transaction on this file. And, obviously, for this to be so there 
          233  +  ** must be an open write transaction on the file itself.
          234  +  */
          235  +  assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
          236  +  assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
          237  +  
          238  +  /* This routine is a no-op if the shared-cache is not enabled */
   107    239     if( !p->sharable ){
   108    240       return SQLITE_OK;
   109    241     }
   110    242   
   111    243     /* If some other connection is holding an exclusive lock, the
   112    244     ** requested lock may not be obtained.
   113    245     */
   114         -  if( pBt->pExclusive && pBt->pExclusive!=p ){
   115         -    return SQLITE_LOCKED;
   116         -  }
   117         -
   118         -  /* This (along with lockTable()) is where the ReadUncommitted flag is
   119         -  ** dealt with. If the caller is querying for a read-lock and the flag is
   120         -  ** set, it is unconditionally granted - even if there are write-locks
   121         -  ** on the table. If a write-lock is requested, the ReadUncommitted flag
   122         -  ** is not considered.
   123         -  **
   124         -  ** In function lockTable(), if a read-lock is demanded and the 
   125         -  ** ReadUncommitted flag is set, no entry is added to the locks list 
   126         -  ** (BtShared.pLock).
   127         -  **
   128         -  ** To summarize: If the ReadUncommitted flag is set, then read cursors do
   129         -  ** not create or respect table locks. The locking procedure for a 
   130         -  ** write-cursor does not change.
   131         -  */
   132         -  if( 
   133         -    0==(p->db->flags&SQLITE_ReadUncommitted) || 
   134         -    eLock==WRITE_LOCK ||
   135         -    iTab==MASTER_ROOT
   136         -  ){
   137         -    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
   138         -      if( pIter->pBtree!=p && pIter->iTable==iTab && 
   139         -          (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
   140         -        return SQLITE_LOCKED;
   141         -      }
          246  +  if( pBt->pWriter!=p && pBt->isExclusive ){
          247  +    sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
          248  +    return SQLITE_LOCKED_SHAREDCACHE;
          249  +  }
          250  +
          251  +  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
          252  +    /* The condition (pIter->eLock!=eLock) in the following if(...) 
          253  +    ** statement is a simplification of:
          254  +    **
          255  +    **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
          256  +    **
          257  +    ** since we know that if eLock==WRITE_LOCK, then no other connection
          258  +    ** may hold a WRITE_LOCK on any table in this file (since there can
          259  +    ** only be a single writer).
          260  +    */
          261  +    assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
          262  +    assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
          263  +    if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
          264  +      sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
          265  +      if( eLock==WRITE_LOCK ){
          266  +        assert( p==pBt->pWriter );
          267  +        pBt->isPending = 1;
          268  +      }
          269  +      return SQLITE_LOCKED_SHAREDCACHE;
   142    270       }
   143    271     }
   144    272     return SQLITE_OK;
   145    273   }
   146    274   #endif /* !SQLITE_OMIT_SHARED_CACHE */
   147    275   
   148    276   #ifndef SQLITE_OMIT_SHARED_CACHE
   149    277   /*
   150    278   ** Add a lock on the table with root-page iTable to the shared-btree used
   151    279   ** by Btree handle p. Parameter eLock must be either READ_LOCK or 
   152    280   ** WRITE_LOCK.
   153    281   **
   154         -** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
   155         -** SQLITE_NOMEM may also be returned.
          282  +** This function assumes the following:
          283  +**
          284  +**   (a) The specified Btree object p is connected to a sharable
          285  +**       database (one with the BtShared.sharable flag set), and
          286  +**
          287  +**   (b) No other Btree objects hold a lock that conflicts
          288  +**       with the requested lock (i.e. querySharedCacheTableLock() has
          289  +**       already been called and returned SQLITE_OK).
          290  +**
          291  +** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 
          292  +** is returned if a malloc attempt fails.
   156    293   */
   157         -static int lockTable(Btree *p, Pgno iTable, u8 eLock){
          294  +static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
   158    295     BtShared *pBt = p->pBt;
   159    296     BtLock *pLock = 0;
   160    297     BtLock *pIter;
   161    298   
   162    299     assert( sqlite3BtreeHoldsMutex(p) );
   163    300     assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
   164    301     assert( p->db!=0 );
   165    302   
   166         -  /* This is a no-op if the shared-cache is not enabled */
   167         -  if( !p->sharable ){
   168         -    return SQLITE_OK;
   169         -  }
          303  +  /* A connection with the read-uncommitted flag set will never try to
          304  +  ** obtain a read-lock using this function. The only read-lock obtained
          305  +  ** by a connection in read-uncommitted mode is on the sqlite_master 
          306  +  ** table, and that lock is obtained in BtreeBeginTrans().  */
          307  +  assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
   170    308   
   171         -  assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
   172         -
   173         -  /* If the read-uncommitted flag is set and a read-lock is requested,
   174         -  ** return early without adding an entry to the BtShared.pLock list. See
   175         -  ** comment in function queryTableLock() for more info on handling 
   176         -  ** the ReadUncommitted flag.
   177         -  */
   178         -  if( 
   179         -    (p->db->flags&SQLITE_ReadUncommitted) && 
   180         -    (eLock==READ_LOCK) &&
   181         -    iTable!=MASTER_ROOT
   182         -  ){
   183         -    return SQLITE_OK;
   184         -  }
          309  +  /* This function should only be called on a sharable b-tree after it 
          310  +  ** has been determined that no other b-tree holds a conflicting lock.  */
          311  +  assert( p->sharable );
          312  +  assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
   185    313   
   186    314     /* First search the list for an existing lock on this table. */
   187    315     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
   188    316       if( pIter->iTable==iTable && pIter->pBtree==p ){
   189    317         pLock = pIter;
   190    318         break;
   191    319       }
................................................................................
   216    344   
   217    345     return SQLITE_OK;
   218    346   }
   219    347   #endif /* !SQLITE_OMIT_SHARED_CACHE */
   220    348   
   221    349   #ifndef SQLITE_OMIT_SHARED_CACHE
   222    350   /*
   223         -** Release all the table locks (locks obtained via calls to the lockTable()
   224         -** procedure) held by Btree handle p.
          351  +** Release all the table locks (locks obtained via calls to
          352  +** the setSharedCacheTableLock() procedure) held by Btree object p.
          353  +**
          354  +** This function assumes that Btree p has an open read or write 
          355  +** transaction. If it does not, then the BtShared.isPending variable
          356  +** may be incorrectly cleared.
   225    357   */
   226         -static void unlockAllTables(Btree *p){
          358  +static void clearAllSharedCacheTableLocks(Btree *p){
   227    359     BtShared *pBt = p->pBt;
   228    360     BtLock **ppIter = &pBt->pLock;
   229    361   
   230    362     assert( sqlite3BtreeHoldsMutex(p) );
   231    363     assert( p->sharable || 0==*ppIter );
          364  +  assert( p->inTrans>0 );
   232    365   
   233    366     while( *ppIter ){
   234    367       BtLock *pLock = *ppIter;
   235         -    assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree );
          368  +    assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
          369  +    assert( pLock->pBtree->inTrans>=pLock->eLock );
   236    370       if( pLock->pBtree==p ){
   237    371         *ppIter = pLock->pNext;
   238         -      sqlite3_free(pLock);
          372  +      assert( pLock->iTable!=1 || pLock==&p->lock );
          373  +      if( pLock->iTable!=1 ){
          374  +        sqlite3_free(pLock);
          375  +      }
   239    376       }else{
   240    377         ppIter = &pLock->pNext;
   241    378       }
   242    379     }
   243    380   
   244         -  if( pBt->pExclusive==p ){
   245         -    pBt->pExclusive = 0;
          381  +  assert( pBt->isPending==0 || pBt->pWriter );
          382  +  if( pBt->pWriter==p ){
          383  +    pBt->pWriter = 0;
          384  +    pBt->isExclusive = 0;
          385  +    pBt->isPending = 0;
          386  +  }else if( pBt->nTransaction==2 ){
          387  +    /* This function is called when Btree p is concluding its 
          388  +    ** transaction. If there currently exists a writer, and p is not
          389  +    ** that writer, then the number of locks held by connections other
          390  +    ** than the writer must be about to drop to zero. In this case
          391  +    ** set the isPending flag to 0.
          392  +    **
          393  +    ** If there is not currently a writer, then BtShared.isPending must
          394  +    ** be zero already. So this next line is harmless in that case.
          395  +    */
          396  +    pBt->isPending = 0;
   246    397     }
   247    398   }
          399  +
          400  +/*
          401  +** This function changes all write-locks held by Btree p into read-locks.
          402  +*/
          403  +static void downgradeAllSharedCacheTableLocks(Btree *p){
          404  +  BtShared *pBt = p->pBt;
          405  +  if( pBt->pWriter==p ){
          406  +    BtLock *pLock;
          407  +    pBt->pWriter = 0;
          408  +    pBt->isExclusive = 0;
          409  +    pBt->isPending = 0;
          410  +    for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
          411  +      assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
          412  +      pLock->eLock = READ_LOCK;
          413  +    }
          414  +  }
          415  +}
          416  +
   248    417   #endif /* SQLITE_OMIT_SHARED_CACHE */
   249    418   
   250    419   static void releasePage(MemPage *pPage);  /* Forward reference */
   251    420   
   252    421   /*
   253         -** Verify that the cursor holds a mutex on the BtShared
          422  +***** This routine is used inside of assert() only ****
          423  +**
          424  +** Verify that the cursor holds the mutex on its BtShared
   254    425   */
   255         -#ifndef NDEBUG
          426  +#ifdef SQLITE_DEBUG
   256    427   static int cursorHoldsMutex(BtCursor *p){
   257    428     return sqlite3_mutex_held(p->pBt->mutex);
   258    429   }
   259    430   #endif
   260    431   
   261    432   
   262    433   #ifndef SQLITE_OMIT_INCRBLOB
................................................................................
   276    447   static void invalidateAllOverflowCache(BtShared *pBt){
   277    448     BtCursor *p;
   278    449     assert( sqlite3_mutex_held(pBt->mutex) );
   279    450     for(p=pBt->pCursor; p; p=p->pNext){
   280    451       invalidateOverflowCache(p);
   281    452     }
   282    453   }
          454  +
          455  +/*
          456  +** This function is called before modifying the contents of a table
          457  +** to invalidate any incrblob cursors that are open on the
          458  +** row or one of the rows being modified.
          459  +**
          460  +** If argument isClearTable is true, then the entire contents of the
          461  +** table is about to be deleted. In this case invalidate all incrblob
          462  +** cursors open on any row within the table with root-page pgnoRoot.
          463  +**
          464  +** Otherwise, if argument isClearTable is false, then the row with
          465  +** rowid iRow is being replaced or deleted. In this case invalidate
          466  +** only those incrblob cursors open on that specific row.
          467  +*/
          468  +static void invalidateIncrblobCursors(
          469  +  Btree *pBtree,          /* The database file to check */
          470  +  i64 iRow,               /* The rowid that might be changing */
          471  +  int isClearTable        /* True if all rows are being deleted */
          472  +){
          473  +  BtCursor *p;
          474  +  BtShared *pBt = pBtree->pBt;
          475  +  assert( sqlite3BtreeHoldsMutex(pBtree) );
          476  +  for(p=pBt->pCursor; p; p=p->pNext){
          477  +    if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
          478  +      p->eState = CURSOR_INVALID;
          479  +    }
          480  +  }
          481  +}
          482  +
   283    483   #else
          484  +  /* Stub functions when INCRBLOB is omitted */
   284    485     #define invalidateOverflowCache(x)
   285    486     #define invalidateAllOverflowCache(x)
   286         -#endif
          487  +  #define invalidateIncrblobCursors(x,y,z)
          488  +#endif /* SQLITE_OMIT_INCRBLOB */
          489  +
          490  +/*
          491  +** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
          492  +** when a page that previously contained data becomes a free-list leaf 
          493  +** page.
          494  +**
          495  +** The BtShared.pHasContent bitvec exists to work around an obscure
          496  +** bug caused by the interaction of two useful IO optimizations surrounding
          497  +** free-list leaf pages:
          498  +**
          499  +**   1) When all data is deleted from a page and the page becomes
          500  +**      a free-list leaf page, the page is not written to the database
          501  +**      (as free-list leaf pages contain no meaningful data). Sometimes
          502  +**      such a page is not even journalled (as it will not be modified,
          503  +**      why bother journalling it?).
          504  +**
          505  +**   2) When a free-list leaf page is reused, its content is not read
          506  +**      from the database or written to the journal file (why should it
          507  +**      be, if it is not at all meaningful?).
          508  +**
          509  +** By themselves, these optimizations work fine and provide a handy
          510  +** performance boost to bulk delete or insert operations. However, if
          511  +** a page is moved to the free-list and then reused within the same
          512  +** transaction, a problem comes up. If the page is not journalled when
          513  +** it is moved to the free-list and it is also not journalled when it
          514  +** is extracted from the free-list and reused, then the original data
          515  +** may be lost. In the event of a rollback, it may not be possible
          516  +** to restore the database to its original configuration.
          517  +**
          518  +** The solution is the BtShared.pHasContent bitvec. Whenever a page is 
          519  +** moved to become a free-list leaf page, the corresponding bit is
          520  +** set in the bitvec. Whenever a leaf page is extracted from the free-list,
          521  +** optimization 2 above is omitted if the corresponding bit is already
          522  +** set in BtShared.pHasContent. The contents of the bitvec are cleared
          523  +** at the end of every transaction.
          524  +*/
          525  +static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
          526  +  int rc = SQLITE_OK;
          527  +  if( !pBt->pHasContent ){
          528  +    assert( pgno<=pBt->nPage );
          529  +    pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
          530  +    if( !pBt->pHasContent ){
          531  +      rc = SQLITE_NOMEM;
          532  +    }
          533  +  }
          534  +  if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
          535  +    rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
          536  +  }
          537  +  return rc;
          538  +}
          539  +
          540  +/*
          541  +** Query the BtShared.pHasContent vector.
          542  +**
          543  +** This function is called when a free-list leaf page is removed from the
          544  +** free-list for reuse. It returns false if it is safe to retrieve the
          545  +** page from the pager layer with the 'no-content' flag set. True otherwise.
          546  +*/
          547  +static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
          548  +  Bitvec *p = pBt->pHasContent;
          549  +  return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
          550  +}
          551  +
          552  +/*
          553  +** Clear (destroy) the BtShared.pHasContent bitvec. This should be
          554  +** invoked at the conclusion of each write-transaction.
          555  +*/
          556  +static void btreeClearHasContent(BtShared *pBt){
          557  +  sqlite3BitvecDestroy(pBt->pHasContent);
          558  +  pBt->pHasContent = 0;
          559  +}
   287    560   
   288    561   /*
   289    562   ** Save the current cursor position in the variables BtCursor.nKey 
   290    563   ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
          564  +**
          565  +** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
          566  +** prior to calling this routine.  
   291    567   */
   292    568   static int saveCursorPosition(BtCursor *pCur){
   293    569     int rc;
   294    570   
   295    571     assert( CURSOR_VALID==pCur->eState );
   296    572     assert( 0==pCur->pKey );
   297    573     assert( cursorHoldsMutex(pCur) );
   298    574   
   299    575     rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
          576  +  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
   300    577   
   301    578     /* If this is an intKey table, then the above call to BtreeKeySize()
   302    579     ** stores the integer key in pCur->nKey. In this case this value is
   303    580     ** all that is required. Otherwise, if pCur is not open on an intKey
   304    581     ** table, then malloc space for and store the pCur->nKey bytes of key 
   305    582     ** data.
   306    583     */
   307         -  if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
   308         -    void *pKey = sqlite3Malloc(pCur->nKey);
          584  +  if( 0==pCur->apPage[0]->intKey ){
          585  +    void *pKey = sqlite3Malloc( (int)pCur->nKey );
   309    586       if( pKey ){
   310         -      rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
          587  +      rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
   311    588         if( rc==SQLITE_OK ){
   312    589           pCur->pKey = pKey;
   313    590         }else{
   314    591           sqlite3_free(pKey);
   315    592         }
   316    593       }else{
   317    594         rc = SQLITE_NOMEM;
   318    595       }
   319    596     }
   320         -  assert( !pCur->pPage->intKey || !pCur->pKey );
          597  +  assert( !pCur->apPage[0]->intKey || !pCur->pKey );
   321    598   
   322    599     if( rc==SQLITE_OK ){
   323         -    releasePage(pCur->pPage);
   324         -    pCur->pPage = 0;
          600  +    int i;
          601  +    for(i=0; i<=pCur->iPage; i++){
          602  +      releasePage(pCur->apPage[i]);
          603  +      pCur->apPage[i] = 0;
          604  +    }
          605  +    pCur->iPage = -1;
   325    606       pCur->eState = CURSOR_REQUIRESEEK;
   326    607     }
   327    608   
   328    609     invalidateOverflowCache(pCur);
   329    610     return rc;
   330    611   }
   331    612   
   332    613   /*
   333         -** Save the positions of all cursors except pExcept open on the table 
   334         -** with root-page iRoot. Usually, this is called just before cursor
          614  +** Save the positions of all cursors (except pExcept) that are open on
          615  +** the table  with root-page iRoot. Usually, this is called just before cursor
   335    616   ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
   336    617   */
   337    618   static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
   338    619     BtCursor *p;
   339    620     assert( sqlite3_mutex_held(pBt->mutex) );
   340    621     assert( pExcept==0 || pExcept->pBt==pBt );
   341    622     for(p=pBt->pCursor; p; p=p->pNext){
................................................................................
   349    630     }
   350    631     return SQLITE_OK;
   351    632   }
   352    633   
   353    634   /*
   354    635   ** Clear the current cursor position.
   355    636   */
   356         -static void clearCursorPosition(BtCursor *pCur){
          637  +void sqlite3BtreeClearCursor(BtCursor *pCur){
   357    638     assert( cursorHoldsMutex(pCur) );
   358    639     sqlite3_free(pCur->pKey);
   359    640     pCur->pKey = 0;
   360    641     pCur->eState = CURSOR_INVALID;
   361    642   }
          643  +
          644  +/*
          645  +** In this version of BtreeMoveto, pKey is a packed index record
          646  +** such as is generated by the OP_MakeRecord opcode.  Unpack the
          647  +** record and then call BtreeMovetoUnpacked() to do the work.
          648  +*/
          649  +static int btreeMoveto(
          650  +  BtCursor *pCur,     /* Cursor open on the btree to be searched */
          651  +  const void *pKey,   /* Packed key if the btree is an index */
          652  +  i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
          653  +  int bias,           /* Bias search to the high end */
          654  +  int *pRes           /* Write search results here */
          655  +){
          656  +  int rc;                    /* Status code */
          657  +  UnpackedRecord *pIdxKey;   /* Unpacked index key */
          658  +  char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
          659  +
          660  +  if( pKey ){
          661  +    assert( nKey==(i64)(int)nKey );
          662  +    pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
          663  +                                      aSpace, sizeof(aSpace));
          664  +    if( pIdxKey==0 ) return SQLITE_NOMEM;
          665  +  }else{
          666  +    pIdxKey = 0;
          667  +  }
          668  +  rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
          669  +  if( pKey ){
          670  +    sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
          671  +  }
          672  +  return rc;
          673  +}
   362    674   
   363    675   /*
   364    676   ** Restore the cursor to the position it was in (or as close to as possible)
   365    677   ** when saveCursorPosition() was called. Note that this call deletes the 
   366    678   ** saved position info stored by saveCursorPosition(), so there can be
   367    679   ** at most one effective restoreCursorPosition() call after each 
   368    680   ** saveCursorPosition().
   369    681   */
   370         -int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){
          682  +static int btreeRestoreCursorPosition(BtCursor *pCur){
   371    683     int rc;
   372    684     assert( cursorHoldsMutex(pCur) );
   373    685     assert( pCur->eState>=CURSOR_REQUIRESEEK );
   374    686     if( pCur->eState==CURSOR_FAULT ){
   375         -    return pCur->skip;
          687  +    return pCur->skipNext;
   376    688     }
   377    689     pCur->eState = CURSOR_INVALID;
   378         -  rc = sqlite3BtreeMoveto(pCur, pCur->pKey, 0, pCur->nKey, 0, &pCur->skip);
          690  +  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
   379    691     if( rc==SQLITE_OK ){
   380    692       sqlite3_free(pCur->pKey);
   381    693       pCur->pKey = 0;
   382    694       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
   383    695     }
   384    696     return rc;
   385    697   }
   386    698   
   387    699   #define restoreCursorPosition(p) \
   388    700     (p->eState>=CURSOR_REQUIRESEEK ? \
   389         -         sqlite3BtreeRestoreCursorPosition(p) : \
          701  +         btreeRestoreCursorPosition(p) : \
   390    702            SQLITE_OK)
   391    703   
   392    704   /*
   393    705   ** Determine whether or not a cursor has moved from the position it
   394         -** was last placed at.  Cursor can move when the row they are pointing
          706  +** was last placed at.  Cursors can move when the row they are pointing
   395    707   ** at is deleted out from under them.
   396    708   **
   397    709   ** This routine returns an error code if something goes wrong.  The
   398    710   ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
   399    711   */
   400    712   int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
   401    713     int rc;
   402    714   
   403    715     rc = restoreCursorPosition(pCur);
   404    716     if( rc ){
   405    717       *pHasMoved = 1;
   406    718       return rc;
   407    719     }
   408         -  if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){
          720  +  if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
   409    721       *pHasMoved = 1;
   410    722     }else{
   411    723       *pHasMoved = 0;
   412    724     }
   413    725     return SQLITE_OK;
   414    726   }
   415    727   
   416    728   #ifndef SQLITE_OMIT_AUTOVACUUM
   417    729   /*
   418    730   ** Given a page number of a regular database page, return the page
   419    731   ** number for the pointer-map page that contains the entry for the
   420    732   ** input page number.
          733  +**
          734  +** Return 0 (not a valid page) for pgno==1 since there is
          735  +** no pointer map associated with page 1.  The integrity_check logic
          736  +** requires that ptrmapPageno(*,1)!=1.
   421    737   */
   422    738   static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
   423         -  int nPagesPerMapPage, iPtrMap, ret;
          739  +  int nPagesPerMapPage;
          740  +  Pgno iPtrMap, ret;
   424    741     assert( sqlite3_mutex_held(pBt->mutex) );
          742  +  if( pgno<2 ) return 0;
   425    743     nPagesPerMapPage = (pBt->usableSize/5)+1;
   426    744     iPtrMap = (pgno-2)/nPagesPerMapPage;
   427    745     ret = (iPtrMap*nPagesPerMapPage) + 2; 
   428    746     if( ret==PENDING_BYTE_PAGE(pBt) ){
   429    747       ret++;
   430    748     }
   431    749     return ret;
................................................................................
   432    750   }
   433    751   
   434    752   /*
   435    753   ** Write an entry into the pointer map.
   436    754   **
   437    755   ** This routine updates the pointer map entry for page number 'key'
   438    756   ** so that it maps to type 'eType' and parent page number 'pgno'.
   439         -** An error code is returned if something goes wrong, otherwise SQLITE_OK.
          757  +**
          758  +** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
          759  +** a no-op.  If an error occurs, the appropriate error code is written
          760  +** into *pRC.
   440    761   */
   441         -static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
          762  +static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
   442    763     DbPage *pDbPage;  /* The pointer map page */
   443    764     u8 *pPtrmap;      /* The pointer map data */
   444    765     Pgno iPtrmap;     /* The pointer map page number */
   445    766     int offset;       /* Offset in pointer map page */
   446         -  int rc;
          767  +  int rc;           /* Return code from subfunctions */
          768  +
          769  +  if( *pRC ) return;
   447    770   
   448    771     assert( sqlite3_mutex_held(pBt->mutex) );
   449    772     /* The master-journal page number must never be used as a pointer map page */
   450    773     assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
   451    774   
   452    775     assert( pBt->autoVacuum );
   453    776     if( key==0 ){
   454         -    return SQLITE_CORRUPT_BKPT;
          777  +    *pRC = SQLITE_CORRUPT_BKPT;
          778  +    return;
   455    779     }
   456    780     iPtrmap = PTRMAP_PAGENO(pBt, key);
   457    781     rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
   458    782     if( rc!=SQLITE_OK ){
   459         -    return rc;
          783  +    *pRC = rc;
          784  +    return;
   460    785     }
   461    786     offset = PTRMAP_PTROFFSET(iPtrmap, key);
          787  +  if( offset<0 ){
          788  +    *pRC = SQLITE_CORRUPT_BKPT;
          789  +    goto ptrmap_exit;
          790  +  }
   462    791     pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
   463    792   
   464    793     if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
   465    794       TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
   466         -    rc = sqlite3PagerWrite(pDbPage);
          795  +    *pRC= rc = sqlite3PagerWrite(pDbPage);
   467    796       if( rc==SQLITE_OK ){
   468    797         pPtrmap[offset] = eType;
   469    798         put4byte(&pPtrmap[offset+1], parent);
   470    799       }
   471    800     }
   472    801   
          802  +ptrmap_exit:
   473    803     sqlite3PagerUnref(pDbPage);
   474         -  return rc;
   475    804   }
   476    805   
   477    806   /*
   478    807   ** Read an entry from the pointer map.
   479    808   **
   480    809   ** This routine retrieves the pointer map entry for page 'key', writing
   481    810   ** the type and parent page number to *pEType and *pPgno respectively.
................................................................................
   504    833   
   505    834     sqlite3PagerUnref(pDbPage);
   506    835     if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
   507    836     return SQLITE_OK;
   508    837   }
   509    838   
   510    839   #else /* if defined SQLITE_OMIT_AUTOVACUUM */
   511         -  #define ptrmapPut(w,x,y,z) SQLITE_OK
          840  +  #define ptrmapPut(w,x,y,z,rc)
   512    841     #define ptrmapGet(w,x,y,z) SQLITE_OK
   513         -  #define ptrmapPutOvfl(y,z) SQLITE_OK
          842  +  #define ptrmapPutOvflPtr(x, y, rc)
   514    843   #endif
   515    844   
   516    845   /*
   517    846   ** Given a btree page and a cell index (0 means the first cell on
   518    847   ** the page, 1 means the second cell, and so forth) return a pointer
   519    848   ** to the cell content.
   520    849   **
................................................................................
   521    850   ** This routine works only for pages that do not contain overflow cells.
   522    851   */
   523    852   #define findCell(P,I) \
   524    853     ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
   525    854   
   526    855   /*
   527    856   ** This a more complex version of findCell() that works for
   528         -** pages that do contain overflow cells.  See insert
          857  +** pages that do contain overflow cells.
   529    858   */
   530    859   static u8 *findOverflowCell(MemPage *pPage, int iCell){
   531    860     int i;
   532    861     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   533    862     for(i=pPage->nOverflow-1; i>=0; i--){
   534    863       int k;
   535    864       struct _OvflCell *pOvfl;
................................................................................
   543    872       }
   544    873     }
   545    874     return findCell(pPage, iCell);
   546    875   }
   547    876   
   548    877   /*
   549    878   ** Parse a cell content block and fill in the CellInfo structure.  There
   550         -** are two versions of this function.  sqlite3BtreeParseCell() takes a 
   551         -** cell index as the second argument and sqlite3BtreeParseCellPtr() 
          879  +** are two versions of this function.  btreeParseCell() takes a 
          880  +** cell index as the second argument and btreeParseCellPtr() 
   552    881   ** takes a pointer to the body of the cell as its second argument.
   553    882   **
   554    883   ** Within this file, the parseCell() macro can be called instead of
   555         -** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
          884  +** btreeParseCellPtr(). Using some compilers, this will be faster.
   556    885   */
   557         -void sqlite3BtreeParseCellPtr(
          886  +static void btreeParseCellPtr(
   558    887     MemPage *pPage,         /* Page containing the cell */
   559    888     u8 *pCell,              /* Pointer to the cell text. */
   560    889     CellInfo *pInfo         /* Fill in this structure */
   561    890   ){
   562         -  int n;                  /* Number bytes in cell content header */
          891  +  u16 n;                  /* Number bytes in cell content header */
   563    892     u32 nPayload;           /* Number of bytes of cell payload */
   564    893   
   565    894     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   566    895   
   567    896     pInfo->pCell = pCell;
   568    897     assert( pPage->leaf==0 || pPage->leaf==1 );
   569    898     n = pPage->childPtrSize;
................................................................................
   579    908     }else{
   580    909       pInfo->nData = 0;
   581    910       n += getVarint32(&pCell[n], nPayload);
   582    911       pInfo->nKey = nPayload;
   583    912     }
   584    913     pInfo->nPayload = nPayload;
   585    914     pInfo->nHeader = n;
          915  +  testcase( nPayload==pPage->maxLocal );
          916  +  testcase( nPayload==pPage->maxLocal+1 );
   586    917     if( likely(nPayload<=pPage->maxLocal) ){
   587    918       /* This is the (easy) common case where the entire payload fits
   588    919       ** on the local page.  No overflow is required.
   589    920       */
   590    921       int nSize;          /* Total size of cell content in bytes */
   591    922       nSize = nPayload + n;
   592         -    pInfo->nLocal = nPayload;
          923  +    pInfo->nLocal = (u16)nPayload;
   593    924       pInfo->iOverflow = 0;
   594    925       if( (nSize & ~3)==0 ){
   595    926         nSize = 4;        /* Minimum cell size is 4 */
   596    927       }
   597         -    pInfo->nSize = nSize;
          928  +    pInfo->nSize = (u16)nSize;
   598    929     }else{
   599    930       /* If the payload will not fit completely on the local page, we have
   600    931       ** to decide how much to store locally and how much to spill onto
   601    932       ** overflow pages.  The strategy is to minimize the amount of unused
   602    933       ** space on overflow pages while keeping the amount of local storage
   603    934       ** in between minLocal and maxLocal.
   604    935       **
................................................................................
   608    939       int minLocal;  /* Minimum amount of payload held locally */
   609    940       int maxLocal;  /* Maximum amount of payload held locally */
   610    941       int surplus;   /* Overflow payload available for local storage */
   611    942   
   612    943       minLocal = pPage->minLocal;
   613    944       maxLocal = pPage->maxLocal;
   614    945       surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
          946  +    testcase( surplus==maxLocal );
          947  +    testcase( surplus==maxLocal+1 );
   615    948       if( surplus <= maxLocal ){
   616         -      pInfo->nLocal = surplus;
          949  +      pInfo->nLocal = (u16)surplus;
   617    950       }else{
   618         -      pInfo->nLocal = minLocal;
          951  +      pInfo->nLocal = (u16)minLocal;
   619    952       }
   620         -    pInfo->iOverflow = pInfo->nLocal + n;
          953  +    pInfo->iOverflow = (u16)(pInfo->nLocal + n);
   621    954       pInfo->nSize = pInfo->iOverflow + 4;
   622    955     }
   623    956   }
   624    957   #define parseCell(pPage, iCell, pInfo) \
   625         -  sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
   626         -void sqlite3BtreeParseCell(
          958  +  btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
          959  +static void btreeParseCell(
   627    960     MemPage *pPage,         /* Page containing the cell */
   628    961     int iCell,              /* The cell index.  First cell is 0 */
   629    962     CellInfo *pInfo         /* Fill in this structure */
   630    963   ){
   631    964     parseCell(pPage, iCell, pInfo);
   632    965   }
   633    966   
   634    967   /*
   635    968   ** Compute the total number of bytes that a Cell needs in the cell
   636    969   ** data area of the btree-page.  The return number includes the cell
   637    970   ** data header and the local payload, but not any overflow page or
   638    971   ** the space used by the cell pointer.
   639    972   */
   640         -#ifndef NDEBUG
   641         -static u16 cellSize(MemPage *pPage, int iCell){
   642         -  CellInfo info;
   643         -  sqlite3BtreeParseCell(pPage, iCell, &info);
   644         -  return info.nSize;
   645         -}
   646         -#endif
   647    973   static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
   648         -  CellInfo info;
   649         -  sqlite3BtreeParseCellPtr(pPage, pCell, &info);
   650         -  return info.nSize;
          974  +  u8 *pIter = &pCell[pPage->childPtrSize];
          975  +  u32 nSize;
          976  +
          977  +#ifdef SQLITE_DEBUG
          978  +  /* The value returned by this function should always be the same as
          979  +  ** the (CellInfo.nSize) value found by doing a full parse of the
          980  +  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
          981  +  ** this function verifies that this invariant is not violated. */
          982  +  CellInfo debuginfo;
          983  +  btreeParseCellPtr(pPage, pCell, &debuginfo);
          984  +#endif
          985  +
          986  +  if( pPage->intKey ){
          987  +    u8 *pEnd;
          988  +    if( pPage->hasData ){
          989  +      pIter += getVarint32(pIter, nSize);
          990  +    }else{
          991  +      nSize = 0;
          992  +    }
          993  +
          994  +    /* pIter now points at the 64-bit integer key value, a variable length 
          995  +    ** integer. The following block moves pIter to point at the first byte
          996  +    ** past the end of the key value. */
          997  +    pEnd = &pIter[9];
          998  +    while( (*pIter++)&0x80 && pIter<pEnd );
          999  +  }else{
         1000  +    pIter += getVarint32(pIter, nSize);
         1001  +  }
         1002  +
         1003  +  testcase( nSize==pPage->maxLocal );
         1004  +  testcase( nSize==pPage->maxLocal+1 );
         1005  +  if( nSize>pPage->maxLocal ){
         1006  +    int minLocal = pPage->minLocal;
         1007  +    nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
         1008  +    testcase( nSize==pPage->maxLocal );
         1009  +    testcase( nSize==pPage->maxLocal+1 );
         1010  +    if( nSize>pPage->maxLocal ){
         1011  +      nSize = minLocal;
         1012  +    }
         1013  +    nSize += 4;
         1014  +  }
         1015  +  nSize += (u32)(pIter - pCell);
         1016  +
         1017  +  /* The minimum size of any cell is 4 bytes. */
         1018  +  if( nSize<4 ){
         1019  +    nSize = 4;
         1020  +  }
         1021  +
         1022  +  assert( nSize==debuginfo.nSize );
         1023  +  return (u16)nSize;
   651   1024   }
         1025  +
         1026  +#ifdef SQLITE_DEBUG
         1027  +/* This variation on cellSizePtr() is used inside of assert() statements
         1028  +** only. */
         1029  +static u16 cellSize(MemPage *pPage, int iCell){
         1030  +  return cellSizePtr(pPage, findCell(pPage, iCell));
         1031  +}
         1032  +#endif
   652   1033   
   653   1034   #ifndef SQLITE_OMIT_AUTOVACUUM
   654   1035   /*
   655   1036   ** If the cell pCell, part of page pPage contains a pointer
   656   1037   ** to an overflow page, insert an entry into the pointer-map
   657   1038   ** for the overflow page.
   658   1039   */
   659         -static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
         1040  +static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
   660   1041     CellInfo info;
         1042  +  if( *pRC ) return;
   661   1043     assert( pCell!=0 );
   662         -  sqlite3BtreeParseCellPtr(pPage, pCell, &info);
         1044  +  btreeParseCellPtr(pPage, pCell, &info);
   663   1045     assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
   664         -  if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
         1046  +  if( info.iOverflow ){
   665   1047       Pgno ovfl = get4byte(&pCell[info.iOverflow]);
   666         -    return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
         1048  +    ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
   667   1049     }
   668         -  return SQLITE_OK;
   669         -}
   670         -/*
   671         -** If the cell with index iCell on page pPage contains a pointer
   672         -** to an overflow page, insert an entry into the pointer-map
   673         -** for the overflow page.
   674         -*/
   675         -static int ptrmapPutOvfl(MemPage *pPage, int iCell){
   676         -  u8 *pCell;
   677         -  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   678         -  pCell = findOverflowCell(pPage, iCell);
   679         -  return ptrmapPutOvflPtr(pPage, pCell);
   680   1050   }
   681   1051   #endif
   682   1052   
   683   1053   
   684   1054   /*
   685   1055   ** Defragment the page given.  All Cells are moved to the
   686   1056   ** end of the page and all free space is collected into one
   687   1057   ** big FreeBlk that occurs in between the header and cell
   688   1058   ** pointer array and the cell content area.
   689   1059   */
   690         -static void defragmentPage(MemPage *pPage){
         1060  +static int defragmentPage(MemPage *pPage){
   691   1061     int i;                     /* Loop counter */
   692   1062     int pc;                    /* Address of a i-th cell */
   693         -  int addr;                  /* Offset of first byte after cell pointer array */
   694   1063     int hdr;                   /* Offset to the page header */
   695   1064     int size;                  /* Size of a cell */
   696   1065     int usableSize;            /* Number of usable bytes on a page */
   697   1066     int cellOffset;            /* Offset to the cell pointer array */
   698         -  int brk;                   /* Offset to the cell content area */
         1067  +  int cbrk;                  /* Offset to the cell content area */
   699   1068     int nCell;                 /* Number of cells on the page */
   700   1069     unsigned char *data;       /* The page data */
   701   1070     unsigned char *temp;       /* Temp area for cell content */
         1071  +  int iCellFirst;            /* First allowable cell index */
         1072  +  int iCellLast;             /* Last possible cell index */
         1073  +
   702   1074   
   703   1075     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   704   1076     assert( pPage->pBt!=0 );
   705   1077     assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
   706   1078     assert( pPage->nOverflow==0 );
   707   1079     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   708   1080     temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
   709   1081     data = pPage->aData;
   710   1082     hdr = pPage->hdrOffset;
   711   1083     cellOffset = pPage->cellOffset;
   712   1084     nCell = pPage->nCell;
   713   1085     assert( nCell==get2byte(&data[hdr+3]) );
   714   1086     usableSize = pPage->pBt->usableSize;
   715         -  brk = get2byte(&data[hdr+5]);
   716         -  memcpy(&temp[brk], &data[brk], usableSize - brk);
   717         -  brk = usableSize;
         1087  +  cbrk = get2byte(&data[hdr+5]);
         1088  +  memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
         1089  +  cbrk = usableSize;
         1090  +  iCellFirst = cellOffset + 2*nCell;
         1091  +  iCellLast = usableSize - 4;
   718   1092     for(i=0; i<nCell; i++){
   719   1093       u8 *pAddr;     /* The i-th cell pointer */
   720   1094       pAddr = &data[cellOffset + i*2];
   721   1095       pc = get2byte(pAddr);
   722         -    assert( pc<pPage->pBt->usableSize );
         1096  +    testcase( pc==iCellFirst );
         1097  +    testcase( pc==iCellLast );
         1098  +#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
         1099  +    /* These conditions have already been verified in btreeInitPage()
         1100  +    ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
         1101  +    */
         1102  +    if( pc<iCellFirst || pc>iCellLast ){
         1103  +      return SQLITE_CORRUPT_BKPT;
         1104  +    }
         1105  +#endif
         1106  +    assert( pc>=iCellFirst && pc<=iCellLast );
   723   1107       size = cellSizePtr(pPage, &temp[pc]);
   724         -    brk -= size;
   725         -    memcpy(&data[brk], &temp[pc], size);
   726         -    put2byte(pAddr, brk);
         1108  +    cbrk -= size;
         1109  +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
         1110  +    if( cbrk<iCellFirst ){
         1111  +      return SQLITE_CORRUPT_BKPT;
         1112  +    }
         1113  +#else
         1114  +    if( cbrk<iCellFirst || pc+size>usableSize ){
         1115  +      return SQLITE_CORRUPT_BKPT;
         1116  +    }
         1117  +#endif
         1118  +    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
         1119  +    testcase( cbrk+size==usableSize );
         1120  +    testcase( pc+size==usableSize );
         1121  +    memcpy(&data[cbrk], &temp[pc], size);
         1122  +    put2byte(pAddr, cbrk);
   727   1123     }
   728         -  assert( brk>=cellOffset+2*nCell );
   729         -  put2byte(&data[hdr+5], brk);
         1124  +  assert( cbrk>=iCellFirst );
         1125  +  put2byte(&data[hdr+5], cbrk);
   730   1126     data[hdr+1] = 0;
   731   1127     data[hdr+2] = 0;
   732   1128     data[hdr+7] = 0;
   733         -  addr = cellOffset+2*nCell;
   734         -  memset(&data[addr], 0, brk-addr);
   735         -}
   736         -
   737         -/*
   738         -** Allocate nByte bytes of space on a page.
   739         -**
   740         -** Return the index into pPage->aData[] of the first byte of
   741         -** the new allocation.  The caller guarantees that there is enough
   742         -** space.  This routine will never fail.
   743         -**
   744         -** If the page contains nBytes of free space but does not contain
   745         -** nBytes of contiguous free space, then this routine automatically
   746         -** calls defragementPage() to consolidate all free space before 
   747         -** allocating the new chunk.
   748         -*/
   749         -static int allocateSpace(MemPage *pPage, int nByte){
   750         -  int addr, pc, hdr;
   751         -  int size;
   752         -  int nFrag;
   753         -  int top;
   754         -  int nCell;
   755         -  int cellOffset;
   756         -  unsigned char *data;
   757         -  
   758         -  data = pPage->aData;
         1129  +  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
         1130  +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
         1131  +  if( cbrk-iCellFirst!=pPage->nFree ){
         1132  +    return SQLITE_CORRUPT_BKPT;
         1133  +  }
         1134  +  return SQLITE_OK;
         1135  +}
         1136  +
         1137  +/*
         1138  +** Allocate nByte bytes of space from within the B-Tree page passed
         1139  +** as the first argument. Write into *pIdx the index into pPage->aData[]
         1140  +** of the first byte of allocated space. Return either SQLITE_OK or
         1141  +** an error code (usually SQLITE_CORRUPT).
         1142  +**
         1143  +** The caller guarantees that there is sufficient space to make the
         1144  +** allocation.  This routine might need to defragment in order to bring
         1145  +** all the space together, however.  This routine will avoid using
         1146  +** the first two bytes past the cell pointer area since presumably this
         1147  +** allocation is being made in order to insert a new cell, so we will
         1148  +** also end up needing a new cell pointer.
         1149  +*/
         1150  +static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
         1151  +  const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
         1152  +  u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
         1153  +  int nFrag;                           /* Number of fragmented bytes on pPage */
         1154  +  int top;                             /* First byte of cell content area */
         1155  +  int gap;        /* First byte of gap between cell pointers and cell content */
         1156  +  int rc;         /* Integer return code */
         1157  +  int usableSize; /* Usable size of the page */
         1158  +  
   759   1159     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   760   1160     assert( pPage->pBt );
   761   1161     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   762   1162     assert( nByte>=0 );  /* Minimum cell size is 4 */
   763   1163     assert( pPage->nFree>=nByte );
   764   1164     assert( pPage->nOverflow==0 );
   765         -  pPage->nFree -= nByte;
   766         -  hdr = pPage->hdrOffset;
         1165  +  usableSize = pPage->pBt->usableSize;
         1166  +  assert( nByte < usableSize-8 );
   767   1167   
   768   1168     nFrag = data[hdr+7];
   769         -  if( nFrag<60 ){
   770         -    /* Search the freelist looking for a slot big enough to satisfy the
   771         -    ** space request. */
   772         -    addr = hdr+1;
   773         -    while( (pc = get2byte(&data[addr]))>0 ){
         1169  +  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
         1170  +  gap = pPage->cellOffset + 2*pPage->nCell;
         1171  +  top = get2byteNotZero(&data[hdr+5]);
         1172  +  if( gap>top ) return SQLITE_CORRUPT_BKPT;
         1173  +  testcase( gap+2==top );
         1174  +  testcase( gap+1==top );
         1175  +  testcase( gap==top );
         1176  +
         1177  +  if( nFrag>=60 ){
         1178  +    /* Always defragment highly fragmented pages */
         1179  +    rc = defragmentPage(pPage);
         1180  +    if( rc ) return rc;
         1181  +    top = get2byteNotZero(&data[hdr+5]);
         1182  +  }else if( gap+2<=top ){
         1183  +    /* Search the freelist looking for a free slot big enough to satisfy 
         1184  +    ** the request. The allocation is made from the first free slot in 
         1185  +    ** the list that is large enough to accomadate it.
         1186  +    */
         1187  +    int pc, addr;
         1188  +    for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
         1189  +      int size;            /* Size of the free slot */
         1190  +      if( pc>usableSize-4 || pc<addr+4 ){
         1191  +        return SQLITE_CORRUPT_BKPT;
         1192  +      }
   774   1193         size = get2byte(&data[pc+2]);
   775   1194         if( size>=nByte ){
   776         -        if( size<nByte+4 ){
         1195  +        int x = size - nByte;
         1196  +        testcase( x==4 );
         1197  +        testcase( x==3 );
         1198  +        if( x<4 ){
         1199  +          /* Remove the slot from the free-list. Update the number of
         1200  +          ** fragmented bytes within the page. */
   777   1201             memcpy(&data[addr], &data[pc], 2);
   778         -          data[hdr+7] = nFrag + size - nByte;
   779         -          return pc;
         1202  +          data[hdr+7] = (u8)(nFrag + x);
         1203  +        }else if( size+pc > usableSize ){
         1204  +          return SQLITE_CORRUPT_BKPT;
   780   1205           }else{
   781         -          put2byte(&data[pc+2], size-nByte);
   782         -          return pc + size - nByte;
         1206  +          /* The slot remains on the free-list. Reduce its size to account
         1207  +          ** for the portion used by the new allocation. */
         1208  +          put2byte(&data[pc+2], x);
   783   1209           }
         1210  +        *pIdx = pc + x;
         1211  +        return SQLITE_OK;
   784   1212         }
   785         -      addr = pc;
   786   1213       }
   787   1214     }
         1215  +
         1216  +  /* Check to make sure there is enough space in the gap to satisfy
         1217  +  ** the allocation.  If not, defragment.
         1218  +  */
         1219  +  testcase( gap+2+nByte==top );
         1220  +  if( gap+2+nByte>top ){
         1221  +    rc = defragmentPage(pPage);
         1222  +    if( rc ) return rc;
         1223  +    top = get2byteNotZero(&data[hdr+5]);
         1224  +    assert( gap+nByte<=top );
         1225  +  }
         1226  +
   788   1227   
   789   1228     /* Allocate memory from the gap in between the cell pointer array
   790         -  ** and the cell content area.
         1229  +  ** and the cell content area.  The btreeInitPage() call has already
         1230  +  ** validated the freelist.  Given that the freelist is valid, there
         1231  +  ** is no way that the allocation can extend off the end of the page.
         1232  +  ** The assert() below verifies the previous sentence.
   791   1233     */
   792         -  top = get2byte(&data[hdr+5]);
   793         -  nCell = get2byte(&data[hdr+3]);
   794         -  cellOffset = pPage->cellOffset;
   795         -  if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
   796         -    defragmentPage(pPage);
   797         -    top = get2byte(&data[hdr+5]);
   798         -  }
   799   1234     top -= nByte;
   800         -  assert( cellOffset + 2*nCell <= top );
   801   1235     put2byte(&data[hdr+5], top);
   802         -  return top;
         1236  +  assert( top+nByte <= pPage->pBt->usableSize );
         1237  +  *pIdx = top;
         1238  +  return SQLITE_OK;
   803   1239   }
   804   1240   
   805   1241   /*
   806   1242   ** Return a section of the pPage->aData to the freelist.
   807   1243   ** The first byte of the new free block is pPage->aDisk[start]
   808   1244   ** and the size of the block is "size" bytes.
   809   1245   **
   810   1246   ** Most of the effort here is involved in coalesing adjacent
   811   1247   ** free blocks into a single big free block.
   812   1248   */
   813         -static void freeSpace(MemPage *pPage, int start, int size){
         1249  +static int freeSpace(MemPage *pPage, int start, int size){
   814   1250     int addr, pbegin, hdr;
         1251  +  int iLast;                        /* Largest possible freeblock offset */
   815   1252     unsigned char *data = pPage->aData;
   816   1253   
   817   1254     assert( pPage->pBt!=0 );
   818   1255     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   819         -  assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
         1256  +  assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
   820   1257     assert( (start + size)<=pPage->pBt->usableSize );
   821   1258     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   822   1259     assert( size>=0 );   /* Minimum cell size is 4 */
   823   1260   
   824         -#ifdef SQLITE_SECURE_DELETE
   825         -  /* Overwrite deleted information with zeros when the SECURE_DELETE 
   826         -  ** option is enabled at compile-time */
   827         -  memset(&data[start], 0, size);
   828         -#endif
         1261  +  if( pPage->pBt->secureDelete ){
         1262  +    /* Overwrite deleted information with zeros when the secure_delete
         1263  +    ** option is enabled */
         1264  +    memset(&data[start], 0, size);
         1265  +  }
   829   1266   
   830         -  /* Add the space back into the linked list of freeblocks */
         1267  +  /* Add the space back into the linked list of freeblocks.  Note that
         1268  +  ** even though the freeblock list was checked by btreeInitPage(),
         1269  +  ** btreeInitPage() did not detect overlapping cells or
         1270  +  ** freeblocks that overlapped cells.   Nor does it detect when the
         1271  +  ** cell content area exceeds the value in the page header.  If these
         1272  +  ** situations arise, then subsequent insert operations might corrupt
         1273  +  ** the freelist.  So we do need to check for corruption while scanning
         1274  +  ** the freelist.
         1275  +  */
   831   1276     hdr = pPage->hdrOffset;
   832   1277     addr = hdr + 1;
         1278  +  iLast = pPage->pBt->usableSize - 4;
         1279  +  assert( start<=iLast );
   833   1280     while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
   834         -    assert( pbegin<=pPage->pBt->usableSize-4 );
   835         -    assert( pbegin>addr );
         1281  +    if( pbegin<addr+4 ){
         1282  +      return SQLITE_CORRUPT_BKPT;
         1283  +    }
   836   1284       addr = pbegin;
   837   1285     }
   838         -  assert( pbegin<=pPage->pBt->usableSize-4 );
         1286  +  if( pbegin>iLast ){
         1287  +    return SQLITE_CORRUPT_BKPT;
         1288  +  }
   839   1289     assert( pbegin>addr || pbegin==0 );
   840   1290     put2byte(&data[addr], start);
   841   1291     put2byte(&data[start], pbegin);
   842   1292     put2byte(&data[start+2], size);
   843         -  pPage->nFree += size;
         1293  +  pPage->nFree = pPage->nFree + (u16)size;
   844   1294   
   845   1295     /* Coalesce adjacent free blocks */
   846         -  addr = pPage->hdrOffset + 1;
         1296  +  addr = hdr + 1;
   847   1297     while( (pbegin = get2byte(&data[addr]))>0 ){
   848         -    int pnext, psize;
         1298  +    int pnext, psize, x;
   849   1299       assert( pbegin>addr );
   850   1300       assert( pbegin<=pPage->pBt->usableSize-4 );
   851   1301       pnext = get2byte(&data[pbegin]);
   852   1302       psize = get2byte(&data[pbegin+2]);
   853   1303       if( pbegin + psize + 3 >= pnext && pnext>0 ){
   854   1304         int frag = pnext - (pbegin+psize);
   855         -      assert( frag<=data[pPage->hdrOffset+7] );
   856         -      data[pPage->hdrOffset+7] -= frag;
   857         -      put2byte(&data[pbegin], get2byte(&data[pnext]));
   858         -      put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
         1305  +      if( (frag<0) || (frag>(int)data[hdr+7]) ){
         1306  +        return SQLITE_CORRUPT_BKPT;
         1307  +      }
         1308  +      data[hdr+7] -= (u8)frag;
         1309  +      x = get2byte(&data[pnext]);
         1310  +      put2byte(&data[pbegin], x);
         1311  +      x = pnext + get2byte(&data[pnext+2]) - pbegin;
         1312  +      put2byte(&data[pbegin+2], x);
   859   1313       }else{
   860   1314         addr = pbegin;
   861   1315       }
   862   1316     }
   863   1317   
   864   1318     /* If the cell content area begins with a freeblock, remove it. */
   865   1319     if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
   866   1320       int top;
   867   1321       pbegin = get2byte(&data[hdr+1]);
   868   1322       memcpy(&data[hdr+1], &data[pbegin], 2);
   869         -    top = get2byte(&data[hdr+5]);
   870         -    put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
         1323  +    top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
         1324  +    put2byte(&data[hdr+5], top);
   871   1325     }
         1326  +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
         1327  +  return SQLITE_OK;
   872   1328   }
   873   1329   
   874   1330   /*
   875   1331   ** Decode the flags byte (the first byte of the header) for a page
   876   1332   ** and initialize fields of the MemPage structure accordingly.
   877   1333   **
   878   1334   ** Only the following combinations are supported.  Anything different
................................................................................
   884   1340   **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
   885   1341   */
   886   1342   static int decodeFlags(MemPage *pPage, int flagByte){
   887   1343     BtShared *pBt;     /* A copy of pPage->pBt */
   888   1344   
   889   1345     assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
   890   1346     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   891         -  pPage->leaf = flagByte>>3;  assert( PTF_LEAF == 1<<3 );
         1347  +  pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
   892   1348     flagByte &= ~PTF_LEAF;
   893   1349     pPage->childPtrSize = 4-4*pPage->leaf;
   894   1350     pBt = pPage->pBt;
   895   1351     if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
   896   1352       pPage->intKey = 1;
   897   1353       pPage->hasData = pPage->leaf;
   898   1354       pPage->maxLocal = pBt->maxLeaf;
................................................................................
   907   1363     }
   908   1364     return SQLITE_OK;
   909   1365   }
   910   1366   
   911   1367   /*
   912   1368   ** Initialize the auxiliary information for a disk block.
   913   1369   **
   914         -** The pParent parameter must be a pointer to the MemPage which
   915         -** is the parent of the page being initialized.  The root of a
   916         -** BTree has no parent and so for that page, pParent==NULL.
   917         -**
   918   1370   ** Return SQLITE_OK on success.  If we see that the page does
   919   1371   ** not contain a well-formed database page, then return 
   920   1372   ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
   921   1373   ** guarantee that the page is well-formed.  It only shows that
   922   1374   ** we failed to detect any corruption.
   923   1375   */
   924         -int sqlite3BtreeInitPage(
   925         -  MemPage *pPage,        /* The page to be initialized */
   926         -  MemPage *pParent       /* The parent.  Might be NULL */
   927         -){
   928         -  int pc;            /* Address of a freeblock within pPage->aData[] */
   929         -  int hdr;           /* Offset to beginning of page header */
   930         -  u8 *data;          /* Equal to pPage->aData */
   931         -  BtShared *pBt;        /* The main btree structure */
   932         -  int usableSize;    /* Amount of usable space on each page */
   933         -  int cellOffset;    /* Offset from start of page to first cell pointer */
   934         -  int nFree;         /* Number of unused bytes on the page */
   935         -  int top;           /* First byte of the cell content area */
         1376  +static int btreeInitPage(MemPage *pPage){
   936   1377   
   937         -  pBt = pPage->pBt;
   938         -  assert( pBt!=0 );
   939         -  assert( pParent==0 || pParent->pBt==pBt );
   940         -  assert( sqlite3_mutex_held(pBt->mutex) );
         1378  +  assert( pPage->pBt!=0 );
         1379  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   941   1380     assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
   942   1381     assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
   943   1382     assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
   944         -  if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
   945         -    /* The parent page should never change unless the file is corrupt */
   946         -    return SQLITE_CORRUPT_BKPT;
   947         -  }
   948         -  if( pPage->isInit ) return SQLITE_OK;
   949         -  if( pPage->pParent==0 && pParent!=0 ){
   950         -    pPage->pParent = pParent;
   951         -    sqlite3PagerRef(pParent->pDbPage);
   952         -  }
   953         -  hdr = pPage->hdrOffset;
   954         -  data = pPage->aData;
   955         -  if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
   956         -  assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
   957         -  pPage->maskPage = pBt->pageSize - 1;
   958         -  pPage->nOverflow = 0;
   959         -  pPage->idxShift = 0;
   960         -  usableSize = pBt->usableSize;
   961         -  pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
   962         -  top = get2byte(&data[hdr+5]);
   963         -  pPage->nCell = get2byte(&data[hdr+3]);
   964         -  if( pPage->nCell>MX_CELL(pBt) ){
   965         -    /* To many cells for a single page.  The page must be corrupt */
   966         -    return SQLITE_CORRUPT_BKPT;
   967         -  }
   968         -  if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
   969         -    /* All pages must have at least one cell, except for root pages */
   970         -    return SQLITE_CORRUPT_BKPT;
   971         -  }
   972         -
   973         -  /* Compute the total free space on the page */
   974         -  pc = get2byte(&data[hdr+1]);
   975         -  nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
   976         -  while( pc>0 ){
   977         -    int next, size;
   978         -    if( pc>usableSize-4 ){
   979         -      /* Free block is off the page */
         1383  +
         1384  +  if( !pPage->isInit ){
         1385  +    u16 pc;            /* Address of a freeblock within pPage->aData[] */
         1386  +    u8 hdr;            /* Offset to beginning of page header */
         1387  +    u8 *data;          /* Equal to pPage->aData */
         1388  +    BtShared *pBt;        /* The main btree structure */
         1389  +    int usableSize;    /* Amount of usable space on each page */
         1390  +    u16 cellOffset;    /* Offset from start of page to first cell pointer */
         1391  +    int nFree;         /* Number of unused bytes on the page */
         1392  +    int top;           /* First byte of the cell content area */
         1393  +    int iCellFirst;    /* First allowable cell or freeblock offset */
         1394  +    int iCellLast;     /* Last possible cell or freeblock offset */
         1395  +
         1396  +    pBt = pPage->pBt;
         1397  +
         1398  +    hdr = pPage->hdrOffset;
         1399  +    data = pPage->aData;
         1400  +    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
         1401  +    assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
         1402  +    pPage->maskPage = (u16)(pBt->pageSize - 1);
         1403  +    pPage->nOverflow = 0;
         1404  +    usableSize = pBt->usableSize;
         1405  +    pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
         1406  +    top = get2byteNotZero(&data[hdr+5]);
         1407  +    pPage->nCell = get2byte(&data[hdr+3]);
         1408  +    if( pPage->nCell>MX_CELL(pBt) ){
         1409  +      /* To many cells for a single page.  The page must be corrupt */
         1410  +      return SQLITE_CORRUPT_BKPT;
         1411  +    }
         1412  +    testcase( pPage->nCell==MX_CELL(pBt) );
         1413  +
         1414  +    /* A malformed database page might cause us to read past the end
         1415  +    ** of page when parsing a cell.  
         1416  +    **
         1417  +    ** The following block of code checks early to see if a cell extends
         1418  +    ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
         1419  +    ** returned if it does.
         1420  +    */
         1421  +    iCellFirst = cellOffset + 2*pPage->nCell;
         1422  +    iCellLast = usableSize - 4;
         1423  +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
         1424  +    {
         1425  +      int i;            /* Index into the cell pointer array */
         1426  +      int sz;           /* Size of a cell */
         1427  +
         1428  +      if( !pPage->leaf ) iCellLast--;
         1429  +      for(i=0; i<pPage->nCell; i++){
         1430  +        pc = get2byte(&data[cellOffset+i*2]);
         1431  +        testcase( pc==iCellFirst );
         1432  +        testcase( pc==iCellLast );
         1433  +        if( pc<iCellFirst || pc>iCellLast ){
         1434  +          return SQLITE_CORRUPT_BKPT;
         1435  +        }
         1436  +        sz = cellSizePtr(pPage, &data[pc]);
         1437  +        testcase( pc+sz==usableSize );
         1438  +        if( pc+sz>usableSize ){
         1439  +          return SQLITE_CORRUPT_BKPT;
         1440  +        }
         1441  +      }
         1442  +      if( !pPage->leaf ) iCellLast++;
         1443  +    }  
         1444  +#endif
         1445  +
         1446  +    /* Compute the total free space on the page */
         1447  +    pc = get2byte(&data[hdr+1]);
         1448  +    nFree = data[hdr+7] + top;
         1449  +    while( pc>0 ){
         1450  +      u16 next, size;
         1451  +      if( pc<iCellFirst || pc>iCellLast ){
         1452  +        /* Start of free block is off the page */
         1453  +        return SQLITE_CORRUPT_BKPT; 
         1454  +      }
         1455  +      next = get2byte(&data[pc]);
         1456  +      size = get2byte(&data[pc+2]);
         1457  +      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
         1458  +        /* Free blocks must be in ascending order. And the last byte of
         1459  +	** the free-block must lie on the database page.  */
         1460  +        return SQLITE_CORRUPT_BKPT; 
         1461  +      }
         1462  +      nFree = nFree + size;
         1463  +      pc = next;
         1464  +    }
         1465  +
         1466  +    /* At this point, nFree contains the sum of the offset to the start
         1467  +    ** of the cell-content area plus the number of free bytes within
         1468  +    ** the cell-content area. If this is greater than the usable-size
         1469  +    ** of the page, then the page must be corrupted. This check also
         1470  +    ** serves to verify that the offset to the start of the cell-content
         1471  +    ** area, according to the page header, lies within the page.
         1472  +    */
         1473  +    if( nFree>usableSize ){
   980   1474         return SQLITE_CORRUPT_BKPT; 
   981   1475       }
   982         -    next = get2byte(&data[pc]);
   983         -    size = get2byte(&data[pc+2]);
   984         -    if( next>0 && next<=pc+size+3 ){
   985         -      /* Free blocks must be in accending order */
   986         -      return SQLITE_CORRUPT_BKPT; 
   987         -    }
   988         -    nFree += size;
   989         -    pc = next;
         1476  +    pPage->nFree = (u16)(nFree - iCellFirst);
         1477  +    pPage->isInit = 1;
   990   1478     }
   991         -  pPage->nFree = nFree;
   992         -  if( nFree>=usableSize ){
   993         -    /* Free space cannot exceed total page size */
   994         -    return SQLITE_CORRUPT_BKPT; 
   995         -  }
   996         -
   997         -#if 0
   998         -  /* Check that all the offsets in the cell offset array are within range. 
   999         -  ** 
  1000         -  ** Omitting this consistency check and using the pPage->maskPage mask
  1001         -  ** to prevent overrunning the page buffer in findCell() results in a
  1002         -  ** 2.5% performance gain.
  1003         -  */
  1004         -  {
  1005         -    u8 *pOff;        /* Iterator used to check all cell offsets are in range */
  1006         -    u8 *pEnd;        /* Pointer to end of cell offset array */
  1007         -    u8 mask;         /* Mask of bits that must be zero in MSB of cell offsets */
  1008         -    mask = ~(((u8)(pBt->pageSize>>8))-1);
  1009         -    pEnd = &data[cellOffset + pPage->nCell*2];
  1010         -    for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2);
  1011         -    if( pOff!=pEnd ){
  1012         -      return SQLITE_CORRUPT_BKPT;
  1013         -    }
  1014         -  }
  1015         -#endif
  1016         -
  1017         -  pPage->isInit = 1;
  1018   1479     return SQLITE_OK;
  1019   1480   }
  1020   1481   
  1021   1482   /*
  1022   1483   ** Set up a raw page so that it looks like a database page holding
  1023   1484   ** no entries.
  1024   1485   */
  1025   1486   static void zeroPage(MemPage *pPage, int flags){
  1026   1487     unsigned char *data = pPage->aData;
  1027   1488     BtShared *pBt = pPage->pBt;
  1028         -  int hdr = pPage->hdrOffset;
  1029         -  int first;
         1489  +  u8 hdr = pPage->hdrOffset;
         1490  +  u16 first;
  1030   1491   
  1031   1492     assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
  1032   1493     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
  1033   1494     assert( sqlite3PagerGetData(pPage->pDbPage) == data );
  1034   1495     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  1035   1496     assert( sqlite3_mutex_held(pBt->mutex) );
  1036         -  /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/
  1037         -  data[hdr] = flags;
  1038         -  first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
         1497  +  if( pBt->secureDelete ){
         1498  +    memset(&data[hdr], 0, pBt->usableSize - hdr);
         1499  +  }
         1500  +  data[hdr] = (char)flags;
         1501  +  first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
  1039   1502     memset(&data[hdr+1], 0, 4);
  1040   1503     data[hdr+7] = 0;
  1041   1504     put2byte(&data[hdr+5], pBt->usableSize);
  1042         -  pPage->nFree = pBt->usableSize - first;
         1505  +  pPage->nFree = (u16)(pBt->usableSize - first);
  1043   1506     decodeFlags(pPage, flags);
  1044   1507     pPage->hdrOffset = hdr;
  1045   1508     pPage->cellOffset = first;
  1046   1509     pPage->nOverflow = 0;
  1047         -  assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
  1048         -  pPage->maskPage = pBt->pageSize - 1;
  1049         -  pPage->idxShift = 0;
         1510  +  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
         1511  +  pPage->maskPage = (u16)(pBt->pageSize - 1);
  1050   1512     pPage->nCell = 0;
  1051   1513     pPage->isInit = 1;
  1052   1514   }
  1053   1515   
         1516  +
         1517  +/*
         1518  +** Convert a DbPage obtained from the pager into a MemPage used by
         1519  +** the btree layer.
         1520  +*/
         1521  +static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
         1522  +  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
         1523  +  pPage->aData = sqlite3PagerGetData(pDbPage);
         1524  +  pPage->pDbPage = pDbPage;
         1525  +  pPage->pBt = pBt;
         1526  +  pPage->pgno = pgno;
         1527  +  pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
         1528  +  return pPage; 
         1529  +}
         1530  +
  1054   1531   /*
  1055   1532   ** Get a page from the pager.  Initialize the MemPage.pBt and
  1056   1533   ** MemPage.aData elements if needed.
  1057   1534   **
  1058   1535   ** If the noContent flag is set, it means that we do not care about
  1059   1536   ** the content of the page at this time.  So do not go to the disk
  1060   1537   ** to fetch the content.  Just fill in the content with zeros for now.
  1061   1538   ** If in the future we call sqlite3PagerWrite() on this page, that
  1062   1539   ** means we have started to be concerned about content and the disk
  1063   1540   ** read should occur at that point.
  1064   1541   */
  1065         -int sqlite3BtreeGetPage(
         1542  +static int btreeGetPage(
  1066   1543     BtShared *pBt,       /* The btree */
  1067   1544     Pgno pgno,           /* Number of the page to fetch */
  1068   1545     MemPage **ppPage,    /* Return the page in this parameter */
  1069   1546     int noContent        /* Do not load page content if true */
  1070   1547   ){
  1071   1548     int rc;
  1072         -  MemPage *pPage;
  1073   1549     DbPage *pDbPage;
  1074   1550   
  1075   1551     assert( sqlite3_mutex_held(pBt->mutex) );
  1076   1552     rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
  1077   1553     if( rc ) return rc;
  1078         -  pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
  1079         -  pPage->aData = sqlite3PagerGetData(pDbPage);
  1080         -  pPage->pDbPage = pDbPage;
  1081         -  pPage->pBt = pBt;
  1082         -  pPage->pgno = pgno;
  1083         -  pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
  1084         -  *ppPage = pPage;
  1085         -  return SQLITE_OK;
  1086         -}
  1087         -
  1088         -/*
  1089         -** Get a page from the pager and initialize it.  This routine
  1090         -** is just a convenience wrapper around separate calls to
  1091         -** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
         1554  +  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
         1555  +  return SQLITE_OK;
         1556  +}
         1557  +
         1558  +/*
         1559  +** Retrieve a page from the pager cache. If the requested page is not
         1560  +** already in the pager cache return NULL. Initialize the MemPage.pBt and
         1561  +** MemPage.aData elements if needed.
         1562  +*/
         1563  +static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
         1564  +  DbPage *pDbPage;
         1565  +  assert( sqlite3_mutex_held(pBt->mutex) );
         1566  +  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
         1567  +  if( pDbPage ){
         1568  +    return btreePageFromDbPage(pDbPage, pgno, pBt);
         1569  +  }
         1570  +  return 0;
         1571  +}
         1572  +
         1573  +/*
         1574  +** Return the size of the database file in pages. If there is any kind of
         1575  +** error, return ((unsigned int)-1).
         1576  +*/
         1577  +static Pgno btreePagecount(BtShared *pBt){
         1578  +  return pBt->nPage;
         1579  +}
         1580  +u32 sqlite3BtreeLastPage(Btree *p){
         1581  +  assert( sqlite3BtreeHoldsMutex(p) );
         1582  +  assert( ((p->pBt->nPage)&0x8000000)==0 );
         1583  +  return (int)btreePagecount(p->pBt);
         1584  +}
         1585  +
         1586  +/*
         1587  +** Get a page from the pager and initialize it.  This routine is just a
         1588  +** convenience wrapper around separate calls to btreeGetPage() and 
         1589  +** btreeInitPage().
         1590  +**
         1591  +** If an error occurs, then the value *ppPage is set to is undefined. It
         1592  +** may remain unchanged, or it may be set to an invalid value.
  1092   1593   */
  1093   1594   static int getAndInitPage(
  1094   1595     BtShared *pBt,          /* The database file */
  1095   1596     Pgno pgno,           /* Number of the page to get */
  1096         -  MemPage **ppPage,    /* Write the page pointer here */
  1097         -  MemPage *pParent     /* Parent of the page */
         1597  +  MemPage **ppPage     /* Write the page pointer here */
  1098   1598   ){
  1099   1599     int rc;
  1100   1600     assert( sqlite3_mutex_held(pBt->mutex) );
  1101         -  if( pgno==0 ){
  1102         -    return SQLITE_CORRUPT_BKPT; 
  1103         -  }
  1104         -  rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
  1105         -  if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
  1106         -    rc = sqlite3BtreeInitPage(*ppPage, pParent);
  1107         -    if( rc!=SQLITE_OK ){
  1108         -      releasePage(*ppPage);
  1109         -      *ppPage = 0;
         1601  +
         1602  +  if( pgno>btreePagecount(pBt) ){
         1603  +    rc = SQLITE_CORRUPT_BKPT;
         1604  +  }else{
         1605  +    rc = btreeGetPage(pBt, pgno, ppPage, 0);
         1606  +    if( rc==SQLITE_OK ){
         1607  +      rc = btreeInitPage(*ppPage);
         1608  +      if( rc!=SQLITE_OK ){
         1609  +        releasePage(*ppPage);
         1610  +      }
  1110   1611       }
  1111   1612     }
         1613  +
         1614  +  testcase( pgno==0 );
         1615  +  assert( pgno!=0 || rc==SQLITE_CORRUPT );
  1112   1616     return rc;
  1113   1617   }
  1114   1618   
  1115   1619   /*
  1116   1620   ** Release a MemPage.  This should be called once for each prior
  1117         -** call to sqlite3BtreeGetPage.
         1621  +** call to btreeGetPage.
  1118   1622   */
  1119   1623   static void releasePage(MemPage *pPage){
  1120   1624     if( pPage ){
  1121   1625       assert( pPage->aData );
  1122   1626       assert( pPage->pBt );
  1123   1627       assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
  1124   1628       assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
  1125   1629       assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1126   1630       sqlite3PagerUnref(pPage->pDbPage);
  1127   1631     }
  1128   1632   }
  1129   1633   
  1130         -/*
  1131         -** This routine is called when the reference count for a page
  1132         -** reaches zero.  We need to unref the pParent pointer when that
  1133         -** happens.
  1134         -*/
  1135         -static void pageDestructor(DbPage *pData, int pageSize){
  1136         -  MemPage *pPage;
  1137         -  assert( (pageSize & 7)==0 );
  1138         -  pPage = (MemPage *)sqlite3PagerGetExtra(pData);
  1139         -  assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) );
  1140         -  if( pPage->pParent ){
  1141         -    MemPage *pParent = pPage->pParent;
  1142         -    assert( pParent->pBt==pPage->pBt );
  1143         -    pPage->pParent = 0;
  1144         -    releasePage(pParent);
  1145         -  }
  1146         -  pPage->isInit = 0;
  1147         -}
  1148         -
  1149   1634   /*
  1150   1635   ** During a rollback, when the pager reloads information into the cache
  1151   1636   ** so that the cache is restored to its original state at the start of
  1152   1637   ** the transaction, for each page restored this routine is called.
  1153   1638   **
  1154   1639   ** This routine needs to reset the extra data section at the end of the
  1155   1640   ** page to agree with the restored data.
  1156   1641   */
  1157         -static void pageReinit(DbPage *pData, int pageSize){
         1642  +static void pageReinit(DbPage *pData){
  1158   1643     MemPage *pPage;
  1159         -  assert( (pageSize & 7)==0 );
  1160   1644     pPage = (MemPage *)sqlite3PagerGetExtra(pData);
         1645  +  assert( sqlite3PagerPageRefcount(pData)>0 );
  1161   1646     if( pPage->isInit ){
  1162   1647       assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1163   1648       pPage->isInit = 0;
  1164         -    sqlite3BtreeInitPage(pPage, pPage->pParent);
         1649  +    if( sqlite3PagerPageRefcount(pData)>1 ){
         1650  +      /* pPage might not be a btree page;  it might be an overflow page
         1651  +      ** or ptrmap page or a free page.  In those cases, the following
         1652  +      ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
         1653  +      ** But no harm is done by this.  And it is very important that
         1654  +      ** btreeInitPage() be called on every btree page so we make
         1655  +      ** the call for every page that comes in for re-initing. */
         1656  +      btreeInitPage(pPage);
         1657  +    }
  1165   1658     }
  1166   1659   }
  1167   1660   
  1168   1661   /*
  1169   1662   ** Invoke the busy handler for a btree.
  1170   1663   */
  1171         -static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){
         1664  +static int btreeInvokeBusyHandler(void *pArg){
  1172   1665     BtShared *pBt = (BtShared*)pArg;
  1173   1666     assert( pBt->db );
  1174   1667     assert( sqlite3_mutex_held(pBt->db->mutex) );
  1175   1668     return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
  1176   1669   }
  1177   1670   
  1178   1671   /*
  1179   1672   ** Open a database file.
  1180   1673   ** 
  1181   1674   ** zFilename is the name of the database file.  If zFilename is NULL
  1182         -** a new database with a random name is created.  This randomly named
  1183         -** database file will be deleted when sqlite3BtreeClose() is called.
         1675  +** then an ephemeral database is created.  The ephemeral database might
         1676  +** be exclusively in memory, or it might use a disk-based memory cache.
         1677  +** Either way, the ephemeral database will be automatically deleted 
         1678  +** when sqlite3BtreeClose() is called.
         1679  +**
  1184   1680   ** If zFilename is ":memory:" then an in-memory database is created
  1185   1681   ** that is automatically destroyed when it is closed.
         1682  +**
         1683  +** The "flags" parameter is a bitmask that might contain bits
         1684  +** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK.  The BTREE_NO_READLOCK
         1685  +** bit is also set if the SQLITE_NoReadlock flags is set in db->flags.
         1686  +** These flags are passed through into sqlite3PagerOpen() and must
         1687  +** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK.
         1688  +**
         1689  +** If the database is already opened in the same database connection
         1690  +** and we are in shared cache mode, then the open will fail with an
         1691  +** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
         1692  +** objects in the same database connection since doing so will lead
         1693  +** to problems with locking.
  1186   1694   */
  1187   1695   int sqlite3BtreeOpen(
  1188   1696     const char *zFilename,  /* Name of the file containing the BTree database */
  1189   1697     sqlite3 *db,            /* Associated database handle */
  1190   1698     Btree **ppBtree,        /* Pointer to new Btree object written here */
  1191   1699     int flags,              /* Options */
  1192   1700     int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
  1193   1701   ){
  1194         -  sqlite3_vfs *pVfs;      /* The VFS to use for this btree */
  1195         -  BtShared *pBt = 0;      /* Shared part of btree structure */
  1196         -  Btree *p;               /* Handle to return */
  1197         -  int rc = SQLITE_OK;
  1198         -  int nReserve;
  1199         -  unsigned char zDbHeader[100];
         1702  +  sqlite3_vfs *pVfs;             /* The VFS to use for this btree */
         1703  +  BtShared *pBt = 0;             /* Shared part of btree structure */
         1704  +  Btree *p;                      /* Handle to return */
         1705  +  sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
         1706  +  int rc = SQLITE_OK;            /* Result code from this function */
         1707  +  u8 nReserve;                   /* Byte of unused space on each page */
         1708  +  unsigned char zDbHeader[100];  /* Database header content */
         1709  +
         1710  +  /* True if opening an ephemeral, temporary database */
         1711  +  const int isTempDb = zFilename==0 || zFilename[0]==0;
  1200   1712   
  1201   1713     /* Set the variable isMemdb to true for an in-memory database, or 
  1202         -  ** false for a file-based database. This symbol is only required if
  1203         -  ** either of the shared-data or autovacuum features are compiled 
  1204         -  ** into the library.
         1714  +  ** false for a file-based database.
  1205   1715     */
  1206         -#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
  1207         -  #ifdef SQLITE_OMIT_MEMORYDB
  1208         -    const int isMemdb = 0;
  1209         -  #else
  1210         -    const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
  1211         -  #endif
         1716  +#ifdef SQLITE_OMIT_MEMORYDB
         1717  +  const int isMemdb = 0;
         1718  +#else
         1719  +  const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
         1720  +                       || (isTempDb && sqlite3TempInMemory(db));
  1212   1721   #endif
  1213   1722   
  1214   1723     assert( db!=0 );
  1215   1724     assert( sqlite3_mutex_held(db->mutex) );
         1725  +  assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
  1216   1726   
         1727  +  /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
         1728  +  assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
         1729  +
         1730  +  /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
         1731  +  assert( (flags & BTREE_SINGLE)==0 || isTempDb );
         1732  +
         1733  +  if( db->flags & SQLITE_NoReadlock ){
         1734  +    flags |= BTREE_NO_READLOCK;
         1735  +  }
         1736  +  if( isMemdb ){
         1737  +    flags |= BTREE_MEMORY;
         1738  +  }
         1739  +  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
         1740  +    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
         1741  +  }
  1217   1742     pVfs = db->pVfs;
  1218   1743     p = sqlite3MallocZero(sizeof(Btree));
  1219   1744     if( !p ){
  1220   1745       return SQLITE_NOMEM;
  1221   1746     }
  1222   1747     p->inTrans = TRANS_NONE;
  1223   1748     p->db = db;
         1749  +#ifndef SQLITE_OMIT_SHARED_CACHE
         1750  +  p->lock.pBtree = p;
         1751  +  p->lock.iTable = 1;
         1752  +#endif
  1224   1753   
  1225   1754   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  1226   1755     /*
  1227   1756     ** If this Btree is a candidate for shared cache, try to find an
  1228   1757     ** existing BtShared object that we can share with
  1229   1758     */
  1230         -  if( isMemdb==0
  1231         -   && (db->flags & SQLITE_Vtab)==0
  1232         -   && zFilename && zFilename[0]
  1233         -  ){
  1234         -    if( sqlite3SharedCacheEnabled ){
         1759  +  if( isMemdb==0 && isTempDb==0 ){
         1760  +    if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
  1235   1761         int nFullPathname = pVfs->mxPathname+1;
  1236   1762         char *zFullPathname = sqlite3Malloc(nFullPathname);
  1237   1763         sqlite3_mutex *mutexShared;
  1238   1764         p->sharable = 1;
  1239         -      db->flags |= SQLITE_SharedCache;
  1240   1765         if( !zFullPathname ){
  1241   1766           sqlite3_free(p);
  1242   1767           return SQLITE_NOMEM;
  1243   1768         }
  1244   1769         sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
         1770  +      mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
         1771  +      sqlite3_mutex_enter(mutexOpen);
  1245   1772         mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  1246   1773         sqlite3_mutex_enter(mutexShared);
  1247         -      for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
         1774  +      for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
  1248   1775           assert( pBt->nRef>0 );
  1249   1776           if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
  1250   1777                    && sqlite3PagerVfs(pBt->pPager)==pVfs ){
         1778  +          int iDb;
         1779  +          for(iDb=db->nDb-1; iDb>=0; iDb--){
         1780  +            Btree *pExisting = db->aDb[iDb].pBt;
         1781  +            if( pExisting && pExisting->pBt==pBt ){
         1782  +              sqlite3_mutex_leave(mutexShared);
         1783  +              sqlite3_mutex_leave(mutexOpen);
         1784  +              sqlite3_free(zFullPathname);
         1785  +              sqlite3_free(p);
         1786  +              return SQLITE_CONSTRAINT;
         1787  +            }
         1788  +          }
  1251   1789             p->pBt = pBt;
  1252   1790             pBt->nRef++;
  1253   1791             break;
  1254   1792           }
  1255   1793         }
  1256   1794         sqlite3_mutex_leave(mutexShared);
  1257   1795         sqlite3_free(zFullPathname);
................................................................................
  1281   1819       assert( sizeof(Pgno)==4 );
  1282   1820     
  1283   1821       pBt = sqlite3MallocZero( sizeof(*pBt) );
  1284   1822       if( pBt==0 ){
  1285   1823         rc = SQLITE_NOMEM;
  1286   1824         goto btree_open_out;
  1287   1825       }
  1288         -    pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler;
  1289         -    pBt->busyHdr.pArg = pBt;
  1290   1826       rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
  1291         -                          EXTRA_SIZE, flags, vfsFlags);
         1827  +                          EXTRA_SIZE, flags, vfsFlags, pageReinit);
  1292   1828       if( rc==SQLITE_OK ){
  1293   1829         rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
  1294   1830       }
  1295   1831       if( rc!=SQLITE_OK ){
  1296   1832         goto btree_open_out;
  1297   1833       }
  1298         -    sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr);
         1834  +    pBt->openFlags = (u8)flags;
         1835  +    pBt->db = db;
         1836  +    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
  1299   1837       p->pBt = pBt;
  1300   1838     
  1301         -    sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
  1302         -    sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
  1303   1839       pBt->pCursor = 0;
  1304   1840       pBt->pPage1 = 0;
  1305   1841       pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
  1306         -    pBt->pageSize = get2byte(&zDbHeader[16]);
         1842  +#ifdef SQLITE_SECURE_DELETE
         1843  +    pBt->secureDelete = 1;
         1844  +#endif
         1845  +    pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
  1307   1846       if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
  1308   1847            || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
  1309   1848         pBt->pageSize = 0;
  1310         -      sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
  1311   1849   #ifndef SQLITE_OMIT_AUTOVACUUM
  1312   1850         /* If the magic name ":memory:" will create an in-memory database, then
  1313   1851         ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
  1314   1852         ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
  1315   1853         ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
  1316   1854         ** regular file-name. In this case the auto-vacuum applies as per normal.
  1317   1855         */
................................................................................
  1325   1863         nReserve = zDbHeader[20];
  1326   1864         pBt->pageSizeFixed = 1;
  1327   1865   #ifndef SQLITE_OMIT_AUTOVACUUM
  1328   1866         pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
  1329   1867         pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
  1330   1868   #endif
  1331   1869       }
         1870  +    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
         1871  +    if( rc ) goto btree_open_out;
  1332   1872       pBt->usableSize = pBt->pageSize - nReserve;
  1333   1873       assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
  1334         -    sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
  1335   1874      
  1336   1875   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  1337   1876       /* Add the new BtShared object to the linked list sharable BtShareds.
  1338   1877       */
  1339   1878       if( p->sharable ){
  1340   1879         sqlite3_mutex *mutexShared;
  1341   1880         pBt->nRef = 1;
  1342   1881         mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  1343         -      if( SQLITE_THREADSAFE && sqlite3Config.bCoreMutex ){
         1882  +      if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
  1344   1883           pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
  1345   1884           if( pBt->mutex==0 ){
  1346   1885             rc = SQLITE_NOMEM;
  1347   1886             db->mallocFailed = 0;
  1348   1887             goto btree_open_out;
  1349   1888           }
  1350   1889         }
  1351   1890         sqlite3_mutex_enter(mutexShared);
  1352         -      pBt->pNext = sqlite3SharedCacheList;
  1353         -      sqlite3SharedCacheList = pBt;
         1891  +      pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
         1892  +      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
  1354   1893         sqlite3_mutex_leave(mutexShared);
  1355   1894       }
  1356   1895   #endif
  1357   1896     }
  1358   1897   
  1359   1898   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  1360   1899     /* If the new Btree uses a sharable pBtShared, then link the new
................................................................................
  1393   1932     if( rc!=SQLITE_OK ){
  1394   1933       if( pBt && pBt->pPager ){
  1395   1934         sqlite3PagerClose(pBt->pPager);
  1396   1935       }
  1397   1936       sqlite3_free(pBt);
  1398   1937       sqlite3_free(p);
  1399   1938       *ppBtree = 0;
         1939  +  }else{
         1940  +    /* If the B-Tree was successfully opened, set the pager-cache size to the
         1941  +    ** default value. Except, when opening on an existing shared pager-cache,
         1942  +    ** do not change the pager-cache size.
         1943  +    */
         1944  +    if( sqlite3BtreeSchema(p, 0, 0)==0 ){
         1945  +      sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
         1946  +    }
         1947  +  }
         1948  +  if( mutexOpen ){
         1949  +    assert( sqlite3_mutex_held(mutexOpen) );
         1950  +    sqlite3_mutex_leave(mutexOpen);
  1400   1951     }
  1401   1952     return rc;
  1402   1953   }
  1403   1954   
  1404   1955   /*
  1405   1956   ** Decrement the BtShared.nRef counter.  When it reaches zero,
  1406   1957   ** remove the BtShared structure from the sharing list.  Return
................................................................................
  1414   1965     int removed = 0;
  1415   1966   
  1416   1967     assert( sqlite3_mutex_notheld(pBt->mutex) );
  1417   1968     pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  1418   1969     sqlite3_mutex_enter(pMaster);
  1419   1970     pBt->nRef--;
  1420   1971     if( pBt->nRef<=0 ){
  1421         -    if( sqlite3SharedCacheList==pBt ){
  1422         -      sqlite3SharedCacheList = pBt->pNext;
         1972  +    if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
         1973  +      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
  1423   1974       }else{
  1424         -      pList = sqlite3SharedCacheList;
         1975  +      pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
  1425   1976         while( ALWAYS(pList) && pList->pNext!=pBt ){
  1426   1977           pList=pList->pNext;
  1427   1978         }
  1428   1979         if( ALWAYS(pList) ){
  1429   1980           pList->pNext = pBt->pNext;
  1430   1981         }
  1431   1982       }
................................................................................
  1465   2016   int sqlite3BtreeClose(Btree *p){
  1466   2017     BtShared *pBt = p->pBt;
  1467   2018     BtCursor *pCur;
  1468   2019   
  1469   2020     /* Close all cursors opened via this handle.  */
  1470   2021     assert( sqlite3_mutex_held(p->db->mutex) );
  1471   2022     sqlite3BtreeEnter(p);
  1472         -  pBt->db = p->db;
  1473   2023     pCur = pBt->pCursor;
  1474   2024     while( pCur ){
  1475   2025       BtCursor *pTmp = pCur;
  1476   2026       pCur = pCur->pNext;
  1477   2027       if( pTmp->pBtree==p ){
  1478   2028         sqlite3BtreeCloseCursor(pTmp);
  1479   2029       }
................................................................................
  1498   2048       ** Clean out and delete the BtShared object.
  1499   2049       */
  1500   2050       assert( !pBt->pCursor );
  1501   2051       sqlite3PagerClose(pBt->pPager);
  1502   2052       if( pBt->xFreeSchema && pBt->pSchema ){
  1503   2053         pBt->xFreeSchema(pBt->pSchema);
  1504   2054       }
  1505         -    sqlite3_free(pBt->pSchema);
         2055  +    sqlite3DbFree(0, pBt->pSchema);
  1506   2056       freeTempSpace(pBt);
  1507   2057       sqlite3_free(pBt);
  1508   2058     }
  1509   2059   
  1510   2060   #ifndef SQLITE_OMIT_SHARED_CACHE
  1511   2061     assert( p->wantToLock==0 );
  1512   2062     assert( p->locked==0 );
................................................................................
  1547   2097   ** how well the database resists damage due to OS crashes and power
  1548   2098   ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
  1549   2099   ** there is a high probability of damage)  Level 2 is the default.  There
  1550   2100   ** is a very low but non-zero probability of damage.  Level 3 reduces the
  1551   2101   ** probability of damage to near zero but with a write performance reduction.
  1552   2102   */
  1553   2103   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  1554         -int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
         2104  +int sqlite3BtreeSetSafetyLevel(
         2105  +  Btree *p,              /* The btree to set the safety level on */
         2106  +  int level,             /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
         2107  +  int fullSync,          /* PRAGMA fullfsync. */
         2108  +  int ckptFullSync       /* PRAGMA checkpoint_fullfync */
         2109  +){
  1555   2110     BtShared *pBt = p->pBt;
  1556   2111     assert( sqlite3_mutex_held(p->db->mutex) );
         2112  +  assert( level>=1 && level<=3 );
  1557   2113     sqlite3BtreeEnter(p);
  1558         -  sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
         2114  +  sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
  1559   2115     sqlite3BtreeLeave(p);
  1560   2116     return SQLITE_OK;
  1561   2117   }
  1562   2118   #endif
  1563   2119   
  1564   2120   /*
  1565   2121   ** Return TRUE if the given btree is set to safety level 1.  In other
................................................................................
  1575   2131     sqlite3BtreeLeave(p);
  1576   2132     return rc;
  1577   2133   }
  1578   2134   
  1579   2135   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
  1580   2136   /*
  1581   2137   ** Change the default pages size and the number of reserved bytes per page.
         2138  +** Or, if the page size has already been fixed, return SQLITE_READONLY 
         2139  +** without changing anything.
  1582   2140   **
  1583   2141   ** The page size must be a power of 2 between 512 and 65536.  If the page
  1584   2142   ** size supplied does not meet this constraint then the page size is not
  1585   2143   ** changed.
  1586   2144   **
  1587   2145   ** Page sizes are constrained to be a power of two so that the region
  1588   2146   ** of the database file used for locking (beginning at PENDING_BYTE,
  1589   2147   ** the first byte past the 1GB boundary, 0x40000000) needs to occur
  1590   2148   ** at the beginning of a page.
  1591   2149   **
  1592   2150   ** If parameter nReserve is less than zero, then the number of reserved
  1593   2151   ** bytes per page is left unchanged.
         2152  +**
         2153  +** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
         2154  +** and autovacuum mode can no longer be changed.
  1594   2155   */
  1595         -int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
         2156  +int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
  1596   2157     int rc = SQLITE_OK;
  1597   2158     BtShared *pBt = p->pBt;
         2159  +  assert( nReserve>=-1 && nReserve<=255 );
  1598   2160     sqlite3BtreeEnter(p);
  1599   2161     if( pBt->pageSizeFixed ){
  1600   2162       sqlite3BtreeLeave(p);
  1601   2163       return SQLITE_READONLY;
  1602   2164     }
  1603   2165     if( nReserve<0 ){
  1604   2166       nReserve = pBt->pageSize - pBt->usableSize;
  1605   2167     }
         2168  +  assert( nReserve>=0 && nReserve<=255 );
  1606   2169     if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
  1607   2170           ((pageSize-1)&pageSize)==0 ){
  1608   2171       assert( (pageSize & 7)==0 );
  1609   2172       assert( !pBt->pPage1 && !pBt->pCursor );
  1610         -    pBt->pageSize = pageSize;
         2173  +    pBt->pageSize = (u32)pageSize;
  1611   2174       freeTempSpace(pBt);
  1612         -    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
  1613   2175     }
  1614         -  pBt->usableSize = pBt->pageSize - nReserve;
         2176  +  rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
         2177  +  pBt->usableSize = pBt->pageSize - (u16)nReserve;
         2178  +  if( iFix ) pBt->pageSizeFixed = 1;
  1615   2179     sqlite3BtreeLeave(p);
  1616   2180     return rc;
  1617   2181   }
  1618   2182   
  1619   2183   /*
  1620   2184   ** Return the currently defined page size
  1621   2185   */
  1622   2186   int sqlite3BtreeGetPageSize(Btree *p){
  1623   2187     return p->pBt->pageSize;
  1624   2188   }
         2189  +
         2190  +/*
         2191  +** Return the number of bytes of space at the end of every page that
         2192  +** are intentually left unused.  This is the "reserved" space that is
         2193  +** sometimes used by extensions.
         2194  +*/
  1625   2195   int sqlite3BtreeGetReserve(Btree *p){
  1626   2196     int n;
  1627   2197     sqlite3BtreeEnter(p);
  1628   2198     n = p->pBt->pageSize - p->pBt->usableSize;
  1629   2199     sqlite3BtreeLeave(p);
  1630   2200     return n;
  1631   2201   }
................................................................................
  1638   2208   int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
  1639   2209     int n;
  1640   2210     sqlite3BtreeEnter(p);
  1641   2211     n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
  1642   2212     sqlite3BtreeLeave(p);
  1643   2213     return n;
  1644   2214   }
         2215  +
         2216  +/*
         2217  +** Set the secureDelete flag if newFlag is 0 or 1.  If newFlag is -1,
         2218  +** then make no changes.  Always return the value of the secureDelete
         2219  +** setting after the change.
         2220  +*/
         2221  +int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
         2222  +  int b;
         2223  +  if( p==0 ) return 0;
         2224  +  sqlite3BtreeEnter(p);
         2225  +  if( newFlag>=0 ){
         2226  +    p->pBt->secureDelete = (newFlag!=0) ? 1 : 0;
         2227  +  } 
         2228  +  b = p->pBt->secureDelete;
         2229  +  sqlite3BtreeLeave(p);
         2230  +  return b;
         2231  +}
  1645   2232   #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
  1646   2233   
  1647   2234   /*
  1648   2235   ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
  1649   2236   ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
  1650   2237   ** is disabled. The default value for the auto-vacuum property is 
  1651   2238   ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
................................................................................
  1652   2239   */
  1653   2240   int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
  1654   2241   #ifdef SQLITE_OMIT_AUTOVACUUM
  1655   2242     return SQLITE_READONLY;
  1656   2243   #else
  1657   2244     BtShared *pBt = p->pBt;
  1658   2245     int rc = SQLITE_OK;
  1659         -  int av = (autoVacuum?1:0);
         2246  +  u8 av = (u8)autoVacuum;
  1660   2247   
  1661   2248     sqlite3BtreeEnter(p);
  1662         -  if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
         2249  +  if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
  1663   2250       rc = SQLITE_READONLY;
  1664   2251     }else{
  1665         -    pBt->autoVacuum = av;
         2252  +    pBt->autoVacuum = av ?1:0;
         2253  +    pBt->incrVacuum = av==2 ?1:0;
  1666   2254     }
  1667   2255     sqlite3BtreeLeave(p);
  1668   2256     return rc;
  1669   2257   #endif
  1670   2258   }
  1671   2259   
  1672   2260   /*
................................................................................
  1696   2284   **
  1697   2285   ** SQLITE_OK is returned on success.  If the file is not a
  1698   2286   ** well-formed database file, then SQLITE_CORRUPT is returned.
  1699   2287   ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
  1700   2288   ** is returned if we run out of memory. 
  1701   2289   */
  1702   2290   static int lockBtree(BtShared *pBt){
  1703         -  int rc;
  1704         -  MemPage *pPage1;
  1705         -  int nPage;
         2291  +  int rc;              /* Result code from subfunctions */
         2292  +  MemPage *pPage1;     /* Page 1 of the database file */
         2293  +  int nPage;           /* Number of pages in the database */
         2294  +  int nPageFile = 0;   /* Number of pages in the database file */
         2295  +  int nPageHeader;     /* Number of pages in the database according to hdr */
  1706   2296   
  1707   2297     assert( sqlite3_mutex_held(pBt->mutex) );
  1708         -  if( pBt->pPage1 ) return SQLITE_OK;
  1709         -  rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
         2298  +  assert( pBt->pPage1==0 );
         2299  +  rc = sqlite3PagerSharedLock(pBt->pPager);
         2300  +  if( rc!=SQLITE_OK ) return rc;
         2301  +  rc = btreeGetPage(pBt, 1, &pPage1, 0);
  1710   2302     if( rc!=SQLITE_OK ) return rc;
  1711   2303   
  1712   2304     /* Do some checking to help insure the file we opened really is
  1713   2305     ** a valid database file. 
  1714   2306     */
  1715         -  rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
  1716         -  if( rc!=SQLITE_OK ){
  1717         -    goto page1_init_failed;
  1718         -  }else if( nPage>0 ){
  1719         -    int pageSize;
  1720         -    int usableSize;
         2307  +  nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
         2308  +  sqlite3PagerPagecount(pBt->pPager, &nPageFile);
         2309  +  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
         2310  +    nPage = nPageFile;
         2311  +  }
         2312  +  if( nPage>0 ){
         2313  +    u32 pageSize;
         2314  +    u32 usableSize;
  1721   2315       u8 *page1 = pPage1->aData;
  1722   2316       rc = SQLITE_NOTADB;
  1723   2317       if( memcmp(page1, zMagicHeader, 16)!=0 ){
  1724   2318         goto page1_init_failed;
  1725   2319       }
         2320  +
         2321  +#ifdef SQLITE_OMIT_WAL
  1726   2322       if( page1[18]>1 ){
  1727   2323         pBt->readOnly = 1;
  1728   2324       }
  1729   2325       if( page1[19]>1 ){
  1730   2326         goto page1_init_failed;
  1731   2327       }
         2328  +#else
         2329  +    if( page1[18]>2 ){
         2330  +      pBt->readOnly = 1;
         2331  +    }
         2332  +    if( page1[19]>2 ){
         2333  +      goto page1_init_failed;
         2334  +    }
         2335  +
         2336  +    /* If the write version is set to 2, this database should be accessed
         2337  +    ** in WAL mode. If the log is not already open, open it now. Then 
         2338  +    ** return SQLITE_OK and return without populating BtShared.pPage1.
         2339  +    ** The caller detects this and calls this function again. This is
         2340  +    ** required as the version of page 1 currently in the page1 buffer
         2341  +    ** may not be the latest version - there may be a newer one in the log
         2342  +    ** file.
         2343  +    */
         2344  +    if( page1[19]==2 && pBt->doNotUseWAL==0 ){
         2345  +      int isOpen = 0;
         2346  +      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
         2347  +      if( rc!=SQLITE_OK ){
         2348  +        goto page1_init_failed;
         2349  +      }else if( isOpen==0 ){
         2350  +        releasePage(pPage1);
         2351  +        return SQLITE_OK;
         2352  +      }
         2353  +      rc = SQLITE_NOTADB;
         2354  +    }
         2355  +#endif
  1732   2356   
  1733   2357       /* The maximum embedded fraction must be exactly 25%.  And the minimum
  1734   2358       ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
  1735   2359       ** The original design allowed these amounts to vary, but as of
  1736   2360       ** version 3.6.0, we require them to be fixed.
  1737   2361       */
  1738   2362       if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
  1739   2363         goto page1_init_failed;
  1740   2364       }
  1741         -    pageSize = get2byte(&page1[16]);
  1742         -    if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
  1743         -        (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
         2365  +    pageSize = (page1[16]<<8) | (page1[17]<<16);
         2366  +    if( ((pageSize-1)&pageSize)!=0
         2367  +     || pageSize>SQLITE_MAX_PAGE_SIZE 
         2368  +     || pageSize<=256 
  1744   2369       ){
  1745   2370         goto page1_init_failed;
  1746   2371       }
  1747   2372       assert( (pageSize & 7)==0 );
  1748   2373       usableSize = pageSize - page1[20];
  1749         -    if( pageSize!=pBt->pageSize ){
         2374  +    if( (u32)pageSize!=pBt->pageSize ){
  1750   2375         /* After reading the first page of the database assuming a page size
  1751   2376         ** of BtShared.pageSize, we have discovered that the page-size is
  1752   2377         ** actually pageSize. Unlock the database, leave pBt->pPage1 at
  1753   2378         ** zero and return SQLITE_OK. The caller will call this function
  1754   2379         ** again with the correct page-size.
  1755   2380         */
  1756   2381         releasePage(pPage1);
  1757   2382         pBt->usableSize = usableSize;
  1758   2383         pBt->pageSize = pageSize;
  1759   2384         freeTempSpace(pBt);
  1760         -      sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
  1761         -      return SQLITE_OK;
         2385  +      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
         2386  +                                   pageSize-usableSize);
         2387  +      return rc;
  1762   2388       }
  1763         -    if( usableSize<500 ){
         2389  +    if( nPageHeader>nPageFile ){
         2390  +      rc = SQLITE_CORRUPT_BKPT;
         2391  +      goto page1_init_failed;
         2392  +    }
         2393  +    if( usableSize<480 ){
  1764   2394         goto page1_init_failed;
  1765   2395       }
  1766   2396       pBt->pageSize = pageSize;
  1767   2397       pBt->usableSize = usableSize;
  1768   2398   #ifndef SQLITE_OMIT_AUTOVACUUM
  1769   2399       pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
  1770   2400       pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
................................................................................
  1776   2406     ** cells can will fit on one page.  We assume a 10-byte page header.
  1777   2407     ** Besides the payload, the cell must store:
  1778   2408     **     2-byte pointer to the cell
  1779   2409     **     4-byte child pointer
  1780   2410     **     9-byte nKey value
  1781   2411     **     4-byte nData value
  1782   2412     **     4-byte overflow page pointer
  1783         -  ** So a cell consists of a 2-byte poiner, a header which is as much as
         2413  +  ** So a cell consists of a 2-byte pointer, a header which is as much as
  1784   2414     ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
  1785   2415     ** page pointer.
  1786   2416     */
  1787         -  pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23;
  1788         -  pBt->minLocal = (pBt->usableSize-12)*32/255 - 23;
  1789         -  pBt->maxLeaf = pBt->usableSize - 35;
  1790         -  pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23;
         2417  +  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
         2418  +  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
         2419  +  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
         2420  +  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
  1791   2421     assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
  1792   2422     pBt->pPage1 = pPage1;
         2423  +  pBt->nPage = nPage;
  1793   2424     return SQLITE_OK;
  1794   2425   
  1795   2426   page1_init_failed:
  1796   2427     releasePage(pPage1);
  1797   2428     pBt->pPage1 = 0;
  1798   2429     return rc;
  1799   2430   }
  1800   2431   
  1801         -/*
  1802         -** This routine works like lockBtree() except that it also invokes the
  1803         -** busy callback if there is lock contention.
  1804         -*/
  1805         -static int lockBtreeWithRetry(Btree *pRef){
  1806         -  int rc = SQLITE_OK;
  1807         -
  1808         -  assert( sqlite3BtreeHoldsMutex(pRef) );
  1809         -  if( pRef->inTrans==TRANS_NONE ){
  1810         -    u8 inTransaction = pRef->pBt->inTransaction;
  1811         -    btreeIntegrity(pRef);
  1812         -    rc = sqlite3BtreeBeginTrans(pRef, 0);
  1813         -    pRef->pBt->inTransaction = inTransaction;
  1814         -    pRef->inTrans = TRANS_NONE;
  1815         -    if( rc==SQLITE_OK ){
  1816         -      pRef->pBt->nTransaction--;
  1817         -    }
  1818         -    btreeIntegrity(pRef);
  1819         -  }
  1820         -  return rc;
  1821         -}
  1822         -       
  1823         -
  1824   2432   /*
  1825   2433   ** If there are no outstanding cursors and we are not in the middle
  1826   2434   ** of a transaction but there is a read lock on the database, then
  1827   2435   ** this routine unrefs the first page of the database file which 
  1828   2436   ** has the effect of releasing the read lock.
  1829   2437   **
  1830         -** If there are any outstanding cursors, this routine is a no-op.
  1831         -**
  1832   2438   ** If there is a transaction in progress, this routine is a no-op.
  1833   2439   */
  1834   2440   static void unlockBtreeIfUnused(BtShared *pBt){
  1835   2441     assert( sqlite3_mutex_held(pBt->mutex) );
  1836         -  if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
  1837         -    if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
  1838         -      assert( pBt->pPage1->aData );
  1839         -#if 0
  1840         -      if( pBt->pPage1->aData==0 ){
  1841         -        MemPage *pPage = pBt->pPage1;
  1842         -        pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
  1843         -        pPage->pBt = pBt;
  1844         -        pPage->pgno = 1;
  1845         -      }
  1846         -#endif
  1847         -      releasePage(pBt->pPage1);
  1848         -    }
         2442  +  assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
         2443  +  if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
         2444  +    assert( pBt->pPage1->aData );
         2445  +    assert( sqlite3PagerRefcount(pBt->pPager)==1 );
         2446  +    assert( pBt->pPage1->aData );
         2447  +    releasePage(pBt->pPage1);
  1849   2448       pBt->pPage1 = 0;
  1850         -    pBt->inStmt = 0;
  1851   2449     }
  1852   2450   }
  1853   2451   
  1854   2452   /*
  1855         -** Create a new database by initializing the first page of the
  1856         -** file.
         2453  +** If pBt points to an empty file then convert that empty file
         2454  +** into a new empty database by initializing the first page of
         2455  +** the database.
  1857   2456   */
  1858   2457   static int newDatabase(BtShared *pBt){
  1859   2458     MemPage *pP1;
  1860   2459     unsigned char *data;
  1861   2460     int rc;
  1862         -  int nPage;
  1863   2461   
  1864   2462     assert( sqlite3_mutex_held(pBt->mutex) );
  1865         -  rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
  1866         -  if( rc!=SQLITE_OK || nPage>0 ){
  1867         -    return rc;
         2463  +  if( pBt->nPage>0 ){
         2464  +    return SQLITE_OK;
  1868   2465     }
  1869   2466     pP1 = pBt->pPage1;
  1870   2467     assert( pP1!=0 );
  1871   2468     data = pP1->aData;
  1872   2469     rc = sqlite3PagerWrite(pP1->pDbPage);
  1873   2470     if( rc ) return rc;
  1874   2471     memcpy(data, zMagicHeader, sizeof(zMagicHeader));
  1875   2472     assert( sizeof(zMagicHeader)==16 );
  1876         -  put2byte(&data[16], pBt->pageSize);
         2473  +  data[16] = (u8)((pBt->pageSize>>8)&0xff);
         2474  +  data[17] = (u8)((pBt->pageSize>>16)&0xff);
  1877   2475     data[18] = 1;
  1878   2476     data[19] = 1;
  1879         -  data[20] = pBt->pageSize - pBt->usableSize;
         2477  +  assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
         2478  +  data[20] = (u8)(pBt->pageSize - pBt->usableSize);
  1880   2479     data[21] = 64;
  1881   2480     data[22] = 32;
  1882   2481     data[23] = 32;
  1883   2482     memset(&data[24], 0, 100-24);
  1884   2483     zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
  1885   2484     pBt->pageSizeFixed = 1;
  1886   2485   #ifndef SQLITE_OMIT_AUTOVACUUM
  1887   2486     assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
  1888   2487     assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
  1889   2488     put4byte(&data[36 + 4*4], pBt->autoVacuum);
  1890   2489     put4byte(&data[36 + 7*4], pBt->incrVacuum);
  1891   2490   #endif
         2491  +  pBt->nPage = 1;
         2492  +  data[31] = 1;
  1892   2493     return SQLITE_OK;
  1893   2494   }
  1894   2495   
  1895   2496   /*
  1896   2497   ** Attempt to start a new transaction. A write-transaction
  1897   2498   ** is started if the second argument is nonzero, otherwise a read-
  1898   2499   ** transaction.  If the second argument is 2 or more and exclusive
................................................................................
  1924   2525   ** of A's read lock.  A tries to promote to reserved but is blocked by B.
  1925   2526   ** One or the other of the two processes must give way or there can be
  1926   2527   ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
  1927   2528   ** when A already has a read lock, we encourage A to give up and let B
  1928   2529   ** proceed.
  1929   2530   */
  1930   2531   int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
         2532  +  sqlite3 *pBlock = 0;
  1931   2533     BtShared *pBt = p->pBt;
  1932   2534     int rc = SQLITE_OK;
  1933   2535   
  1934   2536     sqlite3BtreeEnter(p);
  1935         -  pBt->db = p->db;
  1936   2537     btreeIntegrity(p);
  1937   2538   
  1938   2539     /* If the btree is already in a write-transaction, or it
  1939   2540     ** is already in a read-transaction and a read-transaction
  1940   2541     ** is requested, this is a no-op.
  1941   2542     */
  1942   2543     if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
................................................................................
  1945   2546   
  1946   2547     /* Write transactions are not possible on a read-only database */
  1947   2548     if( pBt->readOnly && wrflag ){
  1948   2549       rc = SQLITE_READONLY;
  1949   2550       goto trans_begun;
  1950   2551     }
  1951   2552   
         2553  +#ifndef SQLITE_OMIT_SHARED_CACHE
  1952   2554     /* If another database handle has already opened a write transaction 
  1953   2555     ** on this shared-btree structure and a second write transaction is
  1954         -  ** requested, return SQLITE_BUSY.
         2556  +  ** requested, return SQLITE_LOCKED.
  1955   2557     */
  1956         -  if( pBt->inTransaction==TRANS_WRITE && wrflag ){
  1957         -    rc = SQLITE_BUSY;
  1958         -    goto trans_begun;
  1959         -  }
  1960         -
  1961         -#ifndef SQLITE_OMIT_SHARED_CACHE
  1962         -  if( wrflag>1 ){
         2558  +  if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
         2559  +    pBlock = pBt->pWriter->db;
         2560  +  }else if( wrflag>1 ){
  1963   2561       BtLock *pIter;
  1964   2562       for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
  1965   2563         if( pIter->pBtree!=p ){
  1966         -        rc = SQLITE_BUSY;
  1967         -        goto trans_begun;
         2564  +        pBlock = pIter->pBtree->db;
         2565  +        break;
  1968   2566         }
  1969   2567       }
  1970   2568     }
         2569  +  if( pBlock ){
         2570  +    sqlite3ConnectionBlocked(p->db, pBlock);
         2571  +    rc = SQLITE_LOCKED_SHAREDCACHE;
         2572  +    goto trans_begun;
         2573  +  }
  1971   2574   #endif
  1972   2575   
         2576  +  /* Any read-only or read-write transaction implies a read-lock on 
         2577  +  ** page 1. So if some other shared-cache client already has a write-lock 
         2578  +  ** on page 1, the transaction cannot be opened. */
         2579  +  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
         2580  +  if( SQLITE_OK!=rc ) goto trans_begun;
         2581  +
         2582  +  pBt->initiallyEmpty = (u8)(pBt->nPage==0);
  1973   2583     do {
  1974         -    if( pBt->pPage1==0 ){
  1975         -      do{
  1976         -        rc = lockBtree(pBt);
  1977         -      }while( pBt->pPage1==0 && rc==SQLITE_OK );
  1978         -    }
         2584  +    /* Call lockBtree() until either pBt->pPage1 is populated or
         2585  +    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
         2586  +    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
         2587  +    ** reading page 1 it discovers that the page-size of the database 
         2588  +    ** file is not pBt->pageSize. In this case lockBtree() will update
         2589  +    ** pBt->pageSize to the page-size of the file on disk.
         2590  +    */
         2591  +    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
  1979   2592   
  1980   2593       if( rc==SQLITE_OK && wrflag ){
  1981   2594         if( pBt->readOnly ){
  1982   2595           rc = SQLITE_READONLY;
  1983   2596         }else{
  1984         -        rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
         2597  +        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
  1985   2598           if( rc==SQLITE_OK ){
  1986   2599             rc = newDatabase(pBt);
  1987   2600           }
  1988   2601         }
  1989   2602       }
  1990   2603     
  1991         -    if( rc==SQLITE_OK ){
  1992         -      if( wrflag ) pBt->inStmt = 0;
  1993         -    }else{
         2604  +    if( rc!=SQLITE_OK ){
  1994   2605         unlockBtreeIfUnused(pBt);
  1995   2606       }
  1996         -  }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
  1997         -          sqlite3BtreeInvokeBusyHandler(pBt, 0) );
         2607  +  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
         2608  +          btreeInvokeBusyHandler(pBt) );
  1998   2609   
  1999   2610     if( rc==SQLITE_OK ){
  2000   2611       if( p->inTrans==TRANS_NONE ){
  2001   2612         pBt->nTransaction++;
         2613  +#ifndef SQLITE_OMIT_SHARED_CACHE
         2614  +      if( p->sharable ){
         2615  +	assert( p->lock.pBtree==p && p->lock.iTable==1 );
         2616  +        p->lock.eLock = READ_LOCK;
         2617  +        p->lock.pNext = pBt->pLock;
         2618  +        pBt->pLock = &p->lock;
         2619  +      }
         2620  +#endif
  2002   2621       }
  2003   2622       p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
  2004   2623       if( p->inTrans>pBt->inTransaction ){
  2005   2624         pBt->inTransaction = p->inTrans;
  2006   2625       }
         2626  +    if( wrflag ){
         2627  +      MemPage *pPage1 = pBt->pPage1;
  2007   2628   #ifndef SQLITE_OMIT_SHARED_CACHE
  2008         -    if( wrflag>1 ){
  2009         -      assert( !pBt->pExclusive );
  2010         -      pBt->pExclusive = p;
         2629  +      assert( !pBt->pWriter );
         2630  +      pBt->pWriter = p;
         2631  +      pBt->isExclusive = (u8)(wrflag>1);
         2632  +#endif
         2633  +
         2634  +      /* If the db-size header field is incorrect (as it may be if an old
         2635  +      ** client has been writing the database file), update it now. Doing
         2636  +      ** this sooner rather than later means the database size can safely 
         2637  +      ** re-read the database size from page 1 if a savepoint or transaction
         2638  +      ** rollback occurs within the transaction.
         2639  +      */
         2640  +      if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
         2641  +        rc = sqlite3PagerWrite(pPage1->pDbPage);
         2642  +        if( rc==SQLITE_OK ){
         2643  +          put4byte(&pPage1->aData[28], pBt->nPage);
         2644  +        }
         2645  +      }
  2011   2646       }
  2012         -#endif
  2013   2647     }
  2014   2648   
  2015   2649   
  2016   2650   trans_begun:
         2651  +  if( rc==SQLITE_OK && wrflag ){
         2652  +    /* This call makes sure that the pager has the correct number of
         2653  +    ** open savepoints. If the second parameter is greater than 0 and
         2654  +    ** the sub-journal is not already open, then it will be opened here.
         2655  +    */
         2656  +    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
         2657  +  }
         2658  +
  2017   2659     btreeIntegrity(p);
  2018   2660     sqlite3BtreeLeave(p);
  2019   2661     return rc;
  2020   2662   }
  2021         -
  2022         -/*
  2023         -** Return the size of the database file in pages.  Or return -1 if
  2024         -** there is any kind of error.
  2025         -*/
  2026         -static int pagerPagecount(Pager *pPager){
  2027         -  int rc;
  2028         -  int nPage;
  2029         -  rc = sqlite3PagerPagecount(pPager, &nPage);
  2030         -  return (rc==SQLITE_OK?nPage:-1);
  2031         -}
  2032         -
  2033   2663   
  2034   2664   #ifndef SQLITE_OMIT_AUTOVACUUM
  2035   2665   
  2036   2666   /*
  2037   2667   ** Set the pointer-map entries for all children of page pPage. Also, if
  2038   2668   ** pPage contains cells that point to overflow pages, set the pointer
  2039   2669   ** map entries for the overflow pages as well.
  2040   2670   */
  2041   2671   static int setChildPtrmaps(MemPage *pPage){
  2042   2672     int i;                             /* Counter variable */
  2043   2673     int nCell;                         /* Number of cells in page pPage */
  2044   2674     int rc;                            /* Return code */
  2045   2675     BtShared *pBt = pPage->pBt;
  2046         -  int isInitOrig = pPage->isInit;
         2676  +  u8 isInitOrig = pPage->isInit;
  2047   2677     Pgno pgno = pPage->pgno;
  2048   2678   
  2049   2679     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  2050         -  rc = sqlite3BtreeInitPage(pPage, pPage->pParent);
         2680  +  rc = btreeInitPage(pPage);
  2051   2681     if( rc!=SQLITE_OK ){
  2052   2682       goto set_child_ptrmaps_out;
  2053   2683     }
  2054   2684     nCell = pPage->nCell;
  2055   2685   
  2056   2686     for(i=0; i<nCell; i++){
  2057   2687       u8 *pCell = findCell(pPage, i);
  2058   2688   
  2059         -    rc = ptrmapPutOvflPtr(pPage, pCell);
  2060         -    if( rc!=SQLITE_OK ){
  2061         -      goto set_child_ptrmaps_out;
  2062         -    }
         2689  +    ptrmapPutOvflPtr(pPage, pCell, &rc);
  2063   2690   
  2064   2691       if( !pPage->leaf ){
  2065   2692         Pgno childPgno = get4byte(pCell);
  2066         -      rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
  2067         -       if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
         2693  +      ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
  2068   2694       }
  2069   2695     }
  2070   2696   
  2071   2697     if( !pPage->leaf ){
  2072   2698       Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
  2073         -    rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
         2699  +    ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
  2074   2700     }
  2075   2701   
  2076   2702   set_child_ptrmaps_out:
  2077   2703     pPage->isInit = isInitOrig;
  2078   2704     return rc;
  2079   2705   }
  2080   2706   
  2081   2707   /*
  2082         -** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
  2083         -** page, is a pointer to page iFrom. Modify this pointer so that it points to
  2084         -** iTo. Parameter eType describes the type of pointer to be modified, as 
  2085         -** follows:
         2708  +** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
         2709  +** that it points to iTo. Parameter eType describes the type of pointer to
         2710  +** be modified, as  follows:
  2086   2711   **
  2087   2712   ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
  2088   2713   **                   page of pPage.
  2089   2714   **
  2090   2715   ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
  2091   2716   **                   page pointed to by one of the cells on pPage.
  2092   2717   **
  2093   2718   ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
  2094   2719   **                   overflow page in the list.
  2095   2720   */
  2096   2721   static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
  2097   2722     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
         2723  +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  2098   2724     if( eType==PTRMAP_OVERFLOW2 ){
  2099   2725       /* The pointer is always the first 4 bytes of the page in this case.  */
  2100   2726       if( get4byte(pPage->aData)!=iFrom ){
  2101   2727         return SQLITE_CORRUPT_BKPT;
  2102   2728       }
  2103   2729       put4byte(pPage->aData, iTo);
  2104   2730     }else{
  2105         -    int isInitOrig = pPage->isInit;
         2731  +    u8 isInitOrig = pPage->isInit;
  2106   2732       int i;
  2107   2733       int nCell;
  2108   2734   
  2109         -    sqlite3BtreeInitPage(pPage, 0);
         2735  +    btreeInitPage(pPage);
  2110   2736       nCell = pPage->nCell;
  2111   2737   
  2112   2738       for(i=0; i<nCell; i++){
  2113   2739         u8 *pCell = findCell(pPage, i);
  2114   2740         if( eType==PTRMAP_OVERFLOW1 ){
  2115   2741           CellInfo info;
  2116         -        sqlite3BtreeParseCellPtr(pPage, pCell, &info);
         2742  +        btreeParseCellPtr(pPage, pCell, &info);
  2117   2743           if( info.iOverflow ){
  2118   2744             if( iFrom==get4byte(&pCell[info.iOverflow]) ){
  2119   2745               put4byte(&pCell[info.iOverflow], iTo);
  2120   2746               break;
  2121   2747             }
  2122   2748           }
  2123   2749         }else{
................................................................................
  2141   2767     return SQLITE_OK;
  2142   2768   }
  2143   2769   
  2144   2770   
  2145   2771   /*
  2146   2772   ** Move the open database page pDbPage to location iFreePage in the 
  2147   2773   ** database. The pDbPage reference remains valid.
         2774  +**
         2775  +** The isCommit flag indicates that there is no need to remember that
         2776  +** the journal needs to be sync()ed before database page pDbPage->pgno 
         2777  +** can be written to. The caller has already promised not to write to that
         2778  +** page.
  2148   2779   */
  2149   2780   static int relocatePage(
  2150   2781     BtShared *pBt,           /* Btree */
  2151   2782     MemPage *pDbPage,        /* Open page to move */
  2152   2783     u8 eType,                /* Pointer map 'type' entry for pDbPage */
  2153   2784     Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
  2154   2785     Pgno iFreePage,          /* The location to move pDbPage to */
  2155         -  int isCommit
         2786  +  int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
  2156   2787   ){
  2157   2788     MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
  2158   2789     Pgno iDbPage = pDbPage->pgno;
  2159   2790     Pager *pPager = pBt->pPager;
  2160   2791     int rc;
  2161   2792   
  2162   2793     assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
................................................................................
  2185   2816       rc = setChildPtrmaps(pDbPage);
  2186   2817       if( rc!=SQLITE_OK ){
  2187   2818         return rc;
  2188   2819       }
  2189   2820     }else{
  2190   2821       Pgno nextOvfl = get4byte(pDbPage->aData);
  2191   2822       if( nextOvfl!=0 ){
  2192         -      rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
         2823  +      ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
  2193   2824         if( rc!=SQLITE_OK ){
  2194   2825           return rc;
  2195   2826         }
  2196   2827       }
  2197   2828     }
  2198   2829   
  2199   2830     /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
  2200   2831     ** that it points at iFreePage. Also fix the pointer map entry for
  2201   2832     ** iPtrPage.
  2202   2833     */
  2203   2834     if( eType!=PTRMAP_ROOTPAGE ){
  2204         -    rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
         2835  +    rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
  2205   2836       if( rc!=SQLITE_OK ){
  2206   2837         return rc;
  2207   2838       }
  2208   2839       rc = sqlite3PagerWrite(pPtrPage->pDbPage);
  2209   2840       if( rc!=SQLITE_OK ){
  2210   2841         releasePage(pPtrPage);
  2211   2842         return rc;
  2212   2843       }
  2213   2844       rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
  2214   2845       releasePage(pPtrPage);
  2215   2846       if( rc==SQLITE_OK ){
  2216         -      rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
         2847  +      ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
  2217   2848       }
  2218   2849     }
  2219   2850     return rc;
  2220   2851   }
  2221   2852   
  2222   2853   /* Forward declaration required by incrVacuumStep(). */
  2223   2854   static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
................................................................................
  2227   2858   ** return SQLITE_OK. If there is no work to do (and therefore no
  2228   2859   ** point in calling this function again), return SQLITE_DONE.
  2229   2860   **
  2230   2861   ** More specificly, this function attempts to re-organize the 
  2231   2862   ** database so that the last page of the file currently in use
  2232   2863   ** is no longer in use.
  2233   2864   **
  2234         -** If the nFin parameter is non-zero, the implementation assumes
         2865  +** If the nFin parameter is non-zero, this function assumes
  2235   2866   ** that the caller will keep calling incrVacuumStep() until
  2236   2867   ** it returns SQLITE_DONE or an error, and that nFin is the
  2237   2868   ** number of pages the database file will contain after this 
  2238         -** process is complete.
         2869  +** process is complete.  If nFin is zero, it is assumed that
         2870  +** incrVacuumStep() will be called a finite amount of times
         2871  +** which may or may not empty the freelist.  A full autovacuum
         2872  +** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
  2239   2873   */
  2240         -static int incrVacuumStep(BtShared *pBt, Pgno nFin){
  2241         -  Pgno iLastPg;             /* Last page in the database */
         2874  +static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
  2242   2875     Pgno nFreeList;           /* Number of pages still on the free-list */
         2876  +  int rc;
  2243   2877   
  2244   2878     assert( sqlite3_mutex_held(pBt->mutex) );
  2245         -  iLastPg = pBt->nTrunc;
  2246         -  if( iLastPg==0 ){
  2247         -    iLastPg = pagerPagecount(pBt->pPager);
  2248         -  }
         2879  +  assert( iLastPg>nFin );
  2249   2880   
  2250   2881     if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
  2251         -    int rc;
  2252   2882       u8 eType;
  2253   2883       Pgno iPtrPage;
  2254   2884   
  2255   2885       nFreeList = get4byte(&pBt->pPage1->aData[36]);
  2256         -    if( nFreeList==0 || nFin==iLastPg ){
         2886  +    if( nFreeList==0 ){
  2257   2887         return SQLITE_DONE;
  2258   2888       }
  2259   2889   
  2260   2890       rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
  2261   2891       if( rc!=SQLITE_OK ){
  2262   2892         return rc;
  2263   2893       }
................................................................................
  2281   2911           assert( iFreePg==iLastPg );
  2282   2912           releasePage(pFreePg);
  2283   2913         }
  2284   2914       } else {
  2285   2915         Pgno iFreePg;             /* Index of free page to move pLastPg to */
  2286   2916         MemPage *pLastPg;
  2287   2917   
  2288         -      rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
         2918  +      rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
  2289   2919         if( rc!=SQLITE_OK ){
  2290   2920           return rc;
  2291   2921         }
  2292   2922   
  2293   2923         /* If nFin is zero, this loop runs exactly once and page pLastPg
  2294   2924         ** is swapped with the first free page pulled off the free list.
  2295   2925         **
................................................................................
  2315   2945         releasePage(pLastPg);
  2316   2946         if( rc!=SQLITE_OK ){
  2317   2947           return rc;
  2318   2948         }
  2319   2949       }
  2320   2950     }
  2321   2951   
  2322         -  pBt->nTrunc = iLastPg - 1;
  2323         -  while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){
  2324         -    pBt->nTrunc--;
         2952  +  if( nFin==0 ){
         2953  +    iLastPg--;
         2954  +    while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
         2955  +      if( PTRMAP_ISPAGE(pBt, iLastPg) ){
         2956  +        MemPage *pPg;
         2957  +        rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
         2958  +        if( rc!=SQLITE_OK ){
         2959  +          return rc;
         2960  +        }
         2961  +        rc = sqlite3PagerWrite(pPg->pDbPage);
         2962  +        releasePage(pPg);
         2963  +        if( rc!=SQLITE_OK ){
         2964  +          return rc;
         2965  +        }
         2966  +      }
         2967  +      iLastPg--;
         2968  +    }
         2969  +    sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
         2970  +    pBt->nPage = iLastPg;
  2325   2971     }
  2326   2972     return SQLITE_OK;
  2327   2973   }
  2328   2974   
  2329   2975   /*
  2330   2976   ** A write-transaction must be opened before calling this function.
  2331   2977   ** It performs a single unit of work towards an incremental vacuum.
  2332   2978   **
  2333   2979   ** If the incremental vacuum is finished after this function has run,
  2334         -** SQLITE_DONE is returned. If it is not finished, but no error occured,
         2980  +** SQLITE_DONE is returned. If it is not finished, but no error occurred,
  2335   2981   ** SQLITE_OK is returned. Otherwise an SQLite error code. 
  2336   2982   */
  2337   2983   int sqlite3BtreeIncrVacuum(Btree *p){
  2338   2984     int rc;
  2339   2985     BtShared *pBt = p->pBt;
  2340   2986   
  2341   2987     sqlite3BtreeEnter(p);
  2342         -  pBt->db = p->db;
  2343   2988     assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
  2344   2989     if( !pBt->autoVacuum ){
  2345   2990       rc = SQLITE_DONE;
  2346   2991     }else{
  2347   2992       invalidateAllOverflowCache(pBt);
  2348         -    rc = incrVacuumStep(pBt, 0);
         2993  +    rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
         2994  +    if( rc==SQLITE_OK ){
         2995  +      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
         2996  +      put4byte(&pBt->pPage1->aData[28], pBt->nPage);
         2997  +    }
  2349   2998     }
  2350   2999     sqlite3BtreeLeave(p);
  2351   3000     return rc;
  2352   3001   }
  2353   3002   
  2354   3003   /*
  2355   3004   ** This routine is called prior to sqlite3PagerCommit when a transaction
................................................................................
  2356   3005   ** is commited for an auto-vacuum database.
  2357   3006   **
  2358   3007   ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
  2359   3008   ** the database file should be truncated to during the commit process. 
  2360   3009   ** i.e. the database has been reorganized so that only the first *pnTrunc
  2361   3010   ** pages are in use.
  2362   3011   */
  2363         -static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
         3012  +static int autoVacuumCommit(BtShared *pBt){
  2364   3013     int rc = SQLITE_OK;
  2365   3014     Pager *pPager = pBt->pPager;
  2366         -#ifndef NDEBUG
  2367         -  int nRef = sqlite3PagerRefcount(pPager);
  2368         -#endif
         3015  +  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
  2369   3016   
  2370   3017     assert( sqlite3_mutex_held(pBt->mutex) );
  2371   3018     invalidateAllOverflowCache(pBt);
  2372   3019     assert(pBt->autoVacuum);
  2373   3020     if( !pBt->incrVacuum ){
  2374         -    Pgno nFin = 0;
  2375         -
  2376         -    if( pBt->nTrunc==0 ){
  2377         -      Pgno nFree;
  2378         -      Pgno nPtrmap;
  2379         -      const int pgsz = pBt->pageSize;
  2380         -      int nOrig = pagerPagecount(pBt->pPager);
  2381         -
  2382         -      if( PTRMAP_ISPAGE(pBt, nOrig) ){
  2383         -        return SQLITE_CORRUPT_BKPT;
  2384         -      }
  2385         -      if( nOrig==PENDING_BYTE_PAGE(pBt) ){
  2386         -        nOrig--;
  2387         -      }
  2388         -      nFree = get4byte(&pBt->pPage1->aData[36]);
  2389         -      nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
  2390         -      nFin = nOrig - nFree - nPtrmap;
  2391         -      if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
  2392         -        nFin--;
  2393         -      }
  2394         -      while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
  2395         -        nFin--;
  2396         -      }
  2397         -    }
  2398         -
  2399         -    while( rc==SQLITE_OK ){
  2400         -      rc = incrVacuumStep(pBt, nFin);
  2401         -    }
  2402         -    if( rc==SQLITE_DONE ){
  2403         -      assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc);
  2404         -      rc = SQLITE_OK;
  2405         -      if( pBt->nTrunc && nFin ){
  2406         -        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
  2407         -        put4byte(&pBt->pPage1->aData[32], 0);
  2408         -        put4byte(&pBt->pPage1->aData[36], 0);
  2409         -        pBt->nTrunc = nFin;
  2410         -      }
         3021  +    Pgno nFin;         /* Number of pages in database after autovacuuming */
         3022  +    Pgno nFree;        /* Number of pages on the freelist initially */
         3023  +    Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
         3024  +    Pgno iFree;        /* The next page to be freed */
         3025  +    int nEntry;        /* Number of entries on one ptrmap page */
         3026  +    Pgno nOrig;        /* Database size before freeing */
         3027  +
         3028  +    nOrig = btreePagecount(pBt);
         3029  +    if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
         3030  +      /* It is not possible to create a database for which the final page
         3031  +      ** is either a pointer-map page or the pending-byte page. If one
         3032  +      ** is encountered, this indicates corruption.
         3033  +      */
         3034  +      return SQLITE_CORRUPT_BKPT;
         3035  +    }
         3036  +
         3037  +    nFree = get4byte(&pBt->pPage1->aData[36]);
         3038  +    nEntry = pBt->usableSize/5;
         3039  +    nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
         3040  +    nFin = nOrig - nFree - nPtrmap;
         3041  +    if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
         3042  +      nFin--;
         3043  +    }
         3044  +    while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
         3045  +      nFin--;
         3046  +    }
         3047  +    if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
         3048  +
         3049  +    for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
         3050  +      rc = incrVacuumStep(pBt, nFin, iFree);
         3051  +    }
         3052  +    if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
         3053  +      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
         3054  +      put4byte(&pBt->pPage1->aData[32], 0);
         3055  +      put4byte(&pBt->pPage1->aData[36], 0);
         3056  +      put4byte(&pBt->pPage1->aData[28], nFin);
         3057  +      sqlite3PagerTruncateImage(pBt->pPager, nFin);
         3058  +      pBt->nPage = nFin;
  2411   3059       }
  2412   3060       if( rc!=SQLITE_OK ){
  2413   3061         sqlite3PagerRollback(pPager);
  2414   3062       }
  2415   3063     }
  2416   3064   
  2417         -  if( rc==SQLITE_OK ){
  2418         -    *pnTrunc = pBt->nTrunc;
  2419         -    pBt->nTrunc = 0;
  2420         -  }
  2421   3065     assert( nRef==sqlite3PagerRefcount(pPager) );
  2422   3066     return rc;
  2423   3067   }
  2424   3068   
         3069  +#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
         3070  +# define setChildPtrmaps(x) SQLITE_OK
  2425   3071   #endif
  2426   3072   
  2427   3073   /*
  2428   3074   ** This routine does the first phase of a two-phase commit.  This routine
  2429   3075   ** causes a rollback journal to be created (if it does not already exist)
  2430   3076   ** and populated with enough information so that if a power loss occurs
  2431   3077   ** the database can be restored to its original state by playing back
  2432   3078   ** the journal.  Then the contents of the journal are flushed out to
  2433   3079   ** the disk.  After the journal is safely on oxide, the changes to the
  2434   3080   ** database are written into the database file and flushed to oxide.
  2435   3081   ** At the end of this call, the rollback journal still exists on the
  2436   3082   ** disk and we are still holding all locks, so the transaction has not
  2437         -** committed.  See sqlite3BtreeCommit() for the second phase of the
         3083  +** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
  2438   3084   ** commit process.
  2439   3085   **
  2440   3086   ** This call is a no-op if no write-transaction is currently active on pBt.
  2441   3087   **
  2442   3088   ** Otherwise, sync the database file for the btree pBt. zMaster points to
  2443   3089   ** the name of a master journal file that should be written into the
  2444   3090   ** individual journal file, or is NULL, indicating no master journal file 
................................................................................
  2450   3096   ** Once this is routine has returned, the only thing required to commit
  2451   3097   ** the write-transaction for this database file is to delete the journal.
  2452   3098   */
  2453   3099   int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
  2454   3100     int rc = SQLITE_OK;
  2455   3101     if( p->inTrans==TRANS_WRITE ){
  2456   3102       BtShared *pBt = p->pBt;
  2457         -    Pgno nTrunc = 0;
  2458   3103       sqlite3BtreeEnter(p);
  2459         -    pBt->db = p->db;
  2460   3104   #ifndef SQLITE_OMIT_AUTOVACUUM
  2461   3105       if( pBt->autoVacuum ){
  2462         -      rc = autoVacuumCommit(pBt, &nTrunc); 
         3106  +      rc = autoVacuumCommit(pBt);
  2463   3107         if( rc!=SQLITE_OK ){
  2464   3108           sqlite3BtreeLeave(p);
  2465   3109           return rc;
  2466   3110         }
  2467   3111       }
  2468   3112   #endif
  2469         -    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0);
         3113  +    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
  2470   3114       sqlite3BtreeLeave(p);
  2471   3115     }
  2472   3116     return rc;
  2473   3117   }
         3118  +
         3119  +/*
         3120  +** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
         3121  +** at the conclusion of a transaction.
         3122  +*/
         3123  +static void btreeEndTransaction(Btree *p){
         3124  +  BtShared *pBt = p->pBt;
         3125  +  assert( sqlite3BtreeHoldsMutex(p) );
         3126  +
         3127  +  btreeClearHasContent(pBt);
         3128  +  if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
         3129  +    /* If there are other active statements that belong to this database
         3130  +    ** handle, downgrade to a read-only transaction. The other statements
         3131  +    ** may still be reading from the database.  */
         3132  +    downgradeAllSharedCacheTableLocks(p);
         3133  +    p->inTrans = TRANS_READ;
         3134  +  }else{
         3135  +    /* If the handle had any kind of transaction open, decrement the 
         3136  +    ** transaction count of the shared btree. If the transaction count 
         3137  +    ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
         3138  +    ** call below will unlock the pager.  */
         3139  +    if( p->inTrans!=TRANS_NONE ){
         3140  +      clearAllSharedCacheTableLocks(p);
         3141  +      pBt->nTransaction--;
         3142  +      if( 0==pBt->nTransaction ){
         3143  +        pBt->inTransaction = TRANS_NONE;
         3144  +      }
         3145  +    }
         3146  +
         3147  +    /* Set the current transaction state to TRANS_NONE and unlock the 
         3148  +    ** pager if this call closed the only read or write transaction.  */
         3149  +    p->inTrans = TRANS_NONE;
         3150  +    unlockBtreeIfUnused(pBt);
         3151  +  }
         3152  +
         3153  +  btreeIntegrity(p);
         3154  +}
  2474   3155   
  2475   3156   /*
  2476   3157   ** Commit the transaction currently in progress.
  2477   3158   **
  2478   3159   ** This routine implements the second phase of a 2-phase commit.  The
  2479         -** sqlite3BtreeSync() routine does the first phase and should be invoked
  2480         -** prior to calling this routine.  The sqlite3BtreeSync() routine did
  2481         -** all the work of writing information out to disk and flushing the
         3160  +** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
         3161  +** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
         3162  +** routine did all the work of writing information out to disk and flushing the
  2482   3163   ** contents so that they are written onto the disk platter.  All this
  2483         -** routine has to do is delete or truncate the rollback journal
  2484         -** (which causes the transaction to commit) and drop locks.
         3164  +** routine has to do is delete or truncate or zero the header in the
         3165  +** the rollback journal (which causes the transaction to commit) and
         3166  +** drop locks.
  2485   3167   **
  2486   3168   ** This will release the write lock on the database file.  If there
  2487   3169   ** are no active cursors, it also releases the read lock.
  2488   3170   */
  2489   3171   int sqlite3BtreeCommitPhaseTwo(Btree *p){
  2490         -  BtShared *pBt = p->pBt;
  2491   3172   
         3173  +  if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
  2492   3174     sqlite3BtreeEnter(p);
  2493         -  pBt->db = p->db;
  2494   3175     btreeIntegrity(p);
  2495   3176   
  2496   3177     /* If the handle has a write-transaction open, commit the shared-btrees 
  2497   3178     ** transaction and set the shared state to TRANS_READ.
  2498   3179     */
  2499   3180     if( p->inTrans==TRANS_WRITE ){
  2500   3181       int rc;
         3182  +    BtShared *pBt = p->pBt;
  2501   3183       assert( pBt->inTransaction==TRANS_WRITE );
  2502   3184       assert( pBt->nTransaction>0 );
  2503   3185       rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
  2504   3186       if( rc!=SQLITE_OK ){
  2505   3187         sqlite3BtreeLeave(p);
  2506   3188         return rc;
  2507   3189       }
  2508   3190       pBt->inTransaction = TRANS_READ;
  2509         -    pBt->inStmt = 0;
  2510         -  }
  2511         -  unlockAllTables(p);
  2512         -
  2513         -  /* If the handle has any kind of transaction open, decrement the transaction
  2514         -  ** count of the shared btree. If the transaction count reaches 0, set
  2515         -  ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
  2516         -  ** will unlock the pager.
  2517         -  */
  2518         -  if( p->inTrans!=TRANS_NONE ){
  2519         -    pBt->nTransaction--;
  2520         -    if( 0==pBt->nTransaction ){
  2521         -      pBt->inTransaction = TRANS_NONE;
  2522         -    }
  2523   3191     }
  2524   3192   
  2525         -  /* Set the handles current transaction state to TRANS_NONE and unlock
  2526         -  ** the pager if this call closed the only read or write transaction.
  2527         -  */
  2528         -  p->inTrans = TRANS_NONE;
  2529         -  unlockBtreeIfUnused(pBt);
  2530         -
  2531         -  btreeIntegrity(p);
         3193  +  btreeEndTransaction(p);
  2532   3194     sqlite3BtreeLeave(p);
  2533   3195     return SQLITE_OK;
  2534   3196   }
  2535   3197   
  2536   3198   /*
  2537   3199   ** Do both phases of a commit.
  2538   3200   */
................................................................................
  2585   3247   ** save the state of the cursor.  The cursor must be
  2586   3248   ** invalidated.
  2587   3249   */
  2588   3250   void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
  2589   3251     BtCursor *p;
  2590   3252     sqlite3BtreeEnter(pBtree);
  2591   3253     for(p=pBtree->pBt->pCursor; p; p=p->pNext){
  2592         -    clearCursorPosition(p);
         3254  +    int i;
         3255  +    sqlite3BtreeClearCursor(p);
  2593   3256       p->eState = CURSOR_FAULT;
  2594         -    p->skip = errCode;
         3257  +    p->skipNext = errCode;
         3258  +    for(i=0; i<=p->iPage; i++){
         3259  +      releasePage(p->apPage[i]);
         3260  +      p->apPage[i] = 0;
         3261  +    }
  2595   3262     }
  2596   3263     sqlite3BtreeLeave(pBtree);
  2597   3264   }
  2598   3265   
  2599   3266   /*
  2600   3267   ** Rollback the transaction in progress.  All cursors will be
  2601   3268   ** invalided by this operation.  Any attempt to use a cursor
................................................................................
  2607   3274   */
  2608   3275   int sqlite3BtreeRollback(Btree *p){
  2609   3276     int rc;
  2610   3277     BtShared *pBt = p->pBt;
  2611   3278     MemPage *pPage1;
  2612   3279   
  2613   3280     sqlite3BtreeEnter(p);
  2614         -  pBt->db = p->db;
  2615   3281     rc = saveAllCursors(pBt, 0, 0);
  2616   3282   #ifndef SQLITE_OMIT_SHARED_CACHE
  2617   3283     if( rc!=SQLITE_OK ){
  2618         -    /* This is a horrible situation. An IO or malloc() error occured whilst
         3284  +    /* This is a horrible situation. An IO or malloc() error occurred whilst
  2619   3285       ** trying to save cursor positions. If this is an automatic rollback (as
  2620   3286       ** the result of a constraint, malloc() failure or IO error) then 
  2621   3287       ** the cache may be internally inconsistent (not contain valid trees) so
  2622   3288       ** we cannot simply return the error to the caller. Instead, abort 
  2623   3289       ** all queries that may be using any of the cursors that failed to save.
  2624   3290       */
  2625   3291       sqlite3BtreeTripAllCursors(p, rc);
  2626   3292     }
  2627   3293   #endif
  2628   3294     btreeIntegrity(p);
  2629         -  unlockAllTables(p);
  2630   3295   
  2631   3296     if( p->inTrans==TRANS_WRITE ){
  2632   3297       int rc2;
  2633   3298   
  2634         -#ifndef SQLITE_OMIT_AUTOVACUUM
  2635         -    pBt->nTrunc = 0;
  2636         -#endif
  2637         -
  2638   3299       assert( TRANS_WRITE==pBt->inTransaction );
  2639   3300       rc2 = sqlite3PagerRollback(pBt->pPager);
  2640   3301       if( rc2!=SQLITE_OK ){
  2641   3302         rc = rc2;
  2642   3303       }
  2643   3304   
  2644   3305       /* The rollback may have destroyed the pPage1->aData value.  So
  2645         -    ** call sqlite3BtreeGetPage() on page 1 again to make
         3306  +    ** call btreeGetPage() on page 1 again to make
  2646   3307       ** sure pPage1->aData is set correctly. */
  2647         -    if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
         3308  +    if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
         3309  +      int nPage = get4byte(28+(u8*)pPage1->aData);
         3310  +      testcase( nPage==0 );
         3311  +      if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
         3312  +      testcase( pBt->nPage!=nPage );
         3313  +      pBt->nPage = nPage;
  2648   3314         releasePage(pPage1);
  2649   3315       }
  2650   3316       assert( countWriteCursors(pBt)==0 );
  2651   3317       pBt->inTransaction = TRANS_READ;
  2652   3318     }
  2653   3319   
  2654         -  if( p->inTrans!=TRANS_NONE ){
  2655         -    assert( pBt->nTransaction>0 );
  2656         -    pBt->nTransaction--;
  2657         -    if( 0==pBt->nTransaction ){
  2658         -      pBt->inTransaction = TRANS_NONE;
  2659         -    }
  2660         -  }
  2661         -
  2662         -  p->inTrans = TRANS_NONE;
  2663         -  pBt->inStmt = 0;
  2664         -  unlockBtreeIfUnused(pBt);
  2665         -
  2666         -  btreeIntegrity(p);
         3320  +  btreeEndTransaction(p);
  2667   3321     sqlite3BtreeLeave(p);
  2668   3322     return rc;
  2669   3323   }
  2670   3324   
  2671   3325   /*
  2672         -** Start a statement subtransaction.  The subtransaction can
  2673         -** can be rolled back independently of the main transaction.
  2674         -** You must start a transaction before starting a subtransaction.
  2675         -** The subtransaction is ended automatically if the main transaction
  2676         -** commits or rolls back.
  2677         -**
  2678         -** Only one subtransaction may be active at a time.  It is an error to try
  2679         -** to start a new subtransaction if another subtransaction is already active.
         3326  +** Start a statement subtransaction. The subtransaction can can be rolled
         3327  +** back independently of the main transaction. You must start a transaction 
         3328  +** before starting a subtransaction. The subtransaction is ended automatically 
         3329  +** if the main transaction commits or rolls back.
  2680   3330   **
  2681   3331   ** Statement subtransactions are used around individual SQL statements
  2682   3332   ** that are contained within a BEGIN...COMMIT block.  If a constraint
  2683   3333   ** error occurs within the statement, the effect of that one statement
  2684   3334   ** can be rolled back without having to rollback the entire transaction.
         3335  +**
         3336  +** A statement sub-transaction is implemented as an anonymous savepoint. The
         3337  +** value passed as the second parameter is the total number of savepoints,
         3338  +** including the new anonymous savepoint, open on the B-Tree. i.e. if there
         3339  +** are no active savepoints and no other statement-transactions open,
         3340  +** iStatement is 1. This anonymous savepoint can be released or rolled back
         3341  +** using the sqlite3BtreeSavepoint() function.
  2685   3342   */
  2686         -int sqlite3BtreeBeginStmt(Btree *p){
         3343  +int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
  2687   3344     int rc;
  2688   3345     BtShared *pBt = p->pBt;
  2689   3346     sqlite3BtreeEnter(p);
  2690         -  pBt->db = p->db;
  2691         -  if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
  2692         -    rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  2693         -  }else{
  2694         -    assert( pBt->inTransaction==TRANS_WRITE );
  2695         -    rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
  2696         -    pBt->inStmt = 1;
  2697         -  }
  2698         -  sqlite3BtreeLeave(p);
  2699         -  return rc;
  2700         -}
  2701         -
  2702         -
  2703         -/*
  2704         -** Commit the statment subtransaction currently in progress.  If no
  2705         -** subtransaction is active, this is a no-op.
  2706         -*/
  2707         -int sqlite3BtreeCommitStmt(Btree *p){
  2708         -  int rc;
  2709         -  BtShared *pBt = p->pBt;
  2710         -  sqlite3BtreeEnter(p);
  2711         -  pBt->db = p->db;
  2712         -  if( pBt->inStmt && !pBt->readOnly ){
  2713         -    rc = sqlite3PagerStmtCommit(pBt->pPager);
  2714         -  }else{
  2715         -    rc = SQLITE_OK;
  2716         -  }
  2717         -  pBt->inStmt = 0;
         3347  +  assert( p->inTrans==TRANS_WRITE );
         3348  +  assert( pBt->readOnly==0 );
         3349  +  assert( iStatement>0 );
         3350  +  assert( iStatement>p->db->nSavepoint );
         3351  +  assert( pBt->inTransaction==TRANS_WRITE );
         3352  +  /* At the pager level, a statement transaction is a savepoint with
         3353  +  ** an index greater than all savepoints created explicitly using
         3354  +  ** SQL statements. It is illegal to open, release or rollback any
         3355  +  ** such savepoints while the statement transaction savepoint is active.
         3356  +  */
         3357  +  rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
  2718   3358     sqlite3BtreeLeave(p);
  2719   3359     return rc;
  2720   3360   }
  2721   3361   
  2722   3362   /*
  2723         -** Rollback the active statement subtransaction.  If no subtransaction
  2724         -** is active this routine is a no-op.
         3363  +** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
         3364  +** or SAVEPOINT_RELEASE. This function either releases or rolls back the
         3365  +** savepoint identified by parameter iSavepoint, depending on the value 
         3366  +** of op.
  2725   3367   **
  2726         -** All cursors will be invalidated by this operation.  Any attempt
  2727         -** to use a cursor that was open at the beginning of this operation
  2728         -** will result in an error.
         3368  +** Normally, iSavepoint is greater than or equal to zero. However, if op is
         3369  +** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 
         3370  +** contents of the entire transaction are rolled back. This is different
         3371  +** from a normal transaction rollback, as no locks are released and the
         3372  +** transaction remains open.
  2729   3373   */
  2730         -int sqlite3BtreeRollbackStmt(Btree *p){
         3374  +int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
  2731   3375     int rc = SQLITE_OK;
  2732         -  BtShared *pBt = p->pBt;
  2733         -  sqlite3BtreeEnter(p);
  2734         -  pBt->db = p->db;
  2735         -  if( pBt->inStmt && !pBt->readOnly ){
  2736         -    rc = sqlite3PagerStmtRollback(pBt->pPager);
  2737         -    pBt->inStmt = 0;
         3376  +  if( p && p->inTrans==TRANS_WRITE ){
         3377  +    BtShared *pBt = p->pBt;
         3378  +    assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
         3379  +    assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
         3380  +    sqlite3BtreeEnter(p);
         3381  +    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
         3382  +    if( rc==SQLITE_OK ){
         3383  +      if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0;
         3384  +      rc = newDatabase(pBt);
         3385  +      pBt->nPage = get4byte(28 + pBt->pPage1->aData);
         3386  +
         3387  +      /* The database size was written into the offset 28 of the header
         3388  +      ** when the transaction started, so we know that the value at offset
         3389  +      ** 28 is nonzero. */
         3390  +      assert( pBt->nPage>0 );
         3391  +    }
         3392  +    sqlite3BtreeLeave(p);
  2738   3393     }
  2739         -  sqlite3BtreeLeave(p);
  2740   3394     return rc;
  2741   3395   }
  2742   3396   
  2743   3397   /*
  2744   3398   ** Create a new cursor for the BTree whose root is on the page
  2745         -** iTable.  The act of acquiring a cursor gets a read lock on 
  2746         -** the database file.
         3399  +** iTable. If a read-only cursor is requested, it is assumed that
         3400  +** the caller already has at least a read-only transaction open
         3401  +** on the database already. If a write-cursor is requested, then
         3402  +** the caller is assumed to have an open write transaction.
  2747   3403   **
  2748   3404   ** If wrFlag==0, then the cursor can only be used for reading.
  2749   3405   ** If wrFlag==1, then the cursor can be used for reading or for
  2750   3406   ** writing if other conditions for writing are also met.  These
  2751   3407   ** are the conditions that must be met in order for writing to
  2752   3408   ** be allowed:
  2753   3409   **
................................................................................
  2762   3418   ** 3:  The database must be writable (not on read-only media)
  2763   3419   **
  2764   3420   ** 4:  There must be an active transaction.
  2765   3421   **
  2766   3422   ** No checking is done to make sure that page iTable really is the
  2767   3423   ** root page of a b-tree.  If it is not, then the cursor acquired
  2768   3424   ** will not work correctly.
         3425  +**
         3426  +** It is assumed that the sqlite3BtreeCursorZero() has been called
         3427  +** on pCur to initialize the memory space prior to invoking this routine.
  2769   3428   */
  2770   3429   static int btreeCursor(
  2771   3430     Btree *p,                              /* The btree */
  2772   3431     int iTable,                            /* Root page of table to open */
  2773   3432     int wrFlag,                            /* 1 to write. 0 read-only */
  2774   3433     struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
  2775   3434     BtCursor *pCur                         /* Space for new cursor */
  2776   3435   ){
  2777         -  int rc;
  2778         -  BtShared *pBt = p->pBt;
         3436  +  BtShared *pBt = p->pBt;                /* Shared b-tree handle */
  2779   3437   
  2780   3438     assert( sqlite3BtreeHoldsMutex(p) );
  2781         -  if( wrFlag ){
  2782         -    if( pBt->readOnly ){
  2783         -      return SQLITE_READONLY;
  2784         -    }
  2785         -    if( checkReadLocks(p, iTable, 0, 0) ){
  2786         -      return SQLITE_LOCKED;
  2787         -    }
  2788         -  }
  2789         -
  2790         -  if( pBt->pPage1==0 ){
  2791         -    rc = lockBtreeWithRetry(p);
  2792         -    if( rc!=SQLITE_OK ){
  2793         -      return rc;
  2794         -    }
  2795         -    if( pBt->readOnly && wrFlag ){
  2796         -      return SQLITE_READONLY;
  2797         -    }
  2798         -  }