~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to db/base/connect.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/****************************************************************************
3
 
 *
4
 
 * MODULE:       db.connect
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
12
 
 *
13
 
 *               This program is free software under the GNU General Public
14
 
 *               License (>=v2). Read the file COPYING that comes with GRASS
15
 
 *               for details.
16
 
 *
17
 
 *****************************************************************************/
18
 
#include <stdio.h>
19
 
#include <stdlib.h>
20
 
#include <string.h>
21
 
#include <grass/gis.h>
22
 
#include <grass/dbmi.h>
23
 
#include <grass/codes.h>
24
 
#include <grass/glocale.h>
25
 
 
26
 
/* database for DBF can be written with variables:
27
 
 *   database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
28
 
 */
29
 
 
30
 
int main(int argc, char *argv[])
31
 
{
32
 
    dbConnection conn;
33
 
    struct Flag *print, *check_set_default;
34
 
 
35
 
    /*    struct Option *driver, *database, *user, *password, *keycol; */
36
 
    struct Option *driver, *database, *schema, *group;
37
 
    struct GModule *module;
38
 
 
39
 
 
40
 
    /* Initialize the GIS calls */
41
 
    G_gisinit(argv[0]);
42
 
 
43
 
    /* Set description */
44
 
    module = G_define_module();
45
 
    module->keywords = _("database, attribute table, connection settings");
46
 
    module->description =
47
 
        _("Prints/sets general DB connection for current mapset and exits.");
48
 
 
49
 
    print = G_define_flag();
50
 
    print->key = 'p';
51
 
    print->description = _("Print current connection parameters and exit");
52
 
    print->guisection = _("Print");
53
 
 
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");
58
 
 
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");
63
 
 
64
 
    database = G_define_standard_option(G_OPT_DATABASE);
65
 
    database->answer = (char *) db_get_default_database_name();
66
 
    database->guisection = _("Settings");
67
 
 
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");
78
 
 
79
 
    group = G_define_option();
80
 
    group->key = "group";
81
 
    group->type = TYPE_STRING;
82
 
    group->required = NO;
83
 
    group->multiple = NO;
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");
88
 
 
89
 
    /* commented due to new mechanism:
90
 
       user = G_define_option() ;
91
 
       user->key        = "user" ;
92
 
       user->type       = TYPE_STRING ;
93
 
       user->required   = NO  ;
94
 
       user->multiple   = NO ;
95
 
       user->description= "User:" ;    
96
 
 
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:" ;
103
 
     */
104
 
 
105
 
    if (G_parser(argc, argv))
106
 
        exit(EXIT_FAILURE);
107
 
 
108
 
 
109
 
    if (print->answer) {
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 : "");
119
 
        }
120
 
        else
121
 
            G_fatal_error(_("Database connection not defined. "
122
 
                            "Run db.connect."));
123
 
 
124
 
        exit(EXIT_SUCCESS);
125
 
    }
126
 
 
127
 
 
128
 
    if (check_set_default->answer) {
129
 
        /* check connection and set to system-wide default in required */
130
 
        /*
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)
133
 
         */
134
 
        db_get_connection(&conn);
135
 
 
136
 
        if (!conn.driverName && !conn.databaseName) {
137
 
 
138
 
            db_set_default_connection();
139
 
            db_get_connection(&conn);
140
 
 
141
 
            G_message(_("Default driver / database set to:\n"
142
 
                        "driver: %s\ndatabase: %s"), conn.driverName,
143
 
                      conn.databaseName);
144
 
        }
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"));
149
 
        }
150
 
        else if (!conn.databaseName) {
151
 
            G_fatal_error(_("Default database is not set"));
152
 
        }
153
 
 
154
 
        /* connection either already existed or now exists */
155
 
        exit(EXIT_SUCCESS);
156
 
    }
157
 
 
158
 
 
159
 
    /* set connection */
160
 
    db_get_connection(&conn);   /* read current */
161
 
 
162
 
    if (driver->answer)
163
 
        conn.driverName = driver->answer;
164
 
 
165
 
    if (database->answer)
166
 
        conn.databaseName = database->answer;
167
 
 
168
 
    if (schema->answer)
169
 
        conn.schemaName = schema->answer;
170
 
 
171
 
    if (group->answer)
172
 
        conn.group = group->answer;
173
 
 
174
 
    db_set_connection(&conn);
175
 
 
176
 
 
177
 
    exit(EXIT_SUCCESS);
178
 
}