-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_semver.c
316 lines (261 loc) · 7.91 KB
/
pg_semver.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "semver.c/semver.h"
#include "semver.c/semver.c"
#define ERR_INVALID_VERSION "Invalid Semver '%s'."
#define ERR_INVALID_VERSION_MSG "Semver '%s' contains invalid character."
#define ERR_INVALID_VERSION_HINT "Did you mean '%s'?"
#define ERR_INVALID_BUMP "Invalid bump number '%d'."
#define ERR_INVALID_BUMP_MSG "Bump number should be between 0 and 2."
#define ERR_INVALID_BUMP_HINT "Use bump number between 0 and 2."
PG_MODULE_MAGIC;
bool pg_semver_op(char *v1, char *v2, char *operator);
bool pg_semver_op(char *v1, char *v2, char *operator) {
bool resolution;
semver_t v_1 = {};
semver_t v_2 = {};
semver_parse(v1, &v_1);
semver_parse(v2, &v_2);
resolution = semver_satisfies(v_1, v_2, operator);
semver_free(&v_1);
semver_free(&v_2);
return resolution;
}
PG_FUNCTION_INFO_V1(pg_semver_in);
Datum pg_semver_in(PG_FUNCTION_ARGS) {
char vc[1000];
char *v1 = PG_GETARG_CSTRING(0);
text *t = cstring_to_text(v1);
if (!semver_is_valid(v1)) {
strcpy(vc, v1);
semver_clean(vc);
ereport(ERROR,
(
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg(ERR_INVALID_VERSION , v1),
errdetail(ERR_INVALID_VERSION_MSG, v1),
errhint(ERR_INVALID_VERSION_HINT, vc)
)
);
}
PG_RETURN_CSTRING(t);
}
PG_FUNCTION_INFO_V1(pg_semver_out);
Datum pg_semver_out(PG_FUNCTION_ARGS) {
char *v1, vc[1000];
text *t = PG_GETARG_TEXT_PP(0);
v1 = text_to_cstring(t);
if (!semver_is_valid(v1)) {
strcpy(vc, v1);
semver_clean(vc);
ereport(ERROR,
(
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg(ERR_INVALID_VERSION , v1),
errdetail(ERR_INVALID_VERSION_MSG, v1),
errhint(ERR_INVALID_VERSION_HINT, vc)
)
);
}
PG_RETURN_CSTRING(v1);
}
PG_FUNCTION_INFO_V1(pg_semver_cmp);
Datum pg_semver_cmp(PG_FUNCTION_ARGS) {
int resolution;
char *v1, *v2, vc1[1000], vc2[1000];
semver_t v_1 = {};
semver_t v_2 = {};
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
if (!semver_is_valid(v1)) {
strcpy(vc1, v1);
semver_clean(vc1);
ereport(ERROR,
(
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg(ERR_INVALID_VERSION , v1),
errdetail(ERR_INVALID_VERSION_MSG, v1),
errhint(ERR_INVALID_VERSION_HINT, vc1)
)
);
}
if (!semver_is_valid(v2)) {
strcpy(vc2, v2);
semver_clean(vc2);
ereport(ERROR,
(
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg(ERR_INVALID_VERSION , v2),
errdetail(ERR_INVALID_VERSION_MSG, v2),
errhint(ERR_INVALID_VERSION_HINT, vc2)
)
);
}
semver_parse(v1, &v_1);
semver_parse(v2, &v_2);
resolution = semver_compare(v_1, v_2);
semver_free(&v_1);
semver_free(&v_2);
PG_RETURN_INT32((int32)resolution);
}
PG_FUNCTION_INFO_V1(pg_semver_bump);
Datum pg_semver_bump(PG_FUNCTION_ARGS) {
char *v1, vc[1000], v2[1000] = {0};
semver_t v_1 = {};
text *t1 = PG_GETARG_TEXT_PP(0);
int32 type = PG_GETARG_INT32(1);
v1 = text_to_cstring(t1);
if (!semver_is_valid(v1)) {
strcpy(vc, v1);
semver_clean(vc);
ereport(ERROR,
(
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg(ERR_INVALID_VERSION , v1),
errdetail(ERR_INVALID_VERSION_MSG, v1),
errhint(ERR_INVALID_VERSION_HINT, vc)
)
);
}
semver_parse(v1, &v_1);
if (type == 0) {
semver_bump(&v_1);
semver_render(&v_1, v2);
semver_free(&v_1);
} else if (type == 1) {
semver_bump_minor(&v_1);
semver_render(&v_1, v2);
semver_free(&v_1);
} else if (type == 2) {
semver_bump_patch(&v_1);
semver_render(&v_1, v2);
semver_free(&v_1);
} else {
ereport(ERROR,
(
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg(ERR_INVALID_BUMP , type),
errdetail(ERR_INVALID_BUMP_MSG),
errhint(ERR_INVALID_BUMP_HINT)
)
);
}
PG_RETURN_CSTRING(cstring_to_text(v2));
}
PG_FUNCTION_INFO_V1(pg_semver_to_int);
Datum pg_semver_to_int(PG_FUNCTION_ARGS) {
int numeric;
char *v1, vc[1000];
semver_t v_1 = {};
text *t1 = PG_GETARG_TEXT_PP(0);
v1 = text_to_cstring(t1);
if (!semver_is_valid(v1)) {
strcpy(vc, v1);
semver_clean(vc);
ereport(ERROR,
(
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg(ERR_INVALID_VERSION , v1),
errdetail(ERR_INVALID_VERSION_MSG, v1),
errhint(ERR_INVALID_VERSION_HINT, vc)
)
);
}
semver_parse(v1, &v_1);
numeric = semver_numeric(&v_1);
semver_free(&v_1);
PG_RETURN_INT32((int32)numeric);
}
PG_FUNCTION_INFO_V1(pg_semver_eq);
Datum pg_semver_eq(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(pg_semver_op(v1, v2, "="));
}
PG_FUNCTION_INFO_V1(pg_semver_ne);
Datum pg_semver_ne(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(!pg_semver_op(v1, v2, "="));
}
PG_FUNCTION_INFO_V1(pg_semver_lt);
Datum pg_semver_lt(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(pg_semver_op(v1, v2, "<"));
}
PG_FUNCTION_INFO_V1(pg_semver_le);
Datum pg_semver_le(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(pg_semver_op(v1, v2, "<="));
}
PG_FUNCTION_INFO_V1(pg_semver_gt);
Datum pg_semver_gt(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(pg_semver_op(v1, v2, ">"));
}
PG_FUNCTION_INFO_V1(pg_semver_ge);
Datum pg_semver_ge(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(pg_semver_op(v1, v2, ">="));
}
PG_FUNCTION_INFO_V1(pg_semver_sat);
Datum pg_semver_sat(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(pg_semver_op(v1, v2, "~"));
}
PG_FUNCTION_INFO_V1(pg_semver_nsat);
Datum pg_semver_nsat(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(!pg_semver_op(v1, v2, "~"));
}
PG_FUNCTION_INFO_V1(pg_semver_car);
Datum pg_semver_car(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(pg_semver_op(v2, v1, "^"));
}
PG_FUNCTION_INFO_V1(pg_semver_ncar);
Datum pg_semver_ncar(PG_FUNCTION_ARGS) {
char *v1, *v2;
text *t1 = PG_GETARG_TEXT_PP(0);
text *t2 = PG_GETARG_TEXT_PP(1);
v1 = text_to_cstring(t1);
v2 = text_to_cstring(t2);
PG_RETURN_BOOL(!pg_semver_op(v2, v1, "^"));
}