php / php/php-src

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

Offen
#7,803 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Extension: pdo (core) Extension: pdo_sqlite Feature
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führen Sie den mitgelieferten PHP-Reproducer aus und beginnen Sie mit den im Issue beschriebenen Einstiegspunkten der PDO-Unterklasse, sqliteCreateFunction() und __call(). Vergleichen Sie die fehlende CONCAT-Funktion mit dem erwarteten Abfrageergebnis; abgeschlossen ist die Aufgabe, wenn das Methoden-Dispatch-Verhalten geklärt oder seine Einschränkung eindeutig festgestellt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php, sqlite
Bereich
databases
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.