2
/****************************************************************************
5
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
6
* Alex Shevlakov <sixote yahoo.com>,
7
* Glynn Clements <glynn gclements.plus.com>,
8
* Markus Neteler <neteler itc.it>,
9
* Hamish Bowman <hamish_b yahoo com>
10
* PURPOSE: set parameters for connection to database
11
* COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
13
* This program is free software under the GNU General Public
14
* License (>=v2). Read the file COPYING that comes with GRASS
17
*****************************************************************************/
21
#include <grass/gis.h>
22
#include <grass/dbmi.h>
23
#include <grass/codes.h>
24
#include <grass/glocale.h>
26
/* database for DBF can be written with variables:
27
* database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
30
int main(int argc, char *argv[])
33
struct Flag *print, *check_set_default;
35
/* struct Option *driver, *database, *user, *password, *keycol; */
36
struct Option *driver, *database, *schema, *group;
37
struct GModule *module;
40
/* Initialize the GIS calls */
44
module = G_define_module();
45
module->keywords = _("database, attribute table");
47
_("Prints/sets general DB connection for current mapset and exits.");
49
print = G_define_flag();
51
print->description = _("Print current connection parameters and exit");
52
print->guisection = _("Print");
54
check_set_default = G_define_flag();
55
check_set_default->key = 'c';
56
check_set_default->description =
57
_("Check connection parameters, set if uninitialized, and exit");
59
driver = G_define_standard_option(G_OPT_DRIVER);
60
driver->options = db_list_drivers();
61
driver->answer = (char *) db_get_default_driver_name();
62
driver->guisection = _("Settings");
64
database = G_define_standard_option(G_OPT_DATABASE);
65
database->answer = (char *) db_get_default_database_name();
66
database->guisection = _("Settings");
68
schema = G_define_option();
69
schema->key = "schema";
70
schema->type = TYPE_STRING;
71
schema->required = NO;
72
schema->multiple = NO;
73
schema->answer = (char *) db_get_default_schema_name();
74
schema->label = _("Database schema");
75
schema->description = _("Do not use this option if schemas "
76
"are not supported by driver/database server");
77
schema->guisection = _("Settings");
79
group = G_define_option();
81
group->type = TYPE_STRING;
84
group->answer = (char*) db_get_default_group_name();
85
group->description = _("Default group of database users to which "
86
"select privilege is granted");
87
group->guisection = _("Settings");
89
/* commented due to new mechanism:
90
user = G_define_option() ;
92
user->type = TYPE_STRING ;
95
user->description= "User:" ;
97
password = G_define_option() ;
98
password->key = "password" ;
99
password->type = TYPE_STRING ;
100
password->required = NO ;
101
password->multiple = NO ;
102
password->description= "Password:" ;
105
if (G_parser(argc, argv))
110
/* get and print connection */
111
if (db_get_connection(&conn) == DB_OK) {
112
fprintf(stdout, "driver:%s\n",
113
conn.driverName ? conn.driverName : "");
114
fprintf(stdout, "database:%s\n",
115
conn.databaseName ? conn.databaseName : "");
116
fprintf(stdout, "schema:%s\n",
117
conn.schemaName ? conn.schemaName : "");
118
fprintf(stdout, "group:%s\n", conn.group ? conn.group : "");
121
G_fatal_error(_("Database connection not defined. "
128
if (check_set_default->answer) {
129
/* check connection and set to system-wide default in required */
131
* TODO: improve db_{get,set}_connection() to not return DB_OK on error
132
* (thus currently there is no point in checking for that here)
134
db_get_connection(&conn);
136
if (!conn.driverName && !conn.databaseName) {
138
db_set_default_connection();
139
db_get_connection(&conn);
141
G_message(_("Default driver / database set to:\n"
142
"driver: %s\ndatabase: %s"), conn.driverName,
145
/* they must be a matched pair, so if one is set but not the other
146
then give up and let the user figure it out */
147
else if (!conn.driverName) {
148
G_fatal_error(_("Default driver is not set"));
150
else if (!conn.databaseName) {
151
G_fatal_error(_("Default database is not set"));
154
/* connection either already existed or now exists */
160
db_get_connection(&conn); /* read current */
163
conn.driverName = driver->answer;
165
if (database->answer)
166
conn.databaseName = database->answer;
169
conn.schemaName = schema->answer;
172
conn.group = group->answer;
174
db_set_connection(&conn);