Passed
Pull Request — master (#72)
by Wilmer
08:40 queued 06:34
created

Connection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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