#ifndef DB_MACRO_H
#define DB_MACRO_H
#include <db.h>

#define DB_set_alloc(db)	db->set_alloc(db,malloc, realloc, free)

#if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3)
#define DB_open(db,loc,name,type,art,mode) db->open(db, loc, name, type, art, mode)
#define DB_associate(db,sec,key,art)       db->associate(db, sec, key, art)
#endif


#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
#define DB_open(db,loc,name,type,art,mode) db->open(db, loc, name, type, art, mode)
#define DB_associate(db,sec,key,art)       db->associate(db, sec, key, art)
#endif

#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 1)
#define DB_open(db,loc,name,type,art,mode) db->open(db, NULL, loc, name, type, art, mode)
#define DB_associate(db,sec,key,art)       db->associate(db, NULL, sec, key, art)
#endif

#define	DB_error()				db_error (__FILE__, __LINE__, err)
#define	DB_warn()				db_warn  (__FILE__, __LINE__, err)

#define DB_truncate(db,cnt)			db->truncate(db, NULL, &cnt, 0);
#define DB_close(db) 				db->close(db,0)
#define DB_c_close(cur) 			cur->c_close(cur) ; cur = NULL
#define DB_cursor(db,cur)			db->cursor(db, NULL, &cur,0)
#define DB_get(db,key,val)			db->get(db, NULL, &key, &val, 0)
#define DB_USE_CACHE				true
#define DB_set_flags(db,flag)			db->set_flags (db, flag)
#define DB_set_cachesize(db,len)		((DB_USE_CACHE)?db->set_cachesize (db, 0, len, 0):0)
#define DB_set_bt_compare(db,cmp)		db->set_bt_compare(db, cmp)
#define DB_del(db,key)				db->del(db, NULL, &key      , 0)
#define DB_put(db,key,val)			db->put(db, NULL, &key, &val, 0)

#define DB_put2(db,key,val)			if (0 != (err = DB_put (db,key,val               ))) DB_error()
#define DB_create(db)				if (0 != (err = db_create(&db, NULL, 0		 ))) DB_error()
#define DB_open2(db,loc,name,type,art,mode)	if (0 != (err = DB_open(db,loc,name,type,art,mode))) DB_error()
#define DB_close2(db)				if (db != NULL) if (0 != (err = DB_close (db	 ))) DB_warn (); db = NULL
#define DB_cursor2(db,cur)			if (0 != (err = DB_cursor(db, cur		 ))) DB_error()
#define DB_associate2(db,sec,key) 		if (0 != (err = DB_associate(db,sec,key,DB_CREATE))) DB_error()
#define DB_set_cachesize2(db,len)		if (0 != (err = DB_set_cachesize(db,len		 ))) DB_error()
#define DB_set_bt_compare2(db,cmp)		if (0 != (err = DB_set_bt_compare(db ,cmp	 ))) DB_error()
#define DB_stat(db,info)			if (0 != (err = db->stat(db, &info, 0		 ))) DB_error()
#define DB_del2(db,key)				if (0 != (err = DB_del (db, key			 ))) if (err != DB_NOTFOUND) DB_error()
#define DB_set_flags2(db,flag)			if (0 != (err = DB_set_flags (db, flag		 ))) DB_error()

#define INIT_DBT(dbt, dat, dat_len)    \
        DBT dbt;                       \
        bzero (&dbt, sizeof(dbt));     \
        dbt.ulen = dbt.size = dat_len; \
        dbt.data = dat;                \
        dbt.flags = DB_DBT_USERMEM;    

#define RESET_DBT(dbt, dat, dat_len)   \
        bzero (&dbt, sizeof(dbt));     \
        dbt.ulen = dbt.size = dat_len; \
        dbt.data = dat;                \
        dbt.flags = DB_DBT_USERMEM;

#endif
