System.Data.SQLite
Check-in [496499d755]
Not logged in

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

Overview
Comment:More fixes to the optional field handling for the virtual table interface.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | vtabFixes
Files: files | file ages | folders
SHA1: 496499d755a259c04c579f68927f43223e27cbe2
User & Date: mistachkin 2015-12-04 06:14:25
Context
2015-12-04
06:16
Reverse accidental check-in of changes performed on the project file by the IDE. check-in: fe4d272115 user: mistachkin tags: vtabFixes
06:14
More fixes to the optional field handling for the virtual table interface. check-in: 496499d755 user: mistachkin tags: vtabFixes
02:04
Add missing checks for NET_452 and NET_461 compile-time defines. check-in: d5792024ef user: mistachkin tags: trunk
Changes
Hide Diffs Unified Diffs Show Whitespace Changes Patch

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

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    <Compile Include="Design\Index.cs" />
    <Compile Include="Design\PrimaryKey.cs" />
    <Compile Include="Design\SimpleTokenizer.cs" />
    <Compile Include="Design\Table.cs" />
    <Compile Include="Design\Trigger.cs" />
    <Compile Include="Design\Unique.cs" />
    <Compile Include="Design\View.cs" />
    <Compile Include="Editors\AutoCompleteColumn.cs">
      <SubType>Component</SubType>
    </Compile>
    <Compile Include="Editors\TableDesignerDoc.cs">
      <SubType>UserControl</SubType>
    </Compile>
    <Compile Include="Editors\TableDesignerDoc.Designer.cs">
      <DependentUpon>TableDesignerDoc.cs</DependentUpon>
    </Compile>
    <Compile Include="Editors\ViewDesignerDoc.cs">
................................................................................
  <ItemGroup>
    <None Include="Resources\info.png" />
    <None Include="Resources\ToolboxItems.txt" />
    <None Include="source.extension.vsixmanifest" />
  </ItemGroup>
  <Import Project="$(SQLiteNetDir)\System.Data.SQLite\Targets\System.Data.SQLite.Properties.targets" />
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <PropertyGroup Condition="'$(VSSDK140Install)' == '' Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.Data.ConnectionUI.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Data.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Data.Services.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.OLE.Interop.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Shell.14.0.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.8.0.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.9.0.dll') Or
                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.10.0.dll')">
    <!--
        NOTE: We cannot build this project without the necessary reference
              assemblies; therefore, skip building it altogether.
    -->
    <BuildDependsOn>
      MissingVsSdk
    </BuildDependsOn>







|
<
<







 







<
|
<
<
<
<
<
<
<
<







110
111
112
113
114
115
116
117


118
119
120
121
122
123
124
...
202
203
204
205
206
207
208

209








210
211
212
213
214
215
216
    <Compile Include="Design\Index.cs" />
    <Compile Include="Design\PrimaryKey.cs" />
    <Compile Include="Design\SimpleTokenizer.cs" />
    <Compile Include="Design\Table.cs" />
    <Compile Include="Design\Trigger.cs" />
    <Compile Include="Design\Unique.cs" />
    <Compile Include="Design\View.cs" />
    <Compile Include="Editors\AutoCompleteColumn.cs" />


    <Compile Include="Editors\TableDesignerDoc.cs">
      <SubType>UserControl</SubType>
    </Compile>
    <Compile Include="Editors\TableDesignerDoc.Designer.cs">
      <DependentUpon>TableDesignerDoc.cs</DependentUpon>
    </Compile>
    <Compile Include="Editors\ViewDesignerDoc.cs">
................................................................................
  <ItemGroup>
    <None Include="Resources\info.png" />
    <None Include="Resources\ToolboxItems.txt" />
    <None Include="source.extension.vsixmanifest" />
  </ItemGroup>
  <Import Project="$(SQLiteNetDir)\System.Data.SQLite\Targets\System.Data.SQLite.Properties.targets" />
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

  <PropertyGroup Condition="'$(VSSDK140Install)' == '' Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.Data.ConnectionUI.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Data.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Data.Services.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.OLE.Interop.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Shell.14.0.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.8.0.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.9.0.dll') Or&#xD;&#xA;                            !Exists('$(VSSDK140Install)VisualStudioIntegration\Common\Assemblies\v2.0\Microsoft.VisualStudio.Shell.Interop.10.0.dll')">








    <!--
        NOTE: We cannot build this project without the necessary reference
              assemblies; therefore, skip building it altogether.
    -->
    <BuildDependsOn>
      MissingVsSdk
    </BuildDependsOn>

Changes to System.Data.SQLite/SQLiteModule.cs.

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
....
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
....
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190

1191
1192
1193
1194
1195
1196
1197
1198
1199
1200





1201
1202
1203

1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
....
1242
1243
1244
1245
1246
1247
1248
1249

















































































































































































































































1250
1251
1252
1253
1254
1255
1256




1257
1258
1259
1260
1261
1262

1263
1264
1265
1266
1267
1268
1269
1270
1271

1272
1273
1274
1275
1276

1277
1278
1279
1280
1281

1282
1283
1284
1285
1286

1287











1288

1289













1290
1291
1292
1293

1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323


























































































1324
1325
1326
1327
1328
1329
1330
....
1335
1336
1337
1338
1339
1340
1341




1342
1343
1344

1345
1346
1347








1348












1349


1350
1351






1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370





















1371
1372
1373






1374
1375
1376




















1377
1378

1379
1380

1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414


1415



1416

1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
....
3277
3278
3279
3280
3281
3282
3283





























3284
3285
3286
3287
3288
3289
3290
....
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
....
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
....
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
....
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
....
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Determines if the native flags field can be used, based on the
        /// available version of the SQLite core library.
        /// </summary>
        /// <returns>
        /// Non-zero if the <see cref="IdxFlags" /> property is supported by
        /// the SQLite core library.
        /// </returns>
        public bool CanUseIdxFlags()
        {
            if (UnsafeNativeMethods.sqlite3_libversion_number() >= 3009000)
                return true;

            return false;
        }

................................................................................
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Determines if the native flags field can be used, based on the
        /// available version of the SQLite core library.
        /// </summary>
        /// <returns>
        /// Non-zero if the <see cref="ColUsed" /> property is supported by
        /// the SQLite core library.
        /// </returns>
        public bool CanUseColUsed()
        {
            if (UnsafeNativeMethods.sqlite3_libversion_number() >= 3010000)
                return true;

            return false;
        }

................................................................................
        {
            get { return estimatedRows; }
            set { estimatedRows = value; }
        }

        ///////////////////////////////////////////////////////////////////////

        private SQLiteIndexFlags? idxFlags;
        /// <summary>
        /// The flags that should be used with this index.  Using a null value
        /// here indicates that a default flags value should be used.  This
        /// property has no effect if the SQLite core library is not at least
        /// version 3.9.0.
        /// </summary>
        public SQLiteIndexFlags? IdxFlags
        {
            get { return idxFlags; }
            set { idxFlags = value; }
        }

        ///////////////////////////////////////////////////////////////////////

        private long? colUsed;
        /// <summary>

        /// The colUsed field indicates which columns of the virtual table
        /// may be required by the current scan.  Virtual table columns are
        /// numbered from zero in the order in which they appear within the
        /// CREATE TABLE statement passed to sqlite3_declare_vtab().  For the
        /// first 63 columns (columns 0-62), the corresponding bit is set
        /// within the colUsed mask if the column may be required by SQLite.
        /// If the table has at least 64 columns and any column to the right
        /// of the first 63 is required, then bit 63 of colUsed is also set.
        /// In other words, column iCol may be required if the expression
        /// (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol)))





        /// evaluates to non-zero.  Using a null value here indicates that a
        /// default flags value should be used.  This property has no effect
        /// if the SQLite core library is not at least version 3.10.0.

        /// </summary>
        public long? ColUsed
        {
            get { return colUsed; }
            set { colUsed = value; }
        }
        #endregion
    }
    #endregion

    ///////////////////////////////////////////////////////////////////////////

................................................................................
            outputs = new SQLiteIndexOutputs(nConstraint);
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Internal Marshal Helper Methods
        /// <summary>

















































































































































































































































        /// Converts a native pointer to a native sqlite3_index_info structure
        /// into a new <see cref="SQLiteIndex" /> object instance.
        /// </summary>
        /// <param name="pIndex">
        /// The native pointer to the native sqlite3_index_info structure to
        /// convert.
        /// </param>




        /// <param name="index">
        /// Upon success, this parameter will be modified to contain the newly
        /// created <see cref="SQLiteIndex" /> object instance.
        /// </param>
        internal static void FromIntPtr(
            IntPtr pIndex,

            ref SQLiteIndex index
            )
        {
            if (pIndex == IntPtr.Zero)
                return;

            int offset = 0;

            int nConstraint = SQLiteMarshal.ReadInt32(pIndex, offset);


            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                IntPtr.Size);

            IntPtr pConstraint = SQLiteMarshal.ReadIntPtr(pIndex, offset);


            offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
                sizeof(int));

            int nOrderBy = SQLiteMarshal.ReadInt32(pIndex, offset);


            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                IntPtr.Size);

            IntPtr pOrderBy = SQLiteMarshal.ReadIntPtr(pIndex, offset);













            index = new SQLiteIndex(nConstraint, nOrderBy);















            Type indexConstraintType = typeof(
                UnsafeNativeMethods.sqlite3_index_constraint);

            int sizeOfConstraintType = Marshal.SizeOf(indexConstraintType);


            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
            {
                IntPtr pOffset = SQLiteMarshal.IntPtrForOffset(
                    pConstraint, iConstraint * sizeOfConstraintType);

                UnsafeNativeMethods.sqlite3_index_constraint constraint =
                    (UnsafeNativeMethods.sqlite3_index_constraint)
                        Marshal.PtrToStructure(pOffset, indexConstraintType);

                index.Inputs.Constraints[iConstraint] =
                    new SQLiteIndexConstraint(constraint);
            }

            Type indexOrderByType = typeof(
                UnsafeNativeMethods.sqlite3_index_orderby);

            int sizeOfOrderByType = Marshal.SizeOf(indexOrderByType);

            for (int iOrderBy = 0; iOrderBy < nOrderBy; iOrderBy++)
            {
                IntPtr pOffset = SQLiteMarshal.IntPtrForOffset(
                    pOrderBy, iOrderBy * sizeOfOrderByType);

                UnsafeNativeMethods.sqlite3_index_orderby orderBy =
                    (UnsafeNativeMethods.sqlite3_index_orderby)
                        Marshal.PtrToStructure(pOffset, indexOrderByType);

                index.Inputs.OrderBys[iOrderBy] =
                    new SQLiteIndexOrderBy(orderBy);


























































































            }
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Populates the outputs of a pre-allocated native sqlite3_index_info
................................................................................
        /// The existing <see cref="SQLiteIndex" /> object instance containing
        /// the output data to use.
        /// </param>
        /// <param name="pIndex">
        /// The native pointer to the pre-allocated native sqlite3_index_info
        /// structure.
        /// </param>




        internal static void ToIntPtr(
            SQLiteIndex index,
            IntPtr pIndex

            )
        {
            if ((index == null) || (index.Inputs == null) ||








                (index.Inputs.Constraints == null) ||












                (index.Outputs == null) ||


                (index.Outputs.ConstraintUsages == null))
            {






                return;
            }

            if (pIndex == IntPtr.Zero)
                return;

            int offset = 0;

            int nConstraint = SQLiteMarshal.ReadInt32(pIndex, offset);

            if (nConstraint != index.Inputs.Constraints.Length)
                return;

            if (nConstraint != index.Outputs.ConstraintUsages.Length)
                return;

            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                IntPtr.Size);






















            offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
                sizeof(int));







            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                IntPtr.Size);





















            offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
                sizeof(int));


            IntPtr pConstraintUsage = SQLiteMarshal.ReadIntPtr(pIndex, offset);


            int sizeOfConstraintUsageType = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_index_constraint_usage));

            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
            {
                UnsafeNativeMethods.sqlite3_index_constraint_usage constraintUsage =
                    new UnsafeNativeMethods.sqlite3_index_constraint_usage(
                        index.Outputs.ConstraintUsages[iConstraint]);

                Marshal.StructureToPtr(
                    constraintUsage, SQLiteMarshal.IntPtrForOffset(
                    pConstraintUsage, iConstraint * sizeOfConstraintUsageType),
                    false);
            }

            offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
                sizeof(int));

            SQLiteMarshal.WriteInt32(pIndex, offset,
                index.Outputs.IndexNumber);

            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                IntPtr.Size);

            SQLiteMarshal.WriteIntPtr(pIndex, offset,
                SQLiteString.Utf8IntPtrFromString(index.Outputs.IndexString));

            offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
                sizeof(int));

            //
            // NOTE: We just allocated the IndexString field; therefore, we
            //       need to set the NeedToFreeIndexString field to non-zero.


            //



            SQLiteMarshal.WriteInt32(pIndex, offset, 1);


            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                sizeof(int));

            SQLiteMarshal.WriteInt32(pIndex, offset,
                index.Outputs.OrderByConsumed);

            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                sizeof(double));

            if (index.Outputs.EstimatedCost.HasValue)
            {
                SQLiteMarshal.WriteDouble(pIndex, offset,
                    index.Outputs.EstimatedCost.GetValueOrDefault());
            }

            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(double),
                sizeof(long));

            if (index.Outputs.CanUseEstimatedRows() &&
                index.Outputs.EstimatedRows.HasValue)
            {
                SQLiteMarshal.WriteInt64(pIndex, offset,
                    index.Outputs.EstimatedRows.GetValueOrDefault());
            }

            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(long),
                sizeof(int));

            if (index.Outputs.CanUseIdxFlags() &&
                index.Outputs.IdxFlags.HasValue)
            {
                SQLiteMarshal.WriteInt32(pIndex, offset,
                   (int)index.Outputs.IdxFlags.GetValueOrDefault());
            }

            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                sizeof(long));

            if (index.Outputs.CanUseColUsed() &&
                index.Outputs.ColUsed.HasValue)
            {
                SQLiteMarshal.WriteInt64(pIndex, offset,
                    index.Outputs.ColUsed.GetValueOrDefault());
            }
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Public Properties
................................................................................
        {
#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt32(pointer, offset);
#else
            return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
#endif
        }






























        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Reads a <see cref="Double" /> value from the specified memory
        /// location.
        /// </summary>
................................................................................
            try
            {
                if (pVtab == IntPtr.Zero)
                    return false;

                int offset = 0;

                offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
                    sizeof(int));

                offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                    IntPtr.Size);

                IntPtr pOldError = SQLiteMarshal.ReadIntPtr(pVtab, offset);

                if (pOldError != IntPtr.Zero)
                {
                    SQLiteMemory.Free(pOldError); pOldError = IntPtr.Zero;
                    SQLiteMarshal.WriteIntPtr(pVtab, offset, pOldError);
................................................................................
            if (pVtab == IntPtr.Zero)
                return;

            int offset = 0;

            SQLiteMarshal.WriteIntPtr(pVtab, offset, IntPtr.Zero);

            offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
                sizeof(int));

            SQLiteMarshal.WriteInt32(pVtab, offset, 0);

            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
                IntPtr.Size);

            SQLiteMarshal.WriteIntPtr(pVtab, offset, IntPtr.Zero);
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
................................................................................
        /// <summary>
        /// Modifies the specified <see cref="SQLiteIndex" /> object instance
        /// to contain the specified flags.
        /// </summary>
        /// <param name="index">
        /// The <see cref="SQLiteIndex" /> object instance to modify.
        /// </param>
        /// <param name="idxFlags">
        /// The index flags value to use.  Using a null value means that the
        /// default value provided by the SQLite core library should be used.
        /// </param>
        /// <returns>
        /// Non-zero upon success.
        /// </returns>
        protected virtual bool SetIdxFlags(
            SQLiteIndex index,
            SQLiteIndexFlags? idxFlags
            )
        {
            if ((index == null) || (index.Outputs == null))
                return false;

            index.Outputs.IdxFlags = idxFlags;
            return true;
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Modifies the specified <see cref="SQLiteIndex" /> object instance
................................................................................
        /// </summary>
        /// <param name="index">
        /// The <see cref="SQLiteIndex" /> object instance to modify.
        /// </param>
        /// <returns>
        /// Non-zero upon success.
        /// </returns>
        protected virtual bool SetIdxFlags(
            SQLiteIndex index
            )
        {
            return SetIdxFlags(index, null);
        }
        #endregion
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Public Properties
................................................................................
            {
                SQLiteVirtualTable table = TableFromIntPtr(pVtab);

                if (table != null)
                {
                    SQLiteIndex index = null;

                    SQLiteIndex.FromIntPtr(pIndex, ref index);

                    if (BestIndex(table, index) == SQLiteErrorCode.Ok)
                    {
                        SQLiteIndex.ToIntPtr(index, pIndex);
                        return SQLiteErrorCode.Ok;
                    }
                }
            }
            catch (Exception e) /* NOTE: Must catch ALL. */
            {
                SetTableError(pVtab, e.ToString());







|


|







 







|


|







 







|






|

|
|




|

>
|
|
|
|
|
<
|
|
|
|
>
>
>
>
>

|
|
>

|

|
|







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
>
>
>






>








|
>

|
|

|
>

|
|

|
>

|
|

|
>

>
>
>
>
>
>
>
>
>
>
>

>

>
>
>
>
>
>
>
>
>
>
>
>
>
|


|
>








|

|
|


|


|








|

<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>


|
>


|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
|
>
>
>
>
>
>










|


|


|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|

>
>
>
>
>
>
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>

|
>








|







|
|


|

|
|


|

|
|



|
>
>

>
>
>
|
>

|
|


|

|
|

|


|


|
|

|
|


|


|
|

|
|


|


|
|

|
|


|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|

|
|







 







|
|



|
|







 







|






|

|





|







 







|



|







 







|



|







1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
....
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
....
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196

1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
....
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603

1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
....
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828

1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
....
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
....
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
....
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
....
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
....
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
....
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Determines if the native flags field can be used, based on the
        /// available version of the SQLite core library.
        /// </summary>
        /// <returns>
        /// Non-zero if the <see cref="IndexFlags" /> property is supported by
        /// the SQLite core library.
        /// </returns>
        public bool CanUseIndexFlags()
        {
            if (UnsafeNativeMethods.sqlite3_libversion_number() >= 3009000)
                return true;

            return false;
        }

................................................................................
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Determines if the native flags field can be used, based on the
        /// available version of the SQLite core library.
        /// </summary>
        /// <returns>
        /// Non-zero if the <see cref="ColumnsUsed" /> property is supported by
        /// the SQLite core library.
        /// </returns>
        public bool CanUseColumnsUsed()
        {
            if (UnsafeNativeMethods.sqlite3_libversion_number() >= 3010000)
                return true;

            return false;
        }

................................................................................
        {
            get { return estimatedRows; }
            set { estimatedRows = value; }
        }

        ///////////////////////////////////////////////////////////////////////

        private SQLiteIndexFlags? indexFlags;
        /// <summary>
        /// The flags that should be used with this index.  Using a null value
        /// here indicates that a default flags value should be used.  This
        /// property has no effect if the SQLite core library is not at least
        /// version 3.9.0.
        /// </summary>
        public SQLiteIndexFlags? IndexFlags
        {
            get { return indexFlags; }
            set { indexFlags = value; }
        }

        ///////////////////////////////////////////////////////////////////////

        private long? columnsUsed;
        /// <summary>
        /// <para>
        /// Indicates which columns of the virtual table may be required by the
        /// current scan.  Virtual table columns are numbered from zero in the
        /// order in which they appear within the CREATE TABLE statement passed
        /// to sqlite3_declare_vtab().  For the first 63 columns (columns 0-62),
        /// the corresponding bit is set within the bit mask if the column may

        /// be required by SQLite.  If the table has at least 64 columns and
        /// any column to the right of the first 63 is required, then bit 63 of
        /// colUsed is also set.  In other words, column iCol may be required
        /// if the expression
        /// </para>
        /// <para><code>
        /// (colUsed &amp; ((sqlite3_uint64)1 &lt;&lt; (iCol&gt;=63 ? 63 : iCol)))
        /// </code></para>
        /// <para>
        /// evaluates to non-zero.  Using a null value here indicates that a
        /// default flags value should be used.  This property has no effect if
        /// the SQLite core library is not at least version 3.10.0.
        /// </para>
        /// </summary>
        public long? ColumnsUsed
        {
            get { return columnsUsed; }
            set { columnsUsed = value; }
        }
        #endregion
    }
    #endregion

    ///////////////////////////////////////////////////////////////////////////

................................................................................
            outputs = new SQLiteIndexOutputs(nConstraint);
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Internal Marshal Helper Methods
        /// <summary>
        /// Attempts to determine the structure sizes needed to create and
        /// populate a native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_info" />
        /// structure.
        /// </summary>
        /// <param name="sizeOfInfoType">
        /// The size of the native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_info" />
        /// structure is stored here.
        /// </param>
        /// <param name="sizeOfConstraintType">
        /// The size of the native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_constraint" />
        /// structure is stored here.
        /// </param>
        /// <param name="sizeOfOrderByType">
        /// The size of the native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_orderby" />
        /// structure is stored here.
        /// </param>
        /// <param name="sizeOfConstraintUsageType">
        /// The size of the native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_constraint_usage" />
        /// structure is stored here.
        /// </param>
        private static void SizeOf( /* NOTE: For test use only. */
            out int sizeOfInfoType,
            out int sizeOfConstraintType,
            out int sizeOfOrderByType,
            out int sizeOfConstraintUsageType
            )
        {
            sizeOfInfoType = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_index_info));

            sizeOfConstraintType = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_index_constraint));

            sizeOfOrderByType = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_index_orderby));

            sizeOfConstraintUsageType = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_index_constraint_usage));
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Attempts to allocate and initialize a native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_info" />
        /// structure.
        /// </summary>
        /// <param name="nConstraint">
        /// The number of <see cref="SQLiteIndexConstraint" /> instances to
        /// pre-allocate space for.
        /// </param>
        /// <param name="nOrderBy">
        /// The number of <see cref="SQLiteIndexOrderBy" /> instances to
        /// pre-allocate space for.
        /// </param>
        /// <returns>
        /// The newly allocated native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_info" /> structure
        /// -OR- <see cref="IntPtr.Zero" /> if it could not be fully allocated.
        /// </returns>
        internal static IntPtr AllocMemoryBlock( /* NOTE: For test use only. */
            int nConstraint,
            int nOrderBy
            )
        {
            IntPtr pIndex = IntPtr.Zero;
            IntPtr pInfo = IntPtr.Zero;
            IntPtr pConstraint = IntPtr.Zero;
            IntPtr pOrderBy = IntPtr.Zero;
            IntPtr pConstraintUsage = IntPtr.Zero;

            try
            {
                int sizeOfInfoType;
                int sizeOfOrderByType;
                int sizeOfConstraintType;
                int sizeOfConstraintUsageType;

                SizeOf(out sizeOfInfoType, out sizeOfConstraintType,
                    out sizeOfOrderByType, out sizeOfConstraintUsageType);

                if ((sizeOfInfoType > 0) &&
                    (sizeOfConstraintType > 0) &&
                    (sizeOfOrderByType > 0) &&
                    (sizeOfConstraintUsageType > 0))
                {
                    pInfo = SQLiteMemory.Allocate(sizeOfInfoType);

                    pConstraint = SQLiteMemory.Allocate(
                        sizeOfConstraintType * nConstraint);

                    pOrderBy = SQLiteMemory.Allocate(
                        sizeOfOrderByType * nOrderBy);

                    pConstraintUsage = SQLiteMemory.Allocate(
                        sizeOfConstraintUsageType * nConstraint);

                    if ((pInfo != IntPtr.Zero) &&
                        (pConstraint != IntPtr.Zero) &&
                        (pOrderBy != IntPtr.Zero) &&
                        (pConstraintUsage != IntPtr.Zero))
                    {
                        int offset = 0;

                        Marshal.WriteInt32(
                            pInfo, offset, nConstraint);

                        offset = SQLiteMarshal.NextOffsetOf(
                            offset, sizeof(int), IntPtr.Size);

                        Marshal.WriteIntPtr(
                            pInfo, offset, pConstraint);

                        offset = SQLiteMarshal.NextOffsetOf(
                            offset, IntPtr.Size, sizeof(int));

                        Marshal.WriteInt32(
                            pInfo, offset, nOrderBy);

                        offset = SQLiteMarshal.NextOffsetOf(
                            offset, sizeof(int), IntPtr.Size);

                        Marshal.WriteIntPtr(
                            pInfo, offset, pOrderBy);

                        offset = SQLiteMarshal.NextOffsetOf(
                            offset, IntPtr.Size, IntPtr.Size);

                        Marshal.WriteIntPtr(
                            pInfo, offset, pConstraintUsage);

                        pIndex = pInfo; /* NOTE: Success. */
                    }
                }
            }
            finally
            {
                if (pIndex == IntPtr.Zero) /* NOTE: Failure? */
                {
                    if (pConstraintUsage != IntPtr.Zero)
                    {
                        SQLiteMemory.Free(pConstraintUsage);
                        pConstraintUsage = IntPtr.Zero;
                    }

                    if (pOrderBy != IntPtr.Zero)
                    {
                        SQLiteMemory.Free(pOrderBy);
                        pOrderBy = IntPtr.Zero;
                    }

                    if (pConstraint != IntPtr.Zero)
                    {
                        SQLiteMemory.Free(pConstraint);
                        pConstraint = IntPtr.Zero;
                    }

                    if (pInfo != IntPtr.Zero)
                    {
                        SQLiteMemory.Free(pInfo);
                        pInfo = IntPtr.Zero;
                    }
                }
            }

            return pIndex;
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Frees all the memory associated with a native
        /// <see cref="UnsafeNativeMethods.sqlite3_index_info" />
        /// structure.
        /// </summary>
        /// <param name="pIndex">
        /// The native pointer to the native sqlite3_index_info structure to
        /// free.
        /// </param>
        internal static void FreeMemoryBlock( /* NOTE: For test use only. */
            IntPtr pIndex
            )
        {
            if (pIndex == IntPtr.Zero)
                return;

            int offset = 0;

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            IntPtr pConstraint = SQLiteMarshal.ReadIntPtr(
                pIndex, offset);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            IntPtr pOrderBy = SQLiteMarshal.ReadIntPtr(pIndex, offset);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, IntPtr.Size);

            IntPtr pConstraintUsage = SQLiteMarshal.ReadIntPtr(
                pIndex, offset);

            if (pConstraintUsage != IntPtr.Zero)
            {
                SQLiteMemory.Free(pConstraintUsage);
                pConstraintUsage = IntPtr.Zero;
            }

            if (pOrderBy != IntPtr.Zero)
            {
                SQLiteMemory.Free(pOrderBy);
                pOrderBy = IntPtr.Zero;
            }

            if (pConstraint != IntPtr.Zero)
            {
                SQLiteMemory.Free(pConstraint);
                pConstraint = IntPtr.Zero;
            }

            if (pIndex != IntPtr.Zero)
            {
                SQLiteMemory.Free(pIndex);
                pIndex = IntPtr.Zero;
            }
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Converts a native pointer to a native sqlite3_index_info structure
        /// into a new <see cref="SQLiteIndex" /> object instance.
        /// </summary>
        /// <param name="pIndex">
        /// The native pointer to the native sqlite3_index_info structure to
        /// convert.
        /// </param>
        /// <param name="includeOutput">
        /// Non-zero to include fields from the outputs portion of the native
        /// structure; otherwise, the "output" fields will not be read.
        /// </param>
        /// <param name="index">
        /// Upon success, this parameter will be modified to contain the newly
        /// created <see cref="SQLiteIndex" /> object instance.
        /// </param>
        internal static void FromIntPtr(
            IntPtr pIndex,
            bool includeOutput,
            ref SQLiteIndex index
            )
        {
            if (pIndex == IntPtr.Zero)
                return;

            int offset = 0;

            int nConstraint = SQLiteMarshal.ReadInt32(
                pIndex, offset);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            IntPtr pConstraint = SQLiteMarshal.ReadIntPtr(
                pIndex, offset);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            int nOrderBy = SQLiteMarshal.ReadInt32(
                pIndex, offset);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            IntPtr pOrderBy = SQLiteMarshal.ReadIntPtr(
                pIndex, offset);

            IntPtr pConstraintUsage = IntPtr.Zero;

            if (includeOutput)
            {
                offset = SQLiteMarshal.NextOffsetOf(
                    offset, IntPtr.Size, IntPtr.Size);

                pConstraintUsage = SQLiteMarshal.ReadIntPtr(
                    pIndex, offset);
            }

            index = new SQLiteIndex(nConstraint, nOrderBy);
            SQLiteIndexInputs inputs = index.Inputs;

            if (inputs == null)
                return;

            SQLiteIndexConstraint[] constraints = inputs.Constraints;

            if (constraints == null)
                return;

            SQLiteIndexOrderBy[] orderBys = inputs.OrderBys;

            if (orderBys == null)
                return;

            Type constraintType = typeof(
                UnsafeNativeMethods.sqlite3_index_constraint);

            int sizeOfConstraintType = Marshal.SizeOf(
                constraintType);

            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
            {
                IntPtr pOffset = SQLiteMarshal.IntPtrForOffset(
                    pConstraint, iConstraint * sizeOfConstraintType);

                UnsafeNativeMethods.sqlite3_index_constraint constraint =
                    (UnsafeNativeMethods.sqlite3_index_constraint)
                        Marshal.PtrToStructure(pOffset, constraintType);

                constraints[iConstraint] = new SQLiteIndexConstraint(
                    constraint);
            }

            Type orderByType = typeof(
                UnsafeNativeMethods.sqlite3_index_orderby);

            int sizeOfOrderByType = Marshal.SizeOf(orderByType);

            for (int iOrderBy = 0; iOrderBy < nOrderBy; iOrderBy++)
            {
                IntPtr pOffset = SQLiteMarshal.IntPtrForOffset(
                    pOrderBy, iOrderBy * sizeOfOrderByType);

                UnsafeNativeMethods.sqlite3_index_orderby orderBy =
                    (UnsafeNativeMethods.sqlite3_index_orderby)
                        Marshal.PtrToStructure(pOffset, orderByType);


                orderBys[iOrderBy] = new SQLiteIndexOrderBy(orderBy);
            }

            if (includeOutput)
            {
                SQLiteIndexOutputs outputs = index.Outputs;

                if (outputs == null)
                    return;

                SQLiteIndexConstraintUsage[] constraintUsages =
                    outputs.ConstraintUsages;

                if (constraintUsages == null)
                    return;

                Type constraintUsageType = typeof(
                    UnsafeNativeMethods.sqlite3_index_constraint_usage);

                int sizeOfConstraintUsageType = Marshal.SizeOf(
                    constraintUsageType);

                for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
                {
                    IntPtr pOffset = SQLiteMarshal.IntPtrForOffset(
                        pConstraintUsage, iConstraint * sizeOfConstraintUsageType);

                    UnsafeNativeMethods.sqlite3_index_constraint_usage constraintUsage =
                        (UnsafeNativeMethods.sqlite3_index_constraint_usage)
                            Marshal.PtrToStructure(pOffset, constraintUsageType);

                    constraintUsages[iConstraint] = new SQLiteIndexConstraintUsage(
                        constraintUsage);
                }

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, IntPtr.Size, sizeof(int));

                outputs.IndexNumber = SQLiteMarshal.ReadInt32(
                    pIndex, offset);

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, sizeof(int), IntPtr.Size);

                outputs.IndexString = SQLiteString.StringFromUtf8IntPtr(
                    SQLiteMarshal.ReadIntPtr(pIndex, offset));

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, IntPtr.Size, sizeof(int));

                outputs.NeedToFreeIndexString = SQLiteMarshal.ReadInt32(
                    pIndex, offset);

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, sizeof(int), sizeof(int));

                outputs.OrderByConsumed = SQLiteMarshal.ReadInt32(
                    pIndex, offset);

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, sizeof(int), sizeof(double));

                outputs.EstimatedCost = SQLiteMarshal.ReadDouble(
                    pIndex, offset);

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, sizeof(double), sizeof(long));

                if (outputs.CanUseEstimatedRows())
                {
                    outputs.EstimatedRows = SQLiteMarshal.ReadInt64(
                        pIndex, offset);
                }

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, sizeof(long), sizeof(int));

                if (outputs.CanUseIndexFlags())
                {
                    outputs.IndexFlags = (SQLiteIndexFlags)
                        SQLiteMarshal.ReadInt32(pIndex, offset);
                }

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, sizeof(int), sizeof(long));

                if (outputs.CanUseColumnsUsed())
                {
                    outputs.ColumnsUsed = SQLiteMarshal.ReadInt64(
                        pIndex, offset);
                }
            }
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Populates the outputs of a pre-allocated native sqlite3_index_info
................................................................................
        /// The existing <see cref="SQLiteIndex" /> object instance containing
        /// the output data to use.
        /// </param>
        /// <param name="pIndex">
        /// The native pointer to the pre-allocated native sqlite3_index_info
        /// structure.
        /// </param>
        /// <param name="includeInput">
        /// Non-zero to include fields from the inputs portion of the native
        /// structure; otherwise, the "input" fields will not be written.
        /// </param>
        internal static void ToIntPtr(
            SQLiteIndex index,
            IntPtr pIndex,
            bool includeInput
            )
        {
            if (index == null)
                return;

            SQLiteIndexOutputs outputs = index.Outputs;

            if (outputs == null)
                return;

            SQLiteIndexConstraintUsage[] constraintUsages =
                outputs.ConstraintUsages;

            if (constraintUsages == null)
                return;

            SQLiteIndexInputs inputs = null;
            SQLiteIndexConstraint[] constraints = null;
            SQLiteIndexOrderBy[] orderBys = null;

            if (includeInput)
            {
                inputs = index.Inputs;

                if (inputs == null)
                    return;

                constraints = inputs.Constraints;

                if (constraints == null)
                    return;

                orderBys = inputs.OrderBys;

                if (orderBys == null)
                    return;
            }

            if (pIndex == IntPtr.Zero)
                return;

            int offset = 0;

            int nConstraint = SQLiteMarshal.ReadInt32(pIndex, offset);

            if (includeInput && (nConstraint != constraints.Length))
                return;

            if (nConstraint != constraintUsages.Length)
                return;

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            if (includeInput)
            {
                IntPtr pConstraint = SQLiteMarshal.ReadIntPtr(
                    pIndex, offset);

                int sizeOfConstraintType = Marshal.SizeOf(typeof(
                    UnsafeNativeMethods.sqlite3_index_constraint));

                for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
                {
                    UnsafeNativeMethods.sqlite3_index_constraint constraint =
                        new UnsafeNativeMethods.sqlite3_index_constraint(
                            constraints[iConstraint]);

                    Marshal.StructureToPtr(
                        constraint, SQLiteMarshal.IntPtrForOffset(
                        pConstraint, iConstraint * sizeOfConstraintType),
                        false);
                }
            }

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            int nOrderBy = includeInput ?
                SQLiteMarshal.ReadInt32(pIndex, offset) : 0;

            if (includeInput && (nOrderBy != orderBys.Length))
                return;

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            if (includeInput)
            {
                IntPtr pOrderBy = SQLiteMarshal.ReadIntPtr(pIndex, offset);

                int sizeOfOrderByType = Marshal.SizeOf(typeof(
                    UnsafeNativeMethods.sqlite3_index_orderby));

                for (int iOrderBy = 0; iOrderBy < nOrderBy; iOrderBy++)
                {
                    UnsafeNativeMethods.sqlite3_index_orderby orderBy =
                        new UnsafeNativeMethods.sqlite3_index_orderby(
                            orderBys[iOrderBy]);

                    Marshal.StructureToPtr(
                        orderBy, SQLiteMarshal.IntPtrForOffset(
                        pOrderBy, iOrderBy * sizeOfOrderByType),
                        false);
                }
            }

            offset = SQLiteMarshal.NextOffsetOf(

                offset, IntPtr.Size, IntPtr.Size);

            IntPtr pConstraintUsage = SQLiteMarshal.ReadIntPtr(
                pIndex, offset);

            int sizeOfConstraintUsageType = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_index_constraint_usage));

            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
            {
                UnsafeNativeMethods.sqlite3_index_constraint_usage constraintUsage =
                    new UnsafeNativeMethods.sqlite3_index_constraint_usage(
                        constraintUsages[iConstraint]);

                Marshal.StructureToPtr(
                    constraintUsage, SQLiteMarshal.IntPtrForOffset(
                    pConstraintUsage, iConstraint * sizeOfConstraintUsageType),
                    false);
            }

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            SQLiteMarshal.WriteInt32(pIndex, offset,
                outputs.IndexNumber);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            SQLiteMarshal.WriteIntPtr(pIndex, offset,
                SQLiteString.Utf8IntPtrFromString(outputs.IndexString));

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            //
            // NOTE: We just allocated the IndexString field; therefore, we
            //       need to set make sure the NeedToFreeIndexString field
            //       is non-zero; however, we are not picky about the exact
            //       value.
            //
            int needToFreeIndexString = outputs.NeedToFreeIndexString != 0 ?
                outputs.NeedToFreeIndexString : 1;

            SQLiteMarshal.WriteInt32(pIndex, offset,
                needToFreeIndexString);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), sizeof(int));

            SQLiteMarshal.WriteInt32(pIndex, offset,
                outputs.OrderByConsumed);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), sizeof(double));

            if (outputs.EstimatedCost.HasValue)
            {
                SQLiteMarshal.WriteDouble(pIndex, offset,
                    outputs.EstimatedCost.GetValueOrDefault());
            }

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(double), sizeof(long));

            if (outputs.CanUseEstimatedRows() &&
                outputs.EstimatedRows.HasValue)
            {
                SQLiteMarshal.WriteInt64(pIndex, offset,
                    outputs.EstimatedRows.GetValueOrDefault());
            }

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(long), sizeof(int));

            if (outputs.CanUseIndexFlags() &&
                outputs.IndexFlags.HasValue)
            {
                SQLiteMarshal.WriteInt32(pIndex, offset,
                   (int)outputs.IndexFlags.GetValueOrDefault());
            }

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), sizeof(long));

            if (outputs.CanUseColumnsUsed() &&
                outputs.ColumnsUsed.HasValue)
            {
                SQLiteMarshal.WriteInt64(pIndex, offset,
                    outputs.ColumnsUsed.GetValueOrDefault());
            }
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Public Properties
................................................................................
        {
#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt32(pointer, offset);
#else
            return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
#endif
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Reads a <see cref="Int64" /> value from the specified memory
        /// location.
        /// </summary>
        /// <param name="pointer">
        /// The <see cref="IntPtr" /> object instance representing the base
        /// memory location.
        /// </param>
        /// <param name="offset">
        /// The integer offset from the base memory location where the
        /// <see cref="Int64" /> value to be read is located.
        /// </param>
        /// <returns>
        /// The <see cref="Int64" /> value at the specified memory location.
        /// </returns>
        public static long ReadInt64(
            IntPtr pointer,
            int offset
            )
        {
#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt64(pointer, offset);
#else
            return Marshal.ReadInt64(IntPtrForOffset(pointer, offset));
#endif
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Reads a <see cref="Double" /> value from the specified memory
        /// location.
        /// </summary>
................................................................................
            try
            {
                if (pVtab == IntPtr.Zero)
                    return false;

                int offset = 0;

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, IntPtr.Size, sizeof(int));

                offset = SQLiteMarshal.NextOffsetOf(
                    offset, sizeof(int), IntPtr.Size);

                IntPtr pOldError = SQLiteMarshal.ReadIntPtr(pVtab, offset);

                if (pOldError != IntPtr.Zero)
                {
                    SQLiteMemory.Free(pOldError); pOldError = IntPtr.Zero;
                    SQLiteMarshal.WriteIntPtr(pVtab, offset, pOldError);
................................................................................
            if (pVtab == IntPtr.Zero)
                return;

            int offset = 0;

            SQLiteMarshal.WriteIntPtr(pVtab, offset, IntPtr.Zero);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, IntPtr.Size, sizeof(int));

            SQLiteMarshal.WriteInt32(pVtab, offset, 0);

            offset = SQLiteMarshal.NextOffsetOf(
                offset, sizeof(int), IntPtr.Size);

            SQLiteMarshal.WriteIntPtr(pVtab, offset, IntPtr.Zero);
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
................................................................................
        /// <summary>
        /// Modifies the specified <see cref="SQLiteIndex" /> object instance
        /// to contain the specified flags.
        /// </summary>
        /// <param name="index">
        /// The <see cref="SQLiteIndex" /> object instance to modify.
        /// </param>
        /// <param name="indexFlags">
        /// The index flags value to use.  Using a null value means that the
        /// default value provided by the SQLite core library should be used.
        /// </param>
        /// <returns>
        /// Non-zero upon success.
        /// </returns>
        protected virtual bool SetIndexFlags(
            SQLiteIndex index,
            SQLiteIndexFlags? indexFlags
            )
        {
            if ((index == null) || (index.Outputs == null))
                return false;

            index.Outputs.IndexFlags = indexFlags;
            return true;
        }

        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Modifies the specified <see cref="SQLiteIndex" /> object instance
................................................................................
        /// </summary>
        /// <param name="index">
        /// The <see cref="SQLiteIndex" /> object instance to modify.
        /// </param>
        /// <returns>
        /// Non-zero upon success.
        /// </returns>
        protected virtual bool SetIndexFlags(
            SQLiteIndex index
            )
        {
            return SetIndexFlags(index, null);
        }
        #endregion
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Public Properties
................................................................................
            {
                SQLiteVirtualTable table = TableFromIntPtr(pVtab);

                if (table != null)
                {
                    SQLiteIndex index = null;

                    SQLiteIndex.FromIntPtr(pIndex, true, ref index);

                    if (BestIndex(table, index) == SQLiteErrorCode.Ok)
                    {
                        SQLiteIndex.ToIntPtr(index, pIndex, true);
                        return SQLiteErrorCode.Ok;
                    }
                }
            }
            catch (Exception e) /* NOTE: Must catch ALL. */
            {
                SetTableError(pVtab, e.ToString());

Changes to System.Data.SQLite/UnsafeNativeMethods.cs.

2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866



2867
2868
2869
2870
2871
2872
2873

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_index_info
    {
        /* Inputs */
        public int nConstraint; /* Number of entries in aConstraint */
        public IntPtr aConstraint;
        public int nOrderBy;
        public IntPtr aOrderBy;
        /* Outputs */
        public IntPtr aConstraintUsage;
        public int idxNum;           /* Number used to identify the index */
        public string idxStr;        /* String, possibly obtained from sqlite3_malloc */
        public int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
        public int orderByConsumed;  /* True if output is already ordered */
        public double estimatedCost; /* Estimated cost of using this index */



    }
#endif
    #endregion
  }

  /////////////////////////////////////////////////////////////////////////////








|








>
>
>







2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_index_info
    {
        /* Inputs */
        public int nConstraint; /* Number of entries in aConstraint */
        public IntPtr aConstraint;
        public int nOrderBy;    /* Number of entries in aOrderBy */
        public IntPtr aOrderBy;
        /* Outputs */
        public IntPtr aConstraintUsage;
        public int idxNum;           /* Number used to identify the index */
        public string idxStr;        /* String, possibly obtained from sqlite3_malloc */
        public int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
        public int orderByConsumed;  /* True if output is already ordered */
        public double estimatedCost; /* Estimated cost of using this index */
        public long estimatedRows;   /* Estimated number of rows returned */
        public SQLiteIndexFlags idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
        public long colUsed;         /* Input: Mask of columns used by statement */
    }
#endif
    #endregion
  }

  /////////////////////////////////////////////////////////////////////////////

Changes to Tests/vtab.eagle.

1771
1772
1773
1774
1775
1776
1777
1778
1779
1780






































































































































































































1781
1782

  unset -nocomplain result code results errors sql dataSource id fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite defineConstant.System.Data.SQLite.INTEROP_VIRTUAL_TABLE\
compileCSharp} -match regexp -result [string map [list \n \r\n] {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \{-?\d+ one -?\d+ two -?\d+\
three -?\d+ 4 -?\d+ 5\.0\}$}]}

###############################################################################







































































































































































































runSQLiteTestEpilogue
runTestEpilogue










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980

  unset -nocomplain result code results errors sql dataSource id fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite defineConstant.System.Data.SQLite.INTEROP_VIRTUAL_TABLE\
compileCSharp} -match regexp -result [string map [list \n \r\n] {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \{-?\d+ one -?\d+ two -?\d+\
three -?\d+ 4 -?\d+ 5\.0\}$}]}

###############################################################################

runTest {test vtab-1.12 {SQLiteIndex managed-to-native-to-managed} -setup {
  set nConstraint 3; set nOrderBy 3
} -body {
  set index(1) [object create -alias -flags +NonPublic \
      System.Data.SQLite.SQLiteIndex $nConstraint $nOrderBy]

  for {set iConstraint 0} {$iConstraint < $nConstraint} {incr iConstraint} {
    set constraint(1) [object create -alias -flags +NonPublic \
        System.Data.SQLite.SQLiteIndexConstraint 0 0 0 0]

    $index(1) Inputs.Constraints.SetValue $constraint(1) $iConstraint

    $constraint(1) iColumn [expr {int(0x01234567 + $iConstraint)}]
    $constraint(1) op [expr {0xFF - $iConstraint}]
    $constraint(1) usable [expr {0xCC + $iConstraint}]
    $constraint(1) iTermOffset [expr {int(0x89ABCDEF + $iConstraint)}]
  }

  for {set iOrderBy 0} {$iOrderBy < $nOrderBy} {incr iOrderBy} {
    set orderBy(1) [object create -alias -flags +NonPublic \
        System.Data.SQLite.SQLiteIndexOrderBy 0 0]

    $index(1) Inputs.OrderBys.SetValue $orderBy(1) $iOrderBy

    $orderBy(1) iColumn [expr {int(0x23016745 + $iOrderBy)}]
    $orderBy(1) desc [expr {0xFF - $iOrderBy}]
  }

  for {set iConstraint 0} {$iConstraint < $nConstraint} {incr iConstraint} {
    set constraintUsage(1) [object create -alias -flags +NonPublic \
        System.Data.SQLite.SQLiteIndexConstraintUsage 0 0]

    $index(1) Outputs.ConstraintUsages.SetValue $constraintUsage(1) \
        $iConstraint

    $constraintUsage(1) argvIndex [expr {int(0xAB89EFCD + $iConstraint)}]
    $constraintUsage(1) omit [expr {0xCC + $iConstraint}]
  }

  $index(1) Outputs.IndexNumber [expr {int(0xAAAAAAAA)}]
  $index(1) Outputs.IndexString "\x01test index string.\xFF"
  $index(1) Outputs.NeedToFreeIndexString [expr {int(0x55555555)}]
  $index(1) Outputs.OrderByConsumed [expr {int(0x33333333)}]
  $index(1) Outputs.EstimatedCost 1.0

  if {[$index(1) Outputs.CanUseEstimatedRows]} then {
    $index(1) Outputs.EstimatedRows [expr {int(0xCCCCCCCC)}]
  }

  if {[$index(1) Outputs.CanUseIndexFlags]} then {
    $index(1) Outputs.IndexFlags [expr {int(0xEEEEEEEE)}]
  }

  if {[$index(1) Outputs.CanUseColumnsUsed]} then {
    $index(1) Outputs.ColumnsUsed [expr {wide(0xBADC0FFEE875621A)}]
  }

  set pIndex(1) [object invoke -create -flags +NonPublic \
      System.Data.SQLite.SQLiteIndex AllocMemoryBlock \
      $nConstraint $nOrderBy]

  object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteIndex ToIntPtr $index(1) $pIndex(1) true

  set index(2) [object create -alias -flags +NonPublic \
      System.Data.SQLite.SQLiteIndex $nConstraint $nOrderBy]

  object invoke -alias -flags +NonPublic \
      System.Data.SQLite.SQLiteIndex FromIntPtr $pIndex(1) true index(2)

  for {set iConstraint 0} {$iConstraint < $nConstraint} {incr iConstraint} {
    set constraint(1) [$index(1) \
        -alias Inputs.Constraints.GetValue $iConstraint]

    set constraint(2) [$index(2) \
        -alias Inputs.Constraints.GetValue $iConstraint]

    if {[$constraint(1) iColumn] != [$constraint(2) iColumn]} then {
      error [appendArgs \
          "iColumn at index " $iConstraint " does not match"]
    }

    if {[$constraint(1) op] != [$constraint(2) op]} then {
      error [appendArgs \
          "op at index " $iConstraint " does not match"]
    }

    if {[$constraint(1) usable] != [$constraint(2) usable]} then {
      error [appendArgs \
          "usable at index " $iConstraint " does not match"]
    }

    if {[$constraint(1) iTermOffset] != [$constraint(2) iTermOffset]} then {
      error [appendArgs \
          "iTermOffset at index " $iConstraint " does not match"]
    }
  }

  for {set iOrderBy 0} {$iOrderBy < $nOrderBy} {incr iOrderBy} {
    set orderBy(1) [$index(1) \
        -alias Inputs.OrderBys.GetValue $iOrderBy]

    set orderBy(2) [$index(2) \
        -alias Inputs.OrderBys.GetValue $iOrderBy]

    if {[$orderBy(1) iColumn] != [$orderBy(2) iColumn]} then {
      error [appendArgs \
          "iColumn at index " $iOrderBy " does not match"]
    }

    if {[$orderBy(1) desc] != [$orderBy(2) desc]} then {
      error [appendArgs \
          "desc at index " $iOrderBy " does not match"]
    }
  }

  for {set iConstraint 0} {$iConstraint < $nConstraint} {incr iConstraint} {
    set constraintUsage(1) [$index(1) \
        -alias Outputs.ConstraintUsages.GetValue $iConstraint]

    set constraintUsage(2) [$index(2) \
        -alias Outputs.ConstraintUsages.GetValue $iConstraint]

    if {[$constraintUsage(1) argvIndex] != \
        [$constraintUsage(2) argvIndex]} then {
      error [appendArgs \
          "argvIndex at index " $iConstraint " does not match"]
    }

    if {[$constraintUsage(1) omit] != [$constraintUsage(2) omit]} then {
      error [appendArgs \
          "omit at index " $iConstraint " does not match"]
    }
  }

  if {[$index(1) Outputs.IndexNumber] != \
      [$index(2) Outputs.IndexNumber]} then {
    error "IndexNumber does not match"
  }

  if {[$index(1) Outputs.IndexString] ne \
      [$index(2) Outputs.IndexString]} then {
    error "IndexString does not match"
  }

  if {[$index(1) Outputs.NeedToFreeIndexString] != \
      [$index(2) Outputs.NeedToFreeIndexString]} then {
    error "NeedToFreeIndexString does not match"
  }

  if {[$index(1) Outputs.OrderByConsumed] != \
      [$index(2) Outputs.OrderByConsumed]} then {
    error "OrderByConsumed does not match"
  }

  if {[$index(1) Outputs.EstimatedCost] != \
      [$index(2) Outputs.EstimatedCost]} then {
    error "EstimatedCost does not match"
  }

  if {[$index(1) Outputs.CanUseEstimatedRows] && \
      [$index(2) Outputs.CanUseEstimatedRows]} then {
    if {[$index(1) Outputs.EstimatedRows] != \
        [$index(2) Outputs.EstimatedRows]} then {
      error "EstimatedRows does not match"
    }
  }

  if {[$index(1) Outputs.CanUseIndexFlags] && \
      [$index(2) Outputs.CanUseIndexFlags]} then {
    if {[$index(1) Outputs.IndexFlags] != \
        [$index(2) Outputs.IndexFlags]} then {
      error "IndexFlags does not match"
    }
  }

  if {[$index(1) Outputs.CanUseColumnsUsed] && \
      [$index(2) Outputs.CanUseColumnsUsed]} then {
    if {[$index(1) Outputs.ColumnsUsed] != \
        [$index(2) Outputs.ColumnsUsed]} then {
      error "ColumnsUsed does not match"
    }
  }
} -cleanup {
  catch {
    object invoke -flags +NonPublic \
        System.Data.SQLite.SQLiteIndex FreeMemoryBlock $pIndex(1)
  }

  unset -nocomplain constraintUsage
  unset -nocomplain orderBy nOrderBy iOrderBy
  unset -nocomplain constraint nConstraint iConstraint
  unset -nocomplain pIndex index
} -constraints {eagle command.object System.Data.SQLite\
defineConstant.System.Data.SQLite.INTEROP_VIRTUAL_TABLE} -result {}}

###############################################################################

runSQLiteTestEpilogue
runTestEpilogue