PGROUTING  2.6
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 
26 #include "c_common/arrays_input.h"
27 
28 #include "catalog/pg_type.h"
29 
30 #include "c_common/debug_macro.h"
31 
32 
33 bool
34 column_found(int colNumber) {
35  /*
36  * [SPI_ERROR_NOATTRIBUTE](https://doxygen.postgresql.org/spi_8h.html#ac1512d8aaa23c2d57bb0d1eb8f453ee2)
37  */
38  return !(colNumber == SPI_ERROR_NOATTRIBUTE);
39 }
40 
41 static
42 bool
44  Column_info_t *info) {
45 /* TODO(vicky) Remove unused code */
46 #if 0
47  PGR_DBG("Fetching column info of %s", info->name);
48 #endif
49  /*
50  * [SPI_fnumber](https://www.postgresql.org/docs/8.2/static/spi-spi-fnumber.html)
51  */
52  info->colNumber = SPI_fnumber(SPI_tuptable->tupdesc, info->name);
53  if (info->strict && !column_found(info->colNumber)) {
54  elog(ERROR, "Column '%s' not Found", info->name);
55  }
56 
57  if (column_found(info->colNumber)) {
58  /*
59  * [SPI_gettypeid](https://www.postgresql.org/docs/9.1/static/spi-spi-gettypeid.html)
60  */
61  (info->type) = SPI_gettypeid(SPI_tuptable->tupdesc, (info->colNumber));
62  if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
63  elog(ERROR, "Type of column '%s' not Found", info->name);
64  }
65 /* TODO(vicky) Remove unused code */
66 #if 0
67  PGR_DBG("Column %s found: %lu", info->name, info->type);
68 #endif
69  return true;
70  }
71  PGR_DBG("Column %s not found", info->name);
72  return false;
73 }
74 
75 
77  Column_info_t info[],
78  int info_size) {
79  int i;
80  for (i = 0; i < info_size; ++i) {
81  if (fetch_column_info(&info[i])) {
82  switch (info[i].eType) {
83  case ANY_INTEGER:
85  break;
86  case ANY_NUMERICAL:
88  break;
89  case TEXT:
90  pgr_check_text_type(info[i]);
91  break;
92  case CHAR1:
93  pgr_check_char_type(info[i]);
94  break;
95  case ANY_INTEGER_ARRAY:
97  break;
98  default:
99  elog(ERROR, "Unknown type of column %s", info[i].name);
100  }
101  }
102  }
103 }
104 
105 /*
106  * [BPCHAROID](https://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html#afa7749dbe36d31874205189d9d6b21d7)
107  * [INT2ARRAYOID](https://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html#ac265fe7b0bb75fead13b16bf072722e9)
108  */
109 
110 void
112  if (!(info.type == BPCHAROID)) {
113  elog(ERROR, "Unexpected Column '%s' type. Expected CHAR", info.name);
114  }
115 }
116 
117 void
119  if (!(info.type == TEXTOID)) {
120  elog(ERROR, "Unexpected Column '%s' type. Expected TEXT", info.name);
121  }
122 }
123 
124 void
126  if (!(info.type == INT2OID
127  || info.type == INT4OID
128  || info.type == INT8OID)) {
129  elog(ERROR,
130  "Unexpected Column '%s' type. Expected ANY-INTEGER",
131  info.name);
132  }
133 }
134 
135 void
137  if (!(info.type == INT2ARRAYOID
138  || info.type == INT4ARRAYOID
139  || info.type == 1016)) {
140  elog(ERROR,
141  "Unexpected Column '%s' type. Expected ANY-INTEGER-ARRAY",
142  info.name);
143  }
144 }
145 
147  if (!(info.type == INT2OID
148  || info.type == INT4OID
149  || info.type == INT8OID
150  || info.type == FLOAT4OID
151  || info.type == FLOAT8OID)) {
152  elog(ERROR,
153  "Unexpected Column '%s' type. Expected ANY-NUMERICAL",
154  info.name);
155  }
156 }
157 
158 
159 /*
160  * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html;
161  * [SPI_getbinval](https://www.postgresql.org/docs/8.1/static/spi-spi-getbinval.html)
162  * [Datum](https://doxygen.postgresql.org/datum_8h.html)
163  * [DatumGetInt16](https://doxygen.postgresql.org/postgres_8h.html#aec991e04209850f29a8a63df0c78ba2d)
164  */
165 
166 char
168  HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info,
169  bool strict, char default_value) {
170  Datum binval;
171  bool isNull;
172  char value = default_value;
173 
174  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isNull);
175  if (!(info.type == BPCHAROID)) {
176  elog(ERROR, "Unexpected Column type of %s. Expected CHAR", info.name);
177  }
178  if (!isNull) {
179  value = ((char*)binval)[1];
180  } else {
181  if (strict) {
182  elog(ERROR, "Unexpected Null value in column %s", info.name);
183  }
184  value = default_value;
185  }
186  return value;
187 }
188 
189 
190 
191 
192 int64_t*
194  HeapTuple *tuple,
195  TupleDesc *tupdesc,
196  Column_info_t info,
197  uint64_t *the_size) {
198  bool is_null = false;
199 
200  Datum raw_array = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &is_null);
201  /*
202  * [DatumGetArrayTypeP](https://doxygen.postgresql.org/array_8h.html#aa1b8e77c103863862e06a7b7c07ec532)
203  * [pgr_get_bigIntArray](http://docs.pgrouting.org/doxy/2.2/arrays__input_8c_source.html)
204  */
205  ArrayType *pg_array = DatumGetArrayTypeP(raw_array);
206 
207  return (int64_t*) pgr_get_bigIntArray(the_size, pg_array);
208 }
209 
210 
211 
212 int64_t
213 pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
214  Datum binval;
215  bool isnull;
216  int64_t value = 0;
217  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
218  if (isnull)
219  elog(ERROR, "Unexpected Null value in column %s", info.name);
220  switch (info.type) {
221  case INT2OID:
222  value = (int64_t) DatumGetInt16(binval);
223  break;
224  case INT4OID:
225  value = (int64_t) DatumGetInt32(binval);
226  break;
227  case INT8OID:
228  value = DatumGetInt64(binval);
229  break;
230  default:
231  elog(ERROR,
232  "Unexpected Column type of %s. Expected ANY-INTEGER",
233  info.name);
234  }
235 /* TODO(vicky) Remove unused code */
236 #if 0
237  PGR_DBG("Variable: %s Value: %ld", info.name, value);
238 #endif
239  return value;
240 }
241 
242 double
243 pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
244  Datum binval;
245  bool isnull = false;
246  double value = 0.0;
247  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
248  if (isnull)
249  elog(ERROR, "Unexpected Null value in column %s", info.name);
250 
251  switch (info.type) {
252  case INT2OID:
253  value = (double) DatumGetInt16(binval);
254  break;
255  case INT4OID:
256  value = (double) DatumGetInt32(binval);
257  break;
258  case INT8OID:
259  value = (double) DatumGetInt64(binval);
260  break;
261  case FLOAT4OID:
262  value = (double) DatumGetFloat4(binval);
263  break;
264  case FLOAT8OID:
265  value = DatumGetFloat8(binval);
266  break;
267  default:
268  elog(ERROR,
269  "Unexpected Column type of %s. Expected ANY-NUMERICAL",
270  info.name);
271  }
272 /* TODO(vicky) Remove unused code */
273 #if 0
274  PGR_DBG("Variable: %s Value: %lf", info.name, value);
275 #endif
276  return value;
277 }
278 
282 /*
283  * [DatumGetCString](https://doxygen.postgresql.org/postgres_8h.html#ae401c8476d1a12b420e3061823a206a7)
284  */
285 char*
286 pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
287  return DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info.colNumber));
288 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in integer type.
char pgr_SPI_getChar(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, bool strict, char default_value)
Function return the value of specified column in char type.
#define PGR_DBG(...)
Definition: debug_macro.h:34
void pgr_check_text_type(Column_info_t info)
The function check whether column type is TEXT or not.
uint64_t type
Definition: column_info_t.h:73
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
void pgr_fetch_column_info(Column_info_t info[], int info_size)
Function tells expected type of each column and then check the correspondence type of each column...
int64_t * pgr_SPI_getBigIntArr(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, uint64_t *the_size)
Function returns the values of specified columns in array.
void pgr_check_any_numerical_type(Column_info_t info)
The function check whether column type is ANY-NUMERICAL.
void pgr_check_any_integer_type(Column_info_t info)
The function check whether column type is ANY-INTEGER or not.
char * pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
under development
bool column_found(int colNumber)
Function will check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUT...
void pgr_check_char_type(Column_info_t info)
The function check whether column type is CHAR or not.
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in double type.
static bool fetch_column_info(Column_info_t *info)
void pgr_check_any_integerarray_type(Column_info_t info)
The function check whether column type is ANY-INTEGER-ARRAY or not.