Rev 496 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
295 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
||
6 | * |
||
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
20 | class OIDplusDatabaseConnectionSQLite3 extends OIDplusDatabaseConnection { |
||
21 | private $conn = null; |
||
22 | private $prepare_cache = array(); |
||
23 | private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior |
||
24 | |||
348 | daniel-mar | 25 | public static function getPlugin(): OIDplusDatabasePlugin { |
26 | return new OIDplusDatabasePluginSQLite3(); |
||
27 | } |
||
28 | |||
295 | daniel-mar | 29 | public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
30 | $this->last_error = null; |
||
31 | if (is_null($prepared_args)) { |
||
32 | try { |
||
33 | $res = $this->conn->query($sql); |
||
34 | } catch (Exception $e) { |
||
35 | $res = false; |
||
36 | } |
||
37 | if ($res === false) { |
||
38 | $this->last_error = $this->conn->lastErrorMsg(); |
||
39 | throw new OIDplusSQLException($sql, $this->error()); |
||
40 | } else { |
||
41 | return new OIDplusQueryResultSQLite3($res); |
||
42 | } |
||
43 | } else { |
||
44 | if (!is_array($prepared_args)) { |
||
360 | daniel-mar | 45 | throw new OIDplusException(_L('"prepared_args" must be either NULL or an ARRAY.')); |
295 | daniel-mar | 46 | } |
47 | |||
48 | // convert ? ? ? to :param1 :param2 :param3 ... |
||
49 | $sql = preg_replace_callback('@\\?@', function($found) { |
||
50 | static $i = 0; |
||
51 | $i++; |
||
52 | return ':param'.$i; |
||
53 | }, $sql); |
||
54 | |||
55 | if (isset($this->prepare_cache[$sql])) { |
||
56 | $stmt = $this->prepare_cache[$sql]; |
||
57 | } else { |
||
58 | try { |
||
59 | $stmt = $this->conn->prepare($sql); |
||
60 | } catch (Exception $e) { |
||
61 | $stmt = false; |
||
62 | } |
||
63 | if ($stmt === false) { |
||
64 | $this->last_error = $this->conn->lastErrorMsg(); |
||
360 | daniel-mar | 65 | throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error()); |
295 | daniel-mar | 66 | } |
67 | $this->prepare_cache[$sql] = $stmt; |
||
68 | } |
||
69 | |||
70 | if ($stmt->paramCount() != count($prepared_args)) { |
||
360 | daniel-mar | 71 | throw new OIDplusException(_L('Prepared argument list size not matching number of prepared statement arguments')); |
295 | daniel-mar | 72 | } |
73 | $i = 1; |
||
74 | foreach ($prepared_args as &$value) { |
||
75 | if (is_bool($value)) $value = $value ? '1' : '0'; |
||
76 | $stmt->bindValue(':param'.$i, $value, SQLITE3_TEXT); |
||
77 | $i++; |
||
78 | } |
||
79 | |||
80 | try { |
||
81 | $ps = $stmt->execute(); |
||
82 | } catch (Exception $e) { |
||
83 | $ps = false; |
||
84 | } |
||
85 | if ($ps === false) { |
||
86 | $this->last_error = $this->conn->lastErrorMsg(); |
||
87 | throw new OIDplusSQLException($sql, $this->error()); |
||
88 | } |
||
89 | return new OIDplusQueryResultSQLite3($ps); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | public function insert_id(): int { |
||
94 | try { |
||
95 | // Note: This will always give results even for tables that do not |
||
96 | // have autoincrements, because SQLite3 assigns an "autoindex" for every table, |
||
97 | // e.g. the config table. Therefore, our testcase will fail. |
||
98 | return (int)$this->conn->lastInsertRowID(); |
||
99 | //return (int)$this->query('select last_insert_rowid() as id')->fetch_object()->id; |
||
100 | } catch (Exception $e) { |
||
101 | return 0; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | public function error(): string { |
||
106 | $err = $this->last_error; |
||
107 | if ($err == null) $err = ''; |
||
108 | return $err; |
||
109 | } |
||
110 | |||
111 | protected function doConnect()/*: void*/ { |
||
360 | daniel-mar | 112 | if (!class_exists('SQLite3')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','SQLite3')); |
295 | daniel-mar | 113 | |
114 | // Try connecting to the database |
||
115 | try { |
||
116 | $filename = OIDplus::baseConfig()->getValue('SQLITE3_FILE', 'userdata/database/oidplus.db'); |
||
117 | $flags = SQLITE3_OPEN_READWRITE/* | SQLITE3_OPEN_CREATE*/; |
||
118 | $encryption = OIDplus::baseConfig()->getValue('SQLITE3_ENCRYPTION', ''); |
||
119 | |||
120 | $is_absolute_path = ((substr($filename,0,1) == '/') || (substr($filename,1,1) == ':')); |
||
121 | if (!$is_absolute_path) { |
||
122 | // Filename must be absolute path, since OIDplus can be called from several locations (e.g. registration wizard) |
||
496 | daniel-mar | 123 | $filename = OIDplus::localpath().$filename; |
295 | daniel-mar | 124 | } |
125 | |||
126 | $this->conn = new SQLite3($filename, $flags, $encryption); |
||
127 | } catch (Exception $e) { |
||
360 | daniel-mar | 128 | throw new OIDplusConfigInitializationException(_L('Connection to the database failed!').' ' . $e->getMessage()); |
295 | daniel-mar | 129 | } |
130 | |||
131 | $this->conn->createCollation('NATURAL_CMP', 'strnatcmp'); // we need that for natSort() |
||
132 | $this->conn->enableExceptions(true); // Throw exceptions instead of PHP warnings |
||
133 | |||
134 | $this->prepare_cache = array(); |
||
135 | $this->last_error = null; |
||
136 | } |
||
137 | |||
138 | protected function doDisconnect()/*: void*/ { |
||
139 | $this->prepare_cache = array(); |
||
140 | $this->conn = null; |
||
141 | } |
||
142 | |||
143 | private $intransaction = false; |
||
144 | |||
145 | public function transaction_supported(): bool { |
||
146 | return true; |
||
147 | } |
||
148 | |||
149 | public function transaction_level(): int { |
||
150 | return $this->intransaction ? 1 : 0; |
||
151 | } |
||
152 | |||
153 | public function transaction_begin()/*: void*/ { |
||
360 | daniel-mar | 154 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
295 | daniel-mar | 155 | $this->query('begin transaction'); |
156 | $this->intransaction = true; |
||
157 | } |
||
158 | |||
159 | public function transaction_commit()/*: void*/ { |
||
160 | $this->query('commit'); |
||
161 | $this->intransaction = false; |
||
162 | } |
||
163 | |||
164 | public function transaction_rollback()/*: void*/ { |
||
165 | $this->query('rollback'); |
||
166 | $this->intransaction = false; |
||
167 | } |
||
168 | |||
169 | public function sqlDate(): string { |
||
170 | return 'datetime()'; |
||
171 | } |
||
172 | |||
173 | public function natOrder($fieldname, $order='asc'): string { |
||
174 | |||
175 | // This collation is defined in the database plugin using SQLite3::createCollation() |
||
176 | return "$fieldname COLLATE NATURAL_CMP $order"; |
||
177 | |||
178 | } |
||
316 | daniel-mar | 179 | |
502 | daniel-mar | 180 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
318 | daniel-mar | 181 | $slang = OIDplus::getSqlSlangPlugin('sqlite'); |
316 | daniel-mar | 182 | if (is_null($slang)) { |
360 | daniel-mar | 183 | throw new OIDplusConfigInitializationException(_L('SQL-Slang plugin "%1" is missing. Please check if it exists in the directory "plugin/sqlSlang". If it is not existing, please recover it from an SVN snapshot or OIDplus ZIP file.','sqlite')); |
316 | daniel-mar | 184 | } |
185 | return $slang; |
||
186 | } |
||
360 | daniel-mar | 187 | } |