Passed
Push — master ( 68da7d...f9cc09 )
by Wilmer
01:46
created

Connection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 88
ccs 19
cts 21
cp 0.9048
rs 10
wmc 11

5 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
A getDriverName() 0 3 1
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 182
    public function createCommand(?string $sql = null, array $params = []): Command
34
    {
35 182
        if ($sql !== null) {
36 182
            $sql = $this->quoteSql($sql);
37
        }
38
39 182
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
40
41 182
        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 362
    public function getSchema(): Schema
50
    {
51 362
        if ($this->schema !== null) {
52 239
            return $this->schema;
53
        }
54
55 362
        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 192
    protected function createPdoInstance(): PDO
67
    {
68 192
        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 192
    protected function initConnection(): void
83
    {
84 192
        if ($this->getPDO() !== null) {
85 192
            $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
86
87 192
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
88
                $this->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
89
            }
90
91 192
            if ($this->getCharset() !== null) {
92
                $this->getPDO()->exec('SET NAMES ' . $this->getPDO()->quote($this->getCharset()));
93
            }
94
        }
95 192
    }
96
97
    /**
98
     * Returns the name of the DB driver.
99
     *
100
     * @return string name of the DB driver
101
     */
102 9
    public function getDriverName(): string
103
    {
104 9
        return 'pgsql';
105
    }
106
}
107