listtab.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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[1000];
  21. *tlist = NULL;
  22. *tcount = 0;
  23. /* Get table names */
  24. rest =
  25. PQexec(pg_conn,
  26. "select * from pg_tables where tablename !~ 'pg_*' order by tablename");
  27. if (!rest || PQresultStatus(rest) != PGRES_TUPLES_OK) {
  28. db_d_append_error("%s\n%s",
  29. _("Unable to select table names."),
  30. PQerrorMessage(pg_conn));
  31. db_d_report_error();
  32. PQclear(rest);
  33. return DB_FAILED;
  34. }
  35. /* Find table and schema col */
  36. ncols = PQnfields(rest);
  37. tschemacol = -1;
  38. for (i = 0; i < ncols; i++) {
  39. if (strcmp(PQfname(rest, i), "tablename") == 0)
  40. tablecol = i;
  41. if (strcmp(PQfname(rest, i), "schemaname") == 0)
  42. tschemacol = i;
  43. }
  44. /* Get view names */
  45. resv =
  46. PQexec(pg_conn,
  47. "SELECT * FROM pg_views WHERE schemaname NOT IN ('pg_catalog','information_schema') AND viewname !~ '^pg_'");
  48. if (!resv || PQresultStatus(resv) != PGRES_TUPLES_OK) {
  49. db_d_append_error("%s\n%s",
  50. _("Unable to select view names."),
  51. PQerrorMessage(pg_conn));
  52. db_d_report_error();
  53. PQclear(resv);
  54. return DB_FAILED;
  55. }
  56. /* Find viewname and schema col */
  57. ncols = PQnfields(resv);
  58. vschemacol = -1;
  59. for (i = 0; i < ncols; i++) {
  60. if (strcmp(PQfname(resv, i), "viewname") == 0)
  61. viewcol = i;
  62. if (strcmp(PQfname(resv, i), "schemaname") == 0)
  63. vschemacol = i;
  64. }
  65. trows = PQntuples(rest);
  66. vrows = PQntuples(resv);
  67. nrows = trows + vrows;
  68. list = db_alloc_string_array(nrows);
  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 < trows; i++) {
  75. if (tschemacol >= 0) {
  76. sprintf(buf, "%s.%s", (char *)PQgetvalue(rest, i, tschemacol),
  77. (char *)PQgetvalue(rest, i, tablecol));
  78. }
  79. else {
  80. sprintf(buf, "%s", (char *)PQgetvalue(rest, i, tablecol));
  81. }
  82. db_set_string(&list[i], buf);
  83. }
  84. PQclear(rest);
  85. for (j = 0; j < vrows; j++) {
  86. if (vschemacol >= 0) {
  87. sprintf(buf, "%s.%s", (char *)PQgetvalue(resv, j, vschemacol),
  88. (char *)PQgetvalue(resv, j, viewcol));
  89. }
  90. else {
  91. sprintf(buf, "%s", (char *)PQgetvalue(resv, j, viewcol));
  92. }
  93. db_set_string(&list[i], buf);
  94. i++;
  95. }
  96. PQclear(resv);
  97. *tlist = list;
  98. *tcount = nrows;
  99. return DB_OK;
  100. }