Changeset 34737
- Timestamp:
- 10/01/2015 05:36:15 AM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/wp-db.php
r34655 r34737 1780 1780 * @param array $data Data to insert (in column => value pairs). 1781 1781 * 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. 1782 1783 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. 1783 1784 * If string, that format will be used for all of the values in $data. … … 1804 1805 * @param array $data Data to insert (in column => value pairs). 1805 1806 * 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. 1806 1808 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. 1807 1809 * If string, that format will be used for all of the values in $data. … … 1828 1830 * @param array $data Data to insert (in column => value pairs). 1829 1831 * 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. 1830 1833 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. 1831 1834 * If string, that format will be used for all of the values in $data. … … 1849 1852 $formats = $values = array(); 1850 1853 foreach ( $data as $value ) { 1854 if ( is_null( $value['value'] ) ) { 1855 $formats[] = 'NULL'; 1856 continue; 1857 } 1858 1851 1859 $formats[] = $value['format']; 1852 1860 $values[] = $value['value']; … … 1876 1884 * @param array $data Data to update (in column => value pairs). 1877 1885 * 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. 1878 1888 * @param array $where A named array of WHERE clauses (in column => value pairs). 1879 1889 * Multiple clauses will be joined with ANDs. 1880 1890 * 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. 1881 1892 * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. 1882 1893 * If string, that format will be used for all of the values in $data. … … 1905 1916 $fields = $conditions = $values = array(); 1906 1917 foreach ( $data as $field => $value ) { 1918 if ( is_null( $value['value'] ) ) { 1919 $fields[] = "`$field` = NULL"; 1920 continue; 1921 } 1922 1907 1923 $fields[] = "`$field` = " . $value['format']; 1908 1924 $values[] = $value['value']; 1909 1925 } 1910 1926 foreach ( $where as $field => $value ) { 1927 if ( is_null( $value['value'] ) ) { 1928 $conditions[] = "`$field` IS NULL"; 1929 continue; 1930 } 1931 1911 1932 $conditions[] = "`$field` = " . $value['format']; 1912 1933 $values[] = $value['value']; … … 1937 1958 * Multiple clauses will be joined with ANDs. 1938 1959 * 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. 1939 1961 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. 1940 1962 * If string, that format will be used for all of the items in $where. … … 1955 1977 $conditions = $values = array(); 1956 1978 foreach ( $where as $field => $value ) { 1979 if ( is_null( $value['value'] ) ) { 1980 $conditions[] = "`$field` IS NULL"; 1981 continue; 1982 } 1983 1957 1984 $conditions[] = "`$field` = " . $value['format']; 1958 1985 $values[] = $value['value']; -
trunk/tests/phpunit/tests/db.php
r33718 r34737 808 808 return 'fake_col_charset'; 809 809 } 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 } 810 944 } 811
Note: See TracChangeset
for help on using the changeset viewer.