Passed
Push — dev ( 43c131...7ed35a )
by Def
24:05 queued 21:31
created

ConnectionPDO::getLastInsertID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 206
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
26
    {
27 206
        $command = new CommandPDO($this, $this->queryCache);
28
29 206
        if ($sql !== null) {
30 206
            $command->setSql($sql);
31
        }
32
33 206
        if ($this->logger !== null) {
34 206
            $command->setLogger($this->logger);
35
        }
36
37 206
        if ($this->profiler !== null) {
38 206
            $command->setProfiler($this->profiler);
39
        }
40
41 206
        return $command->bindValues($params);
42
    }
43
44 11
    public function createTransaction(): TransactionInterface
45
    {
46 11
        return new TransactionPDO($this);
47
    }
48
49 5
    public function getLastInsertID(?string $sequenceName = null): string
50
    {
51 5
        if ($sequenceName === null) {
52 1
            throw new InvalidArgumentException('PgSQL not support lastInsertId without sequence name');
53
        }
54
55 4
        return parent::getLastInsertID($sequenceName);
56
    }
57
58
    /**
59
     * @throws Exception|InvalidConfigException
60
     */
61 419
    public function getQueryBuilder(): QueryBuilderInterface
62
    {
63 419
        if ($this->queryBuilder === null) {
64 419
            $this->queryBuilder = new QueryBuilder(
65 419
                $this->getQuoter(),
66 419
                $this->getSchema(),
67
            );
68
        }
69
70 419
        return $this->queryBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryBuilder could return the type null which is incompatible with the type-hinted return Yiisoft\Db\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
71
    }
72
73 432
    public function getQuoter(): QuoterInterface
74
    {
75 432
        if ($this->quoter === null) {
76 432
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
77
        }
78
79 432
        return $this->quoter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoter could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\QuoterInterface. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82 433
    public function getSchema(): SchemaInterface
83
    {
84 433
        if ($this->schema === null) {
85 433
            $this->schema = new Schema($this, $this->schemaCache);
86
        }
87
88 433
        return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\SchemaInterface. Consider adding an additional type-check to rule them out.
Loading history...
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 211
    protected function initConnection(): void
103
    {
104 211
        if ($this->getEmulatePrepare() !== null) {
105 2
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
106
        }
107
108 211
        $this->pdo = $this->driver->createConnection();
109
    }
110
}
111