Commit 414bbca0 authored by louiz’'s avatar louiz’

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
parent 923cf31b
......@@ -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})
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,11 +271,15 @@ if(LIBIDN_FOUND)
target_link_libraries(test_suite ${LIBIDN_LIBRARIES})
endif()
if(USE_DATABASE)
if(SQLITE3_FOUND)
target_link_libraries(${PROJECT_NAME} ${SQLITE3_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${PQ_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)
# of each source file
......
......@@ -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
......
......@@ -6,8 +6,6 @@
#include <string>
#include <sqlite3.h>
struct CountQuery: public Query
{
CountQuery(std::string name):
......
......@@ -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_");
......
......@@ -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)
......
#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
#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
......@@ -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);
......
......@@ -8,8 +8,6 @@
#include <type_traits>
#include <sqlite3.h>
template <typename... T>
struct Row
{
......
......@@ -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);
......
#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
......@@ -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
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment