System.Data.SQLite
Check-in [8a682d20dc]
Not logged in

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

Overview
Comment:More work in progress.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: 8a682d20dc6e68a8472cff27a56fa49b8e55f647
User & Date: mistachkin 2013-06-15 02:46:54
Context
2013-06-18
06:07
More work on input/output marshalling for the sqlite3_index_info structure. Also, refactoring to allow better portability to the .NET Compact Framework. check-in: ea5335378e user: mistachkin tags: virtualTables
2013-06-15
02:46
More work in progress. check-in: 8a682d20dc user: mistachkin tags: virtualTables
2013-06-13
22:49
Complete most of the initial virtual table module base class. Some fixes to types for marshalling. check-in: df6776ec88 user: mistachkin tags: virtualTables
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

   307    307           private UnsafeNativeMethods.sqlite3_index_constraint_usage constraintUsage;
   308    308       }
   309    309   
   310    310       ///////////////////////////////////////////////////////////////////////////
   311    311   
   312    312       public sealed class SQLiteIndexInputs
   313    313       {
   314         -        private SQLiteIndexConstraint[] Constraints;
   315         -        private SQLiteIndexOrderBy[] OrderBys;
          314  +        public SQLiteIndexInputs(int nConstraint, int nOrderBy)
          315  +        {
          316  +            constraints = new SQLiteIndexConstraint[nConstraint];
          317  +            orderBys = new SQLiteIndexOrderBy[nOrderBy];
          318  +        }
          319  +
          320  +        ///////////////////////////////////////////////////////////////////////
          321  +
          322  +        private SQLiteIndexConstraint[] constraints;
          323  +        public SQLiteIndexConstraint[] Constraints
          324  +        {
          325  +            get { return constraints; }
          326  +        }
          327  +
          328  +        ///////////////////////////////////////////////////////////////////////
          329  +
          330  +        private SQLiteIndexOrderBy[] orderBys;
          331  +        public SQLiteIndexOrderBy[] OrderBys
          332  +        {
          333  +            get { return orderBys; }
          334  +        }
   316    335       }
   317    336   
   318    337       ///////////////////////////////////////////////////////////////////////////
   319    338   
   320    339       public sealed class SQLiteIndexOutputs
   321    340       {
   322         -        private SQLiteIndexConstraintUsage[] ConstraintUsages;
   323         -        private int idxNum;           /* Number used to identify the index */
   324         -        private string idxStr;        /* String, possibly obtained from sqlite3_malloc */
          341  +        public SQLiteIndexOutputs(int nConstraint)
          342  +        {
          343  +            constraintUsages = new SQLiteIndexConstraintUsage[nConstraint];
          344  +        }
          345  +
          346  +        ///////////////////////////////////////////////////////////////////////
          347  +
          348  +        private SQLiteIndexConstraintUsage[] constraintUsages;
          349  +        public SQLiteIndexConstraintUsage[] ConstraintUsages
          350  +        {
          351  +            get { return constraintUsages; }
          352  +        }
          353  +
          354  +        ///////////////////////////////////////////////////////////////////////
          355  +
          356  +        private int idxNum; /* Number used to identify the index */
          357  +        public int IdxNum
          358  +        {
          359  +            get { return idxNum; }
          360  +            set { idxNum = value; }
          361  +        }
          362  +
          363  +        ///////////////////////////////////////////////////////////////////////
          364  +
          365  +        private string idxStr; /* String, possibly obtained from sqlite3_malloc */
          366  +        public string IdxStr
          367  +        {
          368  +            get { return idxStr; }
          369  +            set { idxStr = value; }
          370  +        }
          371  +
          372  +        ///////////////////////////////////////////////////////////////////////
          373  +
   325    374           private int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
   326         -        private int orderByConsumed;  /* True if output is already ordered */
          375  +        public int NeedToFreeIdxStr
          376  +        {
          377  +            get { return needToFreeIdxStr; }
          378  +            set { needToFreeIdxStr = value; }
          379  +        }
          380  +
          381  +        ///////////////////////////////////////////////////////////////////////
          382  +
          383  +        private int orderByConsumed; /* True if output is already ordered */
          384  +        public int OrderByConsumed
          385  +        {
          386  +            get { return orderByConsumed; }
          387  +            set { orderByConsumed = value; }
          388  +        }
          389  +
          390  +        ///////////////////////////////////////////////////////////////////////
          391  +
   327    392           private double estimatedCost; /* Estimated cost of using this index */
          393  +        public double EstimatedCost
          394  +        {
          395  +            get { return estimatedCost; }
          396  +            set { estimatedCost = value; }
          397  +        }
   328    398       }
   329    399   
   330    400       ///////////////////////////////////////////////////////////////////////////
   331    401   
   332    402       public sealed class SQLiteIndex
   333    403       {
   334         -        public SQLiteIndexInputs inputs;
   335         -        public SQLiteIndexOutputs outputs;
          404  +        public SQLiteIndex(int nConstraint, int nOrderBy)
          405  +        {
          406  +            inputs = new SQLiteIndexInputs(nConstraint, nOrderBy);
          407  +            outputs = new SQLiteIndexOutputs(nConstraint);
          408  +        }
          409  +
          410  +        ///////////////////////////////////////////////////////////////////////
          411  +
          412  +        private SQLiteIndexInputs inputs;
          413  +        public SQLiteIndexInputs Inputs
          414  +        {
          415  +            get { return inputs; }
          416  +        }
          417  +
          418  +        ///////////////////////////////////////////////////////////////////////
          419  +
          420  +        private SQLiteIndexOutputs outputs;
          421  +        public SQLiteIndexOutputs Outputs
          422  +        {
          423  +            get { return outputs; }
          424  +        }
   336    425       }
   337    426   
   338    427       ///////////////////////////////////////////////////////////////////////////
   339    428   
   340    429       public class SQLiteVirtualTableCursor
   341    430       {
   342    431           public SQLiteVirtualTableCursor(
................................................................................
   359    448   
   360    449       public interface ISQLiteNativeModule
   361    450       {
   362    451           // https://www.sqlite.org/vtab.html
   363    452   
   364    453           SQLiteErrorCode xCreate(IntPtr pDb, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr pError);
   365    454           SQLiteErrorCode xConnect(IntPtr pDb, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr pError);
   366         -        SQLiteErrorCode xBestIndex(IntPtr pVtab, IntPtr index);
          455  +        SQLiteErrorCode xBestIndex(IntPtr pVtab, IntPtr pIndex);
   367    456           SQLiteErrorCode xDisconnect(IntPtr pVtab);
   368    457           SQLiteErrorCode xDestroy(IntPtr pVtab);
   369    458           SQLiteErrorCode xOpen(IntPtr pVtab, ref IntPtr pCursor);
   370    459           SQLiteErrorCode xClose(IntPtr pCursor);
   371    460           SQLiteErrorCode xFilter(IntPtr pCursor, int idxNum, IntPtr idxStr, int argc, IntPtr[] argv);
   372    461           SQLiteErrorCode xNext(IntPtr pCursor);
   373    462           bool xEof(IntPtr pCursor);
................................................................................
   404    493           SQLiteErrorCode Column(SQLiteVirtualTableCursor cursor, SQLiteContext context, int index);
   405    494           SQLiteErrorCode RowId(SQLiteVirtualTableCursor cursor, ref long rowId);
   406    495           SQLiteErrorCode Update(SQLiteValue[] values, ref long rowId);
   407    496           SQLiteErrorCode Begin();
   408    497           SQLiteErrorCode Sync();
   409    498           SQLiteErrorCode Commit();
   410    499           SQLiteErrorCode Rollback();
   411         -        bool FindFunction(string zName, ref SQLiteFunction function, ref IntPtr pClientData);
          500  +        bool FindFunction(int nArg, string zName, ref SQLiteFunction function, ref IntPtr pClientData);
   412    501           SQLiteErrorCode Rename(string zNew);
   413    502           SQLiteErrorCode Savepoint(int iSavepoint);
   414    503           SQLiteErrorCode Release(int iSavepoint);
   415    504           SQLiteErrorCode RollbackTo(int iSavepoint);
   416    505       }
   417    506   
   418    507       ///////////////////////////////////////////////////////////////////////////
................................................................................
   665    754   
   666    755               SQLiteValue[] result = new SQLiteValue[values.Length];
   667    756   
   668    757               for (int index = 0; index < result.Length; index++)
   669    758                   result[index] = new SQLiteValue(values[index]);
   670    759   
   671    760               return result;
          761  +        }
          762  +
          763  +        ///////////////////////////////////////////////////////////////////////
          764  +
          765  +        private static UnsafeNativeMethods.sqlite3_index_info IndexFromIntPtr(
          766  +            IntPtr pIndex
          767  +            )
          768  +        {
          769  +            if (pIndex == IntPtr.Zero)
          770  +                return new UnsafeNativeMethods.sqlite3_index_info();
          771  +
          772  +            return new UnsafeNativeMethods.sqlite3_index_info();
          773  +        }
          774  +
          775  +        ///////////////////////////////////////////////////////////////////////
          776  +
          777  +        private static void IndexOutputsToIntPtr(
          778  +            UnsafeNativeMethods.sqlite3_index_info index,
          779  +            SQLiteIndexOutputs indexOutputs
          780  +            )
          781  +        {
          782  +
          783  +
   672    784           }
   673    785           #endregion
   674    786   
   675    787           ///////////////////////////////////////////////////////////////////////
   676    788   
   677    789           #region Public Constructors
   678    790           public SQLiteModuleBase()
................................................................................
   936   1048               return SQLiteErrorCode.Error;
   937   1049           }
   938   1050   
   939   1051           ///////////////////////////////////////////////////////////////////////
   940   1052   
   941   1053           public SQLiteErrorCode xBestIndex(
   942   1054               IntPtr pVtab,
   943         -            IntPtr index
         1055  +            IntPtr pIndex
   944   1056               )
   945   1057           {
   946         -            return SQLiteErrorCode.Ok;
         1058  +            try
         1059  +            {
         1060  +                if (BestIndex(null) == SQLiteErrorCode.Ok)
         1061  +                {
         1062  +                    return SQLiteErrorCode.Ok;
         1063  +                }
         1064  +            }
         1065  +            catch (Exception e) /* NOTE: Must catch ALL. */
         1066  +            {
         1067  +                SetTableError(pVtab, e.ToString());
         1068  +            }
         1069  +
         1070  +            return SQLiteErrorCode.Error;
   947   1071           }
   948   1072   
   949   1073           ///////////////////////////////////////////////////////////////////////
   950   1074   
   951   1075           public SQLiteErrorCode xDisconnect(
   952   1076               IntPtr pVtab
   953   1077               )
................................................................................
  1335   1459               )
  1336   1460           {
  1337   1461               try
  1338   1462               {
  1339   1463                   SQLiteFunction function = null;
  1340   1464   
  1341   1465                   if (FindFunction(
  1342         -                        StringFromUtf8IntPtr(zName), ref function,
         1466  +                        nArg, StringFromUtf8IntPtr(zName), ref function,
  1343   1467                           ref pClientData))
  1344   1468                   {
  1345   1469                       if (function != null)
  1346   1470                       {
  1347   1471                           callback = function.ScalarCallback;
  1348   1472                           return true;
  1349   1473                       }
................................................................................
  1550   1674           ///////////////////////////////////////////////////////////////////////
  1551   1675   
  1552   1676           public abstract SQLiteErrorCode Rollback();
  1553   1677   
  1554   1678           ///////////////////////////////////////////////////////////////////////
  1555   1679   
  1556   1680           public abstract bool FindFunction(
         1681  +            int nArg,
  1557   1682               string zName,
  1558   1683               ref SQLiteFunction function,
  1559   1684               ref IntPtr pClientData
  1560   1685               );
  1561   1686   
  1562   1687           ///////////////////////////////////////////////////////////////////////
  1563   1688   

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

   219    219               CheckDisposed();
   220    220               throw new NotImplementedException();
   221    221           }
   222    222   
   223    223           ///////////////////////////////////////////////////////////////////////
   224    224   
   225    225           public override bool FindFunction(
          226  +            int nArg,
   226    227               string zName,
   227    228               ref SQLiteFunction function,
   228    229               ref IntPtr pClientData
   229    230               )
   230    231           {
   231    232               CheckDisposed();
   232    233               throw new NotImplementedException();

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

  1651   1651       ///////////////////////////////////////////////////////////////////////////
  1652   1652   
  1653   1653   #if !PLATFORM_COMPACTFRAMEWORK
  1654   1654       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1655   1655   #endif
  1656   1656       public delegate SQLiteErrorCode xBestIndex(
  1657   1657           IntPtr pVtab,
  1658         -        IntPtr index
         1658  +        IntPtr pIndex
  1659   1659       );
  1660   1660   
  1661   1661       ///////////////////////////////////////////////////////////////////////////
  1662   1662   
  1663   1663   #if !PLATFORM_COMPACTFRAMEWORK
  1664   1664       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1665   1665   #endif
................................................................................
  1958   1958       internal struct sqlite3_index_info
  1959   1959       {
  1960   1960           /* Inputs */
  1961   1961           public int nConstraint; /* Number of entries in aConstraint */
  1962   1962           [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
  1963   1963           public sqlite3_index_constraint[] aConstraint;
  1964   1964           public int nOrderBy;
         1965  +        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
  1965   1966           public sqlite3_index_orderby[] aOrderBy;
  1966   1967           /* Outputs */
         1968  +        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
  1967   1969           public sqlite3_index_constraint_usage[] aConstraintUsage;
  1968   1970           public int idxNum;           /* Number used to identify the index */
  1969   1971           public string idxStr;        /* String, possibly obtained from sqlite3_malloc */
  1970   1972           public int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
  1971   1973           public int orderByConsumed;  /* True if output is already ordered */
  1972   1974           public double estimatedCost; /* Estimated cost of using this index */
  1973   1975       }