Make WordPress Core

Ticket #41956: 41956.5.diff

File 41956.5.diff, 8.3 KB (added by birgire, 7 years ago)
  • src/wp-includes/wp-db.php

    diff --git src/wp-includes/wp-db.php src/wp-includes/wp-db.php
    index 77b64da..6601a72 100644
    class wpdb { 
    7777        public $last_error = '';
    7878
    7979        /**
     80         * The last error code during query.
     81         *
     82         * @since ?.?.?
     83         * @var int
     84         */
     85        protected $last_err_no = 0;
     86
     87        /**
     88         * The last error message during query.
     89         *
     90         * @var string
     91         */
     92        protected $last_err_msg = '';
     93
     94        /**
    8095         * Amount of queries made
    8196         *
    8297         * @since 1.2.0
    class wpdb { 
    14191434        public function print_error( $str = '' ) {
    14201435                global $EZSQL_ERROR;
    14211436
    1422                 if ( ! $str ) {
    1423                         if ( $this->use_mysqli ) {
    1424                                 $str = mysqli_error( $this->dbh );
    1425                         } else {
    1426                                 $str = mysql_error( $this->dbh );
    1427                         }
     1437                if ( '' === $str ) {
     1438                        $str = $this->last_err_msg;
    14281439                }
    14291440                $EZSQL_ERROR[] = array(
    14301441                        'query'     => $this->last_query,
    14311442                        'error_str' => $str,
     1443                        'error_no'  => $this->last_err_no,
    14321444                );
    14331445
    14341446                if ( $this->suppress_errors ) {
    class wpdb { 
    14811493        }
    14821494
    14831495        /**
     1496         * Retrieve error code from the last database error.
     1497         *
     1498         * @since ?.?.?
     1499         *
     1500         * @return int Error code, Zero on no error has occurred.
     1501         */
     1502        public function get_err_no() {
     1503                return $this->last_err_no;
     1504        }
     1505
     1506        /**
     1507         * Retrieve error message from the last database error.
     1508         *
     1509         * @since ?.?.?
     1510         *
     1511         * @return string Error message, empty string on no error has occurred.
     1512         */
     1513        public function get_err_msg() {
     1514                return $this->last_err_msg;
     1515        }
     1516
     1517        /**
     1518         * Retrieve error info from the last database error.
     1519         *
     1520         * @since ?.?.?
     1521         *
     1522         * @return array|bool Array containing error no and message, false on no error has occurred.
     1523         */
     1524        public function get_last_error() {
     1525                if ( $this->last_err_no > 0 ) {
     1526                        return array(
     1527                                'error_no'      => $this->last_err_no,
     1528                                'error_message' => $this->last_err_msg,
     1529                        );
     1530                } else {
     1531                        return false;
     1532                }
     1533        }
     1534
     1535        /**
    14841536         * Enables showing of database errors.
    14851537         *
    14861538         * This function should be used only to enable showing of errors.
    class wpdb { 
    15441596                $this->last_query    = null;
    15451597                $this->rows_affected = $this->num_rows = 0;
    15461598                $this->last_error    = '';
     1599                $this->last_err_no   = 0;
     1600                $this->last_err_msg  = '';
    15471601
    15481602                if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
    15491603                        mysqli_free_result( $this->result );
    class wpdb { 
    18931947                $this->_do_query( $query );
    18941948
    18951949                // MySQL server has gone away, try to reconnect.
    1896                 $mysql_errno = 0;
     1950                $this->last_err_no = 0;
    18971951                if ( ! empty( $this->dbh ) ) {
    18981952                        if ( $this->use_mysqli ) {
    18991953                                if ( $this->dbh instanceof mysqli ) {
    1900                                         $mysql_errno = mysqli_errno( $this->dbh );
     1954                                        $this->last_err_no = mysqli_errno( $this->dbh );
    19011955                                } else {
    19021956                                        // $dbh is defined, but isn't a real connection.
    19031957                                        // Something has gone horribly wrong, let's try a reconnect.
    1904                                         $mysql_errno = 2006;
     1958                                        $this->last_err_no = 2006;
    19051959                                }
    19061960                        } else {
    19071961                                if ( is_resource( $this->dbh ) ) {
    1908                                         $mysql_errno = mysql_errno( $this->dbh );
     1962                                        $this->last_err_no = mysql_errno( $this->dbh );
    19091963                                } else {
    1910                                         $mysql_errno = 2006;
     1964                                        $this->last_err_no = 2006;
    19111965                                }
    19121966                        }
    19131967                }
    19141968
    1915                 if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
     1969                if ( empty( $this->dbh ) || 2006 == $this->last_err_no ) {
    19161970                        if ( $this->check_connection() ) {
    19171971                                $this->_do_query( $query );
    19181972                        } else {
    19191973                                $this->insert_id = 0;
    19201974                                return false;
    19211975                        }
    1922                 }
    1923 
    1924                 // If there is an error then take note of it.
    1925                 if ( $this->use_mysqli ) {
    1926                         if ( $this->dbh instanceof mysqli ) {
    1927                                 $this->last_error = mysqli_error( $this->dbh );
    1928                         } else {
    1929                                 $this->last_error = __( 'Unable to retrieve the error message from MySQL' );
    1930                         }
    1931                 } else {
    1932                         if ( is_resource( $this->dbh ) ) {
    1933                                 $this->last_error = mysql_error( $this->dbh );
     1976                } elseif ( 0 !== $this->last_err_no ) {
     1977                        // If there is an error then take note of it.
     1978                        if ( $this->use_mysqli ) {
     1979                                if ( $this->dbh instanceof mysqli ) {
     1980                                        $this->last_error   = mysqli_error( $this->dbh );
     1981                                        $this->last_err_msg = mysqli_error( $this->dbh );
     1982                                } else {
     1983                                        $this->last_error = __( 'Unable to retrieve the error message from MySQL' );
     1984                                }
    19341985                        } else {
    1935                                 $this->last_error = __( 'Unable to retrieve the error message from MySQL' );
     1986                                if ( is_resource( $this->dbh ) ) {
     1987                                        $this->last_error   = mysql_error( $this->dbh );
     1988                                        $this->last_err_msg = mysql_error( $this->dbh );
     1989                                } else {
     1990                                        $this->last_error = __( 'Unable to retrieve the error message from MySQL' );
     1991                                }
    19361992                        }
    1937                 }
    19381993
    1939                 if ( $this->last_error ) {
    19401994                        // Clear insert_id on a subsequent failed insert.
    19411995                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
    19421996                                $this->insert_id = 0;
  • tests/phpunit/tests/db.php

    diff --git tests/phpunit/tests/db.php tests/phpunit/tests/db.php
    index 5cfd3d4..2978de0 100644
    class Tests_DB extends WP_UnitTestCase { 
    17711771                        ),
    17721772                );
    17731773        }
     1774
     1775        /**
     1776         * The get_err_no() method should return zero when no errors.
     1777         *
     1778         * @ticket 41956
     1779         */
     1780        public function test_get_err_no_should_return_zero_when_no_errors() {
     1781                global $wpdb;
     1782                $this->assertSame( 0, $wpdb->get_err_no() );
     1783        }
     1784
     1785        /**
     1786         * The get_err_no() method should return the error code from the last error.
     1787         *
     1788         * @ticket 41956
     1789         */
     1790        public function test_get_err_no_should_return_error_code_from_last_error() {
     1791                global $wpdb;
     1792
     1793                // Generate error code 1054 (ER_BAD_FIELD_ERROR).
     1794                $invalid_sql_1054 = "SELECT unknown_column FROM {$wpdb->posts}";
     1795                $wpdb->suppress_errors( true );
     1796                $wpdb->query( $invalid_sql_1054 );
     1797                $wpdb->suppress_errors( false );
     1798
     1799                // Generate error code 1064 (ER_PARSE_ERROR).
     1800                $invalid_sql_1064 = 'SELECT 2 FROM';
     1801                $wpdb->suppress_errors( true );
     1802                $wpdb->query( $invalid_sql_1064 );
     1803                $wpdb->suppress_errors( false );
     1804
     1805                $this->assertSame( 1064, $wpdb->get_err_no() );
     1806        }
     1807
     1808        /**
     1809         * The get_err_msg() method should return an empty string when no errors.
     1810         *
     1811         * @ticket 41956
     1812         */
     1813        public function test_get_err_msg_should_return_empty_string_when_no_errors() {
     1814                global $wpdb;
     1815                $this->assertSame( '', $wpdb->get_err_msg() );
     1816        }
     1817
     1818        /**
     1819         * The get_err_msg() method should return the error message from the last error.
     1820         *
     1821         * @ticket 41956
     1822         */
     1823        public function test_get_err_msg_should_return_message_from_last_error() {
     1824                global $wpdb;
     1825
     1826                // Generate error code 1064 (ER_PARSE_ERROR) containing the message: %s near '%s' at line %d
     1827                $invalid_sql_1064 = 'SELECT 2 FROM';
     1828                $wpdb->suppress_errors( true );
     1829                $wpdb->query( $invalid_sql_1064 );
     1830                $wpdb->suppress_errors( false );
     1831
     1832                // Generate error code 1054 (ER_BAD_FIELD_ERROR) containing the message: Unknown column '%s' in '%s'.
     1833                $invalid_sql_1054 = "SELECT unknown_column FROM {$wpdb->posts}";
     1834                $wpdb->suppress_errors( true );
     1835                $wpdb->query( $invalid_sql_1054 );
     1836                $wpdb->suppress_errors( false );
     1837
     1838                $this->assertContains( 'Unknown column', $wpdb->get_err_msg() );
     1839        }
     1840
     1841        /**
     1842         * The get_last_error() method should return false when no errors.
     1843         *
     1844         * @ticket 41956
     1845         */
     1846        public function test_get_last_error_should_return_false_without_errors() {
     1847                global $wpdb;
     1848                $this->assertFalse( $wpdb->get_last_error() );
     1849        }
     1850
     1851        /**
     1852         * The get_last_error() method should return an array of error code and message from the last error.
     1853         *
     1854         * @ticket 41956
     1855         */
     1856        public function test_get_last_error_should_return_error_code_and_msg_from_last_error() {
     1857                global $wpdb;
     1858
     1859                // Generate error code 1064 (ER_PARSE_ERROR) containing the message: %s near '%s' at line %d
     1860                $invalid_sql_1064 = 'SELECT 2 FROM';
     1861                $wpdb->suppress_errors( true );
     1862                $wpdb->query( $invalid_sql_1064 );
     1863                $wpdb->suppress_errors( false );
     1864
     1865                // Generate error code 1054 (ER_BAD_FIELD_ERROR) containing the message: Unknown column '%s' in '%s'.
     1866                $invalid_sql_1054 = "SELECT unknown_column FROM {$wpdb->posts}";
     1867                $wpdb->suppress_errors( true );
     1868                $wpdb->query( $invalid_sql_1054 );
     1869                $wpdb->suppress_errors( false );
     1870
     1871                $actual = $wpdb->get_last_error();
     1872
     1873                $this->assertEquals( $invalid_sql_1054, $wpdb->last_query );
     1874                $this->assertCount( 2, $actual );
     1875
     1876                $this->assertArrayHasKey( 'error_no', $actual );
     1877                $this->assertArrayHasKey( 'error_message', $actual );
     1878
     1879                $this->assertSame( 1054, $actual['error_no'] );
     1880                $this->assertContains( 'Unknown column', $actual['error_message'] );
     1881        }
    17741882}