2018-03-15 01:28:46 +00:00
|
|
|
using System;
|
2013-03-24 04:16:00 +00:00
|
|
|
using System.Collections.Generic;
|
2014-11-24 20:00:19 +00:00
|
|
|
using System.Data;
|
2013-02-05 04:07:07 +00:00
|
|
|
using System.Linq;
|
2013-03-24 04:16:00 +00:00
|
|
|
using System.Linq.Expressions;
|
2020-08-18 20:11:44 +00:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
|
|
|
using Dapper;
|
2013-05-05 21:24:33 +00:00
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-03-25 03:51:32 +00:00
|
|
|
|
2013-02-05 04:07:07 +00:00
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
public interface IBasicRepository<TModel>
|
|
|
|
where TModel : ModelBase, new()
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
2013-02-19 06:56:02 +00:00
|
|
|
IEnumerable<TModel> All();
|
2013-02-23 19:38:25 +00:00
|
|
|
int Count();
|
2013-02-17 05:35:52 +00:00
|
|
|
TModel Get(int id);
|
2013-02-19 06:56:02 +00:00
|
|
|
TModel Insert(TModel model);
|
2013-02-18 07:59:43 +00:00
|
|
|
TModel Update(TModel model);
|
2013-03-24 04:16:00 +00:00
|
|
|
TModel Upsert(TModel model);
|
2020-08-18 20:11:44 +00:00
|
|
|
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
|
2013-03-07 04:34:56 +00:00
|
|
|
void Delete(TModel model);
|
2020-08-18 20:11:44 +00:00
|
|
|
void Delete(int id);
|
|
|
|
IEnumerable<TModel> Get(IEnumerable<int> ids);
|
2013-03-24 04:16:00 +00:00
|
|
|
void InsertMany(IList<TModel> model);
|
|
|
|
void UpdateMany(IList<TModel> model);
|
2020-08-18 20:11:44 +00:00
|
|
|
void SetFields(IList<TModel> models, params Expression<Func<TModel, object>>[] properties);
|
2013-03-06 21:20:33 +00:00
|
|
|
void DeleteMany(List<TModel> model);
|
2020-08-18 20:11:44 +00:00
|
|
|
void DeleteMany(IEnumerable<int> ids);
|
2013-11-28 07:11:05 +00:00
|
|
|
void Purge(bool vacuum = false);
|
2013-03-02 18:25:39 +00:00
|
|
|
bool HasItems();
|
2013-04-25 04:27:49 +00:00
|
|
|
TModel Single();
|
2020-08-18 20:11:44 +00:00
|
|
|
TModel SingleOrDefault();
|
2013-06-05 00:49:53 +00:00
|
|
|
PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec);
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
public class BasicRepository<TModel> : IBasicRepository<TModel>
|
|
|
|
where TModel : ModelBase, new()
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
2013-09-14 06:36:07 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2020-08-18 20:11:44 +00:00
|
|
|
private readonly PropertyInfo _keyProperty;
|
|
|
|
private readonly List<PropertyInfo> _properties;
|
|
|
|
private readonly string _updateSql;
|
|
|
|
private readonly string _insertSql;
|
2013-04-25 04:27:49 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
protected readonly IDatabase _database;
|
|
|
|
protected readonly string _table;
|
2013-03-24 04:25:16 +00:00
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
public BasicRepository(IDatabase database, IEventAggregator eventAggregator)
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
_database = database;
|
2013-09-14 06:36:07 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
var type = typeof(TModel);
|
2013-03-27 03:44:52 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
_table = TableMapping.Mapper.TableNameMapping(type);
|
|
|
|
_keyProperty = type.GetProperty(nameof(ModelBase.Id));
|
|
|
|
|
|
|
|
var excluded = TableMapping.Mapper.ExcludeProperties(type).Select(x => x.Name).ToList();
|
|
|
|
excluded.Add(_keyProperty.Name);
|
|
|
|
_properties = type.GetProperties().Where(x => x.IsMappableProperty() && !excluded.Contains(x.Name)).ToList();
|
|
|
|
|
|
|
|
_insertSql = GetInsertSql();
|
|
|
|
_updateSql = GetUpdateSql(_properties);
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
protected virtual SqlBuilder Builder() => new SqlBuilder();
|
|
|
|
|
|
|
|
protected virtual List<TModel> Query(SqlBuilder builder) => _database.Query<TModel>(builder).ToList();
|
|
|
|
|
|
|
|
protected List<TModel> Query(Expression<Func<TModel, bool>> where) => Query(Builder().Where(where));
|
|
|
|
|
|
|
|
protected virtual List<TModel> QueryDistinct(SqlBuilder builder) => _database.QueryDistinct<TModel>(builder).ToList();
|
|
|
|
|
|
|
|
public int Count()
|
2013-02-23 19:38:25 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
return conn.ExecuteScalar<int>($"SELECT COUNT(*) FROM {_table}");
|
|
|
|
}
|
2013-03-24 04:25:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
public virtual IEnumerable<TModel> All()
|
2013-03-24 04:25:16 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
return Query(Builder());
|
2013-02-23 19:38:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 03:50:22 +00:00
|
|
|
public TModel Get(int id)
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
var model = Query(x => x.Id == id).FirstOrDefault();
|
2013-07-09 22:06:30 +00:00
|
|
|
|
|
|
|
if (model == null)
|
|
|
|
{
|
|
|
|
throw new ModelNotFoundException(typeof(TModel), id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 00:33:38 +00:00
|
|
|
public IEnumerable<TModel> Get(IEnumerable<int> ids)
|
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
if (!ids.Any())
|
|
|
|
{
|
|
|
|
return new List<TModel>();
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = Query(x => ids.Contains(x.Id));
|
2013-06-02 06:41:30 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
if (result.Count != ids.Count())
|
2013-06-02 06:41:30 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
throw new ApplicationException($"Expected query to return {ids.Count()} rows but returned {result.Count}");
|
2013-06-02 06:41:30 +00:00
|
|
|
}
|
2013-04-18 00:33:38 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-06 21:20:33 +00:00
|
|
|
public TModel SingleOrDefault()
|
2013-04-25 04:27:49 +00:00
|
|
|
{
|
|
|
|
return All().SingleOrDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
public TModel Single()
|
2013-03-06 21:20:33 +00:00
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
return All().Single();
|
|
|
|
}
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
public TModel Insert(TModel model)
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
2013-03-24 19:56:51 +00:00
|
|
|
if (model.Id != 0)
|
|
|
|
{
|
2013-06-03 03:44:31 +00:00
|
|
|
throw new InvalidOperationException("Can't insert model with existing ID " + model.Id);
|
2013-03-24 19:56:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
model = Insert(conn, null, model);
|
|
|
|
}
|
2013-05-05 21:24:33 +00:00
|
|
|
|
2013-11-13 20:08:37 +00:00
|
|
|
ModelCreated(model);
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
return model;
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
private string GetInsertSql()
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
var sbColumnList = new StringBuilder(null);
|
|
|
|
for (var i = 0; i < _properties.Count; i++)
|
2013-03-24 19:56:51 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
var property = _properties[i];
|
|
|
|
sbColumnList.AppendFormat("\"{0}\"", property.Name);
|
|
|
|
if (i < _properties.Count - 1)
|
|
|
|
{
|
|
|
|
sbColumnList.Append(", ");
|
|
|
|
}
|
2013-03-24 19:56:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
var sbParameterList = new StringBuilder(null);
|
|
|
|
for (var i = 0; i < _properties.Count; i++)
|
|
|
|
{
|
|
|
|
var property = _properties[i];
|
|
|
|
sbParameterList.AppendFormat("@{0}", property.Name);
|
|
|
|
if (i < _properties.Count - 1)
|
|
|
|
{
|
|
|
|
sbParameterList.Append(", ");
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 20:08:37 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
return $"INSERT INTO {_table} ({sbColumnList.ToString()}) VALUES ({sbParameterList.ToString()}); SELECT last_insert_rowid() id";
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
private TModel Insert(IDbConnection connection, IDbTransaction transaction, TModel model)
|
2013-03-07 04:34:56 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
SqlBuilderExtensions.LogQuery(_insertSql, model);
|
|
|
|
var multi = connection.QueryMultiple(_insertSql, model, transaction);
|
|
|
|
var id = (int)multi.Read().First().id;
|
|
|
|
_keyProperty.SetValue(model, id);
|
|
|
|
|
|
|
|
return model;
|
2013-03-07 04:34:56 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
public void InsertMany(IList<TModel> models)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
if (models.Any(x => x.Id != 0))
|
2013-03-25 03:51:32 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
throw new InvalidOperationException("Can't insert model with existing ID != 0");
|
|
|
|
}
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
using (IDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))
|
2014-02-21 07:03:36 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
foreach (var model in models)
|
|
|
|
{
|
|
|
|
Insert(conn, tran, model);
|
|
|
|
}
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
tran.Commit();
|
|
|
|
}
|
2013-03-25 03:51:32 +00:00
|
|
|
}
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
public TModel Update(TModel model)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
if (model.Id == 0)
|
2013-03-25 03:51:32 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
throw new InvalidOperationException("Can't update model with ID 0");
|
|
|
|
}
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
UpdateFields(conn, null, model, _properties);
|
|
|
|
}
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
ModelUpdated(model);
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
return model;
|
|
|
|
}
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
public void UpdateMany(IList<TModel> models)
|
|
|
|
{
|
|
|
|
if (models.Any(x => x.Id == 0))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Can't update model with ID 0");
|
|
|
|
}
|
|
|
|
|
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
UpdateFields(conn, null, models, _properties);
|
2013-03-25 03:51:32 +00:00
|
|
|
}
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
protected void Delete(Expression<Func<TModel, bool>> where)
|
2013-03-05 19:49:34 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
Delete(Builder().Where<TModel>(where));
|
2013-03-05 19:49:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
protected void Delete(SqlBuilder builder)
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
var sql = builder.AddDeleteTemplate(typeof(TModel)).LogQuery();
|
|
|
|
|
|
|
|
using (var conn = _database.OpenConnection())
|
2013-02-23 19:38:25 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
conn.Execute(sql.RawSql, sql.Parameters);
|
2013-02-23 19:38:25 +00:00
|
|
|
}
|
2020-08-18 20:11:44 +00:00
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
public void Delete(TModel model)
|
|
|
|
{
|
|
|
|
Delete(model.Id);
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 03:50:22 +00:00
|
|
|
public void Delete(int id)
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
Delete(x => x.Id == id);
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
2013-02-23 19:38:25 +00:00
|
|
|
|
|
|
|
public void DeleteMany(IEnumerable<int> ids)
|
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
if (ids.Any())
|
2014-02-21 07:03:36 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
Delete(x => ids.Contains(x.Id));
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
public void DeleteMany(List<TModel> models)
|
|
|
|
{
|
|
|
|
DeleteMany(models.Select(m => m.Id));
|
|
|
|
}
|
2014-02-21 07:03:36 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
public TModel Upsert(TModel model)
|
|
|
|
{
|
|
|
|
if (model.Id == 0)
|
|
|
|
{
|
|
|
|
Insert(model);
|
|
|
|
return model;
|
2014-02-21 07:03:36 +00:00
|
|
|
}
|
2020-08-18 20:11:44 +00:00
|
|
|
|
|
|
|
Update(model);
|
|
|
|
return model;
|
2013-02-23 19:38:25 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 07:11:05 +00:00
|
|
|
public void Purge(bool vacuum = false)
|
2013-02-23 19:38:25 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
conn.Execute($"DELETE FROM [{_table}]");
|
|
|
|
}
|
|
|
|
|
2013-11-28 07:11:05 +00:00
|
|
|
if (vacuum)
|
|
|
|
{
|
|
|
|
Vacuum();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Vacuum()
|
|
|
|
{
|
|
|
|
_database.Vacuum();
|
2013-02-23 19:38:25 +00:00
|
|
|
}
|
2013-03-02 18:25:39 +00:00
|
|
|
|
|
|
|
public bool HasItems()
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
return Count() > 0;
|
|
|
|
}
|
|
|
|
|
2013-03-27 06:16:55 +00:00
|
|
|
public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties)
|
2013-03-24 04:16:00 +00:00
|
|
|
{
|
2013-03-24 04:25:16 +00:00
|
|
|
if (model.Id == 0)
|
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
throw new InvalidOperationException("Attempted to update model without ID");
|
2013-03-24 04:25:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
var propertiesToUpdate = properties.Select(x => x.GetMemberName()).ToList();
|
|
|
|
|
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
UpdateFields(conn, null, model, propertiesToUpdate);
|
|
|
|
}
|
2013-11-13 20:08:37 +00:00
|
|
|
|
|
|
|
ModelUpdated(model);
|
2013-03-02 18:25:39 +00:00
|
|
|
}
|
2013-05-10 23:53:50 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
public void SetFields(IList<TModel> models, params Expression<Func<TModel, object>>[] properties)
|
2019-02-16 14:49:24 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
if (models.Any(x => x.Id == 0))
|
2019-02-16 14:49:24 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
throw new InvalidOperationException("Attempted to update model without ID");
|
|
|
|
}
|
2019-02-16 14:49:24 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
var propertiesToUpdate = properties.Select(x => x.GetMemberName()).ToList();
|
|
|
|
|
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
UpdateFields(conn, null, models, propertiesToUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var model in models)
|
|
|
|
{
|
|
|
|
ModelUpdated(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetUpdateSql(List<PropertyInfo> propertiesToUpdate)
|
|
|
|
{
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendFormat("UPDATE {0} SET ", _table);
|
2019-02-16 14:49:24 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
for (var i = 0; i < propertiesToUpdate.Count; i++)
|
|
|
|
{
|
|
|
|
var property = propertiesToUpdate[i];
|
|
|
|
sb.AppendFormat("\"{0}\" = @{1}", property.Name, property.Name);
|
|
|
|
if (i < propertiesToUpdate.Count - 1)
|
|
|
|
{
|
|
|
|
sb.Append(", ");
|
2019-02-16 14:49:24 +00:00
|
|
|
}
|
2020-08-18 20:11:44 +00:00
|
|
|
}
|
2019-02-16 14:49:24 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
sb.Append($" WHERE \"{_keyProperty.Name}\" = @{_keyProperty.Name}");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateFields(IDbConnection connection, IDbTransaction transaction, TModel model, List<PropertyInfo> propertiesToUpdate)
|
|
|
|
{
|
|
|
|
var sql = propertiesToUpdate == _properties ? _updateSql : GetUpdateSql(propertiesToUpdate);
|
|
|
|
|
|
|
|
SqlBuilderExtensions.LogQuery(sql, model);
|
|
|
|
|
|
|
|
connection.Execute(sql, model, transaction: transaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateFields(IDbConnection connection, IDbTransaction transaction, IList<TModel> models, List<PropertyInfo> propertiesToUpdate)
|
|
|
|
{
|
|
|
|
var sql = propertiesToUpdate == _properties ? _updateSql : GetUpdateSql(propertiesToUpdate);
|
|
|
|
|
|
|
|
foreach (var model in models)
|
|
|
|
{
|
|
|
|
SqlBuilderExtensions.LogQuery(sql, model);
|
2019-02-16 14:49:24 +00:00
|
|
|
}
|
2020-08-18 20:11:44 +00:00
|
|
|
|
|
|
|
connection.Execute(sql, models, transaction: transaction);
|
2019-02-16 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
protected virtual SqlBuilder PagedBuilder() => Builder();
|
|
|
|
protected virtual IEnumerable<TModel> PagedQuery(SqlBuilder sql) => Query(sql);
|
|
|
|
|
2013-06-05 00:49:53 +00:00
|
|
|
public virtual PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec)
|
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
pagingSpec.Records = GetPagedRecords(PagedBuilder(), pagingSpec, PagedQuery);
|
|
|
|
pagingSpec.TotalRecords = GetPagedRecordCount(PagedBuilder().SelectCount(), pagingSpec);
|
2013-06-05 00:49:53 +00:00
|
|
|
|
|
|
|
return pagingSpec;
|
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
private void AddFilters(SqlBuilder builder, PagingSpec<TModel> pagingSpec)
|
2014-02-01 22:09:22 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
var filters = pagingSpec.FilterExpressions;
|
2018-03-15 01:28:46 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
foreach (var filter in filters)
|
2018-03-15 01:28:46 +00:00
|
|
|
{
|
2020-08-18 20:11:44 +00:00
|
|
|
builder.Where<TModel>(filter);
|
2018-03-15 01:28:46 +00:00
|
|
|
}
|
2020-08-18 20:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected List<TModel> GetPagedRecords(SqlBuilder builder, PagingSpec<TModel> pagingSpec, Func<SqlBuilder, IEnumerable<TModel>> queryFunc)
|
|
|
|
{
|
|
|
|
AddFilters(builder, pagingSpec);
|
|
|
|
|
2020-09-01 20:26:36 +00:00
|
|
|
if (pagingSpec.SortKey == null)
|
|
|
|
{
|
|
|
|
pagingSpec.SortKey = $"{_table}.{_keyProperty.Name}";
|
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
var sortDirection = pagingSpec.SortDirection == SortDirection.Descending ? "DESC" : "ASC";
|
|
|
|
var pagingOffset = (pagingSpec.Page - 1) * pagingSpec.PageSize;
|
|
|
|
builder.OrderBy($"{pagingSpec.SortKey} {sortDirection} LIMIT {pagingSpec.PageSize} OFFSET {pagingOffset}");
|
|
|
|
|
|
|
|
return queryFunc(builder).ToList();
|
|
|
|
}
|
2018-03-15 01:28:46 +00:00
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
protected int GetPagedRecordCount(SqlBuilder builder, PagingSpec<TModel> pagingSpec, string template = null)
|
|
|
|
{
|
|
|
|
AddFilters(builder, pagingSpec);
|
|
|
|
|
|
|
|
SqlBuilder.Template sql;
|
|
|
|
if (template != null)
|
|
|
|
{
|
|
|
|
sql = builder.AddTemplate(template).LogQuery();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sql = builder.AddPageCountTemplate(typeof(TModel));
|
|
|
|
}
|
|
|
|
|
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
|
|
|
return conn.ExecuteScalar<int>(sql.RawSql, sql.Parameters);
|
|
|
|
}
|
2014-02-01 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
protected void ModelCreated(TModel model)
|
2013-05-21 04:10:04 +00:00
|
|
|
{
|
2013-09-11 06:33:47 +00:00
|
|
|
PublishModelEvent(model, ModelAction.Created);
|
2013-05-21 04:10:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
protected void ModelUpdated(TModel model)
|
2013-05-30 23:32:50 +00:00
|
|
|
{
|
2013-09-11 06:33:47 +00:00
|
|
|
PublishModelEvent(model, ModelAction.Updated);
|
2013-05-30 23:32:50 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
protected void ModelDeleted(TModel model)
|
2013-05-30 23:32:50 +00:00
|
|
|
{
|
2013-09-11 06:33:47 +00:00
|
|
|
PublishModelEvent(model, ModelAction.Deleted);
|
|
|
|
}
|
2013-05-30 23:32:50 +00:00
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
private void PublishModelEvent(TModel model, ModelAction action)
|
|
|
|
{
|
|
|
|
if (PublishModelEvents)
|
|
|
|
{
|
2013-09-14 06:36:07 +00:00
|
|
|
_eventAggregator.PublishEvent(new ModelEvent<TModel>(model, action));
|
2013-09-11 06:33:47 +00:00
|
|
|
}
|
2013-05-30 23:32:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
protected virtual bool PublishModelEvents => false;
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
}
|