定制 jasperfw/query-builder 二次开发

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

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

jasperfw/query-builder

Composer 安装命令:

composer require jasperfw/query-builder

包简介

System for dynamically generating database queries

README 文档

README

A library for building SQL database queries. Unlike many other query builders that require the entire query be built in the system, this library follows a hybrid approach, allowing the developer to create a complex query and pass it in as a template, replacing certain tokens with autogenerated SQL snippets.

Features

  • Generates SQL queries from scratch
  • Modifies passed SQL queries with generated SQL snippets

Instructions

Installation

Install using composer composer require "jasperfw/query-builder"

Basic Usage

Create a basic select

$query = Query::build($dbc) // Pass in the database connection
    ->setDBC($dbc) // Alteratively, pass the database connection later
    ->template($hardcodedQuery) // Pass in a basic SQL query that will be modified
    ->select() // The query type, select, insert, update or delete
    ->table('schema.tblA', 'tblA') // The base table
    ->join('schema.tblB', 'b') // A simple join on a table with an alias
    ->leftJoin('schema.tblC', 'c', 'tblA.index = c.index') // More complex join with condition
    ->rightJoin('schema.tbld', 'd', 'tblA.index = d.index')
    ->innerJoin('schema.tblE', 'e', 'tblA.index = e.index')
    ->outerJoin('schema.tblF', 'f', 'tblA.index = f.index')
    ->column('table.colA', 'colA', 'bob', 'param') // A column, along with a parameter
    ->column('table.colB', 'colB', 'steve') // A column that will use a default parameter name
    ->column('table.colC', null, 'dave')
    ->where('colA = :a', ['a' => 'b']) // Where condition, with parameters
    ->sortBy('colA', 'ASC') // Sort, can be called multiple times
    ->pageNumber(2) // The page, can be ommitted if doing a limit query
    ->pageSize(50); // Number of records per page

The produces the following query (with newlines added for readablity):

SELECT [table.colA] AS colA, [table.colB] AS colB, [table.colC] AS table.colC 
FROM schema.tblA tblA,
    schema.tblB b, 
    schema.tblC c ON tblA.index = c.index, 
    schema.tbld d ON tblA.index = d.index, 
    schema.tblE e ON tblA.index = e.index, 
    schema.tblF f ON tblA.index = f.index 
WHERE colA = :a
ORDER BY colA ASC
LIMIT 50,50

Use the Query object as a prepared statement

Query objects can be reused for efficiency.

$query->prepare(); // Generates a statement to be used
$query->parameter('a', 'newValue');
$result = $query->execute(); // Execute the query, returns a JasperFW/DataAccess/ResultSet::ResultSet object

Note that if the query structure is changed after the query is executed, the ResultSet object returned by execute will be invalidated. These changes are any changes to columns, tables, query type, pagination. Changes to the parameters will not cause this. Typically, changing the table structure after executing is not recommended, and instead a new Query should be created or the existing Query object should be cloned.

Create a custom template

A base query can be passed in as a string using the template() function. The system will simply replace certain tokens with generated SQL snippets. The query type setting function (select(), insert(), etc) must still be called so that the Query object will generate the appropriate snippets. Note that the template can be passed to the query type function as an argument, for simplicity.

The following tokens in a query will be replaced:

  • {{columns}} will be replaced with a list of columns
  • {{pagination}} will be replaced with either a limit or limit and offset
  • {{tables}} will be replaced with the list of tables and joins
  • {{where}} will be replaced with the where clause(s). By default, these where clauses are preceeded with "WHERE ". This can be overridden by adding a | and a replacement keyword. eg {{where|AND}}. Spaces will be automatically placed.
  • {{sort}} will be replaced with the sort clause(s). By default the sorts will be preceeded with "ORDER BY " but if the {{sort}} is following existing sorts in the template, this can be overridden by adding a | and a new keywork or a comma, eg {{sort|,}}

Note that pagination, sort and where clauses will only be added if the query builder has been passed additonal clauses. This allows a good deal of flexibility for modifying queries on the fly.

jasperfw/query-builder 适用场景与选型建议

jasperfw/query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 69
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-03-01