makinacorpus/query-builder
Composer 安装命令:
composer require makinacorpus/query-builder
包简介
Driver agnostic SQL query builder.
README 文档
README
This is a driver-agnostic SQL query builder with advanced SQL language features.
Per default, it generates (almost) SQL standard compliant queries, almost because some SQL features are simply not implemented by any dialect we support case in which we implement a custom variant.
It is based upon a fluent API, which makes basic SELECT, DELETE, INSERT, UPDATE and MERGE queries easy to write, even if you don't really now SQL.
At any place the user can inject raw SQL in order to escape the query builder own limitations.
This API by itself is only a SQL string generator, in order to use it transparently, a few bridges are provided:
doctrine/dbal:^3bridge and query builder,- soon a
doctrine/dbal:^4bridge and query builder, PDObridge and query builder,- Symfony bundle integrating with
doctrine/doctrine-bundleis available in the makinacorpus/query-builder-bundle package.
Getting started
Read the documentation on read the docs.
Overview list of SQL supported features.
See the officially supported vendor dialects.
Non-exhaustive feature list
- Common Table Expressions:
WITH "foo" AS (SELECT ...)
- VALUES / Constant table expression:
WITH "foo" ("col1", "col2") AS (SELECT VALUES ...)SELECT VALUES ... AS ("col1", "col2")JOIN (VALUES ...) AS "foo", ...
- Aggregate function filter:
- Standard SQL:
SELECT SUM(foo) FILTER (WHERE <condition>) - Emulation for non supporting RDBMS:
SELECT SUM(CASE WHEN <condition> THEN foo END)
- Standard SQL:
- Window functions:
SELECT SUM(foo) OVER (PARTITION BY... ORDER BY...)SELECT SUM(foo) OVER (bar) FROM baz WINDOW bar AS (PARTITION BY... ORDER BY...)
- RETURNING/OUTPUT:
- PostgreSQL:
UPDATE foo SET ... RETURNING foo, bar, baz - SQL Server:
UPDATE foo SET ... OUTPUT DELETED.col1, INSERTED.col1
- PostgreSQL:
- Complex WHERE condition builder
- CASE WHEN ... THEN ... END
- IF THEN emulated using CASE ... WHEN
- ARRAY expression:
ARRAY[? ,? ?]
- ROW expressions:
- Standard SQL:
ROW('foo', 2, ...) - PostgreSQL composite types:
ROW('foo', 2, ...)::some_composite_type
- Standard SQL:
- Mixing complex data structures:
CAST(ROW('foo', CAST(ARRAY[1,2,3] AS bigint[])) AS some_composite_type)
- CAST expressions:
- Standard SQL:
CAST(expression AS type) - PostgreSQL:
expression::type
- Standard SQL:
- LIKE / SIMILAR TO
foo LIKE '%somevalue%'foo SIMILAR TO '%somevalue%'
- Update FROM JOIN
- INSERT INTO ... VALUES ...
- INSERT INTO ... SELECT ...
- Arbitrary RAW SQL from user everywhere
- The builder doesn't support some X or Y advanced feature or specific dialect, then write your own SQL.
- Identifier escaping everywgere
- User value conversion to SQL
Building documentation
Documentation is written using VitePress.
For readers:
cd ./docs/ nvm use npm ci npm run docs:build <your browser> ./.vitepress/dist/index.html
For developers:
cd ./docs/
nvm use
npm ci
npm run docs:dev
Run tests
Most unit tests will work gracefuly by running directly PHPUnit:
composer install vendor/bin/phpunit
For running bridges integration tests, it needs a complete environment with
some databases up and running, for this purpose a script that uses
docker compose is provided, yet it might not work gracefuly in all
environments:
composer install ./run-tests.sh
Please understand that the testing environment is currently at the prototype stage, and will remain as-is until github actions CI is configured.
History
This API is an almost complete rewrite, keeping the base design, using modern
PHP >= 8.2 features and much more unit tested of the query builder from the
makinacorpus/goat-query package.
Rationale is that we deeply used makinacorpus/goat-query as our primary
database access layer for some years, but we are shifting toward using more
commonly used tooling for future projects.
Doctrine DBAL query builder can do everything you would want to do, but its API is unintuitive and written code using is hard to read. Moreover, it doesn't implement modern SQL language features since its goal is mostly to provide lowest common denominator for a huge number of RDBMS.
makinacorpus/query-builder 适用场景与选型建议
makinacorpus/query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 205.4k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「sql」 「mysql」 「postgresql」 「query-builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 makinacorpus/query-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 makinacorpus/query-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 makinacorpus/query-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
The bundle for easy using json-rpc api on your project
Query filtering in your frontend
Database/ORM-agnostic query construction helper
Bundle Symfony DaplosBundle
Laravel SQL Queries Dumper
统计信息
- 总下载量: 205.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 13
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-16