listdb.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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;
  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, host = %s, port = %s, options = %s, tty = %s",
  36. pgconn.dbname, pgconn.user, pgconn.password, pgconn.host,
  37. pgconn.port, pgconn.options, pgconn.tty);
  38. db_get_login("pg", NULL, &user, &passwd);
  39. G_debug(1, "user = %s, passwd = %s", user, passwd ? "xxx" : "");
  40. if (user || passwd) {
  41. pg_conn = PQsetdbLogin(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
  42. "template1", user, passwd);
  43. }
  44. else {
  45. pg_conn =
  46. PQsetdb(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
  47. "template1");
  48. }
  49. if (PQstatus(pg_conn) == CONNECTION_BAD) {
  50. db_d_append_error("%s\n%s",
  51. _("Unable to connect to Postgres:"),
  52. PQerrorMessage(pg_conn));
  53. db_d_report_error();
  54. PQfinish(pg_conn);
  55. return DB_FAILED;
  56. }
  57. res = PQexec(pg_conn, "select datname from pg_database");
  58. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
  59. db_d_append_error("%s\n%s",
  60. _("Unable to select from Postgres:"),
  61. PQerrorMessage(pg_conn));
  62. db_d_report_error();
  63. PQclear(res);
  64. PQfinish(pg_conn);
  65. return DB_FAILED;
  66. }
  67. rec_num = PQntuples(res);
  68. list = db_alloc_handle_array(rec_num);
  69. if (list == NULL) {
  70. db_d_append_error(_("Out of memory"));
  71. db_d_report_error();
  72. return DB_FAILED;
  73. }
  74. for (i = 0; i < rec_num; i++) {
  75. db_init_handle(&list[i]);
  76. if (db_set_handle(&list[i], PQgetvalue(res, i, 0), NULL) != DB_OK) {
  77. db_d_append_error(_("Unable to set handle"));
  78. db_d_report_error();
  79. db_free_handle_array(list, rec_num);
  80. return DB_FAILED;
  81. }
  82. }
  83. PQclear(res);
  84. PQfinish(pg_conn);
  85. *dblist = list;
  86. *dbcount = rec_num;
  87. return DB_OK;
  88. }