pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
get_check_data.c
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 "./pgr_types.h"
26 #include "postgres.h"
27 
28 #include "catalog/pg_type.h"
29 #include "executor/spi.h"
30 
31 
32 // #define DEBUG
33 #include "./debug_macro.h"
34 #include "./get_check_data.h"
35 
36 char*
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);
40  *ret = '\0';
41  return strcat(strcat(ret, a), b);
42 }
43 
44 bool
45 column_found(int colNumber) {
46  return !(colNumber == SPI_ERROR_NOATTRIBUTE);
47 }
48 
49 static
50 bool
51 fetch_column_info(
52  Column_info_t *info) {
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);
57  }
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);
62  }
63  PGR_DBG("Column %s found: %llu", info->name, info->type);
64  return true;
65  }
66  PGR_DBG("Column %s not found", info->name);
67  return false;
68 }
69 
70 
71 void pgr_fetch_column_info(
72  Column_info_t info[],
73  int info_size) {
74  int i;
75  for (i = 0; i < info_size; ++i) {
76  if (fetch_column_info(&info[i])) {
77  switch (info[i].eType) {
78  case ANY_INTEGER:
79  pgr_check_any_integer_type(info[i]);
80  break;
81  case ANY_NUMERICAL:
82  pgr_check_any_numerical_type(info[i]);
83  break;
84  case TEXT:
85  pgr_check_text_type(info[i]);
86  break;
87  case CHAR1:
88  pgr_check_char_type(info[i]);
89  break;
90  default:
91  elog(ERROR, "Unknown type of column %s", info[i].name);
92  }
93  }
94  }
95 }
96 
97 
98 void
99 pgr_check_char_type(Column_info_t info) {
100  if (!(info.type == BPCHAROID)) {
101  elog(ERROR, "Unexpected Column '%s' type. Expected CHAR", info.name);
102  }
103 }
104 
105 void
106 pgr_check_text_type(Column_info_t info) {
107  if (!(info.type == TEXTOID)) {
108  elog(ERROR, "Unexpected Column '%s' type. Expected TEXT", info.name);
109  }
110 }
111 
112 void
113 pgr_check_any_integer_type(Column_info_t info) {
114  if (!(info.type == INT2OID
115  || info.type == INT4OID
116  || info.type == INT8OID)) {
117  elog(ERROR,
118  "Unexpected Column '%s' type. Expected ANY-INTEGER",
119  info.name);
120  }
121 }
122 
123 void pgr_check_any_numerical_type(Column_info_t info) {
124  if (!(info.type == INT2OID
125  || info.type == INT4OID
126  || info.type == INT8OID
127  || info.type == FLOAT4OID
128  || info.type == FLOAT8OID)) {
129  elog(ERROR,
130  "Unexpected Column '%s' type. Expected ANY-NUMERICAL",
131  info.name);
132  }
133 }
134 
135 
136 /*
137  * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html;
138  */
139 char
140 pgr_SPI_getChar(
141  HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info,
142  bool strict, char default_value) {
143  Datum binval;
144  bool isNull;
145  char value = default_value;
146 
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);
150  }
151  if (!isNull) {
152  value = ((char*)binval)[1];
153  } else {
154  if (strict) {
155  elog(ERROR, "Unexpected Null value in column %s", info.name);
156  }
157  value = default_value;
158  }
159  return value;
160 }
161 
162 int64_t
163 pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
164  Datum binval;
165  bool isnull;
166  int64_t value = 0;
167  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
168  if (isnull)
169  elog(ERROR, "Unexpected Null value in column %s", info.name);
170  switch (info.type) {
171  case INT2OID:
172  value = (int64_t) DatumGetInt16(binval);
173  break;
174  case INT4OID:
175  value = (int64_t) DatumGetInt32(binval);
176  break;
177  case INT8OID:
178  value = DatumGetInt64(binval);
179  break;
180  default:
181  elog(ERROR,
182  "Unexpected Column type of %s. Expected ANY-INTEGER",
183  info.name);
184  }
185  PGR_DBG("Variable: %s Value: %lld", info.name, value);
186  return value;
187 }
188 
189 double
190 pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
191  Datum binval;
192  bool isnull;
193  double value = 0.0;
194  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
195  if (isnull)
196  elog(ERROR, "Unexpected Null value in column %s", info.name);
197 
198  switch (info.type) {
199  case INT2OID:
200  value = (double) DatumGetInt16(binval);
201  break;
202  case INT4OID:
203  value = (double) DatumGetInt32(binval);
204  break;
205  case INT8OID:
206  value = (double) DatumGetInt64(binval);
207  break;
208  case FLOAT4OID:
209  value = (double) DatumGetFloat4(binval);
210  break;
211  case FLOAT8OID:
212  value = DatumGetFloat8(binval);
213  break;
214  default:
215  elog(ERROR,
216  "Unexpected Column type of %s. Expected ANY-NUMERICAL",
217  info.name);
218  }
219  PGR_DBG("Variable: %s Value: %lf", info.name, value);
220  return value;
221 }
222 
223 char*
224 pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
225  char* value = NULL;
226  char* val = NULL;
227  val = SPI_getvalue(*tuple, *tupdesc, info.colNumber);
228  value = DatumGetCString(&val);
229  pfree(val);
230  return value;
231 }