Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Having | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
12 | |
100.00% |
1 / 1 |
having | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
orHaving | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
buildHavingSQL | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
getHavingParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
resetHaving | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb\Query\Builder; |
4 | |
5 | /** |
6 | * Having |
7 | * |
8 | * Adds in having(), orHaving() and groupings |
9 | * into the SQL builder. |
10 | * |
11 | * $query->having(function(ConditionBuilder $conditions) { |
12 | * $conditions |
13 | * ->andWith('user', '=', 'Alex') |
14 | * ->andWith('country', '=', 'GB'); |
15 | * }) |
16 | * ->orHaving(function(ConditionBuilder $conditions) { |
17 | * $conditions->andWith('user', '=', 'Lucie'); |
18 | * $conditions->andWith('country', '=', 'CA'); |
19 | * }); |
20 | * |
21 | * Would generate: |
22 | * |
23 | * HAVING (name = 'Alex' AND country = 'GB') |
24 | * OR (name = 'Lucie' AND country = 'CA') |
25 | * |
26 | * Unlike While, this trait is only used once in the Select query type |
27 | * however it's of sufficient complexity to warrant it being split out. |
28 | * |
29 | * @package BO\Zmsdb\Query\Builder |
30 | * @author Alex Gisby<alex@solution10.com> |
31 | * @license MIT |
32 | */ |
33 | trait Having |
34 | { |
35 | /** |
36 | * @var ConditionBuilder |
37 | */ |
38 | protected $havingBuilder; |
39 | |
40 | /** |
41 | * Get/Set an "AND HAVING" clause on the query. You can either pass a simple |
42 | * comparison ('name', '=', 'Alex') or a function to append multiple queries |
43 | * in a group. |
44 | * |
45 | * @param string|\Closure|null $field Fieldname|callback for group|to return |
46 | * @param string|null $operator Operator (=, !=, <>, <= etc) |
47 | * @param mixed|null $value Value to test against |
48 | * @return $this|array $this on set, array on get |
49 | */ |
50 | public function having($field = null, $operator = null, $value = null) |
51 | { |
52 | if (!isset($this->havingBuilder)) { |
53 | $this->havingBuilder = new ConditionBuilder(); |
54 | } |
55 | if ($field == null) { |
56 | return $this->havingBuilder->conditions(); |
57 | } |
58 | $this->havingBuilder->andWith($field, $operator, $value); |
59 | return $this; |
60 | } |
61 | |
62 | /** |
63 | * Adds a new 'OR' predicate to the query. |
64 | * |
65 | * @param string|\Closure|null $field Fieldname|callback for group|to return |
66 | * @param string|null $operator Operator (=, !=, <>, <= etc) |
67 | * @param mixed|null $value Value to test against |
68 | * @return $this|array $this on set, array on get |
69 | */ |
70 | public function orHaving($field = null, $operator = null, $value = null) |
71 | { |
72 | if (!isset($this->havingBuilder)) { |
73 | $this->havingBuilder = new ConditionBuilder(); |
74 | } |
75 | if ($field == null) { |
76 | return $this->havingBuilder->conditions(); |
77 | } |
78 | $this->havingBuilder->orWith($field, $operator, $value); |
79 | return $this; |
80 | } |
81 | |
82 | /** |
83 | * Returns the SQL string for the WHERE portion of the query |
84 | * |
85 | * @param DialectInterface $dialect |
86 | * @return string |
87 | */ |
88 | public function buildHavingSQL(DialectInterface $dialect) |
89 | { |
90 | if (!isset($this->havingBuilder) || !$this->havingBuilder->hasConditions()) { |
91 | return ''; |
92 | } |
93 | |
94 | return 'HAVING ' . $this->havingBuilder->buildConditionSQL($dialect); |
95 | } |
96 | |
97 | /** |
98 | * Returns an array of all the parameter that have been passed to having() |
99 | * ready to be thrown at PDO. |
100 | * |
101 | * @return array |
102 | */ |
103 | public function getHavingParams() |
104 | { |
105 | return (isset($this->havingBuilder)) ? $this->havingBuilder->getConditionParameters() : []; |
106 | } |
107 | |
108 | /** |
109 | * Resets the HAVING portion of this query to empty. |
110 | * |
111 | * @return $this |
112 | */ |
113 | public function resetHaving() |
114 | { |
115 | unset($this->havingBuilder); |
116 | $this->havingBuilder = new ConditionBuilder(); |
117 | return $this; |
118 | } |
119 | } |