listtab.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. init_error();
  22. *tlist = NULL;
  23. *tcount = 0;
  24. /* Get table names */
  25. rest =
  26. PQexec(pg_conn,
  27. "select * from pg_tables where tablename !~ 'pg_*' order by tablename");
  28. if (!rest || PQresultStatus(rest) != PGRES_TUPLES_OK) {
  29. append_error(_("Unable to select table names\n"));
  30. append_error(PQerrorMessage(pg_conn));
  31. 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. append_error(_("Unable to select view names\n"));
  50. append_error(PQerrorMessage(pg_conn));
  51. report_error();
  52. PQclear(resv);
  53. return DB_FAILED;
  54. }
  55. /* Find viewname and schema col */
  56. ncols = PQnfields(resv);
  57. vschemacol = -1;
  58. for (i = 0; i < ncols; i++) {
  59. if (strcmp(PQfname(resv, i), "viewname") == 0)
  60. viewcol = i;
  61. if (strcmp(PQfname(resv, i), "schemaname") == 0)
  62. vschemacol = i;
  63. }
  64. trows = PQntuples(rest);
  65. vrows = PQntuples(resv);
  66. nrows = trows + vrows;
  67. list = db_alloc_string_array(nrows);
  68. if (list == NULL) {
  69. append_error(_("db_alloc_string_array() failed"));
  70. report_error();
  71. return DB_FAILED;
  72. }
  73. for (i = 0; i < trows; i++) {
  74. if (tschemacol >= 0) {
  75. sprintf(buf, "%s.%s", (char *)PQgetvalue(rest, i, tschemacol),
  76. (char *)PQgetvalue(rest, i, tablecol));
  77. }
  78. else {
  79. sprintf(buf, "%s", (char *)PQgetvalue(rest, i, tablecol));
  80. }
  81. db_set_string(&list[i], buf);
  82. }
  83. PQclear(rest);
  84. for (j = 0; j < vrows; j++) {
  85. if (vschemacol >= 0) {
  86. sprintf(buf, "%s.%s", (char *)PQgetvalue(resv, j, vschemacol),
  87. (char *)PQgetvalue(resv, j, viewcol));
  88. }
  89. else {
  90. sprintf(buf, "%s", (char *)PQgetvalue(resv, j, viewcol));
  91. }
  92. db_set_string(&list[i], buf);
  93. i++;
  94. }
  95. PQclear(resv);
  96. *tlist = list;
  97. *tcount = nrows;
  98. return DB_OK;
  99. }