UpdateQuery::arguments() does not return all values.

Created on 28 May 2013, about 11 years ago
Updated 13 June 2024, 5 days ago

Problem/Motivation

With reference to #1222150: dpq() only works for SELECT and INSERT queries (a devel module issue), an outstanding issue with the output of its dpq() function on \Drupal::database()->update() queries is that the update field placeholders are still rendered as the placeholder name, not the value.

Proposed resolution

Implement arguments() in the Update.php to support update field placeholders.

Original report by [jweowu]

With reference to #1222150: dpq() only works for SELECT and INSERT queries (a devel module issue), an outstanding issue with the output of its dpq() function on db_update() queries is that the field placeholders are still rendered as the placeholder name, not the value.

e.g. dpq(db_update('node')->fields(array('uid' => 1))->condition('nid', 50)); produces:

UPDATE {node} SET uid=:db_update_placeholder_0
WHERE  (nid = '50') 

With arguments() returning:

Array
(
    [:db_condition_placeholder_0] => 50
)

This is because UpdateQuery::arguments() returns the db_condition_placeholder values, but does not return the db_update_placeholder values.

The reason the query still works is that UpdateQuery::execute() deals with these field placeholders separately; however it access them directly, and no public API analogous to arguments() is provided, so the likes of devel's dpq() have no way of obtaining them.

Now the docstring for arguments() is "Gets a complete list of all values to insert into the prepared statement.", and that's obviously not the case here, but given how the execute method works, I can't tell whether it's the docstring or the code which is incorrect. I can't actually see a reason not to include the field placeholders, though; because...

Unless I'm mis-reading my grep results, I don't think anything in core calls UpdateQuery::arguments() at all? SelectQuery::arguments() is certainly used, and $condition->arguments() is called directly in lots of places, but nothing in my code base save for devel's dpq() seems like it would ever to call this method on an update query.

Experimentally (using code from UpdateQuery::__toString()), if instead of only returning $this->condition->arguments(), I modify UpdateQuery::arguments() like so:

  /**
   * Implements QueryConditionInterface::arguments().
   */
  public function arguments() {
    $args = $this->condition->arguments();

    $fields = $this->fields;
    $max_placeholder = 0;
    foreach ($fields as $field => $value) {
      $args[':db_update_placeholder_' . ($max_placeholder++)] = $value;
    }
    return $args;
  }

Then arguments() returns:

Array
(
    [:db_condition_placeholder_0] => 50
    [:db_update_placeholder_0] => 1
)

and dpq() gives me:

UPDATE {node} SET uid='1'
WHERE  (nid = '50') 

So that seems like a viable solution?

🐛 Bug report
Status

RTBC

Version

11.0 🔥

Component
Database 

Last updated 5 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇳🇿New Zealand jweowu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Merge request !8080implements arguments function in update → (Open) created by sukr_s
  • Pipeline finished with Failed
    about 1 month ago
    Total: 189s
    #172981
  • Pipeline finished with Failed
    about 1 month ago
    Total: 1080s
    #172991
  • Pipeline finished with Canceled
    about 1 month ago
    Total: 9s
    #173008
  • Pipeline finished with Success
    about 1 month ago
    Total: 615s
    #173009
  • Status changed to Needs review about 1 month ago
  • 🇮🇳India sukr_s

    - Moved the patch to MR
    - Update the test for comment #30

  • Status changed to Needs work 21 days ago
  • 🇺🇸United States smustgrave

    Left a small comment

    Issue summary should follow standard issue template so tagged for such

    Thanks.

  • Pipeline finished with Failed
    20 days ago
    Total: 958s
    #184664
  • Pipeline finished with Success
    20 days ago
    Total: 588s
    #184682
  • Status changed to Needs review 20 days ago
  • 🇮🇳India sukr_s

    IS updated and review comment resolved.

  • Status changed to RTBC 20 days ago
  • 🇺🇸United States smustgrave

    Feedback appears to be resolved.

  • Status changed to Needs work 10 days ago
  • 🇬🇧United Kingdom longwave UK

    Added some comments to the MR.

  • Pipeline finished with Failed
    8 days ago
    Total: 531s
    #195273
  • Pipeline finished with Success
    8 days ago
    Total: 512s
    #195293
  • Status changed to Needs review 8 days ago
  • 🇮🇳India sukr_s

    MR comments resolved.

  • Status changed to RTBC 5 days ago
  • 🇺🇸United States smustgrave

    Refactoring suggestions appear to not have broken anything.

    Left a comment to confirm that @inheritdoc is inherited, least according to phpstorm.

Production build 0.69.0 2024