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 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();
}

View File

@ -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);
}
}