[GRASS-SVN] r65056 - in grass/trunk: include include/defs lib/db/dbmi_base lib/vector/Vlib
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Apr 15 00:11:56 PDT 2015
Author: martinl
Date: 2015-04-15 00:11:56 -0700 (Wed, 15 Apr 2015)
New Revision: 65056
Modified:
grass/trunk/include/dbmi.h
grass/trunk/include/defs/dbmi.h
grass/trunk/lib/db/dbmi_base/connect.c
grass/trunk/lib/db/dbmi_base/login.c
grass/trunk/lib/vector/Vlib/open_pg.c
Log:
dbmi: add db_get|set_login2() which supports also hostname and port
Modified: grass/trunk/include/dbmi.h
===================================================================
--- grass/trunk/include/dbmi.h 2015-04-15 01:43:30 UTC (rev 65055)
+++ grass/trunk/include/dbmi.h 2015-04-15 07:11:56 UTC (rev 65056)
@@ -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/trunk/include/defs/dbmi.h
===================================================================
--- grass/trunk/include/defs/dbmi.h 2015-04-15 01:43:30 UTC (rev 65055)
+++ grass/trunk/include/defs/dbmi.h 2015-04-15 07:11:56 UTC (rev 65056)
@@ -410,7 +410,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/trunk/lib/db/dbmi_base/connect.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/connect.c 2015-04-15 01:43:30 UTC (rev 65055)
+++ grass/trunk/lib/db/dbmi_base/connect.c 2015-04-15 07:11:56 UTC (rev 65056)
@@ -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/trunk/lib/db/dbmi_base/login.c
===================================================================
--- grass/trunk/lib/db/dbmi_base/login.c 2015-04-15 01:43:30 UTC (rev 65055)
+++ grass/trunk/lib/db/dbmi_base/login.c 2015-04-15 07:11:56 UTC (rev 65056)
@@ -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;
@@ -257,6 +308,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 +326,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 +391,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/trunk/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_pg.c 2015-04-15 01:43:30 UTC (rev 65055)
+++ grass/trunk/lib/vector/Vlib/open_pg.c 2015-04-15 07:11:56 UTC (rev 65056)
@@ -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