From 31164e19d1b7a80a96c482abcf1e43250833c7a5 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 29 Jan 2018 17:15:04 +0800 Subject: [PATCH] Update ConditionalModelToSql --- Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs | 15 +- .../QueryableProvider/QueryableProvider.cs | 24 +-- .../SqlSugar/Entities/ConditionalModel.cs | 10 +- Src/Asp.Net/SqlSugar/Enum/ConditionalType.cs | 3 +- Src/Asp.Net/SqlSugar/Enum/WhereType.cs | 13 ++ .../SqlSugar/Infrastructure/ContextMethods.cs | 176 +++++++++++------- .../SqlSugar/Interface/IContextMethods.cs | 2 +- Src/Asp.Net/SqlSugar/Interface/IQueryable.cs | 24 +-- Src/Asp.Net/SqlSugar/SqlSugar.csproj | 1 + 9 files changed, 175 insertions(+), 93 deletions(-) create mode 100644 Src/Asp.Net/SqlSugar/Enum/WhereType.cs diff --git a/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs b/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs index 59d0f8422..18c808b39 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs @@ -37,13 +37,22 @@ namespace OrmTest.Demo private static void ConditionalModel() { var db = GetInstance(); - List conModels = new List(); - conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }); - conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" }); + List conModels = new List(); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1 + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" });// id like '%1%' conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNullOrEmpty }); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.In,FieldValue="1,2,3" }); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NotIn, FieldValue = "1,2,3" }); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1,2,3" }); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot,FieldValue=null});// id is not null + + conModels.Add(new ConditionalCollections() { ConditionalList=new List>()// (id=1 or id=2 and id=1) + { + new KeyValuePair( WhereType.Or ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair ( WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }) + } + }); var student = db.Queryable().Where(conModels).ToList(); } diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 9e793bfdb..a1ce4329d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -122,7 +122,7 @@ namespace SqlSugar return this; } - public virtual ISugarQueryable Where(List conditionalModels) + public virtual ISugarQueryable Where(List conditionalModels) { if (conditionalModels.IsNullOrEmpty()) return this; var sqlObj = this.Context.Utilities.ConditionalModelToSql(conditionalModels); @@ -1240,7 +1240,7 @@ namespace SqlSugar Where(whereString, whereObj); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -1567,7 +1567,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -1749,7 +1749,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -2063,7 +2063,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -2380,7 +2380,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -2687,7 +2687,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -3021,7 +3021,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -3387,7 +3387,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -3765,7 +3765,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -4179,7 +4179,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; @@ -4605,7 +4605,7 @@ namespace SqlSugar _Where(expression); return this; } - public new ISugarQueryable Where(List conditionalModels) + public new ISugarQueryable Where(List conditionalModels) { base.Where(conditionalModels); return this; diff --git a/Src/Asp.Net/SqlSugar/Entities/ConditionalModel.cs b/Src/Asp.Net/SqlSugar/Entities/ConditionalModel.cs index 5593dfb19..9bdb8ca08 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConditionalModel.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConditionalModel.cs @@ -5,7 +5,15 @@ using System.Text; namespace SqlSugar { - public class ConditionalModel + public interface IConditionalModel { + + } + public class ConditionalCollections : IConditionalModel + { + public List> ConditionalList { get; set; } + } + + public class ConditionalModel: IConditionalModel { public ConditionalModel() { diff --git a/Src/Asp.Net/SqlSugar/Enum/ConditionalType.cs b/Src/Asp.Net/SqlSugar/Enum/ConditionalType.cs index d316c0a90..fd54bdc78 100644 --- a/Src/Asp.Net/SqlSugar/Enum/ConditionalType.cs +++ b/Src/Asp.Net/SqlSugar/Enum/ConditionalType.cs @@ -18,6 +18,7 @@ namespace SqlSugar LikeLeft=8, LikeRight=9, NoEqual=10, - IsNullOrEmpty=11 + IsNullOrEmpty=11, + IsNot=12 } } diff --git a/Src/Asp.Net/SqlSugar/Enum/WhereType.cs b/Src/Asp.Net/SqlSugar/Enum/WhereType.cs new file mode 100644 index 000000000..c49492a27 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Enum/WhereType.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SqlSugar +{ + public enum WhereType + { + And=0, + Or=1 + } +} diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs index 250430549..596f7a3ed 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs @@ -160,10 +160,12 @@ namespace SqlSugar { Dictionary result = new Dictionary(); var type = item.PropertyType; - if (UtilConstants.SugarType == type) { + if (UtilConstants.SugarType == type) + { return result; } - if (type.FullName.IsCollectionsList()) { + if (type.FullName.IsCollectionsList()) + { return null; } var classProperties = type.GetProperties().ToList(); @@ -295,74 +297,122 @@ namespace SqlSugar #endregion #region Query - public KeyValuePair ConditionalModelToSql(List models) + public KeyValuePair ConditionalModelToSql(List models,int beginIndex=0) { if (models.IsNullOrEmpty()) return new KeyValuePair(); StringBuilder builder = new StringBuilder(); List parameters = new List(); var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig); - foreach (var item in models) + foreach (var model in models) { - var index = models.IndexOf(item); - var type = index == 0 ? "" : "AND"; - string temp = " {0} {1} {2} {3} "; - string parameterName = string.Format("{0}Conditional{1}{2}", sqlBuilder.SqlParameterKeyWord, item.FieldName, index); - switch (item.ConditionalType) + if (model is ConditionalModel) { - case ConditionalType.Equal: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "=", parameterName); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.Like: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName); - parameters.Add(new SugarParameter(parameterName, "%" + item.FieldValue + "%")); - break; - case ConditionalType.GreaterThan: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), ">", parameterName); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.GreaterThanOrEqual: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), ">=", parameterName); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.LessThan: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<", parameterName); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.LessThanOrEqual: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<=", parameterName); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.In: - if (item.FieldValue == null) item.FieldValue = string.Empty; - var inValue1 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")"); - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "IN", inValue1); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.NotIn: - if (item.FieldValue == null) item.FieldValue = string.Empty; - var inValue2 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")"); - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "NOT IN", inValue2); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.LikeLeft: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName); - parameters.Add(new SugarParameter(parameterName, item.FieldValue + "%")); - break; - case ConditionalType.LikeRight: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName); - parameters.Add(new SugarParameter(parameterName, "%" + item.FieldValue)); - break; - case ConditionalType.NoEqual: - builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<>", parameterName); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - case ConditionalType.IsNullOrEmpty: - builder.AppendFormat("{0} ({1}) OR ({2}) ", type, item.FieldName.ToSqlFilter() + " IS NULL ", item.FieldName.ToSqlFilter() + " = '' "); - parameters.Add(new SugarParameter(parameterName, item.FieldValue)); - break; - default: - break; + var item = model as ConditionalModel; + var index = models.IndexOf(item)+ beginIndex; + var type = index == 0 ? "" : "AND"; + if (beginIndex > 0) { + type = null; + } + string temp = " {0} {1} {2} {3} "; + string parameterName = string.Format("{0}Conditional{1}{2}", sqlBuilder.SqlParameterKeyWord, item.FieldName, index); + switch (item.ConditionalType) + { + case ConditionalType.Equal: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "=", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.Like: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName); + parameters.Add(new SugarParameter(parameterName, "%" + item.FieldValue + "%")); + break; + case ConditionalType.GreaterThan: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), ">", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.GreaterThanOrEqual: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), ">=", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.LessThan: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.LessThanOrEqual: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<=", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.In: + if (item.FieldValue == null) item.FieldValue = string.Empty; + var inValue1 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")"); + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "IN", inValue1); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.NotIn: + if (item.FieldValue == null) item.FieldValue = string.Empty; + var inValue2 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")"); + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "NOT IN", inValue2); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.LikeLeft: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue + "%")); + break; + case ConditionalType.LikeRight: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName); + parameters.Add(new SugarParameter(parameterName, "%" + item.FieldValue)); + break; + case ConditionalType.NoEqual: + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<>", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.IsNullOrEmpty: + builder.AppendFormat("{0} ({1}) OR ({2}) ", type, item.FieldName.ToSqlFilter() + " IS NULL ", item.FieldName.ToSqlFilter() + " = '' "); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + break; + case ConditionalType.IsNot: + if (item.FieldValue == null) + { + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), " IS NOT ", "NULL"); + } + else + { + builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<>", parameterName); + parameters.Add(new SugarParameter(parameterName, item.FieldValue)); + } + break; + default: + break; + } + } + else + { + var item = model as ConditionalCollections; + if (item != null) + { + foreach (var con in item.ConditionalList) + { + var index = item.ConditionalList.IndexOf(con); + var isFirst = index == 0; + var isLast = index == (item.ConditionalList.Count - 1); + if (isFirst) + { + builder.Append(" AND( "); + } + List conModels = new List(); + conModels.Add(con.Value); + var childSqlInfo = ConditionalModelToSql(conModels,1000*(1+index)); + builder.Append(childSqlInfo.Key); + parameters.AddRange(childSqlInfo.Value); + if (isLast) + { + builder.Append(" ) "); + } + else { + + builder.AppendFormat(" {0} ", con.Key.ToString().ToUpper()); + } + } + } } } return new KeyValuePair(builder.ToString(), parameters.ToArray()); diff --git a/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs b/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs index a4dd8079d..046e58dda 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs @@ -23,7 +23,7 @@ namespace SqlSugar void RemoveCacheAll(); void RemoveCacheAll(); void RemoveCache(string key); - KeyValuePair ConditionalModelToSql(List models); + KeyValuePair ConditionalModelToSql(List models,int beginIndex=0); } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index be78ba5b0..6607f3364 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -26,7 +26,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(string whereString, object whereObj = null); - ISugarQueryable Where(List conditionalModels); + ISugarQueryable Where(List conditionalModels); ISugarQueryable Having(Expression> expression); ISugarQueryable Having(string whereString, object whereObj = null); @@ -132,7 +132,7 @@ namespace SqlSugar #region Where new ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -194,7 +194,7 @@ namespace SqlSugar new ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -262,7 +262,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -337,7 +337,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -414,7 +414,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -489,7 +489,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -569,7 +569,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -656,7 +656,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -746,7 +746,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -841,7 +841,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); @@ -941,7 +941,7 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - new ISugarQueryable Where(List conditionalModels); + new ISugarQueryable Where(List conditionalModels); new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index e85903075..7bf6918df 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -81,6 +81,7 @@ +