- 增加 RawValueAttribute 实现自定义表达式时,使用原始值传入参数;

- 增加 IEnumerable<(T1, T2)>.ContainsMany 扩展方法,实现自定义表达式解析多列无法 IN 的问题;
This commit is contained in:
28810
2019-12-08 00:03:35 +08:00
parent c942811548
commit 011cc8d0d8
6 changed files with 154 additions and 14 deletions

View File

@ -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
}