php / php/php-src

Extends PDO, sqliteCreateFunction() and magic method __call()

オープン
#7,803 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Extension: pdo (core) Extension: pdo_sqlite Feature
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

The following code:

<?php
declare(strict_types=1);

class DBStatement extends PDOStatement
{
    protected $db;

    protected function __construct(PDO $db)
    {
        $this->db = $db;
    }
}
class db extends PDO {
    public function __construct(string $dsn, string $username = null, string $password = null, array $options = [])
    {
        $options += [
            self::ATTR_DEFAULT_FETCH_MODE => self::FETCH_ASSOC,
            self::ATTR_EMULATE_PREPARES   => false,
            self::ATTR_STRINGIFY_FETCHES  => false,
            self::ATTR_ERRMODE            => self::ERRMODE_EXCEPTION,
            self::ATTR_STATEMENT_CLASS    => [DBStatement::class, [$this]],
        ];
        parent::__construct($dsn, $username, $password, $options);
        $this->sqliteCreateFunction('CONCAT', function (...$args) {return \implode('', $args);});
    }

    public function __call(string $name, array $args) /* : mixed */
    {
        // Here is the loading of a class with rarely used methods that are different in implementation for different databases.
        // In a real project, an exception is displayed: 
        // PHP OTHER_ERROR: Method 'sqliteCreateFunction' not found in DB driver. in ...\app\Core\DB\Sqlite.php:[70]
    }
}

$db = new db('sqlite:datadb', '', '', []);
var_dump($db->query('SELECT CONCAT(\'123\', \'456\', \'789\')')->fetch());
exit;

Resulted in this output:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 no such function: CONCAT in C:\laragon\www\1.php:33 Stack trace: #0 C:\laragon\www\1.php(33): PDO->query('SELECT CONCAT('...') #1 {main} thrown in C:\laragon\www\1.php on line 33

But I expected this output instead:

array(1) { ["CONCAT('123', '456', '789')"]=> string(9) "123456789" } 

It turns out that the call to the sqliteCreateFunction() method is intercepted by __call()?

P.S. In my project, I extended PDO, extended PDOStatement and use _call () to call rare methods to change the structure of the database. Now I got such an incident when using SQLite.

PHP Version

PHP 8.1.1-Win32-vs16-x64, 8.1.0, 8.0.8, 7.4.5, 7.3.33

Operating System

Windows 7 x64

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供された PHP 再現コードを実行し、issue で説明されている PDO サブクラス、sqliteCreateFunction()、__call() のエントリポイントから調査を始めてください。不足している CONCAT 関数と期待されるクエリ結果を比較してください。メソッドディスパッチの動作が解決されるか、その制限が明確に確認できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php, sqlite
領域
databases
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。