Skip to content

Commit

Permalink
#1108 合并master冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 20, 2022
2 parents cdc9b1c + 04ba968 commit e956d53
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>3.2.650-preview20220518</Version>
<Version>3.2.650-preview20220520</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ static void Main(string[] args)
});
sql1 = sql1.Replace("INNER JOIN ", "FULL JOIN ");

//fsql.Select<UserGroup>()
// .ToList(a => new
// {
// users1 = fsql.Select<User1>().Where(b => b.GroupId == a.Id).ToList(false),
// users2 = fsql.Select<User1>().Where(b => b.GroupId == a.Id).ToList(b => new
// {
// userid = b.Id,
// username = b.Username
// }),
// //users3 = fsql.Ado.Query<User1>("select * from user1 where groupid = @id", new { id = a.Id })
// });



fsql.UseJsonMap();
Expand Down
9 changes: 0 additions & 9 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions FreeSql.DbContext/Repository/ContextSet/RepositoryDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ protected override ISelect<TEntity> OrmSelect(object dywhere)

var filters = (_repo.DataFilter as DataFilter<TEntity>)._filters;
foreach (var filter in filters.Where(a => a.Value.IsEnabled == true)) select.Where(filter.Value.Expression);
var disableFilter = filters.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
if (disableFilter.Any()) select.DisableGlobalFilter();
var disableFilter = filters.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToList();
disableFilter.AddRange((_repo.DataFilter as DataFilter<TEntity>)._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key));
if (disableFilter.Any()) select.DisableGlobalFilter(disableFilter.ToArray());
return select;
}
internal ISelect<TEntity> OrmSelectInternal(object dywhere) => OrmSelect(dywhere);
Expand All @@ -39,8 +40,9 @@ protected override IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
throw new Exception(DbContextStrings.UpdateError_Filter(filter.Key, filter.Value.Expression, _db.OrmOriginal.GetEntityString(_entityType, entity)));
update.Where(filter.Value.Expression);
}
var disableFilter = filters.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
if (disableFilter.Any()) update.DisableGlobalFilter();
var disableFilter = filters.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToList();
disableFilter.AddRange((_repo.DataFilter as DataFilter<TEntity>)._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key));
if (disableFilter.Any()) update.DisableGlobalFilter(disableFilter.ToArray());
return update;
}
internal IUpdate<TEntity> OrmUpdateInternal(IEnumerable<TEntity> entitys) => OrmUpdate(entitys);
Expand All @@ -49,8 +51,9 @@ protected override IDelete<TEntity> OrmDelete(object dywhere)
var delete = base.OrmDelete(dywhere).AsTable(_repo.AsTableValueInternal);
var filters = (_repo.DataFilter as DataFilter<TEntity>)._filters;
foreach (var filter in filters.Where(a => a.Value.IsEnabled == true)) delete.Where(filter.Value.Expression);
var disableFilter = filters.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
if (disableFilter.Any()) delete.DisableGlobalFilter();
var disableFilter = filters.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToList();
disableFilter.AddRange((_repo.DataFilter as DataFilter<TEntity>)._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key));
if (disableFilter.Any()) delete.DisableGlobalFilter(disableFilter.ToArray());
return delete;
}
internal IDelete<TEntity> OrmDeleteInternal(object dywhere) => OrmDelete(dywhere);
Expand Down
108 changes: 100 additions & 8 deletions FreeSql.DbContext/Repository/DataFilter/DataFilter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using FreeSql.Internal;
using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace FreeSql
{
Expand Down Expand Up @@ -48,8 +49,14 @@ internal class FilterItem
public Func<TEntity, bool> ExpressionDelegate => _expressionDelegate ?? (_expressionDelegate = Expression?.Compile());
public bool IsEnabled { get; set; }
}
internal class FilterItemByOrm
{
public GlobalFilter.Item Filter { get; set; }
public bool IsEnabled { get; set; }
}

internal ConcurrentDictionary<string, FilterItem> _filters = new ConcurrentDictionary<string, FilterItem>(StringComparer.CurrentCultureIgnoreCase);
internal ConcurrentDictionary<string, FilterItemByOrm> _filtersByOrm = new ConcurrentDictionary<string, FilterItemByOrm>(StringComparer.CurrentCultureIgnoreCase);
public IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp)
{

Expand All @@ -67,6 +74,7 @@ public IDisposable Disable(params string[] filterName)
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });

List<string> restore = new List<string>();
List<string> restoreByOrm = new List<string>();
foreach (var name in filterName)
{
if (_filters.TryGetValue(name, out var tryfi))
Expand All @@ -77,12 +85,33 @@ public IDisposable Disable(params string[] filterName)
tryfi.IsEnabled = false;
}
}
if (_filtersByOrm.TryGetValue(name, out var tryfiByOrm))
{
if (tryfiByOrm.IsEnabled)
{
restoreByOrm.Add(name);
tryfiByOrm.IsEnabled = false;
}
}
}
return new UsingAny(() => this.Enable(restore.ToArray()));
return new UsingAny(() =>
{
restore.ForEach(name =>
{
if (_filters.TryGetValue(name, out var tryfi) && tryfi.IsEnabled == false)
tryfi.IsEnabled = true;
});
restoreByOrm.ForEach(name =>
{
if (_filtersByOrm.TryGetValue(name, out var tryfiByOrm) && tryfiByOrm.IsEnabled == false)
tryfiByOrm.IsEnabled = true;
});
});
}
public IDisposable DisableAll()
{
List<string> restore = new List<string>();
List<string> restoreByOrm = new List<string>();
foreach (var val in _filters)
{
if (val.Value.IsEnabled)
Expand All @@ -91,7 +120,27 @@ public IDisposable DisableAll()
val.Value.IsEnabled = false;
}
}
return new UsingAny(() => this.Enable(restore.ToArray()));
foreach (var val in _filtersByOrm)
{
if (val.Value.IsEnabled)
{
restoreByOrm.Add(val.Key);
val.Value.IsEnabled = false;
}
}
return new UsingAny(() =>
{
restore.ForEach(name =>
{
if (_filters.TryGetValue(name, out var tryfi) && tryfi.IsEnabled == false)
tryfi.IsEnabled = true;
});
restoreByOrm.ForEach(name =>
{
if (_filtersByOrm.TryGetValue(name, out var tryfiByOrm) && tryfiByOrm.IsEnabled == false)
tryfiByOrm.IsEnabled = true;
});
});
}
class UsingAny : IDisposable
{
Expand All @@ -111,6 +160,7 @@ public IDisposable Enable(params string[] filterName)
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });

List<string> restore = new List<string>();
List<string> restoreByOrm = new List<string>();
foreach (var name in filterName)
{
if (_filters.TryGetValue(name, out var tryfi))
Expand All @@ -121,12 +171,33 @@ public IDisposable Enable(params string[] filterName)
tryfi.IsEnabled = true;
}
}
if (_filtersByOrm.TryGetValue(name, out var tryfiByOrm))
{
if (tryfiByOrm.IsEnabled == false)
{
restoreByOrm.Add(name);
tryfiByOrm.IsEnabled = true;
}
}
}
return new UsingAny(() => this.Disable(restore.ToArray()));
return new UsingAny(() =>
{
restore.ForEach(name =>
{
if (_filters.TryGetValue(name, out var tryfi) && tryfi.IsEnabled == true)
tryfi.IsEnabled = false;
});
restoreByOrm.ForEach(name =>
{
if (_filtersByOrm.TryGetValue(name, out var tryfiByOrm) && tryfiByOrm.IsEnabled == true)
tryfiByOrm.IsEnabled = false;
});
});
}
public IDisposable EnableAll()
{
List<string> restore = new List<string>();
List<string> restoreByOrm = new List<string>();
foreach (var val in _filters)
{
if (val.Value.IsEnabled == false)
Expand All @@ -135,13 +206,34 @@ public IDisposable EnableAll()
val.Value.IsEnabled = true;
}
}
return new UsingAny(() => this.Disable(restore.ToArray()));
foreach (var val in _filtersByOrm)
{
if (val.Value.IsEnabled == false)
{
restoreByOrm.Add(val.Key);
val.Value.IsEnabled = true;
}
}
return new UsingAny(() =>
{
restore.ForEach(name =>
{
if (_filters.TryGetValue(name, out var tryfi) && tryfi.IsEnabled == true)
tryfi.IsEnabled = false;
});
restoreByOrm.ForEach(name =>
{
if (_filtersByOrm.TryGetValue(name, out var tryfiByOrm) && tryfiByOrm.IsEnabled == true)
tryfiByOrm.IsEnabled = false;
});
});
}

public bool IsEnabled(string filterName)
{
if (filterName == null) return false;
return _filters.TryGetValue(filterName, out var tryfi) ? tryfi.IsEnabled : false;
return _filters.TryGetValue(filterName, out var tryfi) ? tryfi.IsEnabled :
_filtersByOrm.TryGetValue(filterName, out var tryfiByOrm) ? tryfiByOrm.IsEnabled : false;
}

~DataFilter() => this.Dispose();
Expand Down
9 changes: 9 additions & 0 deletions FreeSql.DbContext/Repository/Repository/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter,
DataFilterUtil.SetRepositoryDataFilter(this, null);
DataFilter.Apply("", filter);
AsTable(asTable);

fsql.GlobalFilter.GetFilters().ForEach(gf =>
{
(DataFilter as DataFilter<TEntity>)._filtersByOrm.TryAdd(gf.Name, new DataFilter<TEntity>.FilterItemByOrm
{
Filter = gf,
IsEnabled = true
});
});
}

~BaseRepository() => this.Dispose();
Expand Down
10 changes: 10 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/UnitTest2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ public void Test02()
var gft2 = g.mysql.Select<gf_t2>().Where(a => a.id == Guid.NewGuid()).ToList();
var gft3 = g.mysql.Select<gf_t3>().Where(a => a.id == Guid.NewGuid()).ToList();

var repo1 = g.mysql.GetRepository<gf_t1, Guid>();
using (repo1.DataFilter.Disable("gft1", "gft2", "gft3"))
repo1.Get(Guid.NewGuid());
var repo2 = g.mysql.GetRepository<gf_t2, Guid>();
using (repo2.DataFilter.Disable("gft1", "gft2", "gft3"))
repo2.Get(Guid.NewGuid());
var repo3 = g.mysql.GetRepository<gf_t3, Guid>();
using (repo3.DataFilter.Disable("gft1", "gft2", "gft3"))
repo3.Get(Guid.NewGuid());

g.sqlserver.Delete<TBatInst>().Where("1=1").ExecuteAffrows();
g.mysql.Delete<TBatInst>().Where("1=1").ExecuteAffrows();
g.pgsql.Delete<TBatInst>().Where("1=1").ExecuteAffrows();
Expand Down
11 changes: 9 additions & 2 deletions FreeSql.Tests/FreeSql.Tests/UnitTest5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ namespace FreeSql.Tests
{
public class UnitTest5
{

[Fact]
public void AsTable_PropertyName_FormatErrorTest1()
public void TestConstDtoStringEmpty()
{
CoreStrings.AsTable_PropertyName_FormatError("astable");
var fsql = g.mysql;
var sql = fsql.Select<TestDto>().ToSql(a => new
{
empty = ""
});
Assert.Equal(@"SELECT '' as1
FROM `TestDto` a", sql);
}
// DTO
public class TestDto
Expand Down

0 comments on commit e956d53

Please sign in to comment.