runopencode/query-resources-loader-bundle
Composer 安装命令:
composer require runopencode/query-resources-loader-bundle
包简介
Provides you with possibility to keep long queries in separate files in 'query' directory in your project.
README 文档
README
The purpose of query resources loader is to help you manage and organize your big, long, database queries, especially in application that deals with reporting.
Features:
- Store your queries in separate,
*.sqlfiles (or*sql.twigfiles), in your project directory or any other directory that you want to use. - Load or execute your queries using
RunOpenCode\Bundle\QueryResourcesLoader\Contract\QueryResourcesLoaderInterfaceservice. - Full compatibility with Doctrine Dbal. You can move your current queries within repository classes to separate SQL
files and use query loader to execute them. Result of execution is instance of
Doctrine\DBAL\Driver\Result. Of course, there are neat methods which you can utilize to fetch data from result set, such asgetSingleScalarResult(),getSingleResult(),getScalarResult(), etc... SeeRunOpenCode\Bundle\QueryResourcesLoader\Executor\Dbal\DoctrineDbalExecutionResultclass for more details. - Automatically registers
%kernel.project_dir%/querydirectory as query resources directory, as well as allquerydirectories withinResourcesdirectories of your bundles. - Integrated with Twig, so you can use Twig syntax in your queries. You can use this feature to build complex
queries, depending on your application logic. Beside control flow statements, you can use all Twig filters, functions,
tests and blocks as well. With
{% include %},{% embed %},{% use %}and{% extends %}statements, you can reuse your queries and build complex queries from smaller ones. - Transactions. You can execute your queries within transaction. Supports
transactional()API from Doctrine Dbal. You can control transaction isolation level for current statements within transaction. - Distributed transactions. You can execute multiple queries within same transaction against different databases. If any of statements fail, transaction will be rolled back for all databases.
- Caching. You can cache your query results, so they are not loaded from database on each execution.
- Middlewares. You can use middlewares to manipulate query before execution, or to manipulate result after execution. You can switch to other database if query fails, you can add monitoring, logging, load balancing on several databases, etc...
Read the documentation here.
Quick example
Typical reporting repository that has a query string within repository can be implemented like as in example below:
declare(strict_types=1); namespace App\Repository; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Types\Types; final readonly class MyReportingRepository { public function __construct(private Connection $connection) { } public function getInvoicingReport(\DateTimeInterface $from): iterable { $sql = 'SELECT field_1.T as f1, field_2.T as f2, ... field_57.X as f57, ... field_n.N as fn FROM table_name T INNER JOIN table_name_2 T2 ON (T.id = T2.t1_id) INNER JOIN table_name_3 T3 ON (T2.id = T3.t2_id) .... [More joins] WHERE T.create_at >= :from [A lot of where statements and so on...] '; return $this->connection->execute($sql, [ 'from' => $from ], [ 'from' => Types::DATE_IMMUTABLE ]); } }
This is terrible, as it mixes SQL with PHP code, and it is hard to maintain!
With this bundle, you can store your queries in %kernel.project_dir%/query directory as standard .sql file (or
.sql.twig if you use Twig, or any other extension that your query language uses) and load it and execute it using
RunOpenCode\Bundle\QueryResourcesLoader\Contract\QueryResourcesLoaderInterface service, thus, decreasing amount of
code in your repository classes:
declare(strict_types=1); namespace App\Repository; use RunOpenCode\Bundle\QueryResourcesLoader\Contract\QueryResourcesLoaderInterface; use RunOpenCode\Bundle\QueryResourcesLoader\Executor\Dbal\DbalParameters; final readonly class MyReportingRepository { public function __construct(private QueryResourcesLoaderInterface $loader) { } public function getInvoicingReport(\DateTimeInterface $from): iterable { return $this->loader->execute('invoicing_report.sql', DbalParameters::create()->dateTimeImmutable('from', $from)); } }
Building complex queries
Sometimes, you will need a possibility to build up your queries depending on your application logic. For that purpose, query loader uses Twig and all your query resources are pre-parsed with Twig, allowing you to dynamically build your queries, per example:
# file: my_query.sql.twig SELECT * FROM my_table T WHERE T.field_1 = :some_parameter {% if some_other_parameter is defined %} AND T.field_2 = :some_other_parameter {% endif %}
For other details about this bundle, as well as for tips on how to use it, read the documentation here.
TODO
- Add profiling for middlewares and query execution.
runopencode/query-resources-loader-bundle 适用场景与选型建议
runopencode/query-resources-loader-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.29k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2016 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sql」 「query」 「sql loader」 「query loader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 runopencode/query-resources-loader-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 runopencode/query-resources-loader-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 runopencode/query-resources-loader-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony2 bundle that provides features to load assets
Query filtering in your frontend
Anax Database Active Record module for model classes.
An environment variable convenience library extension for vlucas/phpdotenv
PhpStratum SQLite: A stored procedure loader and wrapper generator for SQLite
Zend Framework 1 Loader package
统计信息
- 总下载量: 7.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-14