Add resultset helper function to mariadb_func.h
Added a function that converts resultsets into matrices of strings.
This commit is contained in:
@ -523,21 +523,19 @@ int find_field(MYSQL* conn, const char* sql, const char* field_name, char* value
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Row get_row(MYSQL* conn, std::string sql)
|
Result get_result(MYSQL* conn, std::string sql)
|
||||||
{
|
{
|
||||||
Row rval;
|
Result rval;
|
||||||
MYSQL_RES* res;
|
MYSQL_RES* res;
|
||||||
|
|
||||||
if (mysql_query(conn, sql.c_str()) == 0 && (res = mysql_store_result(conn)))
|
if (mysql_query(conn, sql.c_str()) == 0 && (res = mysql_store_result(conn)))
|
||||||
{
|
{
|
||||||
MYSQL_ROW row = mysql_fetch_row(res);
|
MYSQL_ROW row = mysql_fetch_row(res);
|
||||||
|
|
||||||
if (row)
|
while (row)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < mysql_num_fields(res); i++)
|
rval.emplace_back(&row[0], &row[mysql_num_fields(res)]);
|
||||||
{
|
row = mysql_fetch_row(res);
|
||||||
rval.push_back(row[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mysql_free_result(res);
|
mysql_free_result(res);
|
||||||
}
|
}
|
||||||
@ -549,6 +547,12 @@ Row get_row(MYSQL* conn, std::string sql)
|
|||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Row get_row(MYSQL* conn, std::string sql)
|
||||||
|
{
|
||||||
|
Result res = get_result(conn, sql);
|
||||||
|
return res.empty() ? Row{} : res[0];
|
||||||
|
}
|
||||||
|
|
||||||
int get_int_version(std::string version)
|
int get_int_version(std::string version)
|
||||||
{
|
{
|
||||||
std::istringstream str(version);
|
std::istringstream str(version);
|
||||||
|
|||||||
@ -27,6 +27,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
typedef std::vector<std::string> Row;
|
typedef std::vector<std::string> Row;
|
||||||
|
typedef std::vector<Row> Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens connection to DB: wropper over mysql_real_connect
|
* Opens connection to DB: wropper over mysql_real_connect
|
||||||
@ -215,7 +216,17 @@ int find_field(MYSQL* conn, const char* sql, const char* field_name, char* value
|
|||||||
*
|
*
|
||||||
* @return The first row as a list of strings
|
* @return The first row as a list of strings
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> get_row(MYSQL* conn, std::string sql);
|
Row get_row(MYSQL* conn, std::string sql);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a query and return the result
|
||||||
|
*
|
||||||
|
* @param conn The connection to use
|
||||||
|
* @param sql The query to execute
|
||||||
|
*
|
||||||
|
* @return The result as a list of rows
|
||||||
|
*/
|
||||||
|
Result get_result(MYSQL* conn, std::string sql);
|
||||||
|
|
||||||
int get_int_version(std::string version);
|
int get_int_version(std::string version);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user