Header And Logo

PostgreSQL
| The world's most advanced open source database.

dropuser.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * dropuser
00004  *
00005  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00006  * Portions Copyright (c) 1994, Regents of the University of California
00007  *
00008  * src/bin/scripts/dropuser.c
00009  *
00010  *-------------------------------------------------------------------------
00011  */
00012 
00013 #include "postgres_fe.h"
00014 #include "common.h"
00015 #include "dumputils.h"
00016 
00017 
00018 static void help(const char *progname);
00019 
00020 
00021 int
00022 main(int argc, char *argv[])
00023 {
00024     static int  if_exists = 0;
00025 
00026     static struct option long_options[] = {
00027         {"host", required_argument, NULL, 'h'},
00028         {"port", required_argument, NULL, 'p'},
00029         {"username", required_argument, NULL, 'U'},
00030         {"no-password", no_argument, NULL, 'w'},
00031         {"password", no_argument, NULL, 'W'},
00032         {"echo", no_argument, NULL, 'e'},
00033         {"interactive", no_argument, NULL, 'i'},
00034         {"if-exists", no_argument, &if_exists, 1},
00035         {NULL, 0, NULL, 0}
00036     };
00037 
00038     const char *progname;
00039     int         optindex;
00040     int         c;
00041 
00042     char       *dropuser = NULL;
00043     char       *host = NULL;
00044     char       *port = NULL;
00045     char       *username = NULL;
00046     enum trivalue prompt_password = TRI_DEFAULT;
00047     bool        echo = false;
00048     bool        interactive = false;
00049 
00050     PQExpBufferData sql;
00051 
00052     PGconn     *conn;
00053     PGresult   *result;
00054 
00055     progname = get_progname(argv[0]);
00056     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
00057 
00058     handle_help_version_opts(argc, argv, "dropuser", help);
00059 
00060     while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
00061     {
00062         switch (c)
00063         {
00064             case 'h':
00065                 host = pg_strdup(optarg);
00066                 break;
00067             case 'p':
00068                 port = pg_strdup(optarg);
00069                 break;
00070             case 'U':
00071                 username = pg_strdup(optarg);
00072                 break;
00073             case 'w':
00074                 prompt_password = TRI_NO;
00075                 break;
00076             case 'W':
00077                 prompt_password = TRI_YES;
00078                 break;
00079             case 'e':
00080                 echo = true;
00081                 break;
00082             case 'i':
00083                 interactive = true;
00084                 break;
00085             case 0:
00086                 /* this covers the long options */
00087                 break;
00088             default:
00089                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00090                 exit(1);
00091         }
00092     }
00093 
00094     switch (argc - optind)
00095     {
00096         case 0:
00097             break;
00098         case 1:
00099             dropuser = argv[optind];
00100             break;
00101         default:
00102             fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
00103                     progname, argv[optind + 1]);
00104             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00105             exit(1);
00106     }
00107 
00108     if (dropuser == NULL)
00109     {
00110         if (interactive)
00111             dropuser = simple_prompt("Enter name of role to drop: ", 128, true);
00112         else
00113         {
00114             fprintf(stderr, _("%s: missing required argument role name\n"), progname);
00115             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00116             exit(1);
00117         }
00118     }
00119 
00120     if (interactive)
00121     {
00122         printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
00123         if (!yesno_prompt("Are you sure?"))
00124             exit(0);
00125     }
00126 
00127     initPQExpBuffer(&sql);
00128     appendPQExpBuffer(&sql, "DROP ROLE %s%s;\n",
00129                       (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
00130 
00131     conn = connectDatabase("postgres", host, port, username, prompt_password,
00132                            progname, false);
00133 
00134     if (echo)
00135         printf("%s", sql.data);
00136     result = PQexec(conn, sql.data);
00137 
00138     if (PQresultStatus(result) != PGRES_COMMAND_OK)
00139     {
00140         fprintf(stderr, _("%s: removal of role \"%s\" failed: %s"),
00141                 progname, dropuser, PQerrorMessage(conn));
00142         PQfinish(conn);
00143         exit(1);
00144     }
00145 
00146     PQclear(result);
00147     PQfinish(conn);
00148     exit(0);
00149 }
00150 
00151 
00152 static void
00153 help(const char *progname)
00154 {
00155     printf(_("%s removes a PostgreSQL role.\n\n"), progname);
00156     printf(_("Usage:\n"));
00157     printf(_("  %s [OPTION]... [ROLENAME]\n"), progname);
00158     printf(_("\nOptions:\n"));
00159     printf(_("  -e, --echo                show the commands being sent to the server\n"));
00160     printf(_("  -i, --interactive         prompt before deleting anything, and prompt for\n"
00161              "                            role name if not specified\n"));
00162     printf(_("  -V, --version             output version information, then exit\n"));
00163     printf(_("  --if-exists               don't report error if user doesn't exist\n"));
00164     printf(_("  -?, --help                show this help, then exit\n"));
00165     printf(_("\nConnection options:\n"));
00166     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
00167     printf(_("  -p, --port=PORT           database server port\n"));
00168     printf(_("  -U, --username=USERNAME   user name to connect as (not the one to drop)\n"));
00169     printf(_("  -w, --no-password         never prompt for password\n"));
00170     printf(_("  -W, --password            force password prompt\n"));
00171     printf(_("\nReport bugs to <[email protected]>.\n"));
00172 }