Files
@ 1d3e99673196
Branch filter:
Location: hot67beta/libraries/joomla/database/database/mysqli.php
1d3e99673196
15.8 KiB
text/x-php
ch col
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | <?php
/**
* @version $Id: mysqli.php 11316 2008-11-27 03:11:24Z ian $
* @package Joomla.Framework
* @subpackage Database
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
/**
* MySQLi database driver
*
* @package Joomla.Framework
* @subpackage Database
* @since 1.0
*/
class JDatabaseMySQLi extends JDatabase
{
/**
* The database driver name
*
* @var string
*/
var $name = 'mysqli';
/**
* The null/zero date string
*
* @var string
*/
var $_nullDate = '0000-00-00 00:00:00';
/**
* Quote for named objects
*
* @var string
*/
var $_nameQuote = '`';
/**
* Database object constructor
*
* @access public
* @param array List of options used to configure the connection
* @since 1.5
* @see JDatabase
*/
function __construct( $options )
{
$host = array_key_exists('host', $options) ? $options['host'] : 'localhost';
$user = array_key_exists('user', $options) ? $options['user'] : '';
$password = array_key_exists('password',$options) ? $options['password'] : '';
$database = array_key_exists('database',$options) ? $options['database'] : '';
$prefix = array_key_exists('prefix', $options) ? $options['prefix'] : 'jos_';
$select = array_key_exists('select', $options) ? $options['select'] : true;
// Unlike mysql_connect(), mysqli_connect() takes the port and socket
// as separate arguments. Therefore, we have to extract them from the
// host string.
$port = NULL;
$socket = NULL;
$targetSlot = substr( strstr( $host, ":" ), 1 );
if (!empty( $targetSlot )) {
// Get the port number or socket name
if (is_numeric( $targetSlot ))
$port = $targetSlot;
else
$socket = $targetSlot;
// Extract the host name only
$host = substr( $host, 0, strlen( $host ) - (strlen( $targetSlot ) + 1) );
// This will take care of the following notation: ":3306"
if($host == '')
$host = 'localhost';
}
// perform a number of fatality checks, then return gracefully
if (!function_exists( 'mysqli_connect' )) {
$this->_errorNum = 1;
$this->_errorMsg = 'The MySQL adapter "mysqli" is not available.';
return;
}
// connect to the server
if (!($this->_resource = @mysqli_connect($host, $user, $password, NULL, $port, $socket))) {
$this->_errorNum = 2;
$this->_errorMsg = 'Could not connect to MySQL';
return;
}
// finalize initialization
parent::__construct($options);
// select the database
if ( $select ) {
$this->select($database);
}
}
/**
* Database object destructor
*
* @return boolean
* @since 1.5
*/
function __destruct()
{
$return = false;
if (is_resource($this->_resource)) {
$return = mysqli_close($this->_resource);
}
return $return;
}
/**
* Test to see if the MySQLi connector is available
*
* @static
* @access public
* @return boolean True on success, false otherwise.
*/
function test()
{
return (function_exists( 'mysqli_connect' ));
}
/**
* Determines if the connection to the server is active.
*
* @access public
* @return boolean
* @since 1.5
*/
function connected()
{
return $this->_resource->ping();
}
/**
* Select a database for use
*
* @access public
* @param string $database
* @return boolean True if the database has been successfully selected
* @since 1.5
*/
function select($database)
{
if ( ! $database )
{
return false;
}
if ( !mysqli_select_db($this->_resource, $database)) {
$this->_errorNum = 3;
$this->_errorMsg = 'Could not connect to database';
return false;
}
// if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems
if ( strpos( $this->getVersion(), '5' ) === 0 ) {
$this->setQuery( "SET sql_mode = 'MYSQL40'" );
$this->query();
}
return true;
}
/**
* Determines UTF support
*
* @access public
* @return boolean True - UTF is supported
*/
function hasUTF()
{
$verParts = explode( '.', $this->getVersion() );
return ($verParts[0] == 5 || ($verParts[0] == 4 && $verParts[1] == 1 && (int)$verParts[2] >= 2));
}
/**
* Custom settings for UTF support
*
* @access public
*/
function setUTF()
{
mysqli_query( $this->_resource, "SET NAMES 'utf8'" );
}
/**
* Get a database escaped string
*
* @param string The string to be escaped
* @param boolean Optional parameter to provide extra escaping
* @return string
* @access public
* @abstract
*/
function getEscaped( $text, $extra = false )
{
$result = mysqli_real_escape_string( $this->_resource, $text );
if ($extra) {
$result = addcslashes( $result, '%_' );
}
return $result;
}
/**
* Execute the query
*
* @access public
* @return mixed A database resource if successful, FALSE if not.
*/
function query()
{
if (!is_object($this->_resource)) {
return false;
}
// Take a local copy so that we don't modify the original query and cause issues later
$sql = $this->_sql;
if ($this->_limit > 0 || $this->_offset > 0) {
$sql .= ' LIMIT '.$this->_offset.', '.$this->_limit;
}
if ($this->_debug) {
$this->_ticker++;
$this->_log[] = $sql;
}
$this->_errorNum = 0;
$this->_errorMsg = '';
$this->_cursor = mysqli_query( $this->_resource, $sql );
if (!$this->_cursor)
{
$this->_errorNum = mysqli_errno( $this->_resource );
$this->_errorMsg = mysqli_error( $this->_resource )." SQL=$sql";
if ($this->_debug) {
JError::raiseError(500, 'JDatabaseMySQL::query: '.$this->_errorNum.' - '.$this->_errorMsg );
}
return false;
}
return $this->_cursor;
}
/**
* Description
*
* @access public
* @return int The number of affected rows in the previous operation
* @since 1.0.5
*/
function getAffectedRows()
{
return mysqli_affected_rows( $this->_resource );
}
/**
* Execute a batch query
*
* @access public
* @return mixed A database resource if successful, FALSE if not.
*/
function queryBatch( $abort_on_error=true, $p_transaction_safe = false)
{
$this->_errorNum = 0;
$this->_errorMsg = '';
if ($p_transaction_safe) {
$this->_sql = rtrim($this->_sql, "; \t\r\n\0");
$si = $this->getVersion();
preg_match_all( "/(\d+)\.(\d+)\.(\d+)/i", $si, $m );
if ($m[1] >= 4) {
$this->_sql = 'START TRANSACTION;' . $this->_sql . '; COMMIT;';
} else if ($m[2] >= 23 && $m[3] >= 19) {
$this->_sql = 'BEGIN WORK;' . $this->_sql . '; COMMIT;';
} else if ($m[2] >= 23 && $m[3] >= 17) {
$this->_sql = 'BEGIN;' . $this->_sql . '; COMMIT;';
}
}
$query_split = $this->splitSql($this->_sql);
$error = 0;
foreach ($query_split as $command_line) {
$command_line = trim( $command_line );
if ($command_line != '') {
$this->_cursor = mysqli_query( $this->_resource, $command_line );
if ($this->_debug) {
$this->_ticker++;
$this->_log[] = $command_line;
}
if (!$this->_cursor) {
$error = 1;
$this->_errorNum .= mysqli_errno( $this->_resource ) . ' ';
$this->_errorMsg .= mysqli_error( $this->_resource )." SQL=$command_line <br />";
if ($abort_on_error) {
return $this->_cursor;
}
}
}
}
return $error ? false : true;
}
/**
* Diagnostic function
*
* @access public
* @return string
*/
function explain()
{
$temp = $this->_sql;
$this->_sql = "EXPLAIN $this->_sql";
if (!($cur = $this->query())) {
return null;
}
$first = true;
$buffer = '<table id="explain-sql">';
$buffer .= '<thead><tr><td colspan="99">'.$this->getQuery().'</td></tr>';
while ($row = mysqli_fetch_assoc( $cur )) {
if ($first) {
$buffer .= '<tr>';
foreach ($row as $k=>$v) {
$buffer .= '<th>'.$k.'</th>';
}
$buffer .= '</tr>';
$first = false;
}
$buffer .= '</thead><tbody><tr>';
foreach ($row as $k=>$v) {
$buffer .= '<td>'.$v.'</td>';
}
$buffer .= '</tr>';
}
$buffer .= '</tbody></table>';
mysqli_free_result( $cur );
$this->_sql = $temp;
return $buffer;
}
/**
* Description
*
* @access public
* @return int The number of rows returned from the most recent query.
*/
function getNumRows( $cur=null )
{
return mysqli_num_rows( $cur ? $cur : $this->_cursor );
}
/**
* This method loads the first field of the first row returned by the query.
*
* @access public
* @return The value returned in the query or null if the query failed.
*/
function loadResult()
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($row = mysqli_fetch_row( $cur )) {
$ret = $row[0];
}
mysqli_free_result( $cur );
return $ret;
}
/**
* Load an array of single field results into an array
*
* @access public
*/
function loadResultArray($numinarray = 0)
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysqli_fetch_row( $cur )) {
$array[] = $row[$numinarray];
}
mysqli_free_result( $cur );
return $array;
}
/**
* Fetch a result row as an associative array
*
* @access public
* @return array
*/
function loadAssoc()
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($array = mysqli_fetch_assoc( $cur )) {
$ret = $array;
}
mysqli_free_result( $cur );
return $ret;
}
/**
* Load a assoc list of database rows
*
* @access public
* @param string The field name of a primary key
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
function loadAssocList( $key='' )
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysqli_fetch_assoc( $cur )) {
if ($key) {
$array[$row[$key]] = $row;
} else {
$array[] = $row;
}
}
mysqli_free_result( $cur );
return $array;
}
/**
* This global function loads the first row of a query into an object
*
* @access public
* @return object
*/
function loadObject( )
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($object = mysqli_fetch_object( $cur )) {
$ret = $object;
}
mysqli_free_result( $cur );
return $ret;
}
/**
* Load a list of database objects
*
* If <var>key</var> is not empty then the returned array is indexed by the value
* the database key. Returns <var>null</var> if the query fails.
*
* @access public
* @param string The field name of a primary key
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
function loadObjectList( $key='' )
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysqli_fetch_object( $cur )) {
if ($key) {
$array[$row->$key] = $row;
} else {
$array[] = $row;
}
}
mysqli_free_result( $cur );
return $array;
}
/**
* Description
*
* @access public
* @return The first row of the query.
*/
function loadRow()
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($row = mysqli_fetch_row( $cur )) {
$ret = $row;
}
mysqli_free_result( $cur );
return $ret;
}
/**
* Load a list of database rows (numeric column indexing)
*
* If <var>key</var> is not empty then the returned array is indexed by the value
* the database key. Returns <var>null</var> if the query fails.
*
* @access public
* @param string The field name of a primary key
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
function loadRowList( $key=null )
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysqli_fetch_row( $cur )) {
if ($key !== null) {
$array[$row[$key]] = $row;
} else {
$array[] = $row;
}
}
mysqli_free_result( $cur );
return $array;
}
/**
* Inserts a row into a table based on an objects properties
*
* @access public
* @param string The name of the table
* @param object An object whose properties match table fields
* @param string The name of the primary key. If provided the object property is updated.
*/
function insertObject( $table, &$object, $keyName = NULL )
{
$fmtsql = 'INSERT INTO '.$this->nameQuote($table).' ( %s ) VALUES ( %s ) ';
$fields = array();
foreach (get_object_vars( $object ) as $k => $v) {
if (is_array($v) or is_object($v) or $v === NULL) {
continue;
}
if ($k[0] == '_') { // internal field
continue;
}
$fields[] = $this->nameQuote( $k );
$values[] = $this->isQuoted( $k ) ? $this->Quote( $v ) : (int) $v;
}
$this->setQuery( sprintf( $fmtsql, implode( ",", $fields ) , implode( ",", $values ) ) );
if (!$this->query()) {
return false;
}
$id = $this->insertid();
if ($keyName && $id) {
$object->$keyName = $id;
}
return true;
}
/**
* Description
*
* @access public
* @param [type] $updateNulls
*/
function updateObject( $table, &$object, $keyName, $updateNulls=true )
{
$fmtsql = 'UPDATE '.$this->nameQuote($table).' SET %s WHERE %s';
$tmp = array();
foreach (get_object_vars( $object ) as $k => $v) {
if( is_array($v) or is_object($v) or $k[0] == '_' ) { // internal or NA field
continue;
}
if( $k == $keyName ) { // PK not to be updated
$where = $keyName . '=' . $this->Quote( $v );
continue;
}
if ($v === null)
{
if ($updateNulls) {
$val = 'NULL';
} else {
continue;
}
} else {
$val = $this->isQuoted( $k ) ? $this->Quote( $v ) : (int) $v;
}
$tmp[] = $this->nameQuote( $k ) . '=' . $val;
}
$this->setQuery( sprintf( $fmtsql, implode( ",", $tmp ) , $where ) );
return $this->query();
}
/**
* Description
*
* @access public
*/
function insertid()
{
return mysqli_insert_id( $this->_resource );
}
/**
* Description
*
* @access public
*/
function getVersion()
{
return mysqli_get_server_info( $this->_resource );
}
/**
* Assumes database collation in use by sampling one text field in one table
*
* @access public
* @return string Collation in use
*/
function getCollation ()
{
if ( $this->hasUTF() ) {
$this->setQuery( 'SHOW FULL COLUMNS FROM #__content' );
$array = $this->loadAssocList();
return $array['4']['Collation'];
} else {
return "N/A (mySQL < 4.1.2)";
}
}
/**
* Description
*
* @access public
* @return array A list of all the tables in the database
*/
function getTableList()
{
$this->setQuery( 'SHOW TABLES' );
return $this->loadResultArray();
}
/**
* Shows the CREATE TABLE statement that creates the given tables
*
* @access public
* @param array|string A table name or a list of table names
* @return array A list the create SQL for the tables
*/
function getTableCreate( $tables )
{
settype($tables, 'array'); //force to array
$result = array();
foreach ($tables as $tblval)
{
$this->setQuery( 'SHOW CREATE table ' . $this->getEscaped( $tblval ) );
$rows = $this->loadRowList();
foreach ($rows as $row) {
$result[$tblval] = $row[1];
}
}
return $result;
}
/**
* Retrieves information about the given tables
*
* @access public
* @param array|string A table name or a list of table names
* @param boolean Only return field types, default true
* @return array An array of fields by table
*/
function getTableFields( $tables, $typeonly = true )
{
settype($tables, 'array'); //force to array
$result = array();
foreach ($tables as $tblval)
{
$this->setQuery( 'SHOW FIELDS FROM ' . $tblval );
$fields = $this->loadObjectList();
if($typeonly)
{
foreach ($fields as $field) {
$result[$tblval][$field->Field] = preg_replace("/[(0-9)]/",'', $field->Type );
}
}
else
{
foreach ($fields as $field) {
$result[$tblval][$field->Field] = $field;
}
}
}
return $result;
}
}
|