listtab.c 2.6 KB

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