GRASS Programmer's Manual  6.4.2(2012)
dbcolumns.c
Go to the documentation of this file.
00001 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <grass/glocale.h>
00027 #include <grass/gis.h>
00028 #include <grass/Vect.h>
00029 #include <grass/dbmi.h>
00030 
00040 const char *Vect_get_column_names(struct Map_info *Map, int field)
00041 {
00042     int num_dblinks, ncols, col;
00043     struct field_info *fi;
00044     dbDriver *driver = NULL;
00045     dbHandle handle;
00046     dbString table_name;
00047     dbTable *table;
00048     char buf[2000], temp_buf[2000];
00049 
00050 
00051     num_dblinks = Vect_get_num_dblinks(Map);
00052     if (num_dblinks <= 0)
00053         return (NULL);
00054 
00055     G_debug(3,
00056             "Displaying column names for database connection of layer %d:",
00057             field);
00058     if ((fi = Vect_get_field(Map, field)) == NULL)
00059         return (NULL);
00060     driver = db_start_driver(fi->driver);
00061     if (driver == NULL)
00062         return (NULL);
00063     db_init_handle(&handle);
00064     db_set_handle(&handle, fi->database, NULL);
00065     if (db_open_database(driver, &handle) != DB_OK)
00066         return (NULL);
00067     db_init_string(&table_name);
00068     db_set_string(&table_name, fi->table);
00069     if (db_describe_table(driver, &table_name, &table) != DB_OK)
00070         return (NULL);
00071 
00072     ncols = db_get_table_number_of_columns(table);
00073     sprintf(buf, " ");
00074     for (col = 0; col < ncols; col++) {
00075         if (col == 0)
00076             sprintf(buf, "%s",
00077                     db_get_column_name(db_get_table_column(table, col)));
00078         else {
00079             sprintf(temp_buf, ",%s",
00080                     db_get_column_name(db_get_table_column(table, col)));
00081             strcat(buf, temp_buf);
00082         }
00083     }
00084     G_debug(3, "%s", buf);
00085 
00086     db_close_database(driver);
00087     db_shutdown_driver(driver);
00088 
00089     return G_store(G_chop(buf));
00090 }
00091 
00101 const char *Vect_get_column_types(struct Map_info *Map, int field)
00102 {
00103     int num_dblinks, ncols, col;
00104     struct field_info *fi;
00105     dbDriver *driver = NULL;
00106     dbHandle handle;
00107     dbString table_name;
00108     dbTable *table;
00109     char buf[2000], temp_buf[2000];
00110 
00111 
00112     num_dblinks = Vect_get_num_dblinks(Map);
00113     if (num_dblinks <= 0)
00114         return (NULL);
00115 
00116     G_debug(3,
00117             "Displaying column types for database connection of layer %d:",
00118             field);
00119     if ((fi = Vect_get_field(Map, field)) == NULL)
00120         return (NULL);
00121     driver = db_start_driver(fi->driver);
00122     if (driver == NULL)
00123         return (NULL);
00124     db_init_handle(&handle);
00125     db_set_handle(&handle, fi->database, NULL);
00126     if (db_open_database(driver, &handle) != DB_OK)
00127         return (NULL);
00128     db_init_string(&table_name);
00129     db_set_string(&table_name, fi->table);
00130     if (db_describe_table(driver, &table_name, &table) != DB_OK)
00131         return (NULL);
00132 
00133     ncols = db_get_table_number_of_columns(table);
00134     sprintf(buf, " ");
00135     for (col = 0; col < ncols; col++) {
00136         if (col == 0)
00137             sprintf(buf, "%s",
00138                     db_sqltype_name(db_get_column_sqltype
00139                                     (db_get_table_column(table, col))));
00140         else {
00141             sprintf(temp_buf, ",%s",
00142                     db_sqltype_name(db_get_column_sqltype
00143                                     (db_get_table_column(table, col))));
00144             strcat(buf, temp_buf);
00145         }
00146     }
00147     G_debug(3, "%s", buf);
00148 
00149     db_close_database(driver);
00150     db_shutdown_driver(driver);
00151 
00152     return G_store(G_chop(buf));
00153 }
00154 
00155 
00165 const char *Vect_get_column_names_types(struct Map_info *Map, int field)
00166 {
00167     int num_dblinks, ncols, col;
00168     struct field_info *fi;
00169     dbDriver *driver = NULL;
00170     dbHandle handle;
00171     dbString table_name;
00172     dbTable *table;
00173     char buf[2000], temp_buf[2000];
00174 
00175 
00176     num_dblinks = Vect_get_num_dblinks(Map);
00177     if (num_dblinks <= 0)
00178         return (NULL);
00179 
00180     G_debug(3,
00181             "Displaying column types for database connection of layer %d:",
00182             field);
00183     if ((fi = Vect_get_field(Map, field)) == NULL)
00184         return (NULL);
00185     driver = db_start_driver(fi->driver);
00186     if (driver == NULL)
00187         return (NULL);
00188     db_init_handle(&handle);
00189     db_set_handle(&handle, fi->database, NULL);
00190     if (db_open_database(driver, &handle) != DB_OK)
00191         return (NULL);
00192     db_init_string(&table_name);
00193     db_set_string(&table_name, fi->table);
00194     if (db_describe_table(driver, &table_name, &table) != DB_OK)
00195         return (NULL);
00196 
00197     ncols = db_get_table_number_of_columns(table);
00198     sprintf(buf, " ");
00199     for (col = 0; col < ncols; col++) {
00200         if (col == 0)
00201             sprintf(buf, "%s(%s)",
00202                     db_get_column_name(db_get_table_column(table, col)),
00203                     db_sqltype_name(db_get_column_sqltype
00204                                     (db_get_table_column(table, col))));
00205         else {
00206             sprintf(temp_buf, ",%s(%s)",
00207                     db_get_column_name(db_get_table_column(table, col)),
00208                     db_sqltype_name(db_get_column_sqltype
00209                                     (db_get_table_column(table, col))));
00210             strcat(buf, temp_buf);
00211         }
00212     }
00213     G_debug(3, "%s", buf);
00214 
00215     db_close_database(driver);
00216     db_shutdown_driver(driver);
00217 
00218     return G_store(G_chop(buf));
00219 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines