Make WordPress Core

Ticket #7171: 7171.diff

File 7171.diff, 4.6 KB (added by DD32, 18 years ago)
  • wp-includes/wp-db.php

     
    106106        var $col_info;
    107107
    108108        /**
     109         * Saved info on the table columns
     110         *
     111         * @since 2.7.0
     112         * @access private
     113         * @var array
     114         */
     115        var $column_types = array();
     116
     117        /**
    109118         * Saved queries that were executed
    110119         *
    111120         * @since 1.5.0
     
    629638                        $i = 0;
    630639                        while ($i < @mysql_num_fields($this->result)) {
    631640                                $this->col_info[$i] = @mysql_fetch_field($this->result);
     641                                $this->column_types[ $this->col_info[$i]->table ][ $this->col_info[$i]->name ] = $this->col_info[$i]->type;
    632642                                $i++;
    633643                        }
    634644                        $num_rows = 0;
     
    659669         * @return mixed Results of $this->query()
    660670         */
    661671        function insert($table, $data) {
    662                 $data = add_magic_quotes($data);
    663672                $fields = array_keys($data);
    664                 return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')");
     673
     674                $insertfields = $insertfieldtypes = $prepbits = array();
     675
     676                $types = $this->prepare_columns($table, $keys = array_keys($data));
     677
     678                foreach( (array)$data as $field => $piece ) {
     679                        $insertfields[] = $field;
     680                        $insertfieldtypes[] = $types[$field];
     681                        $prepbits[] = $piece;
     682                }
     683
     684                $sql = 'INSERT INTO ' . $table;
     685                $sql .= ' (`' . implode('`,`', $insertfields) . '`)';
     686                $sql .= ' VALUES (' . implode(',', $insertfieldtypes) . ')';
     687
     688                $sql = call_user_func_array(array(&$this, 'prepare'), array_merge(array($sql), $prepbits));
     689
     690                return $this->query( $sql );
    665691        }
    666692
    667693        /**
     
    675701         * @return mixed Results of $this->query()
    676702         */
    677703        function update($table, $data, $where){
    678                 $data = add_magic_quotes($data);
    679                 $bits = $wheres = array();
    680                 foreach ( (array) array_keys($data) as $k )
    681                         $bits[] = "`$k` = '$data[$k]'";
     704                if ( empty($table) || empty($where) )
     705                        return;
    682706
    683                 if ( is_array( $where ) )
    684                         foreach ( $where as $c => $v )
    685                                 $wheres[] = "$c = '" . $this->escape( $v ) . "'";
    686                 else
    687                         return false;
     707                $bits = $wheres = $prepbits = array();
    688708
    689                 return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) );
     709                $sql = "UPDATE $table SET ";
     710
     711                $types = $this->prepare_columns($table, array_merge(array_keys($data), array_keys($where)) );
     712
     713                foreach ( array_keys($data) as $k ) {
     714                        $bits[] = "`$k` = " . $types[$k];
     715                        $prepbits[] = $data[$k];
     716                }
     717
     718                $sql .= implode( ', ', $bits );
     719               
     720                $sql .= ' WHERE ';
     721
     722                foreach ( array_keys($where) as $k ) {
     723                        $wheres[] = "`$k` = " . $types[$k];
     724                        $prepbits[] = $where[$k];                       
     725                }
     726                $sql .= implode( ' AND ', $wheres );
     727
     728                $sql = call_user_func_array(array(&$this, 'prepare'), array_merge(array($sql), $prepbits));
     729
     730                return $this->query( $sql );
    690731        }
    691732
    692733        /**
     734         * Determines a table column type sprintf()-style token
     735         *
     736         * Uses values cached from the previous queries,
     737         * Loads any column types with a simple SELECT .. LIMIT 1 of any fields which are not allready loaded.
     738         *
     739         * @param string $table WARNING: not sanitized!
     740         * @param array $cols array of columns to prepare. WARNING: not sanitized!
     741         * @return array of key => value pairs of field => token
     742         */
     743        function prepare_columns($table, $cols) {
     744                //Load any columns which are not allready cached.
     745                $load_cols = array();
     746                foreach ( (array) $cols as $col )
     747                        if ( ! isset($this->column_types[$table][$col]) )
     748                                $load_cols[] = $col;
     749
     750                if ( !empty($load_cols) ) {
     751                        $load_cols = implode('`,`', $load_cols);
     752                        $this->query("SELECT `$load_cols` FROM $table LIMIT 1");
     753                }
     754
     755                $return = array();
     756                foreach ( (array) $cols as $col ) {
     757                        switch ( $this->column_types[$table][$col] ) {
     758                                case 'int':
     759                                        $return[$col] = '%d';
     760                                        break;
     761                                case 'real':
     762                                        $return[$col] = '%f';
     763                                        break;
     764                                case 'date':
     765                                case 'datetime':
     766                                case 'string':
     767                                case 'blob':
     768                                default:
     769                                        $return[$col] = '%s';
     770                                        break;
     771                        }
     772                }
     773                return $return;
     774        }
     775
     776        /**
    693777         * Retrieve one variable from the database.
    694778         *
    695779         * This combines the functionality of wpdb::get_row() and wpdb::get_col(),
     
    900984         *
    901985         * @return WP_Error
    902986         */
    903         function check_database_version()
    904         {
     987        function check_database_version() {
    905988                global $wp_version;
    906989                // Make sure the server has MySQL 4.0
    907990                if ( version_compare($this->db_version(), '4.0.0', '<') )
     
    9171000         *
    9181001         * @return bool True if collation is supported, false if version does not
    9191002         */
    920         function supports_collation()
    921         {
     1003        function supports_collation() {
    9221004                return $this->has_cap( 'collation' );
    9231005        }
    9241006