forked from pgbackrest/pgbackrest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.c
239 lines (194 loc) · 9.19 KB
/
render.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/***********************************************************************************************************************************
Render PostgreSQL Interface
***********************************************************************************************************************************/
#include "build.auto.h"
#include "storage/posix/storage.h"
#include "build/common/render.h"
#include "build/postgres/render.h"
#include "config/config.auto.h"
/***********************************************************************************************************************************
Render interface.auto.c.inc
***********************************************************************************************************************************/
#define PG_MODULE "postgres"
#define PG_AUTO_COMMENT "PostgreSQL Interface"
static void
bldPgRenderInterfaceAutoC(const Storage *const storageRepo, const BldPg bldPg)
{
String *pg = bldHeader(PG_MODULE, PG_AUTO_COMMENT);
// PostgreSQL interfaces
// -----------------------------------------------------------------------------------------------------------------------------
for (unsigned int pgIdx = lstSize(bldPg.pgList) - 1; pgIdx < lstSize(bldPg.pgList); pgIdx--)
{
const BldPgVersion *const pgVersion = lstGet(bldPg.pgList, pgIdx);
const String *const pgVersionNoDot = strLstJoin(strLstNewSplitZ(pgVersion->version, "."), "");
const char *const versionNoDot = strZ(strCat(strCat(strNew(), pgVersionNoDot), strIdToStr(pgVersion->fork)));
strCatFmt(
pg,
"\n"
COMMENT_BLOCK_BEGIN "\n"
"%s %s interface\n"
COMMENT_BLOCK_END "\n"
"#define PG_VERSION PG_VERSION_%s\n"
"#define FORK_%s\n"
"\n",
strZ(strIdToStr(pgVersion->fork)), strZ(pgVersion->version),
strZ(pgVersionNoDot),
strZ(strUpper(strIdToStr(pgVersion->fork))));
for (unsigned int typeIdx = 0; typeIdx < strLstSize(bldPg.typeList); typeIdx++)
{
const String *const type = strLstGet(bldPg.typeList, typeIdx);
strCat(pg, bldDefineRender(type, strNewFmt("%s_%s", strZ(type), versionNoDot)));
strCatChr(pg, '\n');
}
if (!pgVersion->release)
{
strCatZ(
pg,
"\n"
"#define CATALOG_VERSION_NO_MAX\n");
}
strCatZ(
pg,
"\n"
"#include \"postgres/interface/version.intern.h\"\n"
"\n");
for (unsigned int functionIdx = 0; functionIdx < strLstSize(bldPg.functionList); functionIdx++)
strCatFmt(pg, "%s(%s);\n", strZ(strLstGet(bldPg.functionList, functionIdx)), versionNoDot);
strCatChr(pg, '\n');
for (unsigned int typeIdx = 0; typeIdx < strLstSize(bldPg.typeList); typeIdx++)
strCatFmt(pg, "#undef %s\n", strZ(strLstGet(bldPg.typeList, typeIdx)));
strCatChr(pg, '\n');
for (unsigned int defineIdx = 0; defineIdx < strLstSize(bldPg.defineList); defineIdx++)
strCatFmt(pg, "#undef %s\n", strZ(strLstGet(bldPg.defineList, defineIdx)));
strCatChr(pg, '\n');
for (unsigned int functionIdx = 0; functionIdx < strLstSize(bldPg.functionList); functionIdx++)
strCatFmt(pg, "#undef %s\n", strZ(strLstGet(bldPg.functionList, functionIdx)));
strCatChr(pg, '\n');
strCatFmt(pg, "#undef FORK_%s\n\n", strZ(strUpper(strIdToStr(pgVersion->fork))));
}
// Interface struct
// -----------------------------------------------------------------------------------------------------------------------------
strCatFmt(
pg,
"\n"
COMMENT_BLOCK_BEGIN "\n"
"PostgreSQL interface struct\n"
COMMENT_BLOCK_END "\n"
"static const PgInterface pgInterface[] =\n"
"{\n");
for (unsigned int pgIdx = lstSize(bldPg.pgList) - 1; pgIdx < lstSize(bldPg.pgList); pgIdx--)
{
const BldPgVersion *const pgVersion = lstGet(bldPg.pgList, pgIdx);
const String *const pgVersionNoDot = strLstJoin(strLstNewSplitZ(pgVersion->version, "."), "");
const char *const versionNoDot = strZ(strCat(strCat(strNew(), pgVersionNoDot), strIdToStr(pgVersion->fork)));
strCatFmt(
pg,
" {\n"
" .version = PG_VERSION_%s,\n"
" .fork = CFGOPTVAL_FORK_%s,\n"
"\n",
strZ(pgVersionNoDot), strZ(strUpper(strIdToStr(pgVersion->fork))));
for (unsigned int functionIdx = 0; functionIdx < strLstSize(bldPg.functionList); functionIdx++)
{
// Convert define name to function name
const StringList *const nameList = strLstNewSplitZ(strLstGet(bldPg.functionList, functionIdx), "_");
String *const name = strNew();
for (unsigned int nameIdx = 0; nameIdx < strLstSize(nameList); nameIdx++)
{
String *const namePart = strLower(strLstGet(nameList, nameIdx));
if (nameIdx != 0)
strFirstUpper(namePart);
strCat(name, namePart);
}
strCatFmt(
pg,
" .%s = %s%s,\n",
strZ(strFirstLower(strSub(name, sizeof("pgInterface") - 1))), strZ(name), versionNoDot);
}
strCatZ(
pg,
" },\n");
}
strCatFmt(
pg,
"};\n");
bldPut(storageRepo, "src/postgres/interface.auto.c.inc", BUFSTR(pg));
}
/**********************************************************************************************************************************/
void
bldPgRender(const Storage *const storageRepo, const BldPg bldPg)
{
bldPgRenderInterfaceAutoC(storageRepo, bldPg);
}
/***********************************************************************************************************************************
Render version.auto.h
***********************************************************************************************************************************/
#define PG_VERSION_MODULE "postgres-version"
#define PG_VERSION_AUTO_COMMENT "PostgreSQL Version"
static void
bldPgRenderVersionAutoH(const Storage *const storageRepo, const BldPg bldPg)
{
String *const pg = strCatFmt(
strNew(),
"%s"
"#ifndef POSTGRES_VERSION_AUTO_H\n"
"#define POSTGRES_VERSION_AUTO_H\n",
strZ(bldHeader(PG_VERSION_MODULE, PG_VERSION_AUTO_COMMENT)));
// PostgreSQL version numbers
// -----------------------------------------------------------------------------------------------------------------------------
strCatFmt(
pg,
"\n"
COMMENT_BLOCK_BEGIN "\n"
"PostgreSQL version constants\n"
COMMENT_BLOCK_END "\n");
for (unsigned int pgIdx = 0; pgIdx < lstSize(bldPg.pgList); pgIdx++)
{
const BldPgVersion *const pgVersion = lstGet(bldPg.pgList, pgIdx);
const char *const versionNoDot = strZ(strLstJoin(strLstNewSplitZ(pgVersion->version, "."), ""));
// Generate version number
unsigned int versionNum;
const int idxStart = strChr(pgVersion->version, '.');
if (idxStart == -1)
versionNum = cvtZToUInt(strZ(pgVersion->version)) * 10000;
else
{
versionNum =
cvtZSubNToUInt(strZ(pgVersion->version), 0, (size_t)idxStart) * 10000 +
cvtZToUInt(strZ(pgVersion->version) + (size_t)idxStart + 1) * 100;
}
// Output version number
strCatFmt(pg, "%s\n", strZ(bldDefineRender(strNewFmt("PG_VERSION_%s", versionNoDot), strNewFmt("%u", versionNum))));
// Output max version
if (pgIdx == lstSize(bldPg.pgList) - 1)
strCatFmt(pg, "\n%s\n", strZ(bldDefineRender(STRDEF("PG_VERSION_MAX"), strNewFmt("PG_VERSION_%s", versionNoDot))));
}
// PostgreSQL version strings
// -----------------------------------------------------------------------------------------------------------------------------
strCatFmt(
pg,
"\n"
COMMENT_BLOCK_BEGIN "\n"
"PostgreSQL version string constants for use in error messages\n"
COMMENT_BLOCK_END "\n");
for (unsigned int pgIdx = 0; pgIdx < lstSize(bldPg.pgList); pgIdx++)
{
const BldPgVersion *const pgVersion = lstGet(bldPg.pgList, pgIdx);
const char *const versionNoDot = strZ(strLstJoin(strLstNewSplitZ(pgVersion->version, "."), ""));
// Output version string
strCatFmt(
pg, "%s\n",
strZ(bldDefineRender(strNewFmt("PG_VERSION_%s_Z", versionNoDot), strNewFmt("\"%s\"", strZ(pgVersion->version)))));
}
strCatZ(
pg,
"\n"
"#endif\n");
bldPut(storageRepo, "src/postgres/version.auto.h", BUFSTR(pg));
}
/**********************************************************************************************************************************/
void
bldPgVersionRender(const Storage *const storageRepo, const BldPg bldPg)
{
bldPgRenderVersionAutoH(storageRepo, bldPg);
}