Skip to content

Commit

Permalink
- 修复 未发布功能WhereObject/WhereItems 单条参数化;- 恢复 Repository.DataFilter Ena…
Browse files Browse the repository at this point in the history
…ble/Disable 功能;
  • Loading branch information
2881099 committed Aug 18, 2024
1 parent 1bc77db commit d3904ff
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 49 deletions.
91 changes: 47 additions & 44 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,9 @@ static void Main(string[] args)
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
//.UseQuoteSqlName(false)

.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")

//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
//.UseAdoConnectionPool(false)
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
////.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=toc;Pooling=true;Maximum Pool Size=2")
Expand Down Expand Up @@ -620,6 +620,51 @@ static void Main(string[] args)
#endregion


var usergroupRepository = fsql.GetAggregateRootRepository<UserGroup>();
usergroupRepository.Delete(a => true);
usergroupRepository.Insert(new[]{
new UserGroup
{
CreateTime = DateTime.Now,
GroupName = "group1",
UpdateTime = DateTime.Now,
Sort = 1,
User1s = new List<User1>
{
new User1 { Nickname = "nickname11", Username = "username11", Description = "desc11" },
new User1 { Nickname = "nickname12", Username = "username12", Description = "desc12" },
new User1 { Nickname = "nickname13", Username = "username13", Description = "desc13" },
}
},
new UserGroup
{
CreateTime = DateTime.Now,
GroupName = "group2",
UpdateTime = DateTime.Now,
Sort = 2,
User1s = new List<User1>
{
new User1 { Nickname = "nickname21", Username = "username21", Description = "desc21" },
new User1 { Nickname = "nickname22", Username = "username22", Description = "desc22" },
new User1 { Nickname = "nickname23", Username = "username23", Description = "desc23" },
}
},
});
var ugroupFirst = usergroupRepository.Select.First();
ugroupFirst.Sort++;
usergroupRepository.Update(ugroupFirst);
var userRepository = fsql.GetAggregateRootRepository<User1>();

var testsublist1 = fsql.Select<UserGroup>()
.First(a => new
{
a.Id,
list = userRepository.Select.Where(b => b.GroupId == a.Id).ToList(),
list2 = userRepository.Select.Where(b => b.GroupId == a.Id).ToList(b => b.Nickname),
});



FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(DateTimeOffset), new DateTimeOffsetTypeHandler());

fsql.Insert(new Account { Name = DateTime.Now.ToString(), Join = DateTimeOffset.Now }).ExecuteAffrows();
Expand Down Expand Up @@ -1297,48 +1342,6 @@ static void Main(string[] args)



var usergroupRepository = fsql.GetAggregateRootRepository<UserGroup>();
usergroupRepository.Delete(a => true);
usergroupRepository.Insert(new[]{
new UserGroup
{
CreateTime = DateTime.Now,
GroupName = "group1",
UpdateTime = DateTime.Now,
Sort = 1,
User1s = new List<User1>
{
new User1 { Nickname = "nickname11", Username = "username11", Description = "desc11" },
new User1 { Nickname = "nickname12", Username = "username12", Description = "desc12" },
new User1 { Nickname = "nickname13", Username = "username13", Description = "desc13" },
}
},
new UserGroup
{
CreateTime = DateTime.Now,
GroupName = "group2",
UpdateTime = DateTime.Now,
Sort = 2,
User1s = new List<User1>
{
new User1 { Nickname = "nickname21", Username = "username21", Description = "desc21" },
new User1 { Nickname = "nickname22", Username = "username22", Description = "desc22" },
new User1 { Nickname = "nickname23", Username = "username23", Description = "desc23" },
}
},
});
var ugroupFirst = usergroupRepository.Select.First();
ugroupFirst.Sort++;
usergroupRepository.Update(ugroupFirst);
var userRepository = fsql.GetAggregateRootRepository<User1>();

var testsublist1 = fsql.Select<UserGroup>()
.First(a => new
{
a.Id,
list = userRepository.Select.Where(b => b.GroupId == a.Id).ToList(),
list2 = userRepository.Select.Where(b => b.GroupId == a.Id).ToList(b => b.Nickname),
});

//fsql.CodeFirst.GetTableByEntity(typeof(User1)).Columns.Values.ToList().ForEach(col =>
//{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public DbContextOptions DbContextOptions
_repository.DbContextOptions.EnableCascadeSave = false;
}
}
public RepositoryDataFilter DataFilter => _repository.DataFilter;
public void AsType(Type entityType) => _repository.AsType(entityType);
Func<Type, string, string> _asTableRule;
public void AsTable(Func<string, string> rule)
Expand Down Expand Up @@ -130,6 +131,7 @@ IBaseRepository<object> GetChildRepository(Type type)
repo.UnitOfWork = UnitOfWork;
repo.DbContextOptions = DbContextOptions;
repo.DbContextOptions.EnableCascadeSave = false;
(repo as BaseRepository<object>).DataFilter = DataFilter;
repo.AsTable(_asTableRule);
return repo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ async Task<int> DeleteWithinBoundaryAsync(IEnumerable<TEntity> entitys, List<obj
var affrows = 0;
for (var a = tracking.DeleteLog.Count - 1; a >= 0; a--)
{
if (tracking.DeleteLog[a].Item2.Any() == false) continue;
var delete = Orm.Delete<object>().AsType(tracking.DeleteLog[a].Item1);
if (_asTableRule != null) delete.AsTable(old => _asTableRule(tracking.DeleteLog[a].Item1, old));
affrows += await delete.WhereDynamic(tracking.DeleteLog[a].Item2).ExecuteAffrowsAsync(cancellationToken);
Expand Down Expand Up @@ -240,6 +241,7 @@ async Task<int> SaveTrackingChangeAsync(AggregateRootTrackingChangeInfo tracking

for (var a = tracking.DeleteLog.Count - 1; a >= 0; a--)
{
if (tracking.DeleteLog[a].Item2.Any() == false) continue;
var delete = Orm.Delete<object>().AsType(tracking.DeleteLog[a].Item1);
if (_asTableRule != null) delete.AsTable(old => _asTableRule(tracking.DeleteLog[a].Item1, old));
affrows += await delete.WhereDynamic(tracking.DeleteLog[a].Item2).ExecuteAffrowsAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ int DeleteWithinBoundary(IEnumerable<TEntity> entitys, List<object> deletedOutpu
var affrows = 0;
for (var a = tracking.DeleteLog.Count - 1; a >= 0; a--)
{
if (tracking.DeleteLog[a].Item2.Any() == false) continue;
var delete = Orm.Delete<object>().AsType(tracking.DeleteLog[a].Item1);
if (_asTableRule != null) delete.AsTable(old => _asTableRule(tracking.DeleteLog[a].Item1, old));
affrows += delete.WhereDynamic(tracking.DeleteLog[a].Item2).ExecuteAffrows();
Expand Down Expand Up @@ -290,7 +291,8 @@ int SaveTrackingChange(AggregateRootTrackingChangeInfo tracking)
}

for (var a = tracking.DeleteLog.Count - 1; a >= 0; a--)
{
{
if (tracking.DeleteLog[a].Item2.Any() == false) continue;
var delete = Orm.Delete<object>().AsType(tracking.DeleteLog[a].Item1);
if (_asTableRule != null) delete.AsTable(old => _asTableRule(tracking.DeleteLog[a].Item1, old));
affrows += delete.WhereDynamic(tracking.DeleteLog[a].Item2).ExecuteAffrows();
Expand Down
31 changes: 31 additions & 0 deletions FreeSql.DbContext/FreeSql.DbContext.xml

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

6 changes: 6 additions & 0 deletions FreeSql.DbContext/Repository/ContextSet/RepositoryDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ protected override ISelect<TEntity> OrmSelect(object dywhere)
{
var select = base.OrmSelect(dywhere);
if (_repo._asTablePriv != null) select.AsTable(_repo._asTablePriv);
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
if (disableFilter.Any()) select.DisableGlobalFilter(disableFilter);
return select;
}
internal ISelect<TEntity> OrmSelectInternal(object dywhere) => OrmSelect(dywhere);
protected override IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
{
var update = base.OrmUpdate(entitys);
if (_repo._asTablePriv != null) update.AsTable(old => _repo._asTablePriv(_entityType, old));
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
if (disableFilter.Any()) update.DisableGlobalFilter(disableFilter);
return update;
}
internal IUpdate<TEntity> OrmUpdateInternal(IEnumerable<TEntity> entitys) => OrmUpdate(entitys);
protected override IDelete<TEntity> OrmDelete(object dywhere)
{
var delete = base.OrmDelete(dywhere);
if (_repo._asTablePriv != null) delete.AsTable(old => _repo._asTablePriv(_entityType, old));
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray();
if (disableFilter.Any()) delete.DisableGlobalFilter(disableFilter);
return delete;
}
internal IDelete<TEntity> OrmDeleteInternal(object dywhere) => OrmDelete(dywhere);
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 @@ -22,6 +22,14 @@ public abstract partial class BaseRepository<TEntity> : IBaseRepository<TEntity>
protected BaseRepository(IFreeSql fsql)
{
_ormScoped = DbContextScopedFreeSql.Create(fsql, () => _db, () => UnitOfWork);
fsql?.GlobalFilter?.GetAllFilters().ForEach(gf =>
{
DataFilter._filtersByOrm.TryAdd(gf.Name, new RepositoryDataFilter.FilterItemByOrm
{
Filter = gf,
IsEnabled = true
});
});
}

~BaseRepository() => this.Dispose();
Expand Down Expand Up @@ -55,6 +63,7 @@ public void AsTable(Func<Type, string, string> rule)
_asTablePriv = rule;
}
public DbContextOptions DbContextOptions { get => _db.Options; set => _db.Options = value; }
public RepositoryDataFilter DataFilter { get; set; } = new RepositoryDataFilter();

internal DbContextScopedFreeSql _ormScoped;
internal IFreeSql OrmOriginal => _ormScoped?._originalFsql;
Expand Down
Loading

0 comments on commit d3904ff

Please sign in to comment.