From 7c1698388a7598c74b701241892ccd411ab5a866 Mon Sep 17 00:00:00 2001 From: "Michael J. Seiferling" Date: Sat, 14 Nov 2015 14:09:29 -0600 Subject: [PATCH] Sync Overhaul (Tag Support and Efficiency) --- GBM/Classes/clsGameTag.vb | 15 ++ GBM/Classes/clsTag.vb | 15 ++ GBM/Managers/mgrGameTags.vb | 132 +++++++++++++++++- GBM/Managers/mgrMonitorList.vb | 243 ++++++++++++++++----------------- GBM/Managers/mgrTags.vb | 148 ++++++++++++++++++-- 5 files changed, 411 insertions(+), 142 deletions(-) diff --git a/GBM/Classes/clsGameTag.vb b/GBM/Classes/clsGameTag.vb index 626f23d..5fce6e4 100644 --- a/GBM/Classes/clsGameTag.vb +++ b/GBM/Classes/clsGameTag.vb @@ -20,4 +20,19 @@ End Set End Property + Public Function CoreEquals(obj As Object) As Boolean + Dim oGameTag As clsGameTag = TryCast(obj, clsGameTag) + If oGameTag Is Nothing Then + Return False + Else + If TagID <> oGameTag.TagID Then + Return False + End If + If MonitorID <> oGameTag.MonitorID Then + Return False + End If + Return True + End If + End Function + End Class diff --git a/GBM/Classes/clsTag.vb b/GBM/Classes/clsTag.vb index 9dc0952..e4cc1f5 100644 --- a/GBM/Classes/clsTag.vb +++ b/GBM/Classes/clsTag.vb @@ -20,4 +20,19 @@ End Set End Property + Public Function CoreEquals(obj As Object) As Boolean + Dim oTag As clsTag = TryCast(obj, clsTag) + If oTag Is Nothing Then + Return False + Else + If ID <> oTag.ID Then + Return False + End If + If Name <> oTag.Name Then + Return False + End If + Return True + End If + End Function + End Class diff --git a/GBM/Managers/mgrGameTags.vb b/GBM/Managers/mgrGameTags.vb index 353c06a..79812a0 100644 --- a/GBM/Managers/mgrGameTags.vb +++ b/GBM/Managers/mgrGameTags.vb @@ -1,7 +1,7 @@ Public Class mgrGameTags - Public Shared Sub DoGameTagAdd(ByVal oGameTag As clsGameTag) - Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local) + Public Shared Sub DoGameTagAdd(ByVal oGameTag As clsGameTag, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) Dim sSQL As String Dim hshParams As New Hashtable @@ -17,7 +17,7 @@ Dim hshParams As Hashtable Dim oParamList As New List(Of Hashtable) - sSQL = "INSERT INTO gametags VALUES (@TagID, @MonitorID)" + sSQL = "INSERT INTO gametags VALUES (@TagID, @MonitorID);" For Each oGameTag As clsGameTag In oGameTags hshParams = New Hashtable @@ -155,4 +155,130 @@ Return hshList End Function + Public Shared Function ReadGameTags(Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) As Hashtable + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim oData As DataSet + Dim sSQL As String + Dim sCompoundKey As String + Dim hshList As New Hashtable + Dim oGameTag As clsGameTag + + sSQL = "SELECT * from gametags" + oData = oDatabase.ReadParamData(sSQL, New Hashtable) + + For Each dr As DataRow In oData.Tables(0).Rows + oGameTag = New clsGameTag + oGameTag.TagID = CStr(dr(0)) + oGameTag.MonitorID = CStr(dr(1)) + sCompoundKey = oGameTag.TagID & ":" & oGameTag.MonitorID + hshList.Add(sCompoundKey, oGameTag) + Next + + Return hshList + End Function + + Public Shared Sub DoGameTagAddSync(ByVal hshTags As Hashtable, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim sSQL As String + Dim hshParams As Hashtable + Dim oParamList As New List(Of Hashtable) + + sSQL = "INSERT INTO gametags VALUES (@TagID, @MonitorID);" + + For Each oGameTag As clsGameTag In hshTags.Values + hshParams = New Hashtable + hshParams.Add("TagID", oGameTag.TagID) + hshParams.Add("MonitorID", oGameTag.MonitorID) + oParamList.Add(hshParams) + Next + + oDatabase.RunMassParamQuery(sSQL, oParamList) + End Sub + + Public Shared Sub DoGameTagDeleteSync(ByVal hshTags As Hashtable, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim sSQL As String + Dim hshParams As Hashtable + Dim oParamList As New List(Of Hashtable) + + sSQL = "DELETE FROM gametags " + sSQL &= "WHERE TagID = @TagID AND MonitorID = @MonitorID;" + + For Each oGameTag As clsGameTag In hshTags.Values + hshParams = New Hashtable + hshParams.Add("TagID", oGameTag.TagID) + hshParams.Add("MonitorID", oGameTag.MonitorID) + oParamList.Add(hshParams) + Next + + oDatabase.RunMassParamQuery(sSQL, oParamList) + End Sub + + Public Shared Function SyncGameTags(Optional ByVal bToRemote As Boolean = True) As Integer + Dim hshCompareFrom As Hashtable + Dim hshCompareTo As Hashtable + Dim hshSyncItems As Hashtable + Dim hshDeleteItems As Hashtable + Dim oFromItem As clsGameTag + Dim oToItem As clsGameTag + Dim sCompoundKey As String + + 'Delete Sync + If bToRemote Then + hshCompareFrom = ReadGameTags(mgrSQLite.Database.Local) + hshCompareTo = ReadGameTags(mgrSQLite.Database.Remote) + Else + hshCompareFrom = ReadGameTags(mgrSQLite.Database.Remote) + hshCompareTo = ReadGameTags(mgrSQLite.Database.Local) + End If + + hshDeleteItems = hshCompareTo.Clone + + For Each oToItem In hshCompareTo.Values + sCompoundKey = oToItem.TagID & ":" & oToItem.MonitorID + If hshCompareFrom.Contains(sCompoundKey) Then + oFromItem = DirectCast(hshCompareFrom(sCompoundKey), clsGameTag) + If oToItem.CoreEquals(oFromItem) Then + hshDeleteItems.Remove(sCompoundKey) + End If + End If + Next + + If bToRemote Then + DoGameTagDeleteSync(hshDeleteItems, mgrSQLite.Database.Remote) + Else + DoGameTagDeleteSync(hshDeleteItems, mgrSQLite.Database.Local) + End If + + 'Add / Update Sync + If bToRemote Then + hshCompareFrom = ReadGameTags(mgrSQLite.Database.Local) + hshCompareTo = ReadGameTags(mgrSQLite.Database.Remote) + Else + hshCompareFrom = ReadGameTags(mgrSQLite.Database.Remote) + hshCompareTo = ReadGameTags(mgrSQLite.Database.Local) + End If + + hshSyncItems = hshCompareFrom.Clone + + For Each oFromItem In hshCompareFrom.Values + sCompoundKey = oFromItem.TagID & ":" & oFromItem.MonitorID + If hshCompareTo.Contains(sCompoundKey) Then + oToItem = DirectCast(hshCompareTo(sCompoundKey), clsGameTag) + If oFromItem.CoreEquals(oToItem) Then + hshSyncItems.Remove(sCompoundKey) + End If + End If + Next + + If bToRemote Then + DoGameTagAddSync(hshSyncItems, mgrSQLite.Database.Remote) + Else + DoGameTagAddSync(hshSyncItems, mgrSQLite.Database.Local) + End If + + Return hshDeleteItems.Count + hshSyncItems.Count + + End Function + End Class diff --git a/GBM/Managers/mgrMonitorList.vb b/GBM/Managers/mgrMonitorList.vb index decd91e..bd39054 100644 --- a/GBM/Managers/mgrMonitorList.vb +++ b/GBM/Managers/mgrMonitorList.vb @@ -39,70 +39,6 @@ Public Class mgrMonitorList End If End Sub - Private Shared Sub ImportMonitorList(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) - Dim hshCompareFrom As Hashtable - Dim hshCompareTo As Hashtable - Dim hshSyncItems As Hashtable - Dim oFromItem As clsGame - Dim oToItem As clsGame - Dim oTag As clsTag - Dim oGameTag As clsGameTag - Dim iItems As Integer = 0 - - Cursor.Current = Cursors.WaitCursor - - 'Add / Update Sync - hshCompareFrom = mgrXML.ReadMonitorList(sLocation, bWebRead) - hshCompareTo = ReadList(eListTypes.FullList, mgrSQLite.Database.Local) - - hshSyncItems = hshCompareFrom.Clone - - For Each oFromItem In hshCompareFrom.Values - If hshCompareTo.Contains(oFromItem.ProcessName) Then - oToItem = DirectCast(hshCompareTo(oFromItem.ProcessName), clsGame) - If oFromItem.CoreEquals(oToItem) Then - hshSyncItems.Remove(oFromItem.ProcessName) - End If - End If - Next - - Cursor.Current = Cursors.Default - - If hshSyncItems.Count > 0 Then - Dim frm As New frmAdvancedImport - frm.ImportData = hshSyncItems - If frm.ShowDialog() = DialogResult.OK Then - Cursor.Current = Cursors.WaitCursor - For Each oGame As clsGame In frm.ImportData.Values - If Not DoDuplicateListCheck(oGame.Name, oGame.TrueProcess) Then - DoListAdd(oGame, mgrSQLite.Database.Local) - 'Handle Tag Import (TODO: This could use some optimization, way too many DB hits.) - For Each t As Tag In oGame.ImportTags - If mgrTags.DoCheckDuplicate(t.Name) Then - oTag = mgrTags.DoTagGetbyName(t.Name) - Else - oTag = New clsTag - oTag.Name = t.Name - mgrTags.DoTagAdd(oTag) - End If - oGameTag = New clsGameTag - oGameTag.MonitorID = oGame.ID - oGameTag.TagID = oTag.ID - mgrGameTags.DoGameTagAdd(oGameTag) - Next - iItems += 1 - End If - Next - Cursor.Current = Cursors.Default - MsgBox("Import Complete. " & iItems & " entries have been imported.", MsgBoxStyle.Information, "Game Backup Monitor") - End If - Else - MsgBox("This list does not contain any new games to import.", MsgBoxStyle.Information, "Game Backup Monitor") - End If - - Application.DoEvents() - End Sub - Public Shared Sub ExportMonitorList(ByVal sLocation As String) 'Dim hshList As Hashtable = ReadList(eListTypes.FullList, mgrSQLite.Database.Local) 'Dim bSuccess As Boolean @@ -117,6 +53,61 @@ Public Class mgrMonitorList End If End Sub + Public Shared Sub DoListAddUpdateSync(ByVal hshGames As Hashtable, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim sSQL As String + Dim hshParams As Hashtable + Dim oParamList As New List(Of Hashtable) + + sSQL = "INSERT OR REPLACE INTO monitorlist (MonitorID, Name, Process, Path, AbsolutePath, FolderSave, FileType, TimeStamp, ExcludeList, Hours, Enabled, MonitorOnly) " + sSQL &= "VALUES (COALESCE((SELECT MonitorID FROM monitorlist WHERE Name = @Name AND Process = @Process), @ID), @Name, @Process, @Path, @AbsolutePath, @FolderSave, @FileType, " + sSQL &= "@TimeStamp, @ExcludeList, @Hours, @Enabled, @MonitorOnly);" + + For Each oGame As clsGame In hshGames.Values + hshParams = New Hashtable + + 'Parameters + hshParams.Add("ID", oGame.ID) + hshParams.Add("Name", oGame.Name) + hshParams.Add("Process", oGame.TrueProcess) + hshParams.Add("Path", oGame.TruePath) + hshParams.Add("AbsolutePath", oGame.AbsolutePath) + hshParams.Add("FolderSave", oGame.FolderSave) + hshParams.Add("FileType", oGame.FileType) + hshParams.Add("TimeStamp", oGame.AppendTimeStamp) + hshParams.Add("ExcludeList", oGame.ExcludeList) + hshParams.Add("Hours", oGame.Hours) + + 'Required Defaults + hshParams.Add("Enabled", True) + hshParams.Add("MonitorOnly", False) + + oParamList.Add(hshParams) + Next + + oDatabase.RunMassParamQuery(sSQL, oParamList) + + End Sub + + Public Shared Sub DoListDeleteSync(ByVal hshGames As Hashtable, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim sSQL As String + Dim hshParams As Hashtable + Dim oParamList As New List(Of Hashtable) + + sSQL = "DELETE FROM monitorlist " + sSQL &= "WHERE Name = @Name AND Process= @Process;" + + For Each oGame As clsGame In hshGames.Values + hshParams = New Hashtable + hshParams.Add("Name", oGame.Name) + hshParams.Add("Process", oGame.TrueProcess) + oParamList.Add(hshParams) + Next + + oDatabase.RunMassParamQuery(sSQL, oParamList) + End Sub + Public Shared Sub SyncMonitorLists(Optional ByVal bToRemote As Boolean = True) Dim hshCompareFrom As Hashtable Dim hshCompareTo As Hashtable @@ -124,6 +115,7 @@ Public Class mgrMonitorList Dim hshDeleteItems As Hashtable Dim oFromItem As clsGame Dim oToItem As clsGame + Dim iChanges As Integer Cursor.Current = Cursors.WaitCursor @@ -153,13 +145,12 @@ Public Class mgrMonitorList End If Next - For Each oGame As clsGame In hshDeleteItems.Values - If bToRemote Then - DoListDeleteSync(oGame, mgrSQLite.Database.Remote) - Else - DoListDeleteSync(oGame, mgrSQLite.Database.Local) - End If - Next + If bToRemote Then + DoListDeleteSync(hshDeleteItems, mgrSQLite.Database.Remote) + Else + DoListDeleteSync(hshDeleteItems, mgrSQLite.Database.Local) + End If + 'Add / Update Sync If bToRemote Then @@ -181,30 +172,64 @@ Public Class mgrMonitorList End If Next - For Each oGame As clsGame In hshSyncItems.Values - 'Clear Extra Data - oGame.Version = String.Empty - oGame.Company = String.Empty - oGame.ProcessPath = String.Empty - oGame.Icon = String.Empty + If bToRemote Then + DoListAddUpdateSync(hshSyncItems, mgrSQLite.Database.Remote) + + Else + DoListAddUpdateSync(hshSyncItems, mgrSQLite.Database.Local) + End If - If bToRemote Then - If DoDuplicateListCheck(oGame.Name, oGame.TrueProcess, mgrSQLite.Database.Remote) Then - DoListUpdateSync(oGame, mgrSQLite.Database.Remote) - Else - DoListAdd(oGame, mgrSQLite.Database.Remote) - End If - Else - If DoDuplicateListCheck(oGame.Name, oGame.TrueProcess, mgrSQLite.Database.Local) Then - DoListUpdateSync(oGame, mgrSQLite.Database.Local) - Else - DoListAdd(oGame, mgrSQLite.Database.Local) + 'Sync Tags + iChanges = mgrTags.SyncTags(bToRemote) + iChanges += mgrGameTags.SyncGameTags(bToRemote) + + RaiseEvent UpdateLog(hshDeleteItems.Count + hshSyncItems.Count + iChanges & " change(s) synced.", False, ToolTipIcon.Info, True) + Cursor.Current = Cursors.Default + Application.DoEvents() + End Sub + + Private Shared Sub ImportMonitorList(ByVal sLocation As String, Optional ByVal bWebRead As Boolean = False) + Dim hshCompareFrom As Hashtable + Dim hshCompareTo As Hashtable + Dim hshSyncItems As Hashtable + Dim oFromItem As clsGame + Dim oToItem As clsGame + + Cursor.Current = Cursors.WaitCursor + + 'Add / Update Sync + hshCompareFrom = mgrXML.ReadMonitorList(sLocation, bWebRead) + hshCompareTo = ReadList(eListTypes.FullList, mgrSQLite.Database.Local) + + hshSyncItems = hshCompareFrom.Clone + + For Each oFromItem In hshCompareFrom.Values + If hshCompareTo.Contains(oFromItem.ProcessName) Then + oToItem = DirectCast(hshCompareTo(oFromItem.ProcessName), clsGame) + If oFromItem.CoreEquals(oToItem) Then + hshSyncItems.Remove(oFromItem.ProcessName) End If End If Next - RaiseEvent UpdateLog(hshDeleteItems.Count + hshSyncItems.Count & " change(s) synced.", False, ToolTipIcon.Info, True) Cursor.Current = Cursors.Default + + If hshSyncItems.Count > 0 Then + Dim frm As New frmAdvancedImport + frm.ImportData = hshSyncItems + If frm.ShowDialog() = DialogResult.OK Then + Cursor.Current = Cursors.WaitCursor + + DoListAddUpdateSync(frm.ImportData) + mgrTags.DoTagAddImport(frm.ImportData) + + Cursor.Current = Cursors.Default + MsgBox("Import Complete.", MsgBoxStyle.Information, "Game Backup Monitor") + End If + Else + MsgBox("This list does not contain any new games to import.", MsgBoxStyle.Information, "Game Backup Monitor") + End If + Application.DoEvents() End Sub @@ -407,46 +432,6 @@ Public Class mgrMonitorList End Sub - Public Shared Sub DoListUpdateSync(ByVal oGame As clsGame, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) - Dim oDatabase As New mgrSQLite(iSelectDB) - Dim sSQL As String - Dim hshParams As New Hashtable - - sSQL = "UPDATE monitorlist SET Name=@Name, Process=@Process, Path=@Path, AbsolutePath=@AbsolutePath, FolderSave=@FolderSave, " - sSQL &= "FileType=@FileType, TimeStamp=@TimeStamp, ExcludeList=@ExcludeList, Hours=@Hours " - sSQL &= "WHERE Name=@QueryName AND Process=@QueryProcess" - - 'Parameters - hshParams.Add("Name", oGame.Name) - hshParams.Add("Process", oGame.TrueProcess) - hshParams.Add("Path", oGame.TruePath) - hshParams.Add("AbsolutePath", oGame.AbsolutePath) - hshParams.Add("FolderSave", oGame.FolderSave) - hshParams.Add("FileType", oGame.FileType) - hshParams.Add("TimeStamp", oGame.AppendTimeStamp) - hshParams.Add("ExcludeList", oGame.ExcludeList) - hshParams.Add("Hours", oGame.Hours) - hshParams.Add("QueryName", oGame.Name) - hshParams.Add("QueryProcess", oGame.TrueProcess) - - oDatabase.RunParamQuery(sSQL, hshParams) - - End Sub - - Public Shared Sub DoListDeleteSync(ByVal oGame As clsGame, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) - Dim oDatabase As New mgrSQLite(iSelectDB) - Dim sSQL As String - Dim hshParams As New Hashtable - - sSQL = "DELETE FROM monitorlist " - sSQL &= "WHERE Name = @Name AND Process= @Process" - - hshParams.Add("Name", oGame.Name) - hshParams.Add("Process", oGame.TrueProcess) - - oDatabase.RunParamQuery(sSQL, hshParams) - End Sub - Public Shared Sub DoListDelete(ByVal sMonitorID As String, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) Dim oDatabase As New mgrSQLite(iSelectDB) Dim sSQL As String diff --git a/GBM/Managers/mgrTags.vb b/GBM/Managers/mgrTags.vb index 37974aa..bf8da7f 100644 --- a/GBM/Managers/mgrTags.vb +++ b/GBM/Managers/mgrTags.vb @@ -1,7 +1,7 @@ Public Class mgrTags - Public Shared Sub DoTagAdd(ByVal oTag As clsTag) - Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local) + Public Shared Sub DoTagAdd(ByVal oTag As clsTag, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) Dim sSQL As String Dim hshParams As New Hashtable @@ -11,8 +11,8 @@ oDatabase.RunParamQuery(sSQL, hshParams) End Sub - Public Shared Sub DoTagUpdate(ByVal oTag As clsTag) - Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local) + Public Shared Sub DoTagUpdate(ByVal oTag As clsTag, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) Dim sSQL As String Dim hshParams As New Hashtable @@ -59,7 +59,7 @@ For Each dr As DataRow In oData.Tables(0).Rows oTag = New clsTag oTag.ID = CStr(dr(0)) - oTag.Name = CStr(dr(1)) + oTag.Name = CStr(dr(1)) Next Return oTag @@ -88,8 +88,8 @@ Return oTag End Function - Public Shared Function DoCheckDuplicate(ByVal sTagName As String, Optional ByVal sExcludeID As String = "") As Boolean - Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local) + Public Shared Function DoCheckDuplicate(ByVal sTagName As String, Optional ByVal sExcludeID As String = "", Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) As Boolean + Dim oDatabase As New mgrSQLite(iSelectDB) Dim sSQL As String Dim oData As DataSet Dim hshParams As New Hashtable @@ -113,8 +113,8 @@ End If End Function - Public Shared Function ReadTags() As Hashtable - Dim oDatabase As New mgrSQLite(mgrSQLite.Database.Local) + Public Shared Function ReadTags(Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) As Hashtable + Dim oDatabase As New mgrSQLite(iSelectDB) Dim oData As DataSet Dim sSQL As String Dim hshList As New Hashtable @@ -126,11 +126,139 @@ For Each dr As DataRow In oData.Tables(0).Rows oTag = New clsTag oTag.ID = CStr(dr(0)) - oTag.Name = CStr(dr(1)) + oTag.Name = CStr(dr(1)) hshList.Add(oTag.Name, oTag) Next Return hshList End Function + Public Shared Sub DoTagAddImport(ByVal hshTags As Hashtable, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim sSQL As String + Dim sMonitorID As String + Dim oTag As clsTag + Dim hshParams As Hashtable + Dim oParamList As New List(Of Hashtable) + + sSQL = "INSERT OR REPLACE INTO tags VALUES (COALESCE((SELECT TagID FROM tags WHERE Name = @Name), @ID), @Name); INSERT INTO gametags VALUES ((SELECT TagID from tags WHERE Name=@Name), @MonitorID);" + For Each oGame As clsGame In hshTags.Values + sMonitorID = oGame.ID + For Each t As Tag In oGame.ImportTags + hshParams = New Hashtable + oTag = New clsTag + oTag.Name = t.Name + hshParams.Add("ID", oTag.ID) + hshParams.Add("Name", oTag.Name) + hshParams.Add("MonitorID", sMonitorID) + oParamList.Add(hshParams) + Next + Next + + oDatabase.RunMassParamQuery(sSQL, oParamList) + End Sub + + Public Shared Sub DoTagAddUpdateSync(ByVal hshTags As Hashtable, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim sSQL As String + Dim hshParams As Hashtable + Dim oParamList As New List(Of Hashtable) + + sSQL = "INSERT OR REPLACE INTO tags VALUES (COALESCE((SELECT TagID FROM tags WHERE Name = @Name), @ID), @Name);" + + For Each oTag As clsTag In hshTags.Values + hshParams = New Hashtable + hshParams.Add("ID", oTag.ID) + hshParams.Add("Name", oTag.Name) + oParamList.Add(hshParams) + Next + + oDatabase.RunMassParamQuery(sSQL, oParamList) + End Sub + + Private Shared Sub DoTagDeleteSync(ByVal hshTags As Hashtable, Optional ByVal iSelectDB As mgrSQLite.Database = mgrSQLite.Database.Local) + Dim oDatabase As New mgrSQLite(iSelectDB) + Dim sSQL As String + Dim hshParams As Hashtable + Dim oParamList As New List(Of Hashtable) + + sSQL = "DELETE FROM gametags " + sSQL &= "WHERE TagID = @ID;" + sSQL = "DELETE FROM tags " + sSQL &= "WHERE TagID = @ID;" + + For Each oTag As clsTag In hshTags.Values + hshParams = New Hashtable + hshParams.Add("ID", oTag.ID) + oParamList.Add(hshParams) + Next + + oDatabase.RunMassParamQuery(sSQL, oParamList) + + End Sub + + Public Shared Function SyncTags(Optional ByVal bToRemote As Boolean = True) As Integer + Dim hshCompareFrom As Hashtable + Dim hshCompareTo As Hashtable + Dim hshSyncItems As Hashtable + Dim hshDeleteItems As Hashtable + Dim oFromItem As clsTag + Dim oToItem As clsTag + + 'Delete Sync + If bToRemote Then + hshCompareFrom = ReadTags(mgrSQLite.Database.Local) + hshCompareTo = ReadTags(mgrSQLite.Database.Remote) + Else + hshCompareFrom = ReadTags(mgrSQLite.Database.Remote) + hshCompareTo = ReadTags(mgrSQLite.Database.Local) + End If + + hshDeleteItems = hshCompareTo.Clone + + For Each oToItem In hshCompareTo.Values + If hshCompareFrom.Contains(oToItem.Name) Then + oFromItem = DirectCast(hshCompareFrom(oToItem.Name), clsTag) + If oToItem.CoreEquals(oFromItem) Then + hshDeleteItems.Remove(oToItem.Name) + End If + End If + Next + + If bToRemote Then + DoTagDeleteSync(hshDeleteItems, mgrSQLite.Database.Remote) + Else + DoTagDeleteSync(hshDeleteItems, mgrSQLite.Database.Local) + End If + + 'Add / Update Sync + If bToRemote Then + hshCompareFrom = ReadTags(mgrSQLite.Database.Local) + hshCompareTo = ReadTags(mgrSQLite.Database.Remote) + Else + hshCompareFrom = ReadTags(mgrSQLite.Database.Remote) + hshCompareTo = ReadTags(mgrSQLite.Database.Local) + End If + + hshSyncItems = hshCompareFrom.Clone + + For Each oFromItem In hshCompareFrom.Values + If hshCompareTo.Contains(oFromItem.Name) Then + oToItem = DirectCast(hshCompareTo(oFromItem.Name), clsTag) + If oFromItem.CoreEquals(oToItem) Then + hshSyncItems.Remove(oFromItem.Name) + End If + End If + Next + + If bToRemote Then + DoTagAddUpdateSync(hshSyncItems, mgrSQLite.Database.Remote) + Else + DoTagAddUpdateSync(hshSyncItems, mgrSQLite.Database.Local) + End If + + Return hshDeleteItems.Count + hshSyncItems.Count + + End Function + End Class