Test Failed
Pull Request — dev (#126)
by Def
20:40 queued 07:50
created

ConnectionPDOPgsql::getQueryBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
ccs 4
cts 4
cp 1
crap 2
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 getLastInsertID(?string $sequenceName = null): string
51
    {
52 11
        if ($sequenceName === null) {
53
            throw new InvalidArgumentException('PgSQL not support lastInsertId without sequence name');
54
        }
55 5
56
        return parent::getLastInsertID($sequenceName);
57 5
    }
58 1
59
    /**
60
     * @throws Exception|InvalidConfigException
61 4
     */
62
    public function getQueryBuilder(): QueryBuilderInterface
63
    {
64
        if ($this->queryBuilder === null) {
65
            $this->queryBuilder = new QueryBuilderPDOPgsql(
66
                $this->getQuoter(),
67 414
                $this->getSchema(),
68
            );
69 414
        }
70 414
71 414
        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 414
    }
73
74
    public function getQuoter(): QuoterInterface
75
    {
76 414
        if ($this->quoter === null) {
77
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
78
        }
79 427
80
        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 427
    }
82 427
83
    public function getSchema(): SchemaInterface
84
    {
85 427
        if ($this->schema === null) {
86
            $this->schema = new Schema($this, $this->schemaCache);
87
        }
88 428
89
        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 428
    }
91 428
92
    /**
93
     * Initializes the DB connection.
94 428
     *
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
    protected function initConnection(): void
104
    {
105
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
106
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
107
        }
108 208
109
        $this->pdo = $this->driver->createConnection();
110 208
    }
111
}
112