#include "libpq-fe.h"
#include "getopt_long.h"
#include "pqexpbuffer.h"
Go to the source code of this file.
Typedefs | |
typedef void(* | help_handler )(const char *progname) |
Enumerations | |
enum | trivalue { TRI_DEFAULT, TRI_NO, TRI_YES, TRI_DEFAULT, TRI_NO, TRI_YES, TRI_DEFAULT, TRI_NO, TRI_YES, TRI_DEFAULT, TRI_NO, TRI_YES } |
Functions | |
const char * | get_user_name (const char *progname) |
void | handle_help_version_opts (int argc, char *argv[], const char *fixed_progname, help_handler hlp) |
PGconn * | connectDatabase (const char *dbname, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool fail_ok) |
PGconn * | connectMaintenanceDatabase (const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname) |
PGresult * | executeQuery (PGconn *conn, const char *query, const char *progname, bool echo) |
void | executeCommand (PGconn *conn, const char *query, const char *progname, bool echo) |
bool | executeMaintenanceCommand (PGconn *conn, const char *query, bool echo) |
bool | yesno_prompt (const char *question) |
void | setup_cancel_handler (void) |
typedef void(* help_handler)(const char *progname) |
enum trivalue |
PGconn* connectDatabase | ( | const char * | dbname, | |
const char * | pghost, | |||
const char * | pgport, | |||
const char * | pguser, | |||
enum trivalue | prompt_password, | |||
const char * | progname, | |||
bool | fail_ok | |||
) |
Definition at line 93 of file common.c.
References _, conn, CONNECTION_BAD, free, NULL, PARAMS_ARRAY_SIZE, pg_malloc(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQerrorMessage(), PQfinish(), PQstatus(), simple_prompt(), TRI_NO, TRI_YES, and values.
{ PGconn *conn; char *password = NULL; bool new_pass; if (prompt_password == TRI_YES) password = simple_prompt("Password: ", 100, false); /* * Start the connection. Loop until we have a password if requested by * backend. */ do { #define PARAMS_ARRAY_SIZE 7 const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords)); const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values)); keywords[0] = "host"; values[0] = pghost; keywords[1] = "port"; values[1] = pgport; keywords[2] = "user"; values[2] = pguser; keywords[3] = "password"; values[3] = password; keywords[4] = "dbname"; values[4] = dbname; keywords[5] = "fallback_application_name"; values[5] = progname; keywords[6] = NULL; values[6] = NULL; new_pass = false; conn = PQconnectdbParams(keywords, values, true); free(keywords); free(values); if (!conn) { fprintf(stderr, _("%s: could not connect to database %s\n"), progname, dbname); exit(1); } if (PQstatus(conn) == CONNECTION_BAD && PQconnectionNeedsPassword(conn) && password == NULL && prompt_password != TRI_NO) { PQfinish(conn); password = simple_prompt("Password: ", 100, false); new_pass = true; } } while (new_pass); if (password) free(password); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) == CONNECTION_BAD) { if (fail_ok) { PQfinish(conn); return NULL; } fprintf(stderr, _("%s: could not connect to database %s: %s"), progname, dbname, PQerrorMessage(conn)); exit(1); } return conn; }
PGconn* connectMaintenanceDatabase | ( | const char * | maintenance_db, | |
const char * | pghost, | |||
const char * | pgport, | |||
const char * | pguser, | |||
enum trivalue | prompt_password, | |||
const char * | progname | |||
) |
Definition at line 176 of file common.c.
References conn, and connectDatabase().
Referenced by cluster_all_databases(), main(), reindex_all_databases(), and vacuum_all_databases().
{ PGconn *conn; /* If a maintenance database name was specified, just connect to it. */ if (maintenance_db) return connectDatabase(maintenance_db, pghost, pgport, pguser, prompt_password, progname, false); /* Otherwise, try postgres first and then template1. */ conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password, progname, true); if (!conn) conn = connectDatabase("template1", pghost, pgport, pguser, prompt_password, progname, false); return conn; }
Definition at line 229 of file common.c.
References _, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus().
{ PGresult *res; if (echo) printf("%s\n", query); res = PQexec(conn, query); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, _("%s: query failed: %s"), progname, PQerrorMessage(conn)); fprintf(stderr, _("%s: query was: %s\n"), progname, query); PQfinish(conn); exit(1); } PQclear(res); }
Definition at line 259 of file common.c.
References PQclear(), PQexec(), PQresultStatus(), ResetCancelConn(), and SetCancelConn().
Referenced by cluster_one_database(), reindex_one_database(), reindex_system_catalogs(), and vacuum_one_database().
{ PGresult *res; bool r; if (echo) printf("%s\n", query); SetCancelConn(conn); res = PQexec(conn, query); ResetCancelConn(); r = (res && PQresultStatus(res) == PGRES_COMMAND_OK); if (res) PQclear(res); return r; }
Definition at line 202 of file common.c.
References _, PGRES_TUPLES_OK, PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus().
{ PGresult *res; if (echo) printf("%s\n", query); res = PQexec(conn, query); if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, _("%s: query failed: %s"), progname, PQerrorMessage(conn)); fprintf(stderr, _("%s: query was: %s\n"), progname, query); PQfinish(conn); exit(1); } return res; }
const char* get_user_name | ( | const char * | progname | ) |
Definition at line 36 of file common.c.
References _, strerror(), and username.
Referenced by main().
{ #ifndef WIN32 struct passwd *pw; pw = getpwuid(geteuid()); if (!pw) { fprintf(stderr, _("%s: could not obtain information about current user: %s\n"), progname, strerror(errno)); exit(1); } return pw->pw_name; #else static char username[128]; /* remains after function exit */ DWORD len = sizeof(username) - 1; if (!GetUserName(username, &len)) { fprintf(stderr, _("%s: could not get current user name: %s\n"), progname, strerror(errno)); exit(1); } return username; #endif }
void handle_help_version_opts | ( | int | argc, | |
char * | argv[], | |||
const char * | fixed_progname, | |||
help_handler | hlp | |||
) |
Definition at line 69 of file common.c.
References get_progname().
Referenced by main().
{ if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { hlp(get_progname(argv[0])); exit(0); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname); exit(0); } } }
void setup_cancel_handler | ( | void | ) |
Definition at line 203 of file common.c.
References handle_sigint(), and pqsignal().
Referenced by main().
{ pqsignal(SIGINT, handle_sigint); }
bool yesno_prompt | ( | const char * | question | ) |
Definition at line 289 of file common.c.
References _, free, PG_NOLETTER, PG_YESLETTER, simple_prompt(), and snprintf().
Referenced by main().
{ char prompt[256]; /*------ translator: This is a question followed by the translated options for "yes" and "no". */ snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "), _(question), _(PG_YESLETTER), _(PG_NOLETTER)); for (;;) { char *resp; resp = simple_prompt(prompt, 1, true); if (strcmp(resp, _(PG_YESLETTER)) == 0) { free(resp); return true; } else if (strcmp(resp, _(PG_NOLETTER)) == 0) { free(resp); return false; } free(resp); printf(_("Please answer \"%s\" or \"%s\".\n"), _(PG_YESLETTER), _(PG_NOLETTER)); } }