From 011cc8d0d8475606fda8c856a20cc4715e6cae8a Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Sun, 8 Dec 2019 00:03:35 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20RawValueAttribute=20?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A1=A8=E8=BE=BE?=
=?UTF-8?q?=E5=BC=8F=E6=97=B6=EF=BC=8C=E4=BD=BF=E7=94=A8=E5=8E=9F=E5=A7=8B?=
=?UTF-8?q?=E5=80=BC=E4=BC=A0=E5=85=A5=E5=8F=82=E6=95=B0=EF=BC=9B=20-=20?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=20IEnumerable<(T1,=20T2)>.ContainsMany=20?=
=?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95=EF=BC=8C=E5=AE=9E=E7=8E=B0?=
=?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=A7=A3?=
=?UTF-8?q?=E6=9E=90=E5=A4=9A=E5=88=97=E6=97=A0=E6=B3=95=20IN=20=E7=9A=84?=
=?UTF-8?q?=E9=97=AE=E9=A2=98=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/UnitTest2.cs | 8 ++
.../ExpressionCallAttribute.cs | 12 +++
FreeSql/Extensions/FreeSqlGlobalExtensions.cs | 80 +++++++++++++++++++
FreeSql/FreeSql.xml | 44 ++++++++++
FreeSql/Internal/CommonExpression.cs | 17 ++--
6 files changed, 154 insertions(+), 14 deletions(-)
diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml
index d9f91124..dc0203b8 100644
--- a/FreeSql.DbContext/FreeSql.DbContext.xml
+++ b/FreeSql.DbContext/FreeSql.DbContext.xml
@@ -110,13 +110,6 @@
清空状态数据
-
-
- 根据 lambda 条件删除数据
-
-
-
-
添加
diff --git a/FreeSql.Tests/FreeSql.Tests/UnitTest2.cs b/FreeSql.Tests/FreeSql.Tests/UnitTest2.cs
index 50e45747..feb4a6c3 100644
--- a/FreeSql.Tests/FreeSql.Tests/UnitTest2.cs
+++ b/FreeSql.Tests/FreeSql.Tests/UnitTest2.cs
@@ -252,6 +252,14 @@ namespace FreeSql.Tests
}
});
+ List<(Guid, DateTime)> contains2linqarr = new List<(Guid, DateTime)>();
+ Assert.Equal("SELECT 1 as1 FROM \"TestIgnoreDefaultValue\" a WHERE (1=0)", g.sqlite.Select().Where(a => contains2linqarr.ContainsMany(a.Id, a.ct1)).ToSql(a => 1).Replace("\r\n", ""));
+ g.sqlite.Select().Where(a => contains2linqarr.ContainsMany(a.Id, a.ct1)).ToList();
+
+ contains2linqarr.Add((Guid.NewGuid(), DateTime.Now));
+ contains2linqarr.Add((Guid.NewGuid(), DateTime.Now));
+ contains2linqarr.Add((Guid.NewGuid(), DateTime.Now));
+ g.sqlite.Select().Where(a => contains2linqarr.ContainsMany(a.Id, a.ct1)).ToList();
var start = DateTime.Now.Date;
var end = DateTime.Now.AddDays(1).Date.AddMilliseconds(-1);
diff --git a/FreeSql/DataAnnotations/ExpressionCallAttribute.cs b/FreeSql/DataAnnotations/ExpressionCallAttribute.cs
index c9c964e9..4342ff13 100644
--- a/FreeSql/DataAnnotations/ExpressionCallAttribute.cs
+++ b/FreeSql/DataAnnotations/ExpressionCallAttribute.cs
@@ -13,6 +13,13 @@ namespace FreeSql.DataAnnotations
public class ExpressionCallAttribute : Attribute
{
}
+ ///
+ /// 自定义表达式函数解析的时候,指定参数不解析 SQL,而是直接传进来
+ ///
+ [AttributeUsage(AttributeTargets.Parameter)]
+ public class RawValueAttribute : Attribute
+ {
+ }
public class ExpressionCallContext
{
@@ -37,6 +44,11 @@ namespace FreeSql.DataAnnotations
///
public List UserParameters { get; internal set; }
+ ///
+ /// 将 c# 对象转换为 SQL
+ ///
+ public Func
+
+
+ 自定义表达式函数解析的时候,指定参数不解析 SQL,而是直接传进来
+
+
数据库类型,可用于适配多种数据库环境
@@ -225,6 +230,11 @@
注意:本属性只有 Where 的表达式解析才可用
+
+
+ 将 c# 对象转换为 SQL
+
+
返回表达式函数表示的 SQL 字符串
@@ -2784,6 +2794,40 @@
+
+
+ C#:从元组集合中查找 exp1, exp2 是否存在
+ SQL:
+ exp1 = that[0].Item1 and exp2 = that[0].Item2 OR
+ exp1 = that[1].Item1 and exp2 = that[1].Item2 OR
+ ...
+ 注意:当 that 为 null 或 empty 时,返回 1=0
+
+
+
+
+
+
+
+
+
+
+ C#:从元组集合中查找 exp1, exp2 是否存在
+ SQL:
+ exp1 = that[0].Item1 and exp2 = that[0].Item2 and exp3 = that[0].Item3 OR
+ exp1 = that[1].Item1 and exp2 = that[1].Item2 and exp3 = that[1].Item3 OR
+ ...
+ 注意:当 that 为 null 或 empty 时,返回 1=0
+
+
+
+
+
+
+
+
+
+
使用 and 拼接两个 lambda 表达式
diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs
index 23156372..b8fdfa9c 100644
--- a/FreeSql/Internal/CommonExpression.cs
+++ b/FreeSql/Internal/CommonExpression.cs
@@ -553,15 +553,15 @@ namespace FreeSql.Internal
exp3.Method.GetCustomAttributes(typeof(ExpressionCallAttribute), true).Any()
))
{
- var ecc = new ExpressionCallContext { DataType = _ado.DataType, UserParameters = tsc.dbParams == null ? null : new List() };
+ var ecc = new ExpressionCallContext { DataType = _ado.DataType, UserParameters = tsc.dbParams == null ? null : new List(), FormatSql = obj => formatSql(obj, null, null, null) };
var exp3MethodParams = exp3.Method.GetParameters();
var dbParamsIndex = tsc.dbParams?.Count;
- ecc.ParsedContent.Add(exp3MethodParams[0].Name, ExpressionLambdaToSql(exp3.Arguments[0], tsc));
+ ecc.ParsedContent.Add(exp3MethodParams[0].Name, exp3MethodParams[0].GetCustomAttributes(typeof(RawValueAttribute), true).Any() ? null: ExpressionLambdaToSql(exp3.Arguments[0], tsc));
if (tsc.dbParams?.Count > dbParamsIndex) ecc.DbParameter = tsc.dbParams.Last();
List oldDbParams = tsc.SetDbParamsReturnOld(null);
for (var a = 1; a < exp3.Arguments.Count; a++)
if (exp3.Arguments[a].Type != typeof(ExpressionCallContext))
- ecc.ParsedContent.Add(exp3MethodParams[a].Name, ExpressionLambdaToSql(exp3.Arguments[a], tsc));
+ ecc.ParsedContent.Add(exp3MethodParams[a].Name, exp3MethodParams[a].GetCustomAttributes(typeof(RawValueAttribute), true).Any() ? null : ExpressionLambdaToSql(exp3.Arguments[a], tsc));
tsc.SetDbParamsReturnOld(oldDbParams);
var exp3InvokeParams = new object[exp3.Arguments.Count];
@@ -570,10 +570,13 @@ namespace FreeSql.Internal
if (exp3.Arguments[a].Type != typeof(ExpressionCallContext))
{
var eccContent = ecc.ParsedContent[exp3MethodParams[a].Name];
- exp3InvokeParams[a] = Utils.GetDataReaderValue(exp3.Arguments[a].Type,
- eccContent.StartsWith("N'") ?
- eccContent.Substring(1).Trim('\'').Replace("''", "'") :
- eccContent.Trim('\'').Replace("''", "'"));// exp3.Arguments[a].Type.CreateInstanceGetDefaultValue();
+ if (eccContent == null)
+ exp3InvokeParams[a] = Expression.Lambda(exp3.Arguments[a]).Compile().DynamicInvoke();
+ else
+ exp3InvokeParams[a] = Utils.GetDataReaderValue(exp3.Arguments[a].Type,
+ eccContent.StartsWith("N'") ?
+ eccContent.Substring(1).Trim('\'').Replace("''", "'") :
+ eccContent.Trim('\'').Replace("''", "'"));// exp3.Arguments[a].Type.CreateInstanceGetDefaultValue();
}
else
exp3InvokeParams[a] = ecc;