home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / isam / update.c < prev    next >
C/C++ Source or Header  |  2000-09-29  |  4KB  |  118 lines

  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.  
  17. /* Uppdaterare nuvarande record i en pisam-databas */
  18.  
  19. #include "isamdef.h"
  20. #ifdef    __WIN__
  21. #include <errno.h>
  22. #endif
  23.  
  24.     /* Updaterar senaste l{sta record i databasen */
  25.  
  26. int nisam_update(register N_INFO *info, const byte *oldrec, const byte *newrec)
  27. {
  28.   int flag,key_changed,save_errno;
  29.   reg3 ulong pos;
  30.   uint i,length;
  31.   uchar old_key[N_MAX_KEY_BUFF],*new_key;
  32.   DBUG_ENTER("nisam_update");
  33.  
  34.   LINT_INIT(save_errno);
  35.   if (!(info->update & HA_STATE_AKTIV))
  36.   {
  37.     my_errno=HA_ERR_KEY_NOT_FOUND;
  38.     DBUG_RETURN(-1);
  39.   }
  40.   if (info->s->base.options & HA_OPTION_READ_ONLY_DATA)
  41.   {
  42.     my_errno=EACCES;
  43.     DBUG_RETURN(-1);
  44.   }
  45.   pos=info->lastpos;
  46. #ifndef NO_LOCKING
  47.   if (_nisam_readinfo(info,F_WRLCK,1)) DBUG_RETURN(-1);
  48. #endif
  49.   if ((*info->s->compare_record)(info,oldrec))
  50.   {
  51.     save_errno=my_errno;
  52.     goto err_end;            /* Record has changed */
  53.   }
  54.   if (info->s->state.key_file_length >=
  55.       info->s->base.max_key_file_length -
  56.       info->s->blocksize* INDEX_BLOCK_MARGIN *info->s->state.keys)
  57.   {
  58.     save_errno=HA_ERR_INDEX_FILE_FULL;
  59.     goto err_end;
  60.   }
  61.  
  62.     /* Flyttar de element i isamfilen som m}ste flyttas */
  63.  
  64.   new_key=info->lastkey+info->s->base.max_key_length;
  65.   key_changed=HA_STATE_KEY_CHANGED;    /* We changed current database */
  66.                     /* Remove key that didn't change */
  67.   for (i=0 ; i < info->s->state.keys ; i++)
  68.   {
  69.     length=_nisam_make_key(info,i,new_key,newrec,pos);
  70.     if (length != _nisam_make_key(info,i,old_key,oldrec,pos) ||
  71.     memcmp((byte*) old_key,(byte*) new_key,length))
  72.     {
  73.       if ((int) i == info->lastinx)
  74.     key_changed|=HA_STATE_WRITTEN;        /* Mark that keyfile changed */
  75.       if (_nisam_ck_delete(info,i,old_key)) goto err;
  76.       if (_nisam_ck_write(info,i,new_key)) goto err;
  77.     }
  78.   }
  79.  
  80.   if ((*info->s->update_record)(info,pos,newrec))
  81.     goto err;
  82.  
  83.   info->update= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED | HA_STATE_AKTIV |
  84.          key_changed);
  85.   nisam_log_record(LOG_UPDATE,info,newrec,info->lastpos,0);
  86.   VOID(_nisam_writeinfo(info,test(key_changed)));
  87.   allow_break();                /* Allow SIGHUP & SIGINT */
  88.   DBUG_RETURN(0);
  89.  
  90. err:
  91.   DBUG_PRINT("error",("key: %d  errno: %d",i,my_errno));
  92.   save_errno=my_errno;
  93.   if (my_errno == HA_ERR_FOUND_DUPP_KEY || my_errno == HA_ERR_RECORD_FILE_FULL)
  94.   {
  95.     info->errkey= (int) i;
  96.     flag=0;
  97.     do
  98.     {
  99.       length=_nisam_make_key(info,i,new_key,newrec,pos);
  100.       if (length != _nisam_make_key(info,i,old_key,oldrec,pos) ||
  101.       memcmp((byte*) old_key,(byte*) new_key,length))
  102.       {
  103.     if ((flag++ && _nisam_ck_delete(info,i,new_key)) ||
  104.         _nisam_ck_write(info,i,old_key))
  105.       break;
  106.       }
  107.     } while (i-- != 0);
  108.   }
  109.   info->update= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED | HA_STATE_AKTIV |
  110.          key_changed);
  111.  err_end:
  112.   nisam_log_record(LOG_UPDATE,info,newrec,info->lastpos,save_errno);
  113.   VOID(_nisam_writeinfo(info,1));
  114.   allow_break();                /* Allow SIGHUP & SIGINT */
  115.   my_errno=(save_errno == HA_ERR_KEY_NOT_FOUND) ? HA_ERR_CRASHED : save_errno;
  116.   DBUG_RETURN(-1);
  117. } /* nisam_update */
  118.