Skip to content

Commit

Permalink
Expose affected rows in MySQLDB classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gibber9809 committed Jan 8, 2024
1 parent efc34ae commit b965926
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions components/core/src/clp/MySQLDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ bool MySQLDB::execute_query(string const& sql_query) {
return true;
}

int MySQLDB::get_affected_rows() {
if (nullptr == m_db_handle) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
}

return mysql_affected_rows(m_db_handle);
}

MySQLPreparedStatement MySQLDB::prepare_statement(char const* statement, size_t statement_length) {
if (nullptr == m_db_handle) {
throw OperationFailed(ErrorCode_NotInit, __FILENAME__, __LINE__);
Expand Down
6 changes: 6 additions & 0 deletions components/core/src/clp/MySQLDB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class MySQLDB {
* @return
*/
bool execute_query(std::string const& sql_query);
/**
* Get the number of rows affected by the previous sql statement
* with the semantics of mysql_affected_rows.
* @return the number of affected rows
*/
int get_affected_rows();
/**
* Prepares a statement on the database server
* @param statement
Expand Down
9 changes: 9 additions & 0 deletions components/core/src/clp/MySQLPreparedStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ bool MySQLPreparedStatement::execute() {
return true;
}

int MySQLPreparedStatement::get_affected_rows() {
if (m_statement_handle == nullptr) {
SPDLOG_ERROR("MySQLPreparedStatement: get_affected_rows called on uninitialized statement");
return 0;
}

return mysql_stmt_affected_rows(m_statement_handle);
}

void MySQLPreparedStatement::close() {
if (nullptr != m_statement_handle) {
if (0 != mysql_stmt_close(m_statement_handle)) {
Expand Down
1 change: 1 addition & 0 deletions components/core/src/clp/MySQLPreparedStatement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MySQLPreparedStatement {
// Methods
void set(char const* statement, size_t statement_length);
bool execute();
int get_affected_rows();

MySQLParamBindings& get_statement_bindings() { return m_statement_bindings; }

Expand Down

0 comments on commit b965926

Please sign in to comment.