zjkiza/sql-blade
Composer 安装命令:
composer require zjkiza/sql-blade
包简介
The packages executes raw SQL queries with the flexibility to embed Blade extensions, enabling the dynamic creation of queries using Blade syntax.
README 文档
README
The packages execute raw SQL queries which are in a separate file with the flexibility to embed Blade extensions, enabling the dynamic creation of queries using Blade syntax.
About the package
- You can use Blade syntax when creating queries.
- You place queries in separate files. (Ex:
select_user.blade.sql). - Execute your queries using
Zjk\SqlBlade\Contract\SqlBladeInterfaceservice. - Result of execution
Zjk\SqlBlade\Contract\SqlBladeInterface->executeQuery(..)is instance of Doctrine\DBAL\Driver\Result, use their methods to get results. - Query execution via transaction
Zjk\SqlBlade\Contract\SqlBladeInterface->transaction(..)
Installation
Add zjkiza/sql-blade to your composer.json file:
composer require zjkiza/sql-blade
A quick example
Working with the bundle
It is necessary to define which directory/directories will be used for storing files with sql queries.
In file config/view.php add paths
Example:
'paths' => [
...
app_path('Sql/User'),
..
],
Create a sql query (to the directory defined above). Example (select_user.blade.sql):
# file select_user.blade.sql SELECT u.id, u.email FROM users u WHERE 1 @isset($emails) AND u.email IN ( :emails ) @endif @isset($ids) AND u.id IN ( :ids ) @endif ORDER BY @isset($ids) u.id @else u.email @endisset ;
Notice: At the end of the query, you must put ;
Working in php, Example:
namespace App\Example; use Zjk\SqlBlade\Contract\SqlBladeInterface; use Doctrine\DBAL\Result; class MyRepository { private SqlTwigInterface $sqlBlade; public function __construct(SqlBladeInterface $sqlBlade) { $this->sqlBlade = $sqlBlade; } public function users(): array { return $this->sqlBlade->executeQuery('select_user', [ 'emails' => ['foo@example.com', 'bar@example.com'] ],[ 'emails' => ArrayParameterType::STRING, ])->fetchAllAssociative() } public function withTransaction(): array { $result = $this->sqlBlade->transaction(function (SqlBladeInterface $sqlBlade): Result { return $sqlBlade->executeQuery('select_without_blade'); }, TransactionIsolationLevel::READ_UNCOMMITTED); return $result->fetchAllAssociative(); } }
Crafting intricate and sophisticated queries.
At times, it's essential to have the flexibility to construct your queries in accordance with the logic of your application. This is where the sqlBlade comes into play, leveraging Blade to pre-parse all your query resources. This enables you to dynamically shape your queries as needed.
Query logging
Executed queries are logged in laravel.log file. In which there are:
- file name,
- sql query that was executed,
- arguments,
- argument types,
- query execution time in sec.
zjkiza/sql-blade 适用场景与选型建议
zjkiza/sql-blade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zjkiza/sql-blade 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zjkiza/sql-blade 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-14