timetoogo / pinq.demo.sql
PINQ DSL查询提供者平台的一个文档示例
dev-master
2015-08-01 13:36 UTC
Requires
- php: >=5.4.0
- timetoogo/pinq: ~3.1
Requires (Dev)
- phpunit/dbunit: >=1.2
- phpunit/phpunit: 3.7.*
This package is auto-updated.
Last update: 2024-08-25 06:22:58 UTC
README
这是PINQ查询提供者功能的演示。这个库基于PINQ V3,展示了DSL查询提供者API的强大功能。这是一个概念验证,不应在生产环境中运行。
概述
这个示例在2KLOC以下编写,实现了PINQ查询API的一个小部分,并将其映射到MySQL数据库后端。表被视为集合,行表示为关联数组。
use Pinq\Demo\Sql\DB; $db = new DB($pdo); $results = $db->table('customers') ->where(function ($row) { return $row['age'] <= 50; }) ->orderByAscending(function ($row) { return $row['firstName']; }) ->thenByAscending(function ($row) { return $row['lastName']; }) ->take(50) ->select(function ($row) { return [ 'fullName' => $row['firstName'] . ' ' . $row['lastName'], 'address' => $row['address'], 'dateOfBirth' => $row['dateOfBirth'], ]; }) ->asArray()
上述查询将被转换为SQL等价形式
SELECT CONCAT(CONCAT(customers.firstName, ' '), customers.lastName) AS fullName, customers.address AS address, customers.dateOfBirth AS dateOfBirth FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM customers) AS customers WHERE (customers.age <= 50) ) AS customers ORDER BY customers.firstName ASC, customers.lastName ASC LIMIT 50 OFFSET 0 ) AS customers
该查询将针对提供的PDO连接执行,并返回结果集。由于这是一个演示,SQL生成相当临时,不能产生最有效的代码。
API支持
范围(筛选)API
->where(function)
=WHERE <expr>
->orderBy(function, direction)
,->orderByAscending(function)
,->orderByDescending(function)
=ORDER BY <expr> ASC|DESC
->unique()
=DISTINCT
->take(amount)
,->skip(amount)
,->slice(skip, take)
=LIMIT <amount> OFFSET <amount>
->select(function)
=SELECT <expr> AS <alias>...
请求(数据检索)API
->asArray()
,->getIterator()
,->getTrueIterator()
,->asTraversable()
,->asCollection()
=SELECT * FROM
操作(数据修改)API
->apply(function)
=UPDATE <table> SET <column> = <expr>...
->clear()
=DELETE FROM <table>
PHP表达式语法
- 二元运算符:
+, -, *, /, %, ==, !=, &&, ||, >, >=, <, <=, .
- 一元运算符:
!, ~, -, +
isset
和empty
- 三元:
?:
- 函数:
strlen, md5
文档
此存储库是PINQ查询提供者实现的示例。有关更多详细信息,您可以在存储库中浏览或通过以下链接了解。