| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  | using System; | 
					
						
							|  |  |  | using System.Collections.Generic; | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  | using System.Linq; | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  | using System.Linq.Expressions; | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  | using Marr.Data; | 
					
						
							|  |  |  | using Marr.Data.QGen; | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  | using NzbDrone.Common.Messaging; | 
					
						
							|  |  |  | using NzbDrone.Core.Datastore.Events; | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace NzbDrone.Core.Datastore | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-03-02 10:25:39 -08:00
										 |  |  |     public interface IBasicRepository<TModel> where TModel : ModelBase, new() | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-02-18 22:56:02 -08:00
										 |  |  |         IEnumerable<TModel> All(); | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         int Count(); | 
					
						
							| 
									
										
										
										
											2013-02-16 21:35:52 -08:00
										 |  |  |         TModel Get(int id); | 
					
						
							| 
									
										
										
										
											2013-04-17 17:33:38 -07:00
										 |  |  |         IEnumerable<TModel> Get(IEnumerable<int> ids); | 
					
						
							| 
									
										
										
										
											2013-03-06 13:20:33 -08:00
										 |  |  |         TModel SingleOrDefault(); | 
					
						
							| 
									
										
										
										
											2013-02-18 22:56:02 -08:00
										 |  |  |         TModel Insert(TModel model); | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         TModel Update(TModel model); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         TModel Upsert(TModel model); | 
					
						
							| 
									
										
										
										
											2013-02-16 21:35:52 -08:00
										 |  |  |         void Delete(int id); | 
					
						
							| 
									
										
										
										
											2013-03-07 13:34:56 +09:00
										 |  |  |         void Delete(TModel model); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         void InsertMany(IList<TModel> model); | 
					
						
							|  |  |  |         void UpdateMany(IList<TModel> model); | 
					
						
							| 
									
										
										
										
											2013-03-06 13:20:33 -08:00
										 |  |  |         void DeleteMany(List<TModel> model); | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         void Purge(); | 
					
						
							| 
									
										
										
										
											2013-03-02 10:25:39 -08:00
										 |  |  |         bool HasItems(); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         void DeleteMany(IEnumerable<int> ids); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:16:55 -07:00
										 |  |  |         void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties); | 
					
						
							| 
									
										
										
										
											2013-04-24 21:27:49 -07:00
										 |  |  |         TModel Single(); | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-17 19:18:25 -08:00
										 |  |  |     public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new() | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |         private readonly IDatabase _database; | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |         private readonly IMessageAggregator _messageAggregator; | 
					
						
							| 
									
										
										
										
											2013-04-24 21:27:49 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //TODO: add assertion to make sure model properly mapped  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |         private IDataMapper DataMapper | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             get { return _database.DataMapper; } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-03-23 21:25:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |         public BasicRepository(IDatabase database, IMessageAggregator messageAggregator) | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             _database = database; | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  |             _messageAggregator = messageAggregator; | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 20:44:52 -07:00
										 |  |  |         protected QueryBuilder<TModel> Query | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             get { return DataMapper.Query<TModel>(); } | 
					
						
							| 
									
										
										
										
											2013-03-26 20:44:52 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         protected void Delete(Expression<Func<TModel, bool>> filter) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             DataMapper.Delete(filter); | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         public IEnumerable<TModel> All() | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             return DataMapper.Query<TModel>().ToList(); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:25:16 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |         public int Count() | 
					
						
							| 
									
										
										
										
											2013-03-23 21:25:16 -07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             return DataMapper.Query<TModel>().GetRowCount(); | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-15 19:50:22 -08:00
										 |  |  |         public TModel Get(int id) | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             return DataMapper.Query<TModel>().Single(c => c.Id == id); | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-17 17:33:38 -07:00
										 |  |  |         public IEnumerable<TModel> Get(IEnumerable<int> ids) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var idList = ids.ToList(); | 
					
						
							|  |  |  |             var result = Query.Where(String.Format("Id IN ({0})", String.Join(",", idList))).ToList(); | 
					
						
							|  |  |  |             var resultCount = result.Count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (resultCount != idList.Count || result.Select(r => r.Id).Distinct().Count() != resultCount) | 
					
						
							|  |  |  |                 throw new InvalidOperationException("Unexpected result from query"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-06 13:20:33 -08:00
										 |  |  |         public TModel SingleOrDefault() | 
					
						
							| 
									
										
										
										
											2013-04-24 21:27:49 -07:00
										 |  |  |         { | 
					
						
							|  |  |  |             return All().SingleOrDefault(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public TModel Single() | 
					
						
							| 
									
										
										
										
											2013-03-06 13:20:33 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |             return All().Single(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-18 22:56:02 -08:00
										 |  |  |         public TModel Insert(TModel model) | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-24 12:56:51 -07:00
										 |  |  |             if (model.Id != 0) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 throw new InvalidOperationException("Can't insert model with existing ID"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             DataMapper.Insert(model); | 
					
						
							| 
									
										
										
										
											2013-05-20 21:10:04 -07:00
										 |  |  |             PublishModelEvent(model, RepositoryAction.Created); | 
					
						
							| 
									
										
										
										
											2013-05-05 14:24:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |             return model; | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         public TModel Update(TModel model) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-24 12:56:51 -07:00
										 |  |  |             if (model.Id == 0) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 throw new InvalidOperationException("Can't update model with ID 0"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             DataMapper.Update(model, c => c.Id == model.Id); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |             return model; | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-07 13:34:56 +09:00
										 |  |  |         public void Delete(TModel model) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             DataMapper.Delete<TModel>(c => c.Id == model.Id); | 
					
						
							| 
									
										
										
										
											2013-03-07 13:34:56 +09:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         public void InsertMany(IList<TModel> models) | 
					
						
							| 
									
										
										
										
											2013-02-19 18:05:15 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             foreach (var model in models) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Insert(model); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-02-19 18:05:15 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         public void UpdateMany(IList<TModel> models) | 
					
						
							| 
									
										
										
										
											2013-02-19 18:05:15 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             foreach (var model in models) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Update(model); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-02-19 18:05:15 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         public void DeleteMany(List<TModel> models) | 
					
						
							| 
									
										
										
										
											2013-03-05 11:49:34 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             models.ForEach(Delete); | 
					
						
							| 
									
										
										
										
											2013-03-05 11:49:34 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         public TModel Upsert(TModel model) | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-02-25 19:58:57 -08:00
										 |  |  |             if (model.Id == 0) | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |                 Insert(model); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |                 return model; | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             Update(model); | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |             return model; | 
					
						
							| 
									
										
										
										
											2013-02-17 23:59:43 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-15 19:50:22 -08:00
										 |  |  |         public void Delete(int id) | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             DataMapper.Delete<TModel>(c => c.Id == id); | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         public void DeleteMany(IEnumerable<int> ids) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-24 20:51:32 -07:00
										 |  |  |             ids.ToList().ForEach(Delete); | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public void Purge() | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             DataMapper.Delete<TModel>(c => c.Id > -1); | 
					
						
							| 
									
										
										
										
											2013-02-23 11:38:25 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-03-02 10:25:39 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         public bool HasItems() | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |             return Count() > 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 23:16:55 -07:00
										 |  |  |         public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties) | 
					
						
							| 
									
										
										
										
											2013-03-23 21:16:00 -07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-03-23 21:25:16 -07:00
										 |  |  |             if (model.Id == 0) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 throw new InvalidOperationException("Attempted to updated model without ID"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 13:06:57 -07:00
										 |  |  |             DataMapper.Update<TModel>() | 
					
						
							| 
									
										
										
										
											2013-03-26 23:16:55 -07:00
										 |  |  |                 .Where(c => c.Id == model.Id) | 
					
						
							|  |  |  |                 .ColumnsIncluding(properties) | 
					
						
							|  |  |  |                 .Entity(model) | 
					
						
							|  |  |  |                 .Execute(); | 
					
						
							| 
									
										
										
										
											2013-03-02 10:25:39 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-05-10 16:53:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 21:10:04 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         protected virtual void PublishModelEvent(TModel model, RepositoryAction action) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _messageAggregator.PublishEvent(new ModelEvent<TModel>(model, action)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-04 20:07:07 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |