#ifndef _DATABASE_H #define _DATABASE_H #include "debug.h" #include "define_platform.h" #include "extensions.h" #include #include using namespace std; class RecordSet; //Provider independent interfaces for the Database/RecordSet //All providers should implement these. Used by the system to access stores class Database { protected: const char *m_host; const char *m_db; const char *m_username; const char *m_password; public: Database(const char *_host, const char *_db, const char *_username = 0, const char *_password = 0); virtual bool connect() {return connected();} virtual bool connected() const {return false;} //exceptions class RequestFailed { //connectivity failed const char *m_sql; public: RequestFailed(const char *_sql = 0); RequestFailed(const RequestFailed& rf); ~RequestFailed(); }; class QueryFailed { //actual SQL failed const char *m_sql; const char *m_error; public: QueryFailed(const char *_sql, const char *_error = 0); QueryFailed(const QueryFailed& qf); //copy constructor ~QueryFailed(); }; //overloaded sql executes: morph result according to result type virtual int execute(const char *sql, RecordSet **result=0, char **sError=0, const bool manageResults = false) = 0; //must at least implement the SQL call virtual int execute(const char *sql, char **result, char **sError=0); virtual int execute(const char *sql, int *result, char **sError=0); //overloaded sproc executes: morph result according to result type virtual int execute(const char *procedure, const int argc, const char *argv[], const bool argt[], RecordSet **result = 0, char **sError = 0, const bool manageResults = false) {return 0;} virtual int execute(const char *procedure, const int argc, const char *argv[], const bool argt[], char **result, char **sError = 0); virtual int execute(const char *procedure, const int argc, const char *argv[], const bool argt[], int *result, char **sError = 0); int execute(const char *procedure, const int argc, const char *argv[], const bool argt[], unsigned int *result, char **sError = 0) {return execute(procedure, argc, argv, argt, (int*) result, sError);} int execute(const char *procedure, const int argc, const char *argv[], const bool argt[], const char **result, char **sError = 0) {return execute(procedure, argc, argv, argt, (char**) result, sError);} }; class RecordSet { unsigned int row; vector m_mallocs; protected: const bool m_manageResults; //decides whether the RecordSet tracks and frees returned values or not int recordMalloc(char *mallocd) {m_mallocs.push_back(mallocd);return 0;} public: RecordSet(const bool _manageResults=false): m_manageResults(_manageResults) {} virtual ~RecordSet(); //virtual means that the derived class destructor is called in polymorphic situations class iterator; //iterator constructors iterator begin() {return iterator(this, 0);} iterator end() {return iterator(this, (unsigned int) size());} //iterator - entire class works off the polymorphic function value below and need not be re-implemented in the derived class iterator { unsigned int m_row; RecordSet *m_recordset; iterator(RecordSet *_recordset, unsigned int _row): m_recordset(_recordset), m_row(_row) {} friend iterator RecordSet::begin(); friend iterator RecordSet::end(); friend ostream& operator<<(ostream &out, RecordSet::iterator i); public: void operator++() {if (m_row < m_recordset->size()) ++m_row;} void operator++(int) {if (m_row < m_recordset->size()) ++m_row;} //postfix void operator--() {if (m_row) --m_row;} void operator--(int) {if (m_row) --m_row;} //postfix bool operator==(iterator other) {return other.m_row == m_row;} bool operator!=(iterator other) {return other.m_row != m_row;} const char *operator()(const int column); const char *operator()(const int column, char*); const int operator()(const int column, int); const bool operator()(const int column, bool); const char *raw() {return m_recordset->raw(m_row);} unsigned int row() const {return m_row;} unsigned int row(const unsigned int _row) {return m_row = _row;} }; //functions virtual size_t size() {return 0;} virtual const char *raw(const unsigned int row = 0) {return 0;} virtual int value(char **value, const unsigned int row = 0, const unsigned int column = 0) = 0; //must be implemented (basic value retrieval) int value(int *value, const unsigned int row = 0, const unsigned int column = 0); int value(bool *value, const unsigned int row = 0, const unsigned int column = 0); virtual int length(size_t *length, const unsigned int row = 0, const unsigned int column = 0) const {*length = 0;return 0;} }; #endif