cocur/nqm 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

cocur/nqm

Composer 安装命令:

composer require cocur/nqm

包简介

Named Query Manager helps you SQL organise queries in files.

README 文档

README

Named Query Manager (NQM) helps you organise SQL queries in files.

Build Status Code Coverage

Features

  • Wrapper for PDO
  • Stores queries in the filesystem
  • Caches queries in arrays or using APC
  • Compatible with PHP >= 5.4 and HHVM

Installation

You can install cocur/nqm using Composer:

$ composer require cocur/nqm:@stable

Tip: Use a concrete version instead of @stable.

Usage

In order to use NQM you need to initialise the Cocur\NQM\NQM class with an instance of \PDO and a query loader. NQM comes with a FilesystemQueryLoader query loader.

use Cocur\NQM\NQM;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$pdo = new \PDO(...);
$nqm = new NQM($pdo, $loader);

After you have initialised the NQM object you can use it. Currently the class has three public methods to retrieve a query, prepare a statement or execute a statement.

The following command will return the SQL query stored in ./queries/find-all-users.sql.

$nqm->getQuery('find-all-users');

NQM can also return a \PDOStatement object directly:

$stmt = $nqm->prepare('find-all-users');
$stmt->execute();

Or you can immediately execute the statement:

$stmt = $nqm->execute('find-user-by-id', [':id' => 42]);

Query Cache

To speed up loading of queries you can use the Cocur\NQM\QueryLoader\CacheQueryLoader to cache queries. The cache class implements the same interface as the other query loaders and the constructor accepts an instance of QueryLoaderInterface. If a query does not exist in the cache, the cache uses this loader to load the query. For example,

use Cocur\NQM\QueryLoader\CacheQueryLoader;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$cache = new CacheQueryLoader($loader);

$pdo = new \PDO(...);
$nqm = new NQM($pdo, $cache);

APC Query Cache

The CacheQueryLoader query loader stores cached queries in an array and therefore only on a per-request basis. While this often suffices in CLI applications for web apps it would be better to cache queries over multiple requests.

use Cocur\NQM\QueryLoader\ApcQueryLoader;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$apc = new ApcQueryLoader($loader);

$pdo = new \PDO(...);
$nqm = new NQM($pdo, $apc);

Additionally if you have queries that you use more than once in a single request you can stack multiple query loaders. In the following example NQM will load queries from the array cache or if it's not cached it will look in the APC cache. As a last resort NQM loads the query from the filesystem.

use Cocur\NQM\QueryLoader\ApcQueryLoader;
use Cocur\NQM\QueryLoader\CacheQueryLoader;
use Cocur\NQM\QueryLoader\FilesystemQueryLoader;

$loader = new FilesystemQueryLoader(__DIR__.'/queries');
$apc = new ApcQueryLoader($loader);
$cache = new CacheQueryLoader($apc);

$pdo = new \PDO(...);
$nqm = new NQM($pdo, $cache);

Array Query Loader

Stores the queries in an array.

use Cocur\NQM\QueryLoader\ArrayQueryLoader;

$loader = new ArrayQueryLoader(['foo' => 'SELECT ...;']);

Query Collection

Sometimes you have multiple queries that are always executed together. For example, a DROP TABLE, CREATE TABLE sequence or if you have to create temporary tables for especially complex queries. Since PDO accepts only a single SQL statement per statement, you can use QueryCollection to execute multiple queries. Queries must be separated by #;, which must be placed on its own line.

DROP TABLE IF EXISTS users;
#;
CREATE TABLE users (...);
use Cocur\NQM\QueryCollection;

$collection = NQMQueryCollection($nqm);
// Just prepare the statements
$statements = $collection->prepare('drop-and-create-user-table');

// Prepare and execute the statements
$statements = $collection->execute('drop-and-create-user-table', ['foo'=>'bar']);

The prepare() and execute() methods return both a Cocur\NQM\StatementCollection. This collection class implements \ArrayAccess and \Countable.

$statements->all(); // -> \PDOStatement[]
$statements->first(); // -> \PDOStatement
$statements->last(); // -> \PDOStatement

Doctrine Bridge

If you don't have a PDO object, but a Doctrine EntityManager object you can use the Doctrine bridge to create a new NQM object.

use Cocur\NQM\Bridge\Doctrine\NQMFactory;

$nqm = NQMFactory::createFromEntityManager($entityManager, $queryLoader);
// NQM(...) object

Change log

Version 0.4 (6 October 2015)

  • Add ArrayQueryLoader

Version 0.3 (16 February 2015)

  • Add support for query collections

Version 0.2 (11 February 2015)

  • Add Doctrine bridge

Version 0.1.2 (3 February 2015)

  • Moved development packages to require-dev in composer.json

Version 0.1.1 (26 August 2014)

  • Renamed query loader class names

Version 0.1 (28 May 2014)

  • Initial release

Author

Florian Eckerstorfer Support Florian

License

The MIT license applies to cocur/nqm. For the full copyright and license information, please view the LICENSE file distributed with this source code.

cocur/nqm 适用场景与选型建议

cocur/nqm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.59k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2015 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-03