Ticket #19019: wpdb-condense.3.diff
File wpdb-condense.3.diff, 10.7 KB (added by , 12 years ago) |
---|
-
wp-includes/wp-db.php
1229 1229 1230 1230 return $return_val; 1231 1231 } 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 ); 1232 1259 1260 if ( empty( $format ) ) 1261 $format = '%s'; 1262 1263 $wheres[] = $values ? $format : "`$field` = {$format}"; 1264 } 1265 1266 return $wheres; 1267 } 1268 1233 1269 /** 1234 1270 * Insert a row into a table. 1235 1271 * … … 1245 1281 * 1246 1282 * @param string $table table name 1247 1283 * @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. 1249 1285 * 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. 1250 1286 * @return int|false The number of rows inserted, or false on error. 1251 1287 */ 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' ); 1254 1290 } 1255 1291 1256 1292 /** … … 1268 1304 * 1269 1305 * @param string $table table name 1270 1306 * @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. 1272 1308 * 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. 1273 1309 * @return int|false The number of rows affected, or false on error. 1274 1310 */ 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' ); 1277 1313 } 1278 1314 1279 1315 /** … … 1289 1325 * 1290 1326 * @param string $table table name 1291 1327 * @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. 1293 1329 * 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. 1294 1330 * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT. 1295 1331 * @return int|false The number of rows affected, or false on error. 1296 1332 */ 1297 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {1333 function _insert_replace_helper( $table, $data, $formats = null, $type = 'INSERT' ) { 1298 1334 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) 1299 1335 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)"; 1313 1341 return $this->query( $this->prepare( $sql, $data ) ); 1314 1342 } 1315 1343 1316 1344 /** 1317 1345 * Update a row in the table 1318 1346 * … … 1329 1357 * @param string $table table name 1330 1358 * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1331 1359 * @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. 1333 1361 * 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. 1335 1363 * @return int|false The number of rows updated, or false on error. 1336 1364 */ 1337 function update( $table, $data, $where, $format = null, $where_format= null ) {1365 function update( $table, $data, $where, $formats = null, $where_formats = null ) { 1338 1366 if ( ! is_array( $data ) || ! is_array( $where ) ) 1339 1367 return false; 1340 1368 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 ) ); 1352 1371 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"; 1365 1373 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 1366 1374 } 1367 1375 … … 1380 1388 * 1381 1389 * @param string $table table name 1382 1390 * @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. 1384 1392 * @return int|false The number of rows updated, or false on error. 1385 1393 */ 1386 function delete( $table, $where, $where_format = null ) {1394 function delete( $table, $where, $where_formats = null ) { 1387 1395 if ( ! is_array( $where ) ) 1388 1396 return false; 1389 1397 1390 $ bits = $wheres = array();1398 $wheres = implode( ' AND ', $this->format_clauses( $where, $where_formats ) ); 1391 1399 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"; 1407 1401 return $this->query( $this->prepare( $sql, $where ) ); 1408 1402 } 1409 1403 1410 1411 1404 /** 1412 1405 * Retrieve one variable from the database. 1413 1406 *