open_pg.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*!
  2. \file lib/vector/Vlib/open_pg.c
  3. \brief Vector library - Open PostGIS layer as vector map layer
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2011-2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <string.h>
  11. #include <grass/vector.h>
  12. #include <grass/dbmi.h>
  13. #include <grass/glocale.h>
  14. #ifdef HAVE_POSTGRES
  15. #include "pg_local_proto.h"
  16. static char *get_key_column(struct Format_info_pg *pg_info)
  17. {
  18. char *key_column;
  19. char stmt[DB_SQL_MAX];
  20. PGresult *res;
  21. sprintf(stmt,
  22. "SELECT kcu.column_name "
  23. "FROM INFORMATION_SCHEMA.TABLES t "
  24. "LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc "
  25. "ON tc.table_catalog = t.table_catalog "
  26. "AND tc.table_schema = t.table_schema "
  27. "AND tc.table_name = t.table_name "
  28. "AND tc.constraint_type = 'PRIMARY KEY' "
  29. "LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu "
  30. "ON kcu.table_catalog = tc.table_catalog "
  31. "AND kcu.table_schema = tc.table_schema "
  32. "AND kcu.table_name = tc.table_name "
  33. "AND kcu.constraint_name = tc.constraint_name "
  34. "WHERE t.table_name = '%s'", pg_info->table_name);
  35. res = PQexec(pg_info->conn, stmt);
  36. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK ||
  37. PQntuples(res) != 1) {
  38. G_warning(_("No key column detected."));
  39. if (res)
  40. PQclear(res);
  41. return NULL;
  42. }
  43. key_column = G_store(PQgetvalue(res, 0, 0));
  44. PQclear(res);
  45. return key_column;
  46. }
  47. static SF_FeatureType ftype_from_string(const char *type)
  48. {
  49. SF_FeatureType sf_type;
  50. if (G_strcasecmp(type, "POINT") == 0)
  51. return SF_POINT;
  52. else if (G_strcasecmp(type, "LINESTRING") == 0)
  53. return SF_LINESTRING;
  54. else if (G_strcasecmp(type, "POLYGON") == 0)
  55. return SF_POLYGON;
  56. else if (G_strcasecmp(type, "MULTIPOINT") == 0)
  57. return SF_MULTIPOINT;
  58. else if (G_strcasecmp(type, "MULTILINESTRING") == 0)
  59. return SF_MULTILINESTRING;
  60. else if (G_strcasecmp(type, "MULTIPOLYGON") == 0)
  61. return SF_MULTIPOLYGON;
  62. else if (G_strcasecmp(type, "GEOMETRYCOLLECTION") == 0)
  63. return SF_GEOMETRYCOLLECTION;
  64. return SF_UNKNOWN;
  65. G_debug(3, "ftype_from_string(): type='%s' -> %d", type, sf_type);
  66. return sf_type;
  67. }
  68. #endif
  69. /*!
  70. \brief Open existing PostGIS feature table (level 1 - without topology)
  71. \todo Check database instead of geometry_columns
  72. \param[in,out] Map pointer to Map_info structure
  73. \param update TRUE for write mode, otherwise read-only
  74. \return 0 success
  75. \return -1 error
  76. */
  77. int V1_open_old_pg(struct Map_info *Map, int update)
  78. {
  79. #ifdef HAVE_POSTGRES
  80. int found, ntables, i;
  81. dbString stmt;
  82. PGresult *res;
  83. struct Format_info_pg *pg_info;
  84. db_init_string(&stmt);
  85. pg_info = &(Map->fInfo.pg);
  86. if (!pg_info->conninfo) {
  87. G_warning(_("Connection string not defined"));
  88. return -1;
  89. }
  90. if (!pg_info->table_name) {
  91. G_warning(_("PostGIS feature table not defined"));
  92. return -1;
  93. }
  94. G_debug(1, "V1_open_old_pg(): conninfo='%s' table='%s'", pg_info->conninfo,
  95. pg_info->table_name);
  96. /* connect database */
  97. pg_info->conn = PQconnectdb(pg_info->conninfo);
  98. G_debug(2, " PQconnectdb(): %s", pg_info->conninfo);
  99. if (PQstatus(pg_info->conn) == CONNECTION_BAD)
  100. G_fatal_error("%s\n%s", _("Connection ton PostgreSQL database failed."),
  101. PQerrorMessage(pg_info->conn));
  102. /* get DB name */
  103. pg_info->db_name = G_store(PQdb(pg_info->conn));
  104. if (!pg_info->db_name) {
  105. G_warning(_("Unable to get database name"));
  106. return -1;
  107. }
  108. /* get fid and geometry column */
  109. db_set_string(&stmt, "SELECT f_table_name, f_geometry_column,"
  110. "coord_dimension,srid,type "
  111. "FROM geometry_columns");
  112. G_debug(2, "SQL: %s", db_get_string(&stmt));
  113. res = PQexec(pg_info->conn, db_get_string(&stmt));
  114. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
  115. G_fatal_error("%s\n%s", _("No feature tables found in database."),
  116. PQresultErrorMessage(res));
  117. ntables = PQntuples(res);
  118. G_debug(3, "\tnrows = %d", ntables);
  119. found = FALSE;
  120. for (i = 0; i < ntables; i++) {
  121. if (strcmp(PQgetvalue(res, i, 0), pg_info->table_name) == 0) {
  122. /* geometry column */
  123. pg_info->geom_column = G_store(PQgetvalue(res, i, 1));
  124. G_debug(3, "\t-> table = %s column = %s", pg_info->table_name,
  125. pg_info->geom_column);
  126. /* fid column */
  127. pg_info->fid_column = get_key_column(pg_info);
  128. /* coordinates dimension */
  129. pg_info->coor_dim = atoi(PQgetvalue(res, i, 2));
  130. /* SRS ID */
  131. pg_info->srid = atoi(PQgetvalue(res, i, 3));
  132. /* feature type */
  133. pg_info->feature_type = ftype_from_string(PQgetvalue(res, i, 4));
  134. found = TRUE;
  135. break;
  136. }
  137. }
  138. /* no feature in cache */
  139. pg_info->cache.fid = -1;
  140. PQclear(res);
  141. db_free_string(&stmt);
  142. if (!found) {
  143. G_warning(_("Feature table <%s> not found in 'geometry_columns'"));
  144. return -1;
  145. }
  146. return 0;
  147. #else
  148. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  149. return -1;
  150. #endif
  151. }
  152. /*!
  153. \brief Open existing PostGIS layer (level 2 - feature index)
  154. \param[in,out] Map pointer to Map_info structure
  155. \return 0 success
  156. \return -1 error
  157. */
  158. int V2_open_old_pg(struct Map_info *Map)
  159. {
  160. #ifdef HAVE_POSTGRES
  161. G_debug(3, "V2_open_old_pg(): name = %s mapset = %s", Map->name,
  162. Map->mapset);
  163. if (Vect_open_fidx(Map, &(Map->fInfo.pg.offset)) != 0) {
  164. G_warning(_("Unable to open feature index file for vector map <%s>"),
  165. Vect_get_full_name(Map));
  166. G_zero(&(Map->fInfo.pg.offset), sizeof(struct Format_info_offset));
  167. }
  168. return 0;
  169. #else
  170. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  171. return -1;
  172. #endif
  173. }
  174. /*!
  175. \brief Prepare PostGIS database for creating new feature table
  176. (level 1)
  177. \todo To implement
  178. \param[out] Map pointer to Map_info structure
  179. \param name name of PostGIS feature table to create
  180. \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
  181. \return 0 success
  182. \return -1 error
  183. */
  184. int V1_open_new_pg(struct Map_info *Map, const char *name, int with_z)
  185. {
  186. G_debug(1, "V1_open_new_pg(): name = %s with_z = %d", name, with_z);
  187. #ifdef HAVE_POSTGRES
  188. return 0;
  189. #else
  190. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  191. return -1;
  192. #endif
  193. }