mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-12 13:44:21 +01:00
Add wpdb::replace() for replace queries. props AaronCampbell fixes #10864
git-svn-id: http://svn.automattic.com/wordpress/trunk@13454 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
9797dacaa0
commit
1a511e40e1
@ -1143,12 +1143,58 @@ class wpdb {
|
|||||||
* @see wp_set_wpdb_vars()
|
* @see wp_set_wpdb_vars()
|
||||||
*
|
*
|
||||||
* @param string $table table name
|
* @param string $table table name
|
||||||
* @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
|
* @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
|
||||||
* @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.
|
* @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.
|
||||||
* A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
|
* A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
|
||||||
* @return int|false The number of rows inserted, or false on error.
|
* @return int|false The number of rows inserted, or false on error.
|
||||||
*/
|
*/
|
||||||
function insert( $table, $data, $format = null ) {
|
function insert( $table, $data, $format = null ) {
|
||||||
|
return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace a row into a table.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
|
||||||
|
* wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
* @see wpdb::prepare()
|
||||||
|
* @see wpdb::$field_types
|
||||||
|
* @see wp_set_wpdb_vars()
|
||||||
|
*
|
||||||
|
* @param string $table table name
|
||||||
|
* @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
|
||||||
|
* @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.
|
||||||
|
* A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
|
||||||
|
* @return int|false The number of rows affected, or false on error.
|
||||||
|
*/
|
||||||
|
function replace( $table, $data, $format = null ) {
|
||||||
|
return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function for insert and replace.
|
||||||
|
*
|
||||||
|
* Runs an insert or replace query based on $type argument.
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
* @since 3.0.0
|
||||||
|
* @see wpdb::prepare()
|
||||||
|
* @see wpdb::$field_types
|
||||||
|
* @see wp_set_wpdb_vars()
|
||||||
|
*
|
||||||
|
* @param string $table table name
|
||||||
|
* @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
|
||||||
|
* @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.
|
||||||
|
* A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
|
||||||
|
* @return int|false The number of rows affected, or false on error.
|
||||||
|
*/
|
||||||
|
function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
|
||||||
|
if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
|
||||||
|
return false;
|
||||||
$formats = $format = (array) $format;
|
$formats = $format = (array) $format;
|
||||||
$fields = array_keys( $data );
|
$fields = array_keys( $data );
|
||||||
$formatted_fields = array();
|
$formatted_fields = array();
|
||||||
@ -1161,11 +1207,10 @@ class wpdb {
|
|||||||
$form = '%s';
|
$form = '%s';
|
||||||
$formatted_fields[] = $form;
|
$formatted_fields[] = $form;
|
||||||
}
|
}
|
||||||
$sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
|
$sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
|
||||||
return $this->query( $this->prepare( $sql, $data ) );
|
return $this->query( $this->prepare( $sql, $data ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update a row in the table
|
* Update a row in the table
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user