WordPress.org

Make WordPress Core

Ticket #6836: wordpress_delete_wpdb.diff

File wordpress_delete_wpdb.diff, 1.9 KB (added by noroute, 5 years ago)

PoC Implementation of an abstraction of DELETE for wp-db.php

  • wp-includes/wp-db.php

     
    688688 
    689689                return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) ); 
    690690        } 
    691  
     691         
    692692        /** 
     693         * Delete rows in a table 
     694         *  
     695         * @since 2.7 
     696         *  
     697         * @param string $table name 
     698         * @param array $where A named array of WHERE column => value pairs. 
     699         * @return mixed Results of $this->query() 
     700         */ 
     701        function delete($table, $where){ 
     702                return $this->query( " DELETE FROM $table WHERE " . $this->make_wheres($where)); 
     703        } 
     704         
     705        /** 
     706         * Create a where clause for use in sql queries. WARNING: Only works for ANDs at the moment 
     707         *  
     708         * @since 2.7 
     709         *  
     710         * @param array $where A named array of WHERE column => value pairs. Multiple pairs will be joined by AND. 
     711         * @return string a WHERE clause for use in SQL statement 
     712         * */ 
     713        function make_wheres($where){ 
     714                $wheres = array(); 
     715                if (is_array( $where ) ) 
     716                        foreach ( $where as $c => $v ) 
     717                                $wheres[] = "$c = '" . $this->escape( $v ) ."'"; 
     718                else 
     719                        return false; 
     720                 
     721                return implode( ' AND ',$wheres); 
     722        } 
     723         
     724        /** 
    693725         * Retrieve one variable from the database. 
    694726         * 
    695727         * This combines the functionality of wpdb::get_row() and wpdb::get_col(), 
  • wp-admin/includes/post.php

     
    587587        $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) ); 
    588588        wp_cache_delete($post_id, 'post_meta'); 
    589589 
    590         return $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) ); 
     590        return $wpdb->delete($wpdb->postmeta, array("meta_id" => $mid) ); 
    591591} 
    592592 
    593593/**