Passed
Push — master ( db954c...ebfaf5 )
by Wilmer
08:14
created

Connection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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