Skip to content

Commit

Permalink
Merge branch 'psql-affected-rows-simplify'
Browse files Browse the repository at this point in the history
Simplify affected rows handling in PostgreSQL backend.

See #1202.
  • Loading branch information
vadz committed Jan 30, 2025
2 parents b13f5aa + 927c0c3 commit 72f9616
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/backends/postgresql/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define SOCI_POSTGRESQL_SOURCE
#include "soci/postgresql/soci-postgresql.h"
#include "soci/soci-platform.h"
#include "soci-cstrtoi.h"
#include <libpq/libpq-fs.h> // libpq
#include <cctype>
#include <cstdio>
Expand Down Expand Up @@ -304,7 +305,8 @@ postgresql_statement_backend::execute(int number)
"Binding for use elements must be either by position "
"or by name.");
}
long long rowsAffectedBulkTemp = 0;

rowsAffectedBulk_ = 0;
for (int i = 0; i != numberOfExecutions; ++i)
{
std::vector<char *> paramValues;
Expand Down Expand Up @@ -417,15 +419,11 @@ postgresql_statement_backend::execute(int number)
{
// there are only bulk use elements (no intos)

// preserve the number of rows affected so far.
rowsAffectedBulk_ = rowsAffectedBulkTemp;

result_.check_for_errors("Cannot execute query.");

rowsAffectedBulkTemp += get_affected_rows();
rowsAffectedBulk_ += get_affected_rows();
}
}
rowsAffectedBulk_ = rowsAffectedBulkTemp;

if (numberOfExecutions > 1)
{
Expand Down Expand Up @@ -649,21 +647,11 @@ long long postgresql_statement_backend::get_affected_rows()
{
// PQcmdTuples() doesn't really modify the result but it takes a non-const
// pointer to it, so we can't rely on implicit conversion here.
const char * const resultStr = PQcmdTuples(result_.get_result());
char * end;
long long result = std::strtoll(resultStr, &end, 0);
if (end != resultStr)
{
long long result;
if (cstring_to_integer(result, PQcmdTuples(result_.get_result())))
return result;
}
else if (rowsAffectedBulk_ >= 0)
{
return rowsAffectedBulk_;
}
else
{
return -1;
}

return rowsAffectedBulk_;
}

int postgresql_statement_backend::get_number_of_rows()
Expand Down

0 comments on commit 72f9616

Please sign in to comment.