StringIsNullOrEmpty

This commit is contained in:
sunkaixuan 2017-01-30 15:10:54 +08:00
parent 3f265d7d61
commit 1971ccd6fd
18 changed files with 215 additions and 10 deletions

Binary file not shown.

View File

@ -21,9 +21,20 @@ namespace OrmTest.ExpressionTest
base.Begin();
for (int i = 0; i < base.Count; i++)
{
StringIsNullOrEmpty();
}
base.End("Method Test");
}
private void StringIsNullOrEmpty()
{
Expression<Func<Student, bool>> exp = it =>it.Id>2|| NBORM.IsNullOrEmpty(it.Id);;
SqlServerExpressionContext expContext = new SqlServerExpressionContext(exp, ResolveExpressType.WhereSingle);
expContext.Resolve();
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "(( Id > @Id0 ) OR ( Id='' OR Id IS NULL ))", new List<SugarParameter>() {
new SugarParameter("@Id0",2)
}, "whereSingle1");
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public enum CommonTempDataType
{
Default = 0,
ChildNodeSet = 1,
Simple = 2
}
}

View File

@ -14,6 +14,7 @@ namespace SqlSugar
public const string Format2 = "o__o";
public const string Format3 = "(";
public const string Format4 = ")";
public const string NBORMFULLNAME = "SqlSugar.NBORM";
public readonly static Type MemberExpressionType = typeof(MemberExpression);
public readonly static Type ConstantExpressionType = typeof(ConstantExpression);
public readonly static Type StringType = typeof(string);

View File

@ -21,5 +21,13 @@ namespace SqlSugar
return ExpressionConst.GetThrowMessage("Expression format error, correct format: it=>it.fieldName","表达示格式错误,正确格式: it=>it.fieldName");
}
}
internal static string MethodError
{
get
{
return ExpressionConst.GetThrowMessage("Please use the following methods in the NBORM class, such as NBORM.IsNullOrEmpty (it.Name) for Lambda parsing", "拉姆达解析请使用 NBORM 类下面的方法,例如 NBORM.IsNullOrEmpty(it.Name)");
}
}
}
}

View File

@ -11,6 +11,7 @@ namespace SqlSugar
public ExpressionContext Context { get; set; }
public ExpressionParameter BaseParameter { get; set; }
public Expression BaseExpression { get; set; }
public Expression ChildExpression { get; set; }
public Expression LeftExpression { get; set; }
public Expression RightExpression { get; set; }
public Expression Expression { get; set; }

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
namespace SqlSugar
{
public class MethodCallExpressionModel
{
public List<MethodCallExpressionArgs> Args { get; set; }
}
public class MethodCallExpressionArgs
{
public bool IsMember { get; set; }
public object Value { get; set; }
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SqlServerExpressionContext : ExpressionContext
{
public SqlServerExpressionContext(Expression expression, ResolveExpressType resolveType) : base(expression, resolveType)
{
base.DbMehtods = new SqlServerMethod();
}
}
public class SqlServerMethod : IDbMethods
{
public string IsNullOrEmpty(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
if (parameter.IsMember)
{
return string.Format("( {0}='' OR {0} IS NULL )", parameter.Value);
}
else
{
return null;
}
}
}
}

View File

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@ -8,6 +11,6 @@ namespace SqlSugar
{
public interface IDbMethods
{
string IsNullOrEmpty(MethodCallExpressionModel model);
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class NBORM
{
public static bool IsNullOrEmpty(object thisValue) { throw new NotImplementedException(); }
public static string ToLower(object thisValue) { throw new NotImplementedException(); }
public static string ToUpper(object thisValue) { throw new NotImplementedException(); }
}
}

View File

@ -13,6 +13,8 @@ namespace SqlSugar
var expression = base.Expression as MemberExpression;
var isLeft = parameter.IsLeft;
string fieldName = string.Empty;
var baseParameter = parameter.BaseParameter;
var isSetTempData = baseParameter.CommonTempData.IsValuable() && baseParameter.CommonTempData.Equals(CommonTempDataType.ChildNodeSet);
switch (parameter.Context.ResolveType)
{
case ResolveExpressType.SelectSingle:
@ -24,12 +26,28 @@ namespace SqlSugar
base.Context.Result.Append(fieldName);
break;
case ResolveExpressType.WhereSingle:
fieldName = getSingleName(parameter, expression, isLeft);
fieldName = AppendMember(parameter, isLeft, fieldName);
if (isSetTempData)
{
fieldName = getSingleName(parameter, expression, null);
baseParameter.CommonTempData = fieldName;
}
else
{
fieldName = getSingleName(parameter, expression, isLeft);
fieldName = AppendMember(parameter, isLeft, fieldName);
}
break;
case ResolveExpressType.WhereMultiple:
fieldName = getMultipleName(parameter, expression, isLeft);
fieldName = AppendMember(parameter, isLeft, fieldName);
if (isSetTempData)
{
fieldName = getMultipleName(parameter, expression, null);
baseParameter.CommonTempData = fieldName;
}
else
{
fieldName = getMultipleName(parameter, expression, isLeft);
fieldName = AppendMember(parameter, isLeft, fieldName);
}
break;
case ResolveExpressType.FieldSingle:
fieldName = getSingleName(parameter, expression, isLeft);

View File

@ -1,15 +1,64 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
public class MethodCallExpressionResolve:BaseResolve
public class MethodCallExpressionResolve : BaseResolve
{
public MethodCallExpressionResolve(ExpressionParameter parameter) : base(parameter)
{
var express = base.Expression as MethodCallExpression;
CheckMethod(express);
var method = express.Method;
string name = method.Name;
var args = express.Arguments;
MethodCallExpressionModel model = new MethodCallExpressionModel();
model.Args = new List<MethodCallExpressionArgs>();
switch (this.Context.ResolveType)
{
case ResolveExpressType.WhereSingle:
case ResolveExpressType.WhereMultiple:
foreach (var item in args)
{
base.Expression = item;
base.Start();
model.Args.Add(new MethodCallExpressionArgs()
{
IsMember = parameter.ChildExpression is MemberExpression,
Value = parameter.CommonTempData
});
}
var methodValue = GetMdthodValue(name,model);
base.Context.Result.Append(methodValue);
break;
case ResolveExpressType.SelectSingle:
case ResolveExpressType.SelectMultiple:
case ResolveExpressType.FieldSingle:
case ResolveExpressType.FieldMultiple:
default:
break;
}
}
private object GetMdthodValue(string name, MethodCallExpressionModel model)
{
switch (name)
{
case "IsNullOrEmpty":
return this.Context.DbMehtods.IsNullOrEmpty(model);
default:
break;
}
return null;
}
public void CheckMethod(MethodCallExpression expression)
{
Check.Exception(expression.Method.ReflectedType.FullName != ExpressionConst.NBORMFULLNAME, ExpressionErrorMessage.MethodError);
}
}
}

View File

@ -5,11 +5,38 @@ using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
public class UnaryExpressionResolve:BaseResolve
public class UnaryExpressionResolve : BaseResolve
{
public UnaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
{
var expression = base.Expression as UnaryExpression;
var baseParameter = parameter.BaseParameter;
switch (this.Context.ResolveType)
{
case ResolveExpressType.WhereSingle:
case ResolveExpressType.WhereMultiple:
base.Expression = expression.Operand;
if (base.Expression is MemberExpression)
{
BaseParameter.ChildExpression = base.Expression;
parameter.CommonTempData = CommonTempDataType.ChildNodeSet;
base.Start();
parameter.BaseParameter.CommonTempData = parameter.CommonTempData;
parameter.BaseParameter.ChildExpression = base.Expression;
parameter.CommonTempData = null;
}
break;
case ResolveExpressType.SelectSingle:
break;
case ResolveExpressType.SelectMultiple:
break;
case ResolveExpressType.FieldSingle:
break;
case ResolveExpressType.FieldMultiple:
break;
default:
break;
}
}
}
}

View File

@ -97,6 +97,10 @@
<Compile Include="Entities\ConnectionConfig.cs" />
<Compile Include="Entities\DbColumnInfo.cs" />
<Compile Include="Entities\DbTableInfo.cs" />
<Compile Include="ExpressionsToSql\Common\CommonTempDataType.cs" />
<Compile Include="ExpressionsToSql\Common\MethodCallExpressionModel.cs" />
<Compile Include="ExpressionsToSql\Databases\SqlServerExpressionContext.cs" />
<Compile Include="ExpressionsToSql\NBORM.cs" />
<Compile Include="ExpressionsToSql\Common\BinaryExpressionInfo.cs" />
<Compile Include="ExpressionsToSql\Common\ExpressionResult.cs" />
<Compile Include="ExpressionsToSql\Common\ExpressionErrorMessage.cs" />