Subversion Repositories oidplus

Rev

Rev 1240 | 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 OIDplusSqlSlangPluginOracle extends OIDplusSqlSlangPlugin {
  27.  
  28.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'oracle';
  33.         }
  34.  
  35.         /**
  36.          * @return string
  37.          */
  38.         public function sqlDate(): string {
  39.                 return 'SYSDATE';
  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 banner FROM v\$version WHERE banner LIKE 'Oracle%'")->fetch_object()->banner;
  49.                         return (stripos($vers, 'Oracle') !== false);
  50.                 } catch (\Exception $e) {
  51.                         return false;
  52.                 }
  53.         }
  54.  
  55.         /**
  56.          * @var string|null
  57.          */
  58.         private $last_insert_table = null;
  59.  
  60.         /**
  61.          * @param OIDplusDatabaseConnection $db
  62.          * @return int
  63.          * @throws OIDplusException
  64.          */
  65.         public function insert_id(OIDplusDatabaseConnection $db): int {
  66.                 if (!$this->last_insert_table) return 0;
  67.                 $res = $db->query("select sequence_name from user_tab_identity_cols where table_name = '".strtoupper($this->last_insert_table)."'");
  68.                 $row = $res->fetch_array();
  69.  
  70.                 if (!isset($row['sequence_name'])) return 0;
  71.                 $res = $db->query("select ".$row['sequence_name'].".currval from dual");
  72.                 $row = $res->fetch_array();
  73.                 return (int)$row['CURRVAL'];
  74.         }
  75.  
  76.         /**
  77.          * @param string $cont
  78.          * @param string $table
  79.          * @param string $prefix
  80.          * @return string
  81.          */
  82.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  83.                 $table = strtoupper($table);
  84.                 $prefix = strtoupper($prefix);
  85.                 return str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
  86.         }
  87.  
  88.         /**
  89.          * @param string $database
  90.          * @return string
  91.          */
  92.         public function setupCreateDbIfNotExists(string $database): string {
  93.                 // TODO! Implement
  94.                 return "";
  95.         }
  96.  
  97.         /**
  98.          * @param string $database
  99.          * @return string
  100.          */
  101.         public function setupUseDatabase(string $database): string {
  102.                 // TODO! Implement
  103.                 return "";
  104.         }
  105.  
  106.         /**
  107.          * @param string $expr1
  108.          * @param string $expr2
  109.          * @return string
  110.          */
  111.         public function isNullFunction(string $expr1, string $expr2): string {
  112.                 // Test via "SELECT NVL(null, 'foo') FROM DUAL;"
  113.                 return "NVL($expr1, $expr2)";
  114.         }
  115.  
  116.         /**
  117.          * @param string $sql
  118.          * @return string
  119.          */
  120.         public function filterQuery(string $sql): string {
  121.                 $sql = trim($sql);
  122.  
  123.                 // SQL-Queries MUST NOT end with a ";", otherwise error "SQL command not property ended"
  124.                 $sql = rtrim($sql, "; \n\r\t\v\x00");
  125.                 // SQL/PL-Programs MUST end with a ";"
  126.                 if (strtolower(substr($sql,-3)) == 'end') $sql .= ';';
  127.  
  128.                 // "select 1" is not valid. You need to add "from dual"
  129.                 if (str_starts_with(trim(strtolower($sql)),'select') && (stripos($sql,'from') === false)) {
  130.                         $sql .= ' from dual';
  131.                 }
  132.  
  133.                 // Dirty hack!!! We need the name of the last inserted table so that insert_id()
  134.                 // works. This is a dirty hack, because the invokation of filterQuery() does
  135.                 // not guarantee that the query was actually executed...
  136.                 if (preg_match("@insert into (.+) @ismU", $sql, $m)) {
  137.                         $this->last_insert_table = $m[1];
  138.                 } else {
  139.                         $this->last_insert_table = null;
  140.                 }
  141.  
  142.                 // Comment is a keyword and cannot be used as column name
  143.                 return str_ireplace('comment', '"COMMENT"', $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.         /**
  163.          * @param string $sql
  164.          * @return string
  165.          */
  166.         public function lowerCase(string $sql): string {
  167.                 return "lower($sql)";
  168.         }
  169.  
  170.         /**
  171.          * @param string $sql
  172.          * @return string
  173.          */
  174.         public function upperCase(string $sql): string {
  175.                 return "upper($sql)";
  176.         }
  177. }
  178.