Skip to content

Commit

Permalink
Fixes a bug in NpgsqlQuerySqlGenerator with PgFunctionExpression
Browse files Browse the repository at this point in the history
- Bug:
  - Input:  PgFunction("abbrev", typeof(string), new [] { inetExpression }) // x.Inet
  - Before: abbrev("x"."Inet",)
  - After:  abbrev("x"."Inet")

- Also includes general cleanup to match project style, remove unused using-statements, etc.
  • Loading branch information
austindrenski authored and roji committed May 25, 2018
1 parent e7ebb3f commit c1abc10
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions src/EFCore.PG/Query/Sql/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region License

// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
Expand All @@ -19,6 +20,7 @@
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

#endregion

using System;
Expand All @@ -27,10 +29,8 @@
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query.Expressions;
using Microsoft.EntityFrameworkCore.Query.Expressions.Internal;
using Microsoft.EntityFrameworkCore.Query.Sql;
using Microsoft.EntityFrameworkCore.Storage;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Utilities;
using Remotion.Linq.Clauses;
Expand Down Expand Up @@ -70,11 +70,11 @@ protected override void GenerateLimitOffset(SelectExpression selectExpression)

if (selectExpression.Offset != null)
{
if (selectExpression.Limit == null) {
if (selectExpression.Limit == null)
Sql.AppendLine();
} else {
else
Sql.Append(' ');
}

Sql.Append("OFFSET ");
Visit(selectExpression.Offset);
}
Expand Down Expand Up @@ -124,6 +124,7 @@ protected override Expression VisitBinary(BinaryExpression expression)
Sql.Append(")");
return exp;
}

break;
}

Expand Down Expand Up @@ -208,22 +209,17 @@ public Expression VisitRegexMatch([NotNull] RegexMatchExpression regexMatchExpre
}

Sql.Append("('(?");
if (options.HasFlag(RegexOptions.IgnoreCase)) {
if (options.HasFlag(RegexOptions.IgnoreCase))
Sql.Append('i');
}

if (options.HasFlag(RegexOptions.Multiline)) {
if (options.HasFlag(RegexOptions.Multiline))
Sql.Append('n');
}
else if (!options.HasFlag(RegexOptions.Singleline)) {
else if (!options.HasFlag(RegexOptions.Singleline))
// In .NET's default mode, . doesn't match newlines but PostgreSQL it does.
Sql.Append('p');
}

if (options.HasFlag(RegexOptions.IgnorePatternWhitespace))
{
Sql.Append('x');
}

Sql.Append(")' || ");
Visit(regexMatchExpression.Pattern);
Expand Down Expand Up @@ -278,10 +274,9 @@ public Expression VisitExplicitStoreTypeCast([NotNull] ExplicitStoreTypeCastExpr

Visit(castExpression.Operand);

Sql
.Append(" AS ")
.Append(castExpression.StoreType)
.Append(")");
Sql.Append(" AS ")
.Append(castExpression.StoreType)
.Append(")");

//_typeMapping = parentTypeMapping;

Expand All @@ -296,28 +291,31 @@ protected override string GenerateOperator(Expression expression)
if (expression.Type == typeof(string))
return " || ";
goto default;

case ExpressionType.And:
if (expression.Type == typeof(bool))
return " AND ";
goto default;

case ExpressionType.Or:
if (expression.Type == typeof(bool))
return " OR ";
goto default;

default:
return base.GenerateOperator(expression);
}
}

protected override void GenerateOrdering([NotNull] Ordering ordering)
protected override void GenerateOrdering(Ordering ordering)
{
base.GenerateOrdering(ordering);

if (_reverseNullOrderingEnabled)
{
Sql.Append(ordering.OrderingDirection == OrderingDirection.Asc
? " NULLS FIRST" : " NULLS LAST");
}
Sql.Append(
ordering.OrderingDirection == OrderingDirection.Asc
? " NULLS FIRST"
: " NULLS LAST");
}

public virtual Expression VisitCustomBinary(CustomBinaryExpression expression)
Expand Down Expand Up @@ -367,38 +365,36 @@ public virtual Expression VisitPgFunction(PgFunctionExpression e)
}
else if (!string.IsNullOrWhiteSpace(e.Schema))
{
Sql
.Append(SqlGenerator.DelimitIdentifier(e.Schema))
.Append(".");
Sql.Append(SqlGenerator.DelimitIdentifier(e.Schema))
.Append(".");

wroteSchema = true;
}

Sql
.Append(
wroteSchema
? SqlGenerator.DelimitIdentifier(e.FunctionName)
: e.FunctionName);
Sql.Append(
wroteSchema
? SqlGenerator.DelimitIdentifier(e.FunctionName)
: e.FunctionName);

Sql.Append("(");

//_typeMapping = null;

GenerateList(e.PositionalArguments);

if (e.PositionalArguments.Count > 0)
Sql.Append(", ");
bool hasArguments = e.PositionalArguments.Count > 0 && e.NamedArguments.Count > 0;

var i = 0;
foreach (var kv in e.NamedArguments)
{
Sql
.Append(kv.Key)
.Append(" => ");
Visit(kv.Value);

if (i++ < e.NamedArguments.Count - 1)
if (hasArguments)
Sql.Append(", ");
else
hasArguments = true;

Sql.Append(kv.Key)
.Append(" => ");

Visit(kv.Value);
}

Sql.Append(")");
Expand Down

0 comments on commit c1abc10

Please sign in to comment.