-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMySQLDriver.php
More file actions
94 lines (81 loc) · 2.6 KB
/
MySQLDriver.php
File metadata and controls
94 lines (81 loc) · 2.6 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* This file is part of Cycle ORM package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cycle\Database\Driver\MySQL;
use Cycle\Database\Config\DriverConfig;
use Cycle\Database\Config\MySQLDriverConfig;
use Cycle\Database\Driver\Driver;
use Cycle\Database\Driver\MySQL\Query\MySQLDeleteQuery;
use Cycle\Database\Driver\MySQL\Query\MySQLSelectQuery;
use Cycle\Database\Driver\MySQL\Query\MySQLUpdateQuery;
use Cycle\Database\Exception\StatementException;
use Cycle\Database\Query\InsertQuery;
use Cycle\Database\Query\QueryBuilder;
use Cycle\Database\Query\UpsertQuery;
/**
* Talks to mysql databases.
*/
class MySQLDriver extends Driver
{
/**
* @param MySQLDriverConfig $config
*/
public static function create(DriverConfig $config): static
{
return new static(
$config,
new MySQLHandler(),
new MySQLCompiler('``'),
new QueryBuilder(
new MySQLSelectQuery(),
new InsertQuery(),
new UpsertQuery(),
new MySQLUpdateQuery(),
new MySQLDeleteQuery(),
),
);
}
/**
* @psalm-return non-empty-string
*/
public function getType(): string
{
return 'MySQL';
}
public function getTransactionLevel(): int
{
if (!$this->getPDO()->inTransaction()) {
$this->transactionLevel = 0;
return 0;
}
return $this->transactionLevel;
}
/**
*
*
* @see https://dev.mysql.com/doc/refman/5.6/en/error-messages-client.html#error_cr_conn_host_error
*/
protected function mapException(\Throwable $exception, string $query): StatementException
{
if ((int) $exception->getCode() === 23000) {
return new StatementException\ConstrainException($exception, $query);
}
$message = \strtolower($exception->getMessage());
if (
\str_contains($message, 'server has gone away')
|| \str_contains($message, 'broken pipe')
|| \str_contains($message, 'connection')
|| \str_contains($message, 'packets out of order')
|| \str_contains($message, 'disconnected by the server because of inactivity')
|| ((int) $exception->getCode() > 2000 && (int) $exception->getCode() < 2100)
) {
return new StatementException\ConnectionException($exception, $query);
}
return new StatementException($exception, $query);
}
}