Passed
Push — master ( 21771a...749afb )
by Wilmer
18:31 queued 03:34
created

Connection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 16
dl 0
loc 77
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommand() 0 9 2
A initConnection() 0 7 4
A getSchema() 0 7 2
A createPdoInstance() 0 9 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
    public function createCommand(?string $sql = null, array $params = []): Command
36
    {
37
        if ($sql !== null) {
38
            $sql = $this->quoteSql($sql);
39
        }
40
41
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
0 ignored issues
show
Bug introduced by
The method getProfiler() does not exist on Yiisoft\Db\Sqlite\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $command = new Command($this->/** @scrutinizer ignore-call */ getProfiler(), $this->getLogger(), $this, $sql);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getLogger() does not exist on Yiisoft\Db\Sqlite\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $command = new Command($this->getProfiler(), $this->/** @scrutinizer ignore-call */ getLogger(), $this, $sql);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43
        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
    public function getSchema(): Schema
52
    {
53
        if ($this->schema !== null) {
54
            return $this->schema;
55
        }
56
57
        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
    protected function createPdoInstance(): PDO
69
    {
70
        $dsn = $this->getDsn();
71
72
        if (strncmp('sqlite:@', $dsn, 8) === 0) {
73
            $dsn = 'sqlite:' . substr($dsn, 7);
74
        }
75
76
        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
    protected function initConnection(): void
91
    {
92
        if ($this->getPDO() !== null) {
93
            $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
94
95
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
0 ignored issues
show
Bug introduced by
The method getEmulatePrepare() does not exist on Yiisoft\Db\Sqlite\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
            if ($this->/** @scrutinizer ignore-call */ getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
                $this->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
97
            }
98
        }
99
    }
100
}
101