mirror of
https://github.com/allankoechke/soci.git
synced 2026-11-30 10:58:24 +00:00
Add "tracefile" connection parameter to PostgreSQL backend
Using this parameter allows to enable tracing of all database calls, which can be useful for diagnostic and profiling purposes. Also add fopen() wrapper to avoid "deprecation" warnings from MSVC.
This commit is contained in:
@@ -44,19 +44,20 @@ session sql("postgresql", "dbname=mydatabase");
|
||||
session sql("postgresql://dbname=mydatabase");
|
||||
```
|
||||
|
||||
The set of parameters used in the connection string for PostgreSQL is the same as accepted by the [PQconnectdb](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNECTDB) function from the `libpq` library.
|
||||
The set of parameters used in the connection string for PostgreSQL is the same as accepted by the [PQconnectdb](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNECTDB) function from the `libpq` library, however in addition to standard PostgreSQL connection parameters, SOCI PostgreSQL backend accepts a couple of additional ones:
|
||||
|
||||
In addition to standard PostgreSQL connection parameters, the following can be set:
|
||||
* `tracefile`: if specified, enables tracing all database activity using [PQtrace()](https://www.postgresql.org/docs/current/libpq-control.html#LIBPQ-PQTRACE) into the specified file. Note that this file is appended to if it already exists.
|
||||
* `singlerow` or `singlerows`: if set to `true` or `yes`, enables single-row mode for the session (see below).
|
||||
|
||||
* `singlerow` or `singlerows`
|
||||
#### Single Row Mode
|
||||
|
||||
For example:
|
||||
If this mode is enabled, e.g.
|
||||
|
||||
```cpp
|
||||
session sql(postgresql, "dbname=mydatabase singlerows=true");
|
||||
```
|
||||
|
||||
If the `singlerows` parameter is set to `true` or `yes`, then queries will be executed in the single-row mode, which prevents the client library from loading full query result sets into memory and instead fetches rows one by one, as they are requested by the statement's fetch() function. This mode can be of interest to those users who want to make their client applications more responsive (with more fine-grained operation) by avoiding potentially long blocking times when complete query results are loaded to client's memory.
|
||||
then queries will be executed in the single-row mode, which prevents the client library from loading full query result sets into memory and instead fetches rows one by one, as they are requested by the statement's fetch() function. This mode can be of interest to those users who want to make their client applications more responsive (with more fine-grained operation) by avoiding potentially long blocking times when complete query results are loaded to client's memory.
|
||||
Note that in the single-row operation:
|
||||
|
||||
* bulk queries are not supported, and
|
||||
|
||||
@@ -423,6 +423,9 @@ struct SOCI_POSTGRESQL_DECL postgresql_session_backend : details::session_backen
|
||||
bool single_row_mode_;
|
||||
PGconn * conn_;
|
||||
connection_parameters connectionParameters_;
|
||||
|
||||
// File used for tracing or null if none.
|
||||
FILE* traceFile_ = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
@@ -123,6 +124,13 @@ namespace soci
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
inline FILE* fopen(const char* filename, const char* mode)
|
||||
{
|
||||
FILE* file = nullptr;
|
||||
fopen_s(&file, filename, mode);
|
||||
return file;
|
||||
}
|
||||
|
||||
inline const char* getenv(const char* name)
|
||||
{
|
||||
#pragma warning(push)
|
||||
@@ -152,6 +160,7 @@ inline char* strncpy(char* dest, const char* src, size_t n)
|
||||
|
||||
#else // !_MSC_VER
|
||||
|
||||
using std::fopen;
|
||||
using std::getenv;
|
||||
using std::sscanf;
|
||||
using std::strncpy;
|
||||
|
||||
@@ -188,6 +188,17 @@ void postgresql_session_backend::connect(
|
||||
single_row_mode_ = connection_parameters::is_true_value(name, value);
|
||||
}
|
||||
|
||||
if (params.extract_option("tracefile", value))
|
||||
{
|
||||
traceFile_ = fopen(value.c_str(), "a");
|
||||
if (!traceFile_)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Cannot open database trace file: \"" << value << "\".";
|
||||
throw soci_error(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
// We can't use SOCI connection string with PQconnectdb() directly because
|
||||
// libpq uses single quotes instead of double quotes used by SOCI.
|
||||
PGconn* conn = PQconnectdb(params.build_string_from_options('\'').c_str());
|
||||
@@ -204,6 +215,11 @@ void postgresql_session_backend::connect(
|
||||
throw soci_error(msg);
|
||||
}
|
||||
|
||||
if (traceFile_)
|
||||
{
|
||||
PQtrace(conn, traceFile_);
|
||||
}
|
||||
|
||||
// Increase the number of digits used for floating point values to ensure
|
||||
// that the conversions to/from text round trip correctly, which is not the
|
||||
// case with the default value of 0. Use the maximal supported value, which
|
||||
@@ -276,6 +292,12 @@ void postgresql_session_backend::clean_up()
|
||||
PQfinish(conn_);
|
||||
conn_ = 0;
|
||||
}
|
||||
|
||||
if (traceFile_)
|
||||
{
|
||||
std::fclose(traceFile_);
|
||||
traceFile_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::string postgresql_session_backend::get_next_statement_name()
|
||||
|
||||
Reference in New Issue
Block a user