Commit 09c26314 authored by Vadim Zeitlin's avatar Vadim Zeitlin

Allow defining SOCI_POSTGRESQL_NOSINLGEROWMODE for PostgreSQL < 9

Since the changes of b70a4e8b, PostgreSQL
backend is not compatible with PostgreSQL < 9, contrary to the documented
supported platforms in the documentation.

Ideal would be to continue supporting the old versions by default and use
PQsetSingleRowMode() only if it's detected as being available by CMake, but
for now at least allow disabling single mode support manually.

Closes #571.
parent 4b451971
......@@ -57,6 +57,11 @@ Note that in the single-row operation:
* bulk queries are not supported, and
* in order to fulfill the expectations of the underlying client library, the complete rowset has to be exhausted before executing further queries on the same session.
Also please note that single rows mode requires PostgreSQL 9 or later, both at
compile- and run-time. If you need to support earlier versions of PostgreSQL,
you can define `SOCI_POSTGRESQL_NOSINLGEROWMODE` when building the library to
disable it.
Once you have created a `session` object as shown above, you can use it to access the database, for example:
int count;
......
......@@ -65,6 +65,12 @@ postgresql_statement_backend::postgresql_statement_backend(
hasIntoElements_(false), hasVectorIntoElements_(false),
hasUseElements_(false), hasVectorUseElements_(false)
{
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode)
{
throw soci_error("Single row mode not supported in this version of the library");
}
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
}
postgresql_statement_backend::~postgresql_statement_backend()
......@@ -218,6 +224,7 @@ void postgresql_statement_backend::prepare(std::string const & query,
// if it fails to prepare it we can't DEALLOCATE it.
std::string statementName = session_.get_next_statement_name();
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
// prepare for single-row retrieval
......@@ -233,6 +240,7 @@ void postgresql_statement_backend::prepare(std::string const & query,
wait_until_operation_complete(session_.conn_);
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row query execution
......@@ -254,11 +262,13 @@ void postgresql_statement_backend::prepare(std::string const & query,
statement_backend::exec_fetch_result
postgresql_statement_backend::execute(int number)
{
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_ && (number > 1))
{
throw soci_error("Bulk operations are not supported with single-row mode.");
}
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
// If the statement was "just described", then we know that
// it was actually executed with all the use elements
// already bound and pre-used. This means that the result of the
......@@ -356,6 +366,7 @@ postgresql_statement_backend::execute(int number)
#ifdef SOCI_POSTGRESQL_NOPREPARE
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
int result = PQsendQueryParams(
......@@ -376,6 +387,7 @@ postgresql_statement_backend::execute(int number)
}
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
result_.reset(PQexecParams(session_.conn_, query_.c_str(),
......@@ -387,6 +399,7 @@ postgresql_statement_backend::execute(int number)
{
// this query was separately prepared
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
int result = PQsendQueryPrepared(session_.conn_,
......@@ -407,6 +420,7 @@ postgresql_statement_backend::execute(int number)
}
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -421,6 +435,7 @@ postgresql_statement_backend::execute(int number)
// this query was not separately prepared and should
// be executed as a one-time query
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
int result = PQsendQueryParams(session_.conn_, query_.c_str(),
......@@ -440,6 +455,7 @@ postgresql_statement_backend::execute(int number)
}
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -483,6 +499,7 @@ postgresql_statement_backend::execute(int number)
#ifdef SOCI_POSTGRESQL_NOPREPARE
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
int result = PQsendQuery(session_.conn_, query_.c_str());
......@@ -500,6 +517,7 @@ postgresql_statement_backend::execute(int number)
}
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -510,6 +528,7 @@ postgresql_statement_backend::execute(int number)
{
// this query was separately prepared
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
int result = PQsendQueryPrepared(session_.conn_,
......@@ -528,6 +547,7 @@ postgresql_statement_backend::execute(int number)
}
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -537,6 +557,7 @@ postgresql_statement_backend::execute(int number)
}
else // stType_ == st_one_time_query
{
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
int result = PQsendQuery(session_.conn_, query_.c_str());
......@@ -554,6 +575,7 @@ postgresql_statement_backend::execute(int number)
}
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -566,6 +588,7 @@ postgresql_statement_backend::execute(int number)
}
bool process_result;
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
if (justDescribed_)
......@@ -582,6 +605,7 @@ postgresql_statement_backend::execute(int number)
process_result = result_.get_result() != NULL;
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -623,11 +647,13 @@ postgresql_statement_backend::execute(int number)
statement_backend::exec_fetch_result
postgresql_statement_backend::fetch(int number)
{
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_ && (number > 1))
{
throw soci_error("Bulk operations are not supported with single-row mode.");
}
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
// Note:
// In the multi-row mode this function does not actually fetch anything from anywhere
// - the data was already retrieved from the server in the execute()
......@@ -641,6 +667,7 @@ postgresql_statement_backend::fetch(int number)
if (currentRow_ >= numberOfRows_)
{
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
PGresult* res = PQgetResult(session_.conn_);
......@@ -667,6 +694,7 @@ postgresql_statement_backend::fetch(int number)
}
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -679,6 +707,7 @@ postgresql_statement_backend::fetch(int number)
{
if (currentRow_ + number > numberOfRows_)
{
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
rowsToConsume_ = 1;
......@@ -686,6 +715,7 @@ postgresql_statement_backend::fetch(int number)
return ef_success;
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
// default multi-row execution
......@@ -699,11 +729,13 @@ postgresql_statement_backend::fetch(int number)
}
else
{
#ifndef SOCI_POSTGRESQL_NOSINLGEROWMODE
if (single_row_mode_)
{
rowsToConsume_ = 1;
}
else
#endif // !SOCI_POSTGRESQL_NOSINLGEROWMODE
{
rowsToConsume_ = number;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment