Skip to content

Translate 'case when X is null...' to 'coalesce(X,...)' #93

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

Closed
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
17 changes: 17 additions & 0 deletions src/EntityFramework6.Npgsql/SqlGenerators/SqlBaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,23 @@ protected string GetDbType(EdmType edmType)

public override VisitedExpression Visit([NotNull] DbCaseExpression expression)
{
// Check for COALESCE like CASE
if (expression.When.Count == 1 &&
expression.When[0].ExpressionKind == DbExpressionKind.IsNull)
{
var isNullExpression = expression.When[0] as DbIsNullExpression;
if (isNullExpression.Argument.Equals(expression.Else))
{
LiteralExpression coalesceExpression = new LiteralExpression(" COALESCE( ");
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for extra space before and after the coalesce statement.

coalesceExpression.Append(expression.Else.Accept(this));
coalesceExpression.Append(",");
coalesceExpression.Append(expression.Then[0].Accept(this));
coalesceExpression.Append(") ");
return coalesceExpression;
}
}

// General CASE
var caseExpression = new LiteralExpression(" CASE ");
for (var i = 0; i < expression.When.Count && i < expression.Then.Count; ++i)
{
Expand Down
62 changes: 62 additions & 0 deletions test/EntityFramework6.Npgsql.Tests/EntityFrameworkBasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -772,5 +772,67 @@ public void TestTableValuedStoredFunctions()
Assert.AreEqual(1, list2[0].Something);
}
}

[Test]
public void Test_issue_60_and_62()
Copy link
Contributor

Choose a reason for hiding this comment

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

This test needs a better name, maybe: Test_string_type_inference_in_coalesce_statements

{
using (var context = new BloggingContext(ConnectionString))
{
context.Database.Log = Console.Out.WriteLine;

context.Blogs.Add( new Blog { Name = "Hello" });
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Extra space before new keyword.

context.SaveChanges();

string stringValue = "string_value";
var query = context.Blogs.Select(b => stringValue + "_postfix");
var blogTitle = query.First();
Assert.That(blogTitle, Is.EqualTo("string_value_postfix"));
Console.WriteLine(query.ToString());
StringAssert.AreEqualIgnoringCase("SELECT COALESCE( @p__linq__0,E'') || E'_postfix' AS \"C1\" FROM \"dbo\".\"Blogs\" AS \"Extent1\"", query.ToString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Readability: This line is too long and is hard to read for other developers, break it at sensible points into 2 or more strings concatenated with "+" so that the length of each line is as near in length to surrounding code. Also there is extra spaces around COALESCE statement. You don't have to add these spaces in the expression. The parsing system automatically handles that.

}
}

[Test]
public void TestNullPropagation_1()
Copy link
Contributor

Choose a reason for hiding this comment

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

Test needs a better name, maybe: Test_string_null_propagation

{
using (var context = new BloggingContext(ConnectionString))
{
context.Database.Log = Console.Out.WriteLine;

context.Blogs.Add( new Blog { Name = "Hello" });
context.SaveChanges();

string stringValue = "string_value";
var query = context.Blogs.Select(b => (stringValue ?? "default_value") + "_postfix");
var blog_title = query.First();
Assert.That(blog_title, Is.EqualTo("string_value_postfix"));

Console.WriteLine(query.ToString());
StringAssert.AreEqualIgnoringCase("SELECT CASE WHEN ( COALESCE( @p__linq__0,E'default_value') IS NULL) THEN (E'') WHEN (@p__linq__0 IS NULL) THEN (E'default_value') ELSE (@p__linq__0) END || E'_postfix' AS \"C1\" FROM \"dbo\".\"Blogs\" AS \"Extent1\"", query.ToString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Readability: This line is too long and is hard to read for other developers, break it at sensible points into 2 or more strings concatenated with "+" so that the length of each line is as near in length to surrounding code.

}
}

[Test]
public void TestNullPropagation_2()
Copy link
Contributor

Choose a reason for hiding this comment

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

Test needs a better name, maybe: Test_string_multiple_null_propagation

{
using (var context = new BloggingContext(ConnectionString))
{
context.Database.Log = Console.Out.WriteLine;

context.Blogs.Add( new Blog { Name = "Hello" });
context.SaveChanges();

string stringValue1 = "string_value1";
string stringValue2 = "string_value2";
string stringValue3 = "string_value3";

var query = context.Blogs.Select(b => (stringValue1 ?? stringValue2 ?? stringValue3) + "_postfix");
var blog_title = query.First();
Assert.That(blog_title, Is.EqualTo("string_value1_postfix"));

Console.WriteLine(query.ToString());
StringAssert.AreEqualIgnoringCase("SELECT CASE WHEN ( COALESCE( @p__linq__0, COALESCE( @p__linq__1,@p__linq__2) ) IS NULL) THEN (E'') WHEN (@p__linq__0 IS NULL) THEN ( COALESCE( @p__linq__1,@p__linq__2) ) ELSE (@p__linq__0) END || E'_postfix' AS \"C1\" FROM \"dbo\".\"Blogs\" AS \"Extent1\"", query.ToString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Readability: This line is too long and is hard to read for other developers, break it at sensible points into 2 or more strings concatenated with "+" so that the length of each line is as near in length to surrounding code.

}
}
}
}