From 28dac2ad8d72381db5ded49c24fa3f08e0622610 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Tue, 17 Sep 2024 16:00:25 +0500 Subject: [PATCH] To prevent rows sent from being considered as affected rows, we avoid extracting affected rows for SELECT queries (except SELECT INTO). --- include/PgSQL_Protocol.h | 4 ++-- lib/PgSQL_Protocol.cpp | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/PgSQL_Protocol.h b/include/PgSQL_Protocol.h index 33ec2622f..fdc095f52 100644 --- a/include/PgSQL_Protocol.h +++ b/include/PgSQL_Protocol.h @@ -201,7 +201,7 @@ public: unsigned int add_row_description(const PGresult* result); unsigned int add_row(const PGresult* result); unsigned int add_row(const PSresult* result); - unsigned int add_command_completion(const PGresult* result); + unsigned int add_command_completion(const PGresult* result, bool extract_affected_rows = true); unsigned int add_error(const PGresult* result); unsigned int add_empty_query_response(const PGresult* result); unsigned int add_ready_status(PGTransactionStatusType txn_status); @@ -262,7 +262,7 @@ public: unsigned int copy_row_description_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result); unsigned int copy_row_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result); - unsigned int copy_command_completion_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result); + unsigned int copy_command_completion_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result, bool extract_affected_rows); unsigned int copy_error_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result); unsigned int copy_empty_query_response_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result); unsigned int copy_ready_status_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, PGTransactionStatusType txn_status); diff --git a/lib/PgSQL_Protocol.cpp b/lib/PgSQL_Protocol.cpp index a1a535c10..40ff303e9 100644 --- a/lib/PgSQL_Protocol.cpp +++ b/lib/PgSQL_Protocol.cpp @@ -1441,7 +1441,8 @@ unsigned int PgSQL_Protocol::copy_row_to_PgSQL_Query_Result(bool send, PgSQL_Que return total_size; } -unsigned int PgSQL_Protocol::copy_command_completion_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result) { +unsigned int PgSQL_Protocol::copy_command_completion_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result, + bool extract_affected_rows) { assert(pg_query_result); assert(result); @@ -1478,10 +1479,14 @@ unsigned int PgSQL_Protocol::copy_command_completion_to_PgSQL_Query_Result(bool pg_query_result->PSarrayOUT.add(_ptr, size); } pg_query_result->pkt_count++; - const char* extracted_affect_rows = PQcmdTuples(const_cast(result)); - if (*extracted_affect_rows) - pg_query_result->affected_rows = strtoull(extracted_affect_rows, NULL, 10); + // To prevent rows sent from being considered as affected rows, + // we avoid extracting affected rows for SELECT queries. + if (extract_affected_rows) { + const char* extracted_affect_rows = PQcmdTuples(const_cast(result)); + if (*extracted_affect_rows) + pg_query_result->affected_rows = strtoull(extracted_affect_rows, NULL, 10); + } return size; } @@ -1912,8 +1917,8 @@ unsigned long long PgSQL_Query_Result::current_size() { return intsize; } -unsigned int PgSQL_Query_Result::add_command_completion(const PGresult* result) { - const unsigned int bytes = proto->copy_command_completion_to_PgSQL_Query_Result(false, this, result); +unsigned int PgSQL_Query_Result::add_command_completion(const PGresult* result, bool extract_affected_rows) { + const unsigned int bytes = proto->copy_command_completion_to_PgSQL_Query_Result(false, this, result, extract_affected_rows); result_packet_type |= PGSQL_QUERY_RESULT_COMMAND; /*if (affected_rows) { myds->sess->CurrentQuery.have_affected_rows = true; // if affected rows is set, last_insert_id is set too