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 Exception; |
||
9 | use SQLite3; |
||
10 | |||
11 | use function preg_match; |
||
12 | use function is_array; |
||
13 | use function is_object; |
||
14 | use function count; |
||
15 | use function unpack; |
||
16 | use function reset; |
||
17 | |||
18 | class Connection extends AbstractConnection |
||
19 | { |
||
20 | use ConfigTrait; |
||
21 | |||
22 | /** |
||
23 | * @inheritDoc |
||
24 | */ |
||
25 | public function open(string $database, string $schema = '') |
||
26 | { |
||
27 | $options = $this->driver->options(); |
||
28 | $filename = $this->filename($database, $options); |
||
29 | $flags = $schema === '__create__' ? SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE : SQLITE3_OPEN_READWRITE; |
||
30 | try { |
||
31 | $this->client = new SQLite3($filename, $flags); |
||
32 | } catch (Exception $ex) { |
||
33 | $this->driver->setError($ex->getMessage()); |
||
34 | return false; |
||
35 | } |
||
36 | $this->query("PRAGMA foreign_keys = 1"); |
||
37 | return true; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @inheritDoc |
||
42 | */ |
||
43 | public function serverInfo() |
||
44 | { |
||
45 | $version = SQLite3::version(); |
||
46 | return $version["versionString"]; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @inheritDoc |
||
51 | */ |
||
52 | public function query(string $query, bool $unbuffered = false) |
||
53 | { |
||
54 | $space = $this->spaceRegex(); |
||
55 | if (preg_match("~^$space*+ATTACH\\b~i", $query, $match)) { |
||
56 | // PHP doesn't support setting SQLITE_LIMIT_ATTACHED |
||
57 | $this->driver->setError($this->utils->trans->lang('ATTACH queries are not supported.')); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
58 | return false; |
||
59 | } |
||
60 | |||
61 | $result = @$this->client->query($query); |
||
62 | $this->driver->setError(); |
||
63 | if (!$result) { |
||
64 | $this->driver->setErrno($this->client->lastErrorCode()); |
||
65 | $this->driver->setError($this->client->lastErrorMsg()); |
||
66 | return false; |
||
67 | } elseif ($result->numColumns() > 0) { |
||
68 | return new Statement($result); |
||
69 | } |
||
70 | $this->setAffectedRows($this->client->changes()); |
||
71 | return true; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @inheritDoc |
||
76 | */ |
||
77 | public function quote(string $string) |
||
78 | { |
||
79 | if ($this->utils->str->isUtf8($string) || !is_array($unpacked = unpack('H*', $string))) { |
||
0 ignored issues
–
show
|
|||
80 | return "'" . $this->client->escapeString($string) . "'"; |
||
81 | } |
||
82 | return "x'" . reset($unpacked) . "'"; |
||
83 | } |
||
84 | |||
85 | public function multiQuery(string $query) |
||
86 | { |
||
87 | $this->statement = $this->driver->execute($query); |
||
88 | return $this->statement !== false; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | */ |
||
94 | public function storedResult() |
||
95 | { |
||
96 | return $this->statement; |
||
97 | } |
||
98 | |||
99 | public function nextResult() |
||
100 | { |
||
101 | return false; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @inheritDoc |
||
106 | */ |
||
107 | public function result(string $query, int $field = -1) |
||
108 | { |
||
109 | if ($field < 0) { |
||
110 | $field = $this->defaultField(); |
||
111 | } |
||
112 | $result = $this->driver->execute($query); |
||
113 | if (!is_object($result)) { |
||
114 | return null; |
||
115 | } |
||
116 | $row = $result->fetchRow(); |
||
117 | return is_array($row) && count($row) > $field ? $row[$field] : null; |
||
118 | } |
||
119 | } |
||
120 |