Skip to content

Commit

Permalink
- 修复 GroupBy + WithTempQuery 别名问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jan 8, 2024
1 parent 9cae4c6 commit 0d2d517
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 156 deletions.
113 changes: 75 additions & 38 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ static void Main(string[] args)
var fsql = new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseNameConvert(NameConvertType.ToLower)
//.UseNameConvert(NameConvertType.ToLower)
//.UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop)
.UseAdoConnectionPool(true)

Expand Down Expand Up @@ -596,7 +596,7 @@ static void Main(string[] args)
//.UseConnectionString(DataType.QuestDb, "host=localhost;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;")

//.UseConnectionString(DataType.ClickHouse, "DataCompress=False;BufferSize=32768;SocketTimeout=10000;CheckCompressedHash=False;Encrypt=False;Compressor=lz4;Host=192.168.0.121;Port=8125;Database=PersonnelLocation;Username=root;Password=123")
.UseConnectionFactory(DataType.ClickHouse, () => null)
//.UseConnectionFactory(DataType.ClickHouse, () => null)
.UseMonitorCommand(cmd =>
{
Console.WriteLine(cmd.CommandText + "\r\n");
Expand All @@ -608,16 +608,55 @@ static void Main(string[] args)
.UseGenerateCommandParameterWithLambda(true)
.Build();
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion

var clickhouseSql1 = fsql.Select<User1>().Where(a => new[] { 1, 2, 3 }.Contains(a.GroupId)).ToSql();
var clickhouseVal1 = new[] { 1, 2, 3 };
var clickhouseSql2 = fsql.Select<User1>().Where(a => clickhouseVal1.Contains(a.GroupId)).ToSql();
var clickhouseSql3 = fsql.Select<User1>().Where(a => a.Tags.Contains("tag1")).ToSql();
var clickhouseVal2 = "tag2";
var clickhouseSql4 = fsql.Select<User1>().Where(a => a.Tags.Contains(clickhouseVal2)).ToSql();
#endregion


var list111222 = fsql.Select<OrderLine, Product>()
.InnerJoin((l, p) => l.ProductId == p.ID)
.GroupBy((l, p) => new { p.ID, ShopType = l.ShopType ?? 0 })
.WithTempQuery(a => new
{
a.Key.ID,
Money = a.Sum(a.Value.Item1.Amount) * a.Key.ShopType
})
.ToList();
Console.WriteLine(list111222);

var list0x1sql = fsql.Select<OrderLine22x, Product22x>()
.InnerJoin((l, p) => l.ProductId == p.ID)
.GroupBy((l, p) => new { p.ID, l.ShopType })
.WithTempQuery(a => new {
a.Key.ID,
Money2 = a.Key.ShopType,
Money = a.Key.ShopType == 1 ? a.Value.Item1.Price * a.Value.Item1.Amount : a.Value.Item1.Price * a.Value.Item1.Amount * 1.1m
})
.ToSql();
Console.WriteLine(list0x1sql);
var sql1c2 = fsql.Select<User1>()
.GroupBy(a => new { a.Nickname, a.Avatar })
.WithTempQuery(b => new
{
sum = b.Sum(b.Value.Sort),
b.Key.Nickname,
b.Key.Avatar,
})
.OrderByDescending(arg => arg.sum)
.ToSql(arg => new
{
str1 = string.Concat(arg.Nickname, '-', arg.Avatar, '-'),
str2 = string.Concat(arg.Nickname, '-', arg.Avatar)
}); //报错 多括号
//.ToOne(arg => string.Concat(arg.Nickname, '-', arg.Avatar)); //正常
Console.WriteLine(sql1c2);

//var clickhouseSql1 = fsql.Select<User1>().Where(a => new[] { 1, 2, 3 }.Contains(a.GroupId)).ToSql();
// var clickhouseVal1 = new[] { 1, 2, 3 };
// var clickhouseSql2 = fsql.Select<User1>().Where(a => clickhouseVal1.Contains(a.GroupId)).ToSql();
// var clickhouseSql3 = fsql.Select<User1>().Where(a => a.Tags.Contains("tag1")).ToSql();
// var clickhouseVal2 = "tag2";
// var clickhouseSql4 = fsql.Select<User1>().Where(a => a.Tags.Contains(clickhouseVal2)).ToSql();

fsql.Update<User1>()
fsql.Update<User1>()
.Where(t => t.GroupId == 1)
.ExecuteUpdated();

Expand Down Expand Up @@ -831,16 +870,6 @@ static void Main(string[] args)
fsql.UseJsonMap();
fsql.Select<MiDevice>().Where(a => a.FormLocking == null).Count();

var list0x1sql = fsql.Select<OrderLine22x, Product22x>()
.InnerJoin((l, p) => l.ProductId == p.ID)
.GroupBy((l, p) => new { p.ID, l.ShopType })
.WithTempQuery(a => new {
a.Key.ID,
Money2 = a.Key.ShopType,
Money = a.Key.ShopType == 1 ? a.Value.Item1.Price * a.Value.Item1.Amount : a.Value.Item1.Price * a.Value.Item1.Amount * 1.1m
})
.ToSql();
Console.WriteLine(list0x1sql);

fsql.Delete<TypeHandler01>().Where("1=1").ExecuteAffrows();
FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(TestIdAndIdentity), new String_TestIdAndIdentity());
Expand Down Expand Up @@ -1211,22 +1240,8 @@ static void Main(string[] args)
.Set((a, b) => a.Nickname == "b.groupname")
.ExecuteAffrows();

var sql1c2 = fsql.Select<User1>()
.GroupBy(a => new { a.Nickname, a.Avatar })
.WithTempQuery(b => new
{
sum = b.Sum(b.Value.Sort),
b.Key.Nickname,
b.Key.Avatar,
})
.OrderByDescending(arg => arg.sum)
.ToSql(arg => new
{
str1 = string.Concat(arg.Nickname, '-', arg.Avatar, '-'),
str2 = string.Concat(arg.Nickname, '-', arg.Avatar)
}); //报错 多括号
//.ToOne(arg => string.Concat(arg.Nickname, '-', arg.Avatar)); //正常
Console.WriteLine(sql1c2);



var xp = new Xpb()
{
Expand Down Expand Up @@ -3065,4 +3080,26 @@ public class Test2
public string ItemName { get; set; }

public bool IsEnabled { get; set; }
}
}
[JsonObject(MemberSerialization.OptIn), Table(Name = "T_OrderLine111222")]
public partial class OrderLine
{

public string Id { get; set; }
public string OrderId { get; set; }

public string ShopId { get; set; }
[JsonProperty, Column(Name = "Shop_Type")]
public int? ShopType { get; set; }
public string ProductId { get; set; }
public decimal Price { get; set; }
public decimal? Amount { get; set; }
}
[JsonObject(MemberSerialization.OptIn), Table(Name = "T_Product111222")]
public partial class Product
{
public string ID { get; set; }
public string Name { get; set; }
public string Model { get; set; }
}

18 changes: 0 additions & 18 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 0 additions & 98 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class SelectGroupingProvider : BaseDiyMemberExpression
public List<SelectTableInfo> _tables;
public int _groupByLimit, _groupBySkip;
public bool _addFieldAlias;
public bool _flagNestedFieldAlias;

public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
{
Expand Down Expand Up @@ -53,6 +54,7 @@ public override string ParseExp(Expression[] members)
}
ParseExpMapResult = read;
if (!_addFieldAlias) return read.DbField;
if (_flagNestedFieldAlias) return read.DbField;
if (_comonExp.EndsWithDbNestedField(read.DbField, read.DbNestedField) == false)
{
_ParseExpOnlyDbField.Value = read.DbField;
Expand Down Expand Up @@ -252,7 +254,8 @@ public ISelect<TDto> WithTempQuery<TDto>(Expression<Func<ISelectGroupingAggregat
if (ret._tables[0].Table == null) ret._tables[0].Table = TableInfo.GetDefaultTable(typeof(TDto));
Select0Provider.WithTempQueryParser parser = null;
_addFieldAlias = true; //解决:[Column(Name = "flevel") 与属性名不一致时,嵌套查询 bug
var old_field = _field;
_flagNestedFieldAlias = true;//解决重复设置别名问题:.GroupBy((l, p) => new { p.ID, ShopType=l.ShopType??0 }).WithTempQuery(a => new { Money = a.Sum(a.Value.Item1.Amount)* a.Key.ShopType })
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())
Expand All @@ -268,7 +271,8 @@ public ISelect<TDto> WithTempQuery<TDto>(Expression<Func<ISelectGroupingAggregat
fieldsb.Clear();
_field = old_field;
_addFieldAlias = false;
}
_flagNestedFieldAlias = false;
}
var sql = $"\r\n{this.ToSql(parser._insideSelectList[0].InsideField)}";
ret.WithSql(sql);
ret._diymemexpWithTempQuery = parser;
Expand Down

0 comments on commit 0d2d517

Please sign in to comment.