Skip to content
Permalink
Browse files

fix(QueryBuilder): Fix using with query param structs

  • Loading branch information
elpete committed Mar 4, 2020
1 parent 79b858b commit 07c9b728bdbad6bf02ccd9d21dbdf6968062c02e
@@ -1306,6 +1306,17 @@ component displayname="QueryBuilder" accessors="true" {
callback( arguments.end );
}

addBindings( utils.extractBinding( arguments.start ), "where" );
addBindings( utils.extractBinding( arguments.end ), "where" );

if ( isStruct( arguments.start ) && !structKeyExists( arguments.start, "isBuilder" ) && arguments.start.keyExists( "value" ) ) {
arguments.start = arguments.start.value;
}

if ( isStruct( arguments.end ) && !structKeyExists( arguments.end, "isBuilder" ) && arguments.end.keyExists( "value" ) ) {
arguments.end = arguments.end.value;
}

variables.wheres.append( {
type: type,
column: applyColumnFormatter( arguments.column ),
@@ -1314,8 +1325,6 @@ component displayname="QueryBuilder" accessors="true" {
combinator: arguments.combinator
} );

addBindings( utils.extractBinding( arguments.start ), "where" );
addBindings( utils.extractBinding( arguments.end ), "where" );

return this;
}
@@ -234,6 +234,22 @@ component extends="testbox.system.BaseSpec" {
}, basicWhere() );
} );

it( "can add a where statement with a query param struct", function() {
testCase( function( builder ) {
builder
.select( "*" )
.from( "users" )
.where(
"createdDate",
">=",
{
value = "01/01/2019",
cfsqltype = "CF_SQL_TIMESTAMP"
}
);
}, basicWhereWithQueryParamStruct() );
} );

it( "can add or where statements", function() {
testCase( function( builder ) {
builder
@@ -490,6 +506,19 @@ component extends="testbox.system.BaseSpec" {
}, whereBetween() );
} );

it( "can add where between statements with query param structs", function() {
testCase( function( builder ) {
builder
.select( "*" )
.from( "users" )
.whereBetween(
"createdDate",
{ value = "1/1/2019", cfsqltype = "CF_SQL_TIMESTAMP" },
{ value = "12/31/2019", cfsqltype = "CF_SQL_TIMESTAMP" }
);
}, whereBetweenWithQueryParamStructs() );
} );

it( "can add where not between statements", function() {
testCase( function( builder ) {
builder
@@ -576,6 +605,12 @@ component extends="testbox.system.BaseSpec" {
}, whereInArray() );
} );

it( "can add where in statements from an array", function() {
testCase( function( builder ) {
builder.from( "users" ).whereIn( "id", [ 1, { value: 2, cfsqltype: "CF_SQL_INTEGER" }, 3 ] );
}, whereInArrayOfQueryParamStructs() );
} );

it( "can add or where in statements", function() {
testCase( function( builder ) {
builder
@@ -99,6 +99,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM `users` WHERE `id` = ?", bindings: [ 1 ] };
}

function basicWhereWithQueryParamStruct() {
return { sql: "SELECT * FROM `users` WHERE `createdDate` >= ?", bindings: [ "01/01/2019" ] };
}

function orWhere() {
return { sql: "SELECT * FROM `users` WHERE `id` = ? OR `email` = ?", bindings: [ 1, "foo" ] };
}
@@ -179,6 +183,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM `users` WHERE `id` BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}

function whereBetweenWithQueryParamStructs() {
return { sql: "SELECT * FROM `users` WHERE `createdDate` BETWEEN ? AND ?", bindings: [ "1/1/2019", "12/31/2019" ] };
}

function whereNotBetween() {
return { sql: "SELECT * FROM `users` WHERE `id` NOT BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}
@@ -191,6 +199,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM `users` WHERE `id` IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function whereInArrayOfQueryParamStructs() {
return { sql: "SELECT * FROM `users` WHERE `id` IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function orWhereIn() {
return { sql: "SELECT * FROM `users` WHERE `email` = ? OR `id` IN (?, ?, ?)", bindings: [ "foo", 1, 2, 3 ] };
}
@@ -102,6 +102,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM ""USERS"" WHERE ""ID"" = ?", bindings: [ 1 ] };
}

function basicWhereWithQueryParamStruct() {
return { sql: "SELECT * FROM ""USERS"" WHERE ""CREATEDDATE"" >= ?", bindings: [ "01/01/2019" ] };
}

function orWhere() {
return { sql: "SELECT * FROM ""USERS"" WHERE ""ID"" = ? OR ""EMAIL"" = ?", bindings: [ 1, "foo" ] };
}
@@ -182,6 +186,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM ""USERS"" WHERE ""ID"" BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}

function whereBetweenWithQueryParamStructs() {
return { sql: "SELECT * FROM ""USERS"" WHERE ""CREATEDDATE"" BETWEEN ? AND ?", bindings: [ "1/1/2019", "12/31/2019" ] };
}

function whereNotBetween() {
return { sql: "SELECT * FROM ""USERS"" WHERE ""ID"" NOT BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}
@@ -194,6 +202,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM ""USERS"" WHERE ""ID"" IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function whereInArrayOfQueryParamStructs() {
return { sql: "SELECT * FROM ""USERS"" WHERE ""ID"" IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function orWhereIn() {
return {
sql: "SELECT * FROM ""USERS"" WHERE ""EMAIL"" = ? OR ""ID"" IN (?, ?, ?)",
@@ -102,6 +102,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM ""users"" WHERE ""id"" = ?", bindings: [ 1 ] };
}

function basicWhereWithQueryParamStruct() {
return { sql: "SELECT * FROM ""users"" WHERE ""createdDate"" >= ?", bindings: [ "01/01/2019" ] };
}

function orWhere() {
return { sql: "SELECT * FROM ""users"" WHERE ""id"" = ? OR ""email"" = ?", bindings: [ 1, "foo" ] };
}
@@ -182,6 +186,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM ""users"" WHERE ""id"" BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}

function whereBetweenWithQueryParamStructs() {
return { sql: "SELECT * FROM ""users"" WHERE ""createdDate"" BETWEEN ? AND ?", bindings: [ "1/1/2019", "12/31/2019" ] };
}

function whereNotBetween() {
return { sql: "SELECT * FROM ""users"" WHERE ""id"" NOT BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}
@@ -194,6 +202,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM ""users"" WHERE ""id"" IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function whereInArrayOfQueryParamStructs() {
return { sql: "SELECT * FROM ""users"" WHERE ""id"" IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function orWhereIn() {
return {
sql: "SELECT * FROM ""users"" WHERE ""email"" = ? OR ""id"" IN (?, ?, ?)",
@@ -99,6 +99,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM [users] WHERE [id] = ?", bindings: [ 1 ] };
}

function basicWhereWithQueryParamStruct() {
return { sql: "SELECT * FROM [users] WHERE [createdDate] >= ?", bindings: [ "01/01/2019" ] };
}

function orWhere() {
return { sql: "SELECT * FROM [users] WHERE [id] = ? OR [email] = ?", bindings: [ 1, "foo" ] };
}
@@ -179,6 +183,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM [users] WHERE [id] BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}

function whereBetweenWithQueryParamStructs() {
return { sql: "SELECT * FROM [users] WHERE [createdDate] BETWEEN ? AND ?", bindings: [ "1/1/2019", "12/31/2019" ] };
}

function whereNotBetween() {
return { sql: "SELECT * FROM [users] WHERE [id] NOT BETWEEN ? AND ?", bindings: [ 1, 2 ] };
}
@@ -191,6 +199,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { sql: "SELECT * FROM [users] WHERE [id] IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function whereInArrayOfQueryParamStructs() {
return { sql: "SELECT * FROM [users] WHERE [id] IN (?, ?, ?)", bindings: [ 1, 2, 3 ] };
}

function orWhereIn() {
return { sql: "SELECT * FROM [users] WHERE [email] = ? OR [id] IN (?, ?, ?)", bindings: [ "foo", 1, 2, 3 ] };
}

0 comments on commit 07c9b72

Please sign in to comment.
You can’t perform that action at this time.