diff --git a/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteSelectTest.cs b/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteSelectTest.cs index b40fe1fb..f918dc59 100644 --- a/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteSelectTest.cs +++ b/FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/Sqlite/Curd/SqliteSelectTest.cs @@ -2541,10 +2541,52 @@ WHERE (((name,no) in (('testname01','testname01')) OR not((a.""no"") LIKE '%test public ts_dyfilter_enum01_status status { get; set; } } public enum ts_dyfilter_enum01_status { staring, stoped, finished } + + + [Fact] + public void WhereDynamicFilter2() + { + var fsql = g.sqlite; + + var dyfilterJson = @" +{ + ""Logic"" : ""Or"", + ""Filters"" : + [ + { + ""Field"" : ""RegCount FreeSql.Tests.Sqlite.DynamicFilterMyCustom, FreeSql.Tests.Provider.Sqlite.Data"", + ""Operator"" : ""Custom"", + ""Value"" : ""19"" + }, + { + ""Field"" : ""name"", + ""Operator"" : ""NotEndsWith"", + ""Value"" : ""testname01"" + } + ] +}"; + var dyfilter = JsonConvert.DeserializeObject(dyfilterJson); + var sql = fsql.Select().WhereDynamicFilter(dyfilter).ToSql(); + + } } + class RegLog + { + public Guid tid { get; set; } + } public class DynamicFilterMyCustom { + [DynamicFilterCustom] + public static string RegCount(object sender, string value) + { + var count = int.Parse(value); + + var select = sender as Select0Provider; + var sql = select._orm.Select().ToSql("count(1)"); + return $"({sql}) > {count}"; + } + [DynamicFilterCustom] public static string MyRawSql(object sender, string value) => value; diff --git a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs index 4001e847..d57aa784 100644 --- a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs @@ -1890,6 +1890,27 @@ INNER JOIN [TestTypeInfo] a__Type With(NoLock) ON a__Type.[Guid] = a.[Id]", sql2 Assert.Equal(@"SELECT a.[Id], a.[Clicks], a.[TypeGuid], a__Type.[Guid], a__Type.[ParentId], a__Type.[Name], a.[Title], a.[CreateTime] FROM [tb_topic22] a INNER JOIN [TestTypeInfo] a__Type With(NoLock) ON a__Type.[Guid] = a.[Id]", sql2); + + sql2 = orm.Select() + .InnerJoin(a => a.Type.Guid == a.Id) + .WithLock(SqlServerLock.NoLock) + .ToSql(); + Assert.Equal(@"SELECT a.[Id], a.[Clicks], a.[TypeGuid], a__Type.[Guid], a__Type.[ParentId], a__Type.[Name], a.[Title], a.[CreateTime] +FROM [tb_topic22] a With(NoLock) +INNER JOIN [TestTypeInfo] a__Type With(NoLock) ON a__Type.[Guid] = a.[Id]", sql2); + + sql2 = orm.Select() + .InnerJoin(a => a.Type.Guid == a.Id) + .WithLock(SqlServerLock.NoLock) + .Where(a => orm.Select() + .WithLock(SqlServerLock.NoLock, null).Where(b => b.Guid == a.TypeGuid).Any()) + .ToSql(); + Assert.Equal(@"SELECT a.[Id], a.[Clicks], a.[TypeGuid], a__Type.[Guid], a__Type.[ParentId], a__Type.[Name], a.[Title], a.[CreateTime] +FROM [tb_topic22] a With(NoLock) +INNER JOIN [TestTypeInfo] a__Type With(NoLock) ON a__Type.[Guid] = a.[Id] +WHERE (exists(SELECT TOP 1 1 + FROM [TestTypeInfo] b With(NoLock) + WHERE (b.[Guid] = a.[TypeGuid])))", sql2); } [Fact] public void ForUpdate() diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index fe98cb90..fb15a822 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -3303,177 +3303,6 @@ - - - 测试数据库是否连接正确,本方法执行如下命令: - 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> - - - - - - - - 可自定义解析表达式 @@ -4497,12 +4326,6 @@ 超时 - - - 获取资源 - - - 使用完毕后,归还资源 @@ -4578,12 +4401,6 @@ 资源对象 - - - 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象 - - 资源对象 - 归还对象给对象池的时候触发 @@ -6025,3 +5842,222 @@ +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 9b13cb3c..1272b088 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -993,6 +993,7 @@ namespace FreeSql.Internal break; } object fsql = null; + Expression fsqlExpLambda = null; Select0Provider fsqlSelect0 = null; List fsqltables = null; var fsqltable1SetAlias = false; @@ -1069,7 +1070,11 @@ namespace FreeSql.Internal while (exp3StackOld.Any()) exp3Stack.Push(exp3StackOld.Pop()); } } - if (fsql == null) fsql = Expression.Lambda(exp3tmp).Compile().DynamicInvoke(); + if (fsql == null) + { + fsql = Expression.Lambda(exp3tmp).Compile().DynamicInvoke(); + fsqlExpLambda = exp3tmp; + } fsqlType = fsql?.GetType(); if (fsqlType == null) break; fsqlSelect0 = fsql as Select0Provider; @@ -1134,6 +1139,10 @@ namespace FreeSql.Internal { args[a] = (arg3Exp as ConstantExpression)?.Value; } + else if (arg3Exp == fsqlExpLambda) + { + args[a] = fsql; + } else { var argExp = (arg3Exp as UnaryExpression)?.Operand;