pine3ree / p3-pdo
一个用于懒加载实例化和查询分析的PDO包装器
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0
- ext-pdo: *
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^9.6.13
- squizlabs/php_codesniffer: ^3.7
Conflicts
README
懒加载PDO的即时替换!
pine3ree-PDO 扩展了 PHP ext-PDO,以便在需要时提供连接、带自动重连的连接过期和查询记录/分析。
安装
该库的版本(3.0.x)需要 php ~8.0 || ~8.1.0 || ~8.2.0。
要支持 php-7.4,请使用版本 2.0.x。
您可以使用 Composer(带有 "minimum-stability": "dev")安装此库
$ composer require pine3ree/pine3ree-pdo
文档
有关标准 ext PDO 方法的详细信息,请参阅 php PDO 书籍。
继续阅读以下内容,了解其他方法。
如何使用懒加载pdo实例
只需像使用标准 ext-pdo PDO 类一样实例化提供的懒类。当真正需要时,将按需创建包装的标准 PDO 实例。
$pdo = new pine3ree\PDO( $dsn = 'sqlite:my-db.sqlite3', $username = '', $password = '', $options = [] );
默认情况下,pine3ree\PDO及其子类pine3ree\PDO\Reconnecting\PDO会在需要时建立数据库连接。
触发连接的方法有
pine3ree\PDO::beginTransaction();pine3ree\PDO::exec(...);pine3ree\PDO::prepare(...);pine3ree\PDO::query(...);pine3ree\PDO::quote(...);pine3ree\PDO::execute(...);
如何启用查询分析
通过提供的分析类和传递另一个pdo实例(无论是标准的 ext-pdo 实例还是扩展它的类的实例(例如此包中的懒-pdo)来记录/分析查询
$pdo = new pine3ree\PDO\Profiling\PDO(new \PDO( $dsn = 'sqlite:my-db.sqlite3', $username = '', $password = '', $options = [] ));
您可以通过调用 pine3ree\PDO\Profiling\PDO::getLog() 方法检索记录的信息。
如何使用自动重连/连接过期实例
使用提供的重连pdo类,并带有额外的 $ttl 构造函数参数
$pdo = new pine3ree\PDO\Reconnecting\PDO( $dsn = 'sqlite:my-db.sqlite3', $username = '', $password = '', $options = [], $ttl = 6 // drops the current connection after 6 seconds and establish a new one on demand );
其他方法
pine3ree\PDO::execute(): \PDOStatement|false
pine3ree\PDO::execute(string $statement, array $input_parameters = [], array $driver_options = [])
将 \PDO::prepare() 和 \PDOStatement::execute() 合并为一个方法调用,如果语句准备或执行失败,则返回 false。
此方法由 pine3ree\PDO\Reconnecting\PDO 继承。
pine3ree\PDO::isConnected(): bool
检查我们是否建立了数据库连接。
此方法由 pine3ree\PDO\Reconnecting\PDO 继承,并在 pine3ree\PDO\Profiling\PDO 中实现。
pine3ree\PDO\Profiling\PDO::getLog(): array
返回以下格式的所有已执行语句的记录分析信息
[
// every runned query including re-runs
'statements' => [
0 => [...],
1 => [...],
//....
n => [
'sql' => "SELECT * FROM `user` WHERE `id` = :id",
'iter' => 2, // the iteration index for this sql expression
'time' => 0.000254..., // in seconds.microseconds
'params' => [':id' => 123]
],
],
// queries indexed by sql expression
'reruns' => [
'md5(sql1)' => [...],
//...,
'md5(sqln)' => [
'sql' => "SELECT * FROM `users` WHERE `status` = 1",
'iter' => 5, // the number of iterations for this sql expression
'time' => 0.001473..., // total time for all re-runs
],
],
'time' => 23.5678, // total query time
'count' => 15, // total query count
];
pine3ree\PDO\Reconnecting\PDO::getConnectionCount(): int
返回迄今为止执行的数据库连接数