This commit is contained in:
sunkaixuan
2017-05-29 11:50:51 +08:00
parent 87a211b7a6
commit 29821631b2
4 changed files with 109 additions and 64 deletions

View File

@@ -12,7 +12,7 @@ namespace SqlSugar
private string PropertyTemplate { get; set; } private string PropertyTemplate { get; set; }
private string PropertyDescriptionTemplate { get; set; } private string PropertyDescriptionTemplate { get; set; }
private string ConstructorTemplate { get; set; } private string ConstructorTemplate { get; set; }
private string NamespaceTemplate { get; set; } private string UsingTemplate { get; set; }
private string Namespace { get; set; } private string Namespace { get; set; }
private bool IsAttribute { get; set; } private bool IsAttribute { get; set; }
private bool IsDefaultValue { get; set; } private bool IsDefaultValue { get; set; }
@@ -20,12 +20,12 @@ namespace SqlSugar
public DbFirstProvider() public DbFirstProvider()
{ {
this.ClassTemplate = DefaultTemplate.ClassTemplate; this.ClassTemplate = DbFirstTemplate.ClassTemplate;
this.ClassDescriptionTemplate = DefaultTemplate.ClassDescriptionTemplate; this.ClassDescriptionTemplate = DbFirstTemplate.ClassDescriptionTemplate;
this.PropertyTemplate = DefaultTemplate.PropertyTemplate; this.PropertyTemplate = DbFirstTemplate.PropertyTemplate;
this.PropertyDescriptionTemplate = DefaultTemplate.PropertyDescriptionTemplate; this.PropertyDescriptionTemplate = DbFirstTemplate.PropertyDescriptionTemplate;
this.ConstructorTemplate = DefaultTemplate.ConstructorTemplate; this.ConstructorTemplate = DbFirstTemplate.ConstructorTemplate;
this.NamespaceTemplate = DefaultTemplate.NamespaceTemplate; this.UsingTemplate = DbFirstTemplate.UsingTemplate;
} }
public void Init() public void Init()
@@ -72,7 +72,7 @@ namespace SqlSugar
public IDbFirst SettingNamespaceTemplate(Func<string, string> func) public IDbFirst SettingNamespaceTemplate(Func<string, string> func)
{ {
this.NamespaceTemplate = func(this.NamespaceTemplate); this.UsingTemplate = func(this.UsingTemplate);
return this; return this;
} }
@@ -122,36 +122,59 @@ namespace SqlSugar
public Dictionary<string, string> ToClassStringList(string nameSpace = "Models") public Dictionary<string, string> ToClassStringList(string nameSpace = "Models")
{ {
this.Namespace = nameSpace;
Dictionary<string, string> result = new Dictionary<string, string>(); Dictionary<string, string> result = new Dictionary<string, string>();
if (this.TableInfoList.IsValuable()) if (this.TableInfoList.IsValuable())
{ {
foreach (var tableInfo in this.TableInfoList) 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; return result;
} }
public void CreateClassFile(string directoryPath, string nameSpace = "Models") public void CreateClassFile(string directoryPath, string nameSpace = "Models")
{ {
Check.ArgumentNullException(directoryPath, "directoryPath can't null"); Check.ArgumentNullException(directoryPath, "directoryPath can't null");
this.Namespace = nameSpace;
var classStringList = ToClassStringList(nameSpace); var classStringList = ToClassStringList(nameSpace);
if (classStringList.IsValuable()) if (classStringList.IsValuable())
{ {
foreach (var item in classStringList) foreach (var item in classStringList)
{ {
string className = item.Key; FileHeper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("{0}\\.cs", item.Key), item.Value, Encoding.UTF8);
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);
} }
} }
} }

View 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
}
}

View File

@@ -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";
}
}

View File

@@ -55,7 +55,7 @@
<Compile Include="Abstract\DbBindProvider\IDataReaderEntityBuilder.cs" /> <Compile Include="Abstract\DbBindProvider\IDataReaderEntityBuilder.cs" />
<Compile Include="Abstract\DbBindProvider\IDataRecordExtensions.cs" /> <Compile Include="Abstract\DbBindProvider\IDataRecordExtensions.cs" />
<Compile Include="Abstract\DbFirstProvider\DbFirstProvider.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\DbMaintenanceProvider\DbMaintenanceProvider.cs" />
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" /> <Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
<Compile Include="Abstract\EntityProvider\EntityProvider.cs" /> <Compile Include="Abstract\EntityProvider\EntityProvider.cs" />