Passed
Push — master ( 2a9aa3...e9c192 )
by Alexander
01:53
created

Connection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 80
ccs 17
cts 19
cp 0.8947
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initConnection() 0 11 5
A getDriverName() 0 3 1
A createPdoInstance() 0 3 1
A getSchema() 0 3 1
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
11
/**
12
 * Database connection class prefilled for PGSQL Server.
13
 */
14
final class Connection extends AbstractConnection
15
{
16
    private ?Schema $schema = null;
17
18
    /**
19
     * Creates a command for execution.
20
     *
21
     * @param string|null $sql the SQL statement to be executed
22
     * @param array $params the parameters to be bound to the SQL statement
23
     *
24
     * @return Command the DB command
25
     */
26 190
    public function createCommand(?string $sql = null, array $params = []): Command
27
    {
28 190
        if ($sql !== null) {
29 190
            $sql = $this->quoteSql($sql);
30
        }
31
32 190
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
33
34 190
        return $command->bindValues($params);
35
    }
36
37
    /**
38
     * Returns the schema information for the database opened by this connection.
39
     *
40
     * @return Schema the schema information for the database opened by this connection.
41
     */
42 370
    public function getSchema(): Schema
43
    {
44 370
        return $this->schema ?? ($this->schema = new Schema($this));
45
    }
46
47
    /**
48
     * Creates the PDO instance.
49
     *
50
     * This method is called by {@see open} to establish a DB connection. The default implementation will create a PHP
51
     * PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS.
52
     *
53
     * @return PDO the pdo instance
54
     */
55 200
    protected function createPdoInstance(): PDO
56
    {
57 200
        return new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
58
    }
59
60
    /**
61
     * Initializes the DB connection.
62
     *
63
     * This method is invoked right after the DB connection is established.
64
     *
65
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
66
     *
67
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
68
     *
69
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
70
     */
71 200
    protected function initConnection(): void
72
    {
73 200
        if ($this->getPDO() !== null) {
74 200
            $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
75
76 200
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
77
                $this->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
78
            }
79
80 200
            if ($this->getCharset() !== null) {
81
                $this->getPDO()->exec('SET NAMES ' . $this->getPDO()->quote($this->getCharset()));
82
            }
83
        }
84 200
    }
85
86
    /**
87
     * Returns the name of the DB driver.
88
     *
89
     * @return string name of the DB driver
90
     */
91 9
    public function getDriverName(): string
92
    {
93 9
        return 'pgsql';
94
    }
95
}
96