listtab.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. \file db/driver/postgres/listdb.c
  3. \brief DBMI - Low Level PostgreSQL database driver - list tables
  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. */
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <grass/dbmi.h>
  11. #include <grass/glocale.h>
  12. #include "globals.h"
  13. #include "proto.h"
  14. int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
  15. {
  16. int i, j, nrows, trows, vrows, ncols, tablecol, tschemacol, viewcol,
  17. vschemacol;
  18. dbString *list;
  19. PGresult *rest, *resv;
  20. char buf[DB_SQL_MAX];
  21. *tlist = NULL;
  22. *tcount = 0;
  23. /* Get table names */
  24. sprintf(buf, "SELECT * FROM pg_tables WHERE schemaname %s "
  25. " ('pg_catalog', 'information_schema') ORDER BY tablename", system ? "IN" : "NOT IN");
  26. G_debug(2, "SQL: %s", buf);
  27. rest = PQexec(pg_conn, buf);
  28. if (!rest || PQresultStatus(rest) != PGRES_TUPLES_OK) {
  29. db_d_append_error("%s\n%s",
  30. _("Unable to select table names."),
  31. PQerrorMessage(pg_conn));
  32. db_d_report_error();
  33. PQclear(rest);
  34. return DB_FAILED;
  35. }
  36. /* Find table and schema col */
  37. ncols = PQnfields(rest);
  38. tschemacol = -1;
  39. for (i = 0; i < ncols; i++) {
  40. if (strcmp(PQfname(rest, i), "tablename") == 0)
  41. tablecol = i;
  42. if (strcmp(PQfname(rest, i), "schemaname") == 0)
  43. tschemacol = i;
  44. }
  45. /* Get view names */
  46. sprintf(buf, "SELECT * FROM pg_views WHERE schemaname %s "
  47. " ('pg_catalog', 'information_schema') ORDER BY viewname", system ? "IN" : "NOT IN");
  48. G_debug(2, "SQL: %s", buf);
  49. resv = PQexec(pg_conn, buf);
  50. if (!resv || PQresultStatus(resv) != PGRES_TUPLES_OK) {
  51. db_d_append_error("%s\n%s",
  52. _("Unable to select view names."),
  53. PQerrorMessage(pg_conn));
  54. db_d_report_error();
  55. PQclear(resv);
  56. return DB_FAILED;
  57. }
  58. /* Find viewname and schema col */
  59. ncols = PQnfields(resv);
  60. vschemacol = -1;
  61. for (i = 0; i < ncols; i++) {
  62. if (strcmp(PQfname(resv, i), "viewname") == 0)
  63. viewcol = i;
  64. if (strcmp(PQfname(resv, i), "schemaname") == 0)
  65. vschemacol = i;
  66. }
  67. trows = PQntuples(rest);
  68. vrows = PQntuples(resv);
  69. nrows = trows + vrows;
  70. list = db_alloc_string_array(nrows);
  71. if (list == NULL) {
  72. db_d_append_error(_("Out of memory"));
  73. db_d_report_error();
  74. return DB_FAILED;
  75. }
  76. for (i = 0; i < trows; i++) {
  77. if (tschemacol >= 0) {
  78. sprintf(buf, "%s.%s", (char *)PQgetvalue(rest, i, tschemacol),
  79. (char *)PQgetvalue(rest, i, tablecol));
  80. }
  81. else {
  82. sprintf(buf, "%s", (char *)PQgetvalue(rest, i, tablecol));
  83. }
  84. db_set_string(&list[i], buf);
  85. }
  86. PQclear(rest);
  87. for (j = 0; j < vrows; j++) {
  88. if (vschemacol >= 0) {
  89. sprintf(buf, "%s.%s", (char *)PQgetvalue(resv, j, vschemacol),
  90. (char *)PQgetvalue(resv, j, viewcol));
  91. }
  92. else {
  93. sprintf(buf, "%s", (char *)PQgetvalue(resv, j, viewcol));
  94. }
  95. db_set_string(&list[i], buf);
  96. i++;
  97. }
  98. PQclear(resv);
  99. *tlist = list;
  100. *tcount = nrows;
  101. return DB_OK;
  102. }