PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
postgres_connection.c
Go to the documentation of this file.
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_connection.h"
26 #include "utils/builtins.h"
27 
28 #include "catalog/pg_type.h"
29 
30 #include <string.h>
31 
32 #include "./debug_macro.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"
39  " edge/fraction/side combination found.");
40  break;
41  case 2:
42  elog(ERROR, "Internal: Unexpected mismatch "
43  "count and sequence number on results");
44  break;
45  default:
46  elog(ERROR, "Unknown exception");
47  }
48 }
49 
50 
51 char*
52 pgr_cstring2char(const char *inStr) {
53 
54  if(!inStr) return NULL;
55 
56  char *outStr;
57  outStr = palloc(strlen(inStr));
58  if (!outStr) return NULL;
59 
60  memcpy(outStr, inStr, strlen(inStr));
61 
62  outStr[strlen(inStr)] = '\0';
63 
64  return outStr;
65 }
66 
67 
68 
69 // http://www.postgresql.org/docs/9.4/static/spi-spi-finish.html
70 void
72 #if 0
73  PGR_DBG("Disconnecting SPI");
74 #endif
75  int code = SPI_OK_FINISH;
76  code = SPI_finish();
77  if (code != SPI_OK_FINISH) { // SPI_ERROR_UNCONNECTED
78  elog(ERROR, "There was no connection to SPI");
79  }
80 }
81 
82 void
84 #if 0
85  PGR_DBG("Connecting to SPI");
86 #endif
87  int SPIcode;
88  SPIcode = SPI_connect();
89  if (SPIcode != SPI_OK_CONNECT) {
90  elog(ERROR, "Couldn't open a connection to SPI");
91  }
92 }
93 
94 SPIPlanPtr
95 pgr_SPI_prepare(char* sql) {
96 #if 0
97  PGR_DBG("Preparing Plan");
98 #endif
99  SPIPlanPtr SPIplan;
100  SPIplan = SPI_prepare(sql, 0, NULL);
101  if (SPIplan == NULL) {
102  elog(ERROR, "Couldn't create query plan via SPI: %s", sql);
103  }
104  return SPIplan;
105 }
106 
107 Portal
108 pgr_SPI_cursor_open(SPIPlanPtr SPIplan) {
109 #if 0
110  PGR_DBG("Opening Portal");
111 #endif
112  Portal SPIportal;
113  SPIportal = SPI_cursor_open(NULL, SPIplan, NULL, NULL, true);
114  if (SPIportal == NULL) {
115  elog(ERROR, "SPI_cursor_open returns NULL");
116  }
117  return SPIportal;
118 }
#define PGR_DBG(...)
Definition: debug_macro.h:34
void pgr_SPI_finish(void)
void pgr_send_error(int errcode)
char * pgr_cstring2char(const char *inStr)
void pgr_SPI_connect(void)
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
SPIPlanPtr pgr_SPI_prepare(char *sql)