v0.0.11 实现分表

This commit is contained in:
28810
2019-03-05 22:16:44 +08:00
parent 90f69f1b57
commit e66bca2fe7
14 changed files with 131 additions and 122 deletions

View File

@@ -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);