Skip to content

Commit

Permalink
Add indenting for multiple line raw SQL strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mikary committed Mar 10, 2015
1 parent c86f22f commit 3efb451
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
Expand Down Expand Up @@ -186,11 +187,17 @@ public virtual Expression VisitRawSqlDerivedTableExpression([NotNull] RawSqlDeri

using (_sql.Indent())
{
_sql.Append(rawSqlDerivedTableExpression.Sql);
var reader = new StringReader(rawSqlDerivedTableExpression.Sql);

var line = reader.ReadLine();
while (line != null)
{
_sql.AppendLine(line);
line = reader.ReadLine();
}
}

_sql.AppendLine()
.Append(") AS ")
_sql.Append(") AS ")
.Append(DelimitIdentifier(rawSqlDerivedTableExpression.Alias));

return rawSqlDerivedTableExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public virtual void From_sql_queryable_where_simple_closure_via_query_cache()
entryCount: 7);
}

[Fact]
public virtual void From_sql_queryable_with_multiple_line_query()
{
AssertQuery<Customer>(
cs => cs.FromSql(@"SELECT *
FROM Customers
WHERE Customers.City = 'London'"),
cs => cs.Where(c => c.City == "London"),
entryCount: 6);
}

protected NorthwindContext CreateContext()
{
return Fixture.CreateContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public override void From_sql_queryable_filter()
SELECT * FROM Customers WHERE Customers.ContactName LIKE '%z%'
) AS [c]",
Sql);

}
public override void From_sql_queryable_cached_by_query()
{
Expand Down Expand Up @@ -68,6 +67,20 @@ public override void From_sql_queryable_where_simple_closure_via_query_cache()
Sql);
}

public override void From_sql_queryable_with_multiple_line_query()
{
base.From_sql_queryable_with_multiple_line_query();

Assert.Equal(
@"SELECT [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[CustomerID], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM (
SELECT *
FROM Customers
WHERE Customers.City = 'London'
) AS [c]",
Sql);
}

public FromSqlQuerySqlServerTest(NorthwindQuerySqlServerFixture fixture)
: base(fixture)
{
Expand Down

0 comments on commit 3efb451

Please sign in to comment.