Skip to content

Commit

Permalink
Refactor SQL cleanup by introducing reusable constants
Browse files Browse the repository at this point in the history
Introduce `QUERY_CLEANUP_REGEX` and `TRIM_CHARACTERS_REGEX` constants to improve code readability and reduce redundancy. Replace hardcoded regex strings with these constants in SQL trimming and cleanup operations. This makes future maintenance and updates to these patterns simpler and more centralized.
  • Loading branch information
koriym committed Dec 18, 2024
1 parent 7f5e302 commit c02772e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/SqlQueryRowList.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

class SqlQueryRowList implements RowListInterface
{
public const QUERY_CLEANUP_REGEX = '/\/\*.*?\*\/|--.*$/m';
public const TRIM_CHARACTERS_REGEX = "\\ \t\n\r\0\x0B";

/** @var ExtendedPdoInterface */
private $pdo;

Expand All @@ -38,7 +41,7 @@ public function __invoke(array ...$queries): iterable
$this->sql .= ';';
}

$sqls = explode(';', trim($this->sql, "\\ \t\n\r\0\x0B"));
$sqls = explode(';', trim($this->sql, self::TRIM_CHARACTERS_REGEX));
array_pop($sqls);
$numQueris = count($queries);
if (count($sqls) !== $numQueris) {
Expand All @@ -54,8 +57,8 @@ public function __invoke(array ...$queries): iterable

$lastQuery = $result
? strtolower(trim(
(string) preg_replace('/\/\*.*?\*\/|--.*$/m', '', (string) $result->queryString),
"\\ \t\n\r\0\x0B",
(string) preg_replace(self::QUERY_CLEANUP_REGEX, '', (string) $result->queryString),
self::TRIM_CHARACTERS_REGEX,
)) : '';
if ($result instanceof PDOStatement && strpos($lastQuery, 'select') === 0) {
return (array) $result->fetchAll(PDO::FETCH_ASSOC);
Expand Down

0 comments on commit c02772e

Please sign in to comment.