File: form.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (311 lines) | stat: -rw-r--r-- 8,255 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>

#include <tcl.h>
#include <tk.h>

#include <locale.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/form.h>

/* Structure to store column names and values */
typedef struct
{
    char *name;
    int ctype;
    char *value;
} COLUMN;

static char *Drvname, *Dbname, *Tblname, *Key;

static COLUMN *Columns = NULL;
static int allocatedRows = 0;	/* allocated space */
static int nRows = 0;

/* Start new sql update */
int reset_values(ClientData cdata, Tcl_Interp * interp, int argc,
		 char *argv[])
{
    nRows = 0;
    Drvname = NULL;
    Dbname = NULL;
    Tblname = NULL;
    Key = NULL;

    return TCL_OK;
}

int set_value(ClientData cdata, Tcl_Interp * interp, int argc, char *argv[])
{
    G_debug(2, "set_value(): %s %s", argv[1], argv[2]);

    if (strcmp(argv[1], F_DRIVER_FNAME) == 0) {
	Drvname = G_store(argv[2]);
    }
    else if (strcmp(argv[1], F_DATABASE_FNAME) == 0) {
	Dbname = G_store(argv[2]);
    }
    else if (strcmp(argv[1], F_TABLE_FNAME) == 0) {
	Tblname = G_store(argv[2]);
    }
    else if (strcmp(argv[1], F_KEY_FNAME) == 0) {
	Key = G_store(argv[2]);
    }
    else {
	if (nRows == allocatedRows) {
	    allocatedRows += 100;
	    Columns =
		(COLUMN *) G_realloc(Columns,
				     (allocatedRows) * sizeof(COLUMN));
	}
	Columns[nRows].name = G_store(argv[1]);
	Columns[nRows].value = G_store(argv[2]);
	nRows++;
    }

    return TCL_OK;
}

/* Update table, use the data previously stored by set_value() */
int submit(ClientData cdata, Tcl_Interp * interp, int argc, char *argv[])
{
    int i, first, ncols, found, col, sqltype, keyval = 0, ret;
    char buf[2001];
    dbString sql, table_name, strval;
    dbDriver *driver;
    dbHandle handle;
    dbTable *table;
    dbColumn *column;

    G_debug(2, "submit()");

    db_init_string(&sql);
    db_init_string(&table_name);
    db_init_string(&strval);

    /* Check if all internal values are set */
    if (Drvname == NULL || Dbname == NULL || Tblname == NULL || Key == NULL) {
	G_warning("db connection was not set by form");
	sprintf(buf, "set submit_msg \"db connection was not set by form.\"");
	Tcl_Eval(interp, buf);
	Tcl_Eval(interp, "set submit_result 0");
	return TCL_OK;
    }

    /* Get column types */
    G_debug(2, "Open driver");
    driver = db_start_driver(Drvname);
    if (driver == NULL) {
	G_warning("Cannot open driver");
	sprintf(buf, "set submit_msg \"Cannot open driver '%s'\"", Drvname);
	Tcl_Eval(interp, buf);
	Tcl_Eval(interp, "set submit_result 0");
	return TCL_OK;
    }
    G_debug(2, "Driver opened");

    db_init_handle(&handle);
    db_set_handle(&handle, Dbname, NULL);
    G_debug(2, "Open database");
    if (db_open_database(driver, &handle) != DB_OK) {
	G_warning("Cannot open database");
	db_shutdown_driver(driver);
	sprintf(buf,
		"set submit_msg \"Cannot open database '%s' by driver '%s'\"",
		Dbname, Drvname);

	Tcl_Eval(interp, buf);
	Tcl_Eval(interp, "set submit_result 0");
	return TCL_OK;
    }
    G_debug(2, "Database opened");

    db_set_string(&table_name, Tblname);
    if (db_describe_table(driver, &table_name, &table) != DB_OK) {
	G_warning("Cannot describe table");
	db_shutdown_driver(driver);
	db_close_database(driver);
	sprintf(buf, "set submit_msg \"Cannot describe table '%s'\"",
		Tblname);
	Tcl_Eval(interp, buf);
	Tcl_Eval(interp, "set submit_result 0");
	return TCL_OK;
    }
    ncols = db_get_table_number_of_columns(table);

    /* For each column get ctype */
    for (i = 0; i < nRows; i++) {
	found = 0;
	for (col = 0; col < ncols; col++) {
	    /* get keyval */
	    if (G_strcasecmp(Columns[i].name, Key) == 0) {
		keyval = atoi(Columns[i].value);
	    }
	    column = db_get_table_column(table, col);
	    if (G_strcasecmp(db_get_column_name(column), Columns[i].name) ==
		0) {
		sqltype = db_get_column_sqltype(column);
		Columns[i].ctype = db_sqltype_to_Ctype(sqltype);
		found = 1;
		break;
	    }
	}
	if (!found && (G_strcasecmp(Columns[i].name, F_ENCODING) != 0)) {
	    G_warning("Cannot find column type");
	    db_close_database(driver);
	    db_shutdown_driver(driver);
	    sprintf(buf, "set submit_msg \"Cannot find column type\"");
	    Tcl_Eval(interp, buf);
	    Tcl_Eval(interp, "set submit_result 0");
	    return TCL_OK;
	}
    }

    /* Construct update statement */
    sprintf(buf, "update %s set ", Tblname);
    db_set_string(&sql, buf);

    first = 1;
    for (i = 0; i < nRows; i++) {
	G_debug(3, "Index = %d of %d Name = %s, Key = %s", i, nRows,
		Columns[i].name, Key);
	if (G_strcasecmp(Columns[i].name, Key) == 0)
	    continue;

	if (G_strcasecmp(Columns[i].name, F_ENCODING) == 0) {

	    G_debug(3, "GRASS_DB_ENCODING env-var is '%s', col val is '%s'",
		    G__getenv("GRASS_DB_ENCODING"), Columns[i].value);

	    if ((strlen(Columns[i].value) == 0) ||
		G_strcasecmp(Columns[i].value,
			     G__getenv("GRASS_DB_ENCODING")) == 0)
		continue;
	    else {
		G_setenv("GRASS_DB_ENCODING", Columns[i].value);
		G_debug(3, "Set env var GRASS_DB_ENCODING to '%s'",
			Columns[i].value);
		if (Tcl_SetSystemEncoding(interp, Columns[i].value) ==
		    TCL_ERROR) {
		    G_warning
			("Could not set Tcl system encoding to '%s' (%s)",
			 Columns[i].value, Tcl_GetStringResult(interp));
		}
	    }
	    continue;
	}

	if (!first) {
	    db_append_string(&sql, ", ");
	}
	if (strlen(Columns[i].value) == 0) {
	    sprintf(buf, "%s = null", Columns[i].name);
	}
	else {
	    if (Columns[i].ctype == DB_C_TYPE_INT ||
		Columns[i].ctype == DB_C_TYPE_DOUBLE) {
		sprintf(buf, "%s = %s", Columns[i].name, Columns[i].value);
	    }
	    else {
		memset(buf, '\0', strlen(buf));
		ret = Tcl_UtfToExternal(interp,
					Tcl_GetEncoding(interp,
							G__getenv
							("GRASS_DB_ENCODING")),
					Columns[i].value,
					strlen(Columns[i].value), 0, NULL,
					buf, 2000, NULL, NULL, NULL);

		if (ret != TCL_OK) {
		    G_warning("Could not convert UTF to external.");
		    db_set_string(&strval, Columns[i].value);
		}
		else {
		    db_set_string(&strval, buf);
		}

		db_double_quote_string(&strval);
		sprintf(buf, "%s = '%s'", Columns[i].name,
			db_get_string(&strval));
	    }
	}
	db_append_string(&sql, buf);
	first = 0;
    }

    sprintf(buf, " where %s = %d", Key, keyval);
    db_append_string(&sql, buf);

    G_debug(2, "SQL: %s", db_get_string(&sql));

    /* Update table */
    ret = db_execute_immediate(driver, &sql);

    db_close_database(driver);
    db_shutdown_driver(driver);

    if (ret != DB_OK) {
	G_warning("Cannot update table");
	Tcl_VarEval(interp, "set submit_msg \"Cannot update table:\n",
		    db_get_error_msg(), "\"", NULL);
	Tcl_Eval(interp, "set submit_result 0");
    }
    else {
	Tcl_Eval(interp, "set submit_msg \"Record successfully updated\"");
	Tcl_Eval(interp, "set submit_result 1");
    }

    return TCL_OK;
}

/* 
 *  Form 
 */
int Tcl_AppInit(Tcl_Interp * interp)
{
    if (Tcl_Init(interp) == TCL_ERROR)
	return TCL_ERROR;

    if (Tk_Init(interp) == TCL_ERROR)
	return TCL_ERROR;

    Tcl_StaticPackage(interp, "Tk", Tk_Init, Tk_SafeInit);

    /*
     * Call Tcl_CreateCommand for application-specific commands, if
     * they weren't already created by the init procedures called above.
     */

    Tcl_CreateCommand(interp, "submit", (Tcl_CmdProc *) submit,
		      (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    Tcl_CreateCommand(interp, "set_value",
		      (Tcl_CmdProc *) set_value,
		      (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    Tcl_CreateCommand(interp, "reset_values",
		      (Tcl_CmdProc *) reset_values,
		      (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    /*
     * Specify a user-specific startup file to invoke if the application
     * is run interactively.  Typically the startup file is "~/.apprc"
     * where "app" is the name of the application.  If this line is deleted
     * then no user-specific startup file will be run under any conditions.
     */

    Tcl_SetVar(interp, "tcl_rcFileName", "~/.grassformrc", TCL_GLOBAL_ONLY);
    return TCL_OK;
}

int main(int argc, char *argv[])
{
    G_gisinit("form");
    G_debug(2, "Form: main()");

    Tk_Main(argc, argv, Tcl_AppInit);
    return 0;
}