Add Filter

This commit is contained in:
sunkaixuan 2017-06-09 20:44:59 +08:00
parent c9424cb49d
commit 4ef318a113
13 changed files with 121 additions and 7 deletions

View File

@ -0,0 +1,21 @@
using OrmTest.Demo;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demos
{
namespace OrmTest.Demo
{
public class Filter : DemoBase
{
public static void Init()
{
var db = GetInstance();
// db.QueryFilter.Add(eSqlFilterItem(){ });
}
}
}
}

View File

@ -55,6 +55,9 @@ namespace OrmTest.Demo
//Rename
db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommand();
//Update Student set Name='jack' Where Id=1
//Column is null no update
db.Updateable(updateObj).Where(true).ExecuteCommand();
}
}
}

View File

@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Demos\Filter.cs" />
<Compile Include="Demos\JoinSql.cs" />
<Compile Include="Demos\DbFirst.cs" />
<Compile Include="Demos\Delete.cs" />

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class QueryFilterProvider
{
internal SqlSugarClient Context{ get; set; }
private List<SqlFilterItem> _Filters { get; set; }
public void Add(SqlFilterItem filter)
{
if (_Filters == null)
_Filters = new List<SqlFilterItem>();
}
public void Remove(string filterName)
{
if (_Filters == null)
_Filters = new List<SqlFilterItem>();
_Filters.RemoveAll(it => it.FilterName == filterName);
}
public List<SqlFilterItem> GeFilterList
{
get
{
if (_Filters == null)
_Filters = new List<SqlFilterItem>();
return _Filters;
}
}
public void Clear()
{
_Filters = new List<SqlFilterItem>();
}
}
}

View File

@ -96,12 +96,12 @@ namespace SqlSugar
return this;
}
public IInsertable<T> Where(bool isInsertNull, bool isOffIdentity = false)
public IInsertable<T> Where(bool isNoInsertNull, bool isOffIdentity = false)
{
this.IsOffIdentity = IsOffIdentity;
if (this.InsertBuilder.LambdaExpressions == null)
this.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig);
this.InsertBuilder.IsInsertNull = isInsertNull;
this.InsertBuilder.IsNoInsertNull = isNoInsertNull;
return this;
}
#endregion
@ -139,7 +139,7 @@ namespace SqlSugar
{
if (this.InsertBuilder.Parameters == null) this.InsertBuilder.Parameters = new List<SugarParameter>();
var paramters = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType);
if (InsertBuilder.IsInsertNull && paramters.Value == null)
if (InsertBuilder.IsNoInsertNull && paramters.Value == null)
{
continue;
}

View File

@ -21,7 +21,7 @@ namespace SqlSugar
public List<SugarParameter> Parameters { get; set; }
public string TableWithString { get; set; }
public List<DbColumnInfo> DbColumnInfoList { get; set; }
public bool IsInsertNull { get; set; }
public bool IsNoInsertNull { get; set; }
public bool IsReturnIdentity { get; set; }
public EntityInfo EntityInfo { get; set; }
@ -100,7 +100,7 @@ namespace SqlSugar
}
public virtual string ToSqlString()
{
if (IsInsertNull)
if (IsNoInsertNull)
{
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
}

View File

@ -26,7 +26,7 @@ namespace SqlSugar
public List<DbColumnInfo> DbColumnInfoList { get; set; }
public List<string> WhereValues { get; set; }
public List<KeyValuePair<string, string>> SetValues { get; set; }
public bool IsUpdateNull { get; set; }
public bool IsNoUpdateNull { get; set; }
public List<string> PrimaryKeys { get; set; }
public bool IsOffIdentity { get; set; }
@ -126,6 +126,10 @@ namespace SqlSugar
}
public virtual string ToSqlString()
{
if (IsNoUpdateNull)
{
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
}
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
var isSingle = groupList.Count() == 1;
if (isSingle)

View File

@ -122,6 +122,9 @@ namespace SqlSugar
public IUpdateable<T> Where(bool isUpdateNull, bool IsOffIdentity = false)
{
UpdateBuilder.IsOffIdentity = IsOffIdentity;
if (this.UpdateBuilder.LambdaExpressions == null)
this.UpdateBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig);
this.UpdateBuilder.IsNoUpdateNull = isUpdateNull;
return this;
}
public IUpdateable<T> Where(Expression<Func<T, bool>> expression)

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class SqlFilterItem
{
public string FilterName { get; set; }
public Func<SqlSugarClient,SqlFilterResult> GetFilterSql { get; set; }
public bool IsJoinQuery { get; set; }
}
public class SqlFilterResult
{
public string Sql { get; set; }
public List<SugarParameter> Parameters { get; set; }
}
}

View File

@ -12,7 +12,7 @@ namespace SqlSugar
int ExecuteCommand();
IUpdateable<T> AS(string tableName);
IUpdateable<T> With(string lockString);
IUpdateable<T> Where(bool isUpdateNull,bool IsOffIdentity = false);
IUpdateable<T> Where(bool isNoUpdateNull,bool IsOffIdentity = false);
IUpdateable<T> Where(Expression<Func<T, bool>> expression);
IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns);
IUpdateable<T> UpdateColumns(Func<string, bool> updateColumMethod);

View File

@ -60,10 +60,12 @@
<Compile Include="Abstract\DbMaintenanceProvider\Properties.cs" />
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
<Compile Include="Abstract\EntityProvider\EntityProvider.cs" />
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
<Compile Include="Entities\SchemaInfo.cs" />
<Compile Include="Entities\SqlFilter.cs" />
<Compile Include="Enum\DbObjectType.cs" />
<Compile Include="Enum\ProperyType.cs" />
<Compile Include="ExpressionsToSql\Common\FileHeper.cs" />

View File

@ -26,6 +26,7 @@ namespace SqlSugar
protected ILambdaExpressions _LambdaExpressions;
protected IRewritableMethods _RewritableMethods;
protected IDbMaintenance _DbMaintenance;
protected QueryFilterProvider _QueryFilterProvider;
#endregion
#region Init mppingInfo

View File

@ -283,6 +283,25 @@ namespace SqlSugar
}
#endregion
#region Gobal Filter
public QueryFilterProvider QueryFilter
{
get
{
if (base._QueryFilterProvider == null)
{
base._QueryFilterProvider = new QueryFilterProvider();
base._QueryFilterProvider.Context = this;
}
return _QueryFilterProvider;
}
set
{
base._QueryFilterProvider = value;
}
}
#endregion
#region Dispose OR Close
public virtual void Close()
{