Пример #1
0
        private static void UpdateItems(IDbConnection connection, ISyncableStore store)
        {
            // loop through types and apply updates or inserts
            foreach (var itemType in store.GetItemTypes())
            {
                System.Diagnostics.Debug.WriteLine("Updating itemType = " + itemType);

                IDbCommand getUpdatedItemsCommand = connection.CreateCommand();
                getUpdatedItemsCommand.CommandText =
                    String.Format(
                        "SELECT SyncStatus, GlobalCreatedReplica, CreatedTickCount, GlobalModifiedReplica, ModifiedTickCount, ItemData  FROM SyncItems WHERE SyncStatus IN ({0},{1}) AND ItemType=@ItemType",
                        (int)SyncStatus.Insert, (int)SyncStatus.Update);
                getUpdatedItemsCommand.AddParameter("@ItemType", itemType);

                using (var reader = getUpdatedItemsCommand.ExecuteReader())
                {
                    while (reader != null && reader.Read())
                    {
                        var createdReplicaInfo  = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Created");
                        var modifiedReplicaInfo = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Modified");
                        var itemData            = JObject.Parse((string)reader["ItemData"]);
                        var itemInfo            = new SyncableItemInfo
                        {
                            ItemType = itemType,
                            Created  = createdReplicaInfo,
                            Modified = modifiedReplicaInfo,
                            Deleted  = false
                        };
                        UpdateItem(connection, store, itemInfo, itemData);
                    }
                }
            }
        }
Пример #2
0
        private static void DeleteItems(IDbConnection connection, ISyncableStore store)
        {
            // loop backwards through types and apply deletions
            foreach (var itemType in store.GetItemTypes().Reverse())
            {
                IDbCommand getDeletedItemsCommand = connection.CreateCommand();
                getDeletedItemsCommand.CommandText =
                    String.Format("SELECT * FROM SyncItems WHERE SyncStatus IN ({0},{1}) AND ItemType=@ItemType",
                                  (int)SyncStatus.Delete, (int)SyncStatus.DeleteNonExisting);
                getDeletedItemsCommand.AddParameter("@ItemType", itemType);

                using (var reader = getDeletedItemsCommand.ExecuteReader())
                {
                    while (reader != null && reader.Read())
                    {
                        var createdReplicaInfo  = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Created");
                        var modifiedReplicaInfo = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Modified");
                        var itemInfo            = new SyncableItemInfo
                        {
                            ItemType = itemType,
                            Created  = createdReplicaInfo,
                            Modified = modifiedReplicaInfo,
                            Deleted  = true
                        };
                        store.DeleteItem(itemInfo);
                    }
                }
            }
        }
Пример #3
0
        public static ISyncableItemInfo SyncableItemInfoFromDataReader(IDataReader reader)
        {
            var               createdReplicaInfo  = ReplicaInfoFromDataReader(reader, "Created");
            var               modifiedReplicaInfo = ReplicaInfoFromDataReader(reader, "Modified");
            string            itemType            = reader["ItemType"].ToString();
            var               status   = (SyncStatus)reader["SyncStatus"];
            ISyncableItemInfo itemInfo = new SyncableItemInfo
            {
                ItemType = itemType,
                Created  = createdReplicaInfo,
                Modified = modifiedReplicaInfo,
                Deleted  = (status == SyncStatus.Delete)
            };

            return(itemInfo);
        }
Пример #4
0
        private static void UpdateItem(IDbConnection connection, ISyncableStore store, ISyncableItemInfo itemInfo,
                                       JObject itemData)
        {
            // Make sure all the item refs for this item have already been handled
            foreach (var itemRefJson in itemData["item"]["itemRefs"])
            {
                var referencedItemInfo      = SyncableItemInfoFromJson(itemRefJson);
                var localReferencedItemInfo = store.LocateCurrentItemInfo(referencedItemInfo);
                if (localReferencedItemInfo == null || localReferencedItemInfo.Deleted)
                {
                    var getReferencedItemCommand = connection.CreateCommand();
                    getReferencedItemCommand.CommandText =
                        String.Format(
                            "SELECT SyncStatus, GlobalCreatedReplica, CreatedTickCount, GlobalModifiedReplica, ModifiedTickCount, ItemData  FROM SyncItems WHERE  GlobalCreatedReplica=@GlobalCreatedReplica AND CreatedTickCount=@CreatedTickCount AND ItemType=@ItemType");
                    getReferencedItemCommand.AddParameter("@GlobalCreatedReplica", referencedItemInfo.Created.ReplicaId);
                    getReferencedItemCommand.AddParameter("@CreatedTickCount",
                                                          referencedItemInfo.Created.ReplicaTickCount);
                    getReferencedItemCommand.AddParameter("@ItemType", referencedItemInfo.ItemType);

                    using (var reader = getReferencedItemCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            var createdReplicaInfo         = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Created");
                            var modifiedReplicaInfo        = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Modified");
                            var refItemData                = JObject.Parse((string)reader["ItemData"]);
                            var possiblyUpdatedRefItemInfo = new SyncableItemInfo
                            {
                                ItemType = referencedItemInfo.ItemType,
                                Created  = createdReplicaInfo,
                                Modified = modifiedReplicaInfo,
                                Deleted  = false
                            };
                            UpdateItem(connection, store, possiblyUpdatedRefItemInfo, refItemData);
                        }
                    }
                }
            }
            store.SaveItemData(itemInfo, itemData);
        }