aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2017-11-26 17:23:38 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2017-11-26 17:23:38 +0200
commit262c1efc9d53b8b70fe677dc7d2d173896ffcee8 (patch)
treee404dc914bee89101f50963ff018225c897244fd /Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
parent0a8af16821cfbb52fe02881921695d378f6578d7 (diff)
downloadTango-262c1efc9d53b8b70fe677dc7d2d173896ffcee8.tar.gz
Tango-262c1efc9d53b8b70fe677dc7d2d173896ffcee8.zip
Implemented Tango SQLite DB Synchronizer.
Diffstat (limited to 'Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs')
-rw-r--r--Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs55
1 files changed, 55 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs b/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
index 07c94045b..980a23f1e 100644
--- a/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
+++ b/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Data;
+using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Tango.Logging;
namespace Tango.Synchronization
{
@@ -40,28 +42,47 @@ namespace Tango.Synchronization
/// <returns></returns>
public List<SqlDiff> Compare()
{
+ LogManager.Log("Comparing databases " + Path.GetFileName(MasterSQL.Source) + " <=> " + Path.GetFileName(SlaveSQL.Source));
+
+ LogManager.Log("Loading master tables...");
+ MasterSQL.LoadTables();
+
+ LogManager.Log("Loading slave tables...");
+ SlaveSQL.LoadTables();
+
List<SqlDiff> diffs = new List<SqlDiff>();
foreach (var masterTable in MasterSQL.Tables)
{
+ LogManager.Log("Comparing table " + masterTable.TableName);
+ LogManager.Log("Searching table " + masterTable.TableName + " on slave...");
+
var slaveTable = SlaveSQL.Tables.SingleOrDefault(x => x.TableName == masterTable.TableName); //Get matching slave table on slave db.
if (slaveTable == null) //Table not found on slave.
{
+ LogManager.Log("Table not found on slave, adding difference.");
//Clone table from slave db to master db including records!
diffs.Add(new SqlDiff(SqlDiffAction.AddTableToSlave, String.Format("Add table {0} to slave", masterTable.TableName), () => SlaveSQL.CloneTableFrom(MasterSQL, masterTable), SlaveSQL.GetCloneTableFromCommand(MasterSQL, masterTable)));
continue;
}
+ LogManager.Log("Table found, comparing columns...");
+
foreach (DataColumn masterColumn in masterTable.Columns)
{
+ LogManager.Log("Searching for column " + masterColumn.ColumnName + " on slave...");
+
var slaveColumn = slaveTable.Columns[masterColumn.ColumnName]; //Get matching slave column on slave table.
if (slaveColumn == null) //Slave column not found.
{
+ LogManager.Log("Column not found on slave, adding difference.");
//Add column to slave table.
diffs.Add(new SqlDiff(SqlDiffAction.AddColumnToSlave, String.Format("Add column {0} to slave table", masterColumn.ColumnName), () => SlaveSQL.AddColumn(masterTable, masterColumn), SlaveSQL.GetAddColumnCommand(masterTable, masterColumn)));
}
+
+ LogManager.Log("Column found.");
}
List<DataRow> addToSlave = new List<DataRow>();
@@ -69,46 +90,76 @@ namespace Tango.Synchronization
List<DataRow> addToMaster = new List<DataRow>();
List<DataRow> updateMaster = new List<DataRow>();
+ LogManager.Log("Comparing rows...");
+
+ int count = 0;
+
foreach (DataRow masterRow in masterTable.Rows)
{
+ LogManager.Log("Comparing row " + count++);
+
String guid = masterRow.Field<String>(Constants.GUID);
+ LogManager.Log("Searching for row with GUID " + guid + " on slave table...");
+
//Get Matching slave row.
DataRow slaveRow = slaveTable.AsEnumerable().SingleOrDefault(x => x.Field<String>(Constants.GUID) == guid);
if (slaveRow != null)
{
+ LogManager.Log("Slave row found, comparing dates...");
+
DateTime masterDate = masterRow.Field<DateTime>(Constants.LAST_UPDATED);
DateTime slaveDate = slaveRow.Field<DateTime>(Constants.LAST_UPDATED);
if (masterDate > slaveDate)
{
+ LogManager.Log("Master => Slave Update " + masterDate.ToSQLiteDateString() + ", adding difference.");
updateSlave.Add(masterRow);
}
else if (slaveDate > masterDate)
{
+ LogManager.Log("Master <= Slave Update " + masterDate.ToSQLiteDateString() + ", adding difference.");
updateMaster.Add(slaveRow);
}
+ else
+ {
+ LogManager.Log("Master <=> Slave No Update.");
+ }
}
else
{
+ LogManager.Log("Slave row not found, adding difference.");
addToSlave.Add(masterRow);
}
}
+ LogManager.Log("Done comparing rows...");
+
+ LogManager.Log("Searching for missing rows on master...");
+
foreach (DataRow slaveRow in slaveTable.Rows)
{
String guid = slaveRow.Field<String>(Constants.GUID);
+ LogManager.Log("Searching for row with GUID " + guid + " on master table...");
+
//Get Matching slave row.
DataRow masterRow = masterTable.AsEnumerable().SingleOrDefault(x => x.Field<String>(Constants.GUID) == guid);
if (masterRow == null)
{
+ LogManager.Log("Master row not found, adding difference.");
addToMaster.Add(slaveRow);
}
+ else
+ {
+ LogManager.Log("Master row found.");
+ }
}
+ LogManager.Log("Done searching for missing rows on master...");
+
foreach (var row in addToSlave)
{
diffs.Add(new SqlDiff(SqlDiffAction.AddRowToSlave, String.Format("Add row to slave table {0}", slaveTable.TableName), () => SlaveSQL.AddRow(slaveTable, row), SlaveSQL.GetAddRowCommand(slaveTable, row)));
@@ -128,8 +179,12 @@ namespace Tango.Synchronization
{
diffs.Add(new SqlDiff(SqlDiffAction.UpdateRowInMaster, String.Format("Update row in master table {0}", masterTable.TableName), () => MasterSQL.UpdateRow(masterTable, row), MasterSQL.GetUpdateRowCommand(masterTable, row)));
}
+
+ LogManager.Log("Done comparing table " + masterTable.TableName);
}
+ LogManager.Log("Databases comparison completed.");
+
return diffs;
}