From cb4bc9c3ef1dea51c09d98f0ffdb97a8a7f682ec Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 29 May 2017 16:28:27 +0800 Subject: [PATCH] - --- .../DbFirstProvider/DbFirstProvider.cs | 43 +++++++++++++------ .../DbFirstProvider/DbFirstTemplate.cs | 2 +- .../SqlBuilderProvider/SqlBuilderProvider.cs | 1 + SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs | 1 + 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs b/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs index 3834cf472..e5a84ad93 100644 --- a/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs +++ b/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; + namespace SqlSugar { public abstract partial class DbFirstProvider : IDbFirst @@ -16,6 +18,13 @@ namespace SqlSugar private string Namespace { get; set; } private bool IsAttribute { get; set; } private bool IsDefaultValue { get; set; } + private ISqlBuilder SqlBuilder + { + get + { + return InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig); + } + } private List TableInfoList { get; set; } public DbFirstProvider() @@ -131,7 +140,7 @@ namespace SqlSugar var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name); string className = tableInfo.Name; string classText = this.ClassTemplate; - string ConstructorText =IsDefaultValue? DbFirstTemplate.ConstructorTemplate:null; + string ConstructorText = IsDefaultValue ? DbFirstTemplate.ConstructorTemplate : null; if (this.Context.MappingTables.IsValuable()) { 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.KeyNamespace, this.Namespace); 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); if (columns.IsValuable()) { @@ -156,17 +165,16 @@ namespace SqlSugar var isLast = columns.Last() == item; string PropertyText = DbFirstTemplate.PropertyTemplate; string PropertyDescriptionText = DbFirstTemplate.PropertyDescriptionTemplate; - string propertyName=GetPropertyName(item); + string propertyName = GetPropertyName(item); string propertyTypeName = GetPropertyTypeName(item); PropertyText = GetPropertyText(item, PropertyText); PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText); PropertyText = PropertyDescriptionText + PropertyText; - classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast?"":("\r\n" + DbFirstTemplate.KeyPropertyName))); - if (ConstructorText.IsValuable()&&item.DefaultValue.IsValuable()) + classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast ? "" : ("\r\n" + DbFirstTemplate.KeyPropertyName))); + if (ConstructorText.IsValuable() && item.DefaultValue.IsValuable()) { ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyName, propertyName); - ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyType, propertyTypeName); - ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) +(isLast?"":"\r\n"+DbFirstTemplate.KeyPropertyName); + ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) + (isLast ? "" : DbFirstTemplate.ConstructorTemplate); } } } @@ -180,16 +188,21 @@ namespace SqlSugar private string GetProertypeDefaultValue(DbColumnInfo item) { - if (item.DefaultValue == null) return null; - string result = item.DefaultValue.TrimStart('(').TrimEnd(')').TrimStart('\'').TrimEnd('\''); - if (item.DefaultValue.GetType() == PubConst.DateType) + var result = item.DefaultValue; + if (result == null) return null; + 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") @@ -242,7 +255,9 @@ namespace SqlSugar } 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; } diff --git a/SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs b/SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs index af8f50684..89c033f2a 100644 --- a/SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs +++ b/SqlSugar/Abstract/DbFirstProvider/DbFirstTemplate.cs @@ -35,7 +35,7 @@ namespace SqlSugar PropertySpace + "/// Nullable:{IsNullable}\r\n" + PropertySpace + "/// "; - 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" + "using System.Linq;\r\n" + diff --git a/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs b/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs index 8622efd31..67eabe909 100644 --- a/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs +++ b/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs @@ -45,6 +45,7 @@ namespace SqlSugar } public virtual string SqlParameterKeyWord { get { return "@"; } } public virtual string SqlFalse { get { return "1=2 "; } } + public virtual string SqlDateNow { get { return "GETDATE()"; } } #endregion } } diff --git a/SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs b/SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs index f59f7bbc4..69928ddc1 100644 --- a/SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs +++ b/SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs @@ -20,6 +20,7 @@ namespace SqlSugar string SqlParameterKeyWord { get; } string SqlFalse { get; } + string SqlDateNow { get; } string GetTranslationTableName(string name); string GetTranslationColumnName(string entityName, string propertyName);