phossa/phossa-db
Composer 安装命令:
composer require phossa/phossa-db
包简介
A PHP db libraray
README 文档
README
Introduction
phossa-db is a PHP db connection management library which handles the interaction with db.
It requires PHP 5.4 and supports PHP 7.0+, HHVM. It is compliant with PSR-1, PSR-2, PSR-4.
Features
-
Simple interface. Nothing you don't need.
-
Multiple db platform/driver support, currently PDO (all PDO drivers) and Mysqli.
-
Handles multiple connections through driver manager
-
Round-robin load balancing
Multiple db connections are used in round-robin fashion and weighting factor (1-10) supported. Each connection is monitored (pinged).
-
driver tagging, so user can tag different db connection as 'reader' or 'writer'
-
-
Easy profiling, get each executed sql and its execution time.
-
Secure. All SQL executed through prepare/execute in low-level drivers.
Getting started
-
Installation
Install via the
composerutility.composer require "phossa/phossa-db=1.*"or add the following lines to your
composer.json{ "require": { "phossa/phossa-db": "1.*" } }
Usage
-
Driver
-
DDL using execute()
$db = new Phossa\Db\Pdo\Driver([ 'dsn' => 'mysql:dbname=test;host=127.0.0.1;charset=utf8' ]); // simple delete $res = $db->execute("DELETE FROM test WHERE id < 10"); if (false === $res) { echo $db->getError() . \PHP_EOL; } else { echo sprintf("Deleted %d records", $res) . \PHP_EOL; } // with parameters $res = $db->execute("INSERT INTO test (name) VALUES (?)", [ 100 ]); if ($res) { $id = (int) $db->getLastInsertId(); }
-
SELECT using query()
// simple select $res = $db->query("SELECT * FROM test WHERE id < 10"); if (false === $res) { echo $db->getError() . \PHP_EOL; } else { $rows = $res->fetchAll(); } // with parameters & fetch first 5 rows $res = $db->query("SELECT * FROM test WHERE id > ? LIMIT ?", [10, 20]); if ($res && $res->isQuery()) { $firstFiveRows = $res->fetchRow(5); } // fetch first field $res = $db->query("SELECT id, name FROM test WHERE id < :id", ['id' => 10]); if ($res && $res->isQuery()) { $firstCols = $res->fetchCol('id'); }
-
-
Statment
Statementis returned by$db->prepare().// PREPARE using prepare() $stmt = $db->prepare("SELECT * FROM test WHERE id < :id"); if (false === $stmt) { echo $db->getError() . \PHP_EOL; } else { $res = $stmt->execute(['id' => 10]); if (false === $res) { echo $db->getError() . \PHP_EOL; } else { $rows = $res->fetchAll(); } }
-
Result
Resultis returned by$db->execute(),$db->query()or$stmt->execute()$res = $db->query(...); if ($res) { // SELECT if ($res->isQuery()) { // get fields count $fieldCount = $res->fieldCount(); // row count $rowCount = $res->rowCount(); // DDL } else { $affectedRows = $res->affectedRows(); } }
Driver manager
Driver manager manages multiple db connections. Weighting factor N means add
one driver virtually N times. Adding driver A with factor 5 and adding driver
B with factor 1 into the pool, means when calling getDriver(), user will
get A five times vs B for one time.
// writable connect 1
$db1 = (new Phossa\Db\Pdo\Driver($conf1))->addTag('RW');
// dbreader 2
$db2 = (new Phossa\Db\Pdo\Driver($conf2))->addTag('RO');
// dbreader 3
$db3 = (new Phossa\Db\Pdo\Driver($conf3))->addTag('RO');
// db manager
$dbm = (new Phossa\Db\Manager\Manager())
->addDriver($db1, 1) // writable connection with factor 1
->addDriver($db2, 5) // read_only, factor 5
->addDriver($db3, 5) // read_only, factor 5
// get a db connect, no matter writable or read only
$db = $dbm->getDriver();
// get a readonly driver
$db = $dbm->getDriver('RO');
SQL profiling
Get the executed SQL and its execution time.
// init driver $db = new Phossa\Db\Pdo\Driver($conf); // enable profiling $db->enableProfiling(); // execute a DELETE $db->execute("DELETE FROM test WHERE test_id > 10"); // get sql $sql = $db->getProfiler()->getSql(); $time = $db->getProfiler()->getExecutionTime();
Dependencies
-
PHP >= 5.4.0
-
phossa/phossa-shared ~1.0.10
License
phossa/phossa-db 适用场景与选型建议
phossa/phossa-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「db」 「phossa」 「rdbm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phossa/phossa-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phossa/phossa-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phossa/phossa-db 相关的其它包
同方向 / 同关键字的高下载量 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
A PHP lib to generate sequential/time based uuid for using as PK in DB.
PHP module for MySql database
统计信息
- 总下载量: 33
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-15