PGROUTING  2.6-dev
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  return !(colNumber == SPI_ERROR_NOATTRIBUTE);
36 }
37 
38 static
39 bool
41  Column_info_t *info) {
42 #if 0
43  PGR_DBG("Fetching column info of %s", info->name);
44 #endif
45  info->colNumber = SPI_fnumber(SPI_tuptable->tupdesc, info->name);
46  if (info->strict && !column_found(info->colNumber)) {
47  elog(ERROR, "Column '%s' not Found", info->name);
48  }
49 
50  if (column_found(info->colNumber)) {
51  (info->type) = SPI_gettypeid(SPI_tuptable->tupdesc, (info->colNumber));
52  if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
53  elog(ERROR, "Type of column '%s' not Found", info->name);
54  }
55 #if 0
56  PGR_DBG("Column %s found: %lu", info->name, info->type);
57 #endif
58  return true;
59  }
60  PGR_DBG("Column %s not found", info->name);
61  return false;
62 }
63 
64 
66  Column_info_t info[],
67  int info_size) {
68  int i;
69  for (i = 0; i < info_size; ++i) {
70  if (fetch_column_info(&info[i])) {
71  switch (info[i].eType) {
72  case ANY_INTEGER:
74  break;
75  case ANY_NUMERICAL:
77  break;
78  case TEXT:
79  pgr_check_text_type(info[i]);
80  break;
81  case CHAR1:
82  pgr_check_char_type(info[i]);
83  break;
84  case ANY_INTEGER_ARRAY:
86  break;
87  default:
88  elog(ERROR, "Unknown type of column %s", info[i].name);
89  }
90  }
91  }
92 }
93 
94 
95 
96 void
98  if (!(info.type == BPCHAROID)) {
99  elog(ERROR, "Unexpected Column '%s' type. Expected CHAR", info.name);
100  }
101 }
102 
103 void
105  if (!(info.type == TEXTOID)) {
106  elog(ERROR, "Unexpected Column '%s' type. Expected TEXT", info.name);
107  }
108 }
109 
110 void
112  if (!(info.type == INT2OID
113  || info.type == INT4OID
114  || info.type == INT8OID)) {
115  elog(ERROR,
116  "Unexpected Column '%s' type. Expected ANY-INTEGER",
117  info.name);
118  }
119 }
120 
121 void
123  if (!(info.type == INT2ARRAYOID
124  || info.type == INT4ARRAYOID
125  || info.type == 1016)) {
126  elog(ERROR,
127  "Unexpected Column '%s' type. Expected ANY-INTEGER-ARRAY",
128  info.name);
129  }
130 }
131 
133  if (!(info.type == INT2OID
134  || info.type == INT4OID
135  || info.type == INT8OID
136  || info.type == FLOAT4OID
137  || info.type == FLOAT8OID)) {
138  elog(ERROR,
139  "Unexpected Column '%s' type. Expected ANY-NUMERICAL",
140  info.name);
141  }
142 }
143 
144 
145 /*
146  * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html;
147  */
148 char
150  HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info,
151  bool strict, char default_value) {
152  Datum binval;
153  bool isNull;
154  char value = default_value;
155 
156  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isNull);
157  if (!(info.type == BPCHAROID)) {
158  elog(ERROR, "Unexpected Column type of %s. Expected CHAR", info.name);
159  }
160  if (!isNull) {
161  value = ((char*)binval)[1];
162  } else {
163  if (strict) {
164  elog(ERROR, "Unexpected Null value in column %s", info.name);
165  }
166  value = default_value;
167  }
168  return value;
169 }
170 
171 
172 
173 
174 int64_t*
176  HeapTuple *tuple,
177  TupleDesc *tupdesc,
178  Column_info_t info,
179  uint64_t *the_size) {
180  bool is_null = false;
181 
182  Datum raw_array = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &is_null);
183  ArrayType *pg_array = DatumGetArrayTypeP(raw_array);
184 
185  return (int64_t*) pgr_get_bigIntArray(the_size, pg_array);
186 }
187 
188 
189 
190 int64_t
191 pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
192  Datum binval;
193  bool isnull;
194  int64_t value = 0;
195  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
196  if (isnull)
197  elog(ERROR, "Unexpected Null value in column %s", info.name);
198  switch (info.type) {
199  case INT2OID:
200  value = (int64_t) DatumGetInt16(binval);
201  break;
202  case INT4OID:
203  value = (int64_t) DatumGetInt32(binval);
204  break;
205  case INT8OID:
206  value = DatumGetInt64(binval);
207  break;
208  default:
209  elog(ERROR,
210  "Unexpected Column type of %s. Expected ANY-INTEGER",
211  info.name);
212  }
213 #if 0
214  PGR_DBG("Variable: %s Value: %ld", info.name, value);
215 #endif
216  return value;
217 }
218 
219 double
220 pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
221  Datum binval;
222  bool isnull = false;
223  double value = 0.0;
224  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
225  if (isnull)
226  elog(ERROR, "Unexpected Null value in column %s", info.name);
227 
228  switch (info.type) {
229  case INT2OID:
230  value = (double) DatumGetInt16(binval);
231  break;
232  case INT4OID:
233  value = (double) DatumGetInt32(binval);
234  break;
235  case INT8OID:
236  value = (double) DatumGetInt64(binval);
237  break;
238  case FLOAT4OID:
239  value = (double) DatumGetFloat4(binval);
240  break;
241  case FLOAT8OID:
242  value = DatumGetFloat8(binval);
243  break;
244  default:
245  elog(ERROR,
246  "Unexpected Column type of %s. Expected ANY-NUMERICAL",
247  info.name);
248  }
249 #if 0
250  PGR_DBG("Variable: %s Value: %lf", info.name, value);
251 #endif
252  return value;
253 }
254 
258 char*
259 pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
260  return DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info.colNumber));
261 }
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:34
void pgr_check_text_type(Column_info_t info)
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)
int64_t * pgr_SPI_getBigIntArr(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, uint64_t *the_size)
void pgr_check_any_numerical_type(Column_info_t info)
void pgr_check_any_integer_type(Column_info_t info)
char * pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
under development
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)
void pgr_check_any_integerarray_type(Column_info_t info)