PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/bin/scripts/dropdb.c

https://github.com/matheusoliveira/postgres
C | 171 lines | 135 code | 23 blank | 13 comment | 13 complexity | c4ab164ed25891c953cb8b7660a220b9 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*-------------------------------------------------------------------------
  2. *
  3. * dropdb
  4. *
  5. * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
  6. * Portions Copyright (c) 1994, Regents of the University of California
  7. *
  8. * src/bin/scripts/dropdb.c
  9. *
  10. *-------------------------------------------------------------------------
  11. */
  12. #include "postgres_fe.h"
  13. #include "common.h"
  14. #include "dumputils.h"
  15. static void help(const char *progname);
  16. int
  17. main(int argc, char *argv[])
  18. {
  19. static int if_exists = 0;
  20. static struct option long_options[] = {
  21. {"host", required_argument, NULL, 'h'},
  22. {"port", required_argument, NULL, 'p'},
  23. {"username", required_argument, NULL, 'U'},
  24. {"no-password", no_argument, NULL, 'w'},
  25. {"password", no_argument, NULL, 'W'},
  26. {"echo", no_argument, NULL, 'e'},
  27. {"interactive", no_argument, NULL, 'i'},
  28. {"if-exists", no_argument, &if_exists, 1},
  29. {"maintenance-db", required_argument, NULL, 2},
  30. {NULL, 0, NULL, 0}
  31. };
  32. const char *progname;
  33. int optindex;
  34. int c;
  35. char *dbname = NULL;
  36. char *maintenance_db = NULL;
  37. char *host = NULL;
  38. char *port = NULL;
  39. char *username = NULL;
  40. enum trivalue prompt_password = TRI_DEFAULT;
  41. bool echo = false;
  42. bool interactive = false;
  43. PQExpBufferData sql;
  44. PGconn *conn;
  45. PGresult *result;
  46. progname = get_progname(argv[0]);
  47. set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
  48. handle_help_version_opts(argc, argv, "dropdb", help);
  49. while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
  50. {
  51. switch (c)
  52. {
  53. case 'h':
  54. host = pg_strdup(optarg);
  55. break;
  56. case 'p':
  57. port = pg_strdup(optarg);
  58. break;
  59. case 'U':
  60. username = pg_strdup(optarg);
  61. break;
  62. case 'w':
  63. prompt_password = TRI_NO;
  64. break;
  65. case 'W':
  66. prompt_password = TRI_YES;
  67. break;
  68. case 'e':
  69. echo = true;
  70. break;
  71. case 'i':
  72. interactive = true;
  73. break;
  74. case 0:
  75. /* this covers the long options */
  76. break;
  77. case 2:
  78. maintenance_db = pg_strdup(optarg);
  79. break;
  80. default:
  81. fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
  82. exit(1);
  83. }
  84. }
  85. switch (argc - optind)
  86. {
  87. case 0:
  88. fprintf(stderr, _("%s: missing required argument database name\n"), progname);
  89. fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
  90. exit(1);
  91. case 1:
  92. dbname = argv[optind];
  93. break;
  94. default:
  95. fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
  96. progname, argv[optind + 1]);
  97. fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
  98. exit(1);
  99. }
  100. if (interactive)
  101. {
  102. printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
  103. if (!yesno_prompt("Are you sure?"))
  104. exit(0);
  105. }
  106. initPQExpBuffer(&sql);
  107. appendPQExpBuffer(&sql, "DROP DATABASE %s%s;",
  108. (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
  109. /* Avoid trying to drop postgres db while we are connected to it. */
  110. if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
  111. maintenance_db = "template1";
  112. conn = connectMaintenanceDatabase(maintenance_db,
  113. host, port, username, prompt_password, progname);
  114. if (echo)
  115. printf("%s\n", sql.data);
  116. result = PQexec(conn, sql.data);
  117. if (PQresultStatus(result) != PGRES_COMMAND_OK)
  118. {
  119. fprintf(stderr, _("%s: database removal failed: %s"),
  120. progname, PQerrorMessage(conn));
  121. PQfinish(conn);
  122. exit(1);
  123. }
  124. PQclear(result);
  125. PQfinish(conn);
  126. exit(0);
  127. }
  128. static void
  129. help(const char *progname)
  130. {
  131. printf(_("%s removes a PostgreSQL database.\n\n"), progname);
  132. printf(_("Usage:\n"));
  133. printf(_(" %s [OPTION]... DBNAME\n"), progname);
  134. printf(_("\nOptions:\n"));
  135. printf(_(" -e, --echo show the commands being sent to the server\n"));
  136. printf(_(" -i, --interactive prompt before deleting anything\n"));
  137. printf(_(" -V, --version output version information, then exit\n"));
  138. printf(_(" --if-exists don't report error if database doesn't exist\n"));
  139. printf(_(" -?, --help show this help, then exit\n"));
  140. printf(_("\nConnection options:\n"));
  141. printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
  142. printf(_(" -p, --port=PORT database server port\n"));
  143. printf(_(" -U, --username=USERNAME user name to connect as\n"));
  144. printf(_(" -w, --no-password never prompt for password\n"));
  145. printf(_(" -W, --password force password prompt\n"));
  146. printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
  147. printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
  148. }