listdb.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*!
  2. \file db/driver/postgres/listdb.c
  3. \brief DBMI - Low Level PostgreSQL database driver - list databases
  4. This program is free software under the GNU General Public License
  5. (>=v2). Read the file COPYING that comes with GRASS for details.
  6. \author Radim Blazek
  7. \author Updated for GRASS 7 by Martin Landa <landa.martin gmail.com>
  8. */
  9. #include <grass/dbmi.h>
  10. #include <grass/glocale.h>
  11. #include "globals.h"
  12. #include "proto.h"
  13. int db__driver_list_databases(dbString * dbpath, int npaths,
  14. dbHandle ** dblist, int *dbcount)
  15. {
  16. int i;
  17. const char *user, *passwd, *host, *port;
  18. PGCONN pgconn;
  19. PGresult *res;
  20. int rec_num = 0;
  21. dbHandle *list;
  22. *dblist = NULL;
  23. *dbcount = 0;
  24. /* TODO: the solution below is not good as user usually does not
  25. * have permissions for "template1" */
  26. if (npaths < 1) {
  27. db_d_append_error(_("No path given"));
  28. db_d_report_error();
  29. return DB_FAILED;
  30. }
  31. if (parse_conn(db_get_string(dbpath), &pgconn) == DB_FAILED) {
  32. db_d_report_error();
  33. return DB_FAILED;
  34. }
  35. G_debug(1, "db = %s, user = %s, pass = %s, "
  36. "host = %s, port = %s, options = %s, tty = %s",
  37. pgconn.dbname, pgconn.user, pgconn.password, pgconn.host,
  38. pgconn.port, pgconn.options, pgconn.tty);
  39. db_get_login2("pg", NULL, &user, &passwd, &host, &port);
  40. G_debug(1, "user = %s, passwd = %s", user, passwd ? "xxx" : "");
  41. if (user || passwd) {
  42. pg_conn = PQsetdbLogin(host, port, pgconn.options, pgconn.tty,
  43. "template1", user, passwd);
  44. }
  45. else {
  46. pg_conn =
  47. PQsetdb(host, port, pgconn.options, pgconn.tty,
  48. "template1");
  49. }
  50. if (PQstatus(pg_conn) == CONNECTION_BAD) {
  51. db_d_append_error("%s\n%s",
  52. _("Unable to connect to Postgres:"),
  53. PQerrorMessage(pg_conn));
  54. db_d_report_error();
  55. PQfinish(pg_conn);
  56. return DB_FAILED;
  57. }
  58. res = PQexec(pg_conn, "select datname from pg_database");
  59. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
  60. db_d_append_error("%s\n%s",
  61. _("Unable to select from Postgres:"),
  62. PQerrorMessage(pg_conn));
  63. db_d_report_error();
  64. PQclear(res);
  65. PQfinish(pg_conn);
  66. return DB_FAILED;
  67. }
  68. rec_num = PQntuples(res);
  69. list = db_alloc_handle_array(rec_num);
  70. if (list == NULL) {
  71. db_d_append_error(_("Out of memory"));
  72. db_d_report_error();
  73. return DB_FAILED;
  74. }
  75. for (i = 0; i < rec_num; i++) {
  76. db_init_handle(&list[i]);
  77. if (db_set_handle(&list[i], PQgetvalue(res, i, 0), NULL) != DB_OK) {
  78. db_d_append_error(_("Unable to set handle"));
  79. db_d_report_error();
  80. db_free_handle_array(list, rec_num);
  81. return DB_FAILED;
  82. }
  83. }
  84. PQclear(res);
  85. PQfinish(pg_conn);
  86. *dblist = list;
  87. *dbcount = rec_num;
  88. return DB_OK;
  89. }