sad_spirit/pg_builder
Composer 安装命令:
composer require sad_spirit/pg_builder
包简介
Query builder for Postgres backed by SQL parser
README 文档
README
This is a query builder for Postgres with a twist: it contains a partial1 reimplementation of PostgreSQL's own query parser. This sets it aside from the usual breed of "write-only" query builders:
- Query is represented as an Abstract Syntax Tree quite similar to PostgreSQL's internal representation.
- Query parts can be added to the AST either as objects or as strings (that will be processed by Parser).
- Nodes can be removed and replaced in AST.
- AST can be analyzed and transformed, the package takes advantage of this to allow named parameters like
:fooinstead of standard PostgreSQL's positional parameters$1and to infer parameters' types from SQL typecasts. - Almost all syntax available for
SELECT(andVALUES) /INSERT/UPDATE/DELETE/MERGEin PostgreSQL 18 is supported, query being built is automatically checked for correct syntax.
Substantial effort was made to optimise parsing, but not parsing is faster anyway, so there are means to cache parts of AST and the resultant query.
Usage example
use sad_spirit\pg_builder\{ Select, StatementFactory, converters\BuilderSupportDecorator }; use sad_spirit\pg_wrapper\{ Connection, converters\DefaultTypeConverterFactory }; $wantPDO = false; if ($wantPDO) { $pdo = new \PDO('pgsql:host=localhost;user=username;dbname=cms'); // Uses DB connection properties to set up parsing and building of SQL $factory = StatementFactory::forPDO($pdo); // NB: This still requires sad_spirit/pg_wrapper for type conversion code $converter = new BuilderSupportDecorator(new DefaultTypeConverterFactory(), $factory->getParser()); } else { $connection = new Connection('host=localhost user=username dbname=cms'); // Uses DB connection properties to set up parsing and building of SQL $factory = StatementFactory::forConnection($connection); // Needed for handling type info extracted from query $connection->setTypeConverterFactory(new BuilderSupportDecorator( $connection->getTypeConverterFactory(), $factory->getParser() )); } // latest 5 news /** @var Select $query */ $query = $factory->createFromString( 'select n.* from news as n order by news_added desc limit 5' ); // we also need pictures for these... $query->list[] = 'p.*'; $query->from[0]->leftJoin('pictures as p')->on = 'n.picture_id = p.picture_id'; // ...and need to limit them to only specific rubrics $query->from[] = 'objects_rubrics as ro'; $query->where->and('ro.rubric_id = any(:rubric::integer[]) and ro.obj_id = n.news_id'); // ...and keep 'em fresh $query->where->and('age(news_added) < :age::interval'); // $generated contains a query, mapping from named parameters to positional ones, types info // it can be easily cached to prevent parsing/building SQL on each request $generated = $factory->createFromAST($query); // Note that we don't have to specify parameter types, these are extracted from query if ($wantPDO) { $result = $pdo->prepare($generated->getSql()); $result->execute($converter->convertParameters( $generated, [ 'rubric' => [19, 20, 21], 'age' => 30 * 24 * 3600 ] )); } else { $result = $generated->executeParams( $connection, [ 'rubric' => [19, 20, 21], 'age' => 30 * 24 * 3600 ] ); } foreach ($result as $row) { print_r($row); } echo $generated->getSql();
the last echo statement will output something like
select n.*, p.* from news as n left join pictures as p on n.picture_id = p.picture_id, objects_rubrics as ro where ro.rubric_id = any($1::pg_catalog.int4[]) and ro.obj_id = n.news_id and age(news_added) < $2::interval order by news_added desc limit 5
if targeting Connection and something like
select n.*, p.* from news as n left join pictures as p on n.picture_id = p.picture_id, objects_rubrics as ro where ro.rubric_id = any(:rubric::pg_catalog.int4[]) and ro.obj_id = n.news_id and age(news_added) < :age::interval order by news_added desc limit 5
if targeting PDO
Installation
Require the package with composer:
composer require "sad_spirit/pg_builder:^3"
pg_builder requires at least PHP 8.2. Either native pgsql extension with pg_wrapper package or PDO with pgsql support can be used to run the built queries.
Minimum supported PostgreSQL version is 12.
It is highly recommended to use PSR-6 compatible cache in production.
Documentation
For in-depth description of package features, visit pg_builder manual.
1: "Partial" here means the following: PostgreSQL grammar file src/backend/parser/gram.y is about 19K lines long.
Of these about 5K lines are used for SELECT / INSERT / UPDATE / DELETE / MERGE queries and are reimplemented here.
sad_spirit/pg_builder 适用场景与选型建议
sad_spirit/pg_builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.64k 次下载、GitHub Stars 达 59, 最近一次更新时间为 2014 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sql」 「parser」 「postgresql」 「query」 「postgres」 「builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sad_spirit/pg_builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sad_spirit/pg_builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sad_spirit/pg_builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Query filtering in your frontend
An MT940 bank statement parser for PHP
Sql dumps containing databases for the linna apps.
统计信息
- 总下载量: 40.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 59
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2014-09-20