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

« back to all changes in this revision

Viewing changes to db/db.copy/main.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.copy
 
5
 * AUTHOR(S):    Radim Blazek <radim.blazek gmail.com> (original contributor)
 
6
 *               Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
 
7
 * PURPOSE:      copy a table
 
8
 * COPYRIGHT:    (C) 2003-2006 by the GRASS Development Team
 
9
 *
 
10
 *               This program is free software under the GNU General Public
 
11
 *               License (>=v2). Read the file COPYING that comes with GRASS
 
12
 *               for details.
 
13
 *
 
14
 *****************************************************************************/
 
15
 
 
16
#include <stdlib.h>
 
17
#include <grass/gis.h>
 
18
#include <grass/dbmi.h>
 
19
#include <grass/glocale.h>
 
20
 
 
21
 
 
22
int main(int argc, char **argv)
 
23
{
 
24
    int ret;
 
25
    struct Option *from_driver, *from_database, *from_table;
 
26
    struct Option *to_driver, *to_database, *to_table;
 
27
    struct Option *where, *select;
 
28
    struct GModule *module;
 
29
    const char *drv, *db;
 
30
 
 
31
    G_gisinit(argv[0]);
 
32
    
 
33
    /* Set description */
 
34
    module = G_define_module();
 
35
    G_add_keyword(_("database"));
 
36
    G_add_keyword(_("attribute table"));
 
37
    G_add_keyword(_("SQL"));
 
38
    module->label = _("Copy a table.");
 
39
    module->description =
 
40
        _("Either 'from_table' (optionally with 'where') can be used "
 
41
          "or 'select' option, but not 'from_table' and 'select' at the same time.");
 
42
    
 
43
    from_driver = G_define_standard_option(G_OPT_DB_DRIVER);
 
44
    from_driver->key = "from_driver";
 
45
    from_driver->options = db_list_drivers();
 
46
    from_driver->description = _("Input driver name");
 
47
    if ((drv = db_get_default_driver_name()))
 
48
      from_driver->answer = (char *) drv;
 
49
 
 
50
    from_database = G_define_standard_option(G_OPT_DB_DATABASE);
 
51
    from_database->key = "from_database";
 
52
    from_database->description = _("Input database name");
 
53
    if ((db = db_get_default_database_name()))
 
54
      from_database->answer = (char *) db;
 
55
 
 
56
    from_table = G_define_standard_option(G_OPT_DB_TABLE);
 
57
    from_table->key = "from_table";
 
58
    from_table->description =
 
59
        _("Input table name (only, if 'select' is not used)");
 
60
 
 
61
    to_driver = G_define_standard_option(G_OPT_DB_DRIVER);
 
62
    to_driver->key = "to_driver";
 
63
    to_driver->options = db_list_drivers();
 
64
    to_driver->required = NO;
 
65
    to_driver->description = _("Output driver name");
 
66
    if ((drv = db_get_default_driver_name()))
 
67
      to_driver->answer = (char *) drv;
 
68
 
 
69
    to_database = G_define_standard_option(G_OPT_DB_DATABASE);
 
70
    to_database->key = "to_database";
 
71
    to_database->description = _("Output database name");
 
72
    if ((db = db_get_default_database_name()))
 
73
      to_database->answer = (char *) db;
 
74
 
 
75
    to_table = G_define_standard_option(G_OPT_DB_TABLE);
 
76
    to_table->key = "to_table";
 
77
    to_table->required = YES;
 
78
    to_table->description = _("Output table name");
 
79
    to_table->gisprompt = "new,dbtable,dbtable";
 
80
 
 
81
    where = G_define_standard_option(G_OPT_DB_WHERE);
 
82
 
 
83
    select = G_define_option();
 
84
    select->key = "select";
 
85
    select->type = TYPE_STRING;
 
86
    select->required = NO;
 
87
    select->label = _("Full select statement (only, if 'from_table' and 'where' is not used)");
 
88
    select->description = _("E.g.: SELECT dedek FROM starobince WHERE obec = 'Frimburg'");
 
89
    
 
90
    if (G_parser(argc, argv))
 
91
        exit(EXIT_FAILURE);
 
92
 
 
93
    /* Check options and copy tables */
 
94
    if (from_table->answer) {
 
95
        if (select->answer)
 
96
            G_fatal_error(_("Cannot combine 'from_table' and 'select' options"));
 
97
 
 
98
        if (!db_table_exists(from_driver->answer, from_database->answer,
 
99
                             from_table->answer)) {
 
100
            G_warning(_("Table <%s> not found in database <%s> using driver <%s>"),
 
101
                       from_table->answer, from_database->answer,
 
102
                       from_driver->answer);
 
103
            exit(EXIT_FAILURE);
 
104
        }
 
105
 
 
106
        if (where->answer) {
 
107
            ret =
 
108
                db_copy_table_where(from_driver->answer,
 
109
                                    from_database->answer, from_table->answer,
 
110
                                    to_driver->answer, to_database->answer,
 
111
                                    to_table->answer, where->answer);
 
112
        }
 
113
        else {
 
114
            ret =
 
115
                db_copy_table(from_driver->answer, from_database->answer,
 
116
                              from_table->answer, to_driver->answer,
 
117
                              to_database->answer, to_table->answer);
 
118
        }
 
119
    }
 
120
    else {
 
121
        if (!select->answer)
 
122
            G_fatal_error(_("Either 'from_table' or 'select' option must be given."));
 
123
 
 
124
        if (where->answer)
 
125
            G_fatal_error(_("Cannot combine 'select' and 'where' options"));
 
126
 
 
127
        ret =
 
128
            db_copy_table_select(from_driver->answer, from_database->answer,
 
129
                                 from_table->answer, to_driver->answer,
 
130
                                 to_database->answer, to_table->answer,
 
131
                                 select->answer);
 
132
    }
 
133
 
 
134
    if (ret == DB_FAILED) {
 
135
        G_warning(_("Copy table failed"));
 
136
        exit(EXIT_FAILURE);
 
137
    }
 
138
 
 
139
    exit(EXIT_SUCCESS);
 
140
}