1 | <?php |
||
26 | class MySQLAdapter implements DataAdapterInterface |
||
27 | { |
||
28 | /** @var PDO */ |
||
29 | protected $dataDriver; |
||
30 | /** @var string */ |
||
31 | protected $dataGroup = null; |
||
32 | /** @var string */ |
||
33 | protected $idKey = null; |
||
34 | |||
35 | /** |
||
36 | * MySQLAdapter constructor. |
||
37 | * |
||
38 | * @param DataDriverInterface $dataDriver |
||
39 | * @throws InvalidArgumentException |
||
40 | */ |
||
41 | 16 | public function __construct(DataDriverInterface $dataDriver) |
|
61 | |||
62 | /** |
||
63 | * Returns the Data Storage instance. |
||
64 | * |
||
65 | * @return DataDriverInterface |
||
66 | */ |
||
67 | 1 | public function getDataDriver() : DataDriverInterface |
|
71 | |||
72 | /** |
||
73 | * Set adapter data group. For Databases this can be the Tables. |
||
74 | * |
||
75 | * @param string $dataGroup |
||
76 | * @return DataAdapterInterface |
||
77 | */ |
||
78 | 11 | public function setDataGroup(string $dataGroup) : DataAdapterInterface |
|
84 | |||
85 | /** |
||
86 | * Set adapter ID key. For Databases this can be the Primary key. Only simple key is allowed. |
||
87 | * |
||
88 | * @param string $idKey |
||
89 | * @return DataAdapterInterface |
||
90 | */ |
||
91 | 3 | public function setIdKey(string $idKey) : DataAdapterInterface |
|
97 | |||
98 | /** |
||
99 | * Get exactly one "row" of data according to the expression. |
||
100 | * |
||
101 | * @param int $identifier |
||
102 | * @return array |
||
103 | * |
||
104 | * @codeCoverageIgnore Don't test external library. |
||
105 | */ |
||
106 | public function getData(int $identifier) : array |
||
119 | |||
120 | /** |
||
121 | * Get a set of data according to the expression and the chunk. |
||
122 | * |
||
123 | * @param array $expression |
||
124 | * @param int $limit |
||
125 | * @param int $offset |
||
126 | * @return array |
||
127 | * |
||
128 | * @codeCoverageIgnore Don't test external library. |
||
129 | */ |
||
130 | public function getDataSet(array $expression, int $limit = PHP_INT_MAX, int $offset = 0) : array |
||
143 | |||
144 | /** |
||
145 | * Get the number of matched data in the set according to the expression. |
||
146 | * |
||
147 | * @param array $expression |
||
148 | * @return int |
||
149 | * |
||
150 | * @codeCoverageIgnore Don't test external library. |
||
151 | */ |
||
152 | public function getDataCardinality(array $expression) : int |
||
163 | |||
164 | /** |
||
165 | * Builds SQL query from the expression. |
||
166 | * |
||
167 | * @param array $expression |
||
168 | * @param array $queryBinds |
||
169 | * @param int $limit |
||
170 | * @param int $offset |
||
171 | * @return string |
||
172 | */ |
||
173 | 9 | protected function getSelectQueryForExpression( |
|
191 | |||
192 | /** |
||
193 | * Creates a WHERE expression for the SQL query. |
||
194 | * |
||
195 | * @param array $expression |
||
196 | * @param array $queryBinds |
||
197 | * @return string |
||
198 | */ |
||
199 | 12 | protected function getWhereExpression(array $expression, array&$queryBinds) : string |
|
214 | |||
215 | /** |
||
216 | * Set the query params and quaery bindings according to the `column` and `value`. |
||
217 | * |
||
218 | * @param string $column |
||
219 | * @param mixed $value |
||
220 | * @param array $queryParams |
||
221 | * @param array $queryBinds |
||
222 | */ |
||
223 | 11 | protected function setParamsAndBinds(string $column, $value, array&$queryParams, array&$queryBinds) : void |
|
236 | |||
237 | /** |
||
238 | * Gets a simple condition for the column. |
||
239 | * |
||
240 | * @param string $column |
||
241 | * @return string 'my_column = ?' |
||
242 | */ |
||
243 | 11 | protected function getSimpleColumnCondition(string $column) : string |
|
247 | |||
248 | /** |
||
249 | * Gets a 'LIKE' condition for the column. |
||
250 | * |
||
251 | * Allows special cases: |
||
252 | * @example ['my_column LIKE ?' => 'some value%'] |
||
253 | * @example ['my_column LIKE' => 'some value%'] |
||
254 | * @example ['my_column' => 'some value%'] |
||
255 | * |
||
256 | * @param string $column |
||
257 | * @return string 'my_column LIKE ?' |
||
258 | */ |
||
259 | 4 | protected function getLikeColumnCondition(string $column) : string |
|
265 | |||
266 | /** |
||
267 | * Gets an 'IN' condition for the column. |
||
268 | * |
||
269 | * Allows special cases: |
||
270 | * @example ['my_column IN (?)' => [1,2,3]] |
||
271 | * @example ['my_column IN ?' => [1,2,3]] |
||
272 | * @example ['my_column IN' => [1,2,3]] |
||
273 | * @example ['my_column' => [1,2,3]] |
||
274 | * |
||
275 | * @param string $column |
||
276 | * @param int $parameterCount |
||
277 | * @return string 'my_column IN (?,?,?)' |
||
278 | */ |
||
279 | 5 | protected function getInColumnCondition(string $column, int $parameterCount = 1) : string |
|
287 | |||
288 | /** |
||
289 | * Insert or update entity in the storage. |
||
290 | * |
||
291 | * @param int $identifier |
||
292 | * @param array $data |
||
293 | * @throws RuntimeException |
||
294 | * @return int The ID of the saved entity in the storage |
||
295 | * |
||
296 | * @codeCoverageIgnore Don't test external library. |
||
297 | */ |
||
298 | public function saveData(? int $identifier = null, array $data) : int |
||
330 | |||
331 | /** |
||
332 | * Binds values to the statement. |
||
333 | * |
||
334 | * @param PDOStatement $statement |
||
335 | * @param array $queryBind |
||
336 | * @return void |
||
337 | * |
||
338 | * @codeCoverageIgnore Don't test external library. |
||
339 | */ |
||
340 | protected function bindValuesToStatement(PDOStatement&$statement, array $queryBind) : void |
||
354 | |||
355 | /** |
||
356 | * Removes an entity from the storage. |
||
357 | * |
||
358 | * @param int $identifier |
||
359 | * @return bool |
||
360 | * |
||
361 | * @codeCoverageIgnore Don't test external library. |
||
362 | */ |
||
363 | public function deleteData(int $identifier) : bool |
||
369 | } |
||
370 |