Passed
Pull Request — master (#32)
by Wilmer
13:07
created

PgsqlConnection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initConnection() 0 11 5
A createCommand() 0 9 2
A createPdoInstance() 0 3 1
A getSchema() 0 7 2
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\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
use Yiisoft\Db\Pgsql\Schema\PgsqlSchema;
14
15
/**
16
 * Database connection class prefilled for PGSQL Server.
17
 */
18
final class PgsqlConnection extends Connection
19
{
20
    private ?PgsqlSchema $schema = null;
21
22
    /**
23
     * Creates a command for execution.
24
     *
25
     * @param string|null $sql the SQL statement to be executed
26
     * @param array $params the parameters to be bound to the SQL statement
27
     *
28
     * @throws InvalidConfigException
29
     * @throws Exception
30
     * @throws NotSupportedException
31
     *
32
     * @return Command the DB command
33
     */
34
    public function createCommand(?string $sql = null, $params = []): Command
35
    {
36
        if ($sql !== null) {
37
            $sql = $this->quoteSql($sql);
38
        }
39
40
        $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

40
        $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

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

88
            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...
89
                $this->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
90
            }
91
92
            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

92
            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...
93
                $this->getPDO()->exec('SET NAMES ' . $this->getPDO()->quote($this->getCharset()));
94
            }
95
        }
96
    }
97
}
98