Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * common.c
4 : : * Common support routines for bin/scripts/
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * src/bin/scripts/common.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres_fe.h"
16 : :
17 : : #include <signal.h>
18 : : #include <unistd.h>
19 : :
20 : : #include "common.h"
21 : : #include "common/connect.h"
22 : : #include "common/logging.h"
23 : : #include "common/string.h"
24 : : #include "fe_utils/query_utils.h"
25 : : #include "fe_utils/string_utils.h"
26 : :
27 : : /*
28 : : * Split TABLE[(COLUMNS)] into TABLE and [(COLUMNS)] portions. When you
29 : : * finish using them, pg_free(*table). *columns is a pointer into "spec",
30 : : * possibly to its NUL terminator.
31 : : */
32 : : void
33 : 68 : splitTableColumnsSpec(const char *spec, int encoding,
34 : : char **table, const char **columns)
35 : : {
36 : 68 : bool inquotes = false;
37 : 68 : const char *cp = spec;
38 : :
39 : : /*
40 : : * Find the first '(' not identifier-quoted. Based on
41 : : * dequote_downcase_identifier().
42 : : */
43 [ + + + + : 939 : while (*cp && (*cp != '(' || inquotes))
+ + ]
44 : : {
45 [ + + ]: 871 : if (*cp == '"')
46 : : {
47 [ + + + + ]: 15 : if (inquotes && cp[1] == '"')
48 : 1 : cp++; /* pair does not affect quoting */
49 : : else
50 : 14 : inquotes = !inquotes;
51 : 15 : cp++;
52 : : }
53 : : else
54 : 856 : cp += PQmblenBounded(cp, encoding);
55 : : }
56 : 68 : *table = pnstrdup(spec, cp - spec);
57 : 68 : *columns = cp;
58 : 68 : }
59 : :
60 : : /*
61 : : * Break apart TABLE[(COLUMNS)] of "spec". With the reset_val of search_path
62 : : * in effect, have regclassin() interpret the TABLE portion. Append to "buf"
63 : : * the qualified name of TABLE, followed by any (COLUMNS). Exit on failure.
64 : : * We use this to interpret --table=foo under the search path psql would get,
65 : : * in advance of "ANALYZE public.foo" under the always-secure search path.
66 : : *
67 : : * The contents generated by this routine are safe to append to a SQL
68 : : * statement in queries, as a SQL identifier. For example, in "VACUUM
69 : : * <here>;" or "REINDEX TABLE <here>". This should not be used inside a
70 : : * SQL string literal, like an ARRAY[] value.
71 : : */
72 : : void
73 : 45 : appendQualifiedRelation(PQExpBuffer buf, const char *spec,
74 : : PGconn *conn, bool echo)
75 : : {
76 : : char *table;
77 : : const char *columns;
78 : : PQExpBufferData sql;
79 : : PGresult *res;
80 : : int ntups;
81 : :
82 : 45 : splitTableColumnsSpec(spec, PQclientEncoding(conn), &table, &columns);
83 : :
84 : : /*
85 : : * Query must remain ABSOLUTELY devoid of unqualified names. This would
86 : : * be unnecessary given a regclassin() variant taking a search_path
87 : : * argument.
88 : : */
89 : 45 : initPQExpBuffer(&sql);
90 : 45 : appendPQExpBufferStr(&sql,
91 : : "SELECT c.relname, ns.nspname\n"
92 : : " FROM pg_catalog.pg_class c,"
93 : : " pg_catalog.pg_namespace ns\n"
94 : : " WHERE c.relnamespace OPERATOR(pg_catalog.=) ns.oid\n"
95 : : " AND c.oid OPERATOR(pg_catalog.=) ");
96 : 45 : appendStringLiteralConn(&sql, table, conn);
97 : 45 : appendPQExpBufferStr(&sql, "::pg_catalog.regclass;");
98 : :
99 : 45 : executeCommand(conn, "RESET search_path;", echo);
100 : :
101 : : /*
102 : : * One row is a typical result, as is a nonexistent relation ERROR.
103 : : * regclassin() unconditionally accepts all-digits input as an OID; if no
104 : : * relation has that OID; this query returns no rows. Catalog corruption
105 : : * might elicit other row counts.
106 : : */
107 : 45 : res = executeQuery(conn, sql.data, echo);
108 : 44 : ntups = PQntuples(res);
109 [ - + ]: 44 : if (ntups != 1)
110 : : {
111 : 0 : pg_log_error(ngettext("query returned %d row instead of one: %s",
112 : : "query returned %d rows instead of one: %s",
113 : : ntups),
114 : : ntups, sql.data);
115 : 0 : PQfinish(conn);
116 : 0 : exit(1);
117 : : }
118 : 88 : appendPQExpBufferStr(buf,
119 : 44 : fmtQualifiedIdEnc(PQgetvalue(res, 0, 1),
120 : 44 : PQgetvalue(res, 0, 0),
121 : : PQclientEncoding(conn)));
122 : 44 : appendPQExpBufferStr(buf, columns);
123 : 44 : PQclear(res);
124 : 44 : termPQExpBuffer(&sql);
125 : 44 : pg_free(table);
126 : :
127 : 44 : PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo));
128 : 44 : }
129 : :
130 : :
131 : : /*
132 : : * Check yes/no answer in a localized way. 1=yes, 0=no, -1=neither.
133 : : */
134 : :
135 : : /* translator: abbreviation for "yes" */
136 : : #define PG_YESLETTER gettext_noop("y")
137 : : /* translator: abbreviation for "no" */
138 : : #define PG_NOLETTER gettext_noop("n")
139 : :
140 : : bool
141 : 0 : yesno_prompt(const char *question)
142 : : {
143 : : char prompt[256];
144 : :
145 : : /*------
146 : : translator: This is a question followed by the translated options for
147 : : "yes" and "no". */
148 : 0 : snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "),
149 : : _(question), _(PG_YESLETTER), _(PG_NOLETTER));
150 : :
151 : : for (;;)
152 : 0 : {
153 : : char *resp;
154 : :
155 : 0 : resp = simple_prompt(prompt, true);
156 : :
157 [ # # ]: 0 : if (strcmp(resp, _(PG_YESLETTER)) == 0)
158 : : {
159 : 0 : free(resp);
160 : 0 : return true;
161 : : }
162 [ # # ]: 0 : if (strcmp(resp, _(PG_NOLETTER)) == 0)
163 : : {
164 : 0 : free(resp);
165 : 0 : return false;
166 : : }
167 : 0 : free(resp);
168 : :
169 : 0 : printf(_("Please answer \"%s\" or \"%s\".\n"),
170 : : _(PG_YESLETTER), _(PG_NOLETTER));
171 : : }
172 : : }
|