Skip to content

Commit 486cd6c

Browse files
committed
Reduced the complexity of MySQL-Class
1 parent 9e24343 commit 486cd6c

File tree

6 files changed

+149
-74
lines changed

6 files changed

+149
-74
lines changed

src/Databases/MySQL.php

Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<?php
22
namespace Kir\MySQL\Databases;
33

4-
use Exception;
5-
use Kir\MySQL\Builder\Select;
6-
use Kir\MySQL\Databases\MySQL\MySQLRunnableSelect;
7-
use PDO;
8-
use PDOException;
9-
use RuntimeException;
10-
use Throwable;
11-
use UnexpectedValueException;
124
use Kir\MySQL\Builder;
5+
use Kir\MySQL\Builder\DBExpr;
136
use Kir\MySQL\Builder\QueryStatement;
7+
use Kir\MySQL\Builder\Select;
148
use Kir\MySQL\Database;
159
use Kir\MySQL\Databases\MySQL\MySQLExceptionInterpreter;
10+
use Kir\MySQL\Databases\MySQL\MySQLExpressionQuoter;
11+
use Kir\MySQL\Databases\MySQL\MySQLFieldQuoter;
12+
use Kir\MySQL\Databases\MySQL\MySQLRunnableSelect;
13+
use Kir\MySQL\Databases\MySQL\MySQLUUIDGenerator;
14+
use Kir\MySQL\Databases\MySQL\MySQLValueQuoter;
1615
use Kir\MySQL\QueryLogger\QueryLoggers;
1716
use Kir\MySQL\Tools\AliasRegistry;
1817
use Kir\MySQL\Tools\VirtualTables;
18+
use PDO;
19+
use PDOException;
20+
use RuntimeException;
21+
use Throwable;
22+
use phpDocumentor\Reflection\Types\Scalar;
1923

2024
/**
2125
*/
@@ -150,63 +154,27 @@ public function getTableFields(string $table): array {
150154

151155
/**
152156
* @param string $expression
153-
* @param array<int, null|int|float|string|array<int, string>|Builder\DBExpr|Builder\Select> $arguments
157+
* @param array<int, null|scalar|array<int, string>|DBExpr|Select> $arguments
154158
* @return string
155159
*/
156160
public function quoteExpression(string $expression, array $arguments = []): string {
157-
$index = -1;
158-
$func = function () use ($arguments, &$index) {
159-
$index++;
160-
if(array_key_exists($index, $arguments)) {
161-
$argument = $arguments[$index];
162-
$value = $this->quote($argument);
163-
} elseif(count($arguments) > 0) {
164-
$args = $arguments;
165-
$value = array_pop($args);
166-
$value = $this->quote($value);
167-
} else {
168-
$value = 'NULL';
169-
}
170-
return $value;
171-
};
172-
$result = preg_replace_callback('/(\\?)/', $func, $expression);
173-
return (string) $result;
161+
return MySQLExpressionQuoter::quoteExpression($this->pdo, $expression, $arguments);
174162
}
175163

176164
/**
177-
* @param null|int|float|string|array<int, string>|Builder\DBExpr|Select $value
165+
* @param null|scalar|array<int, string>|DBExpr|Select $value
178166
* @return string
179167
*/
180168
public function quote($value): string {
181-
if(is_null($value)) {
182-
$result = 'NULL';
183-
} elseif($value instanceof Builder\DBExpr) {
184-
$result = $value->getExpression();
185-
} elseif($value instanceof Builder\Select) {
186-
$result = sprintf('(%s)', (string) $value);
187-
} elseif(is_array($value)) {
188-
$result = implode(', ', array_map(function ($value) { return $this->quote($value); }, $value));
189-
} elseif(is_int($value) || is_float($value)) {
190-
$result = (string) $value;
191-
} else {
192-
$result = $this->pdo->quote($value);
193-
}
194-
return $result;
169+
return MySQLValueQuoter::quote($this->pdo, $value);
195170
}
196171

197172
/**
198173
* @param string $field
199174
* @return string
200175
*/
201176
public function quoteField(string $field): string {
202-
if (is_numeric($field) || !is_string($field)) {
203-
throw new UnexpectedValueException('Field name is invalid');
204-
}
205-
if(strpos($field, '`') !== false) {
206-
return $field;
207-
}
208-
$parts = explode('.', $field);
209-
return '`'.implode('`.`', $parts).'`';
177+
return MySQLFieldQuoter::quoteField($field);
210178
}
211179

212180
/**
@@ -307,7 +275,7 @@ public function dryRun(callable $callback) {
307275
$this->transactionRollback();
308276
}
309277
} else {
310-
$uniqueId = $this->genUniqueId();
278+
$uniqueId = MySQLUUIDGenerator::genUUIDv4();
311279
$this->exec("SAVEPOINT {$uniqueId}");
312280
try {
313281
return $callback($this);
@@ -337,7 +305,7 @@ public function transaction(callable $callback) {
337305
throw $e;
338306
}
339307
}
340-
$uniqueId = $this->genUniqueId();
308+
$uniqueId = MySQLUUIDGenerator::genUUIDv4();
341309
$this->exec("SAVEPOINT {$uniqueId}");
342310
try {
343311
$result = $callback($this);
@@ -394,26 +362,4 @@ private function exceptionHandler(callable $fn) {
394362
}
395363
return null;
396364
}
397-
398-
/**
399-
* @return string
400-
*/
401-
private function genUniqueId(): string {
402-
// Generate a unique id from a former random-uuid-generator
403-
try {
404-
return sprintf('ID%04x%04x%04x%04x%04x%04x%04x%04x',
405-
random_int(0, 0xffff),
406-
random_int(0, 0xffff),
407-
random_int(0, 0xffff),
408-
random_int(0, 0x0fff) | 0x4000,
409-
random_int(0, 0x3fff) | 0x8000,
410-
random_int(0, 0xffff),
411-
random_int(0, 0xffff),
412-
random_int(0, 0xffff)
413-
);
414-
} catch (Exception $e) {
415-
// Should not throw an excepion under normal conditions
416-
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
417-
}
418-
}
419365
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Kir\MySQL\Databases\MySQL;
4+
5+
use Kir\MySQL\Builder\DBExpr;
6+
use Kir\MySQL\Builder\Select;
7+
use PDO;
8+
9+
class MySQLExpressionQuoter {
10+
/**
11+
* @param PDO $pdo
12+
* @param string $expression
13+
* @param array<int, null|scalar|array<int, string>|DBExpr|Select> $arguments
14+
* @return string
15+
*/
16+
public static function quoteExpression(PDO $pdo, string $expression, array $arguments = []): string {
17+
$index = -1;
18+
$func = static function () use ($pdo, $arguments, &$index) {
19+
$index++;
20+
if(array_key_exists($index, $arguments)) {
21+
$argument = $arguments[$index];
22+
$value = MySQLValueQuoter::quote($pdo, $argument);
23+
} elseif(count($arguments) > 0) {
24+
$args = $arguments;
25+
$value = array_pop($args);
26+
$value = MySQLValueQuoter::quote($pdo, $value);
27+
} else {
28+
$value = 'NULL';
29+
}
30+
return $value;
31+
};
32+
return (string) preg_replace_callback('/(\\?)/', $func, $expression);
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Kir\MySQL\Databases\MySQL;
3+
4+
use UnexpectedValueException;
5+
6+
class MySQLFieldQuoter {
7+
/**
8+
* @param string $field
9+
* @return string
10+
*/
11+
public static function quoteField(string $field): string {
12+
if(is_numeric($field) || !is_string($field)) {
13+
throw new UnexpectedValueException('Field name is invalid');
14+
}
15+
if(strpos($field, '`') !== false) {
16+
return $field;
17+
}
18+
$parts = explode('.', $field);
19+
return '`'.implode('`.`', $parts).'`';
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Kir\MySQL\Databases\MySQL;
3+
4+
use RuntimeException;
5+
use Throwable;
6+
7+
class MySQLUUIDGenerator {
8+
/**
9+
* @return string
10+
*/
11+
public static function genUUIDv4(): string {
12+
// Generate a unique id from a former random-uuid-generator
13+
try {
14+
return sprintf('ID%04x%04x%04x%04x%04x%04x%04x%04x',
15+
random_int(0, 0xffff),
16+
random_int(0, 0xffff),
17+
random_int(0, 0xffff),
18+
random_int(0, 0x0fff) | 0x4000,
19+
random_int(0, 0x3fff) | 0x8000,
20+
random_int(0, 0xffff),
21+
random_int(0, 0xffff),
22+
random_int(0, 0xffff)
23+
);
24+
} catch (Throwable $e) {
25+
// Should not throw an excepion under normal conditions
26+
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
27+
}
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace Kir\MySQL\Databases\MySQL;
3+
4+
use Kir\MySQL\Builder\DBExpr;
5+
use Kir\MySQL\Builder\Select;
6+
use PDO;
7+
use phpDocumentor\Reflection\Types\Scalar;
8+
9+
class MySQLValueQuoter {
10+
/**
11+
* @param PDO $pdo
12+
* @param null|scalar|array<int, null|scalar>|DBExpr|Select $value
13+
* @return string
14+
*/
15+
public static function quote(PDO $pdo, $value): string {
16+
if(is_null($value)) {
17+
return 'NULL';
18+
}
19+
20+
if(is_bool($value)) {
21+
return $value ? '1' : '0';
22+
}
23+
24+
if(is_array($value)) {
25+
$fn = static function ($value) use ($pdo) {
26+
return self::quote($pdo, $value);
27+
};
28+
return implode(', ', array_map($fn, $value));
29+
}
30+
31+
if($value instanceof DBExpr) {
32+
return $value->getExpression();
33+
}
34+
35+
if($value instanceof Select) {
36+
return sprintf('(%s)', (string) $value);
37+
}
38+
39+
if(is_int($value) || is_float($value)) {
40+
return (string) $value;
41+
}
42+
43+
return $pdo->quote($value);
44+
}
45+
}

tests/Builder/SelectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public function testParametrizedVirtualTables(): void {
484484
->joinInner('vt2', new VirtualTable('virt_table2', ['active' => true]), 'vt2.field2=t.field2')
485485
->asString();
486486

487-
self::assertEquals("SELECT\n\tt.field1,\n\tt.field2\nFROM\n\ttest t\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a) vt1 ON vt1.field1=t.field1\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a\n\tWHERE\n\t\t(a.active='1')) vt2 ON vt2.field2=t.field2\n", $query);
487+
self::assertEquals("SELECT\n\tt.field1,\n\tt.field2\nFROM\n\ttest t\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a) vt1 ON vt1.field1=t.field1\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a\n\tWHERE\n\t\t(a.active=1)) vt2 ON vt2.field2=t.field2\n", $query);
488488
}
489489

490490
public function testArrayTables(): void {

0 commit comments

Comments
 (0)