mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-03 20:27:56 +08:00
Add SqlFunc.ListAny
This commit is contained in:
parent
d8345b5fce
commit
88912d6d03
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class ListAnyParameter
|
||||
{
|
||||
public string Name { get; internal set; }
|
||||
public string Sql { get; internal set; }
|
||||
public List<EntityColumnInfo> Columns { get; internal set; }
|
||||
public Func<string,string> ConvetColumnFunc { get; internal set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class ParameterExpressionVisitor : ExpressionVisitor
|
||||
{
|
||||
public List<ParameterExpression> Parameters { get; } = new List<ParameterExpression>();
|
||||
|
||||
protected override Expression VisitParameter(ParameterExpression node)
|
||||
{
|
||||
Parameters.Add(node);
|
||||
return base.VisitParameter(node);
|
||||
}
|
||||
}
|
||||
}
|
@ -732,6 +732,81 @@ namespace SqlSugar
|
||||
});
|
||||
return $" ({likeString1} or {likeString2} or {likeString3} or {fullString}={value} ) ";
|
||||
}
|
||||
|
||||
|
||||
public string ListAny(MethodCallExpressionModel model)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (model.Args[0].MemberValue!=null&&(model.Args[0].MemberValue as IList).Count>0)
|
||||
{
|
||||
sb.Append(" ( ");
|
||||
var listPar = model.Args[1].MemberValue as ListAnyParameter;
|
||||
foreach (var item in (model.Args[0].MemberValue as IList))
|
||||
{
|
||||
foreach (var columnInfo in listPar.Columns)
|
||||
{
|
||||
var replace = listPar.ConvetColumnFunc($"{listPar.Name}.{columnInfo.DbColumnName}");
|
||||
if (listPar.Sql.Contains(replace))
|
||||
{
|
||||
if (sb.Length>3)
|
||||
{
|
||||
sb.Append("OR");
|
||||
}
|
||||
var value = columnInfo.PropertyInfo.GetValue(item);
|
||||
var newValue = "null";
|
||||
if (value != null)
|
||||
{
|
||||
if (UtilMethods.IsNumber(columnInfo.UnderType.Name))
|
||||
{
|
||||
newValue = value.ToString();
|
||||
}
|
||||
else if(columnInfo.UnderType==SqlSugar.UtilConstants.GuidType)
|
||||
{
|
||||
newValue = ToGuid(new MethodCallExpressionModel()
|
||||
{
|
||||
Args=new List<MethodCallExpressionArgs>()
|
||||
{
|
||||
new MethodCallExpressionArgs(){
|
||||
MemberValue=value.ToSqlValue(),
|
||||
MemberName=value.ToSqlValue()
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (columnInfo.UnderType == SqlSugar.UtilConstants.DateType)
|
||||
{
|
||||
newValue = ToDate(new MethodCallExpressionModel()
|
||||
{
|
||||
Args = new List<MethodCallExpressionArgs>()
|
||||
{
|
||||
new MethodCallExpressionArgs(){
|
||||
MemberValue=UtilMethods.GetConvertValue( value).ToSqlValue(),
|
||||
MemberName=UtilMethods.GetConvertValue( value).ToSqlValue()
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
newValue = value.ToSqlValue();
|
||||
}
|
||||
}
|
||||
sb.Append(listPar.Sql.Replace(replace, newValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.Append(" ) ");
|
||||
}
|
||||
var result = sb.ToString();
|
||||
if (result.IsNullOrEmpty())
|
||||
{
|
||||
return " 1=2 ";
|
||||
}
|
||||
else
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public virtual string GetTableWithDataBase(string dataBaseName,string tableName)
|
||||
{
|
||||
return $"{dataBaseName}.{tableName}";
|
||||
|
@ -104,6 +104,7 @@ namespace SqlSugar
|
||||
string JsonArrayAny(MethodCallExpressionModel model);
|
||||
string CompareTo(MethodCallExpressionModel model);
|
||||
string SplitIn(MethodCallExpressionModel model);
|
||||
string ListAny(MethodCallExpressionModel model);
|
||||
string GetTableWithDataBase(string databaseName,string tableName);
|
||||
}
|
||||
}
|
||||
|
@ -284,5 +284,10 @@ namespace SqlSugar
|
||||
{
|
||||
throw new NotSupportedException("Can only be used in expressions");
|
||||
}
|
||||
|
||||
public static bool ListAny<T>(List<T> listConstant, Expression<Func<T,bool>> expression)
|
||||
{
|
||||
throw new NotSupportedException("Can only be used in expressions");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -252,6 +252,31 @@ namespace SqlSugar
|
||||
}
|
||||
model.Args.Add(argItem);
|
||||
}
|
||||
else if (name == "ListAny"&& item is LambdaExpression)
|
||||
{
|
||||
var sql =GetNewExpressionValue(item,ResolveExpressType.WhereMultiple);
|
||||
var lamExp = (item as LambdaExpression);
|
||||
var pExp = lamExp.Parameters[0];
|
||||
var pname = pExp.Name;
|
||||
model.Args.Add(new MethodCallExpressionArgs() {
|
||||
MemberValue=new ListAnyParameter() {
|
||||
Sql= sql,
|
||||
Name=pname,
|
||||
Columns=this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(pExp.Type).Columns,
|
||||
ConvetColumnFunc = this.Context.GetTranslationColumnName
|
||||
}
|
||||
});
|
||||
if (this.Context.IsSingle && this.Context.SingleTableNameSubqueryShortName == null)
|
||||
{
|
||||
ParameterExpressionVisitor visitor = new ParameterExpressionVisitor();
|
||||
visitor.Visit(lamExp);
|
||||
var tableParamter=visitor.Parameters.FirstOrDefault(it => it.Name != pname);
|
||||
if (tableParamter != null)
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = tableParamter.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendModel(parameter, model, item);
|
||||
@ -792,6 +817,9 @@ namespace SqlSugar
|
||||
return this.Context.DbMehtods.CompareTo(model);
|
||||
case "SplitIn":
|
||||
return this.Context.DbMehtods.SplitIn(model);
|
||||
case "ListAny":
|
||||
this.Context.Parameters.RemoveAll(it => model.Args[0].MemberName.ObjToString().Contains(it.ParameterName));
|
||||
return this.Context.DbMehtods.ListAny(model);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -149,6 +149,8 @@
|
||||
<Compile Include="Entities\DefaultCustom.cs" />
|
||||
<Compile Include="Entities\DeleteNavOptions.cs" />
|
||||
<Compile Include="Entities\JoinInfoParameter.cs" />
|
||||
<Compile Include="ExpressionsToSql\Common\ParameterExpressionVisitor.cs" />
|
||||
<Compile Include="ExpressionsToSql\Common\ListAnyParameter.cs" />
|
||||
<Compile Include="ExpressionsToSql\Common\MapperSql.cs" />
|
||||
<Compile Include="ExpressionsToSql\Common\NewExpressionInfo.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\BaseResolve_Append.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user