CRM_Utils_SQL - Add "onDuplicate()" and "syncInto()" helpers #17780
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This adds helper functions,
onDuplicate(...)
andsyncInto(...)
, for reading data from one table and writing to another.This is an offshoot of #17724 which only adds the helper function and the unit-test.
Before
It is possible to construct a
SELECT
query and insert the results to another table:However, this will always insert new records. It cannot be used to
UPDATE
existing records (i.e.INSERT INTO ... SELECT ... ON DUPLICATE UPDATE...
).The operation
REPLACE INTO
(replaceInto()
) is somewhat similar toON DUPLICATE UPDATE
. Both operations will insert a new record. But if there's a conflict/pre-existing record, theREPLACE INTO
will remove and replace the record, whereasON DUPLICATE UPDATE
only modifies fields. One benchmark reported a ~35x performance difference betweenREPLACE INTO
andON DUPLICATE UPDATE
.After
This first commit makes it possible to construct an
ON DUPLICATE UPDATE
query, eg.This parallels the underlying SQL structure. However, it is a bit verbose -- observe that every field on every side of the data-transfer (
foo_value1
,bar_value1
,foo_value2
, etc) has to be mentioned multiple times.The second commit provides a convenience function which does the same thing - but with cleaner notation: