~singpolyma/biboumi

ref: 50cadf3dac0d56ef8181d1800cc30f8dcb749141 biboumi/src/database/count_query.hpp -rw-r--r-- 788 bytes
50cadf3d — louiz’ Implement our own database ORM, and update the whole code to use it 5 years ago
                                                                                
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
31
32
33
34
35
#pragma once

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

#include <string>

#include <sqlite3.h>

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

    std::size_t execute(sqlite3* db)
    {
      auto statement = this->prepare(db);
      std::size_t res = 0;
      if (sqlite3_step(statement) == SQLITE_ROW)
        res = sqlite3_column_int64(statement, 0);
      else
        {
          log_error("Count request didn’t return a result");
          return 0;
        }
      if (sqlite3_step(statement) != SQLITE_DONE)
        log_warning("Count request returned more than one result.");

      log_debug("Returning count: ", res);
      return res;
    }
};