From defab45624296a4a268950c7018feb73061b4c11 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Sat, 23 Nov 2019 19:02:38 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=AE=8C=E5=96=84=20ExpressionCall=20?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A1=A8=E8=BE=BE=E5=BC=8F=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 7 +++++++ FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/g.cs | 1 + FreeSql.Tests/FreeSql.Tests/g.cs | 4 ++++ FreeSql/DataAnnotations/ExpressionCallAttribute.cs | 9 +++++---- FreeSql/FreeSql.xml | 5 +++-- FreeSql/Internal/CommonExpression.cs | 8 ++++++-- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index dc0203b8..d9f91124 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -110,6 +110,13 @@ 清空状态数据 + + + 根据 lambda 条件删除数据 + + + + 添加 diff --git a/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/g.cs b/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/g.cs index ee572d0d..a5942054 100644 --- a/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/g.cs +++ b/FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/g.cs @@ -10,6 +10,7 @@ public class g static Lazy mysqlLazy = new Lazy(() => new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd_mysqlconnector;Charset=utf8;SslMode=none;Max pool size=10") .UseAutoSyncStructure(true) + //.UseGenerateCommandParameterWithLambda(true) .UseMonitorCommand( cmd => { diff --git a/FreeSql.Tests/FreeSql.Tests/g.cs b/FreeSql.Tests/FreeSql.Tests/g.cs index ae796f92..4c336e30 100644 --- a/FreeSql.Tests/FreeSql.Tests/g.cs +++ b/FreeSql.Tests/FreeSql.Tests/g.cs @@ -11,6 +11,7 @@ public class g static Lazy mysqlLazy = new Lazy(() => new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2") .UseAutoSyncStructure(true) + //.UseGenerateCommandParameterWithLambda(true) .UseMonitorCommand( cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象,在执行前 (cmd, traceLog) => Console.WriteLine(traceLog)) @@ -24,6 +25,7 @@ public class g return new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2") .UseAutoSyncStructure(true) + //.UseGenerateCommandParameterWithLambda(true) .UseSyncStructureToLower(true) .UseLazyLoading(true) .UseMonitorCommand( @@ -48,6 +50,7 @@ public class g static Lazy oracleLazy = new Lazy(() => new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2") .UseAutoSyncStructure(true) + //.UseGenerateCommandParameterWithLambda(true) .UseLazyLoading(true) .UseSyncStructureToUpper(true) //.UseNoneCommandParameter(true) @@ -61,6 +64,7 @@ public class g static Lazy sqliteLazy = new Lazy(() => new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=2") .UseAutoSyncStructure(true) + //.UseGenerateCommandParameterWithLambda(true) .UseLazyLoading(true) .UseMonitorCommand( cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象,在执行前 diff --git a/FreeSql/DataAnnotations/ExpressionCallAttribute.cs b/FreeSql/DataAnnotations/ExpressionCallAttribute.cs index 2f91e4d8..c9c964e9 100644 --- a/FreeSql/DataAnnotations/ExpressionCallAttribute.cs +++ b/FreeSql/DataAnnotations/ExpressionCallAttribute.cs @@ -7,9 +7,9 @@ namespace FreeSql.DataAnnotations { /// /// 自定义表达式函数解析 - /// 注意:请使用静态扩展类 + /// 注意:请使用静态方法、或者在类上标记 /// - [AttributeUsage(AttributeTargets.Class)] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class ExpressionCallAttribute : Attribute { } @@ -32,9 +32,10 @@ namespace FreeSql.DataAnnotations public DbParameter DbParameter { get; internal set; } /// - /// 可附加参数化对象 + /// 可附加参数化对象 + /// 注意:本属性只有 Where 的表达式解析才可用 /// - public List UserParameters { get; } = new List(); + public List UserParameters { get; internal set; } /// /// 返回表达式函数表示的 SQL 字符串 diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 905d6cd1..996523e3 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -155,7 +155,7 @@ 自定义表达式函数解析 - 注意:请使用静态扩展类 + 注意:请使用静态方法、或者在类上标记 @@ -175,7 +175,8 @@ - 可附加参数化对象 + 可附加参数化对象 + 注意:本属性只有 Where 的表达式解析才可用 diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index 21232567..ee433e88 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -500,6 +500,7 @@ namespace FreeSql.Internal return $"{left} {oper} {right}"; } static ConcurrentDictionary _dicTypeExistsExpressionCallAttribute = new ConcurrentDictionary(); + static ConcurrentDictionary> _dicMethodExistsExpressionCallAttribute = new ConcurrentDictionary>(); static ConcurrentDictionary _dicTypeExpressionCallClassContextFields = new ConcurrentDictionary(); public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc) { @@ -541,9 +542,12 @@ namespace FreeSql.Internal case ExpressionType.Call: tsc.mapType = null; var exp3 = exp as MethodCallExpression; - if (exp3.Object == null && _dicTypeExistsExpressionCallAttribute.GetOrAdd(exp3.Method.DeclaringType, dttp => dttp.GetCustomAttributes(typeof(ExpressionCallAttribute), true).Any())) + if (exp3.Object == null && ( + _dicTypeExistsExpressionCallAttribute.GetOrAdd(exp3.Method.DeclaringType, dttp => dttp.GetCustomAttributes(typeof(ExpressionCallAttribute), true).Any()) || + exp3.Method.GetCustomAttributes(typeof(ExpressionCallAttribute), true).Any() + )) { - var ecc = new ExpressionCallContext { DataType = _ado.DataType }; + var ecc = new ExpressionCallContext { DataType = _ado.DataType, UserParameters = tsc.dbParams == null ? null : new List() }; var exp3MethodParams = exp3.Method.GetParameters(); var dbParamsIndex = tsc.dbParams?.Count; ecc.ParsedContent.Add(exp3MethodParams[0].Name, ExpressionLambdaToSql(exp3.Arguments[0], tsc));