You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-07-05 00:28:52 +02:00
Refactor postgres/client module with inline getters/setters.
Extend the pattern introduced in 79a2d02
to the postgres/client module.
This commit is contained in:
@ -16,13 +16,8 @@ Object type
|
|||||||
***********************************************************************************************************************************/
|
***********************************************************************************************************************************/
|
||||||
struct PgClient
|
struct PgClient
|
||||||
{
|
{
|
||||||
const String *host;
|
PgClientPub pub; // Publicly accessible variables
|
||||||
unsigned int port;
|
PGconn *connection; // Pg connection
|
||||||
const String *database;
|
|
||||||
const String *user;
|
|
||||||
TimeMSec queryTimeout;
|
|
||||||
|
|
||||||
PGconn *connection;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/***********************************************************************************************************************************
|
/***********************************************************************************************************************************
|
||||||
@ -46,14 +41,14 @@ pgClientFreeResource(THIS_VOID)
|
|||||||
|
|
||||||
/**********************************************************************************************************************************/
|
/**********************************************************************************************************************************/
|
||||||
PgClient *
|
PgClient *
|
||||||
pgClientNew(const String *host, const unsigned int port, const String *database, const String *user, const TimeMSec queryTimeout)
|
pgClientNew(const String *host, const unsigned int port, const String *database, const String *user, const TimeMSec timeout)
|
||||||
{
|
{
|
||||||
FUNCTION_LOG_BEGIN(logLevelDebug);
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
||||||
FUNCTION_LOG_PARAM(STRING, host);
|
FUNCTION_LOG_PARAM(STRING, host);
|
||||||
FUNCTION_LOG_PARAM(UINT, port);
|
FUNCTION_LOG_PARAM(UINT, port);
|
||||||
FUNCTION_LOG_PARAM(STRING, database);
|
FUNCTION_LOG_PARAM(STRING, database);
|
||||||
FUNCTION_LOG_PARAM(STRING, user);
|
FUNCTION_LOG_PARAM(STRING, user);
|
||||||
FUNCTION_LOG_PARAM(TIME_MSEC, queryTimeout);
|
FUNCTION_LOG_PARAM(TIME_MSEC, timeout);
|
||||||
FUNCTION_LOG_END();
|
FUNCTION_LOG_END();
|
||||||
|
|
||||||
ASSERT(port >= 1 && port <= 65535);
|
ASSERT(port >= 1 && port <= 65535);
|
||||||
@ -66,12 +61,15 @@ pgClientNew(const String *host, const unsigned int port, const String *database,
|
|||||||
this = OBJ_NEW_ALLOC();
|
this = OBJ_NEW_ALLOC();
|
||||||
|
|
||||||
*this = (PgClient)
|
*this = (PgClient)
|
||||||
|
{
|
||||||
|
.pub =
|
||||||
{
|
{
|
||||||
.host = strDup(host),
|
.host = strDup(host),
|
||||||
.port = port,
|
.port = port,
|
||||||
.database = strDup(database),
|
.database = strDup(database),
|
||||||
.user = strDup(user),
|
.user = strDup(user),
|
||||||
.queryTimeout = queryTimeout,
|
.timeout = timeout,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
OBJ_NEW_END();
|
OBJ_NEW_END();
|
||||||
@ -134,15 +132,16 @@ pgClientOpen(PgClient *this)
|
|||||||
MEM_CONTEXT_TEMP_BEGIN()
|
MEM_CONTEXT_TEMP_BEGIN()
|
||||||
{
|
{
|
||||||
// Base connection string
|
// Base connection string
|
||||||
String *connInfo = strCatFmt(strNew(), "dbname=%s port=%u", strZ(pgClientEscape(this->database)), this->port);
|
String *connInfo = strCatFmt(
|
||||||
|
strNew(), "dbname=%s port=%u", strZ(pgClientEscape(pgClientDatabase(this))), pgClientPort(this));
|
||||||
|
|
||||||
// Add user if specified
|
// Add user if specified
|
||||||
if (this->user != NULL)
|
if (pgClientUser(this) != NULL)
|
||||||
strCatFmt(connInfo, " user=%s", strZ(pgClientEscape(this->user)));
|
strCatFmt(connInfo, " user=%s", strZ(pgClientEscape(pgClientUser(this))));
|
||||||
|
|
||||||
// Add host if specified
|
// Add host if specified
|
||||||
if (this->host != NULL)
|
if (pgClientHost(this) != NULL)
|
||||||
strCatFmt(connInfo, " host=%s", strZ(pgClientEscape(this->host)));
|
strCatFmt(connInfo, " host=%s", strZ(pgClientEscape(pgClientHost(this))));
|
||||||
|
|
||||||
// Make the connection
|
// Make the connection
|
||||||
this->connection = PQconnectdb(strZ(connInfo));
|
this->connection = PQconnectdb(strZ(connInfo));
|
||||||
@ -192,7 +191,7 @@ pgClientQuery(PgClient *this, const String *query)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wait for a result
|
// Wait for a result
|
||||||
Wait *wait = waitNew(this->queryTimeout);
|
Wait *wait = waitNew(pgClientTimeout(this));
|
||||||
bool busy = false;
|
bool busy = false;
|
||||||
|
|
||||||
do
|
do
|
||||||
@ -232,7 +231,7 @@ pgClientQuery(PgClient *this, const String *query)
|
|||||||
{
|
{
|
||||||
// Throw timeout error if cancelled
|
// Throw timeout error if cancelled
|
||||||
if (busy)
|
if (busy)
|
||||||
THROW_FMT(DbQueryError, "query '%s' timed out after %" PRIu64 "ms", strZ(query), this->queryTimeout);
|
THROW_FMT(DbQueryError, "query '%s' timed out after %" PRIu64 "ms", strZ(query), pgClientTimeout(this));
|
||||||
|
|
||||||
// If this was a command that returned no results then we are done
|
// If this was a command that returned no results then we are done
|
||||||
ExecStatusType resultStatus = PQresultStatus(pgResult);
|
ExecStatusType resultStatus = PQresultStatus(pgResult);
|
||||||
@ -357,6 +356,6 @@ String *
|
|||||||
pgClientToLog(const PgClient *this)
|
pgClientToLog(const PgClient *this)
|
||||||
{
|
{
|
||||||
return strNewFmt(
|
return strNewFmt(
|
||||||
"{host: %s, port: %u, database: %s, user: %s, queryTimeout %" PRIu64 "}", strZ(strToLog(this->host)), this->port,
|
"{host: %s, port: %u, database: %s, user: %s, queryTimeout %" PRIu64 "}", strZ(strToLog(pgClientHost(this))),
|
||||||
strZ(strToLog(this->database)), strZ(strToLog(this->user)), this->queryTimeout);
|
pgClientPort(this), strZ(strToLog(pgClientDatabase(this))), strZ(strToLog(pgClientUser(this))), pgClientTimeout(this));
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,54 @@ typedef struct PgClient PgClient;
|
|||||||
Constructors
|
Constructors
|
||||||
***********************************************************************************************************************************/
|
***********************************************************************************************************************************/
|
||||||
PgClient *pgClientNew(
|
PgClient *pgClientNew(
|
||||||
const String *host, const unsigned int port, const String *database, const String *user, const TimeMSec queryTimeout);
|
const String *host, const unsigned int port, const String *database, const String *user, const TimeMSec timeout);
|
||||||
|
|
||||||
|
/***********************************************************************************************************************************
|
||||||
|
Getters/Setters
|
||||||
|
***********************************************************************************************************************************/
|
||||||
|
typedef struct PgClientPub
|
||||||
|
{
|
||||||
|
const String *host; // Pg host
|
||||||
|
unsigned int port; // Pg port
|
||||||
|
const String *database; // Pg database
|
||||||
|
const String *user; // Pg user
|
||||||
|
TimeMSec timeout; // Timeout for statements/queries
|
||||||
|
} PgClientPub;
|
||||||
|
|
||||||
|
// Pg host
|
||||||
|
__attribute__((always_inline)) static inline const String *
|
||||||
|
pgClientHost(const PgClient *const this)
|
||||||
|
{
|
||||||
|
return THIS_PUB(PgClient)->host;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pg port
|
||||||
|
__attribute__((always_inline)) static inline unsigned int
|
||||||
|
pgClientPort(const PgClient *const this)
|
||||||
|
{
|
||||||
|
return THIS_PUB(PgClient)->port;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pg database
|
||||||
|
__attribute__((always_inline)) static inline const String *
|
||||||
|
pgClientDatabase(const PgClient *const this)
|
||||||
|
{
|
||||||
|
return THIS_PUB(PgClient)->database;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pg user
|
||||||
|
__attribute__((always_inline)) static inline const String *
|
||||||
|
pgClientUser(const PgClient *const this)
|
||||||
|
{
|
||||||
|
return THIS_PUB(PgClient)->user;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timeout for statements/queries
|
||||||
|
__attribute__((always_inline)) static inline TimeMSec
|
||||||
|
pgClientTimeout(const PgClient *const this)
|
||||||
|
{
|
||||||
|
return THIS_PUB(PgClient)->timeout;
|
||||||
|
}
|
||||||
|
|
||||||
/***********************************************************************************************************************************
|
/***********************************************************************************************************************************
|
||||||
Functions
|
Functions
|
||||||
|
Reference in New Issue
Block a user