mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-01 19:03:58 +08:00
-
This commit is contained in:
@@ -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<string, string> func)
|
||||
{
|
||||
this.NamespaceTemplate = func(this.NamespaceTemplate);
|
||||
this.UsingTemplate = func(this.UsingTemplate);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -122,36 +122,59 @@ namespace SqlSugar
|
||||
|
||||
public Dictionary<string, string> ToClassStringList(string nameSpace = "Models")
|
||||
{
|
||||
this.Namespace = nameSpace;
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
64
SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs
Normal file
64
SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs
Normal file
@@ -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 = @" /// <summary>
|
||||
///{ClassDescription}
|
||||
/// </summary>\r\n";
|
||||
|
||||
public static string PropertyTemplate = @" {SugarColumn}
|
||||
public {PropertyType} {PropertyName} {get;set;}\r\n";
|
||||
|
||||
public static string PropertyDescriptionTemplate = @"/// <summary>
|
||||
/// Desc:{PropertyDescription}
|
||||
/// Default:{DefaultValue}
|
||||
/// Nullable:{IsNullable}
|
||||
/// </summary>\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
|
||||
}
|
||||
}
|
||||
@@ -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 = @" /// <summary>
|
||||
///{ClassDescription}
|
||||
/// </summary>\r\n";
|
||||
|
||||
public static string PropertyTemplate = @" {SugarColumn}
|
||||
public {PropertyType} {PropertyName} {get;set;}\r\n";
|
||||
|
||||
public static string PropertyDescriptionTemplate = @"/// <summary>
|
||||
/// Desc:{PropertyDescription}
|
||||
/// Default:{DefaultValue}
|
||||
/// Nullable:{IsNullable}
|
||||
/// </summary>\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";
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@
|
||||
<Compile Include="Abstract\DbBindProvider\IDataReaderEntityBuilder.cs" />
|
||||
<Compile Include="Abstract\DbBindProvider\IDataRecordExtensions.cs" />
|
||||
<Compile Include="Abstract\DbFirstProvider\DbFirstProvider.cs" />
|
||||
<Compile Include="Abstract\DbFirstProvider\DefaultTemplate.cs" />
|
||||
<Compile Include="Abstract\DbFirstProvider\DbFirstTemplate.cs" />
|
||||
<Compile Include="Abstract\DbMaintenanceProvider\DbMaintenanceProvider.cs" />
|
||||
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
|
||||
<Compile Include="Abstract\EntityProvider\EntityProvider.cs" />
|
||||
|
||||
Reference in New Issue
Block a user