Browse Source

Support method nesting

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
93192405ce
  1. 48
      SqlServerTest/Program.cs
  2. 38
      SqlServerTest/UnitTest/ExpressionTest/Method.cs
  3. 7
      SqlSugar/ExpressionsToSql/ExpressionContext.cs
  4. 11
      SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs
  5. 1
      SqlSugar/ExpressionsToSql/Method/IDbMethods.cs
  6. 2
      SqlSugar/ExpressionsToSql/Method/SqlFunc.cs
  7. 72
      SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

48
SqlServerTest/Program.cs

@ -17,31 +17,31 @@ namespace OrmTest
{
static void Main(string[] args)
{
///***Unit Test***/
//new Field(1).Init();
//new Where(1).Init();
//new Method(1).Init();
//new JoinQuery(1).Init();
//new SingleQuery(1).Init();
//new SelectQuery(1).Init();
//new AutoClose(1).Init();
//new Insert(1).Init();
//new Delete(1).Init();
//new Update(1).Init();
//new Mapping(1).Init();
//new DataTest(1).Init();
///***Performance Test***/
//new SqlSugarPerformance(100).Select();
/***Unit Test***/
new Field(1).Init();
new Where(1).Init();
new Method(1).Init();
new JoinQuery(1).Init();
new SingleQuery(1).Init();
new SelectQuery(1).Init();
new AutoClose(1).Init();
new Insert(1).Init();
new Delete(1).Init();
new Update(1).Init();
new Mapping(1).Init();
new DataTest(1).Init();
/***Performance Test***/
new SqlSugarPerformance(100).Select();
///***Demo***/
//OrmTest.Demo.Query.Init();
//OrmTest.Demo.Insert.Init();
//OrmTest.Demo.Delete.Init();
//OrmTest.Demo.Update.Init();
//OrmTest.Demo.DbFirst.Init();
//OrmTest.Demo.JoinSql.Init();
//OrmTest.Demo.Filter.Init();
//OrmTest.Demo.MaterSlave.Init();
/***Demo***/
OrmTest.Demo.Query.Init();
OrmTest.Demo.Insert.Init();
OrmTest.Demo.Delete.Init();
OrmTest.Demo.Update.Init();
OrmTest.Demo.DbFirst.Init();
OrmTest.Demo.JoinSql.Init();
OrmTest.Demo.Filter.Init();
OrmTest.Demo.MaterSlave.Init();
OrmTest.Demo.ComplexModel.Init();
}
}

38
SqlServerTest/UnitTest/ExpressionTest/Method.cs

@ -21,6 +21,8 @@ namespace OrmTest.UnitTest
base.Begin();
for (int i = 0; i < base.Count; i++)
{
IIF();
IIF2();
#region StringIsNullOrEmpty
HasValue();
HasNumber();
@ -473,7 +475,8 @@ namespace OrmTest.UnitTest
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "( [Name]<>'' AND [Name] IS NOT NULL )", new List<SugarParameter>() {
base.Check(value, pars, "( [Name]<>'' AND [Name] IS NOT NULL )", new List<SugarParameter>()
{
}, "HasValue error");
}
@ -485,11 +488,42 @@ namespace OrmTest.UnitTest
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "( [Id]>0 AND [Id] IS NOT NULL )", new List<SugarParameter>() {
base.Check(value, pars, "( [Id]>0 AND [Id] IS NOT NULL )", new List<SugarParameter>()
{
}, "HasNumber error");
}
private void IIF()
{
Expression<Func<Student, bool>> exp = it => SqlFunc.IIF(it.Id == 1, 1, 2);
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "( CASE WHEN ( [Id] = @Id1 ) THEN @MethodConst2 ELSE @MethodConst3 END )", new List<SugarParameter>()
{
new SugarParameter("@Id1",1),
new SugarParameter("@MethodConst2",1),
new SugarParameter("@MethodConst3",2)
}, "IIF error");
}
private void IIF2()
{
Expression<Func<Student, bool>> exp = it => SqlFunc.IIF(SqlFunc.Contains(it.Name,"a"), 1, 2);
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "( CASE WHEN ([Name] like '%'+@MethodConst1+'%') THEN @MethodConst2 ELSE @MethodConst3 END )", new List<SugarParameter>()
{
new SugarParameter("@MethodConst1","a"),
new SugarParameter("@MethodConst2",1),
new SugarParameter("@MethodConst3",2)
}, "IIF error");
}
}
}

7
SqlSugar/ExpressionsToSql/ExpressionContext.cs

@ -164,6 +164,13 @@ namespace SqlSugar
base._Result = null;
base._Parameters = new List<SugarParameter>();
}
public ExpressionContext GetCopyContext()
{
ExpressionContext copyContext = (ExpressionContext)Activator.CreateInstance(this.GetType(), true);
copyContext.Index = this.Index + 1;
copyContext.ParameterIndex = this.ParameterIndex + 1;
return copyContext;
}
#endregion
}
}

11
SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs

@ -8,6 +8,14 @@ namespace SqlSugar
{
public abstract partial class DefaultDbMethod : IDbMethods
{
public virtual string IIF(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter2 = model.Args[1];
var parameter3 = model.Args[2];
return string.Format("( CASE WHEN {0} THEN {1} ELSE {2} END )", parameter.MemberName, parameter2.MemberName, parameter3.MemberName);
}
public virtual string IsNullOrEmpty(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
@ -68,7 +76,8 @@ namespace SqlSugar
{
inValueString = inValues.ToArray().ToJoinSqlInVals();
}
else {
else
{
return " (1=2) ";
}
return string.Format(" ({0} IN ({1})) ", value, inValueString);

1
SqlSugar/ExpressionsToSql/Method/IDbMethods.cs

@ -11,6 +11,7 @@ namespace SqlSugar
{
public interface IDbMethods
{
string IIF(MethodCallExpressionModel model);
string HasNumber(MethodCallExpressionModel model);
string HasValue(MethodCallExpressionModel model);
string IsNullOrEmpty(MethodCallExpressionModel model);

2
SqlSugar/ExpressionsToSql/Method/SqlFunc.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
@ -65,6 +66,7 @@ namespace SqlSugar
public static DateTime DateAdd(DateTime date, int addValue) { throw new NotSupportedException("This method is not supported by the current parameter"); }
public static int DateValue(DateTime date, DateType dataType) { throw new NotSupportedException("This method is not supported by the current parameter"); }
public static bool Between(object value, object start, object end) { throw new NotSupportedException("This method is not supported by the current parameter"); }
public static bool IIF(bool Expression, object thenValue, object elseValue) { throw new NotSupportedException("This method is not supported by the current parameter"); }
public static int ToInt32(object value) { return value.ObjToInt(); }
public static long ToInt64(object value) { return Convert.ToInt64(value); }
public static DateTime ToDate(object value) { return value.ObjToDate(); }

72
SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

@ -22,12 +22,12 @@ namespace SqlSugar
{
case ResolveExpressType.WhereSingle:
case ResolveExpressType.WhereMultiple:
WhereMethod(parameter, isLeft, name, args, model);
Where(parameter, isLeft, name, args, model);
break;
case ResolveExpressType.SelectSingle:
case ResolveExpressType.SelectMultiple:
case ResolveExpressType.Update:
SelectMethod(parameter, isLeft, name, args, model);
Select(parameter, isLeft, name, args, model);
break;
case ResolveExpressType.FieldSingle:
case ResolveExpressType.FieldMultiple:
@ -35,42 +35,57 @@ namespace SqlSugar
break;
}
}
private void SelectMethod(ExpressionParameter parameter, bool? isLeft, string name, System.Collections.ObjectModel.ReadOnlyCollection<Expression> args, MethodCallExpressionModel model)
private void Select(ExpressionParameter parameter, bool? isLeft, string name, System.Collections.ObjectModel.ReadOnlyCollection<Expression> args, MethodCallExpressionModel model)
{
foreach (var item in args)
{
parameter.CommonTempData = CommonTempDataType.Result;
base.Expression = item;
base.Start();
var methodCallExpressionArgs = new MethodCallExpressionArgs()
{
IsMember = parameter.ChildExpression is MemberExpression,
MemberName = parameter.CommonTempData
};
var value = methodCallExpressionArgs.MemberName;
if (methodCallExpressionArgs.IsMember)
{
var childExpression = parameter.ChildExpression as MemberExpression;
if (childExpression.Expression != null && childExpression.Expression is ConstantExpression)
var isBinaryExpression = item is BinaryExpression || item is MethodCallExpression;
if (isBinaryExpression)
{
methodCallExpressionArgs.IsMember = false;
Binary(parameter, model, item);
}
}
if (methodCallExpressionArgs.IsMember == false)
else
{
var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.MethodConst + this.Context.ParameterIndex;
this.Context.ParameterIndex++;
methodCallExpressionArgs.MemberName = parameterName;
methodCallExpressionArgs.MemberValue = value;
this.Context.Parameters.Add(new SugarParameter(parameterName, value));
Default(parameter, model, item);
}
model.Args.Add(methodCallExpressionArgs);
}
parameter.BaseParameter.CommonTempData = GetMdthodValue(name, model);
}
private void WhereMethod(ExpressionParameter parameter, bool? isLeft, string name, System.Collections.ObjectModel.ReadOnlyCollection<Expression> args, MethodCallExpressionModel model)
private void Where(ExpressionParameter parameter, bool? isLeft, string name, System.Collections.ObjectModel.ReadOnlyCollection<Expression> args, MethodCallExpressionModel model)
{
foreach (var item in args)
{
var isBinaryExpression = item is BinaryExpression||item is MethodCallExpression;
if (isBinaryExpression)
{
Binary(parameter, model, item);
}
else
{
Default(parameter, model, item);
}
}
var methodValue = GetMdthodValue(name, model);
base.AppendValue(parameter, isLeft, methodValue);
}
private void Binary(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
{
var newContext = this.Context.GetCopyContext();
newContext.Resolve(item, this.Context.ResolveType);
this.Context.Index = newContext.Index ;
this.Context.ParameterIndex = newContext.ParameterIndex ;
if (newContext.Parameters.IsValuable())
{
this.Context.Parameters.AddRange(newContext.Parameters);
}
var methodCallExpressionArgs = new MethodCallExpressionArgs()
{
IsMember = true,
MemberName = newContext.Result.GetResultString()
};
model.Args.Add(methodCallExpressionArgs);
}
private void Default(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
{
parameter.CommonTempData = CommonTempDataType.Result;
base.Expression = item;
@ -103,14 +118,13 @@ namespace SqlSugar
}
model.Args.Add(methodCallExpressionArgs);
}
var methodValue = GetMdthodValue(name, model);
base.AppendValue(parameter, isLeft, methodValue);
}
private object GetMdthodValue(string name, MethodCallExpressionModel model)
{
switch (name)
{
case "IIF":
return this.Context.DbMehtods.IIF(model);
case "HasNumber":
return this.Context.DbMehtods.HasNumber(model);
case "HasValue":

Loading…
Cancel
Save