GRASS Programmer's Manual
6.4.2(2012)
|
00001 00017 #include <stdlib.h> 00018 #include <string.h> 00019 #include <grass/gis.h> 00020 #include <grass/dbmi.h> 00021 #include <grass/glocale.h> 00022 00035 int db_column_sqltype(dbDriver * driver, const char *tab, const char *col) 00036 { 00037 dbTable *table; 00038 dbString table_name; 00039 dbColumn *column; 00040 int ncol, cl, type; 00041 00042 type = -1; 00043 00044 db_init_string(&table_name); 00045 db_set_string(&table_name, tab); 00046 00047 if (db_describe_table(driver, &table_name, &table) != DB_OK) 00048 return -1; 00049 00050 db_free_string(&table_name); 00051 ncol = db_get_table_number_of_columns(table); 00052 for (cl = 0; cl < ncol; cl++) { 00053 column = db_get_table_column(table, cl); 00054 if (strcmp(db_get_column_name(column), col) == 0) { 00055 type = db_get_column_sqltype(column); 00056 type = db_get_column_sqltype(column); 00057 break; 00058 } 00059 } 00060 00061 db_free_table(table); 00062 00063 return type; 00064 } 00065 00078 int db_column_Ctype(dbDriver * driver, const char *tab, const char *col) 00079 { 00080 int type; 00081 00082 if ((type = db_column_sqltype(driver, tab, col)) >= 0) { 00083 type = db_sqltype_to_Ctype(type); 00084 return type; 00085 } 00086 00087 return -1; 00088 } 00089 00103 int db_get_column(dbDriver * Driver, const char *tname, const char *cname, 00104 dbColumn ** Column) 00105 { 00106 int i, ncols, ret; 00107 dbTable *Table; 00108 dbColumn *Col, *NCol; 00109 dbString tabname; 00110 00111 db_init_string(&tabname); 00112 db_set_string(&tabname, tname); 00113 00114 if (db_describe_table(Driver, &tabname, &Table) != DB_OK) { 00115 G_warning(_("Unable to describe table <%s>"), tname); 00116 return DB_FAILED; 00117 } 00118 00119 *Column = NULL; 00120 ret = DB_FAILED; 00121 00122 ncols = db_get_table_number_of_columns(Table); 00123 G_debug(3, "ncol = %d", ncols); 00124 00125 for (i = 0; i < ncols; i++) { 00126 Col = db_get_table_column(Table, i); 00127 if (G_strcasecmp(db_get_column_name(Col), cname) == 0) { 00128 NCol = (dbColumn *) malloc(sizeof(dbColumn)); 00129 db_init_column(NCol); 00130 db_set_string(&(NCol->columnName), db_get_column_name(Col)); 00131 db_set_string(&(NCol->description), 00132 db_get_column_description(Col)); 00133 NCol->sqlDataType = Col->sqlDataType; 00134 NCol->hostDataType = Col->hostDataType; 00135 db_copy_value(&(NCol->value), &(Col->value)); 00136 NCol->dataLen = Col->dataLen; 00137 NCol->precision = Col->precision; 00138 NCol->scale = Col->scale; 00139 NCol->nullAllowed = Col->nullAllowed; 00140 NCol->hasDefaultValue = Col->hasDefaultValue; 00141 NCol->useDefaultValue = Col->useDefaultValue; 00142 db_copy_value(&(NCol->defaultValue), &(Col->defaultValue)); 00143 NCol->select = Col->select; 00144 NCol->update = Col->update; 00145 00146 *Column = NCol; 00147 ret = DB_OK; 00148 break; 00149 } 00150 } 00151 db_free_table(Table); 00152 00153 return ret; 00154 }