API page: http://api.drupal.org/api/drupal/includes%21database%21query.inc/functio...

It's quite important to know what the execute() method returns here. Reading the code (which BTW doesn't seem to be visible on the api site), it looks like that's one of:

- return MergeQuery::STATUS_UPDATE;
- return MergeQuery::STATUS_INSERT;
- I'm not sure if it's possible to get to the end of the function body and return NULL

Issue fork drupal-1479220

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Issue tags: +Needs backport to D7

Code not being visible - That's a known/fixed bug in the API module (the fix just hasn't been deployed to api.drupal.org yet, but should be in a few days).

Lack of return value - agreed, should be fixed. Thanks for reporting!

David Jeyachandran’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
1002 bytes

Here's a new patch where we've added documentation for the execute method found in the file: core/lib/Drupal/Core/Merge.php .

Thanks to Josimar, Julio and David.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the patch! There are a few problems:

a) The exceptions -- it should specifically document which exceptions it can throw, and under what circumstances, rather than a generic "@throws Exception".

b)

+   * Only if the queryOptions['throw_exception'] is TRUE (default), an exception
+   * could be thrown.

Can this be turned around to be clearer? Maybe something like "If there is a problem with the query and $this->queryOptions['throw_exception'] is TRUE (default), an exception will be thrown. Otherwise, a NULL value will be returned.

c) The @return section has a few problems:

+   * @return
+   *   - Marge::STATUS_INSERT: If the entry does NOT exists in the database,
+   *     it executes an INSERT.
+   *   - Merge::STATUS_UPDATE: If the entry exists then it executes an UPDATE.
+   *   - NULL: If there is an exception and queryOptions['throw_exception'] is
+   *   FALSE

- First line: Marge -> Merge
- First line: exists -> exist
- There should be a line introducing the list. Something like "One of the following values:"
- Last line needs to be indented two spaces and end with .
- We shouldn't have NOT be all-caps.
- Can we make the first two items more parallel in their grammatical structure? How about "If the entry already exists, and an UPDATE query is executed" and "If the entry does not already exist, and an INSERT query is executed"?

Thanks!

InternetDevels’s picture

Status: Needs work » Needs review
FileSize
1.22 KB
1.57 KB

Added new patch.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the new patch!

There are a few things to clean up:

a)

+   * If $this->queryOptions['throw_exception'] is TRUE (default)
+   * and there is a problem with the query, an exception will be thrown.
+   * Otherwise, a NULL value will be returned.

Can this be wrapped into one paragraph, closer to 80-character lines? Also, I think the last line "Otherwise..." is a bit ambiguous, because the if() in the first sentence is compound. Can it be more explicit like "If ... is FALSE and there is a problem with the query, a NULL value will be returned." rather than just saying "Otherwise"?

b)

+   *   One of the following values:
+   *     - Merge::STATUS_INSERT: If the entry does not already exist,
+   *       and an INSERT query is executed.
+   *     - Merge::STATUS_UPDATE: If the entry already exists,
+   *       and an UPDATE query is executed.
+   *     - NULL: If there is an exception and queryOptions['throw_exception'] is
+   *       FALSE.

This list is not formatted correctly. See https://drupal.org/coding-standards/docs#lists

I also don't like the last item -- "if there is an exception" when above you said "if there is a problem".

c)

+   * @throws \Drupal\Core\Database\Query\InvalidMergeQueryException
+   * @throws \Drupal\Core\Database\IntegrityConstraintViolationException
+   * @throws \Exception

Not having a description on the first two exceptions is probably fine, because they are self-documenting. But for the last one, you need to describe when the generic "Exception" could be thrown. See
https://drupal.org/coding-standards/docs#throws

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

catch’s picture

Title: document what MergeQuery::execute() returns » Add better return documentation for Merge::execute()
Version: 8.9.x-dev » 9.2.x-dev
Category: Bug report » Task
Issue tags: +Novice, +Bug Smash Initiative

This is still valid, we should start with addressing #5.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%...

mondrake’s picture

Component: documentation » database system
Issue tags: +Documentation
sudiptadas19’s picture

Issue tags: +Needs reroll

Not able to apply patch given in #4, reroll needed.

sudiptadas19’s picture

Status: Needs work » Needs review
FileSize
1.28 KB
2.15 KB

Feedback from #5 addressed.

sudiptadas19’s picture

Fixed PHPCBF issue.

mondrake’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll
  1. +++ b/core/lib/Drupal/Core/Database/Query/Merge.php
    @@ -351,6 +351,28 @@ public function key($field, $value = NULL) {
    +   * If $this->queryOptions['throw_exception'] is TRUE (default)
    +   * and there is a problem with the query, an exception will be thrown.
    +   * If $this->queryOptions['throw_exception'] is FALSE and
    +   * there is a problem with the query, a NULL value will be returned.
    +   *
    

    The 'throw_exception' option is deprecated now, so I think we can just remove this piece.

  2. +++ b/core/lib/Drupal/Core/Database/Query/Merge.php
    @@ -351,6 +351,28 @@ public function key($field, $value = NULL) {
    +   * @throws \Exception
    +   *   When the generic "Exception" could be thrown.
    

    I do not think we would ever throw a generic \Exception nowadays.

  3. +++ b/core/lib/Drupal/Core/Database/Query/Merge.php
    @@ -351,6 +351,28 @@ public function key($field, $value = NULL) {
    +   * @throws \Drupal\Core\Database\Query\InvalidMergeQueryException
    +   * @throws \Drupal\Core\Database\IntegrityConstraintViolationException
    

    We can take from the comments in code and the exception messages and add here the occasion when the exceptions are thrown.

sudiptadas19’s picture

Status: Needs work » Needs review
FileSize
1.07 KB
1.09 KB

Feedback addressed mentioned in #19.
Please review.

paulocs’s picture

Fixing code standard.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The docblock looks good to me.
All the points of @mondrake are addressed.
For me it is RTBC.

mondrake’s picture

Status: Reviewed & tested by the community » Needs work

Sorry to put it back, I read the actual code and have a couple more points.

  1. +++ b/core/lib/Drupal/Core/Database/Query/Merge.php
    @@ -351,6 +351,23 @@ public function key($field, $value = NULL) {
    +   *   - NULL: If there is a problem and queryOptions['throw_exception'] is
    +   *     FALSE.
    

    Since queryOptions['throw_exception'] is deprecated, I think it'd be better to reflect in the comment:

       *   - NULL: (deprecated) If there is a problem and queryOptions['throw_exception'] is  FALSE.
    
  2. +++ b/core/lib/Drupal/Core/Database/Query/Merge.php
    @@ -351,6 +351,23 @@ public function key($field, $value = NULL) {
    +   * @throws \Drupal\Core\Database\IntegrityConstraintViolationException
    +   *   When trying to insert a row that would violate a unique key constraint.
    

    Actually I think this is never thrown outside the method. This exception is used within the method to catch INSERT failures and in that case resolve to UPDATE instead.

guilhermevp’s picture

Assigned: Unassigned » guilhermevp

guilhermevp’s picture

Status: Needs work » Needs review

Created a MR based on the feedback in #23, but I'm not exactly sure in what to do about @throws \Drupal\Core\Database\IntegrityConstraintViolationException, moving to NR for feedback.

mondrake’s picture

Status: Needs review » Needs work

#26 just remove it. The purpose of listing the @throws in the docblock is to let developers know what exceptions they could expect to have to deal with. If its use is within the method, then this is not relevant.

guilhermevp’s picture

Status: Needs work » Needs review

Thanks @mondrake!

Removed, please review!

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me now, thanks!

mondrake’s picture

Title: Add better return documentation for Merge::execute() » Add return documentation for Merge::execute()

  • catch committed 477095b on 9.3.x
    Issue #1479220 by sudiptadas19, guilhermevp, InternetDevels, paulocs,...

  • catch committed 39f783f on 9.2.x
    Issue #1479220 by sudiptadas19, guilhermevp, InternetDevels, paulocs,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.