diff --git a/.vs/SqlSugar/v14/.suo b/.vs/SqlSugar/v14/.suo index b8a0e0280..d3d1644e2 100644 Binary files a/.vs/SqlSugar/v14/.suo and b/.vs/SqlSugar/v14/.suo differ diff --git a/OrmTest/ExpressionTest/Field.cs b/OrmTest/ExpressionTest/Field.cs new file mode 100644 index 000000000..1adb89acc --- /dev/null +++ b/OrmTest/ExpressionTest/Field.cs @@ -0,0 +1,22 @@ +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 Field + { + internal static void Init() + { + Expression> exp = it=>it.Name; + ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.FieldMultiple); + expContext.Resolve(); + var selectorValue = expContext.Result.GetString(); + } + } +} diff --git a/OrmTest/ExpressionTest/Select.cs b/OrmTest/ExpressionTest/Select.cs index fcacf6ad9..104822d88 100644 --- a/OrmTest/ExpressionTest/Select.cs +++ b/OrmTest/ExpressionTest/Select.cs @@ -14,7 +14,7 @@ namespace OrmTest.ExpressionTest internal static void Init() { DateTime b = DateTime.Now; - int count = 10000; + int count = 1; for (int i = 0; i < count; i++) { single(); @@ -29,8 +29,7 @@ namespace OrmTest.ExpressionTest private static void Multiple() { Expression> exp = (it,school) => new Student() { Name = "a", Id = it.Id, SchoolId = school.Id }; - ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle); - expContext.ResolveType = ResolveExpressType.SelectMultiple; + ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.SelectMultiple); expContext.Resolve(); var selectorValue = expContext.Result.GetString(); var pars = expContext.Parameters; @@ -38,8 +37,7 @@ namespace OrmTest.ExpressionTest private static void MultipleDynamic() { Expression> exp = (it, school) => new{ Name = "a", Id = it.Id, SchoolId = school.Id }; - ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle); - expContext.ResolveType = ResolveExpressType.SelectMultiple; + ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.SelectMultiple); expContext.Resolve(); var selectorValue = expContext.Result.GetString(); var pars = expContext.Parameters; @@ -48,8 +46,7 @@ namespace OrmTest.ExpressionTest { int p = 1; Expression> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p }; - ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle); - expContext.ResolveType = ResolveExpressType.SelectSingle; + ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.SelectSingle); expContext.Resolve(); var selectorValue = expContext.Result.GetString(); var pars = expContext.Parameters; @@ -59,8 +56,7 @@ namespace OrmTest.ExpressionTest { string a = "a"; Expression> exp = it => new { x = it.Id, shoolid = 1, name = a }; - ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle); - expContext.ResolveType = ResolveExpressType.SelectSingle; + ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.SelectSingle); expContext.Resolve(); var selectorValue = expContext.Result.GetString(); var pars = expContext.Parameters; diff --git a/OrmTest/OrmTest.csproj b/OrmTest/OrmTest.csproj index ebfffb0fd..687c92b34 100644 --- a/OrmTest/OrmTest.csproj +++ b/OrmTest/OrmTest.csproj @@ -43,6 +43,7 @@ + diff --git a/OrmTest/Program.cs b/OrmTest/Program.cs index 0024c7640..51954f9ca 100644 --- a/OrmTest/Program.cs +++ b/OrmTest/Program.cs @@ -24,6 +24,7 @@ namespace OrmTest OrmTest.ExpressionTest.Select.Init(); + OrmTest.ExpressionTest.Field.Init(); Program.id = "xx"; diff --git a/OrmTest/bin/Debug/OrmTest.exe b/OrmTest/bin/Debug/OrmTest.exe index 63697a3de..7ba9335e5 100644 Binary files a/OrmTest/bin/Debug/OrmTest.exe and b/OrmTest/bin/Debug/OrmTest.exe differ diff --git a/OrmTest/bin/Debug/OrmTest.pdb b/OrmTest/bin/Debug/OrmTest.pdb index b3d3ead4e..f7d296bdf 100644 Binary files a/OrmTest/bin/Debug/OrmTest.pdb and b/OrmTest/bin/Debug/OrmTest.pdb differ diff --git a/OrmTest/bin/Debug/SqlSugar.dll b/OrmTest/bin/Debug/SqlSugar.dll index a766d6191..02d7ebbbd 100644 Binary files a/OrmTest/bin/Debug/SqlSugar.dll and b/OrmTest/bin/Debug/SqlSugar.dll differ diff --git a/OrmTest/bin/Debug/SqlSugar.pdb b/OrmTest/bin/Debug/SqlSugar.pdb index 330ff5686..c979ff4b0 100644 Binary files a/OrmTest/bin/Debug/SqlSugar.pdb and b/OrmTest/bin/Debug/SqlSugar.pdb differ diff --git a/OrmTest/obj/Debug/OrmTest.csprojResolveAssemblyReference.cache b/OrmTest/obj/Debug/OrmTest.csprojResolveAssemblyReference.cache index 1562ecf75..ca9c56447 100644 Binary files a/OrmTest/obj/Debug/OrmTest.csprojResolveAssemblyReference.cache and b/OrmTest/obj/Debug/OrmTest.csprojResolveAssemblyReference.cache differ diff --git a/OrmTest/obj/Debug/OrmTest.exe b/OrmTest/obj/Debug/OrmTest.exe index 63697a3de..7ba9335e5 100644 Binary files a/OrmTest/obj/Debug/OrmTest.exe and b/OrmTest/obj/Debug/OrmTest.exe differ diff --git a/OrmTest/obj/Debug/OrmTest.pdb b/OrmTest/obj/Debug/OrmTest.pdb index b3d3ead4e..f7d296bdf 100644 Binary files a/OrmTest/obj/Debug/OrmTest.pdb and b/OrmTest/obj/Debug/OrmTest.pdb differ diff --git a/SqlSugar/ExpressionsToSql/ExpressionContext.cs b/SqlSugar/ExpressionsToSql/ExpressionContext.cs index 28bc12286..97a2a1b17 100644 --- a/SqlSugar/ExpressionsToSql/ExpressionContext.cs +++ b/SqlSugar/ExpressionsToSql/ExpressionContext.cs @@ -7,6 +7,11 @@ using System.Linq.Expressions; using System.Text; namespace SqlSugar { + /// + /// ** description:Expression to sql + /// ** author:sunkaixuan + /// ** date:2017/1/14 + /// ** qq:610262374 public class ExpressionContext : ExpResolveAccessory { #region constructor @@ -24,7 +29,7 @@ namespace SqlSugar #region properties public IDbMethods DbMehtods { get; set; } public int Index { get; set; } - public ResolveExpressType ResolveType { get; set; } + internal ResolveExpressType ResolveType { get; set; } public Expression Expression { get; set; } public ExpressionResult Result { diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs index cf7078235..9ce8b279a 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs @@ -13,14 +13,25 @@ namespace SqlSugar var expression = base.Expression as ConstantExpression; var isLeft = parameter.IsLeft; object value = expression.Value; - if (parameter.BaseParameter.BinaryExpressionInfoList != null) + switch (parameter.Context.ResolveType) { - parameter.BaseParameter.BinaryExpressionInfoList.Add(new KeyValuePair(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo() - { - IsLeft = Convert.ToBoolean(isLeft), - Value = value, - ExpressionType = expression.GetType() - })); + case ResolveExpressType.WhereSingle: + case ResolveExpressType.WhereMultiple: + case ResolveExpressType.SelectSingle: + case ResolveExpressType.SelectMultiple: + case ResolveExpressType.FieldSingle: + case ResolveExpressType.FieldMultiple: + default: + if (parameter.BaseParameter.BinaryExpressionInfoList != null) + { + parameter.BaseParameter.BinaryExpressionInfoList.Add(new KeyValuePair(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo() + { + IsLeft = Convert.ToBoolean(isLeft), + Value = value, + ExpressionType = expression.GetType() + })); + } + break; } } } diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/MemberConstExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/MemberConstExpressionResolve.cs index c2ac3f9dc..d226ecb65 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/MemberConstExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/MemberConstExpressionResolve.cs @@ -14,14 +14,25 @@ namespace SqlSugar var expression = base.Expression as MemberExpression; var isLeft = parameter.IsLeft; object value = ExpressionTool.GetMemberValue(expression.Member, expression); - if (parameter.BaseParameter.BinaryExpressionInfoList != null) + switch (parameter.Context.ResolveType) { - parameter.BaseParameter.BinaryExpressionInfoList.Add(new KeyValuePair(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo() - { - IsLeft = Convert.ToBoolean(isLeft), - Value = value, - ExpressionType = expression.Expression.GetType() - })); + case ResolveExpressType.WhereSingle: + case ResolveExpressType.WhereMultiple: + case ResolveExpressType.SelectSingle: + case ResolveExpressType.SelectMultiple: + case ResolveExpressType.FieldSingle: + case ResolveExpressType.FieldMultiple: + default: + if (parameter.BaseParameter.BinaryExpressionInfoList != null) + { + parameter.BaseParameter.BinaryExpressionInfoList.Add(new KeyValuePair(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo() + { + IsLeft = Convert.ToBoolean(isLeft), + Value = value, + ExpressionType = expression.Expression.GetType() + })); + } + break; } } } diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs index 279eb1fd0..2a3c32045 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs @@ -15,29 +15,39 @@ namespace SqlSugar switch (parameter.Context.ResolveType) { case ResolveExpressType.WhereSingle: - fieldName = GetFieldNameByWhereSingle(parameter, expression, isLeft); + fieldName = getSingleName(parameter, expression, isLeft); + base.Context.Result.Append(fieldName); break; case ResolveExpressType.WhereMultiple: - fieldName = GetFiledNameByWhereMultiple(parameter, expression, isLeft); + fieldName = getMultipleName(parameter, expression, isLeft); + base.Context.Result.Append(fieldName); break; case ResolveExpressType.SelectSingle: + fieldName = getSingleName(parameter, expression, isLeft); base.Context.Result.Append(fieldName); break; case ResolveExpressType.SelectMultiple: + fieldName = getMultipleName(parameter, expression, isLeft); base.Context.Result.Append(fieldName); break; case ResolveExpressType.FieldSingle: + fieldName = getSingleName(parameter, expression, isLeft); + base.Context.Result.Append(fieldName); break; case ResolveExpressType.FieldMultiple: + fieldName = getMultipleName(parameter, expression, isLeft); + base.Context.Result.Append(fieldName); break; default: break; } } - private string GetFiledNameByWhereMultiple(ExpressionParameter parameter, MemberExpression expression, bool? isLeft) + private string getMultipleName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft) { - string fieldName = expression.Member.ToString(); + string shortName = expression.Expression.ToString(); + string fieldName = expression.Member.Name; + fieldName = shortName + "." + fieldName; if (parameter.BaseParameter.BinaryExpressionInfoList != null) parameter.BaseParameter.BinaryExpressionInfoList.Add(new KeyValuePair(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo() { @@ -48,7 +58,7 @@ namespace SqlSugar return fieldName; } - private string GetFieldNameByWhereSingle(ExpressionParameter parameter, MemberExpression expression, bool? isLeft) + private string getSingleName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft) { string fieldName = expression.Member.Name; if (parameter.BaseParameter.BinaryExpressionInfoList != null) diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs index 188fcb48d..5ff3b98fe 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs @@ -53,6 +53,7 @@ namespace SqlSugar } else { + var memberExpression = (MemberExpression)memberAssignment.Expression; if (memberExpression.Expression.NodeType.IsIn(ExpressionType.Constant)) { @@ -61,7 +62,7 @@ namespace SqlSugar parameter.Context.Result.Append(parameterName); this.Context.Parameters.Add(new SugarParameter(parameterName, value)); } - else + else if (memberExpression.Expression.NodeType.IsIn(ExpressionType.Parameter)) { var fieldNme = (memberExpression).Member.Name; if (isSingle) @@ -71,9 +72,13 @@ namespace SqlSugar else { var shortName = memberExpression.Expression.ToString(); - parameter.Context.Result.Append(shortName+"."+fieldNme); + parameter.Context.Result.Append(shortName + "." + fieldNme); } } + else + { + Check.ThrowNotSupportedException(memberExpression.Expression.NodeType.ToString()); + } } } } diff --git a/SqlSugar/SqlSugarClient.cs b/SqlSugar/SqlSugarClient.cs index 3d89c09ce..8dc74df9e 100644 --- a/SqlSugar/SqlSugarClient.cs +++ b/SqlSugar/SqlSugarClient.cs @@ -9,6 +9,7 @@ namespace SqlSugar /// ** description:Create database access object /// ** author:sunkaixuan /// ** date:2017/1/2 + /// ** qq:610262374 /// public partial class SqlSugarClient : SqlSugarAccessory, IDisposable { diff --git a/SqlSugar/bin/Debug/SqlSugar.dll b/SqlSugar/bin/Debug/SqlSugar.dll index a766d6191..02d7ebbbd 100644 Binary files a/SqlSugar/bin/Debug/SqlSugar.dll and b/SqlSugar/bin/Debug/SqlSugar.dll differ diff --git a/SqlSugar/bin/Debug/SqlSugar.pdb b/SqlSugar/bin/Debug/SqlSugar.pdb index 330ff5686..c979ff4b0 100644 Binary files a/SqlSugar/bin/Debug/SqlSugar.pdb and b/SqlSugar/bin/Debug/SqlSugar.pdb differ diff --git a/SqlSugar/obj/Debug/SqlSugar.dll b/SqlSugar/obj/Debug/SqlSugar.dll index a766d6191..02d7ebbbd 100644 Binary files a/SqlSugar/obj/Debug/SqlSugar.dll and b/SqlSugar/obj/Debug/SqlSugar.dll differ diff --git a/SqlSugar/obj/Debug/SqlSugar.pdb b/SqlSugar/obj/Debug/SqlSugar.pdb index 330ff5686..c979ff4b0 100644 Binary files a/SqlSugar/obj/Debug/SqlSugar.pdb and b/SqlSugar/obj/Debug/SqlSugar.pdb differ