mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
v0.0.11 实现分表
This commit is contained in:
@ -11,29 +11,33 @@ namespace FreeSql {
|
||||
protected IFreeSql _fsql;
|
||||
protected Expression<Func<TEntity, bool>> _filter;
|
||||
protected Func<TEntity, bool> _filterCompile;
|
||||
protected Func<string, string> _asTable;
|
||||
protected Func<Type, string, string> _asTableSelect => _asTable == null ? null : new Func<Type, string, string>((a, b) => a == _entityType ? _asTable(b) : null);
|
||||
protected Type _entityType = typeof(TEntity);
|
||||
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base() {
|
||||
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base() {
|
||||
_fsql = fsql ?? throw new NullReferenceException("fsql 参数不可为空");
|
||||
_filter = filter;
|
||||
_filterCompile = filter?.Compile();
|
||||
_asTable = asTable;
|
||||
}
|
||||
|
||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(_filter);
|
||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(_filter).AsTable(_asTableSelect);
|
||||
|
||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(_filter);
|
||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(_filter).AsTable(_asTable);
|
||||
|
||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).ExecuteAffrows();
|
||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).AsTable(_asTable).ExecuteAffrows();
|
||||
|
||||
public int Delete(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).ExecuteAffrows();
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).AsTable(_asTable).ExecuteAffrows();
|
||||
}
|
||||
|
||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).AsTable(_asTable).ExecuteAffrowsAsync();
|
||||
|
||||
public Task<int> DeleteAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).ExecuteAffrowsAsync();
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual TEntity Insert(TEntity entity) {
|
||||
@ -41,7 +45,7 @@ namespace FreeSql {
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInserted().FirstOrDefault();
|
||||
return _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteInserted().FirstOrDefault();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@ -55,7 +59,7 @@ namespace FreeSql {
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).ExecuteInserted();
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(_asTable).ExecuteInserted();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@ -69,7 +73,7 @@ namespace FreeSql {
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return (await _fsql.Insert<TEntity>().AppendData(entity).ExecuteInsertedAsync()).FirstOrDefault();
|
||||
return (await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteInsertedAsync()).FirstOrDefault();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@ -83,7 +87,7 @@ namespace FreeSql {
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).ExecuteInsertedAsync();
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(_asTable).ExecuteInsertedAsync();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@ -94,12 +98,12 @@ namespace FreeSql {
|
||||
|
||||
public int Update(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).ExecuteAffrows();
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).AsTable(_asTable).ExecuteAffrows();
|
||||
}
|
||||
|
||||
public Task<int> UpdateAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).ExecuteAffrowsAsync();
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
protected void ValidatorEntityAndThrow(TEntity entity) => ValidatorEntityAndThrow(new[] { entity });
|
||||
@ -113,16 +117,16 @@ namespace FreeSql {
|
||||
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter) {
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
|
||||
}
|
||||
|
||||
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).ExecuteAffrows();
|
||||
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).AsTable(_asTable).ExecuteAffrows();
|
||||
|
||||
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
|
||||
|
||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).ToOne();
|
||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).AsTable(_asTableSelect).ToOne();
|
||||
|
||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).ToOneAsync();
|
||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).AsTable(_asTableSelect).ToOneAsync();
|
||||
|
||||
public TEntity Get(TKey id) => Find(id);
|
||||
|
||||
|
@ -9,7 +9,7 @@ namespace FreeSql {
|
||||
BaseRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter) {
|
||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>0.1.10</Version>
|
||||
<Version>0.1.11</Version>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 通用仓库层实现,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>
|
||||
<Description>FreeSql 通用仓库层实现,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite,读写分离、分表分库。</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||
<PackageTags>FreeSql ORM Repository</PackageTags>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
|
@ -10,31 +10,27 @@ namespace FreeSql {
|
||||
BaseRepository<TEntity, Guid>
|
||||
where TEntity : class {
|
||||
|
||||
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter) {
|
||||
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) {
|
||||
}
|
||||
|
||||
public override List<TEntity> Insert(IEnumerable<TEntity> entity) {
|
||||
_fsql.Insert<TEntity>().AppendData(entity).ExecuteAffrows();
|
||||
_fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrows();
|
||||
return entity.ToList();
|
||||
}
|
||||
|
||||
async public override Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
|
||||
await _fsql.Insert<TEntity>().AppendData(entity).ExecuteAffrowsAsync();
|
||||
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrowsAsync();
|
||||
return entity.ToList();
|
||||
}
|
||||
|
||||
public override TEntity Insert(TEntity entity) {
|
||||
_fsql.Insert<TEntity>().AppendData(entity).ExecuteAffrows();
|
||||
_fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrows();
|
||||
return entity;
|
||||
}
|
||||
|
||||
async public override Task<TEntity> InsertAsync(TEntity entity) {
|
||||
await _fsql.Insert<TEntity>().AppendData(entity).ExecuteAffrowsAsync();
|
||||
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrowsAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual string ToDataTable(TEntity entity) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,12 +33,13 @@ public static class IFreeSqlExtenssions {
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="filter">数据过滤 + 验证</param>
|
||||
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
||||
/// <returns></returns>
|
||||
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
||||
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
|
||||
|
||||
if (filter != null) return new GuidRepository<TEntity>(that, filter);
|
||||
if (filter != null || asTable != null) return new GuidRepository<TEntity>(that, filter, asTable);
|
||||
return dicGetGuidRepository
|
||||
.GetOrAdd(typeof(TEntity), key1 => new GuidRepository<TEntity>(that, filter)) as GuidRepository<TEntity>;
|
||||
.GetOrAdd(typeof(TEntity), key1 => new GuidRepository<TEntity>(that, null, null)) as GuidRepository<TEntity>;
|
||||
}
|
||||
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
|
||||
}
|
Reference in New Issue
Block a user