Skip to content

Commit

Permalink
Flesh out MSSQL Query Builder support
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Mar 7, 2018
1 parent 3df7d5e commit 4df2364
Show file tree
Hide file tree
Showing 6 changed files with 636 additions and 59 deletions.
104 changes: 96 additions & 8 deletions models/Grammars/MSSQLGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,56 @@ component extends="qb.models.Grammars.BaseGrammar" {
"groups", "havings", "orders", "offsetValue", "limitValue"
];

/**
* Compiles the columns portion of a sql statement.
*
* @query The Builder instance.
* @columns The selected columns.
*
* @return string
*/
private string function compileColumns(
required QueryBuilder query,
required array columns
) {
if ( ! query.getAggregate().isEmpty() ) {
return "";
}
var select = query.getDistinct() ? "SELECT DISTINCT " : "SELECT ";
if ( ! isNull( query.getLimitValue() ) && isNull( query.getOffsetValue() ) ) {
select &= "TOP (#query.getLimitValue()#) ";
}
return select & columns.map( wrapColumn ).toList( ", " );
}

/**
* Compiles the order by portion of a sql statement.
*
* @query The Builder instance.
* @orders The where clauses.
*
* @return string
*/
private string function compileOrders(
required query,
required array orders
) {
if ( orders.isEmpty() ) {
if ( isNull( query.getOffsetValue() ) ) {
return "";
}
return "ORDER BY 1";
}

var orderBys = orders.map( function( orderBy ) {
return orderBy.direction == "raw" ?
orderBy.column.getSql() :
"#wrapColumn( orderBy.column )# #uCase( orderBy.direction )#";
} );

return "ORDER BY #orderBys.toList( ", " )#";
}

/**
* Compiles the offset portion of a sql statement.
*
Expand All @@ -17,14 +67,10 @@ component extends="qb.models.Grammars.BaseGrammar" {
* @return string
*/
private string function compileOffsetValue( required qb.models.Query.QueryBuilder query, offsetValue ) {
if ( isNull( query.getOffsetValue() ) && isNull( query.getLimitValue() ) ) {
if ( isNull( query.getOffsetValue() ) ) {
return "";
}

if ( isNull( query.getOffsetValue() ) && ! isNull( query.getLimitValue() ) ) {
offsetValue = 0;
}

return "OFFSET #offsetValue# ROWS";
}

Expand All @@ -37,10 +83,52 @@ component extends="qb.models.Grammars.BaseGrammar" {
* @return string
*/
private string function compileLimitValue( required qb.models.Query.QueryBuilder query, limitValue ) {
if ( isNull( arguments.limitValue ) ) {
return "";
if ( ! isNull( arguments.limitValue ) && ! isNull( query.getOffsetValue() ) ) {
return "FETCH NEXT #limitValue# ROWS ONLY";
}
return "FETCH NEXT #limitValue# ROWS ONLY";
return "";
}

/**
* Parses and wraps a value from the Builder for use in a sql statement.
*
* @table The value to parse and wrap.
*
* @return string
*/
public string function wrapValue( required any value ) {
if ( value == "*" ) {
return value;
}
return "[#value#]";
}

/**
* Compile a Builder's query into an update string.
*
* @query The Builder instance.
* @columns The array of columns into which to insert.
*
* @return string
*/
public string function compileUpdate(
required query,
required array columns
) {
var updateList = columns.map( function( column ) {
return "#wrapColumn( column )# = ?";
} ).toList( ", " );

return arrayToList( arrayFilter( [
"UPDATE",
isNull( query.getLimitValue() ) ? "" : "TOP (#query.getLimitValue()#)",
wrapTable( query.getFrom() ),
"SET",
updateList,
compileWheres( query, query.getWheres() )
], function( str ) {
return str != "";
} ), " " );
}

}
6 changes: 6 additions & 0 deletions tests/resources/AbstractQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ component extends="testbox.system.BaseSpec" {
builder.from( "users" ).offset( 3 );
}, offset() );
} );

it( "can offset with an order by", function() {
testCase( function( builder ) {
builder.from( "users" ).orderBy( "id" ).offset( 3 );
}, offsetWithOrderBy() );
} );
} );

describe( "forPage", function() {
Expand Down
51 changes: 0 additions & 51 deletions tests/specs/Query/Builder+MSSQLGrammarSpec.cfc

This file was deleted.

Loading

0 comments on commit 4df2364

Please sign in to comment.