| [ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 /** 24 @ingroup CB_DBLAYER 25 @brief PostgreSQL Database Driver. 26 27 See the dbLayer documentation for common methods. 28 29 This class adds a method for PostgreSQL only: callFunction(). 30 */ 31 class pgsqlConnection extends dbLayer implements i_dbLayer 32 { 33 protected $__driver = 'pgsql'; 34 35 private function get_connection_string($host,$user,$password,$database) 36 { 37 $str = ''; 38 $port = false; 39 40 if ($host) 41 { 42 if (strpos($host,':') !== false) { 43 $bits = explode(':',$host); 44 $host = array_shift($bits); 45 $port = abs((integer) array_shift($bits)); 46 } 47 $str .= "host = '".addslashes($host)."' "; 48 49 if ($port) { 50 $str .= 'port = '.$port.' '; 51 } 52 } 53 if ($user) { 54 $str .= "user = '".addslashes($user)."' "; 55 } 56 if ($password) { 57 $str .= "password = '".addslashes($password)."' "; 58 } 59 if ($database) { 60 $str .= "dbname = '".addslashes($database)."' "; 61 } 62 63 return $str; 64 } 65 66 public function db_connect($host,$user,$password,$database) 67 { 68 if (!function_exists('pg_connect')) { 69 throw new Exception('PHP PostgreSQL functions are not available'); 70 } 71 72 $str = $this->get_connection_string($host,$user,$password,$database); 73 74 if (($link = @pg_connect($str)) === false) { 75 throw new Exception('Unable to connect to database'); 76 } 77 78 return $link; 79 } 80 81 public function db_pconnect($host,$user,$password,$database) 82 { 83 if (!function_exists('pg_pconnect')) { 84 throw new Exception('PHP PostgreSQL functions are not available'); 85 } 86 87 $str = $this->get_connection_string($host,$user,$password,$database); 88 89 if (($link = @pg_pconnect($str)) === false) { 90 throw new Exception('Unable to connect to database'); 91 } 92 93 return $link; 94 } 95 96 public function db_close($handle) 97 { 98 if (is_resource($handle)) { 99 pg_close($handle); 100 } 101 } 102 103 public function db_version($handle) 104 { 105 if (is_resource($handle)) 106 { 107 $v = pg_version($handle); 108 if (isset($v['server'])) { 109 return $v['server']; 110 } 111 } 112 return null; 113 } 114 115 public function db_query($handle,$query) 116 { 117 if (is_resource($handle)) 118 { 119 $res = @pg_query($handle,$query); 120 if ($res === false) { 121 $e = new Exception($this->db_last_error($handle)); 122 $e->sql = $query; 123 throw $e; 124 } 125 return $res; 126 } 127 } 128 129 public function db_exec($handle,$query) 130 { 131 return $this->db_query($handle,$query); 132 } 133 134 public function db_num_fields($res) 135 { 136 if (is_resource($res)) { 137 return pg_num_fields($res); 138 } 139 return 0; 140 } 141 142 public function db_num_rows($res) 143 { 144 if (is_resource($res)) { 145 return pg_num_rows($res); 146 } 147 return 0; 148 } 149 150 public function db_field_name($res,$position) 151 { 152 if (is_resource($res)) { 153 return pg_field_name($res,$position); 154 } 155 } 156 157 public function db_field_type($res,$position) 158 { 159 if (is_resource($res)) { 160 return pg_field_type($res,$position); 161 } 162 } 163 164 public function db_fetch_assoc($res) 165 { 166 if (is_resource($res)) { 167 return pg_fetch_assoc($res); 168 } 169 } 170 171 public function db_result_seek($res,$row) 172 { 173 if (is_resource($res)) { 174 return pg_result_seek($res,(int) $row); 175 } 176 return false; 177 } 178 179 public function db_changes($handle,$res) 180 { 181 if (is_resource($handle) && is_resource($res)) { 182 return pg_affected_rows($res); 183 } 184 } 185 186 public function db_get_tables($handle) 187 { 188 $sql = " 189 SELECT table_name 190 FROM information_schema.tables 191 WHERE table_schema='public' 192 AND table_type='BASE TABLE' 193 ORDER BY table_name 194 "; 195 196 $c = $this->db_query($handle,$sql); 197 198 $res = array(); 199 while (($f = $this->db_fetch_assoc($c)) !== false) { 200 $res[] = $f['table_name']; 201 } 202 return $res; 203 } 204 205 public function db_get_columns($handle,$table) 206 { 207 $sql = " 208 SELECT column_name AS name, udt_name AS type 209 FROM information_schema.columns 210 WHERE table_name = '".$this->db_escape_string($table)."' 211 "; 212 213 $c = $this->db_query($handle,$sql); 214 215 $res = array(); 216 while (($f = $this->db_fetch_assoc($c)) !== false) { 217 $res[$f['name']] = $f['type']; 218 } 219 return $res; 220 } 221 222 public function db_last_error($handle) 223 { 224 if (is_resource($handle)) { 225 return pg_last_error($handle); 226 } 227 return false; 228 } 229 230 public function db_escape_string($str,$handle=null) 231 { 232 return pg_escape_string($str); 233 } 234 235 public function vacuum($table) 236 { 237 $this->execute('VACUUM FULL '.$this->escapeSystem($table)); 238 } 239 240 public function dateFormat($field,$pattern) 241 { 242 $rep = array( 243 '%d' => 'DD', 244 '%H' => 'HH24', 245 '%M' => 'MI', 246 '%m' => 'MM', 247 '%S' => 'SS', 248 '%Y' => 'YYYY' 249 ); 250 251 $pattern = str_replace(array_keys($rep),array_values($rep),$pattern); 252 253 return 'TO_CHAR('.$field.','."'".$this->escape($pattern)."') "; 254 } 255 256 /** 257 Calls a PostgreSQL function an returns the result as a record. After 258 <var>$name</var>, you can add any parameters you want to append them to 259 the PostgreSQL function. You don't need to escape string in arguments. 260 261 @param name <b>string</b> Function name 262 @return <b>record</b> 263 */ 264 public function callFunction($name) 265 { 266 $data = func_get_args(); 267 array_shift($data); 268 269 foreach ($data as $k => $v) 270 { 271 if (is_null($v)) { 272 $data[$k] = 'NULL'; 273 } elseif (is_string($v)) { 274 $data[$k] = "'".$this->escape($v)."'"; 275 } elseif (is_array($v)) { 276 $data[$k] = $v[0]; 277 } else { 278 $data[$k] = $v; 279 } 280 } 281 282 $req = 283 'SELECT '.$name."(\n". 284 implode(",\n",array_values($data)). 285 "\n) "; 286 287 return $this->select($req); 288 } 289 } 290 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |