philiagus/pdostatementbuilder
Composer 安装命令:
composer require philiagus/pdostatementbuilder
包简介
PHP classes to easily build PDO statements
README 文档
README
PHP classes to easily build PDO statements.
For all update information please refer to UPDATE.md.
What is it?
A simple way of building complex PDOStatements in a readable way. Want to build an overly complex filter SQL for your project? This is the project to use!
Is it covered?
- 100% Test covered
- Tested in
- PHP8.1
- PHP8.2
- PHP8.3
- PHP8.4
Why do all this?
It is very common in database related coding to build statements based on a set of input parameters provided to a repository. This usually looks something like this:
class SomeRepository { /** * @type \PDO */ private $pdo; function getSomething(array $ids, bool $someFilter = false, ?int $exlude = null): array { if (empty($ids)) { return []; } $statement = "SELECT * FROM `table` WHERE id IN (?" . str_repeat(',?', count($ids) - 1) . ")"; $params = array_values($ids); if ($someFilter) { $statement .= " AND `filter_field` IS NOT NULL AND `filter_field2` = 1"; } if ($exlude !== null) { $statement .= ' AND `explude_column` = ?'; $params[] = $exlude; } $statement = $this->pdo->prepare($statement); $statement->execute($params); return $statement->fetchAll(\PDO::FETCH_ASSOC); } }
Compare this to the following code:
class SomeRepository { /** * @type \PDO */ private $pdo; function getSomething(array $ids, bool $someFilter = false, ?int $exclude = null): array { if (empty($ids)) { return []; } $builder = new \Philiagus\PDOStatementBuilder\Builder(); $sql = $builder->build(" SELECT * FROM table WHERE id IN ({$builder->in($ids)}) /* {$builder->if($someFilter)} */ AND `filter_field` IS NOT NULL AND `filter_field2` = 1 /* {builder->endif()} */ /* {builder->if($exclude !== null)} */ AND `explude_column` = {$builder->value($exclude)} /* {$builder->endif()} */ " ); $statement = $sql->prepare($this->pdo); $statement->execute(); return $statement->fetchAll(\PDO::FETCH_ASSOC); } }
The code does essentially the same, but the SQL is readable. Additionally, an IDE such as PHPStorm can help you with auto-completion of the SQL.
Simple Statement building
The Builder also supports a static method to simply build Statements.
class SomeRepository { /** * @type \PDO */ private $pdo; function getSomething(array $ids, bool $someFilter = false, ?int $exclude = null): array { if (empty($ids)) { return []; } $builder = new \Philiagus\PDOStatementBuilder\Builder(); $sql = Builder::simple( "SELECT * FROM `table` WHERE `id` = :id", [':id' => 1] ); $statement = $sql->prepare($this->pdo); $statement->execute(); return $statement->fetchAll(\PDO::FETCH_ASSOC); } }
The Builder::simple method does not only take named parameters (:id), but also numeric parameters (for binding with ? in PDO). You can also simply provide an Parameter object (new Parameter(<name>, <value>, <type>)) if you want in-place control of the parameter type.
philiagus/pdostatementbuilder 适用场景与选型建议
philiagus/pdostatementbuilder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.7k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「parse」 「sanitize」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 philiagus/pdostatementbuilder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philiagus/pdostatementbuilder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 philiagus/pdostatementbuilder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
yii2 xml request parser
Sanitizer helper
Parse promotion arrays from the Wayne State University API
Runn Me! Validation and Sanitization Library
A data transforming/formatting package for php.
OriginPHP Html
统计信息
- 总下载量: 7.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-08