diff --git a/server/src/db/database.cpp b/server/src/db/database.cpp index bc320df..8761fc8 100644 --- a/server/src/db/database.cpp +++ b/server/src/db/database.cpp @@ -18,7 +18,7 @@ void sosc::db::init_databases() { sqlite3_open("scape.db", &_ctx.hard_db); db::Query query("SELECT * FROM MIGRATIONS ORDER BY ID ASC"); auto results = query.GetResults(); - while() + //while() _ctx.ready = true; } @@ -64,6 +64,39 @@ void sosc::db::Query::SetQuery(const std::string &query, int db) { this->open = true; } +void sosc::db::Query::Bind(double value, int i) { + sqlite3_bind_double(this->statement, i, value); +} + +void sosc::db::Query::Bind(int32_t value, int i) { + sqlite3_bind_int(this->statement, i, value); +} + +void sosc::db::Query::Bind(int64_t value, int i) { + sqlite3_bind_int64(this->statement, i, value); +} + +void sosc::db::Query::Bind(sosc::time value, int i) { + sqlite3_bind_int64(this->statement, i, clk::to_unix_time(value)); +} + +void sosc::db::Query::Bind + (const std::string& value, int i, int type) +{ + if(type == DB_COL_TEXT) + sqlite3_bind_text( + this->statement, i, + value.c_str(), -1, + SQLITE_TRANSIENT + ); + else + sqlite3_bind_blob( + this->statement, i, + value.c_str(), value.length(), + SQLITE_TRANSIENT + ); +} + void sosc::db::Query::NonQuery() { if(!_ctx.ready || !this->open) return; diff --git a/server/src/db/database.hpp b/server/src/db/database.hpp index e7db6b1..871c0d3 100644 --- a/server/src/db/database.hpp +++ b/server/src/db/database.hpp @@ -34,11 +34,13 @@ private: friend class Query; }; -/*double ResultSet::Get(int column); -int32_t ResultSet::Get(int column); -int64_t ResultSet::Get(int column); -sosc::time ResultSet::Get(int column); -std::string ResultSet::Get(int column, int type = DB_COL_TEXT);*/ +/* +template<> double ResultSet::Get(int column); +template<> int32_t ResultSet::Get(int column); +template<> int64_t ResultSet::Get(int column); +template<> sosc::time ResultSet::Get(int column); +template<> std::string ResultSet::Get(int column, int type); + */ class Query { public: @@ -46,6 +48,11 @@ public: Query(const std::string& query, int db = DB_USE_HARD); void SetQuery(const std::string& query, int db = DB_USE_HARD); + template + void Bind(T value, int i); + template + void Bind(const T& value, int i, int type); + void NonQuery(); template @@ -69,11 +76,20 @@ private: friend class ResultSet; }; -/*double Query::Scalar(); -int32_t Query::Scalar(); -int64_t Query::Scalar(); -sosc::time Query::Scalar(); -std::string Query::Scalar(int type = DB_COL_TEXT);*/ +/* +template<> void Query::Bind(double value, int i); +template<> void Query::Bind(int32_t value, int i); +template<> void Query::Bind(int64_t value, int i); +template<> void Query::Bind(sosc::time value, int i); +template<> std::string Query::Bind + (const std::string& value, int i, int type); + +template<> double Query::Scalar(); +template<> int32_t Query::Scalar(); +template<> int64_t Query::Scalar(); +template<> sosc::time Query::Scalar(); +template<> std::string Query::Scalar(int type); +*/ // THE FOLLOWING ARE NOT THREAD SAFE !! // CALL THEM ONLY WHEN MASTER POOL IS INACTIVE diff --git a/server/src/utils/time.cpp b/server/src/utils/time.cpp index b9e8358..547f457 100644 --- a/server/src/utils/time.cpp +++ b/server/src/utils/time.cpp @@ -12,6 +12,20 @@ sosc::time sosc::clk::from_unix_time(uint64_t unix) { + std::chrono::seconds(unix); } +uint64_t sosc::clk::to_unix_time(sosc::time time) { + 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; + + sosc::time epoch = sosc::clock::from_time_t(mktime(&raw)); + return (uint64_t) + std::chrono::duration_cast + (time - epoch).count(); +} + std::tm sosc::clk::to_utc(sosc::time time) { time_t ctime = sosc::clock::to_time_t(time); return to_utc(&ctime); diff --git a/server/src/utils/time.hpp b/server/src/utils/time.hpp index 8ec2716..3ea4c5f 100644 --- a/server/src/utils/time.hpp +++ b/server/src/utils/time.hpp @@ -15,6 +15,7 @@ typedef std::chrono::time_point time; namespace clk { sosc::time from_unix_time(uint64_t unix); +uint64_t to_unix_time(sosc::time time); std::tm to_utc(sosc::time time); std::tm to_utc(const time_t* time);