-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDriver.php
More file actions
155 lines (127 loc) · 4.02 KB
/
Copy pathDriver.php
File metadata and controls
155 lines (127 loc) · 4.02 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace PhpDb\Pgsql;
use Override;
use PgSql\Result as PgSqlResult;
use PhpDb\Adapter\Driver\ConnectionInterface;
use PhpDb\Adapter\Driver\DriverAwareInterface;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Driver\ResultInterface;
use PhpDb\Adapter\Driver\StatementInterface;
use PhpDb\Adapter\Exception;
use PhpDb\Adapter\Profiler\ProfilerAwareInterface;
use PhpDb\Adapter\Profiler\ProfilerInterface;
use function array_intersect_key;
use function array_merge;
use function extension_loaded;
use function is_string;
/**
* @final
*/
class Driver implements DriverInterface, ProfilerAwareInterface
{
protected ?ProfilerInterface $profiler = null;
/** @var array */
protected $options = [
'buffer_results' => false,
];
public function __construct(
protected readonly DriverAwareInterface&ConnectionInterface&Connection $connection,
protected readonly DriverAwareInterface&StatementInterface&Statement $statementPrototype = new Statement(),
protected readonly ResultInterface&Result $resultPrototype = new Result(),
array $options = []
) {
$this->checkEnvironment();
//todo: verify this usage
$options = array_intersect_key(array_merge($this->options, $options), $this->options);
$this->connection->setDriver($this);
$this->statementPrototype->setDriver($this);
}
#[Override]
public function setProfiler(
ProfilerInterface $profiler
): DriverInterface&ProfilerAwareInterface {
$this->profiler = $profiler;
// @phpstan-ignore instanceof.alwaysTrue
if ($this->connection instanceof ProfilerAwareInterface) {
$this->connection->setProfiler($profiler);
}
// @phpstan-ignore instanceof.alwaysTrue
if ($this->statementPrototype instanceof ProfilerAwareInterface) {
$this->statementPrototype->setProfiler($profiler);
}
return $this;
}
public function getProfiler(): ?ProfilerInterface
{
return $this->profiler;
}
#[Override]
public function checkEnvironment(): bool
{
if (! extension_loaded('pgsql')) {
throw new Exception\RuntimeException(
'The PostgreSQL (pgsql) extension is required for this Driver but the extension is not loaded'
);
}
return true;
}
#[Override]
public function getConnection(): ConnectionInterface&Connection
{
return $this->connection;
}
/**
* Create statement
*
* @param resource|string|null $sqlOrResource
*/
#[Override]
public function createStatement($sqlOrResource = null): StatementInterface&Statement
{
$statement = clone $this->statementPrototype;
if (is_string($sqlOrResource)) {
$statement->setSql($sqlOrResource);
}
if (! $this->connection->isConnected()) {
$this->connection->connect();
}
$statement->initialize($this->connection->getResource());
return $statement;
}
/**
* Create result
*
* @param PgSqlResult|resource $resource
*/
#[Override]
public function createResult($resource): ResultInterface&Result
{
/** @var Result $result */
$result = clone $this->resultPrototype;
$result->initialize($resource, $this->connection->getLastGeneratedValue());
return $result;
}
public function getResultPrototype(): ResultInterface&Result
{
return $this->resultPrototype;
}
#[Override]
public function getPrepareType(): string
{
return self::PARAMETERIZATION_POSITIONAL;
}
#[Override]
public function formatParameterName(string $name, ?string $type = null): string
{
return '$#';
}
/**
* Get last generated value
*/
#[Override]
public function getLastGeneratedValue(?string $name = null): int|string|false|null
{
return $this->connection->getLastGeneratedValue($name);
}
}