~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Brian Aker
  • Date: 2009-11-11 21:40:59 UTC
  • mfrom: (1208.3.3 reference)
  • mto: This revision was merged to the branch mainline in revision 1212.
  • Revision ID: brian@gaz-20091111214059-0xfxkwrrixpx2jd9
Update for cursor name change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
299
299
  ctx_state.restore_state(context, table_list);
300
300
 
301
301
  /*
302
 
    Fill in the given fields and dump it to the table file
 
302
    Fill in the given fields and dump it to the table cursor
303
303
  */
304
304
  memset(&info, 0, sizeof(info));
305
305
  info.ignore= ignore;
322
322
  error=0;
323
323
  session->set_proc_info("update");
324
324
  if (duplic == DUP_REPLACE)
325
 
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
 
325
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
326
326
  if (duplic == DUP_UPDATE)
327
 
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
 
327
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
328
328
  {
329
329
    if (duplic != DUP_ERROR || ignore)
330
 
      table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
331
 
    table->file->ha_start_bulk_insert(values_list.elements);
 
330
      table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
331
    table->cursor->ha_start_bulk_insert(values_list.elements);
332
332
  }
333
333
 
334
334
 
403
403
      Do not do this release if this is a delayed insert, it would steal
404
404
      auto_inc values from the delayed_insert thread as they share Table.
405
405
    */
406
 
    table->file->ha_release_auto_increment();
407
 
    if (table->file->ha_end_bulk_insert() && !error)
 
406
    table->cursor->ha_release_auto_increment();
 
407
    if (table->cursor->ha_end_bulk_insert() && !error)
408
408
    {
409
 
      table->file->print_error(my_errno,MYF(0));
 
409
      table->cursor->print_error(my_errno,MYF(0));
410
410
      error=1;
411
411
    }
412
412
    if (duplic != DUP_ERROR || ignore)
413
 
      table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
413
      table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
414
414
 
415
 
    transactional_table= table->file->has_transactions();
 
415
    transactional_table= table->cursor->has_transactions();
416
416
 
417
417
    changed= (info.copied || info.deleted || info.updated);
418
418
    if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table)
444
444
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
445
445
  table->auto_increment_field_not_null= false;
446
446
  if (duplic == DUP_REPLACE)
447
 
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
447
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
448
448
 
449
449
  if (error)
450
450
    goto abort;
474
474
 
475
475
abort:
476
476
  if (table != NULL)
477
 
    table->file->ha_release_auto_increment();
 
477
    table->cursor->ha_release_auto_increment();
478
478
  if (!joins_freed)
479
479
    free_underlaid_joins(session, &session->lex->select_lex);
480
480
  session->abort_on_warning= 0;
712
712
  int error;
713
713
  char *key=0;
714
714
  MyBitmap *save_read_set, *save_write_set;
715
 
  uint64_t prev_insert_id= table->file->next_insert_id;
 
715
  uint64_t prev_insert_id= table->cursor->next_insert_id;
716
716
  uint64_t insert_id_for_cur_row= 0;
717
717
 
718
718
 
722
722
 
723
723
  if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
724
724
  {
725
 
    while ((error=table->file->ha_write_row(table->record[0])))
 
725
    while ((error=table->cursor->ha_write_row(table->record[0])))
726
726
    {
727
727
      uint32_t key_nr;
728
728
      /*
732
732
        the autogenerated value to avoid session->insert_id_for_cur_row to become
733
733
        0.
734
734
      */
735
 
      if (table->file->insert_id_for_cur_row > 0)
736
 
        insert_id_for_cur_row= table->file->insert_id_for_cur_row;
 
735
      if (table->cursor->insert_id_for_cur_row > 0)
 
736
        insert_id_for_cur_row= table->cursor->insert_id_for_cur_row;
737
737
      else
738
 
        table->file->insert_id_for_cur_row= insert_id_for_cur_row;
 
738
        table->cursor->insert_id_for_cur_row= insert_id_for_cur_row;
739
739
      bool is_duplicate_key_error;
740
 
      if (table->file->is_fatal_error(error, HA_CHECK_DUP))
 
740
      if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
741
741
        goto err;
742
 
      is_duplicate_key_error= table->file->is_fatal_error(error, 0);
 
742
      is_duplicate_key_error= table->cursor->is_fatal_error(error, 0);
743
743
      if (!is_duplicate_key_error)
744
744
      {
745
745
        /*
751
751
          goto gok_or_after_err; /* Ignoring a not fatal error, return 0 */
752
752
        goto err;
753
753
      }
754
 
      if ((int) (key_nr = table->file->get_dup_key(error)) < 0)
 
754
      if ((int) (key_nr = table->cursor->get_dup_key(error)) < 0)
755
755
      {
756
756
        error= HA_ERR_FOUND_DUPP_KEY;         /* Database can't find key */
757
757
        goto err;
768
768
          key_nr == table->s->next_number_index &&
769
769
          (insert_id_for_cur_row > 0))
770
770
        goto err;
771
 
      if (table->file->ha_table_flags() & HA_DUPLICATE_POS)
 
771
      if (table->cursor->ha_table_flags() & HA_DUPLICATE_POS)
772
772
      {
773
 
        if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
773
        if (table->cursor->rnd_pos(table->record[1],table->cursor->dup_ref))
774
774
          goto err;
775
775
      }
776
776
      else
777
777
      {
778
 
        if (table->file->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
 
778
        if (table->cursor->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
779
779
        {
780
780
          error=my_errno;
781
781
          goto err;
790
790
          }
791
791
        }
792
792
        key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
793
 
        if ((error=(table->file->index_read_idx_map(table->record[1],key_nr,
 
793
        if ((error=(table->cursor->index_read_idx_map(table->record[1],key_nr,
794
794
                                                    (unsigned char*) key, HA_WHOLE_KEY,
795
795
                                                    HA_READ_KEY_EXACT))))
796
796
          goto err;
812
812
                                                 info->ignore))
813
813
          goto before_err;
814
814
 
815
 
        table->file->restore_auto_increment(prev_insert_id);
 
815
        table->cursor->restore_auto_increment(prev_insert_id);
816
816
        if (table->next_number_field)
817
 
          table->file->adjust_next_insert_id_after_explicit_value(
 
817
          table->cursor->adjust_next_insert_id_after_explicit_value(
818
818
            table->next_number_field->val_int());
819
819
        info->touched++;
820
 
        if ((table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ &&
 
820
        if ((table->cursor->ha_table_flags() & HA_PARTIAL_COLUMN_READ &&
821
821
             !bitmap_is_subset(table->write_set, table->read_set)) ||
822
822
            table->compare_record())
823
823
        {
824
 
          if ((error=table->file->ha_update_row(table->record[1],
 
824
          if ((error=table->cursor->ha_update_row(table->record[1],
825
825
                                                table->record[0])) &&
826
826
              error != HA_ERR_RECORD_IS_THE_SAME)
827
827
          {
828
828
            if (info->ignore &&
829
 
                !table->file->is_fatal_error(error, HA_CHECK_DUP_KEY))
 
829
                !table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
830
830
            {
831
831
              goto gok_or_after_err;
832
832
            }
844
844
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
845
845
            handled separately by Session::arg_of_last_insert_id_function.
846
846
          */
847
 
          insert_id_for_cur_row= table->file->insert_id_for_cur_row= 0;
 
847
          insert_id_for_cur_row= table->cursor->insert_id_for_cur_row= 0;
848
848
          info->copied++;
849
849
        }
850
850
 
851
851
        if (table->next_number_field)
852
 
          table->file->adjust_next_insert_id_after_explicit_value(
 
852
          table->cursor->adjust_next_insert_id_after_explicit_value(
853
853
            table->next_number_field->val_int());
854
854
        info->touched++;
855
855
 
872
872
          ON UPDATE triggers.
873
873
        */
874
874
        if (last_uniq_key(table,key_nr) &&
875
 
            !table->file->referenced_by_foreign_key() &&
 
875
            !table->cursor->referenced_by_foreign_key() &&
876
876
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
877
877
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
878
878
        {
879
 
          if ((error=table->file->ha_update_row(table->record[1],
 
879
          if ((error=table->cursor->ha_update_row(table->record[1],
880
880
                                                table->record[0])) &&
881
881
              error != HA_ERR_RECORD_IS_THE_SAME)
882
882
            goto err;
884
884
            info->deleted++;
885
885
          else
886
886
            error= 0;
887
 
          session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
887
          session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
888
888
          /*
889
889
            Since we pretend that we have done insert we should call
890
890
            its after triggers.
893
893
        }
894
894
        else
895
895
        {
896
 
          if ((error=table->file->ha_delete_row(table->record[1])))
 
896
          if ((error=table->cursor->ha_delete_row(table->record[1])))
897
897
            goto err;
898
898
          info->deleted++;
899
 
          if (!table->file->has_transactions())
 
899
          if (!table->cursor->has_transactions())
900
900
            session->transaction.stmt.modified_non_trans_table= true;
901
901
          /* Let us attempt do write_row() once more */
902
902
        }
903
903
      }
904
904
    }
905
 
    session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
905
    session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
906
906
    /*
907
907
      Restore column maps if they where replaced during an duplicate key
908
908
      problem.
911
911
        table->write_set != save_write_set)
912
912
      table->column_bitmaps_set(save_read_set, save_write_set);
913
913
  }
914
 
  else if ((error=table->file->ha_write_row(table->record[0])))
 
914
  else if ((error=table->cursor->ha_write_row(table->record[0])))
915
915
  {
916
916
    if (!info->ignore ||
917
 
        table->file->is_fatal_error(error, HA_CHECK_DUP))
 
917
        table->cursor->is_fatal_error(error, HA_CHECK_DUP))
918
918
      goto err;
919
 
    table->file->restore_auto_increment(prev_insert_id);
 
919
    table->cursor->restore_auto_increment(prev_insert_id);
920
920
    goto gok_or_after_err;
921
921
  }
922
922
 
923
923
after_n_copied_inc:
924
924
  info->copied++;
925
 
  session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
925
  session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
926
926
 
927
927
gok_or_after_err:
928
928
  if (key)
929
929
    free(key);
930
 
  if (!table->file->has_transactions())
 
930
  if (!table->cursor->has_transactions())
931
931
    session->transaction.stmt.modified_non_trans_table= true;
932
932
  return(0);
933
933
 
936
936
  /* current_select is NULL if this is a delayed insert */
937
937
  if (session->lex->current_select)
938
938
    session->lex->current_select->no_error= 0;        // Give error
939
 
  table->file->print_error(error,MYF(0));
 
939
  table->cursor->print_error(error,MYF(0));
940
940
 
941
941
before_err:
942
 
  table->file->restore_auto_increment(prev_insert_id);
 
942
  table->cursor->restore_auto_increment(prev_insert_id);
943
943
  if (key)
944
944
    free(key);
945
945
  table->column_bitmaps_set(save_read_set, save_write_set);
1171
1171
      We won't start bulk inserts at all if this statement uses functions or
1172
1172
      should invoke triggers since they may access to the same table too.
1173
1173
    */
1174
 
    table->file->ha_start_bulk_insert((ha_rows) 0);
 
1174
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
1175
1175
  }
1176
1176
  table->restoreRecordAsDefault();              // Get empty record
1177
1177
  table->next_number_field=table->found_next_number_field;
1178
1178
 
1179
1179
  session->cuted_fields=0;
1180
1180
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1181
 
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
1181
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1182
1182
  if (info.handle_duplicates == DUP_REPLACE)
1183
 
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
 
1183
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1184
1184
  if (info.handle_duplicates == DUP_UPDATE)
1185
 
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
 
1185
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1186
1186
  session->abort_on_warning= !info.ignore;
1187
1187
  table->mark_columns_needed_for_insert();
1188
1188
 
1211
1211
{
1212
1212
 
1213
1213
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1214
 
    table->file->ha_start_bulk_insert((ha_rows) 0);
 
1214
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
1215
1215
  return(0);
1216
1216
}
1217
1217
 
1229
1229
  {
1230
1230
    table->next_number_field=0;
1231
1231
    table->auto_increment_field_not_null= false;
1232
 
    table->file->ha_reset();
 
1232
    table->cursor->ha_reset();
1233
1233
  }
1234
1234
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1235
1235
  session->abort_on_warning= 0;
1314
1314
bool select_insert::send_eof()
1315
1315
{
1316
1316
  int error;
1317
 
  bool const trans_table= table->file->has_transactions();
 
1317
  bool const trans_table= table->cursor->has_transactions();
1318
1318
  uint64_t id;
1319
1319
  bool changed;
1320
1320
 
1321
 
  error= table->file->ha_end_bulk_insert();
1322
 
  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1323
 
  table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
1321
  error= table->cursor->ha_end_bulk_insert();
 
1322
  table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
1323
  table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1324
1324
 
1325
1325
  if ((changed= (info.copied || info.deleted || info.updated)))
1326
1326
  {
1334
1334
  assert(trans_table || !changed ||
1335
1335
              session->transaction.stmt.modified_non_trans_table);
1336
1336
 
1337
 
  table->file->ha_release_auto_increment();
 
1337
  table->cursor->ha_release_auto_increment();
1338
1338
 
1339
1339
  if (error)
1340
1340
  {
1341
 
    table->file->print_error(error,MYF(0));
 
1341
    table->cursor->print_error(error,MYF(0));
1342
1342
    DRIZZLE_INSERT_SELECT_DONE(error, 0);
1343
1343
    return 1;
1344
1344
  }
1375
1375
  {
1376
1376
    bool changed, transactional_table;
1377
1377
 
1378
 
    table->file->ha_end_bulk_insert();
 
1378
    table->cursor->ha_end_bulk_insert();
1379
1379
 
1380
1380
    /*
1381
1381
      If at least one row has been inserted/modified and will stay in
1392
1392
      zero, so no check for that is made.
1393
1393
    */
1394
1394
    changed= (info.copied || info.deleted || info.updated);
1395
 
    transactional_table= table->file->has_transactions();
 
1395
    transactional_table= table->cursor->has_transactions();
1396
1396
    assert(transactional_table || !changed ||
1397
1397
                session->transaction.stmt.modified_non_trans_table);
1398
 
    table->file->ha_release_auto_increment();
 
1398
    table->cursor->ha_release_auto_increment();
1399
1399
  }
1400
1400
 
1401
1401
  if (DRIZZLE_INSERT_SELECT_DONE_ENABLED())
1668
1668
  table->restoreRecordAsDefault();      // Get empty record
1669
1669
  session->cuted_fields=0;
1670
1670
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1671
 
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
1671
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1672
1672
  if (info.handle_duplicates == DUP_REPLACE)
1673
 
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
 
1673
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1674
1674
  if (info.handle_duplicates == DUP_UPDATE)
1675
 
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1676
 
  table->file->ha_start_bulk_insert((ha_rows) 0);
 
1675
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
 
1676
  table->cursor->ha_start_bulk_insert((ha_rows) 0);
1677
1677
  session->abort_on_warning= !info.ignore;
1678
1678
  if (check_that_all_fields_are_given_values(session, table, table_list))
1679
1679
    return(1);
1680
1680
  table->mark_columns_needed_for_insert();
1681
 
  table->file->extra(HA_EXTRA_WRITE_CACHE);
 
1681
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1682
1682
  return(0);
1683
1683
}
1684
1684
 
1727
1727
      (void) session->endActiveTransaction();
1728
1728
    }
1729
1729
 
1730
 
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1731
 
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
1730
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
1731
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1732
1732
    if (m_plock)
1733
1733
    {
1734
1734
      mysql_unlock_tables(session, *m_plock);
1772
1772
 
1773
1773
  if (table)
1774
1774
  {
1775
 
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1776
 
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
1775
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
1776
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1777
1777
    if (!create_info->table_existed)
1778
1778
      session->drop_open_table(table, create_table->db, create_table->table_name);
1779
1779
    table= NULL;                                    // Safety