-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.c
160 lines (132 loc) · 4.2 KB
/
misc.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
#include <stdio.h>
#include <postgres.h>
#include <fmgr.h>
#include <foreign/foreign.h>
#include <catalog/pg_type.h>
#include <utils/builtins.h>
#include "dns.h"
#include "misc.h"
#include "fdw.h"
char *get_relation_hosted_zone_id(Oid relation_id) {
ForeignTable *ft = GetForeignTable(relation_id);
ListCell *lcopt;
foreach(lcopt, ft->options) {
DefElem *opt = (DefElem *) lfirst(lcopt);
Value *optval = (Value *) opt->arg;
if (strcmp(opt->defname, "hosted_zone_id") == 0) {
return optval->val.str;
}
}
elog(ERROR, "Missing hosted_zone_id option in foreign table defintion");
}
List *get_column_positions(TupleDesc td) {
List *res = NIL;
for (int attnum = 0; attnum < td->natts; attnum++) {
r53dbColumnPosition *cpos = (r53dbColumnPosition *) palloc0(sizeof(r53dbColumnPosition));
int expected_atttypid = -1;
Form_pg_attribute attr = TupleDescAttr(td, attnum);
if (attr->attisdropped) continue;
char *attname = NameStr(attr->attname);
if (strcmp(attname, "name") == 0) {
cpos->column = name;
cpos->position = attnum;
expected_atttypid = TEXTOID;
} else if (strcmp(attname, "type") == 0) {
cpos->column = type;
cpos->position = attnum;
expected_atttypid = TEXTOID;
} else if (strcmp(attname, "ttl") == 0) {
cpos->column = ttl;
cpos->position = attnum;
expected_atttypid = INT4OID;
} else if (strcmp(attname, "data") == 0) {
cpos->column = data;
cpos->position = attnum;
expected_atttypid = TEXTOID;
} else if (strcmp(attname, "at_dns_name") == 0) {
cpos->column = at_dns_name;
cpos->position = attnum;
expected_atttypid = TEXTOID;
} else if (strcmp(attname, "at_hosted_zone_id") == 0) {
cpos->column = at_hosted_zone_id;
cpos->position = attnum;
expected_atttypid = TEXTOID;
} else if (strcmp(attname, "at_evaluate_target_health") == 0) {
cpos->column = at_evaluate_target_health;
cpos->position = attnum;
expected_atttypid = BOOLOID;
} else {
elog(ERROR, "invalid column name %s in table definition", attname);
return NIL;
}
if (attr->atttypid != expected_atttypid) {
elog(FATAL, "invalid data type for column %s", attname);
return NIL;
}
res = lappend(res, cpos);
}
return res;
}
r53dbDNSRR *get_rr_from_values(Datum *values, bool *isnulls, List *column_positions) {
r53dbDNSRR *rr = palloc0(sizeof(r53dbDNSRR));
ListCell *lc;
foreach(lc, column_positions) {
r53dbColumnPosition *cp = (r53dbColumnPosition *) lfirst(lc);
if (isnulls[cp->position]) continue;
Datum value = values[cp->position];
switch (cp->column) {
case name: rr->name = TextDatumGetCString(value); break;
case type: rr->type = TextDatumGetCString(value); break;
case ttl: rr->ttl = DatumGetInt32(value); break;
case data: rr->data = TextDatumGetCString(value); break;
case at_dns_name: rr->at_dns_name = TextDatumGetCString(value); break;
case at_hosted_zone_id: rr->at_hosted_zone_id = TextDatumGetCString(value); break;
case at_evaluate_target_health: rr->at_evaluate_target_health = DatumGetBool(value); break;
default:
elog(ERROR, "Internal error: Invalid column %d in column list", cp->column);
}
}
return rr;
}
/*
* The C Strings within our structs will have been allocated by Go's
* C.CString(), which malloc()s memory in C land. That needs to
* be freed at some point.
*
* Use palloc_string() to re-map all those malloc()ed C Strings to
* palloc()ed C Strings and to free the malloc()s.
*
* It would be nice to find a less wasteful way to allocate with palloc()
* in the first place.
*/
void palloc_string(char **s) {
if (*s == NULL) return;
char *new = (char *) palloc(strlen(*s) + 1);
strcpy(new, *s);
free(*s);
*s = new;
}
void make_dns_identifier(char *s) {
while (*s != '\0') {
if (s[0] == '.' && s[1] == '\0') {
// remove final '.'
s[0] = '\0';
} else if (!((*s >= 'a' && *s <= 'z') || (*s >= '0' && *s <= '9'))) {
// make every non-[a-z0-9] into a _
*s = '_';
}
s++;
}
}
Datum CStringGetTextDatumOrNULL(char *s) {
char *t = (s != NULL) ? s : "(NULL)";
return CStringGetTextDatum(t);
}
PG_FUNCTION_INFO_V1(r53db_hi);
Datum r53db_hi(PG_FUNCTION_ARGS) {
PG_RETURN_TEXT_P(cstring_to_text("ho"));
}
PG_FUNCTION_INFO_V1(r53db_42);
Datum r53db_42(PG_FUNCTION_ARGS) {
PG_RETURN_INT32(42);
}