[GRASS-SVN] r65057 - in grass/trunk/db: db.login drivers/mysql drivers/postgres
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Apr 15 00:14:54 PDT 2015
Author: martinl
Date: 2015-04-15 00:14:53 -0700 (Wed, 15 Apr 2015)
New Revision: 65057
Modified:
grass/trunk/db/db.login/db.login.html
grass/trunk/db/db.login/main.c
grass/trunk/db/drivers/mysql/db.c
grass/trunk/db/drivers/postgres/db.c
grass/trunk/db/drivers/postgres/listdb.c
Log:
db.login + pg & mysql driver: support hostname and port
Modified: grass/trunk/db/db.login/db.login.html
===================================================================
--- grass/trunk/db/db.login/db.login.html 2015-04-15 07:11:56 UTC (rev 65056)
+++ grass/trunk/db/db.login/db.login.html 2015-04-15 07:14:53 UTC (rev 65057)
@@ -1,46 +1,53 @@
<h2>DESCRIPTION</h2>
-<em>db.login</em> sets the user name and optionally also a password for
-the connection to the selected <b>database</b> through the DB <b>driver</b>.
+<em>db.login</em> sets login parameters such an user name and
+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 is only related to SQL database backends
+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>. The passwords are stored in a
-hidden, unencrypted file in the user account ('home' directory)
+or <a href="grass-odbc.html">ODBC</a>.
+
+<p>
+Note that the passwords are stored in a hidden, <i>unencrypted</i>
+file in the user account ('home' directory)
called <em>.grass7/dblogin</em>. Only the file owner can access this
file.
<h2>EXAMPLES</h2>
-Only username specified:
+Only username specified (assuming locally accessible PostgreSQL DB
+without password):
<div class="code"><pre>
-db.login user=bacava
+db.login driver=pg database=mydb
</pre></div>
<p>
-Username and password specified (note that the command
-lines history will store the password in this way):
+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.server.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 user=bacava password=""
+db.login driver=pg database=mydb user=bacava password=""
</pre></div>
+
<h2>SEE ALSO</h2>
<em>
<a href="db.connect.html">db.connect</a>,
-<a href="db.test.html">db.test</a>
+<a href="db.test.html">db.test</a>,
+<a href="db.tables.html">db.tables</a>
</em>
<p>
@@ -48,7 +55,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 (GRASS 7.1)
<p>
<i>Last changed: $Date$</i>
Modified: grass/trunk/db/db.login/main.c
===================================================================
--- grass/trunk/db/db.login/main.c 2015-04-15 07:11:56 UTC (rev 65056)
+++ grass/trunk/db/db.login/main.c 2015-04-15 07:14:53 UTC (rev 65057)
@@ -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/trunk/db/drivers/mysql/db.c
===================================================================
--- grass/trunk/db/drivers/mysql/db.c 2015-04-15 07:11:56 UTC (rev 65056)
+++ grass/trunk/db/drivers/mysql/db.c 2015-04-15 07:14:53 UTC (rev 65057)
@@ -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/trunk/db/drivers/postgres/db.c
===================================================================
--- grass/trunk/db/drivers/postgres/db.c 2015-04-15 07:11:56 UTC (rev 65056)
+++ grass/trunk/db/drivers/postgres/db.c 2015-04-15 07:14:53 UTC (rev 65057)
@@ -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/trunk/db/drivers/postgres/listdb.c
===================================================================
--- grass/trunk/db/drivers/postgres/listdb.c 2015-04-15 07:11:56 UTC (rev 65056)
+++ grass/trunk/db/drivers/postgres/listdb.c 2015-04-15 07:14:53 UTC (rev 65057)
@@ -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");
}
More information about the grass-commit
mailing list