-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregates.php
57 lines (50 loc) · 1.82 KB
/
aggregates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
require_once('providers/mysql/mysql.php');
use function DarkRoast\select as select;
use function DarkRoast\table as table;
$pdo = new PDO('mysql:host=localhost; dbname=recipe', "dev", "Ig8ajGd1vtZZSaa99kvZ");
$provider = new \DarkRoast\MySQL\DataProvider($pdo);
$recipe = $provider->reflectTable('recipe');
$recipe2 = $recipe->copy();
$aggregates = table(select($recipe2['cook_time']->max()
->name('maxCookingTime'),
$recipe2['prep_time']->min(),
$recipe2['id']->count(),
$recipe2['rest_time']->sum()
->name('sumRestTime'),
$recipe2['group']->group())->groupFilter($recipe2['cook_time']->max()
->lessThan(70)),
$provider);
$query = select($recipe['title'],
$aggregates['maxCookingTime']->sortDescending(),
$aggregates['maxCookingTime']->sortAscending(), // Last sort takes precedence.
$aggregates['sumRestTime'])->filter($recipe['group']->equals($aggregates['group']));
$darkRoast = $query->build($provider);
echo $darkRoast->querySource();
print_r($darkRoast->execute());
/* Generated Query (verbatim):
SELECT
`t0`.`title`,
`u0`.`maxCookingTime`,
`u0`.`maxCookingTime`,
`u0`.`sumRestTime`
FROM
`recipe` AS `t0`
CROSS JOIN (
SELECT
max(`tt0`.`cook_time`) AS maxCookingTime,
min(`tt0`.`prep_time`) AS UserField2,
count(`tt0`.`id`) AS UserField3,
sum(`tt0`.`rest_time`) AS sumRestTime,
`tt0`.`group`
FROM
`recipe` AS `tt0`
GROUP BY
`tt0`.`group`
HAVING
max(`tt0`.`cook_time`) < :b0
) AS u0
WHERE
`t0`.`group` = `u0`.`group`
ORDER BY
`u0`.`maxCookingTime` ASC */