All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DBBase.h
Go to the documentation of this file.
1 /*
2  * DBBase.h
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 #ifndef __DBBase
22 #define __DBBase
23 
24 #include <string>
25 #include <exception>
26 #include <list>
27 #include <vector>
28 #include <functional>
29 
30 #include <HSEPData/DBTypes.h>
31 #include <HSEPData/ErrorCode.h>
32 #include <HSEP/Array.h>
33 #include <HSEP/Dictionary.h>
34 #include <HSEP/HSEPObject.h>
35 #include <iostream>
36 
37 using namespace HSEP;
38 using namespace std;
39 
40 namespace HSEPData {
41 
42  class DBResultSet; // forward declaration
43 
47  struct DBMetadataField {
48  string aColumnName;
51 
53  aColumnType = DBT_Null;
54  }
55 
57  aColumnName = pReference.aColumnName;
58  aColumnType = pReference.aColumnType;
59  aColumnTypeName = pReference.aColumnTypeName;
60  }
61  };
62 
68 
81  class CycleBase {
82  public:
83  virtual ~CycleBase() {}
94  virtual bool execute(DBResultSet* pResultSetPtr,bool pIsFirst,bool pIsLast) = 0;
95  }; // CycleBase class
96 
102  class NullCycle : public CycleBase {
103  public:
104 
105  virtual ~NullCycle() {}
116  virtual bool execute(DBResultSet* pResultSetPtr,bool pIsFirst,bool pIsLast) {
117  return true;
118  }
119 
120  }; // NullCycle
121 
128  class QueryUniqueCycle : public CycleBase {
129  StringDictionary aResults;
130  public:
131  virtual ~QueryUniqueCycle() {
132  }
141  virtual bool execute(DBResultSet* pResultSetPtr,bool pIsFirst,bool pIsLast);
142 
150  string& getValueRef(string& pKeyRef);
151 
157  return aResults;
158  }
159 
160  }; // QueryUniqueCycle
161 
167  typedef function<bool (DBResultSet* pResultSetPtr, bool pIsFirst, bool pIsLast)> ExecuteFunctionType;
168 
172  class DBResultSet : public HSEPObject {
173  public:
174  DBResultSet() : HSEPObject("DBResultSet") {}
175  virtual ~DBResultSet() {}
176 
182  virtual int forEach(CycleBase& pCycleRef) = 0;
183 
195  virtual int forEach(ExecuteFunctionType pFunction) = 0;
196 
202  virtual string getString(string& pNameRef) = 0;
203 
209  virtual string getString(const char* pName) {
210  string vName(pName);
211  return getString(vName);
212  }
213 
219  virtual string getString(int pPosition) = 0;
220 
225  virtual size_t size() = 0;
226 
231  virtual bool isLast() = 0;
232 
241  virtual void getMetadata (DBMetadata& pMetadataRef) = 0;
242 
247  virtual void destroyMetadata(DBMetadata& pMetadataRef);
248 
249  }; // DBResultSet class
250 
254  class DBStatement : public HSEPObject {
255  protected:
256  virtual DBResultSet* executeQueryPtr() = 0;
257  virtual DBResultSet* executeQueryPtr(DBParameters& ParametersRef) = 0;
258  public:
259  DBStatement() : HSEPObject("DBStatement") {
260  }
261  virtual ~DBStatement() {
262  }
263 
269  int cycleQuery(CycleBase& pCycleRef);
270 
276  int cycleQuery(ExecuteFunctionType pFunction);
277 
284  int cycleQuery(DBParameters& pParametersRef, ExecuteFunctionType pFunction);
285 
292  int cycleQuery(DBParameters& pParametersRef, CycleBase& pCycleRef);
293 
294  }; // DBStatement class
295 
296 
300  class DBEnvironment : public HSEPObject {
301  protected:
302  bool aSynchronizes; /*< Defines if the session will synchronize its operations */
303  string aSyncTo; /*< The address where the destination database engine will wait for the data */
304  string aSyncPath; /*< The path where files with the "to synchronize operations will wait for its turn */
305  int aID; /*< Internal ID to be used in a session pool */
306  public:
307  DBEnvironment() : HSEPObject("DBEnvironment") {
308  // Normally won't synchronize or will work as part of a database pool.
309  aSynchronizes = false;
310  aID = 0;
311  }
312  virtual ~DBEnvironment() {}
313 
325  virtual ErrorCode connect(string& pConnectionStringRef,string& pUserRef, string& pPasswordRef) = 0;
326 
334  virtual DBStatement* createStatementPtr(string& pQueryRef) = 0;
335 
341  virtual ErrorCode setSchema(string& pNewSchemaRef) = 0;
342 
347  bool synchronizes() const {
348  return aSynchronizes;
349  }
350 
356  void addSyncTo(string pSyncTo) {
357  aSynchronizes = true;
358  aSyncTo = pSyncTo;
359  }
360 
365  string& syncToRef() {
366  return aSyncTo;
367  }
368 
374  void addSyncPath(string pSyncPath) {
375  aSyncPath = pSyncPath;
376  }
377 
382  string& syncPathRef() {
383  return aSyncPath;
384  }
385 
390  void addID(int pNewID) {
391  aID = pNewID;
392  }
393 
398  int id() {
399  return aID;
400  }
401 
402  }; // DBEnvironment class
403 
404  typedef string (*WhatMethod)(string& pMessageRef, exception* pExceptPtr);
405 
409  class DBException : public exception {
410  string aMessage;
411  public:
417  DBException(exception& pExceptionRef, WhatMethod pMethod);
418 
419  DBException(exception& pExceptionRef);
420  virtual ~DBException() throw() { }
421  virtual const char* what() const throw();
422 
423  }; // DBException class
424 
425 } // HSEPData namespace
426 
427 #endif // __DBBase