internal static class DataTableEX
{
public static void PrintColumns(this DataTable table)
{
DataColumn[] columns = table.Columns.Cast<DataColumn>().ToArray();
if (columns.Length == 0)
{
Console.WriteLine("NO columns");
return;
}
int RealLen(string str) => str.Select(x => (0xFF00 & x) == 0 ? 1 : 2).Sum();
int UnRealLen(string str) => str.Select(x => (0xFF00 & x) == 0 ? 0 : 1).Sum();
string GetDataMemberType(DataColumn column)
{
var rt = column.DataType.ToString();
if (column.DataType.Namespace == "System")
{
rt = rt.Substring(column.DataType.Namespace.Length + 1);
}
return rt + (column.AllowDBNull ? "?" : "");
}
var colNames = new[] { " №", "Column Name", "Data Type", "Nullable" };
var colsLen = new[] {
columns.Select(c => c.Ordinal).Max().ToString().Length,
columns.Select(c => RealLen(c.ColumnName)).Max(),
columns.Select(c => GetDataMemberType(c).Length).Max(),
colNames[3].Length,
}.Select((x, i) => Math.Max(colNames[i].Length, x)).ToArray();
var align = new[] { 1, -1, -1, -1 }.Select((num, i) =>
new Func<string, string>(str =>
String.Format($"{{0,{num * (colsLen[i] - UnRealLen(str))}}}", str))).ToArray();
var pk = columns[0].Table.PrimaryKey;
var fks = from co in columns[0].Table.Constraints.Cast<Constraint>()
where co is ForeignKeyConstraint
select co as ForeignKeyConstraint;
var keys = from o in
from c in columns
select (c, keys: new Dictionary<string, bool>()
{
["pk"] = pk.Contains(c),
["uk"] = c.Unique,
["ro"] = c.ReadOnly
})
where o.keys.Any(x => x.Value)
orderby o.keys.Values.Select((x, i) => (x ? int.MinValue : 0) >> i + 1).Sum() +
o.c.Ordinal
select o.c.ColumnName +
$"({String.Join(",", o.keys.Where(x => x.Value).Select(x => x.Key))})";
string PadBoth(string source, int length)
{
int spaces = length - RealLen(source);
return new String(' ', spaces / 2) + source + new string(' ', spaces - spaces / 2);
}
var preBg = Console.BackgroundColor;
Console.BackgroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(PadBoth(table.TableName, (colsLen.Sum() + 14)));
Console.BackgroundColor = preBg;
if (keys.Count() > 0) Console.WriteLine(string.Join(", ", keys));
if (fks.Count() > 0)
{
var str = string.Join("\n", fks.Select(x => x.ConstraintName + ":(" +
string.Join(",", x.Columns.Select(x => x.ColumnName)) + ") -> " +
x.RelatedColumns[0].Table.TableName + "(" +
string.Join(",", x.RelatedColumns.Select(x => x.ColumnName)) + ")"));
Console.WriteLine(str);
}
Console.WriteLine($"┌{ String.Join('┬', colsLen.Select(x => new String('-', x + 2))) }┐");
Console.WriteLine($"│{ String.Join(" │ ", colNames.Select((x, i) => x.PadRight(colsLen[i])))} │");
Console.WriteLine($"├{ String.Join('┼', colsLen.Select(x => new String('-', x + 2))) }┤");
foreach (DataColumn column in columns)
{
var colsAttr = new object[] {
column.Ordinal,
column.ColumnName,
GetDataMemberType(column),
column.AllowDBNull,
};
Console.WriteLine($"│ {String.Join(" │ ", colsAttr.Select((x, i) => align[i](x.ToString())))} │");
}
Console.WriteLine($"└{ String.Join('┴', colsLen.Select(x => new String('-', x + 2))) }┘");
Console.WriteLine();
}
}