File: dsn.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (83 lines) | stat: -rw-r--r-- 2,389 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <string.h>

#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>

char *get_datasource_name(const char *opt_dsn, int use_ogr)
{
    char *dsn;

    if (G_strncasecmp(opt_dsn, "PG:", 3) == 0) {
        /* PostgreSQL/PostGIS */
        size_t i;
        char connect_str[DB_SQL_MAX], database[GNAME_MAX];
        char *p, *pp;
        const char *user, *passwd, *host, *port;

        /* dbname is mandatory */
        p = G_strcasestr(opt_dsn, "dbname");
        if (!p)
            G_fatal_error(_("Invalid connection string (dbname missing)"));

        /* get dbname */
        p += strlen("dbname=");
        for (i = 0, pp = p; *pp != ' ' && *pp != '\0'; pp++, i++)
            database[i] = *pp;
        database[i] = '\0';

        /* build connection string */
        sprintf(connect_str, "%s", opt_dsn);

        /* add db.login settings (user, password, host, port) */
        if (DB_OK ==
            db_get_login2("pg", database, &user, &passwd, &host, &port)) {
            if (user) {
                if (!G_strcasestr(opt_dsn, "user=")) {
                    strcat(connect_str, " user=");
                    strcat(connect_str, user);
                }
                G_free((char *)user);
            }
            if (passwd) {
                if (!G_strcasestr(opt_dsn, "password=")) {
                    strcat(connect_str, " password=");
                    strcat(connect_str, passwd);
                }
                G_free((char *)passwd);
            }
            if (host) {
                if (!G_strcasestr(opt_dsn, "host=")) {
                    strcat(connect_str, " host=");
                    strcat(connect_str, host);
                }
                G_free((char *)host);
            }
            if (port) {
                if (!G_strcasestr(opt_dsn, "port=")) {
                    strcat(connect_str, " port=");
                    strcat(connect_str, port);
                }
                G_free((char *)port);
            }
        }

        if (use_ogr) {
            dsn = G_store(connect_str);
        }
        else {
            /* strip PG: prefix */
            p = (char *)connect_str;
            p += strlen("PG:");
            dsn = G_store(p);
        }
    }
    else {
        /* other datasources */
        dsn = G_store(opt_dsn);
    }

    G_debug(1, "dsn: %s", dsn);

    return dsn;
}