Skip to content

Commit

Permalink
add ability to return the number of rows processed to execute_stateme…
Browse files Browse the repository at this point in the history
…nt()
  • Loading branch information
t-horikawa committed Jan 24, 2024
1 parent 9969a58 commit a2ae38d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
4 changes: 3 additions & 1 deletion examples/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ int main(int argc, char **argv) {
err = connection->begin(transaction);
if (err != ERROR_CODE::OK) { prt_err(__LINE__, err); return 1; }
std::cerr << "execute_statement \"" << FLAGS_statement << "\"" << std::endl;
err = transaction->execute_statement(FLAGS_statement);
std::size_t num_rows{};
err = transaction->execute_statement(FLAGS_statement, num_rows);
std::cerr << "prodessed " << num_rows << " rows" << std::endl;
if (err != ERROR_CODE::OK) { prt_err(__LINE__, err); return 1; }
err = transaction->commit();
if (err != ERROR_CODE::OK) { prt_err(__LINE__, err); return 1; }
Expand Down
39 changes: 29 additions & 10 deletions include/ogawayama/stub/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,54 @@ class Transaction {

/**
* @brief execute a statement.
* @param statement the SQL statement string
* @param statement the SQL statement string to be executed
* @param num_rows a reference to a variable to which the number of processes
* @return error code defined in error_code.h
*/
ErrorCode execute_statement(std::string_view);
ErrorCode execute_statement(std::string_view statement, std::size_t& num_rows);

/**
* @brief execute a prepared statement.
* @param pointer to the prepared statement
* @param the parameters to be used for execution of the prepared statement
* @param prepared_statement the prepared statement to be executed
* @param parameters the parameters to be used for execution of the prepared statement
* @param num_rows a reference to a variable to which the number of processes
* @return error code defined in error_code.h
*/
ErrorCode execute_statement(PreparedStatementPtr&, parameters_type&);
ErrorCode execute_statement(PreparedStatementPtr& prepared_statement, parameters_type& parameters, std::size_t& num_rows);

/**
* @brief execute a statement.
* @param statement the SQL statement string to be executed
* @return error code defined in error_code.h
*/
__attribute__((__deprecated__))
ErrorCode execute_statement(std::string_view statement);

/**
* @brief execute a prepared statement.
* @param prepared_statement the prepared statement to be executed
* @param parameters the parameters to be used for execution of the prepared statement
* @return error code defined in error_code.h
*/
__attribute__((__deprecated__))
ErrorCode execute_statement(PreparedStatementPtr& prepared_statement, parameters_type& parameters);

/**
* @brief execute a query.
* @param query the SQL query string
* @param query the SQL query string to be executed
* @param result_set returns a result set of the query
* @return error code defined in error_code.h
*/
ErrorCode execute_query(std::string_view, ResultSetPtr&);
ErrorCode execute_query(std::string_view query, ResultSetPtr& result_set);

/**
* @brief execute a prepared query.
* @param pointer to the prepared query
* @param the parameters to be used for execution of the prepared statement
* @param prepared_query the prepared query to be executed
* @param parameters the parameters to be used for execution of the prepared statement
* @param result_set returns a result set of the query
* @return error code defined in error_code.h
*/
ErrorCode execute_query(PreparedStatementPtr&, parameters_type&, ResultSetPtr&);
ErrorCode execute_query(PreparedStatementPtr& prepared_query, parameters_type& parameters, ResultSetPtr& result_set);

/**
* @brief commit the current transaction.
Expand Down
40 changes: 35 additions & 5 deletions src/ogawayama/stub/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,27 @@ Transaction::Impl::~Impl()
}
}

static inline std::size_t num_rows_processed(const ::jogasaki::proto::sql::response::ExecuteResult::Success& message) {
std::size_t num_rows{};
auto counters = message.counters();
for (auto&& e: counters) {
auto type = e.type();
if (type == ::jogasaki::proto::sql::response::ExecuteResult::INSERTED_ROWS ||
type == ::jogasaki::proto::sql::response::ExecuteResult::UPDATED_ROWS ||
type == ::jogasaki::proto::sql::response::ExecuteResult::MERGED_ROWS ||
type == ::jogasaki::proto::sql::response::ExecuteResult::DELETED_ROWS) {
num_rows += e.value();
}
}
return num_rows;
}

/**
* @brief execute a statement.
* @param statement the SQL statement string
* @return error code defined in error_code.h
*/
ErrorCode Transaction::Impl::execute_statement(std::string_view statement) {
ErrorCode Transaction::Impl::execute_statement(std::string_view statement, std::size_t& num_rows) {
if (alive_) {
::jogasaki::proto::sql::request::ExecuteStatement request{};
*(request.mutable_transaction_handle()) = transaction_handle_;
Expand All @@ -50,6 +65,7 @@ ErrorCode Transaction::Impl::execute_statement(std::string_view statement) {
}
auto response = response_opt.value();
if (response.has_success()) {
num_rows = num_rows_processed(response.success());
return ErrorCode::OK;
}
return ErrorCode::SERVER_FAILURE;
Expand Down Expand Up @@ -156,7 +172,7 @@ class parameter {
* @param prepared statement object with parameters
* @return error code defined in error_code.h
*/
ErrorCode Transaction::Impl::execute_statement(PreparedStatementPtr& prepared, const parameters_type& parameters) {
ErrorCode Transaction::Impl::execute_statement(PreparedStatementPtr& prepared, const parameters_type& parameters, std::size_t& num_rows) {
if (alive_) {
auto* ps_impl = prepared->get_impl();

Expand Down Expand Up @@ -185,6 +201,7 @@ ErrorCode Transaction::Impl::execute_statement(PreparedStatementPtr& prepared, c
}
auto response = response_opt.value();
if (response.has_success()) {
num_rows = num_rows_processed(response.success());
return ErrorCode::OK;
}
return ErrorCode::SERVER_FAILURE;
Expand Down Expand Up @@ -349,14 +366,27 @@ Transaction::Transaction(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
*/
Transaction::~Transaction() = default;

ErrorCode Transaction::execute_statement(std::string_view statement)
ErrorCode Transaction::execute_statement(std::string_view statement, std::size_t& num_rows)
{
return impl_->execute_statement(statement);
return impl_->execute_statement(statement, num_rows);
}

ErrorCode Transaction::execute_statement(PreparedStatementPtr& prepared, parameters_type& parameters, std::size_t& num_rows)
{
return impl_->execute_statement(prepared, parameters, num_rows);
}

// deprecated
ErrorCode Transaction::execute_statement(std::string_view statement)
{
std::size_t num_rows;
return impl_->execute_statement(statement, num_rows);
}
// deprecated
ErrorCode Transaction::execute_statement(PreparedStatementPtr& prepared, parameters_type& parameters)
{
return impl_->execute_statement(prepared, parameters);
std::size_t num_rows;
return impl_->execute_statement(prepared, parameters, num_rows);
}

ErrorCode Transaction::execute_query(std::string_view query, std::shared_ptr<ResultSet> &result_set)
Expand Down
6 changes: 4 additions & 2 deletions src/ogawayama/stub/transactionImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ class Transaction::Impl
/**
* @brief execute a statement.
* @param statement the SQL statement string
* @param num_rows a reference to a variable to which the number of processes
* @return true in error, otherwise false
*/
ErrorCode execute_statement(std::string_view statement);
ErrorCode execute_statement(std::string_view statement, std::size_t& num_rows);

/**
* @brief execute a prepared statement.
* @param pointer to the prepared statement
* @param the parameters to be used for execution of the prepared statement
* @param num_rows a reference to a variable to which the number of processes
* @return error code defined in error_code.h
*/
ErrorCode execute_statement(PreparedStatementPtr& prepared_statement, const parameters_type& parameters);
ErrorCode execute_statement(PreparedStatementPtr& prepared_statement, const parameters_type& parameters, std::size_t& num_rows);

/**
* @brief execute a query.
Expand Down
7 changes: 6 additions & 1 deletion test/ogawayama/stub/PreparedStatementTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ TEST_F(PreparedTest, prepare) {
// build reply message for test
jogasaki::proto::sql::response::ExecuteResult er{};
jogasaki::proto::sql::response::ExecuteResult_Success s{};
auto* c = s.add_counters();
c->set_type(jogasaki::proto::sql::response::ExecuteResult::INSERTED_ROWS);
c->set_value(1234);
er.set_allocated_success(&s);
server_->response_message(er);
er.release_success();
Expand All @@ -175,7 +178,9 @@ TEST_F(PreparedTest, prepare) {
parameters.emplace_back("timetz_data", timetz_for_test);
auto time_pointtz_for_test = std::pair<takatori::datetime::time_point, std::int32_t>{time_point_for_test, 720};
parameters.emplace_back("timestamptz_data", time_pointtz_for_test);
EXPECT_EQ(ERROR_CODE::OK, transaction->execute_statement(prepared_statement, parameters));
std::size_t num_rows{};
EXPECT_EQ(ERROR_CODE::OK, transaction->execute_statement(prepared_statement, parameters, num_rows));
EXPECT_EQ(num_rows, 1234);

// verify request message
std::optional<jogasaki::proto::sql::request::Request> request_opt = server_->request_message();
Expand Down

0 comments on commit a2ae38d

Please sign in to comment.