mi_update.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. /* Update an old row in a MyISAM table */
  17. #include "fulltext.h"
  18. #ifdef __WIN__
  19. #include <errno.h>
  20. #endif
  21. int mi_update(register MI_INFO *info, const byte *oldrec, byte *newrec)
  22. {
  23.   int flag,key_changed,save_errno;
  24.   reg3 my_off_t pos;
  25.   uint i;
  26.   uchar old_key[MI_MAX_KEY_BUFF],*new_key;
  27.   bool auto_key_changed=0;
  28.   ulonglong changed;
  29.   MYISAM_SHARE *share=info->s;
  30.   ha_checksum old_checksum;
  31.   DBUG_ENTER("mi_update");
  32.   LINT_INIT(new_key);
  33.   LINT_INIT(changed);
  34.   LINT_INIT(old_checksum);
  35.   if (!(info->update & HA_STATE_AKTIV))
  36.   {
  37.     DBUG_RETURN(my_errno=HA_ERR_KEY_NOT_FOUND);
  38.   }
  39.   if (share->options & HA_OPTION_READ_ONLY_DATA)
  40.   {
  41.     DBUG_RETURN(my_errno=EACCES);
  42.   }
  43.   if (info->state->key_file_length >= share->base.margin_key_file_length)
  44.   {
  45.     DBUG_RETURN(my_errno=HA_ERR_INDEX_FILE_FULL);
  46.   }
  47.   pos=info->lastpos;
  48.   if (_mi_readinfo(info,F_WRLCK,1))
  49.     DBUG_RETURN(my_errno);
  50.   if (share->calc_checksum)
  51.     old_checksum=info->checksum=(*share->calc_checksum)(info,oldrec);
  52.   if ((*share->compare_record)(info,oldrec))
  53.   {
  54.     save_errno=my_errno;
  55.     goto err_end; /* Record has changed */
  56.   }
  57.   /* Calculate and check all unique constraints */
  58.   key_changed=0;
  59.   for (i=0 ; i < share->state.header.uniques ; i++)
  60.   {
  61.     MI_UNIQUEDEF *def=share->uniqueinfo+i;
  62.     if (mi_unique_comp(def, newrec, oldrec,1) &&
  63. mi_check_unique(info, def, newrec, mi_unique_hash(def, newrec),
  64. info->lastpos))
  65.     {
  66.       save_errno=my_errno;
  67.       goto err_end;
  68.     }
  69.   }
  70.   if (_mi_mark_file_changed(info))
  71.   {
  72.     save_errno=my_errno;
  73.     goto err_end;
  74.   }
  75.   /* Check which keys changed from the original row */
  76.   new_key=info->lastkey2;
  77.   key_changed=HA_STATE_KEY_CHANGED; /* We changed current database */
  78. /* Remove key that didn't change */
  79.   changed=0;
  80.   for (i=0 ; i < share->base.keys ; i++)
  81.   {
  82.     if (((ulonglong) 1 << i) & share->state.key_map)
  83.     {
  84.       /* The following code block is for text searching by SerG */
  85.       if (share->keyinfo[i].flag & HA_FULLTEXT )
  86.       {
  87. if(_mi_ft_cmp(info,i,oldrec, newrec))
  88. {
  89.   if ((int) i == info->lastinx)
  90.     key_changed|=HA_STATE_WRITTEN;
  91.   changed|=((ulonglong) 1 << i);
  92.   if (_mi_ft_del(info,i,(char*) old_key,oldrec,pos))
  93.     goto err;
  94.   if (_mi_ft_add(info,i,(char*) new_key,newrec,pos))
  95.     goto err;
  96. }
  97.       }
  98.       else
  99.       {
  100. uint new_length=_mi_make_key(info,i,new_key,newrec,pos);
  101. uint old_length=_mi_make_key(info,i,old_key,oldrec,pos);
  102. if (new_length != old_length ||
  103.     memcmp((byte*) old_key,(byte*) new_key,new_length))
  104. {
  105.   if ((int) i == info->lastinx)
  106.     key_changed|=HA_STATE_WRITTEN; /* Mark that keyfile changed */
  107.   changed|=((ulonglong) 1 << i);
  108.   share->keyinfo[i].version++;
  109.   if (_mi_ck_delete(info,i,old_key,old_length)) goto err;
  110.   if (_mi_ck_write(info,i,new_key,new_length)) goto err;
  111.   if (share->base.auto_key == i+1)
  112.     auto_key_changed=1;
  113. }
  114.       }
  115.     }
  116.   }
  117.   if (share->calc_checksum)
  118.     info->checksum=(*share->calc_checksum)(info,newrec);
  119.   if ((*share->update_record)(info,pos,newrec))
  120.     goto err;
  121.   if (auto_key_changed)
  122.     update_auto_increment(info,newrec);
  123.   if (share->calc_checksum)
  124.     share->state.checksum+=(info->checksum - old_checksum);
  125.   info->update= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED | HA_STATE_AKTIV |
  126.  key_changed);
  127.   myisam_log_record(MI_LOG_UPDATE,info,newrec,info->lastpos,0);
  128.   VOID(_mi_writeinfo(info,key_changed ?  WRITEINFO_UPDATE_KEYFILE : 0));
  129.   allow_break(); /* Allow SIGHUP & SIGINT */
  130.   DBUG_RETURN(0);
  131. err:
  132.   DBUG_PRINT("error",("key: %d  errno: %d",i,my_errno));
  133.   save_errno=my_errno;
  134.   if (my_errno == HA_ERR_FOUND_DUPP_KEY || my_errno == HA_ERR_RECORD_FILE_FULL)
  135.   {
  136.     info->errkey= (int) i;
  137.     flag=0;
  138.     do
  139.     {
  140.       if (((ulonglong) 1 << i) & changed)
  141.       {
  142. /* The following code block is for text searching by SerG */
  143. if (share->keyinfo[i].flag & HA_FULLTEXT)
  144. {
  145.   if ((flag++ && _mi_ft_del(info,i,(char*) new_key,newrec,pos)) ||
  146.       _mi_ft_add(info,i,(char*) old_key,oldrec,pos))
  147.     break;
  148. }
  149. else
  150. {
  151.   uint new_length=_mi_make_key(info,i,new_key,newrec,pos);
  152.   uint old_length= _mi_make_key(info,i,old_key,oldrec,pos);
  153.   if ((flag++ && _mi_ck_delete(info,i,new_key,new_length)) ||
  154.       _mi_ck_write(info,i,old_key,old_length))
  155.     break;
  156. }
  157.       }
  158.     } while (i-- != 0);
  159.   }
  160.   else
  161.     mi_mark_crashed(info);
  162.   info->update= (HA_STATE_CHANGED | HA_STATE_AKTIV | HA_STATE_ROW_CHANGED |
  163.  key_changed);
  164.  err_end:
  165.   myisam_log_record(MI_LOG_UPDATE,info,newrec,info->lastpos,my_errno);
  166.   VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE));
  167.   allow_break(); /* Allow SIGHUP & SIGINT */
  168.   if (save_errno == HA_ERR_KEY_NOT_FOUND)
  169.     save_errno=HA_ERR_CRASHED;
  170.   DBUG_RETURN(my_errno=save_errno);
  171. } /* mi_update */