pine3ree / pine3ree-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
文档
请查看php PDO手册了解标准的ext 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
返回迄今为止执行的数据库连接数