mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 增加 RawValueAttribute 实现自定义表达式时,使用原始值传入参数;
- 增加 IEnumerable<(T1, T2)>.ContainsMany 扩展方法,实现自定义表达式解析多列无法 IN 的问题;
This commit is contained in:
parent
c942811548
commit
011cc8d0d8
@ -110,13 +110,6 @@
|
||||
清空状态数据
|
||||
</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>
|
||||
添加
|
||||
|
@ -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<TestIgnoreDefaultValue>().Where(a => contains2linqarr.ContainsMany(a.Id, a.ct1)).ToSql(a => 1).Replace("\r\n", ""));
|
||||
g.sqlite.Select<TestIgnoreDefaultValue>().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<TestIgnoreDefaultValue>().Where(a => contains2linqarr.ContainsMany(a.Id, a.ct1)).ToList();
|
||||
|
||||
var start = DateTime.Now.Date;
|
||||
var end = DateTime.Now.AddDays(1).Date.AddMilliseconds(-1);
|
||||
|
@ -13,6 +13,13 @@ namespace FreeSql.DataAnnotations
|
||||
public class ExpressionCallAttribute : Attribute
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// 自定义表达式函数解析的时候,指定参数不解析 SQL,而是直接传进来
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Parameter)]
|
||||
public class RawValueAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public class ExpressionCallContext
|
||||
{
|
||||
@ -37,6 +44,11 @@ namespace FreeSql.DataAnnotations
|
||||
/// </summary>
|
||||
public List<DbParameter> UserParameters { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 将 c# 对象转换为 SQL
|
||||
/// </summary>
|
||||
public Func<object, string> FormatSql { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 返回表达式函数表示的 SQL 字符串
|
||||
/// </summary>
|
||||
|
@ -10,6 +10,7 @@ using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
public static partial class FreeSqlGlobalExtensions
|
||||
@ -261,5 +262,84 @@ public static partial class FreeSqlGlobalExtensions
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
|
||||
/// SQL: <para></para>
|
||||
/// exp1 = that[0].Item1 and exp2 = that[0].Item2 OR <para></para>
|
||||
/// exp1 = that[1].Item1 and exp2 = that[1].Item2 OR <para></para>
|
||||
/// ... <para></para>
|
||||
/// 注意:当 that 为 null 或 empty 时,返回 1=0
|
||||
/// </summary>
|
||||
/// <typeparam name="T1"></typeparam>
|
||||
/// <typeparam name="T2"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="exp1"></param>
|
||||
/// <param name="exp2"></param>
|
||||
/// <returns></returns>
|
||||
[ExpressionCall]
|
||||
public static bool ContainsMany<T1, T2>([RawValue] this IEnumerable<(T1, T2)> that, T1 exp1, T2 exp2)
|
||||
{
|
||||
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
|
||||
return that?.Any(a => a.Item1.Equals(exp1) && a.Item2.Equals(exp2)) == true;
|
||||
if (that?.Any() != true)
|
||||
{
|
||||
expContext.Value.Result = "1=0";
|
||||
return false;
|
||||
}
|
||||
var sb = new StringBuilder();
|
||||
var idx = 0;
|
||||
foreach (var item in that)
|
||||
{
|
||||
if (idx++ > 0) sb.Append(" OR \r\n");
|
||||
sb
|
||||
.Append(expContext.Value.ParsedContent["exp1"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T1), item.Item1)))
|
||||
.Append(" AND ")
|
||||
.Append(expContext.Value.ParsedContent["exp2"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T2), item.Item2)));
|
||||
}
|
||||
expContext.Value.Result = sb.ToString();
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
|
||||
/// SQL: <para></para>
|
||||
/// exp1 = that[0].Item1 and exp2 = that[0].Item2 and exp3 = that[0].Item3 OR <para></para>
|
||||
/// exp1 = that[1].Item1 and exp2 = that[1].Item2 and exp3 = that[1].Item3 OR <para></para>
|
||||
/// ... <para></para>
|
||||
/// 注意:当 that 为 null 或 empty 时,返回 1=0
|
||||
/// </summary>
|
||||
/// <typeparam name="T1"></typeparam>
|
||||
/// <typeparam name="T2"></typeparam>
|
||||
/// <typeparam name="T3"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="exp1"></param>
|
||||
/// <param name="exp2"></param>
|
||||
/// <param name="exp3"></param>
|
||||
/// <returns></returns>
|
||||
[ExpressionCall]
|
||||
public static bool ContainsMany<T1, T2, T3>([RawValue] this IEnumerable<(T1, T2, T3)> that, T1 exp1, T2 exp2, T3 exp3)
|
||||
{
|
||||
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
|
||||
return that.Any(a => a.Item1.Equals(exp1) && a.Item2.Equals(exp2) && a.Item3.Equals(exp3));
|
||||
if (that.Any() == false)
|
||||
{
|
||||
expContext.Value.Result = "1=0";
|
||||
return false;
|
||||
}
|
||||
var sb = new StringBuilder();
|
||||
var idx = 0;
|
||||
foreach (var item in that)
|
||||
{
|
||||
if (idx++ > 0) sb.Append(" OR \r\n");
|
||||
sb
|
||||
.Append(expContext.Value.ParsedContent["exp1"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T1), item.Item1)))
|
||||
.Append(" AND ")
|
||||
.Append(expContext.Value.ParsedContent["exp2"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T2), item.Item2)))
|
||||
.Append(" AND ")
|
||||
.Append(expContext.Value.ParsedContent["exp3"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T3), item.Item3)));
|
||||
}
|
||||
expContext.Value.Result = sb.ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
@ -204,6 +204,11 @@
|
||||
注意:请使用静态方法、或者在类上标记
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:FreeSql.DataAnnotations.RawValueAttribute">
|
||||
<summary>
|
||||
自定义表达式函数解析的时候,指定参数不解析 SQL,而是直接传进来
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.DataAnnotations.ExpressionCallContext.DataType">
|
||||
<summary>
|
||||
数据库类型,可用于适配多种数据库环境
|
||||
@ -225,6 +230,11 @@
|
||||
注意:本属性只有 Where 的表达式解析才可用
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.DataAnnotations.ExpressionCallContext.FormatSql">
|
||||
<summary>
|
||||
将 c# 对象转换为 SQL
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.DataAnnotations.ExpressionCallContext.Result">
|
||||
<summary>
|
||||
返回表达式函数表示的 SQL 字符串
|
||||
@ -2784,6 +2794,40 @@
|
||||
<param name="end"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlGlobalExtensions.ContainsMany``2(System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1}},``0,``1)">
|
||||
<summary>
|
||||
C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
|
||||
SQL: <para></para>
|
||||
exp1 = that[0].Item1 and exp2 = that[0].Item2 OR <para></para>
|
||||
exp1 = that[1].Item1 and exp2 = that[1].Item2 OR <para></para>
|
||||
... <para></para>
|
||||
注意:当 that 为 null 或 empty 时,返回 1=0
|
||||
</summary>
|
||||
<typeparam name="T1"></typeparam>
|
||||
<typeparam name="T2"></typeparam>
|
||||
<param name="that"></param>
|
||||
<param name="exp1"></param>
|
||||
<param name="exp2"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlGlobalExtensions.ContainsMany``3(System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1,``2}},``0,``1,``2)">
|
||||
<summary>
|
||||
C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
|
||||
SQL: <para></para>
|
||||
exp1 = that[0].Item1 and exp2 = that[0].Item2 and exp3 = that[0].Item3 OR <para></para>
|
||||
exp1 = that[1].Item1 and exp2 = that[1].Item2 and exp3 = that[1].Item3 OR <para></para>
|
||||
... <para></para>
|
||||
注意:当 that 为 null 或 empty 时,返回 1=0
|
||||
</summary>
|
||||
<typeparam name="T1"></typeparam>
|
||||
<typeparam name="T2"></typeparam>
|
||||
<typeparam name="T3"></typeparam>
|
||||
<param name="that"></param>
|
||||
<param name="exp1"></param>
|
||||
<param name="exp2"></param>
|
||||
<param name="exp3"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:System.Linq.Expressions.LambadaExpressionExtensions.And``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
||||
<summary>
|
||||
使用 and 拼接两个 lambda 表达式
|
||||
|
@ -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<DbParameter>() };
|
||||
var ecc = new ExpressionCallContext { DataType = _ado.DataType, UserParameters = tsc.dbParams == null ? null : new List<DbParameter>(), 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<DbParameter> 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user