1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle\PDO; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use Yiisoft\Db\Driver\PDO\CommandPDOInterface; |
9
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDO; |
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
11
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
12
|
|
|
use Yiisoft\Db\Exception\InvalidCallException; |
13
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
14
|
|
|
use Yiisoft\Db\Oracle\Quoter; |
15
|
|
|
use Yiisoft\Db\Oracle\Schema; |
16
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
17
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
18
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
19
|
|
|
use Yiisoft\Db\Transaction\TransactionInterface; |
20
|
|
|
|
21
|
|
|
use function constant; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Database connection class prefilled for Oracle SQL Server. |
25
|
|
|
* The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php). |
26
|
|
|
*/ |
27
|
|
|
final class ConnectionPDOOracle extends ConnectionPDO |
28
|
|
|
{ |
29
|
182 |
|
public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface |
30
|
|
|
{ |
31
|
182 |
|
$command = new CommandPDOOracle($this, $this->queryCache); |
32
|
|
|
|
33
|
182 |
|
if ($sql !== null) { |
34
|
182 |
|
$command->setSql($sql); |
35
|
|
|
} |
36
|
|
|
|
37
|
182 |
|
if ($this->logger !== null) { |
38
|
182 |
|
$command->setLogger($this->logger); |
39
|
|
|
} |
40
|
|
|
|
41
|
182 |
|
if ($this->profiler !== null) { |
42
|
182 |
|
$command->setProfiler($this->profiler); |
43
|
|
|
} |
44
|
|
|
|
45
|
182 |
|
return $command->bindValues($params); |
46
|
|
|
} |
47
|
|
|
|
48
|
11 |
|
public function createTransaction(): TransactionInterface |
49
|
|
|
{ |
50
|
11 |
|
return new TransactionPDOOracle($this); |
51
|
|
|
} |
52
|
|
|
|
53
|
7 |
|
public function getDriverName(): string |
54
|
|
|
{ |
55
|
7 |
|
return 'oci'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Override base behaviour |
60
|
|
|
*/ |
61
|
5 |
|
public function getLastInsertID(string $sequenceName = null): string |
62
|
|
|
{ |
63
|
5 |
|
if ($sequenceName === null) { |
64
|
1 |
|
throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name'); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
if ($this->isActive()) { |
68
|
|
|
/* get the last insert id from connection */ |
69
|
3 |
|
$sequenceName = $this->getQuoter()->quoteSimpleTableName($sequenceName); |
70
|
|
|
|
71
|
3 |
|
return (string) $this->createCommand("SELECT $sequenceName.CURRVAL FROM DUAL")->queryScalar(); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
throw new InvalidCallException('DB Connection is not active.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws Exception|InvalidConfigException |
79
|
|
|
*/ |
80
|
344 |
|
public function getQueryBuilder(): QueryBuilderInterface |
81
|
|
|
{ |
82
|
344 |
|
if ($this->queryBuilder === null) { |
83
|
344 |
|
$this->queryBuilder = new QueryBuilderPDOOracle( |
84
|
344 |
|
$this->getQuoter(), |
85
|
344 |
|
$this->getSchema(), |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
344 |
|
return $this->queryBuilder; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
357 |
|
public function getQuoter(): QuoterInterface |
93
|
|
|
{ |
94
|
357 |
|
if ($this->quoter === null) { |
95
|
357 |
|
$this->quoter = new Quoter('"', '"', $this->getTablePrefix()); |
96
|
|
|
} |
97
|
|
|
|
98
|
357 |
|
return $this->quoter; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
358 |
|
public function getSchema(): SchemaInterface |
102
|
|
|
{ |
103
|
358 |
|
if ($this->schema === null) { |
104
|
358 |
|
$this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername())); |
105
|
|
|
} |
106
|
|
|
|
107
|
358 |
|
return $this->schema; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Initializes the DB connection. |
112
|
|
|
* |
113
|
|
|
* This method is invoked right after the DB connection is established. |
114
|
|
|
* |
115
|
|
|
* The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`. |
116
|
|
|
* |
117
|
|
|
* if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty. |
118
|
|
|
* |
119
|
|
|
* It then triggers an {@see EVENT_AFTER_OPEN} event. |
120
|
|
|
*/ |
121
|
185 |
|
protected function initConnection(): void |
122
|
|
|
{ |
123
|
185 |
|
$this->pdo = $this->driver->createConnection(); |
124
|
185 |
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
125
|
|
|
|
126
|
185 |
|
if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) { |
127
|
|
|
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare()); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|