From 0718be20472ec91d88c80f61c66a26ee59aa8bd3 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Fri, 18 Sep 2020 06:35:33 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20SqlExt.Sum/Max/Min/Avg?= =?UTF-8?q?=20=E5=90=8C=E6=97=B6=E6=94=AF=E6=8C=81=E5=BC=80=E7=AA=97?= =?UTF-8?q?=E6=88=96=E6=99=AE=E9=80=9A=E8=81=9A=E5=90=88=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 16 - FreeSql.Tests/FreeSql.Tests/UnitTest3.cs | 13 + .../FreeSqlGlobalExpressionCallExtensions.cs | 9 +- FreeSql/FreeSql.xml | 346 ++++++++---------- 4 files changed, 178 insertions(+), 206 deletions(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 743835e4..5a0c8bd0 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -130,13 +130,6 @@ 清空状态数据 - - - 根据 lambda 条件删除数据 - - - - 添加 @@ -532,14 +525,5 @@ - - - 批量注入 Repository,可以参考代码自行调整 - - - - - - diff --git a/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs b/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs index 1f3ef8df..003bd4d2 100644 --- a/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs +++ b/FreeSql.Tests/FreeSql.Tests/UnitTest3.cs @@ -177,6 +177,17 @@ namespace FreeSql.Tests [Fact] public void Test03() { + var sqlextMax112 = g.sqlserver.Select() + .GroupBy(a => a.Id) + .ToSql(a => new + { + Id = a.Key, + EdiId1 = SqlExt.Max(a.Key).Over().PartitionBy(new { a.Value.EdiId, a.Value.Id }).OrderByDescending(new { a.Value.EdiId, a.Value.Id }).ToValue(), + EdiId2 = SqlExt.Max(a.Key).Over().PartitionBy(a.Value.EdiId).OrderByDescending(a.Value.Id).ToValue(), + EdiId3 = SqlExt.Sum(a.Key).ToValue(), + EdiId4 = a.Sum(a.Key) + }); + Assert.Throws(() => g.sqlite.Update().SetSource(new testUpdateNonePk()).ExecuteAffrows()); g.sqlite.Insert(new testInsertNullable()).NoneParameter().ExecuteAffrows(); @@ -304,6 +315,8 @@ INNER JOIN ""userinfo"" p ON p.""userid"" = o.""userid""", select16Sql2); Id = a.Key, EdiId1 = SqlExt.Max(a.Key).Over().PartitionBy(new { a.Value.EdiId, a.Value.Id }).OrderByDescending(new { a.Value.EdiId, a.Value.Id }).ToValue(), EdiId2 = SqlExt.Max(a.Key).Over().PartitionBy(a.Value.EdiId).OrderByDescending(a.Value.Id).ToValue(), + EdiId3 = SqlExt.Sum(a.Key).ToValue(), + EdiId4 = a.Sum(a.Key) }); var sqlextIsNull = g.sqlserver.Select() diff --git a/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs index d534c527..9540c72a 100644 --- a/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs +++ b/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs @@ -152,6 +152,7 @@ namespace FreeSql internal class ExpSbInfo { public StringBuilder Sb { get; } = new StringBuilder(); + public bool IsOver = false; public bool IsOrderBy = false; public bool IsDistinct = false; } @@ -161,12 +162,13 @@ namespace FreeSql { if (expSb.Value == null) expSb.Value = new List(); expSb.Value.Add(new ExpSbInfo()); - expSbLast.Sb.Append(sqlFunc).Append(" "); + expSbLast.Sb.Append(sqlFunc); return null; } public static ISqlOver Over(this ISqlOver that) { - expSbLast.Sb.Append("over("); + expSbLast.Sb.Append(" over("); + expSbLast.IsOver = true; return that; } public static ISqlOver PartitionBy(this ISqlOver that, object column) @@ -218,9 +220,10 @@ namespace FreeSql public static TValue ToValue(this ISqlOver that) { var sql = expSbLast.Sb.ToString().TrimEnd(','); + if (expSbLast.IsOver) sql = $"{sql})"; expSbLast.Sb.Clear(); expSb.Value.RemoveAt(expSb.Value.Count - 1); - expContext.Result = $"{sql})"; + expContext.Result = sql; return default; } public interface ISqlOver { } diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 8e952c15..8869f42b 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2898,6 +2898,153 @@ + + + 测试数据库是否连接正确,本方法执行如下命令: + 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> + + + + + + 可自定义解析表达式 @@ -3667,6 +3814,12 @@ 超时 + + + 获取资源 + + + 使用完毕后,归还资源 @@ -3737,6 +3890,12 @@ 资源对象 + + + 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象 + + 资源对象 + 归还对象给对象池的时候触发 @@ -4347,190 +4506,3 @@ -ons.Expression{System.Func{``0,``1,``2,``3,``4,System.Boolean}})"> - - 使用 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 条件 - - - -