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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* $Rev$ $Date$ */
#ifndef tuscany_pgsql_hpp
#define tuscany_pgsql_hpp
/**
* PostgreSQL access functions.
*/
#include <libpq-fe.h>
#include "string.hpp"
#include "list.hpp"
#include "value.hpp"
#include "monad.hpp"
#include "../../modules/scheme/eval.hpp"
namespace tuscany {
namespace pgsql {
/**
* Return and clear a Postgres result failure.
*/
const string pgfailure(PGresult* const r, PGconn* const conn) {
const string re = PQresultErrorMessage(r);
PQclear(r);
if (length(re) != 0)
return re;
const string ce = PQerrorMessage(conn);
return ce;
}
/**
* Represents a PGSql connection.
*/
class PGSql {
public:
PGSql() : owner(false) {
debug("pgsql::pgsql");
}
PGSql(const string& conninfo, const string& table) : owner(true), conn(NULL), conninfo(conninfo), table(table) {
debug(conninfo, "pgsql::pgsql::conninfo");
debug(table, "pgsql::pgsql::table");
// Connect to the database
conn = PQconnectdb(c_str(conninfo));
if (PQstatus(conn) != CONNECTION_OK) {
mkfailure<bool>(string("Couldn't connect to postgresql database: ") + PQerrorMessage(conn));
return;
}
debug(conn, "pgsql::pgsql::conn");
// Find the name of the first column in the target table
// Assume that's the key we need to use
string ks = string("select a.attname from pg_attribute a, pg_class c where a.attrelid = c.relfilenode and c.relname = '") + table + string("' and a.attnum in (1, 2) order by a.attnum;");
PGresult* const kr = PQexec(conn, c_str(ks));
if (PQresultStatus(kr) != PGRES_TUPLES_OK) {
const string rs = string("Couldn't execute postgresql column select statement: ") + pgfailure(kr, conn);
PQclear(kr);
mkfailure<bool>(rs);
return;
}
if (PQntuples(kr) != 2) {
const string rs = "Couldn't find postgresql table key and value column names";
PQclear(kr);
mkfailure<bool>(rs);
return;
}
kname = c_str(string(PQgetvalue(kr, 0, 0)));
vname = c_str(string(PQgetvalue(kr, 1, 0)));
PQclear(kr);
}
PGSql(const PGSql& c) : owner(false), conn(c.conn), conninfo(c.conninfo), table(c.table), kname(c.kname), vname(c.vname) {
debug("pgsql::pgsql::copy");
}
PGSql& operator=(const PGSql& c) = delete;
~PGSql() {
if (!owner)
return;
if (conn == NULL)
return;
PQfinish(conn);
}
private:
const bool owner;
PGconn *conn;
const string conninfo;
const string table;
const char* kname;
const char* vname;
friend const failable<bool> setup(const PGSql& pgsql);
friend const failable<bool> begin(const PGSql& pgsql);
friend const failable<bool> commit(const PGSql& pgsql);
friend const failable<bool> rollback(const PGSql& pgsql);
friend const failable<bool> post(const value& key, const value& val, const PGSql& pgsql);
friend const failable<bool> put(const value& key, const value& val, const PGSql& pgsql);
friend const failable<bool> patch(const value& key, const value& val, const PGSql& pgsql);
friend const failable<value> get(const value& key, const PGSql& pgsql);
friend const failable<bool> del(const value& key, const PGSql& pgsql);
};
/**
* Setup the database connection if necessary.
*/
const failable<bool> setup(const PGSql& pgsql) {
debug("pgsql::setup");
if (PQstatus(pgsql.conn) == CONNECTION_OK)
return true;
debug(pgsql.conn, "pgsql::setup::reset::conn");
PQreset(pgsql.conn);
if (PQstatus(pgsql.conn) != CONNECTION_OK)
return mkfailure<bool>(string("Couldn't reconnect to postgresql database: ") + PQerrorMessage(pgsql.conn));
return true;
}
/**
* Begin a database transaction.
*/
const failable<bool> begin(const PGSql& pgsql) {
debug("pgsql::begin");
debug(pgsql.conninfo, "pgsql::begin::conninfo");
debug(pgsql.table, "pgsql::begin::table");
setup(pgsql);
PGresult* const r = PQexec(pgsql.conn, "begin transaction isolation level repeatable read");
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute begin SQL statement: ") + pgfailure(r, pgsql.conn);
PQclear(r);
return mkfailure<bool>(rs);
}
PQclear(r);
debug(true, "pgsql::begin::result");
return true;
}
/**
* Commit a database transaction.
*/
const failable<bool> commit(const PGSql& pgsql) {
debug("pgsql::commit");
debug(pgsql.conninfo, "pgsql::commit::conninfo");
debug(pgsql.table, "pgsql::commit::table");
setup(pgsql);
PGresult* const r = PQexec(pgsql.conn, "commit");
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute commit SQL statement: ") + pgfailure(r, pgsql.conn);
PQclear(r);
return mkfailure<bool>(rs);
}
PQclear(r);
debug(true, "pgsql::commit::result");
return true;
}
/**
* Rollback a database transaction.
*/
const failable<bool> rollback(const PGSql& pgsql) {
debug("pgsql::rollback");
debug(pgsql.conninfo, "pgsql::rollback::conninfo");
debug(pgsql.table, "pgsql::rollback::table");
setup(pgsql);
PGresult* const r = PQexec(pgsql.conn, "rollback");
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute rollback SQL statement: ") + pgfailure(r, pgsql.conn);
PQclear(r);
return mkfailure<bool>(rs);
}
PQclear(r);
debug(true, "pgsql::rollback::result");
return true;
}
/**
* Post a new item to the database.
*/
const failable<bool> post(const value& key, const value& val, const PGSql& pgsql) {
debug(key, "pgsql::post::key");
debug(val, "pgsql::post::value");
debug(pgsql.conninfo, "pgsql::post::conninfo");
debug(pgsql.table, "pgsql::post::table");
setup(pgsql);
const string ks(write(content(scheme::writeValue(key))));
const string vs(write(content(scheme::writeValue(val))));
const char* const params[2] = { c_str(ks), c_str(vs) };
PGresult* const r = PQexecParams(pgsql.conn, c_str(string("insert into ") + pgsql.table + string(" values($1, $2);")), 2, NULL, params, NULL, NULL, 0);
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute insert postgresql SQL statement: ") + pgfailure(r, pgsql.conn);
PQclear(r);
return mkfailure<bool>(rs);
}
PQclear(r);
debug(true, "pgsql::post::result");
return true;
}
/**
* Update an item in the database. If the item doesn't exist it is added.
*/
const failable<bool> put(const value& key, const value& val, const PGSql& pgsql) {
debug(key, "pgsql::put::key");
debug(val, "pgsql::put::value");
debug(pgsql.conninfo, "pgsql::put::conninfo");
debug(pgsql.table, "pgsql::put::table");
setup(pgsql);
const string ks(write(content(scheme::writeValue(key))));
const string vs(write(content(scheme::writeValue(val))));
const char* const params[2] = { c_str(ks), c_str(vs) };
PGresult* const r = PQexecParams(pgsql.conn, c_str(string("update ") + pgsql.table + string(" set ") + pgsql.vname + string(" = $2 where ") + pgsql.kname + string(" = $1;")), 2, NULL, params, NULL, NULL, 0);
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute update postgresql SQL statement: ") + pgfailure(r, pgsql.conn);
PQclear(r);
return mkfailure<bool>(rs);
}
const char* const t = PQcmdTuples(r);
if (t != NULL && strcmp(t, "0")) {
PQclear(r);
debug(true, "pgsql::put::result");
return true;
}
PQclear(r);
PGresult* const pr = PQexecParams(pgsql.conn, c_str(string("insert into ") + pgsql.table + string(" values($1, $2);")), 2, NULL, params, NULL, NULL, 0);
if (PQresultStatus(pr) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute insert postgresql SQL statement: ") + pgfailure(pr, pgsql.conn);
PQclear(pr);
return mkfailure<bool>(rs);
}
PQclear(pr);
debug(true, "pgsql::put::result");
return true;
}
/**
* Patch an item in the database. If the item doesn't exist it is added.
*/
const failable<bool> patch(const value& key, const value& val, const PGSql& pgsql) {
debug(key, "pgsql::patch::key");
debug(val, "pgsql::patch::value");
debug(pgsql.conninfo, "pgsql::patch::conninfo");
debug(pgsql.table, "pgsql::patch::table");
setup(pgsql);
const string ks(write(content(scheme::writeValue(key))));
const string vs(write(content(scheme::writeValue(val))));
const char* const params[2] = { c_str(ks), c_str(vs) };
PGresult* const r = PQexecParams(pgsql.conn, c_str(string("update ") + pgsql.table + string(" set ") + pgsql.vname + string(" = $2 where ") + pgsql.kname + string(" = $1;")), 2, NULL, params, NULL, NULL, 0);
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute update postgresql SQL statement: ") + pgfailure(r, pgsql.conn);
const char* const st = PQresultErrorField(r, PG_DIAG_SQLSTATE);
if (st != NULL && !strncmp(st, "40", 2)) {
// Report a transaction serialization conflict
PQclear(r);
return mkfailure<bool>(rs, 409);
}
PQclear(r);
return mkfailure<bool>(rs);
}
const char* const t = PQcmdTuples(r);
if (t != NULL && strcmp(t, "0")) {
PQclear(r);
debug(true, "pgsql::patch::result");
return true;
}
PQclear(r);
PGresult* const pr = PQexecParams(pgsql.conn, c_str(string("insert into ") + pgsql.table + string(" values($1, $2);")), 2, NULL, params, NULL, NULL, 0);
if (PQresultStatus(pr) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute insert postgresql SQL statement: ") + pgfailure(pr, pgsql.conn);
const char* const st = PQresultErrorField(pr, PG_DIAG_SQLSTATE);
if (st != NULL && !strncmp(st, "40", 2)) {
PQclear(pr);
return mkfailure<bool>(rs, 40);
}
PQclear(pr);
return mkfailure<bool>(rs);
}
PQclear(pr);
debug(true, "pgsql::patch::result");
return true;
}
/**
* Convert a key to an item id.
*/
const list<value> keyid(const list<value>& key) {
if (isNil(key))
return nilListValue;
if (isList(car(key)))
return keyid(cdr(key));
return cons<value>(car(key), keyid(cdr(key)));
}
/**
* Convert a key to a (param name, value) assoc.
*/
const list<value> keyparams(const list<value>& key) {
if (isNil(key))
return nilListValue;
if (!isList(car(key)))
return keyparams(cdr(key));
return cons<value>(car(key), keyparams(cdr(key)));
}
/**
* Convert a get result to a list of items.
*/
const list<value> getitems(PGresult* const r, const int i, const int n) {
if (i == n)
return nilListValue;
const value val(content(scheme::readValue(string(PQgetvalue(r, i, 1)))));
return cons<value>(val, getitems(r, i + 1, n));
}
/**
* Parse a text search query and translate single quotes to spaces and double
* quotes to single quotes.
*/
ostringstream& tsparse(ostringstream& os, const char* const c) {
if (!*c)
return os;
os << (*c == '\''? ' ' : *c == '"'? '\'' : *c);
return tsparse(os, c + 1);
}
const string tstranslate(const string& ts) {
ostringstream os;
tsparse(os, c_str(ts));
return str(os);
}
/**
* Get one item or a collection of items from the database.
* The key is a simple value or a list of simple values plus optional name / value
* pairs to specify regex, like, textsearch limit and offset clause
*/
const failable<value> get(const value& key, const PGSql& pgsql) {
debug(key, "pgsql::get::key");
debug(pgsql.conninfo, "pgsql::get::conninfo");
debug(pgsql.table, "pgsql::get::table");
setup(pgsql);
// Get item and id and get parameters from the key
const bool lk = isList(key);
const list<value> kparams = lk? keyparams(key) : nilListValue;
const list<value> regex = assoc<value>("regex", kparams);
const list<value> like = assoc<value>("like", kparams);
const list<value> textsearch = assoc<value>("textsearch", kparams);
const list<value> limit = assoc<value>("limit", kparams);
const list<value> offset = assoc<value>("offset", kparams);
const list<value> rank = assoc<value>("rank", kparams);
const list<value> id = lk? keyid(key) : nilListValue;
const list<value> atable = assoc<value>("table", kparams);
const string table = isNil(atable)? pgsql.table : (string)cadr(atable);
const list<value> akname = assoc<value>("kcolumn", kparams);
const string kname = isNil(akname)? pgsql.kname : (string)cadr(akname);
const list<value> avname = assoc<value>("vcolumn", kparams);
const string vname = isNil(avname)? pgsql.vname : (string)cadr(avname);
// Build the SQL query
const char* sqlparams[6];
int p = 0;
int w = 0;
ostringstream sqlos;
sqlos << "select data." << kname << ", data." << vname;
if (!isNil(textsearch)) {
// Text search, setup text result ranking
sqlos << ", ts_rank_cd(to_tsvector(data." << vname << "), tsquery, 32) as tsrank";
}
if (!isNil(rank)) {
// Ranking, setup rank expression
const string rs = (string)cadr(rank);
sqlparams[p++] = c_str(rs);
sqlos << ", $" << p << " as rank";
}
sqlos << " from " << table << " data";
if (!isNil(textsearch)) {
// Text search, define the query
const string ts = tstranslate((string)cadr(textsearch));
sqlparams[p++] = c_str(ts);
sqlos << ", plainto_tsquery($" << p << ") tsquery";
}
if (!lk || !isNil(id)) {
// Query of the form key = id
sqlparams[p++] = c_str(write(content(scheme::writeValue(lk? (value)id : key))));
sqlos << (w == 0? " where" : " and");
sqlos << " data." << kname << " = $" << p;
w++;
}
if (!isNil(regex)) {
// Query of the form key ~ param
sqlparams[p++] = c_str((string)cadr(regex));
sqlos << (w == 0? " where" : " and");
sqlos << " data." << kname << " ~ $" << p;
w++;
}
if (!isNil(like)) {
// Query of the form key like param
sqlparams[p++] = c_str((string)cadr(like));
sqlos << (w == 0? " where" : " and");
sqlos << " data." << kname << " like $" << p;
w++;
}
if (!isNil(textsearch)) {
// Text search, apply the query
sqlos << (w == 0? " where" : " and");
sqlos << " tsquery @@ to_tsvector(data." << vname << ")";
w++;
}
if (!isNil(textsearch) || !isNil(rank)) {
// Result ordering
sqlos << " order by" << (isNil(rank)? "" : " rank desc") << ((isNil(rank) || isNil(textsearch))? "" : ",") << (isNil(textsearch)? "" : " tsrank desc");
}
if (!isNil(offset)) {
// Result pagination offset
sqlos << " offset " << atoi(c_str((string)cadr(offset)));
}
// Result limit count
const int l = isNil(limit)? 1 : atoi(c_str((string)cadr(limit)));
sqlos << " limit " << l << ";";
// Execute the query
const string sqls = str(sqlos);
debug(sqls, "pgsql::get::sqls");
PGresult* r = PQexecParams(pgsql.conn, c_str(sqls), p, NULL, sqlparams, NULL, NULL, 0);
if (PQresultStatus(r) != PGRES_TUPLES_OK) {
const string rs = string("Couldn't execute select postgresql SQL statement: ") + pgfailure(r, pgsql.conn);
PQclear(r);
return mkfailure<value>(rs);
}
const int n = PQntuples(r);
if (n < 1) {
PQclear(r);
ostringstream os;
os << "Couldn't get postgresql entry: " << key;
return mkfailure<value>(str(os), 404, false);
}
// Return a collection of items
if (l != 1) {
const list<value> lval = getitems(r, 0, n);
PQclear(r);
debug(lval, "pgsql::get::result");
return (value)lval;
}
// Return a single item
const value val(content(scheme::readValue(string(PQgetvalue(r, 0, 1)))));
PQclear(r);
debug(val, "pgsql::get::result");
return val;
}
/**
* Delete an item from the database
*/
const failable<bool> del(const value& key, const PGSql& pgsql) {
debug(key, "pgsql::delete::key");
debug(pgsql.conninfo, "pgsql::delete::conninfo");
debug(pgsql.table, "pgsql::delete::table");
setup(pgsql);
const string ks(write(content(scheme::writeValue(key))));
const char* const params[1] = { c_str(ks) };
PGresult* const r = PQexecParams(pgsql.conn, c_str(string("delete from ") + pgsql.table + string(" where ") + pgsql.kname + string(" = $1;")), 1, NULL, params, NULL, NULL, 0);
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
const string rs = string("Couldn't execute delete postgresql SQL statement: ") + pgfailure(r, pgsql.conn);
PQclear(r);
return mkfailure<bool>(rs);
}
const char* const t = PQcmdTuples(r);
if (t != NULL && !strcmp(t, "0")) {
PQclear(r);
ostringstream os;
os << "Couldn't delete postgresql entry: " << key;
return mkfailure<bool>(str(os), 404, false);
}
PQclear(r);
debug(true, "pgsql::delete::result");
return true;
}
}
}
#endif /* tuscany_pgsql_hpp */
|