Passed
Pull Request — master (#32)
by Wilmer
11:57
created

PgsqlConnection::getSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Connection;
6
7
use PDO;
8
use Yiisoft\Db\Connection\Connection;
9
use Yiisoft\Db\Command\Command;
10
use Yiisoft\Db\Pgsql\Schema\PgsqlSchema;
11
12
use function in_array;
13
14
/**
15
 * Database connection class prefilled for PGSQL Server.
16
 */
17
final class PgsqlConnection extends Connection
18
{
19
    private ?PgsqlSchema $schema = null;
20
21
    /**
22
     * Creates a command for execution.
23
     *
24
     * @param string|null $sql the SQL statement to be executed
25
     * @param array $params the parameters to be bound to the SQL statement
26
     *
27
     * @throws Exception
28
     * @throws InvalidConfigException
29
     *
30
     * @return Command the DB command
31
     */
32
    public function createCommand(?string $sql = null, $params = []): Command
33
    {
34
        if ($sql !== null) {
35
            $sql = $this->quoteSql($sql);
36
        }
37
38
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
0 ignored issues
show
Bug introduced by
The method getLogger() does not exist on Yiisoft\Db\Pgsql\Connection\PgsqlConnection. ( Ignorable by Annotation )

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

38
        $command = new Command($this->getProfiler(), $this->/** @scrutinizer ignore-call */ getLogger(), $this, $sql);

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...
Bug introduced by
The method getProfiler() does not exist on Yiisoft\Db\Pgsql\Connection\PgsqlConnection. ( Ignorable by Annotation )

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

38
        $command = new Command($this->/** @scrutinizer ignore-call */ getProfiler(), $this->getLogger(), $this, $sql);

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...
39
40
        return $command->bindValues($params);
41
    }
42
43
    /**
44
     * Returns the schema information for the database opened by this connection.
45
     *
46
     * @return PgsqlSchema the schema information for the database opened by this connection.
47
     */
48
    public function getSchema(): PgsqlSchema
49
    {
50
        if ($this->schema !== null) {
51
            return $this->schema;
52
        }
53
54
        return $this->schema = new PgsqlSchema($this);
55
    }
56
57
    /**
58
     * Creates the PDO instance.
59
     *
60
     * This method is called by {@see open} to establish a DB connection. The default implementation will create a PHP
61
     * PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS.
62
     *
63
     * @return PDO the pdo instance
64
     */
65
    protected function createPdoInstance(): \PDO
66
    {
67
        return new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
68
    }
69
70
    /**
71
     * Initializes the DB connection.
72
     *
73
     * This method is invoked right after the DB connection is established.
74
     *
75
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
76
     *
77
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
78
     *
79
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
80
     */
81
    protected function initConnection(): void
82
    {
83
        $this->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
84
85
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
0 ignored issues
show
Bug introduced by
The method getEmulatePrepare() does not exist on Yiisoft\Db\Pgsql\Connection\PgsqlConnection. ( Ignorable by Annotation )

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

85
        if ($this->/** @scrutinizer ignore-call */ getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {

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...
86
            $this->getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
87
        }
88
89
        if ($this->getCharset() !== null) {
0 ignored issues
show
Bug introduced by
The method getCharset() does not exist on Yiisoft\Db\Pgsql\Connection\PgsqlConnection. ( Ignorable by Annotation )

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

89
        if ($this->/** @scrutinizer ignore-call */ getCharset() !== null) {

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...
90
            $this->getPdo()->exec('SET NAMES ' . $this->getPdo()->quote($this->getCharset()));
91
        }
92
    }
93
}
94