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

3
#include <database/statement.hpp>
4
#include <database/database.hpp>
5 6
#include <database/column.hpp>
#include <database/query.hpp>
7 8
#include <database/row.hpp>

9 10
#include <logger/logger.hpp>

11 12
#include <utils/is_one_of.hpp>

13 14 15 16 17
#include <type_traits>
#include <vector>
#include <string>
#include <tuple>

louiz’'s avatar
louiz’ committed
18
template <std::size_t N=0, typename... T>
19 20
typename std::enable_if<N < sizeof...(T), void>::type
update_autoincrement_id(std::tuple<T...>& columns, Statement& statement)
21
{
22 23 24 25
  using ColumnType = typename std::decay<decltype(std::get<N>(columns))>::type;
  if (std::is_same<ColumnType, Id>::value)
    auto&& column = std::get<Id>(columns);
  update_autoincrement_id<N+1>(columns, statement);
26 27
}

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&)
{}

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
  {
47 48 49 50
#ifdef DEBUG_SQL_QUERIES
    const auto timer = this->log_and_time();
#endif

louiz’'s avatar
louiz’ committed
51 52 53 54 55 56 57
    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");
58 59
  }

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

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

70
    this->bind_param<N+1>(columns, statement, index);
71 72
  }

73 74 75 76 77
  template <int N=0, typename... T>
  typename std::enable_if<N == sizeof...(T), void>::type
  bind_param(const std::tuple<T...>&, Statement&, int)
  {}

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

86
  template <int N=0, typename... T>
87 88
  typename std::enable_if<N < sizeof...(T), void>::type
  insert_value(const std::tuple<T...>& columns, int index=1)
89
  {
90 91 92
    using ColumnType = std::decay_t<decltype(std::get<N>(columns))>;

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

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

113
  template <int N=0, typename... T>
114 115
  typename std::enable_if<N < sizeof...(T), void>::type
  insert_col_name(const std::tuple<T...>& columns)
116
  {
117
    using ColumnType = std::decay_t<decltype(std::get<N>(columns))>;
118

119 120 121
    if (!std::is_same<ColumnType, Id>::value)
      {
        this->body += ColumnType::name;
louiz’'s avatar
louiz’ committed
122

123 124
        if (N < (sizeof...(T) - 1))
          this->body += ", ";
125
      }
126 127

    this->insert_col_name<N+1>(columns);
128
  }
129 130 131 132 133

  template <int N=0, typename... T>
  typename std::enable_if<N == sizeof...(T), void>::type
  insert_col_name(const std::tuple<T...>&)
  {}
134
};
135 136 137 138 139 140 141 142 143 144

template <typename... T>
void insert(Row<T...>& row, DatabaseEngine& db)
{
  InsertQuery query(row.table_name, row.columns);
  // Ugly workaround for non portable stuff
  if (is_one_of<Id, T...>)
    query.body += db.get_returning_id_sql_string(Id::name);
  query.execute(db, row.columns);
}