This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_route.c
225 lines (180 loc) · 7.14 KB
/
table_route.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
#include "unistd.h"
#include "postgres.h"
/* These are always necessary for a bgworker */
#include "miscadmin.h"
/* these headers are used by this particular worker's code */
#include "access/xact.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "pgstat.h"
#include "utils/builtins.h"
#include "utils/snapmgr.h"
#include "utils/memutils.h"
#include "tcop/utility.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "catalog/indexing.h"
#include "utils/fmgroids.h"
#include "access/htup_details.h"
#include "storage/lmgr.h"
#include "mb/pg_wchar.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
#include <event2/http.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
#include "routes.h"
#include "jsonbuf.h"
#include "util.h"
#include "content_types.h"
void
jsonbuf_add_table_info(struct jsonbuf *jp, Oid oid, const char *uri, Form_pg_class form);
void
jsonbuf_add_column_info(struct jsonbuf *jp, Oid oid, const char *uri, Form_pg_attribute form);
void
jsonbuf_add_column_info(struct jsonbuf *jp, Oid oid, const char *uri, Form_pg_attribute form)
{
char *typeUri;
HeapTuple typeTup = NULL;
Form_pg_type typeForm = NULL;
jsonbuf_member_cstring(jp, "name", NameStr(form->attname));
if(OidIsValid(form->atttypid) && HeapTupleIsValid(typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(form->atttypid)))) {
typeForm = (Form_pg_type) GETSTRUCT(typeTup);
jsonbuf_member_cstring(jp, "type", NameStr(typeForm->typname));
}
jsonbuf_member_bool(jp, "notnull", form->attnotnull);
if(form->attlen > 0)
jsonbuf_member_int(jp, "length", form->attlen);
jsonbuf_member_start_array(jp, "links");
jsonbuf_element_link(jp, "self", TABLE_COLUMN_METADATA_TYPE_V1, uri);
if(typeForm != NULL) {
typeUri = pstr_uri_append_path_component("/types", NameStr(typeForm->typname));
jsonbuf_element_link(jp, "type", TYPE_METADATA_TYPE_V1, typeUri);
pfree(typeUri);
ReleaseSysCache(typeTup);
}
jsonbuf_end_array(jp);
}
void
jsonbuf_add_table_info(struct jsonbuf *jp, Oid oid, const char *uri, Form_pg_class form)
{
Relation arel;
ScanKeyData akey[2];
SysScanDesc ascan;
HeapTuple atup;
Form_pg_attribute attForm;
char *columnUri;
char *columnsUri;
char *rowsUri;
columnsUri = psprintf("%s/columns", uri);
rowsUri = psprintf("%s/rows", uri);
jsonbuf_member_cstring(jp, "name", NameStr(form->relname));
jsonbuf_member_cstring(jp, "oid", psprintf("%u", oid));
jsonbuf_member_int(jp, "pages", form->relpages);
// TODO reltuples, relallvisible, reltoastrelid
jsonbuf_member_bool(jp, "hasindex", form->relhasindex);
jsonbuf_member_bool(jp, "isshared", form->relisshared);
switch(form->relpersistence)
{
case RELPERSISTENCE_PERMANENT: jsonbuf_member_cstring(jp, "persistence", "permanent"); break;
case RELPERSISTENCE_UNLOGGED: jsonbuf_member_cstring(jp, "persistence", "unlogged"); break;
case RELPERSISTENCE_TEMP: jsonbuf_member_cstring(jp, "persistence", "temp"); break;
default: break;
}
switch(form->relkind)
{
case RELKIND_RELATION: jsonbuf_member_cstring(jp, "kind", "relation"); break;
case RELKIND_INDEX: jsonbuf_member_cstring(jp, "kind", "index"); break;
case RELKIND_SEQUENCE: jsonbuf_member_cstring(jp, "kind", "sequence"); break;
case RELKIND_TOASTVALUE: jsonbuf_member_cstring(jp, "kind", "toastvalue"); break;
case RELKIND_VIEW: jsonbuf_member_cstring(jp, "kind", "view"); break;
case RELKIND_COMPOSITE_TYPE: jsonbuf_member_cstring(jp, "kind", "composite_type"); break;
case RELKIND_FOREIGN_TABLE: jsonbuf_member_cstring(jp, "kind", "foreign_table"); break;
case RELKIND_MATVIEW: jsonbuf_member_cstring(jp, "kind", "matview"); break;
default: break;
}
jsonbuf_member_start_array(jp, "columns");
arel = heap_open(AttributeRelationId, AccessShareLock);
ScanKeyInit(&akey[0],
Anum_pg_attribute_attrelid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(oid));
ScanKeyInit(&akey[1],
Anum_pg_attribute_attnum,
BTGreaterStrategyNumber, F_INT2GT,
Int16GetDatum(0));
ascan = systable_beginscan(arel, AttributeRelidNumIndexId, true, NULL, 2, akey);
while (HeapTupleIsValid(atup = systable_getnext(ascan)))
{
attForm = (Form_pg_attribute) GETSTRUCT(atup);
columnUri = pstr_uri_append_path_component(columnsUri, NameStr(attForm->attname));
jsonbuf_element_start_object(jp);
jsonbuf_add_column_info(jp, HeapTupleGetOid(atup), columnUri, attForm);
jsonbuf_end_object(jp);
pfree(columnUri);
}
systable_endscan(ascan);
heap_close(arel, AccessShareLock);
jsonbuf_end_array(jp);
jsonbuf_member_start_array(jp, "links");
jsonbuf_element_link(jp, "self", TABLE_METADATA_TYPE_V1, uri);
if(OidIsValid(form->reltype))
jsonbuf_element_link(jp, "columns", TABLE_COLUMN_LIST_TYPE_V1, columnsUri);
jsonbuf_element_link(jp, "rows", TABLE_ROW_LIST_TYPE_V1, rowsUri);
jsonbuf_element_role_link(jp, "owner", form->relowner);
jsonbuf_add_tablespace_link(jp, "tablespace", form->reltablespace);
jsonbuf_end_array(jp);
// TODO reltype, reloftype, relam, relfilename, relnatts
jsonbuf_member_int(jp, "checks", (int)form->relchecks);
jsonbuf_member_bool(jp, "hasoids", form->relhasoids);
jsonbuf_member_bool(jp, "haspkey", form->relhaspkey);
jsonbuf_member_bool(jp, "hasrules", form->relhasrules);
jsonbuf_member_bool(jp, "hastriggers", form->relhastriggers);
jsonbuf_member_bool(jp, "hassubclass", form->relhassubclass);
jsonbuf_member_bool(jp, "ispopulated", form->relispopulated);
switch(form->relreplident)
{
case REPLICA_IDENTITY_DEFAULT: jsonbuf_member_cstring(jp, "replident", "default"); break;
case REPLICA_IDENTITY_NOTHING: jsonbuf_member_cstring(jp, "replident", "nothing"); break;
case REPLICA_IDENTITY_FULL: jsonbuf_member_cstring(jp, "replident", "full"); break;
case REPLICA_IDENTITY_INDEX: jsonbuf_member_cstring(jp, "replident", "index"); break;
default: break;
}
jsonbuf_member_int(jp, "frozenxid", form->relfrozenxid);
jsonbuf_member_int(jp, "minmxid", form->relminmxid);
// TODO acl's ?
pfree(columnsUri);
}
void
table_route_GET(struct restgres_request *req)
{
struct jsonbuf *jp = req->jsonbuf;
const char *tablename = evhttp_find_header(req->matchdict, "tablename");
const char *schemaname = evhttp_find_header(req->matchdict, "schemaname");
Oid schemaOid = GetSysCacheOid1(NAMESPACENAME, PointerGetDatum(schemaname));
HeapTuple tabletup;
Form_pg_class form;
if (!OidIsValid(schemaOid))
{
elog(ERROR, "lookup failed for schema '%s' in database '%s'", schemaname, evhttp_find_header(req->matchdict, "dbname"));
req->status_code = 404;
return;
}
tabletup = SearchSysCache2(RELNAMENSP, PointerGetDatum(tablename), ObjectIdGetDatum(schemaOid));
if (!HeapTupleIsValid(tabletup))
{
elog(ERROR, "lookup failed for table '%s' in schema '%s' in database '%s'", tablename, schemaname, evhttp_find_header(req->matchdict, "dbname"));
req->status_code = 404;
return;
}
form = (Form_pg_class) GETSTRUCT(tabletup);
add_json_content_type_header(req->reply_headers);
jsonbuf_start_document(jp);
jsonbuf_member_start_object(jp, "table");
jsonbuf_add_table_info(jp, HeapTupleGetOid(tabletup), evhttp_uri_get_path(req->uri), form);
jsonbuf_end_object(jp);
jsonbuf_end_document(jp);
ReleaseSysCache(tabletup);
}