- 完善 ExpressionCall 自定义表达式功能;

This commit is contained in:
28810 2019-11-23 19:02:38 +08:00
parent 5961479f2a
commit defab45624
6 changed files with 26 additions and 8 deletions

View File

@ -110,6 +110,13 @@
清空状态数据
</summary>
</member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)">
<summary>
添加

View File

@ -10,6 +10,7 @@ public class g
static Lazy<IFreeSql> mysqlLazy = new Lazy<IFreeSql>(() => 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 =>
{

View File

@ -11,6 +11,7 @@ public class g
static Lazy<IFreeSql> mysqlLazy = new Lazy<IFreeSql>(() => 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<IFreeSql> oracleLazy = new Lazy<IFreeSql>(() => 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<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => 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命令对象在执行前

View File

@ -7,9 +7,9 @@ namespace FreeSql.DataAnnotations
{
/// <summary>
/// 自定义表达式函数解析<para></para>
/// 注意:请使用静态扩展类
/// 注意:请使用静态方法、或者在类上标记
/// </summary>
[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; }
/// <summary>
/// 可附加参数化对象
/// 可附加参数化对象<para></para>
/// 注意:本属性只有 Where 的表达式解析才可用
/// </summary>
public List<DbParameter> UserParameters { get; } = new List<DbParameter>();
public List<DbParameter> UserParameters { get; internal set; }
/// <summary>
/// 返回表达式函数表示的 SQL 字符串

View File

@ -155,7 +155,7 @@
<member name="T:FreeSql.DataAnnotations.ExpressionCallAttribute">
<summary>
自定义表达式函数解析<para></para>
注意:请使用静态扩展类
注意:请使用静态方法、或者在类上标记
</summary>
</member>
<member name="P:FreeSql.DataAnnotations.ExpressionCallContext.DataType">
@ -175,7 +175,8 @@
</member>
<member name="P:FreeSql.DataAnnotations.ExpressionCallContext.UserParameters">
<summary>
可附加参数化对象
可附加参数化对象<para></para>
注意:本属性只有 Where 的表达式解析才可用
</summary>
</member>
<member name="P:FreeSql.DataAnnotations.ExpressionCallContext.Result">

View File

@ -500,6 +500,7 @@ namespace FreeSql.Internal
return $"{left} {oper} {right}";
}
static ConcurrentDictionary<Type, bool> _dicTypeExistsExpressionCallAttribute = new ConcurrentDictionary<Type, bool>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicMethodExistsExpressionCallAttribute = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
static ConcurrentDictionary<Type, FieldInfo[]> _dicTypeExpressionCallClassContextFields = new ConcurrentDictionary<Type, FieldInfo[]>();
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<DbParameter>() };
var exp3MethodParams = exp3.Method.GetParameters();
var dbParamsIndex = tsc.dbParams?.Count;
ecc.ParsedContent.Add(exp3MethodParams[0].Name, ExpressionLambdaToSql(exp3.Arguments[0], tsc));