LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/pg - ogrpgutility.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 37 41 90.2 %
Date: 2024-04-26 23:38:17 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Utility methods
       5             :  * Author:   Even Rouault, <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2009-2011, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * Permission is hereby granted, free of charge, to any person obtaining a
      11             :  * copy of this software and associated documentation files (the "Software"),
      12             :  * to deal in the Software without restriction, including without limitation
      13             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      14             :  * and/or sell copies of the Software, and to permit persons to whom the
      15             :  * Software is furnished to do so, subject to the following conditions:
      16             :  *
      17             :  * The above copyright notice and this permission notice shall be included
      18             :  * in all copies or substantial portions of the Software.
      19             :  *
      20             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      21             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      22             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      23             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      24             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      25             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      26             :  * DEALINGS IN THE SOFTWARE.
      27             :  ****************************************************************************/
      28             : 
      29             : #include "ogr_pg.h"
      30             : #include "cpl_conv.h"
      31             : 
      32             : /************************************************************************/
      33             : /*                         OGRPG_PQexec()                               */
      34             : /************************************************************************/
      35             : 
      36       14426 : PGresult *OGRPG_PQexec(PGconn *conn, const char *query,
      37             :                        int bMultipleCommandAllowed, int bErrorAsDebug)
      38             : {
      39             :     PGresult *hResult = bMultipleCommandAllowed
      40       14426 :                             ? PQexec(conn, query)
      41       13347 :                             : PQexecParams(conn, query, 0, nullptr, nullptr,
      42       14426 :                                            nullptr, nullptr, 0);
      43             : 
      44             : #ifdef DEBUG
      45       14426 :     const char *pszRetCode = "UNKNOWN";
      46       14426 :     char szNTuples[32] = {};
      47       14426 :     if (hResult)
      48             :     {
      49       14426 :         switch (PQresultStatus(hResult))
      50             :         {
      51        6580 :             case PGRES_TUPLES_OK:
      52        6580 :                 pszRetCode = "PGRES_TUPLES_OK";
      53        6580 :                 snprintf(szNTuples, sizeof(szNTuples), ", ntuples = %d",
      54             :                          PQntuples(hResult));
      55        6580 :                 break;
      56        7627 :             case PGRES_COMMAND_OK:
      57        7627 :                 pszRetCode = "PGRES_COMMAND_OK";
      58        7627 :                 break;
      59           0 :             case PGRES_NONFATAL_ERROR:
      60           0 :                 pszRetCode = "PGRES_NONFATAL_ERROR";
      61           0 :                 break;
      62          43 :             case PGRES_FATAL_ERROR:
      63          43 :                 pszRetCode = "PGRES_FATAL_ERROR";
      64          43 :                 break;
      65         176 :             default:
      66         176 :                 break;
      67             :         }
      68             :     }
      69       14426 :     if (bMultipleCommandAllowed)
      70        1079 :         CPLDebug("PG", "PQexec(%s) = %s%s", query, pszRetCode, szNTuples);
      71             :     else
      72       13347 :         CPLDebug("PG", "PQexecParams(%s) = %s%s", query, pszRetCode, szNTuples);
      73             : #endif
      74             : 
      75             :     /* -------------------------------------------------------------------- */
      76             :     /*      Generate an error report if an error occurred.                  */
      77             :     /* -------------------------------------------------------------------- */
      78       28852 :     if (!hResult || (PQresultStatus(hResult) == PGRES_NONFATAL_ERROR ||
      79       14426 :                      PQresultStatus(hResult) == PGRES_FATAL_ERROR))
      80             :     {
      81          43 :         if (bErrorAsDebug)
      82           5 :             CPLDebug("PG", "%s", PQerrorMessage(conn));
      83             :         else
      84          38 :             CPLError(CE_Failure, CPLE_AppDefined, "%s", PQerrorMessage(conn));
      85             :     }
      86             : 
      87       14426 :     return hResult;
      88             : }
      89             : 
      90             : /************************************************************************/
      91             : /*                       OGRPG_Check_Table_Exists()                     */
      92             : /************************************************************************/
      93             : 
      94         760 : bool OGRPG_Check_Table_Exists(PGconn *hPGConn, const char *pszTableName)
      95             : {
      96         760 :     CPLString osSQL;
      97             :     osSQL.Printf(
      98             :         "SELECT 1 FROM information_schema.tables WHERE table_name = %s LIMIT 1",
      99         760 :         OGRPGEscapeString(hPGConn, pszTableName).c_str());
     100         760 :     PGresult *hResult = OGRPG_PQexec(hPGConn, osSQL);
     101         760 :     bool bRet = (hResult && PQntuples(hResult) == 1);
     102         760 :     if (!bRet)
     103           0 :         CPLDebug("PG", "Does not have %s table", pszTableName);
     104         760 :     OGRPGClearResult(hResult);
     105        1520 :     return bRet;
     106             : }

Generated by: LCOV version 1.14