Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/***********************************************************************************************************************************
PostgreSQL Client
 
Connect to a PostgreSQL database and run queries. This is not intended to be a general purpose client but is suitable for
pgBackRest's limited needs. In particular, data type support is limited to text, int, and bool types so it may be necessary to add
casts to queries to output one of these types.
***********************************************************************************************************************************/
#ifndef POSTGRES_QUERY_H
#define POSTGRES_QUERY_H
 
#include "common/time.h"
#include "common/type/object.h"
#include "common/type/pack.h"
#include "common/type/string.h"
 
/***********************************************************************************************************************************
Query result types
***********************************************************************************************************************************/
typedef enum
{
    pgClientQueryResultAny = STRID5("any", 0x65c10),                // Any number of rows/columns expected (even none)
    pgClientQueryResultRow = STRID5("row", 0x5df20),                // One row expected
    pgClientQueryResultColumn = STRID5("column", 0x1cdab1e30),      // One row/column expected
    pgClientQueryResultNone = STRID5("none", 0x2b9ee0),             // No rows expected
} PgClientQueryResult;
 
/***********************************************************************************************************************************
Object type
***********************************************************************************************************************************/
typedef struct PgClient PgClient;
 
/***********************************************************************************************************************************
Constructors
***********************************************************************************************************************************/
FN_EXTERN PgClient *pgClientNew(
    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
FN_INLINE_ALWAYS const String *
pgClientHost(const PgClient *const this)
{
    return THIS_PUB(PgClient)->host;
}
 
// Pg port
FN_INLINE_ALWAYS unsigned int
pgClientPort(const PgClient *const this)
{
    return THIS_PUB(PgClient)->port;
}
 
// Pg database
FN_INLINE_ALWAYS const String *
pgClientDatabase(const PgClient *const this)
{
    return THIS_PUB(PgClient)->database;
}
 
// Pg user
FN_INLINE_ALWAYS const String *
pgClientUser(const PgClient *const this)
{
    return THIS_PUB(PgClient)->user;
}
 
// Timeout for statements/queries
FN_INLINE_ALWAYS TimeMSec
pgClientTimeout(const PgClient *const this)
{
    return THIS_PUB(PgClient)->timeout;
}
 
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
// Open connection to PostgreSQL
FN_EXTERN PgClient *pgClientOpen(PgClient *this);
 
// Move to a new parent mem context
FN_INLINE_ALWAYS PgClient *
pgClientMove(PgClient *const this, MemContext *const parentNew)
{
    return objMove(this, parentNew);
}
 
// Execute a query and return results
FN_EXTERN Pack *pgClientQuery(PgClient *this, const String *query, PgClientQueryResult resultType);
 
/***********************************************************************************************************************************
Destructor
***********************************************************************************************************************************/
FN_INLINE_ALWAYS void
pgClientFree(PgClient *const this)
{
    objFree(this);
}
 
/***********************************************************************************************************************************
Macros for function logging
***********************************************************************************************************************************/
FN_EXTERN void pgClientToLog(const PgClient *this, StringStatic *debugLog);
 
#define FUNCTION_LOG_PG_CLIENT_TYPE                                                                                                \
    PgClient *
#define FUNCTION_LOG_PG_CLIENT_FORMAT(value, buffer, bufferSize)                                                                   \
    FUNCTION_LOG_OBJECT_FORMAT(value, pgClientToLog, buffer, bufferSize)
 
#endif