From 5a0d4b985ad469f8b1ec610bd8aef787d0cdfda0 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 3 Jul 2017 02:17:16 +0800 Subject: [PATCH] Update Core --- .../CodeFirstProvider/CodeFirstProvider.cs | 15 +++++++-- .../DbFirstProvider/DbFirstProvider.cs | 1 + .../Abstract/EntityProvider/EntityProvider.cs | 1 + .../QueryableProvider/QueryableProvider.cs | 2 +- .../SqlBuilderProvider/DeleteBuilder.cs | 5 +-- .../src/SqlSugar/Common/CacheManager.cs | 31 +++++++++++++++++++ .../src/SqlSugar/Common/RewritableMethods.cs | 15 +++++++++ .../src/SqlSugar/Entities/EntityColumnInfo.cs | 1 + .../Entities/Mapping/SugarMappingAttribute.cs | 8 ++++- .../ExpressionsToSql/Common/SugarParameter.cs | 2 +- .../SqlSugar/Interface/IRewritableMethods.cs | 5 ++- 11 files changed, 77 insertions(+), 9 deletions(-) diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index cc2a19345..495bebd1b 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -22,6 +22,7 @@ namespace SqlSugar } public virtual void InitTables(Type entityType) { + this.Context.RewritableMethods.RemoveCacheAll(); if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) { Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); @@ -112,7 +113,7 @@ namespace SqlSugar .Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(ec => dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName) - && ((ec.Length != dc.Length &&!PubMethod.GetUnderType(ec.PropertyInfo).IsEnum()&& PubMethod.GetUnderType(ec.PropertyInfo).IsIn(PubConst.StringType)) || + && ((ec.Length != dc.Length && !PubMethod.GetUnderType(ec.PropertyInfo).IsEnum() && PubMethod.GetUnderType(ec.PropertyInfo).IsIn(PubConst.StringType)) || ec.IsNullable != dc.IsNullable || IsSamgeType(ec, dc)))).ToList(); var renameColumns = entityColumns @@ -222,9 +223,13 @@ namespace SqlSugar ColumnDescription = item.ColumnDescription, Length = item.Length }; - if (propertyType.IsEnum()) + if (!string.IsNullOrEmpty(item.DataType)) + { + result.DataType = item.DataType; + } + else if (propertyType.IsEnum()) { - result.DataType = this.Context.Ado.DbBind.GetDbTypeName(item.Length>9?PubConst.LongType.Name:PubConst.IntType.Name); + result.DataType = this.Context.Ado.DbBind.GetDbTypeName(item.Length > 9 ? PubConst.LongType.Name : PubConst.IntType.Name); } else { @@ -235,6 +240,10 @@ namespace SqlSugar protected virtual bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc) { + if (!string.IsNullOrEmpty(ec.DataType)) + { + return ec.DataType != dc.DataType; + } var propertyType = PubMethod.GetUnderType(ec.PropertyInfo); var properyTypeName = string.Empty; if (propertyType.IsEnum()) diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs index 32507ab4c..a1ffc5bac 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs @@ -39,6 +39,7 @@ namespace SqlSugar public void Init() { + this.Context.RewritableMethods.RemoveCacheAll(); if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) { Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/EntityProvider/EntityProvider.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/EntityProvider/EntityProvider.cs index 8fa2e839d..a3bb497d6 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/EntityProvider/EntityProvider.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/EntityProvider/EntityProvider.cs @@ -122,6 +122,7 @@ namespace SqlSugar column.IsNullable = sugarColumn.IsNullable; column.Length = sugarColumn.Length; column.OldDbColumnName = sugarColumn.OldColumnName; + column.DataType = sugarColumn.ColumnDataType; } else { diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 1a2881f36..8c2627d48 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -123,7 +123,7 @@ namespace SqlSugar public virtual ISugarQueryable Where(string whereString, object whereObj = null) { var whereValue = QueryBuilder.WhereInfos; - whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString)); + whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString+PubConst.Space)); if (whereObj != null) QueryBuilder.Parameters.AddRange(Context.Ado.GetParameters(whereObj)); return this; diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/SqlBuilderProvider/DeleteBuilder.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/SqlBuilderProvider/DeleteBuilder.cs index 7f99e5703..3fb867aa7 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/SqlBuilderProvider/DeleteBuilder.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/SqlBuilderProvider/DeleteBuilder.cs @@ -125,11 +125,12 @@ namespace SqlSugar resolveExpress.MappingTables = Context.MappingTables; resolveExpress.IgnoreComumnList = Context.IgnoreColumns; resolveExpress.Resolve(expression, resolveType); - this.Parameters = new List(); + if (this.Parameters == null) + this.Parameters = new List(); this.Parameters.AddRange(resolveExpress.Parameters); var reval = resolveExpress.Result; return reval; - } + } #endregion } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/CacheManager.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/CacheManager.cs index 8ba369db8..a75924416 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/CacheManager.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/CacheManager.cs @@ -39,7 +39,11 @@ namespace SqlSugar if (_instance == null) lock (_instanceLock) if (_instance == null) + { _instance = new CacheManager(); + Action addItem =()=> { CacheManager.GetInstance().RemoveAllCache(); }; + CacheManager.Add(addItem); + } return _instance; } @@ -59,6 +63,14 @@ namespace SqlSugar this.InstanceCache.TryRemove(key, out val); } + public void RemoveAllCache() + { + foreach (var key in GetAllKey()) + { + this.Remove(key); + } + } + public IEnumerable GetAllKey() { return this.InstanceCache.Keys; @@ -84,4 +96,23 @@ namespace SqlSugar } } } + public static class CacheManager + { + private static List removeActions = new List(); + internal static void Add(Action removeAction) + { + removeActions.Add(removeAction); + } + + public static void RemoveAllCache() + { + lock (removeActions) + { + foreach (var item in removeActions) + { + item(); + } + } + } + } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/RewritableMethods.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/RewritableMethods.cs index 05a0434ea..0a76ad3a0 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/RewritableMethods.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Common/RewritableMethods.cs @@ -183,6 +183,21 @@ namespace SqlSugar return CacheManager.GetInstance(); } + public void RemoveCacheAll() + { + CacheManager.RemoveAllCache(); + } + + public void RemoveCacheAll() + { + CacheManager.GetInstance().RemoveAllCache(); + } + + public void RemoveCache(string key) + { + CacheManager.GetInstance().Remove(key); + } + #region Private Methods private Dictionary DataReaderToDynamicList_Part(Dictionary readerValues, PropertyInfo item, List reval) { diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/EntityColumnInfo.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/EntityColumnInfo.cs index 722814848..d9c4b1adc 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/EntityColumnInfo.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/EntityColumnInfo.cs @@ -22,5 +22,6 @@ namespace SqlSugar public string EntityName { get; set; } public string DbTableName { get; set; } public bool IsIgnore { get; set; } + public string DataType { get; set; } } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs index 74a84f9b3..e7b3581dc 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs @@ -79,7 +79,13 @@ namespace SqlSugar get { return _OldColumnName; } set { _OldColumnName = value; } } - + + private string _ColumnDataType; + public string ColumnDataType + { + get { return _ColumnDataType; } + set { _ColumnDataType = value; } + } } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs index 2fad22bc3..1bf344f43 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs @@ -49,7 +49,7 @@ namespace SqlSugar } else if (type == PubConst.DateType) { - this.DbType = System.Data.DbType.Date; + this.DbType = System.Data.DbType.DateTime; } else if (type == PubConst.DobType) { diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IRewritableMethods.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IRewritableMethods.cs index baf1bb5c5..730711572 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IRewritableMethods.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IRewritableMethods.cs @@ -17,6 +17,9 @@ namespace SqlSugar T TranslateCopy(T sourceObject); dynamic DataTableToDynamic(DataTable table); ICacheManager GetCacheInstance(); - + void RemoveCacheAll(); + void RemoveCacheAll(); + void RemoveCache(string key); + } }