flat database the RECKONING
This commit is contained in:
parent
f573ffcf65
commit
ff7338d9d4
4 changed files with 148 additions and 6 deletions
|
@ -32,6 +32,10 @@ void sosc::db::close_databases() {
|
|||
_ctx.ready = false;
|
||||
}
|
||||
|
||||
// ************ //
|
||||
// QUERY CODE //
|
||||
// ************ //
|
||||
|
||||
sosc::db::Query::Query() : results(this) {
|
||||
this->open = false;
|
||||
}
|
||||
|
@ -47,8 +51,9 @@ void sosc::db::Query::SetQuery(const std::string &query, int db) {
|
|||
if(!this->open)
|
||||
this->Close();
|
||||
|
||||
this->database = db == DB_USE_MEMORY ? _ctx.mem_db : _ctx.hard_db;
|
||||
int status = sqlite3_prepare_v2(
|
||||
db == DB_USE_MEMORY ? _ctx.mem_db : _ctx.hard_db,
|
||||
this->database,
|
||||
query.c_str(),
|
||||
query.length() + 1,
|
||||
&this->statement,
|
||||
|
@ -63,5 +68,127 @@ void sosc::db::Query::NonQuery() {
|
|||
if(!_ctx.ready || !this->open)
|
||||
return;
|
||||
|
||||
this->results.Step();
|
||||
}
|
||||
|
||||
double sosc::db::Query::Scalar<double>() {
|
||||
if(!_ctx.ready || !this->open)
|
||||
return 0;
|
||||
|
||||
if(this->results.Step())
|
||||
return this->results.Get<double>(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t sosc::db::Query::Scalar<int32_t>() {
|
||||
if(!_ctx.ready || !this->open)
|
||||
return 0;
|
||||
|
||||
if(this->results.Step())
|
||||
return this->results.Get<int32_t>(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t sosc::db::Query::Scalar<int64_t>() {
|
||||
if(!_ctx.ready || !this->open)
|
||||
return 0;
|
||||
|
||||
if(this->results.Step())
|
||||
return this->results.Get<int64_t>(0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
sosc::time sosc::db::Query::Scalar<sosc::time>() {
|
||||
if(!_ctx.ready || !this->open)
|
||||
return sosc::time::min();
|
||||
|
||||
if(this->results.Step())
|
||||
return this->results.Get<sosc::time>(0);
|
||||
else
|
||||
return sosc::time::min();
|
||||
}
|
||||
|
||||
std::string sosc::db::Query::Scalar<std::string>(int type) {
|
||||
if(!_ctx.ready || !this->open)
|
||||
return "";
|
||||
|
||||
if(this->results.Step())
|
||||
return this->results.Get<std::string>(0, type);
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
sosc::db::ResultSet* sosc::db::Query::GetResults() const {
|
||||
return &this->results;
|
||||
}
|
||||
|
||||
void sosc::db::Query::Reset(bool clearBinds) {
|
||||
if(clearBinds)
|
||||
sqlite3_clear_bindings(this->statement);
|
||||
|
||||
sqlite3_reset(this->statement);
|
||||
}
|
||||
|
||||
void sosc::db::Query::Close() {
|
||||
sqlite3_finalize(this->statement);
|
||||
this->open = false;
|
||||
}
|
||||
|
||||
// ***************** //
|
||||
// RESULT SET CODE //
|
||||
// ***************** //
|
||||
|
||||
sosc::db::ResultSet::ResultSet(sosc::db::Query *query) {
|
||||
this->query = query;
|
||||
}
|
||||
|
||||
bool sosc::db::ResultSet::IsOpen() const {
|
||||
return this->query->IsOpen();
|
||||
}
|
||||
|
||||
bool sosc::db::ResultSet::Step() {
|
||||
int result = sqlite3_step(this->query->statement);
|
||||
|
||||
if(result == SQLITE_ROW)
|
||||
return true;
|
||||
else if(result == SQLITE_DONE)
|
||||
return false;
|
||||
else
|
||||
throw std::string(sqlite3_errmsg(this->query->database));
|
||||
}
|
||||
|
||||
double sosc::db::ResultSet::Get<double>(int column) {
|
||||
return sqlite3_column_double(this->query->statement, column);
|
||||
}
|
||||
|
||||
int32_t sosc::db::ResultSet::Get<int32_t>(int column) {
|
||||
return sqlite3_column_int(this->query->statement, column);
|
||||
}
|
||||
|
||||
int64_t sosc::db::ResultSet::Get<int64_t>(int column) {
|
||||
return sqlite3_column_int64(this->query->statement, column);
|
||||
}
|
||||
|
||||
sosc::time sosc::db::ResultSet::Get<sosc::time>(int column) {
|
||||
return clk::from_unix_time(
|
||||
sqlite3_column_int64(this->query->statement, column)
|
||||
);
|
||||
}
|
||||
|
||||
std::string sosc::db::ResultSet::Get<std::string>(int column, int type) {
|
||||
auto data = (const char*)
|
||||
(type == DB_COL_TEXT
|
||||
? sqlite3_column_text(this->query->statement, column)
|
||||
: sqlite3_column_blob(this->query->statement, column));
|
||||
|
||||
return std::string(
|
||||
data, sqlite3_column_bytes(this->query->statement, column)
|
||||
);
|
||||
}
|
||||
|
||||
int sosc::db::ResultSet::ColumnCount() {
|
||||
return sqlite3_column_count(this->query->statement);
|
||||
}
|
|
@ -19,17 +19,17 @@ class Query;
|
|||
class ResultSet {
|
||||
public:
|
||||
bool IsOpen() const;
|
||||
bool IsReadable() const;
|
||||
bool Step();
|
||||
|
||||
template<typename T>
|
||||
T Get(int column);
|
||||
template<typename T>
|
||||
T Get(int column, int type);
|
||||
|
||||
int ColumnCount();
|
||||
private:
|
||||
ResultSet(Query* query);
|
||||
Query* query;
|
||||
bool readable;
|
||||
|
||||
friend class Query;
|
||||
};
|
||||
|
@ -58,14 +58,15 @@ public:
|
|||
return this->open;
|
||||
}
|
||||
|
||||
void Reset();
|
||||
void Reset(bool clearBinds = true);
|
||||
void Close();
|
||||
private:
|
||||
ResultSet results;
|
||||
sqlite3_stmt* statement;
|
||||
|
||||
std::string query;
|
||||
sqlite3* database;
|
||||
bool open;
|
||||
|
||||
friend class ResultSet;
|
||||
};
|
||||
|
||||
/*double Query::Scalar<double>();
|
||||
|
|
|
@ -1,5 +1,17 @@
|
|||
#include "time.hpp"
|
||||
|
||||
sosc::time sosc::clk::from_unix_time(uint64_t unix) {
|
||||
std::tm raw = std::tm();
|
||||
raw.tm_year = 70;
|
||||
raw.tm_yday = 0;
|
||||
raw.tm_hour = 0;
|
||||
raw.tm_min = 0;
|
||||
raw.tm_sec = 0;
|
||||
|
||||
return sosc::clock::from_time_t(std::mktime(&raw))
|
||||
+ std::chrono::seconds(unix);
|
||||
}
|
||||
|
||||
std::tm sosc::clk::to_utc(sosc::time time) {
|
||||
time_t ctime = sosc::clock::to_time_t(time);
|
||||
return to_utc(&ctime);
|
||||
|
|
|
@ -14,6 +14,8 @@ typedef std::chrono::system_clock clock;
|
|||
typedef std::chrono::time_point<sosc::clock> time;
|
||||
|
||||
namespace clk {
|
||||
sosc::time from_unix_time(uint64_t unix);
|
||||
|
||||
std::tm to_utc(sosc::time time);
|
||||
std::tm to_utc(const time_t* time);
|
||||
|
||||
|
|
Loading…
Reference in a new issue