Passed
Push — master ( ea33ba...e9743d )
by Alexander
01:53
created

Connection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 15
c 2
b 0
f 0
dl 0
loc 82
ccs 18
cts 20
cp 0.9
rs 10

5 Methods

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