-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathOracleDriver.php
135 lines (114 loc) · 2.93 KB
/
OracleDriver.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace Cycle\Database\Driver\Oracle;
use Cycle\Database\Driver\Driver;
use Cycle\Database\Exception\StatementException;
use Cycle\Database\Query\QueryBuilder;
use Throwable;
class OracleDriver extends Driver
{
/**
* Default public schema name for all postgres connections.
*
* @var non-empty-string
*/
public const PUBLIC_SCHEMA = 'SYSTEM';
/**
* Option key for all available postgres schema names.
*
* @var non-empty-string
*/
private const OPT_AVAILABLE_SCHEMAS = 'schema';
/**
* Schemas to search tables in
*
* @var string[]
* @psalm-var non-empty-array<non-empty-string>
*/
private array $searchSchemas = [];
/**
* @param array $options
*/
public function __construct(array $options)
{
parent::__construct(
$options,
new OracleHandler(),
new OracleCompiler('""'),
QueryBuilder::defaultBuilder()
);
$this->defineSchemas($this->options);
}
/**
* @inheritDoc
*/
public function getType(): string
{
return 'Oracle';
}
/**
* @inheritDoc
*/
public function getSource(): string
{
// remove "oci:"
return substr($this->getDSN(), 4);
}
/**
* Schemas to search tables in
*
* @return string[]
*/
public function getSearchSchemas(): array
{
return $this->searchSchemas;
}
/**
* Check if schemas are defined
*
* @return bool
*/
public function shouldUseDefinedSchemas(): bool
{
return $this->searchSchemas !== [];
}
/**
* Parse the table name and extract the schema and table.
*
* @param string $name
* @return string[]
*/
public function parseSchemaAndTable(string $name): array
{
$schema = null;
$table = $name;
if (str_contains($name, '.')) {
[$schema, $table] = explode('.', $name, 2);
if ($schema === '$user') {
$schema = $this->options['username'];
}
}
return [$schema ?? $this->searchSchemas[0], $table];
}
/**
* @inheritDoc
*/
protected function mapException(Throwable $exception, string $query): StatementException
{
if ((int)$exception->getCode() === 23000) {
return new StatementException\ConstrainException($exception, $query);
}
return new StatementException($exception, $query);
}
/**
* Define schemas from config
*/
private function defineSchemas(array $options): void
{
$options[self::OPT_AVAILABLE_SCHEMAS] = (array)($options[self::OPT_AVAILABLE_SCHEMAS] ?? []);
$defaultSchema = static::PUBLIC_SCHEMA;
$this->searchSchemas = array_values(array_unique(
[$defaultSchema, ...$options[self::OPT_AVAILABLE_SCHEMAS]]
));
}
}