13 #include <grass/gis.h>
14 #include <grass/dbmi.h>
15 #include <grass/form.h>
25 static char *Drvname, *Dbname, *Tblname, *Key;
28 static int allocatedRows = 0;
44 int set_value(ClientData cdata, Tcl_Interp * interp,
int argc,
char *argv[])
46 G_debug(2,
"set_value(): %s %s", argv[1], argv[2]);
48 if (strcmp(argv[1], F_DRIVER_FNAME) == 0) {
51 else if (strcmp(argv[1], F_DATABASE_FNAME) == 0) {
54 else if (strcmp(argv[1], F_TABLE_FNAME) == 0) {
57 else if (strcmp(argv[1], F_KEY_FNAME) == 0) {
61 if (nRows == allocatedRows) {
64 (
COLUMN *) G_realloc(Columns,
65 (allocatedRows) *
sizeof(
COLUMN));
76 int submit(ClientData cdata, Tcl_Interp * interp,
int argc,
char *argv[])
78 int i,
first, ncols, found, col, sqltype, keyval = 0, ret;
80 dbString sql, table_name, strval;
94 G_warning(
"db connection was not set by form");
95 sprintf(buf,
"set submit_msg \"db connection was not set by form.\"");
96 Tcl_Eval(interp, buf);
97 Tcl_Eval(interp,
"set submit_result 0");
104 if (driver ==
NULL) {
106 sprintf(buf,
"set submit_msg \"Cannot open driver '%s'\"", Drvname);
107 Tcl_Eval(interp, buf);
108 Tcl_Eval(interp,
"set submit_result 0");
120 "set submit_msg \"Cannot open database '%s' by driver '%s'\"",
123 Tcl_Eval(interp, buf);
124 Tcl_Eval(interp,
"set submit_result 0");
134 sprintf(buf,
"set submit_msg \"Cannot describe table '%s'\"",
136 Tcl_Eval(interp, buf);
137 Tcl_Eval(interp,
"set submit_result 0");
143 for (i = 0; i < nRows; i++) {
145 for (col = 0; col < ncols; col++) {
148 keyval = atoi(Columns[i].
value);
163 sprintf(buf,
"set submit_msg \"Cannot find column type\"");
164 Tcl_Eval(interp, buf);
165 Tcl_Eval(interp,
"set submit_result 0");
171 sprintf(buf,
"update %s set ", Tblname);
175 for (i = 0; i < nRows; i++) {
176 G_debug(3,
"Index = %d of %d Name = %s, Key = %s", i, nRows,
177 Columns[i].
name, Key);
183 G_debug(3,
"GRASS_DB_ENCODING env-var is '%s', col val is '%s'",
186 if ((strlen(Columns[i].value) == 0) ||
191 G_setenv(
"GRASS_DB_ENCODING", Columns[i].value);
192 G_debug(3,
"Set env var GRASS_DB_ENCODING to '%s'",
194 if (Tcl_SetSystemEncoding(interp, Columns[i].value) ==
197 (
"Could not set Tcl system encoding to '%s' (%s)",
198 Columns[i].value, Tcl_GetStringResult(interp));
207 if (strlen(Columns[i].
value) == 0) {
208 sprintf(buf,
"%s = null", Columns[i].name);
211 if (Columns[i].ctype == DB_C_TYPE_INT ||
212 Columns[i].ctype == DB_C_TYPE_DOUBLE) {
213 sprintf(buf,
"%s = %s", Columns[i].name, Columns[i].value);
216 memset(buf,
'\0', strlen(buf));
217 ret = Tcl_UtfToExternal(interp,
218 Tcl_GetEncoding(interp,
220 (
"GRASS_DB_ENCODING")),
222 strlen(Columns[i].value), 0,
NULL,
226 G_warning(
"Could not convert UTF to external.");
234 sprintf(buf,
"%s = '%s'", Columns[i].name,
242 sprintf(buf,
" where %s = %d", Key, keyval);
255 Tcl_VarEval(interp,
"set submit_msg \"Cannot update table:\n",
257 Tcl_Eval(interp,
"set submit_result 0");
260 Tcl_Eval(interp,
"set submit_msg \"Record successfully updated\"");
261 Tcl_Eval(interp,
"set submit_result 1");
272 if (Tcl_Init(interp) == TCL_ERROR)
275 if (Tk_Init(interp) == TCL_ERROR)
278 Tcl_StaticPackage(interp,
"Tk", Tk_Init, Tk_SafeInit);
285 Tcl_CreateCommand(interp,
"submit", (Tcl_CmdProc *)
submit,
286 (ClientData)
NULL, (Tcl_CmdDeleteProc *) NULL);
287 Tcl_CreateCommand(interp,
"set_value",
289 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
290 Tcl_CreateCommand(interp,
"reset_values",
292 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
300 Tcl_SetVar(interp,
"tcl_rcFileName",
"~/.grassformrc", TCL_GLOBAL_ONLY);
304 int main(
int argc,
char *argv[])
const char * db_get_error_msg(void)
dbColumn * db_get_table_column(dbTable *table, int n)
returns column structure for given table and column number
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
char * G__getenv(const char *name)
Get environment variable.
char * G_store(const char *s)
Copy string to allocated memory.
const char * db_get_column_name(dbColumn *column)
returns column name for given column
int db_describe_table(dbDriver *driver, dbString *name, dbTable **table)
Describe table.
int db_shutdown_driver(dbDriver *driver)
Closedown the driver, and free the driver structure.
int db_close_database(dbDriver *driver)
Close database connection.
int db_execute_immediate(dbDriver *driver, dbString *SQLstatement)
Execute SQL statements.
int db_append_string(dbString *x, const char *s)
int db_sqltype_to_Ctype(int sqltype)
int db_get_column_sqltype(dbColumn *column)
returns column sqltype for column (the function db_sqltype_name() returns sqltype description) ...
int db_get_table_number_of_columns(dbTable *table)
int main(int argc, char *argv[])
void db_double_quote_string(dbString *src)
int db_set_handle(dbHandle *handle, const char *dbName, const char *dbSchema)
int G_setenv(const char *name, const char *value)
Set environment variable.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
char * db_get_string(dbString *x)
int G_debug(int level, const char *msg,...)
Print debugging message.
void db_init_handle(dbHandle *handle)
int db_set_string(dbString *x, const char *s)
int db_open_database(dbDriver *driver, dbHandle *handle)
Open database connection.
dbDriver * db_start_driver(const char *name)
Initialize a new dbDriver for db transaction.
void db_init_string(dbString *x)