From 29821631b2c68f7f84d5117592889972aab815ad Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 29 May 2017 11:50:51 +0800 Subject: [PATCH] - --- .../DbFirstProvider/DbFirstProvider.cs | 65 +++++++++++++------ .../DbFirstProvider/DbFirstTemplate.cs | 64 ++++++++++++++++++ .../DbFirstProvider/DefaultTemplate.cs | 42 ------------ SqlSugar/SqlSugar.csproj | 2 +- 4 files changed, 109 insertions(+), 64 deletions(-) create mode 100644 SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs delete mode 100644 SqlSugar/Abstract/DbFirstProvider/DefaultTemplate.cs diff --git a/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs b/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs index ec351f9be..f54565f25 100644 --- a/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs +++ b/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs @@ -12,7 +12,7 @@ namespace SqlSugar private string PropertyTemplate { get; set; } private string PropertyDescriptionTemplate { get; set; } private string ConstructorTemplate { get; set; } - private string NamespaceTemplate { get; set; } + private string UsingTemplate { get; set; } private string Namespace { get; set; } private bool IsAttribute { get; set; } private bool IsDefaultValue { get; set; } @@ -20,12 +20,12 @@ namespace SqlSugar public DbFirstProvider() { - this.ClassTemplate = DefaultTemplate.ClassTemplate; - this.ClassDescriptionTemplate = DefaultTemplate.ClassDescriptionTemplate; - this.PropertyTemplate = DefaultTemplate.PropertyTemplate; - this.PropertyDescriptionTemplate = DefaultTemplate.PropertyDescriptionTemplate; - this.ConstructorTemplate = DefaultTemplate.ConstructorTemplate; - this.NamespaceTemplate = DefaultTemplate.NamespaceTemplate; + this.ClassTemplate = DbFirstTemplate.ClassTemplate; + this.ClassDescriptionTemplate = DbFirstTemplate.ClassDescriptionTemplate; + this.PropertyTemplate = DbFirstTemplate.PropertyTemplate; + this.PropertyDescriptionTemplate = DbFirstTemplate.PropertyDescriptionTemplate; + this.ConstructorTemplate = DbFirstTemplate.ConstructorTemplate; + this.UsingTemplate = DbFirstTemplate.UsingTemplate; } public void Init() @@ -72,7 +72,7 @@ namespace SqlSugar public IDbFirst SettingNamespaceTemplate(Func func) { - this.NamespaceTemplate = func(this.NamespaceTemplate); + this.UsingTemplate = func(this.UsingTemplate); return this; } @@ -122,36 +122,59 @@ namespace SqlSugar public Dictionary ToClassStringList(string nameSpace = "Models") { + this.Namespace = nameSpace; Dictionary result = new Dictionary(); if (this.TableInfoList.IsValuable()) { foreach (var tableInfo in this.TableInfoList) { - + var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name); + string className = tableInfo.Name; + string classText = this.ClassTemplate; + string ConstructorText = DbFirstTemplate.ConstructorTemplate; + if (this.Context.MappingTables.IsValuable()) + { + var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(tableInfo.Name, StringComparison.CurrentCultureIgnoreCase)); + if (mappingInfo.IsValuable()) + { + className = mappingInfo.EntityName; + } + if (mappingInfo != null) + { + classText = classText.Replace(DbFirstTemplate.KeyClassName, mappingInfo.EntityName); + } + } + classText = classText.Replace(DbFirstTemplate.KeyClassName, className); + classText = classText.Replace(DbFirstTemplate.KeyNamespace, this.Namespace); + classText = classText.Replace(DbFirstTemplate.KeyUsing, IsAttribute ? (this.UsingTemplate + "using " + PubConst.AssemblyName + "\r\t") : this.UsingTemplate); + classText = classText.Replace(DbFirstTemplate.KeyClassDescription, tableInfo.Description); + classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, tableInfo.Name) : null); + if (columns.IsValuable()) + { + foreach (var item in columns) + { + string PropertyText = DbFirstTemplate.PropertyTemplate; + string PropertyDescriptionText = DbFirstTemplate.PropertyDescriptionTemplate; + string SugarColumnText = DbFirstTemplate.ValueSugarCoulmn; + var hasSugarColumn = item.IsPrimarykey == true || item.IsIdentity == true||item.DbColumnName.IsValuable(); + PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyType,item.DataType); + PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyName, item.DbColumnName); + } + } + result.Add(className, classText); } } - this.Namespace = nameSpace; return result; } public void CreateClassFile(string directoryPath, string nameSpace = "Models") { Check.ArgumentNullException(directoryPath, "directoryPath can't null"); - this.Namespace = nameSpace; var classStringList = ToClassStringList(nameSpace); if (classStringList.IsValuable()) { foreach (var item in classStringList) { - string className = item.Key; - if (this.Context.MappingTables.IsValuable()) - { - var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(item.Key, StringComparison.CurrentCultureIgnoreCase)); - if (mappingInfo.IsValuable()) - { - className = mappingInfo.EntityName; - } - } - FileHeper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("{0}\\.cs", className), item.Value, Encoding.UTF8); + FileHeper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("{0}\\.cs", item.Key), item.Value, Encoding.UTF8); } } } diff --git a/SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs b/SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs new file mode 100644 index 000000000..75f4ed21e --- /dev/null +++ b/SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SqlSugar +{ + public class DbFirstTemplate + { + #region Template + public static string ClassTemplate = @"{using} +namespace {Namespace} +{ + {ClassDescription}{SugarTable} + public class {ClassName} + { + public {ClassName}(){ + {Constructor} + } + {PropertyDescription} + {Property} + } +}"; + public static string ClassDescriptionTemplate = @" /// + ///{ClassDescription} + /// \r\n"; + + public static string PropertyTemplate = @" {SugarColumn} +public {PropertyType} {PropertyName} {get;set;}\r\n"; + + public static string PropertyDescriptionTemplate = @"/// + /// Desc:{PropertyDescription} + /// Default:{DefaultValue} + /// Nullable:{IsNullable} + /// \r\n"; + + public static string ConstructorTemplate = @" this.{$PropertyName} =Convert.To{PropertyType}(""{DefaultValue}"");\r\n"; + + public static string UsingTemplate = @"using System; +using System.Linq; +using System.Text;"+"\r\n"; + #endregion + + #region Replace Key + public const string KeySugarTable = "{SugarTable}"; + public const string KeyClassName = "{ClassName}"; + public const string KeyConstructor = "{Constructor}"; + public const string KeyPropertyDescription = "{PropertyDescription}"; + public const string KeyClassDescription = "{ClassDescription}"; + public const string KeySugarColumn = "{SugarColumn}"; + public const string KeyPropertyType = "{PropertyType}"; + public const string KeyPropertyName = "{PropertyName}"; + public const string KeyDefaultValue = "{DefaultValue}"; + public const string KeyIsNullable = "{IsNullable}"; + public const string KeyNamespace = "{Namespace}"; + public const string KeyUsing = "{using}"; + #endregion + + #region Replace Value + public const string ValueSugarTable = "[SugarTable(\"{0}\")]"; + public const string ValueSugarCoulmn= "[SugarColumn({0})]"; + #endregion + } +} diff --git a/SqlSugar/Abstract/DbFirstProvider/DefaultTemplate.cs b/SqlSugar/Abstract/DbFirstProvider/DefaultTemplate.cs deleted file mode 100644 index f8c07d9fe..000000000 --- a/SqlSugar/Abstract/DbFirstProvider/DefaultTemplate.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace SqlSugar -{ - public class DefaultTemplate - { - public static string ClassTemplate = @"namespace {Namespace} -{ - {ClassPropertyDescription} - {SugarTable} - public class {ClassName} - { - public {ClassName}(){ - {Constructor} - } - {PropertyDescription} - {Property} - } -}"; - public static string ClassDescriptionTemplate = @" /// - ///{ClassDescription} - /// \r\n"; - - public static string PropertyTemplate = @" {SugarColumn} -public {PropertyType} {PropertyName} {get;set;}\r\n"; - - public static string PropertyDescriptionTemplate = @"/// - /// Desc:{PropertyDescription} - /// Default:{DefaultValue} - /// Nullable:{IsNullable} - /// \r\n"; - - public static string ConstructorTemplate = @" this.$PropertyName =Convert.To{PropertyType}(""{DefaultValue}"");\r\n"; - - public static string NamespaceTemplate = @"using System; - using System.Linq; - using System.Text;\r\n"; - } -} diff --git a/SqlSugar/SqlSugar.csproj b/SqlSugar/SqlSugar.csproj index 0a24fb4ec..614e1abd1 100644 --- a/SqlSugar/SqlSugar.csproj +++ b/SqlSugar/SqlSugar.csproj @@ -55,7 +55,7 @@ - +