-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMysqlDriver.php
More file actions
67 lines (54 loc) · 1.69 KB
/
MysqlDriver.php
File metadata and controls
67 lines (54 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace CzProject\SqlGenerator\Drivers;
use CzProject\SqlGenerator\IDriver;
use DateTimeInterface;
class MysqlDriver implements IDriver
{
use DateParserTrait;
/**
* @var string[]
*/
private const TRANSACTION = [
'start' => 'BEGIN;',
'commit' => 'COMMIT;',
'rollback' => 'ROLLBACK;',
];
private const LAST_ID = 'LAST_INSERT_ID()';
public function escapeIdentifier(string $value): string
{
// @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
// @see http://api.dibiphp.com/2.3.2/source-drivers.DibiMySqlDriver.php.html#307
return '`' . str_replace('`', '``', $value) . '`';
}
public function escapeText(string $value): string
{
// https://dev.mysql.com/doc/refman/5.5/en/string-literals.html
// http://us3.php.net/manual/en/function.mysql-real-escape-string.php#101248
return '\'' . str_replace(
['\\', "\0", "\n", "\r", "\t", "'", '"', "\x1a"],
['\\\\', '\\0', '\\n', '\\r', '\\t', "\\'", '\\"', '\\Z'],
$value
) . '\'';
}
public function escapeBool(bool $value): string
{
return $value ? '1' : '0';
}
public function escapeDate(DateTimeInterface|string $value): string
{
return $this->dateFormat($value, "'Y-m-d'");
}
public function escapeDateTime(DateTimeInterface|string $value): string
{
return $this->dateFormat($value, "'Y-m-d H:i:s'");
}
public function transaction(string $action): string
{
return self::TRANSACTION[$action];
}
public function lastId(): string
{
return self::LAST_ID;
}
}