Rev 360 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
360 | 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 OIDplusSqlSlangPluginSQLite extends OIDplusSqlSlangPlugin { |
||
21 | |||
22 | public static function id(): string { |
||
23 | return 'sqlite'; |
||
24 | } |
||
25 | |||
26 | public function natOrder($fieldname, $order='asc'): string { |
||
27 | |||
28 | $order = strtolower($order); |
||
29 | if (($order != 'asc') && ($order != 'desc')) { |
||
30 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
||
31 | } |
||
32 | |||
33 | $out = array(); |
||
34 | |||
35 | // If the SQLite database is accessed through the SQLite3 plugin, |
||
36 | // we use an individual collation, therefore OIDplusDatabasePluginSQLite3 overrides |
||
37 | // natOrder(). |
||
38 | // If we connected to SQLite using ODBC or PDO, we need to do something else, |
||
39 | // but that solution is complex, slow and wrong (since it does not support |
||
40 | // UUIDs etc.) |
||
41 | |||
42 | $max_depth = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); |
||
43 | if ($max_depth > 11) $max_depth = 11; // SQLite3 will crash if max depth > 11 (parser stack overflow); (TODO: can we do something else?!?!) |
||
44 | |||
45 | // 1. sort by namespace (oid, guid, ...) |
||
46 | $out[] = "substr($fieldname,0,instr($fieldname,':')) $order"; |
||
47 | |||
48 | // 2. Sort by the rest arcs one by one |
||
49 | for ($i=1; $i<=$max_depth; $i++) { |
||
50 | if ($i==1) { |
||
51 | $arc = "substr($fieldname,5)||'.'"; |
||
52 | $fieldname = $arc; |
||
53 | $arc = "substr($arc,0,instr($arc,'.'))"; |
||
54 | $out[] = "cast($arc as integer) $order"; |
||
55 | } else { |
||
56 | $arc = "ltrim(ltrim($fieldname,'0123456789'),'.')"; |
||
57 | $fieldname = $arc; |
||
58 | $arc = "substr($arc,0,instr($arc,'.'))"; |
||
59 | $out[] = "cast($arc as integer) $order"; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | // 3. as last resort, sort by the identifier itself, e.g. if the function getOidArc always return 0 (happens if it is not an OID) |
||
64 | $out[] = "$fieldname $order"; |
||
65 | |||
66 | return implode(', ', $out); |
||
67 | |||
68 | } |
||
69 | |||
70 | public function sqlDate(): string { |
||
71 | return 'datetime()'; |
||
72 | } |
||
73 | |||
74 | public function detect(OIDplusDatabaseConnection $db): bool { |
||
75 | try { |
||
386 | daniel-mar | 76 | $db->query("select sqlite_version as dbms_version"); |
77 | return true; |
||
360 | daniel-mar | 78 | } catch (Exception $e) { |
79 | return false; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | public function insert_id(OIDplusDatabaseConnection $db): int { |
||
84 | $res = $db->query("SELECT last_insert_rowid() AS ID"); |
||
85 | $row = $res->fetch_array(); |
||
86 | return (int)$row['ID']; |
||
87 | } |
||
88 | |||
89 | public function setupSetTablePrefix($cont, $table, $prefix): string { |
||
90 | $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont); |
||
91 | return $cont; |
||
92 | } |
||
93 | |||
94 | public function setupCreateDbIfNotExists($database): string { |
||
95 | return ""; |
||
96 | } |
||
97 | |||
98 | public function setupUseDatabase($database): string { |
||
99 | return ""; |
||
100 | } |
||
101 | } |