Skip to content

Commit

Permalink
Fixes for Adobe engines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Peterson authored and Eric Peterson committed Dec 2, 2016
1 parent 3249f98 commit 64c19c7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
32 changes: 18 additions & 14 deletions models/Query/Builder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ component displayname="Builder" accessors="true" {
else {
var method = where ? "where" : "on";
arguments.column = arguments.first;
arguments.value = arguments.second;
arguments.value = isNull( arguments.second ) ? javacast( "null", "" ) : arguments.second;
variables.joins.append(
invoke( join, method, arguments )
);
Expand Down Expand Up @@ -189,6 +189,10 @@ component displayname="Builder" accessors="true" {
// where methods

public Builder function where( column, operator, value, string combinator = "and" ) {
if ( isClosure( column ) ) {
return whereNested( column, combinator );
}

var argCount = argumentCount( arguments );

if ( isInvalidCombinator( arguments.combinator ) ) {
Expand All @@ -209,10 +213,6 @@ component displayname="Builder" accessors="true" {
);
}

if ( isClosure( column ) ) {
return whereNested( column, combinator );
}

if ( isClosure( value ) ) {
return whereSub( column, operator, value, combinator );
}
Expand Down Expand Up @@ -290,13 +290,13 @@ component displayname="Builder" accessors="true" {
}

public Builder function whereNotIn( column, values, combinator = "and" ) {
arguments.negate = true
arguments.negate = true;
return whereIn( argumentCollection = arguments );
}

public Builder function orWhereNotIn( column, values ) {
arguments.combinator = "or";
arguments.negate = true
arguments.negate = true;
return whereIn( argumentCollection = arguments );
}

Expand All @@ -307,7 +307,7 @@ component displayname="Builder" accessors="true" {
variables.wheres.append( {
type = "raw",
sql = sql,
combinator = arguments.combinator,
combinator = arguments.combinator
} );
return this;
}
Expand Down Expand Up @@ -468,7 +468,7 @@ component displayname="Builder" accessors="true" {
return whereBetween( argumentCollection = arguments );
}

public Builder function whereNotBetween( column, start, end, combinator ) {
public Builder function orWhereNotBetween( column, start, end, combinator ) {
arguments.combinator = "or";
arguments.negate = true;
return whereBetween( argumentCollection = arguments );
Expand All @@ -479,8 +479,12 @@ component displayname="Builder" accessors="true" {
public Builder function when(
required boolean condition,
onTrue,
onFalse = function( q ) { return q; }
onFalse
) {
var defaultCallback = function( q ) {
return q;
};
onFalse = isNull( onFalse ) ? defaultCallback : onFalse;
if ( condition ) {
onTrue( this );
} else {
Expand All @@ -496,10 +500,10 @@ component displayname="Builder" accessors="true" {
// It can't be extracted to a function because
// the arguments struct doesn't get passed correctly.
var args = {};
var count = structCount( arguments );
var count = 1;
for ( var arg in arguments ) {
args[ count ] = arguments[ arg ];
count--;
count++;
}

var groupBys = normalizeToArray( argumentCollection = args );
Expand Down Expand Up @@ -561,8 +565,8 @@ component displayname="Builder" accessors="true" {
var bindings = values.map( function( valueArray ) {
return columns.map( function( column ) {
return getUtils().extractBinding( valueArray[ column ] );
} )
} )
} );
} );
addBindings( bindings.reduce( function( allBindings, bindingsArray ) {
allBindings.append( bindingsArray, true /* merge */ );
return allBindings;
Expand Down
17 changes: 8 additions & 9 deletions models/Query/Grammars/Grammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ component displayname="Grammar" accessors="true" {
}

for ( var where in arguments.wheres ) {
var sql = uCase( where.combinator ) & " " &
invoke( this, "where#where.type#", {
where = where, query = query
} );
var whereFunc = variables[ "where#where.type#" ];
var sql = uCase( where.combinator ) & " " & whereFunc( where = where, query = query );
wheresArray.append( sql );
}

Expand All @@ -70,7 +68,7 @@ component displayname="Grammar" accessors="true" {
var conjunction = isInstanceOf( query, "qb.models.Query.JoinClause" ) ?
"ON" : "WHERE";

return "#conjunction# #removeLeadingCombinator( whereList )#";
return trim( "#conjunction# #removeLeadingCombinator( whereList )#" );
}

private string function whereBasic( requried struct where, required Builder query ) {
Expand All @@ -86,21 +84,21 @@ component displayname="Grammar" accessors="true" {
placeholder = where.value.getSql();
}

return "#where.column# #uCase( where.operator )# #placeholder#";
return trim( "#where.column# #uCase( where.operator )# #placeholder#" );
}

private string function whereRaw( required struct where, required Builder query ) {
return where.sql;
}

private string function whereColumn( required struct where, required Builder query ) {
return "#wrapColumn( where.first )# #where.operator# #wrapColumn( where.second )#";
return trim( "#wrapColumn( where.first )# #where.operator# #wrapColumn( where.second )#" );
}

private string function whereNested( required struct where, required Builder query ) {
var sql = compileWheres( arguments.where.query, arguments.where.query.getWheres() );
// cut off the first 7 characters to account for the extra "WHERE"
return "(#mid( sql, 7 )#)";
return trim( "(#mid( sql, 7, len( sql ) - 6 )#)" );
}

private string function whereSub( required struct where, required Builder query ) {
Expand Down Expand Up @@ -200,7 +198,7 @@ component displayname="Grammar" accessors="true" {

var placeholderString = values.map( function( valueArray ) {
return "(" & valueArray.map( function() {
return "?"
return "?";
} ).toList( ", " ) & ")";
} ).toList( ", ");
return trim( "INSERT INTO #wrapTable( query.getFrom() )# (#columnsString#) VALUES #placeholderString#" );
Expand Down Expand Up @@ -263,4 +261,5 @@ component displayname="Grammar" accessors="true" {
}
return """#value#""";
}

}
33 changes: 20 additions & 13 deletions tests/specs/Query/Builder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ component extends="testbox.system.BaseSpec" {
builder.select( "foo" )
.addSelect( "bar" )
.addSelect( [ "baz", "boom" ] )
.addSelect( "fe", "fi", "fo" )
.from( "users" );
expect( builder.toSql() ).toBe(
"SELECT ""foo"", ""bar"", ""baz"", ""boom"", ""fe"", ""fi"", ""fo"" FROM ""users"""
"SELECT ""foo"", ""bar"", ""baz"", ""boom"" FROM ""users"""
);
} );

Expand Down Expand Up @@ -585,14 +584,14 @@ component extends="testbox.system.BaseSpec" {
query.where( "id", "=", 1 );
};

var default = function( query ) {
var defaultCallback = function( query ) {
query.where( "id", "=", 2 );
};

var builder = getBuilder();
builder.select( "*" )
.from( "users" )
.when( false, callback, default )
.when( false, callback, defaultCallback )
.where( "email", "foo" );
expect( builder.toSql() ).toBe(
"SELECT * FROM ""users"" WHERE ""id"" = ? AND ""email"" = ?"
Expand All @@ -605,14 +604,14 @@ component extends="testbox.system.BaseSpec" {
query.where( "id", "=", 1 );
};

var default = function( query ) {
var defaultCallback = function( query ) {
query.where( "id", "=", 2 );
};

var builder = getBuilder();
builder.select( "*" )
.from( "users" )
.when( true, callback, default )
.when( true, callback, defaultCallback )
.where( "email", "foo" );
expect( builder.toSql() ).toBe(
"SELECT * FROM ""users"" WHERE ""id"" = ? AND ""email"" = ?"
Expand All @@ -634,9 +633,19 @@ component extends="testbox.system.BaseSpec" {
it( "can group by multiple fields using variadic parameters", function() {
var builder = getBuilder();
builder.select( "*" ).from( "users" ).groupBy( "id", "email" );
expect( builder.toSql() ).toBe(
"SELECT * FROM ""users"" GROUP BY ""id"", ""email"""
);

// Variadic parameters are not consistent between engines, so test for both options
try {
expect( builder.toSql() ).toBe(
"SELECT * FROM ""users"" GROUP BY ""email"", ""id"""
);
}
catch ( any e ) {
expect( builder.toSql() ).toBe(
"SELECT * FROM ""users"" GROUP BY ""id"", ""email"""
);
}

expect( getTestBindings( builder ) ).toBe( [] );
} );

Expand Down Expand Up @@ -871,14 +880,12 @@ component extends="testbox.system.BaseSpec" {
}

private Builder function getBuilder() {
var builder = getMockBox()
.createMock( "qb.models.Query.Builder" ).init();
var grammar = getMockBox()
.createMock( "qb.models.Query.Grammars.Grammar" );
var queryUtils = getMockBox()
.createMock( "qb.models.Query.QueryUtils" );
builder.$property( propertyName = "grammar", mock = grammar );
builder.$property( propertyName = "utils", mock = queryUtils );
var builder = getMockBox().createMock( "qb.models.Query.Builder" )
.init( grammar, queryUtils);
return builder;
}

Expand Down

0 comments on commit 64c19c7

Please sign in to comment.