mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- FreeSql.Repository 增加 filter 参数,现实数据过滤 + 验证;
如:var postRepos = fsql.GetGuidRepository<Post>(a => a.TopicId == 1); postRepos CURD 方法都会以 lambad 条件作为查询或验证,Update/Insert验证错误时会抛出异常。 - ISelect 增加 First/FirstAsync;
This commit is contained in:
parent
a2f4a8bcd8
commit
30385d2e91
@ -9,25 +9,35 @@ namespace FreeSql {
|
||||
where TEntity : class {
|
||||
|
||||
protected IFreeSql _fsql;
|
||||
protected Expression<Func<TEntity, bool>> _filter;
|
||||
protected Func<TEntity, bool> _filterCompile;
|
||||
|
||||
public BaseRepository(IFreeSql fsql) : base() {
|
||||
_fsql = fsql;
|
||||
if (_fsql == null) throw new NullReferenceException("fsql 参数不可为空");
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base() {
|
||||
_fsql = fsql ?? throw new NullReferenceException("fsql 参数不可为空");
|
||||
_filter = filter;
|
||||
_filterCompile = filter?.Compile();
|
||||
}
|
||||
|
||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>();
|
||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(_filter);
|
||||
|
||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>();
|
||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(_filter);
|
||||
|
||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrows();
|
||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).ExecuteAffrows();
|
||||
|
||||
public int Delete(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrows();
|
||||
public int Delete(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).ExecuteAffrows();
|
||||
}
|
||||
|
||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).ExecuteAffrowsAsync();
|
||||
|
||||
public Task<int> DeleteAsync(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual TEntity Insert(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
@ -40,11 +50,12 @@ namespace FreeSql {
|
||||
}
|
||||
}
|
||||
|
||||
public virtual List<TEntity> Insert(IEnumerable<TEntity> entity) {
|
||||
public virtual List<TEntity> Insert(IEnumerable<TEntity> entitys) {
|
||||
ValidatorEntityAndThrow(entitys);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInserted();
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).ExecuteInserted();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@ -54,6 +65,7 @@ namespace FreeSql {
|
||||
}
|
||||
|
||||
async public virtual Task<TEntity> InsertAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
@ -66,11 +78,12 @@ namespace FreeSql {
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
|
||||
public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys) {
|
||||
ValidatorEntityAndThrow(entitys);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInsertedAsync();
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).ExecuteInsertedAsync();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@ -79,24 +92,37 @@ namespace FreeSql {
|
||||
}
|
||||
}
|
||||
|
||||
public int Update(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrows();
|
||||
public int Update(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).ExecuteAffrows();
|
||||
}
|
||||
|
||||
public Task<int> UpdateAsync(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrowsAsync();
|
||||
public Task<int> UpdateAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
protected void ValidatorEntityAndThrow(TEntity entity) => ValidatorEntityAndThrow(new[] { entity });
|
||||
protected virtual void ValidatorEntityAndThrow(IEnumerable<TEntity> entitys) {
|
||||
foreach (var entity in entitys) {
|
||||
if (_filterCompile?.Invoke(entity) == false) throw new Exception($"FreeSql.Repository Insert 失败,因为设置了 {_filter},插入的数据不符合");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public BaseRepository(IFreeSql fsql) : base(fsql) {
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter) {
|
||||
}
|
||||
|
||||
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrows();
|
||||
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).ExecuteAffrows();
|
||||
|
||||
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).ExecuteAffrowsAsync();
|
||||
|
||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).ToOne();
|
||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).ToOne();
|
||||
|
||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).ToOneAsync();
|
||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).ToOneAsync();
|
||||
|
||||
public TEntity Get(TKey id) => Find(id);
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -8,7 +9,7 @@ namespace FreeSql {
|
||||
BaseRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public DefaultRepository(IFreeSql fsql) : base(fsql) {
|
||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>0.1.6</Version>
|
||||
<Version>0.1.7</Version>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 通用仓库层现实,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -9,7 +10,7 @@ namespace FreeSql {
|
||||
BaseRepository<TEntity, Guid>
|
||||
where TEntity : class {
|
||||
|
||||
public GuidRepository(IFreeSql fsql) : base(fsql) {
|
||||
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter) {
|
||||
}
|
||||
|
||||
public override List<TEntity> Insert(IEnumerable<TEntity> entity) {
|
||||
|
@ -6,11 +6,11 @@ namespace FreeSql {
|
||||
where TEntity : class {
|
||||
TEntity Insert(TEntity entity);
|
||||
|
||||
List<TEntity> Insert(IEnumerable<TEntity> entity);
|
||||
List<TEntity> Insert(IEnumerable<TEntity> entitys);
|
||||
|
||||
Task<TEntity> InsertAsync(TEntity entity);
|
||||
|
||||
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity);
|
||||
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
|
||||
|
||||
int Update(TEntity entity);
|
||||
|
||||
|
@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
public static class IFreeSqlExtenssions {
|
||||
|
||||
@ -12,23 +13,33 @@ public static class IFreeSqlExtenssions {
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="filter">数据过滤 + 验证</param>
|
||||
/// <returns></returns>
|
||||
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that) where TEntity : class {
|
||||
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
||||
|
||||
return dicGetRepository.GetOrAdd(typeof(TEntity), type1 => new ConcurrentDictionary<Type, IRepository>())
|
||||
.GetOrAdd(typeof(TKey), type2 => new DefaultRepository<TEntity, TKey>(that)) as DefaultRepository<TEntity, TKey>;
|
||||
return dicGetRepository
|
||||
.GetOrAdd(typeof(TEntity), key1 => new ConcurrentDictionary<Type, ConcurrentDictionary<string, IRepository>>())
|
||||
.GetOrAdd(typeof(TKey), key2 => new ConcurrentDictionary<string, IRepository>())
|
||||
.GetOrAdd(string.Concat(filter), key3 => new DefaultRepository<TEntity, TKey>(that, filter)) as DefaultRepository<TEntity, TKey>;
|
||||
}
|
||||
static ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>> dicGetRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>>();
|
||||
static ConcurrentDictionary<Type,
|
||||
ConcurrentDictionary<Type,
|
||||
ConcurrentDictionary<string, IRepository>>
|
||||
> dicGetRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, ConcurrentDictionary<string, IRepository>>>();
|
||||
|
||||
/// <summary>
|
||||
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="filter">数据过滤 + 验证</param>
|
||||
/// <returns></returns>
|
||||
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that) where TEntity : class {
|
||||
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
||||
|
||||
return dicGetGuidRepository.GetOrAdd(typeof(TEntity), type1 => new GuidRepository<TEntity>(that)) as GuidRepository<TEntity>;
|
||||
return dicGetGuidRepository
|
||||
.GetOrAdd(typeof(TEntity), key1 => new ConcurrentDictionary<string, IRepository>())
|
||||
.GetOrAdd(string.Concat(filter), key2 => new GuidRepository<TEntity>(that, filter)) as GuidRepository<TEntity>;
|
||||
}
|
||||
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
|
||||
static ConcurrentDictionary<Type,
|
||||
ConcurrentDictionary<string, IRepository>> dicGetGuidRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<string, IRepository>>();
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>0.1.6</Version>
|
||||
<Version>0.1.7</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>打造 .NETCore 最方便的 ORM,DbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>
|
||||
|
@ -28,6 +28,13 @@ namespace FreeSql {
|
||||
T1 ToOne();
|
||||
Task<T1> ToOneAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL查询,返回 T1 实体所有字段的第一条记录,记录不存在时返回 null
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
T1 First();
|
||||
Task<T1> FirstAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 返回即将执行的SQL语句
|
||||
/// </summary>
|
||||
|
@ -203,6 +203,7 @@ namespace FreeSql.Internal {
|
||||
}
|
||||
|
||||
internal string ExpressionLambdaToSql(Expression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, Func<Expression[], string> getSelectGroupingMapString, SelectTableInfoType tbtype, bool isQuoteName) {
|
||||
if (exp == null) return "";
|
||||
switch (exp.NodeType) {
|
||||
case ExpressionType.Not: return $"not({ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName)})";
|
||||
case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
|
||||
|
@ -205,6 +205,9 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
return (await this.ToListAsync()).FirstOrDefault();
|
||||
}
|
||||
|
||||
public T1 First() => this.ToOne();
|
||||
public Task<T1> FirstAsync() => this.ToOneAsync();
|
||||
|
||||
protected List<TReturn> ToListMapReader<TReturn>((ReadAnonymousTypeInfo map, string field) af) {
|
||||
var sql = this.ToSql(af.field);
|
||||
if (_cache.seconds > 0 && string.IsNullOrEmpty(_cache.key)) _cache.key = $"{sql}{string.Join("|", _params.Select(a => a.Value))}";
|
||||
@ -461,7 +464,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
return (await this.ToListMapReaderAsync<TReturn>((map, field.Length > 0 ? field.Remove(0, 2).ToString() : null))).FirstOrDefault();
|
||||
}
|
||||
|
||||
protected TSelect InternalWhere(Expression exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp, null));
|
||||
protected TSelect InternalWhere(Expression exp) => exp == null ? this as TSelect : this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp, null));
|
||||
|
||||
protected TSelect InternalJoin(Expression exp) {
|
||||
return this as TSelect;
|
||||
|
Loading…
x
Reference in New Issue
Block a user