/
OciDriver.php
141 lines (99 loc) 路 3.08 KB
/
OciDriver.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
136
137
138
139
140
141
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Database\Drivers;
use Nette;
/**
* Supplemental Oracle database driver.
*/
class OciDriver extends PdoDriver
{
/** Datetime format */
private string $fmtDateTime;
public function connect(string $dsn, ?string $user = null, ?string $password = null, ?array $options = null): void
{
parent::connect($dsn, $user, $password, $options);
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
}
public function detectExceptionClass(\PDOException $e): ?string
{
$code = $e->errorInfo[1] ?? null;
if (in_array($code, [1, 2299, 38911], true)) {
return Nette\Database\UniqueConstraintViolationException::class;
} elseif (in_array($code, [1400], true)) {
return Nette\Database\NotNullConstraintViolationException::class;
} elseif (in_array($code, [2266, 2291, 2292], true)) {
return Nette\Database\ForeignKeyConstraintViolationException::class;
} else {
return null;
}
}
/********************* SQL ****************d*g**/
public function delimite(string $name): string
{
// @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm
return '"' . str_replace('"', '""', $name) . '"';
}
public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format($this->fmtDateTime);
}
public function formatDateInterval(\DateInterval $value): string
{
throw new Nette\NotSupportedException;
}
public function formatLike(string $value, int $pos): string
{
throw new Nette\NotImplementedException;
}
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{
if ($limit < 0 || $offset < 0) {
throw new Nette\InvalidArgumentException('Negative offset or limit.');
} elseif ($offset) {
// see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
. ($limit !== null ? 'WHERE ROWNUM <= ' . ($offset + $limit) : '')
. ') WHERE "__rnum" > ' . $offset;
} elseif ($limit !== null) {
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . $limit;
}
}
/********************* reflection ****************d*g**/
public function getTables(): array
{
$tables = [];
foreach ($this->pdo->query('SELECT * FROM cat') as $row) {
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
$tables[] = [
'name' => $row[0],
'view' => $row[1] === 'VIEW',
];
}
}
return $tables;
}
public function getColumns(string $table): array
{
throw new Nette\NotImplementedException;
}
public function getIndexes(string $table): array
{
throw new Nette\NotImplementedException;
}
public function getForeignKeys(string $table): array
{
throw new Nette\NotImplementedException;
}
public function getColumnTypes(\PDOStatement $statement): array
{
return [];
}
public function isSupported(string $item): bool
{
return $item === self::SUPPORT_SEQUENCE || $item === self::SUPPORT_SUBSELECT;
}
}