~singpolyma/biboumi

414bbca0bc2bf20c4f424c2368997a46129b32cc — louiz’ 6 years ago 923cf31
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
M CMakeLists.txt => CMakeLists.txt +15 -7
@@ 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)

M src/biboumi.h.cmake => src/biboumi.h.cmake +2 -0
@@ 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

M src/database/count_query.hpp => src/database/count_query.hpp +0 -2
@@ 6,8 6,6 @@

#include <string>

#include <sqlite3.h>

struct CountQuery: public Query
{
    CountQuery(std::string name):

M src/database/database.cpp => src/database/database.cpp +0 -2
@@ 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_");

M src/database/insert_query.hpp => src/database/insert_query.hpp +0 -2
@@ 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)

M src/database/postgresql_engine.cpp => src/database/postgresql_engine.cpp +5 -0
@@ 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

M src/database/postgresql_engine.hpp => src/database/postgresql_engine.hpp +22 -5
@@ 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

M src/database/query.hpp => src/database/query.hpp +0 -2
@@ 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);

M src/database/row.hpp => src/database/row.hpp +0 -2
@@ 8,8 8,6 @@

#include <type_traits>

#include <sqlite3.h>

template <typename... T>
struct Row
{

M src/database/select_query.hpp => src/database/select_query.hpp +1 -3
@@ 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);

M src/database/sqlite3_engine.cpp => src/database/sqlite3_engine.cpp +6 -0
@@ 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

M src/database/sqlite3_engine.hpp => src/database/sqlite3_engine.hpp +18 -1
@@ 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

M src/main.cpp => src/main.cpp +2 -3
@@ 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