Passed
Push — dev ( 65e869...962df0 )
by Def
08:15 queued 05:32
created

ConnectionPDOPgsql   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 97.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
c 2
b 0
f 0
dl 0
loc 100
ccs 39
cts 40
cp 0.975
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchema() 0 7 2
A initConnection() 0 16 5
A getQuoter() 0 7 2
A getDriverName() 0 3 1
A createCommand() 0 17 4
A createTransaction() 0 3 1
A getLastInsertID() 0 7 2
A getQueryBuilder() 0 10 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 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;
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...
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;
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...
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;
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...
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);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        $this->pdo->/** @scrutinizer ignore-call */ 
115
                    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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