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 26... | Line 26... | ||
26 | class OIDplusDatabaseConnectionPgSql extends OIDplusDatabaseConnection { |
26 | class OIDplusDatabaseConnectionPgSql extends OIDplusDatabaseConnection { |
27 | private $conn = null; |
27 | private $conn = null; |
28 | private $already_prepared = array(); |
28 | private $already_prepared = array(); |
29 | private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior |
29 | private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior |
30 | 30 | ||
- | 31 | /** |
|
- | 32 | * @param string $sql |
|
- | 33 | * @param array|null $prepared_args |
|
- | 34 | * @return OIDplusQueryResultPgSql |
|
- | 35 | * @throws OIDplusException |
|
- | 36 | */ |
|
31 | public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
37 | public function doQuery(string $sql, array $prepared_args=null): OIDplusQueryResult { |
32 | $this->last_error = null; |
38 | $this->last_error = null; |
33 | if (is_null($prepared_args)) { |
39 | if (is_null($prepared_args)) { |
34 | $res = @pg_query($this->conn, $sql); |
40 | $res = @pg_query($this->conn, $sql); |
35 | 41 | ||
36 | if ($res === false) { |
42 | if ($res === false) { |
Line 72... | Line 78... | ||
72 | } |
78 | } |
73 | return new OIDplusQueryResultPgSql($ps); |
79 | return new OIDplusQueryResultPgSql($ps); |
74 | } |
80 | } |
75 | } |
81 | } |
76 | 82 | ||
- | 83 | /** |
|
- | 84 | * @return int |
|
- | 85 | */ |
|
77 | public function insert_id(): int { |
86 | public function insert_id(): int { |
78 | try { |
87 | try { |
79 | return (int)$this->query('select lastval() as id')->fetch_object()->id; |
88 | return (int)$this->query('select lastval() as id')->fetch_object()->id; |
80 | } catch (\Exception $e) { |
89 | } catch (\Exception $e) { |
81 | return 0; |
90 | return 0; |
82 | } |
91 | } |
83 | } |
92 | } |
84 | 93 | ||
- | 94 | /** |
|
- | 95 | * @return string |
|
- | 96 | */ |
|
85 | public function error(): string { |
97 | public function error(): string { |
86 | $err = $this->last_error; |
98 | $err = $this->last_error; |
87 | if ($err == null) $err = ''; |
99 | if ($err == null) $err = ''; |
88 | return $err; |
100 | return $err; |
89 | } |
101 | } |
90 | 102 | ||
- | 103 | /** |
|
- | 104 | * @return void |
|
- | 105 | * @throws OIDplusConfigInitializationException |
|
- | 106 | * @throws OIDplusException |
|
- | 107 | */ |
|
91 | protected function doConnect()/*: void*/ { |
108 | protected function doConnect()/*: void*/ { |
92 | if (!function_exists('pg_connect')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PostgreSQL')); |
109 | if (!function_exists('pg_connect')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PostgreSQL')); |
93 | 110 | ||
94 | // Try connecting to the database |
111 | // Try connecting to the database |
95 | ob_start(); |
112 | ob_start(); |
Line 133... | Line 150... | ||
133 | $this->query("SET NAMES 'utf8'"); |
150 | $this->query("SET NAMES 'utf8'"); |
134 | } catch (\Exception $e) { |
151 | } catch (\Exception $e) { |
135 | } |
152 | } |
136 | } |
153 | } |
137 | 154 | ||
- | 155 | /** |
|
- | 156 | * @return void |
|
- | 157 | */ |
|
138 | protected function doDisconnect()/*: void*/ { |
158 | protected function doDisconnect()/*: void*/ { |
139 | $this->already_prepared = array(); |
159 | $this->already_prepared = array(); |
140 | if (!is_null($this->conn)) { |
160 | if (!is_null($this->conn)) { |
141 | pg_close($this->conn); |
161 | pg_close($this->conn); |
142 | $this->conn = null; |
162 | $this->conn = null; |
143 | } |
163 | } |
144 | } |
164 | } |
145 | 165 | ||
- | 166 | /** |
|
- | 167 | * @var bool |
|
- | 168 | */ |
|
146 | private $intransaction = false; |
169 | private $intransaction = false; |
147 | 170 | ||
- | 171 | /** |
|
- | 172 | * @return bool |
|
- | 173 | */ |
|
148 | public function transaction_supported(): bool { |
174 | public function transaction_supported(): bool { |
149 | return true; |
175 | return true; |
150 | } |
176 | } |
151 | 177 | ||
- | 178 | /** |
|
- | 179 | * @return int |
|
- | 180 | */ |
|
152 | public function transaction_level(): int { |
181 | public function transaction_level(): int { |
153 | return $this->intransaction ? 1 : 0; |
182 | return $this->intransaction ? 1 : 0; |
154 | } |
183 | } |
155 | 184 | ||
- | 185 | /** |
|
- | 186 | * @return void |
|
- | 187 | * @throws OIDplusException |
|
- | 188 | */ |
|
156 | public function transaction_begin()/*: void*/ { |
189 | public function transaction_begin()/*: void*/ { |
157 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
190 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
158 | $this->query('begin transaction'); |
191 | $this->query('begin transaction'); |
159 | $this->intransaction = true; |
192 | $this->intransaction = true; |
160 | } |
193 | } |
161 | 194 | ||
- | 195 | /** |
|
- | 196 | * @return void |
|
- | 197 | * @throws OIDplusException |
|
- | 198 | */ |
|
162 | public function transaction_commit()/*: void*/ { |
199 | public function transaction_commit()/*: void*/ { |
163 | $this->query('commit'); |
200 | $this->query('commit'); |
164 | $this->intransaction = false; |
201 | $this->intransaction = false; |
165 | } |
202 | } |
166 | 203 | ||
- | 204 | /** |
|
- | 205 | * @return void |
|
- | 206 | * @throws OIDplusException |
|
- | 207 | */ |
|
167 | public function transaction_rollback()/*: void*/ { |
208 | public function transaction_rollback()/*: void*/ { |
168 | $this->query('rollback'); |
209 | $this->query('rollback'); |
169 | $this->intransaction = false; |
210 | $this->intransaction = false; |
170 | } |
211 | } |
171 | 212 | ||
- | 213 | /** |
|
- | 214 | * @return string |
|
- | 215 | */ |
|
172 | public function sqlDate(): string { |
216 | public function sqlDate(): string { |
173 | return 'now()'; |
217 | return 'now()'; |
174 | } |
218 | } |
175 | 219 | ||
- | 220 | /** |
|
- | 221 | * @param bool $mustExist |
|
- | 222 | * @return OIDplusSqlSlangPlugin|null |
|
- | 223 | * @throws OIDplusConfigInitializationException |
|
- | 224 | */ |
|
176 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
225 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
177 | $slang = OIDplus::getSqlSlangPlugin('pgsql'); |
226 | $slang = OIDplus::getSqlSlangPlugin('pgsql'); |
178 | if (is_null($slang)) { |
227 | if (is_null($slang)) { |
179 | throw new OIDplusConfigInitializationException(_L('SQL-Slang plugin "%1" is missing. Please check if it exists in the directory "plugin/sqlSlang". If it is not existing, please recover it from an SVN snapshot or OIDplus TAR.GZ file.','pgsql')); |
228 | throw new OIDplusConfigInitializationException(_L('SQL-Slang plugin "%1" is missing. Please check if it exists in the directory "plugin/sqlSlang". If it is not existing, please recover it from an SVN snapshot or OIDplus TAR.GZ file.','pgsql')); |
180 | } |
229 | } |