diff --git a/AOP.md b/AOP.md index 0925588..4a5dea3 100644 --- a/AOP.md +++ b/AOP.md @@ -90,7 +90,7 @@ fsql.Aop.ConfigEntityProperty = (s, e) => { 就这样,FreeSql 的实体特性就可以和 EFCore 那样设定了。其他自增、乐观锁等,依葫芦画瓢便是。 -## 自定义表达式 +## 表达式拦截 FreeSql 内部表达式支持非常丰富,对各大数据库的兼容度也做得很好。 diff --git a/更新日志.md b/更新日志.md index 82f5cd9..efcdd7f 100644 --- a/更新日志.md +++ b/更新日志.md @@ -5,7 +5,7 @@ - 增加 ICodeFirst.IsGenerateCommandParameterWithLambda 选项,开启表达式解析的命令参数化;[wiki](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0#%E5%91%BD%E4%BB%A4%E5%8F%82%E6%95%B0%E5%8C%96v0121) > FreeSqlBuilder 上使用 UseGenerateCommandParameterWithLambda(true) 开启 -- 增加 ExpressionCallContext 自定义函数上下文档 DbParameter 属性; +- 优化 ExpressionCallContext 可设置、附加参数化对象; - 修复 IncludeMany(a => a.x1.x2.Childs) 当 x1, x2 为 null 的报 null 错误; ## v0.11.24 diff --git a/表达式函数.md b/表达式函数.md index d6ef2e1..5b1a214 100644 --- a/表达式函数.md +++ b/表达式函数.md @@ -31,15 +31,33 @@ public static class DbFunc //必要定义 static + ThreadLocal static ThreadLocal context = new ThreadLocal(); - public static string FormatDateTime(this DateTime that, string arg1) + public static DateTime FormatDateTime(this DateTime that, string arg1) { var up = context.Value; - if (up.DataType == FreeSql.DataType.Sqlite) - return $"date_format({up.Values["that"]}, {up.Values["arg1"]})"; - return ""; + if (up.DataType == FreeSql.DataType.Sqlite) //重写内容 + context.Value.Result = $"date_format({up.Values["that"]}, {up.Values["arg1"]})"; + return that; } - //...在此类中定义更多方法 + public static string SetDbParameter(this string that, int size) + { + if (context.Value.DbParameter != null) + { + //已经参数化了,直接修改长度 + //提示:此条件可能开启了全局表达式参数化功能 UseGenerateCommandParameterWithLambda(true) + context.Value.DbParameter.Size = size; + return that; + } + var guid = Guid.NewGuid().ToString("N").ToLower(); + context.Value.UserParameters.Add(new SqlParameter + { + ParameterName = guid, + SqlDbType = System.Data.SqlDbType.VarChar, + Size = size, + Value = that + }); + return $"@{guid}"; //重写内容 + } } var sql = fsql.Select() @@ -48,9 +66,15 @@ var sql = fsql.Select() //FROM "SysModule" a ``` -context.Value.DataType 是 FreeSql.DataType 枚举类型,以便实现不同数据库的适配; +| ExpressionCallContext 属性 | 类型 |描述 | +| -- | -- | -- | +| context.Value.DataType | FreeSql.DataType | 用于实现不同数据库的适配判断条件 | +| context.Value.ParsedContent | Dictionary\ |函数的各参数解析结果 | +| context.Value.DbParameter | System.Data.Common.DbParameter | that 被参数化的对象(有可能为 null) | +| context.Value.UserParameters | List\ | 可附加参数化对象 | +| context.Value.Result | string | 返回表达式函数表示的 SQL 字符串 | -context.Value.Values 是 函数的各参数解析结果; +> 当扩展方法返回值为 string 时,其返回值也可以当作 context.Value.Result 功能 ## 命令参数化(v0.12.1) @@ -97,7 +121,7 @@ public static class DbFunc { if (context.Value.DbParameter != null) context.Value.DbParameter.Size = size; - return context.Value.Values["that"]; + return context.Value.ParsedContent["that"]; } } ``` diff --git a/骚操作.md b/骚操作.md index 282026a..9a15b01 100644 --- a/骚操作.md +++ b/骚操作.md @@ -238,15 +238,33 @@ public static class DbFunc //必要定义 static + ThreadLocal static ThreadLocal context = new ThreadLocal(); - public static string FormatDateTime(this DateTime that, string arg1) + public static DateTime FormatDateTime(this DateTime that, string arg1) { var up = context.Value; - if (up.DataType == FreeSql.DataType.Sqlite) - return $"date_format({up.Values["that"]}, {up.Values["arg1"]})"; - return ""; + if (up.DataType == FreeSql.DataType.Sqlite) //重写内容 + context.Value.Result = $"date_format({up.Values["that"]}, {up.Values["arg1"]})"; + return that; } - //...在此类中定义更多方法 + public static string SetDbParameter(this string that, int size) + { + if (context.Value.DbParameter != null) + { + //已经参数化了,直接修改长度 + //提示:此条件可能开启了全局表达式参数化功能 UseGenerateCommandParameterWithLambda(true) + context.Value.DbParameter.Size = size; + return that; + } + var guid = Guid.NewGuid().ToString("N").ToLower(); + context.Value.UserParameters.Add(new SqlParameter + { + ParameterName = guid, + SqlDbType = System.Data.SqlDbType.VarChar, + Size = size, + Value = that + }); + return $"@{guid}"; //重写内容 + } } var sql = fsql.Select() @@ -255,9 +273,15 @@ var sql = fsql.Select() //FROM "SysModule" a ``` -context.Value.DataType 是 FreeSql.DataType 枚举类型,以便实现不同数据库的适配; +| ExpressionCallContext 属性 | 类型 |描述 | +| -- | -- | -- | +| context.Value.DataType | FreeSql.DataType | 用于实现不同数据库的适配判断条件 | +| context.Value.ParsedContent | Dictionary\ |函数的各参数解析结果 | +| context.Value.DbParameter | System.Data.Common.DbParameter | that 被参数化的对象(有可能为 null) | +| context.Value.UserParameters | List\ | 可附加参数化对象 | +| context.Value.Result | string | 返回表达式函数表示的 SQL 字符串 | -context.Value.Values 是 函数的各参数解析结果; +> 当扩展方法返回值为 string 时,其返回值也可以当作 context.Value.Result 功能 ---