static List <DecoratedTextLine> GetTableDiffLines(DatabaseTuple db_tup, List <DecoratedTextLine> lines = null) { if (lines is null) { lines = new List <DecoratedTextLine>(); } var added_tables = new SortedSet <string>(); var removed_tables = new SortedSet <string>(); foreach (var diff_table_tup in db_tup.UnmatchedTables) { if (diff_table_tup.Names[1] is null) { removed_tables.Add(diff_table_tup.Names[0]); } else { added_tables.Add(diff_table_tup.Names[1]); } } foreach (var tup in new Tuple <SortedSet <string>, bool>[] { new Tuple <SortedSet <string>, bool>(added_tables, true), new Tuple <SortedSet <string>, bool>(removed_tables, false) }) { var set = tup.Item1; var b_is_add = tup.Item2; if (set.Count > 0) { foreach (var table_name in set) { string prefix; string color; if (b_is_add) { prefix = "+ "; color = "green"; } else { prefix = "- "; color = "red"; } lines.Add(new DecoratedTextLine(new DecoratedTextBlock($"{prefix}{table_name}").WithDecoration("color", color))); } } } return(lines); }
public TableTuple(DatabaseTuple parent, string orig_table_name, string dest_table_name) { OrigName = orig_table_name; DestName = dest_table_name; MatchedCols = new List <ColumnTuple>(); UnmatchedCols = new List <ColumnTuple>(); this.DbTuple = parent; if (OrigName != null && DestName != null) { match_cols(); } }
static int Main(string[] args) { var b_print_help = false; var b_print_version = false; var b_print_cols = false; var b_print_rows = false; var b_print_tables = false; bool b_print_all = false; bool b_do_colors = true; var p = new OptionSet() { { "h|help", "Show this help message and exit", v => b_print_help = v != null }, { "v|version", "Print the version and exit", v => b_print_version = v != null }, { "c|col", "Print column differences", v => b_print_cols = v != null }, { "r|row", "Print each row differences", v => b_print_rows = v != null }, { "t|table", "Print table differences", v => b_print_tables = v != null }, { "no-color", "Disable colored output", v => b_do_colors = v != null }, }; var filepaths = p.Parse(args); if (b_print_help) { PrintHelp(p); return(0); } else if (b_print_version) { Console.WriteLine($"{PRGM} v{typeof(SDF_comparator).Assembly.GetName().Version}"); return(0); } else if (filepaths.Count != 2) { Console.WriteLine("Error: 2 files required"); PrintHelp(p); return(1); } foreach (var fpath in filepaths) { if (!File.Exists(fpath)) { Console.WriteLine($"Error: \"{fpath}\" is not a file"); PrintHelp(p); return(1); } } if (!b_print_tables && !b_print_cols && !b_print_rows) { b_print_all = true; } var db_tup = new DatabaseTuple( new CachedDatabase(filepaths[0]), new CachedDatabase(filepaths[1])); var headers = new List <string>() { $"--- {db_tup.Orig.Filepath}\n+++ {db_tup.Dest.Filepath}\n" }; if (b_print_all || b_print_tables) { var lines = GetTableDiffLines(db_tup); if (PrintDecoratedSection(headers, lines, b_do_colors)) { headers.Clear(); } } int header_table_start; foreach (var table_tup in db_tup.MatchedTables) { header_table_start = headers.Count; headers.Add($"\n---- {table_tup.OrigName}\n++++ {table_tup.DestName}"); if (b_print_all || b_print_cols) { var lines = GetColDiffLines(table_tup); if (PrintDecoratedSection(headers, lines, b_do_colors)) { headers.Clear(); } } if (b_print_all || b_print_rows) { var changes = table_tup.get_row_changes(); headers.Add("\n Rows:"); var s = " "; var prefix = " |"; foreach (var col_tup in table_tup.MatchedCols) { s += $"{prefix} {col_tup.Names[0]}"; } s += " |"; headers.Add("\n" + s); var lines = GetRowDiffLines(changes); if (PrintDecoratedSection(headers, lines, b_do_colors)) { headers.Clear(); } } if (headers.Count > header_table_start) { headers.RemoveRange(header_table_start, headers.Count - header_table_start); } } return(0); }