private void _Do_0(Func <string, bool> doneToConfirmContinue = null) { if (string.IsNullOrEmpty(_Namespace)) { MessageBox.Show("Please provide a namespace"); return; } Func <MBColumn, string> _getType = c => { switch (c.Type) { case _Types.t_bit: if (c.Spec.Contains("(1)")) { return((c.Nullable) ? "Nullable<bool>" : "bool"); } return((c.Nullable) ? "Nullable<bool>" : "bool"); case _Types.t_boolean: return((c.Nullable) ? "Nullable<bool>" : "bool"); case _Types.t_tinyint: if (c.Spec.EndsWith("unsigned")) { return((c.Nullable) ? "Nullable<byte>" : "byte"); } return((c.Nullable) ? "Nullable<sbyte>" : "sbyte"); case _Types.t_smallint: if (c.Spec.EndsWith("unsigned")) { return((c.Nullable) ? "Nullable<int>" : "int"); } return((c.Nullable) ? "Nullable<short>" : "short"); case _Types.t_year: return((c.Nullable) ? "Nullable<short>" : "short"); case _Types.t_int: if (c.Spec.EndsWith("unsigned")) { return((c.Nullable) ? "Nullable<long>" : "long"); } return((c.Nullable) ? "Nullable<int>" : "int"); case _Types.t_integer: if (c.Spec.EndsWith("unsigned")) { return((c.Nullable) ? "Nullable<long>" : "long"); } return((c.Nullable) ? "Nullable<int>" : "int"); case _Types.t_mediumint: return((c.Nullable) ? "Nullable<int>" : "int"); case _Types.t_bigint: if (c.Spec.EndsWith("unsigned")) { return((c.Nullable) ? "Nullable<decimal>" : "decimal"); } return((c.Nullable) ? "Nullable<long>" : "long"); case _Types.t_float: if (c.Spec.EndsWith("unsigned")) { return((c.Nullable) ? "Nullable<decimal>" : "decimal"); } return((c.Nullable) ? "Nullable<float>" : "float"); case _Types.t_double: if (c.Spec.EndsWith("unsigned")) { return((c.Nullable) ? "Nullable<decimal>" : "decimal"); } return((c.Nullable) ? "Nullable<double>" : "double"); case _Types.t_real: return((c.Nullable) ? "Nullable<double>" : "double"); case _Types.t_rowversion: return("byte[]"); case _Types.t_numeric: case _Types.t_decimal: case _Types.t_dec: case _Types.t_fixed: case _Types.t_serial: return((c.Nullable) ? "Nullable<decimal>" : "decimal"); case _Types.t_date: case _Types.t_datetime: case _Types.t_datetime2: return((c.Nullable) ? "Nullable<DateTime>" : "DateTime"); case _Types.t_timestamp: if (_Context.IsMySql) { return((c.Nullable) ? "Nullable<DateTime>" : "DateTime"); } return("byte[]"); case _Types.t_datetimeoffset: return((c.Nullable) ? "Nullable<System.DateTimeOffset>" : "System.DateTimeOffset"); case _Types.t_time: return((c.Nullable) ? "Nullable<System.TimeSpan>" : "System.TimeSpan"); case _Types.t_smalldatetime: return((c.Nullable) ? "Nullable<DateTime>" : "DateTime"); case _Types.t_image: return("byte[]"); case _Types.t_money: case _Types.t_smallmoney: return((c.Nullable) ? "Nullable<decimal>" : "decimal"); case _Types.t_uniqueidentifier: return((c.Nullable) ? "Nullable<Guid>" : "Guid"); case _Types.t_char: if (_Context.IsMySql && c.Spec == "char(36)") { // char(36) 被认为是 MySql 的一个标志性实现 return((c.Nullable) ? "Nullable<Guid>" : "Guid"); } return("string"); case _Types.t_varchar: case _Types.t_tinytext: case _Types.t_text: case _Types.t_mediumtext: case _Types.t_longtext: case _Types.t_set: case _Types.t_enum: case _Types.t_nchar: case _Types.t_nvarchar: case _Types.t_ntext: case _Types.t_xml: return("string"); case _Types.t_binary: case _Types.t_varbinary: case _Types.t_tinyblob: case _Types.t_blob: case _Types.t_mediumblob: case _Types.t_longblob: return("byte[]"); case _Types.t_spatial_geometry: return((c.Nullable) ? "Nullable<System.Data.Spatial.DbGeometry>" : "System.Data.Spatial.DbGeometry"); case _Types.t_spatial_geography: return((c.Nullable) ? "Nullable<System.Data.Spatial.DbGeography>" : "System.Data.Spatial.DbGeography"); case _Types.t_sql_variant: return("object"); } return(string.Empty); }; var now = DateTime.Now; var projects = (Array)_App.ActiveSolutionProjects; if (projects.Length > 0) { foreach (Project p in projects) { ProjectItem folder = p.ProjectItems .AddFolder("___ENTITIES", Constants.vsProjectItemKindPhysicalFolder); List <MBTable> tables = _Context.Tables.Where(_Filter).OrderBy(t => t.Name).ToList(); // _App Configure // Start typing _BatchIndex = -1; ProjectItem file = null; Window win = null; TextSelection ts = null; var index = 0; var count = -1; for (int i = 0; i < tables.Count; i++) { index = i / _BatchSize; count++; if (index != _BatchIndex) { // New file and insert at the end line file = folder.ProjectItems.AddFromTemplate( DefaultTemplateFile, string.Format("{0}.{1}.cs", _Identifier, index.ToString("D2"))); win = file.Open(Constants.vsViewKindCode); win.Activate(); win.Document.Activate(); ts = _App.ActiveDocument.Selection as TextSelection; ts.EndOfDocument(); _BatchIndex = index; // Update timestamp ts.Insert(@"/// <summary>"); ts.NewLine(); ts.Insert(string.Format(@"{0}", now.ToString())); ts.NewLine(); ts.Insert(@"</summary>"); ts.NewLine(); ts.SelectLine(); ts.Insert(" "); // namespace ts.Insert("namespace " + _Namespace); ts.NewLine(); ts.Insert("{"); ts.NewLine(); } MBTable t = tables[i]; List <string> keys = new List <string>(); if (! string.IsNullOrEmpty(t.KeyInfo)) { foreach (string part in t.KeyInfo.Split(',')) { if (part.Trim().Length > 0) { keys.Add(part.Trim()); } } } var columns = _Context.Columns .Where(c => c.TableId == t.TableId).OrderBy(c => c.Name).ToList(); var properties = _Context.Properties .Where(d => d.TableId == t.TableId).ToList(); // summary if (_Context.IsMySql) { if (!string.IsNullOrEmpty(t.Caption)) { ts.Insert(@"/// <summary>"); ts.NewLine(); ts.Insert(string.Format(@"{0}", t.Caption.ToStringEx())); ts.NewLine(); ts.Insert(@"</summary>"); ts.NewLine(); ts.SelectLine(); ts.Insert(" "); } } else { properties.SingleOrDefault(d => d.TableId == t.TableId && d.Field == string.Empty && d.Name == FIELD_SUMMARY && !string.IsNullOrEmpty(d.Value)).IfNN(d => { ts.Insert(@"/// <summary>"); ts.NewLine(); ts.Insert(string.Format(@"{0}", d.Value)); ts.NewLine(); ts.Insert(@"</summary>"); ts.NewLine(); ts.SelectLine(); ts.Insert(" "); }); } // tableName ts.Insert("[Serializable]"); ts.NewLine(); ts.Insert(string.Format("[Table(\"{0}\")]", t.Name)); ts.NewLine(); ts.Insert(string.Format("public partial class TB_{0}:TBObject<TB_{0}>{{", t.Name)); ts.NewLine(); //ts.Insert(string.Format("public partial class ET_{0} {{", t.Name)); //ts.NewLine(); columns.ForEach(c => { if (_Context.IsMySql) { if (!string.IsNullOrEmpty(c.Caption)) { ts.Insert(@"/// <summary>"); ts.NewLine(); ts.Insert(string.Format(@"{0}", c.Caption)); ts.NewLine(); ts.Insert(@"</summary>"); ts.NewLine(); ts.SelectLine(); ts.Insert(" "); } } else { // 说明 properties.SingleOrDefault(d => d.TableId == t.TableId && d.Field == c.Name && d.Name == FIELD_SUMMARY && !string.IsNullOrEmpty(d.Value)).IfNN(d => { ts.Insert(@"/// <summary>"); ts.NewLine(); ts.Insert(string.Format(@"{0}", d.Value)); ts.NewLine(); ts.Insert(@"</summary>"); ts.NewLine(); ts.SelectLine(); ts.Insert(" "); }); } if (t.KeyInfo.ToStringEx(string.Empty).Contains(c.Name)) { //var singleKey = !t.KeyInfo.ToStringEx(string.Empty).Contains(","); //if (singleKey && c.Type.Contains("int")) //{ // ts.Insert(@"[Key*]"); // 人为编译不成功,mySql 的问题 //} //else //{ // ts.Insert(@"[Key]"); //} ts.Insert(@"[Key]"); ts.NewLine(); } ts.Insert(string.Format(@"[Column(Order = {0})]", c.Ordinal)); ts.NewLine(); if (c.CharMaxLength.HasValue && !c.Type.Contains("blob") && !c.Type.Contains("long") && !c.Type.Contains("text") //!c.Spec.Contains("char(36)") // guid ) { ts.Insert(string.Format(@"[MaxLength({0})]", c.CharMaxLength)); ts.NewLine(); } var s = "public "; s += _getType(c) + " "; s += c.Name; s += " { get; set; }"; ts.Insert(s); ts.NewLine(); }); ts.Insert("}"); ts.NewLine(); if (doneToConfirmContinue != null) { if (!doneToConfirmContinue(t.Name)) { break; } } if (count == _BatchSize - 1) { ts.Insert("}"); ts.NewLine(); ts.SelectAll(); _App.ExecuteCommand("Edit.FormatDocument"); win.Close(vsSaveChanges.vsSaveChangesYes); count = -1; } } // closing if (count != -1) { ts.Insert("}"); ts.NewLine(); ts.SelectAll(); _App.ExecuteCommand("Edit.FormatDocument"); win.Close(vsSaveChanges.vsSaveChangesYes); count = -1; } } } }
private void _Do_1(Func <string, bool> doneToConfirmContinue = null) { if (string.IsNullOrEmpty(_Namespace)) { MessageBox.Show("Please provide a namespace"); return; } var now = DateTime.Now; var projects = (Array)_App.ActiveSolutionProjects; if (projects.Length > 0) { foreach (Project p in projects) { ProjectItem folder = p.ProjectItems .AddFolder("___ENTITIES", Constants.vsProjectItemKindPhysicalFolder); List <MBTable> tables = _Context.Tables.Where(_Filter).OrderBy(t => t.Name).ToList(); // _App Configure // 准备插入代码 _BatchIndex = -1; ProjectItem file = null; Window win = null; TextSelection ts = null; StringBuilder sb = null; var index = 0; var count = -1; for (int i = 0; i < tables.Count; i++) { index = i / _BatchSize; count++; if (index != _BatchIndex) { // 创建新文件,并且在最后方插入 file = folder.ProjectItems.AddFromTemplate( DefaultTemplateFile, string.Format("{0}.{1}.cs", _Identifier, "ALL")); win = file.Open(Constants.vsViewKindCode); win.Activate(); win.Document.Activate(); ts = _App.ActiveDocument.Selection as TextSelection; ts.EndOfDocument(); _BatchIndex = index; sb = new StringBuilder(); // 插入生成日期 sb.AppendLine(@"/// <summary>"); sb.AppendLine(string.Format(@"/// {0}", now.ToString())); sb.AppendLine(@"/// </summary>"); // 插入 namespace 行 sb.AppendLine("namespace " + _Namespace); sb.AppendLine("{"); } MBTable t = tables[i]; List <string> keys = new List <string>(); if (! string.IsNullOrEmpty(t.KeyInfo)) { foreach (string part in t.KeyInfo.Split(',')) { if (part.Trim().Length > 0) { keys.Add(part.Trim()); } } } var columns = _Context.Columns .Where(c => c.TableId == t.TableId).OrderBy(c => c.Name).ToList(); var properties = _Context.Properties .Where(d => d.TableId == t.TableId).ToList(); // 说明 if (_Context.IsMySql) { if (!string.IsNullOrEmpty(t.Caption)) { sb.AppendLine(@"/// <summary>"); sb.AppendLine(string.Format(@"/// {0}", t.Caption.ToStringEx())); sb.AppendLine(@"/// </summary>"); } } else { properties.SingleOrDefault(d => d.TableId == t.TableId && d.Field == string.Empty && d.Name == FIELD_SUMMARY && !string.IsNullOrEmpty(d.Value)).IfNN(d => { sb.AppendLine(@"/// <summary>"); sb.AppendLine(string.Format(@"/// {0}", d.Value)); sb.AppendLine(@"/// </summary>"); }); } // 表格名字 sb.AppendLine("[Serializable]"); sb.AppendLine(string.Format("[Table(\"{0}\")]", t.Name)); sb.AppendLine(string.Format("public partial class TB_{0}:TBObject<TB_{0}>{{", t.Name)); //sb.AppendLine(string.Format("public partial class ET_{0} {{", t.Name)); columns.ForEach(c => { if (_Context.IsMySql) { if (!string.IsNullOrEmpty(c.Caption)) { sb.AppendLine(@"/// <summary>"); sb.AppendLine(string.Format(@"/// {0}", c.Caption)); sb.AppendLine(@"/// </summary>"); } } else { // 说明 properties.SingleOrDefault(d => d.TableId == t.TableId && d.Field == c.Name && d.Name == FIELD_SUMMARY && !string.IsNullOrEmpty(d.Value)).IfNN(d => { sb.AppendLine(@"/// <summary>"); sb.AppendLine(string.Format(@"/// {0}", d.Value)); sb.AppendLine(@"/// </summary>"); }); } if (t.KeyInfo.ToStringEx(string.Empty).Contains(c.Name)) { //var singleKey = !t.KeyInfo.ToStringEx(string.Empty).Contains(","); //if (singleKey && c.Type.Contains("int")) //{ // sb.AppendLine(@"[Key*]"); // 人为编译不成功,mySql 的问题 //} //else //{ // sb.AppendLine(@"[Key]"); //} sb.AppendLine(@"[Key]"); } sb.AppendLine(string.Format(@"[Column(Order = {0})]", c.Ordinal)); if (c.CharMaxLength.HasValue && !c.Type.Contains("blob") && !c.Type.Contains("long") && !c.Type.Contains("text") //!c.Spec.Contains("char(36)") // guid ) { sb.AppendLine(string.Format(@"[MaxLength({0})]", c.CharMaxLength)); } var s = "public "; s += _getType(c) + " "; s += c.Name; s += " { get; set; }"; sb.AppendLine(s); }); sb.AppendLine("}"); if (doneToConfirmContinue != null) { if (!doneToConfirmContinue(t.Name)) { break; } } if (count == _BatchSize - 1) { sb.AppendLine("}"); ts.Insert(sb.ToString()); ts.SelectAll(); _App.ExecuteCommand("Edit.FormatDocument"); win.Close(vsSaveChanges.vsSaveChangesYes); count = -1; } } // closing if (count != -1) { sb.AppendLine("}"); ts.Insert(sb.ToString()); ts.SelectAll(); _App.ExecuteCommand("Edit.FormatDocument"); win.Close(vsSaveChanges.vsSaveChangesYes); count = -1; } } } }