/
PdoConnection.php
119 lines (100 loc) · 3.03 KB
/
PdoConnection.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace Keboola\DbWriterAdapter\PDO;
use ErrorException;
use Keboola\Component\UserException;
use Keboola\DbWriterAdapter\Connection\BaseConnection;
use PDO;
use PDOException;
use PDOStatement;
use Psr\Log\LoggerInterface;
class PdoConnection extends BaseConnection
{
protected string $dsn;
protected string $user;
protected string $password;
/** @var array<int, int|string|bool> */
protected array $options;
/** @var callable|null */
protected $init;
protected PDO $pdo;
/**
* @param array<int, int|string|bool> $options
* @param string[] $userInitQueries
* @throws UserException
*/
public function __construct(
LoggerInterface $logger,
string $dsn,
string $user,
string $password,
array $options,
?callable $init = null,
int $connectMaxRetries = self::CONNECT_DEFAULT_MAX_RETRIES,
array $userInitQueries = [],
) {
// Convert errors to PDOExceptions
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
$this->options = $options;
$this->init = $init;
parent::__construct($logger, $connectMaxRetries, $userInitQueries);
}
protected function connect(): void
{
$this->logger->info(sprintf('Creating PDO connection to "%s".', $this->dsn));
$this->pdo = new PDO($this->dsn, $this->user, $this->password, $this->options);
if ($this->init) {
($this->init)($this->pdo);
}
$this->runUserInitQueries();
}
public function testConnection(): void
{
$this->exec('SELECT 1', 1);
}
public function getConnection(): PDO
{
return $this->pdo;
}
public function quote(string $str): string
{
return $this->pdo->quote($str);
}
public function quoteIdentifier(string $str): string
{
return '`' . str_replace('`', '``', $str) . '`';
}
/**
* @return null|array<int, array<string, mixed>>
* @throws UserException
*/
protected function doQuery(string $queryType, string $query): ?array
{
switch ($queryType) {
case self::QUERY_TYPE_EXEC:
$this->pdo->prepare($query)->execute();
return null;
case self::QUERY_TYPE_FETCH_ALL:
$stmt = $this->pdo->query($query);
if (!$stmt instanceof PDOStatement) {
return [];
}
return $stmt->fetchAll(PDO::FETCH_ASSOC);
default:
throw new UserException(sprintf('Unknown query type "%s".', $queryType));
}
}
/**
* @return string[]
*/
protected function getExpectedExceptionClasses(): array
{
return array_merge(self::BASE_RETRIED_EXCEPTIONS, [
PDOException::class,
ErrorException::class, // eg. ErrorException: Warning: Empty row packet body
]);
}
}