Skip to content

Commit 80a9c21

Browse files
committed
Removed redutant code
1 parent 5d17382 commit 80a9c21

File tree

1 file changed

+119
-91
lines changed

1 file changed

+119
-91
lines changed

src/DbWrap.php

Lines changed: 119 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
abstract class DbWrap
2020
{
2121
/** @var PDO */
22-
private $pdo;
22+
protected $pdo;
2323

2424
/** @var array */
25-
private static $pdoDefaultOptions = [
25+
protected static $pdoDefaultOptions = [
2626
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
2727
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
2828
PDO::ATTR_EMULATE_PREPARES => false
@@ -124,27 +124,9 @@ public function findOneBy($table, $criteria, $orderBy = null)
124124
throw new InvalidArgumentException('Parameter $orderBy has to be NULL or an array.');
125125
}
126126
$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);
148130
return $this->fetchFirstRow($query, $criteria);
149131
}
150132

@@ -179,34 +161,9 @@ public function findBy($table, $criteria, $orderBy = null, $limit = null, $offse
179161
throw new InvalidArgumentException('Parameter $offset could not be parsed to an integer.');
180162
}
181163
$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);
210167
return $this->fetchAll($query, $criteria);
211168
}
212169

@@ -226,15 +183,7 @@ public function findAll($table, $orderBy = null)
226183
throw new InvalidArgumentException('Parameter $orderBy has to be NULL or an array.');
227184
}
228185
$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);
238187
return $this->fetchAll($query);
239188
}
240189

@@ -254,16 +203,7 @@ public function count($table, $criteria = null)
254203
throw new InvalidArgumentException('Parameter $criteria has to be NULL or an array.');
255204
}
256205
$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);
267207
return $this->fetchFirstColumn($query, $criteria);
268208
}
269209

@@ -341,26 +281,7 @@ public function iterate($table, $criteria, $orderBy = null, $batchSize = 500)
341281
throw new InvalidArgumentException('Parameter $batchSize has to be NULL or an integer.');
342282
}
343283
$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);
364285
if (!empty($orderBy))
365286
{
366287
$query .= "ORDER BY ";
@@ -397,7 +318,7 @@ public function iterateQuery($query, $parameters = null, $batchSize = 500)
397318
$index = 0;
398319
do
399320
{
400-
$limitedQuery = $query .= " LIMIT $batchSize OFFSET " . ($index * $batchSize);
321+
$limitedQuery = $this->appendLimitAndOffset($query, $batchSize, ($index * $batchSize));
401322
$index++;
402323
$result = $this->fetchAll($limitedQuery, $parameters);
403324
if (!empty($result))
@@ -590,4 +511,111 @@ public function executeNonQuery($query, $parameters = null)
590511
throw new RuntimeException(sprintf("Executing query '%s' was not successful.", $query));
591512
}
592513
}
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+
}
593621
}

0 commit comments

Comments
 (0)