GRASS Programmer's Manual  6.5.svn(2014)-r66266
 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  break;
57  }
58  }
59 
60  db_free_table(table);
61 
62  return type;
63 }
64 
77 int db_column_Ctype(dbDriver * driver, const char *tab, const char *col)
78 {
79  int type;
80 
81  if ((type = db_column_sqltype(driver, tab, col)) >= 0) {
82  type = db_sqltype_to_Ctype(type);
83  return type;
84  }
85 
86  return -1;
87 }
88 
102 int db_get_column(dbDriver * Driver, const char *tname, const char *cname,
103  dbColumn ** Column)
104 {
105  int i, ncols, ret;
106  dbTable *Table;
107  dbColumn *Col, *NCol;
108  dbString tabname;
109 
110  db_init_string(&tabname);
111  db_set_string(&tabname, tname);
112 
113  if (db_describe_table(Driver, &tabname, &Table) != DB_OK) {
114  G_warning(_("Unable to describe table <%s>"), tname);
115  return DB_FAILED;
116  }
117 
118  *Column = NULL;
119  ret = DB_FAILED;
120 
121  ncols = db_get_table_number_of_columns(Table);
122  G_debug(3, "ncol = %d", ncols);
123 
124  for (i = 0; i < ncols; i++) {
125  Col = db_get_table_column(Table, i);
126  if (G_strcasecmp(db_get_column_name(Col), cname) == 0) {
127  NCol = (dbColumn *) malloc(sizeof(dbColumn));
128  db_init_column(NCol);
129  db_set_string(&(NCol->columnName), db_get_column_name(Col));
130  db_set_string(&(NCol->description),
132  NCol->sqlDataType = Col->sqlDataType;
133  NCol->hostDataType = Col->hostDataType;
134  db_copy_value(&(NCol->value), &(Col->value));
135  NCol->dataLen = Col->dataLen;
136  NCol->precision = Col->precision;
137  NCol->scale = Col->scale;
138  NCol->nullAllowed = Col->nullAllowed;
139  NCol->hasDefaultValue = Col->hasDefaultValue;
140  NCol->useDefaultValue = Col->useDefaultValue;
141  db_copy_value(&(NCol->defaultValue), &(Col->defaultValue));
142  NCol->select = Col->select;
143  NCol->update = Col->update;
144 
145  *Column = NCol;
146  ret = DB_OK;
147  break;
148  }
149  }
150  db_free_table(Table);
151 
152  return ret;
153 }
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)
Definition: strings.c:192
const char * db_get_column_description(dbColumn *column)
returns column description for given column
int db_column_Ctype(dbDriver *driver, const char *tab, const char *col)
Get column ctype.
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.
Definition: c_desc_table.c:28
void db_init_column(dbColumn *column)
int db_column_sqltype(dbDriver *driver, const char *tab, const char *col)
Get column sqltype.
int db_sqltype_to_Ctype(int sqltype)
Definition: sqlCtype.c:9
int db_get_column_sqltype(dbColumn *column)
returns column sqltype for column (the function db_sqltype_name() returns sqltype description) ...
void * malloc(YYSIZE_T)
int db_get_table_number_of_columns(dbTable *table)
int db_get_column(dbDriver *Driver, const char *tname, const char *cname, dbColumn **Column)
Get column structure by table and column name.
return NULL
Definition: dbfopen.c:1394
Definition: driver.h:25
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
void db_free_table(dbTable *table)
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
void db_free_string(dbString *x)
Definition: string.c:142
void db_init_string(dbString *x)
Definition: string.c:11
void db_copy_value(dbValue *dst, dbValue *src)
Definition: value.c:313