/**************************************************************************
 * Source Id :
 *
 * $Id: dbase.h,v 1.1 2001/04/03 20:10:26 aleksandras Exp $
 *-------------------------------------------------------------------------
 * Project Notes :
 *
 *  Diamond Base
 *  ============
 *      A solid database implementation, spurred on by the continuing
 *  Metal (Lead) Base saga.
 *
 *  Project Team :
 *        A. Davison
 *        K. Lentin
 *        D. Platt
 *
 *	Project Commenced : 05-02-1993
 *
 *-------------------------------------------------------------------------
 *  Module Notes :
 *      DiamondBase Object : This object is the main database which will
 *  interact with client code via a transaction protocol
 *
 *  Original Author :
 *		Andy
 *
 **************************************************************************/

#ifndef __DB_DBASE_H__
#define __DB_DBASE_H__

#define MAX_REG 20
#define MAX_DB_INFO 20

#if (MAX_REG > MAX_DB_INFO)
#error There must be at least as many dbObj slots as regInfo slots
#endif

#include <diamond/dbconf.h>
#include <diamond/nserver.h>
#include <diamond/dbobj.h>
#include <time.h>

int DIA_EXPORT diaLoadConfig(const char *configName);
const char * DIA_EXPORT diaVerStr(void);

class diamondBase;
class dbObject;

// Who put this here?
// extern diamondBase theDBase;

//------------------------------------------------------------------
// dbInfo struct. This holds this dbObj. We use this so that dbObj's
// can remain open, thus effecting good cacheing.

struct dbInfo {
	dbObject	*theDbObject;
	int32		nameId;
	time_t		detachTime;
};

//------------------------------------------------
// User Class Registration Information Structure.
//
//	Keeps track of the user classes currently attatched
// to the database server. This information includes
// a pointer to the buffer area, a pointer to the
// dbObject the user has requested access to, the
// nameId for the relation and the current
// query context.

class dbRegInfo
{
	DIA_object* 	buffer;
	int32		dbListId;
	int32		queryId;
public:
	dbRegInfo(DIA_object* theObj, int32 listId)
	{
		buffer = theObj;
		dbListId = listId;
		queryId = -1;
	}

	~dbRegInfo()
	{
		// Repeat a thousand times, I shalt not delete other peoples data!
		// delete buffer;
//		if(!(--theDbObject->usageCount))
//			delete theDbObject;
//		nameId = -1;
	}
	const char* verStr(void) const { return "$Id: dbase.h,v 1.1 2001/04/03 20:10:26 aleksandras Exp $"; }
#if 0
	void saveDbObject(void)
	{
		theDbObject = 0;
	}
#endif
	friend class diamondBase;
};

//---------------------------------------------
//  The staticly linked database server.

class diamondBase  : public TNameServer
{
	private:
		dbRegInfo*		regInfo[MAX_REG];	// The registration info.
		dbInfo			dbList[MAX_DB_INFO]; // The open relations
//		int32			nameIds[MAX_RELATIONS][2];
						// The first element of this is the nameId for
						// some relation which has been registered. The second
						// is the number of user classes currently accessing
						// it. When User classes are detached, this array is
						// checked to determine whether or not to delete the
						// dbObject associated with the user class.
	public:
		enum transId {ti_add,ti_del,ti_begin,ti_end,ti_get,ti_put,ti_extract,
			ti_seekFirst,ti_first,ti_next,ti_prev,ti_seekLast,ti_last,
			ti_peekNext,ti_peekPrev,ti_seek,ti_find,ti_stats,ti_write,
			ti_flush,ti_lock,ti_unlock,ti_count};

		const char* verStr(void) const;
		const char* version(const bool full = false) const;
		diamondBase();
		dbError attach(dbString& name, DIA_object* resBuffer, int32& refId, dbObjData* & objData);
		dbError detach(const int32 refId);
		dbError trans(const int32 refId, const transId id, const int32 idx=0);
		int32 intTrans(const int32 refId, const transId id, const int32 idx=0);
		dbError eject(const dbString& relName);
		~diamondBase();
};
#endif

///////////////////////////////////////////////////////////////////////////
// Revision History:
//
// $Log: dbase.h,v $
// Revision 1.1  2001/04/03 20:10:26  aleksandras
// big one 3.1 (comments later)
//
// Revision 1.24  1996/03/05  21:42:35  dmb
// Preliminary modifications to get Dos/Windows 0.4 portability.
//
// Revision 1.23  1996/02/01  22:47:49  dmb
// int32
//
// Revision 1.22  1995/10/04  02:13:02  davison
// Changed references to objects to DIA_objects to prevent namespace clashes.
//
// Revision 1.21  1994/05/15 05:01:32  kevinl
// Changing char* to dbString
//
// Revision 1.20  1994/04/23  02:39:54  kevinl
// Added ti_count transaction and intTrans function
//
// Revision 1.19  1994/03/17  04:34:45  kevinl
// Added flush, lock, unlock, eject
//
// Revision 1.18  1994/02/03  05:50:31  kevinl
// Moved include files to diamond/ and fixed HAVE_MALLOC_H
//
// Revision 1.17  1994/01/24  03:00:49  davison
// loadConfig added, constructor takes no parameter.
//
// Revision 1.16  1993/12/23  22:58:48  kevinl
// dbErr uses dbString, autoconf stuff, moved logs, added const and size_t
//
// Revision 1.15  1993/10/05  07:30:22  kevinl
// AAdded dbObjData to attach
//
// Revision 1.14  1993/06/23  05:21:22  kevinl
// Mallocs are now in angular brackets
//
// Revision 1.13  1993/05/11  15:14:41  kevinl
// Made constructor for diamondBase have an optional argument since
//      TNameServer can handle the null.
//
// Revision 1.12  1993/05/11  14:44:50  kevinl
// Added version number output
//
// Revision 1.11  1993/05/01  14:37:13  kevinl
// Got rid of ints
//
// Revision 1.10  1993/04/27  07:14:13  kevinl
// Added put and write
//
// Revision 1.9  1993/04/11  05:49:02  kevinl
// Rationalised find/seek/peek methods etc
//
// Revision 1.8  1993/04/09  13:00:29  kevinl
// Stats can be called from diaRel now.
//
// Revision 1.7  1993/04/04  23:57:47  kevinl
// Cache dbObj's
//
// Revision 1.6  1993/04/01  04:23:46  kevinl
// Modified/added transactions
// Now have get, extract, and find
//
// Revision 1.5  1993/03/30  07:13:52  davison
// Usage counter for dbObject implemented, thus burying the delete bug (this
// time Rocky, fur shur !)
//
// Revision 1.4  1993/03/29  08:05:26  darrenp
// Removed deletion of other peoples' data
//
// Revision 1.3  1993/03/26  06:16:38  darrenp
// standardized error codes.
//
// Revision 1.2  1993/03/25  23:25:36  davison
// Added the detach method.
//
// Revision 1.1  1993/03/25  22:28:50  davison
// Initial revision
//
///////////////////////////////////////////////////////////////////////////
