diff --git a/Src/Asp.Net/ExtensionsDemo/Config.cs b/Src/Asp.Net/ExtensionsDemo/Config.cs index d7e34e6ef..760e45df0 100644 --- a/Src/Asp.Net/ExtensionsDemo/Config.cs +++ b/Src/Asp.Net/ExtensionsDemo/Config.cs @@ -8,6 +8,6 @@ namespace ExtensionsDemo { public class Config { - public static string ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest"; + public static string ConnectionString = "server=.;uid=sa;pwd=haosql;database=SqlSugar4XTest"; } } diff --git a/Src/Asp.Net/ExtensionsDemo/DbFirstDemo.cs b/Src/Asp.Net/ExtensionsDemo/DbFirstDemo.cs new file mode 100644 index 000000000..9b966f8a7 --- /dev/null +++ b/Src/Asp.Net/ExtensionsDemo/DbFirstDemo.cs @@ -0,0 +1,30 @@ +using SqlSugar; +using SqlSugar.DbFirstExtensions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExtensionsDemo +{ + public class DbFirstDemo + { + public static void Init() + { + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + ConfigureExternalServices = new ConfigureExternalServices() + { + RazorService = new RazorService() + } + }); + + db.DbFirst.UseRazorAnalysis(RazorFirst.DefaultRazorClassTemplate).CreateClassFile("c:\\Demo\\Razor\\"); + } + } +} + diff --git a/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj b/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj index b89c37ef4..84a297da9 100644 --- a/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj +++ b/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj @@ -45,11 +45,16 @@ + + + {629cdf51-682f-4b22-843a-ba76c232accd} + SqlSugar.DbFirstExtensions + {cdb72abe-0336-4730-a195-abf2611deeaa} SqlSugar.Extensions.DataCache diff --git a/Src/Asp.Net/ExtensionsDemo/Program.cs b/Src/Asp.Net/ExtensionsDemo/Program.cs index cf6187e8d..2b0dcf347 100644 --- a/Src/Asp.Net/ExtensionsDemo/Program.cs +++ b/Src/Asp.Net/ExtensionsDemo/Program.cs @@ -11,6 +11,7 @@ namespace ExtensionsDemo static void Main(string[] args) { CacheDemo.Init(); + DbFirstDemo.Init(); } } } diff --git a/Src/Asp.Net/SqlSugar.DbFirstExtensions/Properties/AssemblyInfo.cs b/Src/Asp.Net/SqlSugar.DbFirstExtensions/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..9ef9617b6 --- /dev/null +++ b/Src/Asp.Net/SqlSugar.DbFirstExtensions/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("SqlSugar.DbFirstExtensions")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SqlSugar.DbFirstExtensions")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("629cdf51-682f-4b22-843a-ba76c232accd")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Src/Asp.Net/SqlSugar.DbFirstExtensions/RazorService.cs b/Src/Asp.Net/SqlSugar.DbFirstExtensions/RazorService.cs new file mode 100644 index 000000000..84f7faeda --- /dev/null +++ b/Src/Asp.Net/SqlSugar.DbFirstExtensions/RazorService.cs @@ -0,0 +1,40 @@ +using RazorEngine; +using RazorEngine.Templating; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar.DbFirstExtensions +{ + public class RazorService : IRazorService + { + public List> GetClassStringList(string razorTemplate, List model) + { + if (model != null && model.Any()) + { + var result = new List>(); + foreach (var item in model) + { + try + { + item.ClassName = item.DbTableName;//Format Class Name + string key = "RazorService.GetClassStringList"+ razorTemplate.Length; + var classString = Engine.Razor.RunCompile(razorTemplate, key, item.GetType(), item); + result.Add(new KeyValuePair(item.ClassName,classString)); + } + catch (Exception ex) + { + new Exception(item.DbTableName + " error ." + ex.Message); + } + } + return result; + } + else + { + return new List> (); + } + } + } +} diff --git a/Src/Asp.Net/SqlSugar.DbFirstExtensions/SqlSugar.DbFirstExtensions.csproj b/Src/Asp.Net/SqlSugar.DbFirstExtensions/SqlSugar.DbFirstExtensions.csproj new file mode 100644 index 000000000..82bee53bb --- /dev/null +++ b/Src/Asp.Net/SqlSugar.DbFirstExtensions/SqlSugar.DbFirstExtensions.csproj @@ -0,0 +1,63 @@ + + + + + Debug + AnyCPU + {629CDF51-682F-4B22-843A-BA76C232ACCD} + Library + Properties + SqlSugar.DbFirstExtensions + SqlSugar.DbFirstExtensions + v4.5 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\RazorEngine.3.10.0\lib\net45\RazorEngine.dll + + + + + ..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll + + + + + + + + + + + + + + + + + + {489bb790-226c-4fad-8d1e-51d72a7ff8e5} + SqlSugar + + + + \ No newline at end of file diff --git a/Src/Asp.Net/SqlSugar.DbFirstExtensions/packages.config b/Src/Asp.Net/SqlSugar.DbFirstExtensions/packages.config new file mode 100644 index 000000000..341d85750 --- /dev/null +++ b/Src/Asp.Net/SqlSugar.DbFirstExtensions/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Src/Asp.Net/SqlSugar.sln b/Src/Asp.Net/SqlSugar.sln index d4032832a..aafa68147 100644 --- a/Src/Asp.Net/SqlSugar.sln +++ b/Src/Asp.Net/SqlSugar.sln @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar.Extensions.DataCac EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NugetTest", "NugetTest\NugetTest.csproj", "{A7202527-A5CC-4F9B-B8F2-63520892F01D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar.DbFirstExtensions", "SqlSugar.DbFirstExtensions\SqlSugar.DbFirstExtensions.csproj", "{629CDF51-682F-4B22-843A-BA76C232ACCD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -65,6 +67,10 @@ Global {A7202527-A5CC-4F9B-B8F2-63520892F01D}.Debug|Any CPU.Build.0 = Debug|Any CPU {A7202527-A5CC-4F9B-B8F2-63520892F01D}.Release|Any CPU.ActiveCfg = Release|Any CPU {A7202527-A5CC-4F9B-B8F2-63520892F01D}.Release|Any CPU.Build.0 = Release|Any CPU + {629CDF51-682F-4B22-843A-BA76C232ACCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {629CDF51-682F-4B22-843A-BA76C232ACCD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {629CDF51-682F-4B22-843A-BA76C232ACCD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {629CDF51-682F-4B22-843A-BA76C232ACCD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -72,6 +78,7 @@ Global GlobalSection(NestedProjects) = preSolution {EBBA686A-C1F0-4823-85EB-32EB306ADD7F} = {B762D1DA-DFCD-4597-A14D-4F20DA0EE70E} {CDB72ABE-0336-4730-A195-ABF2611DEEAA} = {B762D1DA-DFCD-4597-A14D-4F20DA0EE70E} + {629CDF51-682F-4B22-843A-BA76C232ACCD} = {B762D1DA-DFCD-4597-A14D-4F20DA0EE70E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {AE9BE6D6-3952-4582-B65E-83E0E57FDC8A} diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs index 0dc008bde..368889249 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs @@ -89,10 +89,54 @@ namespace SqlSugar this.PropertyTemplate = func(this.PropertyTemplate); return this; } - public DbRazor SettingTemplate(string templatePath) + public RazorFirst UseRazorAnalysis(string razorClassTemplate,string classNamespace="Models" ) { - Check.Exception(true, ErrorMessage.GetThrowMessage("Use db.DbFirst.LoadTemplate('c:\\template.txt').CreateClassFile(classDirectoryPath) Nuget Install SqlSugar.DbFirstRazor ", "当前方法已经过期,请使用 db.DbFirst.LoadTemplate('c:\\template.txt').CreateClassFile(classDirectoryPath) Nuget 安装 SqlSugar.DbFirstRazor ")); - return null; + if (razorClassTemplate == null) + { + razorClassTemplate = ""; + } + razorClassTemplate=razorClassTemplate.Replace("@Model.Namespace", classNamespace); + var result = new RazorFirst(); + if (this.Context.CurrentConnectionConfig.ConfigureExternalServices?.RazorService != null) + { + List razorList = new List(); + var tables = this.Context.DbMaintenance.GetTableInfoList(false); + if (tables.HasValue()) + { + foreach (var item in tables) + { + var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(item.Name, false); + RazorTableInfo table = new RazorTableInfo() + { + Columns = columns.Select(it => new RazorColumnInfo() + { + ColumnDescription = it.ColumnDescription, + DataType = it.DataType, + DbColumnName = it.DbColumnName, + DefaultValue = it.DefaultValue, + IsIdentity = it.IsIdentity, + IsNullable = it.IsNullable, + IsPrimarykey = it.IsPrimarykey, + Length = it.Length + }).ToList(), + Description = item.Description, + DbTableName = item.Name + }; + foreach (var col in table.Columns) + { + col.DataType = GetPropertyTypeName(columns.First(it=>it.DbColumnName==col.DbColumnName)); + } + razorList.Add(table); + } + } + result.ClassStringList = this.Context.CurrentConnectionConfig.ConfigureExternalServices.RazorService.GetClassStringList(razorClassTemplate, razorList); + } + else + { + Check.Exception(true, ErrorMessage.GetThrowMessage("Need to achieve ConnectionConfig.ConfigureExternal Services.RazorService", "需要实现 ConnectionConfig.ConfigureExternal Services.RazorService接口")); + } + this.Context.Utilities.RemoveCacheAll(); + return result; } #endregion @@ -127,7 +171,7 @@ namespace SqlSugar { if (objectNames.HasValue()) { - this.TableInfoList = this.TableInfoList.Where(it => objectNames.Select(x=>x.ToLower()).Contains(it.Name.ToLower())).ToList(); + this.TableInfoList = this.TableInfoList.Where(it => objectNames.Select(x => x.ToLower()).Contains(it.Name.ToLower())).ToList(); } return this; } @@ -226,7 +270,7 @@ namespace SqlSugar classText = classText.Replace(DbFirstTemplate.KeyClassName, className); classText = classText.Replace(DbFirstTemplate.KeyNamespace, this.Namespace); classText = classText.Replace(DbFirstTemplate.KeyUsing, IsAttribute ? (this.UsingTemplate + "using " + UtilConstants.AssemblyName + ";\r\n") : this.UsingTemplate); - classText = classText.Replace(DbFirstTemplate.KeyClassDescription, this.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription,"\r\n")); + classText = classText.Replace(DbFirstTemplate.KeyClassDescription, this.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription, "\r\n")); classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, className) : null); if (columns.HasValue()) { @@ -237,7 +281,7 @@ namespace SqlSugar string PropertyText = this.PropertyTemplate; string PropertyDescriptionText = this.PropertyDescriptionTemplate; string propertyName = GetPropertyName(item); - string propertyTypeName =item.PropertyName; + string propertyTypeName = item.PropertyName; PropertyText = GetPropertyText(item, PropertyText); PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText); PropertyText = PropertyDescriptionText + PropertyText; @@ -260,7 +304,7 @@ namespace SqlSugar } public void CreateClassFile(string directoryPath, string nameSpace = "Models") { - var seChar= Path.DirectorySeparatorChar.ToString(); + var seChar = Path.DirectorySeparatorChar.ToString(); Check.ArgumentNullException(directoryPath, "directoryPath can't null"); var classStringList = ToClassStringList(nameSpace); if (classStringList.IsValuable()) @@ -296,7 +340,7 @@ namespace SqlSugar result = "DateTime.Now"; } result = result.Replace("\r", "\t").Replace("\n", "\t"); - result = result.IsIn("''","\"\"") ? string.Empty : result; + result = result.IsIn("''", "\"\"") ? string.Empty : result; return result; } private string GetPropertyText(DbColumnInfo item, string PropertyText) @@ -351,12 +395,13 @@ namespace SqlSugar } private string GetPropertyTypeName(DbColumnInfo item) { - string result =item.PropertyType!=null?item.PropertyType.Name:this.Context.Ado.DbBind.GetPropertyTypeName(item.DataType); + string result = item.PropertyType != null ? item.PropertyType.Name : this.Context.Ado.DbBind.GetPropertyTypeName(item.DataType); if (result != "string" && result != "byte[]" && result != "object" && item.IsNullable) { result += "?"; } - if (result == "Int32") { + if (result == "Int32") + { result = "int"; } if (result == "String") @@ -370,11 +415,12 @@ namespace SqlSugar var convertString = GetProertypeDefaultValue(item); if (convertString == "DateTime.Now" || convertString == null) return convertString; - if (convertString.ObjToString() == "newid()") { + if (convertString.ObjToString() == "newid()") + { return "Guid.NewGuid()"; } if (item.DataType == "bit") - return (convertString == "1" || convertString.Equals("true",StringComparison.CurrentCultureIgnoreCase)).ToString().ToLower(); + return (convertString == "1" || convertString.Equals("true", StringComparison.CurrentCultureIgnoreCase)).ToString().ToLower(); string result = this.Context.Ado.DbBind.GetConvertString(item.DataType) + "(\"" + convertString + "\")"; return result; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbRazor.cs b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbRazor.cs index 4d51cc570..7e5c6ddb5 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbRazor.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbRazor.cs @@ -1,15 +1,92 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace SqlSugar +namespace SqlSugar { - public class DbRazor + public class RazorFirst { - public void CreateClassFile(string directoryPath) { + internal List> ClassStringList { get; set; } + public static string DefaultRazorClassTemplate = +@"using System; +using System.Linq; +using System.Text; +using SqlSugar; +namespace @Model.Namespace +{ + /// + /// + /// + public partial class @Model.ClassName + { + public @(Model.ClassName)(){ + + + } + @foreach (var item in @Model.Columns) + { + if(item.IsPrimarykey&&item.IsIdentity){ + @:/// + @:/// Desc:@item.ColumnDescription + @:/// Default:@item.DefaultValue + @:/// Nullable:@item.IsNullable + @:/// + @:[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + @:public @item.DataType @item.DbColumnName {get;set;} + } + else if(item.IsPrimarykey) + { + @:/// + @:/// Desc:@item.ColumnDescription + @:/// Default:@item.DefaultValue + @:/// Nullable:@item.IsNullable + @:/// + @:[SqlSugar.SugarColumn(IsPrimaryKey = true)] + @:public @item.DataType @item.DbColumnName {get;set;} + } + else if(item.IsIdentity) + { + @:/// + @:/// Desc:@item.ColumnDescription + @:/// Default:@item.DefaultValue + @:/// Nullable:@item.IsNullable + @:/// + @:[SqlSugar.SugarColumn(IsIdentity = true)] + @:public @item.DataType @item.DbColumnName {get;set;} + } + else + { + @:/// + @:/// Desc:@item.ColumnDescription + @:/// Default:@item.DefaultValue + @:/// Nullable:@item.IsNullable + @:/// + @:public @item.DataType @item.DbColumnName {get;set;} + } + } + + } +}"; + + public void CreateClassFile(string directoryPath) + { + var seChar = Path.DirectorySeparatorChar.ToString(); + if (ClassStringList.HasValue()) + { + foreach (var item in ClassStringList) + { + var filePath = directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format(seChar + "{0}.cs", item.Key); + FileHelper.CreateFile(filePath, item.Value, Encoding.UTF8); + } + } + } + public List> GetClassStringList() + { + return ClassStringList; } } } diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs index 532d26574..c34910cd7 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs @@ -69,6 +69,19 @@ namespace SqlSugar private ISerializeService _SerializeService; private ICacheService _ReflectionInoCache; private ICacheService _DataInfoCache; + private IRazorService _RazorService; + + public IRazorService RazorService + { + get + { + if (_RazorService == null) + return _RazorService; + else + return _RazorService; + } + set { _RazorService = value; } + } public ISerializeService SerializeService { diff --git a/Src/Asp.Net/SqlSugar/Entities/DbTableInfo.cs b/Src/Asp.Net/SqlSugar/Entities/DbTableInfo.cs index 0dd4c7068..cb8feadb8 100644 --- a/Src/Asp.Net/SqlSugar/Entities/DbTableInfo.cs +++ b/Src/Asp.Net/SqlSugar/Entities/DbTableInfo.cs @@ -11,4 +11,24 @@ namespace SqlSugar public string Description { get; set; } public DbObjectType DbObjectType { get; set; } } + + public class RazorTableInfo + { + public string DbTableName { get; set; } + public string ClassName { get; set; } + public string Description { get; set; } + public DbObjectType DbObjectType { get; set; } + public List Columns { get; set; } + } + + public class RazorColumnInfo { + public string DbColumnName { get; set; } + public string DataType { get; set; } + public int Length { get; set; } + public string ColumnDescription { get; set; } + public string DefaultValue { get; set; } + public bool IsNullable { get; set; } + public bool IsIdentity { get; set; } + public bool IsPrimarykey { get; set; } + } } diff --git a/Src/Asp.Net/SqlSugar/ExternalServiceInterface/IRazorService.cs b/Src/Asp.Net/SqlSugar/ExternalServiceInterface/IRazorService.cs new file mode 100644 index 000000000..98dd9e22d --- /dev/null +++ b/Src/Asp.Net/SqlSugar/ExternalServiceInterface/IRazorService.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public interface IRazorService + { + List> GetClassStringList(string razorTemplate, List model); + } +} diff --git a/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs b/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs index c558c7b8f..53978fccf 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs @@ -13,7 +13,7 @@ namespace SqlSugar IDbFirst SettingPropertyDescriptionTemplate(Func func); IDbFirst SettingConstructorTemplate(Func func); IDbFirst SettingNamespaceTemplate(Func func); - DbRazor SettingTemplate(string Path); + RazorFirst UseRazorAnalysis(string razorClassString, string classNamespace = "Models"); IDbFirst IsCreateAttribute(bool isCreateAttribute = true); IDbFirst IsCreateDefaultValue(bool isCreateDefaultValue=true); IDbFirst Where(params string[] objectNames); diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 47b1f627c..359b83f96 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -121,6 +121,7 @@ +