Passed
Push — main ( 989106...9612b3 )
by Thierry
02:02
created

Connection::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Sqlite\Db\Sqlite;
4
5
use Lagdo\DbAdmin\Driver\Db\Connection as AbstractConnection;
6
use Lagdo\DbAdmin\Driver\Sqlite\Db\ConfigTrait;
7
8
use SQLite3;
9
10
class Connection extends AbstractConnection
11
{
12
    use ConfigTrait;
13
14
    public function multiQuery(string $query)
15
    {
16
        return $this->result = $this->query($query);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->result = $this->query($query) also could return the type Lagdo\DbAdmin\Driver\Sqlite\Db\Sqlite\Statement which is incompatible with the return type mandated by Lagdo\DbAdmin\Driver\Db\...Interface::multiQuery() of boolean.
Loading history...
17
    }
18
19
    public function nextResult()
20
    {
21
        return false;
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function open(string $database, string $schema = '')
28
    {
29
        $options = $this->driver->options();
30
        $filename = $this->filename($database, $options);
31
        $this->client = new SQLite3($filename);
32
        $this->query("PRAGMA foreign_keys = 1");
33
        return true;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function serverInfo()
40
    {
41
        $version = SQLite3::version();
42
        return $version["versionString"];
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function query(string $query, bool $unbuffered = false)
49
    {
50
        $result = @$this->client->query($query);
51
        $this->driver->setError();
52
        if (!$result) {
53
            $this->driver->setErrno($this->client->lastErrorCode());
54
            $this->driver->setError($this->client->lastErrorMsg());
55
            return false;
56
        } elseif ($result->numColumns()) {
57
            return new Statement($result);
58
        }
59
        $this->driver->setAffectedRows($this->client->changes());
60
        return true;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function quote(string $string)
67
    {
68
        return ($this->util->isUtf8($string) ?
69
            "'" . $this->client->escapeString($string) . "'" :
70
            "x'" . reset(unpack('H*', $string)) . "'");
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function storedResult()
77
    {
78
        return $this->result;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function result(string $query, int $field = -1)
85
    {
86
        if ($field === -1) {
87
            $field = $this->defaultField();
88
        }
89
        $result = $this->query($query);
90
        if (!is_object($result)) {
91
            return false;
92
        }
93
        $row = $result->fetchRow();
94
        return is_array($row) && array_key_exists($field, $row) ? $row[$field] : false;
95
    }
96
}
97