Rev 316 | 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 OIDplusSqlSlangPluginMySQL extends OIDplusSqlSlangPlugin { |
||
21 | |||
22 | public static function id(): string { |
||
23 | return 'mysql'; |
||
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 | $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 |
||
36 | |||
37 | // 1. sort by namespace (oid, guid, ...) |
||
38 | $out[] = "SUBSTRING_INDEX($fieldname,':',1) $order"; |
||
39 | |||
40 | // 2. sort by first arc (0,1,2) |
||
41 | $out[] = "SUBSTRING(SUBSTRING_INDEX($fieldname,'.',1), LENGTH(SUBSTRING_INDEX($fieldname,':',1))+2, $max_arc_len) $order"; |
||
42 | |||
43 | for ($i=2; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) { |
||
44 | // 3. Sort by the rest arcs one by one, not that MySQL can only handle decimal(65), not decimal($max_arc_len) |
||
45 | $out[] = "cast(SUBSTRING(SUBSTRING_INDEX($fieldname,'.',$i), LENGTH(SUBSTRING_INDEX($fieldname,'.',".($i-1)."))+2, $max_arc_len) as decimal($max_arc_len)) $order"; |
||
46 | } |
||
47 | |||
48 | // 4. as last resort, sort by the identifier itself, e.g. if the casts above did fail (happens if it is not an OID) |
||
49 | $out[] = "$fieldname $order"; |
||
50 | |||
51 | return implode(', ', $out); |
||
52 | |||
53 | } |
||
54 | |||
55 | public function sqlDate(): string { |
||
56 | return 'now()'; |
||
57 | } |
||
58 | |||
59 | public function detect(OIDplusDatabaseConnection $db): bool { |
||
60 | try { |
||
61 | $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version; |
||
62 | $vers = strtolower($vers); |
||
63 | return (strpos($vers, 'mysql') !== false) || (strpos($vers, 'mariadb') !== false); |
||
64 | } catch (Exception $e) { |
||
65 | return false; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public function insert_id(OIDplusDatabaseConnection $db): int { |
||
70 | $res = $db->query("SELECT LAST_INSERT_ID() AS ID"); |
||
71 | $row = $res->fetch_array(); |
||
72 | return (int)$row['ID']; |
||
73 | } |
||
74 | |||
75 | |||
76 | public function setupSetTablePrefix($cont, $table, $prefix): string { |
||
77 | $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont); |
||
78 | return $cont; |
||
79 | } |
||
80 | |||
81 | public function setupCreateDbIfNotExists($database): string { |
||
82 | return "CREATE DATABASE IF NOT EXISTS `$database`;\n\n"; |
||
83 | } |
||
84 | |||
85 | public function setupUseDatabase($database): string { |
||
86 | return "USE `$database`;\n\n"; |
||
87 | } |
||
88 | } |