Ticket #19019: wpdb-condense.2.diff
File wpdb-condense.2.diff, 7.6 KB (added by , 13 years ago) |
---|
-
wp-includes/load.php
354 354 global $wpdb, $table_prefix; 355 355 if ( !empty( $wpdb->error ) ) 356 356 dead_db(); 357 357 /** 358 * List all columns in WP tables that should be formatted as %d 359 * These columns will be matched against the master list of tables in $wpdb->tables 360 * - so "parent" won't indiscrimately be cast to INT in a user-defined table where its type is VARCHAR or TEXT, etc 361 * 362 */ 358 363 $wpdb->field_types = array( 'post_author' => '%d', 'post_parent' => '%d', 'menu_order' => '%d', 'term_id' => '%d', 'term_group' => '%d', 'term_taxonomy_id' => '%d', 359 'parent' => '%d', 'count' => '%d','object_id' => '%d', 'term_order' => '%d', 'ID' => '%d', 'comm ment_ID' => '%d', 'comment_post_ID' => '%d', 'comment_parent' => '%d',364 'parent' => '%d', 'count' => '%d','object_id' => '%d', 'term_order' => '%d', 'ID' => '%d', 'comment_ID' => '%d', 'comment_post_ID' => '%d', 'comment_parent' => '%d', 360 365 'user_id' => '%d', 'link_id' => '%d', 'link_owner' => '%d', 'link_rating' => '%d', 'option_id' => '%d', 'blog_id' => '%d', 'meta_id' => '%d', 'post_id' => '%d', 361 366 'user_status' => '%d', 'umeta_id' => '%d', 'comment_karma' => '%d', 'comment_count' => '%d', 362 367 // multisite: -
wp-includes/wp-db.php
1186 1186 } 1187 1187 1188 1188 /** 1189 * Helper function for resolving form field formats 1190 * 1191 * @access protected 1192 * @since 3.5.0 1193 * @see wpdb::prepare() 1194 * @see wpdb::$field_types 1195 * @see wp_set_wpdb_vars() 1196 * 1197 * @param string $table table name 1198 * @param string $field the column to format 1199 * @param array|string $format Optional. An array of formats, the first item will be returned. 1200 * If string, that format will be used. 1201 * A format is one of '%d', '%f', '%s' (integer, float, string). 1202 * If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. 1203 * $format is passed by-reference so that array_shift() will move the cursor to the next item in $format on subsequent calls 1204 * 1205 * @return string format of value. 1206 */ 1207 protected function field_format( $table, $field, &$format = null ) { 1208 if ( ! empty( $format ) ) { 1209 if ( is_string( $format ) ) 1210 $form = $format; 1211 elseif ( is_array( $format ) ) 1212 $form = array_shift( $format ); 1213 } elseif ( in_array( $table, array_keys( $this->tables ) ) && isset( $this->field_types[$field] ) ) { 1214 $form = $this->field_types[$field]; 1215 } else { 1216 $form = '%s'; 1217 } 1218 1219 return $form; 1220 } 1221 1222 /** 1189 1223 * Helper function for insert and replace. 1190 1224 * 1191 1225 * Runs an insert or replace query based on $type argument. … … 1198 1232 * 1199 1233 * @param string $table table name 1200 1234 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1201 * @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.1235 * @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. 1202 1236 * 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. 1203 1237 * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT. 1204 1238 * @return int|false The number of rows affected, or false on error. … … 1206 1240 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { 1207 1241 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) 1208 1242 return false; 1209 $formats = $format = (array) $format;1210 1243 $fields = array_keys( $data ); 1211 1244 $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 } 1245 foreach ( $fields as $field ) 1246 $formatted_fields[] = $this->field_format( $table, $field, $format ); 1247 1221 1248 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")"; 1222 1249 return $this->query( $this->prepare( $sql, $data ) ); 1223 1250 } 1224 1251 1225 1252 /** 1253 * Helper function for format of WHERE clauses 1254 * 1255 * @access protected 1256 * @since 3.5.0 1257 * @see wpdb::prepare() 1258 * @see wpdb::$field_types 1259 * @see wp_set_wpdb_vars() 1260 * 1261 * @param string $table table name 1262 * @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". 1263 * @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. 1264 * @return array of formatted field => $value 1265 */ 1266 protected function where_clauses( $table, $where, $where_format = null ) { 1267 $wheres = array(); 1268 foreach ( (array) array_keys( $where ) as $field ) { 1269 $form = $this->field_format( $table, $field, $where_format ); 1270 $wheres[] = "`$field` = {$form}"; 1271 } 1272 1273 return $wheres; 1274 } 1275 1276 /** 1226 1277 * Update a row in the table 1227 1278 * 1228 1279 * <code> … … 1247 1298 if ( ! is_array( $data ) || ! is_array( $where ) ) 1248 1299 return false; 1249 1300 1250 $formats = $format = (array) $format; 1251 $bits = $wheres = array(); 1301 $bits = array(); 1252 1302 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'; 1303 $form = $this->field_format( $table, $field, $format ); 1259 1304 $bits[] = "`$field` = {$form}"; 1260 1305 } 1261 1306 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 } 1307 $wheres = $this->where_clauses( $table, $where, $where_format ); 1272 1308 1273 1309 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 1274 1310 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); … … 1296 1332 if ( ! is_array( $where ) ) 1297 1333 return false; 1298 1334 1299 $ bits = $wheres = array();1335 $wheres = $this->where_clauses( $table, $where, $where_format ); 1300 1336 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 1337 $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres ); 1316 1338 return $this->query( $this->prepare( $sql, $where ) ); 1317 1339 }