25 #include "./pgr_types.h"
28 #include "catalog/pg_type.h"
29 #include "executor/spi.h"
33 #include "./debug_macro.h"
34 #include "./get_check_data.h"
37 pgr_stradd(
const char* a,
const char* b) {
38 size_t len = strlen(a) + strlen(b);
39 char *ret = (
char*)malloc(len *
sizeof(
char) + 1);
41 return strcat(strcat(ret, a), b);
45 column_found(
int colNumber) {
46 return !(colNumber == SPI_ERROR_NOATTRIBUTE);
53 PGR_DBG(
"Fetching column info of %s", info->name);
54 info->colNumber = SPI_fnumber(SPI_tuptable->tupdesc, info->name);
55 if (info->strict && !column_found(info->colNumber)) {
56 elog(ERROR,
"Column '%s' not Found", info->name);
58 if (column_found(info->colNumber)) {
59 (info->type) = SPI_gettypeid(SPI_tuptable->tupdesc, (info->colNumber));
60 if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
61 elog(ERROR,
"Type of column '%s' not Found", info->name);
63 PGR_DBG(
"Column %s found: %llu", info->name, info->type);
66 PGR_DBG(
"Column %s not found", info->name);
71 void pgr_fetch_column_info(
75 for (i = 0; i < info_size; ++i) {
76 if (fetch_column_info(&info[i])) {
77 switch (info[i].eType) {
79 pgr_check_any_integer_type(info[i]);
82 pgr_check_any_numerical_type(info[i]);
85 pgr_check_text_type(info[i]);
88 pgr_check_char_type(info[i]);
91 elog(ERROR,
"Unknown type of column %s", info[i].name);
100 if (!(info.type == BPCHAROID)) {
101 elog(ERROR,
"Unexpected Column '%s' type. Expected CHAR", info.name);
107 if (!(info.type == TEXTOID)) {
108 elog(ERROR,
"Unexpected Column '%s' type. Expected TEXT", info.name);
114 if (!(info.type == INT2OID
115 || info.type == INT4OID
116 || info.type == INT8OID)) {
118 "Unexpected Column '%s' type. Expected ANY-INTEGER",
124 if (!(info.type == INT2OID
125 || info.type == INT4OID
126 || info.type == INT8OID
127 || info.type == FLOAT4OID
128 || info.type == FLOAT8OID)) {
130 "Unexpected Column '%s' type. Expected ANY-NUMERICAL",
142 bool strict,
char default_value) {
145 char value = default_value;
147 binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isNull);
148 if (!(info.type == BPCHAROID)) {
149 elog(ERROR,
"Unexpected Column type of %s. Expected CHAR", info.name);
152 value = ((
char*)binval)[1];
155 elog(ERROR,
"Unexpected Null value in column %s", info.name);
157 value = default_value;
163 pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc,
Column_info_t info) {
167 binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
169 elog(ERROR,
"Unexpected Null value in column %s", info.name);
172 value = (int64_t) DatumGetInt16(binval);
175 value = (int64_t) DatumGetInt32(binval);
178 value = DatumGetInt64(binval);
182 "Unexpected Column type of %s. Expected ANY-INTEGER",
185 PGR_DBG(
"Variable: %s Value: %lld", info.name, value);
190 pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc,
Column_info_t info) {
194 binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
196 elog(ERROR,
"Unexpected Null value in column %s", info.name);
200 value = (double) DatumGetInt16(binval);
203 value = (double) DatumGetInt32(binval);
206 value = (double) DatumGetInt64(binval);
209 value = (double) DatumGetFloat4(binval);
212 value = DatumGetFloat8(binval);
216 "Unexpected Column type of %s. Expected ANY-NUMERICAL",
219 PGR_DBG(
"Variable: %s Value: %lf", info.name, value);
224 pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc,
Column_info_t info) {
227 val = SPI_getvalue(*tuple, *tupdesc, info.colNumber);
228 value = DatumGetCString(&val);