Subversion Repositories oidplus

Rev

Rev 1116 | 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 OIDplusSqlSlangPluginOracle extends OIDplusSqlSlangPlugin {
  27.  
  28.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'oracle';
  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[] = "regexp_substr($fieldname, '(.*?)(:|\$)', 1, 1, NULL, 1) $order";
  54.  
  55.                 // 2. sort by first arc (0,1,2)
  56.                 $tmp = "regexp_substr($fieldname, '(.*?)(:|\$)', 1, 2, NULL, 1)";
  57.                 $i = 1;
  58.                 $out[] = "lpad(regexp_substr($tmp, '(.*?)(\\.|\$)', 1, $i, NULL, 1),$max_arc_len,'0') $order";
  59.  
  60.                 for ($i=2; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) {
  61.                         // 3. Sort by the rest arcs one by one, not that MySQL can only handle decimal(65), not decimal($max_arc_len)
  62.                         $out[] = "lpad(regexp_substr($fieldname, '(.*?)(\\.|\$)', 1, $i, NULL, 1),$max_arc_len,'0') $order";
  63.                 }
  64.  
  65.                 // 4. as last resort, sort by the identifier itself, e.g. if the casts above did fail (happens if it is not an OID)
  66.                 $out[] = "$fieldname $order";
  67.  
  68.                 return implode(', ', $out);
  69.  
  70.         }
  71.  
  72.         /**
  73.          * @return string
  74.          */
  75.         public function sqlDate(): string {
  76.                 return 'SYSDATE';
  77.         }
  78.  
  79.         /**
  80.          * @param OIDplusDatabaseConnection $db
  81.          * @return bool
  82.          */
  83.         public function detect(OIDplusDatabaseConnection $db): bool {
  84.                 try {
  85.                         $vers = $db->query("SELECT banner FROM v\$version WHERE banner LIKE 'Oracle%'")->fetch_object()->banner;
  86.                         $vers = strtolower($vers);
  87.                         return (strpos($vers, 'oracle') !== false);
  88.                 } catch (\Exception $e) {
  89.                         return false;
  90.                 }
  91.         }
  92.  
  93.         /**
  94.          * @var ?string
  95.          */
  96.         private $last_insert_table = null;
  97.  
  98.         /**
  99.          * @param OIDplusDatabaseConnection $db
  100.          * @return int
  101.          * @throws OIDplusException
  102.          */
  103.         public function insert_id(OIDplusDatabaseConnection $db): int {
  104.                 if (!$this->last_insert_table) return 0;
  105.                 $res = $db->query("select sequence_name from user_tab_identity_cols where table_name = '".strtoupper($this->last_insert_table)."'");
  106.                 $row = $res->fetch_array();
  107.  
  108.                 if (!isset($row['sequence_name'])) return 0;
  109.                 $res = $db->query("select ".$row['sequence_name'].".currval from dual");
  110.                 $row = $res->fetch_array();
  111.                 return (int)$row['CURRVAL'];
  112.         }
  113.  
  114.         /**
  115.          * @param string $cont
  116.          * @param string $table
  117.          * @param string $prefix
  118.          * @return string
  119.          */
  120.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  121.                 $table = strtoupper($table);
  122.                 $prefix = strtoupper($prefix);
  123.                 return str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
  124.         }
  125.  
  126.         /**
  127.          * @param string $database
  128.          * @return string
  129.          */
  130.         public function setupCreateDbIfNotExists(string $database): string {
  131.                 // TODO! Implement
  132.                 return "";
  133.         }
  134.  
  135.         /**
  136.          * @param string $database
  137.          * @return string
  138.          */
  139.         public function setupUseDatabase(string $database): string {
  140.                 // TODO! Implement
  141.                 return "";
  142.         }
  143.  
  144.         /**
  145.          * @param string $expr1
  146.          * @param string $expr2
  147.          * @return string
  148.          */
  149.         public function isNullFunction(string $expr1, string $expr2): string {
  150.                 // Test via "SELECT NVL(null, 'foo') FROM DUAL;"
  151.                 return "NVL($expr1, $expr2)";
  152.         }
  153.  
  154.         /**
  155.          * @param string $sql
  156.          * @return string
  157.          */
  158.         public function filterQuery(string $sql): string {
  159.  
  160.                 // "select 1" is not valid. You need to add "from dual"
  161.                 if ((stripos($sql,'select') !== false) && (stripos($sql,'from') === false)) {
  162.                         $sql .= ' from dual';
  163.                 }
  164.  
  165.                 // SQL-Queries MUST NOT end with a ";", otherwise error "SQL command not property ended"
  166.                 $sql = rtrim(trim($sql), "; \n\r\t\v\x00");
  167.                 // SQL/PL-Programs MUST end with a ";"
  168.                 if (strtolower(substr($sql,-3)) == 'end') $sql .= ';';
  169.  
  170.                 // Dirty hack!!! We need the name of the last inserted table so that insert_id()
  171.                 // works. This is a dirty hack, because the invokation of filterQuery() does
  172.                 // not guarantee that the query was actually executed...
  173.                 if (preg_match("@insert into (.+) @ismU", $sql, $m)) {
  174.                         $this->last_insert_table = $m[1];
  175.                 } else {
  176.                         $this->last_insert_table = null;
  177.                 }
  178.  
  179.                 // Comment is a keyword and cannot be used as column name
  180.                 return str_ireplace('comment', '"COMMENT"', $sql);
  181.         }
  182.  
  183.         /**
  184.          * @param bool $bool
  185.          * @return string
  186.          */
  187.         public function getSQLBool(bool $bool): string {
  188.                 return $bool ? '1' : '0';
  189.         }
  190.  
  191.         /**
  192.          * @param string $str
  193.          * @return string
  194.          */
  195.         public function escapeString(string $str): string {
  196.                 return str_replace("'", "''", $str);
  197.         }
  198. }
  199.