GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dbmi_client/column.c
Go to the documentation of this file.
1 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <grass/gis.h>
20 #include <grass/dbmi.h>
21 #include <grass/glocale.h>
22 
35 int db_column_sqltype(dbDriver * driver, const char *tab, const char *col)
36 {
37  dbTable *table;
38  dbString table_name;
39  dbColumn *column;
40  int ncol, cl, type;
41 
42  type = -1;
43 
44  db_init_string(&table_name);
45  db_set_string(&table_name, tab);
46 
47  if (db_describe_table(driver, &table_name, &table) != DB_OK)
48  return -1;
49 
50  db_free_string(&table_name);
51  ncol = db_get_table_number_of_columns(table);
52  for (cl = 0; cl < ncol; cl++) {
53  column = db_get_table_column(table, cl);
54  if (strcmp(db_get_column_name(column), col) == 0) {
55  type = db_get_column_sqltype(column);
56  type = db_get_column_sqltype(column);
57  break;
58  }
59  }
60 
61  db_free_table(table);
62 
63  return type;
64 }
65 
78 int db_column_Ctype(dbDriver * driver, const char *tab, const char *col)
79 {
80  int type;
81 
82  if ((type = db_column_sqltype(driver, tab, col)) >= 0) {
83  type = db_sqltype_to_Ctype(type);
84  return type;
85  }
86 
87  return -1;
88 }
89 
103 int db_get_column(dbDriver * Driver, const char *tname, const char *cname,
104  dbColumn ** Column)
105 {
106  int i, ncols, ret;
107  dbTable *Table;
108  dbColumn *Col, *NCol;
109  dbString tabname;
110 
111  db_init_string(&tabname);
112  db_set_string(&tabname, tname);
113 
114  if (db_describe_table(Driver, &tabname, &Table) != DB_OK) {
115  G_warning(_("Unable to describe table <%s>"), tname);
116  return DB_FAILED;
117  }
118 
119  *Column = NULL;
120  ret = DB_FAILED;
121 
122  ncols = db_get_table_number_of_columns(Table);
123  G_debug(3, "ncol = %d", ncols);
124 
125  for (i = 0; i < ncols; i++) {
126  Col = db_get_table_column(Table, i);
127  if (G_strcasecmp(db_get_column_name(Col), cname) == 0) {
128  NCol = (dbColumn *) malloc(sizeof(dbColumn));
129  db_init_column(NCol);
130  db_set_string(&(NCol->columnName), db_get_column_name(Col));
131  db_set_string(&(NCol->description),
133  NCol->sqlDataType = Col->sqlDataType;
134  NCol->hostDataType = Col->hostDataType;
135  db_copy_value(&(NCol->value), &(Col->value));
136  NCol->dataLen = Col->dataLen;
137  NCol->precision = Col->precision;
138  NCol->scale = Col->scale;
139  NCol->nullAllowed = Col->nullAllowed;
140  NCol->hasDefaultValue = Col->hasDefaultValue;
141  NCol->useDefaultValue = Col->useDefaultValue;
142  db_copy_value(&(NCol->defaultValue), &(Col->defaultValue));
143  NCol->select = Col->select;
144  NCol->update = Col->update;
145 
146  *Column = NCol;
147  ret = DB_OK;
148  break;
149  }
150  }
151  db_free_table(Table);
152 
153  return ret;
154 }