listtab.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/dbmi.h>
  4. #include "globals.h"
  5. #include "proto.h"
  6. int db__driver_list_tables(dbString ** tlist, int *tcount, int system)
  7. {
  8. int i, nrows, trows, vrows, ncols, tablecol, tschemacol, viewcol,
  9. vschemacol;
  10. dbString *list;
  11. PGresult *rest, *resv;
  12. char buf[1000];
  13. init_error();
  14. *tlist = NULL;
  15. *tcount = 0;
  16. /* Get table names */
  17. rest =
  18. PQexec(pg_conn,
  19. "select * from pg_tables where tablename !~ 'pg_*' order by tablename");
  20. if (!rest || PQresultStatus(rest) != PGRES_TUPLES_OK) {
  21. append_error("Cannot select table names\n");
  22. append_error(PQerrorMessage(pg_conn));
  23. report_error();
  24. PQclear(rest);
  25. return DB_FAILED;
  26. }
  27. /* Find table and schema col */
  28. ncols = PQnfields(rest);
  29. tschemacol = -1;
  30. for (i = 0; i < ncols; i++) {
  31. if (strcmp(PQfname(rest, i), "tablename") == 0)
  32. tablecol = i;
  33. if (strcmp(PQfname(rest, i), "schemaname") == 0)
  34. tschemacol = i;
  35. }
  36. /* Get view names */
  37. resv =
  38. PQexec(pg_conn,
  39. "SELECT * FROM pg_views WHERE schemaname NOT IN ('pg_catalog','information_schema') AND viewname !~ '^pg_'");
  40. if (!resv || PQresultStatus(resv) != PGRES_TUPLES_OK) {
  41. append_error("Cannot select view names\n");
  42. append_error(PQerrorMessage(pg_conn));
  43. report_error();
  44. PQclear(resv);
  45. return DB_FAILED;
  46. }
  47. /* Find viewname and schema col */
  48. ncols = PQnfields(resv);
  49. vschemacol = -1;
  50. for (i = 0; i < ncols; i++) {
  51. if (strcmp(PQfname(resv, i), "viewname") == 0)
  52. viewcol = i;
  53. if (strcmp(PQfname(resv, i), "schemaname") == 0)
  54. vschemacol = i;
  55. }
  56. trows = PQntuples(rest);
  57. vrows = PQntuples(resv);
  58. nrows = trows + vrows;
  59. list = db_alloc_string_array(nrows);
  60. if (list == NULL) {
  61. append_error("Cannot db_alloc_string_array()");
  62. report_error();
  63. return DB_FAILED;
  64. }
  65. for (i = 0; i < trows; i++) {
  66. if (tschemacol >= 0) {
  67. sprintf(buf, "%s.%s", (char *)PQgetvalue(rest, i, tschemacol),
  68. (char *)PQgetvalue(rest, i, tablecol));
  69. }
  70. else {
  71. sprintf(buf, "%s", (char *)PQgetvalue(rest, i, tablecol));
  72. }
  73. db_set_string(&list[i], buf);
  74. }
  75. PQclear(rest);
  76. for (i = 0; i < vrows; i++) {
  77. if (vschemacol >= 0) {
  78. sprintf(buf, "%s.%s", (char *)PQgetvalue(resv, i, vschemacol),
  79. (char *)PQgetvalue(resv, i, viewcol));
  80. }
  81. else {
  82. sprintf(buf, "%s", (char *)PQgetvalue(resv, i, viewcol));
  83. }
  84. db_set_string(&list[i], buf);
  85. }
  86. PQclear(resv);
  87. *tlist = list;
  88. *tcount = nrows;
  89. return DB_OK;
  90. }