/*
** SellaNMS
** $Id: storagedata.h,v 1.3 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_COMMON_STORAGEDATA_H
#define __DGS_COMMON_STORAGEDATA_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include <dbi/dbi.h>
#include <stl/string>
#include <stl/vector>
#include <stl/hash_map>
#include <stl/utility>

/* Notes: This is a storage container that is understood by storage modules. */

class StorageData {
	public:
		StorageData();
		StorageData(StorageData const &);
		~StorageData();
		StorageData const& operator=(StorageData const &src);

		/* Used by Storage::query() */
		RESULT setResultSet(const dbi_result resultSet, dbi_conn connection, pthread_mutex_t *connection_mutex);

		/* Data Navigation Functions */
		bool firstRow();
		bool lastRow();
		bool nextRow();
		bool prevRow();
		bool seekRow(const unsigned int &row);
		int getCurRow();
		int getNumRow();
		int getNumRowAffected();
		unsigned long long getLastInsertId(const string &table);

		unsigned int getFieldSize(const string &field);
		unsigned int getFieldSizeIdx(const unsigned int &idx);
		unsigned int getFieldLength(const string &field);
		unsigned int getFieldLengthIdx(const unsigned int &idx);
		int getFieldIdx(const string &field);
		bool getFieldName(string &field, const unsigned int &idx);
		unsigned int getNumFields();
		bool getFieldType(unsigned short &type, const string &field);
		bool getFieldTypeIdx(unsigned short &type, const unsigned int &idx);
		bool getFieldAttrib(unsigned int &attrib, const string &field, const unsigned long &attrib_min, const unsigned long attrib_max);
		bool getFieldAttribIdx(unsigned int &attrib, const unsigned int &idx, const unsigned long &attrib_min, const unsigned long attrib_max);
		bool getFieldAttribs(unsigned int &attribs, const string &field);
		bool getFieldAttribsIdx(unsigned int &attribs, const unsigned int &idx);

		/* Get Data */
		string quoteString(const string &field);

		int getFields(const char *format, ...);

		bool getAuto(string &value, const string &feild);
		bool getChar(string &value, const string &field);
		bool getChar(char &value, const string &field);
		bool getUChar(string &value, const string &field);
		bool getUChar(unsigned char &value, const string &field);
		bool getShort(short &value, const string &field);
		bool getShort(string &value, const string &field);
		bool getUShort(unsigned short &value, const string &field);
		bool getUShort(string &value, const string &field);
		bool getLong(long &value, const string &field);
		bool getLong(string &value, const string &field);
		bool getULong(unsigned long &value, const string &field);
		bool getULong(string &value, const string &field);
		bool getLongLong(long long &value, const string &field);
		bool getLongLong(string &value, const string &field);
		bool getULongLong(unsigned long long &value, const string &field);
		bool getULongLong(string &value, const string &field);
		bool getFloat(float &value, const string &field);
		bool getFloat(string &value, const string &field);
		bool getDouble(double &value, const string &field);
		bool getDouble(string &value, const string &field);
		bool getString(string &value, const string &field);
		bool getBinary(const unsigned char *&value, const string &field);
		bool getBinary(string &value, const string &field);
		bool getStringCopy(string *&value, const string &field);
		bool getBinaryCopy(unsigned char *&value, const string &field);
		bool getDatetime(time_t &value, const string &field);
		bool getDatetime(string &value, const string &field);

		bool getCharIdx(char &value, unsigned int &index);
		bool getUCharIdx(unsigned char &value, unsigned int &index);
		bool getShortIdx(short &value, unsigned int &index);
		bool getUShortIdx(unsigned short &value, unsigned int &index);
		bool getLongIdx(long &value, unsigned int &index);
		bool getULongIdx(unsigned long &value, unsigned int &index);
		bool getLongLongIdx(long long &value, unsigned int &index);
		bool getULongLongIdx(unsigned long long &value, unsigned int &index);
		bool getFloatIdx(float &value, unsigned int &index);
		bool getDoubleIdx(double &value, unsigned int &index);
		bool getStringIdx(string &string, unsigned int &index);
		bool getBinaryIdx(const unsigned char *&value, unsigned int &index);
		bool getStringCopyIdx(string *&value, unsigned int &index);
		bool getBinaryCopyIdx(unsigned char *&value, unsigned int &index);
		bool getDatetimeIdx(time_t &value, unsigned int &index);

#if 0
		bool bindFields();
		bool bindChar();
		bool bindUChar();
		bool bindShort();
		bool bindUShort();
		bool bindLong();
		bool bindULong();
		bool bindLongLong();
		bool bindULongLong();
		bool bindFloat();
		bool bindDouble();
		bool bindString();
		bool bindBinary();
		bool bindStringCopy();
		bool bindBinaryCopy();
		bool bindEnum();
		bool bindSet();
		bool bindDatetime();
#endif

		/* New API - Functions */
		void clear();
		bool empty() const;
		bool isError();

	private:
		/* Functions */
		bool isSane(bool get = 0);

		/* Variables */
		bool error;
		dbi_result resultSet;
		dbi_conn connection;
		pthread_mutex_t *connectionMutex;
		unsigned long currentRow;
		unsigned long numberRows;
};

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/
