- 优化 WithTempQuery 多对象选择同别名问题;#1192

This commit is contained in:
2881099
2022-09-25 00:16:15 +08:00
parent 93ea581281
commit e91f1157d8
11 changed files with 210 additions and 79 deletions

View File

@ -51,6 +51,7 @@ namespace FreeSql.Internal
}
internal bool EndsWithDbNestedField(string dbField, string dbNestedField)
{
if (string.IsNullOrWhiteSpace(dbNestedField)) return true;
switch (_ado.DataType)
{
case DataType.SqlServer:
@ -161,9 +162,8 @@ namespace FreeSql.Internal
foreach (var child in parent.GetAllChilds())
{
if (withTempQueryParser != null)
field.Append(", ").Append(withTempQueryParser.ParseExpMatchedTable.Alias).Append(".").Append(child.DbNestedField);
else
field.Append(", ").Append(child.DbField);
child.DbField = $"{withTempQueryParser.ParseExpMatchedTable.Alias}.{child.DbNestedField}";
field.Append(", ").Append(child.DbField);
if (index >= 0)
{
child.DbNestedField = $"as{++index}";

View File

@ -192,6 +192,31 @@ namespace FreeSql.Internal.CommonProvider
}
InsideField = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
InsideAf = new ReadAnonymousTypeAfInfo(InsideMap, "*");
field.Clear();
if (InsideMap.Childs.Where(a => a.Childs.Count > 1).Count() > 1)
{
var childs = InsideMap.GetAllChilds();
var duplicateNames = childs.GroupBy(a => a.DbNestedField).Where(a => a.Count() > 1).ToList();
if (duplicateNames.Count > 0)
{
foreach (var duplicateName in duplicateNames)
{
var dupmapIdx = 0;
foreach (var dupmap in duplicateName)
{
if (++dupmapIdx == 1) continue;
var newfield = insideSelect._commonUtils.TrimQuoteSqlName(dupmap.DbNestedField);
while (InsideField.Contains($"{newfield}{dupmapIdx}"))
++dupmapIdx;
dupmap.DbNestedField = insideSelect._commonUtils.QuoteSqlName($"{newfield}{dupmapIdx}");
}
}
foreach (var child in childs)
field.Append(", ").Append(child.DbField).Append(InsideSelect._commonExpression.EndsWithDbNestedField(child.DbField, child.DbNestedField) ? "" : InsideSelect._commonUtils.FieldAsAlias(child.DbNestedField));
InsideField = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
field.Clear();
}
}
}
}
@ -1208,6 +1233,11 @@ namespace FreeSql.Internal.CommonProvider
ret._cancel = _cancel;
ret._params.AddRange(_params);
if (ret._tables[0].Table == null) ret._tables[0].Table = TableInfo.GetDefaultTable(typeof(TDto));
if (selector is LambdaExpression lambdaExp && lambdaExp != null)
{
for (var a = 0; a < lambdaExp.Parameters.Count; a++)
_tables[a].Parameter = lambdaExp.Parameters[a];
}
var parser = new WithTempQueryParser(this, null, selector, ret._tables[0]);
var sql = $"\r\n{this.ToSql(parser._insideSelectList[0].InsideField)}";
ret.WithSql(sql);

View File

@ -247,6 +247,7 @@ namespace FreeSql.Internal.CommonProvider
_addFieldAlias = true; //解决:[Column(Name = "flevel") 与属性名不一致时,嵌套查询 bug
var old_field = _field;
var fieldsb = new StringBuilder();
if (_map.Childs.Any() == false) fieldsb.Append(", ").Append(_map.DbField).Append(_comonExp.EndsWithDbNestedField(_map.DbField, _map.DbNestedField) ? "" : _comonExp._common.FieldAsAlias(_map.DbNestedField));
foreach (var child in _map.GetAllChilds())
fieldsb.Append(", ").Append(child.DbField).Append(_comonExp.EndsWithDbNestedField(child.DbField, child.DbNestedField) ? "" : _comonExp._common.FieldAsAlias(child.DbNestedField));
_field = fieldsb.ToString();

View File

@ -2459,9 +2459,9 @@ namespace FreeSql.Internal
}
break;
}
if (startLength > 0)
if (startLength >= 0)
{
var pvalue = sql.Substring(startIdx, startLength).Replace("''", "'");
var pvalue = startLength == 0 ? "" : sql.Substring(startIdx, startLength).Replace("''", "'");
var pname = parms.Where(a => a.Value == pvalue).Select(a => a.Key).FirstOrDefault();
if (string.IsNullOrEmpty(pname))
{