dbmi_client/column.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include <grass/gis.h>
00004 #include <grass/dbmi.h>
00005 
00012 /* returns column sqltype  or -1 on error*/
00013 int
00014 db_column_sqltype ( 
00015     dbDriver *driver,  
00016     char *tab, /* table name */ 
00017     char *col) /* column  name*/
00018 {
00019     dbTable *table;
00020     dbString table_name;
00021     dbColumn *column;
00022     int ncol, cl, type;
00023 
00024     db_init_string(&table_name);
00025     db_set_string(&table_name, tab);
00026     
00027     if(db_describe_table (driver, &table_name, &table) != DB_OK)
00028        return -1;
00029     
00030     db_free_string ( &table_name );
00031     ncol = db_get_table_number_of_columns(table);
00032     for (cl = 0; cl < ncol; cl++) {
00033         column = db_get_table_column (table, cl);
00034         if ( strcmp (  db_get_column_name(column), col ) == 0 ) {
00035             type = db_get_column_sqltype(column);
00036             return type;
00037         }
00038     }
00039     
00040     return -1;
00041 }
00042 
00049 /* returns column Ctype  or -1 on error */
00050 int
00051 db_column_Ctype ( 
00052     dbDriver *driver,  
00053     char *tab, /* table name */ 
00054     char *col) /* column  name*/
00055 {
00056     int type;
00057     if ( ( type = db_column_sqltype ( driver, tab, col ) ) >= 0 ) {
00058         type = db_sqltype_to_Ctype(type); 
00059         return type;
00060     }
00061 
00062     return -1;
00063 }
00064 
00073 int
00074 db_get_column ( dbDriver *Driver, char *tname, char *cname, dbColumn **Column )
00075 {
00076     int   i, ncols;
00077     dbTable *Table;
00078     dbColumn *Col, *NCol;
00079     dbString tabname;
00080 
00081     db_init_string(&tabname);
00082     db_set_string(&tabname, tname);
00083 
00084     if(db_describe_table (Driver, &tabname, &Table) != DB_OK) {
00085          G_warning("Cannot describe table %s", tname);
00086          return DB_FAILED;
00087     }
00088 
00089     *Column = NULL;
00090 
00091     ncols = db_get_table_number_of_columns(Table);
00092     G_debug (3, "ncol = %d", ncols );
00093              
00094     for (i = 0; i < ncols; i++) {
00095         Col = db_get_table_column (Table, i);
00096         if ( G_strcasecmp ( db_get_column_name(Col), cname ) == 0 ) {
00097             NCol = (dbColumn *) malloc ( sizeof ( dbColumn ) );
00098             db_init_column ( NCol );
00099             db_set_string ( &(NCol->columnName), db_get_column_name(Col) );
00100             db_set_string ( &(NCol->description), db_get_column_description(Col) );
00101             NCol->sqlDataType = Col->sqlDataType;
00102             NCol->hostDataType = Col->hostDataType;
00103             db_copy_value ( &(NCol->value), &(Col->value) );
00104             NCol->dataLen = Col->dataLen;
00105             NCol->precision = Col->precision;
00106             NCol->scale = Col->scale;
00107             NCol->nullAllowed = Col->nullAllowed;
00108             NCol->hasDefaultValue = Col->hasDefaultValue;
00109             NCol->useDefaultValue = Col->useDefaultValue;
00110             db_copy_value ( &(NCol->defaultValue), &(Col->defaultValue) );
00111             NCol->select = Col->select;
00112             NCol->update = Col->update;
00113 
00114             *Column = NCol;
00115             return DB_OK;
00116         }
00117     }
00118     return DB_OK;
00119 }
00120 

Generated on Sun Apr 6 17:31:38 2008 for GRASS by  doxygen 1.5.5