19
19
abstract class DbWrap
20
20
{
21
21
/** @var PDO */
22
- private $ pdo ;
22
+ protected $ pdo ;
23
23
24
24
/** @var array */
25
- private static $ pdoDefaultOptions = [
25
+ protected static $ pdoDefaultOptions = [
26
26
PDO ::ATTR_ERRMODE => PDO ::ERRMODE_EXCEPTION ,
27
27
PDO ::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8 " ,
28
28
PDO ::ATTR_EMULATE_PREPARES => false
@@ -124,27 +124,9 @@ public function findOneBy($table, $criteria, $orderBy = null)
124
124
throw new InvalidArgumentException ('Parameter $orderBy has to be NULL or an array. ' );
125
125
}
126
126
$ query = "SELECT * FROM $ table " ;
127
- if (!empty ($ criteria ))
128
- {
129
- $ query .= "WHERE " ;
130
- foreach ($ criteria as $ column => $ value )
131
- {
132
- $ query .= "` $ column` = : $ column AND " ;
133
- }
134
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 4 );
135
- $ query .= " " ;
136
- }
137
- if (!empty ($ orderBy ))
138
- {
139
- $ query .= "ORDER BY " ;
140
- foreach ($ orderBy as $ column => $ direction )
141
- {
142
- $ query .= "` $ column` $ direction, " ;
143
- }
144
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 2 );
145
- $ query .= " " ;
146
- }
147
- $ query .= "LIMIT 1 " ;
127
+ $ query = $ this ->appendCriteriaToQuery ($ query , $ criteria );
128
+ $ query = $ this ->appendOrderByToQuery ($ query , $ orderBy );
129
+ $ query = $ this ->appendLimitAndOffset ($ query , 1 );
148
130
return $ this ->fetchFirstRow ($ query , $ criteria );
149
131
}
150
132
@@ -179,34 +161,9 @@ public function findBy($table, $criteria, $orderBy = null, $limit = null, $offse
179
161
throw new InvalidArgumentException ('Parameter $offset could not be parsed to an integer. ' );
180
162
}
181
163
$ query = "SELECT * FROM $ table " ;
182
- if (!empty ($ criteria ))
183
- {
184
- $ query .= "WHERE " ;
185
- foreach ($ criteria as $ column => $ value )
186
- {
187
- $ query .= "` $ column` = : $ column AND " ;
188
- }
189
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 4 );
190
- $ query .= " " ;
191
- }
192
- if (!empty ($ orderBy ))
193
- {
194
- $ query .= "ORDER BY " ;
195
- foreach ($ orderBy as $ column => $ direction )
196
- {
197
- $ query .= "` $ column` $ direction, " ;
198
- }
199
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 2 );
200
- $ query .= " " ;
201
- }
202
- if ($ limit !== null )
203
- {
204
- $ query .= "LIMIT $ limit " ;
205
- }
206
- if ($ offset !== null )
207
- {
208
- $ query .= "OFFSET $ offset " ;
209
- }
164
+ $ query = $ this ->appendCriteriaToQuery ($ query , $ criteria );
165
+ $ query = $ this ->appendOrderByToQuery ($ query , $ orderBy );
166
+ $ query = $ this ->appendLimitAndOffset ($ query , $ limit , $ offset );
210
167
return $ this ->fetchAll ($ query , $ criteria );
211
168
}
212
169
@@ -226,15 +183,7 @@ public function findAll($table, $orderBy = null)
226
183
throw new InvalidArgumentException ('Parameter $orderBy has to be NULL or an array. ' );
227
184
}
228
185
$ query = "SELECT * FROM $ table " ;
229
- if (!empty ($ orderBy ))
230
- {
231
- $ query .= "ORDER BY " ;
232
- foreach ($ orderBy as $ column => $ direction )
233
- {
234
- $ query .= "` $ column` $ direction, " ;
235
- }
236
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 2 );
237
- }
186
+ $ query = $ this ->appendOrderByToQuery ($ query , $ orderBy );
238
187
return $ this ->fetchAll ($ query );
239
188
}
240
189
@@ -254,16 +203,7 @@ public function count($table, $criteria = null)
254
203
throw new InvalidArgumentException ('Parameter $criteria has to be NULL or an array. ' );
255
204
}
256
205
$ query = "SELECT COUNT(*) FROM $ table " ;
257
- if (!empty ($ criteria ))
258
- {
259
- $ query .= "WHERE " ;
260
- foreach ($ criteria as $ column => $ value )
261
- {
262
- $ query .= "` $ column` = : $ column AND " ;
263
- }
264
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 4 );
265
- $ query .= " " ;
266
- }
206
+ $ query = $ this ->appendCriteriaToQuery ($ query , $ criteria );
267
207
return $ this ->fetchFirstColumn ($ query , $ criteria );
268
208
}
269
209
@@ -341,26 +281,7 @@ public function iterate($table, $criteria, $orderBy = null, $batchSize = 500)
341
281
throw new InvalidArgumentException ('Parameter $batchSize has to be NULL or an integer. ' );
342
282
}
343
283
$ query = "SELECT * FROM $ table " ;
344
- if (!empty ($ criteria ))
345
- {
346
- $ query .= "WHERE " ;
347
- foreach ($ criteria as $ column => $ value )
348
- {
349
- $ query .= "` $ column` = : $ column AND " ;
350
- }
351
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 4 );
352
- $ query .= " " ;
353
- }
354
- if (!empty ($ criteria ))
355
- {
356
- $ query .= "WHERE " ;
357
- foreach ($ criteria as $ column => $ value )
358
- {
359
- $ query .= "` $ column` = : $ column AND " ;
360
- }
361
- $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 4 );
362
- $ query .= " " ;
363
- }
284
+ $ this ->appendCriteriaToQuery ($ query , $ criteria );
364
285
if (!empty ($ orderBy ))
365
286
{
366
287
$ query .= "ORDER BY " ;
@@ -397,7 +318,7 @@ public function iterateQuery($query, $parameters = null, $batchSize = 500)
397
318
$ index = 0 ;
398
319
do
399
320
{
400
- $ limitedQuery = $ query .= " LIMIT $ batchSize OFFSET " . ($ index * $ batchSize );
321
+ $ limitedQuery = $ this -> appendLimitAndOffset ( $ query, $ batchSize, ($ index * $ batchSize) );
401
322
$ index ++;
402
323
$ result = $ this ->fetchAll ($ limitedQuery , $ parameters );
403
324
if (!empty ($ result ))
@@ -590,4 +511,111 @@ public function executeNonQuery($query, $parameters = null)
590
511
throw new RuntimeException (sprintf ("Executing query '%s' was not successful. " , $ query ));
591
512
}
592
513
}
514
+
515
+ /**
516
+ * @param string $query
517
+ * @param array $criteria
518
+ * @return string
519
+ */
520
+ protected function appendCriteriaToQuery ($ query , $ criteria )
521
+ {
522
+ if (Strings::isNullOrWhiteSpace ($ query ))
523
+ {
524
+ throw new InvalidArgumentException ('Parameter $query cannot be NULL, empty string ("") or only white-space characters. ' );
525
+ }
526
+ if (!is_array ($ criteria ))
527
+ {
528
+ throw new InvalidArgumentException ('Parameter $criteria has to be an array. ' );
529
+ }
530
+ if (!empty ($ criteria ))
531
+ {
532
+ $ query = $ this ->appendWhiteSpaceIfNecessary ($ query );
533
+ $ query .= "WHERE " ;
534
+ foreach ($ criteria as $ column => $ value )
535
+ {
536
+ $ query .= "` $ column` = : $ column AND " ;
537
+ }
538
+ $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 4 );
539
+ }
540
+ return $ query ;
541
+ }
542
+
543
+ /**
544
+ * @param string $query
545
+ * @param array|null $orderBy
546
+ * @return string
547
+ */
548
+ protected function appendOrderByToQuery ($ query , $ orderBy = null )
549
+ {
550
+ if (Strings::isNullOrWhiteSpace ($ query ))
551
+ {
552
+ throw new InvalidArgumentException ('Parameter $query cannot be NULL, empty string ("") or only white-space characters. ' );
553
+ }
554
+ if (!is_array ($ orderBy ) && $ orderBy != null )
555
+ {
556
+ throw new InvalidArgumentException ('Parameter $orderBy has to be NULL or an array. ' );
557
+ }
558
+ if (!empty ($ orderBy ))
559
+ {
560
+ $ query = $ this ->appendWhiteSpaceIfNecessary ($ query );
561
+ $ query .= "ORDER BY " ;
562
+ foreach ($ orderBy as $ column => $ direction )
563
+ {
564
+ $ query .= "` $ column` $ direction, " ;
565
+ }
566
+ $ query = Strings::substring ($ query , 0 , Strings::length ($ query ) - 2 );
567
+ $ query .= " " ;
568
+ }
569
+ return $ query ;
570
+ }
571
+
572
+ /**
573
+ * @param string $query
574
+ * @param int|null $limit
575
+ * @param int|null $offset
576
+ * @return string
577
+ */
578
+ protected function appendLimitAndOffset ($ query , $ limit = null , $ offset = null )
579
+ {
580
+ if (Strings::isNullOrWhiteSpace ($ query ))
581
+ {
582
+ throw new InvalidArgumentException ('Parameter $query cannot be NULL, empty string ("") or only white-space characters. ' );
583
+ }
584
+ if ($ limit !== null && !Scalars::tryParse ($ limit , $ limit , Scalars::INTEGER ))
585
+ {
586
+ throw new InvalidArgumentException ('Parameter $limit could not be parsed to an integer. ' );
587
+ }
588
+ if ($ offset !== null && !Scalars::tryParse ($ offset , $ offset , Scalars::INTEGER ))
589
+ {
590
+ throw new InvalidArgumentException ('Parameter $offset could not be parsed to an integer. ' );
591
+ }
592
+ if ($ limit !== null )
593
+ {
594
+ $ query = $ this ->appendWhiteSpaceIfNecessary ($ query );
595
+ $ query .= "LIMIT $ limit " ;
596
+ }
597
+ if ($ offset !== null )
598
+ {
599
+ $ query = $ this ->appendWhiteSpaceIfNecessary ($ query );
600
+ $ query .= "OFFSET $ offset " ;
601
+ }
602
+ return $ query ;
603
+ }
604
+
605
+ /**
606
+ * @param string $query
607
+ * @return string
608
+ */
609
+ protected function appendWhiteSpaceIfNecessary ($ query )
610
+ {
611
+ if (Strings::isNullOrWhiteSpace ($ query ))
612
+ {
613
+ throw new InvalidArgumentException ('Parameter $query cannot be NULL, empty string ("") or only white-space characters. ' );
614
+ }
615
+ if (!Strings::endsWith ($ query , " " ))
616
+ {
617
+ $ query .= " " ;
618
+ }
619
+ return $ query ;
620
+ }
593
621
}
0 commit comments