Ticket #19019: wpdb-condense.diff
File wpdb-condense.diff, 4.9 KB (added by , 12 years ago) |
---|
-
wp-includes/wp-db.php
1186 1186 } 1187 1187 1188 1188 /** 1189 * Helper function for resolving form field formats 1190 * 1191 * 1192 * @access private 1193 * @since 3.5 1194 * @see wpdb::$field_types 1195 * 1196 * @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. 1197 * 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. 1198 * @return string format of value. 1199 */ 1200 function _field_format( $field, $format = null ) { 1201 $formats = (array) $format; 1202 if ( ! empty( $format ) ) 1203 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1204 elseif ( isset( $this->field_types[$field] ) ) 1205 $form = $this->field_types[$field]; 1206 else 1207 $form = '%s'; 1208 return $form; 1209 } 1210 1211 /** 1189 1212 * Helper function for insert and replace. 1190 1213 * 1191 1214 * Runs an insert or replace query based on $type argument. … … 1206 1229 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { 1207 1230 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) 1208 1231 return false; 1209 $formats = $format = (array) $format;1210 1232 $fields = array_keys( $data ); 1211 1233 $formatted_fields = array(); 1212 foreach ( $fields as $field ) { 1213 if ( !empty( $format ) ) 1214 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1215 elseif ( isset( $this->field_types[$field] ) ) 1216 $form = $this->field_types[$field]; 1217 else 1218 $form = '%s'; 1219 $formatted_fields[] = $form; 1220 } 1234 foreach ( $fields as $field ) 1235 $formatted_fields[] = $this->_field_format( $field, $format ); 1236 1221 1237 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")"; 1222 1238 return $this->query( $this->prepare( $sql, $data ) ); 1223 1239 } 1224 1240 1225 1241 /** 1242 * Helper function for format of WHERE clauses 1243 * 1244 * @since 3.5.0 1245 * @see wpdb::$field_types 1246 * 1247 * @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". 1248 * @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. 1249 * @return array of formatted field = $value 1250 */ 1251 function _where_clauses( $where, $where_format = null ) { 1252 $wheres = array(); 1253 foreach ( (array) array_keys( $where ) as $field ) { 1254 $form = $this->_field_format( $field, $where_format ); 1255 $wheres[] = "`$field` = {$form}"; 1256 } 1257 1258 return $wheres; 1259 } 1260 1261 /** 1226 1262 * Update a row in the table 1227 1263 * 1228 1264 * <code> … … 1247 1283 if ( ! is_array( $data ) || ! is_array( $where ) ) 1248 1284 return false; 1249 1285 1250 $formats = $format = (array) $format; 1251 $bits = $wheres = array(); 1286 $bits = array(); 1252 1287 foreach ( (array) array_keys( $data ) as $field ) { 1253 if ( !empty( $format ) ) 1254 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1255 elseif ( isset($this->field_types[$field]) ) 1256 $form = $this->field_types[$field]; 1257 else 1258 $form = '%s'; 1288 $form = $this->_field_format( $field, $format ); 1259 1289 $bits[] = "`$field` = {$form}"; 1260 1290 } 1261 1291 1262 $where_formats = $where_format = (array) $where_format; 1263 foreach ( (array) array_keys( $where ) as $field ) { 1264 if ( !empty( $where_format ) ) 1265 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1266 elseif ( isset( $this->field_types[$field] ) ) 1267 $form = $this->field_types[$field]; 1268 else 1269 $form = '%s'; 1270 $wheres[] = "`$field` = {$form}"; 1271 } 1292 $wheres = $this->_where_clauses( $where, $where_format ); 1272 1293 1273 1294 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 1274 1295 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); … … 1296 1317 if ( ! is_array( $where ) ) 1297 1318 return false; 1298 1319 1299 $ bits = $wheres = array();1320 $wheres = $this->_where_clauses( $where, $where_format ); 1300 1321 1301 $where_formats = $where_format = (array) $where_format;1302 1303 foreach ( array_keys( $where ) as $field ) {1304 if ( !empty( $where_format ) ) {1305 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];1306 } elseif ( isset( $this->field_types[ $field ] ) ) {1307 $form = $this->field_types[ $field ];1308 } else {1309 $form = '%s';1310 }1311 1312 $wheres[] = "$field = $form";1313 }1314 1315 1322 $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres ); 1316 1323 return $this->query( $this->prepare( $sql, $where ) ); 1317 1324 }