Rev 1086 | Rev 1130 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1086 | Rev 1116 | ||
---|---|---|---|
Line 23... | Line 23... | ||
23 | \defined('INSIDE_OIDPLUS') or die; |
23 | \defined('INSIDE_OIDPLUS') or die; |
24 | // phpcs:enable PSR1.Files.SideEffects |
24 | // phpcs:enable PSR1.Files.SideEffects |
25 | 25 | ||
26 | class OIDplusSqlSlangPluginPgSQL extends OIDplusSqlSlangPlugin { |
26 | class OIDplusSqlSlangPluginPgSQL extends OIDplusSqlSlangPlugin { |
27 | 27 | ||
- | 28 | /** |
|
- | 29 | * @return string |
|
- | 30 | */ |
|
28 | public static function id(): string { |
31 | public static function id(): string { |
29 | return 'pgsql'; |
32 | return 'pgsql'; |
30 | } |
33 | } |
31 | 34 | ||
- | 35 | /** |
|
- | 36 | * @param string $fieldname |
|
- | 37 | * @param string $order |
|
- | 38 | * @return string |
|
- | 39 | * @throws OIDplusException |
|
- | 40 | */ |
|
32 | public function natOrder($fieldname, $order='asc'): string { |
41 | public function natOrder(string $fieldname, string $order='asc'): string { |
33 | 42 | ||
34 | $order = strtolower($order); |
43 | $order = strtolower($order); |
35 | if (($order != 'asc') && ($order != 'desc')) { |
44 | if (($order != 'asc') && ($order != 'desc')) { |
36 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
45 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
37 | } |
46 | } |
Line 51... | Line 60... | ||
51 | 60 | ||
52 | return implode(', ', $out); |
61 | return implode(', ', $out); |
53 | 62 | ||
54 | } |
63 | } |
55 | 64 | ||
- | 65 | /** |
|
- | 66 | * @return string |
|
- | 67 | */ |
|
56 | public function sqlDate(): string { |
68 | public function sqlDate(): string { |
57 | return 'now()'; |
69 | return 'now()'; |
58 | } |
70 | } |
59 | 71 | ||
- | 72 | /** |
|
- | 73 | * @param OIDplusDatabaseConnection $db |
|
- | 74 | * @return bool |
|
- | 75 | */ |
|
60 | public function detect(OIDplusDatabaseConnection $db): bool { |
76 | public function detect(OIDplusDatabaseConnection $db): bool { |
61 | try { |
77 | try { |
62 | $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version; |
78 | $vers = $db->query("select version() as dbms_version")->fetch_object()->dbms_version; |
63 | $vers = strtolower($vers); |
79 | $vers = strtolower($vers); |
64 | return strpos($vers, 'postgresql') !== false; |
80 | return strpos($vers, 'postgresql') !== false; |
65 | } catch (\Exception $e) { |
81 | } catch (\Exception $e) { |
66 | return false; |
82 | return false; |
67 | } |
83 | } |
68 | } |
84 | } |
69 | 85 | ||
- | 86 | /** |
|
- | 87 | * @param OIDplusDatabaseConnection $db |
|
- | 88 | * @return int |
|
- | 89 | * @throws OIDplusException |
|
- | 90 | */ |
|
70 | public function insert_id(OIDplusDatabaseConnection $db): int { |
91 | public function insert_id(OIDplusDatabaseConnection $db): int { |
71 | $res = $db->query("SELECT LASTVAL() AS ID"); |
92 | $res = $db->query("SELECT LASTVAL() AS ID"); |
72 | $row = $res->fetch_array(); |
93 | $row = $res->fetch_array(); |
73 | return (int)$row['ID']; |
94 | return (int)$row['ID']; |
74 | } |
95 | } |
75 | 96 | ||
- | 97 | /** |
|
- | 98 | * @param string $cont |
|
- | 99 | * @param string $table |
|
- | 100 | * @param string $prefix |
|
- | 101 | * @return string |
|
- | 102 | */ |
|
76 | public function setupSetTablePrefix($cont, $table, $prefix): string { |
103 | public function setupSetTablePrefix(string $cont, string $table, string $prefix): string { |
77 | $cont = str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont); |
104 | $cont = str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont); |
78 | $cont = str_replace('"index_'.$table, '"index_'.$prefix.$table, $cont); |
105 | $cont = str_replace('"index_'.$table, '"index_'.$prefix.$table, $cont); |
79 | return $cont; |
106 | return $cont; |
80 | } |
107 | } |
81 | 108 | ||
- | 109 | /** |
|
- | 110 | * @param string $database |
|
- | 111 | * @return string |
|
- | 112 | */ |
|
82 | public function setupCreateDbIfNotExists($database): string { |
113 | public function setupCreateDbIfNotExists(string $database): string { |
83 | return "-- CREATE DATABASE $database;\n\n"; |
114 | return "-- CREATE DATABASE $database;\n\n"; |
84 | } |
115 | } |
85 | 116 | ||
- | 117 | /** |
|
- | 118 | * @param string $database |
|
- | 119 | * @return string |
|
- | 120 | */ |
|
86 | public function setupUseDatabase($database): string { |
121 | public function setupUseDatabase(string $database): string { |
87 | return "-- \connect $database;\n\n"; |
122 | return "-- \connect $database;\n\n"; |
88 | } |
123 | } |
89 | 124 | ||
- | 125 | /** |
|
- | 126 | * @param string $expr1 |
|
- | 127 | * @param string $expr2 |
|
- | 128 | * @return string |
|
- | 129 | */ |
|
90 | public function isNullFunction($expr1, $expr2): string { |
130 | public function isNullFunction(string $expr1, string $expr2): string { |
91 | return "coalesce($expr1, $expr2)"; |
131 | return "coalesce($expr1, $expr2)"; |
92 | } |
132 | } |
93 | 133 | ||
- | 134 | /** |
|
- | 135 | * @param string $sql |
|
- | 136 | * @return string |
|
- | 137 | */ |
|
94 | public function filterQuery($sql): string { |
138 | public function filterQuery(string $sql): string { |
95 | return $sql; |
139 | return $sql; |
96 | } |
140 | } |
97 | 141 | ||
- | 142 | /** |
|
- | 143 | * @param bool $bool |
|
- | 144 | * @return string |
|
- | 145 | */ |
|
98 | public function getSQLBool($bool): string { |
146 | public function getSQLBool(bool $bool): string { |
99 | return $bool ? '1' : '0'; |
147 | return $bool ? '1' : '0'; |
100 | } |
148 | } |
101 | 149 | ||
- | 150 | /** |
|
- | 151 | * @param string $str |
|
- | 152 | * @return string |
|
- | 153 | */ |
|
102 | public function escapeString($str): string { |
154 | public function escapeString(string $str): string { |
103 | return str_replace("'", "''", $str); |
155 | return str_replace("'", "''", $str); |
104 | } |
156 | } |
105 | } |
157 | } |