GRASS Programmer's Manual
6.4.1(2011)
|
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 db_init_string(&table_name); 00043 db_set_string(&table_name, tab); 00044 00045 if (db_describe_table(driver, &table_name, &table) != DB_OK) 00046 return -1; 00047 00048 db_free_string(&table_name); 00049 ncol = db_get_table_number_of_columns(table); 00050 for (cl = 0; cl < ncol; cl++) { 00051 column = db_get_table_column(table, cl); 00052 if (strcmp(db_get_column_name(column), col) == 0) { 00053 type = db_get_column_sqltype(column); 00054 return type; 00055 } 00056 } 00057 00058 return -1; 00059 } 00060 00073 int db_column_Ctype(dbDriver * driver, const char *tab, const char *col) 00074 { 00075 int type; 00076 00077 if ((type = db_column_sqltype(driver, tab, col)) >= 0) { 00078 type = db_sqltype_to_Ctype(type); 00079 return type; 00080 } 00081 00082 return -1; 00083 } 00084 00098 int db_get_column(dbDriver * Driver, const char *tname, const char *cname, 00099 dbColumn ** Column) 00100 { 00101 int i, ncols; 00102 dbTable *Table; 00103 dbColumn *Col, *NCol; 00104 dbString tabname; 00105 00106 db_init_string(&tabname); 00107 db_set_string(&tabname, tname); 00108 00109 if (db_describe_table(Driver, &tabname, &Table) != DB_OK) { 00110 G_warning(_("Unable to describe table <%s>"), tname); 00111 return DB_FAILED; 00112 } 00113 00114 *Column = NULL; 00115 00116 ncols = db_get_table_number_of_columns(Table); 00117 G_debug(3, "ncol = %d", ncols); 00118 00119 for (i = 0; i < ncols; i++) { 00120 Col = db_get_table_column(Table, i); 00121 if (G_strcasecmp(db_get_column_name(Col), cname) == 0) { 00122 NCol = (dbColumn *) malloc(sizeof(dbColumn)); 00123 db_init_column(NCol); 00124 db_set_string(&(NCol->columnName), db_get_column_name(Col)); 00125 db_set_string(&(NCol->description), 00126 db_get_column_description(Col)); 00127 NCol->sqlDataType = Col->sqlDataType; 00128 NCol->hostDataType = Col->hostDataType; 00129 db_copy_value(&(NCol->value), &(Col->value)); 00130 NCol->dataLen = Col->dataLen; 00131 NCol->precision = Col->precision; 00132 NCol->scale = Col->scale; 00133 NCol->nullAllowed = Col->nullAllowed; 00134 NCol->hasDefaultValue = Col->hasDefaultValue; 00135 NCol->useDefaultValue = Col->useDefaultValue; 00136 db_copy_value(&(NCol->defaultValue), &(Col->defaultValue)); 00137 NCol->select = Col->select; 00138 NCol->update = Col->update; 00139 00140 *Column = NCol; 00141 return DB_OK; 00142 } 00143 } 00144 return DB_OK; 00145 }