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];
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(buf, "%s,%s", buf,
00080                     db_get_column_name(db_get_table_column(table, col)));
00081     }
00082     G_debug(3, "%s", buf);
00083 
00084     db_close_database(driver);
00085     db_shutdown_driver(driver);
00086 
00087     return G_store(G_chop(buf));
00088 }
00089 
00099 const char *Vect_get_column_types(struct Map_info *Map, int field)
00100 {
00101     int num_dblinks, ncols, col;
00102     struct field_info *fi;
00103     dbDriver *driver = NULL;
00104     dbHandle handle;
00105     dbString table_name;
00106     dbTable *table;
00107     char buf[2000];
00108 
00109 
00110     num_dblinks = Vect_get_num_dblinks(Map);
00111     if (num_dblinks <= 0)
00112         return (NULL);
00113 
00114     G_debug(3,
00115             "Displaying column types for database connection of layer %d:",
00116             field);
00117     if ((fi = Vect_get_field(Map, field)) == NULL)
00118         return (NULL);
00119     driver = db_start_driver(fi->driver);
00120     if (driver == NULL)
00121         return (NULL);
00122     db_init_handle(&handle);
00123     db_set_handle(&handle, fi->database, NULL);
00124     if (db_open_database(driver, &handle) != DB_OK)
00125         return (NULL);
00126     db_init_string(&table_name);
00127     db_set_string(&table_name, fi->table);
00128     if (db_describe_table(driver, &table_name, &table) != DB_OK)
00129         return (NULL);
00130 
00131     ncols = db_get_table_number_of_columns(table);
00132     sprintf(buf, " ");
00133     for (col = 0; col < ncols; col++) {
00134         if (col == 0)
00135             sprintf(buf, "%s",
00136                     db_sqltype_name(db_get_column_sqltype
00137                                     (db_get_table_column(table, col))));
00138         else
00139             sprintf(buf, "%s,%s", buf,
00140                     db_sqltype_name(db_get_column_sqltype
00141                                     (db_get_table_column(table, col))));
00142     }
00143     G_debug(3, "%s", buf);
00144 
00145     db_close_database(driver);
00146     db_shutdown_driver(driver);
00147 
00148     return G_store(G_chop(buf));
00149 }
00150 
00151 
00161 const char *Vect_get_column_names_types(struct Map_info *Map, int field)
00162 {
00163     int num_dblinks, ncols, col;
00164     struct field_info *fi;
00165     dbDriver *driver = NULL;
00166     dbHandle handle;
00167     dbString table_name;
00168     dbTable *table;
00169     char buf[2000];
00170 
00171 
00172     num_dblinks = Vect_get_num_dblinks(Map);
00173     if (num_dblinks <= 0)
00174         return (NULL);
00175 
00176     G_debug(3,
00177             "Displaying column types for database connection of layer %d:",
00178             field);
00179     if ((fi = Vect_get_field(Map, field)) == NULL)
00180         return (NULL);
00181     driver = db_start_driver(fi->driver);
00182     if (driver == NULL)
00183         return (NULL);
00184     db_init_handle(&handle);
00185     db_set_handle(&handle, fi->database, NULL);
00186     if (db_open_database(driver, &handle) != DB_OK)
00187         return (NULL);
00188     db_init_string(&table_name);
00189     db_set_string(&table_name, fi->table);
00190     if (db_describe_table(driver, &table_name, &table) != DB_OK)
00191         return (NULL);
00192 
00193     ncols = db_get_table_number_of_columns(table);
00194     sprintf(buf, " ");
00195     for (col = 0; col < ncols; col++) {
00196         if (col == 0)
00197             sprintf(buf, "%s(%s)",
00198                     db_get_column_name(db_get_table_column(table, col)),
00199                     db_sqltype_name(db_get_column_sqltype
00200                                     (db_get_table_column(table, col))));
00201         else
00202             sprintf(buf, "%s,%s(%s)", buf,
00203                     db_get_column_name(db_get_table_column(table, col)),
00204                     db_sqltype_name(db_get_column_sqltype
00205                                     (db_get_table_column(table, col))));
00206     }
00207     G_debug(3, "%s", buf);
00208 
00209     db_close_database(driver);
00210     db_shutdown_driver(driver);
00211 
00212     return G_store(G_chop(buf));
00213 }