~ubuntu-branches/ubuntu/hardy/mysql-dfsg-5.0/hardy-updates

« back to all changes in this revision

Viewing changes to sql/sql_insert.cc

  • Committer: Bazaar Package Importer
  • Author(s): sean finney
  • Date: 2007-05-13 12:32:45 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20070513123245-8c3l187dk34cz2ar
Tags: 5.0.41-2
the previous "translation changes" inadvertently introduced unrelated
changes in the package control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
330
330
 
331
331
 
332
332
/*
 
333
  Prepare triggers  for INSERT-like statement.
 
334
 
 
335
  SYNOPSIS
 
336
    prepare_triggers_for_insert_stmt()
 
337
      thd     The current thread
 
338
      table   Table to which insert will happen
 
339
      duplic  Type of duplicate handling for insert which will happen
 
340
 
 
341
  NOTE
 
342
    Prepare triggers for INSERT-like statement by marking fields
 
343
    used by triggers and inform handlers that batching of UPDATE/DELETE 
 
344
    cannot be done if there are BEFORE UPDATE/DELETE triggers.
 
345
*/
 
346
 
 
347
void prepare_triggers_for_insert_stmt(THD *thd, TABLE *table,
 
348
                                      enum_duplicates duplic)
 
349
{
 
350
  if (table->triggers)
 
351
  {
 
352
    if (table->triggers->has_triggers(TRG_EVENT_DELETE,
 
353
                                      TRG_ACTION_AFTER))
 
354
    {
 
355
      /*
 
356
        The table has AFTER DELETE triggers that might access to 
 
357
        subject table and therefore might need delete to be done 
 
358
        immediately. So we turn-off the batching.
 
359
      */ 
 
360
      (void) table->file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
 
361
    }
 
362
    if (table->triggers->has_triggers(TRG_EVENT_UPDATE,
 
363
                                      TRG_ACTION_AFTER))
 
364
    {
 
365
      /*
 
366
        The table has AFTER UPDATE triggers that might access to subject 
 
367
        table and therefore might need update to be done immediately. 
 
368
        So we turn-off the batching.
 
369
      */ 
 
370
      (void) table->file->extra(HA_EXTRA_UPDATE_CANNOT_BATCH);
 
371
    }
 
372
    mark_fields_used_by_triggers_for_insert_stmt(thd, table, duplic);
 
373
  }
 
374
}
 
375
 
 
376
 
 
377
/*
333
378
  Mark fields used by triggers for INSERT-like statement.
334
379
 
335
380
  SYNOPSIS
470
515
  if (res || thd->is_fatal_error)
471
516
    DBUG_RETURN(TRUE);
472
517
 
473
 
  thd->proc_info="init";
 
518
  thd_proc_info(thd, "init");
474
519
  thd->used_tables=0;
475
520
  values= its++;
 
521
  value_count= values->elements;
476
522
 
477
523
  if (mysql_prepare_insert(thd, table_list, table, fields, values,
478
524
                           update_fields, update_values, duplic, &unused_conds,
479
 
                           FALSE))
 
525
                           FALSE,
 
526
                           (fields.elements || !value_count),
 
527
                           !ignore && (thd->variables.sql_mode &
 
528
                                       (MODE_STRICT_TRANS_TABLES |
 
529
                                        MODE_STRICT_ALL_TABLES))))
480
530
    goto abort;
481
531
 
482
532
  /* mysql_prepare_insert set table_list->table if it was not set */
502
552
  table_list->next_local= 0;
503
553
  context->resolve_in_table_list_only(table_list);
504
554
 
505
 
  value_count= values->elements;
506
555
  while ((values= its++))
507
556
  {
508
557
    counter++;
522
571
  /*
523
572
    Fill in the given fields and dump it to the table file
524
573
  */
525
 
  info.records= info.deleted= info.copied= info.updated= 0;
 
574
  info.records= info.deleted= info.copied= info.updated= info.touched= 0;
526
575
  info.ignore= ignore;
527
576
  info.handle_duplicates=duplic;
528
577
  info.update_fields= &update_fields;
551
600
 
552
601
  error=0;
553
602
  id=0;
554
 
  thd->proc_info="update";
 
603
  thd_proc_info(thd, "update");
555
604
  if (duplic != DUP_ERROR || ignore)
556
605
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
557
606
  if (duplic == DUP_REPLACE)
581
630
    table->file->start_bulk_insert(values_list.elements);
582
631
 
583
632
  thd->no_trans_update= 0;
584
 
  thd->abort_on_warning= (!ignore &&
585
 
                          (thd->variables.sql_mode &
586
 
                           (MODE_STRICT_TRANS_TABLES |
587
 
                            MODE_STRICT_ALL_TABLES)));
588
 
 
589
 
  if ((fields.elements || !value_count) &&
590
 
      check_that_all_fields_are_given_values(thd, table, table_list))
591
 
  {
592
 
    /* thd->net.report_error is now set, which will abort the next loop */
593
 
    error= 1;
594
 
  }
595
 
 
596
 
  mark_fields_used_by_triggers_for_insert_stmt(thd, table, duplic);
 
633
  thd->abort_on_warning= (!ignore && (thd->variables.sql_mode &
 
634
                                       (MODE_STRICT_TRANS_TABLES |
 
635
                                        MODE_STRICT_ALL_TABLES)));
 
636
 
 
637
  prepare_triggers_for_insert_stmt(thd, table, duplic);
597
638
 
598
639
  if (table_list->prepare_where(thd, 0, TRUE) ||
599
640
      table_list->prepare_check_option(thd))
757
798
      thd->lock=0;
758
799
    }
759
800
  }
760
 
  thd->proc_info="end";
 
801
  thd_proc_info(thd, "end");
761
802
  table->next_number_field=0;
762
803
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
763
804
  thd->next_insert_id=0;                        // Reset this if wrongly used
 
805
  table->auto_increment_field_not_null= FALSE;
764
806
  if (duplic != DUP_ERROR || ignore)
765
807
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
766
808
  if (duplic == DUP_REPLACE &&
767
809
      (!table->triggers || !table->triggers->has_delete_triggers()))
768
810
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
769
811
 
770
 
  /* Reset value of LAST_INSERT_ID if no rows where inserted */
771
 
  if (!info.copied && thd->insert_id_used)
 
812
  /* Reset value of LAST_INSERT_ID if no rows were inserted or touched */
 
813
  if (!info.copied && !info.touched && thd->insert_id_used)
772
814
  {
773
815
    thd->insert_id(0);
774
816
    id=0;
963
1005
                        be taken from table_list->table)    
964
1006
    where               Where clause (for insert ... select)
965
1007
    select_insert       TRUE if INSERT ... SELECT statement
 
1008
    check_fields        TRUE if need to check that all INSERT fields are 
 
1009
                        given values.
 
1010
    abort_on_warning    whether to report if some INSERT field is not 
 
1011
                        assigned as an error (TRUE) or as a warning (FALSE).
966
1012
 
967
1013
  TODO (in far future)
968
1014
    In cases of:
983
1029
                          TABLE *table, List<Item> &fields, List_item *values,
984
1030
                          List<Item> &update_fields, List<Item> &update_values,
985
1031
                          enum_duplicates duplic,
986
 
                          COND **where, bool select_insert)
 
1032
                          COND **where, bool select_insert,
 
1033
                          bool check_fields, bool abort_on_warning)
987
1034
{
988
1035
  SELECT_LEX *select_lex= &thd->lex->select_lex;
989
1036
  Name_resolution_context *context= &select_lex->context;
1046
1093
    table_list->next_local= 0;
1047
1094
    context->resolve_in_table_list_only(table_list);
1048
1095
 
1049
 
    if (!(res= check_insert_fields(thd, context->table_list, fields, *values,
1050
 
                                 !insert_into_view, &map) ||
1051
 
          setup_fields(thd, 0, *values, 0, 0, 0)) 
1052
 
        && duplic == DUP_UPDATE)
 
1096
    res= check_insert_fields(thd, context->table_list, fields, *values,
 
1097
                             !insert_into_view, &map) ||
 
1098
      setup_fields(thd, 0, *values, 0, 0, 0);
 
1099
 
 
1100
    if (!res && check_fields)
 
1101
    {
 
1102
      bool saved_abort_on_warning= thd->abort_on_warning;
 
1103
      thd->abort_on_warning= abort_on_warning;
 
1104
      res= check_that_all_fields_are_given_values(thd, 
 
1105
                                                  table ? table : 
 
1106
                                                  context->table_list->table,
 
1107
                                                  context->table_list);
 
1108
      thd->abort_on_warning= saved_abort_on_warning;
 
1109
    }
 
1110
 
 
1111
    if (!res && duplic == DUP_UPDATE)
1053
1112
    {
1054
1113
      select_lex->no_wrap_view_item= TRUE;
1055
1114
      res= check_update_fields(thd, context->table_list, update_fields, &map);
1221
1280
          }
1222
1281
          goto err;
1223
1282
        }
 
1283
 
 
1284
        if (table->next_number_field)
 
1285
          table->file->adjust_next_insert_id_after_explicit_value(
 
1286
            table->next_number_field->val_int());
 
1287
        info->touched++;
 
1288
 
1224
1289
        if ((table->file->table_flags() & HA_PARTIAL_COLUMN_READ) ||
1225
1290
            compare_record(table, thd->query_id))
1226
1291
        {
1227
1292
          info->updated++;
1228
 
 
1229
 
          if (table->next_number_field)
1230
 
            table->file->adjust_next_insert_id_after_explicit_value(
1231
 
              table->next_number_field->val_int());
1232
 
 
1233
1293
          trg_error= (table->triggers &&
1234
1294
                      table->triggers->process_triggers(thd, TRG_EVENT_UPDATE,
1235
1295
                                                        TRG_ACTION_AFTER,
1236
1296
                                                        TRUE));
1237
1297
          info->copied++;
1238
1298
        }
 
1299
 
1239
1300
        goto ok_or_after_trg_err;
1240
1301
      }
1241
1302
      else /* DUP_REPLACE */
1492
1553
 
1493
1554
delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list)
1494
1555
{
1495
 
  thd->proc_info="waiting for delay_list";
 
1556
  thd_proc_info(thd, "waiting for delay_list");
1496
1557
  pthread_mutex_lock(&LOCK_delayed_insert);     // Protect master list
1497
1558
  I_List_iterator<delayed_insert> it(delayed_threads);
1498
1559
  delayed_insert *tmp;
1529
1590
    */
1530
1591
    if (delayed_insert_threads >= thd->variables.max_insert_delayed_threads)
1531
1592
      DBUG_RETURN(0);
1532
 
    thd->proc_info="Creating delayed handler";
 
1593
    thd_proc_info(thd, "Creating delayed handler");
1533
1594
    pthread_mutex_lock(&LOCK_delayed_create);
1534
1595
    /*
1535
1596
      The first search above was done without LOCK_delayed_create.
1571
1632
      }
1572
1633
 
1573
1634
      /* Wait until table is open */
1574
 
      thd->proc_info="waiting for handler open";
 
1635
      thd_proc_info(thd, "waiting for handler open");
1575
1636
      while (!tmp->thd.killed && !tmp->table && !thd->killed)
1576
1637
      {
1577
1638
        pthread_cond_wait(&tmp->cond_client,&tmp->mutex);
1578
1639
      }
1579
1640
      pthread_mutex_unlock(&tmp->mutex);
1580
 
      thd->proc_info="got old table";
 
1641
      thd_proc_info(thd, "got old table");
1581
1642
      if (tmp->thd.killed)
1582
1643
      {
1583
1644
        if (tmp->thd.is_fatal_error)
1637
1698
  tables_in_use++;
1638
1699
  if (!thd.lock)                                // Table is not locked
1639
1700
  {
1640
 
    client_thd->proc_info="waiting for handler lock";
 
1701
    thd_proc_info(client_thd, "waiting for handler lock");
1641
1702
    pthread_cond_signal(&cond);                 // Tell handler to lock table
1642
1703
    while (!dead && !thd.lock && ! client_thd->killed)
1643
1704
    {
1644
1705
      pthread_cond_wait(&cond_client,&mutex);
1645
1706
    }
1646
 
    client_thd->proc_info="got handler lock";
 
1707
    thd_proc_info(client_thd, "got handler lock");
1647
1708
    if (client_thd->killed)
1648
1709
      goto error;
1649
1710
    if (dead)
1661
1722
    bytes. Since the table copy is used for creating one record only,
1662
1723
    the other record buffers and alignment are unnecessary.
1663
1724
  */
1664
 
  client_thd->proc_info="allocating local table";
 
1725
  thd_proc_info(client_thd, "allocating local table");
1665
1726
  copy= (TABLE*) client_thd->alloc(sizeof(*copy)+
1666
1727
                                   (table->s->fields+1)*sizeof(Field**)+
1667
1728
                                   table->s->reclength);
1741
1802
  delayed_insert *di=thd->di;
1742
1803
  DBUG_ENTER("write_delayed");
1743
1804
 
1744
 
  thd->proc_info="waiting for handler insert";
 
1805
  thd_proc_info(thd, "waiting for handler insert");
1745
1806
  pthread_mutex_lock(&di->mutex);
1746
1807
  while (di->stacked_inserts >= delayed_queue_size && !thd->killed)
1747
1808
    pthread_cond_wait(&di->cond_client,&di->mutex);
1748
 
  thd->proc_info="storing row into queue";
 
1809
  thd_proc_info(thd, "storing row into queue");
1749
1810
 
1750
1811
  if (thd->killed || !(row= new delayed_row(duplic, ignore, log_on)))
1751
1812
    goto err;
1954
2015
      /* Information for pthread_kill */
1955
2016
      di->thd.mysys_var->current_mutex= &di->mutex;
1956
2017
      di->thd.mysys_var->current_cond= &di->cond;
1957
 
      di->thd.proc_info="Waiting for INSERT";
 
2018
      thd_proc_info(&(di->thd), "Waiting for INSERT");
1958
2019
 
1959
2020
      DBUG_PRINT("info",("Waiting for someone to insert rows"));
1960
2021
      while (!thd->killed)
1989
2050
      pthread_mutex_unlock(&di->thd.mysys_var->mutex);
1990
2051
      pthread_mutex_lock(&di->mutex);
1991
2052
    }
1992
 
    di->thd.proc_info=0;
 
2053
    thd_proc_info(&(di->thd), 0);
1993
2054
 
1994
2055
    if (di->tables_in_use && ! thd->lock)
1995
2056
    {
2108
2169
 
2109
2170
  table->next_number_field=table->found_next_number_field;
2110
2171
 
2111
 
  thd.proc_info="upgrading lock";
 
2172
  thd_proc_info(&thd, "upgrading lock");
2112
2173
  if (thr_upgrade_write_delay_lock(*thd.lock->locks))
2113
2174
  {
2114
2175
    /* This can only happen if thread is killed by shutdown */
2116
2177
    goto err;
2117
2178
  }
2118
2179
 
2119
 
  thd.proc_info="insert";
 
2180
  thd_proc_info(&thd, "insert");
2120
2181
  max_rows= delayed_insert_limit;
2121
2182
  if (thd.killed || table->s->version != refresh_version)
2122
2183
  {
2234
2295
      {
2235
2296
        if (tables_in_use)
2236
2297
          pthread_cond_broadcast(&cond_client); // If waiting clients
2237
 
        thd.proc_info="reschedule";
 
2298
        thd_proc_info(&thd, "reschedule");
2238
2299
        pthread_mutex_unlock(&mutex);
2239
2300
        if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
2240
2301
        {
2253
2314
        if (!using_bin_log)
2254
2315
          table->file->extra(HA_EXTRA_WRITE_CACHE);
2255
2316
        pthread_mutex_lock(&mutex);
2256
 
        thd.proc_info="insert";
 
2317
        thd_proc_info(&thd, "insert");
2257
2318
      }
2258
2319
      if (tables_in_use)
2259
2320
        pthread_cond_broadcast(&cond_client);   // If waiting clients
2260
2321
    }
2261
2322
  }
2262
2323
 
2263
 
  thd.proc_info=0;
 
2324
  thd_proc_info(&thd, 0);
2264
2325
  table->next_number_field=0;
2265
2326
  pthread_mutex_unlock(&mutex);
2266
2327
  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
2324
2385
                           lex->query_tables->table, lex->field_list, 0,
2325
2386
                           lex->update_list, lex->value_list,
2326
2387
                           lex->duplicates,
2327
 
                           &select_lex->where, TRUE))
 
2388
                           &select_lex->where, TRUE, FALSE, FALSE))
2328
2389
    DBUG_RETURN(TRUE);
2329
2390
 
2330
2391
  /*
2386
2447
                           !insert_into_view, &map) ||
2387
2448
       setup_fields(thd, 0, values, 0, 0, 0);
2388
2449
 
2389
 
  if (info.handle_duplicates == DUP_UPDATE)
 
2450
  if (!res && fields->elements)
 
2451
  {
 
2452
    bool saved_abort_on_warning= thd->abort_on_warning;
 
2453
    thd->abort_on_warning= !info.ignore && (thd->variables.sql_mode &
 
2454
                                            (MODE_STRICT_TRANS_TABLES |
 
2455
                                             MODE_STRICT_ALL_TABLES));
 
2456
    res= check_that_all_fields_are_given_values(thd, table_list->table, 
 
2457
                                                table_list);
 
2458
    thd->abort_on_warning= saved_abort_on_warning;
 
2459
  }
 
2460
 
 
2461
  if (info.handle_duplicates == DUP_UPDATE && !res)
2390
2462
  {
2391
2463
    Name_resolution_context *context= &lex->select_lex.context;
2392
2464
    Name_resolution_context_state ctx_state;
2497
2569
                          (thd->variables.sql_mode &
2498
2570
                           (MODE_STRICT_TRANS_TABLES |
2499
2571
                            MODE_STRICT_ALL_TABLES)));
2500
 
  res= ((fields->elements &&
2501
 
         check_that_all_fields_are_given_values(thd, table, table_list)) ||
2502
 
        table_list->prepare_where(thd, 0, TRUE) ||
 
2572
  res= (table_list->prepare_where(thd, 0, TRUE) ||
2503
2573
        table_list->prepare_check_option(thd));
2504
2574
 
2505
2575
  if (!res)
2506
 
    mark_fields_used_by_triggers_for_insert_stmt(thd, table,
2507
 
                                                 info.handle_duplicates);
 
2576
    prepare_triggers_for_insert_stmt(thd, table,
 
2577
                                     info.handle_duplicates);
2508
2578
  DBUG_RETURN(res);
2509
2579
}
2510
2580
 
2547
2617
  if (table)
2548
2618
  {
2549
2619
    table->next_number_field=0;
 
2620
    table->auto_increment_field_not_null= FALSE;
2550
2621
    table->file->reset();
2551
2622
  }
2552
2623
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;