Repository navigation
Expand file tree
/
Copy pathAdapter.php
More file actions
180 lines (160 loc) · 5.57 KB
/
Copy pathAdapter.php
File metadata and controls
180 lines (160 loc) · 5.57 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
<?php
declare(strict_types=1);
namespace PhpDb\Adapter;
use Exception as PhpException;
use Override;
use PhpDb\ResultSet;
use function func_get_args;
use function in_array;
use function is_array;
use function is_string;
use function strtolower;
class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface, SchemaAwareInterface
{
/**
* @throws Exception\InvalidArgumentException
*/
public function __construct(
protected Driver\DriverInterface $driver,
protected Platform\PlatformInterface $platform,
protected ResultSet\ResultSetInterface $queryResultSetPrototype = new ResultSet\ResultSet(),
protected ?Profiler\ProfilerInterface $profiler = null
) {
if ($profiler) {
$this->setProfiler($profiler);
}
}
#[Override]
public function setProfiler(Profiler\ProfilerInterface $profiler): Profiler\ProfilerAwareInterface
{
$this->profiler = $profiler;
if ($this->driver instanceof Profiler\ProfilerAwareInterface) {
$this->driver->setProfiler($profiler);
}
return $this;
}
#[Override]
public function getDriver(): Driver\DriverInterface
{
return $this->driver;
}
#[Override]
public function getPlatform(): Platform\PlatformInterface
{
return $this->platform;
}
#[Override]
public function getProfiler(): ?Profiler\ProfilerInterface
{
return $this->profiler;
}
#[Override]
public function getQueryResultSetPrototype(): ResultSet\ResultSetInterface
{
return $this->queryResultSetPrototype;
}
#[Override]
public function getCurrentSchema(): string|false
{
return $this->driver->getConnection()->getCurrentSchema();
}
/**
* query() is a convenience function
*
* @throws Exception\InvalidArgumentException
* @throws PhpException
*/
#[Override]
public function query(
string $sql,
ParameterContainer|array|string $parametersOrQueryMode = self::QUERY_MODE_PREPARE,
?ResultSet\ResultSetInterface $resultPrototype = null
): Driver\StatementInterface|ResultSet\ResultSetInterface|Driver\ResultInterface {
if (
is_string($parametersOrQueryMode)
&& in_array($parametersOrQueryMode, [self::QUERY_MODE_PREPARE, self::QUERY_MODE_EXECUTE])
) {
$mode = $parametersOrQueryMode;
$parameters = null;
} elseif (is_array($parametersOrQueryMode) || $parametersOrQueryMode instanceof ParameterContainer) {
$mode = self::QUERY_MODE_PREPARE;
$parameters = $parametersOrQueryMode;
} else {
throw new Exception\InvalidArgumentException(
'Parameter 2 to this method must be a flag, an array, or ParameterContainer'
);
}
if ($mode === self::QUERY_MODE_PREPARE) {
$lastPreparedStatement = $this->driver->createStatement($sql);
$lastPreparedStatement->prepare();
if (is_array($parameters) || $parameters instanceof ParameterContainer) {
if (is_array($parameters)) {
$lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
} else {
$lastPreparedStatement->setParameterContainer($parameters);
}
$result = $lastPreparedStatement->execute();
} else {
return $lastPreparedStatement;
}
} else {
$result = $this->driver->getConnection()->execute($sql);
}
if ($result instanceof Driver\ResultInterface && $result->isQueryResult()) {
$resultSet = $resultPrototype ?? $this->queryResultSetPrototype;
$resultSetCopy = clone $resultSet;
$resultSetCopy->initialize($result);
return $resultSetCopy;
}
return $result;
}
/**
* Create statement
*/
#[Override]
public function createStatement(
?string $initialSql = null,
ParameterContainer|array $initialParameters = []
): Driver\StatementInterface {
$statement = $this->driver->createStatement($initialSql);
if (
is_array($initialParameters)
) {
$initialParameters = new ParameterContainer($initialParameters);
}
$statement->setParameterContainer($initialParameters);
return $statement;
}
/**
* {@inheritDoc}
*/
public function getHelpers()
{
$functions = [];
$platform = $this->platform;
foreach (func_get_args() as $arg) {
switch ($arg) {
case self::FUNCTION_QUOTE_IDENTIFIER:
$functions[] = function ($value) use ($platform) {
return $platform->quoteIdentifier($value);
};
break;
case self::FUNCTION_QUOTE_VALUE:
$functions[] = function ($value) use ($platform) {
return $platform->quoteValue($value);
};
break;
}
}
return $functions;
}
/** @throws Exception\InvalidArgumentException */
public function __get(string $name): Driver\DriverInterface|Platform\PlatformInterface
{
return match (strtolower($name)) {
'driver' => $this->driver,
'platform' => $this->platform,
default => throw new Exception\InvalidArgumentException('Invalid magic property on adapter'),
};
}
}