public DpoClass(ITableSchema metaTable, ClassName cname, DpoOption option) { this.metaTable = metaTable; this.nameSpace = cname.Namespace; this.className = cname.Class; this.option = option; this.code = new CSharpBuilder { Namespace = cname.Namespace, }; code.AddUsing("System"); code.AddUsing("System.Collections.Generic"); code.AddUsing("System.Text"); code.AddUsing("System.Data"); code.AddUsing("System.Drawing"); code.AddUsing("Sys.Data"); code.AddUsing("Sys.Data.Manager"); clss = new Class(cname.Class, new CodeBuilder.TypeInfo { Type = typeof(DPObject) }) { Modifier = Modifier.Public | Modifier.Partial, Sorted = option.CodeSorted }; this.code.AddClass(clss); nonvalized = NonvalizedList(nameSpace, className); nullableFields = NullableList(nameSpace, className); }
public DpoClass(ITable metaTable, ClassName cname, DpoOption option) { this.metaTable = metaTable; this.nameSpace = cname.Namespace; this.className = cname.Class; this.option = option; this.code = new CSharpBuilder { nameSpace = cname.Namespace, }; code.AddUsing("System"); code.AddUsing("System.Collections.Generic"); code.AddUsing("System.Text"); code.AddUsing("System.Data"); code.AddUsing("System.Drawing"); code.AddUsing("Sys.Data"); code.AddUsing("Sys.Data.Manager"); clss = new Class(cname.Class, new CodeBuilder.TypeInfo { type = typeof(DPObject) }) { modifier = Modifier.Public | Modifier.Partial, Sorted = option.CodeSorted }; this.code.AddClass(clss); nonvalized = NonvalizedList(nameSpace, className); nullableFields = NullableList(nameSpace, className); }
private CSharpBuilder CreateClass(IEnumerable <Buildable> elements) { CSharpBuilder builder = new CSharpBuilder { Namespace = NamespaceName }; Class clss = new Class(ClassName) { Modifier = Modifier.Public | Modifier.Static | Modifier.Partial }; builder.AddUsing("System"); clss.AddRange(elements); builder.AddClass(clss); return(builder); }
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); }
private void ExportEnum(DataTable dt) { int count = dt.Columns.Count; if (count < 2) { cerr.WriteLine("cannot generate enum class because table is < 2 columns"); return; } CSharpBuilder builder = new CSharpBuilder() { Namespace = NamespaceName }; builder.AddUsingRange(base.Usings); string cname = ClassName; if (count > 2) { builder.AddUsing("System.ComponentModel"); } DataColumn _feature = null; //1st string column as property name DataColumn _value = null; //1st int column as property value DataColumn _label = null; //2nd string column as attribute [DataEnum("label")] DataColumn _category = null; //3rd string column as category to generate multiple enum types foreach (DataColumn column in dt.Columns) { if (column.DataType == typeof(string)) { if (_feature == null) { _feature = column; } else if (_label == null) { _label = column; } else if (_category == null) { _category = column; } } if (_value == null && column.DataType == typeof(int)) { _value = column; } } if (_feature == null) { cerr.WriteLine("invalid enum property name"); return; } if (_value == null) { cerr.WriteLine("invalid enum property value"); return; } var rows = dt .AsEnumerable() .Select(row => new { Feature = row.Field <string>(_feature), Value = row.Field <int>(_value), Category = _category != null ? row.Field <string>(_category) : null, Label = _label != null ? row.Field <string>(_label) : null }); if (_category != null) { var groups = rows.GroupBy(row => row.Category); foreach (var group in groups) { var _enum = new EnumType(group.First().Category); foreach (var row in group) { _enum.Add(row.Feature, row.Value, $"\"{row.Label}\""); } builder.AddEnum(_enum); } } else { var _enum = new EnumType(cname); foreach (var row in rows) { _enum.Add(row.Feature, row.Value, $"\"{row.Label}\""); } builder.AddEnum(_enum); } PrintOutput(builder, cname); }
public void ExportCSharpData() { string code = LoadCode(); if (code == null) { return; } ClassType ctype = getClassType(); string _GetValueMethodName = cmd.GetValue("method"); string _ConstKeyClassName = cmd.GetValue("kc"); string _DefaultValueClassName = cmd.GetValue("dc"); var builder = new CSharpBuilder { Namespace = NamespaceName }; builder.AddUsing("System"); builder.AddUsing("System.Collections.Generic"); string cname = ClassName; if (ctype == ClassType.TieDataContract || ctype == ClassType.JsonDataContract) { bool isExpression = ctype == ClassType.JsonDataContract; string inputPath = cmd.InputPath(); if (inputPath != null && Path.GetExtension(inputPath).ToLower() == ".json") { isExpression = true; } ConvertJson2CS(code, builder, cname, isExpression); return; } var maker = new ConfigScript(code); if ((ctype & ClassType.HierarchicalProperty) == ClassType.HierarchicalProperty) { maker.HierarchicalMemberType = CodeMemberType.Property; } else if ((ctype & ClassType.HierarchicalMethod) == ClassType.HierarchicalMethod) { maker.HierarchicalMemberType = CodeMemberType.Method; } else { maker.HierarchicalMemberType = CodeMemberType.Field; } if (_GetValueMethodName != null) { maker.GetValueMethodName = _GetValueMethodName; } if (_ConstKeyClassName != null) { maker.ConstKeyClassName = _ConstKeyClassName; } if (_DefaultValueClassName != null) { maker.DefaultValueClassName = _DefaultValueClassName; } var clss = maker.Generate(cname); builder.AddClass(clss); if (ctype == ClassType.ConstKey) { builder = CreateClass(maker.ConstKeyFields); } else if (ctype == ClassType.DefaultValue) { builder = CreateClass(maker.DefaultValueFields); } else if (ctype == ClassType.StaticField) { builder = CreateClass(maker.StaticFields); } else if (ctype == ClassType.StaticPropery) { builder = CreateClass(maker.StaticProperties); } else if (ctype == ClassType.StaticMethod) { builder = CreateClass(maker.StaticMethods); } else if (ctype == ClassType.HierarchicalField || ctype == ClassType.HierarchicalProperty || ctype == ClassType.HierarchicalMethod) { //skip, because clss has created class already } else { if ((ctype & ClassType.HierarchicalField) != ClassType.HierarchicalField && (ctype & ClassType.HierarchicalProperty) != ClassType.HierarchicalProperty && (ctype & ClassType.HierarchicalMethod) != ClassType.HierarchicalMethod ) { clss.Clear(); } if ((ctype & ClassType.StaticField) == ClassType.StaticField) { clss.AddRange(maker.StaticFields); } if ((ctype & ClassType.StaticPropery) == ClassType.StaticPropery) { clss.AddRange(maker.StaticProperties); } if ((ctype & ClassType.StaticMethod) == ClassType.StaticMethod) { clss.AddRange(maker.StaticMethods); } if ((ctype & ClassType.ConstKey) == ClassType.ConstKey) { clss.AddRange(maker.ConstKeyFields); } if ((ctype & ClassType.DefaultValue) == ClassType.DefaultValue) { clss.AddRange(maker.DefaultValueFields); } } builder.AddUsingRange(base.Usings); PrintOutput(builder, cname); }