~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_update.cc

MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
  uint keynr;
101
101
  MY_BITMAP unique_map; /* Fields in offended unique. */
102
102
  my_bitmap_map unique_map_buf[bitmap_buffer_size(MAX_FIELDS)];
103
 
  DBUG_ENTER("prepare_record_for_error_message");
104
 
 
 
103
  
105
104
  /*
106
105
    Only duplicate key errors print the key value.
107
106
    If storage engine does always read all columns, we have the value alraedy.
108
107
  */
109
108
  if ((error != HA_ERR_FOUND_DUPP_KEY) ||
110
109
      !(table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ))
111
 
    DBUG_VOID_RETURN;
 
110
    return;
112
111
 
113
112
  /*
114
113
    Get the number of the offended index.
115
114
    We will see MAX_KEY if the engine cannot determine the affected index.
116
115
  */
117
116
  if ((keynr= table->file->get_dup_key(error)) >= MAX_KEY)
118
 
    DBUG_VOID_RETURN;
 
117
    return;
119
118
 
120
119
  /* Create unique_map with all fields used by that index. */
121
120
  bitmap_init(&unique_map, unique_map_buf, table->s->fields, false);
131
130
    Otherwise no need to do anything.
132
131
  */
133
132
  if (bitmap_is_clear_all(&unique_map))
134
 
    DBUG_VOID_RETURN;
 
133
    return;
135
134
 
136
135
  /* Get identifier of last read record into table->file->ref. */
137
136
  table->file->position(table->record[0]);
146
145
    if (bitmap_is_set(&unique_map, field->field_index))
147
146
      field->copy_from_tmp(table->s->rec_buff_length);
148
147
 
149
 
  DBUG_VOID_RETURN;
 
148
  return;
150
149
}
151
150
 
152
151
 
198
197
  ulonglong     id;
199
198
  List<Item> all_fields;
200
199
  THD::killed_state killed_status= THD::NOT_KILLED;
201
 
  DBUG_ENTER("mysql_update");
202
 
 
 
200
  
203
201
  for ( ; ; )
204
202
  {
205
203
    if (open_tables(thd, &table_list, &table_count, 0))
206
 
      DBUG_RETURN(1);
 
204
      return(1);
207
205
 
208
206
    if (!lock_tables(thd, table_list, table_count, &need_reopen))
209
207
      break;
210
208
    if (!need_reopen)
211
 
      DBUG_RETURN(1);
 
209
      return(1);
212
210
    close_tables_for_reopen(thd, &table_list);
213
211
  }
214
212
 
215
213
  if (mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
216
214
      (thd->fill_derived_tables() &&
217
215
       mysql_handle_derived(thd->lex, &mysql_derived_filling)))
218
 
    DBUG_RETURN(1);
 
216
    return(1);
219
217
 
220
218
  MYSQL_UPDATE_START();
221
219
  thd_proc_info(thd, "init");
257
255
    fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array))
258
256
  {
259
257
    MYSQL_UPDATE_END();
260
 
    DBUG_RETURN(-1);
 
258
    return(-1);
261
259
  }
262
260
 
263
261
  if (conds)
294
292
      goto abort;                               // Error in where
295
293
    MYSQL_UPDATE_END();
296
294
    my_ok(thd);                         // No matching records
297
 
    DBUG_RETURN(0);
 
295
    return(0);
298
296
  }
299
297
  if (!select && limit != HA_POS_ERROR)
300
298
  {
640
638
    The cached value can not change whereas the killed status can
641
639
    (externally) since this point and change of the latter won't affect
642
640
    binlogging.
643
 
    It's assumed that if an error was set in combination with an effective 
 
641
    It's assumed that if an error was set in combination with an effective
644
642
    killed status then the error is due to killing.
645
643
  */
646
 
  killed_status= thd->killed; // get the status of the volatile 
 
644
  killed_status= thd->killed; // get the status of the volatile
647
645
  // simulated killing after the loop must be ineffective for binlogging
648
 
  DBUG_EXECUTE_IF("simulate_kill_bug27571",
649
 
                  {
650
 
                    thd->killed= THD::KILL_QUERY;
651
 
                  };);
652
646
  error= (killed_status == THD::NOT_KILLED)?  error : 1;
653
 
  
 
647
 
654
648
  if (error &&
655
649
      will_batch &&
656
650
      (loc_error= table->file->exec_bulk_update(&dup_key_found)))
688
682
    last one without error. error > 0 means an error (e.g. unique key
689
683
    violation and no IGNORE or REPLACE). error == 0 is also an error (if
690
684
    preparing the record or invoking before triggers fails). See
691
 
    ha_autocommit_or_rollback(error>=0) and DBUG_RETURN(error>=0) below.
 
685
    ha_autocommit_or_rollback(error>=0) and return(error>=0) below.
692
686
    Sometimes we want to binlog even if we updated no rows, in case user used
693
687
    it to be sure master and slave are in same state.
694
688
  */
709
703
    if (thd->transaction.stmt.modified_non_trans_table)
710
704
      thd->transaction.all.modified_non_trans_table= true;
711
705
  }
712
 
  DBUG_ASSERT(transactional_table || !updated || thd->transaction.stmt.modified_non_trans_table);
 
706
  assert(transactional_table || !updated || thd->transaction.stmt.modified_non_trans_table);
713
707
  free_underlaid_joins(thd, select_lex);
714
708
 
715
709
  /* If LAST_INSERT_ID(X) was used, report X */
725
719
    thd->row_count_func=
726
720
      (thd->client_capabilities & CLIENT_FOUND_ROWS) ? found : updated;
727
721
    my_ok(thd, (ulong) thd->row_count_func, id, buff);
728
 
    DBUG_PRINT("info",("%ld records updated", (long) updated));
729
722
  }
730
723
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;          /* calc cuted fields */
731
724
  thd->abort_on_warning= 0;
732
 
  DBUG_RETURN((error >= 0 || thd->is_error()) ? 1 : 0);
 
725
  return((error >= 0 || thd->is_error()) ? 1 : 0);
733
726
 
734
727
err:
735
728
  delete select;
743
736
 
744
737
abort:
745
738
  MYSQL_UPDATE_END();
746
 
  DBUG_RETURN(1);
 
739
  return(1);
747
740
}
748
741
 
749
742
/*
767
760
  Item *fake_conds= 0;
768
761
  List<Item> all_fields;
769
762
  SELECT_LEX *select_lex= &thd->lex->select_lex;
770
 
  DBUG_ENTER("mysql_prepare_update");
771
 
 
 
763
  
772
764
  /*
773
765
    Statement-based replication of UPDATE ... LIMIT is not safe as order of
774
766
    rows is not defined, so in mixed mode we go to row-based.
794
786
      select_lex->setup_ref_array(thd, order_num) ||
795
787
      setup_order(thd, select_lex->ref_pointer_array,
796
788
                  table_list, all_fields, all_fields, order))
797
 
    DBUG_RETURN(true);
 
789
    return(true);
798
790
 
799
791
  /* Check that we are not using table that we are updating in a sub select */
800
792
  {
803
795
    {
804
796
      update_non_unique_table_error(table_list, "UPDATE", duplicate);
805
797
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name);
806
 
      DBUG_RETURN(true);
 
798
      return(true);
807
799
    }
808
800
  }
809
801
  select_lex->fix_prepare_information(thd, conds, &fake_conds);
810
 
  DBUG_RETURN(false);
 
802
  return(false);
811
803
}
812
804
 
813
805
 
827
819
 
828
820
  while ((item= (Item_field *) item_it++)) 
829
821
    map|= item->used_tables();
830
 
  DBUG_PRINT("info", ("table_map: 0x%08lx", (long) map));
831
822
  return map;
832
823
}
833
824
 
861
852
  const bool using_lock_tables= thd->locked_tables != 0;
862
853
  bool original_multiupdate= (thd->lex->sql_command == SQLCOM_UPDATE_MULTI);
863
854
  bool need_reopen= false;
864
 
  DBUG_ENTER("mysql_multi_update_prepare");
 
855
  
865
856
 
866
857
  /* following need for prepared statements, to run next time multi-update */
867
858
  thd->lex->sql_command= SQLCOM_UPDATE_MULTI;
872
863
  if (((original_multiupdate || need_reopen) &&
873
864
       open_tables(thd, &table_list, &table_count, 0)) ||
874
865
      mysql_handle_derived(lex, &mysql_derived_prepare))
875
 
    DBUG_RETURN(true);
 
866
    return(true);
876
867
  /*
877
868
    setup_tables() need for VIEWs. JOIN::prepare() will call setup_tables()
878
869
    second time, but this call will do nothing (there are check for second
883
874
                                    &lex->select_lex.top_join_list,
884
875
                                    table_list,
885
876
                                    &lex->select_lex.leaf_tables, false))
886
 
    DBUG_RETURN(true);
 
877
    return(true);
887
878
 
888
879
  if (setup_fields_with_no_wrap(thd, 0, *fields, MARK_COLUMNS_WRITE, 0, 0))
889
 
    DBUG_RETURN(true);
 
880
    return(true);
890
881
 
891
882
  if (update_view && check_fields(thd, *fields))
892
883
  {
893
 
    DBUG_RETURN(true);
 
884
    return(true);
894
885
  }
895
886
 
896
887
  tables_for_update= get_table_map(fields);
912
903
    if (table->map & tables_for_update)
913
904
    {
914
905
      table->mark_columns_needed_for_update();
915
 
      DBUG_PRINT("info",("setting table `%s` for update", tl->alias));
916
906
      /*
917
907
        If table will be updated we should not downgrade lock for it and
918
908
        leave it as is.
920
910
    }
921
911
    else
922
912
    {
923
 
      DBUG_PRINT("info",("setting table `%s` for read-only", tl->alias));
924
913
      /*
925
914
        If we are using the binary log, we need TL_READ_NO_INSERT to get
926
915
        correct order of statements. Otherwise, we use a TL_READ lock to
938
927
  if (lock_tables(thd, table_list, table_count, &need_reopen))
939
928
  {
940
929
    if (!need_reopen)
941
 
      DBUG_RETURN(true);
 
930
      return(true);
942
931
 
943
932
    /*
944
933
      We have to reopen tables since some of them were altered or dropped
974
963
      if ((duplicate= unique_table(thd, tl, table_list, 0)))
975
964
      {
976
965
        update_non_unique_table_error(table_list, "UPDATE", duplicate);
977
 
        DBUG_RETURN(true);
 
966
        return(true);
978
967
      }
979
968
    }
980
969
  }
986
975
 
987
976
  if (thd->fill_derived_tables() &&
988
977
      mysql_handle_derived(lex, &mysql_derived_filling))
989
 
    DBUG_RETURN(true);
 
978
    return(true);
990
979
 
991
 
  DBUG_RETURN (false);
 
980
  return (false);
992
981
}
993
982
 
994
983
 
1007
996
{
1008
997
  multi_update *result;
1009
998
  bool res;
1010
 
  DBUG_ENTER("mysql_multi_update");
1011
 
 
 
999
  
1012
1000
  if (!(result= new multi_update(table_list,
1013
1001
                                 thd->lex->select_lex.leaf_tables,
1014
1002
                                 fields, values,
1015
1003
                                 handle_duplicates, ignore)))
1016
 
    DBUG_RETURN(true);
 
1004
    return(true);
1017
1005
 
1018
1006
  thd->abort_on_warning= test(thd->variables.sql_mode &
1019
1007
                              (MODE_STRICT_TRANS_TABLES |
1028
1016
                      options | SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1029
1017
                      OPTION_SETUP_TABLES_DONE,
1030
1018
                      result, unit, select_lex);
1031
 
  DBUG_PRINT("info",("res: %d  report_error: %d", res,
1032
 
                     (int) thd->is_error()));
1033
1019
  res|= thd->is_error();
1034
1020
  if (unlikely(res))
1035
1021
  {
1039
1025
  }
1040
1026
  delete result;
1041
1027
  thd->abort_on_warning= 0;
1042
 
  DBUG_RETURN(false);
 
1028
  return(false);
1043
1029
}
1044
1030
 
1045
1031
 
1071
1057
  List_iterator_fast<Item> value_it(*values);
1072
1058
  uint i, max_fields;
1073
1059
  uint leaf_table_count= 0;
1074
 
  DBUG_ENTER("multi_update::prepare");
1075
 
 
 
1060
  
1076
1061
  thd->count_cuted_fields= CHECK_FIELD_WARN;
1077
1062
  thd->cuted_fields=0L;
1078
1063
  thd_proc_info(thd, "updating main table");
1082
1067
  if (!tables_to_update)
1083
1068
  {
1084
1069
    my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
1085
 
    DBUG_RETURN(1);
 
1070
    return(1);
1086
1071
  }
1087
1072
 
1088
1073
  /*
1091
1076
  */
1092
1077
 
1093
1078
  if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
1094
 
    DBUG_RETURN(1);
 
1079
    return(1);
1095
1080
 
1096
1081
  /*
1097
1082
    Save tables beeing updated in update_tables
1110
1095
      TABLE_LIST *tl= (TABLE_LIST*) thd->memdup((char*) table_ref,
1111
1096
                                                sizeof(*tl));
1112
1097
      if (!tl)
1113
 
        DBUG_RETURN(1);
 
1098
        return(1);
1114
1099
      update.link_in_list((uchar*) tl, (uchar**) &tl->next_local);
1115
1100
      tl->shared= table_count++;
1116
1101
      table->no_keyread=1;
1131
1116
  values_for_table= (List_item **) thd->alloc(sizeof(List_item *) *
1132
1117
                                              table_count);
1133
1118
  if (thd->is_fatal_error)
1134
 
    DBUG_RETURN(1);
 
1119
    return(1);
1135
1120
  for (i=0 ; i < table_count ; i++)
1136
1121
  {
1137
1122
    fields_for_table[i]= new List_item;
1138
1123
    values_for_table[i]= new List_item;
1139
1124
  }
1140
1125
  if (thd->is_fatal_error)
1141
 
    DBUG_RETURN(1);
 
1126
    return(1);
1142
1127
 
1143
1128
  /* Split fields into fields_for_table[] and values_by_table[] */
1144
1129
 
1150
1135
    values_for_table[offset]->push_back(value);
1151
1136
  }
1152
1137
  if (thd->is_fatal_error)
1153
 
    DBUG_RETURN(1);
 
1138
    return(1);
1154
1139
 
1155
1140
  /* Allocate copy fields */
1156
1141
  max_fields=0;
1157
1142
  for (i=0 ; i < table_count ; i++)
1158
1143
    set_if_bigger(max_fields, fields_for_table[i]->elements + leaf_table_count);
1159
1144
  copy_field= new Copy_field[max_fields];
1160
 
  DBUG_RETURN(thd->is_fatal_error != 0);
 
1145
  return(thd->is_fatal_error != 0);
1161
1146
}
1162
1147
 
1163
1148
 
1238
1223
multi_update::initialize_tables(JOIN *join)
1239
1224
{
1240
1225
  TABLE_LIST *table_ref;
1241
 
  DBUG_ENTER("initialize_tables");
1242
 
 
 
1226
  
1243
1227
  if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
1244
 
    DBUG_RETURN(1);
 
1228
    return(1);
1245
1229
  main_table=join->join_tab->table;
1246
1230
  table_to_update= 0;
1247
1231
 
1248
1232
  /* Any update has at least one pair (field, value) */
1249
 
  DBUG_ASSERT(fields->elements);
 
1233
  assert(fields->elements);
1250
1234
 
1251
1235
  /* Create a temporary table for keys to all tables, except main table */
1252
1236
  for (table_ref= update_tables; table_ref; table_ref= table_ref->next_local)
1287
1271
      Field_string *field= new Field_string(tbl->file->ref_length, 0,
1288
1272
                                            tbl->alias, &my_charset_bin);
1289
1273
      if (!field)
1290
 
        DBUG_RETURN(1);
 
1274
        return(1);
1291
1275
      field->init(tbl);
1292
1276
      /*
1293
1277
        The field will be converted to varstring when creating tmp table if
1296
1280
      field->can_alter_field_type= 0;
1297
1281
      Item_field *ifield= new Item_field((Field *) field);
1298
1282
      if (!ifield)
1299
 
         DBUG_RETURN(1);
 
1283
         return(1);
1300
1284
      ifield->maybe_null= 0;
1301
1285
      if (temp_fields.push_back(ifield))
1302
 
        DBUG_RETURN(1);
 
1286
        return(1);
1303
1287
    } while ((tbl= tbl_it++));
1304
1288
 
1305
1289
    temp_fields.concat(fields_for_table[cnt]);
1320
1304
                                           TMP_TABLE_ALL_COLUMNS,
1321
1305
                                           HA_POS_ERROR,
1322
1306
                                           (char *) "")))
1323
 
      DBUG_RETURN(1);
 
1307
      return(1);
1324
1308
    tmp_tables[cnt]->file->extra(HA_EXTRA_WRITE_CACHE);
1325
1309
  }
1326
 
  DBUG_RETURN(0);
 
1310
  return(0);
1327
1311
}
1328
1312
 
1329
1313
 
1351
1335
  if (copy_field)
1352
1336
    delete [] copy_field;
1353
1337
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;          // Restore this setting
1354
 
  DBUG_ASSERT(trans_safe || !updated || 
 
1338
  assert(trans_safe || !updated || 
1355
1339
              thd->transaction.all.modified_non_trans_table);
1356
1340
}
1357
1341
 
1359
1343
bool multi_update::send_data(List<Item> &not_used_values)
1360
1344
{
1361
1345
  TABLE_LIST *cur_table;
1362
 
  DBUG_ENTER("multi_update::send_data");
1363
 
 
 
1346
  
1364
1347
  for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
1365
1348
  {
1366
1349
    TABLE *table= cur_table->table;
1396
1379
      store_record(table,record[1]);
1397
1380
      if (fill_record(thd, *fields_for_table[offset],
1398
1381
                      *values_for_table[offset], 0))
1399
 
        DBUG_RETURN(1);
 
1382
        return(1);
1400
1383
 
1401
1384
      found++;
1402
1385
      if (!can_compare_record || compare_record(table))
1430
1413
 
1431
1414
            prepare_record_for_error_message(error, table);
1432
1415
            table->file->print_error(error,MYF(flags));
1433
 
            DBUG_RETURN(1);
 
1416
            return(1);
1434
1417
          }
1435
1418
        }
1436
1419
        else
1487
1470
                                         error, 1))
1488
1471
        {
1489
1472
          do_update=0;
1490
 
          DBUG_RETURN(1);                       // Not a table_is_full error
 
1473
          return(1);                    // Not a table_is_full error
1491
1474
        }
1492
1475
        found++;
1493
1476
      }
1494
1477
    }
1495
1478
  }
1496
 
  DBUG_RETURN(0);
 
1479
  return(0);
1497
1480
}
1498
1481
 
1499
1482
 
1517
1500
 
1518
1501
  if (! trans_safe)
1519
1502
  {
1520
 
    DBUG_ASSERT(thd->transaction.stmt.modified_non_trans_table);
 
1503
    assert(thd->transaction.stmt.modified_non_trans_table);
1521
1504
    if (do_update && table_count > 1)
1522
1505
    {
1523
1506
      /* Add warning here */
1547
1530
    }
1548
1531
    thd->transaction.all.modified_non_trans_table= true;
1549
1532
  }
1550
 
  DBUG_ASSERT(trans_safe || !updated || thd->transaction.stmt.modified_non_trans_table);
 
1533
  assert(trans_safe || !updated || thd->transaction.stmt.modified_non_trans_table);
1551
1534
}
1552
1535
 
1553
1536
 
1558
1541
  ha_rows org_updated;
1559
1542
  TABLE *table, *tmp_table;
1560
1543
  List_iterator_fast<TABLE> check_opt_it(unupdated_check_opt_tables);
1561
 
  DBUG_ENTER("multi_update::do_updates");
1562
 
 
 
1544
  
1563
1545
  do_update= 0;                                 // Don't retry this function
1564
1546
  if (!found)
1565
 
    DBUG_RETURN(0);
 
1547
    return(0);
1566
1548
  for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
1567
1549
  {
1568
1550
    bool can_compare_record;
1676
1658
        tbl->file->ha_rnd_end();
1677
1659
 
1678
1660
  }
1679
 
  DBUG_RETURN(0);
 
1661
  return(0);
1680
1662
 
1681
1663
err:
1682
1664
  {
1700
1682
      thd->transaction.stmt.modified_non_trans_table= true;
1701
1683
    }
1702
1684
  }
1703
 
  DBUG_RETURN(1);
 
1685
  return(1);
1704
1686
}
1705
1687
 
1706
1688
 
1711
1693
  char buff[STRING_BUFFER_USUAL_SIZE];
1712
1694
  ulonglong id;
1713
1695
  THD::killed_state killed_status= THD::NOT_KILLED;
1714
 
  DBUG_ENTER("multi_update::send_eof");
 
1696
  
1715
1697
  thd_proc_info(thd, "updating reference tables");
1716
1698
 
1717
1699
  /* 
1735
1717
    either from the query's list or via a stored routine: bug#13270,23333
1736
1718
  */
1737
1719
 
1738
 
  DBUG_ASSERT(trans_safe || !updated || 
 
1720
  assert(trans_safe || !updated || 
1739
1721
              thd->transaction.stmt.modified_non_trans_table);
1740
1722
  if (local_error == 0 || thd->transaction.stmt.modified_non_trans_table)
1741
1723
  {
1748
1730
                            transactional_tables, false, killed_status) &&
1749
1731
          trans_safe)
1750
1732
      {
1751
 
        local_error= 1;                         // Rollback update
 
1733
        local_error= 1;                         // Rollback update
1752
1734
      }
1753
1735
    }
1754
1736
    if (thd->transaction.stmt.modified_non_trans_table)
1762
1744
    /* Safety: If we haven't got an error before (can happen in do_updates) */
1763
1745
    my_message(ER_UNKNOWN_ERROR, "An error occured in multi-table update",
1764
1746
               MYF(0));
1765
 
    DBUG_RETURN(true);
 
1747
    return(true);
1766
1748
  }
1767
1749
 
1768
1750
  id= thd->arg_of_last_insert_id_function ?
1772
1754
  thd->row_count_func=
1773
1755
    (thd->client_capabilities & CLIENT_FOUND_ROWS) ? found : updated;
1774
1756
  ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
1775
 
  DBUG_RETURN(false);
 
1757
  return(false);
1776
1758
}