pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
postgres_connection.c
1 /*PGR-GNU*****************************************************************
2 File: postgres_connection.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 "postgres.h"
26 #include "catalog/pg_type.h"
27 #include "executor/spi.h"
28 
29 
30 // #define DEBUG
31 #include "./debug_macro.h"
32 #include "./postgres_connection.h"
33 
34 void
35 pgr_send_error(int errcode) {
36  switch (errcode) {
37  case 1:
38  elog(ERROR, "Unexpected point(s) with same pid but different edge/fraction/side combination found.");
39  break;
40  case 2:
41  elog(ERROR, "Internal: Unexpected missmatch count and sequence number on results");
42  break;
43  default:
44  elog(ERROR, "Unknown exception");
45  }
46 }
47 
48 
49 char*
50 pgr_text2char(text *in) {
51  char *out = palloc(VARSIZE(in));
52 
53  memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ);
54  out[VARSIZE(in) - VARHDRSZ] = '\0';
55  return out;
56 }
57 
58 
59 // http://www.postgresql.org/docs/9.4/static/spi-spi-finish.html
60 void
61 pgr_SPI_finish(void) {
62  PGR_DBG("Disconnecting SPI");
63  int code = SPI_OK_FINISH;
64  code = SPI_finish();
65  if (code != SPI_OK_FINISH) { // SPI_ERROR_UNCONNECTED
66  elog(ERROR, "There was no connection to SPI");
67  }
68 }
69 
70 void
71 pgr_SPI_connect(void) {
72  PGR_DBG("Connecting to SPI");
73  int SPIcode;
74  SPIcode = SPI_connect();
75  if (SPIcode != SPI_OK_CONNECT) {
76  elog(ERROR, "Couldn't open a connection to SPI");
77  }
78 }
79 
80 SPIPlanPtr
81 pgr_SPI_prepare(char* sql) {
82  PGR_DBG("Preparing Plan");
83  SPIPlanPtr SPIplan;
84  SPIplan = SPI_prepare(sql, 0, NULL);
85  if (SPIplan == NULL) {
86  elog(ERROR, "Couldn't create query plan via SPI: %s", sql);
87  }
88  return SPIplan;
89 }
90 
91 Portal
92 pgr_SPI_cursor_open(SPIPlanPtr SPIplan) {
93  PGR_DBG("Opening Portal");
94  Portal SPIportal;
95  SPIportal = SPI_cursor_open(NULL, SPIplan, NULL, NULL, true);
96  if (SPIportal == NULL) {
97  elog(ERROR, "SPI_cursor_open returns NULL");
98  }
99  return SPIportal;
100 }