private string FormatTitle(SchemaComparisonItem item) { string etype; if (item.LeftDdlStatement is SQLiteCreateTableStatement) { etype = "Table"; } else if (item.LeftDdlStatement is SQLiteCreateIndexStatement) { etype = "Index"; } else if (item.LeftDdlStatement is SQLiteCreateTriggerStatement) { etype = "Trigger"; } else if (item.LeftDdlStatement is SQLiteCreateViewStatement) { etype = "View"; } else { throw new ArgumentException("illegal SQL entity type"); } return(etype + " " + item.LeftDdlStatement.ObjectName.ToString()); }
private void CompareSchema(SchemaComparisonItem item) { string[] left = _nlrx.Split(item.LeftDdlStatement.ToString()); string[] right = _nlrx.Split(item.RightDdlStatement.ToString()); ucDiff.CompareTexts(left, right, _leftdb, _rightdb); UpdateState(); }
public void SetTableChanges(SchemaComparisonItem item, string leftdb, string rightdb, TableChanges changes, string diff) { _item = item; _leftdb = leftdb; _rightdb = rightdb; _diff = diff; _tableChanges = changes; PrepareDataTab(); }
/// <summary> /// Copies the DB entity specified in the comparison item from left to right /// or vice versa (depending on the value of the leftToRight parameter). /// </summary> /// <param name="schema">The schema.</param> /// <param name="item">The comparison item</param> /// <param name="leftdb">The path to the left database file</param> /// <param name="rightdb">The path to the right database file</param> /// <param name="leftToRight">TRUE means copying will be done from the left database /// to the right database, FALSE means copying will be done from the right database /// to the left database.</param> public ItemCopier(Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > leftSchema, Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > rightSchema, SchemaComparisonItem item, string leftdb, string rightdb, bool leftToRight) { _leftSchema = leftSchema; _rightSchema = rightSchema; _item = item; _leftdb = leftdb; _rightdb = rightdb; _leftToRight = leftToRight; _name = item.ObjectName; _pevent = new ProgressEventArgs(false, 0, null, null); }
private static string GenerateTableChangeScript(SchemaComparisonItem item, Dictionary <SchemaObject, List <SchemaComparisonItem> > comp, ChangeDirection direction, Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > leftSchema, Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > rightSchema) { StringBuilder sb = new StringBuilder(); if (item.Result == ComparisonResult.ExistsInLeftDB) { if (direction == ChangeDirection.LeftToRight) { DeleteTable(sb, item.LeftDdlStatement); } else { CopyTable(sb, item.LeftDdlStatement, leftSchema); } } else if (item.Result == ComparisonResult.ExistsInRightDB) { if (direction == ChangeDirection.RightToLeft) { DeleteTable(sb, item.RightDdlStatement); } else { CopyTable(sb, item.RightDdlStatement, rightSchema); } } else if (item.Result == ComparisonResult.DifferentSchema) { if (direction == ChangeDirection.LeftToRight) { MigrateTable(sb, item.RightDdlStatement, leftSchema, rightSchema); } else { MigrateTable(sb, item.LeftDdlStatement, rightSchema, leftSchema); } } return(sb.ToString()); }
private static string GenerateViewChangeScript(SchemaComparisonItem item, ChangeDirection direction) { StringBuilder sb = new StringBuilder(); if (item.Result == ComparisonResult.ExistsInLeftDB) { if (direction == ChangeDirection.LeftToRight) { DeleteView(sb, item.LeftDdlStatement); } else { CopyView(sb, item.LeftDdlStatement); } } else if (item.Result == ComparisonResult.ExistsInRightDB) { if (direction == ChangeDirection.LeftToRight) { CopyView(sb, item.RightDdlStatement); } else { DeleteView(sb, item.RightDdlStatement); } } else if (item.Result == ComparisonResult.DifferentSchema) { if (direction == ChangeDirection.LeftToRight) { CopyView(sb, item.RightDdlStatement); } else { CopyView(sb, item.LeftDdlStatement); } } // else return(sb.ToString()); }
/// <summary> /// Prepare the dialog with the comparison item /// </summary> /// <param name="item">The comparison item</param> /// <param name="leftSchema">The left schema.</param> /// <param name="rightSchema">The right schema.</param> /// <param name="leftdb">The path to the left database file</param> /// <param name="rightdb">the path to the right database file</param> public void Prepare(SchemaComparisonItem item, Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > leftSchema, Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > rightSchema, string leftdb, string rightdb) { _item = item; _leftdb = leftdb; _rightdb = rightdb; _leftSchema = leftSchema; _rightSchema = rightSchema; this.Text = FormatTitle(item); // In case no data comparison took place - hide the data tab if (item.TableChanges == null) { tbcViews.TabPages.Remove(tbpData); } else { _tableChanges = item.TableChanges; tbcViews.SelectTab(tbpData); UpdateDataTab(); } // Load the two schema texts into the diff control CompareSchema(item); // Display error title if there was any error if (item.ErrorMessage != null) { string errmsg = item.ErrorMessage.Replace("\r\n", " "); panel1.Visible = true; lblErrorMessage.Text = "ERROR: data comparison failed (" + errmsg + ")"; } }
/// <summary> /// Compare the schema information in the specified left/right database schema objects /// and return a list of differences. /// </summary> /// <param name="left">The schema information for the left database.</param> /// <param name="right">The schema information for the right database.</param> /// <returns>A dictionary that maps, for every schema object type - a list of differences /// that were found between the two databases.</returns> private Dictionary <SchemaObject, List <SchemaComparisonItem> > CompareSchema( Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > left, Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > right) { Dictionary <SchemaObject, List <SchemaComparisonItem> > res = new Dictionary <SchemaObject, List <SchemaComparisonItem> >(); res.Add(SchemaObject.Table, new List <SchemaComparisonItem>()); res.Add(SchemaObject.Index, new List <SchemaComparisonItem>()); res.Add(SchemaObject.Trigger, new List <SchemaComparisonItem>()); res.Add(SchemaObject.View, new List <SchemaComparisonItem>()); // Prepare auxiliary variables used for notifying progress int total = left[SchemaObject.Table].Count + left[SchemaObject.Index].Count + left[SchemaObject.Trigger].Count + left[SchemaObject.View].Count + right[SchemaObject.Table].Count + right[SchemaObject.Index].Count + right[SchemaObject.Trigger].Count + right[SchemaObject.View].Count; // First locate all objects that exist in the left DB but not in the right DB int index = 0; foreach (SchemaObject so in left.Keys) { foreach (string objname in left[so].Keys) { // Ignore internal sqlite tables if (objname.StartsWith("sqlite_")) { continue; } if (!right[so].ContainsKey(objname)) { // This object exists only in the left DB SchemaComparisonItem item = new SchemaComparisonItem(objname, left[so][objname], null, ComparisonResult.ExistsInLeftDB); res[so].Add(item); } else if (!left[so][objname].Equals(right[so][objname])) { // This object exists in both the left DB and the right DB, but it has // different schema layout. SchemaComparisonItem item = new SchemaComparisonItem(objname, left[so][objname], right[so][objname], ComparisonResult.DifferentSchema); res[so].Add(item); } else { // This object exists in both the left DB abd the right DB and it has // the same schema layout in both databases. SchemaComparisonItem item = new SchemaComparisonItem(objname, left[so][objname], right[so][objname], ComparisonResult.Same); res[so].Add(item); } double progress = 100.0 * index++ / total; NotifySecondaryProgress(false, (int)progress, "Compared object " + objname); if (_cancelled) { throw new UserCancellationException(); } } // foreach } // foreach // Next locate all objects that exist only in the right DB foreach (SchemaObject so in right.Keys) { foreach (string objname in right[so].Keys) { // Ignore internal sqlite tables if (objname.StartsWith("sqlite_")) { continue; } if (!left[so].ContainsKey(objname)) { // This object exists only in the right DB SchemaComparisonItem item = new SchemaComparisonItem(objname, null, right[so][objname], ComparisonResult.ExistsInRightDB); res[so].Add(item); } // if double progress = 100.0 * index++ / total; NotifySecondaryProgress(false, (int)progress, "Compared object " + objname); if (_cancelled) { throw new UserCancellationException(); } } // foreach } // foreach NotifySecondaryProgress(true, 100, "Finished schema comparisons"); return(res); }