Browse Source

update next up queries

Luke Pulverenti 8 years ago
parent
commit
d84bb7160f

+ 61 - 1
Emby.Server.Implementations/Data/SqliteItemRepository.cs

@@ -2443,6 +2443,66 @@ namespace Emby.Server.Implementations.Data
             return " from TypedBaseItems " + alias;
         }
 
+        public int GetCount(InternalItemsQuery query)
+        {
+            if (query == null)
+            {
+                throw new ArgumentNullException("query");
+            }
+
+            CheckDisposed();
+
+            //Logger.Info("GetItemList: " + _environmentInfo.StackTrace);
+
+            var now = DateTime.UtcNow;
+
+            // Hack for right now since we currently don't support filtering out these duplicates within a query
+            if (query.Limit.HasValue && query.EnableGroupByMetadataKey)
+            {
+                query.Limit = query.Limit.Value + 4;
+            }
+
+            var commandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, new [] { "count(distinct PresentationUniqueKey)" })) + GetFromText();
+            commandText += GetJoinUserDataText(query);
+
+            var whereClauses = GetWhereClauses(query, null);
+
+            var whereText = whereClauses.Count == 0 ?
+                string.Empty :
+                " where " + string.Join(" AND ", whereClauses.ToArray());
+
+            commandText += whereText;
+
+            //commandText += GetGroupBy(query);
+
+            int count = 0;
+
+            using (WriteLock.Read())
+            {
+                using (var connection = CreateConnection(true))
+                {
+                    using (var statement = PrepareStatementSafe(connection, commandText))
+                    {
+                        if (EnableJoinUserData(query))
+                        {
+                            statement.TryBind("@UserId", query.User.Id);
+                        }
+
+                        BindSimilarParams(query, statement);
+
+                        // Running this again will bind the params
+                        GetWhereClauses(query, statement);
+
+                        count = statement.ExecuteQuery().SelectScalarInt().First();
+                    }
+                }
+
+                LogQueryTime("GetCount", commandText, now);
+            }
+
+            return count;
+        }
+
         public List<BaseItem> GetItemList(InternalItemsQuery query)
         {
             if (query == null)
@@ -2859,7 +2919,7 @@ namespace Emby.Server.Implementations.Data
             }
             if (string.Equals(name, ItemSortBy.SeriesDatePlayed, StringComparison.OrdinalIgnoreCase))
             {
-                return new Tuple<string, bool>("(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)", false);
+                return new Tuple<string, bool>("(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where Played=1 and B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)", false);
             }
 
             return new Tuple<string, bool>(name, false);

+ 20 - 0
Emby.Server.Implementations/Library/LibraryManager.cs

@@ -1279,6 +1279,26 @@ namespace Emby.Server.Implementations.Library
             return ItemRepository.GetItemList(query);
         }
 
+        public int GetCount(InternalItemsQuery query)
+        {
+            if (query.Recursive && query.ParentId.HasValue)
+            {
+                var parent = GetItemById(query.ParentId.Value);
+                if (parent != null)
+                {
+                    SetTopParentIdsOrAncestors(query, new List<BaseItem> { parent });
+                    query.ParentId = null;
+                }
+            }
+
+            if (query.User != null)
+            {
+                AddUserToQuery(query, query.User);
+            }
+
+            return ItemRepository.GetCount(query);
+        }
+
         public IEnumerable<BaseItem> GetItemList(InternalItemsQuery query, List<BaseItem> parents)
         {
             SetTopParentIdsOrAncestors(query, parents);

+ 20 - 8
MediaBrowser.Controller/Entities/TV/Series.cs

@@ -138,16 +138,24 @@ namespace MediaBrowser.Controller.Entities.TV
             var enableSeriesPresentationKey = ConfigurationManager.Configuration.EnableSeriesPresentationUniqueKey;
             var seriesKey = GetUniqueSeriesKey(this);
 
-            var result = LibraryManager.GetItemsResult(new InternalItemsQuery(user)
+            var result = LibraryManager.GetCount(new InternalItemsQuery(user)
             {
                 AncestorWithPresentationUniqueKey = enableSeriesPresentationKey ? null : seriesKey,
                 SeriesPresentationUniqueKey = enableSeriesPresentationKey ? seriesKey : null,
                 IncludeItemTypes = new[] { typeof(Season).Name },
                 IsVirtualItem = false,
-                Limit = 0
+                Limit = 0,
+                DtoOptions = new Dto.DtoOptions
+                {
+                    Fields = new List<ItemFields>
+                    {
+
+                    },
+                    EnableImages = false
+                }
             });
 
-            return result.TotalRecordCount;
+            return result;
         }
 
         public override int GetRecursiveChildCount(User user)
@@ -159,19 +167,23 @@ namespace MediaBrowser.Controller.Entities.TV
             {
                 AncestorWithPresentationUniqueKey = enableSeriesPresentationKey ? null : seriesKey,
                 SeriesPresentationUniqueKey = enableSeriesPresentationKey ? seriesKey : null,
+                DtoOptions = new Dto.DtoOptions
+                {
+                    Fields = new List<ItemFields>
+                    {
+                        
+                    },
+                    EnableImages = false
+                }
             };
 
-            if (query.SortBy.Length == 0)
-            {
-                query.SortBy = new[] { ItemSortBy.SortName };
-            }
             if (query.IncludeItemTypes.Length == 0)
             {
                 query.IncludeItemTypes = new[] { typeof(Episode).Name, typeof(Season).Name };
             }
             query.IsVirtualItem = false;
             query.Limit = 0;
-            var totalRecordCount = LibraryManager.GetItemsResult(query).TotalRecordCount;
+            var totalRecordCount = LibraryManager.GetCount(query);
 
             return totalRecordCount;
         }

+ 1 - 0
MediaBrowser.Controller/Library/ILibraryManager.cs

@@ -571,5 +571,6 @@ namespace MediaBrowser.Controller.Library
 
         void RegisterIgnoredPath(string path);
         void UnRegisterIgnoredPath(string path);
+        int GetCount(InternalItemsQuery query);
     }
 }

+ 2 - 0
MediaBrowser.Controller/Persistence/IItemRepository.cs

@@ -163,6 +163,8 @@ namespace MediaBrowser.Controller.Persistence
         /// <returns>Task.</returns>
         Task UpdateInheritedValues(CancellationToken cancellationToken);
 
+        int GetCount(InternalItemsQuery query);
+
         QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery query);
         QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuery query);
         QueryResult<Tuple<BaseItem, ItemCounts>> GetGameGenres(InternalItemsQuery query);