Mercurial > hg > rc2
annotate program/lib/Roundcube/rcube_db_pgsql.php @ 19:b6a96bdd6b29
More cleaning up php8 Warnings/deprecations
| author | Charlie Root |
|---|---|
| date | Wed, 08 Oct 2025 08:50:44 -0400 |
| parents | b631c1c7a3ce |
| children |
| rev | line source |
|---|---|
| 0 | 1 <?php |
| 2 | |
| 3 /** | |
| 4 +-----------------------------------------------------------------------+ | |
| 5 | This file is part of the Roundcube Webmail client | | |
| 6 | Copyright (C) 2005-2017, The Roundcube Dev Team | | |
| 7 | | | |
| 8 | Licensed under the GNU General Public License version 3 or | | |
| 9 | any later version with exceptions for skins & plugins. | | |
| 10 | See the README file for a full license statement. | | |
| 11 | | | |
| 12 | PURPOSE: | | |
| 13 | Database wrapper class that implements PHP PDO functions | | |
| 14 | for PostgreSQL database | | |
| 15 +-----------------------------------------------------------------------+ | |
| 16 | Author: Aleksander Machniak <alec@alec.pl> | | |
| 17 +-----------------------------------------------------------------------+ | |
| 18 */ | |
| 19 | |
| 20 /** | |
| 21 * Database independent query interface | |
| 22 * This is a wrapper for the PHP PDO | |
| 23 * | |
| 24 * @package Framework | |
| 25 * @subpackage Database | |
| 26 */ | |
| 27 class rcube_db_pgsql extends rcube_db | |
| 28 { | |
| 29 public $db_provider = 'postgres'; | |
| 30 | |
| 31 /** | |
| 32 * Object constructor | |
| 33 * | |
| 34 * @param string $db_dsnw DSN for read/write operations | |
| 35 * @param string $db_dsnr Optional DSN for read only operations | |
| 36 * @param bool $pconn Enables persistent connections | |
| 37 */ | |
| 38 public function __construct($db_dsnw, $db_dsnr = '', $pconn = false) | |
| 39 { | |
| 40 parent::__construct($db_dsnw, $db_dsnr, $pconn); | |
| 41 | |
| 42 // use date/time input format with timezone spec. | |
| 43 $this->options['datetime_format'] = 'c'; | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * Driver-specific configuration of database connection | |
| 48 * | |
| 49 * @param array $dsn DSN for DB connections | |
| 50 * @param PDO $dbh Connection handler | |
| 51 */ | |
| 52 protected function conn_configure($dsn, $dbh) | |
| 53 { | |
| 54 $dbh->query("SET NAMES 'utf8'"); | |
| 55 $dbh->query("SET DATESTYLE TO ISO"); | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Get last inserted record ID | |
| 60 * | |
| 61 * @param string $table Table name (to find the incremented sequence) | |
| 62 * | |
| 63 * @return mixed ID or false on failure | |
| 64 */ | |
| 65 public function insert_id($table = null) | |
| 66 { | |
| 67 if (!$this->db_connected || $this->db_mode == 'r') { | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 if ($table) { | |
| 72 $table = $this->sequence_name($table); | |
| 73 } | |
| 74 | |
| 75 $id = $this->dbh->lastInsertId($table); | |
| 76 | |
| 77 return $id; | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Return correct name for a specific database sequence | |
| 82 * | |
| 83 * @param string $table Table name | |
| 84 * | |
| 85 * @return string Translated sequence name | |
| 86 */ | |
| 87 protected function sequence_name($table) | |
| 88 { | |
| 89 // Note: we support only one sequence per table | |
| 90 // Note: The sequence name must be <table_name>_seq | |
| 91 $sequence = $table . '_seq'; | |
| 92 | |
| 93 // modify sequence name if prefix is configured | |
| 94 if ($prefix = $this->options['table_prefix']) { | |
| 95 return $prefix . $sequence; | |
| 96 } | |
| 97 | |
| 98 return $sequence; | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Return SQL statement to convert a field value into a unix timestamp | |
| 103 * | |
| 104 * @param string $field Field name | |
| 105 * | |
| 106 * @return string SQL statement to use in query | |
| 107 * @deprecated | |
| 108 */ | |
| 109 public function unixtimestamp($field) | |
| 110 { | |
| 111 return "EXTRACT (EPOCH FROM $field)"; | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Return SQL function for current time and date | |
| 116 * | |
| 117 * @param int $interval Optional interval (in seconds) to add/subtract | |
| 118 * | |
| 119 * @return string SQL function to use in query | |
| 120 */ | |
| 121 public function now($interval = 0) | |
| 122 { | |
|
18
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
123 $result = 'now()'; |
| 0 | 124 |
|
18
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
125 if ($interval) { |
|
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
126 $result .= ' ' . ($interval > 0 ? '+' : '-') . " interval '"; |
|
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
127 $result .= $interval > 0 ? intval($interval) : intval($interval) * -1; |
|
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
128 $result .= " seconds'"; |
|
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
129 } |
|
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
130 |
|
b631c1c7a3ce
More cleaning up more php8 Warnings/deprecations,
Charlie Root
parents:
0
diff
changeset
|
131 return $result; |
| 0 | 132 } |
| 133 | |
| 134 /** | |
| 135 * Return SQL statement for case insensitive LIKE | |
| 136 * | |
| 137 * @param string $column Field name | |
| 138 * @param string $value Search value | |
| 139 * | |
| 140 * @return string SQL statement to use in query | |
| 141 */ | |
| 142 public function ilike($column, $value) | |
| 143 { | |
| 144 return $this->quote_identifier($column) . ' ILIKE ' . $this->quote($value); | |
| 145 } | |
| 146 | |
| 147 /** | |
| 148 * Get database runtime variables | |
| 149 * | |
| 150 * @param string $varname Variable name | |
| 151 * @param mixed $default Default value if variable is not set | |
| 152 * | |
| 153 * @return mixed Variable value or default | |
| 154 */ | |
| 155 public function get_variable($varname, $default = null) | |
| 156 { | |
| 157 // There's a known case when max_allowed_packet is queried | |
| 158 // PostgreSQL doesn't have such limit, return immediately | |
| 159 if ($varname == 'max_allowed_packet') { | |
| 160 return rcube::get_instance()->config->get('db_' . $varname, $default); | |
| 161 } | |
| 162 | |
| 163 $this->variables[$varname] = rcube::get_instance()->config->get('db_' . $varname); | |
| 164 | |
| 165 if (!isset($this->variables)) { | |
| 166 $this->variables = array(); | |
| 167 | |
| 168 $result = $this->query('SHOW ALL'); | |
| 169 | |
| 170 while ($row = $this->fetch_array($result)) { | |
| 171 $this->variables[$row[0]] = $row[1]; | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 return isset($this->variables[$varname]) ? $this->variables[$varname] : $default; | |
| 176 } | |
| 177 | |
| 178 /** | |
| 179 * Returns list of tables in a database | |
| 180 * | |
| 181 * @return array List of all tables of the current database | |
| 182 */ | |
| 183 public function list_tables() | |
| 184 { | |
| 185 // get tables if not cached | |
| 186 if ($this->tables === null) { | |
| 187 $q = $this->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES" | |
| 188 . " WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA NOT IN ('pg_catalog', 'information_schema')" | |
| 189 . " ORDER BY TABLE_NAME"); | |
| 190 | |
| 191 $this->tables = $q ? $q->fetchAll(PDO::FETCH_COLUMN, 0) : array(); | |
| 192 } | |
| 193 | |
| 194 return $this->tables; | |
| 195 } | |
| 196 | |
| 197 /** | |
| 198 * Returns PDO DSN string from DSN array | |
| 199 * | |
| 200 * @param array $dsn DSN parameters | |
| 201 * | |
| 202 * @return string DSN string | |
| 203 */ | |
| 204 protected function dsn_string($dsn) | |
| 205 { | |
| 206 $params = array(); | |
| 207 $result = 'pgsql:'; | |
| 208 | |
| 209 if ($dsn['hostspec']) { | |
| 210 $params[] = 'host=' . $dsn['hostspec']; | |
| 211 } | |
| 212 else if ($dsn['socket']) { | |
| 213 $params[] = 'host=' . $dsn['socket']; | |
| 214 } | |
| 215 | |
| 216 if ($dsn['port']) { | |
| 217 $params[] = 'port=' . $dsn['port']; | |
| 218 } | |
| 219 | |
| 19 | 220 if (!empty($dsn['database'])) { |
| 0 | 221 $params[] = 'dbname=' . $dsn['database']; |
| 222 } | |
| 223 | |
| 224 if (!empty($params)) { | |
| 225 $result .= implode(';', $params); | |
| 226 } | |
| 227 | |
| 228 return $result; | |
| 229 } | |
| 230 | |
| 231 /** | |
| 232 * Parse SQL file and fix table names according to table prefix | |
| 233 */ | |
| 234 protected function fix_table_names($sql) | |
| 235 { | |
| 236 if (!$this->options['table_prefix']) { | |
| 237 return $sql; | |
| 238 } | |
| 239 | |
| 240 $sql = parent::fix_table_names($sql); | |
| 241 | |
| 242 // replace sequence names, and other postgres-specific commands | |
| 243 $sql = preg_replace_callback( | |
| 244 '/((SEQUENCE |RENAME TO |nextval\()["\']*)([^"\' \r\n]+)/', | |
| 245 array($this, 'fix_table_names_callback'), | |
| 246 $sql | |
| 247 ); | |
| 248 | |
| 249 return $sql; | |
| 250 } | |
| 251 } |
