Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions models/QuickQB.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,27 @@ component
return super.update( argumentCollection = arguments );
}

/**
* Inserts rows that do not exist and updates rows matching the target columns.
*
* @values The values to insert or the columns selected by the source query.
* @target The columns used to determine whether a row already exists.
* @update The columns or explicit values to update when a row matches.
* @source An optional query builder or callback used as the source rows.
* @deleteUnmatched Whether to delete target rows missing from the source, or a callback constraining those deletes.
* @options Options passed to `queryExecute`.
* @toSql Whether to return SQL instead of executing the query.
* @matchNulls Whether two NULL target values should be considered a match. Supported by MERGE grammars.
*/
public any function upsert(
required any values,
required any target,
any update,
any source,
boolean deleteUnmatched = false,
struct options = {},
boolean toSql = false
any deleteUnmatched = false,
struct options = {},
boolean toSql = false,
boolean matchNulls = false
) {
if (
!isNull( arguments.source ) && isStruct( arguments.source ) && structKeyExists(
Expand Down
25 changes: 25 additions & 0 deletions tests/specs/integration/BaseEntity/QuerySpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ component extends="tests.resources.ModuleIntegrationSpec" {
expect( users[ 2 ].getId() ).toBe( 4 );
expect( users[ 2 ].getUsername() ).toBe( "elpete2" );
} );

it( "can upsert records through the entity query API", function() {
getInstance( "User" ).upsert(
values = [
{
"id" : 1,
"username" : "elpete",
"firstName" : "Updated",
"lastName" : "Peterson"
},
{
"id" : 99,
"username" : "new-user",
"firstName" : "New",
"lastName" : "User"
}
],
target = "id",
update = [ "firstName" ],
matchNulls = false
);

expect( getInstance( "User" ).findOrFail( 1 ).getFirstName() ).toBe( "Updated" );
expect( getInstance( "User" ).findOrFail( 99 ).getFirstName() ).toBe( "New" );
} );
} );
}

Expand Down
Loading