| [ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: dbo_mysql.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 /** 4 * MySQL layer for DBO 5 * 6 * Long description for file 7 * 8 * PHP versions 4 and 5 9 * 10 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 11 * Copyright 2005-2007, Cake Software Foundation, Inc. 12 * 1785 E. Sahara Avenue, Suite 490-204 13 * Las Vegas, Nevada 89104 14 * 15 * Licensed under The MIT License 16 * Redistributions of files must retain the above copyright notice. 17 * 18 * @filesource 19 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 20 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 21 * @package cake 22 * @subpackage cake.cake.libs.model.dbo 23 * @since CakePHP(tm) v 0.10.5.1790 24 * @version $Revision: 4409 $ 25 * @modifiedby $LastChangedBy: phpnut $ 26 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 27 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 28 */ 29 /** 30 * Include DBO. 31 */ 32 uses('model'.DS.'datasources'.DS.'dbo_source'); 33 /** 34 * Short description for class. 35 * 36 * Long description for class 37 * 38 * @package cake 39 * @subpackage cake.cake.libs.model.dbo 40 */ 41 class DboMysql extends DboSource { 42 /** 43 * Enter description here... 44 * 45 * @var unknown_type 46 */ 47 var $description = "MySQL DBO Driver"; 48 /** 49 * Enter description here... 50 * 51 * @var unknown_type 52 */ 53 var $startQuote = "`"; 54 /** 55 * Enter description here... 56 * 57 * @var unknown_type 58 */ 59 var $endQuote = "`"; 60 /** 61 * Base configuration settings for MySQL driver 62 * 63 * @var array 64 */ 65 var $_baseConfig = array('persistent' => true, 66 'host' => 'localhost', 67 'login' => 'root', 68 'password' => '', 69 'database' => 'cake', 70 'port' => '3306', 71 'connect' => 'mysql_pconnect'); 72 /** 73 * MySQL column definition 74 * 75 * @var array 76 */ 77 var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'), 78 'string' => array('name' => 'varchar', 'limit' => '255'), 79 'text' => array('name' => 'text'), 80 'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), 81 'float' => array('name' => 'float', 'formatter' => 'floatval'), 82 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 83 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 84 'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'), 85 'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'), 86 'year' => array('name' => 'year', 'format' => 'Y', 'formatter' => 'date'), 87 'binary' => array('name' => 'blob'), 88 'boolean' => array('name' => 'tinyint', 'limit' => '1')); 89 /** 90 * Connects to the database using options in the given configuration array. 91 * 92 * @return boolean True if the database could be connected, else false 93 */ 94 function connect() { 95 $config = $this->config; 96 $connect = $config['connect']; 97 $this->connected = false; 98 99 if (!$config['persistent']) { 100 $this->connection = mysql_connect($config['host'], $config['login'], $config['password'], true); 101 } else { 102 $this->connection = $connect($config['host'], $config['login'], $config['password']); 103 } 104 105 if (mysql_select_db($config['database'], $this->connection)) { 106 $this->connected = true; 107 } 108 return $this->connected; 109 } 110 /** 111 * Disconnects from database. 112 * 113 * @return boolean True if the database could be disconnected, else false 114 */ 115 function disconnect() { 116 @mysql_free_result($this->results); 117 $this->connected = !@mysql_close($this->connection); 118 return !$this->connected; 119 } 120 /** 121 * Executes given SQL statement. 122 * 123 * @param string $sql SQL statement 124 * @return resource Result resource identifier 125 * @access protected 126 */ 127 function _execute($sql) { 128 return mysql_query($sql, $this->connection); 129 } 130 /** 131 * Returns an array of sources (tables) in the database. 132 * 133 * @return array Array of tablenames in the database 134 */ 135 function listSources() { 136 $cache = parent::listSources(); 137 if ($cache != null) { 138 return $cache; 139 } 140 141 $result = mysql_list_tables($this->config['database'], $this->connection); 142 if (!$result) { 143 return array(); 144 } else { 145 146 $tables = array(); 147 while ($line = mysql_fetch_array($result)) { 148 $tables[] = $line[0]; 149 } 150 151 parent::listSources($tables); 152 return $tables; 153 } 154 } 155 /** 156 * Returns an array of the fields in given table name. 157 * 158 * @param string $tableName Name of database table to inspect 159 * @return array Fields in table. Keys are name and type 160 */ 161 function describe(&$model) { 162 163 $cache = parent::describe($model); 164 if ($cache != null) { 165 return $cache; 166 } 167 168 $fields = false; 169 $cols = $this->query('DESC ' . $this->fullTableName($model)); 170 171 foreach ($cols as $column) { 172 $colKey = array_keys($column); 173 if (isset($column[$colKey[0]]) && !isset($column[0])) { 174 $column[0] = $column[$colKey[0]]; 175 } 176 if (isset($column[0])) { 177 $fields[] = array('name' => $column[0]['Field'], 178 'type' => $this->column($column[0]['Type']), 179 'null' => $column[0]['Null']); 180 } 181 } 182 183 $this->__cacheDescription($model->tablePrefix.$model->table, $fields); 184 return $fields; 185 } 186 /** 187 * Returns a quoted name of $data for use in an SQL statement. 188 * 189 * @param string $data Name (table.field) to be prepared for use in an SQL statement 190 * @return string Quoted for MySQL 191 */ 192 function name($data) { 193 if ($data == '*') { 194 return '*'; 195 } 196 $pos = strpos($data, '`'); 197 if ($pos === false) { 198 $data = '`'. str_replace('.', '`.`', $data) .'`'; 199 } 200 return $data; 201 } 202 /** 203 * Returns a quoted and escaped string of $data for use in an SQL statement. 204 * 205 * @param string $data String to be prepared for use in an SQL statement 206 * @param string $column The column into which this data will be inserted 207 * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided 208 * @return string Quoted and escaped data 209 */ 210 function value($data, $column = null, $safe = false) { 211 $parent = parent::value($data, $column, $safe); 212 213 if ($parent != null) { 214 return $parent; 215 } 216 217 if ($data === null) { 218 return 'NULL'; 219 } 220 221 if($data === '') { 222 return "''"; 223 } 224 225 switch ($column) { 226 case 'boolean': 227 $data = $this->boolean((bool)$data); 228 break; 229 default: 230 $data = mysql_real_escape_string($data, $this->connection); 231 break; 232 } 233 234 return "'" . $data . "'"; 235 } 236 /** 237 * Begin a transaction 238 * 239 * @param unknown_type $model 240 * @return boolean True on success, false on fail 241 * (i.e. if the database/model does not support transactions). 242 */ 243 function begin(&$model) { 244 if (parent::begin($model)) { 245 if ($this->execute('START TRANSACTION')) { 246 $this->__transactionStarted = true; 247 return true; 248 } 249 } 250 return false; 251 } 252 /** 253 * Commit a transaction 254 * 255 * @param unknown_type $model 256 * @return boolean True on success, false on fail 257 * (i.e. if the database/model does not support transactions, 258 * or a transaction has not started). 259 */ 260 function commit(&$model) { 261 if (parent::commit($model)) { 262 $this->__transactionStarted = false; 263 return $this->execute('COMMIT'); 264 } 265 return false; 266 } 267 /** 268 * Rollback a transaction 269 * 270 * @param unknown_type $model 271 * @return boolean True on success, false on fail 272 * (i.e. if the database/model does not support transactions, 273 * or a transaction has not started). 274 */ 275 function rollback(&$model) { 276 if (parent::rollback($model)) { 277 return $this->execute('ROLLBACK'); 278 } 279 return false; 280 } 281 /** 282 * Returns a formatted error message from previous database operation. 283 * 284 * @return string Error message with error number 285 */ 286 function lastError() { 287 if (mysql_errno($this->connection)) { 288 return mysql_errno($this->connection).': '.mysql_error($this->connection); 289 } 290 return null; 291 } 292 /** 293 * Returns number of affected rows in previous database operation. If no previous operation exists, 294 * this returns false. 295 * 296 * @return int Number of affected rows 297 */ 298 function lastAffected() { 299 if ($this->_result) { 300 return mysql_affected_rows($this->connection); 301 } 302 return null; 303 } 304 /** 305 * Returns number of rows in previous resultset. If no previous resultset exists, 306 * this returns false. 307 * 308 * @return int Number of rows in resultset 309 */ 310 function lastNumRows() { 311 if ($this->_result and is_resource($this->_result)) { 312 return @mysql_num_rows($this->_result); 313 } 314 return null; 315 } 316 /** 317 * Returns the ID generated from the previous INSERT operation. 318 * 319 * @param unknown_type $source 320 * @return in 321 */ 322 function lastInsertId($source = null) { 323 $id = mysql_insert_id($this->connection); 324 if ($id) { 325 return $id; 326 } 327 328 $data = $this->fetchAll('SELECT LAST_INSERT_ID() as id From '.$source); 329 if ($data && isset($data[0]['id'])) { 330 return $data[0]['id']; 331 } 332 } 333 /** 334 * Converts database-layer column types to basic types 335 * 336 * @param string $real Real database-layer column type (i.e. "varchar(255)") 337 * @return string Abstract column type (i.e. "string") 338 */ 339 function column($real) { 340 if (is_array($real)) { 341 $col = $real['name']; 342 if (isset($real['limit'])) 343 { 344 $col .= '('.$real['limit'].')'; 345 } 346 return $col; 347 } 348 349 $col = r(')', '', $real); 350 $limit = null; 351 @list($col, $limit) = explode('(', $col); 352 353 if (in_array($col, array('date', 'time', 'datetime', 'timestamp', 'year'))) { 354 return $col; 355 } 356 if ($col == 'tinyint' && $limit == '1') { 357 return 'boolean'; 358 } 359 if (strpos($col, 'int') !== false) { 360 return 'integer'; 361 } 362 if (strpos($col, 'char') !== false || $col == 'tinytext') { 363 return 'string'; 364 } 365 if (strpos($col, 'text') !== false) { 366 return 'text'; 367 } 368 if (strpos($col, 'blob') !== false) { 369 return 'binary'; 370 } 371 if (in_array($col, array('float', 'double', 'decimal'))) { 372 return 'float'; 373 } 374 if (strpos($col, 'enum') !== false) { 375 return "enum($limit)"; 376 } 377 if ($col == 'boolean') { 378 return $col; 379 } 380 return 'text'; 381 } 382 /** 383 * Enter description here... 384 * 385 * @param unknown_type $results 386 */ 387 function resultSet(&$results) { 388 $this->results =& $results; 389 $this->map = array(); 390 $num_fields = mysql_num_fields($results); 391 $index = 0; 392 $j = 0; 393 394 while ($j < $num_fields) { 395 396 $column = mysql_fetch_field($results,$j); 397 if (!empty($column->table)) { 398 $this->map[$index++] = array($column->table, $column->name); 399 } else { 400 $this->map[$index++] = array(0, $column->name); 401 } 402 $j++; 403 } 404 } 405 /** 406 * Fetches the next row from the current result set 407 * 408 * @return unknown 409 */ 410 function fetchResult() { 411 if ($row = mysql_fetch_row($this->results)) { 412 $resultRow = array(); 413 $i = 0; 414 foreach ($row as $index => $field) { 415 list($table, $column) = $this->map[$index]; 416 $resultRow[$table][$column] = $row[$index]; 417 $i++; 418 } 419 return $resultRow; 420 } else { 421 return false; 422 } 423 } 424 /** 425 * Enter description here... 426 * 427 * @param unknown_type $schema 428 * @return unknown 429 */ 430 function buildSchemaQuery($schema) { 431 $search = array('{AUTOINCREMENT}', '{PRIMARY}', '{UNSIGNED}', '{FULLTEXT}', 432 '{FULLTEXT_MYSQL}', '{BOOLEAN}', '{UTF_8}'); 433 $replace = array('int(11) not null auto_increment', 'primary key', 'unsigned', 434 'FULLTEXT', 'FULLTEXT', 'enum (\'true\', \'false\') NOT NULL default \'true\'', 435 '/*!40100 CHARACTER SET utf8 COLLATE utf8_unicode_ci */'); 436 $query = trim(r($search, $replace, $schema)); 437 return $query; 438 } 439 } 440 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |