Browse Source

Support native HasValue

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
d3de52b89f
  1. 2
      Src/Asp.Net/SqlServerTest/Program.cs
  2. 7
      Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Select.cs
  3. 38
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs
  4. 30
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs

2
Src/Asp.Net/SqlServerTest/Program.cs

@ -18,7 +18,7 @@ namespace OrmTest
static void Main(string[] args)
{
// /***Unit Test***/
//new Select(1).Init();
new Select(1).Init();
new Field(1).Init();
new Where(1).Init();
new Method(1).Init();

7
Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Select.cs

@ -129,8 +129,9 @@ namespace OrmTest.UnitTest
"Select.single4 Error");
}
private void single5(DateTime? p = null)
private void single5()
{
var p =(DateTime?) DateTime.Now;
Expression<Func<Student, object>> exp = it => p.HasValue;
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.IsSingle = false;
@ -138,7 +139,9 @@ namespace OrmTest.UnitTest
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
@"( [it].[SchoolId]<>'' AND [it].[SchoolId] IS NOT NULL )", null, selectorValue, null,
@"( @constant0<>'' AND @constant0 IS NOT NULL )", new List<SugarParameter>() {
new SugarParameter("@constant0",p)
}, selectorValue, pars,
"Select.single4 Error");
}

38
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs

@ -106,6 +106,7 @@ namespace SqlSugar
}
return null;
}
protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue)
{
@ -197,6 +198,25 @@ namespace SqlSugar
this.Context.Result.Append(" " + ExpressionConst.Format1 + parameter.BaseParameter.Index);
}
}
protected string AppendParameter(object paramterValue)
{
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
this.Context.ParameterIndex++;;
this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
return parameterName;
}
protected void AppendNot(object Value)
{
var isAppend = !this.Context.Result.Contains(ExpressionConst.Format0);
if (isAppend)
{
this.Context.Result.Append("NOT");
}
else
{
this.Context.Result.Replace(ExpressionConst.Format0, "NOT");
}
}
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item)
{
@ -215,7 +235,6 @@ namespace SqlSugar
};
return methodCallExpressionArgs;
}
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
{
if (item.NodeType == ExpressionType.Constant)
@ -229,7 +248,7 @@ namespace SqlSugar
}
else if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
{
var paramterValue= ExpressionTool.GetPropertyValue(item as MemberExpression);
var paramterValue = ExpressionTool.GetPropertyValue(item as MemberExpression);
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
this.Context.ParameterIndex++;
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
@ -337,7 +356,7 @@ namespace SqlSugar
var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
foreach (var property in listProperties)
{
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name,StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name,StringComparison.CurrentCultureIgnoreCase));
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
if (hasIgnore)
{
continue;
@ -366,18 +385,5 @@ namespace SqlSugar
Check.ThrowNotSupportedException(item.GetType().Name);
}
}
protected void AppendNot(object Value)
{
var isAppend = !this.Context.Result.Contains(ExpressionConst.Format0);
if (isAppend)
{
this.Context.Result.Append("NOT");
}
else
{
this.Context.Result.Replace(ExpressionConst.Format0, "NOT");
}
}
}
}

30
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs

@ -60,17 +60,31 @@ namespace SqlSugar
var member = memberExpression.Expression as MemberExpression;
parameter.CommonTempData = CommonTempDataType.Result;
var isConst = member.Expression != null && member.Expression is ConstantExpression;
this.Expression = isConst?member.Expression:member;
this.Start();
var methodParamter = isConst ? new MethodCallExpressionArgs() { IsMember=false } : new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null };
var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel()
if (isConst)
{
Args = new List<MethodCallExpressionArgs>() {
var paramterValue = ExpressionTool.DynamicInvoke(member);
var paramterName= base.AppendParameter(paramterValue);
var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel()
{
Args = new List<MethodCallExpressionArgs>() {
new MethodCallExpressionArgs() { IsMember=false, MemberName=paramterName, MemberValue=paramterValue } }
});
this.Context.Result.Append(result);
}
else
{
this.Expression = isConst ? member.Expression : member;
this.Start();
var methodParamter = isConst ? new MethodCallExpressionArgs() { IsMember = false } : new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null };
var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel()
{
Args = new List<MethodCallExpressionArgs>() {
methodParamter
}
});
this.Context.Result.Append(result);
parameter.CommonTempData = null;
});
this.Context.Result.Append(result);
parameter.CommonTempData = null;
}
}
else if (memberExpression.Type == PubConst.BoolType && isLogicOperator)
{

Loading…
Cancel
Save