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

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

Overview
Comment:Further refinements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: a16ea17d383872c9820e82933e0eed5531d28ef5
User & Date: mistachkin 2017-10-05 22:33:56
Context
2017-10-05
22:55
The 'Apply' methods logically belong to the change set, not the session. check-in: baed0aa503 user: mistachkin tags: sessions
22:33
Further refinements. check-in: a16ea17d38 user: mistachkin tags: sessions
22:19
Fix some inconsistencies in the API, minor cleanup. check-in: 520bab9c18 user: mistachkin tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

    33     33       }
    34     34       #endregion
    35     35   
    36     36       ///////////////////////////////////////////////////////////////////////////
    37     37   
    38     38       #region Session Extension Delegates
    39     39       public delegate bool SessionTableFilterCallback(
    40         -        object context,
           40  +        object clientData,
    41     41           string name
    42     42       );
    43     43   
    44     44       ///////////////////////////////////////////////////////////////////////////
    45     45   
    46     46       public delegate SQLiteChangeSetConflictResult SessionConflictCallback(
    47         -        object context,
           47  +        object clientData,
    48     48           SQLiteChangeSetConflictType type,
    49     49           ISQLiteChangeSetMetadataItem item
    50     50       );
    51     51       #endregion
    52     52   
    53     53       ///////////////////////////////////////////////////////////////////////////
    54     54   
................................................................................
    65     65   
    66     66       public interface ISQLiteChangeGroup
    67     67       {
    68     68           void AddChangeSet(byte[] rawData);
    69     69           void AddChangeSet(Stream stream);
    70     70   
    71     71           void CreateChangeSet(ref byte[] rawData);
    72         -        void CreateChangeSet(Stream stream, SQLiteConnectionFlags flags);
           72  +        void CreateChangeSet(Stream stream);
    73     73       }
    74     74   
    75     75       ///////////////////////////////////////////////////////////////////////////
    76     76   
    77     77       public interface ISQLiteChangeSetMetadataItem
    78     78       {
    79     79           string TableName { get; }
................................................................................
   104    104   
   105    105           bool IsEmpty();
   106    106   
   107    107           void AttachTable(string name);
   108    108   
   109    109           void SetTableFilter(
   110    110               SessionTableFilterCallback callback,
   111         -            object context
          111  +            object clientData
   112    112           );
   113    113   
   114    114           void CreateChangeSet(ref byte[] rawData);
   115         -        void CreateChangeSet(Stream stream, SQLiteConnectionFlags flags);
          115  +        void CreateChangeSet(Stream stream);
   116    116   
   117    117           void CreatePatchSet(ref byte[] rawData);
   118         -        void CreatePatchSet(Stream stream, SQLiteConnectionFlags flags);
          118  +        void CreatePatchSet(Stream stream);
   119    119   
   120    120           void LoadDifferencesFromTable(
   121    121               string fromDatabaseName,
   122    122               string tableName
   123    123           );
   124    124   
   125    125           void ApplyChangeSet(
   126    126               byte[] rawData,
   127    127               SessionConflictCallback conflictCallback,
   128         -            object context
          128  +            object clientData
   129    129           );
   130    130   
   131    131           void ApplyChangeSet(
   132    132               Stream stream,
   133    133               SessionConflictCallback conflictCallback,
   134    134               SessionTableFilterCallback tableFilterCallback,
   135         -            object context
          135  +            object clientData
   136    136           );
   137    137       }
   138    138       #endregion
   139    139   
   140    140       ///////////////////////////////////////////////////////////////////////////
   141    141   
   142    142       #region SQLiteChangeSetIterator Class
................................................................................
   556    556           #region Private Data
   557    557           private SQLiteConnection connection;
   558    558           private IntPtr session;
   559    559   
   560    560           ///////////////////////////////////////////////////////////////////////
   561    561   
   562    562           private SessionTableFilterCallback tableFilterCallback;
   563         -        private object tableFilterContext;
          563  +        private object tableFilterClientData;
   564    564           #endregion
   565    565   
   566    566           ///////////////////////////////////////////////////////////////////////
   567    567   
   568    568           #region Public Constructors
   569    569           public SQLiteSession(
   570    570               SQLiteConnection connection,
................................................................................
   587    587           {
   588    588               if (session == IntPtr.Zero)
   589    589                   throw new InvalidOperationException("session is not open");
   590    590           }
   591    591   
   592    592           ///////////////////////////////////////////////////////////////////////
   593    593   
          594  +        private SQLiteConnectionFlags GetConnectionFlags()
          595  +        {
          596  +            return SQLiteConnectionFlags.None;
          597  +        }
          598  +
          599  +        ///////////////////////////////////////////////////////////////////////
          600  +
   594    601           #region Native Callback Methods
   595    602           private int xFilter(
   596    603               IntPtr context, /* NOT USED */
   597    604               byte[] tblName
   598    605               )
   599    606           {
   600         -            return tableFilterCallback(tableFilterContext,
          607  +            return tableFilterCallback(tableFilterClientData,
   601    608                   SQLiteString.GetStringFromUtf8Bytes(tblName)) ? 1 : 0;
   602    609           }
   603    610           #endregion
   604    611           #endregion
   605    612   
   606    613           ///////////////////////////////////////////////////////////////////////
   607    614   
................................................................................
   688    695                   throw new SQLiteException(rc, "sqlite3session_attach");
   689    696           }
   690    697   
   691    698           ///////////////////////////////////////////////////////////////////////
   692    699   
   693    700           public void SetTableFilter(
   694    701               SessionTableFilterCallback callback,
   695         -            object context
          702  +            object clientData
   696    703               )
   697    704           {
   698    705               CheckDisposed();
   699    706               CheckHandle();
   700    707   
   701    708               this.tableFilterCallback = callback;
   702         -            this.tableFilterContext = context;
          709  +            this.tableFilterClientData = clientData;
   703    710   
   704    711               UnsafeNativeMethods.sqlite3session_table_filter(
   705    712                   session, xFilter, IntPtr.Zero);
   706    713           }
   707    714   
   708    715           ///////////////////////////////////////////////////////////////////////
   709    716   
................................................................................
   737    744                   }
   738    745               }
   739    746           }
   740    747   
   741    748           ///////////////////////////////////////////////////////////////////////
   742    749   
   743    750           public void CreateChangeSet(
   744         -            Stream stream,
   745         -            SQLiteConnectionFlags flags
          751  +            Stream stream
   746    752               )
   747    753           {
   748    754               CheckDisposed();
   749    755               CheckHandle();
          756  +
          757  +            SQLiteConnectionFlags flags = GetConnectionFlags();
   750    758   
   751    759               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3session_changeset_strm(
   752    760                   session, new SQLiteStreamAdapter(stream, flags).xOutput,
   753    761                   IntPtr.Zero);
   754    762   
   755    763               if (rc != SQLiteErrorCode.Ok)
   756    764                   throw new SQLiteException(rc, "sqlite3session_changeset_strm");
................................................................................
   788    796                   }
   789    797               }
   790    798           }
   791    799   
   792    800           ///////////////////////////////////////////////////////////////////////
   793    801   
   794    802           public void CreatePatchSet(
   795         -            Stream stream,
   796         -            SQLiteConnectionFlags flags
          803  +            Stream stream
   797    804               )
   798    805           {
   799    806               CheckDisposed();
   800    807               CheckHandle();
          808  +
          809  +            SQLiteConnectionFlags flags = GetConnectionFlags();
   801    810   
   802    811               SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3session_patchset_strm(
   803    812                   session, new SQLiteStreamAdapter(stream, flags).xOutput,
   804    813                   IntPtr.Zero);
   805    814   
   806    815               if (rc != SQLiteErrorCode.Ok)
   807    816                   throw new SQLiteException(rc, "sqlite3session_patchset_strm");
................................................................................
   861    870               byte[] rawData,
   862    871               SessionConflictCallback conflictCallback,
   863    872               object clientData
   864    873               )
   865    874           {
   866    875               CheckDisposed();
   867    876   
          877  +            SQLiteConnectionFlags flags = GetConnectionFlags();
          878  +
   868    879               int nData = 0;
   869    880               IntPtr pData = IntPtr.Zero;
   870         -            SQLiteConnectionFlags flags = connection.Flags;
   871    881   
   872         -            UnsafeNativeMethods.xSessionConflict xConflict = new UnsafeNativeMethods.xSessionConflict(delegate(IntPtr context, SQLiteChangeSetConflictType type, IntPtr iterator)
          882  +            UnsafeNativeMethods.xSessionConflict xConflict =
          883  +                new UnsafeNativeMethods.xSessionConflict(
          884  +                    delegate(IntPtr context,
          885  +                             SQLiteChangeSetConflictType type,
          886  +                             IntPtr iterator)
          887  +            {
          888  +                ISQLiteChangeSetMetadataItem item = null;
          889  +
          890  +                try
          891  +                {
          892  +                    return conflictCallback(clientData, type, item);
          893  +                }
          894  +                catch (Exception e)
          895  +                {
          896  +                    try
          897  +                    {
          898  +                        if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
          899  +                                SQLiteConnectionFlags.LogCallbackException)
          900  +                        {
          901  +                            SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
          902  +                                HelperMethods.StringFormat(CultureInfo.CurrentCulture,
          903  +                                "Caught exception in \"xConflict\" method: {0}",
          904  +                                e)); /* throw */
          905  +                        }
          906  +                    }
          907  +                    catch
          908  +                    {
          909  +                        // do nothing.
          910  +                    }
          911  +                }
          912  +
          913  +                return SQLiteChangeSetConflictResult.Abort;
          914  +            });
          915  +
          916  +            SQLiteConnectionHandle handle = SQLiteConnection.GetNativeHandle(
          917  +                connection);
          918  +
          919  +            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply(
          920  +                handle, nData, pData, null, xConflict, IntPtr.Zero);
          921  +
          922  +            if (rc != SQLiteErrorCode.Ok)
          923  +                throw new SQLiteException(rc, "sqlite3changeset_apply");
          924  +        }
          925  +
          926  +        ///////////////////////////////////////////////////////////////////////
          927  +
          928  +        public void ApplyChangeSet(
          929  +            Stream stream,
          930  +            SessionConflictCallback conflictCallback,
          931  +            SessionTableFilterCallback tableFilterCallback,
          932  +            object clientData
          933  +            )
          934  +        {
          935  +            CheckDisposed();
          936  +
          937  +            SQLiteConnectionFlags flags = GetConnectionFlags();
          938  +
          939  +            UnsafeNativeMethods.xSessionFilter xFilter =
          940  +                new UnsafeNativeMethods.xSessionFilter(
          941  +                    delegate(IntPtr context, byte[] tblName)
          942  +            {
          943  +                try
          944  +                {
          945  +                    string name = SQLiteString.GetStringFromUtf8Bytes(
          946  +                        tblName);
          947  +
          948  +                    return tableFilterCallback(clientData, name) ? 1 : 0;
          949  +                }
          950  +                catch (Exception e)
          951  +                {
          952  +                    try
          953  +                    {
          954  +                        if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
          955  +                                SQLiteConnectionFlags.LogCallbackException)
          956  +                        {
          957  +                            SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
          958  +                                HelperMethods.StringFormat(CultureInfo.CurrentCulture,
          959  +                                "Caught exception in \"xFilter\" method: {0}",
          960  +                                e)); /* throw */
          961  +                        }
          962  +                    }
          963  +                    catch
          964  +                    {
          965  +                        // do nothing.
          966  +                    }
          967  +                }
          968  +
          969  +                return 0;
          970  +            });
          971  +
          972  +            UnsafeNativeMethods.xSessionConflict xConflict =
          973  +                new UnsafeNativeMethods.xSessionConflict(
          974  +                    delegate(IntPtr context,
          975  +                             SQLiteChangeSetConflictType type,
          976  +                             IntPtr iterator)
   873    977               {
   874    978                   ISQLiteChangeSetMetadataItem item = null;
   875    979   
   876    980                   try
   877    981                   {
   878    982                       return conflictCallback(clientData, type, item);
   879    983                   }
................................................................................
   895    999                           // do nothing.
   896   1000                       }
   897   1001                   }
   898   1002   
   899   1003                   return SQLiteChangeSetConflictResult.Abort;
   900   1004               });
   901   1005   
   902         -            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply(
   903         -                SQLiteConnection.GetNativeHandle(connection), nData, pData,
   904         -                null, xConflict, IntPtr.Zero);
   905         -        }
         1006  +            SQLiteConnectionHandle handle = SQLiteConnection.GetNativeHandle(
         1007  +                connection);
   906   1008   
   907         -        ///////////////////////////////////////////////////////////////////////
         1009  +            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply_strm(
         1010  +                handle, new SQLiteStreamAdapter(stream, flags).xInput,
         1011  +                IntPtr.Zero, xFilter, xConflict, IntPtr.Zero);
   908   1012   
   909         -        public void ApplyChangeSet(
   910         -            Stream stream,
   911         -            SessionConflictCallback conflictCallback,
   912         -            SessionTableFilterCallback tableFilterCallback,
   913         -            object context
   914         -            )
   915         -        {
   916         -            CheckDisposed();
   917         -
   918         -            throw new NotImplementedException();
         1013  +            if (rc != SQLiteErrorCode.Ok)
         1014  +                throw new SQLiteException(rc, "sqlite3changeset_apply_strm");
   919   1015           }
   920   1016           #endregion
   921   1017   
   922   1018           ///////////////////////////////////////////////////////////////////////
   923   1019   
   924   1020           #region IDisposable Members
   925   1021           public void Dispose()