Skip to content
Snippets Groups Projects

Replace \PDO::FETCH_* constants to indicate fetch mode with an enumeration

Closed mondrake requested to merge issue/drupal-3487851:3487851-replace-pdofetch-constants into 11.x

Closes #3487851

Merge request reports

Merge request pipeline passed for 6bbc0adc

Code Quality is loading
Test summary results are being parsed

Closed by mondrakemondrake 2 months ago (Feb 26, 2025 10:46pm UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
103 * @param \Drupal\Core\Database\FetchAs|null $mode
104 * (Optional) one of the cases of the FetchAs enum. If not specified,
105 * defaults to what is specified by setFetchMode().
106 * @param int|null $cursorOrientation
107 * Not implemented in all database drivers, don't use.
108 * @param int|null $cursorOffset
109 * Not implemented in all database drivers, don't use.
110 *
111 * @return array<string|int|float|bool>|object|false
112 * A result, formatted according to $mode, or FALSE on failure.
113 */
114 protected function clientFetch(?FetchAs $mode = NULL, ?int $cursorOrientation = NULL, ?int $cursorOffset = NULL) {
115 return $this->getClientStatement()->fetch(
116 $mode ? $this->fetchAsToPdo($mode) : \PDO::FETCH_DEFAULT,
117 $cursorOrientation ?? \PDO::FETCH_ORI_NEXT,
118 $cursorOffset ?? 0,
  • daffie
  • daffie
    daffie @daffie started a thread on the diff
  • 1 <?php
    2
    3 declare(strict_types=1);
    4
    5 namespace Drupal\Core\Database\Statement;
    6
    7 /**
    8 * A trait for calling \PDOStatement methods.
    9 */
    10 trait PdoTrait {
    11
    12 protected function fetchAsToPdo(FetchAs $mode): int {
    13 return match ($mode) {
    14 FetchAs::Associative => \PDO::FETCH_ASSOC,
    15 FetchAs::ClassObject => \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE,
    • Are with this change no longer supporting the single \PDO::FETCH_CLASS setting and no longer supporting \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE? I am not saying that it is wrong. Just asking the question.

    • Author Contributor

      Support for \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE was already deprecated in 10.2 and dropped in 11: https://www.drupal.org/node/3377999

      \PDO::FETCH_CLASS without \PDO::FETCH_PROPS_LATE cannot be supported. Props late means that the values are associated to the object properties after the object constructor is called. That is the only possible way if we want to allow non-PDO drivers to emulate the mode. Apparently PDO can work on low level to get the properties assigned before calling the constructor, but I am not aware of such a possibility in pure PHP.

    • Author Contributor

      Just a note that SQLite in HEAD is already missing the possibility to define properties before construction, because StatementPrefetchIterator does this: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Core/Database/StatementPrefetchIterator.php?ref_type=heads#L248,

      and assocToClass does instantiate the class before setting the properties:

        protected function assocToClass(array $rowAssoc, string $className, array $constructorArguments): object {
          $classObj = new $className(...$constructorArguments);
          foreach ($rowAssoc as $column => $value) {
            $classObj->$column = $value;
          }
          return $classObj;
        }
      Edited by mondrake
    • Please register or sign in to reply
  • daffie
  • The MR looks good. Just a couple questions?

  • mondrake added 1 commit

    added 1 commit

    Compare with previous version

  • mondrake added 21 commits

    added 21 commits

    Compare with previous version

  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Please register or sign in to reply
    Loading