|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\Database\Storage; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\Database\Storage\Query\Join; |
|
5
|
|
|
use Darya\Storage\Query as StorageQuery; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Darya's database storage query representation. |
|
9
|
|
|
* |
|
10
|
|
|
* Provides joins and subqueries. |
|
11
|
|
|
* |
|
12
|
|
|
* @property-read Join[] $joins |
|
13
|
|
|
* @property-read string[] $groupings |
|
14
|
|
|
* @property-read array $having |
|
15
|
|
|
* @property-read StorageQuery $insertSubquery |
|
16
|
|
|
* |
|
17
|
|
|
* @author Chris Andrew <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Query extends StorageQuery |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Joins to apply to the query. |
|
23
|
|
|
* |
|
24
|
|
|
* @var Join[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $joins = array(); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The set of fields to group results by. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $groupings = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Filters to apply to results after grouping. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $having = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The subquery to use for the insert query. |
|
44
|
|
|
* |
|
45
|
|
|
* @var StorageQuery |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $insertSubquery; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Add an inner join to the query. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $resource |
|
53
|
|
|
* @param mixed $condition [optional] |
|
54
|
|
|
* @param string $type [optional] |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function join($resource, $condition = null, $type = 'inner') |
|
58
|
|
|
{ |
|
59
|
|
|
$join = new Join($type, $resource); |
|
60
|
|
|
|
|
61
|
|
|
if (is_callable($condition)) { |
|
62
|
|
|
call_user_func($condition, $join); |
|
63
|
|
|
} else { |
|
64
|
|
|
$join->on($condition); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->joins[] = $join; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add a left join to the query. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $resource |
|
76
|
|
|
* @param mixed $condition |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function leftJoin($resource, $condition = null) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->join($resource, $condition, 'left'); |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Add a right join to the query. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $resource |
|
90
|
|
|
* @param mixed $condition |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function rightJoin($resource, $condition = null) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->join($resource, $condition, 'right'); |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Add a grouping to the query. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $field |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
public function group($field) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->groupings[] = $field; |
|
109
|
|
|
|
|
110
|
|
|
$this->groupings = array_keys(array_count_values($this->groupings)); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Add a set of groupings to the query. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string[] $fields |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function groupings($fields) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->groupings = array_merge($this->groupings, $fields); |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Add a "having" filter condition to the query. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $field |
|
132
|
|
|
* @param mixed $value [optional] |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public function having($field, $value = null) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->having = array_merge($this->having, array($field => $value)); |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Add multiple "having" filter conditions to the query. |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $filters |
|
146
|
|
|
* @return $this |
|
147
|
|
|
*/ |
|
148
|
|
|
public function havings(array $filters = array()) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->having = array_merge($this->having, $filters); |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Make this a create query with the given subquery. |
|
157
|
|
|
* |
|
158
|
|
|
* @param StorageQuery $query |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
|
|
public function createFrom(StorageQuery $query) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->modify(static::CREATE); |
|
164
|
|
|
$this->insertSubquery = $query; |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Alias for createFrom(). |
|
171
|
|
|
* |
|
172
|
|
|
* @param StorageQuery $query |
|
173
|
|
|
* @return $this |
|
174
|
|
|
*/ |
|
175
|
|
|
public function insertFrom(StorageQuery $query) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->createFrom($query); |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..