public static void removeSentMessage(long timestamp) { lock (lockObj) { using (HikeMqttPersistenceDb context = new HikeMqttPersistenceDb(App.MqttDBConnectionstring)) { List <HikePacket> entriesToDelete = DbCompiledQueries.GetMqttMsgForTimestamp(context, timestamp).ToList(); if (entriesToDelete == null || entriesToDelete.Count == 0) { return; } context.mqttMessages.DeleteAllOnSubmit <HikePacket>(entriesToDelete); try { context.SubmitChanges(ConflictMode.ContinueOnConflict); Debug.WriteLine("Removed unsent packet with timestamp :: " + timestamp); } catch (ChangeConflictException e) { Debug.WriteLine(e.Message); Debug.WriteLine("Failed to remove unsent packet with timestamp :: " + timestamp); // Automerge database values for members that client // has not modified. foreach (ObjectChangeConflict occ in context.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } // Submit succeeds on second try. context.SubmitChanges(ConflictMode.FailOnFirstConflict); } } }
/// <summary> /// Retrives all messages thet were unsent previously, and are required to send when connection re-establishes /// deletes pending messages from db after reading /// </summary> /// <returns></returns> public static List <HikePacket> getAllSentMessages() { try { List <HikePacket> res; using (HikeMqttPersistenceDb context = new HikeMqttPersistenceDb(App.MqttDBConnectionstring)) { res = DbCompiledQueries.GetAllSentMessages(context).ToList <HikePacket>(); //context.mqttMessages.DeleteAllOnSubmit(context.mqttMessages); //context.SubmitChanges(); } return((res == null || res.Count() == 0) ? null : res); } catch (Exception e) { Debug.WriteLine("Exception while fetching MQTT msgs : " + e.StackTrace); return(null); } }
public static void UpdateToVersionOne() { using (HikeMqttPersistenceDb context = new HikeMqttPersistenceDb(App.MqttDBConnectionstring)) { DatabaseSchemaUpdater schemaUpdater = context.CreateDatabaseSchemaUpdater(); // get current database schema version // if not changed the version is 0 by default int version = schemaUpdater.DatabaseSchemaVersion; // if current version of database schema is old if (version == 0) { // add Address column to the table corresponding to the Person class // IMPORTANT: update database schema version before calling Execute schemaUpdater.DatabaseSchemaVersion = Latest_Version; // execute changes to database schema schemaUpdater.Execute(); } } }
public static void addSentMessage(HikePacket packet) { if (packet.Message != null && packet.Message.Length < 8000) { lock (lockObj) { try { HikePacket mqttMessage = new HikePacket(packet.MessageId, packet.Message, packet.Timestamp); using (HikeMqttPersistenceDb context = new HikeMqttPersistenceDb(App.MqttDBConnectionstring)) { context.mqttMessages.InsertOnSubmit(mqttMessage); context.SubmitChanges(); } } catch (Exception e) { Debug.WriteLine("Failed to add unsent packet with id :: " + packet.MessageId); } } } //TODO update observable list }
public static void deleteAllUnsentMessages() { using (HikeMqttPersistenceDb context = new HikeMqttPersistenceDb(App.MqttDBConnectionstring)) { context.mqttMessages.DeleteAllOnSubmit <HikePacket>(context.GetTable <HikePacket>()); try { context.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException e) { Debug.WriteLine(e.Message); // Automerge database values for members that client // has not modified. foreach (ObjectChangeConflict occ in context.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } // Submit succeeds on second try. context.SubmitChanges(ConflictMode.FailOnFirstConflict); } }
public static void clearDatabase() { #region DELETE CONVS,CHAT MSGS, GROUPS, GROUP MEMBERS,THUMBNAILS ConversationTableUtils.deleteAllConversations(); DeleteAllThumbnails(); DeleteAllAttachmentData(); GroupManager.Instance.DeleteAllGroups(); using (HikeChatsDb context = new HikeChatsDb(App.MsgsDBConnectionstring)) { context.messages.DeleteAllOnSubmit <ConvMessage>(context.GetTable <ConvMessage>()); context.groupInfo.DeleteAllOnSubmit <GroupInfo>(context.GetTable <GroupInfo>()); try { context.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException e) { Debug.WriteLine(e.Message); // Automerge database values for members that client // has not modified. foreach (ObjectChangeConflict occ in context.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } // Submit succeeds on second try. context.SubmitChanges(ConflictMode.FailOnFirstConflict); } #endregion #region DELETE USERS, BLOCKLIST using (HikeUsersDb context = new HikeUsersDb(App.UsersDBConnectionstring)) { context.blockedUsersTable.DeleteAllOnSubmit <Blocked>(context.GetTable <Blocked>()); context.users.DeleteAllOnSubmit <ContactInfo>(context.GetTable <ContactInfo>()); try { context.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException e) { Debug.WriteLine(e.Message); // Automerge database values for members that client // has not modified. foreach (ObjectChangeConflict occ in context.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } // Submit succeeds on second try. context.SubmitChanges(ConflictMode.FailOnFirstConflict); } #endregion #region DELETE MQTTPERSISTED MESSAGES using (HikeMqttPersistenceDb context = new HikeMqttPersistenceDb(App.MqttDBConnectionstring)) { context.mqttMessages.DeleteAllOnSubmit <HikePacket>(context.GetTable <HikePacket>()); try { context.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException e) { Debug.WriteLine(e.Message); // Automerge database values for members that client // has not modified. foreach (ObjectChangeConflict occ in context.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } // Submit succeeds on second try. context.SubmitChanges(ConflictMode.FailOnFirstConflict); } #endregion #region DELETE FAVOURITES AND PENDING REQUESTS DeleteFavourites(); DeletePendingRequests(); #endregion }
public static void createDatabaseAsync() { if (App.appSettings.Contains(App.IS_DB_CREATED)) // shows db are created { return; } BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += (s, e) => { try { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!string.IsNullOrEmpty(MiscDBUtil.THUMBNAILS) && !store.DirectoryExists(MiscDBUtil.THUMBNAILS)) { store.CreateDirectory(MiscDBUtil.THUMBNAILS); } if (!string.IsNullOrEmpty(MiscDBUtil.MISC_DIR) && !store.DirectoryExists(MiscDBUtil.MISC_DIR)) { store.CreateDirectory(MiscDBUtil.MISC_DIR); } if (!store.DirectoryExists(ConversationTableUtils.CONVERSATIONS_DIRECTORY)) { store.CreateDirectory(ConversationTableUtils.CONVERSATIONS_DIRECTORY); } if (!store.DirectoryExists(HikeConstants.SHARED_FILE_LOCATION)) { store.CreateDirectory(HikeConstants.SHARED_FILE_LOCATION); } if (!store.DirectoryExists(HikeConstants.ANALYTICS_OBJECT_DIRECTORY)) { store.CreateDirectory(HikeConstants.ANALYTICS_OBJECT_DIRECTORY); } } // Create the database if it does not exist. Stopwatch st = Stopwatch.StartNew(); using (HikeChatsDb db = new HikeChatsDb(MsgsDBConnectionstring)) { if (db.DatabaseExists() == false) { db.CreateDatabase(); } } using (HikeUsersDb db = new HikeUsersDb(UsersDBConnectionstring)) { if (db.DatabaseExists() == false) { db.CreateDatabase(); } } using (HikeMqttPersistenceDb db = new HikeMqttPersistenceDb(MqttDBConnectionstring)) { if (db.DatabaseExists() == false) { db.CreateDatabase(); } } WriteToIsoStorageSettings(App.IS_DB_CREATED, true); st.Stop(); long msec = st.ElapsedMilliseconds; Debug.WriteLine("APP: Time to create Dbs : {0}", msec); } catch { RemoveKeyFromAppSettings(App.IS_DB_CREATED); } }; bw.RunWorkerAsync(); }