1 : /******************************************************************************
2 : * File : PostGISRasterDriver.cpp
3 : * Project: PostGIS Raster driver
4 : * Purpose: Implements PostGIS Raster driver class methods
5 : * Author: Jorge Arevalo, jorge.arevalo@deimos-space.com
6 : *
7 : * Last changes: $Id: $
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2010, Jorge Arevalo, jorge.arevalo@deimos-space.com
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ******************************************************************************/
30 : #include "postgisraster.h"
31 : #include "cpl_string.h"
32 :
33 : /************************
34 : * \brief Constructor
35 : ************************/
36 1093 : PostGISRasterDriver::PostGISRasterDriver() {
37 1093 : papoConnection = NULL;
38 1093 : nRefCount = 0;
39 1093 : }
40 :
41 : /************************
42 : * \brief Destructor
43 : ************************/
44 1057 : PostGISRasterDriver::~PostGISRasterDriver() {
45 1057 : int i = 0;
46 :
47 1057 : for (i = 0; i < nRefCount; i++) {
48 : /*
49 : * Segmentation fault here. Tested CPLFree and delete. Same result
50 : */
51 0 : if (papoConnection[i]) {
52 0 : PQfinish(papoConnection[i]);
53 : }
54 :
55 : }
56 :
57 1057 : if (papoConnection)
58 0 : CPLFree(papoConnection);
59 1057 : }
60 :
61 : /***************************************************************************
62 : * \brief Create a PQconn object and store it in a list
63 : *
64 : * The PostGIS Raster driver keeps the connection with the PostgreSQL database
65 : * server for as long it leaves. Following PostGISRasterDataset instance
66 : * can re-use the existing connection as long it used the same database,
67 : * same user name, same password and same port.
68 : *
69 : * The PostGIS Raster driver will keep a list of all the successful
70 : * connections so, when connection is requested and it does not exist
71 : * on the list a new one will be instantiated, added to the list and
72 : * returned to the caller.
73 : *
74 : * All connection will be destroyed when the PostGISRasterDriver is destroyed.
75 : *
76 : ***************************************************************************/
77 0 : PGconn* PostGISRasterDriver::GetConnection(const char* pszConnectionString,
78 : const char * pszHostIn, const char * pszPortIn, const char * pszUserIn,
79 : const char * pszPasswordIn) {
80 0 : int i = 0;
81 0 : PGconn * poConn = NULL;
82 :
83 : /**
84 : * Look for an existing connection in the list
85 : **/
86 0 : for (i = 0; i < nRefCount; i++) {
87 : CPLDebug("PostGIS_Raster", "PostGISRasterDriver::GetConnection(): "
88 : "User: %s\nPassword: %s\nHost: %s\nPort: %s", pszUserIn,
89 0 : pszPasswordIn, pszHostIn, pszPortIn);
90 0 : if (EQUAL(pszUserIn, PQuser(papoConnection[i])) &&
91 0 : EQUAL(pszPasswordIn, PQpass(papoConnection[i])) &&
92 0 : EQUAL(pszHostIn, PQhost(papoConnection[i])) &&
93 0 : EQUAL(pszPortIn, PQport(papoConnection[i]))) {
94 0 : return papoConnection[i];
95 : }
96 :
97 : }
98 :
99 : /**
100 : * There's no existing connection. Create a new one.
101 : **/
102 0 : poConn = PQconnectdb(pszConnectionString);
103 0 : if (poConn == NULL ||
104 : PQstatus(poConn) == CONNECTION_BAD) {
105 : CPLError(CE_Failure, CPLE_AppDefined, "PGconnectcb failed: %s\n",
106 0 : PQerrorMessage(poConn));
107 0 : PQfinish(poConn);
108 0 : return NULL;
109 : }
110 :
111 : /**
112 : * Save connection in connection list.
113 : **/
114 0 : nRefCount++;
115 : papoConnection = (PGconn**) CPLRealloc(papoConnection,
116 0 : sizeof (PGconn*) * nRefCount);
117 0 : if (NULL != papoConnection) {
118 0 : papoConnection[nRefCount - 1] = poConn;
119 0 : return poConn;
120 : }
121 : else {
122 : CPLError(CE_Failure, CPLE_AppDefined, "Reallocation for new connection\
123 0 : failed.\n");
124 0 : PQfinish(poConn);
125 0 : return NULL;
126 : }
127 :
128 : }
129 :
130 :
131 :
|