-
Notifications
You must be signed in to change notification settings - Fork 79
/
PqResultImpl.cpp
654 lines (514 loc) · 13.8 KB
/
PqResultImpl.cpp
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include "pch.h"
#include "PqResultImpl.h"
#include "DbConnection.h"
#include "DbResult.h"
#include "DbColumnStorage.h"
#include "PqDataFrame.h"
#ifdef _WIN32
#include <winsock2.h>
#define SOCKERR WSAGetLastError()
#define SOCKET_EINTR WSAEINTR
#else
#include <errno.h>
#define SOCKERR errno
#define SOCKET_EINTR EINTR
#endif
PqResultImpl::PqResultImpl(const DbConnectionPtr& pConn, const std::string& sql, bool immediate) :
pConnPtr_(pConn),
pConn_(pConn->conn()),
sql_(sql),
immediate_(immediate),
pSpec_(NULL),
complete_(false),
ready_(false),
data_ready_(false),
nrows_(0),
rows_affected_(0),
group_(0),
groups_(0),
pRes_(NULL)
{
LOG_DEBUG << sql;
prepare();
try {
if (cache.nparams_ == 0) {
bind();
}
} catch (...) {
PQclear(pSpec_);
pSpec_ = NULL;
throw;
}
}
PqResultImpl::~PqResultImpl() {
try {
if (pSpec_) PQclear(pSpec_);
} catch (...) {}
if (pRes_) PQclear(pRes_);
}
// Cache ///////////////////////////////////////////////////////////////////////
PqResultImpl::_cache::_cache() :
initialized_(false),
ncols_(0),
nparams_(0)
{
}
void PqResultImpl::_cache::set(PGresult* spec)
{
// always: should be fast
if (nparams_ == 0) {
nparams_ = PQnparams(spec);
}
std::vector<std::string> new_names = get_column_names(spec);
std::vector<Oid> new_oids = get_column_oids(spec);
if (initialized_ || new_names.size() == 0) {
LOG_VERBOSE;
if (names_.size() != 0 && new_names.size() != 0 && names_ != new_names) {
cpp11::stop("Multiple queries must use the same column names.");
}
if (oids_.size() != 0 && new_oids.size() != 0 && oids_ != new_oids) {
cpp11::stop("Multiple queries must use the same column types.");
}
return;
}
initialized_ = true;
names_ = new_names;
oids_ = new_oids;
types_ = get_column_types(oids_, names_);
known_ = get_column_known(oids_);
ncols_ = names_.size();
LOG_DEBUG << nparams_;
for (int i = 0; i < nparams_; ++i) {
LOG_VERBOSE << PQparamtype(spec, i);
}
}
std::vector<std::string> PqResultImpl::_cache::get_column_names(PGresult* spec) {
std::vector<std::string> names;
int ncols_ = PQnfields(spec);
names.reserve(ncols_);
for (int i = 0; i < ncols_; ++i) {
names.push_back(std::string(PQfname(spec, i)));
}
return names;
}
DATA_TYPE PqResultImpl::_cache::get_column_type_from_oid(const Oid type) {
// SELECT oid, typname FROM pg_type WHERE typtype = 'b'
switch (type) {
case 20: // BIGINT
return DT_INT64;
break;
case 21: // SMALLINT
case 23: // INTEGER
case 26: // OID
return DT_INT;
break;
case 1700: // DECIMAL
case 701: // FLOAT8
case 700: // FLOAT
case 790: // MONEY
return DT_REAL;
break;
case 18: // CHAR
case 19: // NAME
case 25: // TEXT
case 1042: // CHAR
case 1043: // VARCHAR
return DT_STRING;
break;
case 1082: // DATE
return DT_DATE;
break;
case 1083: // TIME
case 1266: // TIMETZOID
return DT_TIME;
break;
case 1114: // TIMESTAMP
return DT_DATETIME;
break;
case 1184: // TIMESTAMPTZOID
return DT_DATETIMETZ;
break;
case 1186: // INTERVAL
case 2950: // UUID
return DT_STRING;
break;
case 16: // BOOL
return DT_BOOL;
break;
case 17: // BYTEA
case 2278: // NULL
return DT_BLOB;
break;
case 705: // UNKNOWN
return DT_STRING;
break;
default:
return DT_UNKNOWN;
}
}
std::vector<Oid> PqResultImpl::_cache::get_column_oids(PGresult* spec) {
std::vector<Oid> oids;
int ncols_ = PQnfields(spec);
oids.reserve(ncols_);
for (int i = 0; i < ncols_; ++i) {
Oid oid = PQftype(spec, i);
oids.push_back(oid);
}
return oids;
}
std::vector<DATA_TYPE> PqResultImpl::_cache::get_column_types(const std::vector<Oid>& oids, const std::vector<std::string>& names) {
std::vector<DATA_TYPE> types;
size_t ncols_ = oids.size();
types.reserve(ncols_);
for (size_t i = 0; i < ncols_; ++i) {
Oid oid = oids[i];
DATA_TYPE data_type = get_column_type_from_oid(oid);
if (data_type == DT_UNKNOWN) {
LOG_INFO << "Unknown field type (" << oid << ") in column " << names[i];
data_type = DT_STRING;
}
types.push_back(data_type);
}
return types;
}
std::vector<bool> PqResultImpl::_cache::get_column_known(const std::vector<Oid>& oids) {
std::vector<bool> known;
size_t ncols_ = oids.size();
known.reserve(ncols_);
for (size_t i = 0; i < ncols_; ++i) {
Oid oid = oids[i];
DATA_TYPE data_type = get_column_type_from_oid(oid);
known.push_back(data_type != DT_UNKNOWN);
}
return known;
}
void PqResultImpl::prepare() {
if (immediate_) {
return;
}
LOG_DEBUG << sql_;
// Prepare query
PGresult* prep = PQprepare(pConn_, "", sql_.c_str(), 0, NULL);
if (PQresultStatus(prep) != PGRES_COMMAND_OK) {
PQclear(prep);
conn_stop("Failed to prepare query");
}
PQclear(prep);
// Retrieve query specification
PGresult* spec = PQdescribePrepared(pConn_, "");
if (PQresultStatus(spec) != PGRES_COMMAND_OK) {
PQclear(spec);
conn_stop("Failed to retrieve query result metadata");
}
pSpec_ = spec;
cache.set(spec);
}
void PqResultImpl::init(bool params_have_rows) {
ready_ = true;
nrows_ = 0;
complete_ = !params_have_rows;
}
// Publics /////////////////////////////////////////////////////////////////////
bool PqResultImpl::complete() const {
return complete_;
}
int PqResultImpl::n_rows_fetched() {
return nrows_;
}
int PqResultImpl::n_rows_affected() {
if (!ready_) return NA_INTEGER;
if (cache.ncols_ > 0) return 0;
return rows_affected_;
}
void PqResultImpl::bind(const cpp11::list& params) {
LOG_DEBUG << params.size();
if (immediate_ && params.size() > 0) {
cpp11::stop("Immediate query cannot be parameterized.");
}
if (params.size() != cache.nparams_) {
cpp11::stop("Query requires %i params; %i supplied.",
cache.nparams_, params.size());
}
if (params.size() == 0 && ready_) {
cpp11::stop("Query does not require parameters.");
}
set_params(params);
if (params.size() > 0) {
SEXP first_col = params[0];
groups_ = Rf_length(first_col);
}
else {
groups_ = 1;
}
group_ = 0;
rows_affected_ = 0;
bool has_params = bind_row();
after_bind(has_params);
}
cpp11::list PqResultImpl::fetch(const int n_max) {
LOG_DEBUG << n_max;
if (!ready_)
cpp11::stop("Query needs to be bound before fetching");
int n = 0;
cpp11::list out;
if (n_max != 0)
out = fetch_rows(n_max, n);
else
out = peek_first_row();
return out;
}
cpp11::list PqResultImpl::get_column_info() {
using namespace cpp11::literals;
peek_first_row();
cpp11::writable::strings names(cache.names_.size());
auto it = cache.names_.begin();
for (int i = 0; i < names.size(); i++, it++)
names[i] = *it;
cpp11::writable::strings types(cache.ncols_);
for (size_t i = 0; i < cache.ncols_; i++) {
types[i] = Rf_type2char(DbColumnStorage::sexptype_from_datatype(cache.types_[i]));
}
return cpp11::list({
"name"_nm = names,
"type"_nm = types,
".oid"_nm = cache.oids_,
".known"_nm = cache.known_
});
}
// Publics (custom) ////////////////////////////////////////////////////////////
// Privates ////////////////////////////////////////////////////////////////////
void PqResultImpl::set_params(const cpp11::list& params) {
params_ = params;
}
bool PqResultImpl::bind_row() {
LOG_VERBOSE << "groups: " << group_ << "/" << groups_;
if (group_ >= groups_) {
// Multi-statement support for immediate queries
return immediate_;
}
if (ready_ || group_ > 0) {
LOG_VERBOSE;
DbConnection::finish_query(pConn_);
}
std::vector<const char*> c_params(cache.nparams_);
std::vector<int> formats(cache.nparams_);
std::vector<int> lengths(cache.nparams_);
for (int i = 0; i < cache.nparams_; ++i) {
if (TYPEOF(params_[i]) == VECSXP) {
cpp11::list param(params_[i]);
if (!Rf_isNull(param[group_])) {
Rbyte* param_value = RAW(param[group_]);
c_params[i] = reinterpret_cast<const char*>(param_value);
formats[i] = 1;
lengths[i] = Rf_length(param[group_]);
}
}
else {
cpp11::strings param(params_[i]);
if (param[group_] != NA_STRING) {
c_params[i] = CHAR(param[group_]);
}
}
}
// Pointer to first element of empty vector is undefined behavior!
data_ready_ = false;
if (immediate_) {
int success = PQsendQuery(pConn_, sql_.c_str());
if (!success) {
conn_stop("Failed to send query");
}
}
else {
int success = PQsendQueryPrepared(
pConn_, "", cache.nparams_,
cache.nparams_ ? &c_params[0] : NULL,
cache.nparams_ ? &lengths[0] : NULL,
cache.nparams_ ? &formats[0] : NULL,
0);
if (!success)
conn_stop("Failed to set query parameters");
}
if (!PQsetSingleRowMode(pConn_))
conn_stop("Failed to set single row mode");
return true;
}
void PqResultImpl::after_bind(bool params_have_rows) {
init(params_have_rows);
if (params_have_rows)
step();
}
cpp11::list PqResultImpl::fetch_rows(const int n_max, int& n) {
LOG_DEBUG << n_max << "/" << n;
n = (n_max < 0) ? 100 : n_max;
PqDataFrame data(this, cache.names_, n_max, cache.types_);
if (complete_ && data.get_ncols() == 0) {
cpp11::warning(std::string("Don't need to call dbFetch() for statements, only for queries"));
}
while (!complete_) {
LOG_VERBOSE << nrows_ << "/" << n;
data.set_col_values();
step();
nrows_++;
if (!data.advance())
break;
}
LOG_VERBOSE << nrows_;
cpp11::writable::list ret = data.get_data();
add_oids(ret);
return ret;
}
void PqResultImpl::step() {
LOG_VERBOSE;
while (step_run())
;
}
bool PqResultImpl::step_run() {
LOG_VERBOSE;
if (pRes_) {
PQclear(pRes_);
pRes_ = NULL;
}
bool need_cache_reset = false;
LOG_VERBOSE << data_ready_;
// Check user interrupts while waiting for the data to be ready
if (!data_ready_) {
LOG_VERBOSE;
bool proceed = wait_for_data();
data_ready_ = true;
if (!proceed) {
pConnPtr_->cancel_query();
complete_ = TRUE;
cpp11::stop("Interrupted.");
}
need_cache_reset = true;
}
pRes_ = PQgetResult(pConn_);
LOG_VERBOSE;
// We're done, but we need to call PQgetResult until it returns NULL
if (PQresultStatus(pRes_) == PGRES_TUPLES_OK) {
LOG_VERBOSE;
step_done();
return true;
}
if (pRes_ == NULL) {
LOG_VERBOSE;
complete_ = true;
return false;
}
ExecStatusType status = PQresultStatus(pRes_);
if (status == PGRES_FATAL_ERROR) {
PQclear(pRes_);
pRes_ = NULL;
conn_stop("Failed to fetch row");
return false;
}
if (need_cache_reset) {
cache.set(pRes_);
}
if (status == PGRES_SINGLE_TUPLE) {
LOG_VERBOSE;
return false;
}
LOG_VERBOSE;
return step_done();
}
bool PqResultImpl::step_done() {
char* tuples = PQcmdTuples(pRes_);
LOG_VERBOSE << tuples;
rows_affected_ += atoi(tuples);
LOG_VERBOSE << rows_affected_;
++group_;
data_ready_ = false;
bool more_params = bind_row();
if (!more_params)
complete_ = true;
LOG_VERBOSE << "group: " << group_ << ", more_params: " << more_params;
return more_params;
}
cpp11::list PqResultImpl::peek_first_row() {
PqDataFrame data(this, cache.names_, 1, cache.types_);
if (!complete_)
data.set_col_values();
// Not calling data.advance(), remains a zero-row data frame
cpp11::writable::list ret = data.get_data();
add_oids(ret);
return ret;
}
void PqResultImpl::conn_stop(const char* msg) const {
DbConnection::conn_stop(pConn_, msg);
}
void PqResultImpl::bind() {
bind(cpp11::list());
}
void PqResultImpl::add_oids(cpp11::writable::list& data) const {
data.attr("oids") = cpp11::as_sexp(cache.oids_);
data.attr("known") = cpp11::as_sexp(cache.known_);
auto is_without_tz = cpp11::writable::logicals(cache.types_.size());
for (size_t i = 0; i < cache.types_.size(); ++i) {
bool set = (cache.types_[i] == DT_DATETIME);
LOG_VERBOSE << "is_without_tz[" << i << "]: " << set;
is_without_tz[i] = set;
}
data.attr("without_tz") = is_without_tz;
}
PGresult* PqResultImpl::get_result() {
return pRes_;
}
// checks user interrupts while waiting for the first row of data to be ready
// see https://www.postgresql.org/docs/current/static/libpq-async.html
// Returns `false` if an interrupt was detected
bool PqResultImpl::wait_for_data() {
LOG_DEBUG << pConnPtr_->is_check_interrupts();
if (!pConnPtr_->is_check_interrupts())
return true;
// update db connection state using data available on the socket
if (!PQconsumeInput(pConn_)) {
cpp11::stop("Failed to consume input from the server");
}
// check if PQgetResult will block before waiting
if (!PQisBusy(pConn_)) {
return true;
}
int socket, ret;
fd_set input;
FD_ZERO(&input);
socket = PQsocket(pConn_);
if (socket < 0) {
cpp11::stop("Failed to get connection socket");
}
do {
LOG_DEBUG;
// wait for any traffic on the db connection socket but no longer than 1s
timeval timeout = {0, 0};
timeout.tv_sec = 1;
FD_SET(socket, &input);
const int nfds = socket + 1;
ret = select(nfds, &input, NULL, NULL, &timeout);
if (ret == 0) {
LOG_DEBUG;
// timeout reached - check user interrupt
try {
// FIXME: Do we even need this?
cpp11::check_user_interrupt();
}
catch (...) {
LOG_DEBUG;
return false;
}
} else if (ret < 0) {
// caught interrupt in select()
if (SOCKERR == SOCKET_EINTR) {
LOG_DEBUG;
return false;
} else {
LOG_DEBUG;
cpp11::stop("select() failed with error code %d", SOCKERR);
}
}
// update db connection state using data available on the socket
if (!PQconsumeInput(pConn_)) {
cpp11::stop("Failed to consume input from the server");
}
} while (PQisBusy(pConn_)); // check if PQgetResult will still block
return true;
}