Skip to content

Commit

Permalink
Add ts_rewrite select form (#2844)
Browse files Browse the repository at this point in the history
  • Loading branch information
ProTip authored Aug 18, 2023
1 parent 135eb77 commit 171345e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public static string GetResultHeadline(this NpgsqlTsQuery query, string config,
public static NpgsqlTsQuery Rewrite(this NpgsqlTsQuery query, NpgsqlTsQuery target, NpgsqlTsQuery substitute)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(NpgsqlTsQuery) + "." + nameof(Rewrite)));

/// <summary>
/// For each row of the SQL <paramref name="select" /> result, occurrences of the first column value (the target)
/// are replaced by the second column value (the substitute) within the current <paramref name="query" /> value.
/// The <paramref name="select" /> must yield two columns of tsquery type.
/// http://www.postgresql.org/docs/current/static/textsearch-features.html#TEXTSEARCH-MANIPULATE-TSQUERY
/// </summary>
public static NpgsqlTsQuery Rewrite(this NpgsqlTsQuery query, string select)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(NpgsqlTsQuery) + "." + nameof(Rewrite)));

/// <summary>
/// Returns a tsquery that searches for a match to <paramref name="query1" /> followed by a match
/// to <paramref name="query2" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,16 @@ arguments[1] is SqlConstantExpression constant
argumentsPropagateNullability: TrueArrays[4],
method.ReturnType),

nameof(NpgsqlFullTextSearchLinqExtensions.Rewrite)
nameof(NpgsqlFullTextSearchLinqExtensions.Rewrite) when arguments.Count == 2
=> _sqlExpressionFactory.Function(
"ts_rewrite",
arguments,
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
typeof(NpgsqlTsQuery),
_tsQueryMapping),

nameof(NpgsqlFullTextSearchLinqExtensions.Rewrite) when arguments.Count == 3
=> _sqlExpressionFactory.Function(
"ts_rewrite",
arguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,25 @@ LIMIT 1
""");
}

[Fact]
public void Rewrite_with_select()
{
using var context = CreateContext();
var rewritten = context.Customers
.Select(
c => EF.Functions.ToTsQuery("a & b").Rewrite(
"""SELECT 'a'::tsquery, 'c'::tsquery"""))
.First();

Assert.NotNull(rewritten);
AssertSql(
"""
SELECT ts_rewrite(to_tsquery('a & b'), 'SELECT ''a''::tsquery, ''c''::tsquery')
FROM "Customers" AS c
LIMIT 1
""");
}

[Fact]
public void ToPhrase()
{
Expand Down

0 comments on commit 171345e

Please sign in to comment.