WordPress.org

Make WordPress Core

Ticket #46287: wp-db-insert-ignore.patch

File wp-db-insert-ignore.patch, 2.7 KB (added by lev0, 6 months ago)
  • wp-db.php

    a b  
    21292129        }
    21302130
    21312131        /**
     2132         * Insert a row into a table ignoring errors, primarily to avoid an insert when it would conflict with an existing unique key entry.
     2133         *
     2134         *     wpdb::insert_ignore( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
     2135         *     wpdb::insert_ignore( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
     2136         *
     2137         * @since 5.0.4
     2138         * @see wpdb::prepare()
     2139         * @see wpdb::$field_types
     2140         * @see wp_set_wpdb_vars()
     2141         *
     2142         * @param string       $table  Table name
     2143         * @param array        $data   Data to insert (in column => value pairs).
     2144         *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     2145         *                             Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
     2146         * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
     2147         *                             If string, that format will be used for all of the values in $data.
     2148         *                             A format is one of '%d', '%f', '%s' (integer, float, string).
     2149         *                             If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
     2150         * @return int|false The number of rows inserted, or false on error. This may be 0 so use identity operators (===, !==) to check for failure.
     2151         */
     2152        public function insert_ignore( $table, $data, $format = null ) {
     2153                return $this->_insert_replace_helper( $table, $data, $format, 'INSERT IGNORE' );
     2154        }
     2155
     2156
     2157        /**
    21322158         * Helper function for insert and replace.
    21332159         *
    21342160         * Runs an insert or replace query based on $type argument.
     
    21462172         *                             If string, that format will be used for all of the values in $data.
    21472173         *                             A format is one of '%d', '%f', '%s' (integer, float, string).
    21482174         *                             If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    2149          * @param string $type         Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
     2175         * @param string $type         Optional. What type of operation is this? INSERT, REPLACE or INSERT IGNORE. Defaults to INSERT.
    21502176         * @return int|false The number of rows affected, or false on error.
    21512177         */
    21522178        function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
    21532179                $this->insert_id = 0;
    21542180
    2155                 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) {
     2181                if ( ! in_array( strtoupper( $type ), array( 'INSERT', 'REPLACE', 'INSERT IGNORE' ) ) ) {
    21562182                        return false;
    21572183                }
    21582184