| [ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: dbo_mysqli.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 /** 4 * MySQLi 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 1.1.4.2974 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 DboMysqli extends DboSource { 42 /** 43 * Enter description here... 44 * 45 * @var unknown_type 46 */ 47 var $description = "Mysqli 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 Mysqli 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' => 'mysqli_pconnect'); 72 /** 73 * Mysqli 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 'binary' => array('name' => 'blob'), 87 'boolean' => array('name' => 'tinyint', 'limit' => '1')); 88 /** 89 * Connects to the database using options in the given configuration array. 90 * 91 * @return boolean True if the database could be connected, else false 92 */ 93 function connect() { 94 $config = $this->config; 95 $connect = $config['connect']; 96 $this->connected = false; 97 98 if (!$config['persistent']) { 99 $this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], true); 100 } else { 101 $this->connection = $connect($config['host'], $config['login'], $config['password']); 102 } 103 104 if (mysqli_select_db($this->connection, $config['database'])) { 105 $this->connected = true; 106 } 107 return $this->connected; 108 } 109 /** 110 * Disconnects from database. 111 * 112 * @return boolean True if the database could be disconnected, else false 113 */ 114 function disconnect() { 115 @mysqli_free_result($this->results); 116 $this->connected = !@mysqli_close($this->connection); 117 return !$this->connected; 118 } 119 /** 120 * Executes given SQL statement. 121 * 122 * @param string $sql SQL statement 123 * @return resource Result resource identifier 124 * @access protected 125 */ 126 function _execute($sql) { 127 $firstAnswerResult = mysqli_multi_query($this->connection, $sql); 128 129 $firstResult = mysqli_store_result($this->connection); 130 131 if(mysqli_more_results($this->connection)) 132 while($lastResult = mysqli_next_result($this->connection)); 133 134 if (!$firstResult) 135 return $firstAnswerResult; 136 137 return $firstResult; 138 } 139 /** 140 * Returns an array of sources (tables) in the database. 141 * 142 * @return array Array of tablenames in the database 143 */ 144 function listSources() { 145 $cache = parent::listSources(); 146 if ($cache != null) { 147 return $cache; 148 } 149 $result = $this->_execute('SHOW TABLES FROM ' . $this->config['database'] . ';'); 150 151 if (!$result) { 152 return array(); 153 } else { 154 155 $tables = array(); 156 while ($line = mysqli_fetch_array($result)) { 157 $tables[] = $line[0]; 158 } 159 160 parent::listSources($tables); 161 return $tables; 162 } 163 } 164 /** 165 * Returns an array of the fields in given table name. 166 * 167 * @param string $tableName Name of database table to inspect 168 * @return array Fields in table. Keys are name and type 169 */ 170 function describe(&$model) { 171 172 $cache = parent::describe($model); 173 if ($cache != null) { 174 return $cache; 175 } 176 177 $fields = false; 178 $cols = $this->query('DESCRIBE ' . $this->fullTableName($model)); 179 180 foreach ($cols as $column) { 181 $colKey = array_keys($column); 182 if (isset($column[$colKey[0]]) && !isset($column[0])) { 183 $column[0] = $column[$colKey[0]]; 184 } 185 if (isset($column[0])) { 186 $fields[] = array( 187 'name' => $column[0]['Field'], 188 'type' => $this->column($column[0]['Type']), 189 'null' => $column[0]['Null'], 190 'default' => $column[0]['Default'] 191 ); 192 } 193 } 194 195 $this->__cacheDescription($model->tablePrefix.$model->table, $fields); 196 return $fields; 197 } 198 /** 199 * Returns a quoted name of $data for use in an SQL statement. 200 * 201 * @param string $data Name (table.field) to be prepared for use in an SQL statement 202 * @return string Quoted for MySQL 203 */ 204 function name($data) { 205 if ($data == '*') { 206 return '*'; 207 } 208 $pos = strpos($data, '`'); 209 if ($pos === false) { 210 $data = '`'. str_replace('.', '`.`', $data) .'`'; 211 } 212 return $data; 213 } 214 /** 215 * Returns a quoted and escaped string of $data for use in an SQL statement. 216 * 217 * @param string $data String to be prepared for use in an SQL statement 218 * @param string $column The column into which this data will be inserted 219 * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided 220 * @return string Quoted and escaped data 221 */ 222 function value($data, $column = null, $safe = false) { 223 $parent = parent::value($data, $column, $safe); 224 225 if ($parent != null) { 226 return $parent; 227 } 228 229 if ($data === null) { 230 return 'NULL'; 231 } 232 233 if($data === '') { 234 return "''"; 235 } 236 237 switch ($column) { 238 case 'boolean': 239 $data = $this->boolean((bool)$data); 240 break; 241 default: 242 $data = mysqli_real_escape_string($this->connection, $data); 243 break; 244 } 245 246 return "'" . $data . "'"; 247 } 248 /** 249 * Begin a transaction 250 * 251 * @param unknown_type $model 252 * @return boolean True on success, false on fail 253 * (i.e. if the database/model does not support transactions). 254 */ 255 function begin(&$model) { 256 if (parent::begin($model)) { 257 if ($this->execute('START TRANSACTION')) { 258 $this->__transactionStarted = true; 259 return true; 260 } 261 } 262 return false; 263 } 264 /** 265 * Commit a transaction 266 * 267 * @param unknown_type $model 268 * @return boolean True on success, false on fail 269 * (i.e. if the database/model does not support transactions, 270 * or a transaction has not started). 271 */ 272 function commit(&$model) { 273 if (parent::commit($model)) { 274 $this->__transactionStarted = false; 275 return $this->execute('COMMIT'); 276 } 277 return false; 278 } 279 /** 280 * Rollback a transaction 281 * 282 * @param unknown_type $model 283 * @return boolean True on success, false on fail 284 * (i.e. if the database/model does not support transactions, 285 * or a transaction has not started). 286 */ 287 function rollback(&$model) { 288 if (parent::rollback($model)) { 289 return $this->execute('ROLLBACK'); 290 } 291 return false; 292 } 293 /** 294 * Returns a formatted error message from previous database operation. 295 * 296 * @return string Error message with error number 297 */ 298 function lastError() { 299 if (mysqli_errno($this->connection)) { 300 return mysqli_errno($this->connection).': '.mysqli_error($this->connection); 301 } 302 return null; 303 } 304 /** 305 * Returns number of affected rows in previous database operation. If no previous operation exists, 306 * this returns false. 307 * 308 * @return int Number of affected rows 309 */ 310 function lastAffected() { 311 if ($this->_result) { 312 return mysqli_affected_rows($this->connection); 313 } 314 return null; 315 } 316 /** 317 * Returns number of rows in previous resultset. If no previous resultset exists, 318 * this returns false. 319 * 320 * @return int Number of rows in resultset 321 */ 322 function lastNumRows() { 323 if ($this->_result and is_object($this->_result)) { 324 return @mysqli_num_rows($this->_result); 325 } 326 return null; 327 } 328 /** 329 * Returns the ID generated from the previous INSERT operation. 330 * 331 * @param unknown_type $source 332 * @return in 333 */ 334 function lastInsertId($source = null) { 335 $id = mysqli_insert_id($this->connection); 336 if ($id) { 337 return $id; 338 } 339 340 $data = $this->fetchAll('SELECT LAST_INSERT_ID() as id From '.$source); 341 if ($data && isset($data[0]['id'])) { 342 return $data[0]['id']; 343 } 344 } 345 /** 346 * Converts database-layer column types to basic types 347 * 348 * @param string $real Real database-layer column type (i.e. "varchar(255)") 349 * @return string Abstract column type (i.e. "string") 350 */ 351 function column($real) { 352 if (is_array($real)) { 353 $col = $real['name']; 354 if (isset($real['limit'])) 355 { 356 $col .= '('.$real['limit'].')'; 357 } 358 return $col; 359 } 360 361 $col = r(')', '', $real); 362 $limit = null; 363 @list($col, $limit) = explode('(', $col); 364 365 if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) { 366 return $col; 367 } 368 if ($col == 'tinyint' && $limit == '1') { 369 return 'boolean'; 370 } 371 if (strpos($col, 'int') !== false) { 372 return 'integer'; 373 } 374 if (strpos($col, 'char') !== false || $col == 'tinytext') { 375 return 'string'; 376 } 377 if (strpos($col, 'text') !== false) { 378 return 'text'; 379 } 380 if (strpos($col, 'blob') !== false) { 381 return 'binary'; 382 } 383 if (in_array($col, array('float', 'double', 'decimal'))) { 384 return 'float'; 385 } 386 if (strpos($col, 'enum') !== false) { 387 return "enum($limit)"; 388 } 389 if ($col == 'boolean') { 390 return $col; 391 } 392 return 'text'; 393 } 394 /** 395 * Enter description here... 396 * 397 * @param unknown_type $results 398 */ 399 function resultSet(&$results) { 400 $this->results =& $results; 401 $this->map = array(); 402 $num_fields = mysqli_num_fields($results); 403 $index = 0; 404 $j = 0; 405 406 while ($j < $num_fields) { 407 $column = mysqli_fetch_field_direct($results, $j); 408 if (!empty($column->table)) { 409 $this->map[$index++] = array($column->table, $column->name); 410 } else { 411 $this->map[$index++] = array(0, $column->name); 412 } 413 $j++; 414 } 415 } 416 /** 417 * Fetches the next row from the current result set 418 * 419 * @return unknown 420 */ 421 function fetchResult() { 422 if ($row = mysqli_fetch_row($this->results)) { 423 $resultRow = array(); 424 $i = 0; 425 foreach ($row as $index => $field) { 426 @list($table, $column) = $this->map[$index]; 427 $resultRow[$table][$column] = $row[$index]; 428 $i++; 429 } 430 return $resultRow; 431 } else { 432 return false; 433 } 434 } 435 /** 436 * Returns a row from given resultset as an array . 437 * 438 * @param bool $assoc Associative array only, or both? 439 * @return array The fetched row as an array 440 */ 441 function fetchRow($assoc = false) { 442 if (is_object($this->_result)) { 443 $this->resultSet($this->_result); 444 $resultRow = $this->fetchResult(); 445 return $resultRow; 446 } else { 447 return null; 448 } 449 } 450 /** 451 * Enter description here... 452 * 453 * @param unknown_type $schema 454 * @return unknown 455 */ 456 function buildSchemaQuery($schema) { 457 $search = array('{AUTOINCREMENT}', '{PRIMARY}', '{UNSIGNED}', '{FULLTEXT}', 458 '{FULLTEXT_MYSQL}', '{BOOLEAN}', '{UTF_8}'); 459 $replace = array('int(11) not null auto_increment', 'primary key', 'unsigned', 460 'FULLTEXT', 'FULLTEXT', 'enum (\'true\', \'false\') NOT NULL default \'true\'', 461 '/*!40100 CHARACTER SET utf8 COLLATE utf8_unicode_ci */'); 462 $query = trim(r($search, $replace, $schema)); 463 return $query; 464 } 465 } 466 ?>
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 |