AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
sqlConnectionFactory.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_SQL_CON_FACT_H_
21 #define _ANCH_SQL_CON_FACT_H_
22 
23 #include "sql/connection.hpp"
24 #include "sql/sqlException.hpp"
25 
26 #ifdef ANCH_SQL_MYSQL
27 #include "sql/mysqlConnection.hpp"
28 #endif // ANCH_SQL_MYSQL
29 
30 #ifdef ANCH_SQL_POSTGRESQL
31 #include "sql/postgresqlConnection.hpp"
32 #endif // ANCH_SQL_POSTGRESQL
33 
34 #ifdef ANCH_SQL_SQLITE3
35 #include "sql/sqlite3Connection.hpp"
36 #endif // ANCH_SQL_SQLITE3
37 
38 #include "resourcePool.hpp"
39 #include "singleton.hpp"
40 
41 #include <string>
42 #include <map>
43 
44 
45 namespace anch {
46  namespace sql {
47 
57  std::shared_ptr<Connection>
59  std::shared_ptr<Connection> conn;
60 #ifdef ANCH_SQL_MYSQL
61  if(config.driver == "MySQL") {
62  conn = std::make_shared<MySQLConnection>(config);
63  }
64 #endif // ANCH_SQL_MYSQL
65 #ifdef ANCH_SQL_POSTGRESQL
66  if(config.driver == "PostgreSQL") {
67  conn = std::make_shared<PostgreSQLConnection>(config);
68  }
69 #endif // ANCH_SQL_POSTGRESQL
70 #ifdef ANCH_SQL_SQLITE3
71  if(config.driver == "SQLite3") {
72  conn = std::make_shared<SQLite3Connection>(config);
73  }
74 #endif // ANCH_SQL_SQLITE3
75  if(conn.get() == NULL) {
76  std::ostringstream out;
77  out << "Can not create connection from configuration: driver "
78  << config.driver << " is not taken into account." << std::endl;
79  throw SqlException(out.str());
80  }
81  return conn;
82  }
83 
86 
94  class SqlConnectionFactory: public anch::Singleton<SqlConnectionFactory> {
96 
97  // Attributes +
98  private:
100  std::map<std::string, SqlConnectionConfiguration> _configs;
101 
103  std::map<std::string, SqlConnectionPool* > _pools;
104  // Attributes -
105 
106  // Constructors +
107  private:
113 
119  SqlConnectionFactory(const SqlConnectionFactory& other) = delete;
120  // Constructors -
121 
122  // Destructor +
123  private:
127  virtual ~SqlConnectionFactory();
128  // Destructor -
129 
130  // Methods +
131  public:
139  Connection* createConnection(const std::string& name) throw(SqlException);
140 
148  SqlConnectionPool& getPool(const std::string& name) throw(SqlException);
149  // Methods -
150 
151  };
152 
153  }
154 }
155 
156 #endif // _ANCH_SQL_CON_FACT_H_
SQL database connection configuration.
Definition: connection.hpp:41
SQL connection factory.
Definition: sqlConnectionFactory.hpp:94
SQL exception.
Definition: sqlException.hpp:39
Connection * createConnection(const std::string &name)
Definition: sqlConnectionFactory.cpp:124
AnCH framework base namespace.
Definition: base64.hpp:28
SQL connection virtual class.
Definition: connection.hpp:73
std::shared_ptr< Connection > make_shared_connection(const SqlConnectionConfiguration &config)
Definition: sqlConnectionFactory.hpp:58
SqlConnectionPool & getPool(const std::string &name)
Definition: sqlConnectionFactory.cpp:158
Generic resource pool utility class.
Definition: resourcePool.hpp:117
Meyers&#39; singleton implemtation.
Definition: singleton.hpp:35