From dda813f3aa1470822c818881a6cefb0de3936079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 17 Apr 2019 15:48:32 +0300 Subject: [PATCH] MXS-2441: Extend maxsql extraction functions Having helper methods that combine the index position search and value extraction make it easier to deal with results that have a lot of fields e.g. the output of SHOW SLAVE STATUS. --- maxutils/maxsql/include/maxsql/mariadb.hh | 3 ++ maxutils/maxsql/src/mariadb.cc | 36 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/maxutils/maxsql/include/maxsql/mariadb.hh b/maxutils/maxsql/include/maxsql/mariadb.hh index e7628f80e..15b1113f4 100644 --- a/maxutils/maxsql/include/maxsql/mariadb.hh +++ b/maxutils/maxsql/include/maxsql/mariadb.hh @@ -115,6 +115,7 @@ public: * @return Value as string */ std::string get_string(int64_t column_ind) const; + std::string get_string(const std::string& name) const; /** * Read an integer value from the current row and given column. @@ -123,6 +124,7 @@ public: * @return Value as integer. If the data could not be parsed an error flag is set. */ int64_t get_int(int64_t column_ind) const; + int64_t get_int(const std::string& name) const; /** * Check if field is null. @@ -140,6 +142,7 @@ public: * @return Value as boolean. Returns true if the field contains '1'. */ bool get_bool(int64_t column_ind) const; + bool get_bool(const std::string& name) const; /** * Has a parsing error occurred during current row? diff --git a/maxutils/maxsql/src/mariadb.cc b/maxutils/maxsql/src/mariadb.cc index dc69c2cd1..8e0c6237f 100644 --- a/maxutils/maxsql/src/mariadb.cc +++ b/maxutils/maxsql/src/mariadb.cc @@ -155,11 +155,35 @@ string QueryResult::get_string(int64_t column_ind) const return data ? data : ""; } +string QueryResult::get_string(const std::string& name) const +{ + auto idx = get_col_index(name); + + if (idx != -1) + { + return get_string(idx); + } + + return ""; +} + int64_t QueryResult::get_int(int64_t column_ind) const { return parse_integer(column_ind, "integer"); } +int64_t QueryResult::get_int(const std::string& name) const +{ + auto idx = get_col_index(name); + + if (idx != -1) + { + return get_int(idx); + } + + return 0; +} + /** * Parse a 64bit integer. On parse error an error flag is set. * @@ -212,6 +236,18 @@ bool QueryResult::get_bool(int64_t column_ind) const return rval; } +bool QueryResult::get_bool(const std::string& name) const +{ + auto idx = get_col_index(name); + + if (idx != -1) + { + return get_bool(idx); + } + + return 0; +} + bool QueryResult::field_is_null(int64_t column_ind) const { mxb_assert(column_ind < get_col_count() && column_ind >= 0 && m_rowdata);