LCOV - code coverage report
Current view: directory - frmts/postgisraster - postgisrasterdriver.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 35 0 0.0 %
Date: 2011-12-18 Functions: 6 0 0.0 %

       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               0 : PostGISRasterDriver::PostGISRasterDriver() {
      37               0 :     papoConnection = NULL;
      38               0 :     nRefCount = 0;
      39               0 : }
      40                 : 
      41                 : /************************
      42                 :  * \brief Destructor
      43                 :  ************************/
      44               0 : PostGISRasterDriver::~PostGISRasterDriver() {
      45               0 :     int i = 0;
      46                 : 
      47               0 :     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               0 :     if (papoConnection)
      58               0 :         CPLFree(papoConnection);
      59               0 : }
      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               0 :     char ** papszParams = NULL;
      83                 : 
      84                 :     /**
      85                 :      * Look for an existing connection in the list
      86                 :      **/
      87               0 :     for (i = 0; i < nRefCount; i++) {
      88               0 :         if (EQUAL(pszUserIn, PQuser(papoConnection[i])) &&
      89               0 :                 EQUAL(pszPasswordIn, PQpass(papoConnection[i])) &&
      90               0 :                 EQUAL(pszHostIn, PQhost(papoConnection[i])) &&
      91               0 :                 EQUAL(pszPortIn, PQport(papoConnection[i]))) {
      92               0 :             return papoConnection[i];
      93                 :         }
      94                 : 
      95                 :     }
      96                 : 
      97                 :     /**
      98                 :      * There's no existing connection. Create a new one.
      99                 :      **/
     100               0 :     poConn = PQconnectdb(pszConnectionString);
     101               0 :     if (poConn == NULL ||
     102                 :             PQstatus(poConn) == CONNECTION_BAD) {
     103                 :         CPLError(CE_Failure, CPLE_AppDefined, "PGconnectcb failed: %s\n",
     104               0 :                 PQerrorMessage(poConn));
     105               0 :         PQfinish(poConn);
     106               0 :         return NULL;
     107                 :     }
     108                 : 
     109                 :     /**
     110                 :      * Save connection in connection list.
     111                 :      **/
     112               0 :     nRefCount++;
     113                 :     papoConnection = (PGconn**) CPLRealloc(papoConnection,
     114               0 :             sizeof (PGconn*) * nRefCount);
     115               0 :     if (NULL != papoConnection) {
     116               0 :         papoConnection[nRefCount - 1] = poConn;
     117               0 :         return poConn;
     118                 :     }
     119                 :     else {
     120                 :         CPLError(CE_Failure, CPLE_AppDefined, "Reallocation for new connection\
     121               0 :             failed.\n");
     122               0 :         PQfinish(poConn);
     123               0 :         return NULL;
     124                 :     }
     125                 : 
     126                 : }
     127                 : 
     128                 : 
     129                 : 

Generated by: LCOV version 1.7