Make WordPress Core


Ignore:
Timestamp:
10/01/2015 05:36:15 AM (11 years ago)
Author:
pento
Message:

WPDB: Allow null values in the CRUD functions.

Specifically, ::insert(), ::replace(), ::update(), and ::delete() can now set a column to NULL, or add the IS NULL condition to the WHERE clause.

This is based on ​[backpress 279].

Props pento, nbachiyski, sorich87.

Fixes #15158.

File:
1 edited

Legend:

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

    r34655 r34737  
    17801780         * @param array        $data   Data to insert (in column => value pairs).
    17811781         *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     1782         *                             Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
    17821783         * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
    17831784         *                             If string, that format will be used for all of the values in $data.
    … …  
    18041805         * @param array        $data   Data to insert (in column => value pairs).
    18051806         *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     1807         *                             Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
    18061808         * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
    18071809         *                             If string, that format will be used for all of the values in $data.
    … …  
    18281830         * @param array        $data   Data to insert (in column => value pairs).
    18291831         *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     1832         *                             Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
    18301833         * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
    18311834         *                             If string, that format will be used for all of the values in $data.
    … …  
    18491852                $formats = $values = array();
    18501853                foreach ( $data as $value ) {
     1854                        if ( is_null( $value['value'] ) ) {
     1855                                $formats[] = 'NULL';
     1856                                continue;
     1857                        }
     1858
    18511859                        $formats[] = $value['format'];
    18521860                        $values[]  = $value['value'];
    … …  
    18761884         * @param array        $data         Data to update (in column => value pairs).
    18771885         *                                   Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     1886         *                                   Sending a null value will cause the column to be set to NULL - the corresponding
     1887         *                                   format is ignored in this case.
    18781888         * @param array        $where        A named array of WHERE clauses (in column => value pairs).
    18791889         *                                   Multiple clauses will be joined with ANDs.
    18801890         *                                   Both $where columns and $where values should be "raw".
     1891         *                                   Sending a null value will create an IS NULL comparison - the corresponding format will be ignored in this case.
    18811892         * @param array|string $format       Optional. An array of formats to be mapped to each of the values in $data.
    18821893         *                                   If string, that format will be used for all of the values in $data.
    … …  
    19051916                $fields = $conditions = $values = array();
    19061917                foreach ( $data as $field => $value ) {
     1918                        if ( is_null( $value['value'] ) ) {
     1919                                $fields[] = "`$field` = NULL";
     1920                                continue;
     1921                        }
     1922
    19071923                        $fields[] = "`$field` = " . $value['format'];
    19081924                        $values[] = $value['value'];
    19091925                }
    19101926                foreach ( $where as $field => $value ) {
     1927                        if ( is_null( $value['value'] ) ) {
     1928                                $conditions[] = "`$field` IS NULL";
     1929                                continue;
     1930                        }
     1931
    19111932                        $conditions[] = "`$field` = " . $value['format'];
    19121933                        $values[] = $value['value'];
    … …  
    19371958         *                                   Multiple clauses will be joined with ANDs.
    19381959         *                                   Both $where columns and $where values should be "raw".
     1960         *                                   Sending a null value will create an IS NULL comparison - the corresponding format will be ignored in this case.
    19391961         * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where.
    19401962         *                                   If string, that format will be used for all of the items in $where.
    … …  
    19551977                $conditions = $values = array();
    19561978                foreach ( $where as $field => $value ) {
     1979                        if ( is_null( $value['value'] ) ) {
     1980                                $conditions[] = "`$field` IS NULL";
     1981                                continue;
     1982                        }
     1983
    19571984                        $conditions[] = "`$field` = " . $value['format'];
    19581985                        $values[] = $value['value'];
Note: See TracChangeset for help on using the changeset viewer.