From b8fff306bf15927b96d1c280482d14c5a02d630d Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 8 Jan 2013 00:57:38 -0800 Subject: [PATCH] Goodbye EF! --- NzbDrone.Core.Test/App.config | 2 - NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 8 --- .../LogProviderTests/LogProviderFixture.cs | 5 -- NzbDrone.Core.Test/packages.config | 2 - NzbDrone.Core/App.config | 2 - NzbDrone.Core/CentralDispatch.cs | 4 -- NzbDrone.Core/Datastore/Connection.cs | 13 ----- NzbDrone.Core/Instrumentation/LogDbContext.cs | 20 ------- NzbDrone.Core/Instrumentation/LogProvider.cs | 42 +++++++++++--- NzbDrone.Core/NzbDrone.Core.csproj | 9 --- NzbDrone.Core/packages.config | 2 - NzbDrone.Web/Controllers/LogController.cs | 55 +++++-------------- NzbDrone.Web/NzbDrone.Web.csproj | 8 --- NzbDrone.Web/Views/System/Logs.cshtml | 10 ++-- NzbDrone.Web/Web.config | 2 - NzbDrone.Web/packages.config | 2 - 16 files changed, 52 insertions(+), 134 deletions(-) delete mode 100644 NzbDrone.Core/Instrumentation/LogDbContext.cs diff --git a/NzbDrone.Core.Test/App.config b/NzbDrone.Core.Test/App.config index 6b446d8e8..1b714ac64 100644 --- a/NzbDrone.Core.Test/App.config +++ b/NzbDrone.Core.Test/App.config @@ -1,9 +1,7 @@  -
- diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 3c1b49c32..46aad0b85 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -69,10 +69,6 @@ False ..\Libraries\DeskMetrics\DeskMetrics.NET.dll - - False - ..\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll - ..\packages\NBuilder.3.0.1.1\lib\FizzWare.NBuilder.dll @@ -127,10 +123,6 @@ True ..\packages\Microsoft.SqlServer.Compact.4.0.8876.1\lib\net40\System.Data.SqlServerCe.dll - - True - ..\packages\EntityFramework.SqlServerCompact.4.3.6\lib\net40\System.Data.SqlServerCe.Entity.dll - diff --git a/NzbDrone.Core.Test/ProviderTests/LogProviderTests/LogProviderFixture.cs b/NzbDrone.Core.Test/ProviderTests/LogProviderTests/LogProviderFixture.cs index 1f9742029..665c1800f 100644 --- a/NzbDrone.Core.Test/ProviderTests/LogProviderTests/LogProviderFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/LogProviderTests/LogProviderFixture.cs @@ -19,7 +19,6 @@ namespace NzbDrone.Core.Test.ProviderTests.LogProviderTests { private const string LOGGER_NAME = "Core.Test.ProviderTests.LogProviderTests.LogProviderFixture"; - private static LogDbContext dbContext; private static string UniqueMessage; Logger Logger; @@ -29,10 +28,6 @@ namespace NzbDrone.Core.Test.ProviderTests.LogProviderTests { WithRealDb(); - dbContext = Connection.GetLogDbContext(TestDbHelper.ConnectionString); - - Mocker.SetConstant(dbContext); - new DatabaseTarget(Db).Register(); Logger = LogManager.GetCurrentClassLogger(); diff --git a/NzbDrone.Core.Test/packages.config b/NzbDrone.Core.Test/packages.config index 2bdc043fe..b65271bdb 100644 --- a/NzbDrone.Core.Test/packages.config +++ b/NzbDrone.Core.Test/packages.config @@ -3,8 +3,6 @@ - - diff --git a/NzbDrone.Core/App.config b/NzbDrone.Core/App.config index 1e9033de7..fc91a3745 100644 --- a/NzbDrone.Core/App.config +++ b/NzbDrone.Core/App.config @@ -1,9 +1,7 @@  -
- diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 8af83f05a..87210a0b0 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -63,10 +63,6 @@ namespace NzbDrone.Core ContainerBuilder.Register(c => c.Resolve().GetLogPetaPocoDb()) .Named("LogProvider"); - ContainerBuilder.Register(c => c.Resolve().GetLogEfContext()) - .As() - .SingleInstance(); - ContainerBuilder.RegisterType().WithParameter(ResolvedParameter.ForNamed("DatabaseTarget")); ContainerBuilder.RegisterType().WithParameter(ResolvedParameter.ForNamed("LogProvider")); } diff --git a/NzbDrone.Core/Datastore/Connection.cs b/NzbDrone.Core/Datastore/Connection.cs index 5f9d15b1a..aaa16fb27 100644 --- a/NzbDrone.Core/Datastore/Connection.cs +++ b/NzbDrone.Core/Datastore/Connection.cs @@ -59,11 +59,6 @@ namespace NzbDrone.Core.Datastore return GetPetaPocoDb(LogConnectionString, profiled); } - public LogDbContext GetLogEfContext() - { - return GetLogDbContext(LogConnectionString); - } - public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true) { MigrationsHelper.Run(connectionString, true); @@ -81,13 +76,5 @@ namespace NzbDrone.Core.Datastore return db; } - - public static LogDbContext GetLogDbContext(string connectionString) - { - MigrationsHelper.Run(connectionString, true); - DbConnection connection = new SqlCeConnection(connectionString); - return new LogDbContext(connection); - } - } } diff --git a/NzbDrone.Core/Instrumentation/LogDbContext.cs b/NzbDrone.Core/Instrumentation/LogDbContext.cs deleted file mode 100644 index e8d21a2d3..000000000 --- a/NzbDrone.Core/Instrumentation/LogDbContext.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Data.Common; -using System.Data.Entity; -using System.Linq; - -namespace NzbDrone.Core.Instrumentation -{ - public class LogDbContext : DbContext - { - public LogDbContext(DbConnection connection) - : base(connection, false) - { - } - - public LogDbContext() - { - } - - public DbSet Logs { get; set; } - } -} diff --git a/NzbDrone.Core/Instrumentation/LogProvider.cs b/NzbDrone.Core/Instrumentation/LogProvider.cs index 792cb2e73..84a5c3c53 100644 --- a/NzbDrone.Core/Instrumentation/LogProvider.cs +++ b/NzbDrone.Core/Instrumentation/LogProvider.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; +using DataTables.Mvc.Core.Helpers; +using DataTables.Mvc.Core.Models; using NLog; using NzbDrone.Common; using PetaPoco; @@ -10,24 +12,20 @@ namespace NzbDrone.Core.Instrumentation public class LogProvider { private readonly IDatabase _database; - private readonly LogDbContext _logDbContext; private readonly DiskProvider _diskProvider; private readonly EnvironmentProvider _environmentProvider; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - - - public LogProvider(IDatabase database, LogDbContext logDbContext, DiskProvider diskProvider, EnvironmentProvider environmentProvider) + public LogProvider(IDatabase database, DiskProvider diskProvider, EnvironmentProvider environmentProvider) { _database = database; - _logDbContext = logDbContext; _diskProvider = diskProvider; _environmentProvider = environmentProvider; } - public IQueryable GetAllLogs() + public List GetAllLogs() { - return _logDbContext.Logs; + return _database.Fetch(); } public IList TopLogs(int count) @@ -44,9 +42,35 @@ namespace NzbDrone.Core.Instrumentation return logs; } - public Page GetPagedLogs(int pageNumber, int pageSize) + public virtual Page GetPagedItems(DataTablesPageRequest pageRequest) + { + var query = Sql.Builder + .Select(@"*") + .From("Logs"); + + var startPage = (pageRequest.DisplayLength == 0) ? 1 : pageRequest.DisplayStart / pageRequest.DisplayLength + 1; + + if (!string.IsNullOrEmpty(pageRequest.Search)) + { + var whereClause = string.Join(" OR ", SqlBuilderHelper.GetSearchClause(pageRequest)); + + if (!string.IsNullOrEmpty(whereClause)) + query.Append("WHERE " + whereClause, "%" + pageRequest.Search + "%"); + } + + var orderBy = string.Join(",", SqlBuilderHelper.GetOrderByClause(pageRequest)); + + if (!string.IsNullOrEmpty(orderBy)) + { + query.Append("ORDER BY " + orderBy); + } + + return _database.Page(startPage, pageRequest.DisplayLength, query); + } + + public virtual long Count() { - return _database.Page(pageNumber, pageSize, "SELECT * FROM Logs ORDER BY Time DESC"); + return _database.Single(@"SELECT COUNT(*) from Logs"); } public void DeleteAll() diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index ef1622891..7c2b17564 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -136,10 +136,6 @@ ..\Libraries\DeskMetrics\DeskMetrics.NET.dll - - False - ..\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll - ..\packages\Growl.0.6\lib\Growl.Connector.dll @@ -199,10 +195,6 @@ True ..\packages\Microsoft.SqlServer.Compact.4.0.8876.1\lib\net40\System.Data.SqlServerCe.dll - - True - ..\packages\EntityFramework.SqlServerCompact.4.3.6\lib\net40\System.Data.SqlServerCe.Entity.dll - @@ -279,7 +271,6 @@ - diff --git a/NzbDrone.Core/packages.config b/NzbDrone.Core/packages.config index 07e295284..9672ac959 100644 --- a/NzbDrone.Core/packages.config +++ b/NzbDrone.Core/packages.config @@ -3,8 +3,6 @@ - - diff --git a/NzbDrone.Web/Controllers/LogController.cs b/NzbDrone.Web/Controllers/LogController.cs index 4cb3962c7..ea23d01d7 100644 --- a/NzbDrone.Web/Controllers/LogController.cs +++ b/NzbDrone.Web/Controllers/LogController.cs @@ -49,53 +49,26 @@ namespace NzbDrone.Web.Controllers public ActionResult AjaxBinding(DataTablesPageRequest pageRequest) { - var logs = _logProvider.GetAllLogs(); - var totalCount = logs.Count(); + var pageResult = _logProvider.GetPagedItems(pageRequest); + var totalItems = _logProvider.Count(); - IQueryable q = logs; - if (!string.IsNullOrEmpty(pageRequest.Search)) + var items = pageResult.Items.Select(l => new LogModel { - q = q.Where(b => b.Logger.Contains(pageRequest.Search) - || b.Exception.Contains(pageRequest.Search) - || b.Message.Contains(pageRequest.Search)); - } - - int filteredCount = q.Count(); - - IQueryable sorted = q; - - for (int i = 0; i < pageRequest.SortingCols; i++) - { - int sortCol = pageRequest.SortCol[i]; - var sortColName = sortCol == 0 ? "Time" : sortCol == 1 ? "Level" : "Logger"; - var sortExpression = String.Format("{0} {1}", sortColName, pageRequest.SortDir[i]); - - sorted = sorted.OrderBy(sortExpression); - } - - IQueryable filteredAndSorted = sorted; - if (filteredCount > pageRequest.DisplayLength) - { - filteredAndSorted = sorted.Skip(pageRequest.DisplayStart).Take(pageRequest.DisplayLength); - } - - var logModels = filteredAndSorted.ToList().Select(s => new LogModel - { - Time = s.Time.ToString(), - Level = s.Level, - Source = s.Logger, - Message = s.Message, - Method = s.Method, - ExceptionType = s.ExceptionType, - Exception = s.Exception - }); + Time = l.Time.ToString(), + Level = l.Level, + Source = l.Logger, + Message = l.Message, + Method = l.Method, + ExceptionType = l.ExceptionType, + Exception = l.Exception + }); return Json(new { sEcho = pageRequest.Echo, - iTotalRecords = totalCount, - iTotalDisplayRecords = filteredCount, - aaData = logModels + iTotalRecords = totalItems, + iTotalDisplayRecords = pageResult.TotalItems, + aaData = items }, JsonRequestBehavior.AllowGet); } diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index e567f8d2e..92127348c 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -69,10 +69,6 @@ ..\packages\DynamicQuery.1.0\lib\35\Dynamic.dll - - False - ..\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll - ..\packages\LowercaseRoutesMVC.1.0.3\lib\LowercaseRoutesMVC.dll @@ -151,10 +147,6 @@ True ..\packages\Microsoft.SqlServer.Compact.4.0.8876.1\lib\net40\System.Data.SqlServerCe.dll - - True - ..\packages\EntityFramework.SqlServerCompact.4.3.6\lib\net40\System.Data.SqlServerCe.Entity.dll - diff --git a/NzbDrone.Web/Views/System/Logs.cshtml b/NzbDrone.Web/Views/System/Logs.cshtml index 3dd8ce0bf..7406acda3 100644 --- a/NzbDrone.Web/Views/System/Logs.cshtml +++ b/NzbDrone.Web/Views/System/Logs.cshtml @@ -62,11 +62,11 @@ "iDisplayLength": 50, "sPaginationType": "four_button", "aoColumns": [ - { sWidth: '150px', "mDataProp": "Time" }, //Time - {sWidth: '60px', "mDataProp": "Level" }, //Level - {sWidth: '240px', "mDataProp": "Source" }, //Source - {sWidth: 'auto', "mDataProp": "Message", "bSortable": false }, //Message - {sWidth: 'auto', "mDataProp": "Details", "bSortable": false, "bVisible": false, "fnRender": function (row) { + { sName: "Time", sWidth: '150px', "mDataProp": "Time", bSearchable: false }, //Time + { sName: "Level", sWidth: '60px', "mDataProp": "Level" }, //Level + { sName: "Logger", sWidth: '240px', "mDataProp": "Source" }, //Source + { sName: "Message", sWidth: 'auto', "mDataProp": "Message", "bSortable": false }, //Message + { sName: "Exception", sWidth: 'auto', "mDataProp": "Details", "bSortable": false, "bVisible": false, "fnRender": function (row) { var result = "
Method: " + row.aData["Method"] + "
"; if (row.aData["ExceptionType"] !== null) { diff --git a/NzbDrone.Web/Web.config b/NzbDrone.Web/Web.config index c9b3f01c6..7c9e049a3 100644 --- a/NzbDrone.Web/Web.config +++ b/NzbDrone.Web/Web.config @@ -1,9 +1,7 @@  -
- diff --git a/NzbDrone.Web/packages.config b/NzbDrone.Web/packages.config index c386675b2..86e2d5b33 100644 --- a/NzbDrone.Web/packages.config +++ b/NzbDrone.Web/packages.config @@ -6,8 +6,6 @@ - -