Test Failed
Pull Request — master (#180)
by Def
13:05 queued 10:26
created

ConnectionPDO::initConnection()   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 0
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 254
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
26
    {
27 254
        $command = new CommandPDO($this, $this->queryCache);
28
29 254
        if ($sql !== null) {
30 232
            $command->setSql($sql);
31
        }
32
33 254
        if ($this->logger !== null) {
34 109
            $command->setLogger($this->logger);
35
        }
36
37 254
        if ($this->profiler !== null) {
38 109
            $command->setProfiler($this->profiler);
39
        }
40
41 254
        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 515
    public function getQueryBuilder(): QueryBuilderInterface
62
    {
63 515
        if ($this->queryBuilder === null) {
64 515
            $this->queryBuilder = new QueryBuilder(
65 515
                $this->getQuoter(),
66 515
                $this->getSchema(),
67
            );
68
        }
69
70 515
        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 557
    public function getQuoter(): QuoterInterface
74
    {
75 557
        if ($this->quoter === null) {
76 557
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
77
        }
78
79 557
        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 535
    public function getSchema(): SchemaInterface
83
    {
84 535
        if ($this->schema === null) {
85 535
            $this->schema = new Schema($this, $this->schemaCache);
86
        }
87
88 535
        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