r71791 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71790‎ | r71791 | r71792 >
Date:16:42, 27 August 2010
Author:simetrical
Status:reverted
Tags:
Comment:
Style fixes for r71609 and follow-ups

Avoid one-letter variable names, be descriptive. Name an unused value
variable in foreach "$unused". Add spaces where needed. Wrap comments
at 79 columns, not some arbitrarily-selected number. Remove blank line.
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/Database.php
@@ -1039,32 +1039,30 @@
10401040 }
10411041
10421042 /**
1043 - * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if
1044 - * duplicate primary key found
 1043+ * INSERT ... ON DUPLICATE KEY UPDATE wrapper, inserts an array into a
 1044+ * table, optionally updating if duplicate primary key found
10451045 *
1046 - * $a may be a single associative array, or an array of these with numeric keys, for
1047 - * multi-row insert.
 1046+ * $rows may be a single associative array, or an array of these with
 1047+ * numeric keys, for multi-row insert.
10481048 *
1049 - * Usually aborts on failure
1050 - * If errors are explicitly ignored, returns success
 1049+ * Usually aborts on failure. If errors are explicitly ignored, returns success.
10511050 *
10521051 * @param $table String: table name (prefix auto-added)
1053 - * @param $a Array: Array of rows to insert
 1052+ * @param $rows Array: Array of rows to insert
10541053 * @param $fname String: Calling function name (use __METHOD__) for logs/profiling
10551054 * @param $onDupeUpdate Array: Associative array of fields to update on duplicate
10561055 *
10571056 * @return bool
10581057 */
1059 - function insertOrUpdate( $table, $a, $fname = 'DatabaseBase::insertOrUpdate', $onDupeUpdate = array() ) {
1060 -
1061 - if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1062 - $keys = array_keys( $a[0] );
 1058+ function insertOrUpdate( $table, $rows, $fname = 'DatabaseBase::insertOrUpdate', $onDupeUpdate = array() ) {
 1059+ if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
 1060+ $keys = array_keys( $rows[0] );
10631061 } else {
1064 - $keys = array_keys( $a );
 1062+ $keys = array_keys( $rows );
10651063 }
10661064
1067 - //Get what is only to be set if inserted
1068 - $where = array_diff( $a, $onDupeUpdate );
 1065+ // Get what is only to be set if inserted
 1066+ $where = array_diff( $rows, $onDupeUpdate );
10691067
10701068 $res = $this->select(
10711069 $table,
@@ -1074,12 +1072,13 @@
10751073 );
10761074
10771075 if ( $res ) {
1078 - // Where there is a different value to set if this is being "updated", use the $onDupeUpdate value for that to
1079 - // replace the original option (if it was an insert), and replace the column name with the value read from
1080 - // the existing row
1081 - foreach( $where as $k => $v ) {
1082 - if ( isset( $onDupeUpdate[$k] ) ) {
1083 - $options[$k] = str_replace( $k, $res[0]->{$k}, $onDupeUpdate[$k] );
 1076+ // Where there is a different value to set if this is being
 1077+ // "updated", use the $onDupeUpdate value for that to replace the
 1078+ // original option (if it was an insert), and replace the column
 1079+ // name with the value read from the existing row
 1080+ foreach ( $where as $key => $unused ) {
 1081+ if ( isset( $onDupeUpdate[$key] ) ) {
 1082+ $options[$key] = str_replace( $key, $res[0]->{$key}, $onDupeUpdate[$key] );
10841083 }
10851084 }
10861085 } else {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r71609Create insertOnDupeUpdate, called by insert...reedy01:03, 25 August 2010

Status & tagging log