using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using Microsoft.AspNetCore.Http; using MySqlConnector; using PetaPoco.Business.Interface; using PetaPoco.Internal; namespace PetaPoco.Business { public class PetaPocoRepository : IRepository { private const string RequestCacheKey = "PetaPocoRepositoryPerRequest"; private const string RequestReadOnlyCacheKey = "PetaPocoReadOnlyRepositoryPerRequest"; private static IHttpContextAccessor _httpContextAccessor; public PetaPocoRepository(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } public static PetaPocoRepository Instance { get { if (_httpContextAccessor != null && _httpContextAccessor.HttpContext != null) { if (_httpContextAccessor?.HttpContext?.Items[RequestCacheKey] == null) { var repo = new PetaPocoRepository("db"); _httpContextAccessor.HttpContext.Items[RequestCacheKey] = repo; return repo; } return _httpContextAccessor.HttpContext.Items[RequestCacheKey] as PetaPocoRepository; } else { return new PetaPocoRepository("db"); } } } public static PetaPocoRepository ReadOnlyInstance { get { if (_httpContextAccessor != null && _httpContextAccessor?.HttpContext != null) { if (_httpContextAccessor?.HttpContext.Items[RequestReadOnlyCacheKey] == null) { var repo = new PetaPocoRepository("dbReadOnly"); _httpContextAccessor.HttpContext.Items[RequestReadOnlyCacheKey] = repo; return repo; } return _httpContextAccessor.HttpContext.Items[RequestReadOnlyCacheKey] as PetaPocoRepository; } else { return new PetaPocoRepository("dbReadOnly"); } } } private PetaPocoRepository(string connectionStringName) { Db = new Database(connectionStringName); Db.CommandTimeout = 200000; } public Database Db { get; set; } public TPassType Single(object primaryKey) { return Db.Single(primaryKey); } public TPassType SingleOrDefault(object primaryKey) { return Db.SingleOrDefault(primaryKey); } public TPassType SingleOrDefaultWithSql(string sql, params object[] args) { return Db.SingleOrDefault(sql, args); } public TPassType FirstOrDefaultWithSql(string sql, params object[] args) { return Db.FirstOrDefault(sql, args); } public IEnumerable Query() { var pd = PocoData.ForType(typeof(TPassType)); var sql = "SELECT * FROM " + pd.TableInfo.TableName; return Db.Query(sql); } public IEnumerable Query(string sql, params object[] args) { return Db.Query(sql, args); } public List Fetch() { var pd = PocoData.ForType(typeof(TPassType)); var sql = "SELECT * FROM " + pd.TableInfo.TableName; return Db.Fetch(sql); } public List Fetch(Sql sql) { return Db.Fetch(sql); } public List Fetch(string sql, params object[] args) { return Db.Fetch(sql, args); } public Page PagedQuery(long pageNumber, long itemsPerPage, string sql, params object[] args) { return Db.Page(pageNumber, itemsPerPage, sql, args) as Page; } public Page PagedQuery(long pageNumber, long itemsPerPage, Sql sql) { return Db.Page(pageNumber, itemsPerPage, sql) as Page; } /// /// Inserts into database and returns >1 int on auto-incrementing primary keys and 0 otherwise. /// /// public int Insert(object poco) { return Db.Insert(poco) as int? ?? 0; } public long InsertLong(object poco) { return Convert.ToInt64(Db.Insert(poco)); } public int Insert(string tableName, string primaryKeyName, bool autoIncrement, object poco) { return Convert.ToInt32(Db.Insert(tableName, primaryKeyName, autoIncrement, poco)); } public int Insert(string tableName, string primaryKeyName, object poco) { return Convert.ToInt32(Db.Insert(tableName, primaryKeyName, poco)); } public int Update(object poco) { return Db.Update(poco); } public long UpdateLong(object poco) { return Db.Update(poco); } public int Update(object poco, object primaryKeyValue) { return Db.Update(poco, primaryKeyValue); } public int Update(string tableName, string primaryKeyName, object poco) { return Db.Update(tableName, primaryKeyName, poco); } public int Update(object poco, IEnumerable columns) { return Db.Update(poco, columns); } public int Delete(object pocoOrPrimaryKey) { return Db.Delete(pocoOrPrimaryKey); } public int Delete(Sql sql) { return Db.Delete(sql); } public int Delete(string sql, params object[] args) { return Db.Delete(sql, args); } public int Execute(Sql sql) { return Db.Execute(sql); } public bool IsNew(object poco) { return Db.IsNew(poco); } public TPassType Upsert(TPassType poco) { if (IsNew(poco)) Insert(poco); else Update(poco); return poco; } public async Task ImportBulkFileLoaderBatchesAsync( IEnumerable pocos, MySqlBulkLoaderConflictOption conflictionOption = MySqlBulkLoaderConflictOption.Ignore, int batchSize = 20_000, Action successAction = null, Action failAction = null) { if (!pocos.Any()) { return; } await Db.ImportBulkFileLoaderByBatchesAsync(pocos, conflictionOption, batchSize, successAction, failAction); } public async Task BulkInsertAsync(IEnumerable pocos, int? batchSize=null, bool ignoreExisting = false) { await Db.BulkInsertAsync(pocos, batchSize, ignoreExisting: ignoreExisting); } public async Task ImportBulkFileLoaderAsync(IEnumerable pocos, MySqlBulkLoaderConflictOption conflictionOption = MySqlBulkLoaderConflictOption.Ignore) { if (!pocos.Any()) return; await Db.ImportBulkFileLoaderAsync(pocos, conflictionOption); } public void ImportBulkFileLoader(IEnumerable pocos, MySqlBulkLoaderConflictOption conflictionOption = MySqlBulkLoaderConflictOption.Ignore) { if (!pocos.Any()) return; Db.ImportBulkFileLoader(pocos, conflictionOption); } } }