-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStatement.php
More file actions
183 lines (149 loc) · 4.72 KB
/
Copy pathStatement.php
File metadata and controls
183 lines (149 loc) · 4.72 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
namespace PhpDb\Pgsql;
use Override;
use PgSql\Connection as PgSqlConnection;
use PgSql\Result as PgSqlResult;
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\ParameterContainer;
use PhpDb\Adapter\Profiler\ProfilerAwareInterface;
use PhpDb\Adapter\Profiler\ProfilerInterface;
use function is_array;
use function pg_execute;
use function pg_last_error;
use function pg_prepare;
use function preg_replace_callback;
class Statement implements
StatementInterface,
DriverAwareInterface,
ProfilerAwareInterface
{
protected bool $bufferResults = false;
protected static int $statementIndex = 0;
protected string $statementName = 'statement';
protected DriverInterface|Driver $driver;
protected ?ProfilerInterface $profiler = null;
protected PgSqlConnection $pgsql;
protected PgSqlResult|false $resource;
protected string $sql;
public function __construct(
protected ParameterContainer $parameterContainer = new ParameterContainer(),
array|bool $options = false
) {
if (is_array($options)) {
$this->bufferResults = $options['buffer_results'] ?? false;
} else {
$this->bufferResults = $options;
}
}
#[Override]
public function setDriver(
DriverInterface $driver
): StatementInterface&DriverAwareInterface&ProfilerAwareInterface {
$this->driver = $driver;
return $this;
}
#[Override]
public function setProfiler(
ProfilerInterface $profiler
): StatementInterface&DriverAwareInterface&ProfilerAwareInterface {
$this->profiler = $profiler;
return $this;
}
public function getProfiler(): ?ProfilerInterface
{
return $this->profiler;
}
public function initialize(PgSqlConnection $pgsql): void
{
$this->pgsql = $pgsql;
}
#[Override]
public function getResource(): PgSqlResult|false
{
return $this->resource;
}
#[Override]
public function setSql(
?string $sql
): StatementInterface&DriverAwareInterface&ProfilerAwareInterface {
$this->sql = $sql;
return $this;
}
#[Override]
public function getSql(): ?string
{
return $this->sql;
}
#[Override]
public function setParameterContainer(
ParameterContainer $parameterContainer
): StatementInterface&DriverAwareInterface&ProfilerAwareInterface {
$this->parameterContainer = $parameterContainer;
return $this;
}
#[Override]
public function getParameterContainer(): ParameterContainer
{
return $this->parameterContainer;
}
#[Override]
public function prepare(
?string $sql = null
): StatementInterface&DriverAwareInterface&ProfilerAwareInterface {
$sql = $sql ?? $this->sql;
$pCount = 1;
$sql = preg_replace_callback(
'#\$\##',
function () use (&$pCount) {
return '$' . $pCount++;
},
$sql
);
$this->sql = $sql;
$this->statementName .= ++static::$statementIndex;
$this->resource = pg_prepare($this->pgsql, $this->statementName, $sql);
return $this;
}
#[Override]
public function isPrepared(): bool
{
return isset($this->resource);
}
/**
* Execute
*
* @throws Exception\InvalidQueryException
*/
#[Override]
public function execute(ParameterContainer|array|null $parameters = null): ?ResultInterface
{
if (! $this->isPrepared()) {
$this->prepare();
}
/** START Standard ParameterContainer Merging Block */
if ($parameters instanceof ParameterContainer) {
$this->parameterContainer = $parameters;
$parameters = null;
}
if (is_array($parameters)) {
$this->parameterContainer->setFromArray($parameters);
}
if ($this->parameterContainer->count() > 0) {
$parameters = $this->parameterContainer->getPositionalArray();
}
/** END Standard ParameterContainer Merging Block */
$this->profiler?->profilerStart($this);
$resultResource = pg_execute($this->pgsql, $this->statementName, (array) $parameters);
$this->profiler?->profilerFinish();
if ($resultResource === false) {
throw new Exception\InvalidQueryException(pg_last_error());
}
/** @phpstan-ignore argument.type */
return $this->driver->createResult($resultResource);
}
}