Make WordPress Core

Changeset 34737


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.

Location:
trunk
Files:
2 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'];
  • trunk/tests/phpunit/tests/db.php

    r33718 r34737  
    808808                return 'fake_col_charset';
    809809        }
     810
     811        /**
     812         * @ticket 15158
     813         */
     814        function test_null_insert() {
     815                global $wpdb;
     816
     817                $key = 'null_insert_key';
     818
     819                $wpdb->insert(
     820                        $wpdb->postmeta,
     821                        array(
     822                                'meta_key' => $key,
     823                                'meta_value' => NULL
     824                        ),
     825                        array( '%s', '%s' )
     826                );
     827
     828                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     829
     830                $this->assertNull( $row->meta_value );
     831        }
     832
     833        /**
     834         * @ticket 15158
     835         */
     836        function test_null_update_value() {
     837                global $wpdb;
     838
     839                $key = 'null_update_value_key';
     840                $value = 'null_update_value_key';
     841
     842                $wpdb->insert(
     843                        $wpdb->postmeta,
     844                        array(
     845                                'meta_key' => $key,
     846                                'meta_value' => $value
     847                        ),
     848                        array( '%s', '%s' )
     849                );
     850
     851                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     852
     853                $this->assertSame( $value, $row->meta_value );
     854
     855                $wpdb->update(
     856                        $wpdb->postmeta,
     857                        array( 'meta_value' => NULL ),
     858                        array(
     859                                'meta_key' => $key,
     860                                'meta_value' => $value
     861                        ),
     862                        array( '%s' ),
     863                        array( '%s', '%s' )
     864                );
     865
     866                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     867
     868                $this->assertNull( $row->meta_value );
     869        }
     870
     871        /**
     872         * @ticket 15158
     873         */
     874        function test_null_update_where() {
     875                global $wpdb;
     876
     877                $key = 'null_update_where_key';
     878                $value = 'null_update_where_key';
     879
     880                $wpdb->insert(
     881                        $wpdb->postmeta,
     882                        array(
     883                                'meta_key' => $key,
     884                                'meta_value' => NULL
     885                        ),
     886                        array( '%s', '%s' )
     887                );
     888
     889                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     890
     891                $this->assertNull( $row->meta_value );
     892
     893                $wpdb->update(
     894                        $wpdb->postmeta,
     895                        array( 'meta_value' => $value ),
     896                        array(
     897                                'meta_key' => $key,
     898                                'meta_value' => NULL
     899                        ),
     900                        array( '%s' ),
     901                        array( '%s', '%s' )
     902                );
     903
     904                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     905
     906                $this->assertSame( $value, $row->meta_value );
     907        }
     908
     909        /**
     910         * @ticket 15158
     911         */
     912        function test_null_delete() {
     913                global $wpdb;
     914
     915                $key = 'null_update_where_key';
     916                $value = 'null_update_where_key';
     917
     918                $wpdb->insert(
     919                        $wpdb->postmeta,
     920                        array(
     921                                'meta_key' => $key,
     922                                'meta_value' => NULL
     923                        ),
     924                        array( '%s', '%s' )
     925                );
     926
     927                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     928
     929                $this->assertNull( $row->meta_value );
     930
     931                $wpdb->delete(
     932                        $wpdb->postmeta,
     933                        array(
     934                                'meta_key' => $key,
     935                                'meta_value' => NULL
     936                        ),
     937                        array( '%s', '%s' )
     938                );
     939
     940                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     941
     942                $this->assertNull( $row );
     943        }
    810944}
    811 
Note: See TracChangeset for help on using the changeset viewer.