承接 firewox/qel 相关项目开发

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

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

firewox/qel

Composer 安装命令:

composer require firewox/qel

包简介

Library for handling custom query expressions that can be translated into SQL CRUD queries for any database type

README 文档

README

Library for handling custom query expressions that can be translated into SQL CRUD queries for any database type

Getting Started

Use composer package manager to install QEL.

composer require firewox/qel

Prerequisites & configurations

This library requires at least PHP 7.0 with PDO extension to run. In your PHP file include composer autoload:

require_once 'vendor/autoload.php';

Structuring your code

QEL is simply a language to express your data queries independent of the underlying platform or implementations.

  1. Define model classes with public properties directly mapping database table fields
use Firewox\QEL\Model;

class User extends Model
{

  const SCHEMA = 'public';
  const TABLE = 'user';

  public $id;
  public $username;
  public $password;
  ...
  1. Implement Manager abstract method defined in Model parent class
public function manager(): FunctionCommon
  {

    // Get column conversion / transformation maps
    $forwardMap = [];
    $reverseMap = [];

    // Define functions associated with this Model
    $functions = [];

    // Define joins to this model
    $joins = [];

    // Instantiate function class
    return new FunctionCommon(
      $this,
      self::SCHEMA,
      self::TABLE,
      self::PRIMARY_KEY,
      $joins,
      $forwardMap,
      $reverseMap,
      $functions);

  }

You can also add column mapping, joins, and functions that affect the Model child class. Please see the advanced topics below for more information on how you can accomplish that.

  1. Implement Databases abstract method defined in Model parent class. This method must return DatabaseInterface instance, which can be one of PostgreSQL, MySQL etc. database. These database clients also support in-memory data caching to speed up data queries. Currently supported is Redis (https://redis.io)
  public function database(): DatabaseInterface
  {

    // Postgres singleton instance (but you can use constructor directly)
    return Db::get('127.0.0.1', 5432, 'sample', 'postgres', 'admin', Redis::default());

  }

Advanced features

You can define add column mapping, joins, and functions that affect the Model child class and consequently the output queries for any of the implemented database clients.

  1. Model functions
// Define functions associated with this Model
$functions = [
  'fx:count' => new SQLFx('count', [ new SQLFxParam('id')], 'Database COUNT function.')
];
  1. Joins
// Define joins to this model
$joins = [
  'user' => new JoinWireMap(User::class, QueryJoin::TYPE_LEFT, 'userid', 'id')
];
  1. Reverse column mapping
$reverseMap = [
    'activated' => 'CAST(COALESCE(activated, FALSE) AS integer) AS activated',
    'geom' => 'ST_ASText(geom) AS geom'
]
  1. Forward column mapping
$forwardMap = [
    'geom' => 'ST_GeomFromText(:geom)'
]

Usage examples

QEL currently has 3 fundamental uses, which are (1) Data projection, (2) data updating, and (3) data deletion. To achieve these there are 3 helpers defined in each database client:

Data projection

public function generateJoinSelect(
  string $columns,
  string $table,
  array $joins,
  ?string $where = null,
  ?string $order = null,
  int $offset = 0,
  int $limit = 0): string;

Data updating

public function generateJoinUpdate(
  array $columns, 
  string $table, 
  array $joinedTables,
  array $conditions,
  ?string $where = null): string;

Data deletion

public function generateJoinDelete(
  string $table,
  array $joinedTables, 
  array $conditions,
  string $where): string;

Example 1: Generating data projection SQL

$session = new Session();
$man = $session->manager();

// Get all sessions where user id is 1
$qel = 'user.id = 1';

// Determine join depth
$depth = $man->determineJoinDepth($qel);      // Expected depth is 1 (number of dots in query)

// Get qel evaluator for to parse QEL
$evaluator = $man->getQueryEvaluator($depth);
$parser = new QueryParser($evaluator);

// Parse QEL
$parser->parse($qel);

// Create select query base on session database (ie. PgSQL)
$sql = $man->getDb()->generateJoinSelect(
  $evaluator->getProjection(),
  $man->getTableWithAlias(),
  $evaluator->getJoinQueries(),
  $evaluator->getQuery(),
  $evaluator->getOrder()
);

echo $sql;

// OUTPUT
// 'SELECT * FROM public.session AS a 
//    LEFT OUTER JOIN public.user AS b 
//    ON a.userid = b.id WHERE b.id = 1';

We can even call functions that we have defined in the model functions variables:

$qel = 'fx:count(id) of user.id = 1';

SQL generated will be:

SELECT count ( a.id ) FROM public.session AS a 
    LEFT OUTER JOIN public.user AS b 
        ON a.userid = b.id WHERE b.id = 1

Example 2: Generating data update SQL

We can use the same QEL defined in example 1 to generate our update query

// Create update join query base on session database (ie. PgSQL)
$sql = $man->getDb()->generateJoinUpdate(
  $man->getData(),
  $man->getTableWithAlias(),
  $evaluator->getJoinTables(),
  $evaluator->getJoinConditions(),
  $evaluator->getQuery()
);

echo $sql;

// OUTPUT
// 'UPDATE public.session AS a 
// SET id = :id, token = :token, userid = :userid, expireson = :expireson
// FROM public.user AS b WHERE a.userid = b.id AND (b.id = 1)';

Example 3: Generating data deletion SQL

// Create delete join query base on session database (ie. PgSQL)
$sql = $man->getDb()->generateJoinDelete(
  $man->getTableWithAlias(),
  $evaluator->getJoinTables(),
  $evaluator->getJoinConditions(),
  $evaluator->getQuery()
);

echo $sql;
// OUTPUT
// DELETE FROM public.session AS a 
// USING public.user AS b 
// WHERE a.userid = b.id AND (b.id = 1)

firewox/qel 适用场景与选型建议

firewox/qel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 186 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-18