Subversion Repositories oidplus

Rev

Rev 635 | Rev 1086 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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