query.cpp 1.6 KB
Newer Older
1 2 3
#include <database/query.hpp>
#include <database/column.hpp>

louiz’'s avatar
louiz’ committed
4 5 6 7 8
void actual_bind(Statement& statement, const std::string& value, int index)
{
  statement.bind_text(index, value);
}

9
void actual_bind(Statement& statement, const std::int64_t& value, int index)
louiz’'s avatar
louiz’ committed
10 11 12 13
{
  statement.bind_int64(index, value);
}

14
void actual_bind(Statement& statement, const OptionalBool& value, int index)
louiz’'s avatar
louiz’ committed
15
{
16
  if (!value.is_set)
louiz’'s avatar
louiz’ committed
17
    statement.bind_int64(index, 0);
18
  else if (value.value)
louiz’'s avatar
louiz’ committed
19 20 21 22 23
    statement.bind_int64(index, 1);
  else
    statement.bind_int64(index, -1);
}

louiz’'s avatar
louiz’ committed
24 25 26 27 28 29 30
void actual_bind(Statement& statement, const DateTime& value, int index)
{
  const auto epoch = value.epoch().count();
  const auto result = std::to_string(static_cast<long double>(epoch) / std::chrono::system_clock::period::den);
  statement.bind_text(index, result);
}

31 32 33 34
void actual_add_param(Query& query, const std::string& val)
{
  query.params.push_back(val);
}
35

36
void actual_add_param(Query& query, const OptionalBool& val)
37
{
38
  if (!val.is_set)
39
    query.params.push_back("0");
40
  else if (val.value)
41 42 43
    query.params.push_back("1");
  else
    query.params.push_back("-1");
44 45 46 47 48 49 50 51 52 53
}

Query& operator<<(Query& query, const char* str)
{
  query.body += str;
  return query;
}

Query& operator<<(Query& query, const std::string& str)
{
louiz’'s avatar
louiz’ committed
54 55
  query.body += "$" + std::to_string(query.current_param);
  query.current_param++;
56 57 58
  actual_add_param(query, str);
  return query;
}
louiz’'s avatar
louiz’ committed
59 60 61 62 63 64 65 66 67

Query& operator<<(Query& query, const DatetimeWriter& datetime_writer)
{
  query.body += datetime_writer.escape_param_number(query.current_param++);
  actual_add_param(query, datetime_writer.get_value());
  return query;
}