Passed
Pull Request — dev (#114)
by Def
19:21 queued 16:50
created

ConnectionPDOPgsql::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
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
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\Command\CommandInterface;
9
use Yiisoft\Db\Connection\ConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidArgumentException;
12
use Yiisoft\Db\Exception\InvalidConfigException;
13
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 ConnectionPDOPgsql extends ConnectionPDO
24
{
25 406
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
26
    {
27 406
        $command = new CommandPDOPgsql($this, $this->queryCache);
28
29 406
        if ($sql !== null) {
30 196
            $command->setSql($sql);
31
        }
32
33 406
        if ($this->logger !== null) {
34 406
            $command->setLogger($this->logger);
35
        }
36
37 406
        if ($this->profiler !== null) {
38 406
            $command->setProfiler($this->profiler);
39
        }
40
41 406
        return $command->bindValues($params);
42
    }
43
44 8
    public function createTransaction(): TransactionInterface
45
    {
46 8
        return new TransactionPDOPgsql($this);
47
    }
48
49 11
    public function getDriverName(): string
50
    {
51 11
        return 'pgsql';
52
    }
53
54 5
    public function getLastInsertID(?string $sequenceName = null): string
55
    {
56 5
        if ($sequenceName === null) {
57 1
            throw new InvalidArgumentException('PgSQL not support lastInsertId without sequence name');
58
        }
59
60 4
        return parent::getLastInsertID($sequenceName);
61
    }
62
63
    /**
64
     * @throws Exception|InvalidConfigException
65
     */
66 406
    public function getQueryBuilder(): QueryBuilderInterface
67
    {
68 406
        if ($this->queryBuilder === null) {
69 406
            $this->queryBuilder = new QueryBuilderPDOPgsql(
70 406
                $this->createCommand(),
71 406
                $this->getQuoter(),
72 406
                $this->getSchema(),
73
            );
74
        }
75
76 406
        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 419
    public function getQuoter(): QuoterInterface
80
    {
81 419
        if ($this->quoter === null) {
82 419
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
83
        }
84
85 419
        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 420
    public function getSchema(): SchemaInterface
89
    {
90 420
        if ($this->schema === null) {
91 420
            $this->schema = new SchemaPDOPgsql($this, $this->schemaCache);
92
        }
93
94 420
        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 200
    protected function initConnection(): void
109
    {
110 200
        if ($this->pdo === null) {
111 200
            $this->pdo = $this->driver->createConnection();
112
        }
113
114 200
        $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 200
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
117 1
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
118
        }
119
120 200
        $charset = $this->driver->getCharset();
121
122 200
        if ($charset !== null) {
123
            $this->pdo->exec('SET NAMES ' . $this->pdo->quote($charset));
124
        }
125
    }
126
}
127