Ticket #19019: wpdb-condense.4.diff
File wpdb-condense.4.diff, 10.7 KB (added by , 12 years ago) |
---|
-
src/wp-includes/wp-db.php
1243 1243 1244 1244 return $return_val; 1245 1245 } 1246 1247 /** 1248 * Helper function for format of name value pairs joined by = 1249 * 1250 * @access protected 1251 * @since 3.5.0 1252 * @see wpdb::prepare() 1253 * @see wpdb::$field_types 1254 * @see wp_set_wpdb_vars() 1255 * 1256 * @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". 1257 * @param array|string $formats Optional. An array of formats to be mapped to each of the values in $where. 1258 * 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). 1259 * If omitted, all values in $where will be treated as strings. 1260 * @param boolean $values If set to true, only returns an array of formats 1261 * @return array of formatted field => $value 1262 */ 1263 protected function format_clauses( $data, $formats = null, $values = false ) { 1264 $wheres = array(); 1265 foreach ( (array) array_keys( $data ) as $field ) { 1266 $format = $formats; 1267 1268 if ( isset( $this->field_types[$field] ) ) 1269 $format = $this->field_types[$field]; 1270 1271 if ( is_array( $formats ) ) 1272 $format = empty( $formats ) ? '%s' : array_shift( $formats ); 1246 1273 1274 if ( empty( $format ) ) 1275 $format = '%s'; 1276 1277 $wheres[] = $values ? $format : "`$field` = {$format}"; 1278 } 1279 1280 return $wheres; 1281 } 1282 1247 1283 /** 1248 1284 * Insert a row into a table. 1249 1285 * … … 1259 1295 * 1260 1296 * @param string $table table name 1261 1297 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1262 * @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.1298 * @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. 1263 1299 * 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. 1264 1300 * @return int|false The number of rows inserted, or false on error. 1265 1301 */ 1266 function insert( $table, $data, $format = null ) {1267 return $this->_insert_replace_helper( $table, $data, $format , 'INSERT' );1302 function insert( $table, $data, $formats = null ) { 1303 return $this->_insert_replace_helper( $table, $data, $formats, 'INSERT' ); 1268 1304 } 1269 1305 1270 1306 /** … … 1282 1318 * 1283 1319 * @param string $table table name 1284 1320 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1285 * @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.1321 * @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. 1286 1322 * 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. 1287 1323 * @return int|false The number of rows affected, or false on error. 1288 1324 */ 1289 function replace( $table, $data, $format = null ) {1290 return $this->_insert_replace_helper( $table, $data, $format , 'REPLACE' );1325 function replace( $table, $data, $formats = null ) { 1326 return $this->_insert_replace_helper( $table, $data, $formats, 'REPLACE' ); 1291 1327 } 1292 1328 1293 1329 /** … … 1303 1339 * 1304 1340 * @param string $table table name 1305 1341 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1306 * @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.1342 * @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. 1307 1343 * 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. 1308 1344 * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT. 1309 1345 * @return int|false The number of rows affected, or false on error. 1310 1346 */ 1311 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {1347 function _insert_replace_helper( $table, $data, $formats = null, $type = 'INSERT' ) { 1312 1348 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) 1313 1349 return false; 1314 1350 $this->insert_id = 0; 1315 $formats = $format = (array) $format; 1316 $fields = array_keys( $data ); 1317 $formatted_fields = array(); 1318 foreach ( $fields as $field ) { 1319 if ( !empty( $format ) ) 1320 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1321 elseif ( isset( $this->field_types[$field] ) ) 1322 $form = $this->field_types[$field]; 1323 else 1324 $form = '%s'; 1325 $formatted_fields[] = $form; 1326 } 1327 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")"; 1351 1352 $fields = implode( '`,`', array_keys( $data ) ); 1353 $formatted_fields = implode( ",", $this->format_clauses( $data, $formats, true ) ); 1354 1355 $sql = "{$type} INTO `$table` (`$fields`) VALUES ($formatted_fields)"; 1328 1356 return $this->query( $this->prepare( $sql, $data ) ); 1329 1357 } 1330 1358 … … 1344 1372 * @param string $table table name 1345 1373 * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1346 1374 * @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". 1347 * @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.1375 * @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. 1348 1376 * 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. 1349 * @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.1377 * @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. 1350 1378 * @return int|false The number of rows updated, or false on error. 1351 1379 */ 1352 function update( $table, $data, $where, $format = null, $where_format= null ) {1380 function update( $table, $data, $where, $formats = null, $where_formats = null ) { 1353 1381 if ( ! is_array( $data ) || ! is_array( $where ) ) 1354 1382 return false; 1355 1383 1356 $formats = $format = (array) $format; 1357 $bits = $wheres = array(); 1358 foreach ( (array) array_keys( $data ) as $field ) { 1359 if ( !empty( $format ) ) 1360 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1361 elseif ( isset($this->field_types[$field]) ) 1362 $form = $this->field_types[$field]; 1363 else 1364 $form = '%s'; 1365 $bits[] = "`$field` = {$form}"; 1366 } 1384 $bits = implode( ', ', $this->format_clauses( $data, $formats ) ); 1385 $wheres = implode( ' AND ', $this->format_clauses( $where, $where_formats ) ); 1367 1386 1368 $where_formats = $where_format = (array) $where_format; 1369 foreach ( (array) array_keys( $where ) as $field ) { 1370 if ( !empty( $where_format ) ) 1371 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1372 elseif ( isset( $this->field_types[$field] ) ) 1373 $form = $this->field_types[$field]; 1374 else 1375 $form = '%s'; 1376 $wheres[] = "`$field` = {$form}"; 1377 } 1378 1379 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 1387 $sql = "UPDATE `$table` SET $bits WHERE $wheres"; 1380 1388 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 1381 1389 } 1382 1390 … … 1395 1403 * 1396 1404 * @param string $table table name 1397 1405 * @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". 1398 * @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.1406 * @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. 1399 1407 * @return int|false The number of rows updated, or false on error. 1400 1408 */ 1401 function delete( $table, $where, $where_format = null ) {1409 function delete( $table, $where, $where_formats = null ) { 1402 1410 if ( ! is_array( $where ) ) 1403 1411 return false; 1404 1412 1405 $ bits = $wheres = array();1413 $wheres = implode( ' AND ', $this->format_clauses( $where, $where_formats ) ); 1406 1414 1407 $where_formats = $where_format = (array) $where_format; 1408 1409 foreach ( array_keys( $where ) as $field ) { 1410 if ( !empty( $where_format ) ) { 1411 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1412 } elseif ( isset( $this->field_types[ $field ] ) ) { 1413 $form = $this->field_types[ $field ]; 1414 } else { 1415 $form = '%s'; 1416 } 1417 1418 $wheres[] = "$field = $form"; 1419 } 1420 1421 $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres ); 1415 $sql = "DELETE FROM `$table` WHERE $wheres"; 1422 1416 return $this->query( $this->prepare( $sql, $where ) ); 1423 1417 } 1424 1418 1425 1426 1419 /** 1427 1420 * Retrieve one variable from the database. 1428 1421 *