|
22 | 22 | #include "common/logging.h"
|
23 | 23 | #include "common/string.h"
|
24 | 24 | #include "fe_utils/cancel.h"
|
| 25 | +#include "fe_utils/query_utils.h" |
25 | 26 | #include "fe_utils/string_utils.h"
|
26 | 27 |
|
27 |
| -#define ERRCODE_UNDEFINED_TABLE "42P01" |
28 |
| - |
29 |
| -/* |
30 |
| - * Provide strictly harmonized handling of --help and --version |
31 |
| - * options. |
32 |
| - */ |
33 |
| -void |
34 |
| -handle_help_version_opts(int argc, char *argv[], |
35 |
| - const char *fixed_progname, help_handler hlp) |
36 |
| -{ |
37 |
| - if (argc > 1) |
38 |
| - { |
39 |
| - if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) |
40 |
| - { |
41 |
| - hlp(get_progname(argv[0])); |
42 |
| - exit(0); |
43 |
| - } |
44 |
| - if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) |
45 |
| - { |
46 |
| - printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname); |
47 |
| - exit(0); |
48 |
| - } |
49 |
| - } |
50 |
| -} |
51 |
| - |
52 |
| - |
53 |
| -/* |
54 |
| - * Make a database connection with the given parameters. |
55 |
| - * |
56 |
| - * An interactive password prompt is automatically issued if needed and |
57 |
| - * allowed by cparams->prompt_password. |
58 |
| - * |
59 |
| - * If allow_password_reuse is true, we will try to re-use any password |
60 |
| - * given during previous calls to this routine. (Callers should not pass |
61 |
| - * allow_password_reuse=true unless reconnecting to the same database+user |
62 |
| - * as before, else we might create password exposure hazards.) |
63 |
| - */ |
64 |
| -PGconn * |
65 |
| -connectDatabase(const ConnParams *cparams, const char *progname, |
66 |
| - bool echo, bool fail_ok, bool allow_password_reuse) |
67 |
| -{ |
68 |
| - PGconn *conn; |
69 |
| - bool new_pass; |
70 |
| - static char *password = NULL; |
71 |
| - |
72 |
| - /* Callers must supply at least dbname; other params can be NULL */ |
73 |
| - Assert(cparams->dbname); |
74 |
| - |
75 |
| - if (!allow_password_reuse && password) |
76 |
| - { |
77 |
| - free(password); |
78 |
| - password = NULL; |
79 |
| - } |
80 |
| - |
81 |
| - if (cparams->prompt_password == TRI_YES && password == NULL) |
82 |
| - password = simple_prompt("Password: ", false); |
83 |
| - |
84 |
| - /* |
85 |
| - * Start the connection. Loop until we have a password if requested by |
86 |
| - * backend. |
87 |
| - */ |
88 |
| - do |
89 |
| - { |
90 |
| - const char *keywords[8]; |
91 |
| - const char *values[8]; |
92 |
| - int i = 0; |
93 |
| - |
94 |
| - /* |
95 |
| - * If dbname is a connstring, its entries can override the other |
96 |
| - * values obtained from cparams; but in turn, override_dbname can |
97 |
| - * override the dbname component of it. |
98 |
| - */ |
99 |
| - keywords[i] = "host"; |
100 |
| - values[i++] = cparams->pghost; |
101 |
| - keywords[i] = "port"; |
102 |
| - values[i++] = cparams->pgport; |
103 |
| - keywords[i] = "user"; |
104 |
| - values[i++] = cparams->pguser; |
105 |
| - keywords[i] = "password"; |
106 |
| - values[i++] = password; |
107 |
| - keywords[i] = "dbname"; |
108 |
| - values[i++] = cparams->dbname; |
109 |
| - if (cparams->override_dbname) |
110 |
| - { |
111 |
| - keywords[i] = "dbname"; |
112 |
| - values[i++] = cparams->override_dbname; |
113 |
| - } |
114 |
| - keywords[i] = "fallback_application_name"; |
115 |
| - values[i++] = progname; |
116 |
| - keywords[i] = NULL; |
117 |
| - values[i++] = NULL; |
118 |
| - Assert(i <= lengthof(keywords)); |
119 |
| - |
120 |
| - new_pass = false; |
121 |
| - conn = PQconnectdbParams(keywords, values, true); |
122 |
| - |
123 |
| - if (!conn) |
124 |
| - { |
125 |
| - pg_log_error("could not connect to database %s: out of memory", |
126 |
| - cparams->dbname); |
127 |
| - exit(1); |
128 |
| - } |
129 |
| - |
130 |
| - /* |
131 |
| - * No luck? Trying asking (again) for a password. |
132 |
| - */ |
133 |
| - if (PQstatus(conn) == CONNECTION_BAD && |
134 |
| - PQconnectionNeedsPassword(conn) && |
135 |
| - cparams->prompt_password != TRI_NO) |
136 |
| - { |
137 |
| - PQfinish(conn); |
138 |
| - if (password) |
139 |
| - free(password); |
140 |
| - password = simple_prompt("Password: ", false); |
141 |
| - new_pass = true; |
142 |
| - } |
143 |
| - } while (new_pass); |
144 |
| - |
145 |
| - /* check to see that the backend connection was successfully made */ |
146 |
| - if (PQstatus(conn) == CONNECTION_BAD) |
147 |
| - { |
148 |
| - if (fail_ok) |
149 |
| - { |
150 |
| - PQfinish(conn); |
151 |
| - return NULL; |
152 |
| - } |
153 |
| - pg_log_error("%s", PQerrorMessage(conn)); |
154 |
| - exit(1); |
155 |
| - } |
156 |
| - |
157 |
| - /* Start strict; callers may override this. */ |
158 |
| - PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo)); |
159 |
| - |
160 |
| - return conn; |
161 |
| -} |
162 |
| - |
163 |
| -/* |
164 |
| - * Try to connect to the appropriate maintenance database. |
165 |
| - * |
166 |
| - * This differs from connectDatabase only in that it has a rule for |
167 |
| - * inserting a default "dbname" if none was given (which is why cparams |
168 |
| - * is not const). Note that cparams->dbname should typically come from |
169 |
| - * a --maintenance-db command line parameter. |
170 |
| - */ |
171 |
| -PGconn * |
172 |
| -connectMaintenanceDatabase(ConnParams *cparams, |
173 |
| - const char *progname, bool echo) |
174 |
| -{ |
175 |
| - PGconn *conn; |
176 |
| - |
177 |
| - /* If a maintenance database name was specified, just connect to it. */ |
178 |
| - if (cparams->dbname) |
179 |
| - return connectDatabase(cparams, progname, echo, false, false); |
180 |
| - |
181 |
| - /* Otherwise, try postgres first and then template1. */ |
182 |
| - cparams->dbname = "postgres"; |
183 |
| - conn = connectDatabase(cparams, progname, echo, true, false); |
184 |
| - if (!conn) |
185 |
| - { |
186 |
| - cparams->dbname = "template1"; |
187 |
| - conn = connectDatabase(cparams, progname, echo, false, false); |
188 |
| - } |
189 |
| - return conn; |
190 |
| -} |
191 |
| - |
192 |
| -/* |
193 |
| - * Disconnect the given connection, canceling any statement if one is active. |
194 |
| - */ |
195 |
| -void |
196 |
| -disconnectDatabase(PGconn *conn) |
197 |
| -{ |
198 |
| - char errbuf[256]; |
199 |
| - |
200 |
| - Assert(conn != NULL); |
201 |
| - |
202 |
| - if (PQtransactionStatus(conn) == PQTRANS_ACTIVE) |
203 |
| - { |
204 |
| - PGcancel *cancel; |
205 |
| - |
206 |
| - if ((cancel = PQgetCancel(conn))) |
207 |
| - { |
208 |
| - (void) PQcancel(cancel, errbuf, sizeof(errbuf)); |
209 |
| - PQfreeCancel(cancel); |
210 |
| - } |
211 |
| - } |
212 |
| - |
213 |
| - PQfinish(conn); |
214 |
| -} |
215 |
| - |
216 |
| -/* |
217 |
| - * Run a query, return the results, exit program on failure. |
218 |
| - */ |
219 |
| -PGresult * |
220 |
| -executeQuery(PGconn *conn, const char *query, bool echo) |
221 |
| -{ |
222 |
| - PGresult *res; |
223 |
| - |
224 |
| - if (echo) |
225 |
| - printf("%s\n", query); |
226 |
| - |
227 |
| - res = PQexec(conn, query); |
228 |
| - if (!res || |
229 |
| - PQresultStatus(res) != PGRES_TUPLES_OK) |
230 |
| - { |
231 |
| - pg_log_error("query failed: %s", PQerrorMessage(conn)); |
232 |
| - pg_log_info("query was: %s", query); |
233 |
| - PQfinish(conn); |
234 |
| - exit(1); |
235 |
| - } |
236 |
| - |
237 |
| - return res; |
238 |
| -} |
239 |
| - |
240 |
| - |
241 |
| -/* |
242 |
| - * As above for a SQL command (which returns nothing). |
243 |
| - */ |
244 |
| -void |
245 |
| -executeCommand(PGconn *conn, const char *query, bool echo) |
246 |
| -{ |
247 |
| - PGresult *res; |
248 |
| - |
249 |
| - if (echo) |
250 |
| - printf("%s\n", query); |
251 |
| - |
252 |
| - res = PQexec(conn, query); |
253 |
| - if (!res || |
254 |
| - PQresultStatus(res) != PGRES_COMMAND_OK) |
255 |
| - { |
256 |
| - pg_log_error("query failed: %s", PQerrorMessage(conn)); |
257 |
| - pg_log_info("query was: %s", query); |
258 |
| - PQfinish(conn); |
259 |
| - exit(1); |
260 |
| - } |
261 |
| - |
262 |
| - PQclear(res); |
263 |
| -} |
264 |
| - |
265 |
| - |
266 |
| -/* |
267 |
| - * As above for a SQL maintenance command (returns command success). |
268 |
| - * Command is executed with a cancel handler set, so Ctrl-C can |
269 |
| - * interrupt it. |
270 |
| - */ |
271 |
| -bool |
272 |
| -executeMaintenanceCommand(PGconn *conn, const char *query, bool echo) |
273 |
| -{ |
274 |
| - PGresult *res; |
275 |
| - bool r; |
276 |
| - |
277 |
| - if (echo) |
278 |
| - printf("%s\n", query); |
279 |
| - |
280 |
| - SetCancelConn(conn); |
281 |
| - res = PQexec(conn, query); |
282 |
| - ResetCancelConn(); |
283 |
| - |
284 |
| - r = (res && PQresultStatus(res) == PGRES_COMMAND_OK); |
285 |
| - |
286 |
| - if (res) |
287 |
| - PQclear(res); |
288 |
| - |
289 |
| - return r; |
290 |
| -} |
291 |
| - |
292 |
| -/* |
293 |
| - * Consume all the results generated for the given connection until |
294 |
| - * nothing remains. If at least one error is encountered, return false. |
295 |
| - * Note that this will block if the connection is busy. |
296 |
| - */ |
297 |
| -bool |
298 |
| -consumeQueryResult(PGconn *conn) |
299 |
| -{ |
300 |
| - bool ok = true; |
301 |
| - PGresult *result; |
302 |
| - |
303 |
| - SetCancelConn(conn); |
304 |
| - while ((result = PQgetResult(conn)) != NULL) |
305 |
| - { |
306 |
| - if (!processQueryResult(conn, result)) |
307 |
| - ok = false; |
308 |
| - } |
309 |
| - ResetCancelConn(); |
310 |
| - return ok; |
311 |
| -} |
312 |
| - |
313 |
| -/* |
314 |
| - * Process (and delete) a query result. Returns true if there's no error, |
315 |
| - * false otherwise -- but errors about trying to work on a missing relation |
316 |
| - * are reported and subsequently ignored. |
317 |
| - */ |
318 |
| -bool |
319 |
| -processQueryResult(PGconn *conn, PGresult *result) |
320 |
| -{ |
321 |
| - /* |
322 |
| - * If it's an error, report it. Errors about a missing table are harmless |
323 |
| - * so we continue processing; but die for other errors. |
324 |
| - */ |
325 |
| - if (PQresultStatus(result) != PGRES_COMMAND_OK) |
326 |
| - { |
327 |
| - char *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE); |
328 |
| - |
329 |
| - pg_log_error("processing of database \"%s\" failed: %s", |
330 |
| - PQdb(conn), PQerrorMessage(conn)); |
331 |
| - |
332 |
| - if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0) |
333 |
| - { |
334 |
| - PQclear(result); |
335 |
| - return false; |
336 |
| - } |
337 |
| - } |
338 |
| - |
339 |
| - PQclear(result); |
340 |
| - return true; |
341 |
| -} |
342 |
| - |
343 |
| - |
344 | 28 | /*
|
345 | 29 | * Split TABLE[(COLUMNS)] into TABLE and [(COLUMNS)] portions. When you
|
346 | 30 | * finish using them, pg_free(*table). *columns is a pointer into "spec",
|
|
0 commit comments