dbcolumns.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*!
  2. \file lib/vector/Vlib/dbcolumns.c
  3. \brief Vector library - DB info on vectors maps
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2005-2009 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Markus Neteler
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <grass/glocale.h>
  17. #include <grass/vector.h>
  18. #include <grass/dbmi.h>
  19. #define BUFF_MAX 2000
  20. /*!
  21. \brief Fetches list of DB column names of vector map attribute table
  22. \param Map vector map
  23. \param field layer number
  24. \return list of column(s) names on success
  25. \return NULL on error
  26. */
  27. const char *Vect_get_column_names(const struct Map_info *Map, int field)
  28. {
  29. int num_dblinks, ncols, col;
  30. struct field_info *fi;
  31. dbDriver *driver = NULL;
  32. dbHandle handle;
  33. dbString table_name;
  34. dbTable *table;
  35. const char **col_names;
  36. char *list;
  37. num_dblinks = Vect_get_num_dblinks(Map);
  38. if (num_dblinks <= 0)
  39. return (NULL);
  40. G_debug(3,
  41. "Displaying column names for database connection of layer %d:",
  42. field);
  43. if ((fi = Vect_get_field(Map, field)) == NULL)
  44. return (NULL);
  45. driver = db_start_driver(fi->driver);
  46. if (driver == NULL)
  47. return (NULL);
  48. db_init_handle(&handle);
  49. db_set_handle(&handle, fi->database, NULL);
  50. if (db_open_database(driver, &handle) != DB_OK)
  51. return (NULL);
  52. db_init_string(&table_name);
  53. db_set_string(&table_name, fi->table);
  54. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  55. return (NULL);
  56. ncols = db_get_table_number_of_columns(table);
  57. col_names = G_malloc(ncols * sizeof(char *));
  58. for (col = 0; col < ncols; col++)
  59. col_names[col] = db_get_column_name(db_get_table_column(table, col));
  60. if ((list = G_str_concat(col_names, ncols, ",", BUFF_MAX)) == NULL)
  61. list = G_store("");
  62. G_free(col_names);
  63. G_debug(3, "%s", list);
  64. db_close_database(driver);
  65. db_shutdown_driver(driver);
  66. return list;
  67. }
  68. /*!
  69. \brief Fetches list of DB column types of vector map attribute table
  70. \param Map vector map
  71. \param field layer number
  72. \return list of column(s) types on success
  73. \return NULL on error
  74. */
  75. const char *Vect_get_column_types(const struct Map_info *Map, int field)
  76. {
  77. int num_dblinks, ncols, col;
  78. struct field_info *fi;
  79. dbDriver *driver = NULL;
  80. dbHandle handle;
  81. dbString table_name;
  82. dbTable *table;
  83. const char **sqltype_names;
  84. char *list;
  85. num_dblinks = Vect_get_num_dblinks(Map);
  86. if (num_dblinks <= 0)
  87. return (NULL);
  88. G_debug(3,
  89. "Displaying column types for database connection of layer %d:",
  90. field);
  91. if ((fi = Vect_get_field(Map, field)) == NULL)
  92. return (NULL);
  93. driver = db_start_driver(fi->driver);
  94. if (driver == NULL)
  95. return (NULL);
  96. db_init_handle(&handle);
  97. db_set_handle(&handle, fi->database, NULL);
  98. if (db_open_database(driver, &handle) != DB_OK)
  99. return (NULL);
  100. db_init_string(&table_name);
  101. db_set_string(&table_name, fi->table);
  102. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  103. return (NULL);
  104. ncols = db_get_table_number_of_columns(table);
  105. sqltype_names = G_malloc(ncols * sizeof(char *));
  106. for (col = 0; col < ncols; col++)
  107. sqltype_names[col] = db_sqltype_name(db_get_column_sqltype
  108. (db_get_table_column
  109. (table, col)));
  110. if ((list = G_str_concat(sqltype_names, ncols, ",", BUFF_MAX)) == NULL)
  111. list = G_store("");
  112. G_free(sqltype_names);
  113. G_debug(3, "%s", list);
  114. db_close_database(driver);
  115. db_shutdown_driver(driver);
  116. return list;
  117. }
  118. /*!
  119. \brief Fetches list of DB column names and types of vector map attribute table
  120. \param Map vector map
  121. \param field layer number
  122. \return list of column(s) types on success
  123. \return NULL on error
  124. */
  125. const char *Vect_get_column_names_types(const struct Map_info *Map, int field)
  126. {
  127. int num_dblinks, ncols, col;
  128. struct field_info *fi;
  129. dbDriver *driver = NULL;
  130. dbHandle handle;
  131. dbString table_name;
  132. dbTable *table;
  133. const char **col_type_names;
  134. char *list;
  135. num_dblinks = Vect_get_num_dblinks(Map);
  136. if (num_dblinks <= 0)
  137. return (NULL);
  138. G_debug(3,
  139. "Displaying column types for database connection of layer %d:",
  140. field);
  141. if ((fi = Vect_get_field(Map, field)) == NULL)
  142. return (NULL);
  143. driver = db_start_driver(fi->driver);
  144. if (driver == NULL)
  145. return (NULL);
  146. db_init_handle(&handle);
  147. db_set_handle(&handle, fi->database, NULL);
  148. if (db_open_database(driver, &handle) != DB_OK)
  149. return (NULL);
  150. db_init_string(&table_name);
  151. db_set_string(&table_name, fi->table);
  152. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  153. return (NULL);
  154. ncols = db_get_table_number_of_columns(table);
  155. col_type_names = G_malloc(ncols * sizeof(char *));
  156. for (col = 0; col < ncols; col++) {
  157. char buf[256];
  158. sprintf(buf, "%s(%s)",
  159. db_get_column_name(db_get_table_column(table, col)),
  160. db_sqltype_name(db_get_column_sqltype
  161. (db_get_table_column(table, col))));
  162. col_type_names[col] = buf;
  163. }
  164. if ((list = G_str_concat(col_type_names, ncols, ",", BUFF_MAX)) == NULL)
  165. list = G_store("");
  166. G_free(col_type_names);
  167. G_debug(3, "%s", list);
  168. db_close_database(driver);
  169. db_shutdown_driver(driver);
  170. return list;
  171. }