mouf/magic-query
Composer 安装命令:
composer require mouf/magic-query
包简介
A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!
README 文档
README
What is Magic-query?
Magic-query is a PHP library that helps you work with complex SQL queries.
It comes with 3 great features:
- MagicParameters: it helps you work with SQL queries that require a variable number of parameters.
- MagicJoin: it writes JOINs for you!
- MagicTwig: use Twig templating in your SQL queries
Installation
Simply use the composer package:
{
"require": {
"mouf/magic-query": "^1.2"
},
"minimum-stability": "dev",
"prefer-stable": true
}
Automatically discard unused parameters with MagicParameters
Just write the query with all possible parameters.
use Mouf\Database\MagicQuery; $sql = "SELECT * FROM users WHERE name LIKE :name AND country LIKE :country"; // Get a MagicQuery object. $magicQuery = new MagicQuery(); // Let's pass only the "name" parameter $result = $magicQuery->build($sql, [ "name" => "%John%" ]); // $result = SELECT * FROM users WHERE name LIKE '%John%' // Did you notice how the bit about the country simply vanished? // Let's pass no parameter at all! $result2 = $magicQuery->build($sql, []); // $result2 = SELECT * FROM users // The whole WHERE condition disappeared because it is not needed anymore!
Curious to know how this work? Check out the parameters guide!
Automatically guess JOINs with MagicJoin!
Fed up of writing joins in SQL? Let MagicQuery do the work for you!
Seriously? Yes! All you have to do is:
- Pass a Doctrine DBAL connection to MagicQuery's constructor. MagicQuery will analyze your schema.
- In your SQL query, replace the tables with
magicjoin(start_table) - For each column of your query, use the complete name ([table_name].[column_name] instead of [column_name] alone)
Let's assume your database schema is:
Using MagicJoin, you can write this SQL query:
SELECT users.* FROM MAGICJOIN(users) WHERE groups.name = 'Admins' AND country.name='France';
and it will automatically be transformed into this:
SELECT users.* FROM users LEFT JOIN users_groups ON users.user_id = users_groups.user_id LEFT JOIN groups ON groups.group_id = users_groups.group_id LEFT JOIN country ON country.country_id = users.country_id WHERE groups.name = 'Admins' AND country.name='France';
And the code is so simple!
use Mouf\Database\MagicQuery; $sql = "SELECT users.* FROM MAGICJOIN(users) WHERE groups.name = 'Admins' AND country.name='France'"; // Get a MagicQuery object. // $conn is a Doctrine DBAL connection. $magicQuery = new MagicQuery($conn); $completeSql = $magicQuery->build($sql); // $completeSql contains the complete SQL request, with all joins.
Want to know more? Check out the MagicJoin guide!
Use Twig templating in your SQL queries!
Discarding unused parameters and auto-joining keys is not enough? You have very specific needs? Say hello to Twig integration!
Using Twig integration, you can directly add Twig conditions right into your SQL.
use Mouf\Database\MagicQuery; $sql = "SELECT users.* FROM users {% if isAdmin %} WHERE users.admin = 1 {% endif %}"; $magicQuery = new MagicQuery(); // By default, Twig integration is disabled. You need to enable it. $magicQuery->setEnableTwig(true); $completeSql = $magicQuery->build($sql, ['isAdmin' => true]); // Parameters are passed to the Twig SQL query, and the SQL query is returned.Heads up! The Twig integration cannot be used to insert parameters into the SQL query. You should use classic SQL parameters for this. This means that instead if writing
{{ id }}, you should write :id.
Want to know more? Check out the MagicTwig guide!
Is it a MySQL only tool?
No. By default, your SQL is parsed and then rewritten using the MySQL dialect, but you use any kind of dialect
known by Doctrine DBAL. Magic-query optionally uses Doctrine DBAL. You can pass a Connection object
as the first parameter of the MagicQuery constructor. Magic-query will then use the matching dialect.
For instance:
$config = new \Doctrine\DBAL\Configuration(); $connectionParams = array( 'url' => 'sqlite:///somedb.sqlite', ); $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); $magicQuery = new \Mouf\Database\MagicQuery($conn);
Also, if you have no connection to your database configured but you want to generate SQL in some specific dialect, you can instead set the DBAL database platform used:
$magicQuery->setOutputDialect(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform()); $magicQuery = new \Mouf\Database\MagicQuery();
What about performances?
MagicQuery does a lot to your query. It will parse it, render it internally as a tree of SQL nodes, etc... This processing is time consuming. So you should definitely consider using a cache system. MagicQuery is compatible with Doctrine Cache. You simply have to pass a Doctrine Cache instance has the second parameter of the constructor.
use Mouf\Database\MagicQuery; use Doctrine\Common\Cache\ApcCache; // $conn is a Doctrine connection $magicQuery = new MagicQuery($conn, new ApcCache());
Any problem?
With MagicQuery, a lot happens to your SQL query. In particular, it is parsed using a modified version of the php-sql-parser library. If you face any issues with a complex query, it is likely there is a bug in the parser. Please open an issue on Github and we'll try to fix it.
mouf/magic-query 适用场景与选型建议
mouf/magic-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 266.62k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2015 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「query」 「mouf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mouf/magic-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mouf/magic-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mouf/magic-query 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The Mouf-installer package is a composer in charge of installing any package with the 'mouf-library' type.
This package contains an interface used by many objects to say they can do stuff. Actually, they can perform one particular action and has been designed for that. The action performed is completely up to the implementer (sending a mail, storing a result in database, displaying something on the scree
Dibi is Database Abstraction Library for PHP
This package contains the History.js library, along a Mouf installer file to add History.js easily in your Mouf project.
The interfaces to implement when working with mouf/security.forgot-your-password.
The interfaces to implement when working with mouf/security.user-management-ui.
统计信息
- 总下载量: 266.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 38
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-22
