Passed
Push — master ( 244aa0...c1114e )
by Wilmer
27:09 queued 12:07
created

Connection   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 createPdoInstance() 0 3 1
A getSchema() 0 7 2
A createCommand() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
6
7
use PDO;
8
use Yiisoft\Db\Connection\Connection as AbstractConnection;
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
14
/**
15
 * Database connection class prefilled for PGSQL Server.
16
 */
17
final class Connection extends AbstractConnection
18
{
19
    private ?Schema $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 InvalidConfigException
28
     * @throws Exception
29
     * @throws NotSupportedException
30
     *
31
     * @return Command the DB command
32
     */
33
    public function createCommand(?string $sql = null, array $params = []): Command
34
    {
35
        if ($sql !== null) {
36
            $sql = $this->quoteSql($sql);
37
        }
38
39
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
0 ignored issues
show
Bug introduced by
The method getProfiler() does not exist on Yiisoft\Db\Pgsql\Connection. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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