public void ExportClass(Command cmd) { DpoOption option = new DpoOption(); option.NameSpace = cfg.GetValue<string>("dpo.ns", "Sys.DataModel.Dpo"); option.OutputPath = cfg.GetValue<string>("dpo.path", $"{Configuration.MyDocuments}\\DataModel\\Dpo"); option.Level = cfg.GetValue<Level>("dpo.level", Level.Application); option.HasProvider = cfg.GetValue<bool>("dpo.hasProvider", false); option.HasTableAttribute = cfg.GetValue<bool>("dpo.hasTableAttr", true); option.HasColumnAttribute = cfg.GetValue<bool>("dpo.hasColumnAttr", true); option.IsPack = cfg.GetValue<bool>("dpo.isPack", true); option.CodeSorted = cmd.Has("sort"); option.ClassNameSuffix = cfg.GetValue<string>("dpo.suffix", Setting.DPO_CLASS_SUFFIX_CLASS_NAME); option.ClassNameRule = name => name.Substring(0, 1).ToUpper() + name.Substring(1).ToLower() + option.ClassNameSuffix; if (tname != null) { var clss = new DpoGenerator(tname) { Option = option }; clss.CreateClass(); stdio.WriteLine("generated class {0} at {1}", tname.ShortName, option.OutputPath); } else if (dname != null) { stdio.WriteLine("start to generate database {0} class to directory: {1}", dname, option.OutputPath); CancelableWork.CanCancel(cts => { var md = new MatchedDatabase(dname, cmd.wildcard, cfg.exportExcludedTables); TableName[] tnames = md.MatchedTableNames; foreach (var tn in tnames) { if (cts.IsCancellationRequested) return; try { var clss = new DpoGenerator(tn) { Option = option }; clss.CreateClass(); stdio.WriteLine("generated class for {0} at {1}", tn.ShortName, option.OutputPath); } catch (Exception ex) { stdio.ErrorFormat("failed to generate class {0}, {1}", tn.ShortName, ex.Message); } } stdio.WriteLine("completed"); return; }); } else { stdio.ErrorFormat("warning: database is not selected"); } }
public bool Display(Command cmd) { SqlBuilder builder; int top = cmd.top; string[] columns = cmd.Columns; if (cmd.wildcard != null) { string where = LikeExpr(cmd.wildcard, cmd.Columns); builder = new SqlBuilder().SELECT.ROWID(cmd.HasRowId).COLUMNS().FROM(tname).WHERE(where); } else if (cmd.where != null) { var locator = new Locator(cmd.where); builder = new SqlBuilder().SELECT.TOP(top).ROWID(cmd.HasRowId).COLUMNS(columns).FROM(tname).WHERE(locator); } else if (cmd.Has("dup")) { DuplicatedTable dup = new DuplicatedTable(tname, columns); if (dup.group.Rows.Count == 0) { stdio.WriteLine("no duplicated record found"); return true; } if (cmd.IsSchema) { Display(cmd, dup.group, 0); } else { dup.Dispaly(dt => Display(cmd, dt, 0)); } return true; } else builder = new SqlBuilder().SELECT.TOP(top).ROWID(cmd.HasRowId).COLUMNS(columns).FROM(tname); return Display(cmd, builder, tname, top); }
private static void _DisplayTable(UniqueTable udt, bool more, Command cmd) { DataTable table = udt.Table; if (table == null) return; if (cmd.ToJson) { stdio.WriteLine(ToJson(table)); return; } if (cmd.ToCSharp) { string clss = cmd.GetValue("class"); stdio.WriteLine(ToCSharp(table, clss)); return; } if (cmd.Has("edit")) { var editor = new Windows.TableEditor(cmd.Configuration, udt); editor.ShowDialog(); return; } if (cmd.IsVertical) table.ToVConsole(more); else table.ToConsole(more); }
public void export(Command cmd, Configuration cfg, ShellContext context) { if (cmd.HasHelp) { stdio.WriteLine("export data, schema, class, and template on current selected server/db/table"); stdio.WriteLine("option:"); stdio.WriteLine(" /insert : export INSERT INTO script on current table/database"); stdio.WriteLine(" [/if] : option /if generate if exists row then UPDATE else INSERT"); stdio.WriteLine(" /create : generate CREATE TABLE script on current table/database"); stdio.WriteLine(" /select : generate SELECT FROM WHERE template"); stdio.WriteLine(" /update : generate UPDATE SET WHERE template"); stdio.WriteLine(" /save : generate IF EXISTS UPDATE ELSE INSERT template"); stdio.WriteLine(" /delete : generate DELETE FROM WHERE template, delete rows with foreign keys constraints"); stdio.WriteLine(" /schema : generate database schema xml file"); stdio.WriteLine(" /data : generate database/table data xml file"); stdio.WriteLine(" /dpo : generate C# table class"); stdio.WriteLine(" /enum : generate C# enum class"); stdio.WriteLine(" /l2s : generate C# Linq to SQL class"); stdio.WriteLine(" /dc1 : generate C# data contract class and extension class from last result"); stdio.WriteLine(" /dc2 : generate C# data contract class from last result"); stdio.WriteLine(" [/ns:name] default name space is defined on the .cfg"); stdio.WriteLine(" [/class:name] default class name is defined on the .cfg"); stdio.WriteLine(" [/method:foo] default convert method is defined on the .cfg"); stdio.WriteLine(" [/col:pk1,pk2] default primary key is the first column"); stdio.WriteLine(" [/out:path] output path"); stdio.WriteLine(" /entity : generate C# method copy/compare/clone for Entity framework"); stdio.WriteLine(" [/ns:name] default name space is defined on the .cfg"); stdio.WriteLine(" [/base:type] define base class or interface, use ~ to represent generic class itself, delimited by ;"); stdio.WriteLine(" [/using:assembly] allow the use of types in a namespace, delimited by ;"); stdio.WriteLine(" [/out:path] output path"); stdio.WriteLine(" /csv : generate table csv file"); stdio.WriteLine(" /json : generate json from last result"); stdio.WriteLine(" /c# : generate C# data from last result"); stdio.WriteLine(" [/ns:name] default name space is defined on the .cfg"); stdio.WriteLine(" [/class:name] default class name is defined on the .cfg"); stdio.WriteLine(" [/out:path] output path"); return; } if (!Navigate(cmd.Path1)) return; if (pt.Item is TableName || pt.Item is Locator || pt.Item is DatabaseName || pt.Item is ServerName) { var exporter = new Exporter(mgr, pt, cfg); if (cmd.Has("insert")) exporter.ExportInsert(cmd); else if (cmd.Has("create")) exporter.ExportCreate(cmd); else if (cmd.Has("select")) exporter.ExportScud(SqlScriptType.SELECT); else if (cmd.Has("delete")) exporter.ExportScud(SqlScriptType.DELETE); else if (cmd.Has("update")) exporter.ExportScud(SqlScriptType.UPDATE); else if (cmd.Has("save")) exporter.ExportScud(SqlScriptType.INSERT_OR_UPDATE); else if (cmd.Has("schema")) exporter.ExportSchema(); else if (cmd.Has("data")) exporter.ExportData(cmd); else if (cmd.Has("dpo")) exporter.ExportClass(cmd); else if (cmd.Has("enum")) exporter.ExportEnum(cmd); else if (cmd.Has("csv")) exporter.ExportCsvFile(cmd); else if (cmd.Has("dc1")) exporter.ExportDataContract(cmd, 1); else if (cmd.Has("dc2")) exporter.ExportDataContract(cmd, 2); else if (cmd.Has("entity")) exporter.ExportEntityClass(cmd); else if (cmd.Has("l2s")) exporter.ExportLinq2SQLClass(cmd); else if (cmd.ToJson) { if (SqlShell.LastResult is DataTable) { var dt = SqlShell.LastResult as DataTable; stdio.WriteLine(TableOut.ToJson(dt)); } else { stdio.ErrorFormat("display data table first by sql clause or command [type]"); } } else if (cmd.ToCSharp) exporter.ExportCSharpData(cmd); else stdio.ErrorFormat("invalid command options"); } else stdio.ErrorFormat("select server, database or table first"); }
public void clean(Command cmd, Configuration cfg) { if (cmd.HasHelp) { stdio.WriteLine("clean duplicated rows"); stdio.WriteLine("clean [path]|[pattern]| : clean current database or table, or search pattern"); stdio.WriteLine("options:"); stdio.WriteLine(" /col:c1,c2,.. : clean columns, compare column c1, c2, ..."); stdio.WriteLine(" /d : commit cleaning duplicated rows on database server, otherwise display # of duplicated rows"); stdio.WriteLine("example:"); stdio.WriteLine("clean match*s /col:c1,c2 : clean duplicated rows by comparing columns:c1 and c2"); stdio.WriteLine("clean : clean by comparing entire row"); return; } if (!Navigate(cmd.Path1)) return; if (pt.Item is TableName) { var tname = (TableName)pt.Item; var dup = new DuplicatedTable(tname, cmd.Columns); if (cmd.Has("d")) { int count = dup.Clean(); stdio.WriteLine("completed to clean {0} #rows at {1}", count, tname); } else { int count = dup.DuplicatedRowCount(); if (count == 0) stdio.WriteLine("no duplicated rows at {0}", tname); else stdio.WriteLine("{0} duplicated row(s) at {1}", count, tname); } return; } if (pt.Item is DatabaseName) { var dname = (DatabaseName)pt.Item; var m = new MatchedDatabase(dname, cmd.wildcard, cfg.compareExcludedTables); var T = m.MatchedTableNames; CancelableWork.CanCancel(cts => { foreach (var tn in T) { if (cts.IsCancellationRequested) return; if (cmd.Has("d")) { stdio.WriteLine("start to clean {0}", tn); var dup = new DuplicatedTable(tn, cmd.Columns); int count = dup.Clean(); stdio.WriteLine("cleaned {0} #rows", count); } else { stdio.WriteLine("start to query {0}", tn); var dup = new DuplicatedTable(tn, cmd.Columns); int count = dup.DuplicatedRowCount(); if (count == 0) stdio.WriteLine("distinct rows"); else stdio.WriteLine("{0} duplicated row(s)", count, tn); } } }); return; } stdio.ErrorFormat("select database or table first"); }