Passed
Pull Request — master (#31)
by Wilmer
12:52
created

Connection::getSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
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\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
13
use function constant;
14
use function strncmp;
15
use function substr;
16
17
/**
18
 * Database connection class prefilled for MYSQL Server.
19
 */
20
final class Connection extends AbstractConnection
21
{
22
    private ?Schema $schema = null;
23
24
    /**
25
     * Creates a command for execution.
26
     *
27
     * @param string|null $sql the SQL statement to be executed
28
     * @param array $params the parameters to be bound to the SQL statement
29
     *
30
     * @return Command the DB command
31
     * @throws Exception
32
     * @throws InvalidConfigException
33
     * @throws NotSupportedException
34
     */
35 170
    public function createCommand(?string $sql = null, array $params = []): Command
36
    {
37 170
        if ($sql !== null) {
38 170
            $sql = $this->quoteSql($sql);
39
        }
40
41 170
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
42
43 170
        return $command->bindValues($params);
44
    }
45
46
    /**
47
     * Returns the schema information for the database opened by this connection.
48
     *
49
     * @return Schema the schema information for the database opened by this connection.
50
     */
51 295
    public function getSchema(): Schema
52
    {
53 295
        if ($this->schema !== null) {
54 154
            return $this->schema;
55
        }
56
57 295
        return $this->schema = new Schema($this);
58
    }
59
60
    /**
61
     * Creates the PDO instance.
62
     *
63
     * This method is called by {@see open} to establish a DB connection. The default implementation will create a PHP
64
     * PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS.
65
     *
66
     * @return PDO the pdo instance
67
     */
68 181
    protected function createPdoInstance(): PDO
69
    {
70 181
        $dsn = $this->getDsn();
71
72 181
        if (strncmp('sqlite:@', $dsn, 8) === 0) {
73
            $dsn = 'sqlite:' . substr($dsn, 7);
74
        }
75
76 181
        return new PDO($dsn, $this->getUsername(), $this->getPassword(), $this->getAttributes());
77
    }
78
79
    /**
80
     * Initializes the DB connection.
81
     *
82
     * This method is invoked right after the DB connection is established.
83
     *
84
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
85
     *
86
     * 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 181
    protected function initConnection(): void
91
    {
92 181
        if ($this->getPDO() !== null) {
93 181
            $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
94
95 181
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
96
                $this->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
97
            }
98
        }
99 181
    }
100
101
    /**
102
     * Returns the name of the DB driver.
103
     *
104
     * @return string name of the DB driver
105
     */
106
    public function getDriverName(): string
107
    {
108
        return 'sqlite';
109
    }
110
}
111