1
#include <grass/glocale.h>
3
#include "local_proto.h"
5
int mk_att(int cat, struct field_info *Fi, dbDriver *driver, int ncol,
6
int *colctype, const char **colname, int doatt, int nocat,
7
OGRFeatureH Ogr_feature, int *noatt)
13
static dbString dbstring;
16
char buf[SQL_BUFFER_SIZE];
20
G_debug(2, "mk_att() cat = %d, doatt = %d", cat, doatt);
24
db_init_string(&dbstring);
31
ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, GV_KEY_COLUMN);
33
OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
34
/* doatt reset moved into have cat loop as the table needs to be
35
open to know the OGR field ID. Hopefully this has no ill consequences */
38
/* Read & set attributes */
39
if (cat >= 0) { /* Line with category */
41
/* Fetch all attribute records for cat <cat> */
42
/* opening and closing the cursor is slow,
43
* but the cursor really needs to be opened for each cat separately */
44
sprintf(buf, "SELECT * FROM %s WHERE %s = %d", Fi->table, Fi->key, cat);
45
G_debug(2, "SQL: %s", buf);
46
db_set_string(&dbstring, buf);
47
if (db_open_select_cursor (driver, &dbstring, &cursor, DB_SEQUENTIAL) != DB_OK) {
48
G_fatal_error(_("Cannot select attributes for cat = %d"),
52
if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
53
G_fatal_error(_("Unable to fetch data from table"));
56
/* G_warning ("No database record for cat = %d", cat); */
57
/* Set at least key column to category */
60
OGR_F_GetFieldIndex(Ogr_feature, Fi->key);
61
OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
65
G_fatal_error(_("No database record for cat = %d and export of 'cat' disabled"),
70
Table = db_get_cursor_table(&cursor);
71
for (j = 0; j < ncol; j++) {
72
Column = db_get_table_column(Table, j);
73
Value = db_get_column_value(Column);
74
db_convert_column_value_to_string(Column, &dbstring); /* for debug only */
75
G_debug(2, "col %d : val = %s", j,
76
db_get_string(&dbstring));
78
G_debug(2, " colctype = %d", colctype[j]);
80
if (nocat && strcmp(Fi->key, colname[j]) == 0)
83
ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature,
85
G_debug(2, " column = %s -> fieldnum = %d",
86
colname[j], ogrfieldnum);
88
if (ogrfieldnum < 0) {
89
G_debug(4, "Could not get OGR field number for column %s",
95
if ((nocat && strcmp(Fi->key, colname[j]) == 0) == 0) {
96
/* if this is 'cat', then execute the following only if the '-s' flag was NOT given*/
97
OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
100
/* prevent writing NULL values */
101
if (!db_test_value_isnull(Value)) {
102
if ((nocat && strcmp(Fi->key, colname[j]) == 0) == 0) {
103
/* if this is 'cat', then execute the following only if the '-s' flag was NOT given*/
105
switch (colctype[j]) {
107
OGR_F_SetFieldInteger(Ogr_feature,
112
case DB_C_TYPE_DOUBLE:
113
OGR_F_SetFieldDouble(Ogr_feature, ogrfieldnum,
117
case DB_C_TYPE_STRING:
118
OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
122
case DB_C_TYPE_DATETIME:
123
db_convert_column_value_to_string(Column,
125
OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
134
db_close_cursor(&cursor);
136
else { /* Use cat only */
137
ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, GV_KEY_COLUMN);
138
OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
141
/* else G_warning ("Line without cat of layer %d", field); */
144
db_free_string(&dbstring);