Subversion Repositories oidplus

Rev

Rev 433 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 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. class OIDplusSqlSlangPluginPgSQL extends OIDplusSqlSlangPlugin {
  21.  
  22.         public static function id(): string {
  23.                 return 'pgsql';
  24.         }
  25.  
  26.         public function natOrder($fieldname, $order='asc'): string {
  27.  
  28.                 $order = strtolower($order);
  29.                 if (($order != 'asc') && ($order != 'desc')) {
  30.                         throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order));
  31.                 }
  32.  
  33.                 $out = array();
  34.  
  35.                 $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
  36.  
  37.                 // 1. Sort by namespace (oid, guid, ...)
  38.                 $out[] = "SPLIT_PART($fieldname, ':', 1) $order";
  39.  
  40.                 // 2. Only if namespace is 'oid:': Sort OID as integer array
  41.                 $out[] = "STRING_TO_ARRAY(SPLIT_PART($fieldname, 'oid:', 2), '.')::numeric($max_arc_len)[] $order";
  42.  
  43.                 // 3. Otherwise order by ID
  44.                 $out[] = "$fieldname $order";
  45.  
  46.                 return implode(', ', $out);
  47.  
  48.         }
  49.  
  50.         public function sqlDate(): string {
  51.                 return 'now()';
  52.         }
  53.  
  54.         public function detect(OIDplusDatabaseConnection $db): bool {
  55.                 try {
  56.                         $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version;
  57.                         $vers = strtolower($vers);
  58.                         return strpos($vers, 'postgresql') !== false;
  59.                 } catch (Exception $e) {
  60.                         return false;
  61.                 }
  62.         }
  63.  
  64.         public function insert_id(OIDplusDatabaseConnection $db): int {
  65.                 $res = $db->query("SELECT LASTVAL() AS ID");
  66.                 $row = $res->fetch_array();
  67.                 return (int)$row['ID'];
  68.         }
  69.  
  70.         public function setupSetTablePrefix($cont, $table, $prefix): string {
  71.                 $cont = str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
  72.                 $cont = str_replace('"index_'.$table, '"index_'.$prefix.$table, $cont);
  73.                 return $cont;
  74.         }
  75.  
  76.         public function setupCreateDbIfNotExists($database): string {
  77.                 return "-- CREATE DATABASE $database;\n\n";
  78.         }
  79.  
  80.         public function setupUseDatabase($database): string {
  81.                 return "-- \connect $database;\n\n";
  82.         }
  83.  
  84.         public function isNullFunction($expr1, $expr2): string {
  85.                 return "coalesce($expr1, $expr2)";
  86.         }
  87.  
  88.         public function filterQuery($sql): string {
  89.                 return $sql;
  90.         }
  91.  
  92.         public function getSQLBool($bool): string {
  93.                 return $bool ? '1' : '0';
  94.         }
  95. }