Browse Source

Update ConditionalModelToSql

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
31164e19d1
  1. 15
      Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs
  2. 24
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 10
      Src/Asp.Net/SqlSugar/Entities/ConditionalModel.cs
  4. 3
      Src/Asp.Net/SqlSugar/Enum/ConditionalType.cs
  5. 13
      Src/Asp.Net/SqlSugar/Enum/WhereType.cs
  6. 176
      Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs
  7. 2
      Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs
  8. 24
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs
  9. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

15
Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs

@ -37,13 +37,22 @@ namespace OrmTest.Demo
private static void ConditionalModel()
{
var db = GetInstance();
List<ConditionalModel> conModels = new List<ConditionalModel>();
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" });
List<IConditionalModel> conModels = new List<IConditionalModel>();
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<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()// (id=1 or id=2 and id=1)
{
new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
new KeyValuePair<WhereType, ConditionalModel> (WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
new KeyValuePair<WhereType, ConditionalModel> ( WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" })
}
});
var student = db.Queryable<Student>().Where(conModels).ToList();
}

24
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -122,7 +122,7 @@ namespace SqlSugar
return this;
}
public virtual ISugarQueryable<T> Where(List<ConditionalModel> conditionalModels)
public virtual ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels)
{
if (conditionalModels.IsNullOrEmpty()) return this;
var sqlObj = this.Context.Utilities.ConditionalModelToSql(conditionalModels);
@ -1240,7 +1240,7 @@ namespace SqlSugar
Where<T>(whereString, whereObj);
return this;
}
public new ISugarQueryable<T, T2> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -1567,7 +1567,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -1749,7 +1749,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -2063,7 +2063,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -2380,7 +2380,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5, T6> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5, T6> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -2687,7 +2687,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -3021,7 +3021,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -3387,7 +3387,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -3765,7 +3765,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -4179,7 +4179,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;
@ -4605,7 +4605,7 @@ namespace SqlSugar
_Where(expression);
return this;
}
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Where(List<ConditionalModel> conditionalModels)
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Where(List<IConditionalModel> conditionalModels)
{
base.Where(conditionalModels);
return this;

10
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<KeyValuePair<WhereType, ConditionalModel>> ConditionalList { get; set; }
}
public class ConditionalModel: IConditionalModel
{
public ConditionalModel()
{

3
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
}
}

13
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
}
}

176
Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs

@ -160,10 +160,12 @@ namespace SqlSugar
{
Dictionary<string, object> result = new Dictionary<string, object>();
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<string, SugarParameter[]> ConditionalModelToSql(List<ConditionalModel> models)
public KeyValuePair<string, SugarParameter[]> ConditionalModelToSql(List<IConditionalModel> models,int beginIndex=0)
{
if (models.IsNullOrEmpty()) return new KeyValuePair<string, SugarParameter[]>();
StringBuilder builder = new StringBuilder();
List<SugarParameter> parameters = new List<SugarParameter>();
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));