using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using MySql.Data.MySqlClient; using PetaPoco.Business.Interface; using PetaPoco.Internal; using Sony.Filtr.Functional; namespace PetaPoco.Business { public class PetaPocoRepository : IRepository { private const string RequestCacheKey = "PetaPocoRepositoryPerRequest"; private const string RequestReadOnlyCacheKey = "PetaPocoReadOnlyRepositoryPerRequest"; public static PetaPocoRepository Instance { get { if (HttpContext.Current != null) { if (HttpContext.Current.Items[RequestCacheKey] == null) { var repo = new PetaPocoRepository("db"); HttpContext.Current.Items[RequestCacheKey] = repo; return repo; } return HttpContext.Current.Items[RequestCacheKey] as PetaPocoRepository; } else { return new PetaPocoRepository("db"); } } } public static PetaPocoRepository ReadOnlyInstance { get { if (HttpContext.Current != null) { if (HttpContext.Current.Items[RequestReadOnlyCacheKey] == null) { var repo = new PetaPocoRepository("dbReadOnly"); HttpContext.Current.Items[RequestReadOnlyCacheKey] = repo; return repo; } return HttpContext.Current.Items[RequestReadOnlyCacheKey] as PetaPocoRepository; } else { return new PetaPocoRepository("dbReadOnly"); } } } private PetaPocoRepository(string connectionStringName) { Db = new Database(connectionStringName); } 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 ToNumber(Db.Insert(poco)); } private static int ToNumber(object obj) { return obj.FirstOrDefault( nullableInt => nullableInt.HasValue, (Nullable)0, CastToIntegerNumberFrom, CastToIntegerNumberFrom, CastToIntegerNumberFrom).Value; } private static Nullable CastToIntegerNumberFrom(object obj) where T: struct, IConvertible { return obj is T ? Convert.ToInt32((T)obj) : (Nullable)null; } 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 BulkInsertAsync(IEnumerable pocos, int? batchSize=null, bool ignoreExisting = false) { await Db.BulkInsertAsync(pocos, batchSize, ignoreExisting: ignoreExisting); } public async Task ImportBulkFileLoaderBatchesAsync(IEnumerable pocos, MySqlBulkLoaderConflictOption conflictionOption = MySqlBulkLoaderConflictOption.Ignore, int batchSize = 20_000) { if (!pocos.Any()) { return; } await Db.ImportBulkFileLoaderByBatchesAsync(pocos, conflictionOption, batchSize); } 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); } } }