WordPress.org

Make WordPress Core

Ticket #19019: wpdb-condense.3.diff

File wpdb-condense.3.diff, 10.7 KB (added by wonderboymusic, 6 years ago)
  • wp-includes/wp-db.php

     
    12291229
    12301230                return $return_val;
    12311231        }
     1232       
     1233        /**
     1234         * Helper function for format of name value pairs joined by =
     1235         *
     1236         * @access protected
     1237         * @since 3.5.0
     1238         * @see wpdb::prepare()
     1239         * @see wpdb::$field_types
     1240         * @see wp_set_wpdb_vars()
     1241         *
     1242         * @param array $data A named array of clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $data columns and $data values should be "raw".
     1243         * @param array|string $formats Optional. An array of formats to be mapped to each of the values in $where.
     1244         *      If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string).
     1245         *      If omitted, all values in $where will be treated as strings.
     1246         * @param boolean $values If set to true, only returns an array of formats
     1247         * @return array of formatted field => $value
     1248         */
     1249        protected function format_clauses( $data, $formats = null, $values = false ) {
     1250                $wheres = array();
     1251                foreach ( (array) array_keys( $data ) as $field ) {
     1252                        $format = $formats;
     1253                       
     1254                        if ( isset( $this->field_types[$field] ) )
     1255                                $format = $this->field_types[$field];                                   
     1256                       
     1257                        if ( is_array( $formats ) )
     1258                                $format = empty( $formats ) ? '%s' : array_shift( $formats );
    12321259
     1260                        if ( empty( $format ) )
     1261                                $format = '%s';
     1262                       
     1263                        $wheres[] = $values ? $format : "`$field` = {$format}";
     1264                }
     1265
     1266                return $wheres;
     1267        }       
     1268       
    12331269        /**
    12341270         * Insert a row into a table.
    12351271         *
     
    12451281         *
    12461282         * @param string $table table name
    12471283         * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1248          * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
     1284         * @param array|string $formats Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
    12491285         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    12501286         * @return int|false The number of rows inserted, or false on error.
    12511287         */
    1252         function insert( $table, $data, $format = null ) {
    1253                 return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
     1288        function insert( $table, $data, $formats = null ) {
     1289                return $this->_insert_replace_helper( $table, $data, $formats, 'INSERT' );
    12541290        }
    12551291
    12561292        /**
     
    12681304         *
    12691305         * @param string $table table name
    12701306         * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1271          * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
     1307         * @param array|string $formats Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
    12721308         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    12731309         * @return int|false The number of rows affected, or false on error.
    12741310         */
    1275         function replace( $table, $data, $format = null ) {
    1276                 return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
     1311        function replace( $table, $data, $formats = null ) {
     1312                return $this->_insert_replace_helper( $table, $data, $formats, 'REPLACE' );
    12771313        }
    12781314
    12791315        /**
     
    12891325         *
    12901326         * @param string $table table name
    12911327         * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1292          * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
     1328         * @param array|string $formats Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
    12931329         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    12941330         * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
    12951331         * @return int|false The number of rows affected, or false on error.
    12961332         */
    1297         function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
     1333        function _insert_replace_helper( $table, $data, $formats = null, $type = 'INSERT' ) {
    12981334                if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
    12991335                        return false;
    1300                 $formats = $format = (array) $format;
    1301                 $fields = array_keys( $data );
    1302                 $formatted_fields = array();
    1303                 foreach ( $fields as $field ) {
    1304                         if ( !empty( $format ) )
    1305                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1306                         elseif ( isset( $this->field_types[$field] ) )
    1307                                 $form = $this->field_types[$field];
    1308                         else
    1309                                 $form = '%s';
    1310                         $formatted_fields[] = $form;
    1311                 }
    1312                 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
     1336
     1337                $fields = implode( '`,`', array_keys( $data ) );
     1338                $formatted_fields = implode( ",", $this->format_clauses( $data, $formats, true ) );
     1339               
     1340                $sql = "{$type} INTO `$table` (`$fields`) VALUES ($formatted_fields)";
    13131341                return $this->query( $this->prepare( $sql, $data ) );
    13141342        }
    1315 
     1343       
    13161344        /**
    13171345         * Update a row in the table
    13181346         *
     
    13291357         * @param string $table table name
    13301358         * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    13311359         * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
    1332          * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.
     1360         * @param array|string $formats Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.
    13331361         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    1334          * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings.
     1362         * @param array|string $where_formats Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings.
    13351363         * @return int|false The number of rows updated, or false on error.
    13361364         */
    1337         function update( $table, $data, $where, $format = null, $where_format = null ) {
     1365        function update( $table, $data, $where, $formats = null, $where_formats = null ) {
    13381366                if ( ! is_array( $data ) || ! is_array( $where ) )
    13391367                        return false;
    13401368
    1341                 $formats = $format = (array) $format;
    1342                 $bits = $wheres = array();
    1343                 foreach ( (array) array_keys( $data ) as $field ) {
    1344                         if ( !empty( $format ) )
    1345                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1346                         elseif ( isset($this->field_types[$field]) )
    1347                                 $form = $this->field_types[$field];
    1348                         else
    1349                                 $form = '%s';
    1350                         $bits[] = "`$field` = {$form}";
    1351                 }
     1369                $bits = implode( ', ', $this->format_clauses( $data, $formats ) );
     1370                $wheres = implode( ' AND ', $this->format_clauses( $where, $where_formats ) );
    13521371
    1353                 $where_formats = $where_format = (array) $where_format;
    1354                 foreach ( (array) array_keys( $where ) as $field ) {
    1355                         if ( !empty( $where_format ) )
    1356                                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    1357                         elseif ( isset( $this->field_types[$field] ) )
    1358                                 $form = $this->field_types[$field];
    1359                         else
    1360                                 $form = '%s';
    1361                         $wheres[] = "`$field` = {$form}";
    1362                 }
    1363 
    1364                 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
     1372                $sql = "UPDATE `$table` SET $bits WHERE $wheres";
    13651373                return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    13661374        }
    13671375
     
    13801388         *
    13811389         * @param string $table table name
    13821390         * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
    1383          * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
     1391         * @param array|string $where_formats Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
    13841392         * @return int|false The number of rows updated, or false on error.
    13851393         */
    1386         function delete( $table, $where, $where_format = null ) {
     1394        function delete( $table, $where, $where_formats = null ) {
    13871395                if ( ! is_array( $where ) )
    13881396                        return false;
    13891397
    1390                 $bits = $wheres = array();
     1398                $wheres = implode( ' AND ', $this->format_clauses( $where, $where_formats ) );
    13911399
    1392                 $where_formats = $where_format = (array) $where_format;
    1393 
    1394                 foreach ( array_keys( $where ) as $field ) {
    1395                         if ( !empty( $where_format ) ) {
    1396                                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    1397                         } elseif ( isset( $this->field_types[ $field ] ) ) {
    1398                                 $form = $this->field_types[ $field ];
    1399                         } else {
    1400                                 $form = '%s';
    1401                         }
    1402 
    1403                         $wheres[] = "$field = $form";
    1404                 }
    1405 
    1406                 $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
     1400                $sql = "DELETE FROM `$table` WHERE $wheres";
    14071401                return $this->query( $this->prepare( $sql, $where ) );
    14081402        }
    14091403
    1410 
    14111404        /**
    14121405         * Retrieve one variable from the database.
    14131406         *