-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor query SQL generation for standardization
* GenerateTop() implementation moved to SqlServer * GenerateLimitOffset() generates ISO SQL:2008 by default, overridden by SqlServer (and Sqlite)
- Loading branch information
Showing
7 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/EFCore.SqlServer/Query/Pipeline/SqlServerQuerySqlGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline; | ||
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Pipeline | ||
{ | ||
public class SqlServerQuerySqlGenerator : QuerySqlGenerator | ||
{ | ||
public SqlServerQuerySqlGenerator( | ||
IRelationalCommandBuilderFactory relationalCommandBuilderFactory, | ||
ISqlGenerationHelper sqlGenerationHelper) | ||
: base(relationalCommandBuilderFactory, sqlGenerationHelper) | ||
{ | ||
} | ||
|
||
protected override void GenerateTop(SelectExpression selectExpression) | ||
{ | ||
if (selectExpression.Limit != null | ||
&& selectExpression.Offset == null) | ||
{ | ||
Sql.Append("TOP("); | ||
|
||
Visit(selectExpression.Limit); | ||
|
||
Sql.Append(") "); | ||
} | ||
} | ||
|
||
protected override void GenerateLimitOffset(SelectExpression selectExpression) | ||
{ | ||
// Note: For Limit without Offset, SqlServer generates TOP() | ||
if (selectExpression.Offset != null) | ||
{ | ||
Sql.AppendLine() | ||
.Append("OFFSET "); | ||
|
||
Visit(selectExpression.Offset); | ||
|
||
Sql.Append(" ROWS"); | ||
|
||
if (selectExpression.Limit != null) | ||
{ | ||
Sql.Append(" FETCH NEXT "); | ||
|
||
Visit(selectExpression.Limit); | ||
|
||
Sql.Append(" ROWS ONLY"); | ||
} | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/EFCore.SqlServer/Query/Pipeline/SqlServerQuerySqlGeneratorFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Pipeline | ||
{ | ||
public class SqlServerQuerySqlGeneratorFactory : QuerySqlGeneratorFactory | ||
{ | ||
private readonly IRelationalCommandBuilderFactory _commandBuilderFactory; | ||
private readonly ISqlGenerationHelper _sqlGenerationHelper; | ||
|
||
public SqlServerQuerySqlGeneratorFactory( | ||
IRelationalCommandBuilderFactory commandBuilderFactory, | ||
ISqlGenerationHelper sqlGenerationHelper) | ||
: base(commandBuilderFactory, sqlGenerationHelper) | ||
{ | ||
_commandBuilderFactory = commandBuilderFactory; | ||
_sqlGenerationHelper = sqlGenerationHelper; | ||
} | ||
|
||
public override QuerySqlGenerator Create() | ||
=> new SqlServerQuerySqlGenerator(_commandBuilderFactory, _sqlGenerationHelper); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters