承接 colshrapnel/safemysql 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

colshrapnel/safemysql

Composer 安装命令:

composer require colshrapnel/safemysql

包简介

A real safe and convenient way to handle MySQL queries.

关键字:

README 文档

README

SafeMySQL is a PHP class for safe and convenient handling of MySQL queries.

  • Safe because every dynamic query part goes into the query via placeholder
  • Convenient because it makes application code short and meaningful, without useless repetitions, making it ''extra'' DRY

This class is distinguished by three main features

  • Unlike standard libraries, it is using type-hinted placeholders, for the everything that may be put into the query
  • Unlike standard libraries, it requires no repetitive binding, fetching and such, thanks to set of helper methods to get the desired result right out of the query
  • Unlike standard libraries, it can parse placeholders not in the whole query only, but in the arbitary query part, thanks to the indispensabe parse() method, making complex queries as easy and safe as regular ones.

Yet, it is very easy to use. You need to learn only a few things:

  1. You have to always pass whatever dynamical data into the query via placeholder
  2. Each placeholder have to be marked with data type. At the moment there are six types:
  • ?s ("string") - strings (also DATE, FLOAT and DECIMAL)
  • ?i ("integer") - the name says it all
  • ?n ("name") - identifiers (table and field names)
  • ?a ("array") - complex placeholder for IN() operator (substituted with string of 'a','b','c' format, without parentesis)
  • ?u ("update") - complex placeholder for SET operator (substituted with string of field='value',field='value' format)
  • ?p ("parsed") - special type placeholder, for inserting already parsed statements without any processing, to avoid double parsing.
  1. To get data right out of the query there are helper methods for the most used:
  • query($query,$param1,$param2, ...) - returns mysqli resource.
  • getOne($query,$param1,$param2, ...) - returns scalar value
  • getRow($query,$param1,$param2, ...) - returns 1-dimensional array, a row
  • getCol($query,$param1,$param2, ...) - returns 1-dimensional array, a column
  • getAll($query,$param1,$param2, ...) - returns 2-dimensional array, an array of rows
  • getInd($key,$query,$par1,$par2, ...) - returns an indexed 2-dimensional array, an array of rows
  • getIndCol($key,$query,$par1,$par2, ...) - returns 1-dimensional array, an indexed column, consists of key => value pairs
  1. For the whatever complex case always use the parse() method. And insert

The rest is as usual - just create a regular SQL (with placeholders) and get a result:

  • $name = $db->getOne('SELECT name FROM table WHERE id = ?i',$_GET['id']);
  • $data = $db->getInd('id','SELECT * FROM ?n WHERE id IN (?a)','table', array(1,2));
  • $data = $db->getAll("SELECT * FROM ?n WHERE mod=?s LIMIT ?i",$table,$mod,$limit);

The main feature of this class is a type-hinted placeholders. And it's a really great step further from just ordinal placeholders used in prepared statements. Simply because dynamical parts of the query aren't limited to just scalar data! In the real life we have to add identifiers, arrays for IN operator, and arrays for INSERT and UPDATE queries. So - we need many different types of data formatting. Thus, we need the way to tell the driver how to format this particular data. Conventional prepared statements use toilsome and repeating bind_* functions. But there is a way more sleek and useful way - to set the type along with placeholder itself. It is not something new - well-known printf() function uses exactly the same mechanism. So, I hesitated not to borrow such a brilliant idea.

To implement such a feature, no doubt one have to have their own query parser. No problem, it's not a big deal. But the benefits are innumerable. Look at all the questions on Stack Overflow where developers are trying in vain to bind a field name. Voila - with the identifier placeholder it is as easy as adding a field value:

$field = $_POST['field'];
$value = $_POST['value'];
$sql   = "SELECT * FROM table WHERE ?n LIKE ?s";
$data  = $db->query($sql,$field,"%$value%");

Nothing could be easier!

Of course we will have placeholders for the common types - strings and numbers. But as we started inventing new placeholders - let's make some more!

Another trouble in creating prepared queries - arrays going to the IN operator. Everyone is trying to do it their own way, but the type-hinted placeholder makes it as simple as adding a string:

$array = array(1,2,3);
$data  = $db->query("SELECT * FROM table WHERE id IN (?a)",$array);

The same goes for such toilsome queries like INSERT and UPDATE.

And, of course, we have a set of helper functions to turn type-hinted placeholders into real brilliant, making almost every call to the database as simple as one or two lines of code for all the regular real life tasks.

colshrapnel/safemysql 适用场景与选型建议

colshrapnel/safemysql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 109.88k 次下载、GitHub Stars 达 398, 最近一次更新时间为 2013 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 colshrapnel/safemysql 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 109.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 400
  • 点击次数: 19
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 398
  • Watchers: 55
  • Forks: 192
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2013-11-06