Skip to content

Commit

Permalink
feat(QueryBuilder): Use addUpdate to progressively add columns to update
Browse files Browse the repository at this point in the history
The addUpdate method can be used like addSelect to
progressively add columns to the SET clause.
The `values` passed to `update` will be merged with
the values already set using `addUpdate`.
  • Loading branch information
elpete committed Oct 3, 2019
1 parent ec23bb7 commit 65ad791
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
22 changes: 21 additions & 1 deletion models/Query/QueryBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ component displayname="QueryBuilder" accessors="true" {
*/
property name="returning" type="array";

/**
* An array of columns to return from an insert statement.
* @default []
*/
property name="updates" type="struct";

/**
* The list of allowed operators in join and where statements.
*/
Expand Down Expand Up @@ -214,6 +220,7 @@ component displayname="QueryBuilder" accessors="true" {
variables.orders = [];
variables.unions = [];
variables.returning = [];
variables.updates = {};
}

/**********************************************************************************************\
Expand Down Expand Up @@ -2037,10 +2044,11 @@ component displayname="QueryBuilder" accessors="true" {
* @return query
*/
public any function update(
required struct values,
struct values = {},
struct options = {},
boolean toSql = false
) {
structAppend( arguments.values, variables.updates );
var updateArray = values.keyArray().map( applyColumnFormatter );
updateArray.sort( "textnocase" );

Expand All @@ -2060,6 +2068,18 @@ component displayname="QueryBuilder" accessors="true" {
return runQuery( sql, arguments.options, "result" );
}

/**
* Adds values to later update
*
* @values A struct of values to update
*
* @return qb.models.Query.QueryBuilder
*/
function addUpdate( required struct values ) {
structAppend( variables.updates, arguments.values, true );
return this;
}

/**
* If the query returns any rows, updates the first result found. Otherwise, inserts the values into the table.
* This call must come after setting the query's table using `from` or `table`.
Expand Down
22 changes: 22 additions & 0 deletions tests/resources/AbstractQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,28 @@ component extends="testbox.system.BaseSpec" {
}, toSql = true );
}, updateWithRaw() );
} );

it( "can add incrementally with addUpdate", function() {
testCase( function( builder ) {
return builder.from( "users" )
.whereId( 1 )
.addUpdate( {
"email" = "foo",
"name" = "bar"
} )
.when( true, function( q ) {
q.addUpdate( {
"foo": "yes"
} );
} )
.when( false, function( q ) {
q.addUpdate( {
"bar": "no"
} );
} )
.update( toSql = true );
}, addUpdate() );
} );
} );

describe( "updateOrInsert statements", function() {
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/Query/MSSQLQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function addUpdate() {
return {
sql = "UPDATE [users] SET [email] = ?, [foo] = ?, [name] = ? WHERE [Id] = ?",
bindings = [ "foo", "yes", "bar", 1 ]
};
}

function updateOrInsertNotExists() {
return {
sql = "INSERT INTO [users] ([name]) VALUES (?)",
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/Query/MySQLQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function addUpdate() {
return {
sql = "UPDATE `users` SET `email` = ?, `foo` = ?, `name` = ? WHERE `Id` = ?",
bindings = [ "foo", "yes", "bar", 1 ]
};
}

function updateOrInsertNotExists() {
return {
sql = "INSERT INTO `users` (`name`) VALUES (?)",
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/Query/OracleQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function addUpdate() {
return {
sql = "UPDATE ""USERS"" SET ""EMAIL"" = ?, ""FOO"" = ?, ""NAME"" = ? WHERE ""ID"" = ?",
bindings = [ "foo", "yes", "bar", 1 ]
};
}

function updateOrInsertNotExists() {
return {
sql = "INSERT ALL INTO ""USERS"" (""NAME"") VALUES (?) SELECT 1 FROM dual",
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/Query/PostgresQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function addUpdate() {
return {
sql = "UPDATE ""users"" SET ""email"" = ?, ""foo"" = ?, ""name"" = ? WHERE ""Id"" = ?",
bindings = [ "foo", "yes", "bar", 1 ]
};
}

function updateOrInsertNotExists() {
return {
sql = "INSERT INTO ""users"" (""name"") VALUES (?)",
Expand Down

0 comments on commit 65ad791

Please sign in to comment.