Passed
Pull Request — master (#80)
by Wilmer
02:46
created

Connection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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
use Yiisoft\Db\Cache\QueryCache;
11
use Yiisoft\Db\Cache\SchemaCache;
12
13
/**
14
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
15
 */
16
final class Connection extends AbstractConnection
17
{
18
    private QueryCache $queryCache;
19
    private SchemaCache $schemaCache;
20
21 465
    public function __construct(string $dsn, QueryCache $queryCache, SchemaCache $schemaCache)
22
    {
23 465
        $this->queryCache = $queryCache;
24 465
        $this->schemaCache = $schemaCache;
25
26 465
        parent::__construct($dsn, $queryCache);
27 465
    }
28
29
    /**
30
     * Creates a command for execution.
31
     *
32
     * @param string|null $sql the SQL statement to be executed
33
     * @param array $params the parameters to be bound to the SQL statement
34
     *
35
     * @return Command the DB command
36
     */
37 195
    public function createCommand(string $sql = null, array $params = []): Command
38
    {
39 195
        if ($sql !== null) {
40 195
            $sql = $this->quoteSql($sql);
41
        }
42
43 195
        $command = new Command($this, $this->queryCache, $sql);
44
45 195
        if ($this->logger !== null) {
46 195
            $command->setLogger($this->logger);
47
        }
48
49 195
        if ($this->profiler !== null) {
50 195
            $command->setProfiler($this->profiler);
51
        }
52
53 195
        return $command->bindValues($params);
54
    }
55
56
    /**
57
     * Returns the schema information for the database opened by this connection.
58
     *
59
     * @return Schema the schema information for the database opened by this connection.
60
     */
61 378
    public function getSchema(): Schema
62
    {
63 378
        return new Schema($this, $this->schemaCache);
64
    }
65
66
    /**
67
     * Creates the PDO instance.
68
     *
69
     * This method is called by {@see open} to establish a DB connection. The default implementation will create a PHP
70
     * PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS.
71
     *
72
     * @return PDO the pdo instance
73
     */
74 206
    protected function createPdoInstance(): PDO
75
    {
76 206
        return new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
77
    }
78
79
    /**
80
     * Initializes the DB connection.
81
     *
82
     * This method is invoked right after the DB connection is established.
83
     *
84
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
85
     *
86
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
87
     *
88
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
89
     */
90 206
    protected function initConnection(): void
91
    {
92 206
        $pdo = $this->getPDO();
93
94 206
        if ($pdo !== null) {
95 206
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
96
97 206
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
98 1
                $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
99
            }
100
101 206
            $charset = $this->getCharset();
102
103 206
            if ($charset !== null) {
104 206
                $pdo->exec('SET NAMES ' . $pdo->quote($charset));
105
            }
106
        }
107 206
    }
108
109
    /**
110
     * Returns the name of the DB driver.
111
     *
112
     * @return string name of the DB driver
113
     */
114 11
    public function getDriverName(): string
115
    {
116 11
        return 'pgsql';
117
    }
118
}
119