Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from position to strpos for text comparisons #155

Closed
wants to merge 1 commit into from
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
40 changes: 40 additions & 0 deletions EF6.PG.Tests/EntityFrameworkBasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,46 @@ where p.Rating < 3
}
}

[Test]
public void Contains()
{
using (var context = new BloggingContext(ConnectionString))
{
context.Blogs.Add(new Blog
{
Name = "foo blog"
});
context.SaveChanges();
}

using (var context = new BloggingContext(ConnectionString))
{
var searchTerms = new[] { "foo", "bar" };
var matches = context.Blogs.Count(b => searchTerms.Any(t => b.Name.Contains(t)));
Assert.AreEqual(1, matches);
}
}

[Test]
public void StartsWith()
{
using (var context = new BloggingContext(ConnectionString))
{
context.Blogs.Add(new Blog
{
Name = "foo blog"
});
context.SaveChanges();
}

using (var context = new BloggingContext(ConnectionString))
{
var searchTerms = new[] { "foo", "bar" };
var matches = context.Blogs.Count(b => searchTerms.Any(t => b.Name.StartsWith(t)));
Assert.AreEqual(1, matches);
}
}

[Test]
public void SelectWithWhere_Ef_TruncateTime()
{
Expand Down
25 changes: 9 additions & 16 deletions EF6.PG/SqlGenerators/SqlBaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,6 @@ VisitedExpression VisitFunction(EdmFunction function, IList<DbExpression> args,
{
if (function.NamespaceName == "Edm")
{
VisitedExpression arg;
switch (function.Name)
{
// string functions
Expand All @@ -944,21 +943,17 @@ VisitedExpression VisitFunction(EdmFunction function, IList<DbExpression> args,
return OperatorExpression.Build(Operator.Concat, _useNewPrecedences, args[0].Accept(this), args[1].Accept(this));
case "Contains":
Debug.Assert(args.Count == 2);
var contains = new FunctionExpression("position");
arg = args[1].Accept(this);
arg.Append(" in ");
arg.Append(args[0].Accept(this));
contains.AddArgument(arg);
var contains = new FunctionExpression("strpos");
contains.AddArgument(args[0].Accept(this));
contains.AddArgument(args[1].Accept(this));
// if position returns zero, then contains is false
return OperatorExpression.Build(Operator.GreaterThan, _useNewPrecedences, contains, new LiteralExpression("0"));
// case "EndsWith": - depends on a reverse function to be able to implement with parameterized queries
case "IndexOf":
Debug.Assert(args.Count == 2);
var indexOf = new FunctionExpression("position");
arg = args[0].Accept(this);
arg.Append(" in ");
arg.Append(args[1].Accept(this));
indexOf.AddArgument(arg);
var indexOf = new FunctionExpression("strpos");
indexOf.AddArgument(args[1].Accept(this));
indexOf.AddArgument(args[0].Accept(this));
return indexOf;
case "Left":
Debug.Assert(args.Count == 2);
Expand Down Expand Up @@ -995,11 +990,9 @@ VisitedExpression VisitFunction(EdmFunction function, IList<DbExpression> args,
return Substring(args[0].Accept(this), args[1].Accept(this), args[2].Accept(this));
case "StartsWith":
Debug.Assert(args.Count == 2);
var startsWith = new FunctionExpression("position");
arg = args[1].Accept(this);
arg.Append(" in ");
arg.Append(args[0].Accept(this));
startsWith.AddArgument(arg);
var startsWith = new FunctionExpression("strpos");
startsWith.AddArgument(args[0].Accept(this));
startsWith.AddArgument(args[1].Accept(this));
return OperatorExpression.Build(Operator.Equals, _useNewPrecedences, startsWith, new LiteralExpression("1"));
case "ToLower":
return StringModifier("lower", args);
Expand Down