Extends PDO, sqliteCreateFunction() and magic method __call()
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the supplied PHP reproducer and start with the PDO subclass, sqliteCreateFunction(), and __call() entry points described in the issue. Compare the missing CONCAT function with the expected query result; done means the method-dispatch behavior is resolved or its limitation is clearly established.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, sqlite
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100