From 48735766475b40558fefe7423fd946e13d4dbb2e Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 23 Apr 2022 22:06:38 +0800 Subject: [PATCH] Update Core --- .../CodeFirstProvider/CodeFirstProvider.cs | 9 ++++- .../SplitCodeFirstProvider.cs | 14 +++++++ .../Abstract/DbMaintenanceProvider/Methods.cs | 3 +- .../InsertableProvider/InsertableProvider.cs | 8 ++++ .../QueryableProvider/QueryableProvider.cs | 1 + .../SqlBuilderProvider/QueryBuilder.cs | 13 ++++++ .../PostgreSQL/PostgreSQLProvider.cs | 13 ++++++ .../DbMaintenance/SqlServerDbMaintenance.cs | 40 ++++++++++++++++++- Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs | 1 + 9 files changed, 98 insertions(+), 4 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index ea3c6f3d3..b02e1fd14 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -366,7 +366,14 @@ namespace SqlSugar { if (!string.IsNullOrEmpty(ec.DataType)) { - return ec.DataType != dc.DataType; + if (ec.IsIdentity && dc.IsIdentity) + { + return false; + } + else + { + return ec.DataType != dc.DataType; + } } var propertyType = UtilMethods.GetUnderType(ec.PropertyInfo); var properyTypeName = string.Empty; diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs index 9f9f0fa08..29787a86f 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -16,6 +17,19 @@ namespace SqlSugar } public void InitTables(Type type) + { + var isSplitEntity = type.GetCustomAttributes() != null; + if (isSplitEntity) + { + _InitTables(type); + } + else + { + this.Context.CodeFirst.InitTables(type); + } + + } + private void _InitTables(Type type) { //var oldMapping = this.Context.Utilities.TranslateCopy(this.Context.MappingTables); SplitTableContext helper = new SplitTableContext(Context) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index 73b02d198..237ccac0d 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -206,7 +206,8 @@ namespace SqlSugar this.Context.Ado.ExecuteCommand(sql); if (isAddNotNUll) { - var dtColums = this.Context.Queryable().AS(columnInfo.TableName).Where("1=2").ToDataTable().Columns.Cast(); + var dtColums = this.Context.Queryable().AS(columnInfo.TableName).Where("1=2") + .Select(this.SqlBuilder.GetTranslationColumnName(columnInfo.DbColumnName)).ToDataTable().Columns.Cast(); var dtColumInfo = dtColums.First(it => it.ColumnName.EqualCase(columnInfo.DbColumnName)); var type = UtilMethods.GetUnderType(dtColumInfo.DataType); var value= type==UtilConstants.StringType?(object)"": Activator.CreateInstance(type); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 2547d4300..118c7283f 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -203,6 +203,14 @@ namespace SqlSugar object setValue = 0; if (idValue > int.MaxValue) setValue = idValue; + else if (this.EntityInfo.Columns.Any(it => it.IsIdentity && it.PropertyInfo.PropertyType == typeof(uint))) + { + setValue = Convert.ToUInt32(idValue); + } + else if (this.EntityInfo.Columns.Any(it => it.IsIdentity && it.PropertyInfo.PropertyType == typeof(ulong))) + { + setValue = Convert.ToUInt64(idValue); + } else setValue = Convert.ToInt32(idValue); this.Context.EntityMaintenance.GetProperty(identityKey).SetValue(result, setValue, null); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index f1f656065..78c7d7fc1 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -2943,6 +2943,7 @@ namespace SqlSugar _Size=it._Size }).ToList(); } + asyncQueryableBuilder.IsQueryInQuery = this.QueryBuilder.IsQueryInQuery; asyncQueryableBuilder.Includes = this.QueryBuilder.Includes; asyncQueryableBuilder.Take = this.QueryBuilder.Take; asyncQueryableBuilder.Skip = this.QueryBuilder.Skip; diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index 268356643..89f2628c1 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -33,6 +33,7 @@ namespace SqlSugar #endregion #region Splicing basic + public bool IsQueryInQuery { get; set; } public List Includes { get; set; } public List IgnoreColumns { get; set; } public bool IsCount { get; set; } @@ -641,6 +642,18 @@ namespace SqlSugar if (this.AsTables.Any(it=>it.Key==EntityName)) { name = this.AsTables.FirstOrDefault(it => it.Key == EntityName).Value; + if (this.IsQueryInQuery && this.SelectValue != null && this.SelectValue is Expression) + { + if (this.SelectValue.ToString().Contains("Subqueryable()")&& name.TrimStart().StartsWith("(")) + { + var oldName = name; + name = Regex.Match(name, @"\(.+\)").Value; + if (name.IsNullOrEmpty()) + { + name = oldName; + } + } + } } var result = Builder.GetTranslationTableName(name); result += UtilConstants.Space; diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs index f8071b167..c6e2848b5 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs @@ -115,6 +115,19 @@ namespace SqlSugar } else if (type==DBNull.Value.GetType()) { + if (parameter.DbType.IsIn(System.Data.DbType.Int32)) + { + sqlParameter.NpgsqlDbType = NpgsqlDbType.Integer | NpgsqlDbType.Array; + } + else if (parameter.DbType.IsIn(System.Data.DbType.Int64)) + { + sqlParameter.NpgsqlDbType = NpgsqlDbType.Bigint | NpgsqlDbType.Array; + } + else + { + sqlParameter.NpgsqlDbType =NpgsqlDbType.Text | NpgsqlDbType.Array; + } + } else { diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs b/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs index fde44844a..1284a2a13 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs @@ -313,6 +313,28 @@ namespace SqlSugar #endregion #region Methods + public override bool IsAnyTable(string tableName, bool isCache = true) + { + if (tableName.Contains(".")) + { + var schemas = GetSchemas(); + var first =this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First()); + var schemaInfo= schemas.FirstOrDefault(it=>it.EqualCase(first)); + if (schemaInfo == null) + { + return base.IsAnyTable(tableName, isCache); + } + else + { + var result= this.Context.Ado.GetInt($"select object_id('{tableName}')"); + return result > 0; + } + } + else + { + return base.IsAnyTable(tableName, isCache); + } + } public List GetSchemas() { return this.Context.Ado.SqlQuery("SELECT name FROM sys.schemas where name <> 'dbo'"); @@ -327,7 +349,14 @@ namespace SqlSugar if (schemas.Any(y => y.EqualCase(tableSchemas))) { sql = string.Format(this.DeleteTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last())); - sql = sql.Replace(",dbo,", $",{tableSchemas},").Replace("'user'", "'SCHEMA'"); + if (tableSchemas.EqualCase("user")) + { + sql = sql.Replace("'user'", "'SCHEMA'").Replace("dbo", $"'{tableSchemas}'"); + } + else + { + sql = sql.Replace(",dbo,", $",{tableSchemas},").Replace("'user'", "'SCHEMA'"); + } } } this.Context.Ado.ExecuteCommand(sql); @@ -359,7 +388,14 @@ namespace SqlSugar if (schemas.Any(y => y.EqualCase(tableSchemas))) { sql = string.Format(this.AddTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last()), description); - sql = sql.Replace("N'dbo'", $"N'{tableSchemas}'").Replace("N'user'", "N'SCHEMA'"); + if (tableSchemas.EqualCase("user")) + { + sql = sql.Replace("N'user', N'dbo'", $"N'user', '{tableSchemas}'").Replace("N'user'", "N'SCHEMA'"); + } + else + { + sql = sql.Replace("N'dbo'", $"N'{tableSchemas}'").Replace("N'user'", "N'SCHEMA'"); + } } } this.Context.Ado.ExecuteCommand(sql); diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs index c4cca3c45..b0e3b1371 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs @@ -346,6 +346,7 @@ namespace SqlSugar var result= this.Context.Queryable(queryable); var QueryBuilder = queryable.QueryBuilder; + result.QueryBuilder.IsQueryInQuery = true; result.QueryBuilder.WhereIndex = QueryBuilder.WhereIndex++; result.QueryBuilder.LambdaExpressions.ParameterIndex = QueryBuilder.LambdaExpressions.ParameterIndex++; result.QueryBuilder.LambdaExpressions.Index = QueryBuilder.LambdaExpressions.Index++;