Index: trunk/phase3/includes/db/Database.php |
— | — | @@ -1164,15 +1164,49 @@ |
1165 | 1165 | * @param $table String: table name (prefix auto-added) |
1166 | 1166 | * @param $a Array: Array of rows to insert |
1167 | 1167 | * @param $fname String: Calling function name (use __METHOD__) for logs/profiling |
1168 | | - * @param $options Mixed: Associative array of options |
| 1168 | + * @param $options Mixed: Associative array of options (ignored) |
1169 | 1169 | * @param $onDupeUpdate Array: Associative array of fields to update on duplicate |
1170 | 1170 | * |
1171 | 1171 | * @return bool |
1172 | 1172 | */ |
1173 | 1173 | function insertOrUpdate( $table, $a, $fname = 'DatabaseBase::insertOnDupeUpdate', $options = array(), $onDupeUpdate = array() ) { |
1174 | | - //TODO:FIXME |
1175 | | - //$this->select(); |
1176 | | - //$this->replace(); |
| 1174 | + |
| 1175 | + if ( isset( $a[0] ) && is_array( $a[0] ) ) { |
| 1176 | + $keys = array_keys( $a[0] ); |
| 1177 | + } else { |
| 1178 | + $keys = array_keys( $a ); |
| 1179 | + } |
| 1180 | + |
| 1181 | + //Get what is only to be set if inserted |
| 1182 | + $where = array_diff( $a, $onDupeUpdate ); |
| 1183 | + |
| 1184 | + $res = $this->select( |
| 1185 | + $table, |
| 1186 | + $keys, |
| 1187 | + $this->makeList( $where, LIST_AND ), |
| 1188 | + __METHOD__ |
| 1189 | + ); |
| 1190 | + |
| 1191 | + if ( $res ) { |
| 1192 | + //Where there is a different value to set if this is being "updated", use the $onDupeUpdate value for that to |
| 1193 | + //replace the original option (if it was an insert), and replace the column name with the value read from |
| 1194 | + //the existing row |
| 1195 | + foreach( $where as $k => $v ){ |
| 1196 | + if ( isset( $onDupeUpdate[$k] ) ){ |
| 1197 | + $options[$k] = str_replace( $k, $res[0]->{$k}, $onDupeUpdate[$k] ); |
| 1198 | + } |
| 1199 | + } |
| 1200 | + } else { |
| 1201 | + //No results, it's just an insert |
| 1202 | + $update = $where; |
| 1203 | + } |
| 1204 | + |
| 1205 | + return (bool)$this->replace( |
| 1206 | + $table, |
| 1207 | + $update, |
| 1208 | + array(), |
| 1209 | + __METHOD__ |
| 1210 | + ); |
1177 | 1211 | } |
1178 | 1212 | |
1179 | 1213 | /** |
— | — | @@ -1660,6 +1694,9 @@ |
1661 | 1695 | } |
1662 | 1696 | $sql .= '(' . $this->makeList( $row ) . ')'; |
1663 | 1697 | } |
| 1698 | + if ($fname === 'insertOnDupeUpdate') { |
| 1699 | + var_dump($sql); die(); |
| 1700 | + } |
1664 | 1701 | return $this->query( $sql, $fname ); |
1665 | 1702 | } |
1666 | 1703 | |