Rev 635 | Rev 1086 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 635 | Rev 1050 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with 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 |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | if (!defined('INSIDE_OIDPLUS')) die(); |
20 | namespace ViaThinkSoft\OIDplus; |
21 | 21 | ||
22 | class OIDplusSqlSlangPluginMySQL extends OIDplusSqlSlangPlugin { |
22 | class OIDplusSqlSlangPluginMySQL extends OIDplusSqlSlangPlugin { |
23 | 23 | ||
24 | public static function id(): string { |
24 | public static function id(): string { |
25 | return 'mysql'; |
25 | return 'mysql'; |
26 | } |
26 | } |
27 | 27 | ||
28 | public function natOrder($fieldname, $order='asc'): string { |
28 | public function natOrder($fieldname, $order='asc'): string { |
29 | 29 | ||
30 | $order = strtolower($order); |
30 | $order = strtolower($order); |
31 | if (($order != 'asc') && ($order != 'desc')) { |
31 | if (($order != 'asc') && ($order != 'desc')) { |
32 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
32 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
33 | } |
33 | } |
34 | 34 | ||
35 | $out = array(); |
35 | $out = array(); |
36 | 36 | ||
37 | $max_arc_len = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE') > 65 ? 65 : OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE'); // Limit of "decimal()" type |
37 | $max_arc_len = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE') > 65 ? 65 : OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE'); // Limit of "decimal()" type |
38 | 38 | ||
39 | // 1. sort by namespace (oid, guid, ...) |
39 | // 1. sort by namespace (oid, guid, ...) |
40 | $out[] = "SUBSTRING_INDEX($fieldname,':',1) $order"; |
40 | $out[] = "SUBSTRING_INDEX($fieldname,':',1) $order"; |
41 | 41 | ||
42 | // 2. sort by first arc (0,1,2) |
42 | // 2. sort by first arc (0,1,2) |
43 | $out[] = "SUBSTRING(SUBSTRING_INDEX($fieldname,'.',1), LENGTH(SUBSTRING_INDEX($fieldname,':',1))+2, $max_arc_len) $order"; |
43 | $out[] = "SUBSTRING(SUBSTRING_INDEX($fieldname,'.',1), LENGTH(SUBSTRING_INDEX($fieldname,':',1))+2, $max_arc_len) $order"; |
44 | 44 | ||
45 | for ($i=2; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) { |
45 | for ($i=2; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) { |
46 | // 3. Sort by the rest arcs one by one, not that MySQL can only handle decimal(65), not decimal($max_arc_len) |
46 | // 3. Sort by the rest arcs one by one, not that MySQL can only handle decimal(65), not decimal($max_arc_len) |
47 | $out[] = "cast(SUBSTRING(SUBSTRING_INDEX($fieldname,'.',$i), LENGTH(SUBSTRING_INDEX($fieldname,'.',".($i-1)."))+2, $max_arc_len) as decimal($max_arc_len)) $order"; |
47 | $out[] = "cast(SUBSTRING(SUBSTRING_INDEX($fieldname,'.',$i), LENGTH(SUBSTRING_INDEX($fieldname,'.',".($i-1)."))+2, $max_arc_len) as decimal($max_arc_len)) $order"; |
48 | } |
48 | } |
49 | 49 | ||
50 | // 4. as last resort, sort by the identifier itself, e.g. if the casts above did fail (happens if it is not an OID) |
50 | // 4. as last resort, sort by the identifier itself, e.g. if the casts above did fail (happens if it is not an OID) |
51 | $out[] = "$fieldname $order"; |
51 | $out[] = "$fieldname $order"; |
52 | 52 | ||
53 | return implode(', ', $out); |
53 | return implode(', ', $out); |
54 | 54 | ||
55 | } |
55 | } |
56 | 56 | ||
57 | public function sqlDate(): string { |
57 | public function sqlDate(): string { |
58 | return 'now()'; |
58 | return 'now()'; |
59 | } |
59 | } |
60 | 60 | ||
61 | public function detect(OIDplusDatabaseConnection $db): bool { |
61 | public function detect(OIDplusDatabaseConnection $db): bool { |
62 | try { |
62 | try { |
63 | $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version; |
63 | $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version; |
64 | $vers = strtolower($vers); |
64 | $vers = strtolower($vers); |
65 | return (strpos($vers, 'mysql') !== false) || (strpos($vers, 'mariadb') !== false); |
65 | return (strpos($vers, 'mysql') !== false) || (strpos($vers, 'mariadb') !== false); |
66 | } catch (Exception $e) { |
66 | } catch (\Exception $e) { |
67 | return false; |
67 | return false; |
68 | } |
68 | } |
69 | } |
69 | } |
70 | 70 | ||
71 | public function insert_id(OIDplusDatabaseConnection $db): int { |
71 | public function insert_id(OIDplusDatabaseConnection $db): int { |
72 | $res = $db->query("SELECT LAST_INSERT_ID() AS ID"); |
72 | $res = $db->query("SELECT LAST_INSERT_ID() AS ID"); |
73 | $row = $res->fetch_array(); |
73 | $row = $res->fetch_array(); |
74 | return (int)$row['ID']; |
74 | return (int)$row['ID']; |
75 | } |
75 | } |
76 | 76 | ||
77 | 77 | ||
78 | public function setupSetTablePrefix($cont, $table, $prefix): string { |
78 | public function setupSetTablePrefix($cont, $table, $prefix): string { |
79 | $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont); |
79 | $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont); |
80 | return $cont; |
80 | return $cont; |
81 | } |
81 | } |
82 | 82 | ||
83 | public function setupCreateDbIfNotExists($database): string { |
83 | public function setupCreateDbIfNotExists($database): string { |
84 | return "CREATE DATABASE IF NOT EXISTS `$database`;\n\n"; |
84 | return "CREATE DATABASE IF NOT EXISTS `$database`;\n\n"; |
85 | } |
85 | } |
86 | 86 | ||
87 | public function setupUseDatabase($database): string { |
87 | public function setupUseDatabase($database): string { |
88 | return "USE `$database`;\n\n"; |
88 | return "USE `$database`;\n\n"; |
89 | } |
89 | } |
90 | 90 | ||
91 | public function isNullFunction($expr1, $expr2): string { |
91 | public function isNullFunction($expr1, $expr2): string { |
92 | return "ifnull($expr1, $expr2)"; |
92 | return "ifnull($expr1, $expr2)"; |
93 | } |
93 | } |
94 | 94 | ||
95 | public function filterQuery($sql): string { |
95 | public function filterQuery($sql): string { |
96 | return $sql; |
96 | return $sql; |
97 | } |
97 | } |
98 | 98 | ||
99 | public function getSQLBool($bool): string { |
99 | public function getSQLBool($bool): string { |
100 | return $bool ? '1' : '0'; |
100 | return $bool ? '1' : '0'; |
101 | } |
101 | } |
102 | 102 | ||
103 | public function escapeString($str): string { |
103 | public function escapeString($str): string { |
104 | return str_replace("'", "''", $str); |
104 | return str_replace("'", "''", $str); |
105 | } |
105 | } |
106 | } |
106 | } |
107 | 107 |