diff options
author | louiz’ <louiz@louiz.org> | 2017-12-01 14:59:22 +0100 |
---|---|---|
committer | louiz’ <louiz@louiz.org> | 2017-12-02 20:08:50 +0100 |
commit | 414bbca0bc2bf20c4f424c2368997a46129b32cc (patch) | |
tree | c9e67c8eedee84963ecc920d8158738770f9238e | |
parent | 923cf31ba62ebfd2369a9594a60020477730b649 (diff) | |
download | biboumi-414bbca0bc2bf20c4f424c2368997a46129b32cc.tar.gz biboumi-414bbca0bc2bf20c4f424c2368997a46129b32cc.tar.bz2 biboumi-414bbca0bc2bf20c4f424c2368997a46129b32cc.tar.xz biboumi-414bbca0bc2bf20c4f424c2368997a46129b32cc.zip |
Handle postgresql and sqlite3 libs properly
Do not fail to compile when one of them is missing but the other one is not.
Raise an error when trying to open a database with the missing library.
see #3237
-rw-r--r-- | CMakeLists.txt | 22 | ||||
-rw-r--r-- | src/biboumi.h.cmake | 2 | ||||
-rw-r--r-- | src/database/count_query.hpp | 2 | ||||
-rw-r--r-- | src/database/database.cpp | 2 | ||||
-rw-r--r-- | src/database/insert_query.hpp | 2 | ||||
-rw-r--r-- | src/database/postgresql_engine.cpp | 5 | ||||
-rw-r--r-- | src/database/postgresql_engine.hpp | 27 | ||||
-rw-r--r-- | src/database/query.hpp | 2 | ||||
-rw-r--r-- | src/database/row.hpp | 2 | ||||
-rw-r--r-- | src/database/select_query.hpp | 4 | ||||
-rw-r--r-- | src/database/sqlite3_engine.cpp | 6 | ||||
-rw-r--r-- | src/database/sqlite3_engine.hpp | 19 | ||||
-rw-r--r-- | src/main.cpp | 5 |
13 files changed, 71 insertions, 29 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index f7dd534..ba11aa6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,13 +193,17 @@ file(GLOB source_network src/network/*.[hc]pp) add_library(network OBJECT ${source_network}) -if(SQLITE3_FOUND) +if(SQLITE3_FOUND OR PQ_FOUND) file(GLOB source_database src/database/*.[hc]pp) add_library(database OBJECT ${source_database}) - include_directories(database ${SQLITE3_INCLUDE_DIRS}) - include_directories(database ${PQ_INCLUDE_DIRS}) + if(SQLITE3_FOUND) + include_directories(database ${SQLITE3_INCLUDE_DIRS}) + endif() + if(PQ_FOUND) + include_directories(database ${PQ_INCLUDE_DIRS}) + endif() set(USE_DATABASE TRUE) else() add_library(database OBJECT "") @@ -267,10 +271,14 @@ if(LIBIDN_FOUND) target_link_libraries(test_suite ${LIBIDN_LIBRARIES}) endif() if(USE_DATABASE) - target_link_libraries(${PROJECT_NAME} ${SQLITE3_LIBRARIES}) - target_link_libraries(${PROJECT_NAME} ${PQ_LIBRARIES}) - target_link_libraries(test_suite ${SQLITE3_LIBRARIES}) - target_link_libraries(test_suite ${PQ_LIBRARIES}) + if(SQLITE3_FOUND) + target_link_libraries(${PROJECT_NAME} ${SQLITE3_LIBRARIES}) + target_link_libraries(test_suite ${SQLITE3_LIBRARIES}) + endif() + if(PQ_FOUND) + target_link_libraries(${PROJECT_NAME} ${PQ_LIBRARIES}) + target_link_libraries(test_suite ${PQ_LIBRARIES}) +endif() endif() # Define a __FILENAME__ macro with the relative path (from the base project directory) diff --git a/src/biboumi.h.cmake b/src/biboumi.h.cmake index 1ad9a40..5bc1004 100644 --- a/src/biboumi.h.cmake +++ b/src/biboumi.h.cmake @@ -6,6 +6,8 @@ #cmakedefine BOTAN_FOUND #cmakedefine GCRYPT_FOUND #cmakedefine UDNS_FOUND +#cmakedefine PQ_FOUND +#cmakedefine SQLITE3_FOUND #cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}" #cmakedefine PROJECT_NAME "${PROJECT_NAME}" #cmakedefine HAS_GET_TIME diff --git a/src/database/count_query.hpp b/src/database/count_query.hpp index b462e0f..e8d24ef 100644 --- a/src/database/count_query.hpp +++ b/src/database/count_query.hpp @@ -6,8 +6,6 @@ #include <string> -#include <sqlite3.h> - struct CountQuery: public Query { CountQuery(std::string name): diff --git a/src/database/database.cpp b/src/database/database.cpp index f9a365d..ae5654c 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -15,8 +15,6 @@ #include <memory> -#include <sqlite3.h> - std::unique_ptr<DatabaseEngine> Database::db; Database::MucLogLineTable Database::muc_log_lines("muclogline_"); Database::GlobalOptionsTable Database::global_options("globaloptions_"); diff --git a/src/database/insert_query.hpp b/src/database/insert_query.hpp index 2fc0512..853b7f1 100644 --- a/src/database/insert_query.hpp +++ b/src/database/insert_query.hpp @@ -10,8 +10,6 @@ #include <string> #include <tuple> -#include <sqlite3.h> - template <std::size_t N=0, typename... T> typename std::enable_if<N < sizeof...(T), void>::type update_autoincrement_id(std::tuple<T...>& columns, Statement& statement) diff --git a/src/database/postgresql_engine.cpp b/src/database/postgresql_engine.cpp index 9f603ab..1c01ed5 100644 --- a/src/database/postgresql_engine.cpp +++ b/src/database/postgresql_engine.cpp @@ -1,3 +1,6 @@ +#include <biboumi.h> +#ifdef PQ_FOUND + #include <database/postgresql_engine.hpp> #include <database/postgresql_statement.hpp> @@ -75,3 +78,5 @@ std::string PostgresqlEngine::id_column_type() { return "SERIAL"; } + +#endif diff --git a/src/database/postgresql_engine.hpp b/src/database/postgresql_engine.hpp index e6444d4..fe4fb53 100644 --- a/src/database/postgresql_engine.hpp +++ b/src/database/postgresql_engine.hpp @@ -1,16 +1,20 @@ #pragma once -#include <database/engine.hpp> +#include <biboumi.h> +#include <string> +#include <stdexcept> +#include <memory> #include <database/statement.hpp> +#include <database/engine.hpp> -#include <libpq-fe.h> - -#include <memory> -#include <string> #include <tuple> #include <set> +#ifdef PQ_FOUND + +#include <libpq-fe.h> + class PostgresqlEngine: public DatabaseEngine { public: @@ -29,3 +33,16 @@ class PostgresqlEngine: public DatabaseEngine private: PGconn* const conn; }; + +#else + +class PostgresqlEngine +{ +public: + static std::unique_ptr<DatabaseEngine> open(const std::string& string) + { + throw std::runtime_error("Cannot open postgresql database "s + string + ": biboumi is not compiled with libpq."); + } +}; + +#endif diff --git a/src/database/query.hpp b/src/database/query.hpp index 2467749..7f8aecb 100644 --- a/src/database/query.hpp +++ b/src/database/query.hpp @@ -9,8 +9,6 @@ #include <vector> #include <string> -#include <sqlite3.h> - void actual_bind(Statement& statement, const std::string& value, int index); void actual_bind(Statement& statement, const std::size_t value, int index); void actual_bind(Statement& statement, const OptionalBool& value, int index); diff --git a/src/database/row.hpp b/src/database/row.hpp index b69fac5..1b50ff9 100644 --- a/src/database/row.hpp +++ b/src/database/row.hpp @@ -8,8 +8,6 @@ #include <type_traits> -#include <sqlite3.h> - template <typename... T> struct Row { diff --git a/src/database/select_query.hpp b/src/database/select_query.hpp index 22b342e..837a866 100644 --- a/src/database/select_query.hpp +++ b/src/database/select_query.hpp @@ -12,12 +12,10 @@ #include <vector> #include <string> -#include <sqlite3.h> - using namespace std::string_literals; template <typename T> -typename std::enable_if<std::is_integral<T>::value, sqlite3_int64>::type +typename std::enable_if<std::is_integral<T>::value, std::int64_t>::type extract_row_value(Statement& statement, const int i) { return statement.get_column_int64(i); diff --git a/src/database/sqlite3_engine.cpp b/src/database/sqlite3_engine.cpp index 7e34f89..a94a892 100644 --- a/src/database/sqlite3_engine.cpp +++ b/src/database/sqlite3_engine.cpp @@ -1,3 +1,7 @@ +#include <biboumi.h> + +#ifdef SQLITE3_FOUND + #include <database/sqlite3_engine.hpp> #include <database/sqlite3_statement.hpp> @@ -91,3 +95,5 @@ std::string Sqlite3Engine::id_column_type() { return "INTEGER PRIMARY KEY AUTOINCREMENT"; } + +#endif diff --git a/src/database/sqlite3_engine.hpp b/src/database/sqlite3_engine.hpp index eb18d0c..5b8176c 100644 --- a/src/database/sqlite3_engine.hpp +++ b/src/database/sqlite3_engine.hpp @@ -4,12 +4,17 @@ #include <database/statement.hpp> -#include <sqlite3.h> #include <memory> #include <string> #include <tuple> #include <set> +#include <biboumi.h> + +#ifdef SQLITE3_FOUND + +#include <sqlite3.h> + class Sqlite3Engine: public DatabaseEngine { public: @@ -28,3 +33,15 @@ private: sqlite3* const db; }; +#else + +class Sqlite3Engine +{ +public: + static std::unique_ptr<DatabaseEngine> open(const std::string& string) + { + throw std::runtime_error("Cannot open sqlite3 database "s + string + ": biboumi is not compiled with sqlite3 lib."); + } +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 284d289..c877e43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,6 @@ #include <utils/xdg.hpp> #include <utils/reload.hpp> -#include <libpq-fe.h> - #ifdef UDNS_FOUND # include <network/dns_handler.hpp> #endif @@ -90,7 +88,8 @@ int main(int ac, char** av) #ifdef USE_DATABASE try { open_database(); - } catch (...) { + } catch (const std::exception& e) { + log_error(e.what()); return 1; } #endif |