From 4393c60e5fe38cb15cea032d36a40df97db7fe36 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 14 Nov 2021 21:04:48 +0100 Subject: [PATCH] Added get_cell_float and get_cell_double helpers to QueryResult. --- core/database/query_result.cpp | 29 ++++++++++++++++++ core/database/query_result.h | 2 ++ database/sqlite/sqlite3_query_builder.cpp | 37 +++++++++++++++++++++++ database/sqlite/sqlite3_query_builder.h | 4 +++ 4 files changed, 72 insertions(+) diff --git a/core/database/query_result.cpp b/core/database/query_result.cpp index da9f3d9..2519d32 100644 --- a/core/database/query_result.cpp +++ b/core/database/query_result.cpp @@ -33,6 +33,35 @@ const int QueryResult::get_cell_int(const int index) { return r; } +const float QueryResult::get_cell_float(const int index) { + if (is_cell_null(index)) { + return 0; + } + + //todo better way + std::stringstream ss; + ss.str(get_cell(index)); + + float r; + ss >> r; + + return r; +} +const double QueryResult::get_cell_double(const int index) { + if (is_cell_null(index)) { + return 0; + } + + //todo better way + std::stringstream ss; + ss.str(get_cell(index)); + + double r; + ss >> r; + + return r; +} + bool QueryResult::is_cell_null(const int index) { return true; } diff --git a/core/database/query_result.h b/core/database/query_result.h index e9168be..8992fdb 100644 --- a/core/database/query_result.h +++ b/core/database/query_result.h @@ -12,6 +12,8 @@ public: virtual const String get_cell_str(const int index); virtual const bool get_cell_bool(const int index); virtual const int get_cell_int(const int index); + virtual const float get_cell_float(const int index); + virtual const double get_cell_double(const int index); virtual bool is_cell_null(const int index); diff --git a/database/sqlite/sqlite3_query_builder.cpp b/database/sqlite/sqlite3_query_builder.cpp index 7e1a0a9..5dc8ba6 100644 --- a/database/sqlite/sqlite3_query_builder.cpp +++ b/database/sqlite/sqlite3_query_builder.cpp @@ -162,6 +162,25 @@ QueryBuilder *SQLite3QueryBuilder::val(const bool param) { return this; } +QueryBuilder *SQLite3QueryBuilder::valf(const float param) { + //todo add a better way + std::stringstream ss; + ss << param; + + query_result += ss.str() + ", "; + + return this; +} +QueryBuilder *SQLite3QueryBuilder::vald(const double param) { + //todo add a better way + std::stringstream ss; + ss << param; + + query_result += ss.str() + ", "; + + return this; +} + QueryBuilder *SQLite3QueryBuilder::nlike(const String &str) { query_result += "LIKE '" + str + "' "; @@ -205,6 +224,24 @@ QueryBuilder *SQLite3QueryBuilder::setp(const String &col, const bool param) { return this; } +QueryBuilder *SQLite3QueryBuilder::setpf(const String &col, const float param) { + //todo add a better way + std::stringstream ss; + ss << param; + + query_result += col + "=" + ss.str() + ", "; + + return this; +} +QueryBuilder *SQLite3QueryBuilder::setpd(const String &col, const double param) { + //todo add a better way + std::stringstream ss; + ss << param; + + query_result += col + "=" + ss.str() + ", "; + + return this; +} QueryBuilder *SQLite3QueryBuilder::nwp(const String &col, const String ¶m) { query_result += col + "='" + param + "' "; diff --git a/database/sqlite/sqlite3_query_builder.h b/database/sqlite/sqlite3_query_builder.h index 7cfa865..c4bb5bd 100644 --- a/database/sqlite/sqlite3_query_builder.h +++ b/database/sqlite/sqlite3_query_builder.h @@ -43,6 +43,8 @@ public: QueryBuilder *val(const char *param); QueryBuilder *val(const int param); QueryBuilder *val(const bool param); + QueryBuilder *valf(const float param); + QueryBuilder *vald(const double param); QueryBuilder *nlike(const String &str); @@ -52,6 +54,8 @@ public: QueryBuilder *setp(const String &col, const char *param); QueryBuilder *setp(const String &col, const int param); QueryBuilder *setp(const String &col, const bool param); + QueryBuilder *setpf(const String &col, const float param); + QueryBuilder *setpd(const String &col, const double param); QueryBuilder *nwp(const String &col, const String ¶m); QueryBuilder *wp(const String &col, const char *param);