Lazy-load column info in wpdb. props pento. fixes #20838.

git-svn-id: http://core.svn.wordpress.org/trunk@21472 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2012-08-08 06:24:59 +00:00
parent 4e5fd4de46
commit 1425762319

View File

@ -134,10 +134,10 @@ class wpdb {
* Saved info on the table column
*
* @since 1.2.0
* @access private
* @access protected
* @var array
*/
var $col_info;
protected $col_info;
/**
* Saved queries that were executed
@ -515,6 +515,21 @@ class wpdb {
return true;
}
/**
* PHP5 style magic getter, used to lazy-load expensive data.
*
* @since 3.5.0
*
* @param string $var The private member to get, and optionally process
* @return mixed The private member
*/
function __get( $var ) {
if ( 'col_info' == $var )
$this->load_col_info();
return $this->$var;
}
/**
* Set $this->charset and $this->collate
*
@ -1025,6 +1040,7 @@ class wpdb {
$this->last_result = array();
$this->col_info = null;
$this->last_query = null;
@mysql_free_result( $this->result );
}
/**
@ -1117,19 +1133,12 @@ class wpdb {
// Return number of rows affected
$return_val = $this->rows_affected;
} else {
$i = 0;
while ( $i < @mysql_num_fields( $this->result ) ) {
$this->col_info[$i] = @mysql_fetch_field( $this->result );
$i++;
}
$num_rows = 0;
while ( $row = @mysql_fetch_object( $this->result ) ) {
$this->last_result[$num_rows] = $row;
$num_rows++;
}
@mysql_free_result( $this->result );
// Log number of rows the query returned
// and return number of rows selected
$this->num_rows = $num_rows;
@ -1457,6 +1466,22 @@ class wpdb {
return null;
}
/**
* Load the column metadata from the last query.
*
* @since 3.5.0
*
* @access protected
*/
protected function load_col_info() {
if ( $this->col_info )
return;
for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
$this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
}
}
/**
* Retrieve column metadata from the last query.
*
@ -1467,6 +1492,8 @@ class wpdb {
* @return mixed Column Results
*/
function get_col_info( $info_type = 'name', $col_offset = -1 ) {
$this->load_col_info();
if ( $this->col_info ) {
if ( $col_offset == -1 ) {
$i = 0;