Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
20869af172
  1. 58
      SqlServerTest/Demos/Filter.cs
  2. 1
      SqlServerTest/Program.cs
  3. 15
      SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  4. 17
      SqlSugar/Abstract/SqlBuilderProvider/Detail/QueryBuilder.cs
  5. 2
      SqlSugar/Entities/SqlFilter.cs
  6. 1
      SqlSugar/Interface/IQueryable.cs

58
SqlServerTest/Demos/Filter.cs

@ -6,48 +6,46 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demos
namespace OrmTest.Demo
{
namespace OrmTest.Demo
public class Filter : DemoBase
{
public class Filter : DemoBase
public static void Init()
{
public static void Init()
{
//gobal filter
var db = GetInstance1();
//gobal filter
var db = GetInstance1();
var sql = db.Queryable<Student>().ToSql();
var sql = db.Queryable<Student>().ToSql();
var sql2 = db.Queryable<Student, School>((f, s) => new object[] { JoinType.Left, f.SchoolId == s.Id }).ToSql();
var sql2 = db.Queryable<Student, School>((f, s) => new object[] { JoinType.Left, f.SchoolId == s.Id }).ToSql();
//Specify name filter
//Specify name filter
}
}
public static SqlSugarClient GetInstance1()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.QueryFilter
.Add(new SqlFilterItem()
public static SqlSugarClient GetInstance1()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.QueryFilter
.Add(new SqlFilterItem()
{
FilterValue = filterDb =>
{
FilterValue = filterDb =>
{
return new SqlFilterResult() { Sql = " isDelete=0", Parameters = new List<SugarParameter>() { } };
},
IsJoinQuery = false
}).Add(new SqlFilterItem()
return new SqlFilterResult() { Sql = " isDelete=0", Parameters = new { id=1} };
},
IsJoinQuery = false
}).Add(new SqlFilterItem()
{
FilterValue = filterDb =>
{
FilterValue = filterDb =>
{
return new SqlFilterResult() { Sql = " f.isDelete=0", Parameters = new List<SugarParameter>() { } };
},
IsJoinQuery = true
});
return db;
}
return new SqlFilterResult() { Sql = " f.isDelete=0", Parameters = new { id=1 } };
},
IsJoinQuery = true
});
return db;
}
}
}

1
SqlServerTest/Program.cs

@ -40,6 +40,7 @@ namespace OrmTest
OrmTest.Demo.Update.Init();
OrmTest.Demo.DbFirst.Init();
OrmTest.Demo.JoinSql.Init();
OrmTest.Demo.Filter.Init();
}
}
}

15
SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -62,6 +62,21 @@ namespace SqlSugar
return this;
}
public ISugarQueryable<T> Filter(string FilterName,bool isDisabledGobalFilter = false)
{
QueryBuilder.IsDisabledGobalFilter = isDisabledGobalFilter;
if (this.Context.QueryFilter.GeFilterList.IsValuable()&&FilterName.IsValuable())
{
var list = this.Context.QueryFilter.GeFilterList.Where(it => it.FilterName == FilterName&&it.IsJoinQuery == !QueryBuilder.IsSingle());
foreach (var item in list)
{
var filterResult = item.FilterValue(this.Context);
Where(SqlBuilder.AppendWhereOrAnd(QueryBuilder.WhereInfos.IsNullOrEmpty(),filterResult.Sql), filterResult.Parameters);
}
}
return this;
}
public ISugarQueryable<T> AddParameters(object parameters)
{
if (parameters != null)

17
SqlSugar/Abstract/SqlBuilderProvider/Detail/QueryBuilder.cs

@ -43,6 +43,7 @@ namespace SqlSugar
public string GroupByValue { get; set; }
public int WhereIndex { get; set; }
public int JoinIndex { get; set; }
public bool IsDisabledGobalFilter { get; set; }
public virtual List<SugarParameter> Parameters { get; set; }
public virtual List<JoinQueryInfo> JoinQueryInfos
{
@ -198,6 +199,20 @@ namespace SqlSugar
}
public virtual string ToSqlString()
{
if (!IsDisabledGobalFilter && this.Context.QueryFilter.GeFilterList.IsValuable())
{
var gobalFilterList = this.Context.QueryFilter.GeFilterList.Where(it => it.FilterName.IsNullOrEmpty()).ToList();
foreach (var item in gobalFilterList.Where(it => it.IsJoinQuery == !IsSingle()))
{
var filterResult = item.FilterValue(this.Context);
WhereInfos.Add(this.Builder.AppendWhereOrAnd(this.WhereInfos.IsNullOrEmpty(),filterResult.Sql));
var filterParamters = this.Context.Ado.GetParameters(filterResult.Parameters);
if (filterParamters.IsValuable())
{
this.Parameters.AddRange(filterParamters);
}
}
}
sql = new StringBuilder();
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
if (IsCount) { return sql.ToString(); }
@ -214,7 +229,7 @@ namespace SqlSugar
else if (Skip != null && Take != null)
{
if (this.OrderByValue == null) this.OrderByValue = " Order By GetDate() ";
return string.Format(PageTempalte, sql.ToString(), GetOrderByString, Skip.ObjToInt() + 1, Skip.ObjToInt() +Take.ObjToInt());
return string.Format(PageTempalte, sql.ToString(), GetOrderByString, Skip.ObjToInt() + 1, Skip.ObjToInt() + Take.ObjToInt());
}
else
{

2
SqlSugar/Entities/SqlFilter.cs

@ -21,6 +21,6 @@ namespace SqlSugar
public class SqlFilterResult
{
public string Sql { get; set; }
public List<SugarParameter> Parameters { get; set; }
public object Parameters { get; set; }
}
}

1
SqlSugar/Interface/IQueryable.cs

@ -15,6 +15,7 @@ namespace SqlSugar
ISugarQueryable<T> AS<T2>(string tableName);
ISugarQueryable<T> AS(string tableName);
ISugarQueryable<T> With(string withString);
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter= false);
ISugarQueryable<T> AddParameters(object pars);
ISugarQueryable<T> AddParameters(SugarParameter[] pars);
ISugarQueryable<T> AddJoinInfo(string tableName, string shortName, string joinWhere, JoinType type = JoinType.Left);

Loading…
Cancel
Save