PostgreSQL Source Code  git master
common.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include <unistd.h>
#include "common.h"
#include "common/connect.h"
#include "common/logging.h"
#include "common/string.h"
#include "fe_utils/cancel.h"
#include "fe_utils/query_utils.h"
#include "fe_utils/string_utils.h"
Include dependency graph for common.c:

Go to the source code of this file.

Macros

#define PG_YESLETTER   gettext_noop("y")
 
#define PG_NOLETTER   gettext_noop("n")
 

Functions

void splitTableColumnsSpec (const char *spec, int encoding, char **table, const char **columns)
 
void appendQualifiedRelation (PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
 
bool yesno_prompt (const char *question)
 

Macro Definition Documentation

◆ PG_NOLETTER

#define PG_NOLETTER   gettext_noop("n")

Definition at line 133 of file common.c.

◆ PG_YESLETTER

#define PG_YESLETTER   gettext_noop("y")

Definition at line 131 of file common.c.

Function Documentation

◆ appendQualifiedRelation()

void appendQualifiedRelation ( PQExpBuffer  buf,
const char *  spec,
PGconn conn,
bool  echo 
)

Definition at line 69 of file common.c.

71 {
72  char *table;
73  const char *columns;
74  PQExpBufferData sql;
75  PGresult *res;
76  int ntups;
77 
78  splitTableColumnsSpec(spec, PQclientEncoding(conn), &table, &columns);
79 
80  /*
81  * Query must remain ABSOLUTELY devoid of unqualified names. This would
82  * be unnecessary given a regclassin() variant taking a search_path
83  * argument.
84  */
85  initPQExpBuffer(&sql);
87  "SELECT c.relname, ns.nspname\n"
88  " FROM pg_catalog.pg_class c,"
89  " pg_catalog.pg_namespace ns\n"
90  " WHERE c.relnamespace OPERATOR(pg_catalog.=) ns.oid\n"
91  " AND c.oid OPERATOR(pg_catalog.=) ");
92  appendStringLiteralConn(&sql, table, conn);
93  appendPQExpBufferStr(&sql, "::pg_catalog.regclass;");
94 
95  executeCommand(conn, "RESET search_path;", echo);
96 
97  /*
98  * One row is a typical result, as is a nonexistent relation ERROR.
99  * regclassin() unconditionally accepts all-digits input as an OID; if no
100  * relation has that OID; this query returns no rows. Catalog corruption
101  * might elicit other row counts.
102  */
103  res = executeQuery(conn, sql.data, echo);
104  ntups = PQntuples(res);
105  if (ntups != 1)
106  {
107  pg_log_error(ngettext("query returned %d row instead of one: %s",
108  "query returned %d rows instead of one: %s",
109  ntups),
110  ntups, sql.data);
111  PQfinish(conn);
112  exit(1);
113  }
116  PQgetvalue(res, 0, 0)));
117  appendPQExpBufferStr(buf, columns);
118  PQclear(res);
119  termPQExpBuffer(&sql);
120  pg_free(table);
121 
123 }
void splitTableColumnsSpec(const char *spec, int encoding, char **table, const char **columns)
Definition: common.c:34
#define ngettext(s, p, n)
Definition: c.h:1179
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6847
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4257
void PQclear(PGresult *res)
Definition: fe-exec.c:694
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
void pg_free(void *ptr)
Definition: fe_memutils.c:105
exit(1)
#define pg_log_error(...)
Definition: logging.h:80
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1902
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1879
static char * buf
Definition: pg_test_fsync.c:69
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
PGconn * conn
Definition: streamutil.c:54
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
const char * fmtQualifiedId(const char *schema, const char *id)
Definition: string_utils.c:145

References ALWAYS_SECURE_SEARCH_PATH_SQL, appendPQExpBufferStr(), appendStringLiteralConn(), buf, conn, PQExpBufferData::data, executeCommand(), executeQuery(), exit(), fmtQualifiedId(), initPQExpBuffer(), ngettext, pg_free(), pg_log_error, PQclear(), PQclientEncoding(), PQfinish(), PQgetvalue(), PQntuples(), res, splitTableColumnsSpec(), and termPQExpBuffer().

Referenced by cluster_one_database(), and run_reindex_command().

◆ splitTableColumnsSpec()

void splitTableColumnsSpec ( const char *  spec,
int  encoding,
char **  table,
const char **  columns 
)

Definition at line 34 of file common.c.

36 {
37  bool inquotes = false;
38  const char *cp = spec;
39 
40  /*
41  * Find the first '(' not identifier-quoted. Based on
42  * dequote_downcase_identifier().
43  */
44  while (*cp && (*cp != '(' || inquotes))
45  {
46  if (*cp == '"')
47  {
48  if (inquotes && cp[1] == '"')
49  cp++; /* pair does not affect quoting */
50  else
51  inquotes = !inquotes;
52  cp++;
53  }
54  else
55  cp += PQmblenBounded(cp, encoding);
56  }
57  *table = pnstrdup(spec, cp - spec);
58  *columns = cp;
59 }
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1202
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1310
int32 encoding
Definition: pg_database.h:41

References encoding, pnstrdup(), and PQmblenBounded().

Referenced by appendQualifiedRelation(), and vacuum_one_database().

◆ yesno_prompt()

bool yesno_prompt ( const char *  question)

Definition at line 136 of file common.c.

137 {
138  char prompt[256];
139 
140  /*------
141  translator: This is a question followed by the translated options for
142  "yes" and "no". */
143  snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "),
144  _(question), _(PG_YESLETTER), _(PG_NOLETTER));
145 
146  for (;;)
147  {
148  char *resp;
149 
150  resp = simple_prompt(prompt, true);
151 
152  if (strcmp(resp, _(PG_YESLETTER)) == 0)
153  {
154  free(resp);
155  return true;
156  }
157  if (strcmp(resp, _(PG_NOLETTER)) == 0)
158  {
159  free(resp);
160  return false;
161  }
162  free(resp);
163 
164  printf(_("Please answer \"%s\" or \"%s\".\n"),
166  }
167 }
#define PG_YESLETTER
Definition: common.c:131
#define PG_NOLETTER
Definition: common.c:133
#define _(x)
Definition: elog.c:89
#define free(a)
Definition: header.h:65
#define snprintf
Definition: port.h:222
#define printf(...)
Definition: port.h:228
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38

References _, free, PG_NOLETTER, PG_YESLETTER, printf, simple_prompt(), and snprintf.

Referenced by main().