[GRASS-SVN] r51105 - grass/trunk/vector/v.external

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 18 08:50:21 EDT 2012


Author: martinl
Date: 2012-03-18 05:50:21 -0700 (Sun, 18 Mar 2012)
New Revision: 51105

Modified:
   grass/trunk/vector/v.external/list.c
   grass/trunk/vector/v.external/local_proto.h
   grass/trunk/vector/v.external/main.c
Log:
v.external: support schema info for postgis links


Modified: grass/trunk/vector/v.external/list.c
===================================================================
--- grass/trunk/vector/v.external/list.c	2012-03-18 12:16:45 UTC (rev 51104)
+++ grass/trunk/vector/v.external/list.c	2012-03-18 12:50:21 UTC (rev 51105)
@@ -53,11 +53,29 @@
     return -1;
 }
 
+void get_table_name(const char *table, char **table_name, char **schema_name)
+{
+    char **tokens;
+
+    tokens = G_tokenize(table, ".");
+
+    if (G_number_of_tokens(tokens) > 1) {
+	*schema_name = G_store(tokens[0]);
+	*table_name  = G_store(tokens[1]);
+    }
+    else {
+	*schema_name = NULL;
+	*table_name  = G_store(tokens[0]);
+    }
+    
+    G_free_tokens(tokens);
+}
+
+#ifdef HAVE_POSTGRES
 int list_layers_pg(FILE *fd, const char *conninfo, const char *table, int print_types, int *is3D)
 {
-#ifdef HAVE_POSTGRES
     int row, ntables, ret, print_schema;
-    char *value;
+    char *value, *schema_name, *table_name;
     PGconn *conn;
     PGresult *res;
     
@@ -80,20 +98,27 @@
     if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	G_fatal_error("%s\n%s", _("No feature tables found in database."),
 		      PQresultErrorMessage(res));
+
+    /* get schema & table_name */
+    table_name = schema_name = NULL;
+    if (table)
+	get_table_name(table, &table_name, &schema_name);
     
     ntables = PQntuples(res);
     G_debug(3, "   nrows = %d", ntables);
     if (fd)
 	G_message(_("PostGIS database <%s> contains %d feature table(s):"),
 		  PQdb(conn), ntables);
-
+    
     /* report also schemas */
     print_schema = FALSE;
-    for (row = 0; row < ntables; row++) {
-	value = PQgetvalue(res, row, 0);
-	if (strcmp(value, "public") != 0) {
-	    print_schema = TRUE;
-	    break;
+    if (fd) {
+	for (row = 0; row < ntables; row++) {
+	    value = PQgetvalue(res, row, 0);
+	    if (strcmp(value, "public") != 0) {
+		print_schema = TRUE;
+		break;
+	    }
 	}
     }
     
@@ -116,21 +141,24 @@
 		    fprintf(fd, "%s\n", value);
 	    }
 	}
-	if (table && strcmp(value, table) == 0) {
+	if (table_name && strcmp(value, table_name) == 0) {
 	    ret = row;
 	    *is3D = WITHOUT_Z;
 	}
     }
     
+    if (table_name)
+	G_free(table_name);
+    if (schema_name)
+	G_free(schema_name);
+    
     PQclear(res);
     PQfinish(conn);
     G_debug(1, "PQfinish()");
     
     return ret;
-#else
-    G_fatal_error(_("GRASS not compiled with PostgreSQL/PostGIS support"));
-#endif
 }
+#endif /* HAVE_POSTGRES */
 
 #ifdef HAVE_OGR
 int list_layers_ogr(FILE *fd, const char *dsn, const char *layer, int print_types, int *is3D)

Modified: grass/trunk/vector/v.external/local_proto.h
===================================================================
--- grass/trunk/vector/v.external/local_proto.h	2012-03-18 12:16:45 UTC (rev 51104)
+++ grass/trunk/vector/v.external/local_proto.h	2012-03-18 12:50:21 UTC (rev 51105)
@@ -16,5 +16,6 @@
 /* list.c */
 void list_formats(FILE *);
 int list_layers(FILE *, const char *, const char *, int, int, int *);
+void get_table_name(const char *, char **, char **);
 
 #endif

Modified: grass/trunk/vector/v.external/main.c
===================================================================
--- grass/trunk/vector/v.external/main.c	2012-03-18 12:16:45 UTC (rev 51104)
+++ grass/trunk/vector/v.external/main.c	2012-03-18 12:50:21 UTC (rev 51105)
@@ -142,9 +142,18 @@
     }
     
     if (flags.postgis->answer) {
+	char *table_name, *schema_name;
+	
+	get_table_name(options.layer->answer, &table_name, &schema_name);
+	
 	fprintf(fd, "FORMAT: postgis\n");
 	fprintf(fd, "CONNINFO: %s\n", dsn);
-	fprintf(fd, "TABLE: %s\n", options.layer->answer);
+	if (schema_name)
+	    fprintf(fd, "SCHEMA: %s\n", schema_name);
+	fprintf(fd, "TABLE: %s\n", table_name);
+
+	G_free(table_name);
+	G_free(schema_name);
     }
     else {
 	fprintf(fd, "FORMAT: ogr\n");



More information about the grass-commit mailing list