mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 04:35:29 +08:00
-
This commit is contained in:
parent
837610ff53
commit
3805d5291f
Binary file not shown.
@ -9,26 +9,38 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.ExpressionTest
|
||||
{
|
||||
public class Field
|
||||
public class Field:ExpTestBase
|
||||
{
|
||||
internal static void Init()
|
||||
private Field() { }
|
||||
public Field(int eachCount)
|
||||
{
|
||||
FieldSingle();
|
||||
FieldMultiple();
|
||||
this.Count = eachCount;
|
||||
}
|
||||
private static void FieldSingle()
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
FieldSingle();
|
||||
FieldMultiple();
|
||||
}
|
||||
base.End("Filed Test");
|
||||
}
|
||||
private void FieldSingle()
|
||||
{
|
||||
Expression<Func<Student, object>> exp = it => it.Name;
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.FieldSingle);
|
||||
expContext.Resolve();
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
Check(selectorValue, null, "Name", null, "FieldSingle");
|
||||
}
|
||||
private static void FieldMultiple()
|
||||
private void FieldMultiple()
|
||||
{
|
||||
Expression<Func<Student, object>> exp = it => it.Name;
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.FieldMultiple);
|
||||
expContext.Resolve();
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
Check(selectorValue, null, "it.Name", null, "FieldMultiple");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,80 @@
|
||||
using System;
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.ExpressionTest
|
||||
{
|
||||
public class Where
|
||||
public class Where : ExpTestBase
|
||||
{
|
||||
private Where() { }
|
||||
public Where(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
whereSingle1();
|
||||
whereSingle2();
|
||||
whereSingle3();
|
||||
whereSingle4();
|
||||
}
|
||||
base.End("Where Test");
|
||||
}
|
||||
|
||||
private void whereSingle1()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 1;
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle);
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1)
|
||||
}, "whereSingle1");
|
||||
}
|
||||
private void whereSingle2()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 1 || it.Name == "a";
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle);
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) OR ( Name = @Name2 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1),
|
||||
new SugarParameter("@Name2","a")
|
||||
}, "whereSingle2");
|
||||
}
|
||||
private void whereSingle3()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 1 || it.Name == "a";
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle);
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) OR ( Name = @Name2 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1),
|
||||
new SugarParameter("@Name2","a")
|
||||
}, "whereSingle2");
|
||||
}
|
||||
private void whereSingle4()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it =>( it.Id > 1 &&it.Name!="a")|| it.Name == "a";
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle);
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) OR ( Name = @Name2 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1),
|
||||
new SugarParameter("@Name2","a")
|
||||
}, "whereSingle4");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,8 @@ namespace OrmTest
|
||||
{
|
||||
int eachCount = 1;
|
||||
new OrmTest.ExpressionTest.Select(eachCount).Init();
|
||||
OrmTest.ExpressionTest.Field.Init();
|
||||
new OrmTest.ExpressionTest.Field(eachCount).Init();
|
||||
new OrmTest.ExpressionTest.Where(eachCount).Init();
|
||||
|
||||
|
||||
// Program.id = "xx";
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -11,6 +11,7 @@ namespace SqlSugar
|
||||
public const string BinaryFormatString = " ( {0} {1} {2} ) ";
|
||||
public const string Format0 = "{0}";
|
||||
public const string Format1 = "{1}";
|
||||
public const string Format2 = "{2}";
|
||||
public readonly static Type MemberExpressionType = typeof(MemberExpression);
|
||||
public readonly static Type ConstantExpressionType = typeof(ConstantExpression);
|
||||
public readonly static Type StringType = typeof(string);
|
||||
|
@ -73,7 +73,13 @@ namespace SqlSugar
|
||||
public void TrimEnd()
|
||||
{
|
||||
if (this._Result == null) return;
|
||||
this.Result=this.Result.Remove(this.Result.Length-1,1);
|
||||
this.Result = this.Result.Remove(this.Result.Length - 1, 1);
|
||||
}
|
||||
|
||||
public bool Contains(string value)
|
||||
{
|
||||
if (this.Result.Equals(value)) return true;
|
||||
return (this.Result.ToString().Contains(value));
|
||||
}
|
||||
|
||||
public void Append(object parameter)
|
||||
|
@ -26,18 +26,53 @@ namespace SqlSugar
|
||||
expression.NodeType != ExpressionType.Or &&
|
||||
expression.NodeType != ExpressionType.OrElse;
|
||||
base.BaseExpression = expression;
|
||||
var leftExpression = expression.Left;
|
||||
var rightExpression = expression.Right;
|
||||
var leftIsBinary = leftExpression is BinaryExpression;
|
||||
var rightBinary = rightExpression is BinaryExpression;
|
||||
int i = 0;
|
||||
var lbrs = leftIsBinary && !rightBinary;
|
||||
var lsrb = !leftIsBinary && rightBinary;
|
||||
var lbrb = rightBinary && leftIsBinary;
|
||||
var lsbs = !leftIsBinary && !rightBinary;
|
||||
if (lbrs)
|
||||
{
|
||||
base.Context.Result.Append("{" + i + "}");
|
||||
++i;
|
||||
}
|
||||
else if (lsrb)
|
||||
{
|
||||
base.Context.Result.Append("{" + i + "}");
|
||||
}
|
||||
else if (lbrb)
|
||||
{
|
||||
base.Context.Result.Append("{0}");
|
||||
base.Context.Result.Append("{2}");
|
||||
base.Context.Result.Append("{1}");
|
||||
}
|
||||
base.Expression = leftExpression;
|
||||
base.IsLeft = true;
|
||||
base.Expression = expression.Left;
|
||||
base.Start();
|
||||
base.IsLeft = false;
|
||||
base.Expression = expression.Right;
|
||||
base.Expression = rightExpression;
|
||||
base.Start();
|
||||
base.IsLeft = null;
|
||||
string leftString = GetLeftString(parameter);
|
||||
string rightString = GetRightString(parameter);
|
||||
string binarySql = string.Format(ExpressionConst.BinaryFormatString, leftString, operatorValue, rightString);
|
||||
string sqlWhereString = base.Context.Result.GetResultString();
|
||||
if (sqlWhereString.Contains(ExpressionConst.Format0))
|
||||
string leftString = null;
|
||||
if (!leftIsBinary)
|
||||
leftString = GetLeftString(parameter);
|
||||
string rightString = null;
|
||||
if (!rightBinary)
|
||||
rightString = GetRightString(parameter);
|
||||
string binarySql = null;
|
||||
if (lsbs)
|
||||
{
|
||||
binarySql = string.Format(ExpressionConst.BinaryFormatString, leftString, operatorValue, rightString);
|
||||
}
|
||||
else if (lbrb)
|
||||
{
|
||||
binarySql = operatorValue;
|
||||
}
|
||||
if (Context.Result.Contains(ExpressionConst.Format0))
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format0, binarySql);
|
||||
}
|
||||
@ -45,9 +80,10 @@ namespace SqlSugar
|
||||
{
|
||||
base.Context.Result.Append(binarySql);
|
||||
}
|
||||
if (sqlWhereString.Contains(ExpressionConst.Format1))
|
||||
if (Context.Result.Contains(ExpressionConst.Format1))
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format1, ExpressionConst.Format0);
|
||||
base.Context.Result.Replace(ExpressionConst.Format2, ExpressionConst.Format1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -60,7 +96,8 @@ namespace SqlSugar
|
||||
{
|
||||
var sqlParameterKeyWord = parameter.Context.SqlParameterKeyWord;
|
||||
var reval = string.Format("{0}{1}{2}", sqlParameterKeyWord, leftInfo.Value, parameter.Context.Index + parameter.Index);
|
||||
if (parameter.Context.Parameters == null) {
|
||||
if (parameter.Context.Parameters == null)
|
||||
{
|
||||
parameter.Context.Parameters = new List<SugarParameter>();
|
||||
}
|
||||
parameter.Context.Parameters.Add(new SugarParameter(reval, rightInfo.Value));
|
||||
|
@ -15,14 +15,6 @@ namespace SqlSugar
|
||||
string fieldName = string.Empty;
|
||||
switch (parameter.Context.ResolveType)
|
||||
{
|
||||
case ResolveExpressType.WhereSingle:
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
break;
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
fieldName = getMultipleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
break;
|
||||
case ResolveExpressType.SelectSingle:
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
@ -31,6 +23,21 @@ namespace SqlSugar
|
||||
fieldName = getMultipleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
break;
|
||||
case ResolveExpressType.WhereSingle:
|
||||
if (parameter.BaseExpression is BinaryExpression)
|
||||
{
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
}
|
||||
break;
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
fieldName = getMultipleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
break;
|
||||
case ResolveExpressType.FieldSingle:
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
@ -44,7 +51,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private string getMultipleName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft)
|
||||
private string getMultipleName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft)
|
||||
{
|
||||
string shortName = expression.Expression.ToString();
|
||||
string fieldName = expression.Member.Name;
|
||||
|
Loading…
Reference in New Issue
Block a user