Rev 1116 | Rev 1148 | 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 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 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 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusSqlSlangPluginPgSQL extends OIDplusSqlSlangPlugin { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @return string |
||
30 | */ |
||
635 | daniel-mar | 31 | public static function id(): string { |
32 | return 'pgsql'; |
||
33 | } |
||
34 | |||
1116 | daniel-mar | 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 { |
||
635 | daniel-mar | 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 | |||
1116 | daniel-mar | 65 | /** |
66 | * @return string |
||
67 | */ |
||
635 | daniel-mar | 68 | public function sqlDate(): string { |
69 | return 'now()'; |
||
70 | } |
||
71 | |||
1116 | daniel-mar | 72 | /** |
73 | * @param OIDplusDatabaseConnection $db |
||
74 | * @return bool |
||
75 | */ |
||
635 | daniel-mar | 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; |
||
1050 | daniel-mar | 81 | } catch (\Exception $e) { |
635 | daniel-mar | 82 | return false; |
83 | } |
||
84 | } |
||
85 | |||
1116 | daniel-mar | 86 | /** |
87 | * @param OIDplusDatabaseConnection $db |
||
88 | * @return int |
||
89 | * @throws OIDplusException |
||
90 | */ |
||
635 | daniel-mar | 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 | |||
1116 | daniel-mar | 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 { |
||
635 | daniel-mar | 104 | $cont = str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont); |
1130 | daniel-mar | 105 | return str_replace('"index_'.$table, '"index_'.$prefix.$table, $cont); |
635 | daniel-mar | 106 | } |
107 | |||
1116 | daniel-mar | 108 | /** |
109 | * @param string $database |
||
110 | * @return string |
||
111 | */ |
||
112 | public function setupCreateDbIfNotExists(string $database): string { |
||
635 | daniel-mar | 113 | return "-- CREATE DATABASE $database;\n\n"; |
114 | } |
||
115 | |||
1116 | daniel-mar | 116 | /** |
117 | * @param string $database |
||
118 | * @return string |
||
119 | */ |
||
120 | public function setupUseDatabase(string $database): string { |
||
635 | daniel-mar | 121 | return "-- \connect $database;\n\n"; |
122 | } |
||
123 | |||
1116 | daniel-mar | 124 | /** |
125 | * @param string $expr1 |
||
126 | * @param string $expr2 |
||
127 | * @return string |
||
128 | */ |
||
129 | public function isNullFunction(string $expr1, string $expr2): string { |
||
635 | daniel-mar | 130 | return "coalesce($expr1, $expr2)"; |
131 | } |
||
132 | |||
1116 | daniel-mar | 133 | /** |
134 | * @param string $sql |
||
135 | * @return string |
||
136 | */ |
||
137 | public function filterQuery(string $sql): string { |
||
635 | daniel-mar | 138 | return $sql; |
139 | } |
||
140 | |||
1116 | daniel-mar | 141 | /** |
142 | * @param bool $bool |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getSQLBool(bool $bool): string { |
||
635 | daniel-mar | 146 | return $bool ? '1' : '0'; |
147 | } |
||
148 | |||
1116 | daniel-mar | 149 | /** |
150 | * @param string $str |
||
151 | * @return string |
||
152 | */ |
||
153 | public function escapeString(string $str): string { |
||
635 | daniel-mar | 154 | return str_replace("'", "''", $str); |
155 | } |
||
156 | } |