承接 processmaker/pmql 相关项目开发

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

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

processmaker/pmql

Composer 安装命令:

composer create-project processmaker/pmql

包简介

An Eloquent trait that provides the pmql scope to allow converting simple sql criteria clauses to Eloquent

README 文档

README

ProcessMaker Query Language

Support for simple SQL-like expressions and converting to Laravel Eloquent. Exposes a Eloquent scope 'pmql' to pass in clauses.

Table of Contents

Simple Usage

$results = Record::where('id', '<', 500)->pmql('username = "foobar" AND age < 25')->get();

Operators

Comparison Operators

Operator Name
= Equal
!= Not Equal
< Less Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal To
LIKE Pattern Match

Logical Operators

Operator Name
AND Match both conditions
OR Match either condition

Case Sensitivity

Note that PMQL syntax is not case sensitive. However, queries are case sensitive. For example, if querying for a string, PMQL will return results only if the case matches your query exactly. This may be bypassed by utilizing the lower(field) syntax. Examples are provided below.

Casting

Fields can be cast to various data types using the cast(field as type) syntax. Currently supported types are text and number. Examples are provided below.

Dates

Strings entered in the format "YYYY-MM-DD" are interpreted as dates and can be used in comparative queries. Dates can be compared dynamically based on the current time utilizing the now keyword. Arithmetic operations can be performed on dates using the date (+ or -)number interval syntax. The interval can be either day, hour, minute, or second. Examples are provided below.

Syntax Examples

Sample Dataset

Let's say we are managing a roster for a basketball team.

id first_name last_name position dob experience starter
8 Liz Cambage center 1991-08-18 3 true
51 Sydney Colson guard 1989-08-06 5 false
5 Dearica Hamby forward 1993-11-06 4 false
21 Kayla McBride guard 1992-06-25 5 true
19 JiSu Park center 1998-12-06 1 false
10 Kelsey Plum guard 1994-08-24 2 true
11 Epiphanny Prince guard 1988-01-11 9 false
14 Sugar Rodgers guard 1989-12-08 6 false
4 Carolyn Swords center 1989-07-19 7 false
22 A'ja Wilson forward 1996-08-08 1 true
1 Tamera Young forward 1986-10-30 11 false
0 Jackie Young guard 1997-09-16 0 true

Basic Syntax

Find players with a specific last name.

Query

last_name = "Young"

Result

id first_name last_name position dob experience starter
1 Tamera Young forward 1986-10-30 11 false
0 Jackie Young guard 1997-09-16 0 true

And

Find players with a specific last name in a specific position.

Query

last_name = "Young" and position = "forward"

Result

id first_name last_name position dob experience starter
1 Tamera Young forward 1986-10-30 11 false

Or

Find players in two different positions.

Query

position = "center" or position = "forward"

Result

id first_name last_name position dob experience starter
8 Liz Cambage center 1991-08-18 3 true
5 Dearica Hamby forward 1993-11-06 4 false
19 JiSu Park center 1998-12-06 1 false
4 Carolyn Swords center 1989-07-19 7 false
22 A'ja Wilson forward 1996-08-08 1 true
1 Tamera Young forward 1986-10-30 11 false

IN

Similar to multiple OR operators, find players with last names Colson or Young

Query

last_name IN ["Colson", "Young"]

Result

id first_name last_name position dob experience starter
51 Sydney Colson guard 1989-08-06 5 false
1 Tamera Young forward 1986-10-30 11 false
0 Jackie Young guard 1997-09-16 0 true

NOT IN

List all players without the last name Colson or Young

Query

last_name NOT IN ["Colson", "Young"]

Result

id first_name last_name position dob experience starter
8 Liz Cambage center 1991-08-18 3 true
5 Dearica Hamby forward 1993-11-06 4 false
21 Kayla McBride guard 1992-06-25 5 true
19 JiSu Park center 1998-12-06 1 false
10 Kelsey Plum guard 1994-08-24 2 true
11 Epiphanny Prince guard 1988-01-11 9 false
14 Sugar Rodgers guard 1989-12-08 6 false
4 Carolyn Swords center 1989-07-19 7 false
22 A'ja Wilson forward 1996-08-08 1 true

Grouping

Find players matching grouped criteria:

Query

(position = "center" or position = "forward") and starter = "true"

Result

id first_name last_name position dob experience starter
8 Liz Cambage center 1991-08-18 3 true
22 A'ja Wilson forward 1996-08-08 1 true

Numeric Comparison

Find players based on years of experience.

Query

experience > 8

Result

id first_name last_name position dob experience starter
11 Epiphanny Prince guard 1988-01-11 9 false
1 Tamera Young forward 1986-10-30 11 false

Casting To Number

What if a field we want to compare mathematically is stored as a string instead of an integer? No problem. We can simply cast it as a number.

Let's say our dataset has changed to store the experience field as a string but we want to find all players with 2 years of experience or less.

Query

cast(experience as number) <= 2

Result

id first_name last_name position dob experience starter
19 JiSu Park center 1998-12-06 1 false
10 Kelsey Plum guard 1994-08-24 2 true
22 A'ja Wilson forward 1996-08-08 1 true
0 Jackie Young guard 1997-09-16 0 true

Date Comparison

Find players born before 1990.

Query

dob < "1990-01-01"

Result

id first_name last_name position dob experience starter
51 Sydney Colson guard 1989-08-06 5 false
11 Epiphanny Prince guard 1988-01-11 9 false
14 Sugar Rodgers guard 1989-12-08 6 false
4 Carolyn Swords center 1989-07-19 7 false
1 Tamera Young forward 1986-10-30 11 false

Dynamic Date Comparison

Find players under 25 as of right now. We utilize the now keyword and subtract 9,125 days (365 * 25 = 9,125).

Query

dob > now -9125 day

Result

id first_name last_name position dob experience starter
19 JiSu Park center 1998-12-06 1 false
22 A'ja Wilson forward 1996-08-08 1 true
0 Jackie Young guard 1997-09-16 0 true

Pattern Matching

We can use the LIKE operator to perform pattern matching with a field. % is a wildcard which matches zero, one, or more characters. _ is a wildcard which matches one character.

Start of String

Let's find all players whose last names begin with the letter P.

Query
last_name like "P%"
Result
id first_name last_name position dob experience starter
19 JiSu Park center 1998-12-06 1 false
10 Kelsey Plum guard 1994-08-24 2 true
11 Epiphanny Prince guard 1988-01-11 9 false

Exact Pattern

Let's find all players whose last names begin with P and have three letters after that.

Query
last_name like "P___"
Result
id first_name last_name position dob experience starter
19 JiSu Park center 1998-12-06 1 false
10 Kelsey Plum guard 1994-08-24 2 true

End of String

Let's find all players whose last names end in "son."

Query
last_name like "%son"
Result
id first_name last_name position dob experience starter
51 Sydney Colson guard 1989-08-06 5 false
22 A'ja Wilson forward 1996-08-08 1 true

String Contains

Let's find all players whose names contain "am."

Query
first_name like "%am%" or last_name like "%am%"
Result
id first_name last_name position dob experience starter
8 Liz Cambage center 1991-08-18 3 true
5 Dearica Hamby forward 1993-11-06 4 false
1 Tamera Young forward 1986-10-30 11 false

Ignore Case

Let's find all players whose names contain "de" regardless of capitalization.

Query
lower(first_name) like "%de%" or lower(last_name) like "%de%"
Result
id first_name last_name position dob experience starter
5 Dearica Hamby forward 1993-11-06 4 false
21 Kayla McBride guard 1992-06-25 5 true

Custom Callbacks

You can utilize custom callbacks in your pmql call to override behavior for a specific expression

$results = Record::where('id', '<', 500)->pmql('username = "FOOBAR" AND age < 25', function($expression) {
    // This example will ensure checking for lowercase usernames as thats how it stored in our database
    if($expression->field->field() == 'username') {
        // If you want to modify the query, you need to return an anonymous function that will add your additional criteria
        return function($query) use($expression) {
                $query->where(DB::raw('LOWER(username)', $expression->operator, strtolower($expression->value->value()));
        }
    }
    // Let default behavior win for non username fields
    return false;
})->get();

processmaker/pmql 适用场景与选型建议

processmaker/pmql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 87.68k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2019 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 87.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 23
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2019-04-15