From dcd7bd7e7e7dd3c1237f4e0c23fc133d4cc9d4a3 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 7 Dec 2022 10:01:30 +0800 Subject: [PATCH] Synchronization code --- .../QueryableProvider/NavigatManager.cs | 4 +++ .../QueryableProvider/QueryableHelper.cs | 24 +++++++++++++++++- .../QueryableProvider/QueryableProvider.cs | 25 ++++++++++++++++++- .../DbMaintenance/PostgreSQLDbMaintenance.cs | 2 +- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs index 010bb5dbb..51286e3b3 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs @@ -326,6 +326,10 @@ namespace SqlSugar { navPkColumn = navEntityInfo.Columns.Where(it => it.PropertyName== navObjectNameColumnInfo.Navigat.Name2).FirstOrDefault(); } + if (navPkColumn == null && navType.FullName.IsCollectionsList()) + { + Check.ExceptionEasy($"{navObjectNamePropety.Name} type error ", $"一对一不能是List对象 {navObjectNamePropety.Name} "); + } var ids = list.Select(it => it.GetType().GetProperty(navObjectNameColumnInfo.Navigat.Name).GetValue(it)).Select(it => it == null ? "null" : it).Distinct().ToList(); List conditionalModels = new List(); conditionalModels.Add((new ConditionalModel() diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs index 1f9329e98..67d7d0581 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs @@ -942,9 +942,19 @@ namespace SqlSugar { this.QueryBuilder.AsTables[tableinfo.Key] = " (SELECT * FROM " + this.QueryBuilder.AsTables.First().Value + $" {SqlWith.NoLock} )"; } + else if (this.QueryBuilder.IsSqlQuery && this.QueryBuilder.AsTables.First().Value.ObjToString().StartsWith("(")) + { + var tableName = this.QueryBuilder.AsTables.First().Value; + this.QueryBuilder.AsTables[tableinfo.Key] = " (SELECT * FROM " + tableName + ")" + this.QueryBuilder.TableShortName; + } else { - this.QueryBuilder.AsTables[tableinfo.Key] = " (SELECT * FROM " + this.QueryBuilder.AsTables.First().Value + ")"; + var tableName = this.QueryBuilder.AsTables.First().Value; + if (tableName != null && Regex.IsMatch(tableName, @"^\w+$")) + { + tableName = SqlBuilder.GetTranslationTableName(tableName); + } + this.QueryBuilder.AsTables[tableinfo.Key] = " (SELECT * FROM " + tableName + ")"; } this.QueryBuilder.SelectValue = this.QueryBuilder.TableShortName + ".*"; } @@ -1575,5 +1585,17 @@ namespace SqlSugar return this.Context.Ado.SqlQuery(sql,parameters); } #endregion + + #region Bool + + private bool MasterHasWhereFirstJoin() + { + return this.QueryBuilder.JoinIndex == 0 && + this.QueryBuilder.IsSqlQuery == false && + !this.QueryBuilder.AsTables.Any() && + this.QueryBuilder.IsSingle() && + this.QueryBuilder.WhereInfos.Any(); + } + #endregion } } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 3393f6c3e..2506bc05f 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -116,6 +116,11 @@ namespace SqlSugar } public ISugarQueryable LeftJoin(Expression> joinExpression) { + if (MasterHasWhereFirstJoin()) + { + return this.MergeTable().LeftJoin(joinExpression); + } + this.Context.InitMappingInfo(); var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); result.SqlBuilder = this.SqlBuilder; @@ -126,6 +131,11 @@ namespace SqlSugar } public ISugarQueryable FullJoin(Expression> joinExpression) { + if (MasterHasWhereFirstJoin()) + { + return this.MergeTable().FullJoin(joinExpression); + } + this.Context.InitMappingInfo(); var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); result.SqlBuilder = this.SqlBuilder; @@ -135,6 +145,12 @@ namespace SqlSugar } public ISugarQueryable RightJoin(Expression> joinExpression) { + + if (MasterHasWhereFirstJoin()) + { + return this.MergeTable().RightJoin(joinExpression); + } + this.Context.InitMappingInfo(); var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); result.SqlBuilder = this.SqlBuilder; @@ -143,8 +159,14 @@ namespace SqlSugar return result; } - public ISugarQueryable InnerJoin(Expression> joinExpression) + public ISugarQueryable InnerJoin(Expression> joinExpression) { + + if (MasterHasWhereFirstJoin()) + { + return this.MergeTable().InnerJoin(joinExpression); + } + this.Context.InitMappingInfo(); var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); result.SqlBuilder = this.SqlBuilder; @@ -152,6 +174,7 @@ namespace SqlSugar result.QueryBuilder.JoinQueryInfos.Add(GetJoinInfo(joinExpression, JoinType.Inner)); return result; } + public void Clear() { QueryBuilder.Clear(); diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs index 7f3b73ae7..17b02769b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs @@ -181,7 +181,7 @@ namespace SqlSugar protected override string IsAnyTableRemarkSql { get { throw new NotSupportedException(); } } - protected override string RenameTableSql => "alter table 表名 {0} to {1}"; + protected override string RenameTableSql => "alter table {0} to {1}"; protected override string CreateIndexSql {