PostgreSQL Source Code git master
common.h File Reference
#include "common/username.h"
#include "fe_utils/connect_utils.h"
#include "getopt_long.h"
#include "libpq-fe.h"
#include "pqexpbuffer.h"
Include dependency graph for common.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

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)
 

Function Documentation

◆ appendQualifiedRelation()

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

Definition at line 68 of file common.c.

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

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(), get_parallel_object_list(), and run_reindex_command().

◆ splitTableColumnsSpec()

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

Definition at line 33 of file common.c.

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

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

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

Referenced by main().