Skip to content

Commit

Permalink
Implement between statements
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Nov 28, 2016
1 parent 9f86158 commit 26f937b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
36 changes: 36 additions & 0 deletions models/Query/Builder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,48 @@ component displayname="Builder" accessors="true" {
arguments.negate = true;
return whereNull( argumentCollection = arguments );
}

public Builder function orWhereNotNull( column ) {
arguments.combinator = "or";
arguments.negate = true;
return whereNull( argumentCollection = arguments );
}

public Builder function whereBetween( column, start, end, combinator = "and", negate = false ) {
var type = negate ? "notBetween" : "between";

variables.wheres.append( {
type = type,
column = arguments.column,
start = arguments.start,
end = arguments.end,
combinator = arguments.combinator
} );

var startBinding = utils.extractBinding( arguments.start );
arrayAppend( bindings.where, startBinding );
var endBinding = utils.extractBinding( arguments.end );
arrayAppend( bindings.where, endBinding );

return this;
}

public Builder function orWhereBetween( column, start, end, negate = false ) {
arguments.combinator = "or";
return whereBetween( argumentCollection = arguments );
}

public Builder function whereNotBetween( column, start, end, combinator ) {
arguments.negate = true;
return whereBetween( argumentCollection = arguments );
}

public Builder function whereNotBetween( column, start, end, combinator ) {
arguments.combinator = "or";
arguments.negate = true;
return whereBetween( argumentCollection = arguments );
}

public Expression function raw( required string sql ) {
return new quick.models.Query.Expression( sql );
}
Expand Down
8 changes: 8 additions & 0 deletions models/Query/Grammars/Grammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ component displayname="Grammar" accessors="true" {
return "#wrapColumn( where.column )# IS NOT NULL";
}

private string function whereBetween( required struct where, required Builder query ) {
return "#wrapColumn( where.column )# BETWEEN ? AND ?";
}

private string function whereNotBetween( required struct where, required Builder query ) {
return "#wrapColumn( where.column )# NOT BETWEEN ? AND ?";
}

private string function concatenate( required array sql ) {
return arrayToList( arrayFilter( sql, function( item ) {
return item != "";
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/Query/Builder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ component extends="testbox.system.BaseSpec" {
describe( "where between", function() {
it( "can add where between statements", function() {
var builder = getBuilder();
builder.select( "*" ).from( "users" ).whereBetween( "id", [ 1, 2 ] );
builder.select( "*" ).from( "users" ).whereBetween( "id", 1, 2 );
expect( builder.toSql() ).toBe(
"SELECT * FROM ""users"" WHERE ""id"" BETWEEN ? AND ?"
);
Expand All @@ -279,7 +279,7 @@ component extends="testbox.system.BaseSpec" {

it( "can add where not between statements", function() {
var builder = getBuilder();
builder.select( "*" ).from( "users" ).whereNotBetween( "id", [ 1, 2 ] );
builder.select( "*" ).from( "users" ).whereNotBetween( "id", 1, 2 );
expect( builder.toSql() ).toBe(
"SELECT * FROM ""users"" WHERE ""id"" NOT BETWEEN ? AND ?"
);
Expand Down

0 comments on commit 26f937b

Please sign in to comment.