[GRASS-SVN] r51106 - in grass/trunk: include/vect lib/vector/Vlib lib/vector/diglib

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 18 08:59:01 EDT 2012


Author: martinl
Date: 2012-03-18 05:59:01 -0700 (Sun, 18 Mar 2012)
New Revision: 51106

Modified:
   grass/trunk/include/vect/dig_structs.h
   grass/trunk/lib/vector/Vlib/build_sfa.c
   grass/trunk/lib/vector/Vlib/close_pg.c
   grass/trunk/lib/vector/Vlib/open_pg.c
   grass/trunk/lib/vector/diglib/frmt.c
Log:
vlib(pg): support schemas


Modified: grass/trunk/include/vect/dig_structs.h
===================================================================
--- grass/trunk/include/vect/dig_structs.h	2012-03-18 12:50:21 UTC (rev 51105)
+++ grass/trunk/include/vect/dig_structs.h	2012-03-18 12:59:01 UTC (rev 51106)
@@ -590,6 +590,10 @@
     */
     char    *db_name;
     /*!
+      \brief Schema name
+    */
+    char    *schema_name;
+    /*!
       \brief Table name
     */
     char    *table_name;

Modified: grass/trunk/lib/vector/Vlib/build_sfa.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build_sfa.c	2012-03-18 12:50:21 UTC (rev 51105)
+++ grass/trunk/lib/vector/Vlib/build_sfa.c	2012-03-18 12:59:01 UTC (rev 51106)
@@ -330,13 +330,14 @@
     G_zero(&fparts, sizeof(struct feat_parts));
     
     /* get records */
-    sprintf(stmt, "SELECT %s,%s FROM %s",
+    sprintf(stmt, "SELECT %s,%s FROM %s.%s",
 	    pg_info->fid_column,
-	    pg_info->geom_column, pg_info->table_name);
+	    pg_info->geom_column, pg_info->schema_name, pg_info->table_name);
     G_debug(2, "SQL: %s", stmt);
     pg_info->res = PQexec(pg_info->conn, stmt);
     if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
 	PQclear(pg_info->res);
+	pg_info->res = NULL;
 	G_warning(_("Unable to get features:\n%s"),
 		  PQerrorMessage(pg_info->conn));
 	return; /* reading failed */

Modified: grass/trunk/lib/vector/Vlib/close_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close_pg.c	2012-03-18 12:50:21 UTC (rev 51105)
+++ grass/trunk/lib/vector/Vlib/close_pg.c	2012-03-18 12:59:01 UTC (rev 51106)
@@ -69,13 +69,12 @@
     }
     if (pg_info->cache.lines)
 	G_free(pg_info->cache.lines);
-    if (pg_info->db_name)
-	G_free(pg_info->db_name);
-    if (pg_info->geom_column)
-	G_free(pg_info->geom_column);
-    if (pg_info->fid_column)
-	G_free(pg_info->fid_column);
 
+    G_free(pg_info->db_name);
+    G_free(pg_info->schema_name);
+    G_free(pg_info->geom_column);
+    G_free(pg_info->fid_column);
+
     return 0;
 #else
     G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));

Modified: grass/trunk/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_pg.c	2012-03-18 12:50:21 UTC (rev 51105)
+++ grass/trunk/lib/vector/Vlib/open_pg.c	2012-03-18 12:59:01 UTC (rev 51106)
@@ -140,8 +140,8 @@
     }
     
     /* get fid and geometry column */
-    db_set_string(&stmt, "SELECT f_table_name, f_geometry_column,"
-		  "coord_dimension,srid,type "
+    db_set_string(&stmt, "SELECT f_table_schema, f_table_name, f_geometry_column,"
+		  "coord_dimension, srid, type "
 		  "FROM geometry_columns");
     G_debug(2, "SQL: %s", db_get_string(&stmt));
     
@@ -154,19 +154,27 @@
     G_debug(3, "\tnrows = %d", ntables);
     found = FALSE;
     for (i = 0; i < ntables; i++) {
-	if (strcmp(PQgetvalue(res, i, 0), pg_info->table_name) == 0) {
+	if ((pg_info->schema_name &&   /* schema defined */
+	     strcmp(PQgetvalue(res, i, 0), pg_info->schema_name) == 0 &&
+	     strcmp(PQgetvalue(res, i, 1), pg_info->table_name) == 0) ||
+	    (!pg_info->schema_name && /* schema not defined, get first match */
+	     strcmp(PQgetvalue(res, i, 1), pg_info->table_name) == 0)) {
+	    /* schema name */
+	    if (!pg_info->schema_name) {
+		pg_info->schema_name = G_store(PQgetvalue(res, i, 0));
+	    }
 	    /* geometry column */
-	    pg_info->geom_column = G_store(PQgetvalue(res, i, 1));
+	    pg_info->geom_column = G_store(PQgetvalue(res, i, 2));
 	    G_debug(3, "\t-> table = %s column = %s", pg_info->table_name,
 		    pg_info->geom_column);
 	    /* fid column */
 	    pg_info->fid_column = get_key_column(pg_info);
 	    /* coordinates dimension */
-	    pg_info->coor_dim = atoi(PQgetvalue(res, i, 2));
+	    pg_info->coor_dim = atoi(PQgetvalue(res, i, 3));
 	    /* SRS ID */
-	    pg_info->srid = atoi(PQgetvalue(res, i, 3));
+	    pg_info->srid = atoi(PQgetvalue(res, i, 4));
 	    /* feature type */
-	    pg_info->feature_type = ftype_from_string(PQgetvalue(res, i, 4));
+	    pg_info->feature_type = ftype_from_string(PQgetvalue(res, i, 5));
 	    found = TRUE;
 	    break;
 	}
@@ -180,7 +188,8 @@
     db_free_string(&stmt);
 
     if (!found) {
-	G_warning(_("Feature table <%s> not found in 'geometry_columns'"));
+	G_warning(_("Feature table <%s> not found in 'geometry_columns'"),
+		  pg_info->table_name);
 	return -1;
     }
     

Modified: grass/trunk/lib/vector/diglib/frmt.c
===================================================================
--- grass/trunk/lib/vector/diglib/frmt.c	2012-03-18 12:50:21 UTC (rev 51105)
+++ grass/trunk/lib/vector/diglib/frmt.c	2012-03-18 12:59:01 UTC (rev 51106)
@@ -117,6 +117,8 @@
 	if (frmt == GV_FORMAT_POSTGIS) {
 	    if (strcmp(buf1, "CONNINFO") == 0)
 		finfo->pg.conninfo = G_store(ptr);
+	    if (strcmp(buf1, "SCHEMA") == 0)
+		finfo->pg.schema_name = G_store(ptr);
 	    if (strcmp(buf1, "TABLE") == 0)
 		finfo->pg.table_name = G_store(ptr);
 	}



More information about the grass-commit mailing list