1 /***********************************************************************************************************************************
2 PostgreSQL Client
3 
4 Connect to a PostgreSQL database and run queries.  This is not intended to be a general purpose client but is suitable for
5 pgBackRest's limited needs.  In particular, data type support is limited to text, int, and bool types so it may be necessary to add
6 casts to queries to output one of these types.
7 ***********************************************************************************************************************************/
8 #ifndef POSTGRES_QUERY_H
9 #define POSTGRES_QUERY_H
10 
11 #include "common/type/object.h"
12 #include "common/type/string.h"
13 #include "common/type/variantList.h"
14 #include "common/time.h"
15 
16 /***********************************************************************************************************************************
17 Object type
18 ***********************************************************************************************************************************/
19 typedef struct PgClient PgClient;
20 
21 /***********************************************************************************************************************************
22 Constructors
23 ***********************************************************************************************************************************/
24 PgClient *pgClientNew(
25     const String *host, const unsigned int port, const String *database, const String *user, const TimeMSec queryTimeout);
26 
27 /***********************************************************************************************************************************
28 Functions
29 ***********************************************************************************************************************************/
30 // Open connection to PostgreSQL
31 PgClient *pgClientOpen(PgClient *this);
32 
33 // Move to a new parent mem context
34 __attribute__((always_inline)) static inline PgClient *
pgClientMove(PgClient * const this,MemContext * const parentNew)35 pgClientMove(PgClient *const this, MemContext *const parentNew)
36 {
37     return objMove(this, parentNew);
38 }
39 
40 // Execute a query and return results
41 VariantList *pgClientQuery(PgClient *this, const String *query);
42 
43 // Close connection to PostgreSQL
44 void pgClientClose(PgClient *this);
45 
46 /***********************************************************************************************************************************
47 Destructor
48 ***********************************************************************************************************************************/
49 __attribute__((always_inline)) static inline void
pgClientFree(PgClient * const this)50 pgClientFree(PgClient *const this)
51 {
52     objFree(this);
53 }
54 
55 /***********************************************************************************************************************************
56 Macros for function logging
57 ***********************************************************************************************************************************/
58 String *pgClientToLog(const PgClient *this);
59 
60 #define FUNCTION_LOG_PG_CLIENT_TYPE                                                                                                \
61     PgClient *
62 #define FUNCTION_LOG_PG_CLIENT_FORMAT(value, buffer, bufferSize)                                                                   \
63     FUNCTION_LOG_STRING_OBJECT_FORMAT(value, pgClientToLog, buffer, bufferSize)
64 
65 #endif
66