Repository navigation
Expand file tree
/
Copy pathPlatform.php
More file actions
175 lines (145 loc) · 5.68 KB
/
Copy pathPlatform.php
File metadata and controls
175 lines (145 loc) · 5.68 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
<?php
declare(strict_types=1);
namespace PhpDb\Sql\Platform;
use Override;
use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Adapter\StatementContainerInterface;
use PhpDb\Sql\Exception;
use PhpDb\Sql\PreparableSqlInterface;
use PhpDb\Sql\SqlInterface;
use function is_a;
use function str_replace;
use function strtolower;
class Platform extends AbstractPlatform
{
protected PlatformInterface $defaultPlatform;
protected ?string $cachedPlatformName = null;
/**
* @todo sat-migration
* We have removed the default behaviour of setting a decorator for the adapter's platform.
* $platformName = $this->resolvePlatformName($platform);
* $this->decorators[$platformName] = $this->defaultPlatform->getSqlPlatformDecorator();
*
* The migration of the adapters means checking the below:-
* $mySqlPlatform = new Mysql\Mysql();
* $sqlServerPlatform = new SqlServer\SqlServer();
* $oraclePlatform = new Oracle\Oracle();
* $ibmDb2Platform = new IbmDb2\IbmDb2();
* $sqlitePlatform = new Sqlite\Sqlite();
* $this->decorators['mysql'] = $mySqlPlatform->getDecorators();
* $this->decorators['sqlserver'] = $sqlServerPlatform->getDecorators();
* $this->decorators['oracle'] = $oraclePlatform->getDecorators();
* $this->decorators['ibmdb2'] = $ibmDb2Platform->getDecorators();
* $this->decorators['sqlite'] = $sqlitePlatform->getDecorators();
*/
public function __construct(PlatformInterface $platform)
{
$this->defaultPlatform = $platform;
}
#[Override]
public function getDecorators(): array
{
$platformName = $this->resolvePlatformName($this->getDefaultPlatform());
return $this->decorators[$platformName];
}
/**
* {@inheritDoc}
*
* @throws Exception\RuntimeException
*/
#[Override]
public function getSqlString(?PlatformInterface $adapterPlatform = null): string
{
if (! $this->subject instanceof SqlInterface) {
throw new Exception\RuntimeException(
'The subject does not appear to implement PhpDb\Sql\SqlInterface, thus calling '
. 'prepareStatement() has no effect',
);
}
$adapterPlatform = $this->resolvePlatform($adapterPlatform);
return $this->getTypeDecorator($this->subject, $adapterPlatform)->getSqlString($adapterPlatform);
}
#[Override]
public function getTypeDecorator(
PreparableSqlInterface|SqlInterface $subject,
AdapterInterface|PlatformInterface|null $adapterOrPlatform = null,
): PlatformDecoratorInterface|PreparableSqlInterface|SqlInterface {
$platformName = $this->resolvePlatformName($adapterOrPlatform);
if (! isset($this->decorators[$platformName])) {
return $subject;
}
$subjectClass = $subject::class;
if (isset($this->decorators[$platformName][$subjectClass])) {
$decorator = $this->decorators[$platformName][$subjectClass];
$decorator->setSubject($subject);
return $decorator;
}
/** @var PlatformDecoratorInterface $decorator */
foreach ($this->decorators[$platformName] as $type => $decorator) {
if (! ($subject instanceof $type && is_a($decorator, $type, true))) {
continue;
}
$decorator->setSubject($subject);
return $decorator;
}
return $subject;
}
/**
* {@inheritDoc}
*
* @throws Exception\RuntimeException
*/
#[Override]
public function prepareStatement(
AdapterInterface $adapter,
StatementContainerInterface $statementContainer,
): StatementContainerInterface {
if (! $this->subject instanceof PreparableSqlInterface) {
throw new Exception\RuntimeException(
'The subject does not appear to implement PhpDb\Sql\PreparableSqlInterface, thus calling '
. 'prepareStatement() has no effect',
);
}
$this->getTypeDecorator($this->subject, $adapter)->prepareStatement($adapter, $statementContainer);
return $statementContainer;
}
#[Override]
public function setTypeDecorator(
string $type,
PlatformDecoratorInterface $decorator,
AdapterInterface|PlatformInterface|null $adapterOrPlatform = null,
): void {
$platformName = $this->resolvePlatformName($adapterOrPlatform);
$this->decorators[$platformName][$type] = $decorator;
}
protected function getDefaultPlatform(): PlatformInterface
{
return $this->defaultPlatform;
}
/**
* @throws Exception\InvalidArgumentException
*/
protected function resolvePlatform(PlatformInterface|AdapterInterface|null $adapterOrPlatform): PlatformInterface
{
if (null === $adapterOrPlatform) {
return $this->getDefaultPlatform();
}
if ($adapterOrPlatform instanceof AdapterInterface) {
return $adapterOrPlatform->getPlatform();
}
return $adapterOrPlatform;
}
protected function resolvePlatformName(PlatformInterface|AdapterInterface|null $adapterOrPlatform): string
{
if (null === $adapterOrPlatform && null !== $this->cachedPlatformName) {
return $this->cachedPlatformName;
}
$platformName = $this->resolvePlatform($adapterOrPlatform)->getName();
$normalized = str_replace([' ', '_'], '', strtolower($platformName));
if (null === $adapterOrPlatform) {
$this->cachedPlatformName = $normalized;
}
return $normalized;
}
}