public TheClassBuilder(string ns, Command cmd) { this.cmd = cmd; this._using = cmd.GetValue("using"); this._base = cmd.GetValue("base"); builder = new CSharpBuilder { nameSpace = ns }; }
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 ExportEnum(Command cmd) { DataTable dt = null; if (SqlShell.LastResult is DataTable) { dt = SqlShell.LastResult as DataTable; } if (dt == null) { stdio.ErrorFormat("data table cannot find, use command type or select first"); return; } string path = cfg.GetValue<string>("de.path", $"{Configuration.MyDocuments}\\DataModel\\DataEnum"); string ns = cmd.GetValue("ns") ?? cfg.GetValue<string>("de.ns", "Sys.DataModel.DataEnum"); CSharpBuilder builder = new CSharpBuilder() { nameSpace = ns }; builder.AddUsing("Sys.Data"); var rows = dt .AsEnumerable() .Select(row => new { Category = row.Field<string>("Category"), Feature = row.Field<string>("Feature"), Value = row.Field<int>("Value"), Label = row.Field<string>("Label") }); var groups = rows.GroupBy(row => row.Category); foreach (var group in groups) { var _enum = new Sys.CodeBuilder.Enum(group.First().Category); foreach (var row in group) _enum.Add(row.Feature, row.Value, row.Label); builder.AddEnum(_enum); } string filename = "DataEnum"; string code = builder.ToString(); string file = Path.ChangeExtension(Path.Combine(path, filename), "cs"); code.WriteIntoFile(file); stdio.WriteLine("code generated on {0}", file); }
public void ExportLinq2SQLClass(Command cmd) { string path = cfg.GetValue<string>("l2s.path", $"{Configuration.MyDocuments}\\dc"); string ns = cmd.GetValue("ns") ?? cfg.GetValue<string>("l2s.ns", "Sys.DataModel.L2s"); Dictionary<TableName, TableSchema> schemas = new Dictionary<TableName, TableSchema>(); if (tname != null) { var builder = new Linq2SQLClassBuilder(ns, cmd, tname, schemas); string file = builder.WriteFile(path); stdio.WriteLine("code generated on {0}", file); } else if (dname != null) { TableName[] tnames; if (cmd.wildcard != null) { var md = new MatchedDatabase(dname, cmd.wildcard, new string[] { }); tnames = md.MatchedTableNames; if (tnames.Length == 0) { stdio.ErrorFormat("warning: no table is matched"); return; } } else { tnames = dname.GetTableNames(); } foreach (var tname in tnames) { var builder = new Linq2SQLClassBuilder(ns, cmd, tname, schemas); string file = builder.WriteFile(path); stdio.WriteLine("code generated on {0}", file); } return; } else { stdio.ErrorFormat("warning: table or database is not seleted"); } }
public void ExportEntityClass(Command cmd) { if (dname == null) { stdio.ErrorFormat("select a database first"); return; } string path = cmd.GetValue("out") ?? cfg.GetValue<string>("dc.path", $"{Configuration.MyDocuments}\\dc"); string ns = cmd.GetValue("ns") ?? cfg.GetValue<string>("dc.ns", "Sys.DataModel.DataContracts"); if (tname != null) { stdio.WriteLine("start to generate {0} entity framework class file", tname); var builder = new EntityClassBuilder(ns, cmd, tname); string file = builder.WriteFile(path); stdio.WriteLine("completed {0} => {1}", tname.ShortName, file); } else if (dname != null) { stdio.WriteLine("start to generate {0} entity framework class to directory: {1}", dname, path); CancelableWork.CanCancel(cts => { var md = new MatchedDatabase(dname, cmd.wildcard, null); //cfg.exportExcludedTables); TableName[] tnames = md.MatchedTableNames; foreach (var tn in tnames) { if (cts.IsCancellationRequested) return; try { var builder = new EntityClassBuilder(ns, cmd, tn); string file = builder.WriteFile(path); stdio.WriteLine("generated for {0} at {1}", tn.ShortName, path); } catch (Exception ex) { stdio.ErrorFormat("failed to generate {0}, {1}", tn.ShortName, ex.Message); } } stdio.WriteLine("completed"); return; }); } else { stdio.ErrorFormat("warning: table or database is not seleted"); } }
public void ExportDataContract(Command cmd, int version) { DataTable dt = null; if (SqlShell.LastResult is DataTable) { dt = SqlShell.LastResult as DataTable; } if (dt == null) { if (tname != null) { dt = new SqlCmd(tname.Provider, $"SELECT TOP 1 * FROM {tname.FormalName}").FillDataTable(); } else { stdio.ErrorFormat("data table cannot find, use command type or select first"); return; } } string path = cmd.GetValue("out") ?? cfg.GetValue<string>("dc.path", $"{Configuration.MyDocuments}\\dc"); string ns = cmd.GetValue("ns") ?? cfg.GetValue<string>("dc.ns", "Sys.DataModel.DataContracts"); string clss = cmd.GetValue("class") ?? cfg.GetValue<string>("dc.class", "DataContract"); string mtd = cmd.GetValue("method"); string[] keys = cmd.Columns; if (version == 1) { var builder = new DataContractClassBuilder(ns, cmd, dt) { cname = clss, mtd = mtd, keys = keys }; string file = builder.WriteFile(path); stdio.WriteLine("code generated on {0}", file); } else { var builder = new DataContract2ClassBuilder(ns, cmd, dt) { cname = clss, mtd = mtd }; string file = builder.WriteFile(path); stdio.WriteLine("code generated on {0}", file); } }
/// <summary> /// create C# data from data table /// </summary> /// <param name="cmd"></param> public void ExportCSharpData(Command cmd) { if (!(SqlShell.LastResult is DataTable)) { stdio.ErrorFormat("display data table first by sql clause or command [type]"); return; } string ns = cmd.GetValue("ns") ?? "Sql.Data"; string cname = cmd.GetValue("class") ?? "Table"; var dt = SqlShell.LastResult as DataTable; var builder = new CSharpBuilder { nameSpace = ns }; var clss = new Class(cname) { modifier = Modifier.Public | Modifier.Partial }; builder.AddClass(clss); Property prop; foreach (DataColumn column in dt.Columns) { bool nullable = dt.AsEnumerable().Any(row => row[column] is DBNull); TypeInfo ty = new TypeInfo(column.DataType) { Nullable = nullable }; prop = new Property(ty, column.ColumnName.ToFieldName()) { modifier = Modifier.Public}; clss.Add(prop); } clss = new Class(cname + "Data") { modifier = Modifier.Public }; builder.AddClass(clss); Func<int, string> tab = n => new string('\t', n); string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray(); List<string> L = new List<string>(); foreach (DataRow row in dt.Rows) { List<string> V = new List<string>(); for (int i = 0; i < columns.Length; i++) { V.Add(string.Format("{0} = {1}", columns[i], VAL.Boxing(row[i]).ToString())); } L.Add($"{tab(3)}new {cname} {{ " + string.Join(", ", V) + " }"); } var value = $"new {cname}[]\n" + $"{tab(2)}{{\n" + string.Join(",\n", L) + $"\n{tab(2)}}}"; Field field = new Field(new TypeInfo { userType = $"{cname}[]" }, "data") { modifier = Modifier.Public | Modifier.Static | Modifier.Readonly, userValue = value }; clss.Add(field); string code = $"{builder}"; string path = cmd.GetValue("out"); if (path == null) { stdio.WriteLine(code); } else { string file = Path.ChangeExtension(Path.Combine(path, cname), "cs"); code.WriteIntoFile(file); stdio.WriteLine("code generated on {0}", file); } }
public void mount(Command cmd, Configuration cfg) { if (cmd.HasHelp) { stdio.WriteLine("mount database server"); stdio.WriteLine("mount alias=server_name : alias must start with letter"); stdio.WriteLine("options:"); stdio.WriteLine(" /db:database : initial catalog, default is 'master'"); stdio.WriteLine(" /u:username : user id, default is 'sa'"); stdio.WriteLine(" /p:password : password, default is empty, use Windows Security when /u /p not setup"); stdio.WriteLine(" /pvd:provider : sqloledb,xmlfile, default is SQL client"); stdio.WriteLine("example:"); stdio.WriteLine(" mount ip100=192.168.0.100\\sqlexpress /u:sa /pwd:p@ss"); stdio.WriteLine(" mount web=http://192.168.0.100/db/northwind.xml /u:sa /pwd:p@ss"); stdio.WriteLine(" mount xml=file://c:\\db\\northwind.xml"); return; } if (cmd.arg1 == null) { stdio.ErrorFormat("invalid arguments"); return; } var items = cmd.arg1.Split('='); if (items.Length != 2) { stdio.ErrorFormat("invalid arguments, correct format is alias=server_name"); return; } string serverName = items[0].Trim(); string dataSource = items[1].Trim(); StringBuilder builder = new StringBuilder(); string pvd = cmd.GetValue("pvd"); if (pvd != null) { if (pvd != "sqloledb" && pvd != "xmlfile") { stdio.ErrorFormat("provider={0} is not supported", pvd); return; } builder.AppendFormat("provider={0};", pvd); } else { if (dataSource.StartsWith("file://") || dataSource.StartsWith("http://") || dataSource.StartsWith("ftp://")) builder.Append("provider=xmlfile;"); } builder.AppendFormat("data source={0};", dataSource); string db = cmd.GetValue("db"); if (db != null) builder.AppendFormat("initial catalog={0};", db); else builder.Append("initial catalog=master;"); string userId = cmd.GetValue("u"); string password = cmd.GetValue("p"); if (userId == null && password == null) { builder.Append("integrated security=SSPI;packet size=4096"); } else { if (userId != null) builder.AppendFormat("User Id={0};", userId); else builder.Append("User Id=sa;"); if (password != null) builder.AppendFormat("Password={0}", password); else builder.Append("Password="******"database is offline or wrong parameter"); return; } var snode = new TreeNode<IDataPath>(provider.ServerName); var result = cfg.Providers.FirstOrDefault(row => row.ServerName.Path == serverName); if (result != null) { cfg.Providers.Remove(result); var node = mgr.RootNode.Nodes.FirstOrDefault(row => row.Item.Path == serverName); if (node != null) mgr.RootNode.Nodes.Remove(node); } cfg.Providers.Add(provider); mgr.RootNode.Nodes.Add(snode); var xnode = mgr.Navigate(new PathName("\\" + serverName)); if (xnode != null) { mgr.current = xnode; } }
public void edit(Command cmd, Side theSide) { if (cmd.HasHelp) { stdio.WriteLine("edit, view and execute sql script"); stdio.WriteLine("edit : create new file and edit"); stdio.WriteLine("edit [file] : edit file, it is read-only if file is hyperlink"); stdio.WriteLine("options:"); stdio.WriteLine(" /usr : FTP user name"); stdio.WriteLine(" /pwd : FTP password"); stdio.WriteLine("examples:"); stdio.WriteLine(" edit c:\\db\\northwind.sql"); stdio.WriteLine(" edit file://datconn/northwind.sql"); stdio.WriteLine(" edit http://www.datconn.com/demos/northwind.sql"); stdio.WriteLine(" edit ftp://www.datconn.com/demos/northwind.sql /usr:user /pwd:password"); return; } FileLink fileLink = null; if (cmd.arg1 != null) { string inputfile = cmd.arg1; if (inputfile.IndexOf("://") < 0) { if (Path.GetDirectoryName(inputfile) == string.Empty) { string path = cmd.Configuration.GetValue<string>("MyDocuments", Directory.GetCurrentDirectory()); inputfile = $"{path}\\{inputfile}"; } } fileLink = FileLink.CreateLink(inputfile, cmd.GetValue("usr"), cmd.GetValue("pwd")); try { if (!fileLink.Exists) { if (!fileLink.IsLocalLink) { stdio.ErrorFormat("file {0} doesn't exist", fileLink); return; } else { File.WriteAllText(inputfile, string.Empty); fileLink = FileLink.CreateLink(inputfile); } } } catch (Exception ex) { stdio.Error(ex.Message); return; } } try { var editor = new Windows.SqlEditor(cmd.Configuration, theSide.Provider, fileLink); editor.ShowDialog(); } catch (Exception ex) { stdio.Error(ex.Message); return; } }