Move query result helper class to maxsql

Added some asserts to ensure the class is used correctly.
This commit is contained in:
Esa Korhonen
2019-01-09 17:50:20 +02:00
parent 07cbda7771
commit b4d91d4b9a
8 changed files with 230 additions and 190 deletions

View File

@ -22,11 +22,11 @@
#include <maxsql/mariadb.hh>
#include <maxscale/mysql_utils.hh>
using std::string;
using maxbase::string_printf;
using maxbase::Duration;
using maxbase::StopWatch;
using maxsql::QueryResult;
class MariaDBServer::EventInfo
{
@ -82,18 +82,7 @@ uint64_t MariaDBServer::relay_log_events(const SlaveStatus& slave_conn)
std::unique_ptr<QueryResult> MariaDBServer::execute_query(const string& query, std::string* errmsg_out)
{
auto conn = m_server_base->con;
std::unique_ptr<QueryResult> rval;
MYSQL_RES* result = NULL;
if (mxs_mysql_query(conn, query.c_str()) == 0 && (result = mysql_store_result(conn)) != NULL)
{
rval = std::unique_ptr<QueryResult>(new QueryResult(result));
}
else if (errmsg_out)
{
*errmsg_out = string_printf("Query '%s' failed: '%s'.", query.c_str(), mysql_error(conn));
}
return rval;
return maxscale::execute_query(m_server_base->con, query, errmsg_out);
}
/**

View File

@ -15,11 +15,11 @@
#include <functional>
#include <string>
#include <memory>
#include <maxscale/monitor.hh>
#include <maxbase/stopwatch.hh>
#include <maxsql/mariadb.hh>
#include <maxscale/monitor.hh>
#include "server_utils.hh"
class QueryResult;
class MariaDBServer;
// Server pointer array
typedef std::vector<MariaDBServer*> ServerArray;
@ -187,7 +187,7 @@ public:
* @param errmsg_out Where to store an error message if query fails. Can be null.
* @return Pointer to query results, or an empty pointer on failure
*/
std::unique_ptr<QueryResult> execute_query(const std::string& query, std::string* errmsg_out = NULL);
std::unique_ptr<mxq::QueryResult> execute_query(const std::string& query, std::string* errmsg_out = NULL);
/**
* execute_cmd_ex with query retry ON.

View File

@ -405,94 +405,3 @@ Gtid GtidList::get_gtid(uint32_t domain) const
}
return rval;
}
QueryResult::QueryResult(MYSQL_RES* resultset)
: m_resultset(resultset)
{
if (m_resultset)
{
auto columns = mysql_num_fields(m_resultset);
MYSQL_FIELD* field_info = mysql_fetch_fields(m_resultset);
for (int64_t column_index = 0; column_index < columns; column_index++)
{
string key(field_info[column_index].name);
// TODO: Think of a way to handle duplicate names nicely. Currently this should only be used
// for known queries.
mxb_assert(m_col_indexes.count(key) == 0);
m_col_indexes[key] = column_index;
}
}
}
QueryResult::~QueryResult()
{
if (m_resultset)
{
mysql_free_result(m_resultset);
}
}
bool QueryResult::next_row()
{
mxb_assert(m_resultset);
m_rowdata = mysql_fetch_row(m_resultset);
if (m_rowdata)
{
m_current_row_ind++;
return true;
}
return false;
}
int64_t QueryResult::get_current_row_index() const
{
return m_current_row_ind;
}
int64_t QueryResult::get_col_count() const
{
return m_resultset ? mysql_num_fields(m_resultset) : -1;
}
int64_t QueryResult::get_row_count() const
{
return m_resultset ? mysql_num_rows(m_resultset) : -1;
}
int64_t QueryResult::get_col_index(const string& col_name) const
{
auto iter = m_col_indexes.find(col_name);
return (iter != m_col_indexes.end()) ? iter->second : -1;
}
string QueryResult::get_string(int64_t column_ind) const
{
mxb_assert(column_ind < get_col_count() && column_ind >= 0);
char* data = m_rowdata[column_ind];
return data ? data : "";
}
int64_t QueryResult::get_uint(int64_t column_ind) const
{
mxb_assert(column_ind < get_col_count() && column_ind >= 0);
char* data = m_rowdata[column_ind];
int64_t rval = -1;
if (data && *data)
{
errno = 0; // strtoll sets this
char* endptr = NULL;
auto parsed = strtoll(data, &endptr, 10);
if (parsed >= 0 && errno == 0 && *endptr == '\0')
{
rval = parsed;
}
}
return rval;
}
bool QueryResult::get_bool(int64_t column_ind) const
{
mxb_assert(column_ind < get_col_count() && column_ind >= 0);
char* data = m_rowdata[column_ind];
return data ? (strcmp(data, "Y") == 0 || strcmp(data, "1") == 0) : false;
}

View File

@ -16,7 +16,7 @@
#include <string>
#include <vector>
#include <maxbase/stopwatch.hh>
#include <maxscale/mysql_utils.hh>
#include <maxscale/server.hh>
class MariaDBServer;
@ -240,84 +240,3 @@ public:
ServerOperation(MariaDBServer* target, bool was_is_master, bool handle_events,
const std::string& sql_file, const SlaveStatusArray& conns_to_copy);
};
/**
* Helper class for simplifying working with resultsets. Used in MariaDBServer.
*/
class QueryResult
{
// These need to be banned to avoid premature destruction.
QueryResult(const QueryResult&) = delete;
QueryResult& operator=(const QueryResult&) = delete;
public:
QueryResult(MYSQL_RES* resultset = NULL);
~QueryResult();
/**
* Advance to next row. Affects all result returning functions.
*
* @return True if the next row has data, false if the current row was the last one.
*/
bool next_row();
/**
* Get the index of the current row.
*
* @return Current row index, or -1 if no data or next_row() has not been called yet.
*/
int64_t get_current_row_index() const;
/**
* How many columns the result set has.
*
* @return Column count, or -1 if no data.
*/
int64_t get_col_count() const;
/**
* How many rows does the result set have?
*
* @return The number of rows or -1 on error
*/
int64_t get_row_count() const;
/**
* Get a numeric index for a column name. May give wrong results if column names are not unique.
*
* @param col_name Column name
* @return Index or -1 if not found.
*/
int64_t get_col_index(const std::string& col_name) const;
/**
* Read a string value from the current row and given column. Empty string and (null) are both interpreted
* as the empty string.
*
* @param column_ind Column index
* @return Value as string
*/
std::string get_string(int64_t column_ind) const;
/**
* Read a non-negative integer value from the current row and given column.
*
* @param column_ind Column index
* @return Value as integer. 0 or greater indicates success, -1 is returned on error.
*/
int64_t get_uint(int64_t column_ind) const;
/**
* Read a boolean value from the current row and given column.
*
* @param column_ind Column index
* @return Value as boolean. Returns true if the text is either 'Y' or '1'.
*/
bool get_bool(int64_t column_ind) const;
private:
MYSQL_RES* m_resultset = NULL; // Underlying result set, freed at dtor.
std::unordered_map<std::string, int64_t> m_col_indexes; // Map of column name -> index
MYSQL_ROW m_rowdata = NULL; // Data for current row
int64_t m_current_row_ind = -1;// Index of current row
};