System.Data.SQLite
Check-in [3b281ced11]
Not logged in

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

Overview
Comment:Heavily refactor how native delegates and exception handling are performed.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 3b281ced1169273dd472518804e51761d23053c7
User & Date: mistachkin 2017-10-10 21:38:27
Context
2017-10-10
21:41
Merge updates from trunk. check-in: 0b70bd2e0e user: mistachkin tags: sessions
21:38
Heavily refactor how native delegates and exception handling are performed. check-in: 3b281ced11 user: mistachkin tags: sessions
20:08
Add and revise tests for the session extension support classes. check-in: ca9127aa51 user: mistachkin tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

   148    148       ///////////////////////////////////////////////////////////////////////////
   149    149   
   150    150       #region SQLiteConnectionLock Class
   151    151       internal abstract class SQLiteConnectionLock : IDisposable
   152    152       {
   153    153           #region Private Constants
   154    154           private const string LockNopSql = "SELECT 1;";
          155  +
          156  +        ///////////////////////////////////////////////////////////////////////
          157  +
          158  +        private const string StatementMessageFormat =
          159  +            "Connection lock object was {0} with statement {1}";
   155    160           #endregion
   156    161   
   157    162           ///////////////////////////////////////////////////////////////////////
   158    163   
   159    164           #region Private Data
   160    165           private SQLiteConnectionHandle handle;
   161    166           private SQLiteConnectionFlags flags;
................................................................................
   178    183   
   179    184               Lock();
   180    185           }
   181    186           #endregion
   182    187   
   183    188           ///////////////////////////////////////////////////////////////////////
   184    189   
   185         -        #region Private Methods
   186         -        private IntPtr GetHandle()
          190  +        #region Protected Methods
          191  +        protected SQLiteConnectionHandle GetHandle()
          192  +        {
          193  +            return handle;
          194  +        }
          195  +
          196  +        ///////////////////////////////////////////////////////////////////////
          197  +
          198  +        protected SQLiteConnectionFlags GetFlags()
          199  +        {
          200  +            return flags;
          201  +        }
          202  +
          203  +        ///////////////////////////////////////////////////////////////////////
          204  +
          205  +        protected IntPtr GetIntPtr()
   187    206           {
   188    207               if (handle == null)
   189    208               {
   190    209                   throw new InvalidOperationException(
   191    210                       "Connection lock object has an invalid handle.");
   192    211               }
   193    212   
................................................................................
   221    240   
   222    241                   pSql = SQLiteString.Utf8IntPtrFromString(LockNopSql, ref nSql);
   223    242   
   224    243                   IntPtr pRemain = IntPtr.Zero;
   225    244                   int nRemain = 0;
   226    245   
   227    246                   SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3_prepare_interop(
   228         -                    GetHandle(), pSql, nSql, ref statement, ref pRemain,
          247  +                    GetIntPtr(), pSql, nSql, ref statement, ref pRemain,
   229    248                       ref nRemain);
   230    249   
   231    250                   if (rc != SQLiteErrorCode.Ok)
   232    251                       throw new SQLiteException(rc, "sqlite3_prepare_interop");
   233    252               }
   234    253               finally
   235    254               {
................................................................................
   309    328                           //
   310    329                           // NOTE: This should never happen.  This object was
   311    330                           //       disposed (or finalized) without the Unlock
   312    331                           //       method being called first.
   313    332                           //
   314    333                           try
   315    334                           {
   316         -                            if ((flags & SQLiteConnectionFlags.LogPrepare) ==
   317         -                                    SQLiteConnectionFlags.LogPrepare)
          335  +                            if (HelperMethods.LogPrepare(GetFlags()))
   318    336                               {
   319    337                                   /* throw */
   320         -                                SQLiteLog.LogMessage(SQLiteErrorCode.Misuse,
   321         -                                    HelperMethods.StringFormat(CultureInfo.CurrentCulture,
   322         -                                    "Connection lock object was {0} with statement {1}",
   323         -                                    disposing ? "disposed" : "finalized", statement));
          338  +                                SQLiteLog.LogMessage(
          339  +                                    SQLiteErrorCode.Misuse,
          340  +                                    HelperMethods.StringFormat(
          341  +                                    CultureInfo.CurrentCulture,
          342  +                                    StatementMessageFormat, disposing ?
          343  +                                        "disposed" : "finalized",
          344  +                                    statement));
   324    345                               }
   325    346                           }
   326    347                           catch
   327    348                           {
   328    349                               // do nothing.
   329    350                           }
   330    351   
................................................................................
   382    403           {
   383    404               if (iterator == IntPtr.Zero)
   384    405                   throw new InvalidOperationException("iterator is not open");
   385    406           }
   386    407   
   387    408           ///////////////////////////////////////////////////////////////////////
   388    409   
   389         -        internal IntPtr GetHandle()
          410  +        internal IntPtr GetIntPtr()
   390    411           {
   391    412               return iterator;
   392    413           }
   393    414           #endregion
   394    415   
   395    416           ///////////////////////////////////////////////////////////////////////
   396    417   
................................................................................
   767    788               this.stream = stream;
   768    789               this.flags = flags;
   769    790           }
   770    791           #endregion
   771    792   
   772    793           ///////////////////////////////////////////////////////////////////////
   773    794   
          795  +        #region Private Methods
          796  +        private SQLiteConnectionFlags GetFlags()
          797  +        {
          798  +            return flags;
          799  +        }
          800  +        #endregion
          801  +
          802  +        ///////////////////////////////////////////////////////////////////////
          803  +
   774    804           #region Native Callback Methods
   775    805           public SQLiteErrorCode xInput(
   776    806               IntPtr context,
   777    807               IntPtr pData,
   778    808               ref int nData
   779    809               )
   780    810           {
................................................................................
   795    825   
   796    826                   return SQLiteErrorCode.Ok;
   797    827               }
   798    828               catch (Exception e)
   799    829               {
   800    830                   try
   801    831                   {
   802         -                    if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
   803         -                            SQLiteConnectionFlags.LogCallbackException)
          832  +                    if (HelperMethods.LogCallbackExceptions(GetFlags()))
   804    833                       {
   805         -                        SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
   806         -                            HelperMethods.StringFormat(CultureInfo.CurrentCulture,
   807         -                            "Caught exception in \"xInput\" method: {0}",
   808         -                            e)); /* throw */
          834  +                        SQLiteLog.LogMessage(
          835  +                            SQLiteBase.COR_E_EXCEPTION,
          836  +                            HelperMethods.StringFormat(
          837  +                            CultureInfo.CurrentCulture,
          838  +                            UnsafeNativeMethods.ExceptionMessageFormat,
          839  +                            "xInput", e)); /* throw */
   809    840                       }
   810    841                   }
   811    842                   catch
   812    843                   {
   813    844                       // do nothing.
   814    845                   }
   815    846               }
................................................................................
   842    873   
   843    874                   return SQLiteErrorCode.Ok;
   844    875               }
   845    876               catch (Exception e)
   846    877               {
   847    878                   try
   848    879                   {
   849         -                    if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
   850         -                            SQLiteConnectionFlags.LogCallbackException)
          880  +                    if (HelperMethods.LogCallbackExceptions(GetFlags()))
   851    881                       {
   852         -                        SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
   853         -                            HelperMethods.StringFormat(CultureInfo.CurrentCulture,
   854         -                            "Caught exception in \"xOutput\" method: {0}",
   855         -                            e)); /* throw */
          882  +                        SQLiteLog.LogMessage(
          883  +                            SQLiteBase.COR_E_EXCEPTION,
          884  +                            HelperMethods.StringFormat(
          885  +                            CultureInfo.CurrentCulture,
          886  +                            UnsafeNativeMethods.ExceptionMessageFormat,
          887  +                            "xOutput", e)); /* throw */
   856    888                       }
   857    889                   }
   858    890                   catch
   859    891                   {
   860    892                       // do nothing.
   861    893                   }
   862    894               }
................................................................................
  1565   1597           }
  1566   1598           #endregion
  1567   1599       }
  1568   1600       #endregion
  1569   1601   
  1570   1602       ///////////////////////////////////////////////////////////////////////////
  1571   1603   
         1604  +    #region SQLiteChangeSetBase Class
         1605  +    internal class SQLiteChangeSetBase : SQLiteConnectionLock
         1606  +    {
         1607  +        #region Private Constants
         1608  +        private const string ExceptionMessageFormat =
         1609  +            "Caught exception in \"{0}\" method: {1}";
         1610  +        #endregion
         1611  +
         1612  +        ///////////////////////////////////////////////////////////////////////
         1613  +
         1614  +        #region Private Constructors
         1615  +        internal SQLiteChangeSetBase(
         1616  +            SQLiteConnectionHandle handle,
         1617  +            SQLiteConnectionFlags flags
         1618  +            )
         1619  +            : base(handle, flags)
         1620  +        {
         1621  +            // do nothing.
         1622  +        }
         1623  +        #endregion
         1624  +
         1625  +        ///////////////////////////////////////////////////////////////////////
         1626  +
         1627  +        #region Protected Methods
         1628  +        protected UnsafeNativeMethods.xSessionFilter GetDelegate(
         1629  +            SessionTableFilterCallback tableFilterCallback,
         1630  +            object clientData
         1631  +            )
         1632  +        {
         1633  +            if (tableFilterCallback == null)
         1634  +                return null;
         1635  +
         1636  +            UnsafeNativeMethods.xSessionFilter xFilter;
         1637  +
         1638  +            xFilter = new UnsafeNativeMethods.xSessionFilter(
         1639  +                delegate(IntPtr context, byte[] tblName)
         1640  +            {
         1641  +                try
         1642  +                {
         1643  +                    string name = SQLiteString.GetStringFromUtf8Bytes(
         1644  +                        tblName);
         1645  +
         1646  +                    return tableFilterCallback(clientData, name) ? 1 : 0;
         1647  +                }
         1648  +                catch (Exception e)
         1649  +                {
         1650  +                    try
         1651  +                    {
         1652  +                        if (HelperMethods.LogCallbackExceptions(GetFlags()))
         1653  +                        {
         1654  +                            SQLiteLog.LogMessage( /* throw */
         1655  +                                SQLiteBase.COR_E_EXCEPTION,
         1656  +                                HelperMethods.StringFormat(
         1657  +                                CultureInfo.CurrentCulture,
         1658  +                                UnsafeNativeMethods.ExceptionMessageFormat,
         1659  +                                "xFilter", e));
         1660  +                        }
         1661  +                    }
         1662  +                    catch
         1663  +                    {
         1664  +                        // do nothing.
         1665  +                    }
         1666  +                }
         1667  +
         1668  +                return 0;
         1669  +            });
         1670  +
         1671  +            return xFilter;
         1672  +        }
         1673  +
         1674  +        ///////////////////////////////////////////////////////////////////////
         1675  +
         1676  +        protected UnsafeNativeMethods.xSessionConflict GetDelegate(
         1677  +            SessionConflictCallback conflictCallback,
         1678  +            object clientData
         1679  +            )
         1680  +        {
         1681  +            if (conflictCallback == null)
         1682  +                return null;
         1683  +
         1684  +            UnsafeNativeMethods.xSessionConflict xConflict;
         1685  +
         1686  +            xConflict = new UnsafeNativeMethods.xSessionConflict(
         1687  +                delegate(IntPtr context,
         1688  +                         SQLiteChangeSetConflictType type,
         1689  +                         IntPtr iterator)
         1690  +            {
         1691  +                ISQLiteChangeSetMetadataItem item = null;
         1692  +
         1693  +                try
         1694  +                {
         1695  +                    return conflictCallback(clientData, type, item);
         1696  +                }
         1697  +                catch (Exception e)
         1698  +                {
         1699  +                    try
         1700  +                    {
         1701  +                        if (HelperMethods.LogCallbackExceptions(GetFlags()))
         1702  +                        {
         1703  +                            SQLiteLog.LogMessage( /* throw */
         1704  +                                SQLiteBase.COR_E_EXCEPTION,
         1705  +                                HelperMethods.StringFormat(
         1706  +                                CultureInfo.CurrentCulture,
         1707  +                                UnsafeNativeMethods.ExceptionMessageFormat,
         1708  +                                "xConflict", e));
         1709  +                        }
         1710  +                    }
         1711  +                    catch
         1712  +                    {
         1713  +                        // do nothing.
         1714  +                    }
         1715  +                }
         1716  +
         1717  +                return SQLiteChangeSetConflictResult.Abort;
         1718  +            });
         1719  +
         1720  +            return xConflict;
         1721  +        }
         1722  +        #endregion
         1723  +
         1724  +        ///////////////////////////////////////////////////////////////////////
         1725  +
         1726  +        #region IDisposable "Pattern" Members
         1727  +        private bool disposed;
         1728  +        private void CheckDisposed() /* throw */
         1729  +        {
         1730  +#if THROW_ON_DISPOSED
         1731  +            if (disposed)
         1732  +            {
         1733  +                throw new ObjectDisposedException(
         1734  +                    typeof(SQLiteChangeSetBase).Name);
         1735  +            }
         1736  +#endif
         1737  +        }
         1738  +
         1739  +        ///////////////////////////////////////////////////////////////////////
         1740  +
         1741  +        protected override void Dispose(bool disposing)
         1742  +        {
         1743  +            try
         1744  +            {
         1745  +                if (!disposed)
         1746  +                {
         1747  +                    if (disposing)
         1748  +                    {
         1749  +                        ////////////////////////////////////
         1750  +                        // dispose managed resources here...
         1751  +                        ////////////////////////////////////
         1752  +                    }
         1753  +
         1754  +                    //////////////////////////////////////
         1755  +                    // release unmanaged resources here...
         1756  +                    //////////////////////////////////////
         1757  +
         1758  +                    Unlock();
         1759  +                }
         1760  +            }
         1761  +            finally
         1762  +            {
         1763  +                base.Dispose(disposing);
         1764  +
         1765  +                //
         1766  +                // NOTE: Everything should be fully disposed at this point.
         1767  +                //
         1768  +                disposed = true;
         1769  +            }
         1770  +        }
         1771  +        #endregion
         1772  +    }
         1773  +    #endregion
         1774  +
         1775  +    ///////////////////////////////////////////////////////////////////////////
         1776  +
  1572   1777       #region SQLiteMemoryChangeSet Class
  1573   1778       internal sealed class SQLiteMemoryChangeSet :
  1574         -        SQLiteConnectionLock, ISQLiteChangeSet
         1779  +        SQLiteChangeSetBase, ISQLiteChangeSet
  1575   1780       {
  1576   1781           #region Private Data
  1577   1782           private byte[] rawData;
  1578         -        private SQLiteConnectionHandle handle;
  1579         -        private SQLiteConnectionFlags flags;
  1580   1783           #endregion
  1581   1784   
  1582   1785           ///////////////////////////////////////////////////////////////////////
  1583   1786   
  1584   1787           #region Private Constructors
  1585   1788           internal SQLiteMemoryChangeSet(
  1586   1789               byte[] rawData,
  1587   1790               SQLiteConnectionHandle handle,
  1588   1791               SQLiteConnectionFlags flags
  1589   1792               )
  1590   1793               : base(handle, flags)
  1591   1794           {
  1592   1795               this.rawData = rawData;
  1593         -            this.handle= handle;
  1594         -            this.flags = flags;
  1595   1796           }
  1596   1797           #endregion
  1597   1798   
  1598   1799           ///////////////////////////////////////////////////////////////////////
  1599   1800   
  1600   1801           #region Private Methods
  1601   1802           private void CheckRawData()
................................................................................
  1628   1829                       nInData, pInData, ref nOutData, ref pOutData);
  1629   1830   
  1630   1831                   if (rc != SQLiteErrorCode.Ok)
  1631   1832                       throw new SQLiteException(rc, "sqlite3changeset_invert");
  1632   1833   
  1633   1834                   byte[] newData = SQLiteBytes.FromIntPtr(pOutData, nOutData);
  1634   1835   
  1635         -                return new SQLiteMemoryChangeSet(newData, handle, flags);
         1836  +                return new SQLiteMemoryChangeSet(
         1837  +                    newData, GetHandle(), GetFlags());
  1636   1838               }
  1637   1839               finally
  1638   1840               {
  1639   1841                   if (pOutData != IntPtr.Zero)
  1640   1842                   {
  1641   1843                       SQLiteMemory.Free(pOutData);
  1642   1844                       pOutData = IntPtr.Zero;
................................................................................
  1692   1894                       ref pOutData);
  1693   1895   
  1694   1896                   if (rc != SQLiteErrorCode.Ok)
  1695   1897                       throw new SQLiteException(rc, "sqlite3changeset_concat");
  1696   1898   
  1697   1899                   byte[] newData = SQLiteBytes.FromIntPtr(pOutData, nOutData);
  1698   1900   
  1699         -                return new SQLiteMemoryChangeSet(newData, handle, flags);
         1901  +                return new SQLiteMemoryChangeSet(
         1902  +                    newData, GetHandle(), GetFlags());
  1700   1903               }
  1701   1904               finally
  1702   1905               {
  1703   1906                   if (pOutData != IntPtr.Zero)
  1704   1907                   {
  1705   1908                       SQLiteMemory.Free(pOutData);
  1706   1909                       pOutData = IntPtr.Zero;
................................................................................
  1742   1945           {
  1743   1946               CheckDisposed();
  1744   1947               CheckRawData();
  1745   1948   
  1746   1949               if (conflictCallback == null)
  1747   1950                   throw new ArgumentNullException("conflictCallback");
  1748   1951   
  1749         -            ///////////////////////////////////////////////////////////////////
         1952  +            UnsafeNativeMethods.xSessionFilter xFilter = GetDelegate(
         1953  +                tableFilterCallback, clientData);
  1750   1954   
  1751         -            #region Native Callback Methods
  1752         -            UnsafeNativeMethods.xSessionFilter xFilter = null;
  1753         -
  1754         -            if (tableFilterCallback != null)
  1755         -            {
  1756         -                xFilter = new UnsafeNativeMethods.xSessionFilter(
  1757         -                    delegate(IntPtr context, byte[] tblName)
  1758         -                {
  1759         -                    try
  1760         -                    {
  1761         -                        string name = SQLiteString.GetStringFromUtf8Bytes(
  1762         -                            tblName);
  1763         -
  1764         -                        return tableFilterCallback(clientData, name) ? 1 : 0;
  1765         -                    }
  1766         -                    catch (Exception e)
  1767         -                    {
  1768         -                        try
  1769         -                        {
  1770         -                            if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
  1771         -                                    SQLiteConnectionFlags.LogCallbackException)
  1772         -                            {
  1773         -                                SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
  1774         -                                    HelperMethods.StringFormat(CultureInfo.CurrentCulture,
  1775         -                                    "Caught exception in \"xFilter\" method: {0}",
  1776         -                                    e)); /* throw */
  1777         -                            }
  1778         -                        }
  1779         -                        catch
  1780         -                        {
  1781         -                            // do nothing.
  1782         -                        }
  1783         -                    }
  1784         -
  1785         -                    return 0;
  1786         -                });
  1787         -            }
  1788         -
  1789         -            ///////////////////////////////////////////////////////////////////
  1790         -
  1791         -            UnsafeNativeMethods.xSessionConflict xConflict;
  1792         -
  1793         -            xConflict = new UnsafeNativeMethods.xSessionConflict(
  1794         -                delegate(IntPtr context,
  1795         -                         SQLiteChangeSetConflictType type,
  1796         -                         IntPtr iterator)
  1797         -            {
  1798         -                ISQLiteChangeSetMetadataItem item = null;
  1799         -
  1800         -                try
  1801         -                {
  1802         -                    return conflictCallback(clientData, type, item);
  1803         -                }
  1804         -                catch (Exception e)
  1805         -                {
  1806         -                    try
  1807         -                    {
  1808         -                        if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
  1809         -                                SQLiteConnectionFlags.LogCallbackException)
  1810         -                        {
  1811         -                            SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
  1812         -                                HelperMethods.StringFormat(CultureInfo.CurrentCulture,
  1813         -                                "Caught exception in \"xConflict\" method: {0}",
  1814         -                                e)); /* throw */
  1815         -                        }
  1816         -                    }
  1817         -                    catch
  1818         -                    {
  1819         -                        // do nothing.
  1820         -                    }
  1821         -                }
  1822         -
  1823         -                return SQLiteChangeSetConflictResult.Abort;
  1824         -            });
  1825         -            #endregion
  1826         -
  1827         -            ///////////////////////////////////////////////////////////////////
         1955  +            UnsafeNativeMethods.xSessionConflict xConflict = GetDelegate(
         1956  +                conflictCallback, clientData);
  1828   1957   
  1829   1958               IntPtr pData = IntPtr.Zero;
  1830   1959   
  1831   1960               try
  1832   1961               {
  1833   1962                   int nData = 0;
  1834   1963   
  1835   1964                   pData = SQLiteBytes.ToIntPtr(rawData, ref nData);
  1836   1965   
  1837   1966                   SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply(
  1838         -                    handle, nData, pData, xFilter, xConflict, IntPtr.Zero);
         1967  +                    GetIntPtr(), nData, pData, xFilter, xConflict, IntPtr.Zero);
  1839   1968   
  1840   1969                   if (rc != SQLiteErrorCode.Ok)
  1841   1970                       throw new SQLiteException(rc, "sqlite3changeset_apply");
  1842   1971               }
  1843   1972               finally
  1844   1973               {
  1845   1974                   if (pData != IntPtr.Zero)
................................................................................
  1923   2052       }
  1924   2053       #endregion
  1925   2054   
  1926   2055       ///////////////////////////////////////////////////////////////////////////
  1927   2056   
  1928   2057       #region SQLiteStreamChangeSet Class
  1929   2058       internal sealed class SQLiteStreamChangeSet :
  1930         -        SQLiteConnectionLock, ISQLiteChangeSet
         2059  +        SQLiteChangeSetBase, ISQLiteChangeSet
  1931   2060       {
  1932   2061           #region Private Data
  1933   2062           private Stream inputStream;
  1934   2063           private Stream outputStream;
  1935         -        private SQLiteConnectionHandle handle;
  1936         -        private SQLiteConnectionFlags flags;
  1937   2064           #endregion
  1938   2065   
  1939   2066           ///////////////////////////////////////////////////////////////////////
  1940   2067   
  1941   2068           #region Private Constructors
  1942   2069           internal SQLiteStreamChangeSet(
  1943   2070               Stream inputStream,
................................................................................
  1945   2072               SQLiteConnectionHandle handle,
  1946   2073               SQLiteConnectionFlags flags
  1947   2074               )
  1948   2075               : base(handle, flags)
  1949   2076           {
  1950   2077               this.inputStream = inputStream;
  1951   2078               this.outputStream = outputStream;
  1952         -            this.handle = handle;
  1953         -            this.flags = flags;
  1954   2079           }
  1955   2080           #endregion
  1956   2081   
  1957   2082           ///////////////////////////////////////////////////////////////////////
  1958   2083   
  1959   2084           #region Private Methods
  1960   2085           private void CheckInputStream()
................................................................................
  1982   2107   
  1983   2108           #region ISQLiteChangeSet Members
  1984   2109           public ISQLiteChangeSet Invert()
  1985   2110           {
  1986   2111               CheckDisposed();
  1987   2112               CheckInputStream();
  1988   2113               CheckOutputStream();
         2114  +
         2115  +            SQLiteConnectionFlags flags = GetFlags();
  1989   2116   
  1990   2117               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_invert_strm(
  1991   2118                   new SQLiteStreamAdapter(inputStream, flags).xInput, IntPtr.Zero,
  1992   2119                   new SQLiteStreamAdapter(outputStream, flags).xOutput, IntPtr.Zero);
  1993   2120   
  1994   2121               if (rc != SQLiteErrorCode.Ok)
  1995   2122                   throw new SQLiteException(rc, "sqlite3changeset_invert_strm");
................................................................................
  2013   2140               if (streamChangeSet == null)
  2014   2141               {
  2015   2142                   throw new ArgumentException(
  2016   2143                       "not a stream based change set", "changeSet");
  2017   2144               }
  2018   2145   
  2019   2146               streamChangeSet.CheckInputStream();
         2147  +
         2148  +            SQLiteConnectionFlags otherFlags = streamChangeSet.GetFlags();
         2149  +            SQLiteConnectionFlags flags = GetFlags();
  2020   2150   
  2021   2151               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_concat_strm(
  2022         -                new SQLiteStreamAdapter(inputStream, flags).xInput, IntPtr.Zero,
  2023         -                new SQLiteStreamAdapter(streamChangeSet.inputStream,
  2024         -                streamChangeSet.flags).xInput, IntPtr.Zero,
  2025         -                new SQLiteStreamAdapter(outputStream, flags).xOutput, IntPtr.Zero);
         2152  +                new SQLiteStreamAdapter(inputStream, flags).xInput,
         2153  +                IntPtr.Zero, new SQLiteStreamAdapter(streamChangeSet.inputStream,
         2154  +                otherFlags).xInput, IntPtr.Zero, new SQLiteStreamAdapter(
         2155  +                outputStream, flags).xOutput, IntPtr.Zero);
  2026   2156   
  2027   2157               if (rc != SQLiteErrorCode.Ok)
  2028   2158                   throw new SQLiteException(rc, "sqlite3changeset_concat_strm");
  2029   2159   
  2030   2160               return null;
  2031   2161           }
  2032   2162   
................................................................................
  2052   2182           {
  2053   2183               CheckDisposed();
  2054   2184               CheckInputStream();
  2055   2185   
  2056   2186               if (conflictCallback == null)
  2057   2187                   throw new ArgumentNullException("conflictCallback");
  2058   2188   
  2059         -            ///////////////////////////////////////////////////////////////////
  2060         -
  2061         -            #region Native Callback Methods
  2062         -            UnsafeNativeMethods.xSessionFilter xFilter;
  2063         -
  2064         -            xFilter = new UnsafeNativeMethods.xSessionFilter(
  2065         -                delegate(IntPtr context, byte[] tblName)
  2066         -            {
  2067         -                try
  2068         -                {
  2069         -                    string name = SQLiteString.GetStringFromUtf8Bytes(
  2070         -                        tblName);
  2071         -
  2072         -                    return tableFilterCallback(clientData, name) ? 1 : 0;
  2073         -                }
  2074         -                catch (Exception e)
  2075         -                {
  2076         -                    try
  2077         -                    {
  2078         -                        if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
  2079         -                                SQLiteConnectionFlags.LogCallbackException)
  2080         -                        {
  2081         -                            SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
  2082         -                                HelperMethods.StringFormat(CultureInfo.CurrentCulture,
  2083         -                                "Caught exception in \"xFilter\" method: {0}",
  2084         -                                e)); /* throw */
  2085         -                        }
  2086         -                    }
  2087         -                    catch
  2088         -                    {
  2089         -                        // do nothing.
  2090         -                    }
  2091         -                }
  2092         -
  2093         -                return 0;
  2094         -            });
  2095         -
  2096         -            ///////////////////////////////////////////////////////////////////
  2097         -
  2098         -            UnsafeNativeMethods.xSessionConflict xConflict;
  2099         -
  2100         -            xConflict = new UnsafeNativeMethods.xSessionConflict(
  2101         -                delegate(IntPtr context,
  2102         -                         SQLiteChangeSetConflictType type,
  2103         -                         IntPtr iterator)
  2104         -            {
  2105         -                ISQLiteChangeSetMetadataItem item = null;
  2106         -
  2107         -                try
  2108         -                {
  2109         -                    return conflictCallback(clientData, type, item);
  2110         -                }
  2111         -                catch (Exception e)
  2112         -                {
  2113         -                    try
  2114         -                    {
  2115         -                        if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
  2116         -                                SQLiteConnectionFlags.LogCallbackException)
  2117         -                        {
  2118         -                            SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
  2119         -                                HelperMethods.StringFormat(CultureInfo.CurrentCulture,
  2120         -                                "Caught exception in \"xConflict\" method: {0}",
  2121         -                                e)); /* throw */
  2122         -                        }
  2123         -                    }
  2124         -                    catch
  2125         -                    {
  2126         -                        // do nothing.
  2127         -                    }
  2128         -                }
  2129         -
  2130         -                return SQLiteChangeSetConflictResult.Abort;
  2131         -            });
  2132         -            #endregion
  2133         -
  2134         -            ///////////////////////////////////////////////////////////////////
         2189  +            UnsafeNativeMethods.xSessionFilter xFilter = GetDelegate(
         2190  +                tableFilterCallback, clientData);
         2191  +
         2192  +            UnsafeNativeMethods.xSessionConflict xConflict = GetDelegate(
         2193  +                conflictCallback, clientData);
         2194  +
         2195  +            SQLiteConnectionFlags flags = GetFlags();
  2135   2196   
  2136   2197               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply_strm(
  2137         -                handle, new SQLiteStreamAdapter(inputStream, flags).xInput,
         2198  +                GetIntPtr(), new SQLiteStreamAdapter(inputStream, flags).xInput,
  2138   2199                   IntPtr.Zero, xFilter, xConflict, IntPtr.Zero);
  2139   2200   
  2140   2201               if (rc != SQLiteErrorCode.Ok)
  2141   2202                   throw new SQLiteException(rc, "sqlite3changeset_apply_strm");
  2142   2203           }
  2143   2204           #endregion
  2144   2205   
  2145   2206           ///////////////////////////////////////////////////////////////////////
  2146   2207   
  2147   2208           #region IEnumerable<ISQLiteChangeSetMetadataItem> Members
  2148   2209           public IEnumerator<ISQLiteChangeSetMetadataItem> GetEnumerator()
  2149   2210           {
         2211  +            SQLiteConnectionFlags flags = GetFlags();
  2150   2212               return new SQLiteStreamChangeSetEnumerator(inputStream, flags);
  2151   2213           }
  2152   2214           #endregion
  2153   2215   
  2154   2216           ///////////////////////////////////////////////////////////////////////
  2155   2217   
  2156   2218           #region IEnumerable Members
................................................................................
  2588   2650   
  2589   2651                   IntPtr pTblName = IntPtr.Zero;
  2590   2652                   SQLiteAuthorizerActionCode op = SQLiteAuthorizerActionCode.None;
  2591   2653                   int bIndirect = 0;
  2592   2654                   int nColumns = 0;
  2593   2655   
  2594   2656                   SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_op(
  2595         -                    iterator.GetHandle(), ref pTblName, ref nColumns, ref op,
         2657  +                    iterator.GetIntPtr(), ref pTblName, ref nColumns, ref op,
  2596   2658                       ref bIndirect);
  2597   2659   
  2598   2660                   if (rc != SQLiteErrorCode.Ok)
  2599   2661                       throw new SQLiteException(rc, "sqlite3changeset_op");
  2600   2662   
  2601   2663                   tableName = SQLiteString.StringFromUtf8IntPtr(pTblName);
  2602   2664                   numberOfColumns = nColumns;
................................................................................
  2613   2675               {
  2614   2676                   CheckIterator();
  2615   2677   
  2616   2678                   IntPtr pPrimaryKeys = IntPtr.Zero;
  2617   2679                   int nColumns = 0;
  2618   2680   
  2619   2681                   SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_pk(
  2620         -                    iterator.GetHandle(), ref pPrimaryKeys, ref nColumns);
         2682  +                    iterator.GetIntPtr(), ref pPrimaryKeys, ref nColumns);
  2621   2683   
  2622   2684                   if (rc != SQLiteErrorCode.Ok)
  2623   2685                       throw new SQLiteException(rc, "sqlite3changeset_pk");
  2624   2686   
  2625   2687                   byte[] bytes = SQLiteBytes.FromIntPtr(pPrimaryKeys, nColumns);
  2626   2688   
  2627   2689                   if (bytes != null)
................................................................................
  2642   2704               {
  2643   2705                   CheckIterator();
  2644   2706   
  2645   2707                   int conflicts = 0;
  2646   2708   
  2647   2709                   SQLiteErrorCode rc =
  2648   2710                       UnsafeNativeMethods.sqlite3changeset_fk_conflicts(
  2649         -                        iterator.GetHandle(), ref conflicts);
         2711  +                        iterator.GetIntPtr(), ref conflicts);
  2650   2712   
  2651   2713                   if (rc != SQLiteErrorCode.Ok)
  2652   2714                   {
  2653   2715                       throw new SQLiteException(rc,
  2654   2716                           "sqlite3changeset_fk_conflicts");
  2655   2717                   }
  2656   2718   
................................................................................
  2752   2814           {
  2753   2815               CheckDisposed();
  2754   2816               CheckIterator();
  2755   2817   
  2756   2818               IntPtr pValue = IntPtr.Zero;
  2757   2819   
  2758   2820               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_old(
  2759         -                iterator.GetHandle(), columnIndex, ref pValue);
         2821  +                iterator.GetIntPtr(), columnIndex, ref pValue);
  2760   2822   
  2761   2823               return SQLiteValue.FromIntPtr(pValue);
  2762   2824           }
  2763   2825   
  2764   2826           ///////////////////////////////////////////////////////////////////////
  2765   2827   
  2766   2828           public SQLiteValue GetNewValue(
................................................................................
  2769   2831           {
  2770   2832               CheckDisposed();
  2771   2833               CheckIterator();
  2772   2834   
  2773   2835               IntPtr pValue = IntPtr.Zero;
  2774   2836   
  2775   2837               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_new(
  2776         -                iterator.GetHandle(), columnIndex, ref pValue);
         2838  +                iterator.GetIntPtr(), columnIndex, ref pValue);
  2777   2839   
  2778   2840               return SQLiteValue.FromIntPtr(pValue);
  2779   2841           }
  2780   2842   
  2781   2843           ///////////////////////////////////////////////////////////////////////
  2782   2844   
  2783   2845           public SQLiteValue GetConflictValue(
................................................................................
  2786   2848           {
  2787   2849               CheckDisposed();
  2788   2850               CheckIterator();
  2789   2851   
  2790   2852               IntPtr pValue = IntPtr.Zero;
  2791   2853   
  2792   2854               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_conflict(
  2793         -                iterator.GetHandle(), columnIndex, ref pValue);
         2855  +                iterator.GetIntPtr(), columnIndex, ref pValue);
  2794   2856   
  2795   2857               return SQLiteValue.FromIntPtr(pValue);
  2796   2858           }
  2797   2859           #endregion
  2798   2860   
  2799   2861           ///////////////////////////////////////////////////////////////////////
  2800   2862