Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
3805d5291f
  1. BIN
      .vs/SqlSugar/v14/.suo
  2. 24
      OrmTest/ExpressionTest/Field.cs
  3. 72
      OrmTest/ExpressionTest/Where.cs
  4. 3
      OrmTest/Program.cs
  5. BIN
      OrmTest/bin/Debug/OrmTest.exe
  6. BIN
      OrmTest/bin/Debug/OrmTest.pdb
  7. BIN
      OrmTest/bin/Debug/SqlSugar.dll
  8. BIN
      OrmTest/bin/Debug/SqlSugar.pdb
  9. 1
      SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs
  10. 8
      SqlSugar/ExpressionsToSql/Common/ExpressionResult.cs
  11. 55
      SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs
  12. 21
      SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

BIN
.vs/SqlSugar/v14/.suo

Binary file not shown.

24
OrmTest/ExpressionTest/Field.cs

@ -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");
}
}
}

72
OrmTest/ExpressionTest/Where.cs

@ -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");
}
}
}

3
OrmTest/Program.cs

@ -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";

BIN
OrmTest/bin/Debug/OrmTest.exe

Binary file not shown.

BIN
OrmTest/bin/Debug/OrmTest.pdb

Binary file not shown.

BIN
OrmTest/bin/Debug/SqlSugar.dll

Binary file not shown.

BIN
OrmTest/bin/Debug/SqlSugar.pdb

Binary file not shown.

1
SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs

@ -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);

8
SqlSugar/ExpressionsToSql/Common/ExpressionResult.cs

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

55
SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs

@ -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));

21
SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

@ -15,19 +15,26 @@ namespace SqlSugar
string fieldName = string.Empty;
switch (parameter.Context.ResolveType)
{
case ResolveExpressType.WhereSingle:
case ResolveExpressType.SelectSingle:
fieldName = getSingleName(parameter, expression, isLeft);
base.Context.Result.Append(fieldName);
break;
case ResolveExpressType.WhereMultiple:
case ResolveExpressType.SelectMultiple:
fieldName = getMultipleName(parameter, expression, isLeft);
base.Context.Result.Append(fieldName);
break;
case ResolveExpressType.SelectSingle:
fieldName = getSingleName(parameter, expression, isLeft);
base.Context.Result.Append(fieldName);
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.SelectMultiple:
case ResolveExpressType.WhereMultiple:
fieldName = getMultipleName(parameter, expression, isLeft);
base.Context.Result.Append(fieldName);
break;
@ -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…
Cancel
Save