ANNA Suite  2020b
Multipurpose development suite for Telco applications
Accesor.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_dbos_Accesor_hpp
10 #define anna_dbos_Accesor_hpp
11 
13 #include <anna/core/mt/Mutex.hpp>
14 
16 
17 #include <anna/dbos/defines.hpp>
18 
19 namespace anna {
20 
21 namespace dbms {
22 class Database;
23 class Connection;
24 class Statement;
25 }
26 
27 namespace dbos {
28 
29 class StorageArea;
30 
35 class Accesor : public Mutex {
36 public:
37  typedef short Id;
42  virtual ~Accesor();
43 
48  Id getId() const { return a_id; }
49 
55  noexcept(false) {
56  return (a_statement == NULL && a_database != NULL) ? (a_statement = initialize(*a_database)) : a_statement;
57  }
58 
63  bool hasDataBase() const { return a_database != NULL; }
64 
71  dbms::Database& getDatabase() { return *a_database; }
72 
77  dbms::Connection& getConnection() noexcept(false) {
78  if(a_connection == NULL) {
79  std::string msg(asString());
80  msg += " | No available database connection";
82  }
83 
84  return *a_connection;
85  }
86 
91  virtual std::string asString() const = 0;
92 
99  virtual const char* getClassName() const = 0;
100 
101 protected:
108  Accesor(dbms::Database& database, const Id id) :
109  a_database(&database),
110  a_id(id),
111  a_statement(NULL),
112  a_connection(NULL),
113  a_emodeIsNull(true)
114  {;}
115 
123  Accesor(dbms::Database& database, const Id id, const Exception::Mode::_v emode) :
124  a_database(&database),
125  a_id(id),
126  a_statement(NULL),
127  a_connection(NULL),
128  a_emodeIsNull(false),
129  a_exceptionMode(emode)
130  {;}
131 
136  Accesor(const Id id) :
137  a_database(NULL),
138  a_id(id),
139  a_statement(NULL),
140  a_connection(NULL),
141  a_emodeIsNull(true)
142  {;}
143 
152  virtual dbms::Statement* initialize(dbms::Database& database) noexcept(false) = 0;
153 
154 private:
155  dbms::Database* a_database;
156  const Id a_id;
157  dbms::Statement* a_statement;
158  dbms::Connection* a_connection;
159  bool a_emodeIsNull;
160  Exception::Mode::_v a_exceptionMode;
161 
162  void setStatement(dbms::Statement* statement) { a_statement = statement; }
163 
164  virtual bool load(dbms::Connection*, const StorageArea*) noexcept(false);
165 
166  friend class StorageArea;
167 };
168 
169 }
170 }
171 
172 #endif
173 
174 
175 
Id getId() const
Definition: Accesor.hpp:48
bool hasDataBase() const
Definition: Accesor.hpp:63
Definition: Connection.hpp:38
_v
Definition: Exception.hpp:26
Accesor(dbms::Database &database, const Id id)
Definition: Accesor.hpp:108
Definition: Accesor.hpp:35
Accesor(dbms::Database &database, const Id id, const Exception::Mode::_v emode)
Definition: Accesor.hpp:123
Definition: Statement.hpp:43
dbms::Statement * getStatement() noexcept(false)
Definition: Accesor.hpp:54
Definition: Database.hpp:37
short Id
Definition: Accesor.hpp:37
Accesor(const Id id)
Definition: Accesor.hpp:136
dbms::Database & getDatabase()
Definition: Accesor.hpp:71
Definition: app.hpp:12
#define ANNA_FILE_LOCATION
Definition: defines.hpp:23
Definition: Mutex.hpp:41
Definition: StorageArea.hpp:58
dbms::Connection & getConnection() noexcept(false)
Definition: Accesor.hpp:77
Definition: RuntimeException.hpp:23