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 OIDplusSqlSlangPluginPgSQL extends OIDplusSqlSlangPlugin {
  27.  
  28.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'pgsql';
  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') > 131072 ? 131072 : OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE'); // Limit of the "numeric()" type
  51.  
  52.                 // 1. Sort by namespace (oid, guid, ...)
  53.                 $out[] = "SPLIT_PART($fieldname, ':', 1) $order";
  54.  
  55.                 // 2. Only if namespace is 'oid:': Sort OID as integer array
  56.                 $out[] = "STRING_TO_ARRAY(SPLIT_PART($fieldname, 'oid:', 2), '.')::numeric($max_arc_len)[] $order";
  57.  
  58.                 // 3. Otherwise order by ID
  59.                 $out[] = "$fieldname $order";
  60.  
  61.                 return implode(', ', $out);
  62.  
  63.         }
  64.  
  65.         /**
  66.          * @return string
  67.          */
  68.         public function sqlDate(): string {
  69.                 return 'now()';
  70.         }
  71.  
  72.         /**
  73.          * @param OIDplusDatabaseConnection $db
  74.          * @return bool
  75.          */
  76.         public function detect(OIDplusDatabaseConnection $db): bool {
  77.                 try {
  78.                         $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version;
  79.                         $vers = strtolower($vers);
  80.                         return strpos($vers, 'postgresql') !== false;
  81.                 } catch (\Exception $e) {
  82.                         return false;
  83.                 }
  84.         }
  85.  
  86.         /**
  87.          * @param OIDplusDatabaseConnection $db
  88.          * @return int
  89.          * @throws OIDplusException
  90.          */
  91.         public function insert_id(OIDplusDatabaseConnection $db): int {
  92.                 $res = $db->query("SELECT LASTVAL() AS ID");
  93.                 $row = $res->fetch_array();
  94.                 return (int)$row['ID'];
  95.         }
  96.  
  97.         /**
  98.          * @param string $cont
  99.          * @param string $table
  100.          * @param string $prefix
  101.          * @return string
  102.          */
  103.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  104.                 $cont = str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
  105.                 $cont = str_replace('"index_'.$table, '"index_'.$prefix.$table, $cont);
  106.                 return $cont;
  107.         }
  108.  
  109.         /**
  110.          * @param string $database
  111.          * @return string
  112.          */
  113.         public function setupCreateDbIfNotExists(string $database): string {
  114.                 return "-- CREATE DATABASE $database;\n\n";
  115.         }
  116.  
  117.         /**
  118.          * @param string $database
  119.          * @return string
  120.          */
  121.         public function setupUseDatabase(string $database): string {
  122.                 return "-- \connect $database;\n\n";
  123.         }
  124.  
  125.         /**
  126.          * @param string $expr1
  127.          * @param string $expr2
  128.          * @return string
  129.          */
  130.         public function isNullFunction(string $expr1, string $expr2): string {
  131.                 return "coalesce($expr1, $expr2)";
  132.         }
  133.  
  134.         /**
  135.          * @param string $sql
  136.          * @return string
  137.          */
  138.         public function filterQuery(string $sql): string {
  139.                 return $sql;
  140.         }
  141.  
  142.         /**
  143.          * @param bool $bool
  144.          * @return string
  145.          */
  146.         public function getSQLBool(bool $bool): string {
  147.                 return $bool ? '1' : '0';
  148.         }
  149.  
  150.         /**
  151.          * @param string $str
  152.          * @return string
  153.          */
  154.         public function escapeString(string $str): string {
  155.                 return str_replace("'", "''", $str);
  156.         }
  157. }
  158.