Passed
Push — master ( 03d3d0...cea9c0 )
by Alexander
07:36 queued 05:58
created

Connection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 82
ccs 21
cts 21
cp 1
rs 10
wmc 10

5 Methods

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