Rev 1050 | Rev 1116 | 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 OIDplusSqlSlangPluginMySQL extends OIDplusSqlSlangPlugin { |
27 | |||
28 | public static function id(): string { |
||
29 | return 'mysql'; |
||
30 | } |
||
31 | |||
32 | public function natOrder($fieldname, $order='asc'): string { |
||
33 | |||
34 | $order = strtolower($order); |
||
35 | if (($order != 'asc') && ($order != 'desc')) { |
||
36 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
||
37 | } |
||
38 | |||
39 | $out = array(); |
||
40 | |||
41 | $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 |
||
42 | |||
43 | // 1. sort by namespace (oid, guid, ...) |
||
44 | $out[] = "SUBSTRING_INDEX($fieldname,':',1) $order"; |
||
45 | |||
46 | // 2. sort by first arc (0,1,2) |
||
47 | $out[] = "SUBSTRING(SUBSTRING_INDEX($fieldname,'.',1), LENGTH(SUBSTRING_INDEX($fieldname,':',1))+2, $max_arc_len) $order"; |
||
48 | |||
49 | for ($i=2; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) { |
||
50 | // 3. Sort by the rest arcs one by one, not that MySQL can only handle decimal(65), not decimal($max_arc_len) |
||
51 | $out[] = "cast(SUBSTRING(SUBSTRING_INDEX($fieldname,'.',$i), LENGTH(SUBSTRING_INDEX($fieldname,'.',".($i-1)."))+2, $max_arc_len) as decimal($max_arc_len)) $order"; |
||
52 | } |
||
53 | |||
54 | // 4. as last resort, sort by the identifier itself, e.g. if the casts above did fail (happens if it is not an OID) |
||
55 | $out[] = "$fieldname $order"; |
||
56 | |||
57 | return implode(', ', $out); |
||
58 | |||
59 | } |
||
60 | |||
61 | public function sqlDate(): string { |
||
62 | return 'now()'; |
||
63 | } |
||
64 | |||
65 | public function detect(OIDplusDatabaseConnection $db): bool { |
||
66 | try { |
||
67 | $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version; |
||
68 | $vers = strtolower($vers); |
||
69 | return (strpos($vers, 'mysql') !== false) || (strpos($vers, 'mariadb') !== false); |
||
1050 | daniel-mar | 70 | } catch (\Exception $e) { |
635 | daniel-mar | 71 | return false; |
72 | } |
||
73 | } |
||
74 | |||
75 | public function insert_id(OIDplusDatabaseConnection $db): int { |
||
76 | $res = $db->query("SELECT LAST_INSERT_ID() AS ID"); |
||
77 | $row = $res->fetch_array(); |
||
78 | return (int)$row['ID']; |
||
79 | } |
||
80 | |||
81 | |||
82 | public function setupSetTablePrefix($cont, $table, $prefix): string { |
||
83 | $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont); |
||
84 | return $cont; |
||
85 | } |
||
86 | |||
87 | public function setupCreateDbIfNotExists($database): string { |
||
88 | return "CREATE DATABASE IF NOT EXISTS `$database`;\n\n"; |
||
89 | } |
||
90 | |||
91 | public function setupUseDatabase($database): string { |
||
92 | return "USE `$database`;\n\n"; |
||
93 | } |
||
94 | |||
95 | public function isNullFunction($expr1, $expr2): string { |
||
96 | return "ifnull($expr1, $expr2)"; |
||
97 | } |
||
98 | |||
99 | public function filterQuery($sql): string { |
||
100 | return $sql; |
||
101 | } |
||
102 | |||
103 | public function getSQLBool($bool): string { |
||
104 | return $bool ? '1' : '0'; |
||
105 | } |
||
106 | |||
107 | public function escapeString($str): string { |
||
108 | return str_replace("'", "''", $str); |
||
109 | } |
||
110 | } |