Skip to content

Commit

Permalink
Add first MSSQL-specific grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Peterson authored and Eric Peterson committed Jan 18, 2017
1 parent b11ea7f commit 89b9c84
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ModuleConfig.cfc
Expand Up @@ -27,6 +27,10 @@ component {
.to( "qb.models.Query.Grammars.OracleGrammar" )
.asSingleton();

binder.map( "MSSQLGrammar@qb" )
.to( "qb.models.Query.Grammars.MSSQLGrammar" )
.asSingleton();

binder.map( "QueryUtils@qb" )
.to( "qb.models.Query.QueryUtils" )
.asSingleton();
Expand Down
27 changes: 27 additions & 0 deletions models/Query/Grammars/MSSQLGrammar.cfc
@@ -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";
}

}
53 changes: 53 additions & 0 deletions tests/specs/Query/Builder+MSSQLGrammarSpec.cfc
@@ -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;
} );
}

}

0 comments on commit 89b9c84

Please sign in to comment.