makinacorpus/goat-query
Composer 安装命令:
composer require makinacorpus/goat-query
包简介
Goat SQL query builder built over a PHP to SQL and SQL to PHP type converter
README 文档
README
This is an SQL query builder built over a PHP to SQL and SQL to PHP type converter.
Working with PDO and ext-pgsql, with officially supported drivers:
- MySQL 5.7 using
PDO, - MySQL 8.x using
PDO, - PostgreSQL >= 9.5 (until latest) using
PDO, - PostgreSQL >= 9.5 (until latest) using
ext-pgsql(recommended driver), - SQLite >= 3 using
PDO(experimental), - With a few hacks, any RDBMS speaking
SQL-92standard usingPDO.
Documentation is in the ./docs/ folder, generated using Sphinx.
Quickstart
Install it:
composer require makinacorpus/goat-query
Create a connexion:
$driver = \Goat\Driver\DriverFactory::fromUri('pgsql://username:password@hostname:5432/database?option1=value1&option2=value2');
Or create a connexion the verbose way:
$driver = new \Goat\Driver\ExtPgSQLDriver(); $driver->setConfiguration( \Goat\Driver\Configuration::fromString( 'pgsql://username:password@hostname:5432/database?option1=value1&option2=value2' ) );
Please note that options given will be treated differently depending upon driver.
Then use it:
$runner = $driver->getRunner(); $platform = $runner->getPlatform(); $queryBuilder = $runner->getQueryBuilder(); if ($platform->supportsReturning()) { $result = $queryBuilder ->insertValues('users') ->columns(['id', 'name']) ->values([1, 'Jean']) ->values([1, 'Robert']) ->returning('*') ->setOption('class', \App\Domain\Model\User::class) ->execute() ; } else { $queryBuilder ->insertValues('users') ->columns(['id', 'name']) ->values([1, 'Jean']) ->values([2, 'Robert']) ->execute() ; $result = $queryBuilder ->select('users') ->where('id', [1, 2]) ->setOption('class', \App\Domain\Model\User::class) ->execute() } foreach ($result as $user) { \assert($user instanceof \App\Domain\Model\User); echo "Hello, ", $user->getName(), " !\n"; }
For advanced documentation, please see the ./docs/ folder.
Roadmap
- 2.0 - bumps requirement to PHP 7.4,
- 2.1 - includes MERGE query support, functional testing, driver and platform segregation, as well as many fixes, and deprecated some 1.x methods,
- 3.0 - is a major overhaul of sql writer, converter context, and query builder,
- 3.0 - brings an experimental version of schema introspector and console tool,
- 3.1 - will be a features with many shortcuts and sugar candy additions,
- 4.0 - will stabilize schema introspector and console tool.
Driver organisation
Driver instance is responsible of (in order):
- connecting to the database,
- send configuration,
- inspect backend variant and version to build platform.
It gets connexion option and configures it, then creates the platform.
Platform contains SQL version-specific code, such as query formatter,
schema introspector, and other things the user cannot configure, and which may
vary depending upon the SQL server version. It handles everything the user
cannot have hands onto, but SQL server has.
Runner is the only runtime object the user needs:
- public facade for executing SQL queries,
- holds the converter (which can be injected and may contain user code),
- creates and holds the query builder,
- manages transactions.
It contains user configuration and runtime. The runner knows nothing about SQL itself, it just holds a connexion, send requests, and handles iterators and transactions.
In other words:
- drivers connects,
- platform handles SQL dialect,
- runner executes,
- a single runner implementation can use different plaform implementations, real reason why both implementations are actually separate.
Framework integration
- Symfony bundle in https://github.com/pounard/goat-query-bundle
Upgrade
Upgrade from 2.x to 3.x
-
3.x deprecated all
\Goat\Query\Expression*classes. Their backward compatible equivalent still exists, in order to make your code resilient, please use their new implementations in\Goat\Query\Expression\*Expression. -
3.x ships a complete
\Goat\Driver\Query\SqlWriterinterface and implementations rewrite. New code is faster, easier to read and has much less dependencies, driver developers or users using it directly must adapt their code. -
3.x removes the
\Goat\Query\ArgumentBag,\Goat\Query\ArgumentList,\Goat\Query\Value,\Goat\Query\ValueRepresentationclasses and interfaces, people using those must adapt their code. -
3.x changes the
\Goat\Converter\ValueConverterInterfacecontracts slightly, you need to adapt your existing custom value converters, -
3.x completely changes date handling, for most people, it should go unnoticed and fix many bugs,
-
Generally speaking, this will be the last version providing backward compatible deprecated code, following deprecation notices and the
@deprecatedPHP documentation annotaton to fix your existing code. -
For most users, upgrade will be transparent and will not cause any trouble.
Upgrade from 1.x to 2.x
-
2.x introduced a single user facing change: the Symfony bundle was originally provided by the makinacorpus/goat package, it is now bundled as the standalone makinacorpus/goat-query-bundle package.
-
2.x changed internal runners implementation and introduces a new
\Goat\Driver\namespace, which focuses on low-level driver implementations, driver developpers will need to convert their code to the new API.
This also introduce a dependency conflict between makinacorpus/goat version
prior to 3.0.0 version, if you were using it, you need to upgrade.
Query builder, database runner and result iterator end-user API did not change.
History
Originating and extracted from https://github.com/pounard/goat
makinacorpus/goat-query 适用场景与选型建议
makinacorpus/goat-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.69k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「symfony」 「mysql」 「postgresql」 「pdo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 makinacorpus/goat-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 makinacorpus/goat-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 makinacorpus/goat-query 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
The bundle for easy using json-rpc api on your project
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 11.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 9
- 推荐数: 3
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2018-11-19