System.Data.SQLite

Check-in [a16ea17d38]
Login

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

Overview
Comment:Further refinements.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: a16ea17d383872c9820e82933e0eed5531d28ef5
User & Date: mistachkin 2017-10-05 22:33:56.092
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
Unified Diff Show Whitespace Changes Patch
Changes to System.Data.SQLite/SQLiteSessions.cs.
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    }
    #endregion

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

    #region Session Extension Delegates
    public delegate bool SessionTableFilterCallback(
        object context,
        string name
    );

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

    public delegate SQLiteChangeSetConflictResult SessionConflictCallback(
        object context,
        SQLiteChangeSetConflictType type,
        ISQLiteChangeSetMetadataItem item
    );
    #endregion

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








|






|







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    }
    #endregion

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

    #region Session Extension Delegates
    public delegate bool SessionTableFilterCallback(
        object clientData,
        string name
    );

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

    public delegate SQLiteChangeSetConflictResult SessionConflictCallback(
        object clientData,
        SQLiteChangeSetConflictType type,
        ISQLiteChangeSetMetadataItem item
    );
    #endregion

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

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    public interface ISQLiteChangeGroup
    {
        void AddChangeSet(byte[] rawData);
        void AddChangeSet(Stream stream);

        void CreateChangeSet(ref byte[] rawData);
        void CreateChangeSet(Stream stream, SQLiteConnectionFlags flags);
    }

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

    public interface ISQLiteChangeSetMetadataItem
    {
        string TableName { get; }







|







65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    public interface ISQLiteChangeGroup
    {
        void AddChangeSet(byte[] rawData);
        void AddChangeSet(Stream stream);

        void CreateChangeSet(ref byte[] rawData);
        void CreateChangeSet(Stream stream);
    }

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

    public interface ISQLiteChangeSetMetadataItem
    {
        string TableName { get; }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

        bool IsEmpty();

        void AttachTable(string name);

        void SetTableFilter(
            SessionTableFilterCallback callback,
            object context
        );

        void CreateChangeSet(ref byte[] rawData);
        void CreateChangeSet(Stream stream, SQLiteConnectionFlags flags);

        void CreatePatchSet(ref byte[] rawData);
        void CreatePatchSet(Stream stream, SQLiteConnectionFlags flags);

        void LoadDifferencesFromTable(
            string fromDatabaseName,
            string tableName
        );

        void ApplyChangeSet(
            byte[] rawData,
            SessionConflictCallback conflictCallback,
            object context
        );

        void ApplyChangeSet(
            Stream stream,
            SessionConflictCallback conflictCallback,
            SessionTableFilterCallback tableFilterCallback,
            object context
        );
    }
    #endregion

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

    #region SQLiteChangeSetIterator Class







|



|


|









|






|







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

        bool IsEmpty();

        void AttachTable(string name);

        void SetTableFilter(
            SessionTableFilterCallback callback,
            object clientData
        );

        void CreateChangeSet(ref byte[] rawData);
        void CreateChangeSet(Stream stream);

        void CreatePatchSet(ref byte[] rawData);
        void CreatePatchSet(Stream stream);

        void LoadDifferencesFromTable(
            string fromDatabaseName,
            string tableName
        );

        void ApplyChangeSet(
            byte[] rawData,
            SessionConflictCallback conflictCallback,
            object clientData
        );

        void ApplyChangeSet(
            Stream stream,
            SessionConflictCallback conflictCallback,
            SessionTableFilterCallback tableFilterCallback,
            object clientData
        );
    }
    #endregion

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

    #region SQLiteChangeSetIterator Class
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
        #region Private Data
        private SQLiteConnection connection;
        private IntPtr session;

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

        private SessionTableFilterCallback tableFilterCallback;
        private object tableFilterContext;
        #endregion

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

        #region Public Constructors
        public SQLiteSession(
            SQLiteConnection connection,







|







556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
        #region Private Data
        private SQLiteConnection connection;
        private IntPtr session;

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

        private SessionTableFilterCallback tableFilterCallback;
        private object tableFilterClientData;
        #endregion

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

        #region Public Constructors
        public SQLiteSession(
            SQLiteConnection connection,
587
588
589
590
591
592
593







594
595
596
597
598
599
600
601
602
603
604
605
606
607
        {
            if (session == IntPtr.Zero)
                throw new InvalidOperationException("session is not open");
        }

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








        #region Native Callback Methods
        private int xFilter(
            IntPtr context, /* NOT USED */
            byte[] tblName
            )
        {
            return tableFilterCallback(tableFilterContext,
                SQLiteString.GetStringFromUtf8Bytes(tblName)) ? 1 : 0;
        }
        #endregion
        #endregion

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








>
>
>
>
>
>
>






|







587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
        {
            if (session == IntPtr.Zero)
                throw new InvalidOperationException("session is not open");
        }

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

        private SQLiteConnectionFlags GetConnectionFlags()
        {
            return SQLiteConnectionFlags.None;
        }

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

        #region Native Callback Methods
        private int xFilter(
            IntPtr context, /* NOT USED */
            byte[] tblName
            )
        {
            return tableFilterCallback(tableFilterClientData,
                SQLiteString.GetStringFromUtf8Bytes(tblName)) ? 1 : 0;
        }
        #endregion
        #endregion

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

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
                throw new SQLiteException(rc, "sqlite3session_attach");
        }

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

        public void SetTableFilter(
            SessionTableFilterCallback callback,
            object context
            )
        {
            CheckDisposed();
            CheckHandle();

            this.tableFilterCallback = callback;
            this.tableFilterContext = context;

            UnsafeNativeMethods.sqlite3session_table_filter(
                session, xFilter, IntPtr.Zero);
        }

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








|






|







695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
                throw new SQLiteException(rc, "sqlite3session_attach");
        }

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

        public void SetTableFilter(
            SessionTableFilterCallback callback,
            object clientData
            )
        {
            CheckDisposed();
            CheckHandle();

            this.tableFilterCallback = callback;
            this.tableFilterClientData = clientData;

            UnsafeNativeMethods.sqlite3session_table_filter(
                session, xFilter, IntPtr.Zero);
        }

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

737
738
739
740
741
742
743
744
745
746
747
748
749


750
751
752
753
754
755
756
                }
            }
        }

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

        public void CreateChangeSet(
            Stream stream,
            SQLiteConnectionFlags flags
            )
        {
            CheckDisposed();
            CheckHandle();



            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3session_changeset_strm(
                session, new SQLiteStreamAdapter(stream, flags).xOutput,
                IntPtr.Zero);

            if (rc != SQLiteErrorCode.Ok)
                throw new SQLiteException(rc, "sqlite3session_changeset_strm");







|
<




>
>







744
745
746
747
748
749
750
751

752
753
754
755
756
757
758
759
760
761
762
763
764
                }
            }
        }

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

        public void CreateChangeSet(
            Stream stream

            )
        {
            CheckDisposed();
            CheckHandle();

            SQLiteConnectionFlags flags = GetConnectionFlags();

            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3session_changeset_strm(
                session, new SQLiteStreamAdapter(stream, flags).xOutput,
                IntPtr.Zero);

            if (rc != SQLiteErrorCode.Ok)
                throw new SQLiteException(rc, "sqlite3session_changeset_strm");
788
789
790
791
792
793
794
795
796
797
798
799
800


801
802
803
804
805
806
807
                }
            }
        }

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

        public void CreatePatchSet(
            Stream stream,
            SQLiteConnectionFlags flags
            )
        {
            CheckDisposed();
            CheckHandle();



            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3session_patchset_strm(
                session, new SQLiteStreamAdapter(stream, flags).xOutput,
                IntPtr.Zero);

            if (rc != SQLiteErrorCode.Ok)
                throw new SQLiteException(rc, "sqlite3session_patchset_strm");







|
<




>
>







796
797
798
799
800
801
802
803

804
805
806
807
808
809
810
811
812
813
814
815
816
                }
            }
        }

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

        public void CreatePatchSet(
            Stream stream

            )
        {
            CheckDisposed();
            CheckHandle();

            SQLiteConnectionFlags flags = GetConnectionFlags();

            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3session_patchset_strm(
                session, new SQLiteStreamAdapter(stream, flags).xOutput,
                IntPtr.Zero);

            if (rc != SQLiteErrorCode.Ok)
                throw new SQLiteException(rc, "sqlite3session_patchset_strm");
861
862
863
864
865
866
867


868
869
























































870
871

































872




873
874
875
876
877
878
879
            byte[] rawData,
            SessionConflictCallback conflictCallback,
            object clientData
            )
        {
            CheckDisposed();



            int nData = 0;
            IntPtr pData = IntPtr.Zero;
























































            SQLiteConnectionFlags flags = connection.Flags;


































            UnsafeNativeMethods.xSessionConflict xConflict = new UnsafeNativeMethods.xSessionConflict(delegate(IntPtr context, SQLiteChangeSetConflictType type, IntPtr iterator)




            {
                ISQLiteChangeSetMetadataItem item = null;

                try
                {
                    return conflictCallback(clientData, type, item);
                }







>
>


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

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







870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
            byte[] rawData,
            SessionConflictCallback conflictCallback,
            object clientData
            )
        {
            CheckDisposed();

            SQLiteConnectionFlags flags = GetConnectionFlags();

            int nData = 0;
            IntPtr pData = IntPtr.Zero;

            UnsafeNativeMethods.xSessionConflict xConflict =
                new UnsafeNativeMethods.xSessionConflict(
                    delegate(IntPtr context,
                             SQLiteChangeSetConflictType type,
                             IntPtr iterator)
            {
                ISQLiteChangeSetMetadataItem item = null;

                try
                {
                    return conflictCallback(clientData, type, item);
                }
                catch (Exception e)
                {
                    try
                    {
                        if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
                                SQLiteConnectionFlags.LogCallbackException)
                        {
                            SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
                                HelperMethods.StringFormat(CultureInfo.CurrentCulture,
                                "Caught exception in \"xConflict\" method: {0}",
                                e)); /* throw */
                        }
                    }
                    catch
                    {
                        // do nothing.
                    }
                }

                return SQLiteChangeSetConflictResult.Abort;
            });

            SQLiteConnectionHandle handle = SQLiteConnection.GetNativeHandle(
                connection);

            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply(
                handle, nData, pData, null, xConflict, IntPtr.Zero);

            if (rc != SQLiteErrorCode.Ok)
                throw new SQLiteException(rc, "sqlite3changeset_apply");
        }

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

        public void ApplyChangeSet(
            Stream stream,
            SessionConflictCallback conflictCallback,
            SessionTableFilterCallback tableFilterCallback,
            object clientData
            )
        {
            CheckDisposed();

            SQLiteConnectionFlags flags = GetConnectionFlags();

            UnsafeNativeMethods.xSessionFilter xFilter =
                new UnsafeNativeMethods.xSessionFilter(
                    delegate(IntPtr context, byte[] tblName)
            {
                try
                {
                    string name = SQLiteString.GetStringFromUtf8Bytes(
                        tblName);

                    return tableFilterCallback(clientData, name) ? 1 : 0;
                }
                catch (Exception e)
                {
                    try
                    {
                        if ((flags & SQLiteConnectionFlags.LogCallbackException) ==
                                SQLiteConnectionFlags.LogCallbackException)
                        {
                            SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
                                HelperMethods.StringFormat(CultureInfo.CurrentCulture,
                                "Caught exception in \"xFilter\" method: {0}",
                                e)); /* throw */
                        }
                    }
                    catch
                    {
                        // do nothing.
                    }
                }

                return 0;
            });

            UnsafeNativeMethods.xSessionConflict xConflict =
                new UnsafeNativeMethods.xSessionConflict(
                    delegate(IntPtr context,
                             SQLiteChangeSetConflictType type,
                             IntPtr iterator)
            {
                ISQLiteChangeSetMetadataItem item = null;

                try
                {
                    return conflictCallback(clientData, type, item);
                }
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
                        // do nothing.
                    }
                }

                return SQLiteChangeSetConflictResult.Abort;
            });

            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply(
                SQLiteConnection.GetNativeHandle(connection), nData, pData,
                null, xConflict, IntPtr.Zero);
        }

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

        public void ApplyChangeSet(
            Stream stream,
            SessionConflictCallback conflictCallback,
            SessionTableFilterCallback tableFilterCallback,
            object context
            )
        {
            CheckDisposed();

            throw new NotImplementedException();
        }
        #endregion

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

        #region IDisposable Members
        public void Dispose()







<
|
|
|
|
<
|
<
<
|
<
<
|
<
<
|
|







999
1000
1001
1002
1003
1004
1005

1006
1007
1008
1009

1010


1011


1012


1013
1014
1015
1016
1017
1018
1019
1020
1021
                        // do nothing.
                    }
                }

                return SQLiteChangeSetConflictResult.Abort;
            });


            SQLiteConnectionHandle handle = SQLiteConnection.GetNativeHandle(
                connection);

            SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3changeset_apply_strm(

                handle, new SQLiteStreamAdapter(stream, flags).xInput,


                IntPtr.Zero, xFilter, xConflict, IntPtr.Zero);





            if (rc != SQLiteErrorCode.Ok)
                throw new SQLiteException(rc, "sqlite3changeset_apply_strm");
        }
        #endregion

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

        #region IDisposable Members
        public void Dispose()