定制 romainbessugesmeusy/php-sql-query 二次开发

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

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

romainbessugesmeusy/php-sql-query

Composer 安装命令:

composer require romainbessugesmeusy/php-sql-query

包简介

php-sql-query is an abstraction layer for SQL query construction

README 文档

README

The php-sql-query package aims to provide a consistent abstraction layer for SQL query construction. It's divided in two parts:

  1. abstract query components (columns, tables, limit, groups)
  2. renderers (MySQL, PgSql, Sql Server)

Creating a select is really straightforward:

$select = new \RBM\SqlQuery\Select();
$select->setTable("project");
$select->setColumns(["project_id", "name"]);
$select->limit(0, 10);

To output the correct query according to the database system in use, you'll have to instanciate the renderer, and call its render method:

$renderer = new \RBM\SqlQuery\RendererAdapter\MySql();
echo $renderer->render($select);

This will print the following string:

SELECT
	`project`.`project_id`
	, `project`.`name`
FROM
	`project`
LIMIT 0, 10

Hopefully, you don't have to be that verbose to get the job done.

// do this once
\RBM\SqlQuery\Select::setDefaultRenderer("\RBM\SqlQuery\RendererAdapter");
// […]
$select = new \RBM\SqlQuery\Select("project", ["project_id", "name"]);
echo $select;

Filtering

In this API there's no ->where() method, instead, there's ->filter() which returns a \RBM\SqlQuery\Filter object. This object provides some basic methods :

  • equals($column, $value)
  • greaterThan($colum, $value)
  • lowerThanEquals($colum, $value)
  • isNull($colum)

There are also less basic methods such as :

  • in($column, $values)
  • between($column, $a, $b)

All of these are chainable for readability and effortless development sake.

	$select = new Select('project', ['project_id', 'name']);
	$select->filter()
		   		->equals('owner_id', 1)
		   		->isNull('date_deleted');

Of course, you can determine which operator you want, and nest clauses:

	$select->filter()->subFilter()
					->operator('OR')
					->equals('status', 'DRAFT')
					->equals('status', 'PUBLISHED');

Result:

	SELECT
		project.project_id
		, project.name
	FROM
		project	
	WHERE
		( project.owner_id = 1 )
	 AND ( project.date_deleted IS NULL )
	 AND (
	 		( project.status = 'DRAFT' )
	 		OR ( project.status = 'PUBLISHED' )
	 	)

SELECT & JOINS

A JOIN is a SELECT. In fact, there is no \RBM\SqlQuery\Join class and there won't be.

$select = new Select('project', ['project_id', 'name']);
$owner = $select->join('user', 'owner_id', 'user_id');
print_r($owner);

Gives us

RBM\SqlQuery\Select Object
(
	[_table:protected] => RBM\SqlQuery\Table Object
    	(
        	[_name:protected] => user
…

The most effective advantage of considering J0IN as SELECT is reusability. Let say you have an entity that provides some selects.

class UserEntity 
{

	public function getSelectForActiveUsers()
	{
		$select = new Select('user');
		$select->filter()->equals('active', 1);
		return $select;
	}
	
}	

class ProjectEntity 
{

	public function getSelectForProjectsOfActiveUsers()
	{
		$userEntity = new UserEntity();
		$select = new Select('project');
		$select->addJoin($userEntity->getSelectForActiveUsers(), 'owner_id', 'user_id');
	}	
}

Overloading and inheritance

One intersting feature of this package is that it can be extended to customize the query and filter layer. The finality of inheritance is to objectify the query construction, by making it fluent and business oriented.

###Select

While extending selects, you'll discover that the first obvious usage is to define shortcuts for joins:

class ProjectSelect extends \RBM\SqlQuery\Select
{
	// overloading the table (simple way)
	protected $_table = 'project';

	public function owner()
	{
		return $this->join('user', 'owner_id', 'user_id');
	}
}

Usage:

$projects = new ProjectSelect();
$projects->owner()->filter()->equals('user_id', 1);

###Filter

Creating a filter for our project table is straightforward:

class ProjectFilter extends \RBM\SqlQuery\Filter
{
	public function deleted($deleted)
	{
		return ($deleted) ? $this->isNull('date_deleted') : $this->isNotNull('date_deleted');
	}
}

To use this filter, you'll have to modified the filterClass property from the select:

$projects = new ProjectSelect();
$projects->filter()->deleted(true);

romainbessugesmeusy/php-sql-query 适用场景与选型建议

romainbessugesmeusy/php-sql-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.83k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2013 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 romainbessugesmeusy/php-sql-query 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-18