NET C# » History » Version 1
Tomek Dziemidowicz, 2019-07-04 08:51 PM
1 | 1 | Tomek Dziemidowicz | h1. SYNC - .NET C# |
---|---|---|---|
2 | |||
3 | <pre><code class="csharp"> |
||
4 | /// <summary> |
||
5 | /// Get changes from server. |
||
6 | /// </summary> |
||
7 | /// <param name="subscriberId">identifier of subscriber</param> |
||
8 | private void GetChangesFromServer(string subscriberId) |
||
9 | { |
||
10 | using (SQLiteConnection conn = new SQLiteConnection(this.connString)) |
||
11 | { |
||
12 | using (SQLiteCommand cmd = new SQLiteCommand()) |
||
13 | { |
||
14 | cmd.Connection = conn; |
||
15 | conn.Open(); |
||
16 | |||
17 | SQLiteHelper sh = new SQLiteHelper(cmd); |
||
18 | |||
19 | //get list of tables in local database |
||
20 | DataTable tables = sh.Select("select tbl_Name from sqlite_master where type='table';"); |
||
21 | |||
22 | foreach (DataRow table in tables.Rows) |
||
23 | { |
||
24 | try |
||
25 | { |
||
26 | sh.BeginTransaction(); |
||
27 | |||
28 | //get changes from server for current table |
||
29 | var request = new RestRequest("Sync/{subscriberUUID}/{tableName}", Method.GET); |
||
30 | request.AddUrlSegment("subscriberUUID", subscriberId); |
||
31 | request.AddUrlSegment("tableName", table["tbl_Name"].ToString()); |
||
32 | request.AddHeader("Accept", "*/*"); |
||
33 | IRestResponse response = wsClient.Execute(request); |
||
34 | List<DataObject> tablesData = JsonConvert.DeserializeObject<List<DataObject>>(response.Content); |
||
35 | |||
36 | //parse resposonse |
||
37 | foreach (DataObject tableData in tablesData) |
||
38 | if (tableData.SyncId > 0) |
||
39 | { |
||
40 | sh.Execute(tableData.TriggerDeleteDrop); |
||
41 | sh.Execute(tableData.TriggerInsertDrop); |
||
42 | sh.Execute(tableData.TriggerUpdateDrop); |
||
43 | |||
44 | XmlDocument xmlRecords = new XmlDocument(); |
||
45 | xmlRecords.LoadXml(tableData.Records); |
||
46 | XPathNavigator oRecordsPathNavigator = xmlRecords.CreateNavigator(); |
||
47 | XPathNodeIterator oRecordsNodesIterator = oRecordsPathNavigator.Select("/records/r"); |
||
48 | |||
49 | foreach (XPathNavigator oCurrentRecord in oRecordsNodesIterator) |
||
50 | { |
||
51 | string action = oCurrentRecord.GetAttribute("a", ""); |
||
52 | XmlDocument xmlRecord = new XmlDocument(); |
||
53 | xmlRecord.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><columns>" + oCurrentRecord.InnerXml + "</columns>"); |
||
54 | XPathNavigator oColumnsPathNavigator = xmlRecord.CreateNavigator(); |
||
55 | XPathNodeIterator oColumnsNodesIterator = oColumnsPathNavigator.Select("/columns/c"); |
||
56 | int coumnsCount = oColumnsNodesIterator.Count; |
||
57 | |||
58 | SQLiteParameter[] parameters = new SQLiteParameter[coumnsCount]; |
||
59 | int idx = 0; |
||
60 | foreach (XPathNavigator oCurrentColumn in oColumnsNodesIterator) |
||
61 | { |
||
62 | SQLiteParameter parameter = new SQLiteParameter(); |
||
63 | parameter.Value = oCurrentColumn.InnerXml; |
||
64 | parameters[idx] = parameter; |
||
65 | idx++; |
||
66 | } |
||
67 | |||
68 | switch (action) |
||
69 | { |
||
70 | case "1"://insert |
||
71 | sh.Execute(tableData.QueryInsert, parameters); |
||
72 | break; |
||
73 | case "2"://update |
||
74 | sh.Execute(tableData.QueryUpdate, parameters); |
||
75 | break; |
||
76 | case "3"://delete |
||
77 | sh.Execute(tableData.QueryDelete, parameters); |
||
78 | break; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | sh.Execute(tableData.TriggerDelete); |
||
83 | sh.Execute(tableData.TriggerInsert); |
||
84 | sh.Execute(tableData.TriggerUpdate); |
||
85 | |||
86 | //now we need to tell server that we successfully received changes |
||
87 | request = new RestRequest("CommitSync/{syncId}", Method.GET); |
||
88 | request.AddUrlSegment("syncId", tableData.SyncId.ToString()); |
||
89 | request.AddHeader("Accept", "*/*"); |
||
90 | IRestResponse responseCommit = wsClient.Execute(request); |
||
91 | } |
||
92 | |||
93 | sh.Commit(); |
||
94 | |||
95 | } |
||
96 | catch (Exception ex) |
||
97 | { |
||
98 | sh.Rollback(); |
||
99 | throw ex; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | conn.Close(); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | </code></pre> |