mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 23:13:42 +08:00
Add Filter
This commit is contained in:
parent
c9424cb49d
commit
4ef318a113
21
SqlServerTest/Demos/Filter.cs
Normal file
21
SqlServerTest/Demos/Filter.cs
Normal 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(){ });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -55,6 +55,9 @@ namespace OrmTest.Demo
|
|||||||
//Rename
|
//Rename
|
||||||
db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommand();
|
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
|
//Update Student set Name='jack' Where Id=1
|
||||||
|
|
||||||
|
//Column is null no update
|
||||||
|
db.Updateable(updateObj).Where(true).ExecuteCommand();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Config.cs" />
|
<Compile Include="Config.cs" />
|
||||||
|
<Compile Include="Demos\Filter.cs" />
|
||||||
<Compile Include="Demos\JoinSql.cs" />
|
<Compile Include="Demos\JoinSql.cs" />
|
||||||
<Compile Include="Demos\DbFirst.cs" />
|
<Compile Include="Demos\DbFirst.cs" />
|
||||||
<Compile Include="Demos\Delete.cs" />
|
<Compile Include="Demos\Delete.cs" />
|
||||||
|
40
SqlSugar/Abstract/FilterProvider/FilterProvider.cs
Normal file
40
SqlSugar/Abstract/FilterProvider/FilterProvider.cs
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -96,12 +96,12 @@ namespace SqlSugar
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IInsertable<T> Where(bool isInsertNull, bool isOffIdentity = false)
|
public IInsertable<T> Where(bool isNoInsertNull, bool isOffIdentity = false)
|
||||||
{
|
{
|
||||||
this.IsOffIdentity = IsOffIdentity;
|
this.IsOffIdentity = IsOffIdentity;
|
||||||
if (this.InsertBuilder.LambdaExpressions == null)
|
if (this.InsertBuilder.LambdaExpressions == null)
|
||||||
this.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig);
|
this.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig);
|
||||||
this.InsertBuilder.IsInsertNull = isInsertNull;
|
this.InsertBuilder.IsNoInsertNull = isNoInsertNull;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@ -139,7 +139,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
if (this.InsertBuilder.Parameters == null) this.InsertBuilder.Parameters = new List<SugarParameter>();
|
if (this.InsertBuilder.Parameters == null) this.InsertBuilder.Parameters = new List<SugarParameter>();
|
||||||
var paramters = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType);
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ namespace SqlSugar
|
|||||||
public List<SugarParameter> Parameters { get; set; }
|
public List<SugarParameter> Parameters { get; set; }
|
||||||
public string TableWithString { get; set; }
|
public string TableWithString { get; set; }
|
||||||
public List<DbColumnInfo> DbColumnInfoList { get; set; }
|
public List<DbColumnInfo> DbColumnInfoList { get; set; }
|
||||||
public bool IsInsertNull { get; set; }
|
public bool IsNoInsertNull { get; set; }
|
||||||
public bool IsReturnIdentity { get; set; }
|
public bool IsReturnIdentity { get; set; }
|
||||||
public EntityInfo EntityInfo { get; set; }
|
public EntityInfo EntityInfo { get; set; }
|
||||||
|
|
||||||
@ -100,7 +100,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
public virtual string ToSqlString()
|
public virtual string ToSqlString()
|
||||||
{
|
{
|
||||||
if (IsInsertNull)
|
if (IsNoInsertNull)
|
||||||
{
|
{
|
||||||
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
|
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ namespace SqlSugar
|
|||||||
public List<DbColumnInfo> DbColumnInfoList { get; set; }
|
public List<DbColumnInfo> DbColumnInfoList { get; set; }
|
||||||
public List<string> WhereValues { get; set; }
|
public List<string> WhereValues { get; set; }
|
||||||
public List<KeyValuePair<string, string>> SetValues { 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 List<string> PrimaryKeys { get; set; }
|
||||||
public bool IsOffIdentity { get; set; }
|
public bool IsOffIdentity { get; set; }
|
||||||
|
|
||||||
@ -126,6 +126,10 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
public virtual string ToSqlString()
|
public virtual string ToSqlString()
|
||||||
{
|
{
|
||||||
|
if (IsNoUpdateNull)
|
||||||
|
{
|
||||||
|
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
|
||||||
|
}
|
||||||
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
||||||
var isSingle = groupList.Count() == 1;
|
var isSingle = groupList.Count() == 1;
|
||||||
if (isSingle)
|
if (isSingle)
|
||||||
|
@ -122,6 +122,9 @@ namespace SqlSugar
|
|||||||
public IUpdateable<T> Where(bool isUpdateNull, bool IsOffIdentity = false)
|
public IUpdateable<T> Where(bool isUpdateNull, bool IsOffIdentity = false)
|
||||||
{
|
{
|
||||||
UpdateBuilder.IsOffIdentity = IsOffIdentity;
|
UpdateBuilder.IsOffIdentity = IsOffIdentity;
|
||||||
|
if (this.UpdateBuilder.LambdaExpressions == null)
|
||||||
|
this.UpdateBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig);
|
||||||
|
this.UpdateBuilder.IsNoUpdateNull = isUpdateNull;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public IUpdateable<T> Where(Expression<Func<T, bool>> expression)
|
public IUpdateable<T> Where(Expression<Func<T, bool>> expression)
|
||||||
|
20
SqlSugar/Entities/SqlFilter.cs
Normal file
20
SqlSugar/Entities/SqlFilter.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,7 @@ namespace SqlSugar
|
|||||||
int ExecuteCommand();
|
int ExecuteCommand();
|
||||||
IUpdateable<T> AS(string tableName);
|
IUpdateable<T> AS(string tableName);
|
||||||
IUpdateable<T> With(string lockString);
|
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> Where(Expression<Func<T, bool>> expression);
|
||||||
IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns);
|
IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns);
|
||||||
IUpdateable<T> UpdateColumns(Func<string, bool> updateColumMethod);
|
IUpdateable<T> UpdateColumns(Func<string, bool> updateColumMethod);
|
||||||
|
@ -60,10 +60,12 @@
|
|||||||
<Compile Include="Abstract\DbMaintenanceProvider\Properties.cs" />
|
<Compile Include="Abstract\DbMaintenanceProvider\Properties.cs" />
|
||||||
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
|
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
|
||||||
<Compile Include="Abstract\EntityProvider\EntityProvider.cs" />
|
<Compile Include="Abstract\EntityProvider\EntityProvider.cs" />
|
||||||
|
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||||
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
|
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
|
||||||
<Compile Include="Entities\SchemaInfo.cs" />
|
<Compile Include="Entities\SchemaInfo.cs" />
|
||||||
|
<Compile Include="Entities\SqlFilter.cs" />
|
||||||
<Compile Include="Enum\DbObjectType.cs" />
|
<Compile Include="Enum\DbObjectType.cs" />
|
||||||
<Compile Include="Enum\ProperyType.cs" />
|
<Compile Include="Enum\ProperyType.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Common\FileHeper.cs" />
|
<Compile Include="ExpressionsToSql\Common\FileHeper.cs" />
|
||||||
|
@ -26,6 +26,7 @@ namespace SqlSugar
|
|||||||
protected ILambdaExpressions _LambdaExpressions;
|
protected ILambdaExpressions _LambdaExpressions;
|
||||||
protected IRewritableMethods _RewritableMethods;
|
protected IRewritableMethods _RewritableMethods;
|
||||||
protected IDbMaintenance _DbMaintenance;
|
protected IDbMaintenance _DbMaintenance;
|
||||||
|
protected QueryFilterProvider _QueryFilterProvider;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Init mppingInfo
|
#region Init mppingInfo
|
||||||
|
@ -283,6 +283,25 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
#endregion
|
#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
|
#region Dispose OR Close
|
||||||
public virtual void Close()
|
public virtual void Close()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user