LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/pg - ogrpgutility.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 36 41 87.8 %
Date: 2024-11-21 22:18:42 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             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "ogr_pg.h"
      14             : #include "cpl_conv.h"
      15             : 
      16             : /************************************************************************/
      17             : /*                         OGRPG_PQexec()                               */
      18             : /************************************************************************/
      19             : 
      20       15480 : PGresult *OGRPG_PQexec(PGconn *conn, const char *query,
      21             :                        int bMultipleCommandAllowed, int bErrorAsDebug)
      22             : {
      23             :     PGresult *hResult = bMultipleCommandAllowed
      24       15480 :                             ? PQexec(conn, query)
      25       14354 :                             : PQexecParams(conn, query, 0, nullptr, nullptr,
      26       15480 :                                            nullptr, nullptr, 0);
      27             : 
      28             : #ifdef DEBUG
      29       15480 :     const char *pszRetCode = "UNKNOWN";
      30       15480 :     char szNTuples[32] = {};
      31       15480 :     if (hResult)
      32             :     {
      33       15480 :         switch (PQresultStatus(hResult))
      34             :         {
      35        7174 :             case PGRES_TUPLES_OK:
      36        7174 :                 pszRetCode = "PGRES_TUPLES_OK";
      37        7174 :                 snprintf(szNTuples, sizeof(szNTuples), ", ntuples = %d",
      38             :                          PQntuples(hResult));
      39        7174 :                 break;
      40        8096 :             case PGRES_COMMAND_OK:
      41        8096 :                 pszRetCode = "PGRES_COMMAND_OK";
      42        8096 :                 break;
      43           0 :             case PGRES_NONFATAL_ERROR:
      44           0 :                 pszRetCode = "PGRES_NONFATAL_ERROR";
      45           0 :                 break;
      46          25 :             case PGRES_FATAL_ERROR:
      47          25 :                 pszRetCode = "PGRES_FATAL_ERROR";
      48          25 :                 break;
      49         185 :             default:
      50         185 :                 break;
      51             :         }
      52             :     }
      53       15480 :     if (bMultipleCommandAllowed)
      54        1126 :         CPLDebug("PG", "PQexec(%s) = %s%s", query, pszRetCode, szNTuples);
      55             :     else
      56       14354 :         CPLDebug("PG", "PQexecParams(%s) = %s%s", query, pszRetCode, szNTuples);
      57             : #endif
      58             : 
      59             :     /* -------------------------------------------------------------------- */
      60             :     /*      Generate an error report if an error occurred.                  */
      61             :     /* -------------------------------------------------------------------- */
      62       30960 :     if (!hResult || (PQresultStatus(hResult) == PGRES_NONFATAL_ERROR ||
      63       15480 :                      PQresultStatus(hResult) == PGRES_FATAL_ERROR))
      64             :     {
      65          25 :         if (bErrorAsDebug)
      66           0 :             CPLDebug("PG", "%s", PQerrorMessage(conn));
      67             :         else
      68          25 :             CPLError(CE_Failure, CPLE_AppDefined, "%s", PQerrorMessage(conn));
      69             :     }
      70             : 
      71       15480 :     return hResult;
      72             : }
      73             : 
      74             : /************************************************************************/
      75             : /*                       OGRPG_Check_Table_Exists()                     */
      76             : /************************************************************************/
      77             : 
      78         828 : bool OGRPG_Check_Table_Exists(PGconn *hPGConn, const char *pszTableName)
      79             : {
      80         828 :     CPLString osSQL;
      81             :     osSQL.Printf(
      82             :         "SELECT 1 FROM information_schema.tables WHERE table_name = %s LIMIT 1",
      83         828 :         OGRPGEscapeString(hPGConn, pszTableName).c_str());
      84         828 :     PGresult *hResult = OGRPG_PQexec(hPGConn, osSQL);
      85         828 :     bool bRet = (hResult && PQntuples(hResult) == 1);
      86         828 :     if (!bRet)
      87           0 :         CPLDebug("PG", "Does not have %s table", pszTableName);
      88         828 :     OGRPGClearResult(hResult);
      89        1656 :     return bRet;
      90             : }

Generated by: LCOV version 1.14