Index: trunk/phase3/includes/db/Database.php |
— | — | @@ -1097,7 +1097,7 @@ |
1098 | 1098 | } |
1099 | 1099 | return !$indexInfo[0]->Non_unique; |
1100 | 1100 | } |
1101 | | - |
| 1101 | + |
1102 | 1102 | /** |
1103 | 1103 | * INSERT wrapper, inserts an array into a table |
1104 | 1104 | * |
— | — | @@ -1115,6 +1115,28 @@ |
1116 | 1116 | * @return bool |
1117 | 1117 | */ |
1118 | 1118 | function insert( $table, $a, $fname = 'Database::insert', $options = array() ) { |
| 1119 | + return this->insertOnDupeUpdate( $table, $a, $fname, $options ); |
| 1120 | + } |
| 1121 | + |
| 1122 | + /** |
| 1123 | + * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if |
| 1124 | + * duplicate primary key found |
| 1125 | + * |
| 1126 | + * $a may be a single associative array, or an array of these with numeric keys, for |
| 1127 | + * multi-row insert. |
| 1128 | + * |
| 1129 | + * Usually aborts on failure |
| 1130 | + * If errors are explicitly ignored, returns success |
| 1131 | + * |
| 1132 | + * @param $table String: table name (prefix auto-added) |
| 1133 | + * @param $a Array: Array of rows to insert |
| 1134 | + * @param $fname String: Calling function name (use __METHOD__) for logs/profiling |
| 1135 | + * @param $options Mixed: Associative array of options |
| 1136 | + * @param $onDupeUpdate Array: Associative array of fields to update on duplicate |
| 1137 | + * |
| 1138 | + * @return bool |
| 1139 | + */ |
| 1140 | + function insertOnDupeUpdate( $table, $a, $fname = 'Database::insertOnDupeUpdate', $options = array(), $onDupeUpdate = array() ) { |
1119 | 1141 | # No rows to insert, easy just return now |
1120 | 1142 | if ( !count( $a ) ) { |
1121 | 1143 | return true; |
— | — | @@ -1148,6 +1170,11 @@ |
1149 | 1171 | } else { |
1150 | 1172 | $sql .= '(' . $this->makeList( $a ) . ')'; |
1151 | 1173 | } |
| 1174 | + |
| 1175 | + if ( count( $onDupeUpdate ) ) { |
| 1176 | + $sql .= ' ON DUPLICATE KEY UPDATE ' . $this->makeList( $onDupeUpdate ); |
| 1177 | + } |
| 1178 | + |
1152 | 1179 | return (bool)$this->query( $sql, $fname ); |
1153 | 1180 | } |
1154 | 1181 | |