PGROUTING  2.4
 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 "./../../common/src/postgres_connection.h"
26 
27 #include "catalog/pg_type.h"
28 
29 
30 #include "./pgr_types.h"
31 #include "./debug_macro.h"
32 #include "./get_check_data.h"
33 
34 char*
35 pgr_stradd(const char* a, const char* b) {
36  size_t len = strlen(a) + strlen(b);
37  char *ret = (char*)malloc(len * sizeof(char) + 1);
38  *ret = '\0';
39  return strcat(strcat(ret, a), b);
40 }
41 
42 bool
43 column_found(int colNumber) {
44  return !(colNumber == SPI_ERROR_NOATTRIBUTE);
45 }
46 
47 static
48 bool
50  Column_info_t *info) {
51 #if 0
52  PGR_DBG("Fetching column info of %s", info->name);
53 #endif
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 #if 0
64  PGR_DBG("Column %s found: %lu", info->name, info->type);
65 #endif
66  return true;
67  }
68  PGR_DBG("Column %s not found", info->name);
69  return false;
70 }
71 
72 
74  Column_info_t info[],
75  int info_size) {
76  int i;
77  for (i = 0; i < info_size; ++i) {
78  if (fetch_column_info(&info[i])) {
79  switch (info[i].eType) {
80  case ANY_INTEGER:
82  break;
83  case ANY_NUMERICAL:
85  break;
86  case TEXT:
87  pgr_check_text_type(info[i]);
88  break;
89  case CHAR1:
90  pgr_check_char_type(info[i]);
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 
126  if (!(info.type == INT2OID
127  || info.type == INT4OID
128  || info.type == INT8OID
129  || info.type == FLOAT4OID
130  || info.type == FLOAT8OID)) {
131  elog(ERROR,
132  "Unexpected Column '%s' type. Expected ANY-NUMERICAL",
133  info.name);
134  }
135 }
136 
137 
138 /*
139  * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html;
140  */
141 char
143  HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info,
144  bool strict, char default_value) {
145  Datum binval;
146  bool isNull;
147  char value = default_value;
148 
149  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isNull);
150  if (!(info.type == BPCHAROID)) {
151  elog(ERROR, "Unexpected Column type of %s. Expected CHAR", info.name);
152  }
153  if (!isNull) {
154  value = ((char*)binval)[1];
155  } else {
156  if (strict) {
157  elog(ERROR, "Unexpected Null value in column %s", info.name);
158  }
159  value = default_value;
160  }
161  return value;
162 }
163 
164 int64_t
165 pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
166  Datum binval;
167  bool isnull;
168  int64_t value = 0;
169  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
170  if (isnull)
171  elog(ERROR, "Unexpected Null value in column %s", info.name);
172  switch (info.type) {
173  case INT2OID:
174  value = (int64_t) DatumGetInt16(binval);
175  break;
176  case INT4OID:
177  value = (int64_t) DatumGetInt32(binval);
178  break;
179  case INT8OID:
180  value = DatumGetInt64(binval);
181  break;
182  default:
183  elog(ERROR,
184  "Unexpected Column type of %s. Expected ANY-INTEGER",
185  info.name);
186  }
187 #if 0
188  PGR_DBG("Variable: %s Value: %ld", info.name, value);
189 #endif
190  return value;
191 }
192 
193 double
194 pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
195  Datum binval;
196  bool isnull;
197  double value = 0.0;
198  binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull);
199  if (isnull)
200  elog(ERROR, "Unexpected Null value in column %s", info.name);
201 
202  switch (info.type) {
203  case INT2OID:
204  value = (double) DatumGetInt16(binval);
205  break;
206  case INT4OID:
207  value = (double) DatumGetInt32(binval);
208  break;
209  case INT8OID:
210  value = (double) DatumGetInt64(binval);
211  break;
212  case FLOAT4OID:
213  value = (double) DatumGetFloat4(binval);
214  break;
215  case FLOAT8OID:
216  value = DatumGetFloat8(binval);
217  break;
218  default:
219  elog(ERROR,
220  "Unexpected Column type of %s. Expected ANY-NUMERICAL",
221  info.name);
222  }
223 #if 0
224  PGR_DBG("Variable: %s Value: %lf", info.name, value);
225 #endif
226  return value;
227 }
228 
232 char*
233 pgr_SPI_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
234  return DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info.colNumber));
235 }
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: pgr_types.h:200
char * name
Definition: pgr_types.h:202
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)