/******************************************************************************
  @header BDBErrorHandling
 
  @availability OS X, GNUstep
  @copyright (C) 2004, 2005, 2006 Oliver Langer
 
  Author: Oliver Langer

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  27.12.2004 ola     initial version
  22.08.2006 ola     license changed
  -------------------------------------------------------------------------
  </pre>
******************************************************************************/

#if !defined(__BDBERRORHANDLING_H)
#define __BDBERRORHANDLING_H

#include <db.h>
#include <Encore/ECExceptions.h>
#include "BDBExceptions.h"

/**
 * @defined BDB_THROW_EXCEPTION_ON_ERROR
 * @discussion based on the given error code it throws a corresponding
 *   exception, if specified. If no mapping is specified then the error code
 *   is ignored. The following error codes are mapped:
 *     DB_LOCK_DEADLOCK, DB_LOCK_NOTGRANTED, DB_SECONDARY_BAD
 * The macros may be used as follows:
   BDB_ERROR_CODE_EVAL_BEGIN(dbResult);
    BDB_ERROR_CODE_EVAL_ON_THROW_BEGIN
	  [ pool release ];
	  if( NULL != dbValue.data ) {
        free( dbValue.data );
      }
	BDB_ERROR_CODE_EVAL_ON_THROW_END
  BDB_ERROR_CODE_EVAL_END;
  * The code between BDB_ERROR_CODE_EVAL_ON_THROW_BEGIN and
  * BDB_ERROR_CODE_EVAL_ON_THROW_END is evaluated if an exception *will* be
  * thrown. The caller may use this block to do clean up tasks before
  * the exception is being thrown.
 */
#define BDB_ERROR_CODE_EVAL_BEGIN(__bdb_errorcode) \
  { id __error_code_exception = nil; \
    switch( (__bdb_errorcode) ) { \
      case DB_LOCK_DEADLOCK: \
	    __error_code_exception = [[BDBDeadLockException alloc]initWithErrorCode:(__bdb_errorcode)]; \
	    break; \
	  case DB_LOCK_NOTGRANTED: \
	    __error_code_exception = [[BDBLockNotGrantedException alloc]initWithErrorCode:(__bdb_errorcode)]; \
	    break; \
	  case DB_SECONDARY_BAD: \
	    __error_code_exception = [[BDBIllegalIndexReference alloc] initWithErrorCode:(__bdb_errorcode)];\
	    break; \
	  case DB_REP_HANDLE_DEAD: \
	    __error_code_exception = [[BDBInvalidDatabaseHandle alloc] initWithErrorCode:(__bdb_errorcode)];\
	    break;\
	  case EINVAL: \
	    __error_code_exception = [[ECIllegalArgumentException alloc] initWithArgumentInfo: @"Argument unknown"]; \
      default: break;\
    }
  
#define BDB_ERROR_CODE_EVAL_ON_THROW_BEGIN\
    if( nil != __error_code_exception ) {
	
#define BDB_ERROR_CODE_EVAL_ON_THROW_END \
    }
	
#define BDB_ERROR_CODE_EVAL_END \
    if(nil!=__error_code_exception) { \
	  [__error_code_exception raise];\
	}\
  }
#endif


