Skip to content

Commit

Permalink
Require in-support version for array translations
Browse files Browse the repository at this point in the history
  • Loading branch information
austindrenski committed Dec 12, 2018
1 parent 75293ec commit eabbc36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ public class NpgsqlArrayMemberTranslator : IMemberTranslator

/// <inheritdoc />
public Expression Translate(MemberExpression e)
=> ArrayInstanceHandler(e) ??
ListInstanceHandler(e);
{
if (!VersionAtLeast(9, 4))
return null;

return ArrayInstanceHandler(e) ??
ListInstanceHandler(e);
}

#region Handlers

[CanBeNull]
Expression ArrayInstanceHandler([NotNull] MemberExpression e)
static Expression ArrayInstanceHandler([NotNull] MemberExpression e)
{
var instance = e.Expression;

Expand All @@ -44,15 +49,15 @@ Expression ArrayInstanceHandler([NotNull] MemberExpression e)

switch (e.Member.Name)
{
case nameof(Array.Length) when VersionAtLeast(8, 4):
case nameof(Array.Length):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
typeof(int?),
new[] { instance, Expression.Constant(1) }),
Expression.Constant(0));

case nameof(Array.Rank) when VersionAtLeast(8, 4):
case nameof(Array.Rank):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_ndims",
Expand All @@ -66,7 +71,7 @@ Expression ArrayInstanceHandler([NotNull] MemberExpression e)
}

[CanBeNull]
Expression ListInstanceHandler([NotNull] MemberExpression e)
static Expression ListInstanceHandler([NotNull] MemberExpression e)
{
var instance = e.Expression;

Expand All @@ -75,7 +80,7 @@ Expression ListInstanceHandler([NotNull] MemberExpression e)

switch (e.Member.Name)
{
case nameof(IList.Count) when VersionAtLeast(8, 4):
case nameof(IList.Count):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class NpgsqlArrayMethodCallTranslator : IMethodCallTranslator
[CanBeNull]
public Expression Translate(MethodCallExpression e)
{
if (!VersionAtLeast(9, 4))
return null;

var declaringType = e.Method.DeclaringType;

if (declaringType != null &&
Expand Down Expand Up @@ -68,7 +71,7 @@ Expression EnumerableHandler([NotNull] MethodCallExpression e)

switch (e.Method.Name)
{
case nameof(Enumerable.Count) when VersionAtLeast(8, 4):
case nameof(Enumerable.Count):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
Expand Down Expand Up @@ -104,19 +107,19 @@ Expression NpgsqlArrayExtensionsHandler([NotNull] MethodCallExpression e)

switch (e.Method.Name)
{
case nameof(NpgsqlArrayExtensions.Contains) when VersionAtLeast(8, 2):
case nameof(NpgsqlArrayExtensions.Contains):
return new CustomBinaryExpression(e.Arguments[1], e.Arguments[2], "@>", typeof(bool));

case nameof(NpgsqlArrayExtensions.ContainedBy) when VersionAtLeast(8, 2):
case nameof(NpgsqlArrayExtensions.ContainedBy):
return new CustomBinaryExpression(e.Arguments[1], e.Arguments[2], "<@", typeof(bool));

case nameof(NpgsqlArrayExtensions.Overlaps) when VersionAtLeast(8, 2):
case nameof(NpgsqlArrayExtensions.Overlaps):
return new CustomBinaryExpression(e.Arguments[1], e.Arguments[2], "&&", typeof(bool));

case nameof(NpgsqlArrayExtensions.ArrayFill) when VersionAtLeast(8, 4):
case nameof(NpgsqlArrayExtensions.ArrayFill):
return new SqlFunctionExpression("array_fill", e.Method.ReturnType, e.Arguments.Skip(1));

case nameof(NpgsqlArrayExtensions.ListFill) when VersionAtLeast(8, 4):
case nameof(NpgsqlArrayExtensions.ListFill):
return new SqlFunctionExpression("array_fill", e.Method.ReturnType, e.Arguments.Skip(1));

case nameof(NpgsqlArrayExtensions.ArrayDimensions):
Expand All @@ -125,29 +128,20 @@ Expression NpgsqlArrayExtensionsHandler([NotNull] MethodCallExpression e)
case nameof(NpgsqlArrayExtensions.ArrayPositions) when VersionAtLeast(9, 5):
return new SqlFunctionExpression("array_positions", e.Method.ReturnType, e.Arguments.Skip(1));

case nameof(NpgsqlArrayExtensions.ArrayRemove) when VersionAtLeast(9, 3):
case nameof(NpgsqlArrayExtensions.ArrayRemove):
return new SqlFunctionExpression("array_remove", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));

case nameof(NpgsqlArrayExtensions.ArrayReplace) when VersionAtLeast(9, 3):
case nameof(NpgsqlArrayExtensions.ArrayReplace):
return new SqlFunctionExpression("array_replace", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.ArrayToString) when VersionAtLeast(9, 1):
return new SqlFunctionExpression("array_to_string", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.ArrayToString):
return new SqlFunctionExpression("array_to_string", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));

case nameof(NpgsqlArrayExtensions.StringToArray) when VersionAtLeast(9, 1):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));
return new SqlFunctionExpression("array_to_string", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.StringToArray):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));

case nameof(NpgsqlArrayExtensions.StringToList) when VersionAtLeast(9, 1):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

case nameof(NpgsqlArrayExtensions.StringToList):
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(2));
return new SqlFunctionExpression("string_to_array", e.Method.ReturnType, e.Arguments.Skip(1).Take(3));

default:
return null;
Expand Down Expand Up @@ -189,7 +183,7 @@ Expression ArrayInstanceHandler([NotNull] MethodCallExpression e)

switch (e.Method.Name)
{
case nameof(Array.GetLength) when VersionAtLeast(8, 4):
case nameof(Array.GetLength):
return Expression.Coalesce(
new SqlFunctionExpression(
"array_length",
Expand Down

0 comments on commit eabbc36

Please sign in to comment.