Passed
Pull Request — dev (#128)
by Def
38:26 queued 25:50
created

ConnectionPDOPgsql::getDriverName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 205
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
27
    {
28 205
        $command = new CommandPDOPgsql($this, $this->queryCache);
29
30 205
        if ($sql !== null) {
31 205
            $command->setSql($sql);
32
        }
33
34 205
        if ($this->logger !== null) {
35 205
            $command->setLogger($this->logger);
36
        }
37
38 205
        if ($this->profiler !== null) {
39 205
            $command->setProfiler($this->profiler);
40
        }
41
42 205
        return $command->bindValues($params);
43
    }
44
45 11
    public function createTransaction(): TransactionInterface
46
    {
47 11
        return new TransactionPDOPgsql($this);
48
    }
49
50 5
    public function getLastInsertID(?string $sequenceName = null): string
51
    {
52 5
        if ($sequenceName === null) {
53 1
            throw new InvalidArgumentException('PgSQL not support lastInsertId without sequence name');
54
        }
55
56 4
        return parent::getLastInsertID($sequenceName);
57
    }
58
59
    /**
60
     * @throws Exception|InvalidConfigException
61
     */
62 418
    public function getQueryBuilder(): QueryBuilderInterface
63
    {
64 418
        if ($this->queryBuilder === null) {
65 418
            $this->queryBuilder = new QueryBuilderPDOPgsql(
66 418
                $this->getQuoter(),
67 418
                $this->getSchema(),
68
            );
69
        }
70
71 418
        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\Query\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
72
    }
73
74 431
    public function getQuoter(): QuoterInterface
75
    {
76 431
        if ($this->quoter === null) {
77 431
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
78
        }
79
80 431
        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...
81
    }
82
83 432
    public function getSchema(): SchemaInterface
84
    {
85 432
        if ($this->schema === null) {
86 432
            $this->schema = new Schema($this, $this->schemaCache);
87
        }
88
89 432
        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...
90
    }
91
92
    /**
93
     * Initializes the DB connection.
94
     *
95
     * This method is invoked right after the DB connection is established.
96
     *
97
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
98
     *
99
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
100
     *
101
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
102
     */
103 210
    protected function initConnection(): void
104
    {
105 210
        if ($this->getEmulatePrepare() !== null) {
106 2
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
107
        }
108
109 210
        $this->pdo = $this->driver->createConnection();
110
    }
111
}
112