pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
get_check_data.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: get_check_data.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
25 #include <postgres.h>
26 #include "catalog/pg_type.h"
27 #include "executor/spi.h"
28 
29 
30 // #define DEBUG
31 #include "./pgr_types.h"
32 #include "./debug_macro.h"
33 #include "./get_check_data.h"
34 
35 char*
36 pgr_stradd(const char* a, const char* b) {
37  size_t len = strlen(a) + strlen(b);
38  char *ret = (char*)malloc(len * sizeof(char) + 1);
39  *ret = '\0';
40  return strcat(strcat(ret, a), b);
41 }
42 
43 bool
44 column_found(int colNumber) {
45  return !(colNumber == SPI_ERROR_NOATTRIBUTE);
46 }
47 
48 static
49 bool
51  Column_info_t *info) {
52  PGR_DBG("Fetching column info of %s", info->name);
53  info->colNumber = SPI_fnumber(SPI_tuptable->tupdesc, info->name);
54  if (info->strict && !column_found(info->colNumber)) {
55  elog(ERROR, "Column '%s' not Found", info->name);
56  }
57  if (column_found(info->colNumber)) {
58  (info->type) = SPI_gettypeid(SPI_tuptable->tupdesc, (info->colNumber));
59  if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
60  elog(ERROR, "Type of column '%s' not Found", info->name);
61  }
62  PGR_DBG("Column %s found: %lu", info->name, info->type);
63  return true;
64  }
65  PGR_DBG("Column %s not found", info->name);
66  return false;
67 }
68 
69 
71  Column_info_t info[],
72  int info_size) {
73  int i;
74  for (i = 0; i < info_size; ++i) {
75  if (fetch_column_info(&info[i])) {
76  switch (info[i].eType) {
77  case ANY_INTEGER:
79  break;
80  case ANY_NUMERICAL:
82  break;
83  case TEXT:
84  pgr_check_text_type(info[i]);
85  break;
86  case CHAR1:
87  pgr_check_char_type(info[i]);
88  break;
89  default:
90  elog(ERROR, "Unknown type of column %s", info[i].name);
91  }
92  }
93  }
94 }
95 
96 
97 void
99  if (!(info.type == BPCHAROID)) {
100  elog(ERROR, "Unexpected Column '%s' type. Expected CHAR", info.name);
101  }
102 }
103 
104 void
106  if (!(info.type == TEXTOID)) {
107  elog(ERROR, "Unexpected Column '%s' type. Expected TEXT", info.name);
108  }
109 }
110 
111 void
113  if (!(info.type == INT2OID
114  || info.type == INT4OID
115  || info.type == INT8OID)) {
116  elog(ERROR,
117  "Unexpected Column '%s' type. Expected ANY-INTEGER",
118  info.name);
119  }
120 }
121 
123  if (!(info.type == INT2OID
124  || info.type == INT4OID
125  || info.type == INT8OID
126  || info.type == FLOAT4OID
127  || info.type == FLOAT8OID)) {
128  elog(ERROR,
129  "Unexpected Column '%s' type. Expected ANY-NUMERICAL",
130  info.name);
131  }
132 }
133 
134 
135 /*
136  * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html;
137  */
138 char
140  HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info,
141  bool strict, char default_value) {
142  Datum binval;
143  bool isNull;
144  char value = default_value;
145 
146  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isNull);
147  if (!(info.type == BPCHAROID)) {
148  elog(ERROR, "Unexpected Column type of %s. Expected CHAR", info.name);
149  }
150  if (!isNull) {
151  value = ((char*)binval)[1];
152  } else {
153  if (strict) {
154  elog(ERROR, "Unexpected Null value in column %s", info.name);
155  }
156  value = default_value;
157  }
158  return value;
159 }
160 
161 int64_t
162 pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
163  Datum binval;
164  bool isnull;
165  int64_t value = 0;
166  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
167  if (isnull)
168  elog(ERROR, "Unexpected Null value in column %s", info.name);
169  switch (info.type) {
170  case INT2OID:
171  value = (int64_t) DatumGetInt16(binval);
172  break;
173  case INT4OID:
174  value = (int64_t) DatumGetInt32(binval);
175  break;
176  case INT8OID:
177  value = DatumGetInt64(binval);
178  break;
179  default:
180  elog(ERROR,
181  "Unexpected Column type of %s. Expected ANY-INTEGER",
182  info.name);
183  }
184  PGR_DBG("Variable: %s Value: %ld", info.name, value);
185  return value;
186 }
187 
188 double
189 pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
190  Datum binval;
191  bool isnull;
192  double value = 0.0;
193  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
194  if (isnull)
195  elog(ERROR, "Unexpected Null value in column %s", info.name);
196 
197  switch (info.type) {
198  case INT2OID:
199  value = (double) DatumGetInt16(binval);
200  break;
201  case INT4OID:
202  value = (double) DatumGetInt32(binval);
203  break;
204  case INT8OID:
205  value = (double) DatumGetInt64(binval);
206  break;
207  case FLOAT4OID:
208  value = (double) DatumGetFloat4(binval);
209  break;
210  case FLOAT8OID:
211  value = DatumGetFloat8(binval);
212  break;
213  default:
214  elog(ERROR,
215  "Unexpected Column type of %s. Expected ANY-NUMERICAL",
216  info.name);
217  }
218  PGR_DBG("Variable: %s Value: %lf", info.name, value);
219  return value;
220 }
221 
222 char*
223 pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
224  char* value = NULL;
225  char* val = NULL;
226  val = SPI_getvalue(*tuple, *tupdesc, info.colNumber);
227  value = DatumGetCString(&val);
228  pfree(val);
229  return value;
230 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
char pgr_SPI_getChar(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, bool strict, char default_value)
#define PGR_DBG(...)
Definition: debug_macro.h:33
void pgr_check_text_type(Column_info_t info)
char * pgr_stradd(const char *a, const char *b)
uint64_t type
Definition: pgr_types.h:186
static int a
Definition: tsplib.c:114
char * name
Definition: pgr_types.h:188
void pgr_fetch_column_info(Column_info_t info[], int info_size)
void pgr_check_any_numerical_type(Column_info_t info)
void pgr_check_any_integer_type(Column_info_t info)
static int b
Definition: tsplib.c:115
char * pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
bool column_found(int colNumber)
void pgr_check_char_type(Column_info_t info)
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
static bool fetch_column_info(Column_info_t *info)