[GRASS-SVN] r48294 - sandbox/martinl/v.topo.to.postgis

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Sep 14 07:43:20 EDT 2011


Author: martinl
Date: 2011-09-14 04:43:20 -0700 (Wed, 14 Sep 2011)
New Revision: 48294

Modified:
   sandbox/martinl/v.topo.to.postgis/main.c
   sandbox/martinl/v.topo.to.postgis/topo.c
Log:
v.topo.to.postgis: add -p flag to print sql statements


Modified: sandbox/martinl/v.topo.to.postgis/main.c
===================================================================
--- sandbox/martinl/v.topo.to.postgis/main.c	2011-09-14 07:40:51 UTC (rev 48293)
+++ sandbox/martinl/v.topo.to.postgis/main.c	2011-09-14 11:43:20 UTC (rev 48294)
@@ -29,9 +29,11 @@
 {
     struct GModule *module;
     struct {
-      struct Option *map, *db;
+	struct Option *map, *db;
     } opt;
-    
+    struct {
+	struct Flag *p;
+    } flag;
     char *conninfo;
     const char *name, *dbname;
     struct Map_info Map;
@@ -56,6 +58,11 @@
     opt.db->type = TYPE_STRING;
     opt.db->key_desc = "name";
     
+    flag.p = G_define_flag();
+    flag.p->key = 'p';
+    flag.p->description = _("Print SQL statemets and exit");
+    flag.p->suppress_required = YES;
+    
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -67,11 +74,14 @@
     Vect_open_old(&Map, name, "");
 
     /* connect database */
-    G_asprintf(&conninfo, "dbname=%s", dbname);
-    conn = PQconnectdb(conninfo);
-    if (PQstatus(conn) != CONNECTION_OK) {
-	PQfinish(conn);
-	G_fatal_error(_("Unable to open database <%s>"), dbname);
+    conn = NULL;
+    if (!flag.p->answer) {
+	G_asprintf(&conninfo, "dbname=%s", dbname);
+	conn = PQconnectdb(conninfo);
+	if (PQstatus(conn) != CONNECTION_OK) {
+	    PQfinish(conn);
+	    G_fatal_error(_("Unable to open database <%s>"), dbname);
+	}
     }
     
     /* convert GRASS topo to PostGIS */

Modified: sandbox/martinl/v.topo.to.postgis/topo.c
===================================================================
--- sandbox/martinl/v.topo.to.postgis/topo.c	2011-09-14 07:40:51 UTC (rev 48293)
+++ sandbox/martinl/v.topo.to.postgis/topo.c	2011-09-14 11:43:20 UTC (rev 48294)
@@ -59,7 +59,8 @@
     PQclear(res);
 
     /* Start a transaction block */
-    res = PQexec(conn, "BEGIN");
+    db_set_string(&stmt, "BEGIN");
+    execute(&stmt, conn, &res, "BEGIN", PGRES_COMMAND_OK);
     PQclear(res);
 
     /* create topology tables */
@@ -72,7 +73,7 @@
     nnodes = Vect_get_num_nodes(Map);
     for (node = 1; node <= nnodes; node++) {
 	Vect_get_node_coor(Map, node, &x, &y, &z);
-	G_debug(0, "node %d: x = %f y = %f z = %f", node, x, y, z);
+	G_debug(3, "node %d: x = %f y = %f z = %f", node, x, y, z);
 	G_asprintf(&sql, "SELECT topology.ST_AddIsoNode('%s', 0, "
 		  "'POINT(%f %f)')", name, x, y);
 	db_set_string(&stmt, sql);
@@ -87,7 +88,7 @@
 	    G_fatal_error(_("Unable to read line %d"), line);
 	if (ltype == -2)
 	    break; /* should not happen */
-	G_debug(0, "line %d: type = %d npoints = %d", line, ltype,
+	G_debug(3, "line %d: type = %d npoints = %d", line, ltype,
 		Points->n_points);
 	
 	switch(ltype) {
@@ -104,6 +105,7 @@
 	    /* -> edge */
 	    add_adge_face(Map, line, Points, name, &stmt);
 	    execute(&stmt, conn, &res, "SELECT", PGRES_TUPLES_OK);
+	    break;
 	default:
 	    G_warning(_("Unsupported type %d"), ltype);
 	    break;
@@ -111,9 +113,13 @@
     }
 
     /* end the transaction */
-    res = PQexec(conn, "END");
+    db_set_string(&stmt, "END");
+    execute(&stmt, conn, &res, "END", PGRES_COMMAND_OK);
     PQclear(res);
 
+    if (!conn)
+	fflush(stdout);
+    
     Vect_destroy_list(list);
     Vect_destroy_line_struct(Points);
     
@@ -123,8 +129,13 @@
 void execute(const dbString *stmt, PGconn *conn, PGresult **res,
 	     const char *cmd, ExecStatusType status)
 {
-    G_debug(0, "SQL: %s", db_get_string(stmt));
-	    
+    G_debug(1, "SQL: %s", db_get_string(stmt));
+    
+    if (!conn) {
+	fprintf(stdout, "%s;\n", db_get_string(stmt));
+	return;
+    }
+    
     *res = PQexec(conn, db_get_string(stmt));
     if (PQresultStatus(*res) != status) {
 	PQclear(*res);



More information about the grass-commit mailing list