blrf / dbal
ReactPHP 的异步 DBAL。
v1.0.3
2024-04-25 05:31 UTC
Requires
- php: >=8.1.0
- react/mysql: ^0.6
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11 || ^10
- react/async: ^4.2
README
ReactPHP 的异步数据库抽象层。
完整示例可在 Bookstore 仓库 中找到。Bookstore 示例使用 blrf/dbal、blrf/orm 和 framework-x 来展示当前的 DBAL/ORM 开发。
DBAL 文档可在 https://blrf.net/dbal/ 查找。
目录
示例
<?php require __DIR__ . '/vendor/autoload.php'; $config = new Blrf\Dbal\Config('mysql://user:pass@localhost/bookstore'); $config->create()->then( function (Blrf\Dbal\Connection $db) { // start query builder $qb = $db->query() ->select('*') ->from('book') ->where( fn(Blrf\Dbal\Query\ConditionBuilder $cb) => $cb->or( $cb->and( $cb->eq('isbn13'), $cb->eq('language_id'), ), $cb->eq('title') ) ) ->setParameters(['9789998691568', 1, 'Moby Dick']) ->limit(3); // $qb->getSql(): SELECT * FROM book WHERE ((isbn13 = ? AND language_id = ?) OR title = ?) LIMIT 3 return $qb->execute(); } )->then( function (Blrf\Dbal\Result $result) { print_r($result->rows); } );
流式示例
此方法返回一个可读流,该流将作为 data
事件发射结果集的每一行。它只会在内存中缓冲完整的一行数据,而不会存储整个结果集。这允许您处理那些否则无法放入内存的无限制大小的结果集。
require __DIR__ . '/../vendor/autoload.php'; $config = new Blrf\Dbal\Config('mysql://user:pass@localhost/bookstore'); $config->create()->then( function (Blrf\Dbal\Connection $db) { // start query builder $qb = $db->query() ->select('*') ->from('book') ->where( fn(Blrf\Dbal\Query\ConditionBuilder $cb) => $cb->or( $cb->and( $cb->eq('isbn13'), $cb->eq('language_id'), ), $cb->eq('title') ) ) ->setParameters(['9789998691568', 1, 'Moby Dick']) ->limit(3); // sql: SELECT * FROM book WHERE ((isbn13 = ? AND language_id = ?) OR title = ?) LIMIT 3 $stream = $qb->stream(); $stream->on('data', function (array $row) { echo " - received row: \n"; print_r($row); }); $stream->on('error', function (\Throwable $e) { echo " ! error: " . $e->getMessage() . "\n"; }); $stream->on('close', function () { echo " - Stream done\n"; }); } );
使用方法
请参阅 DBAL 文档网站。
安装
composer require blrf/dbal
测试
要运行测试套件,请转到项目根目录并运行
vendor/bin/phpunit
许可证
MIT,查看 LICENSE 文件。
待办事项
- 编写更多示例
- 编写 having
- 模式管理器