-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheck_get_data.cpp
More file actions
825 lines (721 loc) · 25.5 KB
/
Copy pathcheck_get_data.cpp
File metadata and controls
825 lines (721 loc) · 25.5 KB
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*PGR-GNU*****************************************************************
File: check_get_data.cpp
Copyright (c) 2024 pgRouting developers
Mail: pgrouting-dev@discourse.osgeo.org
Developer:
Copyright (c) 2024 Celia Virginia Vergara Castillo
Mail: vicky at erosion.dev
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#include "cpp_common/check_get_data.hpp"
extern "C" {
#include <postgres.h>
#include <executor/spi.h>
#include <funcapi.h>
#include <fmgr.h>
#include <access/htup_details.h>
#include <catalog/pg_type.h>
#include <utils/lsyscache.h>
#include <utils/builtins.h>
#include <utils/timestamp.h>
#include <utils/date.h>
#include <utils/datetime.h>
}
#include <vector>
#include <unordered_set>
#include <string>
#include <ctime>
#include <limits>
#include "cpp_common/undefPostgresDefine.hpp"
#include "cpp_common/alloc.hpp"
#include "cpp_common/info.hpp"
namespace {
void
check_interval_type(const vrprouting::Info &info) {
if (!(info.type == 1186)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected INTERVAL";
}
}
void
check_jsonb_type(vrprouting::Info info) {
if (!(info.type == JSONBOID)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected JSONB";
}
}
void
check_integer_type(vrprouting::Info info) {
if (!(info.type == INT2OID || info.type == INT4OID)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected SMALLINT or INTEGER";
}
}
void
check_integerarray_type(vrprouting::Info info) {
if (!(info.type == INT2ARRAYOID || info.type == INT4ARRAYOID)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected INTEGER-ARRAY";
}
}
/**
* @brief The function check whether column type is ANY-INTEGER-ARRAY or not.
*
* Where ANY-INTEGER-ARRAY is SQL type:
* SMALLINT[], INTEGER[], BIGINT[]
*
* @param[in] info contain column information.
* @throw ERROR Unexpected type in column. Expected ANY-INTEGER-ARRAY.
*/
void
check_any_integerarray_type(vrprouting::Info info) {
if (!(info.type == INT2ARRAYOID
|| info.type == INT4ARRAYOID
|| info.type == 1016)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected ANY-INTEGER-ARRAY";
}
}
void
check_timestamp_type(vrprouting::Info info) {
if (!(info.type == 1114)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected TIMESTAMP";
}
}
/** @brief Function tells expected type of each column and then check the correspondence type of each column.
*
* [SPI_fnumber](https://www.postgresql.org/docs/current/spi-spi-fnumber.html)
* [SPI_gettypeid](https://www.postgresql.org/docs/9.1/static/spi-spi-gettypeid.html)
* @param[in] tupdesc tuple description.
* @param[in] info contain one or more column information.
* @throw column not found.
* @throw ERROR Unknown type of column.
* @return @b TRUE when column exist.
* @b FALSE when column was not found.
*/
bool
get_column_info(const TupleDesc &tupdesc, vrprouting::Info &info) {
info.colNumber = SPI_fnumber(tupdesc, info.name.c_str());
if (info.strict && info.colNumber == SPI_ERROR_NOATTRIBUTE) {
throw std::string("Column '") + info.name + "' not Found";
}
if (info.colNumber != SPI_ERROR_NOATTRIBUTE) {
info.type = SPI_gettypeid(tupdesc, info.colNumber);
if (info.type == InvalidOid) {
throw std::string("Type of column '") + info.name + "' not Found";
}
return true;
}
return false;
}
/** @brief The function check whether column type is ANY-INTEGER or not.
*
* Where ANY-INTEGER is SQL type: SMALLINT, INTEGER, BIGINT
*
* @param[in] info contain column information.
* @throw ERROR Unexpected type in column. Expected column type is ANY-INTEGER.
*/
void
check_any_integer_type(const vrprouting::Info &info) {
if (!(info.type == INT2OID
|| info.type == INT4OID
|| info.type == INT8OID)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected ANY-INTEGER";
}
}
/**
* @brief The function check whether column type is ANY-NUMERICAL.
* Where ANY-NUMERICAL is SQL type:
* SMALLINT, INTEGER, BIGINT, REAL, FLOAT
*
* @param[in] info contain column information.
* @throw ERROR Unexpected type in column. Expected column type is ANY-NUMERICAL.
*/
void check_any_numerical_type(const vrprouting::Info &info) {
if (!(info.type == INT2OID
|| info.type == INT4OID
|| info.type == INT8OID
|| info.type == FLOAT4OID
|| info.type == FLOAT8OID
|| info.type == NUMERICOID)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected ANY-NUMERICAL";
}
}
/**
* @brief The function check whether column type is TEXT or not.
* Where TEXT is SQL type:
* TEXT
*
* @param[in] info contain column information.
* @throw ERROR Unexpected type in column. Expected column type is TEXT.
*/
void
check_text_type(const vrprouting::Info &info) {
if (!(info.type == TEXTOID)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected TEXT";
}
}
/**
* @brief The function check whether column type is CHAR or not.
* Where CHAR is SQL type:
* CHARACTER
*
* [BPCHAROID](https://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html#afa7749dbe36d31874205189d9d6b21d7)
* @param[in] info contain column information.
* @throw ERROR Unexpected type in column. Expected column type is CHAR.
*/
void
check_char_type(const vrprouting::Info &info) {
if (!(info.type == BPCHAROID)) {
throw std::string("Unexpected type in column '") + info.name + "'. Expected TEXT";
}
}
TInterval
getInterval(const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) {
Datum binval;
bool isnull;
Interval* interval;
binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull);
if (isnull) throw std::string("Unexpected Null value in column ") + info.name;
switch (info.type) {
case INTERVALOID:
interval = DatumGetIntervalP(binval);
break;
default:
throw std::string("Unexpected type value in column '") + info.name + "'. Expected INTERVALOID";
}
return interval->time / 1000000
+ interval->day * SECS_PER_DAY
+ static_cast<int64_t>(
interval->month * ((DAYS_PER_YEAR / static_cast<double>(MONTHS_PER_YEAR)) * SECS_PER_DAY));
}
TTimestamp
getTimeStamp(const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) {
Datum binval;
bool isnull;
TTimestamp value = 0;
binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull);
if (isnull) throw std::string("Unexpected Null value in column ") + info.name;
switch (info.type) {
case 1114:
value = vrprouting::get_timestamp_without_timezone(DatumGetInt64(binval));
break;
default:
throw std::string("Unexpected type value in column '") + info.name + "'. Expected 1114";
}
return value;
}
/**
* @param[in] tuple input row to be examined.
* @param[in] tupdesc tuple descriptor
* @param[in] info contain column information.
* @throw ERROR Unexpected type in column. Expected column type is ANY-INTEGER.
* @throw ERROR When value of column is NULL.
*
* @return Integer type of column value is returned.
*/
int64_t getBigInt(
const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) {
Datum binval;
bool isnull;
int64_t value = 0;
binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull);
if (isnull)
throw std::string("Unexpected Null value in column ") + info.name;
switch (info.type) {
case INT2OID:
value = static_cast<int64_t>(DatumGetInt16(binval));
break;
case INT4OID:
value = static_cast<int64_t>(DatumGetInt32(binval));
break;
case INT8OID:
value = DatumGetInt64(binval);
break;
default:
throw std::string("Unexpected type in column type of ") + info.name + ". Expected ANY-INTEGER";
}
return value;
}
/**
* @param[in] tuple input row to be examined.
* @param[in] tupdesc tuple descriptor
* @param[in] info contain column information.
* @throw ERROR Unexpected type in column. Expected column type is ANY-NUMERICAL.
* @throw ERROR When value of column is NULL.
* @return Double type of column value is returned.
*/
double getFloat8(
const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) {
Datum binval;
bool isnull = false;
binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull);
if (isnull)
throw std::string("Unexpected Null value in column ") + info.name;
switch (info.type) {
case INT2OID:
return static_cast<double>(DatumGetInt16(binval));
break;
case INT4OID:
return static_cast<double>(DatumGetInt32(binval));
break;
case INT8OID:
return static_cast<double>(DatumGetInt64(binval));
break;
case FLOAT4OID:
return static_cast<double>(DatumGetFloat4(binval));
break;
case FLOAT8OID:
return static_cast<double>(DatumGetFloat8(binval));
break;
case NUMERICOID:
/* Note: out-of-range values will be clamped to +-HUGE_VAL */
return static_cast<double>(DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, binval)));
break;
default:
throw std::string("Unexpected type in column type of ") + info.name + ". Expected ANY-NUMERICAL";
}
return 0.0;
}
/**
* http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html;
* [SPI_getbinval](https://www.postgresql.org/docs/8.1/static/spi-spi-getbinval.html)
* [Datum](https://doxygen.postgresql.org/datum_8h.html)
* [DatumGetInt16](https://doxygen.postgresql.org/postgres_8h.html#aec991e04209850f29a8a63df0c78ba2d)
*
* @param[in] tuple input row to be examined.
* @param[in] tupdesc tuple descriptor
* @param[in] info contain column information.
* @param[in] default_value returned when column contain NULL value.
* @throw ERROR Unexpected type in column. Expected column type is CHAR.
* @throw ERROR When value of column is NULL.
* @return Char type of column value is returned.
*/
char getChar(
const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info, char default_value) {
Datum binval;
bool isNull;
char value = default_value;
binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isNull);
if (!(info.type == BPCHAROID)) {
throw std::string("Unexpected type in column type of ") + info.name + ". Expected CHAR";
}
if (!isNull) {
value = reinterpret_cast<char*>(binval)[1];
} else {
if (info.strict) {
throw std::string("Unexpected Null value in column ") + info.name;
}
value = default_value;
}
return value;
}
/** @brief get the array contents from postgres
*
* @details This function generates the array inputs according to their type
* received through @a ArrayType *v parameter and store them in @a c_array. It
* can be empty also if received @a allow_empty true. The cases of failure are:-
* 1. When @a ndim is not equal to one dimension.
* 2. When no element is found i.e. nitems is zero or negative.
* 3. If the element type doesn't lie in switch cases, give the error of expected array of any integer type
* 4. When size of @a c_array is out of range or memory.
* 5. When null value is found in the array.
*
* All these failures are represented as error through @a elog.
* @param[in] v Pointer to the postgres C array
*
* @pre the array has to be one dimension
* @pre Must have elements (when allow_empty is false)
*
* @returns set of elements on the PostgreSQL array
*/
std::unordered_set<uint32_t>
pgarray_to_unordered_set(ArrayType *v) {
std::unordered_set<uint32_t> results;
if (!v) return results;
auto element_type = ARR_ELEMTYPE(v);
auto dim = ARR_DIMS(v);
auto ndim = ARR_NDIM(v);
auto nitems = ArrayGetNItems(ndim, dim);
Datum *elements = nullptr;
bool *nulls = nullptr;
int16 typlen;
bool typbyval;
char typalign;
if (ndim == 0 || nitems <= 0) {
return results;
}
if (ndim != 1) {
throw std::string("One dimension expected");
}
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
switch (element_type) {
case INT2OID:
case INT4OID:
case INT8OID:
break;
default:
throw std::string("Expected array of ANY-INTEGER");
}
deconstruct_array(v, element_type, typlen, typbyval,
typalign, &elements, &nulls,
&nitems);
int64_t data(0);
for (int i = 0; i < nitems; i++) {
if (nulls[i]) {
throw std::string("NULL value found in Array!");
} else {
switch (element_type) {
case INT2OID:
data = static_cast<int64_t>(DatumGetInt16(elements[i]));
break;
case INT4OID:
data = static_cast<int64_t>(DatumGetInt32(elements[i]));
break;
case INT8OID:
data = DatumGetInt64(elements[i]);
break;
}
}
/*
* Before saving, check if its a uint32_t
*/
if (data < 0 || data > std::numeric_limits<uint32_t>::max()) {
throw std::string("Illegal value found on array");
}
results.insert(static_cast<uint32_t>(data));
}
pfree(elements);
pfree(nulls);
return results;
}
/** @brief get the array contents from postgres
*
* @details This function generates the array inputs according to their type
* received through @a ArrayType *v parameter and store them in @a c_array. It
* can be empty also if received @a allow_empty true. The cases of failure are:-
* 1. When @a ndim is not equal to one dimension.
* 2. When no element is found i.e. nitems is zero or negative.
* 3. If the element type doesn't lie in switch cases, give the error of expected array of any integer type
* 4. When size of @a c_array is out of range or memory.
* 5. When null value is found in the array.
*
* All these failures are represented as error through @a elog.
* @param[in] v Pointer to the postgres C array
* @param[in] allow_empty flag to allow empty arrays
*
* @pre the array has to be one dimension
* @pre Must have elements (when allow_empty is false)
*
* @returns Vector of elements of the PostgreSQL array
*/
std::vector<int64_t>
get_pgarray(ArrayType *v, bool allow_empty) {
std::vector<int64_t> results;
if (!v) return results;
auto element_type = ARR_ELEMTYPE(v);
auto dim = ARR_DIMS(v);
auto ndim = ARR_NDIM(v);
auto nitems = ArrayGetNItems(ndim, dim);
Datum *elements = nullptr;
bool *nulls = nullptr;
int16 typlen;
bool typbyval;
char typalign;
if (allow_empty && (ndim == 0 || nitems <= 0)) {
return results;
}
if (ndim != 1) {
throw std::string("One dimension expected");
}
if (nitems <= 0) {
throw std::string("No elements found");
}
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
/* validate input data type */
switch (element_type) {
case INT2OID:
case INT4OID:
case INT8OID:
break;
default:
throw std::string("Expected array of ANY-INTEGER");
}
deconstruct_array(v, element_type, typlen, typbyval,
typalign, &elements, &nulls,
&nitems);
int64_t data(0);
results.reserve(static_cast<size_t>(nitems));
for (int i = 0; i < nitems; i++) {
if (nulls[i]) {
throw std::string("NULL value found in Array!");
} else {
switch (element_type) {
case INT2OID:
data = static_cast<int64_t>(DatumGetInt16(elements[i]));
break;
case INT4OID:
data = static_cast<int64_t>(DatumGetInt32(elements[i]));
break;
case INT8OID:
data = DatumGetInt64(elements[i]);
break;
}
}
results.push_back(data);
}
pfree(elements);
pfree(nulls);
return results;
}
std::vector<int64_t>
get_BigIntArr_wEmpty(
const HeapTuple tuple, const TupleDesc &tupdesc,
const vrprouting::Info &info) {
bool is_null = false;
Datum raw_array = SPI_getbinval(tuple, tupdesc, info.colNumber, &is_null);
/*
* [DatumGetArrayTypeP](https://doxygen.postgresql.org/array_8h.html#aa1b8e77c103863862e06a7b7c07ec532)
* [pgr_get_bigIntArray](http://docs.vrprouting.org/doxy/2.2/arrays__input_8c_source.html)
*/
if (!raw_array) return std::vector<int64_t>();
ArrayType *pg_array = DatumGetArrayTypeP(raw_array);
return get_pgarray(pg_array, true);
}
} // namespace
namespace vrprouting {
namespace detail {
std::vector<int64_t>
get_any_positive_array(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) {
if (!column_found(info)) return std::vector<int64_t>();
auto data = get_BigIntArr_wEmpty(tuple, tupdesc, info);
for (const auto &e : data) {
if (e < 0) throw std::string("Unexpected negative value in array '") + info.name + "'";
}
return data;
}
std::vector<uint32_t>
get_uint_array(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) {
bool is_null = false;
Datum raw_array = SPI_getbinval(tuple, tupdesc, info.colNumber, &is_null);
if (!raw_array) return std::vector<uint32_t>();
ArrayType *pg_array = DatumGetArrayTypeP(raw_array);
auto data = get_pgarray(pg_array, true);
std::vector<uint32_t> results(data.begin(), data.end());
return results;
}
/**
* @param [in] tuple
* @param [in] tupdesc
* @param [in] info about the column been fetched
* @param [in] opt_value default value when the column does not exist
*
* @returns The value found
* @returns opt_value when the column does not exist
*
* exceptions when the value is negative
* @pre for positive values only
*/
TInterval
get_interval(
const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, TInterval opt_value) {
TInterval value = column_found(info)? getInterval(tuple, tupdesc, info) : opt_value;
if (value < 0) throw std::string("Unexpected negative value in column '") + info.name + "'";
return (TInterval) value;
}
/**
* @param [in] tuple
* @param [in] tupdesc
* @param [in] info about the column been fetched
* @param [in] opt_value default value when the column does not exist
*
* @returns The value found
* @returns opt_value when the column does not exist
*/
TTimestamp
get_timestamp(
const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, TTimestamp opt_value) {
return column_found(info)? getTimeStamp(tuple, tupdesc, info) : opt_value;
}
/**
* @param [in] tuple from postgres
* @param [in] tupdesc from postgres
* @param [in] info about the column been fetched
* @param [in] opt_value default value when the column does not exist
*
* @returns The value found
* @returns opt_value when the column does not exist
*
* Used with vrprouting::ANY_INTEGER
*/
int64_t
get_anyinteger(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, int64_t opt_value) {
return column_found(info)? getBigInt(tuple, tupdesc, info) : opt_value;
}
} // namespace detail
/**
* @param[in] info column information
* @return @b TRUE when colNumber exist.
* @b FALSE when colNumber was not found.
*
* [SPI_ERROR_NOATTRIBUTE](https://doxygen.postgresql.org/spi_8h.html#ac1512d8aaa23c2d57bb0d1eb8f453ee2)
*/
bool column_found(const Info &info) {
return !(info.colNumber == SPI_ERROR_NOATTRIBUTE);
}
/**
* @param[in] tupdesc tuple descriptor
* @param[in] info contain one or more column information.
*
* @throw ERROR Unknown type of column.
*/
void fetch_column_info(
const TupleDesc &tupdesc,
std::vector<vrprouting::Info> &info) {
for (auto &coldata : info) {
if (get_column_info(tupdesc, coldata)) {
switch (coldata.eType) {
case ANY_INTEGER:
case TINTERVAL:
case ANY_UINT:
check_any_integer_type(coldata);
break;
case ANY_NUMERICAL:
check_any_numerical_type(coldata);
break;
case TEXT:
check_text_type(coldata);
break;
case CHAR1:
check_char_type(coldata);
break;
case ANY_INTEGER_ARRAY:
case ANY_POSITIVE_ARRAY:
check_any_integerarray_type(coldata);
break;
case POSITIVE_INTEGER:
case INTEGER:
check_integer_type(coldata);
break;
case JSONB:
check_jsonb_type(coldata);
break;
case INTEGER_ARRAY:
case ANY_UINT_ARRAY:
check_integerarray_type(coldata);
break;
case TIMESTAMP:
check_timestamp_type(coldata);
break;
case INTERVAL:
check_interval_type(coldata);
break;
default:
throw std::string("Case not found in column '") + coldata.name + "' Please inform the developers";
}
}
}
}
/**
* @param [in] tuple from postgres
* @param [in] tupdesc from postgres
* @param [in] info about the column been fetched
* @param [in] opt_value default value when the column does not exist
*
* @returns The value found
* @returns opt_value when the column does not exist
*
* Used with vrprouting::ANY_NUMERICAL
*/
double
get_anynumerical(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, double opt_value) {
return column_found(info)? getFloat8(tuple, tupdesc, info) : opt_value;
}
/**
* @param [in] tuple
* @param [in] tupdesc
* @param [in] info about the column been fetched
* @param [in] opt_value default value when the column does not exist
*
* @returns The value found
* @returns opt_value when the column does not exist
*/
char
get_char(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, char opt_value) {
return getChar(tuple, tupdesc, info, opt_value);
}
/**
* @param [in] tuple
* @param [in] tupdesc
* @param [in] info about the column been fetched
*
* @returns "{}" (empty jsonb) when when the column does not exist
*/
std::string get_jsonb(const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) {
bool isnull;
return column_found(info)? DatumGetCString(SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull)) : "{}";
}
std::unordered_set<uint32_t>
get_uint_unordered_set(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) {
bool is_null = false;
Datum raw_array = SPI_getbinval(tuple, tupdesc, info.colNumber, &is_null);
if (!raw_array) return std::unordered_set<uint32_t>();
ArrayType *pg_array = DatumGetArrayTypeP(raw_array);
return pgarray_to_unordered_set(pg_array);
}
/**
* Steps:
* 1) Similar to: https://doxygen.postgresql.org/backend_2utils_2adt_2timestamp_8c.html#a52973f03ed8296b632d4028121f7e077
* 2) Using time.h to convert
*
* from time.h
* struct tm
* timezone
*/
TTimestamp
get_timestamp_without_timezone(TTimestamp timestamp) {
/*
* step 1
*/
Timestamp date;
Timestamp time = timestamp;
TMODULO(time, date, USECS_PER_DAY);
if (time < INT64CONST(0)) {
time += USECS_PER_DAY;
date -= 1;
}
date += POSTGRES_EPOCH_JDATE;
/* Julian day routine does not work for negative Julian days */
if (date < 0 || date > (Timestamp) INT_MAX) {
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Julian day routine does not work for negative Julian days")));
}
/*
* using structure from time.h to store values
*/
struct tm info;
fsec_t fsec;
/*
* calling postgres functions
*/
j2date(static_cast<int>(date), &info.tm_year, &info.tm_mon, &info.tm_mday);
dt2time(time, &info.tm_hour, &info.tm_min, &info.tm_sec, &fsec);
/*
* adjust values before calling mktime
*/
info.tm_isdst = -1;
info.tm_year = info.tm_year - 1900;
info.tm_mon = info.tm_mon - 1;
/*
* mktime & timezone are defined in time.h
*/
return mktime(&info) - timezone;
}
} // namespace vrprouting