~singpolyma/biboumi

ref: 7a9b2dca06c390531ec7f5d85e58cc27e17e7b74 biboumi/src/database/table.cpp -rw-r--r-- 797 bytes
7a9b2dca — louiz’ Use a more precise return type, instead of auto 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
#include <database/table.hpp>

std::set<std::string> get_all_columns_from_table(sqlite3* db, const std::string& table_name)
{
  std::set<std::string> result;
  char* errmsg;
  std::string query{"PRAGMA table_info("s + table_name + ")"};
  log_debug(query);
  int res = sqlite3_exec(db, query.data(), [](void* param, int columns_nb, char** columns, char**) -> int {
    constexpr int name_column = 1;
    std::set<std::string>* result = static_cast<std::set<std::string>*>(param);
    log_debug("Table has column ", columns[name_column]);
    if (name_column < columns_nb)
      result->insert(columns[name_column]);
    return 0;
  }, &result, &errmsg);

  if (res != SQLITE_OK)
    {
      log_error("Error executing ", query, ": ", errmsg);
      sqlite3_free(errmsg);
    }

  return result;
}