diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp index 537626f..3264728 100644 --- a/examples/cli/main.cpp +++ b/examples/cli/main.cpp @@ -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; } diff --git a/include/ogawayama/stub/api.h b/include/ogawayama/stub/api.h index afaf78b..1b76328 100644 --- a/include/ogawayama/stub/api.h +++ b/include/ogawayama/stub/api.h @@ -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. diff --git a/src/ogawayama/stub/transaction.cpp b/src/ogawayama/stub/transaction.cpp index aa57588..6af17c5 100644 --- a/src/ogawayama/stub/transaction.cpp +++ b/src/ogawayama/stub/transaction.cpp @@ -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_; @@ -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; @@ -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(); @@ -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; @@ -349,14 +366,27 @@ Transaction::Transaction(std::unique_ptr 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 &result_set) diff --git a/src/ogawayama/stub/transactionImpl.h b/src/ogawayama/stub/transactionImpl.h index 792606f..19e09ba 100644 --- a/src/ogawayama/stub/transactionImpl.h +++ b/src/ogawayama/stub/transactionImpl.h @@ -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. diff --git a/test/ogawayama/stub/PreparedStatementTest.cpp b/test/ogawayama/stub/PreparedStatementTest.cpp index 2925c24..15d7617 100644 --- a/test/ogawayama/stub/PreparedStatementTest.cpp +++ b/test/ogawayama/stub/PreparedStatementTest.cpp @@ -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(); @@ -175,7 +178,9 @@ TEST_F(PreparedTest, prepare) { parameters.emplace_back("timetz_data", timetz_for_test); auto time_pointtz_for_test = std::pair{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 request_opt = server_->request_message();