[GRASS-SVN] r67741 - in grass/branches/releasebranch_7_0: db/db.login db/drivers/mysql db/drivers/postgres include include/defs lib/db/dbmi_base lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Feb 6 07:25:05 PST 2016


Author: martinl
Date: 2016-02-06 07:25:05 -0800 (Sat, 06 Feb 2016)
New Revision: 67741

Modified:
   grass/branches/releasebranch_7_0/db/db.login/db.login.html
   grass/branches/releasebranch_7_0/db/db.login/main.c
   grass/branches/releasebranch_7_0/db/drivers/mysql/db.c
   grass/branches/releasebranch_7_0/db/drivers/postgres/db.c
   grass/branches/releasebranch_7_0/db/drivers/postgres/listdb.c
   grass/branches/releasebranch_7_0/include/dbmi.h
   grass/branches/releasebranch_7_0/include/defs/dbmi.h
   grass/branches/releasebranch_7_0/lib/db/dbmi_base/connect.c
   grass/branches/releasebranch_7_0/lib/db/dbmi_base/login.c
   grass/branches/releasebranch_7_0/lib/vector/Vlib/open_pg.c
Log:
db.login + pg & mysql driver: support hostname and port
dbmi: add db_get|set_login2() which supports also hostname and port
      (merge r65332, r65056, and r65057 from trunk)


Modified: grass/branches/releasebranch_7_0/db/db.login/db.login.html
===================================================================
--- grass/branches/releasebranch_7_0/db/db.login/db.login.html	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/db/db.login/db.login.html	2016-02-06 15:25:05 UTC (rev 67741)
@@ -1,12 +1,12 @@
 <h2>DESCRIPTION</h2>
 
 <em>db.login</em> sets login parameters such an user name and
-optionally also a password for the connection to
+optionally also a password, a hostname or a port for the connection to
 the selected <b>database</b> through the DB <b>driver</b>.
 
 <h2>NOTE</h2>
 
-This command is only related to SQL database
+Options <b>host</b> and <b>port</b> are related to only SQL database
 backends like <a href="grass-pg.html">PostgreSQL</a>,
 <a href="grass-mysql.html">MySQL</a> or <a href="grass-odbc.html">ODBC</a>.
 
@@ -31,16 +31,15 @@
 </pre></div>
 
 <p>
-Username and password specified (note that the command lines
+Username, password and hostname specified (note that the command lines
 history will store the password in this way):
 
 <div class="code"><pre>
-db.login user=bacava password=secret
+db.login driver=pg database=mydb user=bacava password=secret host=db.example.com
 </pre></div>
 
 <p>
-Username and empty password specified (note that the command
-lines history will store the password in this way):
+Username and empty password specified:
 
 <div class="code"><pre>
 db.login driver=pg database=mydb user=bacava password=""
@@ -60,7 +59,8 @@
 
 <h2>AUTHOR</h2>
 
-Radim Blazek
+Radim Blazek<br>
+Support for hostname and port by Martin Landa, OSGeoREL, Czech Technical University in Prague, Czech Republic
 
 <p>
 <i>Last changed: $Date$</i>

Modified: grass/branches/releasebranch_7_0/db/db.login/main.c
===================================================================
--- grass/branches/releasebranch_7_0/db/db.login/main.c	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/db/db.login/main.c	2016-02-06 15:25:05 UTC (rev 67741)
@@ -6,7 +6,7 @@
  *               Glynn Clements <glynn gclements.plus.com>
  *               Markus Neteler <neteler itc.it>
  * PURPOSE:      Store db login settings
- * COPYRIGHT:    (C) 2004-2014 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2004-2015 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
  *               Public License (>=v2). Read the file COPYING that
@@ -29,7 +29,7 @@
 
 int main(int argc, char *argv[])
 {
-    struct Option *driver, *database, *user, *password;
+    struct Option *driver, *database, *user, *password, *host, *port;
     struct Flag *print;
     struct GModule *module;
     
@@ -40,7 +40,8 @@
     G_add_keyword(_("database"));
     G_add_keyword(_("connection settings"));
     module->description = _("Sets user/password for DB driver/database.");
-
+    module->overwrite = TRUE;
+    
     driver = G_define_standard_option(G_OPT_DB_DRIVER);
     driver->options = db_list_drivers();
     driver->required = YES;
@@ -55,7 +56,7 @@
     user->type = TYPE_STRING;
     user->required = NO;
     user->multiple = NO;
-    user->description = _("Username to set for DB connection");
+    user->description = _("Username");
     user->guisection = _("Settings");
     
     password = G_define_option();
@@ -63,9 +64,27 @@
     password->type = TYPE_STRING;
     password->required = NO;
     password->multiple = NO;
-    password->description = _("Password to set for DB connection");
+    password->description = _("Password");
     password->guisection = _("Settings");
 
+    host = G_define_option();
+    host->key = "host";
+    host->type = TYPE_STRING;
+    host->required = NO;
+    host->multiple = NO;
+    host->label = _("Hostname");
+    host->description = _("Relevant only for pg and mysql driver");
+    host->guisection = _("Settings");
+
+    port = G_define_option();
+    port->key = "port";
+    port->type = TYPE_STRING;
+    port->required = NO;
+    port->multiple = NO;
+    port->label = _("Port");
+    port->description = _("Relevant only for pg and mysql driver");
+    port->guisection = _("Settings");
+
     print = G_define_flag();
     print->key = 'p';
     print->description = _("Print connection settings and exit");
@@ -80,13 +99,15 @@
         exit(EXIT_SUCCESS);
     }
 
-    if (db_set_login(driver->answer, database->answer, user->answer,
-                     password->answer) == DB_FAILED) {
+    if (db_set_login2(driver->answer, database->answer, user->answer,
+                      password->answer, host->answer, port->answer,
+                      G_get_overwrite()) == DB_FAILED) {
         G_fatal_error(_("Unable to set user/password"));
     }
     
     if (password->answer)
-	G_important_message(_("The password was stored in file (%s%cdblogin)"), G_config_path(), HOST_DIRSEP);
+	G_important_message(_("The password was stored in file (%s%cdblogin)"),
+                            G_config_path(), HOST_DIRSEP);
     
     exit(EXIT_SUCCESS);
 }

Modified: grass/branches/releasebranch_7_0/db/drivers/mysql/db.c
===================================================================
--- grass/branches/releasebranch_7_0/db/drivers/mysql/db.c	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/db/drivers/mysql/db.c	2016-02-06 15:25:05 UTC (rev 67741)
@@ -37,7 +37,7 @@
 
     {
 	/* Client version */
-	const char *user, *password;
+        const char *user, *password, *host, *port;
 	CONNPAR connpar;
 
 	if (parse_conn(name, &connpar) == DB_FAILED) {
@@ -50,11 +50,11 @@
 		connpar.host, connpar.port, connpar.dbname,
 		connpar.user, connpar.password);
 
-	db_get_login("mysql", name, &user, &password);
+	db_get_login2("mysql", name, &user, &password, &host, &port);
 
 	connection = mysql_init(NULL);
-	res = mysql_real_connect(connection, connpar.host, user, password,
-				 connpar.dbname, connpar.port, NULL, 0);
+	res = mysql_real_connect(connection, host, user, password,
+				 connpar.dbname, port, NULL, 0);
 
 	if (res == NULL) {
 	    db_d_append_error("%s\n%s",

Modified: grass/branches/releasebranch_7_0/db/drivers/postgres/db.c
===================================================================
--- grass/branches/releasebranch_7_0/db/drivers/postgres/db.c	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/db/drivers/postgres/db.c	2016-02-06 15:25:05 UTC (rev 67741)
@@ -32,7 +32,7 @@
 int db__driver_open_database(dbHandle * handle)
 {
     char buf[500];
-    const char *name, *schema, *user, *password;
+    const char *name, *schema, *user, *password, *host, *port;
     dbConnection connection;
     PGCONN pgconn;
     PGresult *res;
@@ -56,14 +56,15 @@
 
     G_debug(3,
 	    "db_driver_open_database(): host = %s, port = %s, options = %s, tty = %s, "
-	    "dbname = %s, user = %s, password = %s, "
+	    "dbname = %s, user = %s, password = %s, host = %s, port = %s "
 	    "schema = %s", pgconn.host, pgconn.port, pgconn.options,
 	    pgconn.tty, pgconn.dbname, pgconn.user, pgconn.password,
+            pgconn.host, pgconn.port,
 	    pgconn.schema);
 
-    db_get_login("pg", name, &user, &password);
+    db_get_login2("pg", name, &user, &password, &host, &port);
 
-    pg_conn = PQsetdbLogin(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
+    pg_conn = PQsetdbLogin(host, port, pgconn.options, pgconn.tty,
 			   pgconn.dbname, user, password);
     
     if (PQstatus(pg_conn) == CONNECTION_BAD) {
@@ -221,7 +222,7 @@
 int create_delete_db(dbHandle *handle, int create)
 {
     dbString stmt;
-    const char *template_db, *name, *user, *password;
+    const char *template_db, *name, *user, *password, *host, *port;
     
     PGCONN pgconn;
     PGresult *res;
@@ -237,13 +238,14 @@
     }
     G_debug(3,
 	    "db_driver_create_database(): host = %s, port = %s, options = %s, tty = %s, "
-	    "dbname = %s, user = %s, password = %s, "
+	    "dbname = %s, user = %s, password = %s, host = %s, port = %s"
 	    "schema = %s", pgconn.host, pgconn.port, pgconn.options,
 	    pgconn.tty, pgconn.dbname, pgconn.user, pgconn.password,
+            pgconn.host, pgconn.port,
 	    pgconn.schema);
-    db_get_login("pg", template_db, &user, &password);
+    db_get_login2("pg", template_db, &user, &password, &host, &port);
     
-    pg_conn = PQsetdbLogin(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
+    pg_conn = PQsetdbLogin(host, port, pgconn.options, pgconn.tty,
 			   pgconn.dbname, user, password);
     if (PQstatus(pg_conn) == CONNECTION_BAD) {
 	db_d_append_error(_("Connection failed."));

Modified: grass/branches/releasebranch_7_0/db/drivers/postgres/listdb.c
===================================================================
--- grass/branches/releasebranch_7_0/db/drivers/postgres/listdb.c	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/db/drivers/postgres/listdb.c	2016-02-06 15:25:05 UTC (rev 67741)
@@ -19,7 +19,7 @@
 			      dbHandle ** dblist, int *dbcount)
 {
     int i;
-    const char *user, *passwd;
+    const char *user, *passwd, *host, *port;
     PGCONN pgconn;
     PGresult *res;
     int rec_num = 0;
@@ -42,20 +42,21 @@
         return DB_FAILED;
     }
     
-    G_debug(1, "db = %s, user = %s, pass = %s, host = %s, port = %s, options = %s, tty = %s",
+    G_debug(1, "db = %s, user = %s, pass = %s, "
+            "host = %s, port = %s, options = %s, tty = %s",
 	    pgconn.dbname, pgconn.user, pgconn.password, pgconn.host,
             pgconn.port, pgconn.options, pgconn.tty);
 
-    db_get_login("pg", NULL, &user, &passwd);
+    db_get_login2("pg", NULL, &user, &passwd, &host, &port);
     G_debug(1, "user = %s, passwd = %s", user, passwd ? "xxx" : "");
 
     if (user || passwd) {
-        pg_conn = PQsetdbLogin(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
+        pg_conn = PQsetdbLogin(host, port, pgconn.options, pgconn.tty,
                                "template1", user, passwd);
     }
     else {
         pg_conn =
-            PQsetdb(pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
+            PQsetdb(host, port, pgconn.options, pgconn.tty,
                     "template1");
     }
 

Modified: grass/branches/releasebranch_7_0/include/dbmi.h
===================================================================
--- grass/branches/releasebranch_7_0/include/dbmi.h	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/include/dbmi.h	2016-02-06 15:25:05 UTC (rev 67741)
@@ -293,10 +293,10 @@
 typedef struct _db_connection
 {
     char *driverName;
-    /* char *hostName; */
+    char *hostName;
     char *databaseName;
     char *schemaName;
-    char *location;
+    char *port;
     char *user;
     char *password;
     char *keycol;		/* name of default key column */

Modified: grass/branches/releasebranch_7_0/include/defs/dbmi.h
===================================================================
--- grass/branches/releasebranch_7_0/include/defs/dbmi.h	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/include/defs/dbmi.h	2016-02-06 15:25:05 UTC (rev 67741)
@@ -411,7 +411,11 @@
 void db_zero_string(dbString *);
 unsigned int db_sizeof_string(const dbString *);
 int db_set_login(const char *, const char *, const char *, const char *);
+int db_set_login2(const char *, const char *, const char *, const char *,
+                  const char *, const char *, int);
 int db_get_login(const char *, const char *, const char **, const char **);
+int db_get_login2(const char *, const char *, const char **, const char **,
+                  const char **, const char **);
 int db_get_login_dump(FILE *);
 
 #endif

Modified: grass/branches/releasebranch_7_0/lib/db/dbmi_base/connect.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/db/dbmi_base/connect.c	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/lib/db/dbmi_base/connect.c	2016-02-06 15:25:05 UTC (rev 67741)
@@ -80,17 +80,12 @@
     connection->schemaName = (char *)G_getenv_nofatal2("DB_SCHEMA", G_VAR_MAPSET);
     connection->group = (char *)G_getenv_nofatal2("DB_GROUP", G_VAR_MAPSET);
 
-    /* below commented due to new mechanism: see db_get_login()
-       connection->hostName = G_getenv_nofatal("DB_HOST");
-       connection->location = G_getenv_nofatal("DB_LOCATION");
-       connection->user = G_getenv_nofatal("DB_USER");
-       connection->password = G_getenv_nofatal("DB_PASSWORD");
-     */
-
     /* try to get user/password */
-    db_get_login(connection->driverName, connection->databaseName,
-                 (const char **) &(connection->user),
-                 (const char **) &(connection->password));
+    db_get_login2(connection->driverName, connection->databaseName,
+                  (const char **) &(connection->user),
+                  (const char **) &(connection->password),
+                  (const char **) &(connection->hostName),
+                  (const char **) &(connection->port));
     
     return DB_OK;
 }

Modified: grass/branches/releasebranch_7_0/lib/db/dbmi_base/login.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/db/dbmi_base/login.c	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/lib/db/dbmi_base/login.c	2016-02-06 15:25:05 UTC (rev 67741)
@@ -3,7 +3,7 @@
   
   \brief DBMI Library (base) - login settings
   
-  (C) 1999-2014 by the GRASS Development Team
+  (C) 1999-2015 by the GRASS Development Team
   
   This program is free software under the GNU General Public
   License (>=v2). Read the file COPYING that comes with GRASS
@@ -29,6 +29,8 @@
     char *database;
     char *user;
     char *password;
+    char *host;
+    char *port;
 } DATA;
 
 typedef struct
@@ -57,19 +59,28 @@
 }
 
 static void add_login(LOGIN * login, const char *dr, const char *db, const char *usr,
-		      const char *pwd)
+		      const char *pwd, const char *host, const char *port, int idx)
 {
+    int login_idx;
+    
     if (login->n == login->a) {
 	login->a += 10;
 	login->data =
 	    (DATA *) realloc((void *)login->data, login->a * sizeof(DATA));
     }
-    login->data[login->n].driver = G_store(dr);
-    login->data[login->n].database = G_store(db);
-    login->data[login->n].user = G_store(usr ? usr : "");
-    login->data[login->n].password = G_store(pwd ? pwd : "");
-
-    login->n++;
+    if (idx > -1 && idx < login->n) {
+        login_idx = idx;
+    }
+    else {
+        login_idx = login->n;
+        login->n++;
+    }
+    login->data[login_idx].driver = G_store(dr);
+    login->data[login_idx].database = G_store(db);
+    login->data[login_idx].user = G_store(usr ? usr : "");
+    login->data[login_idx].password = G_store(pwd ? pwd : "");
+    login->data[login_idx].host = G_store(host ? host : "");
+    login->data[login_idx].port = G_store(port ? port : "");
 }
 
 /*
@@ -82,7 +93,7 @@
     int ret;
     const char *file;
     FILE *fd;
-    char buf[2001], dr[500], db[500], usr[500], pwd[500];
+    char buf[2001], dr[500], db[500], usr[500], pwd[500], host[500], port[500];
 
     login->n = 0;
     file = login_filename();
@@ -103,18 +114,19 @@
     while (G_getl2(buf, 2000, fd)) {
 	G_chop(buf);
 
-	usr[0] = pwd[0] = '\0';
-	ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^\n]", dr, db, usr, pwd);
+	usr[0] = pwd[0] = host[0] = port[0] = '\0';
+	ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^\n]",
+                     dr, db, usr, pwd, host, port);
 
-	G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s]",
-		ret, dr, db, usr, pwd);
+	G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s], port=[%s]",
+		ret, dr, db, usr, pwd, host, port);
 
 	if (ret < 2) {
 	    G_warning(_("Login file (%s) corrupted (line: %s)"), file, buf);
 	    continue;
 	}
 
-	add_login(login, dr, db, usr, pwd);
+	add_login(login, dr, db, usr, pwd, host, port, -1);
     }
 
     fclose(fd);
@@ -155,7 +167,12 @@
 	    if (login->data[i].password)
 		fprintf(fd, "|%s", login->data[i].password);
 	}
-	fprintf(fd, "\n");
+        if (login->data[i].host)
+            fprintf(fd, "|%s", login->data[i].host);
+        if (login->data[i].port)
+            fprintf(fd, "|%s", login->data[i].port);
+
+        fprintf(fd, "\n");
     }
 
     fclose(fd);
@@ -163,32 +180,22 @@
     return 0;
 }
 
-/*!
-  \brief Set user/password for driver/database
-  
-  \param driver driver name
-  \param database database name
-  \param user user name
-  \param password password string
-  
-  \return DB_OK on success
-  \return DB_FAILED on failure
- */
-int db_set_login(const char *driver, const char *database, const char *user,
-		 const char *password)
+static int set_login(const char *driver, const char *database, const char *user,
+                     const char *password, const char *host, const char *port,
+                     int overwrite)
 {
     int i, found;
     LOGIN login;
 
-    G_debug(3, "db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s]",
-	    driver, database, user, password);
+    G_debug(3, "db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s] port=[%s]",
+	    driver, database, user, password, host, port);
 
     init_login(&login);
 
     if (read_file(&login) == -1)
 	return DB_FAILED;
 
-    found = 0;
+    found = FALSE;
     for (i = 0; i < login.n; i++) {
 	if (strcmp(login.data[i].driver, driver) == 0 &&
 	    strcmp(login.data[i].database, database) == 0) {
@@ -202,13 +209,25 @@
 	    else
 		login.data[i].password = G_store("");
 
-	    found = 1;
+	    found = TRUE;
 	    break;
 	}
     }
 
+    if (found) {
+        if (overwrite)
+            G_warning(_("DB connection <%s/%s> already exists and will be overwritten"),
+                      driver, database ? database : "");
+        else
+            G_fatal_error(_("DB connection <%s/%s> already exists. "
+                            "Re-run '%s' with '--%s' flag to overwrite existing settings."),
+                          driver, database ? database : "", G_program_name(), "overwrite");
+    }
+    
     if (!found)
-	add_login(&login, driver, database, user, password);
+	add_login(&login, driver, database, user, password, host, port, -1);
+    else
+        add_login(&login, driver, database, user, password, host, port, i);
 
     if (write_file(&login) == -1)
 	return DB_FAILED;
@@ -216,21 +235,53 @@
     return DB_OK;
 }
 
-/*!  
-  \brief Get user/password for driver/database if driver/database
-  is not found, user/password are set to NULL
+/*!
+  \brief Set login parameters for driver/database
   
+  \deprecated Use db_set_login2() instead.
+  
+  \todo: GRASS 8: to be replaced by db_set_login2().
+
   \param driver driver name
-  \param database database name (can be NULL)
-  \param[out] user name
-  \param[out] password string
+  \param database database name
+  \param user user name
+  \param password password string
   
   \return DB_OK on success
   \return DB_FAILED on failure
-*/
-int db_get_login(const char *driver, const char *database, const char **user,
-		 const char **password)
+ */
+int db_set_login(const char *driver, const char *database, const char *user,
+                 const char *password)
 {
+    return set_login(driver, database, user,
+                     password, NULL, NULL, FALSE);
+}
+
+/*!
+  \brief Set login parameters for driver/database
+  
+  \param driver driver name
+  \param database database name
+  \param user user name
+  \param password password string
+  \param host host name
+  \param port
+  \param overwrite TRUE to overwrite exising connections
+  
+  \return DB_OK on success
+  \return DB_FAILED on failure
+ */
+int db_set_login2(const char *driver, const char *database, const char *user,
+		 const char *password, const char *host, const char *port,
+                 int overwrite)
+{
+    return set_login(driver, database, user,
+                     password, host, port, overwrite);
+}
+
+static int get_login(const char *driver, const char *database, const char **user,
+                     const char **password, const char **host, const char **port)
+{
     int i;
     LOGIN login;
 
@@ -238,7 +289,9 @@
 
     user[0] = '\0';
     password[0] = '\0';
-
+    host[0] = '\0';
+    port[0] = '\0';
+    
     init_login(&login);
 
     if (read_file(&login) == -1)
@@ -257,6 +310,16 @@
 	    else
 		*password = NULL;
 
+            if (login.data[i].host && strlen(login.data[i].host) > 0 && host)
+		*host = G_store(login.data[i].host);
+	    else
+		*host = NULL;
+
+            if (login.data[i].port && strlen(login.data[i].port) > 0 && port)
+		*port = G_store(login.data[i].port);
+	    else
+		*port = NULL;
+
 	    break;
 	}
     }
@@ -265,6 +328,52 @@
 }
 
 /*!  
+  \brief Get login parameters for driver/database
+
+  If driver/database is not found, output arguments are set to NULL.
+
+  \deprecated Use db_set_login2() instead.
+  
+  \todo: GRASS 8: to be replaced by db_set_login2().
+
+  \param driver driver name
+  \param database database name (can be NULL)
+  \param[out] user name
+  \param[out] password string
+
+  \return DB_OK on success
+  \return DB_FAILED on failure
+*/
+int db_get_login(const char *driver, const char *database, const char **user,
+		 const char **password)
+{
+    return get_login(driver, database, user,
+                     password, NULL, NULL);
+}
+
+/*!  
+  \brief Get login parameters for driver/database
+
+  If driver/database is not found, output arguments are set to NULL.
+  
+  \param driver driver name
+  \param database database name (can be NULL)
+  \param[out] user name
+  \param[out] password string
+  \param[out] host name
+  \param[out] port
+
+  \return DB_OK on success
+  \return DB_FAILED on failure
+*/
+int db_get_login2(const char *driver, const char *database, const char **user,
+                  const char **password, const char **host, const char **port)
+{
+    return get_login(driver, database, user,
+                     password, host, port);
+}
+
+/*!  
   \brief Print all connection settings to file
   
   \param fd file where to print settings
@@ -284,11 +393,13 @@
 	return DB_FAILED;
     
     for (i = 0; i < login.n; i++) {
-        fprintf(fd, "%s|%s|%s|%s\n",
+        fprintf(fd, "%s|%s|%s|%s|%s|%s\n",
                 login.data[i].driver,
                 login.data[i].database,
                 login.data[i].user,
-                login.data[i].password);
+                login.data[i].password,
+                login.data[i].host,
+                login.data[i].port);
     }
     
     return DB_OK;

Modified: grass/branches/releasebranch_7_0/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/vector/Vlib/open_pg.c	2016-02-06 15:03:37 UTC (rev 67740)
+++ grass/branches/releasebranch_7_0/lib/vector/Vlib/open_pg.c	2016-02-06 15:25:05 UTC (rev 67741)
@@ -507,7 +507,7 @@
     if (!strstr(pg_info->conninfo, "user")) {
         char dbname[GNAME_MAX];
         char *p;
-        const char *user, *passwd;
+        const char *user, *passwd, *host, *port;
         
         dbname[0] = '\0';
         p = strstr(pg_info->conninfo, "dbname");
@@ -521,11 +521,11 @@
         
         /* try connection settings for given database first, then try
          * any settings defined for pg driver */
-        db_get_login("pg", dbname, &user, &passwd);
+        db_get_login2("pg", dbname, &user, &passwd, &host, &port);
         if (strlen(dbname) > 0 && !user && !passwd)
-            db_get_login("pg", NULL, &user, &passwd);
+            db_get_login2("pg", NULL, &user, &passwd, &host, &port);
         
-        if (user || passwd) {
+        if (user || passwd || host || port) {
             char  conninfo[DB_SQL_MAX];
 
             sprintf(conninfo, "%s", pg_info->conninfo);
@@ -537,6 +537,14 @@
                 strcat(conninfo, " password=");
                 strcat(conninfo, passwd);
             }
+            if (host) {
+                strcat(conninfo, " host=");
+                strcat(conninfo, host);
+            }
+            if (port) {
+                strcat(conninfo, " port=");
+                strcat(conninfo, port);
+            }
             G_free(pg_info->conninfo);
             pg_info->conninfo = G_store(conninfo);
         }



More information about the grass-commit mailing list