summaryrefslogtreecommitdiff
path: root/src/database/count_query.hpp
blob: e8d24eff736bedcecb47b6d2797d3afc24aaeb2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once

#include <database/query.hpp>
#include <database/table.hpp>
#include <database/statement.hpp>

#include <string>

struct CountQuery: public Query
{
    CountQuery(std::string name):
        Query("SELECT count(*) FROM ")
    {
      this->body += std::move(name);
    }

    int64_t execute(DatabaseEngine& db)
    {
      auto statement = db.prepare(this->body);
      int64_t res = 0;
      if (statement->step() != StepResult::Error)
        res = statement->get_column_int64(0);
      else
        {
          log_error("Count request didn’t return a result");
          return 0;
        }
      return res;
    }
};