[ 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 MySQL Database Driver. 26 27 See the dbLayer documentation for common methods. 28 */ 29 class mysqlConnection extends dbLayer implements i_dbLayer 30 { 31 protected $__driver = 'mysql'; 32 33 public function db_connect($host,$user,$password,$database) 34 { 35 if (!function_exists('mysql_connect')) { 36 throw new Exception('PHP MySQL functions are not available'); 37 } 38 39 if (($link = @mysql_connect($host,$user,$password)) === false) { 40 throw new Exception('Unable to connect to database'); 41 } 42 43 $this->db_post_connect($link,$database); 44 45 return $link; 46 } 47 48 public function db_pconnect($host,$user,$password,$database) 49 { 50 if (!function_exists('mysql_pconnect')) { 51 throw new Exception('PHP MySQL functions are not available'); 52 } 53 54 if (($link = @mysql_pconnect($host,$user,$password)) === false) { 55 throw new Exception('Unable to connect to database'); 56 } 57 58 $this->db_post_connect($link,$database); 59 60 return $link; 61 } 62 63 private function db_post_connect($link,$database) 64 { 65 if (@mysql_select_db($database,$link) === false) { 66 throw new Exception('Unable to use database '.$database); 67 } 68 69 if (version_compare($this->db_version($link),'4.1','>=')) 70 { 71 $this->db_query($link,'SET NAMES utf8'); 72 $this->db_query($link,'SET CHARACTER SET utf8'); 73 $this->db_query($link,"SET COLLATION_CONNECTION = 'utf8_general_ci'"); 74 $this->db_query($link,"SET COLLATION_SERVER = 'utf8_general_ci'"); 75 $this->db_query($link,"SET CHARACTER_SET_SERVER = 'utf8'"); 76 $this->db_query($link,"SET CHARACTER_SET_DATABASE = 'utf8'"); 77 } 78 } 79 80 public function db_close($handle) 81 { 82 if (is_resource($handle)) { 83 mysql_close($handle); 84 } 85 } 86 87 public function db_version($handle) 88 { 89 if (is_resource($handle)) { 90 return mysql_get_server_info(); 91 } 92 return null; 93 } 94 95 public function db_query($handle,$query) 96 { 97 if (is_resource($handle)) 98 { 99 $res = @mysql_query($query,$handle); 100 if ($res === false) { 101 $e = new Exception($this->db_last_error($handle)); 102 $e->sql = $query; 103 throw $e; 104 } 105 return $res; 106 } 107 } 108 109 public function db_exec($handle,$query) 110 { 111 return $this->db_query($handle,$query); 112 } 113 114 public function db_num_fields($res) 115 { 116 if (is_resource($res)) { 117 return mysql_num_fields($res); 118 } 119 return 0; 120 } 121 122 public function db_num_rows($res) 123 { 124 if (is_resource($res)) { 125 return mysql_num_rows($res); 126 } 127 return 0; 128 } 129 130 public function db_field_name($res,$position) 131 { 132 if (is_resource($res)) { 133 return mysql_field_name($res,$position); 134 } 135 } 136 137 public function db_field_type($res,$position) 138 { 139 if (is_resource($res)) { 140 return mysql_field_type($res,$position); 141 } 142 } 143 144 public function db_fetch_assoc($res) 145 { 146 if (is_resource($res)) { 147 return mysql_fetch_assoc($res); 148 } 149 } 150 151 public function db_result_seek($res,$row) 152 { 153 if (is_resource($res)) { 154 return mysql_data_seek($res,$row); 155 } 156 } 157 158 public function db_changes($handle,$res) 159 { 160 if (is_resource($handle)) { 161 return mysql_affected_rows($handle); 162 } 163 } 164 165 public function db_get_tables($handle) 166 { 167 $sql = 'SHOW TABLES'; 168 $c = $this->db_query($handle,$sql); 169 170 $res = array(); 171 while (($f = $this->db_fetch_assoc($c)) !== false) { 172 $res[] = current($f); 173 } 174 return $res; 175 } 176 177 public function db_get_columns($handle,$table) 178 { 179 $sql = 'SHOW COLUMNS FROM '.$this->escapeSystem($table); 180 $c = $this->db_query($handle,$sql); 181 182 $res = array(); 183 while (($f = $this->db_fetch_assoc($c)) !== false) { 184 $res[$f['Field']] = $f['Type']; 185 } 186 return $res; 187 } 188 189 public function db_last_error($handle) 190 { 191 if (is_resource($handle)) 192 { 193 $e = mysql_error($handle); 194 if ($e) { 195 return $e.' ('.mysql_errno($handle).')'; 196 } 197 } 198 return false; 199 } 200 201 public function db_escape_string($str,$handle=null) 202 { 203 if (is_resource($handle)) { 204 return mysql_real_escape_string($str,$handle); 205 } else { 206 return mysql_escape_string($str); 207 } 208 } 209 210 public function vacuum($table) 211 { 212 $this->execute('OPTIMIZE TABLE '.$this->escapeSystem($table)); 213 } 214 215 public function dateFormat($field,$pattern) 216 { 217 $pattern = str_replace('%M','%i',$pattern); 218 219 return 'DATE_FORMAT('.$field.','."'".$this->escape($pattern)."') "; 220 } 221 222 public function concat() 223 { 224 $args = func_get_args(); 225 return 'CONCAT('.implode(',',$args).')'; 226 } 227 228 public function escapeSystem($str) 229 { 230 return '`'.$str.'`'; 231 } 232 } 233 ?>
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 |