Skip to content

Commit

Permalink
allow getting error and affected rows from a resultset
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Jul 15, 2024
1 parent 6880fed commit 46f8b85
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
81 changes: 79 additions & 2 deletions include/ssod/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,86 @@ namespace db {
using row = std::map<std::string, std::string>;

/**
* @brief Definition of a result set, a vector of maps
* @brief Definition of a result set. Supports iteration and accessing its
* rows via operator[] and at(). You can also insert new rows with emplace_back
* and push_back().
*/
using resultset = std::vector<row>;
struct resultset {
/**
* Row values
*/
std::vector<row> rows;
/**
* Error message of last query or an empty string on success
*/
std::string error;
/**
* Number of affected rows, if an UPDATE, DELETE, INSERT
*/
size_t affected_rows{};

/**
* Get a row by index
* @param index row to retrieve
* @return row
*/
[[nodiscard]] inline const row& operator[] (size_t index) const {
return rows[index];
}

/**
* Get a row by index with range checking
* @param index row to rerieve
* @return row
*/
[[nodiscard]] inline const row& at(size_t index) const {
return rows.at(index);
}

/**
* Emplace a new row
* @param r row
*/
inline void emplace_back(const row& r) {
rows.emplace_back(r);
}

/**
* Push back a new row
* @param r row
*/
inline void push_back(const row& r) {
rows.push_back(r);
}

/**
* Get the start iterator of the container,
* for iteration
* @return beginning of container
*/
[[nodiscard]] inline auto begin() const {
return rows.begin();
}

/**
* Get the end iterator of the container,
* for iteration
* @return end of container
*/
[[nodiscard]] inline auto end() const {
return rows.end();
}

[[nodiscard]] inline bool empty() const {
return rows.empty();
}

[[nodiscard]] inline size_t size() const {
return rows.size();
}
};



/**
* @brief Possible parameter types for SQL parameters
Expand Down
11 changes: 8 additions & 3 deletions src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,16 +460,18 @@ namespace db {
cc.st = mysql_stmt_init(&connection);
if (mysql_stmt_prepare(cc.st, format.c_str(), format.length())) {
log_error(format, mysql_stmt_error(cc.st));
rv.error = mysql_stmt_error(cc.st);
mysql_stmt_close(cc.st);
return rv;
}

/* Check the parameter count provided matches that which MySQL expects */
size_t expected_param_count = mysql_stmt_param_count(cc.st);
if (parameters.size() != expected_param_count) {
log_error(format, "Incorrect number of parameters: " + format + " (" + std::to_string(parameters.size()) + " vs " + std::to_string(expected_param_count) + ")");
rv.error = "Incorrect number of parameters: " + format + " (" + std::to_string(parameters.size()) + " vs " + std::to_string(expected_param_count) + ")";
log_error(format, rv.error);
mysql_stmt_close(cc.st);
return rv;
return rv;
}

/* Allocate memory for awful C stuff 🐉 */
Expand Down Expand Up @@ -518,6 +520,7 @@ namespace db {
/* Bind parameters to statement */
if (mysql_stmt_bind_param(cc.st, (MYSQL_BIND*)cc.bindings)) {
log_error(format, mysql_stmt_error(cc.st));
rv.error = mysql_stmt_error(cc.st);
return rv;
}
}
Expand All @@ -531,8 +534,9 @@ namespace db {
result = mysql_stmt_execute(cc.st);
if (result) {
log_error(format, mysql_stmt_error(cc.st));
rv.error = mysql_stmt_error(cc.st);
} else {
rows_affected = mysql_stmt_affected_rows(cc.st);
rv.affected_rows = rows_affected = mysql_stmt_affected_rows(cc.st);
}
} else {
/**
Expand Down Expand Up @@ -609,6 +613,7 @@ namespace db {
}
} else {
log_error(format, mysql_stmt_error(cc.st));
rv.error = mysql_stmt_error(cc.st);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ dpp::task<void> add_chat(std::string& text, const dpp::interaction_create_t& eve
text += std::string(80, ' ') + "\n";
}
if (!rs.empty()) {
std::reverse(rs.begin(), rs.end());
std::reverse(rs.rows.begin(), rs.rows.end());
}
for (const auto& row : rs) {
if (row.at("event_type") == "chat") {
Expand Down

0 comments on commit 46f8b85

Please sign in to comment.