pine3ree/pine3ree-pdo
Composer 安装命令:
composer require pine3ree/pine3ree-pdo
包简介
A tiny PDO wrapper for lazy instantiation and query profiling
README 文档
README
A lazy-loading PDO drop-in replacement!
pine3ree-PDO extends PHP ext-PDO in order to provide on demand connection, connection expiration with auto-reconnect and query logging/profiling.
Installation
This version (3.0.x) of the library requires php ~8.0 || ~8.1.0 || ~8.2.0.
For php-7.4 support please use version 2.0.x.
You can install it library using Composer (with "minimum-stability": "dev"):
$ composer require pine3ree/pine3ree-pdo
Documentation
Check the php PDO book for standard ext PDO methods.
Continue reading below for additional methods.
How to use the lazy pdo instance
Just instantiate the provided lazy class as you would with the standard ext-pdo PDO class. A wrapped standard PDO instance will be created on demand when really needed.
$pdo = new pine3ree\PDO( $dsn = 'sqlite:my-db.sqlite3', $username = '', $password = '', $options = [] );
By default pine3ree\PDO and its descendant pine3ree\PDO\Reconnecting\PDO establish a
database connection on demand.
The methods that trigger the connection are:
pine3ree\PDO::beginTransaction();pine3ree\PDO::exec(...);pine3ree\PDO::prepare(...);pine3ree\PDO::query(...);pine3ree\PDO::quote(...);pine3ree\PDO::execute(...);
How to enable query-profiling
Query logging/profiling can be achieved via the provided profiling class and passing another pdo instance (either a standard ext-pdo instance or an instance of a class extending it (such as the lazy-pdo in this package) in the constructor:
$pdo = new pine3ree\PDO\Profiling\PDO(new \PDO( $dsn = 'sqlite:my-db.sqlite3', $username = '', $password = '', $options = [] ));
You can retrieve the recorded information by calling the pine3ree\PDO\Profiling\PDO::getLog() method.
How to use the auto-reconnecting/connection-expiration instance
Use the provided reconnecting-pdo class with an extra $ttl constructor argument:
$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 );
Additional methods
pine3ree\PDO::execute(): \PDOStatement|false
pine3ree\PDO::execute(string $statement, array $input_parameters = [], array $driver_options = [])
combines \PDO::prepare() and \PDOStatement::execute() into one method call,
returning false if either the statement preparation or execution fails.
This method is inherited by pine3ree\PDO\Reconnecting\PDO.
pine3ree\PDO::isConnected(): bool
checks if we have an established database connection.
This method is inherited by pine3ree\PDO\Reconnecting\PDO and also implemented in pine3ree\PDO\Profiling\PDO.
pine3ree\PDO\Profiling\PDO::getLog(): array
returns recorded profiling information about all the executed statements in the following format:
[
// 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
returns the number of database connections performed so far
pine3ree/pine3ree-pdo 适用场景与选型建议
pine3ree/pine3ree-pdo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 102 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「pdo」 「lazy-pdo」 「pine3ree」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pine3ree/pine3ree-pdo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pine3ree/pine3ree-pdo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pine3ree/pine3ree-pdo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
PHP module for MySql database
VV database abstraction layer with query builder and DB structure models
统计信息
- 总下载量: 102
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-10-13