浏览代码

Add mapping from BaseItemKind to Type.FullName for querying

Cody Robibero 3 年之前
父节点
当前提交
249a1ca955
共有 1 个文件被更改,包括 63 次插入12 次删除
  1. 63 12
      Emby.Server.Implementations/Data/SqliteItemRepository.cs

+ 63 - 12
Emby.Server.Implementations/Data/SqliteItemRepository.cs

@@ -62,7 +62,7 @@ namespace Emby.Server.Implementations.Data
 
 
         private readonly ItemFields[] _allItemFields = Enum.GetValues<ItemFields>();
         private readonly ItemFields[] _allItemFields = Enum.GetValues<ItemFields>();
 
 
-        private static readonly string[] _retriveItemColumns =
+        private static readonly string[] _retrieveItemColumns =
         {
         {
             "type",
             "type",
             "data",
             "data",
@@ -133,7 +133,7 @@ namespace Emby.Server.Implementations.Data
             "OwnerId"
             "OwnerId"
         };
         };
 
 
-        private static readonly string _retriveItemColumnsSelectQuery = $"select {string.Join(',', _retriveItemColumns)} from TypedBaseItems where guid = @guid";
+        private static readonly string _retrieveItemColumnsSelectQuery = $"select {string.Join(',', _retrieveItemColumns)} from TypedBaseItems where guid = @guid";
 
 
         private static readonly string[] _mediaStreamSaveColumns =
         private static readonly string[] _mediaStreamSaveColumns =
         {
         {
@@ -284,6 +284,43 @@ namespace Emby.Server.Implementations.Data
 
 
         private readonly Dictionary<string, string> _types = GetTypeMapDictionary();
         private readonly Dictionary<string, string> _types = GetTypeMapDictionary();
 
 
+        private static readonly Dictionary<BaseItemKind, string> _baseItemKindNames = new()
+        {
+            { BaseItemKind.AggregateFolder, typeof(AggregateFolder).FullName },
+            { BaseItemKind.Audio, typeof(Audio).FullName },
+            { BaseItemKind.AudioBook, typeof(AudioBook).FullName },
+            { BaseItemKind.BasePluginFolder, typeof(BasePluginFolder).FullName },
+            { BaseItemKind.Book, typeof(Book).FullName },
+            { BaseItemKind.BoxSet, typeof(BoxSet).FullName },
+            { BaseItemKind.Channel, typeof(Channel).FullName },
+            { BaseItemKind.CollectionFolder, typeof(CollectionFolder).FullName },
+            { BaseItemKind.Episode, typeof(Episode).FullName },
+            { BaseItemKind.Folder, typeof(Folder).FullName },
+            { BaseItemKind.Genre, typeof(Genre).FullName },
+            { BaseItemKind.Movie, typeof(Movie).FullName },
+            { BaseItemKind.LiveTvChannel, typeof(LiveTvChannel).FullName },
+            { BaseItemKind.LiveTvProgram, typeof(LiveTvProgram).FullName },
+            { BaseItemKind.MusicAlbum, typeof(MusicAlbum).FullName },
+            { BaseItemKind.MusicArtist, typeof(MusicArtist).FullName },
+            { BaseItemKind.MusicGenre, typeof(MusicGenre).FullName },
+            { BaseItemKind.MusicVideo, typeof(MusicVideo).FullName },
+            { BaseItemKind.Person, typeof(Person).FullName },
+            { BaseItemKind.Photo, typeof(Photo).FullName },
+            { BaseItemKind.PhotoAlbum, typeof(PhotoAlbum).FullName },
+            { BaseItemKind.Playlist, typeof(Playlist).FullName },
+            { BaseItemKind.PlaylistsFolder, typeof(PlaylistsFolder).FullName },
+            { BaseItemKind.Season, typeof(Season).FullName },
+            { BaseItemKind.Series, typeof(Series).FullName },
+            { BaseItemKind.Studio, typeof(Studio).FullName },
+            { BaseItemKind.Trailer, typeof(Trailer).FullName },
+            { BaseItemKind.TvChannel, typeof(LiveTvChannel).FullName },
+            { BaseItemKind.TvProgram, typeof(LiveTvProgram).FullName },
+            { BaseItemKind.UserRootFolder, typeof(UserRootFolder).FullName },
+            { BaseItemKind.UserView, typeof(UserView).FullName },
+            { BaseItemKind.Video, typeof(Video).FullName },
+            { BaseItemKind.Year, typeof(Year).FullName }
+        };
+
         static SqliteItemRepository()
         static SqliteItemRepository()
         {
         {
             var queryPrefixText = new StringBuilder();
             var queryPrefixText = new StringBuilder();
@@ -1319,7 +1356,7 @@ namespace Emby.Server.Implementations.Data
 
 
             using (var connection = GetConnection(true))
             using (var connection = GetConnection(true))
             {
             {
-                using (var statement = PrepareStatement(connection, _retriveItemColumnsSelectQuery))
+                using (var statement = PrepareStatement(connection, _retrieveItemColumnsSelectQuery))
                 {
                 {
                     statement.TryBind("@guid", id);
                     statement.TryBind("@guid", id);
 
 
@@ -2629,7 +2666,7 @@ namespace Emby.Server.Implementations.Data
                 query.Limit = query.Limit.Value + 4;
                 query.Limit = query.Limit.Value + 4;
             }
             }
 
 
-            var columns = _retriveItemColumns.ToList();
+            var columns = _retrieveItemColumns.ToList();
             SetFinalColumnsToSelect(query, columns);
             SetFinalColumnsToSelect(query, columns);
             var commandTextBuilder = new StringBuilder("select ", 1024)
             var commandTextBuilder = new StringBuilder("select ", 1024)
                 .AppendJoin(',', columns)
                 .AppendJoin(',', columns)
@@ -2820,7 +2857,7 @@ namespace Emby.Server.Implementations.Data
                 query.Limit = query.Limit.Value + 4;
                 query.Limit = query.Limit.Value + 4;
             }
             }
 
 
-            var columns = _retriveItemColumns.ToList();
+            var columns = _retrieveItemColumns.ToList();
             SetFinalColumnsToSelect(query, columns);
             SetFinalColumnsToSelect(query, columns);
             var commandTextBuilder = new StringBuilder("select ", 512)
             var commandTextBuilder = new StringBuilder("select ", 512)
                 .AppendJoin(',', columns)
                 .AppendJoin(',', columns)
@@ -3569,23 +3606,37 @@ namespace Emby.Server.Implementations.Data
                 var excludeTypes = query.ExcludeItemTypes;
                 var excludeTypes = query.ExcludeItemTypes;
                 if (excludeTypes.Length == 1)
                 if (excludeTypes.Length == 1)
                 {
                 {
-                    whereClauses.Add("type<>@type");
-                    statement?.TryBind("@type", excludeTypes[0].ToString());
+                    if (_baseItemKindNames.TryGetValue(excludeTypes[0], out var excludeTypeName))
+                    {
+                        whereClauses.Add("type<>@type");
+                        statement?.TryBind("@type", excludeTypeName);
+                    }
                 }
                 }
                 else if (excludeTypes.Length > 1)
                 else if (excludeTypes.Length > 1)
                 {
                 {
-                    var inClause = string.Join(',', excludeTypes.Select(i => "'" + i + "'"));
+                    var inClause = string.Join(
+                        ',',
+                        excludeTypes
+                            .Select(i => _baseItemKindNames.TryGetValue(i, out var baseItemKindName) ? "'" + baseItemKindName + "'" : null)
+                            .Where(i => !string.IsNullOrEmpty(i)));
                     whereClauses.Add($"type not in ({inClause})");
                     whereClauses.Add($"type not in ({inClause})");
                 }
                 }
             }
             }
             else if (includeTypes.Length == 1)
             else if (includeTypes.Length == 1)
             {
             {
-                whereClauses.Add("type=@type");
-                statement?.TryBind("@type", includeTypes[0].ToString());
+                if (_baseItemKindNames.TryGetValue(includeTypes[0], out var includeTypeName))
+                {
+                    whereClauses.Add("type=@type");
+                    statement?.TryBind("@type", includeTypeName);
+                }
             }
             }
             else if (includeTypes.Length > 1)
             else if (includeTypes.Length > 1)
             {
             {
-                var inClause = string.Join(',', includeTypes.Select(i => "'" + i + "'"));
+                var inClause = string.Join(
+                    ',',
+                    includeTypes
+                        .Select(i => _baseItemKindNames.TryGetValue(i, out var baseItemKindName) ? "'" + baseItemKindName + "'" : null)
+                        .Where(i => !string.IsNullOrEmpty(i)));
                 whereClauses.Add($"type in ({inClause})");
                 whereClauses.Add($"type in ({inClause})");
             }
             }
 
 
@@ -5334,7 +5385,7 @@ AND Type = @InternalPersonType)");
                 stringBuilder.Clear();
                 stringBuilder.Clear();
             }
             }
 
 
-            List<string> columns = _retriveItemColumns.ToList();
+            List<string> columns = _retrieveItemColumns.ToList();
             // Unfortunately we need to add it to columns to ensure the order of the columns in the select
             // Unfortunately we need to add it to columns to ensure the order of the columns in the select
             if (!string.IsNullOrEmpty(itemCountColumns))
             if (!string.IsNullOrEmpty(itemCountColumns))
             {
             {