hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DBSession.hpp
Go to the documentation of this file.
1 #ifndef MYSQL_SESSION
2 #define MYSQL_SESSION
3 
4 #include <iostream>
5 #include <string>
6 #include <Poco/Data/MySQL/MySQL.h>
7 #include <Poco/Data/SQLite/Connector.h>
8 #include <Poco/Data/MySQL/Connector.h>
9 #include <Poco/Data/Session.h>
10 #include <Poco/Data/SessionFactory.h>
11 #include <Poco/SharedPtr.h>
12 #include <Poco/ScopedLock.h>
13 #include <Poco/Mutex.h>
14 #include <Poco/Bugcheck.h>
15 #include <Poco/Data/MySQL/MySQLException.h>
16 #include <Poco/Util/AbstractConfiguration.h>
17 
18 #define DataBase_KEY "DataBase"
19 #define DataBase_HOST "DataBase.host"
20 #define DataBase_USER "DataBase.user"
21 #define DataBase_PASSWORD "DataBase.password"
22 #define DataBase_DB "DataBase.db"
23 #define DataBase_COMPRESS "DataBase.compress"
24 #define DataBase_COMPRESS_DEFAULT "true"
25 #define DataBase_AUTO_RECONNECT "DataBase.auto-reconnect"
26 #define DataBase_AUTO_RECONNECT_DEFAULT "true"
27 #define DataBase_TYPE "DataBase.type"
28 
29 #define MYSQL "mysql"
30 #define SQLITE "sqlite"
31 
32 //MYSQL - mysql
33 #define MYSQL_KEY Poco::Data::MySQL::Connector::KEY
34 //SQLite - SQLite
35 #define SQLITE_KEY Poco::Data::SQLite::Connector::KEY
36 
37 namespace HCE {
38  namespace database {
39  //thread-safe database
40  class DBSession {
41  private:
42  Poco::SharedPtr<Poco::Data::Session> _pSession;
43  Poco::Mutex mutex;
44  public:
45  //constructor can throw an exception
46  DBSession (const std::string& KEY,const std::string& dbConnString);
47  ~DBSession () throw ();
48  public:
49  template <typename T>
50  Poco::Data::Statement operator << (const T& t) {
51  // Creates a Statement with the given data as SQLContent
52  Poco::Mutex::ScopedLock lock(this->mutex);
53  return *_pSession << t;
54  }
55  // Closes the connection
56  void close() throw ();
57  // Return true if session is connected.
58  bool isConnected() throw();
59  public:
60  static std::string getConnectionString (Poco::AutoPtr<Poco::Util::AbstractConfiguration> _config);
61  };
62  }
63 }
64 #endif