-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathmain.c
225 lines (186 loc) · 6.31 KB
/
main.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/****************************************************************************
*
* MODULE: db.execute
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
* Huidae Cho <grass4u gmail.com>
* Glynn Clements <glynn gclements.plus.com>
* Hamish Bowman <hamish_b yahoo.com>
* Markus Neteler <neteler itc.it>
* Stephan Holl
* Martin Landa <landa.martin gmail.com>
* PURPOSE: process one non-select sql statement
* COPYRIGHT: (C) 2002-2011 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 for details.
*
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
struct {
const char *driver, *database, *schema, *sql, *input;
int i;
} parms;
/* function prototypes */
static void parse_command_line(int, char **);
static int get_stmt(FILE *, dbString *);
static int stmt_is_empty(dbString *);
static void error_handler(void *);
int main(int argc, char **argv)
{
dbString stmt;
dbDriver *driver;
dbHandle handle;
int ret;
FILE *fd;
int error;
error = 0;
parse_command_line(argc, argv);
/* read from file or stdin ? */
if (parms.input && strcmp(parms.input, "-") != 0) {
fd = fopen(parms.input, "r");
if (fd == NULL) {
G_fatal_error(_("Unable to open file <%s>: %s"), parms.input,
strerror(errno));
}
}
else {
fd = stdin;
}
/* open DB connection */
db_init_string(&stmt);
driver = db_start_driver(parms.driver);
if (driver == NULL) {
G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
}
db_init_handle(&handle);
db_set_handle(&handle, parms.database, parms.schema);
if (db_open_database(driver, &handle) != DB_OK)
G_fatal_error(_("Unable to open database <%s>"), parms.database);
G_add_error_handler(error_handler, driver);
if (parms.sql) {
/* parms.sql */
db_set_string(&stmt, parms.sql);
ret = db_execute_immediate(driver, &stmt);
if (ret != DB_OK) {
if (parms.i) { /* ignore SQL errors */
G_warning(_("Error while executing: '%s'"),
db_get_string(&stmt));
error++;
}
else {
G_fatal_error(_("Error while executing: '%s'"),
db_get_string(&stmt));
}
}
}
else { /* parms.input */
while (get_stmt(fd, &stmt)) {
if (stmt_is_empty(&stmt))
continue;
G_debug(3, "sql: %s", db_get_string(&stmt));
ret = db_execute_immediate(driver, &stmt);
if (ret != DB_OK) {
if (parms.i) { /* ignore SQL errors */
G_warning(_("Error while executing: '%s'"),
db_get_string(&stmt));
error++;
}
else {
G_fatal_error(_("Error while executing: '%s'"),
db_get_string(&stmt));
}
}
}
}
db_close_database(driver);
db_shutdown_driver(driver);
exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
}
static void parse_command_line(int argc, char **argv)
{
struct Option *driver, *database, *schema, *sql, *input;
struct Flag *i;
struct GModule *module;
const char *drv, *db, *schema_name;
/* Initialize the GIS calls */
G_gisinit(argv[0]);
/* Set description */
module = G_define_module();
G_add_keyword(_("database"));
G_add_keyword(_("attribute table"));
G_add_keyword(_("SQL"));
module->label = _("Executes any SQL statement.");
module->description = _("For SELECT statements use 'db.select'.");
sql = G_define_standard_option(G_OPT_DB_SQL);
sql->label = _("SQL statement");
sql->description =
_("Example: update rybniky set kapri = 'hodne' where kapri = 'malo'");
sql->guisection = _("SQL");
input = G_define_standard_option(G_OPT_F_INPUT);
input->required = NO;
input->label = _("Name of file containing SQL statement(s)");
input->description = _("'-' for standard input");
input->guisection = _("SQL");
driver = G_define_standard_option(G_OPT_DB_DRIVER);
driver->options = db_list_drivers();
driver->guisection = _("Connection");
if ((drv = db_get_default_driver_name()))
driver->answer = (char *)drv;
database = G_define_standard_option(G_OPT_DB_DATABASE);
database->guisection = _("Connection");
if ((db = db_get_default_database_name()))
database->answer = (char *)db;
schema = G_define_standard_option(G_OPT_DB_SCHEMA);
schema->guisection = _("Connection");
if ((schema_name = db_get_default_schema_name()))
schema->answer = (char *)schema_name;
i = G_define_flag();
i->key = 'i';
i->description = _("Ignore SQL errors and continue");
i->guisection = _("Errors");
if (G_parser(argc, argv))
exit(EXIT_SUCCESS);
if (!sql->answer && !input->answer) {
G_fatal_error(_("You must provide <%s> or <%s> option"), sql->key,
input->key);
}
parms.driver = driver->answer;
parms.database = database->answer;
parms.schema = schema->answer;
parms.sql = sql->answer;
parms.input = input->answer;
parms.i = i->answer ? TRUE : FALSE;
}
int get_stmt(FILE *fd, dbString *stmt)
{
char buf[DB_SQL_MAX], buf2[DB_SQL_MAX];
size_t len;
db_zero_string(stmt);
if (G_getl2(buf, sizeof(buf), fd) == 0)
return 0;
strcpy(buf2, buf);
G_chop(buf2);
len = strlen(buf2);
if (buf2[len - 1] == ';') { /* end of statement */
buf2[len - 1] = 0; /* truncate ';' */
}
db_set_string(stmt, buf2);
return 1;
}
int stmt_is_empty(dbString *stmt)
{
char dummy[2];
return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
}
void error_handler(void *p)
{
dbDriver *driver = (dbDriver *)p;
db_close_database(driver);
db_shutdown_driver(driver);
}