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