Rev 348 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
295 | daniel-mar | 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 OIDplusDatabaseConnectionPgSql extends OIDplusDatabaseConnection { |
||
21 | private $conn = null; |
||
22 | private $already_prepared = array(); |
||
23 | private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior |
||
24 | |||
348 | daniel-mar | 25 | public static function getPlugin(): OIDplusDatabasePlugin { |
26 | return new OIDplusDatabasePluginPgSql(); |
||
27 | } |
||
28 | |||
295 | daniel-mar | 29 | public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
30 | $this->last_error = null; |
||
31 | if (is_null($prepared_args)) { |
||
32 | $res = @pg_query($this->conn, $sql); |
||
33 | |||
34 | if ($res === false) { |
||
35 | $this->last_error = pg_last_error($this->conn); |
||
36 | throw new OIDplusSQLException($sql, $this->error()); |
||
37 | } else { |
||
38 | return new OIDplusQueryResultPgSql($res); |
||
39 | } |
||
40 | } else { |
||
41 | if (!is_array($prepared_args)) { |
||
360 | daniel-mar | 42 | throw new OIDplusException(_L('"prepared_args" must be either NULL or an ARRAY.')); |
295 | daniel-mar | 43 | } |
44 | |||
45 | // convert ? ? ? to $1 $2 $3 |
||
46 | $sql = preg_replace_callback('@\\?@', function($found) { |
||
47 | static $i = 0; |
||
48 | $i++; |
||
49 | return '$'.$i; |
||
50 | }, $sql); |
||
51 | |||
52 | $prepare_name = 'OIDplus_ps_'.sha1($sql); |
||
53 | if (!in_array($prepare_name, $this->already_prepared)) { |
||
54 | $res = @pg_prepare($this->conn, $prepare_name, $sql); |
||
55 | if ($res === false) { |
||
56 | $this->last_error = pg_last_error($this->conn); |
||
360 | daniel-mar | 57 | throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error()); |
295 | daniel-mar | 58 | } |
59 | $this->already_prepared[] = $prepare_name; |
||
60 | } |
||
61 | |||
62 | foreach ($prepared_args as &$value) { |
||
63 | if (is_bool($value)) $value = $value ? '1' : '0'; |
||
64 | } |
||
65 | |||
66 | $ps = pg_execute($this->conn, $prepare_name, $prepared_args); |
||
67 | if ($ps === false) { |
||
68 | $this->last_error = pg_last_error($this->conn); |
||
69 | throw new OIDplusSQLException($sql, $this->error()); |
||
70 | } |
||
71 | return new OIDplusQueryResultPgSql($ps); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | public function insert_id(): int { |
||
76 | try { |
||
77 | return (int)$this->query('select lastval() as id')->fetch_object()->id; |
||
78 | } catch (Exception $e) { |
||
79 | return 0; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | public function error(): string { |
||
84 | $err = $this->last_error; |
||
85 | if ($err == null) $err = ''; |
||
86 | return $err; |
||
87 | } |
||
88 | |||
89 | protected function doConnect()/*: void*/ { |
||
360 | daniel-mar | 90 | if (!function_exists('pg_connect')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PostgreSQL')); |
295 | daniel-mar | 91 | |
92 | // Try connecting to the database |
||
93 | ob_start(); |
||
94 | $err = ''; |
||
95 | try { |
||
96 | $host = OIDplus::baseConfig()->getValue('PGSQL_HOST', 'localhost:5432'); |
||
97 | $username = OIDplus::baseConfig()->getValue('PGSQL_USERNAME', 'postgres'); |
||
98 | $password = OIDplus::baseConfig()->getValue('PGSQL_PASSWORD', ''); |
||
99 | $database = OIDplus::baseConfig()->getValue('PGSQL_DATABASE', 'oidplus'); |
||
100 | list($hostname, $port) = explode(':', "$host:5432"); |
||
316 | daniel-mar | 101 | // We need to use PGSQL_CONNECT_FORCE_NEW because we require two connectoins (for isolated log message queries) |
102 | $this->conn = pg_connect("host=$hostname user=$username password=$password port=$port dbname=$database", PGSQL_CONNECT_FORCE_NEW); |
||
295 | daniel-mar | 103 | } finally { |
104 | # TODO: this does not seem to work?! (at least not for CLI) |
||
105 | $err = ob_get_contents(); |
||
106 | ob_end_clean(); |
||
107 | } |
||
108 | |||
109 | if (!$this->conn) { |
||
360 | daniel-mar | 110 | throw new OIDplusConfigInitializationException(_L('Connection to the database failed!').' ' . strip_tags($err)); |
295 | daniel-mar | 111 | } |
112 | |||
113 | $this->already_prepared = array(); |
||
114 | $this->last_error = null; |
||
115 | |||
116 | try { |
||
117 | $this->query("SET NAMES 'utf8'"); |
||
118 | } catch (Exception $e) { |
||
119 | } |
||
120 | } |
||
121 | |||
122 | protected function doDisconnect()/*: void*/ { |
||
123 | $this->already_prepared = array(); |
||
124 | if (!is_null($this->conn)) { |
||
125 | pg_close($this->conn); |
||
126 | $this->conn = null; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | private $intransaction = false; |
||
131 | |||
132 | public function transaction_supported(): bool { |
||
133 | return true; |
||
134 | } |
||
135 | |||
136 | public function transaction_level(): int { |
||
137 | return $this->intransaction ? 1 : 0; |
||
138 | } |
||
139 | |||
140 | public function transaction_begin()/*: void*/ { |
||
360 | daniel-mar | 141 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
295 | daniel-mar | 142 | $this->query('begin transaction'); |
143 | $this->intransaction = true; |
||
144 | } |
||
145 | |||
146 | public function transaction_commit()/*: void*/ { |
||
147 | $this->query('commit'); |
||
148 | $this->intransaction = false; |
||
149 | } |
||
150 | |||
151 | public function transaction_rollback()/*: void*/ { |
||
152 | $this->query('rollback'); |
||
153 | $this->intransaction = false; |
||
154 | } |
||
155 | |||
156 | public function sqlDate(): string { |
||
157 | return 'now()'; |
||
158 | } |
||
316 | daniel-mar | 159 | |
160 | public function getSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
||
318 | daniel-mar | 161 | $slang = OIDplus::getSqlSlangPlugin('pgsql'); |
316 | daniel-mar | 162 | if (is_null($slang)) { |
360 | daniel-mar | 163 | 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 ZIP file.','pgsql')); |
316 | daniel-mar | 164 | } |
165 | return $slang; |
||
166 | } |
||
360 | daniel-mar | 167 | } |