mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-03 04:13:48 +08:00
-
This commit is contained in:
parent
24b53f7435
commit
11b4ebc77e
Binary file not shown.
@ -15,10 +15,10 @@ namespace OrmTest
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 0;
|
||||
Expression<Func<Student, bool>> exp = it => it.Name!="";
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.Single);
|
||||
// var x = expContext.GetFiledName();
|
||||
var xx = expContext.GetWhere();
|
||||
var xx = expContext.ToString();
|
||||
|
||||
|
||||
var b = DateTime.Now;
|
||||
@ -26,7 +26,7 @@ namespace OrmTest
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest",
|
||||
EntityNamespace = "SqlSugar4XTest.Models",
|
||||
EntityNamespace = "OrmTest.Models",
|
||||
IsAutoCloseConnection = false
|
||||
};
|
||||
SqlSugarClient db = new SqlSugarClient(config);
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MemberExpressionResolve:BaseResolve
|
||||
{
|
||||
public MemberExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
var isLeft = this.IsLeft;
|
||||
this.IsLeft = null;
|
||||
var isSingle=base.Context.IsSingle;
|
||||
var expression = exp as MemberExpression;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ExpressionContext : ExpResolveAccessory
|
||||
{
|
||||
public ResolveExpressType Type { get; set; }
|
||||
public Expression Expression { get; set; }
|
||||
public bool IsSingle { get { return this.Type == ResolveExpressType.Single; } }
|
||||
|
||||
public ExpressionContext(Expression expression, ResolveExpressType type)
|
||||
{
|
||||
this.Type = type;
|
||||
this.Expression = expression;
|
||||
}
|
||||
|
||||
public string GetFiledName()
|
||||
{
|
||||
string reval = null;
|
||||
LambdaExpression lambda = this.Expression as LambdaExpression;
|
||||
var isConvet = lambda.Body.NodeType.IsIn(ExpressionType.Convert);
|
||||
var isMember = lambda.Body.NodeType.IsIn(ExpressionType.MemberAccess);
|
||||
if (!isConvet && !isMember)
|
||||
throw new SqlSugarException(ErrorMessage.ExpFileldError);
|
||||
try
|
||||
{
|
||||
if (isConvet && IsSingle)
|
||||
{
|
||||
var memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
|
||||
reval = memberExpr.Member.Name;
|
||||
}
|
||||
else if (!isConvet && IsSingle)//isMember
|
||||
{
|
||||
reval = (lambda.Body as MemberExpression).Member.Name;
|
||||
}
|
||||
else if (isConvet && !IsSingle)
|
||||
{
|
||||
var memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
|
||||
reval = memberExpr.ToString();
|
||||
}
|
||||
else if (!isConvet && !IsSingle)
|
||||
{
|
||||
reval = lambda.Body.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new SqlSugarException(ErrorMessage.ExpFileldError);
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
|
||||
public string GetWhere()
|
||||
{
|
||||
BaseResolve resolve = new BaseResolve(this.Expression);
|
||||
resolve.Context = this;
|
||||
resolve.Start();
|
||||
return resolve.SqlWhere;
|
||||
}
|
||||
|
||||
public string GetSelect() { return ""; }
|
||||
|
||||
public List<SqlParameter> Parameters
|
||||
{
|
||||
get
|
||||
{
|
||||
return PubMethod.IsNullReturnNew(base._Parameters);
|
||||
}
|
||||
set
|
||||
{
|
||||
base._Parameters = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int Index { get; set; }
|
||||
}
|
||||
}
|
@ -8,7 +8,6 @@ namespace SqlSugar
|
||||
public class BaseResolve
|
||||
{
|
||||
protected Expression Expression { get; set; }
|
||||
protected Expression BaseExpression { get; set; }
|
||||
public ExpressionContext Context { get; set; }
|
||||
public string SqlWhere { get; set; }
|
||||
public bool IsFinished { get; set; }
|
||||
@ -19,24 +18,31 @@ namespace SqlSugar
|
||||
{
|
||||
|
||||
}
|
||||
public BaseResolve(Expression expression)
|
||||
public BaseResolve(ExpressionParameter parameter)
|
||||
{
|
||||
this.Expression = expression;
|
||||
this.Expression = parameter.Expression;
|
||||
this.Context = parameter.Context;
|
||||
}
|
||||
|
||||
public BaseResolve Start()
|
||||
{
|
||||
this.Index++;
|
||||
this.IsFinished = false;
|
||||
this.BaseExpression = null;
|
||||
Expression exp = this.Expression;
|
||||
ExpressionParameter parameter = new ExpressionParameter()
|
||||
{
|
||||
Context = this.Context,
|
||||
Expression = exp,
|
||||
IsLeft = this.IsLeft,
|
||||
BaseExpression=this.Expression
|
||||
};
|
||||
if (exp is LambdaExpression)
|
||||
{
|
||||
return new LambdaExpressionResolve(exp);
|
||||
return new LambdaExpressionResolve(parameter);
|
||||
}
|
||||
else if (exp is BinaryExpression)
|
||||
{
|
||||
return new BinaryExpressionResolve(exp);
|
||||
return new BinaryExpressionResolve(parameter);
|
||||
}
|
||||
else if (exp is BlockExpression)
|
||||
{
|
||||
@ -48,19 +54,19 @@ namespace SqlSugar
|
||||
}
|
||||
else if (exp is MethodCallExpression)
|
||||
{
|
||||
return new MethodCallExpressionResolve(exp);
|
||||
return new MethodCallExpressionResolve(parameter);
|
||||
}
|
||||
else if (exp is ConstantExpression)
|
||||
{
|
||||
return new ConstantExpressionResolve(exp);
|
||||
return new ConstantExpressionResolve(parameter);
|
||||
}
|
||||
else if (exp is MemberExpression)
|
||||
{
|
||||
return new MemberExpressionResolve(exp);
|
||||
return new MemberExpressionResolve(parameter);
|
||||
}
|
||||
else if (exp is UnaryExpression)
|
||||
{
|
||||
return new UnaryExpressionResolve(exp);
|
||||
return new UnaryExpressionResolve(parameter);
|
||||
}
|
||||
else if (exp != null && exp.NodeType.IsIn(ExpressionType.New, ExpressionType.NewArrayBounds, ExpressionType.NewArrayInit))
|
||||
{
|
@ -7,10 +7,10 @@ namespace SqlSugar
|
||||
{
|
||||
public class BinaryExpressionResolve : BaseResolve
|
||||
{
|
||||
public BinaryExpressionResolve(Expression exp) : base(exp)
|
||||
public BinaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
var expression = this.Expression as BinaryExpression;
|
||||
base.BaseExpression = expression;
|
||||
var baseExpression = parameter.Expression;
|
||||
base.IsLeft = true;
|
||||
base.Expression = expression.Left;
|
||||
base.Start();
|
@ -7,7 +7,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class BlockExpressionResolve:BaseResolve
|
||||
{
|
||||
public BlockExpressionResolve(Expression exp):base(exp)
|
||||
public BlockExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class ConditionalExpressionResolve:BaseResolve
|
||||
{
|
||||
public ConditionalExpressionResolve(Expression exp) : base(exp)
|
||||
public ConditionalExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class ConstantExpressionResolve:BaseResolve
|
||||
{
|
||||
public ConstantExpressionResolve(Expression exp) : base(exp)
|
||||
public ConstantExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
|
||||
}
|
@ -7,9 +7,9 @@ namespace SqlSugar
|
||||
{
|
||||
public class LambdaExpressionResolve : BaseResolve
|
||||
{
|
||||
public LambdaExpressionResolve(Expression exp) : base(exp)
|
||||
public LambdaExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
LambdaExpression lambda = exp as LambdaExpression;
|
||||
LambdaExpression lambda =base.Expression as LambdaExpression;
|
||||
var expression = lambda.Body;
|
||||
if (expression.NodeType == ExpressionType.MemberAccess)
|
||||
{
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MemberExpressionResolve : BaseResolve
|
||||
{
|
||||
public MemberExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
var expression = base.Expression as MemberExpression;
|
||||
var isLeft = this.IsLeft;
|
||||
this.IsLeft = null;
|
||||
var isSingle = base.Context.IsSingle;
|
||||
string field = string.Empty;
|
||||
field = isSingle ? expression.Member.Name : expression.Member.ToString();
|
||||
base.IsFinished = true;
|
||||
if (IsLeft == true)
|
||||
{
|
||||
base.SqlWhere += ExpTool.GetOperator(parameter.BaseExpression.NodeType);
|
||||
}
|
||||
else if (isLeft == false)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class MethodCallExpressionResolve:BaseResolve
|
||||
{
|
||||
public MethodCallExpressionResolve(Expression exp) : base(exp)
|
||||
public MethodCallExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class UnaryExpressionResolve:BaseResolve
|
||||
{
|
||||
public UnaryExpressionResolve(Expression exp) : base(exp)
|
||||
public UnaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -7,6 +8,6 @@ namespace SqlSugar
|
||||
{
|
||||
public class ExpResolveAccessory
|
||||
{
|
||||
protected List<SqlParameter> _Parameters { get; set; }
|
||||
protected List<DbParameter> _Parameters { get; set; }
|
||||
}
|
||||
}
|
45
SqlSugar/ExpressionsToSql/ExpressionContext.cs
Normal file
45
SqlSugar/ExpressionsToSql/ExpressionContext.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ExpressionContext : ExpResolveAccessory
|
||||
{
|
||||
public ResolveExpressType Type { get; set; }
|
||||
public Expression Expression { get; set; }
|
||||
public bool IsSingle { get { return this.Type == ResolveExpressType.Single; } }
|
||||
|
||||
public ExpressionContext(Expression expression, ResolveExpressType type)
|
||||
{
|
||||
this.Type = type;
|
||||
this.Expression = expression;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
BaseResolve resolve = new BaseResolve(new ExpressionParameter() { Expression=this.Expression, Context=this });
|
||||
resolve.Start();
|
||||
return resolve.SqlWhere;
|
||||
}
|
||||
|
||||
public string GetSelect() { return ""; }
|
||||
|
||||
public List<DbParameter> Parameters
|
||||
{
|
||||
get
|
||||
{
|
||||
return PubMethod.IsNullReturnNew(base._Parameters);
|
||||
}
|
||||
set
|
||||
{
|
||||
base._Parameters = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int Index { get; set; }
|
||||
}
|
||||
}
|
@ -9,6 +9,8 @@ namespace SqlSugar
|
||||
public class ExpressionParameter
|
||||
{
|
||||
public Expression Expression { get; set; }
|
||||
public SqlSugarClient DbContext { get; set; }
|
||||
public ExpressionContext Context { get; set; }
|
||||
public bool? IsLeft { get; set; }
|
||||
public Expression BaseExpression { get; set; }
|
||||
}
|
||||
}
|
@ -97,7 +97,7 @@
|
||||
<Compile Include="Entities\ConnectionConfig.cs" />
|
||||
<Compile Include="Entities\DbColumnInfo.cs" />
|
||||
<Compile Include="Entities\DbTableInfo.cs" />
|
||||
<Compile Include="Entities\ExpressionParameter.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpressionParameter.cs" />
|
||||
<Compile Include="Entities\JoinQueryInfo.cs" />
|
||||
<Compile Include="Entities\Mapping\MappingColumn.cs" />
|
||||
<Compile Include="Entities\Mapping\MappingTable.cs" />
|
||||
@ -106,21 +106,21 @@
|
||||
<Compile Include="Enum\ApplyType.cs" />
|
||||
<Compile Include="Enum\JoinType.cs" />
|
||||
<Compile Include="Enum\OrderByType.cs" />
|
||||
<Compile Include="Enum\ResolveExpressType.cs" />
|
||||
<Compile Include="Expressions\ExpConst.cs" />
|
||||
<Compile Include="Expressions\ExpInfo.cs" />
|
||||
<Compile Include="Expressions\ExpItems\BaseResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\BinaryExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\BlockExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\ConditionalExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\ConstantExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\LambdaExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\MemberExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\MethodCallExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpItems\UnaryExpressionResolve.cs" />
|
||||
<Compile Include="Expressions\ExpResolveAccessory.cs" />
|
||||
<Compile Include="Expressions\ExpressionContext.cs" />
|
||||
<Compile Include="Expressions\ExpTool.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveExpressType.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpConst.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpInfo.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\BaseResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\BinaryExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\BlockExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\ConditionalExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\ConstantExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\LambdaExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\MemberExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\MethodCallExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpItems\UnaryExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpResolveAccessory.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpressionContext.cs" />
|
||||
<Compile Include="ExpressionsToSql\ExpTool.cs" />
|
||||
<Compile Include="InstanceFactory.cs" />
|
||||
<Compile Include="Interface\ICodeFirst.cs" />
|
||||
<Compile Include="Interface\IDb.cs" />
|
||||
|
Binary file not shown.
Binary file not shown.
@ -3,4 +3,3 @@ F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\SqlSugar\bin\Debug\SqlSugar.pdb
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\SqlSugar\bin\Debug\Newtonsoft.Json.dll
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\SqlSugar\obj\Debug\SqlSugar.dll
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\SqlSugar\obj\Debug\SqlSugar.pdb
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\SqlSugar\obj\Debug\SqlSugar.csprojResolveAssemblyReference.cache
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user