Skip to content

Commit

Permalink
issue #8 - additional tests for rawExpressions in the array, removed …
Browse files Browse the repository at this point in the history
…lists as a valid value for array value and refactored validDirections array to be an instance variable aptly named to match the other naming conventions.
  • Loading branch information
Tim Brown authored and elpete committed Mar 23, 2017
1 parent e0b9b63 commit 8704ff4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
36 changes: 11 additions & 25 deletions models/Query/Builder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ component displayname="Builder" accessors="true" {
"update" = []
};

/**
* Array holding the valid directions that a column can be sorted by in an order by clause.
*/
variables.directions = [ "asc", "desc" ];

/**
* Creates an empty query builder.
*
Expand All @@ -141,6 +146,8 @@ component displayname="Builder" accessors="true" {
variables.grammar = arguments.grammar;
variables.utils = arguments.utils;



setReturnFormat( arguments.returnFormat );

setDefaultValues();
Expand Down Expand Up @@ -1114,7 +1121,6 @@ component displayname="Builder" accessors="true" {
* @return qb.models.Query.Builder
*/
public Builder function orderBy( required any column, string direction = "asc" ) {
var validDirections = [ "asc", "desc" ];

if ( getUtils().isExpression( column ) ) {
variables.orders.append( {
Expand All @@ -1134,32 +1140,12 @@ component displayname="Builder" accessors="true" {
column = col
} );
}
// ex: "favorite_color|desc,height|asc,weight|desc"
else if ( isSimpleValue( col ) && listLen( col ) > 1 ) {
//convert list to array for easier looping and access to vals
var arCols = listToArray( col );

for ( var c in arCols ) {
var colName = listFirst( c, "|" );

if ( listLen( c, "|" ) == 2 ) {
var dir = ( arrayFindNoCase( validDirections, listLast( c, "|" ) ) ) ? listLast( c, "|" ) : direction;
} else {
var dir = direction;
}

variables.orders.append( {
direction = dir,
column = colName
} );
}
}
// ex: "age|desc" || "last_name"
else if ( isSimpleValue( col ) ) {
var colName = listFirst( col, "|" );
// ex: "age|desc"
if ( listLen( col, "|" ) == 2 ) {
var dir = ( arrayFindNoCase( validDirections, listLast( col, "|" ) ) ) ? listLast( col, "|" ) : direction;
var dir = ( arrayFindNoCase( variables.directions, listLast( col, "|" ) ) ) ? listLast( col, "|" ) : direction;
} else {
var dir = direction;
}
Expand All @@ -1176,7 +1162,7 @@ component displayname="Builder" accessors="true" {
if ( getUtils().isExpression( col.column ) ) {
var dir = "raw";
} else {
var dir = ( structKeyExists( col, "direction") && arrayFindNoCase( validDirections, col.direction ) ) ? col.direction : direction;
var dir = ( structKeyExists( col, "direction") && arrayFindNoCase( variables.directions, col.direction ) ) ? col.direction : direction;
}
variables.orders.append({
direction = dir,
Expand All @@ -1187,7 +1173,7 @@ component displayname="Builder" accessors="true" {
else if ( isArray( col ) ) {
//assume position 1 is the column name and position 2 if it exists and is a valid direction ( asc | desc ) use it.
variables.orders.append({
direction = ( arrayLen( col ) == 2 && arrayFindNoCase( validDirections, col[2] ) ) ? col[2] : direction,
direction = ( arrayLen( col ) == 2 && arrayFindNoCase( variables.directions, col[2] ) ) ? col[2] : direction,
column = col[1]
});
}
Expand All @@ -1204,7 +1190,7 @@ component displayname="Builder" accessors="true" {
var colName = listFirst( col, "|" );

if ( listLen( col, "|" ) == 2 ) {
var dir = ( arrayFindNoCase( validDirections, listLast( col, "|" ) ) ) ? listLast( col, "|" ) : direction;
var dir = ( arrayFindNoCase( variables.directions, listLast( col, "|" ) ) ) ? listLast( col, "|" ) : direction;
} else {
var dir = direction;
}
Expand Down
40 changes: 33 additions & 7 deletions tests/specs/Query/Builder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -827,10 +827,13 @@ component extends="testbox.system.BaseSpec" {
it( "as raw expressions", function(){
var builder = getBuilder();
builder.select( "*").from( "users" )
.orderBy( [ "last_name|desc", "age|asc", "favorite_color|desc" ] );
.orderBy( [
builder.raw( "DATE(created_at)" ),
{ column = builder.raw( "DATE(modified_at)" ) }
] );

expect( builder.toSql() ).toBeWithCase(
"SELECT * FROM ""users"" ORDER BY ""last_name"" DESC, ""age"" ASC, ""favorite_color"" DESC"
"SELECT * FROM ""users"" ORDER BY DATE(created_at), DATE(modified_at)"
);
});

Expand All @@ -847,20 +850,44 @@ component extends="testbox.system.BaseSpec" {
it( "can accept a struct with a column key and optionally the direction key", function(){
var builder = getBuilder();
builder.select( "*").from( "users" )
.orderBy( [ { column = "last_name" } , { column = "age", direction = "asc" },{ column = "favorite_color", direction = "desc" } ], "desc" );
.orderBy( [
{ column = "last_name" },
{ column = "age", direction = "asc" },
{ column = "favorite_color", direction = "desc" }
], "desc" );

expect( builder.toSql() ).toBeWithCase(
"SELECT * FROM ""users"" ORDER BY ""last_name"" DESC, ""age"" ASC, ""favorite_color"" DESC"
);
});

it( "as values that when additional orderBy() calls are chained the chained calls preserve the order of the calls", function(){
var builder = getBuilder();
builder.select( "*").from( "users" )
.orderBy( [
"last_name",
"age|desc"
])
.orderBy( "favorite_color", "desc" )
.orderBy( column = [ { column = "height" }, { column = "weight", direction = "asc" } ], direction = "desc" )
.orderBy( column = "eye_color", direction = "desc" )
.orderBy( [
{ column = "is_athletic", direction = "desc", extraKey = "ignored" },
builder.raw( "DATE(created_at)" )
])
.orderBy( builder.raw( "DATE(modified_at)" ) );

expect( builder.toSql() ).toBeWithCase(
"SELECT * FROM ""users"" ORDER BY ""last_name"" ASC, ""age"" DESC, ""favorite_color"" DESC, ""height"" DESC, ""weight"" ASC, ""eye_color"" DESC, ""is_athletic"" DESC, DATE(created_at), DATE(modified_at)"
);
});

it( "as any combo of any valid values intermingled", function(){
var builder = getBuilder();
builder.select( "*").from( "users" )
.orderBy( [
"last_name",
"age|desc",
"favorite_color|desc,height|asc,weight|desc",
[ "eye_color", "desc" ],
[ "hair_color" ],
{ column = "is_musical" },
Expand All @@ -870,7 +897,7 @@ component extends="testbox.system.BaseSpec" {
] );

expect( builder.toSql() ).toBeWithCase(
"SELECT * FROM ""users"" ORDER BY ""last_name"" ASC, ""age"" DESC, ""favorite_color"" DESC, ""height"" ASC, ""weight"" DESC, ""eye_color"" DESC, ""hair_color"" ASC, ""is_musical"" ASC, ""is_athletic"" DESC, DATE(created_at), DATE(modified_at)"
"SELECT * FROM ""users"" ORDER BY ""last_name"" ASC, ""age"" DESC, ""eye_color"" DESC, ""hair_color"" ASC, ""is_musical"" ASC, ""is_athletic"" DESC, DATE(created_at), DATE(modified_at)"
);
});

Expand Down Expand Up @@ -912,7 +939,7 @@ component extends="testbox.system.BaseSpec" {
);
});

it( "as column names with secondary piped delimited value representing the direction for each column and inheriting direction from the direction argument's value when supplied", function(){
it( "as column names with optional secondary piped delimited value representing the direction for that column and inherits the direction argument's value when supplied", function(){
var builder = getBuilder();
builder.select( "*").from( "users" )
.orderBy( "last_name|asc,age,favorite_color|asc", "desc" );
Expand All @@ -925,7 +952,6 @@ component extends="testbox.system.BaseSpec" {
});

});


} );

Expand Down

0 comments on commit 8704ff4

Please sign in to comment.