Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric Peterson
authored and
Eric Peterson
committed
Jan 18, 2017
1 parent
b11ea7f
commit 89b9c84
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
component extends="qb.models.Query.Grammars.Grammar" { | ||
|
||
variables.selectComponents = [ | ||
"aggregate", "columns", "from", "joins", "wheres", | ||
"groups", "havings", "orders", "offsetValue", "limitValue" | ||
]; | ||
|
||
private string function compileOffsetValue( required Builder query, offsetValue ) { | ||
if ( isNull( query.getOffsetValue() ) && isNull( query.getLimitValue() ) ) { | ||
return ""; | ||
} | ||
|
||
if ( isNull( query.getOffsetValue() ) && ! isNull( query.getLimitValue() ) ) { | ||
offsetValue = 0; | ||
} | ||
|
||
return "OFFSET #offsetValue# ROWS"; | ||
} | ||
|
||
private string function compileLimitValue( required Builder query, limitValue ) { | ||
if ( isNull( arguments.limitValue ) ) { | ||
return ""; | ||
} | ||
return "FETCH NEXT #limitValue# ROWS ONLY"; | ||
} | ||
|
||
} |
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,53 @@ | ||
import qb.models.Query.Builder; | ||
|
||
component extends="testbox.system.BaseSpec" { | ||
|
||
function run() { | ||
describe( "MSSQL Grammar", function() { | ||
describe( "limits and offsets", function() { | ||
it( "correctly compiles limits", function() { | ||
var builder = getBuilder(); | ||
builder.from( "users" ).orderBy( "id" ).limit( 1 ); | ||
expect( builder.toSql() ).toBe( | ||
"SELECT * FROM ""users"" ORDER BY ""id"" ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY" | ||
); | ||
} ); | ||
|
||
it( "correctly compiles offsets", function() { | ||
var builder = getBuilder(); | ||
builder.from( "users" ).orderBy( "id" ).offset( 1 ); | ||
expect( builder.toSql() ).toBe( | ||
"SELECT * FROM ""users"" ORDER BY ""id"" ASC OFFSET 1 ROWS" | ||
); | ||
} ); | ||
|
||
it( "correctly compiles both limits and offsets", function() { | ||
var builder = getBuilder(); | ||
builder.from( "users" ).orderBy( "id" ).limit( 1 ).offset( 1 ); | ||
expect( builder.toSql() ).toBe( | ||
"SELECT * FROM ""users"" ORDER BY ""id"" ASC OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY" | ||
); | ||
} ); | ||
} ); | ||
} ); | ||
} | ||
|
||
private Builder function getBuilder( returningArrays = false ) { | ||
var grammar = getMockBox() | ||
.createMock( "qb.models.Query.Grammars.MSSQLGrammar" ); | ||
var queryUtils = getMockBox() | ||
.createMock( "qb.models.Query.QueryUtils" ); | ||
var builder = getMockBox().createMock( "qb.models.Query.Builder" ) | ||
.init( grammar, queryUtils ); | ||
builder.setReturningArrays( returningArrays ); | ||
builder.setReturnFormat( "" ); | ||
return builder; | ||
} | ||
|
||
private array function getTestBindings( required Builder builder ) { | ||
return builder.getBindings().map( function( binding ) { | ||
return binding.value; | ||
} ); | ||
} | ||
|
||
} |