定制 mouf/database.querywriter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mouf/database.querywriter

Composer 安装命令:

composer require mouf/database.querywriter

包简介

This package contains classes useful to generate SQL statements such as SELECT queries, etc...

README 文档

README

QueryWritter is a PHP library that parses SQL queries, transforms those into an object representation, stores them in a dependency injection container, and returns them as string. It is a Mouf plugin.

It is heavily based on mouf/magic-query. Actually, magic-query's code was extracted from query-writer.

Ok, but why would I use QueryWritter?

Because it is the most effecient way to deal with queries that can have a variable number of parameters! Think about a typical datagrid with a bunch of filter (for instance a list of products filtered by name, company, price, ...). If you have the very common idea to generate the SQL query using no PHP library, your code will look like this:

You should not do this!
// People usually write queries like this:
$sql = "SELECT * FROM products p JOIN companies c ON p.company_id = c.id WHERE 1=1 ";
// They keep testing for parameters, and concatenating strings....
if (isset($params['name'])) {
	$sql .= "AND p.name LIKE '".addslashes($params['name'])."%'";
}
if (isset($params['company'])) {
	$sql .= "AND c.name LIKE '".addslashes($params['company'])."%'";
}
// And so on... for each parameter, we have a "if" statement

Concatenating SQL queries is dangerous (especially if you forget to protect parameters). You can always use parameterized SQL queries, but you will still have to concatenate the filters.

To avoid concatenating strings, frameworks and libraries have used different strategies. Building a full ORM (like Doctrine or Propel) is a good idea, but it makes writing complex queries even more complex. Other frameworks like Zend are building queries using function calls. These are valid strategies, but you are no more typing SQL queries directly, and let's face it, it is always useful to use a query directly.

This is where QueryWritter becomes helpful.

How does it work?

// TODO: schema... or even better... video!

###1- Write your query You start by writing your query, in plain SQL. No ORM, no special query language (DQL or HQL anyone?), just plain and simple SQL. This is cool because everybody knows SQL. In your query, you put absolutely all the parameters you can imagine.

###2- Store your query in Mouf In Mouf UI, go to DB > SQL queries > Create SQL query. Here, you can copy and paste your query. Since this is Mouf, every query is an "instance", and you have to pick a name for your query.

Behind the scenes, QueryWritter will parse your query and make sure every piece of the query (each table, each column, each filter...) is transformed into an object. But you really don't have to care about that right now.

###3- Test your query Right from Mouf UI, you can test your query! And lo and behold! Because the query was parsed, QueryWritter will dynamically add parts of the query depending on the parameters you decide to use.

###4- Use it in your code If you are not a Mouf user (if you are using Drupal, Symfony, Zend Framework...), you can directly use the query by fetching the instance from Mouf and calling the toSql method, passing parameters in... parameter :)

$mySelect = Mouf::getMySelectStatement();
$sql = $mySelect->toSql(array("status"=>1, "search"=>"hello"));

If you are a Mouf user, you can even directly run the query using the QueryResult class that executes the query directly. Or even better, use the Evolugrid module, and display your query result in an HTML datagrid, directly!

mouf/database.querywriter 适用场景与选型建议

mouf/database.querywriter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.93k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2013 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「query」 「mouf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mouf/database.querywriter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mouf/database.querywriter 我们能提供哪些服务?
定制开发 / 二次开发

基于 mouf/database.querywriter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 30.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 7
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-01-12