Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
cb4bc9c3ef
  1. 43
      SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs
  2. 2
      SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs
  3. 1
      SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs
  4. 1
      SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs

43
SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs

@ -2,6 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar namespace SqlSugar
{ {
public abstract partial class DbFirstProvider : IDbFirst public abstract partial class DbFirstProvider : IDbFirst
@ -16,6 +18,13 @@ namespace SqlSugar
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; }
private ISqlBuilder SqlBuilder
{
get
{
return InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
}
}
private List<DbTableInfo> TableInfoList { get; set; } private List<DbTableInfo> TableInfoList { get; set; }
public DbFirstProvider() public DbFirstProvider()
@ -131,7 +140,7 @@ namespace SqlSugar
var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name); var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name);
string className = tableInfo.Name; string className = tableInfo.Name;
string classText = this.ClassTemplate; string classText = this.ClassTemplate;
string ConstructorText =IsDefaultValue? DbFirstTemplate.ConstructorTemplate:null; string ConstructorText = IsDefaultValue ? DbFirstTemplate.ConstructorTemplate : null;
if (this.Context.MappingTables.IsValuable()) if (this.Context.MappingTables.IsValuable())
{ {
var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(tableInfo.Name, StringComparison.CurrentCultureIgnoreCase)); var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(tableInfo.Name, StringComparison.CurrentCultureIgnoreCase));
@ -147,7 +156,7 @@ namespace SqlSugar
classText = classText.Replace(DbFirstTemplate.KeyClassName, className); classText = classText.Replace(DbFirstTemplate.KeyClassName, className);
classText = classText.Replace(DbFirstTemplate.KeyNamespace, this.Namespace); 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.KeyUsing, IsAttribute ? (this.UsingTemplate + "using " + PubConst.AssemblyName + "\r\t") : this.UsingTemplate);
classText = classText.Replace(DbFirstTemplate.KeyClassDescription,DbFirstTemplate.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription, tableInfo.Description+"\r\n")); classText = classText.Replace(DbFirstTemplate.KeyClassDescription, DbFirstTemplate.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription, tableInfo.Description + "\r\n"));
classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, tableInfo.Name) : null); classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, tableInfo.Name) : null);
if (columns.IsValuable()) if (columns.IsValuable())
{ {
@ -156,17 +165,16 @@ namespace SqlSugar
var isLast = columns.Last() == item; var isLast = columns.Last() == item;
string PropertyText = DbFirstTemplate.PropertyTemplate; string PropertyText = DbFirstTemplate.PropertyTemplate;
string PropertyDescriptionText = DbFirstTemplate.PropertyDescriptionTemplate; string PropertyDescriptionText = DbFirstTemplate.PropertyDescriptionTemplate;
string propertyName=GetPropertyName(item); string propertyName = GetPropertyName(item);
string propertyTypeName = GetPropertyTypeName(item); string propertyTypeName = GetPropertyTypeName(item);
PropertyText = GetPropertyText(item, PropertyText); PropertyText = GetPropertyText(item, PropertyText);
PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText); PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText);
PropertyText = PropertyDescriptionText + PropertyText; PropertyText = PropertyDescriptionText + PropertyText;
classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast?"":("\r\n" + DbFirstTemplate.KeyPropertyName))); classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast ? "" : ("\r\n" + DbFirstTemplate.KeyPropertyName)));
if (ConstructorText.IsValuable()&&item.DefaultValue.IsValuable()) if (ConstructorText.IsValuable() && item.DefaultValue.IsValuable())
{ {
ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyName, propertyName); ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyName, propertyName);
ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyType, propertyTypeName); ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) + (isLast ? "" : DbFirstTemplate.ConstructorTemplate);
ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) +(isLast?"":"\r\n"+DbFirstTemplate.KeyPropertyName);
} }
} }
} }
@ -180,16 +188,21 @@ namespace SqlSugar
private string GetProertypeDefaultValue(DbColumnInfo item) private string GetProertypeDefaultValue(DbColumnInfo item)
{ {
if (item.DefaultValue == null) return null; var result = item.DefaultValue;
string result = item.DefaultValue.TrimStart('(').TrimEnd(')').TrimStart('\'').TrimEnd('\''); if (result == null) return null;
if (item.DefaultValue.GetType() == PubConst.DateType) if (Regex.IsMatch(result, @"^\(\'(.+)\'\)$"))
{ {
return result.ObjToDate().ToString("yyyy-MM-dd hh:mm:ss.fff"); result = Regex.Match(result, @"^\(\'(.+)\'\)$").Groups[1].Value;
} }
else if (Regex.IsMatch(result, @"^\((.+)\)$"))
{ {
return result; result = Regex.Match(result, @"^\((.+)\)$").Groups[1].Value;
} }
if (result.Equals(this.SqlBuilder.SqlDateNow,StringComparison.CurrentCultureIgnoreCase))
{
result = "DateTime.Now";
}
return result;
} }
public void CreateClassFile(string directoryPath, string nameSpace = "Models") public void CreateClassFile(string directoryPath, string nameSpace = "Models")
@ -242,7 +255,9 @@ namespace SqlSugar
} }
private string GetPropertyTypeConvert(DbColumnInfo item) private string GetPropertyTypeConvert(DbColumnInfo item)
{ {
string result = "(\""+GetProertypeDefaultValue(item) +"\")"; var convertString = GetProertypeDefaultValue(item);
if (convertString == "DateTime.Now"|| convertString==null) return convertString;
string result = this.Context.Ado.DbBind.GetCSharpConvert(item.DataType) + "(\"" + convertString + "\")";
return result; return result;
} }

2
SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs

@ -35,7 +35,7 @@ namespace SqlSugar
PropertySpace + "/// Nullable:{IsNullable}\r\n" + PropertySpace + "/// Nullable:{IsNullable}\r\n" +
PropertySpace + "/// </summary>"; PropertySpace + "/// </summary>";
public static string ConstructorTemplate = PropertySpace + "this.{PropertyName} ={PropertyType}(\"{DefaultValue}\");\r\n"; public static string ConstructorTemplate = PropertySpace + " this.{PropertyName} ={DefaultValue};\r\n";
public static string UsingTemplate = "using System;\r\n" + public static string UsingTemplate = "using System;\r\n" +
"using System.Linq;\r\n" + "using System.Linq;\r\n" +

1
SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs

@ -45,6 +45,7 @@ namespace SqlSugar
} }
public virtual string SqlParameterKeyWord { get { return "@"; } } public virtual string SqlParameterKeyWord { get { return "@"; } }
public virtual string SqlFalse { get { return "1=2 "; } } public virtual string SqlFalse { get { return "1=2 "; } }
public virtual string SqlDateNow { get { return "GETDATE()"; } }
#endregion #endregion
} }
} }

1
SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs

@ -20,6 +20,7 @@ namespace SqlSugar
string SqlParameterKeyWord { get; } string SqlParameterKeyWord { get; }
string SqlFalse { get; } string SqlFalse { get; }
string SqlDateNow { get; }
string GetTranslationTableName(string name); string GetTranslationTableName(string name);
string GetTranslationColumnName(string entityName, string propertyName); string GetTranslationColumnName(string entityName, string propertyName);

Loading…
Cancel
Save