Passed
Push — master ( b1a1af...f07e26 )
by Alexander
25:59 queued 19:47
created

Connection::createPdoInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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 387
    public function __construct(string $dsn, QueryCache $queryCache, SchemaCache $schemaCache)
25
    {
26 387
        $this->queryCache = $queryCache;
27 387
        $this->schemaCache = $schemaCache;
28
29 387
        parent::__construct($dsn, $queryCache);
30 387
    }
31
32
    /**
33
     * Creates a command for execution.
34
     *
35
     * @param string|null $sql the SQL statement to be executed
36
     * @param array $params the parameters to be bound to the SQL statement
37
     *
38
     * @return Command the DB command
39
     */
40 177
    public function createCommand(string $sql = null, array $params = []): Command
41
    {
42 177
        if ($sql !== null) {
43 177
            $sql = $this->quoteSql($sql);
44
        }
45
46 177
        $command = new Command($this, $this->queryCache, $sql);
47
48 177
        if ($this->logger !== null) {
49 177
            $command->setLogger($this->logger);
50
        }
51
52 177
        if ($this->profiler !== null) {
53 177
            $command->setProfiler($this->profiler);
54
        }
55
56 177
        return $command->bindValues($params);
57
    }
58
59
    /**
60
     * Returns the schema information for the database opened by this connection.
61
     *
62
     * @return Schema the schema information for the database opened by this connection.
63
     */
64 308
    public function getSchema(): Schema
65
    {
66 308
        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 188
    protected function createPdoInstance(): PDO
78
    {
79 188
        $dsn = $this->getDsn();
80
81 188
        if (strncmp('sqlite:@', $dsn, 8) === 0) {
82
            $dsn = 'sqlite:' . substr($dsn, 7);
83
        }
84
85 188
        return new PDO($dsn, $this->getUsername(), $this->getPassword(), $this->getAttributes());
86
    }
87
88
    /**
89
     * 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
     *
97
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
98
     */
99 188
    protected function initConnection(): void
100
    {
101 188
        $pdo = $this->getPDO();
102
103 188
        if ($pdo !== null) {
104 188
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
105
106 188
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
107
                $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
108
            }
109
        }
110 188
    }
111
112
    /**
113
     * Returns the name of the DB driver.
114
     *
115
     * @return string name of the DB driver
116
     */
117 9
    public function getDriverName(): string
118
    {
119 9
        return 'sqlite';
120
    }
121
}
122