-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.cc
314 lines (260 loc) · 8.52 KB
/
postgres.cc
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
#include "postgres.hh"
#include "config.h"
#include <iostream>
#ifndef HAVE_BOOST_REGEX
#include <regex>
#else
#include <boost/regex.hpp>
using boost::regex;
using boost::smatch;
using boost::regex_match;
#endif
using namespace std;
static regex e_timeout("ERROR: canceling statement due to statement timeout(\n|.)*");
static regex e_syntax("ERROR: syntax error at or near(\n|.)*");
bool pg_type::consistent(sqltype *rvalue)
{
pg_type *t = dynamic_cast<pg_type*>(rvalue);
if (!t) {
cerr << "unknown type: " << rvalue->name << endl;
return false;
}
switch(typtype_) {
case 'b': /* base type */
case 'c': /* composite type */
case 'd': /* domain */
case 'r': /* range */
case 'e': /* enum */
return this == t;
case 'p':
if (name == "anyarray") {
return t->typelem_ != InvalidOid;
} else if (name == "anynonarray") {
return t->typelem_ == InvalidOid;
} else if(name == "anyenum") {
return t->typtype_ == 'e';
} else if (name == "any") {
return true;
} else if (name == "anyelement") {
return t->typelem_ == InvalidOid;
} else if (name == "anyrange") {
return t->typtype_ == 'r';
} else if (name == "record") {
return t->typtype_ == 'c';
} else if (name == "cstring") {
return this == t;
} else {
return false;
}
default:
throw std::logic_error("unknown typtype");
}
}
dut_pqxx::dut_pqxx(std::string conninfo)
: c(conninfo)
{
}
void dut_pqxx::test(const std::string &stmt)
{
try {
if (reset_gucs) {
if(!c.is_open())
c.activate();
pqxx::work w(c);
w.exec("set statement_timeout to '1s';"
"set client_min_messages to 'ERROR';"
"set application_name to '" PACKAGE "::dut';");
w.commit();
reset_gucs = false;
}
pqxx::work w(c);
w.exec(stmt.c_str());
} catch (const pqxx::failure &e) {
if ((dynamic_cast<const pqxx::broken_connection *>(&e))) {
/* re-throw to outer loop to recover session. */
reset_gucs = true;
throw dut::broken(e.what());
}
if (regex_match(e.what(), e_timeout))
throw dut::timeout(e.what());
else if (regex_match(e.what(), e_syntax))
throw dut::syntax(e.what());
else
throw dut::failure(e.what());
}
}
schema_pqxx::schema_pqxx(std::string &conninfo) : c(conninfo)
{
pqxx::work w(c);
w.exec("set application_name to '" PACKAGE "::schema';");
pqxx::result r = w.exec("select version()");
version = r[0][0].as<string>();
cerr << "Loading types...";
r = w.exec("select quote_ident(typname), oid, typdelim, typrelid, typelem, typarray, typtype "
"from pg_type ");
for (auto row = r.begin(); row != r.end(); ++row) {
string name(row[0].as<string>());
OID oid(row[1].as<OID>());
string typdelim(row[2].as<string>());
OID typrelid(row[3].as<OID>());
OID typelem(row[4].as<OID>());
OID typarray(row[5].as<OID>());
string typtype(row[6].as<string>());
// if (schema == "pg_catalog")
// continue;
// if (schema == "information_schema")
// continue;
pg_type *t = new pg_type(name,oid,typdelim[0],typrelid, typelem, typarray, typtype[0]);
oid2type[oid] = t;
name2type[name] = t;
types.push_back(t);
}
booltype = name2type["bool"];
inttype = name2type["int4"];
internaltype = name2type["internal"];
arraytype = name2type["anyarray"];
cerr << "done." << endl;
cerr << "Loading tables...";
r = w.exec("select table_name, "
"table_schema, "
"is_insertable_into, "
"table_type "
"from information_schema.tables");
for (auto row = r.begin(); row != r.end(); ++row) {
string schema(row[1].as<string>());
string insertable(row[2].as<string>());
string table_type(row[3].as<string>());
// if (schema == "pg_catalog")
// continue;
// if (schema == "information_schema")
// continue;
tables.push_back(table(row[0].as<string>(),
schema,
((insertable == "YES") ? true : false),
((table_type == "BASE TABLE") ? true : false)));
}
cerr << "done." << endl;
cerr << "Loading columns and constraints...";
for (auto t = tables.begin(); t != tables.end(); ++t) {
string q("select attname, "
"atttypid "
"from pg_attribute join pg_class c on( c.oid = attrelid ) "
"join pg_namespace n on n.oid = relnamespace "
"where not attisdropped "
"and attname not in "
"('xmin', 'xmax', 'ctid', 'cmin', 'cmax', 'tableoid', 'oid') ");
q += " and relname = " + w.quote(t->name);
q += " and nspname = " + w.quote(t->schema);
r = w.exec(q);
for (auto row : r) {
column c(row[0].as<string>(), oid2type[row[1].as<OID>()]);
t->columns().push_back(c);
}
q = "select conname from pg_class t "
"join pg_constraint c on (t.oid = c.conrelid) "
"where contype in ('f', 'u', 'p') ";
q += " and relnamespace = " " (select oid from pg_namespace where nspname = " + w.quote(t->schema) + ")";
q += " and relname = " + w.quote(t->name);
for (auto row : w.exec(q)) {
t->constraints.push_back(row[0].as<string>());
}
}
cerr << "done." << endl;
cerr << "Loading operators...";
r = w.exec("select oprname, oprleft,"
"oprright, oprresult "
"from pg_catalog.pg_operator "
"where 0 not in (oprresult, oprright, oprleft) ");
for (auto row : r) {
op o(row[0].as<string>(),
oid2type[row[1].as<OID>()],
oid2type[row[2].as<OID>()],
oid2type[row[3].as<OID>()]);
register_operator(o);
}
cerr << "done." << endl;
cerr << "Loading routines...";
r = w.exec("select (select nspname from pg_namespace where oid = pronamespace), oid, prorettype, proname "
"from pg_proc "
"where prorettype::regtype::text not in ('event_trigger', 'trigger', 'opaque', 'internal') "
"and proname <> 'pg_event_trigger_table_rewrite_reason' "
"and proname <> 'pg_event_trigger_table_rewrite_oid' "
"and proname !~ '^ri_fkey_' "
"and not (proretset or proisagg or proiswindow) ");
for (auto row : r) {
routine proc(row[0].as<string>(),
row[1].as<string>(),
oid2type[row[2].as<long>()],
row[3].as<string>());
register_routine(proc);
}
cerr << "done." << endl;
cerr << "Loading routine parameters...";
for (auto &proc : routines) {
string q("select unnest(proargtypes) "
"from pg_proc ");
q += " where oid = " + w.quote(proc.specific_name);
r = w.exec(q);
for (auto row : r) {
sqltype *t = oid2type[row[0].as<OID>()];
assert(t);
proc.argtypes.push_back(t);
}
}
cerr << "done." << endl;
cerr << "Loading aggregates...";
r = w.exec("select (select nspname from pg_namespace where oid = pronamespace), oid, prorettype, proname "
"from pg_proc "
"where prorettype::regtype::text not in ('event_trigger', 'trigger', 'opaque', 'internal') "
"and proname not in ('pg_event_trigger_table_rewrite_reason') "
"and proname not in ('percentile_cont', 'dense_rank', 'cume_dist', "
"'rank', 'test_rank', 'percent_rank', 'percentile_disc', 'mode', 'test_percentile_disc') "
"and proname !~ '^ri_fkey_' "
"and not (proretset or proiswindow) "
"and proisagg");
for (auto row : r) {
routine proc(row[0].as<string>(),
row[1].as<string>(),
oid2type[row[2].as<OID>()],
row[3].as<string>());
register_aggregate(proc);
}
cerr << "done." << endl;
cerr << "Loading aggregate parameters...";
for (auto &proc : aggregates) {
string q("select unnest(proargtypes) "
"from pg_proc ");
q += " where oid = " + w.quote(proc.specific_name);
r = w.exec(q);
for (auto row : r) {
sqltype *t = oid2type[row[0].as<OID>()];
assert(t);
proc.argtypes.push_back(t);
}
}
cerr << "done." << endl;
c.disconnect();
generate_indexes();
// cerr << "print loaded information to check correctness" << endl;
// cerr << "Loaded tables.... " << endl;
/* for(auto item : tables)
{
cerr << item.name << "; " << item.schema << "; " << item.is_insertable << "; " << item.is_base_table << endl;
}
*/
// cerr << "Loaded columns... " << endl;
/* for(auto tab : tables)
{
for(auto col: tab.columns())
cerr << tab.name << "; " << col.name << "; "<<col.type->name << endl;
}
*/
// cerr << "Loaded aggregates and parameters... " << endl;
/*for(auto &proc : aggregates)
{
cerr << proc.specific_name << "; " << proc.schema << "; " << proc.name <<"; " << proc.restype->name ;
for(auto item : proc.argtypes)
cerr << "; " << item->name;
cerr << endl;
}*/
}