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