1 | <?php |
||
11 | abstract class Base implements IBulkSql, IGeneralSql |
||
12 | { |
||
13 | /** |
||
14 | * @var TransactionPDO Connection to database |
||
15 | */ |
||
16 | private $dbConnection; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $table; |
||
22 | |||
23 | /** |
||
24 | * @var array Column names |
||
25 | */ |
||
26 | private $fields; |
||
27 | |||
28 | /** |
||
29 | * @var int Items to be executed per query. Zero if executed once with finish() |
||
30 | */ |
||
31 | private $itemsPerQuery = 0; |
||
32 | |||
33 | /** |
||
34 | * @var bool Flag to use IGNORE in query |
||
35 | */ |
||
36 | private $useIgnore = false; |
||
37 | |||
38 | /** |
||
39 | * @var bool Flag to disable indexes during execution |
||
40 | */ |
||
41 | private $disableIndexes = false; |
||
42 | |||
43 | /** |
||
44 | * @var array Items data to be executed with query |
||
45 | */ |
||
46 | private $preparedItems = []; |
||
47 | |||
48 | /** |
||
49 | * @var int Total item count that were used in queries |
||
50 | */ |
||
51 | private $totalItemsCount = 0; |
||
52 | |||
53 | /** |
||
54 | * @var int Count of rows, that were affected by query execution |
||
55 | */ |
||
56 | private $affectedCount = 0; |
||
57 | |||
58 | /** |
||
59 | * @var bool Flag used to know if finish() method was called |
||
60 | */ |
||
61 | private $isFinished = false; |
||
62 | |||
63 | /** |
||
64 | * @var array Column which will not be bind via bindValue() |
||
65 | */ |
||
66 | private $ignoreColumnBinding = []; |
||
67 | |||
68 | /** |
||
69 | * Base constructor. |
||
70 | * |
||
71 | * @param TransactionPDO $db |
||
72 | * @param string $table |
||
73 | * @param array $fields |
||
74 | */ |
||
75 | public function __construct(TransactionPDO $db, string $table, array $fields = []) |
||
81 | |||
82 | /** |
||
83 | * Base destructor |
||
84 | * Forcibly call finish() method, if queries wasn't executed |
||
85 | * |
||
86 | * @throws \League\Database\Exceptions\LogicException |
||
87 | */ |
||
88 | public function __destruct() |
||
96 | |||
97 | /** |
||
98 | * Build query line to be used for PDO prepare() method |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | abstract protected function buildQuery() : string; |
||
103 | |||
104 | /** |
||
105 | * Bind params to PDO statement |
||
106 | * |
||
107 | * @param PDOStatement $statement |
||
108 | * |
||
109 | * @return mixed |
||
110 | */ |
||
111 | abstract protected function bindValues(PDOStatement $statement); |
||
112 | |||
113 | /** |
||
114 | * Add new data to be executed |
||
115 | * |
||
116 | * @param array $item |
||
117 | * |
||
118 | * @throws LogicException |
||
119 | * @return bool Return if it was executed |
||
120 | */ |
||
121 | final public function add(array $item) : bool |
||
140 | |||
141 | /** |
||
142 | * Execute query with prepared items |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | final public function execute() |
||
168 | |||
169 | /** |
||
170 | * Finish query execution |
||
171 | * |
||
172 | * @throws LogicException |
||
173 | * @return void |
||
174 | */ |
||
175 | final public function finish() |
||
190 | |||
191 | /** |
||
192 | * Get count affected rows |
||
193 | * |
||
194 | * @return int |
||
195 | */ |
||
196 | final public function getAffectedCount() : int |
||
200 | |||
201 | /** |
||
202 | * Get count of items that will be executed per one query |
||
203 | * |
||
204 | * @return int |
||
205 | */ |
||
206 | final public function getItemsPerQuery() : int |
||
210 | |||
211 | /** |
||
212 | * Set count of items that will be executed per one query |
||
213 | * |
||
214 | * @param int $itemsPerQuery |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | final public function setItemsPerQuery(int $itemsPerQuery) : self |
||
224 | |||
225 | /** |
||
226 | * Check if IGNORE will be used in query |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | final public function isIgnoreUsed(): bool |
||
234 | |||
235 | /** |
||
236 | * Set usage of IGNORE in query |
||
237 | * |
||
238 | * @param bool $useIgnore |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | final public function useIgnore(bool $useIgnore) : self |
||
248 | |||
249 | /** |
||
250 | * Check if indexes will be disabled while query execution |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | final public function isIndexesDisabled(): bool |
||
258 | |||
259 | /** |
||
260 | * Set disabling indexes while query execution |
||
261 | * |
||
262 | * @param bool $disableIndexes |
||
263 | * |
||
264 | * @return $this |
||
265 | */ |
||
266 | final public function disableIndexes(bool $disableIndexes) : self |
||
272 | |||
273 | /** |
||
274 | * Get prepared items to be executed |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | public function getPreparedItems() : array |
||
282 | |||
283 | /** |
||
284 | * Get count of items used in query |
||
285 | * |
||
286 | * @return int |
||
287 | */ |
||
288 | public function getTotalItemsCount() : int |
||
292 | |||
293 | /** |
||
294 | * Get column which will not be bind via bindValue() |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | public function getIgnoredColumn() : array |
||
302 | |||
303 | /** |
||
304 | * Set column which will not be bind via bindValue() |
||
305 | * |
||
306 | * @param string $ignoredColumn |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function addIgnoredColumn(string $ignoredColumn) : self |
||
316 | |||
317 | /** |
||
318 | * Reset column which will be ignored via bindValue() |
||
319 | * |
||
320 | * @return $this |
||
321 | */ |
||
322 | public function purgeIgnoredColumns() : self |
||
328 | |||
329 | /** |
||
330 | * Get column names |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | final protected function getFields(): array |
||
338 | |||
339 | /** |
||
340 | * Get table name |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | final protected function getTable() : string |
||
348 | |||
349 | /** |
||
350 | * Get Database connection |
||
351 | * |
||
352 | * @return TransactionPDO |
||
353 | */ |
||
354 | final protected function getDbConnection(): TransactionPDO |
||
358 | |||
359 | /** |
||
360 | * Reset fields array |
||
361 | * |
||
362 | * @return void |
||
363 | */ |
||
364 | final protected function resetFields() : void |
||
368 | |||
369 | /** |
||
370 | * Reset items array |
||
371 | * |
||
372 | * @return void |
||
373 | */ |
||
374 | final protected function resetPreparedItems() : void |
||
378 | } |
||
379 |