LCOV - code coverage report
Current view: top level - src/fe_utils - query_utils.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 28 32 87.5 %
Date: 2024-04-27 04:11:27 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * Facilities for frontend code to query a databases.
       4             :  *
       5             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       6             :  * Portions Copyright (c) 1994, Regents of the University of California
       7             :  *
       8             :  * src/fe_utils/query_utils.c
       9             :  *
      10             :  *-------------------------------------------------------------------------
      11             :  */
      12             : #include "postgres_fe.h"
      13             : 
      14             : #include "common/logging.h"
      15             : #include "fe_utils/cancel.h"
      16             : #include "fe_utils/query_utils.h"
      17             : 
      18             : /*
      19             :  * Run a query, return the results, exit program on failure.
      20             :  */
      21             : PGresult *
      22        1372 : executeQuery(PGconn *conn, const char *query, bool echo)
      23             : {
      24             :     PGresult   *res;
      25             : 
      26        1372 :     if (echo)
      27          94 :         printf("%s\n", query);
      28             : 
      29        1372 :     res = PQexec(conn, query);
      30        2744 :     if (!res ||
      31        1372 :         PQresultStatus(res) != PGRES_TUPLES_OK)
      32             :     {
      33           4 :         pg_log_error("query failed: %s", PQerrorMessage(conn));
      34           4 :         pg_log_error_detail("Query was: %s", query);
      35           4 :         PQfinish(conn);
      36           4 :         exit(1);
      37             :     }
      38             : 
      39        1368 :     return res;
      40             : }
      41             : 
      42             : 
      43             : /*
      44             :  * As above for a SQL command (which returns nothing).
      45             :  */
      46             : void
      47         226 : executeCommand(PGconn *conn, const char *query, bool echo)
      48             : {
      49             :     PGresult   *res;
      50             : 
      51         226 :     if (echo)
      52          16 :         printf("%s\n", query);
      53             : 
      54         226 :     res = PQexec(conn, query);
      55         452 :     if (!res ||
      56         226 :         PQresultStatus(res) != PGRES_COMMAND_OK)
      57             :     {
      58           0 :         pg_log_error("query failed: %s", PQerrorMessage(conn));
      59           0 :         pg_log_error_detail("Query was: %s", query);
      60           0 :         PQfinish(conn);
      61           0 :         exit(1);
      62             :     }
      63             : 
      64         226 :     PQclear(res);
      65         226 : }
      66             : 
      67             : 
      68             : /*
      69             :  * As above for a SQL maintenance command (returns command success).
      70             :  * Command is executed with a cancel handler set, so Ctrl-C can
      71             :  * interrupt it.
      72             :  */
      73             : bool
      74          32 : executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
      75             : {
      76             :     PGresult   *res;
      77             :     bool        r;
      78             : 
      79          32 :     if (echo)
      80          16 :         printf("%s\n", query);
      81             : 
      82          32 :     SetCancelConn(conn);
      83          32 :     res = PQexec(conn, query);
      84          32 :     ResetCancelConn();
      85             : 
      86          32 :     r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
      87             : 
      88          32 :     PQclear(res);
      89             : 
      90          32 :     return r;
      91             : }

Generated by: LCOV version 1.14