dbcolumns.c

Go to the documentation of this file.
00001 /****************************************************************************
00002 *
00003 * MODULE:       Vector library 
00004 *               
00005 * AUTHOR(S):    Markus Neteler
00006 *
00007 * PURPOSE:      DB info on vectors maps
00008 *
00009 * COPYRIGHT:    (C) 2005 by the GRASS Development Team
00010 *
00011 *               This program is free software under the GNU General Public
00012 *               License (>=v2). Read the file COPYING that comes with GRASS
00013 *               for details.
00014 *
00015 *****************************************************************************/
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <unistd.h>
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <grass/glocale.h>
00023 #include <grass/gis.h>
00024 #include <grass/Vect.h>
00025 #include <grass/dbmi.h>
00026 
00034 char *Vect_get_column_names(struct Map_info *Map, int field)
00035 {
00036     int num_dblinks, ncols, col;
00037     struct field_info *fi;
00038     dbDriver *driver = NULL;
00039     dbHandle handle;
00040     dbString table_name;
00041     dbTable *table;
00042     char buf[2000];
00043     char *ptr;
00044 
00045     
00046     num_dblinks = Vect_get_num_dblinks(Map);
00047     if (num_dblinks <= 0) {
00048         return (NULL);
00049     }
00050     else {                      /* num_dblinks > 0 */
00051 
00052         G_debug(3,
00053                 "Displaying column names for database connection of layer %d:",
00054                 field);
00055         if ((fi = Vect_get_field(Map, field)) == NULL)
00056             return (NULL);
00057         driver = db_start_driver(fi->driver);
00058         if (driver == NULL)
00059             return (NULL);
00060         db_init_handle(&handle);
00061         db_set_handle(&handle, fi->database, NULL);
00062         if (db_open_database(driver, &handle) != DB_OK)
00063             return (NULL);
00064         db_init_string(&table_name);
00065         db_set_string(&table_name, fi->table);
00066         if (db_describe_table(driver, &table_name, &table) != DB_OK)
00067             return (NULL);
00068 
00069         ncols = db_get_table_number_of_columns(table);
00070         sprintf ( buf," " );
00071         for (col = 0; col < ncols; col++) {
00072             if (col == 0)
00073                 sprintf(buf, "%s", db_get_column_name(db_get_table_column(table, col)));
00074             else
00075                 sprintf(buf, "%s,%s", buf, db_get_column_name(db_get_table_column(table, col)));
00076         }
00077         G_debug(3, "%s", buf);
00078 
00079         db_close_database(driver);
00080         db_shutdown_driver(driver);
00081 
00082         ptr = G_malloc ( strlen(G_chop(buf)) );
00083         sprintf (ptr, "%s", buf);
00084         return (ptr);
00085     }
00086 }
00087 
00094 char *Vect_get_column_types(struct Map_info *Map, int field)
00095 {
00096     int num_dblinks, ncols, col;
00097     struct field_info *fi;
00098     dbDriver *driver = NULL;
00099     dbHandle handle;
00100     dbString table_name;
00101     dbTable *table;
00102     char buf[2000];
00103     char *ptr;
00104 
00105     
00106     num_dblinks = Vect_get_num_dblinks(Map);
00107     if (num_dblinks <= 0) {
00108         return (NULL);
00109     }
00110     else {                      /* num_dblinks > 0 */
00111 
00112         G_debug(3,
00113                 "Displaying column types for database connection of layer %d:",
00114                 field);
00115         if ((fi = Vect_get_field(Map, field)) == NULL)
00116             return (NULL);
00117         driver = db_start_driver(fi->driver);
00118         if (driver == NULL)
00119             return (NULL);
00120         db_init_handle(&handle);
00121         db_set_handle(&handle, fi->database, NULL);
00122         if (db_open_database(driver, &handle) != DB_OK)
00123             return (NULL);
00124         db_init_string(&table_name);
00125         db_set_string(&table_name, fi->table);
00126         if (db_describe_table(driver, &table_name, &table) != DB_OK)
00127             return (NULL);
00128 
00129         ncols = db_get_table_number_of_columns(table);
00130         sprintf ( buf," " );
00131         for (col = 0; col < ncols; col++) {
00132             if (col == 0)
00133                 sprintf(buf, "%s", db_sqltype_name(db_get_column_sqltype(db_get_table_column(table, col))));
00134             else
00135                 sprintf(buf, "%s,%s", buf, db_sqltype_name(db_get_column_sqltype(db_get_table_column(table, col))));
00136         }
00137         G_debug(3, "%s", buf);
00138 
00139         db_close_database(driver);
00140         db_shutdown_driver(driver);
00141 
00142         ptr = G_malloc ( strlen(G_chop(buf)) );
00143         sprintf (ptr, "%s", buf);
00144         return (ptr);
00145     }
00146 }
00147 
00148 
00155 char *Vect_get_column_names_types(struct Map_info *Map, int field)
00156 {
00157     int num_dblinks, ncols, col;
00158     struct field_info *fi;
00159     dbDriver *driver = NULL;
00160     dbHandle handle;
00161     dbString table_name;
00162     dbTable *table;
00163     char buf[2000];
00164     char *ptr;
00165 
00166     
00167     num_dblinks = Vect_get_num_dblinks(Map);
00168     if (num_dblinks <= 0) {
00169         return (NULL);
00170     }
00171     else {                      /* num_dblinks > 0 */
00172 
00173         G_debug(3,
00174                 "Displaying column types for database connection of layer %d:",
00175                 field);
00176         if ((fi = Vect_get_field(Map, field)) == NULL)
00177             return (NULL);
00178         driver = db_start_driver(fi->driver);
00179         if (driver == NULL)
00180             return (NULL);
00181         db_init_handle(&handle);
00182         db_set_handle(&handle, fi->database, NULL);
00183         if (db_open_database(driver, &handle) != DB_OK)
00184             return (NULL);
00185         db_init_string(&table_name);
00186         db_set_string(&table_name, fi->table);
00187         if (db_describe_table(driver, &table_name, &table) != DB_OK)
00188             return (NULL);
00189 
00190         ncols = db_get_table_number_of_columns(table);
00191         sprintf ( buf," " );
00192         for (col = 0; col < ncols; col++) {
00193             if (col == 0)
00194                 sprintf(buf, "%s(%s)", db_get_column_name(db_get_table_column(table, col)),
00195                         db_sqltype_name(db_get_column_sqltype(db_get_table_column(table, col))));
00196             else
00197                 sprintf(buf, "%s,%s(%s)", buf, db_get_column_name(db_get_table_column(table, col)),
00198                         db_sqltype_name(db_get_column_sqltype(db_get_table_column(table, col))));
00199         }
00200         G_debug(3, "%s", buf);
00201 
00202         db_close_database(driver);
00203         db_shutdown_driver(driver);
00204 
00205         ptr = G_malloc ( strlen(G_chop(buf)) );
00206         sprintf (ptr, "%s", buf);
00207         return (ptr);
00208     }
00209 }

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5