Skip to content

Commit 63056e2

Browse files
authored
Merge pull request #2061 from 1340896123/master
feat(GlobalFilter): 添加过滤器类型支持并优化各操作过滤逻辑,使其,可以在Select,Update,Delete使用不同的过滤器
2 parents 1509714 + a023662 commit 63056e2

File tree

6 files changed

+38
-26
lines changed

6 files changed

+38
-26
lines changed

FreeSql.DbContext/Repository/ContextSet/RepositoryDbSet.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FreeSql.Extensions.EntityUtil;
2+
using FreeSql.Internal;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -32,42 +33,44 @@ protected override ISelect<TEntity> OrmSelect(object dywhere)
3233
{
3334
var select = base.OrmSelect(dywhere);
3435
if (_repo._asTablePriv != null) select.AsTable(_repo._asTablePriv);
35-
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
36+
var disableFilter = _repo.DataFilter._filtersByOrm
37+
.Where(a => a.Value.IsEnabled == false || (a.Value.Filter.FilterType & GlobalFilter.FilterType.Query) != GlobalFilter.FilterType.Query)
38+
.Select(a => a.Key).ToArray();
3639
if (disableFilter.Any()) select.DisableGlobalFilter(disableFilter);
3740
return select;
3841
}
3942
internal ISelect<TEntity> OrmSelectInternal(object dywhere) => OrmSelect(dywhere);
4043
protected override IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
4144
{
4245
var update = base.OrmUpdate(entitys);
43-
if (_repo._asTablePriv != null) update.AsTable(old => _repo._asTablePriv(_entityType, old));
44-
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
46+
if (_repo._asTablePriv != null) update.AsTable(old => _repo._asTablePriv(_entityType, old));
47+
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false || (a.Value.Filter.FilterType & GlobalFilter.FilterType.Update) != GlobalFilter.FilterType.Update).Select(a => a.Key).ToArray();
4548
if (disableFilter.Any()) update.DisableGlobalFilter(disableFilter);
4649
return update;
4750
}
4851
internal IUpdate<TEntity> OrmUpdateInternal(IEnumerable<TEntity> entitys) => OrmUpdate(entitys);
4952
protected override IDelete<TEntity> OrmDelete(object dywhere)
5053
{
5154
var delete = base.OrmDelete(dywhere);
52-
if (_repo._asTablePriv != null) delete.AsTable(old => _repo._asTablePriv(_entityType, old));
53-
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
55+
if (_repo._asTablePriv != null) delete.AsTable(old => _repo._asTablePriv(_entityType, old));
56+
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false || (a.Value.Filter.FilterType & GlobalFilter.FilterType.Delete) != GlobalFilter.FilterType.Delete).Select(a => a.Key).ToArray();
5457
if (disableFilter.Any()) delete.DisableGlobalFilter(disableFilter);
5558
return delete;
5659
}
5760
internal IDelete<TEntity> OrmDeleteInternal(object dywhere) => OrmDelete(dywhere);
5861

59-
protected override IDelete<object> OrmDeleteAsType(Type entityType)
60-
{
61-
var delete = base.OrmDeleteAsType(entityType);
62-
if (_repo._asTablePriv != null) delete.AsTable(old => _repo._asTablePriv(_entityType, old));
62+
protected override IDelete<object> OrmDeleteAsType(Type entityType)
63+
{
64+
var delete = base.OrmDeleteAsType(entityType);
65+
if (_repo._asTablePriv != null) delete.AsTable(old => _repo._asTablePriv(_entityType, old));
6366
return delete;
64-
}
67+
}
6568

66-
protected override IInsert<TEntity> OrmInsert(TEntity entity) => OrmInsert(new[] { entity });
69+
protected override IInsert<TEntity> OrmInsert(TEntity entity) => OrmInsert(new[] { entity });
6770
protected override IInsert<TEntity> OrmInsert(IEnumerable<TEntity> entitys)
6871
{
6972
var insert = base.OrmInsert(entitys);
70-
if (_repo._asTablePriv != null) insert.AsTable(old => _repo._asTablePriv(_entityType, old));
73+
if (_repo._asTablePriv != null) insert.AsTable(old => _repo._asTablePriv(_entityType, old));
7174
return insert;
7275
}
7376
internal IInsert<TEntity> OrmInsertInternal(TEntity entity) => OrmInsert(entity);

FreeSql.DbContext/Repository/Repository/BaseRepository.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public IUnitOfWork UnitOfWork
8080
get => _unitOfWork;
8181
}
8282
public IUpdate<TEntity> UpdateDiy => _dbset.OrmUpdateInternal(null);
83-
8483
public virtual ISelect<TEntity> Select => _dbset.OrmSelectInternal(null);
8584
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
8685
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);

FreeSql/Internal/CommonProvider/DeleteProvider.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public DeleteProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression co
3939
_isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure;
4040
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params));
4141
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
42-
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
42+
_whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Delete) == GlobalFilter.FilterType.Delete).ToList();
4343
}
4444

4545
protected void ClearData()
4646
{
4747
_where.Clear();
4848
_whereTimes = 0;
4949
_params.Clear();
50-
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
50+
_whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Delete) == GlobalFilter.FilterType.Delete).ToList();
5151
}
5252

5353
public IDelete<T1> WithTransaction(DbTransaction transaction)
@@ -120,21 +120,21 @@ public IDelete<T1> WhereIf(bool condition, string sql, object parms = null)
120120
public IDelete<T1> WhereDynamic(object dywhere, bool not = false) => not == false ?
121121
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params)) :
122122
this.Where($"not({_commonUtils.WhereObject(_table, "", dywhere, _params)})");
123-
public IDelete<T1> WhereDynamicFilter(DynamicFilterInfo filter)
123+
public IDelete<T1> WhereDynamicFilter(DynamicFilterInfo filter)
124124
{
125125
var alias = "t_" + Guid.NewGuid().ToString("n").Substring(0, 8);
126-
var tempQuery = _orm.Select<object>().AsType(_table.Type).DisableGlobalFilter().As(alias);
126+
var tempQuery = _orm.Select<object>().AsType(_table.Type).DisableGlobalFilter().As(alias);
127127
tempQuery.WhereDynamicFilter(filter);
128128
var where = (tempQuery as Select0Provider)._where.ToString().Replace(alias + ".", "");
129129
if (where.StartsWith(" AND "))
130130
{
131-
if (++_whereTimes == 1) _where.Append(where.Substring(5));
131+
if (++_whereTimes == 1) _where.Append(where.Substring(5));
132132
else _where.Append(where);
133133
}
134-
return this;
134+
return this;
135135
}
136136

137-
public IDelete<T1> DisableGlobalFilter(params string[] name)
137+
public IDelete<T1> DisableGlobalFilter(params string[] name)
138138
{
139139
if (_whereGlobalFilter.Any() == false) return this;
140140
if (name?.Any() != true)
@@ -219,7 +219,7 @@ public void ToSqlFetch(Action<StringBuilder> fetch)
219219
_tableRule = old => name;
220220
sb.Clear().Append("DELETE FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(newwhere);
221221
_interceptSql?.Invoke(sb);
222-
if (sb.Length > 0) fetch(sb);
222+
if (sb.Length > 0) fetch(sb);
223223
}
224224
_tableRule = oldTableRule;
225225
return;
@@ -254,7 +254,7 @@ async public Task ToSqlFetchAsync(Func<StringBuilder, Task> fetchAsync)
254254
_tableRule = old => name;
255255
sb.Clear().Append("DELETE FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append(newwhere);
256256
_interceptSql?.Invoke(sb);
257-
if (sb.Length > 0) await fetchAsync(sb);
257+
if (sb.Length > 0) await fetchAsync(sb);
258258
}
259259
_tableRule = oldTableRule;
260260
return;

FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class Select1Provider<T1> : Select0Provider<ISelect<T1>, T1>, IS
2222
{
2323
public Select1Provider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere)
2424
{
25-
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
25+
_whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Query) == GlobalFilter.FilterType.Query).ToList();
2626
}
2727

2828
protected ISelect<T1> InternalFrom(LambdaExpression lambdaExp)

FreeSql/Internal/CommonProvider/UpdateProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public UpdateProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression co
184184
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params));
185185
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
186186
IgnoreCanUpdate();
187-
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
187+
_whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Update) == GlobalFilter.FilterType.Update).ToList();
188188
_sourceOld = _source;
189189
}
190190

@@ -212,7 +212,7 @@ protected void ClearData()
212212
_params.Clear();
213213
_paramsSource.Clear();
214214
IgnoreCanUpdate();
215-
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
215+
_whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Update) == GlobalFilter.FilterType.Update).ToList();
216216
_batchProgress = null;
217217
_interceptSql = null;
218218
_tableAlias = null;

FreeSql/Internal/GlobalFilter.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ public class GlobalFilter
1212
ConcurrentDictionary<string, Item> _filters = new ConcurrentDictionary<string, Item>(StringComparer.CurrentCultureIgnoreCase);
1313
int _id = 0;
1414

15+
[Flags]
16+
public enum FilterType
17+
{
18+
Query = 1, // 2^0
19+
Update = 2, // 2^1
20+
Delete = 4 // 2^2
21+
}
22+
1523
public class Item
1624
{
25+
public FilterType FilterType { get; set; }
1726
public int Id { get; internal set; }
1827
public string Name { get; internal set; }
1928
internal Func<bool> Condition { get; set; }
@@ -67,7 +76,7 @@ public class Item
6776
/// <returns></returns>
6877
public GlobalFilter ApplyOnlyIf<TEntity>(string name, Func<bool> condition, Expression<Func<TEntity, bool>> where, bool before = false) => Apply(true, name, condition, where, before);
6978

70-
GlobalFilter Apply<TEntity>(bool only, string name, Func<bool> condition, Expression<Func<TEntity, bool>> where, bool before)
79+
GlobalFilter Apply<TEntity>(bool only, string name, Func<bool> condition, Expression<Func<TEntity, bool>> where, bool before, FilterType filterType = FilterType.Query | FilterType.Update | FilterType.Delete)
7180
{
7281
if (name == null) throw new ArgumentNullException(nameof(name));
7382
if (where == null) return this;
@@ -84,6 +93,7 @@ GlobalFilter Apply<TEntity>(bool only, string name, Func<bool> condition, Expres
8493
item.Condition = condition;
8594
item.Only = only;
8695
item.Before = before;
96+
item.FilterType = filterType;
8797
_filters.AddOrUpdate(name, item, (_, __) => item);
8898
return this;
8999
}

0 commit comments

Comments
 (0)