修复 .From 多表查询别名的匹配 bug

This commit is contained in:
28810 2019-07-17 18:01:52 +08:00
parent a5f524d154
commit 558d154486
2 changed files with 29 additions and 2 deletions

View File

@ -283,6 +283,8 @@ namespace FreeSql.Tests
[Navigate("TbId")] [Navigate("TbId")]
public virtual ICollection<TaskBuildInfo> Builds { get; set; } public virtual ICollection<TaskBuildInfo> Builds { get; set; }
public Templates Templates { get; set; } public Templates Templates { get; set; }
public TaskBuild Parent { get; set; }
} }
public class SqlFunc public class SqlFunc
@ -296,6 +298,26 @@ namespace FreeSql.Tests
[Fact] [Fact]
public void Test1() public void Test1()
{ {
var sqksdkfjl = g.sqlite.Select<TaskBuild>()
.LeftJoin(a => a.Templates.Id2 == a.TemplatesId)
.LeftJoin(a => a.Parent.Id == a.Id)
.LeftJoin(a => a.Parent.Templates.Id2 == a.Parent.TemplatesId)
.ToSql(a => new
{
code1 = a.Templates.Code,
code2 = a.Parent.Templates.Code
});
var sqksdkfjl2223 = g.sqlite.Select<TaskBuild>().From<TaskBuild, Templates, Templates>((s1, tb2, b1, b2) => s1
.LeftJoin(a => a.Id == tb2.TemplatesId)
.LeftJoin(a => a.TemplatesId == b1.Id2)
.LeftJoin(a => a.TemplatesId == b2.Id2)
).ToSql((a, tb2, b1, b2) => new
{
code1 = b1.Code,
code2 = b2.Code
});
var teklksjdg = g.sqlite.Select<ZX.Model.CustomerCheckupGroup>() var teklksjdg = g.sqlite.Select<ZX.Model.CustomerCheckupGroup>()

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace FreeSql.Internal namespace FreeSql.Internal
{ {
@ -341,6 +342,7 @@ namespace FreeSql.Internal
return GetBoolString(sql); return GetBoolString(sql);
return sql; return sql;
} }
static ConcurrentDictionary<string, Regex> dicRegexAlias = new ConcurrentDictionary<string, Regex>();
public void ExpressionJoinLambda(List<SelectTableInfo> _tables, SelectTableInfoType tbtype, Expression exp, Func<Expression[], string> getSelectGroupingMapString) public void ExpressionJoinLambda(List<SelectTableInfo> _tables, SelectTableInfoType tbtype, Expression exp, Func<Expression[], string> getSelectGroupingMapString)
{ {
var tbidx = _tables.Count; var tbidx = _tables.Count;
@ -360,7 +362,10 @@ namespace FreeSql.Internal
} }
else else
{ {
var find = _tables.Where((a, c) => c > 0 && (a.Type == tbtype || a.Type == SelectTableInfoType.From) && string.IsNullOrEmpty(a.On) && sql.Contains(a.Alias + ".")).LastOrDefault(); var find = _tables.Where((a, c) => c > 0 &&
(a.Type == tbtype || a.Type == SelectTableInfoType.From) &&
string.IsNullOrEmpty(a.On) &&
dicRegexAlias.GetOrAdd(a.Alias, alias => new Regex($@"\b{alias}\.", RegexOptions.Compiled)).IsMatch(sql)).LastOrDefault();
if (find != null) if (find != null)
{ {
find.Type = tbtype; find.Type = tbtype;