diff --git a/backends/smstools3/main.cpp b/backends/smstools3/main.cpp index 1b0bb044..c152ef4f 100644 --- a/backends/smstools3/main.cpp +++ b/backends/smstools3/main.cpp @@ -337,55 +337,16 @@ int main (int argc, char* argv[]) { log4cxx::PropertyConfigurator::configure(p); } -#ifdef WITH_SQLITE - if (CONFIG_STRING(&config, "database.type") == "sqlite3") { - storageBackend = new SQLite3Backend(&config); - if (!storageBackend->connect()) { - std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; - return -1; - } - } -#else - if (CONFIG_STRING(&config, "database.type") == "sqlite3") { - std::cerr << "Spectrum2 is not compiled with mysql backend.\n"; + std::string error; + storageBackend = StorageBackend::createBackend(&config, error); + if (storageBackend == NULL) { + std::cerr << error << "\n"; return -2; } -#endif -#ifdef WITH_MYSQL - if (CONFIG_STRING(&config, "database.type") == "mysql") { - storageBackend = new MySQLBackend(&config); - if (!storageBackend->connect()) { - std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; - return -1; - } - } -#else - if (CONFIG_STRING(&config, "database.type") == "mysql") { - std::cerr << "Spectrum2 is not compiled with mysql backend.\n"; - return -2; - } -#endif - -#ifdef WITH_PQXX - if (CONFIG_STRING(&config, "database.type") == "pqxx") { - storageBackend = new PQXXBackend(&config); - if (!storageBackend->connect()) { - std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; - return -1; - } - } -#else - if (CONFIG_STRING(&config, "database.type") == "pqxx") { - std::cerr << "Spectrum2 is not compiled with pqxx backend.\n"; - return -2; - } -#endif - - if (CONFIG_STRING(&config, "database.type") != "mysql" && CONFIG_STRING(&config, "database.type") != "sqlite3" - && CONFIG_STRING(&config, "database.type") != "pqxx" && CONFIG_STRING(&config, "database.type") != "none") { - std::cerr << "Unknown storage backend " << CONFIG_STRING(&config, "database.type") << "\n"; - return -2; + if (!storageBackend->connect()) { + std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; + return -1; } Swift::SimpleEventLoop eventLoop; diff --git a/include/transport/storagebackend.h b/include/transport/storagebackend.h index 6bac4aa7..f0a13df5 100644 --- a/include/transport/storagebackend.h +++ b/include/transport/storagebackend.h @@ -80,6 +80,8 @@ struct BuddyInfo { int flags; }; +class Config; + /// Abstract class defining storage backends. class StorageBackend { @@ -87,6 +89,8 @@ class StorageBackend /// Virtual desctructor. virtual ~StorageBackend() {} + static StorageBackend *createBackend(Config *config, std::string &error); + /// connect virtual bool connect() = 0; diff --git a/spectrum/src/main.cpp b/spectrum/src/main.cpp index d05bdeb8..02e9e226 100644 --- a/spectrum/src/main.cpp +++ b/spectrum/src/main.cpp @@ -349,57 +349,16 @@ int main(int argc, char **argv) component_ = &transport; // Logger logger(&transport); - StorageBackend *storageBackend = NULL; - -#ifdef WITH_SQLITE - if (CONFIG_STRING(&config, "database.type") == "sqlite3") { - storageBackend = new SQLite3Backend(&config); - if (!storageBackend->connect()) { - std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; - return -1; - } - } -#else - if (CONFIG_STRING(&config, "database.type") == "sqlite3") { - std::cerr << "Spectrum2 is not compiled with mysql backend.\n"; + std::string error; + StorageBackend *storageBackend = StorageBackend::createBackend(&config, error); + if (storageBackend == NULL) { + std::cerr << error << "\n"; return -2; } -#endif -#ifdef WITH_MYSQL - if (CONFIG_STRING(&config, "database.type") == "mysql") { - storageBackend = new MySQLBackend(&config); - if (!storageBackend->connect()) { - std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; - return -1; - } - } -#else - if (CONFIG_STRING(&config, "database.type") == "mysql") { - std::cerr << "Spectrum2 is not compiled with mysql backend.\n"; - return -2; - } -#endif - -#ifdef WITH_PQXX - if (CONFIG_STRING(&config, "database.type") == "pqxx") { - storageBackend = new PQXXBackend(&config); - if (!storageBackend->connect()) { - std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; - return -1; - } - } -#else - if (CONFIG_STRING(&config, "database.type") == "pqxx") { - std::cerr << "Spectrum2 is not compiled with pqxx backend.\n"; - return -2; - } -#endif - - if (CONFIG_STRING(&config, "database.type") != "mysql" && CONFIG_STRING(&config, "database.type") != "sqlite3" - && CONFIG_STRING(&config, "database.type") != "pqxx" && CONFIG_STRING(&config, "database.type") != "none") { - std::cerr << "Unknown storage backend " << CONFIG_STRING(&config, "database.type") << "\n"; - return -2; + if (!storageBackend->connect()) { + std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; + return -1; } UserManager userManager(&transport, &userRegistry, storageBackend);