Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
1fd736566f
  1. 13
      SqlServerTest/UnitTest/ExpressionTest/Method.cs
  2. 36
      SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

13
SqlServerTest/UnitTest/ExpressionTest/Method.cs

@ -26,6 +26,7 @@ namespace OrmTest.UnitTest
ConvetToString();
ExtendToString();
ExtendSubstring();
ExtendDate();
//SqlFun methods
IIF();
@ -222,6 +223,18 @@ namespace OrmTest.UnitTest
new SugarParameter("@MethodConst0","2015-1-1"),new SugarParameter("@Const1",x2)
}, "ToDate error");
}
private void ExtendDate()
{
var x2 = DateTime.Now;
Expression<Func<Student, bool>> exp = it => Convert.ToDateTime("2015-1-1") == x2;
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "(CAST(@MethodConst0 AS DATETIME) = @Const1 )", new List<SugarParameter>() {
new SugarParameter("@MethodConst0","2015-1-1"),new SugarParameter("@Const1",x2)
}, "ExtendDate error");
}
private void ToInt64()
{

36
SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

@ -13,16 +13,11 @@ namespace SqlSugar
{
var express = base.Expression as MethodCallExpression;
var isLeft = parameter.IsLeft;
var isValidNativeMethod =
express.Method.Name.IsIn(
"ToString", "ToInt32", "ToInt64",
"Length", "Replace", "Substring",
"Contains", "EndsWith", "StartsWith",
"HasValue", "Trim", "Equals",
"ToLower", "ToUpper");
string methodName = express.Method.Name;
var isValidNativeMethod = MethodMapping.ContainsKey(methodName)&&express.Method.DeclaringType.Namespace==("System");
if (isValidNativeMethod)
{
NativeExtensionMethod(parameter, express, isLeft);
NativeExtensionMethod(parameter, express, isLeft, MethodMapping[methodName]);
}
else
{
@ -56,10 +51,9 @@ namespace SqlSugar
}
}
private void NativeExtensionMethod(ExpressionParameter parameter, MethodCallExpression express, bool? isLeft)
private void NativeExtensionMethod(ExpressionParameter parameter, MethodCallExpression express, bool? isLeft,string name)
{
var method = express.Method;
string name = method.Name;
var args = express.Arguments.Cast<Expression>().ToList();
MethodCallExpressionModel model = new MethodCallExpressionModel();
model.Args = new List<MethodCallExpressionArgs>();
@ -234,6 +228,28 @@ namespace SqlSugar
return null;
}
private static Dictionary<string, string> MethodMapping = new Dictionary<string, string>() {
{ "ToString","ToString"},
{ "ToInt32","ToInt32"},
{ "ToInt16","ToInt32"},
{ "ToInt64","ToInt64"},
{ "ToDecimal","ToDecimal"},
{ "ToDateTime","ToDate"},
{ "ToBoolean","ToBool"},
{ "ToDouble","ToDouble"},
{ "Length","Length"},
{ "Replace","Replace"},
{ "Contains","Contains"},
{ "EndsWith","EndsWith"},
{ "StartsWith","StartsWith"},
{ "HasValue","HasValue"},
{ "Trim","Trim"},
{ "Equals","Equals"},
{ "ToLower","ToLower"},
{ "ToUpper","ToUpper"},
{ "Substring","Substring"}
};
private void CheckMethod(MethodCallExpression expression)
{
Check.Exception(expression.Method.ReflectedType.FullName != ExpressionConst.SqlFuncFullName, ExpressionErrorMessage.MethodError);

Loading…
Cancel
Save