Passed
Pull Request — master (#80)
by Wilmer
14:36 queued 55s
created

Connection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    public function __construct(string $dsn, QueryCache $queryCache, SchemaCache $schemaCache)
22
    {
23
        $this->queryCache = $queryCache;
24 193
        $this->schemaCache = $schemaCache;
25
26 193
        parent::__construct($dsn, $queryCache);
27 193
    }
28
29
    /**
30 193
     * Creates a command for execution.
31
     *
32 193
     * @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
    public function createCommand(string $sql = null, array $params = []): Command
38
    {
39
        if ($sql !== null) {
40 373
            $sql = $this->quoteSql($sql);
41
        }
42 373
43
        $command = new Command($this, $this->queryCache, $sql);
44
45
        if ($this->logger !== null) {
46
            $command->setLogger($this->logger);
47
        }
48
49
        if ($this->profiler !== null) {
50
            $command->setProfiler($this->profiler);
51
        }
52
53 204
        return $command->bindValues($params);
54
    }
55 204
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
    public function getSchema(): Schema
62
    {
63
        return new Schema($this, $this->schemaCache);
64
    }
65
66
    /**
67
     * Creates the PDO instance.
68
     *
69 204
     * 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 204
     *
72
     * @return PDO the pdo instance
73 204
     */
74 204
    protected function createPdoInstance(): PDO
75
    {
76 204
        return new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
77 1
    }
78
79
    /**
80 204
     * Initializes the DB connection.
81
     *
82 204
     * This method is invoked right after the DB connection is established.
83 203
     *
84
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
85
     *
86 204
     * 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
    protected function initConnection(): void
91
    {
92
        $pdo = $this->getPDO();
93 11
94
        if ($pdo !== null) {
95 11
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
96
97
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
98
                $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
99
            }
100
101
            $charset = $this->getCharset();
102
103
            if ($charset !== null) {
104
                $pdo->exec('SET NAMES ' . $pdo->quote($charset));
105
            }
106
        }
107
    }
108
109
    /**
110
     * Returns the name of the DB driver.
111
     *
112
     * @return string name of the DB driver
113
     */
114
    public function getDriverName(): string
115
    {
116
        return 'pgsql';
117
    }
118
}
119