Added table identifier to OrderBy to avoid column ambiguity on joins

Co-Authored-By: Richard <1252123+kharenis@users.noreply.github.com>
This commit is contained in:
Qstick 2023-08-10 21:07:27 -05:00
parent a13011aa49
commit c57ceac4de
2 changed files with 9 additions and 14 deletions

View File

@ -429,7 +429,7 @@ namespace NzbDrone.Core.Datastore
var sortKey = TableMapping.Mapper.GetSortKey(pagingSpec.SortKey); var sortKey = TableMapping.Mapper.GetSortKey(pagingSpec.SortKey);
var sortDirection = pagingSpec.SortDirection == SortDirection.Descending ? "DESC" : "ASC"; var sortDirection = pagingSpec.SortDirection == SortDirection.Descending ? "DESC" : "ASC";
var pagingOffset = Math.Max(pagingSpec.Page - 1, 0) * pagingSpec.PageSize; 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(); return queryFunc(builder).ToList();
} }

View File

@ -91,33 +91,28 @@ namespace NzbDrone.Core.Datastore
return true; return true;
} }
public string GetSortKey(string sortKey) public (string Table, string Column) GetSortKey(string sortKey)
{ {
string table = null; string table = null;
if (sortKey.Contains('.')) if (sortKey.Contains('.'))
{ {
var split = sortKey.Split('.'); var split = sortKey.Split('.');
if (split.Length != 2) if (split.Length == 2)
{ {
return sortKey;
}
table = split[0]; table = split[0];
sortKey = split[1]; sortKey = split[1];
} }
if (table != null && !TableMap.Values.Contains(table, StringComparer.OrdinalIgnoreCase))
{
return sortKey;
} }
if (!_allowedOrderBy.Contains(sortKey)) if (table != null)
{ {
return sortKey; table = TableMap.Values.FirstOrDefault(x => x.Equals(table, StringComparison.OrdinalIgnoreCase)) ?? table;
} }
return _allowedOrderBy.First(x => x.Equals(sortKey, StringComparison.OrdinalIgnoreCase)); sortKey = _allowedOrderBy.FirstOrDefault(x => x.Equals(sortKey, StringComparison.OrdinalIgnoreCase)) ?? sortKey;
return (table, sortKey);
} }
} }