From c57ceac4debf7419be84096f997ba7b75c906586 Mon Sep 17 00:00:00 2001 From: Qstick Date: Thu, 10 Aug 2023 21:07:27 -0500 Subject: [PATCH] Added table identifier to OrderBy to avoid column ambiguity on joins Co-Authored-By: Richard <1252123+kharenis@users.noreply.github.com> --- .../Datastore/BasicRepository.cs | 2 +- src/NzbDrone.Core/Datastore/TableMapper.cs | 21 +++++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index 86e1240e4..b5e57be87 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -429,7 +429,7 @@ namespace NzbDrone.Core.Datastore var sortKey = TableMapping.Mapper.GetSortKey(pagingSpec.SortKey); var sortDirection = pagingSpec.SortDirection == SortDirection.Descending ? "DESC" : "ASC"; var pagingOffset = Math.Max(pagingSpec.Page - 1, 0) * pagingSpec.PageSize; - builder.OrderBy($"\"{sortKey}\" {sortDirection} LIMIT {pagingSpec.PageSize} OFFSET {pagingOffset}"); + builder.OrderBy($"\"{sortKey.Table ?? _table}\".\"{sortKey.Column}\" {sortDirection} LIMIT {pagingSpec.PageSize} OFFSET {pagingOffset}"); return queryFunc(builder).ToList(); } diff --git a/src/NzbDrone.Core/Datastore/TableMapper.cs b/src/NzbDrone.Core/Datastore/TableMapper.cs index 24ab5869e..7107e97c0 100644 --- a/src/NzbDrone.Core/Datastore/TableMapper.cs +++ b/src/NzbDrone.Core/Datastore/TableMapper.cs @@ -91,33 +91,28 @@ namespace NzbDrone.Core.Datastore return true; } - public string GetSortKey(string sortKey) + public (string Table, string Column) GetSortKey(string sortKey) { string table = null; if (sortKey.Contains('.')) { var split = sortKey.Split('.'); - if (split.Length != 2) + if (split.Length == 2) { - return sortKey; + table = split[0]; + sortKey = split[1]; } - - table = split[0]; - sortKey = split[1]; } - if (table != null && !TableMap.Values.Contains(table, StringComparer.OrdinalIgnoreCase)) + if (table != null) { - return sortKey; + table = TableMap.Values.FirstOrDefault(x => x.Equals(table, StringComparison.OrdinalIgnoreCase)) ?? table; } - if (!_allowedOrderBy.Contains(sortKey)) - { - return sortKey; - } + sortKey = _allowedOrderBy.FirstOrDefault(x => x.Equals(sortKey, StringComparison.OrdinalIgnoreCase)) ?? sortKey; - return _allowedOrderBy.First(x => x.Equals(sortKey, StringComparison.OrdinalIgnoreCase)); + return (table, sortKey); } }