|
#include <iostream>
|
|
|
|
#include "DBManager.h"
|
|
|
|
// Helper function for int64_t type
|
|
void get_value(int64_t& val, int idx, sqlite3_stmt* stmt)
|
|
{
|
|
if (sqlite3_column_type(stmt, idx) != SQLITE_INTEGER)
|
|
{
|
|
assert(false);
|
|
}
|
|
|
|
val = sqlite3_column_int64(stmt, idx);
|
|
}
|
|
|
|
// Helper function for double type
|
|
void get_value(double& val, int idx, sqlite3_stmt* stmt)
|
|
{
|
|
if (sqlite3_column_type(stmt, idx) != SQLITE_FLOAT)
|
|
{
|
|
assert(false);
|
|
}
|
|
|
|
val = sqlite3_column_double(stmt, idx);
|
|
}
|
|
|
|
// Helper function for string type
|
|
void get_value(std::string& val, int idx, sqlite3_stmt* stmt)
|
|
{
|
|
if (sqlite3_column_type(stmt, idx) == SQLITE_TEXT)
|
|
{
|
|
val.assign(reinterpret_cast<const char*>(sqlite3_column_text(stmt, idx)));
|
|
}
|
|
else if (sqlite3_column_type(stmt, idx) == SQLITE_NULL)
|
|
{
|
|
val.assign("");
|
|
}
|
|
else
|
|
{
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
DBManager::DBManager(const std::string& db_path)
|
|
{
|
|
|
|
int rc = sqlite3_open(db_path.c_str(), &this->db);
|
|
if (rc)
|
|
{
|
|
std::cerr << "Uh-oh spaghetti O's" << std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "DB connected\n" << std::endl;
|
|
}
|
|
}
|
|
|
|
DBManager::~DBManager()
|
|
{
|
|
sqlite3_close(this->db);
|
|
}
|