Skip to content

Commit

Permalink
Add SortOrder enum and use in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Feb 18, 2019
1 parent 9d9f339 commit d3d0a9a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public override MethodCallCodeFragment GenerateFluentApi(IIndex index, IAnnotati
return new MethodCallCodeFragment(nameof(NpgsqlIndexBuilderExtensions.ForNpgsqlHasOperators), annotation.Value);
if (annotation.Name == NpgsqlAnnotationNames.IndexCollation)
return new MethodCallCodeFragment(nameof(NpgsqlIndexBuilderExtensions.ForNpgsqlHasCollation), annotation.Value);
if (annotation.Name == NpgsqlAnnotationNames.IndexDescendingOrder)
if (annotation.Name == NpgsqlAnnotationNames.IndexSortOrder)
return new MethodCallCodeFragment(nameof(NpgsqlIndexBuilderExtensions.ForNpgsqlHasDescendingOrder), annotation.Value);
if (annotation.Name == NpgsqlAnnotationNames.IndexNullsFirst)
return new MethodCallCodeFragment(nameof(NpgsqlIndexBuilderExtensions.ForNpgsqlHasNullsFirst), annotation.Value);
Expand Down
5 changes: 3 additions & 2 deletions src/EFCore.PG/Extensions/NpgsqlIndexBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Utilities;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -88,11 +89,11 @@ public static IndexBuilder ForNpgsqlHasCollation(
/// <returns> A builder to further configure the index. </returns>
public static IndexBuilder ForNpgsqlHasDescendingOrder(
[NotNull] this IndexBuilder indexBuilder,
[CanBeNull, ItemNotNull] params bool?[] values)
[CanBeNull] params SortOrder[] values)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

indexBuilder.Metadata.Npgsql().DescendingOrder = values;
indexBuilder.Metadata.Npgsql().SortOrder = values;

return indexBuilder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.PG/Metadata/INpgsqlIndexAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface INpgsqlIndexAnnotations : IRelationalIndexAnnotations
/// <remarks>
/// https://www.postgresql.org/docs/current/static/indexes-ordering.html
/// </remarks>
IReadOnlyList<bool?> DescendingOrder { get; }
IReadOnlyList<SortOrder> SortOrder { get; }

/// <summary>
/// The column NULL sort orders to be used, or <c>null</c> if they have not been specified.
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.PG/Metadata/Internal/NpgsqlAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class NpgsqlAnnotationNames
public const string IndexMethod = Prefix + "IndexMethod";
public const string IndexOperators = Prefix + "IndexOperators";
public const string IndexCollation = Prefix + "IndexCollation";
public const string IndexDescendingOrder = Prefix + "IndexDescendingOrder";
public const string IndexSortOrder = Prefix + "IndexSortOrder";
public const string IndexNullsFirst = Prefix + "IndexNullsFirst";
public const string IndexInclude = Prefix + "IndexInclude";
public const string PostgresExtensionPrefix = Prefix + "PostgresExtension:";
Expand Down
12 changes: 6 additions & 6 deletions src/EFCore.PG/Metadata/NpgsqlIndexAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public string[] Collation
/// <remarks>
/// https://www.postgresql.org/docs/current/static/indexes-ordering.html
/// </remarks>
public bool?[] DescendingOrder
public SortOrder[] SortOrder
{
get => (bool?[])Annotations.Metadata[NpgsqlAnnotationNames.IndexDescendingOrder];
set => SetDescendingOrder(value);
get => (SortOrder[])Annotations.Metadata[NpgsqlAnnotationNames.IndexSortOrder];
set => SetSortOrder(value);
}

IReadOnlyList<bool?> INpgsqlIndexAnnotations.DescendingOrder => DescendingOrder;
IReadOnlyList<SortOrder> INpgsqlIndexAnnotations.SortOrder => SortOrder;

/// <summary>
/// The column NULL sort orders to be used, or <c>null</c> if they have not been specified.
Expand Down Expand Up @@ -108,8 +108,8 @@ protected virtual bool SetOperators(string[] value)
protected virtual bool SetCollation(string[] value)
=> Annotations.SetAnnotation(NpgsqlAnnotationNames.IndexCollation, value);

protected virtual bool SetDescendingOrder(bool?[] value)
=> Annotations.SetAnnotation(NpgsqlAnnotationNames.IndexDescendingOrder, value);
protected virtual bool SetSortOrder(SortOrder[] value)
=> Annotations.SetAnnotation(NpgsqlAnnotationNames.IndexSortOrder, value);

protected virtual bool SetNullsFirst(bool?[] value)
=> Annotations.SetAnnotation(NpgsqlAnnotationNames.IndexNullsFirst, value);
Expand Down
23 changes: 23 additions & 0 deletions src/EFCore.PG/Metadata/SortOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata
{
/// <summary>
/// Options for modifying sort ordering of index values.
/// </summary>
public enum SortOrder
{
/// <summary>
/// Represents an unspecified sort order. The database default will be used.
/// </summary>
Unspecified = 0,

/// <summary>
/// Specifies ascending sort order, which is the default.
/// </summary>
Ascending = 1,

/// <summary>
/// Specifies descending sort order.
/// </summary>
Descending = 2,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public override IEnumerable<IAnnotation> For(IIndex index)
yield return new Annotation(NpgsqlAnnotationNames.IndexOperators, index.Npgsql().Operators);
if (index.Npgsql().Collation != null)
yield return new Annotation(NpgsqlAnnotationNames.IndexCollation, index.Npgsql().Collation);
if (index.Npgsql().DescendingOrder != null)
yield return new Annotation(NpgsqlAnnotationNames.IndexDescendingOrder, index.Npgsql().DescendingOrder);
if (index.Npgsql().SortOrder != null)
yield return new Annotation(NpgsqlAnnotationNames.IndexSortOrder, index.Npgsql().SortOrder);
if (index.Npgsql().NullsFirst != null)
yield return new Annotation(NpgsqlAnnotationNames.IndexNullsFirst, index.Npgsql().NullsFirst);
if (index.Npgsql().IncludeProperties != null)
Expand Down
28 changes: 20 additions & 8 deletions src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,21 @@ string IndexColumnList(IndexColumn[] columns)
builder.Append(" COLLATE ").Append(column.Collation);
}

if (column.DescendingOrder.HasValue)
if (column.SortOrder != SortOrder.Unspecified)
{
builder.Append(" ").Append(column.DescendingOrder.Value ? "DESC" : "ASC");
builder.Append(" ");

switch (column.SortOrder)
{
case SortOrder.Ascending:
builder.Append("ASC");
break;
case SortOrder.Descending:
builder.Append("DESC");
break;
default:
throw new ArgumentOutOfRangeException();
}
}

if (column.NullsFirst.HasValue)
Expand Down Expand Up @@ -1265,7 +1277,7 @@ static IndexColumn[] GetIndexColumns(CreateIndexOperation operation)
{
var operators = operation[NpgsqlAnnotationNames.IndexOperators] as string[];
var collations = operation[NpgsqlAnnotationNames.IndexCollation] as string[];
var descendingOrders = operation[NpgsqlAnnotationNames.IndexDescendingOrder] as bool?[];
var sortOrders = operation[NpgsqlAnnotationNames.IndexSortOrder] as SortOrder[];
var nullsFirsts = operation[NpgsqlAnnotationNames.IndexNullsFirst] as bool?[];

var columns = new IndexColumn[operation.Columns.Length];
Expand All @@ -1275,23 +1287,23 @@ static IndexColumn[] GetIndexColumns(CreateIndexOperation operation)
var name = operation.Columns[i];
var @operator = i < operators?.Length ? operators[i] : null;
var collation = i < collations?.Length ? collations[i] : null;
var descendingOrder = i < descendingOrders?.Length ? descendingOrders[i] : null;
var sortOrder = i < sortOrders?.Length ? sortOrders[i] : SortOrder.Unspecified;
var nullsFirst = i < nullsFirsts?.Length ? nullsFirsts[i] : null;

columns[i] = new IndexColumn(name, @operator, collation, descendingOrder, nullsFirst);
columns[i] = new IndexColumn(name, @operator, collation, sortOrder, nullsFirst);
}

return columns;
}

struct IndexColumn
{
public IndexColumn(string name, string @operator, string collation, bool? descendingOrder, bool? nullsFirst)
public IndexColumn(string name, string @operator, string collation, SortOrder sortOrder, bool? nullsFirst)
{
Name = name;
Operator = @operator;
Collation = collation;
DescendingOrder = descendingOrder;
SortOrder = sortOrder;
NullsFirst = nullsFirst;
}

Expand All @@ -1301,7 +1313,7 @@ public IndexColumn(string name, string @operator, string collation, bool? descen

public string Collation { get; }

public bool? DescendingOrder { get; }
public SortOrder SortOrder { get; }

public bool? NullsFirst { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ public void CreateIndexOperation_sort_order()
Table = "People",
Schema = "dbo",
Columns = new[] { "FirstName", "MiddleName", "LastName" },
[NpgsqlAnnotationNames.IndexDescendingOrder] = new bool?[] { true, null, false }
[NpgsqlAnnotationNames.IndexSortOrder] = new bool?[] { true, null, false }
});

Assert.Equal(
Expand Down

0 comments on commit d3d0a9a

Please sign in to comment.