update

28810
2019-11-23 01:49:27 +08:00
parent 1497772f3c
commit c224cb6727
4 changed files with 65 additions and 17 deletions

2
AOP.md

@@ -90,7 +90,7 @@ fsql.Aop.ConfigEntityProperty = (s, e) => {
就这样FreeSql 的实体特性就可以和 EFCore 那样设定了。其他自增、乐观锁等,依葫芦画瓢便是。 就这样FreeSql 的实体特性就可以和 EFCore 那样设定了。其他自增、乐观锁等,依葫芦画瓢便是。
## 自定义表达式 ## 表达式拦截
FreeSql 内部表达式支持非常丰富,对各大数据库的兼容度也做得很好。 FreeSql 内部表达式支持非常丰富,对各大数据库的兼容度也做得很好。

@@ -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) - 增加 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) 开启 > FreeSqlBuilder 上使用 UseGenerateCommandParameterWithLambda(true) 开启
- 增加 ExpressionCallContext 自定义函数上下文档 DbParameter 属性 - 优化 ExpressionCallContext 可设置、附加参数化对象
- 修复 IncludeMany(a => a.x1.x2.Childs) 当 x1, x2 为 null 的报 null 错误; - 修复 IncludeMany(a => a.x1.x2.Childs) 当 x1, x2 为 null 的报 null 错误;
## v0.11.24 ## v0.11.24

@@ -31,15 +31,33 @@ public static class DbFunc
//必要定义 static + ThreadLocal //必要定义 static + ThreadLocal
static ThreadLocal<ExpressionCallContext> context = new ThreadLocal<ExpressionCallContext>(); static ThreadLocal<ExpressionCallContext> context = new ThreadLocal<ExpressionCallContext>();
public static string FormatDateTime(this DateTime that, string arg1) public static DateTime FormatDateTime(this DateTime that, string arg1)
{ {
var up = context.Value; var up = context.Value;
if (up.DataType == FreeSql.DataType.Sqlite) if (up.DataType == FreeSql.DataType.Sqlite) //重写内容
return $"date_format({up.Values["that"]}, {up.Values["arg1"]})"; context.Value.Result = $"date_format({up.Values["that"]}, {up.Values["arg1"]})";
return ""; 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<SysModule>() var sql = fsql.Select<SysModule>()
@@ -48,9 +66,15 @@ var sql = fsql.Select<SysModule>()
//FROM "SysModule" a //FROM "SysModule" a
``` ```
context.Value.DataType 是 FreeSql.DataType 枚举类型,以便实现不同数据库的适配; | ExpressionCallContext 属性 | 类型 |描述 |
| -- | -- | -- |
| context.Value.DataType | FreeSql.DataType | 用于实现不同数据库的适配判断条件 |
| context.Value.ParsedContent | Dictionary\<string, string\> |函数的各参数解析结果 |
| context.Value.DbParameter | System.Data.Common.DbParameter | that 被参数化的对象(有可能为 null) |
| context.Value.UserParameters | List\<System.Data.Common.DbParameter\> | 可附加参数化对象 |
| context.Value.Result | string | 返回表达式函数表示的 SQL 字符串 |
context.Value.Values 是 函数的各参数解析结果; > 当扩展方法返回值为 string 时,其返回值也可以当作 context.Value.Result 功能
## 命令参数化(v0.12.1) ## 命令参数化(v0.12.1)
@@ -97,7 +121,7 @@ public static class DbFunc
{ {
if (context.Value.DbParameter != null) if (context.Value.DbParameter != null)
context.Value.DbParameter.Size = size; context.Value.DbParameter.Size = size;
return context.Value.Values["that"]; return context.Value.ParsedContent["that"];
} }
} }
``` ```

@@ -238,15 +238,33 @@ public static class DbFunc
//必要定义 static + ThreadLocal //必要定义 static + ThreadLocal
static ThreadLocal<ExpressionCallContext> context = new ThreadLocal<ExpressionCallContext>(); static ThreadLocal<ExpressionCallContext> context = new ThreadLocal<ExpressionCallContext>();
public static string FormatDateTime(this DateTime that, string arg1) public static DateTime FormatDateTime(this DateTime that, string arg1)
{ {
var up = context.Value; var up = context.Value;
if (up.DataType == FreeSql.DataType.Sqlite) if (up.DataType == FreeSql.DataType.Sqlite) //重写内容
return $"date_format({up.Values["that"]}, {up.Values["arg1"]})"; context.Value.Result = $"date_format({up.Values["that"]}, {up.Values["arg1"]})";
return ""; 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<SysModule>() var sql = fsql.Select<SysModule>()
@@ -255,9 +273,15 @@ var sql = fsql.Select<SysModule>()
//FROM "SysModule" a //FROM "SysModule" a
``` ```
context.Value.DataType 是 FreeSql.DataType 枚举类型,以便实现不同数据库的适配; | ExpressionCallContext 属性 | 类型 |描述 |
| -- | -- | -- |
| context.Value.DataType | FreeSql.DataType | 用于实现不同数据库的适配判断条件 |
| context.Value.ParsedContent | Dictionary\<string, string\> |函数的各参数解析结果 |
| context.Value.DbParameter | System.Data.Common.DbParameter | that 被参数化的对象(有可能为 null) |
| context.Value.UserParameters | List\<System.Data.Common.DbParameter\> | 可附加参数化对象 |
| context.Value.Result | string | 返回表达式函数表示的 SQL 字符串 |
context.Value.Values 是 函数的各参数解析结果; > 当扩展方法返回值为 string 时,其返回值也可以当作 context.Value.Result 功能
--- ---