1 | <?php |
||||||
2 | |||||||
3 | namespace MocOrm\Model; |
||||||
4 | |||||||
5 | abstract class Query |
||||||
6 | { |
||||||
7 | /** |
||||||
8 | * @var array Save the current query for search on database |
||||||
9 | */ |
||||||
10 | protected $_current_custom_query = []; |
||||||
11 | |||||||
12 | /** |
||||||
13 | * @var array Values on currents join |
||||||
14 | */ |
||||||
15 | protected $_joins = []; |
||||||
16 | |||||||
17 | /** |
||||||
18 | * @var string Values on currents group in query |
||||||
19 | */ |
||||||
20 | protected $_group; |
||||||
21 | |||||||
22 | /** |
||||||
23 | * This function is a closed on 'SELECT' and execute all parameter |
||||||
24 | * @return array $objects Return all data on parameters before sending |
||||||
25 | */ |
||||||
26 | public function done() |
||||||
27 | { |
||||||
28 | $this->_data = []; |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||
29 | $query = $this->queryBuilder(); |
||||||
30 | |||||||
31 | $objetos = $this->query($query, $this->_current_custom_query_values); |
||||||
0 ignored issues
–
show
The method
query() does not exist on MocOrm\Model\Query . Since it exists in all sub-types, consider adding an abstract or default implementation to MocOrm\Model\Query .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
32 | |||||||
33 | $this->_current_custom_query_values = []; |
||||||
0 ignored issues
–
show
|
|||||||
34 | |||||||
35 | $this->_data = $objetos; |
||||||
36 | |||||||
37 | $this->cleanNewData(); |
||||||
0 ignored issues
–
show
The method
cleanNewData() does not exist on MocOrm\Model\Query . Since it exists in all sub-types, consider adding an abstract or default implementation to MocOrm\Model\Query .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
38 | |||||||
39 | return $objetos; |
||||||
40 | } |
||||||
41 | |||||||
42 | /** |
||||||
43 | * This function is a mirror of 'where' on database |
||||||
44 | * @param string|null $parameter The colunm for compare |
||||||
45 | * @param string|null $value Value to compare |
||||||
46 | * @return $this This object for others interation |
||||||
47 | * @throws \Exception Case types on parameter is invalid or not set. |
||||||
48 | */ |
||||||
49 | final public function where($parameter = null, $value = null) |
||||||
50 | { |
||||||
51 | if (!is_string($parameter)) throw new \Exception('Invalid parameter type.'); |
||||||
52 | if (!isset($value)) throw new \Exception('This value not set.'); |
||||||
53 | |||||||
54 | $this->_current_custom_query[] = " WHERE $parameter = ? "; |
||||||
55 | $this->_current_custom_query_values[] = $value; |
||||||
0 ignored issues
–
show
|
|||||||
56 | |||||||
57 | return $this; |
||||||
58 | } |
||||||
59 | |||||||
60 | /** |
||||||
61 | * This function is a mirror of 'AND' on database |
||||||
62 | * @param string|null $parameter The colunm for compare |
||||||
63 | * @param string|null $value Value to compare |
||||||
64 | * @return $this This object for others interation |
||||||
65 | * @throws \Exception Case types on parameter is invalid or not set. |
||||||
66 | */ |
||||||
67 | final public function and ($parameter = null, $value = null) |
||||||
68 | { |
||||||
69 | if (!is_string($parameter)) throw new \Exception('Invalid parameter type.'); |
||||||
70 | if (!isset($value)) throw new \Exception('This value not set.'); |
||||||
71 | |||||||
72 | $this->_current_custom_query[] = " AND $parameter = ? "; |
||||||
73 | $this->_current_custom_query_values[] = $value; |
||||||
0 ignored issues
–
show
|
|||||||
74 | |||||||
75 | return $this; |
||||||
76 | } |
||||||
77 | |||||||
78 | /** |
||||||
79 | * This function is a mirror of 'OR' on database |
||||||
80 | * @param string|null $parameter The colunm for compare |
||||||
81 | * @param string|null $value Value to compare |
||||||
82 | * @return $this This object for others interation |
||||||
83 | * @throws \Exception Case types on parameter is invalid or not set. |
||||||
84 | */ |
||||||
85 | final public function or ($parameter = null, $value = null) |
||||||
86 | { |
||||||
87 | if (!is_string($parameter)) throw new \Exception('Invalid parameter type.'); |
||||||
88 | if (!isset($value)) throw new \Exception('This value not set.'); |
||||||
89 | |||||||
90 | $this->_current_custom_query[] = " OR $parameter = ? "; |
||||||
91 | $this->_current_custom_query_values[] = $value; |
||||||
0 ignored issues
–
show
|
|||||||
92 | |||||||
93 | return $this; |
||||||
94 | } |
||||||
95 | |||||||
96 | /** |
||||||
97 | * This function is a mirror of 'Order By' on database |
||||||
98 | * @param string|null $parameter The colunm for compare |
||||||
99 | * @param string|null $value Value to compare |
||||||
100 | * @return $this This object for others interation |
||||||
101 | * @throws \Exception Case types on parameter is invalid or not set. |
||||||
102 | */ |
||||||
103 | final public function orderBy($parameter = null, $value = null) |
||||||
104 | { |
||||||
105 | if (!is_string($parameter)) throw new \Exception('Invalid parameter type.'); |
||||||
106 | if (!isset($value)) throw new \Exception('This value not set.'); |
||||||
107 | if (!is_string($value)) throw new \Exception('Don\'t accepted this type on value.'); |
||||||
0 ignored issues
–
show
|
|||||||
108 | if ($value != 'ASC' AND $value != 'DESC') throw new \Exception('This value not found.'); |
||||||
109 | |||||||
110 | $this->_current_custom_query[] = " ORDER BY $parameter $value"; |
||||||
111 | |||||||
112 | return $this; |
||||||
113 | } |
||||||
114 | |||||||
115 | /** |
||||||
116 | * This function is a mirror of 'LEFT JOIN' on database |
||||||
117 | * @param string $join |
||||||
118 | * @return $this |
||||||
119 | */ |
||||||
120 | final public function custom($partialQuery) |
||||||
121 | { |
||||||
122 | if (!is_string($partialQuery)) throw new \Exception('Invalid parameter type.'); |
||||||
123 | |||||||
124 | $this->_current_custom_query[] = $partialQuery; |
||||||
125 | |||||||
126 | return $this; |
||||||
127 | } |
||||||
128 | |||||||
129 | /** |
||||||
130 | * This function is a mirror of 'LEFT JOIN' on database |
||||||
131 | * @param string $join |
||||||
132 | * @return $this |
||||||
133 | */ |
||||||
134 | final public function leftJoin($join = '') |
||||||
135 | { |
||||||
136 | $this->_joins[] = ' LEFT JOIN ' . $join; |
||||||
137 | return $this; |
||||||
138 | } |
||||||
139 | |||||||
140 | /** |
||||||
141 | * This function is a mirror of 'Right JOIN' on database |
||||||
142 | * @param string $join |
||||||
143 | * @return $this |
||||||
144 | */ |
||||||
145 | final public function rightJoin($join = '') |
||||||
146 | { |
||||||
147 | $this->_joins[] = ' RIGHT JOIN ' . $join; |
||||||
148 | return $this; |
||||||
149 | } |
||||||
150 | |||||||
151 | /** |
||||||
152 | * This function is a mirror of 'INNER JOIN' on database |
||||||
153 | * @param string $join |
||||||
154 | * @return $this |
||||||
155 | */ |
||||||
156 | final public function innerJoin($join = '') |
||||||
157 | { |
||||||
158 | $this->_joins[] = ' INNER JOIN ' . $join; |
||||||
159 | return $this; |
||||||
160 | } |
||||||
161 | |||||||
162 | /** |
||||||
163 | * This function is a mirror of 'FULL OUTER JOIN' on database |
||||||
164 | * @param string $join |
||||||
165 | * @return $this |
||||||
166 | */ |
||||||
167 | final public function fullOuterJoin($join = '') |
||||||
168 | { |
||||||
169 | $this->_joins[] = ' FULL OUTER JOIN ' . $join; |
||||||
170 | return $this; |
||||||
171 | } |
||||||
172 | |||||||
173 | /** |
||||||
174 | * This function is a mirror of 'Group By' on database |
||||||
175 | * @param string $colunm |
||||||
176 | * @return $this |
||||||
177 | * @throws \Exception |
||||||
178 | */ |
||||||
179 | final public function groupBy($colunm) |
||||||
180 | { |
||||||
181 | if (!is_string($colunm)) throw new \Exception('The colunm isn\'t an string.'); |
||||||
0 ignored issues
–
show
|
|||||||
182 | |||||||
183 | $this->_group = " GROUP BY $colunm "; |
||||||
184 | |||||||
185 | return $this; |
||||||
186 | } |
||||||
187 | |||||||
188 | /** |
||||||
189 | * Builder query |
||||||
190 | * @return string The query mounted for using. |
||||||
191 | */ |
||||||
192 | final protected function queryBuilder() |
||||||
193 | { |
||||||
194 | $sql = array_shift($this->_current_custom_query); |
||||||
195 | |||||||
196 | $sql .= implode('', $this->_joins); |
||||||
197 | |||||||
198 | $last = array_pop($this->_current_custom_query); |
||||||
199 | |||||||
200 | $sql .= implode('', $this->_current_custom_query); |
||||||
201 | |||||||
202 | if (substr_count($last, 'ORDER BY') > 0) { |
||||||
203 | $sql .= isset($this->_group) ? $this->_group : ''; |
||||||
204 | $sql .= $last; |
||||||
205 | } else { |
||||||
206 | $sql .= $last; |
||||||
207 | $sql .= isset($this->_group) ? $this->_group : ''; |
||||||
208 | } |
||||||
209 | |||||||
210 | $this->_current_custom_query = []; |
||||||
211 | |||||||
212 | return $sql; |
||||||
213 | } |
||||||
214 | } |