From 837c38d158512b633187b2d2797f122058d1eae8 Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Tue, 29 Sep 2020 10:46:56 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20WhereDynamicFilter=20?=
=?UTF-8?q?=E6=94=AF=E6=8C=81=20string=20=E6=AF=94=E8=BE=83=E5=A4=A7?=
=?UTF-8?q?=E5=B0=8F=20>=20<=20>=3D=20<=3D=EF=BC=9B#479?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql.DbContext/FreeSql.DbContext.xml | 22 +-
.../Sqlite/Curd/SqliteSelectTest.cs | 22 +-
.../FreeSqlGlobalExpressionCallExtensions.cs | 39 ++
FreeSql/FreeSql.xml | 371 +++++++++---------
.../SelectProvider/Select0Provider.cs | 8 +-
5 files changed, 266 insertions(+), 196 deletions(-)
diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml
index 645b01c3..7ff55af8 100644
--- a/FreeSql.DbContext/FreeSql.DbContext.xml
+++ b/FreeSql.DbContext/FreeSql.DbContext.xml
@@ -130,6 +130,13 @@
清空状态数据
+
+
+ 根据 lambda 条件删除数据
+
+
+
+
添加
@@ -172,10 +179,6 @@
-
-
- 开始编辑数据,然后调用方法 EndEdit 分析出添加、修改、删除 SQL 语句进行执行
- 场景:winform 加载表数据后,一顿添加ember>
开始编辑数据,然后调用方法 EndEdit 分析出添加、修改、删除 SQL 语句进行执行
@@ -531,3 +534,14 @@
+Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
+
+ 批量注入 Repository,可以参考代码自行调整
+
+
+
+
+
+
+
+
diff --git a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
index 302d38e4..151a25d5 100644
--- a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
+++ b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
@@ -2174,13 +2174,33 @@ WHERE (((cast(a.""Id"" as character)) in (SELECT b.""Title""
""Field"" : ""CreateTime"",
""Operator"" : ""GreaterThanOrEqual"",
""Value"" : ""2010-10-10""
+ },
+ {
+ ""Field"" : ""Name"",
+ ""Operator"" : ""GreaterThan"",
+ ""Value"" : ""val31"",
+ },
+ {
+ ""Field"" : ""Name"",
+ ""Operator"" : ""GreaterThanOrEqual"",
+ ""Value"" : ""val32"",
+ },
+ {
+ ""Field"" : ""Name"",
+ ""Operator"" : ""LessThan"",
+ ""Value"" : ""val33"",
+ },
+ {
+ ""Field"" : ""Name"",
+ ""Operator"" : ""LessThanOrEqual"",
+ ""Value"" : ""val34"",
}
]
}
")).ToSql();
Assert.Equal(@"SELECT a.""Code"", a.""Name"", a.""CreateTime"", a.""testint"", a.""ParentCode""
FROM ""D_District"" a
-WHERE (((a.""Code"") LIKE '%val1%' AND (a.""Name"") LIKE 'val2%' OR (a.""Name"") LIKE '%val3' OR a.""ParentCode"" = 'val4' OR a.""CreateTime"" >= '2010-10-10 00:00:00'))", sql);
+WHERE (((a.""Code"") LIKE '%val1%' AND (a.""Name"") LIKE 'val2%' OR (a.""Name"") LIKE '%val3' OR a.""ParentCode"" = 'val4' OR a.""CreateTime"" >= '2010-10-10 00:00:00' OR a.""Name"" > 'val31' OR a.""Name"" >= 'val32' OR a.""Name"" < 'val33' OR a.""Name"" <= 'val34'))", sql);
sql = fsql.Select().WhereDynamicFilter(JsonConvert.DeserializeObject(@"
{
diff --git a/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs
index 9540c72a..2524d1af 100644
--- a/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs
+++ b/FreeSql/Extensions/FreeSqlGlobalExpressionCallExtensions.cs
@@ -117,6 +117,45 @@ namespace FreeSql
return default(TValue);
}
+ #region 大小判断
+ ///
+ /// 大于 >
+ ///
+ ///
+ public static bool GreaterThan(TValue value1, TValue value2)
+ {
+ expContext.Value.Result = $"{expContext.Value.ParsedContent["value1"]} > {expContext.Value.ParsedContent["value2"]}";
+ return false;
+ }
+ ///
+ /// 大于或等于 >=
+ ///
+ ///
+ public static bool GreaterThanOrEqual(TValue value1, TValue value2)
+ {
+ expContext.Value.Result = $"{expContext.Value.ParsedContent["value1"]} >= {expContext.Value.ParsedContent["value2"]}";
+ return false;
+ }
+ ///
+ /// 小于 <
+ ///
+ ///
+ public static bool LessThan(TValue value1, TValue value2)
+ {
+ expContext.Value.Result = $"{expContext.Value.ParsedContent["value1"]} < {expContext.Value.ParsedContent["value2"]}";
+ return false;
+ }
+ ///
+ /// 小于或等于 <=
+ ///
+ ///
+ public static bool LessThanOrEqual(TValue value1, TValue value2)
+ {
+ expContext.Value.Result = $"{expContext.Value.ParsedContent["value1"]} <= {expContext.Value.ParsedContent["value2"]}";
+ return false;
+ }
+ #endregion
+
///
/// case when .. then .. end
///
diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml
index 7c9e44f2..8fd7f738 100644
--- a/FreeSql/FreeSql.xml
+++ b/FreeSql/FreeSql.xml
@@ -1123,6 +1123,30 @@
+
+
+ 大于 >
+
+
+
+
+
+ 大于或等于 >=
+
+
+
+
+
+ 小于 <
+
+
+
+
+
+ 小于或等于 <=
+
+
+
case when .. then .. end
@@ -2973,6 +2997,154 @@
+
+
+ 测试数据库是否连接正确,本方法执行如下命令:
+ 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>
+
+
+
+
+
+
可自定义解析表达式
@@ -3779,6 +3951,12 @@
超时
+
+
+ 获取资源
+
+
+
使用完毕后,归还资源
@@ -3849,6 +4027,12 @@
资源对象
+
+
+ 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象
+
+ 资源对象
+
归还对象给对象池的时候触发
@@ -4459,190 +4643,3 @@
-essions.Expression{System.Func{``0,``1,``2,``3,``4,System.Boolean}},System.Linq.Expressions.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 条件
-
-
-
-
diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs
index e6020a4a..962039dd 100644
--- a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs
+++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs
@@ -533,10 +533,10 @@ namespace FreeSql.Internal.CommonProvider
case DynamicFilterOperator.Equal: exp = Expression.Equal(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
case DynamicFilterOperator.NotEqual: exp = Expression.NotEqual(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
- case DynamicFilterOperator.GreaterThan: exp = Expression.GreaterThan(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
- case DynamicFilterOperator.GreaterThanOrEqual: exp = Expression.GreaterThanOrEqual(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
- case DynamicFilterOperator.LessThan: exp = Expression.LessThan(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
- case DynamicFilterOperator.LessThanOrEqual: exp = Expression.LessThanOrEqual(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
+ case DynamicFilterOperator.GreaterThan: exp = Expression.Call(typeof(SqlExt).GetMethod("GreaterThan").MakeGenericMethod(exp.Type), exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
+ case DynamicFilterOperator.GreaterThanOrEqual: exp = Expression.Call(typeof(SqlExt).GetMethod("GreaterThanOrEqual").MakeGenericMethod(exp.Type), exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
+ case DynamicFilterOperator.LessThan: exp = Expression.Call(typeof(SqlExt).GetMethod("LessThan").MakeGenericMethod(exp.Type), exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
+ case DynamicFilterOperator.LessThanOrEqual: exp = Expression.Call(typeof(SqlExt).GetMethod("LessThanOrEqual").MakeGenericMethod(exp.Type), exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fi.Value?.ToString()), exp.Type)); break;
case DynamicFilterOperator.Range:
var fiValueRangeArray = getFiListValue();
if (fiValueRangeArray.Length != 2) throw new ArgumentException($"Range 要求 Value 应该逗号分割,并且长度为 2");