PGROUTING  2.5
 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 
26 
27 #include "catalog/pg_type.h"
28 
29 #include "c_common/debug_macro.h"
30 
31 char*
32 pgr_stradd(const char* a, const char* b) {
33  size_t len = strlen(a) + strlen(b);
34  char *ret = (char*)malloc(len * sizeof(char) + 1);
35  *ret = '\0';
36  return strcat(strcat(ret, a), b);
37 }
38 
39 bool
40 column_found(int colNumber) {
41  return !(colNumber == SPI_ERROR_NOATTRIBUTE);
42 }
43 
44 static
45 bool
47  Column_info_t *info) {
48 #if 0
49  PGR_DBG("Fetching column info of %s", info->name);
50 #endif
51  info->colNumber = SPI_fnumber(SPI_tuptable->tupdesc, info->name);
52  if (info->strict && !column_found(info->colNumber)) {
53  elog(ERROR, "Column '%s' not Found", info->name);
54  }
55  if (column_found(info->colNumber)) {
56  (info->type) = SPI_gettypeid(SPI_tuptable->tupdesc, (info->colNumber));
57  if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
58  elog(ERROR, "Type of column '%s' not Found", info->name);
59  }
60 #if 0
61  PGR_DBG("Column %s found: %lu", info->name, info->type);
62 #endif
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  case ANY_INTEGER_ARRAY:
91  break;
92  default:
93  elog(ERROR, "Unknown type of column %s", info[i].name);
94  }
95  }
96  }
97 }
98 
99 
100 void
102  if (!(info.type == BPCHAROID)) {
103  elog(ERROR, "Unexpected Column '%s' type. Expected CHAR", info.name);
104  }
105 }
106 
107 void
109  if (!(info.type == TEXTOID)) {
110  elog(ERROR, "Unexpected Column '%s' type. Expected TEXT", info.name);
111  }
112 }
113 
114 void
116  if (!(info.type == INT2OID
117  || info.type == INT4OID
118  || info.type == INT8OID)) {
119  elog(ERROR,
120  "Unexpected Column '%s' type. Expected ANY-INTEGER",
121  info.name);
122  }
123 }
124 
125 void
127  if (!(info.type == INT2ARRAYOID
128  || info.type == INT4ARRAYOID
129  || info.type == 1016)) {
130  elog(ERROR,
131  "Unexpected Column '%s' type. Expected ANY-INTEGER-ARRAY",
132  info.name);
133  }
134 }
135 
137  if (!(info.type == INT2OID
138  || info.type == INT4OID
139  || info.type == INT8OID
140  || info.type == FLOAT4OID
141  || info.type == FLOAT8OID)) {
142  elog(ERROR,
143  "Unexpected Column '%s' type. Expected ANY-NUMERICAL",
144  info.name);
145  }
146 }
147 
148 
149 /*
150  * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html;
151  */
152 char
154  HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info,
155  bool strict, char default_value) {
156  Datum binval;
157  bool isNull;
158  char value = default_value;
159 
160  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isNull);
161  if (!(info.type == BPCHAROID)) {
162  elog(ERROR, "Unexpected Column type of %s. Expected CHAR", info.name);
163  }
164  if (!isNull) {
165  value = ((char*)binval)[1];
166  } else {
167  if (strict) {
168  elog(ERROR, "Unexpected Null value in column %s", info.name);
169  }
170  value = default_value;
171  }
172  return value;
173 }
174 
175 int64_t
176 pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
177  Datum binval;
178  bool isnull;
179  int64_t value = 0;
180  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
181  if (isnull)
182  elog(ERROR, "Unexpected Null value in column %s", info.name);
183  switch (info.type) {
184  case INT2OID:
185  value = (int64_t) DatumGetInt16(binval);
186  break;
187  case INT4OID:
188  value = (int64_t) DatumGetInt32(binval);
189  break;
190  case INT8OID:
191  value = DatumGetInt64(binval);
192  break;
193  default:
194  elog(ERROR,
195  "Unexpected Column type of %s. Expected ANY-INTEGER",
196  info.name);
197  }
198 #if 0
199  PGR_DBG("Variable: %s Value: %ld", info.name, value);
200 #endif
201  return value;
202 }
203 
204 double
205 pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
206  Datum binval;
207  bool isnull = false;
208  double value = 0.0;
209  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
210  if (isnull)
211  elog(ERROR, "Unexpected Null value in column %s", info.name);
212 
213  switch (info.type) {
214  case INT2OID:
215  value = (double) DatumGetInt16(binval);
216  break;
217  case INT4OID:
218  value = (double) DatumGetInt32(binval);
219  break;
220  case INT8OID:
221  value = (double) DatumGetInt64(binval);
222  break;
223  case FLOAT4OID:
224  value = (double) DatumGetFloat4(binval);
225  break;
226  case FLOAT8OID:
227  value = DatumGetFloat8(binval);
228  break;
229  default:
230  elog(ERROR,
231  "Unexpected Column type of %s. Expected ANY-NUMERICAL",
232  info.name);
233  }
234 #if 0
235  PGR_DBG("Variable: %s Value: %lf", info.name, value);
236 #endif
237  return value;
238 }
239 
243 char*
244 pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
245  return DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info.colNumber));
246 }
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)
char * pgr_stradd(const char *a, const char *b)
uint64_t type
Definition: column_info_t.h:73
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)
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)