| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]> |
| 4 | // |
| 5 | // Distributed under: |
| 6 | // |
| 7 | // the Boost Software License, Version 1.0. |
| 8 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | // |
| 11 | // or (at your opinion) under: |
| 12 | // |
| 13 | // The MIT License |
| 14 | // (See accompanying file MIT.txt or a copy at |
| 15 | // http://www.opensource.org/licenses/mit-license.php) |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | #define CPPDB_SOURCE |
| 19 | #include <cppdb/driver_manager.h> |
| 20 | #include <cppdb/shared_object.h> |
| 21 | #include <cppdb/backend.h> |
| 22 | #include <cppdb/utils.h> |
| 23 | #include <cppdb/mutex.h> |
| 24 | |
| 25 | #include <vector> |
| 26 | #include <list> |
| 27 | |
| 28 | extern "C" { |
| 29 | #ifdef CPPDB_WITH_SQLITE3 |
| 30 | cppdb::backend::connection *cppdb_sqlite3_get_connection(cppdb::connection_info const &cs); |
| 31 | #endif |
| 32 | #ifdef CPPDB_WITH_PQ |
| 33 | cppdb::backend::connection *cppdb_postgresql_get_connection(cppdb::connection_info const &cs); |
| 34 | #endif |
| 35 | #ifdef CPPDB_WITH_ODBC |
| 36 | cppdb::backend::connection *cppdb_odbc_get_connection(cppdb::connection_info const &cs); |
| 37 | #endif |
| 38 | #ifdef CPPDB_WITH_MYSQL |
| 39 | cppdb::backend::connection *cppdb_mysql_get_connection(cppdb::connection_info const &cs); |
| 40 | #endif |
| 41 | } |
| 42 | |
| 43 | |
| 44 | namespace cppdb { |
| 45 | |
| 46 | typedef backend::static_driver::connect_function_type connect_function_type; |
| 47 | |
| 48 | class so_driver : public backend::loadable_driver { |
| 49 | public: |
| 50 | so_driver(std::string const &name,std::vector<std::string> const &so_list) : |
| 51 | connect_(0) |
| 52 | { |
| 53 | std::string symbol_name = "cppdb_" + name + "_get_connection"; |
| 54 | for(unsigned i=0;i<so_list.size();i++) { |
| 55 | so_ = shared_object::open(so_list[i]); |
| 56 | if(so_) { |
| 57 | so_->safe_resolve(symbol_name,connect_); |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | if(!so_ || !connect_) { |
| 62 | throw cppdb_error("cppdb::driver failed to load driver " + name + " - no module found"); |
| 63 | } |
| 64 | } |
| 65 | virtual backend::connection *open(connection_info const &ci) |
| 66 | { |
| 67 | return connect_(ci); |
| 68 | } |
| 69 | private: |
| 70 | connect_function_type connect_; |
| 71 | ref_ptr<shared_object> so_; |
| 72 | }; |
| 73 | |
| 74 | backend::connection *driver_manager::connect(std::string const &str) |
| 75 | { |
| 76 | connection_info conn(str); |
| 77 | return connect(conn); |
| 78 | } |
| 79 | |
| 80 | backend::connection *driver_manager::connect(connection_info const &conn) |
| 81 | { |
| 82 | ref_ptr<backend::driver> drv_ptr; |
| 83 | drivers_type::iterator p; |
| 84 | { // get driver |
| 85 | mutex::guard lock(lock_); |
| 86 | p=drivers_.find(conn.driver); |
| 87 | if(p!=drivers_.end()) { |
| 88 | drv_ptr = p->second; |
| 89 | } |
| 90 | else { |
| 91 | drv_ptr = load_driver(conn); |
| 92 | drivers_[conn.driver] = drv_ptr; |
| 93 | } |
| 94 | } |
| 95 | return drv_ptr->connect(conn); |
| 96 | } |
| 97 | void driver_manager::collect_unused() |
| 98 | { |
| 99 | std::list<ref_ptr<backend::driver> > garbage; |
| 100 | { |
| 101 | mutex::guard lock(lock_); |
| 102 | drivers_type::iterator p=drivers_.begin(),tmp; |
| 103 | while(p!=drivers_.end()) { |
| 104 | if(!p->second->in_use()) { |
| 105 | garbage.push_back(p->second); |
| 106 | tmp=p; |
| 107 | ++p; |
| 108 | drivers_.erase(tmp); |
| 109 | } |
| 110 | else { |
| 111 | ++p; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | garbage.clear(); |
| 116 | } |
| 117 | |
| 118 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__CYGWIN__) |
| 119 | |
| 120 | # define CPPDB_LIBRARY_SUFFIX_V1 "-" CPPDB_SOVERSION CPPDB_LIBRARY_SUFFIX |
| 121 | # define CPPDB_LIBRARY_SUFFIX_V2 CPPDB_LIBRARY_SUFFIX |
| 122 | |
| 123 | #elif defined(__APPLE__) |
| 124 | |
| 125 | # define CPPDB_LIBRARY_SUFFIX_V1 "." CPPDB_SOVERSION CPPDB_LIBRARY_SUFFIX |
| 126 | # define CPPDB_LIBRARY_SUFFIX_V2 CPPDB_LIBRARY_SUFFIX |
| 127 | |
| 128 | #else |
| 129 | |
| 130 | # define CPPDB_LIBRARY_SUFFIX_V1 CPPDB_LIBRARY_SUFFIX "." CPPDB_SOVERSION |
| 131 | # define CPPDB_LIBRARY_SUFFIX_V2 CPPDB_LIBRARY_SUFFIX |
| 132 | |
| 133 | #endif |
| 134 | |
| 135 | #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__) |
| 136 | |
| 137 | # define PATH_SEPARATOR ';' |
| 138 | |
| 139 | #else |
| 140 | |
| 141 | # define PATH_SEPARATOR ':' |
| 142 | |
| 143 | #endif |
| 144 | |
| 145 | |
| 146 | ref_ptr<backend::driver> driver_manager::load_driver(connection_info const &conn) |
| 147 | { |
| 148 | std::vector<std::string> so_names; |
| 149 | std::string module; |
| 150 | std::vector<std::string> search_paths = search_paths_; |
| 151 | std::string mpath=conn.get("@modules_path"); |
| 152 | if(!mpath.empty()) { |
| 153 | size_t sep = mpath.find(PATH_SEPARATOR); |
| 154 | search_paths.push_back(mpath.substr(0,sep)); |
| 155 | while(sep<mpath.size()) { |
| 156 | size_t next = mpath.find(PATH_SEPARATOR,sep+1); |
| 157 | search_paths.push_back(mpath.substr(sep+1,next - sep+1)); |
| 158 | sep = next; |
| 159 | } |
| 160 | } |
| 161 | if(!(module=conn.get("@module")).empty()) { |
| 162 | so_names.push_back(module); |
| 163 | } |
| 164 | else { |
| 165 | std::string so_name1 = CPPDB_LIBRARY_PREFIX "cppdb_" + conn.driver + CPPDB_LIBRARY_SUFFIX_V1; |
| 166 | std::string so_name2 = CPPDB_LIBRARY_PREFIX "cppdb_" + conn.driver + CPPDB_LIBRARY_SUFFIX_V2; |
| 167 | |
| 168 | for(unsigned i=0;i<search_paths.size();i++) { |
| 169 | so_names.push_back(search_paths[i]+"/" + so_name1); |
| 170 | so_names.push_back(search_paths[i]+"/" + so_name2); |
| 171 | } |
| 172 | if(!no_default_directory_) { |
| 173 | so_names.push_back(so_name1); |
| 174 | so_names.push_back(so_name2); |
| 175 | } |
| 176 | } |
| 177 | ref_ptr<backend::driver> drv=new so_driver(conn.driver,so_names); |
| 178 | return drv; |
| 179 | } |
| 180 | |
| 181 | void driver_manager::install_driver(std::string const &name,ref_ptr<backend::driver> drv) |
| 182 | { |
| 183 | if(!drv) { |
| 184 | throw cppdb_error("cppdb::driver_manager::install_driver: Can't install empty driver"); |
| 185 | } |
| 186 | mutex::guard lock(lock_); |
| 187 | drivers_[name]=drv; |
| 188 | } |
| 189 | |
| 190 | driver_manager::driver_manager() : |
| 191 | no_default_directory_(false) |
| 192 | { |
| 193 | } |
| 194 | // Borland erros on hidden destructors in classes without only static methods. |
| 195 | #ifndef __BORLANDC__ |
| 196 | driver_manager::~driver_manager() |
| 197 | { |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | void driver_manager::add_search_path(std::string const &p) |
| 202 | { |
| 203 | mutex::guard l(lock_); |
| 204 | search_paths_.push_back(p); |
| 205 | } |
| 206 | void driver_manager::clear_search_paths() |
| 207 | { |
| 208 | mutex::guard l(lock_); |
| 209 | search_paths_.clear(); |
| 210 | } |
| 211 | void driver_manager::use_default_search_path(bool v) |
| 212 | { |
| 213 | mutex::guard l(lock_); |
| 214 | no_default_directory_ = !v; |
| 215 | } |
| 216 | driver_manager &driver_manager::instance() |
| 217 | { |
| 218 | static driver_manager instance; |
| 219 | return instance; |
| 220 | } |
| 221 | namespace { |
| 222 | struct initializer { |
| 223 | initializer() { |
| 224 | driver_manager::instance(); |
| 225 | #ifdef CPPDB_WITH_SQLITE3 |
| 226 | driver_manager::instance().install_driver( |
| 227 | "sqlite3",new backend::static_driver(cppdb_sqlite3_get_connection) |
| 228 | ); |
| 229 | #endif |
| 230 | #ifdef CPPDB_WITH_ODBC |
| 231 | driver_manager::instance().install_driver( |
| 232 | "odbc",new backend::static_driver(cppdb_odbc_get_connection) |
| 233 | ); |
| 234 | #endif |
| 235 | #ifdef CPPDB_WITH_PQ |
| 236 | driver_manager::instance().install_driver( |
| 237 | "postgresql",new backend::static_driver(cppdb_postgresql_get_connection) |
| 238 | ); |
| 239 | #endif |
| 240 | #ifdef CPPDB_WITH_MYSQL |
| 241 | driver_manager::instance().install_driver( |
| 242 | "mysql",new backend::static_driver(cppdb_mysql_get_connection) |
| 243 | ); |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | } init; |
| 248 | } |
| 249 | } // cppdb |