1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ___ _ _ |
5
|
|
|
* | _ \__ _| |_ __ _| |__ __ _ ___ ___ |
6
|
|
|
* | _/ _` | _/ _` | '_ \/ _` (_-</ -_) |
7
|
|
|
* |_| \__,_|\__\__,_|_.__/\__,_/__/\___| |
8
|
|
|
* |
9
|
|
|
* This file is part of Kristuff\Patabase. |
10
|
|
|
* (c) Kristuff <[email protected]> |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* @version 1.0.1 |
16
|
|
|
* @copyright 2017-2022 Christophe Buliard |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Kristuff\Patabase\Query; |
20
|
|
|
|
21
|
|
|
use Kristuff\Patabase\Query\QueryBuilder; |
22
|
|
|
use Kristuff\Patabase\Query\QueryFilter; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Handle SQL HAVING conditions |
26
|
|
|
*/ |
27
|
|
|
class Having extends QueryFilter |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* sql base: WHERE or HAVING |
32
|
|
|
* |
33
|
|
|
* @access protected |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $sqlBase = 'HAVING'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add an HAVING function filter |
40
|
|
|
* |
41
|
|
|
* @access public |
42
|
|
|
* @param string $function The function name without parenthesis ('SUM', 'COUNT', ...) |
43
|
|
|
* @param string $column The column name |
44
|
|
|
* @param string $operator The logic operator (example '=', '<', ...) |
45
|
|
|
* @param mixed $value The condition value |
46
|
|
|
* |
47
|
|
|
* @return $this|QueryBuilder |
48
|
|
|
*/ |
49
|
|
|
public function fn(string $function, string $column, string $operator, $value) |
50
|
|
|
{ |
51
|
|
|
$sql = $function .'('. ($column ? $this->query->escape($column): '') . ') ' . $operator . ' '; |
52
|
|
|
$this->addCondition($function, $sql, $column, $value); |
53
|
|
|
return $this->returnFunction(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add an HAVING COUNT() filter |
58
|
|
|
* |
59
|
|
|
* @access public |
60
|
|
|
* @param string $operator The logic operator (example '=', '<', ...) |
61
|
|
|
* @param mixed $value The condition value |
62
|
|
|
* |
63
|
|
|
* @return $this|QueryBuilder |
64
|
|
|
*/ |
65
|
|
|
public function count(string $operator, $value) |
66
|
|
|
{ |
67
|
|
|
$sql = 'COUNT(*) '. $operator. ' '; |
68
|
|
|
$this->addCondition('COUNT', $sql, 'COUNT', $value); |
69
|
|
|
return $this->returnFunction(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add an HAVING SUM() filter |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* @param string $column The column name |
77
|
|
|
* @param string $operator The logic operator (example '=', '<', ...) |
78
|
|
|
* @param mixed $value The condition value |
79
|
|
|
* |
80
|
|
|
* @return $this|QueryBuilder |
81
|
|
|
*/ |
82
|
|
|
public function sum(string $column, string $operator, $value) |
83
|
|
|
{ |
84
|
|
|
$this->fn('SUM', $column, $operator, $value); |
85
|
|
|
return $this->returnFunction(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |