[GRASS-SVN] r67510 - in grass/trunk/vector: v.external v.in.ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 7 02:58:00 PST 2016


Author: martinl
Date: 2016-01-07 02:58:00 -0800 (Thu, 07 Jan 2016)
New Revision: 67510

Added:
   grass/trunk/vector/v.external/dsn.c
   grass/trunk/vector/v.in.ogr/dsn.c
Modified:
   grass/trunk/vector/v.external/local_proto.h
   grass/trunk/vector/v.external/main.c
   grass/trunk/vector/v.in.ogr/main.c
Log:
v.in.ogr doesn't use information from dblogin file about external PostgreSQL server (#2385)
         experimental implementation containing duplicated code (TODO: will be probably moved to Vlib)


Added: grass/trunk/vector/v.external/dsn.c
===================================================================
--- grass/trunk/vector/v.external/dsn.c	                        (rev 0)
+++ grass/trunk/vector/v.external/dsn.c	2016-01-07 10:58:00 UTC (rev 67510)
@@ -0,0 +1,78 @@
+#include <string.h>
+
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+char *get_datasource_name(const char *opt_dsn, int use_ogr)
+{
+    char *dsn;
+    
+    if (G_strncasecmp(opt_dsn, "PG:", 3) == 0) {
+        /* PostgreSQL/PostGIS */
+        size_t i;
+        char connect_str[DB_SQL_MAX], database[GNAME_MAX];
+        char *p, *pp;
+        const char *user, *passwd, *host, *port;
+
+        /* dbname is mandatory */
+        p = G_strcasestr(opt_dsn, "dbname");
+        if (!p) 
+            G_fatal_error(_("Invalid connection string (dbname missing)"));
+        
+        /* get dbname */
+        p += strlen("dbname=");
+        for (i = 0, pp = p; *pp != ' ' && *pp != '\0'; pp++, i++)
+            database[i] = *pp;
+        database[i] = '\0';
+        
+        /* build connection string */
+        sprintf(connect_str, "dbname=%s", database);
+        
+        /* add db.login settings (user, password, host, port) */
+        if (DB_OK == db_get_login2("pg", database, &user, &passwd, &host, &port)) {
+            if (user) {
+                if (!G_strcasestr(opt_dsn, "user=")) {
+                    strcat(connect_str, " user=");
+                    strcat(connect_str, user);
+                }
+                G_free((char *)user);
+            }
+            if (passwd) {
+                if (!G_strcasestr(opt_dsn, "password=")) {
+                    strcat(connect_str, " password=");
+                    strcat(connect_str, passwd);
+                }
+                G_free((char *)passwd);
+            }
+            if (host) {
+                if (!G_strcasestr(opt_dsn, "host=")) {
+                    strcat(connect_str, " host=");
+                    strcat(connect_str, host);
+                }
+                G_free((char *)host);
+            }
+            if (port) {
+                if (!G_strcasestr(opt_dsn, "port=")) {
+                    strcat(connect_str, " port=");
+                    strcat(connect_str, port);
+                }
+                G_free((char *)port);
+            }
+        }
+        
+        if (!use_ogr)
+            /* be friendly, ignored 'PG:' prefix for PostGIS links */
+            dsn = G_store(connect_str);
+        else
+            G_asprintf(&dsn, "PG:%s", connect_str);
+    }
+    else {
+        /* other datasources */
+        dsn = G_store(opt_dsn);
+    }
+
+    G_debug(1, "dsn: %s", dsn);
+
+    return dsn;
+}

Modified: grass/trunk/vector/v.external/local_proto.h
===================================================================
--- grass/trunk/vector/v.external/local_proto.h	2016-01-07 09:21:18 UTC (rev 67509)
+++ grass/trunk/vector/v.external/local_proto.h	2016-01-07 10:58:00 UTC (rev 67510)
@@ -13,6 +13,9 @@
 void parse_args(int, char **,
 		struct _options *, struct _flags*);
 
+/* dsn.c */
+char *get_datasource_name(const char *, int);
+
 /* list.c */
 void list_formats();
 int list_layers(FILE *, const char *, const char *, int, int);

Modified: grass/trunk/vector/v.external/main.c
===================================================================
--- grass/trunk/vector/v.external/main.c	2016-01-07 09:21:18 UTC (rev 67509)
+++ grass/trunk/vector/v.external/main.c	2016-01-07 10:58:00 UTC (rev 67510)
@@ -8,7 +8,7 @@
  *               
  * PURPOSE:      Create a new vector as a link to OGR layer
  *               
- * COPYRIGHT:    (C) 2003-2011 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2016 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
  *               Public License (>=v2). Read the file COPYING that
@@ -95,22 +95,9 @@
         exit(EXIT_SUCCESS);
     }
 
-    /* be friendly, ignored 'PG:' prefix for PostGIS links */
     dsn = NULL;
-    if (options.dsn->answer) {
-        if (!use_ogr) {
-            int i, length;
-            
-            length = strlen(options.dsn->answer);
-            dsn = (char *) G_malloc(length - 3);
-            for (i = 3; i < length; i++)
-                dsn[i-3] = options.dsn->answer[i];
-            dsn[length-3] = '\0';
-        }
-        else {
-            dsn = G_store(options.dsn->answer);
-        }
-    }
+    if (options.dsn->answer)
+        dsn = get_datasource_name(options.dsn->answer, use_ogr);
     
     if (flags.list->answer || flags.tlist->answer) {
         /* list layers */

Added: grass/trunk/vector/v.in.ogr/dsn.c
===================================================================
--- grass/trunk/vector/v.in.ogr/dsn.c	                        (rev 0)
+++ grass/trunk/vector/v.in.ogr/dsn.c	2016-01-07 10:58:00 UTC (rev 67510)
@@ -0,0 +1,78 @@
+#include <string.h>
+
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+char *get_datasource_name(const char *opt_dsn, int use_ogr)
+{
+    char *dsn;
+    
+    if (G_strncasecmp(opt_dsn, "PG:", 3) == 0) {
+        /* PostgreSQL/PostGIS */
+        size_t i;
+        char connect_str[DB_SQL_MAX], database[GNAME_MAX];
+        char *p, *pp;
+        const char *user, *passwd, *host, *port;
+
+        /* dbname is mandatory */
+        p = G_strcasestr(opt_dsn, "dbname");
+        if (!p) 
+            G_fatal_error(_("Invalid connection string (dbname missing)"));
+        
+        /* get dbname */
+        p += strlen("dbname=");
+        for (i = 0, pp = p; *pp != ' ' && *pp != '\0'; pp++, i++)
+            database[i] = *pp;
+        database[i] = '\0';
+        
+        /* build connection string */
+        sprintf(connect_str, "dbname=%s", database);
+        
+        /* add db.login settings (user, password, host, port) */
+        if (DB_OK == db_get_login2("pg", database, &user, &passwd, &host, &port)) {
+            if (user) {
+                if (!G_strcasestr(opt_dsn, "user=")) {
+                    strcat(connect_str, " user=");
+                    strcat(connect_str, user);
+                }
+                G_free((char *)user);
+            }
+            if (passwd) {
+                if (!G_strcasestr(opt_dsn, "password=")) {
+                    strcat(connect_str, " password=");
+                    strcat(connect_str, passwd);
+                }
+                G_free((char *)passwd);
+            }
+            if (host) {
+                if (!G_strcasestr(opt_dsn, "host=")) {
+                    strcat(connect_str, " host=");
+                    strcat(connect_str, host);
+                }
+                G_free((char *)host);
+            }
+            if (port) {
+                if (!G_strcasestr(opt_dsn, "port=")) {
+                    strcat(connect_str, " port=");
+                    strcat(connect_str, port);
+                }
+                G_free((char *)port);
+            }
+        }
+        
+        if (!use_ogr)
+            /* be friendly, ignored 'PG:' prefix for PostGIS links */
+            dsn = G_store(connect_str);
+        else
+            G_asprintf(&dsn, "PG:%s", connect_str);
+    }
+    else {
+        /* other datasources */
+        dsn = G_store(opt_dsn);
+    }
+
+    G_debug(1, "dsn: %s", dsn);
+
+    return dsn;
+}

Modified: grass/trunk/vector/v.in.ogr/main.c
===================================================================
--- grass/trunk/vector/v.in.ogr/main.c	2016-01-07 09:21:18 UTC (rev 67509)
+++ grass/trunk/vector/v.in.ogr/main.c	2016-01-07 10:58:00 UTC (rev 67510)
@@ -46,6 +46,8 @@
 	     int field, int cat, double min_area, int type);
 int poly_count(OGRGeometryH hGeom, int line2boundary);
 
+char *get_datasource_name(const char *, int);
+
 int main(int argc, char *argv[])
 {
     struct GModule *module;
@@ -360,44 +362,9 @@
     else
 	datetime_type = "datetime";
 
-    /* dsn is 'PG:', check default connection settings */
     dsn = NULL;
-    if (driver_name && strcmp(driver_name, "pg") == 0 &&
-        G_strcasecmp(param.dsn->answer, "PG:") == 0) {
-        const char *dbname;
-        dbConnection conn;
-        
-        dbname = db_get_default_database_name();
-        if (!dbname)
-            G_fatal_error(_("Database not defined, please check default "
-                            " connection settings by db.connect"));
-
-        dsn = (char *) G_malloc(GPATH_MAX);
-        /* -> dbname */
-        sprintf(dsn, "PG:dbname=%s", dbname);
-        
-        /* -> user/passwd */
-        if (DB_OK == db_get_connection(&conn) &&
-            strcmp(conn.driverName, "pg") == 0 &&
-            strcmp(conn.databaseName, dbname) == 0) {
-            if (conn.user) {
-                strcat(dsn, " user=");
-                strcat(dsn, conn.user);
-            }
-            if (conn.password) {
-                strcat(dsn, " passwd=");
-                strcat(dsn, conn.password);
-            }
-            /* TODO: host/port... */
-        }
-        else {
-            G_debug(1, "unable to get connection");
-        }
-        G_debug(1, "Using dsn=%s", dsn);
-    }
-    else if (param.dsn->answer) {
-        dsn = G_store(param.dsn->answer);
-    }
+    if (param.dsn->answer)
+        dsn = get_datasource_name(param.dsn->answer, TRUE);
     
     min_area = atof(param.min_area->answer);
     snap = atof(param.snap->answer);



More information about the grass-commit mailing list