WordPress.org

Make WordPress Core

Changeset 21472


Ignore:
Timestamp:
08/08/2012 06:24:59 AM (6 years ago)
Author:
nacin
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r21471 r21472  
    135135     *
    136136     * @since 1.2.0
    137      * @access private
     137     * @access protected
    138138     * @var array
    139139     */
    140     var $col_info;
     140    protected $col_info;
    141141
    142142    /**
     
    514514    function __destruct() {
    515515        return true;
     516    }
     517
     518    /**
     519     * PHP5 style magic getter, used to lazy-load expensive data.
     520     *
     521     * @since 3.5.0
     522     *
     523     * @param string $var The private member to get, and optionally process
     524     * @return mixed The private member
     525     */
     526    function __get( $var ) {
     527        if ( 'col_info' == $var )
     528            $this->load_col_info();
     529
     530        return $this->$var;
    516531    }
    517532
     
    903918        $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
    904919        $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
    905         $query = str_replace( '%f' , '%F', $query ); // Force floats to be locale unaware       
     920        $query = str_replace( '%f' , '%F', $query ); // Force floats to be locale unaware
    906921        $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
    907922        array_walk( $args, array( &$this, 'escape_by_ref' ) );
     
    10261041        $this->col_info    = null;
    10271042        $this->last_query  = null;
     1043        @mysql_free_result( $this->result );
    10281044    }
    10291045
     
    11181134            $return_val = $this->rows_affected;
    11191135        } else {
    1120             $i = 0;
    1121             while ( $i < @mysql_num_fields( $this->result ) ) {
    1122                 $this->col_info[$i] = @mysql_fetch_field( $this->result );
    1123                 $i++;
    1124             }
    11251136            $num_rows = 0;
    11261137            while ( $row = @mysql_fetch_object( $this->result ) ) {
     
    11281139                $num_rows++;
    11291140            }
    1130 
    1131             @mysql_free_result( $this->result );
    11321141
    11331142            // Log number of rows the query returned
     
    14591468
    14601469    /**
     1470     * Load the column metadata from the last query.
     1471     *
     1472     * @since 3.5.0
     1473     *
     1474     * @access protected
     1475     */
     1476    protected function load_col_info() {
     1477        if ( $this->col_info )
     1478            return;
     1479
     1480        for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
     1481            $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1482        }
     1483    }
     1484
     1485    /**
    14611486     * Retrieve column metadata from the last query.
    14621487     *
     
    14681493     */
    14691494    function get_col_info( $info_type = 'name', $col_offset = -1 ) {
     1495        $this->load_col_info();
     1496
    14701497        if ( $this->col_info ) {
    14711498            if ( $col_offset == -1 ) {
Note: See TracChangeset for help on using the changeset viewer.