~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_update.cc

Resolving conflicts for a few files regarding FALSE=>false

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 |
1037
1025
  }
1038
1026
  delete result;
1039
1027
  thd->abort_on_warning= 0;
1040
 
  DBUG_RETURN(false);
 
1028
  return(false);
1041
1029
}
1042
1030
 
1043
1031
 
1069
1057
  List_iterator_fast<Item> value_it(*values);
1070
1058
  uint i, max_fields;
1071
1059
  uint leaf_table_count= 0;
1072
 
  DBUG_ENTER("multi_update::prepare");
1073
 
 
 
1060
  
1074
1061
  thd->count_cuted_fields= CHECK_FIELD_WARN;
1075
1062
  thd->cuted_fields=0L;
1076
1063
  thd_proc_info(thd, "updating main table");
1080
1067
  if (!tables_to_update)
1081
1068
  {
1082
1069
    my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
1083
 
    DBUG_RETURN(1);
 
1070
    return(1);
1084
1071
  }
1085
1072
 
1086
1073
  /*
1089
1076
  */
1090
1077
 
1091
1078
  if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
1092
 
    DBUG_RETURN(1);
 
1079
    return(1);
1093
1080
 
1094
1081
  /*
1095
1082
    Save tables beeing updated in update_tables
1108
1095
      TABLE_LIST *tl= (TABLE_LIST*) thd->memdup((char*) table_ref,
1109
1096
                                                sizeof(*tl));
1110
1097
      if (!tl)
1111
 
        DBUG_RETURN(1);
 
1098
        return(1);
1112
1099
      update.link_in_list((uchar*) tl, (uchar**) &tl->next_local);
1113
1100
      tl->shared= table_count++;
1114
1101
      table->no_keyread=1;
1129
1116
  values_for_table= (List_item **) thd->alloc(sizeof(List_item *) *
1130
1117
                                              table_count);
1131
1118
  if (thd->is_fatal_error)
1132
 
    DBUG_RETURN(1);
 
1119
    return(1);
1133
1120
  for (i=0 ; i < table_count ; i++)
1134
1121
  {
1135
1122
    fields_for_table[i]= new List_item;
1136
1123
    values_for_table[i]= new List_item;
1137
1124
  }
1138
1125
  if (thd->is_fatal_error)
1139
 
    DBUG_RETURN(1);
 
1126
    return(1);
1140
1127
 
1141
1128
  /* Split fields into fields_for_table[] and values_by_table[] */
1142
1129
 
1148
1135
    values_for_table[offset]->push_back(value);
1149
1136
  }
1150
1137
  if (thd->is_fatal_error)
1151
 
    DBUG_RETURN(1);
 
1138
    return(1);
1152
1139
 
1153
1140
  /* Allocate copy fields */
1154
1141
  max_fields=0;
1155
1142
  for (i=0 ; i < table_count ; i++)
1156
1143
    set_if_bigger(max_fields, fields_for_table[i]->elements + leaf_table_count);
1157
1144
  copy_field= new Copy_field[max_fields];
1158
 
  DBUG_RETURN(thd->is_fatal_error != 0);
 
1145
  return(thd->is_fatal_error != 0);
1159
1146
}
1160
1147
 
1161
1148
 
1236
1223
multi_update::initialize_tables(JOIN *join)
1237
1224
{
1238
1225
  TABLE_LIST *table_ref;
1239
 
  DBUG_ENTER("initialize_tables");
1240
 
 
 
1226
  
1241
1227
  if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
1242
 
    DBUG_RETURN(1);
 
1228
    return(1);
1243
1229
  main_table=join->join_tab->table;
1244
1230
  table_to_update= 0;
1245
1231
 
1246
1232
  /* Any update has at least one pair (field, value) */
1247
 
  DBUG_ASSERT(fields->elements);
 
1233
  assert(fields->elements);
1248
1234
 
1249
1235
  /* Create a temporary table for keys to all tables, except main table */
1250
1236
  for (table_ref= update_tables; table_ref; table_ref= table_ref->next_local)
1285
1271
      Field_string *field= new Field_string(tbl->file->ref_length, 0,
1286
1272
                                            tbl->alias, &my_charset_bin);
1287
1273
      if (!field)
1288
 
        DBUG_RETURN(1);
 
1274
        return(1);
1289
1275
      field->init(tbl);
1290
1276
      /*
1291
1277
        The field will be converted to varstring when creating tmp table if
1294
1280
      field->can_alter_field_type= 0;
1295
1281
      Item_field *ifield= new Item_field((Field *) field);
1296
1282
      if (!ifield)
1297
 
         DBUG_RETURN(1);
 
1283
         return(1);
1298
1284
      ifield->maybe_null= 0;
1299
1285
      if (temp_fields.push_back(ifield))
1300
 
        DBUG_RETURN(1);
 
1286
        return(1);
1301
1287
    } while ((tbl= tbl_it++));
1302
1288
 
1303
1289
    temp_fields.concat(fields_for_table[cnt]);
1318
1304
                                           TMP_TABLE_ALL_COLUMNS,
1319
1305
                                           HA_POS_ERROR,
1320
1306
                                           (char *) "")))
1321
 
      DBUG_RETURN(1);
 
1307
      return(1);
1322
1308
    tmp_tables[cnt]->file->extra(HA_EXTRA_WRITE_CACHE);
1323
1309
  }
1324
 
  DBUG_RETURN(0);
 
1310
  return(0);
1325
1311
}
1326
1312
 
1327
1313
 
1349
1335
  if (copy_field)
1350
1336
    delete [] copy_field;
1351
1337
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;          // Restore this setting
1352
 
  DBUG_ASSERT(trans_safe || !updated || 
 
1338
  assert(trans_safe || !updated || 
1353
1339
              thd->transaction.all.modified_non_trans_table);
1354
1340
}
1355
1341
 
1357
1343
bool multi_update::send_data(List<Item> &not_used_values)
1358
1344
{
1359
1345
  TABLE_LIST *cur_table;
1360
 
  DBUG_ENTER("multi_update::send_data");
1361
 
 
 
1346
  
1362
1347
  for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
1363
1348
  {
1364
1349
    TABLE *table= cur_table->table;
1394
1379
      store_record(table,record[1]);
1395
1380
      if (fill_record(thd, *fields_for_table[offset],
1396
1381
                      *values_for_table[offset], 0))
1397
 
        DBUG_RETURN(1);
 
1382
        return(1);
1398
1383
 
1399
1384
      found++;
1400
1385
      if (!can_compare_record || compare_record(table))
1428
1413
 
1429
1414
            prepare_record_for_error_message(error, table);
1430
1415
            table->file->print_error(error,MYF(flags));
1431
 
            DBUG_RETURN(1);
 
1416
            return(1);
1432
1417
          }
1433
1418
        }
1434
1419
        else
1485
1470
                                         error, 1))
1486
1471
        {
1487
1472
          do_update=0;
1488
 
          DBUG_RETURN(1);                       // Not a table_is_full error
 
1473
          return(1);                    // Not a table_is_full error
1489
1474
        }
1490
1475
        found++;
1491
1476
      }
1492
1477
    }
1493
1478
  }
1494
 
  DBUG_RETURN(0);
 
1479
  return(0);
1495
1480
}
1496
1481
 
1497
1482
 
1515
1500
 
1516
1501
  if (! trans_safe)
1517
1502
  {
1518
 
    DBUG_ASSERT(thd->transaction.stmt.modified_non_trans_table);
 
1503
    assert(thd->transaction.stmt.modified_non_trans_table);
1519
1504
    if (do_update && table_count > 1)
1520
1505
    {
1521
1506
      /* Add warning here */
1545
1530
    }
1546
1531
    thd->transaction.all.modified_non_trans_table= true;
1547
1532
  }
1548
 
  DBUG_ASSERT(trans_safe || !updated || thd->transaction.stmt.modified_non_trans_table);
 
1533
  assert(trans_safe || !updated || thd->transaction.stmt.modified_non_trans_table);
1549
1534
}
1550
1535
 
1551
1536
 
1556
1541
  ha_rows org_updated;
1557
1542
  TABLE *table, *tmp_table;
1558
1543
  List_iterator_fast<TABLE> check_opt_it(unupdated_check_opt_tables);
1559
 
  DBUG_ENTER("multi_update::do_updates");
1560
 
 
 
1544
  
1561
1545
  do_update= 0;                                 // Don't retry this function
1562
1546
  if (!found)
1563
 
    DBUG_RETURN(0);
 
1547
    return(0);
1564
1548
  for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
1565
1549
  {
1566
1550
    bool can_compare_record;
1674
1658
        tbl->file->ha_rnd_end();
1675
1659
 
1676
1660
  }
1677
 
  DBUG_RETURN(0);
 
1661
  return(0);
1678
1662
 
1679
1663
err:
1680
1664
  {
1698
1682
      thd->transaction.stmt.modified_non_trans_table= true;
1699
1683
    }
1700
1684
  }
1701
 
  DBUG_RETURN(1);
 
1685
  return(1);
1702
1686
}
1703
1687
 
1704
1688
 
1709
1693
  char buff[STRING_BUFFER_USUAL_SIZE];
1710
1694
  ulonglong id;
1711
1695
  THD::killed_state killed_status= THD::NOT_KILLED;
1712
 
  DBUG_ENTER("multi_update::send_eof");
 
1696
  
1713
1697
  thd_proc_info(thd, "updating reference tables");
1714
1698
 
1715
1699
  /* 
1733
1717
    either from the query's list or via a stored routine: bug#13270,23333
1734
1718
  */
1735
1719
 
1736
 
  DBUG_ASSERT(trans_safe || !updated || 
 
1720
  assert(trans_safe || !updated || 
1737
1721
              thd->transaction.stmt.modified_non_trans_table);
1738
1722
  if (local_error == 0 || thd->transaction.stmt.modified_non_trans_table)
1739
1723
  {
1746
1730
                            transactional_tables, false, killed_status) &&
1747
1731
          trans_safe)
1748
1732
      {
1749
 
        local_error= 1;                         // Rollback update
 
1733
        local_error= 1;                         // Rollback update
1750
1734
      }
1751
1735
    }
1752
1736
    if (thd->transaction.stmt.modified_non_trans_table)
1760
1744
    /* Safety: If we haven't got an error before (can happen in do_updates) */
1761
1745
    my_message(ER_UNKNOWN_ERROR, "An error occured in multi-table update",
1762
1746
               MYF(0));
1763
 
    DBUG_RETURN(true);
 
1747
    return(true);
1764
1748
  }
1765
1749
 
1766
1750
  id= thd->arg_of_last_insert_id_function ?
1770
1754
  thd->row_count_func=
1771
1755
    (thd->client_capabilities & CLIENT_FOUND_ROWS) ? found : updated;
1772
1756
  ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
1773
 
  DBUG_RETURN(false);
 
1757
  return(false);
1774
1758
}