Skip to content

Commit

Permalink
Add some SQLiteStatement functions to prepare for other PRs (#203)
Browse files Browse the repository at this point in the history
* Add datetime to sql statement, data stored in millisecond resolution

Signed-off-by: Marc Emmers <[email protected]>

* Add reset for reuse of a statement and add bind_double

Signed-off-by: Marc Emmers <[email protected]>

---------

Signed-off-by: Marc Emmers <[email protected]>
  • Loading branch information
marcemmers authored Oct 2, 2023
1 parent 24fb839 commit 1cb0999
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions include/ocpp/common/sqlite_statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sqlite3.h>

#include <everest/logging.hpp>
#include <ocpp/common/types.hpp>

namespace ocpp {

Expand Down Expand Up @@ -46,6 +47,10 @@ class SQLiteStatement {
return sqlite3_step(this->stmt);
}

int reset() {
return sqlite3_reset(this->stmt);
}

int bind_text(const int idx, const std::string& val, SQLiteString lifetime = SQLiteString::Static) {
return sqlite3_bind_text(this->stmt, idx, val.c_str(), val.length(),
lifetime == SQLiteString::Static ? SQLITE_STATIC : SQLITE_TRANSIENT);
Expand All @@ -71,6 +76,32 @@ class SQLiteStatement {
return bind_int(index, val);
}

int bind_datetime(const int idx, const ocpp::DateTime val) {
return sqlite3_bind_int64(
this->stmt, idx,
std::chrono::duration_cast<std::chrono::milliseconds>(val.to_time_point().time_since_epoch()).count());
}

int bind_datetime(const std::string& param, const ocpp::DateTime val) {
int index = sqlite3_bind_parameter_index(this->stmt, param.c_str());
if (index <= 0) {
throw std::out_of_range("Parameter not found in SQL query");
}
return bind_datetime(index, val);
}

int bind_double(const int idx, const double val) {
return sqlite3_bind_double(this->stmt, idx, val);
}

int bind_double(const std::string& param, const double val) {
int index = sqlite3_bind_parameter_index(this->stmt, param.c_str());
if (index <= 0) {
throw std::out_of_range("Parameter not found in SQL query");
}
return bind_double(index, val);
}

int bind_null(const int idx) {
return sqlite3_bind_null(this->stmt, idx);
}
Expand All @@ -95,6 +126,11 @@ class SQLiteStatement {
return sqlite3_column_int(this->stmt, idx);
}

ocpp::DateTime column_datetime(const int idx) {
int64_t time = sqlite3_column_int64(this->stmt, idx);
return DateTime(date::utc_clock::time_point(std::chrono::milliseconds(time)));
}

double column_double(const int idx) {
return sqlite3_column_double(this->stmt, idx);
}
Expand Down

0 comments on commit 1cb0999

Please sign in to comment.