Added On Duplicate Update Function to Database Class#3694
Conversation
a2b5d23 to
214716f
Compare
php/libraries/Database.class.inc
Outdated
There was a problem hiding this comment.
small typo: determines whether the insert throws and error -> determines whether the insert throws an error.
|
Let's say a call to your new method is made in these situations:
What will be returned by |
|
Since getLastInsertID() only returns the value of the last
Perhaps it would be useful to have the |
php/libraries/Database.class.inc
Outdated
|
Does MySQL or the PDO provide any way to determine if the row was inserted or updated? I'm not sure that it's worth the effort, because I'm not sure what practical use cases it would be needed in, but if it's trivial to add, go ahead. Assuming that there's no way (short of a second select query) to determine that, I think documenting it in the function comment is sufficient. |
|
Sounds good. Looks like there's ways to go about doing this, but it would required some combination of The MySQL documentation states: |
9b5c60f to
955d099
Compare
|
After function rename commit, I was able to edit the date of administration and changes appeared in the module! |
miebeers
left a comment
There was a problem hiding this comment.
Looks great! Was able to successfully upload a file, edit a field, and see the changes reflected in the database.
|
Passed manual tests for me too. |
|
Looks good, but can you send it to 20.0-release branch so that it can be used after the next release instead of needing to wait for 20.1? I think I saw a PR to 20.0 that I wanted to recommend using this for, but then realized it's not merged there.. |
|
Nevermind, I don't think it's appropriate where I thought in 20.0, so 20.1 is fine. |
|
@driusan hey dave, is this PR ready to go? |
|
|
||
| if ($onDuplicateUpdate) { | ||
| $prepQ .= ' ON DUPLICATE KEY UPDATE '; | ||
| $prepQ .= $this->_implodeAsPrepared(",", $set, $exec_params); |
There was a problem hiding this comment.
from looking at line 382, should this line have a space after the comma in quotes? also, could we keep consistent with double and single quotes or is there a reason for the use of both here?
There was a problem hiding this comment.
No, no space is necessary after the comma here. The _implodeAsPrepared function takes care of that, while _implodeWithKeys is a less specialized function that doesn't. And I left it as a double quotes because the line is identical to line 380, not for any specific functional purpose. If anything, in the context of this class, the single quotes on lines 379, 381 and 382 look out of place. Actually, the whole class is a bit of a mix tbh. But I really don't mind either way.
|
I thought this was already merged.. |
This pull request adds two new methods to the database class:
insertOnDuplicateUpdate()andunsafeInsertOnDuplicateUpdate()These methods will attempt to
inserta new row into the database given the query array passed as an argument. However, if one of the values to be inserted is a duplicate value for a unique key column, the query will be changed to anupdatestatement for the row where the duplicate was found.The
unsafeversion of the method will not escape insertion of HTML characters or JSON objects.Reference:
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
This PR also includes a use-case in the media module, which consolidates a few lines of code using the new function.
How to test:
Mediamodule and click on theUploadtab.Mediamodule filter.editbutton, edit a field (e.g. data) and clickUpdate.Mediamodule filter.