Rev 1116 | Rev 1148 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusSqlSlangPluginSQLite extends OIDplusSqlSlangPlugin { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @return string |
||
30 | */ |
||
635 | daniel-mar | 31 | public static function id(): string { |
32 | return 'sqlite'; |
||
33 | } |
||
34 | |||
1116 | daniel-mar | 35 | /** |
36 | * @param string $fieldname |
||
37 | * @param string $order |
||
38 | * @return string |
||
39 | * @throws OIDplusException |
||
40 | */ |
||
41 | public function natOrder(string $fieldname, string $order='asc'): string { |
||
635 | daniel-mar | 42 | |
43 | $order = strtolower($order); |
||
44 | if (($order != 'asc') && ($order != 'desc')) { |
||
45 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
||
46 | } |
||
47 | |||
48 | $out = array(); |
||
49 | |||
50 | // If the SQLite database is accessed through the SQLite3 plugin, |
||
51 | // we use an individual collation, therefore OIDplusDatabasePluginSQLite3 overrides |
||
52 | // natOrder(). |
||
53 | // If we connected to SQLite using ODBC or PDO, we need to do something else, |
||
54 | // but that solution is complex, slow and wrong (since it does not support |
||
55 | // UUIDs etc.) |
||
56 | |||
57 | $max_depth = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); |
||
58 | if ($max_depth > 11) $max_depth = 11; // SQLite3 will crash if max depth > 11 (parser stack overflow); (TODO: can we do something else?!?!) |
||
59 | |||
60 | // 1. sort by namespace (oid, guid, ...) |
||
61 | $out[] = "substr($fieldname,0,instr($fieldname,':')) $order"; |
||
62 | |||
63 | // 2. Sort by the rest arcs one by one |
||
64 | for ($i=1; $i<=$max_depth; $i++) { |
||
65 | if ($i==1) { |
||
66 | $arc = "substr($fieldname,5)||'.'"; |
||
67 | $fieldname = $arc; |
||
68 | $arc = "substr($arc,0,instr($arc,'.'))"; |
||
69 | $out[] = "cast($arc as integer) $order"; |
||
70 | } else { |
||
71 | $arc = "ltrim(ltrim($fieldname,'0123456789'),'.')"; |
||
72 | $fieldname = $arc; |
||
73 | $arc = "substr($arc,0,instr($arc,'.'))"; |
||
74 | $out[] = "cast($arc as integer) $order"; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // 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) |
||
79 | $out[] = "$fieldname $order"; |
||
80 | |||
81 | return implode(', ', $out); |
||
82 | |||
83 | } |
||
84 | |||
1116 | daniel-mar | 85 | /** |
86 | * @return string |
||
87 | */ |
||
635 | daniel-mar | 88 | public function sqlDate(): string { |
89 | return 'datetime()'; |
||
90 | } |
||
91 | |||
1116 | daniel-mar | 92 | /** |
93 | * @param OIDplusDatabaseConnection $db |
||
94 | * @return bool |
||
95 | */ |
||
635 | daniel-mar | 96 | public function detect(OIDplusDatabaseConnection $db): bool { |
97 | try { |
||
98 | $db->query("select sqlite_version as dbms_version"); |
||
99 | return true; |
||
1050 | daniel-mar | 100 | } catch (\Exception $e) { |
635 | daniel-mar | 101 | return false; |
102 | } |
||
103 | } |
||
104 | |||
1116 | daniel-mar | 105 | /** |
106 | * @param OIDplusDatabaseConnection $db |
||
107 | * @return int |
||
108 | * @throws OIDplusException |
||
109 | */ |
||
635 | daniel-mar | 110 | public function insert_id(OIDplusDatabaseConnection $db): int { |
111 | $res = $db->query("SELECT last_insert_rowid() AS ID"); |
||
112 | $row = $res->fetch_array(); |
||
113 | return (int)$row['ID']; |
||
114 | } |
||
115 | |||
1116 | daniel-mar | 116 | /** |
117 | * @param string $cont |
||
118 | * @param string $table |
||
119 | * @param string $prefix |
||
120 | * @return string |
||
121 | */ |
||
122 | public function setupSetTablePrefix(string $cont, string $table, string $prefix): string { |
||
1130 | daniel-mar | 123 | return str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont); |
635 | daniel-mar | 124 | } |
125 | |||
1116 | daniel-mar | 126 | /** |
127 | * @param string $database |
||
128 | * @return string |
||
129 | */ |
||
130 | public function setupCreateDbIfNotExists(string $database): string { |
||
635 | daniel-mar | 131 | return ""; |
132 | } |
||
133 | |||
1116 | daniel-mar | 134 | /** |
135 | * @param string $database |
||
136 | * @return string |
||
137 | */ |
||
138 | public function setupUseDatabase(string $database): string { |
||
635 | daniel-mar | 139 | return ""; |
140 | } |
||
141 | |||
1116 | daniel-mar | 142 | /** |
143 | * @param string $expr1 |
||
144 | * @param string $expr2 |
||
145 | * @return string |
||
146 | */ |
||
147 | public function isNullFunction(string $expr1, string $expr2): string { |
||
635 | daniel-mar | 148 | return "ifnull($expr1, $expr2)"; |
149 | } |
||
150 | |||
1116 | daniel-mar | 151 | /** |
152 | * @param string $sql |
||
153 | * @return string |
||
154 | */ |
||
155 | public function filterQuery(string $sql): string { |
||
635 | daniel-mar | 156 | return $sql; |
157 | } |
||
158 | |||
1116 | daniel-mar | 159 | /** |
160 | * @param bool $bool |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getSQLBool(bool $bool): string { |
||
635 | daniel-mar | 164 | return $bool ? '1' : '0'; |
165 | } |
||
166 | |||
1116 | daniel-mar | 167 | /** |
168 | * @param string $str |
||
169 | * @return string |
||
170 | */ |
||
171 | public function escapeString(string $str): string { |
||
635 | daniel-mar | 172 | return str_replace("'", "''", $str); |
173 | } |
||
174 | } |