Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/mapping-and-translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ Below are some Npgsql-specific translations, many additional standard ones are s
| .Where(c => c.SomeArray.Length == 3) | [WHERE array_length("c"."SomeArray, 1) == 3](https://www.postgresql.org/docs/current/static/functions-array.html#ARRAY-FUNCTIONS-TABLE)
| .Where(c => EF.Functions.Like(c.Name, "foo%") | [WHERE "c"."Name" LIKE 'foo%'](https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-LIKE)
| .Where(c => EF.Functions.ILike(c.Name, "foo%") | [WHERE "c"."Name" ILIKE 'foo%'](https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-LIKE) (case-insensitive LIKE)
| .Select(c => EF.Functions.ToTsVector("english", c.Name)) | [SELECT to_tsvector('english'::regconfig, "c"."Name")](https://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-DOCUMENTS)
| .Select(c => EF.Functions.ToTsQuery("english", "pgsql")) | [SELECT to_tsquery('english'::regconfig, 'pgsql')](https://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES)
| .Where(c => c.SearchVector.Matches("Npgsql")) | [WHERE "c"."SearchVector" @@ 'Npgsql'](https://www.postgresql.org/docs/current/static/textsearch-intro.html#TEXTSEARCH-MATCHING)
| .Select(c => EF.Functions.ToTsQuery(c.SearchQuery).ToNegative()) | [SELECT (!! to_tsquery("c"."SearchQuery"))](https://www.postgresql.org/docs/current/static/textsearch-features.html#TEXTSEARCH-MANIPULATE-TSQUERY)
| .Select(c => EF.Functions.ToTsVector(c.Name).SetWeight(NpgsqlTsVector.Lexeme.Weight.A)) | [SELECT setweight(to_tsvector("c"."Name"), 'A')](https://www.postgresql.org/docs/current/static/textsearch-features.html#TEXTSEARCH-MANIPULATE-TSVECTOR)
1 change: 1 addition & 0 deletions doc/migration/2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Aside from general EF Core features new in 2.1.0, the Npgsql provider contains t
* Support PostgreSQL 10 sequences with type `int` and `smallint` ([#301](https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/301)).
* Identifiers will only be quoted if needed ([#327](https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/327)), this should make the generated SQL much easier to read.
* You can now specify the [tablespace](https://www.postgresql.org/docs/10/static/manage-ag-tablespaces.html) when creating your databases ([#332](https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/332)).
* You can now use the PostgreSQL Full Text Search functions and operators from LINQ queries (except for ```array_to_tsquery```, which is not possible to implement currently due to Entity Framework Core limitations). ```NpgsqlTsQuery``` and ```NpgsqlTsVector``` are now fully supported property types that will create ```tsquery``` and ```tsvector``` columns. Full text search functions are implemented as extensions on ```DbFunctions``` and both full text types. Raw SQL migrations are still needed to create and drop update triggers however.

Here's the [full list of issues](https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/milestone/8?closed=1). Please report any problems to https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL.

Expand Down
3 changes: 3 additions & 0 deletions src/EFCore.PG/Extensions/NpgsqlServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Query.Sql;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update;
Expand All @@ -45,6 +46,7 @@
using Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Utilities;
using Npgsql.EntityFrameworkCore.PostgreSQL.ValueGeneration.Internal;
using Remotion.Linq.Parsing.ExpressionVisitors.TreeEvaluation;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
Expand Down Expand Up @@ -107,6 +109,7 @@ public static IServiceCollection AddEntityFrameworkNpgsql([NotNull] this IServic
.TryAdd<IQuerySqlGeneratorFactory, NpgsqlQuerySqlGeneratorFactory>()
.TryAdd<ISqlTranslatingExpressionVisitorFactory, NpgsqlSqlTranslatingExpressionVisitorFactory>()
.TryAdd<ISingletonOptions, INpgsqlOptions>(p => p.GetService<INpgsqlOptions>())
.TryAdd<IEvaluatableExpressionFilter, NpgsqlEvaluatableExpressionFilter>()
.TryAddProviderSpecificServices(b => b
.TryAddSingleton<INpgsqlValueGeneratorCache, NpgsqlValueGeneratorCache>()
.TryAddSingleton<INpgsqlOptions, NpgsqlOptions>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#region License
// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// 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;
using System.Diagnostics.CodeAnalysis;
using NpgsqlTypes;

namespace Microsoft.EntityFrameworkCore
{
[SuppressMessage("ReSharper", "UnusedParameter.Global")]
public static class NpgsqlFullTextSearchDbFunctionsExtensions
{
/// <summary>
/// Reduce <paramref name="document" /> to tsvector.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-DOCUMENTS
/// </summary>
public static NpgsqlTsVector ToTsVector(this DbFunctions _, string document) =>
throw new NotSupportedException();

/// <summary>
/// Reduce <paramref name="document" /> to tsvector using the text search configuration specified
/// by <paramref name="config" />.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-DOCUMENTS
/// </summary>
public static NpgsqlTsVector ToTsVector(this DbFunctions _, string config, string document) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be a bit more idiomatic for document to be the first parameter here? I'm aware that this is the order in the PostgreSQL function, but we shouldn't be afraid to change that.

Copy link
Contributor Author

@rwasef1830 rwasef1830 Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this, but since both parameter types are strings, I thought it will trip up people who know the full text functions and cause confusion and hate :-D, so I preferred sticking to the postgresql ordering as much as possible.

It also makes the method translation slightly simpler, but that's not an excuse I know :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense.

throw new NotSupportedException();

/// <summary>
/// Produce tsquery from <paramref name="query" /> ignoring punctuation.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
/// </summary>
public static NpgsqlTsQuery PlainToTsQuery(this DbFunctions _, string query) =>
throw new NotSupportedException();

/// <summary>
/// Produce tsquery from <paramref name="query" /> ignoring punctuation and using the text search
/// configuration specified by <paramref name="config" />.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
/// </summary>
public static NpgsqlTsQuery PlainToTsQuery(this DbFunctions _, string config, string query) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above (consider switching parameter order)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my reply above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

throw new NotSupportedException();

/// <summary>
/// Produce tsquery that searches for a phrase from <paramref name="query" /> ignoring punctuation.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
/// </summary>
public static NpgsqlTsQuery PhraseToTsQuery(this DbFunctions _, string query) =>
throw new NotSupportedException();

/// <summary>
/// Produce tsquery that searches for a phrase from <paramref name="query" /> ignoring punctuation
/// and using the text search configuration specified by <paramref name="config" />.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
/// </summary>
public static NpgsqlTsQuery PhraseToTsQuery(this DbFunctions _, string config, string query) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my reply above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

throw new NotSupportedException();

/// <summary>
/// Normalize words in <paramref name="query" /> and convert to tsquery. If your input
/// contains punctuation that should not be treated as text search operators, use
/// <see cref="PlainToTsQuery(DbFunctions, string)" /> instead.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
/// </summary>
public static NpgsqlTsQuery ToTsQuery(this DbFunctions _, string query) => throw new NotSupportedException();

/// <summary>
/// Normalize words in <paramref name="query" /> and convert to tsquery using the text search
/// configuration specified by <paramref name="config" />. If your input contains punctuation
/// that should not be treated as text search operators, use
/// <see cref="PlainToTsQuery(DbFunctions, string, string)" /> instead.
/// http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
/// </summary>
public static NpgsqlTsQuery ToTsQuery(this DbFunctions _, string config, string query) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

throw new NotSupportedException();
}
}
Loading