From 456b75355aae403c5a3600031a7008e44dcf31a0 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 16 Mar 2022 22:17:38 +0800 Subject: [PATCH] Update core --- .../Abstract/AdoProvider/AdoProvider.cs | 7 +++++- .../Abstract/DbMaintenanceProvider/Methods.cs | 6 +++++ .../EntityMaintenance/EntityMaintenance.cs | 2 +- .../InsertableProvider/ParameterInsertable.cs | 8 +++++++ .../QueryableProvider/QueryableProvider.cs | 22 ++++++++++++++--- .../SqlBuilderProvider/QueryBuilder.cs | 6 ++--- .../SqlBuilderProvider/SqlBuilderProvider.cs | 24 +++++++++++++++++++ .../SqlSugar/Entities/SugarConnection.cs | 22 +++++++++++++++++ .../ResolveItems/BaseResolve.cs | 20 ++++++++++++++++ .../MethodCallExpressionResolve.cs | 4 ++++ .../SqlSeverTest/SqlSugar/Interface/IAdo.cs | 2 +- .../SqlSugar/Interface/IDbMaintenance.cs | 1 + .../DbMaintenance/SqlServerDbMaintenance.cs | 2 +- .../SqlBuilder/SqlServerExpressionContext.cs | 13 ++++++++++ .../SqlSeverTest/SqlSugar/SqlSugar.csproj | 2 +- .../SqlSugar/SqlSugarForCore.nuspec | 6 ++--- .../SqlSugar/SqlSugarForCoreNoDrive.nuspec | 4 ++-- 17 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SugarConnection.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 650fb596c..2087ec38a 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -77,10 +77,15 @@ namespace SqlSugar { CheckConnection(); } - public virtual void OpenAlways() + public SugarConnection OpenAlways() { + SugarConnection result = new SugarConnection(); + result.IsAutoClose = this.Context.CurrentConnectionConfig.IsAutoCloseConnection; + result.conn = this.Connection; + result.Context = this.Context; this.Context.CurrentConnectionConfig.IsAutoCloseConnection = false; this.Open(); + return result; } public virtual void Close() { diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index 100c02c88..12bf743fa 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -325,6 +325,12 @@ namespace SqlSugar this.Context.Ado.ExecuteCommand(sql); return true; } + public virtual bool CreateIndex(string tableName, string[] columnNames, string IndexName, bool isUnique = false) + { + string sql = string.Format("CREATE {3} INDEX {2} ON {0}({1})", tableName, string.Join(",", columnNames), IndexName, isUnique ? "UNIQUE" : ""); + this.Context.Ado.ExecuteCommand(sql); + return true; + } public virtual bool IsAnyIndex(string indexName) { string sql = string.Format(this.IsAnyIndexSql, indexName); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index 8b98889f4..f74608a3f 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -166,7 +166,7 @@ namespace SqlSugar { return string.Empty; } - return xeNode.Element("summary").Value.Trim(); + return xeNode.Element("summary").Value.ToSqlFilter().Trim(); } /// /// Gets the code annotation for the database table diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs index 63416d738..e0f3d528d 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs @@ -99,6 +99,10 @@ namespace SqlSugar var removeCacheFunc = inserable.RemoveCacheFunc; var objects = inserable.InsertObjs; var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray(); + if (inserable.IsOffIdentity) + { + identityList = new string[] { }; + } this.Context.Utilities.PageEach(objects, 100, pagelist => { @@ -120,6 +124,10 @@ namespace SqlSugar var removeCacheFunc = inserable.RemoveCacheFunc; var objects = inserable.InsertObjs; var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray(); + if (inserable.IsOffIdentity) + { + identityList = new string[] { }; + } await this.Context.Utilities.PageEachAsync(objects, 100,async pagelist => { diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 66a1223b6..7e0c8962d 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -485,6 +485,18 @@ namespace SqlSugar FieldName = column.DbColumnName, FieldValue = value.ObjToString() }); + if (value != null && value.GetType().IsEnum()) + { + if (this.Context.CurrentConnectionConfig?.MoreSettings?.TableEnumIsString == true) + { + + } + else + { + data.Value.FieldValue = Convert.ToInt64(value).ObjToString(); + } + + } cons.ConditionalList.Add(data); if (this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL) { @@ -1136,16 +1148,16 @@ namespace SqlSugar var parentIdName = UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name; var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName); var parentPropertyName = ParentInfo.DbColumnName; - var current =await this.Context.Queryable().InSingleAsync(primaryKeyValue); + var current =await this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).InSingleAsync(primaryKeyValue); if (current != null) { result.Add(current); object parentId = ParentInfo.PropertyInfo.GetValue(current, null); int i = 0; - while (parentId != null &&await this.Context.Queryable().In(parentId).AnyAsync()) + while (parentId != null &&await this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).In(parentId).AnyAsync()) { Check.Exception(i > 100, ErrorMessage.GetThrowMessage("Dead cycle", "出现死循环或超出循环上限(100),检查最顶层的ParentId是否是null或者0")); - var parent =await this.Context.Queryable().InSingleAsync(parentId); + var parent =await this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).InSingleAsync(parentId); result.Add(parent); parentId = ParentInfo.PropertyInfo.GetValue(parent, null); ++i; @@ -2161,6 +2173,10 @@ namespace SqlSugar Check.Exception(true, ".Mapper() parameter error"); } List inValues = entitys.Select(it => it.GetType().GetProperty(filedName).GetValue(it, null).ObjToString()).ToList(); + if (inValues!=null&& inValues.Any()&&UtilMethods.GetUnderType(entitys.First().GetType().GetProperty(filedName).PropertyType) == UtilConstants.GuidType) + { + inValues = inValues.Select(x => x == "" ? "null" : x).Distinct().ToList(); + } List wheres = new List() { new ConditionalModel() diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index 3ad6afa05..1ef73ef9a 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -243,7 +243,7 @@ namespace SqlSugar } resolveExpress.RootExpression = expression; resolveExpress.JoinQueryInfos = Builder.QueryBuilder.JoinQueryInfos; - resolveExpress.IsSingle = IsSingle(); + resolveExpress.IsSingle = IsSingle()&& resolveType!= ResolveExpressType.WhereMultiple; resolveExpress.MappingColumns = Context.MappingColumns; resolveExpress.MappingTables = Context.MappingTables; resolveExpress.IgnoreComumnList = Context.IgnoreColumns; @@ -641,12 +641,12 @@ namespace SqlSugar result += UtilConstants.Space; if (IsSingle() && result.Contains("MergeTable") && result.Trim().EndsWith(" MergeTable") && TableShortName != null) { - result = result.Replace(") MergeTable ", ") " + TableShortName); + result = result.Replace(") MergeTable ", ") " + TableShortName+UtilConstants.Space); TableShortName = null; } if (IsSingle() && result.Contains("unionTable") && result.Trim().EndsWith(" unionTable")&& TableShortName!=null) { - result = result.Replace(" ) unionTable ", ") "+TableShortName); + result = result.Replace(" ) unionTable ", ") "+TableShortName + UtilConstants.Space); TableShortName = null; } if (this.TableShortName.HasValue()) diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs index 0540d0e2e..79f59ab20 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs @@ -189,12 +189,36 @@ namespace SqlSugar case ConditionalType.In: if (item.FieldValue == null) item.FieldValue = string.Empty; var inValue1 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")"); + if (item.CSharpTypeName.HasValue()&&UtilMethods.IsNumber(item.CSharpTypeName)) + { + inValue1= inValue1.Replace("'",""); + } + else if(inValue1.Contains("'null'")) + { + inValue1 = inValue1.Replace("'null'", "null"); + } + else if (inValue1.Contains("[null]")) + { + inValue1 = inValue1.Replace("[null]", "null"); + } builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "IN", inValue1); parameters.Add(new SugarParameter(parameterName, item.FieldValue)); break; case ConditionalType.NotIn: if (item.FieldValue == null) item.FieldValue = string.Empty; var inValue2 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")"); + if (item.CSharpTypeName.HasValue() && UtilMethods.IsNumber(item.CSharpTypeName)) + { + inValue2 = inValue2.Replace("'", ""); + } + else if (inValue2.Contains("'null'")) + { + inValue2 = inValue2.Replace("'null'", "null"); + } + else if (inValue2.Contains("[null]")) + { + inValue2 = inValue2.Replace("[null]", "null"); + } builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "NOT IN", inValue2); parameters.Add(new SugarParameter(parameterName, item.FieldValue)); break; diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SugarConnection.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SugarConnection.cs new file mode 100644 index 000000000..82aae7e01 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SugarConnection.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SugarConnection:IDisposable + { + public IDbConnection conn { get; set; } + public bool IsAutoClose { get; set; } + public ISqlSugarClient Context { get; set; } + + public void Dispose() + { + conn.Close(); + this.Context.CurrentConnectionConfig.IsAutoCloseConnection = IsAutoClose; + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs index ceeedd054..160ec040d 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs @@ -532,6 +532,26 @@ namespace SqlSugar { return false; } + else if ((item as MethodCallExpression).Method.Name== "AggregateMax") + { + return false; + } + else if ((item as MethodCallExpression).Method.Name == "AggregateMin") + { + return false; + } + else if ((item as MethodCallExpression).Method.Name == "AggregateSum") + { + return false; + } + else if ((item as MethodCallExpression).Method.Name == "ToBool") + { + return false; + } + else if ((item as MethodCallExpression).Method.Name == "ToBoolean") + { + return false; + } else { return true; diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index ffd3f7ca3..dc004857e 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -1006,6 +1006,10 @@ namespace SqlSugar { return string.Format("CONVERT(varchar(100),convert(datetime,{0}), {1})", value, formatString); } + else if (IsSqlServer()) + { + return string.Format("FORMAT({0},'{1}','en-US')", value, formatString); + } var parameter = new MethodCallExpressionArgs() { IsMember = true, MemberValue = DateType.Year }; var parameter2 = new MethodCallExpressionArgs() { IsMember = true, MemberName = value }; var parameters = new MethodCallExpressionModel() { Args = new List() { parameter2, parameter } }; diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IAdo.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IAdo.cs index 171770c93..ad2ae3193 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IAdo.cs @@ -163,7 +163,7 @@ namespace SqlSugar void Dispose(); void Close(); void Open(); - void OpenAlways(); + SugarConnection OpenAlways(); void CheckConnection(); void BeginTran(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDbMaintenance.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDbMaintenance.cs index b28ccd38a..1a00a863b 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDbMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDbMaintenance.cs @@ -29,6 +29,7 @@ namespace SqlSugar #region DDL bool AddDefaultValue(string tableName,string columnName,string defaultValue); bool CreateIndex(string tableName, string [] columnNames, bool isUnique=false); + bool CreateIndex(string tableName, string[] columnNames, string IndexName, bool isUnique = false); bool DropTable(string tableName); bool TruncateTable(string tableName); bool TruncateTable(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs index 45c60e1fa..fde44844a 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs @@ -12,7 +12,7 @@ namespace SqlSugar { get { - return "SELECT NAME FROM MASTER.DBO.SYSDATABASES ORDER BY NAME"; + return "SELECT NAME FROM master.dbo.sysdatabases ORDER BY NAME"; } } protected override string GetColumnInfosByTableNameSql diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs index 210f4872b..82490bed0 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs @@ -20,6 +20,19 @@ namespace SqlSugar } public partial class SqlServerMethod : DefaultDbMethod, IDbMethods { + public override string DateValue(MethodCallExpressionModel model) + { + var parameter = model.Args[0]; + var parameter2 = model.Args[1]; + if (parameter.MemberName != null && parameter.MemberName is DateTime) + { + return string.Format(" datepart({0},'{1}') ", parameter2.MemberValue, parameter.MemberName); + } + else + { + return string.Format(" datepart({0},{1}) ", parameter2.MemberValue, parameter.MemberName); + } + } public override string HasValue(MethodCallExpressionModel model) { if (model.Args[0].Type == UtilConstants.GuidType) diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugar.csproj b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugar.csproj index 677ea0287..fc0909a13 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugar.csproj @@ -2,7 +2,7 @@ netstandard2.1 - 5.0.5.9 + 5.0.6 sun_kai_xuan https://github.com/sunkaixuan/SqlSugar diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCore.nuspec b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCore.nuspec index 0b46aa338..5c6b42569 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCore.nuspec +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCore.nuspec @@ -2,16 +2,16 @@ SqlSugarCore - 5.0.5.9 + 5.0.6 sunkaixuan - Landa + 果糖大数据科技 http://www.apache.org/licenses/LICENSE-2.0.html https://github.com/sunkaixuan/SqlSugar https://secure.gravatar.com/avatar/a82c03402497b2e58fd65038a3699b30 false 5.0.3.5-Max 最低要求.Net Core 3.0+ ,5.0.0-5.0.3.4 最低要求 .Net Core 2.0+ SqlSugar ORM ,High-performance, lightweight Copyright 2016 - Asp.net core orm + SqlSugar Sql Sugar core Asp.net core orm diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCoreNoDrive.nuspec b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCoreNoDrive.nuspec index e4b033841..b4b9fa6e7 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCoreNoDrive.nuspec +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarForCoreNoDrive.nuspec @@ -2,7 +2,7 @@ SqlSugarCoreNoDrive - 5.0.5.9 + 5.0.6 sunkaixuan Landa http://www.apache.org/licenses/LICENSE-2.0.html @@ -11,7 +11,7 @@ false .Net Core/.net5 SqlSugar ORM ,High-performance, lightweight https://github.com/sunkaixuan/SqlSugar Copyright 2016 - Asp.net core orm + SqlSugar Sql Sugar Core Asp.net core orm