Subversion Repositories oidplus

Rev

Rev 1148 | 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.          * @return string
  37.          */
  38.         public function sqlDate(): string {
  39.                 return 'now()';
  40.         }
  41.  
  42.         /**
  43.          * @param OIDplusDatabaseConnection $db
  44.          * @return bool
  45.          */
  46.         public function detect(OIDplusDatabaseConnection $db): bool {
  47.                 try {
  48.                         $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version;
  49.                         return (stripos($vers, 'MySQL') !== false) || (stripos($vers, 'MariaDB') !== false);
  50.                 } catch (\Exception $e) {
  51.                         return false;
  52.                 }
  53.         }
  54.  
  55.         /**
  56.          * @param OIDplusDatabaseConnection $db
  57.          * @return int
  58.          * @throws OIDplusException
  59.          */
  60.         public function insert_id(OIDplusDatabaseConnection $db): int {
  61.                 $res = $db->query("SELECT LAST_INSERT_ID() AS ID");
  62.                 $row = $res->fetch_array();
  63.                 return (int)$row['ID'];
  64.         }
  65.  
  66.         /**
  67.          * @param string $cont
  68.          * @param string $table
  69.          * @param string $prefix
  70.          * @return string
  71.          */
  72.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  73.                 return str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont);
  74.         }
  75.  
  76.         /**
  77.          * @param string $database
  78.          * @return string
  79.          */
  80.         public function setupCreateDbIfNotExists(string $database): string {
  81.                 return "CREATE DATABASE IF NOT EXISTS `$database`;\n\n";
  82.         }
  83.  
  84.         /**
  85.          * @param string $database
  86.          * @return string
  87.          */
  88.         public function setupUseDatabase(string $database): string {
  89.                 return "USE `$database`;\n\n";
  90.         }
  91.  
  92.         /**
  93.          * @param string $expr1
  94.          * @param string $expr2
  95.          * @return string
  96.          */
  97.         public function isNullFunction(string $expr1, string $expr2): string {
  98.                 return "ifnull($expr1, $expr2)";
  99.         }
  100.  
  101.         /**
  102.          * @param string $sql
  103.          * @return string
  104.          */
  105.         public function filterQuery(string $sql): string {
  106.                 return $sql;
  107.         }
  108.  
  109.         /**
  110.          * @param bool $bool
  111.          * @return string
  112.          */
  113.         public function getSQLBool(bool $bool): string {
  114.                 return $bool ? '1' : '0';
  115.         }
  116.  
  117.         /**
  118.          * @param string $str
  119.          * @return string
  120.          */
  121.         public function escapeString(string $str): string {
  122.                 return str_replace("'", "''", $str);
  123.         }
  124. }
  125.