This commit is contained in:
28810 2019-02-25 10:12:33 +08:00
parent fb1871e558
commit 9d87c69fb2
12 changed files with 93 additions and 33 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
@ -18,21 +19,69 @@ namespace FreeSql {
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>();
public void Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrows();
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrows();
public void Delete(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrows();
public int Delete(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrows();
public Task 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(predicate).ExecuteAffrowsAsync();
public Task DeleteAsync(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrowsAsync();
public Task<int> DeleteAsync(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrowsAsync();
public TEntity Insert(TEntity entity) => _fsql.Insert<TEntity>().AppendData(entity).ExecuteInserted().FirstOrDefault();
public virtual TEntity Insert(TEntity entity) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInserted().FirstOrDefault();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
default:
throw new NotImplementedException($"{_fsql.Ado.DataType}不支持类似returning或output类型的特性请参考FreeSql插入数据的方法重新现实。");
}
}
async public Task<TEntity> InsertAsync(TEntity entity) => (await _fsql.Insert<TEntity>().AppendData(entity).ExecuteInsertedAsync()).FirstOrDefault();
public virtual List<TEntity> Insert(List<TEntity> entity) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInserted();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
default:
throw new NotImplementedException($"{_fsql.Ado.DataType}不支持类似returning或output类型的特性请参考FreeSql插入数据的方法重新现实。");
}
}
public void Update(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrows();
async public virtual Task<TEntity> InsertAsync(TEntity entity) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return (await _fsql.Insert<TEntity>().AppendData(entity).ExecuteInsertedAsync()).FirstOrDefault();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
default:
throw new NotImplementedException($"{_fsql.Ado.DataType}不支持类似returning或output类型的特性请参考FreeSql插入数据的方法重新现实。");
}
}
public Task UpdateAsync(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrowsAsync();
public virtual Task<List<TEntity>> InsertAsync(List<TEntity> entity) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInsertedAsync();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
default:
throw new NotImplementedException($"{_fsql.Ado.DataType}不支持类似returning或output类型的特性请参考FreeSql插入数据的方法重新现实。");
}
}
public int Update(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrows();
public Task<int> UpdateAsync(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrowsAsync();
}
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
@ -41,9 +90,9 @@ namespace FreeSql {
public BaseRepository(IFreeSql fsql) : base(fsql) {
}
public void Delete(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrows();
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrows();
public Task DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrowsAsync();
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrowsAsync();
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).ToOne();

View File

@ -2,9 +2,9 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<Authors>YeXiangQin</Authors>
<Description>打造 .NETCore 最方便的 ORMDbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 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>

View File

@ -1,28 +1,33 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace FreeSql {
public interface IBasicRepository<TEntity> : IReadOnlyRepository<TEntity>
where TEntity : class {
TEntity Insert(TEntity entity);
List<TEntity> Insert(List<TEntity> entity);
Task<TEntity> InsertAsync(TEntity entity);
void Update(TEntity entity);
Task<List<TEntity>> InsertAsync(List<TEntity> entity);
Task UpdateAsync(TEntity entity);
int Update(TEntity entity);
Task<int> UpdateAsync(TEntity entity);
IUpdate<TEntity> UpdateDiy { get; }
void Delete(TEntity entity);
int Delete(TEntity entity);
Task DeleteAsync(TEntity entity);
Task<int> DeleteAsync(TEntity entity);
}
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>
where TEntity : class {
void Delete(TKey id);
int Delete(TKey id);
Task DeleteAsync(TKey id);
Task<int> DeleteAsync(TKey id);
}
}

View File

@ -10,9 +10,9 @@ namespace FreeSql {
public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>
where TEntity : class {
void Delete(Expression<Func<TEntity, bool>> predicate);
int Delete(Expression<Func<TEntity, bool>> predicate);
Task DeleteAsync(Expression<Func<TEntity, bool>> predicate);
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
}
public interface IRepository<TEntity, TKey> : IRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey>

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>打造 .NETCore 最方便的 ORMDbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>

View File

@ -25,6 +25,10 @@ namespace FreeSql {
/// 监视数据库命令对象(执行后,用于监视执行性能)
/// </summary>
Action<DbCommand, string> AopCommandExecuted { get; set; }
/// <summary>
/// 数据库类型
/// </summary>
DataType DataType { get; }
#region
/// <summary>

View File

@ -23,15 +23,17 @@ namespace FreeSql.Internal.CommonProvider {
public ObjectPool<DbConnection> MasterPool { get; protected set; }
public List<ObjectPool<DbConnection>> SlavePools { get; } = new List<ObjectPool<DbConnection>>();
public DataType DataType { get; }
protected ICache _cache { get; set; }
protected ILogger _log { get; set; }
protected int slaveUnavailables = 0;
private object slaveLock = new object();
private Random slaveRandom = new Random();
public AdoProvider(ICache cache, ILogger log) {
public AdoProvider(ICache cache, ILogger log, DataType dataType) {
this._cache = cache;
this._log = log;
this.DataType = dataType;
}
void LoggerException(ObjectPool<DbConnection> pool, DbCommand cmd, Exception e, DateTime dt, StringBuilder logtxt, bool isThrowException = true) {

View File

@ -12,8 +12,8 @@ namespace FreeSql.MySql {
class MySqlAdo : FreeSql.Internal.CommonProvider.AdoProvider {
CommonUtils _util;
public MySqlAdo() : base(null, null) { }
public MySqlAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log) {
public MySqlAdo() : base(null, null, DataType.MySql) { }
public MySqlAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log, DataType.MySql) {
this._util = util;
MasterPool = new MySqlConnectionPool("主库", masterConnectionString, null, null);
if (slaveConnectionStrings != null) {

View File

@ -12,8 +12,8 @@ namespace FreeSql.Oracle {
class OracleAdo : FreeSql.Internal.CommonProvider.AdoProvider {
CommonUtils _util;
public OracleAdo() : base(null, null) { }
public OracleAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log) {
public OracleAdo() : base(null, null, DataType.Oracle) { }
public OracleAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log, DataType.Oracle) {
this._util = util;
MasterPool = new OracleConnectionPool("主库", masterConnectionString, null, null);
if (slaveConnectionStrings != null) {

View File

@ -14,8 +14,8 @@ namespace FreeSql.PostgreSQL {
class PostgreSQLAdo : FreeSql.Internal.CommonProvider.AdoProvider {
CommonUtils _util;
public PostgreSQLAdo() : base(null, null) { }
public PostgreSQLAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log) {
public PostgreSQLAdo() : base(null, null, DataType.PostgreSQL) { }
public PostgreSQLAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log, DataType.PostgreSQL) {
this._util = util;
MasterPool = new PostgreSQLConnectionPool("主库", masterConnectionString, null, null);
if (slaveConnectionStrings != null) {

View File

@ -12,8 +12,8 @@ namespace FreeSql.SqlServer {
class SqlServerAdo : FreeSql.Internal.CommonProvider.AdoProvider {
CommonUtils _util;
public SqlServerAdo() : base(null, null) { }
public SqlServerAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log) {
public SqlServerAdo() : base(null, null, DataType.SqlServer) { }
public SqlServerAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log, DataType.SqlServer) {
this._util = util;
MasterPool = new SqlServerConnectionPool("主库", masterConnectionString, null, null);
if (slaveConnectionStrings != null) {

View File

@ -12,8 +12,8 @@ namespace FreeSql.Sqlite {
class SqliteAdo : FreeSql.Internal.CommonProvider.AdoProvider {
CommonUtils _util;
public SqliteAdo() : base(null, null) { }
public SqliteAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log) {
public SqliteAdo() : base(null, null, DataType.Sqlite) { }
public SqliteAdo(CommonUtils util, ICache cache, ILogger log, string masterConnectionString, string[] slaveConnectionStrings) : base(cache, log, DataType.Sqlite) {
this._util = util;
MasterPool = new SqliteConnectionPool("主库", masterConnectionString, null, null);
if (slaveConnectionStrings != null) {