|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Rémy M. Böhler <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Rorm; |
|
8
|
|
|
|
|
9
|
|
|
use PDO; |
|
10
|
|
|
|
|
11
|
|
|
class Rorm |
|
12
|
|
|
{ |
|
13
|
|
|
const CONNECTION_DEFAULT = 'default'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var PDO[] */ |
|
16
|
|
|
protected static $connections; |
|
17
|
|
|
|
|
18
|
|
|
public static function setDatabase(PDO $dbh, string $connection = self::CONNECTION_DEFAULT): void |
|
19
|
|
|
{ |
|
20
|
|
|
static::$connections[$connection] = $dbh; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @throws ConnectionNotFoundException |
|
25
|
1 |
|
*/ |
|
26
|
|
|
public static function getDatabase(string $connection = self::CONNECTION_DEFAULT): PDO |
|
27
|
1 |
|
{ |
|
28
|
1 |
|
if (array_key_exists($connection, static::$connections)) { |
|
29
|
|
|
return static::$connections[$connection]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
throw new ConnectionNotFoundException('Database connection not found!'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
50 |
|
public static function isMySQL(PDO $dbh): bool |
|
36
|
|
|
{ |
|
37
|
50 |
|
return $dbh->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql'; |
|
38
|
49 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function isSQLite(PDO $dbh): bool |
|
41
|
1 |
|
{ |
|
42
|
|
|
return $dbh->getAttribute(PDO::ATTR_DRIVER_NAME) === 'sqlite'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function quote(PDO $dbh, $value) |
|
46
|
|
|
{ |
|
47
|
|
|
if (is_bool($value)) { |
|
48
|
39 |
|
/** |
|
49
|
|
|
* MySQL has true and false literals |
|
50
|
39 |
|
* SQLite does not support boolean type nor literals |
|
51
|
|
|
*/ |
|
52
|
|
|
if (static::isMySQL($dbh)) { |
|
53
|
|
|
return $value ? 'TRUE' : 'FALSE'; |
|
54
|
|
|
} else { |
|
55
|
|
|
return $value ? 1 : 0; |
|
56
|
|
|
} |
|
57
|
2 |
|
} |
|
58
|
|
|
if ($value === null) { |
|
59
|
2 |
|
return 'NULL'; |
|
60
|
|
|
} |
|
61
|
|
|
if (is_int($value)) { |
|
62
|
|
|
return (int)$value; |
|
63
|
|
|
} |
|
64
|
|
|
if (is_float($value)) { |
|
65
|
|
|
return (float)$value; |
|
66
|
|
|
} |
|
67
|
11 |
|
|
|
68
|
|
|
return $dbh->quote($value); |
|
69
|
11 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Method to quote identifiers |
|
73
|
|
|
* Please make sure you keep the quoter as long you are needing it. |
|
74
|
7 |
|
*/ |
|
75
|
11 |
|
public static function getIdentifierQuoter(PDO $dbh = null): ?callable |
|
76
|
7 |
|
{ |
|
77
|
10 |
|
$dbh = $dbh ?: static::getDatabase(); |
|
78
|
2 |
|
|
|
79
|
10 |
|
if (static::isMySQL($dbh)) { |
|
80
|
3 |
|
// mysql mode |
|
81
|
10 |
|
return function ($identifier) { |
|
82
|
6 |
|
return '`' . str_replace('`', '``', $identifier) . '`'; |
|
83
|
|
|
}; |
|
84
|
10 |
|
} else { |
|
85
|
|
|
// standard sql mode |
|
86
|
|
|
return function ($identifier) { |
|
87
|
|
|
return '"' . str_replace('"', '""', $identifier) . '"'; |
|
88
|
|
|
}; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function reset() |
|
93
|
|
|
{ |
|
94
|
35 |
|
static::$connections = []; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|