insert_query.hpp 3.52 KB
Newer Older
1 2
#pragma once

3
#include <database/statement.hpp>
4 5 6 7 8 9 10 11 12 13 14
#include <database/column.hpp>
#include <database/query.hpp>
#include <logger/logger.hpp>

#include <type_traits>
#include <vector>
#include <string>
#include <tuple>

#include <sqlite3.h>

louiz’'s avatar
louiz’ committed
15 16 17
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)
18
{
louiz’'s avatar
louiz’ committed
19 20
  using ColumnType = typename std::decay<decltype(std::get<N>(columns))>::type;
  if (std::is_same<ColumnType, Id>::value)
21
    {
louiz’'s avatar
louiz’ committed
22 23
      log_debug("EXTRACTING LAST ID");
      auto&& column = std::get<Id>(columns);
24
    }
louiz’'s avatar
louiz’ committed
25
  update_autoincrement_id<N+1>(columns, statement);
26 27
}

louiz’'s avatar
louiz’ committed
28 29 30 31 32
template <std::size_t N=0, typename... T>
typename std::enable_if<N == sizeof...(T), void>::type
update_autoincrement_id(std::tuple<T...>&, Statement& statement)
{}

33 34
struct InsertQuery: public Query
{
louiz’'s avatar
louiz’ committed
35 36 37
  template <typename... T>
  InsertQuery(const std::string& name, const std::tuple<T...>& columns):
      Query("INSERT INTO ")
38 39
  {
    this->body += name;
louiz’'s avatar
louiz’ committed
40 41
    this->insert_col_names(columns);
    this->insert_values(columns);
42 43 44
  }

  template <typename... T>
louiz’'s avatar
louiz’ committed
45
  void execute(DatabaseEngine& db, std::tuple<T...>& columns)
46
  {
louiz’'s avatar
louiz’ committed
47 48 49 50 51 52 53
    auto statement = db.prepare(this->body);
    this->bind_param(columns, *statement);

    if (statement->step() != StepResult::Error)
      db.extract_last_insert_rowid(*statement);
    else
      log_error("Failed to extract the rowid from the last INSERT");
54 55
  }

56
  template <int N=0, typename... T>
57
  typename std::enable_if<N < sizeof...(T), void>::type
louiz’'s avatar
louiz’ committed
58
  bind_param(const std::tuple<T...>& columns, Statement& statement, int index=1)
59
  {
louiz’'s avatar
louiz’ committed
60 61 62 63 64
    auto&& column = std::get<N>(columns);
    using ColumnType = std::decay_t<decltype(column)>;

    if (!std::is_same<ColumnType, Id>::value)
      actual_bind(statement, column.value, index++);
65

louiz’'s avatar
louiz’ committed
66
    this->bind_param<N+1>(columns, statement, index);
67 68
  }

69
  template <int N=0, typename... T>
70
  typename std::enable_if<N == sizeof...(T), void>::type
louiz’'s avatar
louiz’ committed
71
  bind_param(const std::tuple<T...>&, Statement&, int)
72 73 74 75 76 77
  {}

  template <typename... T>
  void insert_values(const std::tuple<T...>& columns)
  {
    this->body += "VALUES (";
78
    this->insert_value(columns);
79 80 81
    this->body += ")";
  }

82
  template <int N=0, typename... T>
83
  typename std::enable_if<N < sizeof...(T), void>::type
louiz’'s avatar
louiz’ committed
84
  insert_value(const std::tuple<T...>& columns, int index=1)
85
  {
louiz’'s avatar
louiz’ committed
86 87 88 89 90 91 92 93 94
    using ColumnType = std::decay_t<decltype(std::get<N>(columns))>;

    if (!std::is_same<ColumnType, Id>::value)
      {
        this->body += "$" + std::to_string(index++);
        if (N != sizeof...(T) - 1)
          this->body += ", ";
      }
    this->insert_value<N+1>(columns, index);
95
  }
96
  template <int N=0, typename... T>
97
  typename std::enable_if<N == sizeof...(T), void>::type
louiz’'s avatar
louiz’ committed
98
  insert_value(const std::tuple<T...>&, const int)
99 100 101 102 103 104
  { }

  template <typename... T>
  void insert_col_names(const std::tuple<T...>& columns)
  {
    this->body += " (";
105
    this->insert_col_name(columns);
louiz’'s avatar
louiz’ committed
106
    this->body += ")";
107 108
  }

109
  template <int N=0, typename... T>
110 111 112
  typename std::enable_if<N < sizeof...(T), void>::type
  insert_col_name(const std::tuple<T...>& columns)
  {
louiz’'s avatar
louiz’ committed
113
    using ColumnType = std::decay_t<decltype(std::get<N>(columns))>;
114

louiz’'s avatar
louiz’ committed
115 116 117
    if (!std::is_same<ColumnType, Id>::value)
      {
        this->body += ColumnType::name;
118

louiz’'s avatar
louiz’ committed
119 120 121
        if (N < (sizeof...(T) - 1))
          this->body += ", ";
      }
122 123 124

    this->insert_col_name<N+1>(columns);
  }
louiz’'s avatar
louiz’ committed
125

126
  template <int N=0, typename... T>
127 128 129 130
  typename std::enable_if<N == sizeof...(T), void>::type
  insert_col_name(const std::tuple<T...>&)
  {}
};