MCPcopy Create free account
hub / github.com/MariaDB/server / insert_on_duplicate_update

Method insert_on_duplicate_update

sql/sql_insert.cc:2235–2372  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2233
2234
2235int Write_record::insert_on_duplicate_update(ha_rows *inserted,
2236 ha_rows *updated)
2237{
2238 int res= table->file->ha_write_row(table->record[0]);
2239 if (likely(!res))
2240 return after_insert(inserted);
2241
2242 res=prepare_handle_duplicate(res);
2243 if (unlikely(res))
2244 return restore_on_error();
2245
2246 ulonglong prev_insert_id_for_cur_row= 0;
2247 /*
2248 We don't check for other UNIQUE keys - the first row
2249 that matches, is updated. If update causes a conflict again,
2250 an error is returned
2251 */
2252 DBUG_ASSERT(table->insert_values != NULL);
2253 store_record(table,insert_values);
2254 restore_record(table,record[1]);
2255 table->reset_default_fields();
2256
2257 /*
2258 in INSERT ... ON DUPLICATE KEY UPDATE the set of modified fields can
2259 change per row. Thus, we have to do reset_default_fields() per row.
2260 Twice (before insert and before update).
2261 */
2262 DBUG_ASSERT(info->update_fields->elements ==
2263 info->update_values->elements);
2264
2265 bool trg_skip_row= false;
2266 if (fill_record_n_invoke_before_triggers(thd, table,
2267 *info->update_fields,
2268 *info->update_values,
2269 info->ignore,
2270 TRG_EVENT_UPDATE,
2271 &trg_skip_row))
2272 return restore_on_error();
2273
2274 if (trg_skip_row)
2275 return 0;
2276
2277 bool different_records= (!records_are_comparable(table) ||
2278 compare_record(table));
2279 /*
2280 Default fields must be updated before checking view updateability.
2281 This branch of INSERT is executed only when a UNIQUE key was violated
2282 with the ON DUPLICATE KEY UPDATE option. In this case the INSERT
2283 operation is transformed to an UPDATE, and the default fields must
2284 be updated as if this is an UPDATE.
2285 */
2286 if (different_records && table->default_field)
2287 table->evaluate_update_default_function();
2288
2289 /* CHECK OPTION for VIEW ... ON DUPLICATE KEY UPDATE ... */
2290 res= info->table_list->view_check_option(table->in_use, info->ignore);
2291 if (unlikely(res))
2292 {

Callers

nothing calls this directly

Calls 15

records_are_comparableFunction · 0.85
vers_insert_history_rowFunction · 0.85
ha_write_rowMethod · 0.80
reset_default_fieldsMethod · 0.80
view_check_optionMethod · 0.80
ha_update_rowMethod · 0.80
vers_check_updateMethod · 0.80
compare_recordFunction · 0.70

Tested by

no test coverage detected