From a41903699951b05a70f87177b74e9e554bfed8f8 Mon Sep 17 00:00:00 2001
From: 2881099 <2881099@qq.com>
Date: Mon, 25 Jul 2022 16:08:29 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E5=AE=8C=E5=96=84=20WithTempQuery=20+=20Fr?=
=?UTF-8?q?omQuery=20=E5=88=86=E7=BB=84=E5=B5=8C=E5=A5=97=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql.DbContext/FreeSql.DbContext.xml | 9 +
.../Curd/SqlServerSelectWithTempQueryTest.cs | 41 ++
FreeSql/FreeSql.xml | 402 ++++++++----------
FreeSql/Internal/CommonExpression.cs | 15 +-
4 files changed, 242 insertions(+), 225 deletions(-)
diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml
index 537315e2..26522f10 100644
--- a/FreeSql.DbContext/FreeSql.DbContext.xml
+++ b/FreeSql.DbContext/FreeSql.DbContext.xml
@@ -800,5 +800,14 @@
+
+
+ 批量注入 Repository,可以参考代码自行调整
+
+
+
+
+
+
diff --git a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectWithTempQueryTest.cs b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectWithTempQueryTest.cs
index d1fc0b52..2d2502d1 100644
--- a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectWithTempQueryTest.cs
+++ b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectWithTempQueryTest.cs
@@ -672,6 +672,47 @@ WHERE (a.[rownum] = 1) AND ((a.[Nickname] = N'name03' OR a.[Nickname] = N'name02
Assert.Equal(list10[1].rownum, 1);
Assert.Equal(list10[1].Id, 0);
Assert.Null(list10[1].remark);
+
+
+ var sql11 = fsql.Select()
+ .WithTempQuery(a => new
+ {
+ user = a,
+ rownum = SqlExt.RowNumber().Over().PartitionBy(a.Nickname).OrderBy(a.Id).ToValue()
+ })
+ .Where(a => a.rownum == 1)
+ .FromQuery(fsql.Select().Where(b => b.UserId > 0).GroupBy(b => b.UserId).WithTempQuery(b => new { uid = b.Key, rownum = b.Sum(b.Value.UserId) }))
+ .InnerJoin((a, b) => a.user.Id == b.uid)
+ .Where((a, b) => a.user.Nickname == "name03" || a.user.Nickname == "name02")
+ .ToSql((a, b) => new TwoTablePartitionBy_UserDto());
+ var assertSql11 = @"SELECT a.[rownum] as1
+FROM (
+ SELECT a.[Id], a.[Nickname], row_number() over( partition by a.[Nickname] order by a.[Id]) [rownum]
+ FROM [TwoTablePartitionBy_User] a ) a
+INNER JOIN ( SELECT a.[UserId] [uid], sum(a.[UserId]) [rownum]
+ FROM [TwoTablePartitionBy_UserExt] a
+ WHERE (a.[UserId] > 0)
+ GROUP BY a.[UserId] ) b ON a.[Id] = b.[uid]
+WHERE (a.[rownum] = 1) AND ((a.[Nickname] = N'name03' OR a.[Nickname] = N'name02'))";
+ Assert.Equal(sql11, assertSql11);
+ var list11 = fsql.Select()
+ .WithTempQuery(a => new
+ {
+ user = a,
+ rownum = SqlExt.RowNumber().Over().PartitionBy(a.Nickname).OrderBy(a.Id).ToValue()
+ })
+ .Where(a => a.rownum == 1)
+ .FromQuery(fsql.Select().Where(b => b.UserId > 0).GroupBy(b => b.UserId).WithTempQuery(b => new { uid = b.Key, rownum = b.Sum(b.Value.UserId) }))
+ .InnerJoin((a, b) => a.user.Id == b.uid)
+ .Where((a, b) => a.user.Nickname == "name03" || a.user.Nickname == "name02")
+ .ToList();
+ Assert.Equal(list11.Count, 2);
+ Assert.Equal(list11[0].rownum, 1);
+ Assert.Equal(list11[0].Id, 0);
+ Assert.Null(list11[0].remark);
+ Assert.Equal(list11[1].rownum, 1);
+ Assert.Equal(list11[1].Id, 0);
+ Assert.Null(list11[1].remark);
}
class TwoTablePartitionBy_User
{
diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml
index f7862590..6cda4744 100644
--- a/FreeSql/FreeSql.xml
+++ b/FreeSql/FreeSql.xml
@@ -3324,6 +3324,177 @@
+
+
+ 测试数据库是否连接正确,本方法执行如下命令:
+ MySql/SqlServer/PostgreSQL/达梦/人大金仓/神通: SELECT 1
+ Oracle: SELECT 1 FROM dual
+
+ 命令超时设置(秒)
+
+ true: 成功, false: 失败
+
+
+
+ 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】
+
+
+
+
+
+
+
+
+
+ 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > @age", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
+ 查询
+
+
+
+
+
+
+
+
+ 查询,ExecuteArrayAsync("select * from user where age > @age", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
+ 查询
+
+
+
+
+
+
+
+
+ 查询,ExecuteDataSetAsync("select * from user where age > @age; select 2", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
+ 查询
+
+
+
+
+
+
+
+
+ 查询,ExecuteDataTableAsync("select * from user where age > @age", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
+ 在【主库】执行
+
+
+
+
+
+
+
+
+ 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > @age", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
+ 在【主库】执行
+
+
+
+
+
+
+
+
+ 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > @age", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 })
+
+
+
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 })
+
+
+
+
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,Query<User, Address>("select * from user where age > @age; select * from address", new { age = 25 })
+ 提示:parms 参数还可以传 Dictionary<string, object>
+
+
+
+
+
+
+
+
可自定义解析表达式
@@ -4347,6 +4518,12 @@
超时
+
+
+ 获取资源
+
+
+
使用完毕后,归还资源
@@ -4422,6 +4599,12 @@
资源对象
+
+
+ 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象
+
+ 资源对象
+
归还对象给对象池的时候触发
@@ -5863,222 +6046,3 @@
-ystem.Boolean}})">
-
- 使用 and 拼接两个 lambda 表达式
-
-
- true 时生效
-
-
-
-
-
- 使用 or 拼接两个 lambda 表达式
-
-
-
-
-
- 使用 or 拼接两个 lambda 表达式
-
-
- true 时生效
-
-
-
-
-
- 将 lambda 表达式取反
-
-
- true 时生效
-
-
-
-
- 使用 and 拼接两个 lambda 表达式
-
-
-
-
-
- 使用 and 拼接两个 lambda 表达式
-
-
- true 时生效
-
-
-
-
-
- 使用 or 拼接两个 lambda 表达式
-
-
-
-
-
- 使用 or 拼接两个 lambda 表达式
-
-
- true 时生效
-
-
-
-
-
- 将 lambda 表达式取反
-
-
- true 时生效
-
-
-
-
- 生成类似Mongodb的ObjectId有序、不重复Guid
-
-
-
-
-
- 插入数据
-
-
-
-
-
-
- 插入数据,传入实体
-
-
-
-
-
-
-
- 插入数据,传入实体数组
-
-
-
-
-
-
-
- 插入数据,传入实体集合
-
-
-
-
-
-
-
- 插入数据,传入实体集合
-
-
-
-
-
-
-
- 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下:
- MySql 5.6+: on duplicate key update
- PostgreSQL 9.4+: on conflict do update
- SqlServer 2008+: merge into
- Oracle 11+: merge into
- Sqlite: replace into
- 达梦: merge into
- 人大金仓:on conflict do update
- 神通:merge into
- MsAccess:不支持
- 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性)
-
-
-
-
-
-
- 修改数据
-
-
-
-
-
-
- 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
-
-
- 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合
-
-
-
-
- 查询数据
-
-
-
-
-
-
- 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
-
-
- 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合
-
-
-
-
- 删除数据
-
-
-
-
-
-
- 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
-
-
- 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合
-
-
-
-
- 开启事务(不支持异步)
- v1.5.0 关闭了线程事务超时自动提交的机制
-
- 事务体 () => {}
-
-
-
- 开启事务(不支持异步)
- v1.5.0 关闭了线程事务超时自动提交的机制
-
-
- 事务体 () => {}
-
-
-
- 数据库访问对象
-
-
-
-
- 所有拦截方法都在这里
-
-
-
-
- CodeFirst 模式开发相关方法
-
-
-
-
- DbFirst 模式开发相关方法
-
-
-
-
- 全局过滤设置,可默认附加为 Select/Update/Delete 条件
-
-
-
-
diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs
index b24dcec0..a42e34bf 100644
--- a/FreeSql/Internal/CommonExpression.cs
+++ b/FreeSql/Internal/CommonExpression.cs
@@ -242,23 +242,26 @@ namespace FreeSql.Internal
if (diymemexp != null && exp is MemberExpression expMem2 && expMem2.Member.Name == "Key" && expMem2.Expression.Type.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`"))
{
field.Append(diymemexp._field);
+ string dbNestedField = null;
if (diymemexp._map.Childs.Any() == false) //处理 GroupBy(a => a.Title) ToSql(g => new { tit = a.Key }, FieldAliasOptions.AsProperty) 问题
{
if (index >= 0)
{
- parent.DbNestedField = $"as{++index}";
- field.Append(_common.FieldAsAlias(parent.DbNestedField));
+ dbNestedField = $"as{++index}";
+ field.Append(_common.FieldAsAlias(dbNestedField));
}
else if (string.IsNullOrEmpty(parent.CsName) == false)
{
- parent.DbNestedField = GetFieldAsCsName(parent.CsName);
- if (index == ReadAnonymousFieldAsCsName && diymemexp._field.EndsWith(parent.DbNestedField, StringComparison.CurrentCultureIgnoreCase) == false) //DbField 和 CsName 相同的时候,不处理
- field.Append(_common.FieldAsAlias(parent.DbNestedField));
+ dbNestedField = GetFieldAsCsName(parent.CsName);
+ if (index == ReadAnonymousFieldAsCsName && diymemexp._field.EndsWith(dbNestedField, StringComparison.CurrentCultureIgnoreCase) == false) //DbField 和 CsName 相同的时候,不处理
+ field.Append(_common.FieldAsAlias(dbNestedField));
}
}
var parentProp = parent.Property;
- diymemexp._map.CopyTo(parent);
+ diymemexp._map.CopyTo(parent); //可能会清空 parent.DbNestedField、CsName 值
parent.Property = parentProp; //若不加此行,会引用 GroupBy(..).ToList(a => new Dto { key = a.Key }) null 错误,CopyTo 之后 Property 变为 null
+ if (string.IsNullOrWhiteSpace(dbNestedField) == false)
+ parent.DbNestedField = dbNestedField;
return false;
}
if (parent.CsType == null) parent.CsType = exp.Type;