|
GRASS Programmer's Manual
6.5.svn(2012)-r51648
|
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 break; 00057 } 00058 } 00059 00060 db_free_table(table); 00061 00062 return type; 00063 } 00064 00077 int db_column_Ctype(dbDriver * driver, const char *tab, const char *col) 00078 { 00079 int type; 00080 00081 if ((type = db_column_sqltype(driver, tab, col)) >= 0) { 00082 type = db_sqltype_to_Ctype(type); 00083 return type; 00084 } 00085 00086 return -1; 00087 } 00088 00102 int db_get_column(dbDriver * Driver, const char *tname, const char *cname, 00103 dbColumn ** Column) 00104 { 00105 int i, ncols, ret; 00106 dbTable *Table; 00107 dbColumn *Col, *NCol; 00108 dbString tabname; 00109 00110 db_init_string(&tabname); 00111 db_set_string(&tabname, tname); 00112 00113 if (db_describe_table(Driver, &tabname, &Table) != DB_OK) { 00114 G_warning(_("Unable to describe table <%s>"), tname); 00115 return DB_FAILED; 00116 } 00117 00118 *Column = NULL; 00119 ret = DB_FAILED; 00120 00121 ncols = db_get_table_number_of_columns(Table); 00122 G_debug(3, "ncol = %d", ncols); 00123 00124 for (i = 0; i < ncols; i++) { 00125 Col = db_get_table_column(Table, i); 00126 if (G_strcasecmp(db_get_column_name(Col), cname) == 0) { 00127 NCol = (dbColumn *) malloc(sizeof(dbColumn)); 00128 db_init_column(NCol); 00129 db_set_string(&(NCol->columnName), db_get_column_name(Col)); 00130 db_set_string(&(NCol->description), 00131 db_get_column_description(Col)); 00132 NCol->sqlDataType = Col->sqlDataType; 00133 NCol->hostDataType = Col->hostDataType; 00134 db_copy_value(&(NCol->value), &(Col->value)); 00135 NCol->dataLen = Col->dataLen; 00136 NCol->precision = Col->precision; 00137 NCol->scale = Col->scale; 00138 NCol->nullAllowed = Col->nullAllowed; 00139 NCol->hasDefaultValue = Col->hasDefaultValue; 00140 NCol->useDefaultValue = Col->useDefaultValue; 00141 db_copy_value(&(NCol->defaultValue), &(Col->defaultValue)); 00142 NCol->select = Col->select; 00143 NCol->update = Col->update; 00144 00145 *Column = NCol; 00146 ret = DB_OK; 00147 break; 00148 } 00149 } 00150 db_free_table(Table); 00151 00152 return ret; 00153 }