Reworked TableMapping.Mapper.GetSortKey to return correct case table and column where possible.

This commit is contained in:
Kharenis 2023-08-01 17:31:56 +01:00
parent b01e72311e
commit 58c9788e35
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($"\"{_table}\".\"{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);
}
}