Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1130 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusSqlSlangPluginMySQL extends OIDplusSqlSlangPlugin {
  27.  
  28.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'mysql';
  33.         }
  34.  
  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 {
  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.                 $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
  51.  
  52.                 // 1. sort by namespace (oid, guid, ...)
  53.                 $out[] = "SUBSTRING_INDEX($fieldname,':',1) $order";
  54.  
  55.                 // 2. sort by first arc (0,1,2)
  56.                 $out[] = "SUBSTRING(SUBSTRING_INDEX($fieldname,'.',1), LENGTH(SUBSTRING_INDEX($fieldname,':',1))+2, $max_arc_len) $order";
  57.  
  58.                 for ($i=2; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) {
  59.                         // 3. Sort by the rest arcs one by one, not that MySQL can only handle decimal(65), not decimal($max_arc_len)
  60.                         $out[] = "cast(SUBSTRING(SUBSTRING_INDEX($fieldname,'.',$i), LENGTH(SUBSTRING_INDEX($fieldname,'.',".($i-1)."))+2, $max_arc_len) as decimal($max_arc_len)) $order";
  61.                 }
  62.  
  63.                 // 4. as last resort, sort by the identifier itself, e.g. if the casts above did fail (happens if it is not an OID)
  64.                 $out[] = "$fieldname $order";
  65.  
  66.                 return implode(', ', $out);
  67.  
  68.         }
  69.  
  70.         /**
  71.          * @return string
  72.          */
  73.         public function sqlDate(): string {
  74.                 return 'now()';
  75.         }
  76.  
  77.         /**
  78.          * @param OIDplusDatabaseConnection $db
  79.          * @return bool
  80.          */
  81.         public function detect(OIDplusDatabaseConnection $db): bool {
  82.                 try {
  83.                         $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version;
  84.                         $vers = strtolower($vers);
  85.                         return (strpos($vers, 'mysql') !== false) || (strpos($vers, 'mariadb') !== false);
  86.                 } catch (\Exception $e) {
  87.                         return false;
  88.                 }
  89.         }
  90.  
  91.         /**
  92.          * @param OIDplusDatabaseConnection $db
  93.          * @return int
  94.          * @throws OIDplusException
  95.          */
  96.         public function insert_id(OIDplusDatabaseConnection $db): int {
  97.                 $res = $db->query("SELECT LAST_INSERT_ID() AS ID");
  98.                 $row = $res->fetch_array();
  99.                 return (int)$row['ID'];
  100.         }
  101.  
  102.         /**
  103.          * @param string $cont
  104.          * @param string $table
  105.          * @param string $prefix
  106.          * @return string
  107.          */
  108.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  109.                 $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont);
  110.                 return $cont;
  111.         }
  112.  
  113.         /**
  114.          * @param string $database
  115.          * @return string
  116.          */
  117.         public function setupCreateDbIfNotExists(string $database): string {
  118.                 return "CREATE DATABASE IF NOT EXISTS `$database`;\n\n";
  119.         }
  120.  
  121.         /**
  122.          * @param string $database
  123.          * @return string
  124.          */
  125.         public function setupUseDatabase(string $database): string {
  126.                 return "USE `$database`;\n\n";
  127.         }
  128.  
  129.         /**
  130.          * @param string $expr1
  131.          * @param string $expr2
  132.          * @return string
  133.          */
  134.         public function isNullFunction(string $expr1, string $expr2): string {
  135.                 return "ifnull($expr1, $expr2)";
  136.         }
  137.  
  138.         /**
  139.          * @param string $sql
  140.          * @return string
  141.          */
  142.         public function filterQuery(string $sql): string {
  143.                 return $sql;
  144.         }
  145.  
  146.         /**
  147.          * @param bool $bool
  148.          * @return string
  149.          */
  150.         public function getSQLBool(bool $bool): string {
  151.                 return $bool ? '1' : '0';
  152.         }
  153.  
  154.         /**
  155.          * @param string $str
  156.          * @return string
  157.          */
  158.         public function escapeString(string $str): string {
  159.                 return str_replace("'", "''", $str);
  160.         }
  161. }
  162.