[GRASS-SVN] r59295 - grass/trunk/lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 23 07:18:01 PDT 2014


Author: martinl
Date: 2014-03-23 07:18:00 -0700 (Sun, 23 Mar 2014)
New Revision: 59295

Modified:
   grass/trunk/lib/vector/Vlib/open_pg.c
Log:
vlib/pg: fix drop_table() when no PostGIS Topology is available


Modified: grass/trunk/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_pg.c	2014-03-23 12:05:33 UTC (rev 59294)
+++ grass/trunk/lib/vector/Vlib/open_pg.c	2014-03-23 14:18:00 UTC (rev 59295)
@@ -98,13 +98,6 @@
     if (!pg_info->conn)
         connect_db(pg_info);
     
-    /* get DB name */
-    pg_info->db_name = G_store(PQdb(pg_info->conn));
-    if (!pg_info->db_name) {
-        G_warning(_("Unable to get database name"));
-        return -1;
-    }
-
     /* get fid and geometry column */
     sprintf(stmt, "SELECT f_geometry_column, coord_dimension, srid, type "
             "FROM geometry_columns WHERE f_table_schema = '%s' AND "
@@ -248,16 +241,9 @@
     G_debug(1, "V1_open_new_pg(): conninfo='%s' table='%s'",
             pg_info->conninfo, pg_info->table_name);
 
-    /* connect database */
+    /* connect database & get DB name */
     connect_db(pg_info);
     
-    /* get DB name */
-    pg_info->db_name = G_store(PQdb(pg_info->conn));
-    if (!pg_info->db_name) {
-        G_warning(_("Unable to get database name"));
-        return -1;
-    }
-
     /* if schema not defined, use 'public' */
     if (!pg_info->schema_name)
         pg_info->schema_name = G_store("public");
@@ -462,34 +448,38 @@
     
     PGresult *result, *result_drop;
     
-    /* drop topology schema(s) related to the feature table */
-    sprintf(stmt, "SELECT t.name FROM topology.layer AS l JOIN "
-            "topology.topology AS t ON l.topology_id = t.id "
-            "WHERE l.table_name = '%s'", pg_info->table_name);
-    G_debug(2, "SQL: %s", stmt);
-    
-    result = PQexec(pg_info->conn, stmt);
-    if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
-        G_warning(_("Execution failed: %s"), PQerrorMessage(pg_info->conn));
-        PQclear(result);
-        return -1;
-    }
-    for (i = 0; i < PQntuples(result); i++) {
-        topo_schema = PQgetvalue(result, i, 0);
-        sprintf(stmt, "SELECT topology.DropTopology('%s')",
-                topo_schema);
+    /* check if topology schema exists */
+    sprintf(stmt, "SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'topology'");
+    if (Vect__execute_get_value_pg(pg_info->conn, stmt) != 0) {
+        /* drop topology schema(s) related to the feature table */
+        sprintf(stmt, "SELECT t.name FROM topology.layer AS l JOIN "
+                "topology.topology AS t ON l.topology_id = t.id "
+                "WHERE l.table_name = '%s'", pg_info->table_name);
         G_debug(2, "SQL: %s", stmt);
         
-        result_drop = PQexec(pg_info->conn, stmt);
-        if (!result_drop || PQresultStatus(result_drop) != PGRES_TUPLES_OK)
+        result = PQexec(pg_info->conn, stmt);
+        if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
             G_warning(_("Execution failed: %s"), PQerrorMessage(pg_info->conn));
-        
-        G_verbose_message(_("PostGIS topology schema <%s> dropped"),
-                          topo_schema);
-        PQclear(result_drop);
+            PQclear(result);
+            return -1;
+        }
+        for (i = 0; i < PQntuples(result); i++) {
+            topo_schema = PQgetvalue(result, i, 0);
+            sprintf(stmt, "SELECT topology.DropTopology('%s')",
+                    topo_schema);
+            G_debug(2, "SQL: %s", stmt);
+            
+            result_drop = PQexec(pg_info->conn, stmt);
+            if (!result_drop || PQresultStatus(result_drop) != PGRES_TUPLES_OK)
+                G_warning(_("Execution failed: %s"), PQerrorMessage(pg_info->conn));
+            
+            G_verbose_message(_("PostGIS topology schema <%s> dropped"),
+                              topo_schema);
+            PQclear(result_drop);
+        }
+        PQclear(result);
     }
-    PQclear(result);
-    
+
     /* drop feature table */
     sprintf(stmt, "DROP TABLE \"%s\".\"%s\"",
             pg_info->schema_name, pg_info->table_name);
@@ -520,10 +510,16 @@
                       _("Connection to PostgreSQL database failed."),
                       PQerrorMessage(pg_info->conn));
 
+    /* get DB name */
+    pg_info->db_name = G_store(PQdb(pg_info->conn));
+    if (!pg_info->db_name)
+        G_warning(_("Unable to get database name"));
+    
     sprintf(stmt, "SELECT COUNT(*) FROM pg_tables WHERE tablename = 'spatial_ref_sys'");
     if (Vect__execute_get_value_pg(pg_info->conn, stmt) != 1) {
         PQfinish(pg_info->conn);
-        G_fatal_error(_("Spatial-enabled PostGIS database is required"));
+        G_fatal_error(_("<%s> is not PostGIS database. DB table 'spatial_ref_sys' not found."),
+                      pg_info->db_name ? pg_info->db_name : pg_info->conninfo);
     }
 
     if (pg_info->toposchema_name) {
@@ -531,7 +527,8 @@
         sprintf(stmt, "SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'topology'");
         if (Vect__execute_get_value_pg(pg_info->conn, stmt) == 0) {
             PQfinish(pg_info->conn);
-            G_fatal_error(_("PostGIS Topology extension not found in the database"));
+            G_fatal_error(_("PostGIS Topology extension not found in the database <%s>"),
+                          pg_info->db_name);
         }
     }
 



More information about the grass-commit mailing list