dbcolumns.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*!
  2. \file 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 <grass/config.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <grass/glocale.h>
  18. #include <grass/gis.h>
  19. #include <grass/vector.h>
  20. #include <grass/dbmi.h>
  21. /*!
  22. \brief Fetches list of DB column names of vector map attribute table
  23. \param Map vector map
  24. \param field layer number
  25. \return list of column(s) names on success
  26. \return NULL on error
  27. */
  28. const char *Vect_get_column_names(const struct Map_info *Map, int field)
  29. {
  30. int num_dblinks, ncols, col;
  31. struct field_info *fi;
  32. dbDriver *driver = NULL;
  33. dbHandle handle;
  34. dbString table_name;
  35. dbTable *table;
  36. char buf[2000];
  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. sprintf(buf, " ");
  58. for (col = 0; col < ncols; col++) {
  59. if (col == 0)
  60. sprintf(buf, "%s",
  61. db_get_column_name(db_get_table_column(table, col)));
  62. else
  63. sprintf(buf, "%s,%s", buf,
  64. db_get_column_name(db_get_table_column(table, col)));
  65. }
  66. G_debug(3, "%s", buf);
  67. db_close_database(driver);
  68. db_shutdown_driver(driver);
  69. return G_store(G_chop(buf));
  70. }
  71. /*!
  72. \brief Fetches list of DB column types of vector map attribute table
  73. \param Map vector map
  74. \param field layer number
  75. \return list of column(s) types on success
  76. \return NULL on error
  77. */
  78. const char *Vect_get_column_types(const struct Map_info *Map, int field)
  79. {
  80. int num_dblinks, ncols, col;
  81. struct field_info *fi;
  82. dbDriver *driver = NULL;
  83. dbHandle handle;
  84. dbString table_name;
  85. dbTable *table;
  86. char buf[2000];
  87. num_dblinks = Vect_get_num_dblinks(Map);
  88. if (num_dblinks <= 0)
  89. return (NULL);
  90. G_debug(3,
  91. "Displaying column types for database connection of layer %d:",
  92. field);
  93. if ((fi = Vect_get_field(Map, field)) == NULL)
  94. return (NULL);
  95. driver = db_start_driver(fi->driver);
  96. if (driver == NULL)
  97. return (NULL);
  98. db_init_handle(&handle);
  99. db_set_handle(&handle, fi->database, NULL);
  100. if (db_open_database(driver, &handle) != DB_OK)
  101. return (NULL);
  102. db_init_string(&table_name);
  103. db_set_string(&table_name, fi->table);
  104. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  105. return (NULL);
  106. ncols = db_get_table_number_of_columns(table);
  107. sprintf(buf, " ");
  108. for (col = 0; col < ncols; col++) {
  109. if (col == 0)
  110. sprintf(buf, "%s",
  111. db_sqltype_name(db_get_column_sqltype
  112. (db_get_table_column(table, col))));
  113. else
  114. sprintf(buf, "%s,%s", buf,
  115. db_sqltype_name(db_get_column_sqltype
  116. (db_get_table_column(table, col))));
  117. }
  118. G_debug(3, "%s", buf);
  119. db_close_database(driver);
  120. db_shutdown_driver(driver);
  121. return G_store(G_chop(buf));
  122. }
  123. /*!
  124. \brief Fetches list of DB column names and types of vector map attribute table
  125. \param Map vector map
  126. \param field layer number
  127. \return list of column(s) types on success
  128. \retutn NULL on error
  129. */
  130. const char *Vect_get_column_names_types(const struct Map_info *Map, int field)
  131. {
  132. int num_dblinks, ncols, col;
  133. struct field_info *fi;
  134. dbDriver *driver = NULL;
  135. dbHandle handle;
  136. dbString table_name;
  137. dbTable *table;
  138. char buf[2000];
  139. num_dblinks = Vect_get_num_dblinks(Map);
  140. if (num_dblinks <= 0)
  141. return (NULL);
  142. G_debug(3,
  143. "Displaying column types for database connection of layer %d:",
  144. field);
  145. if ((fi = Vect_get_field(Map, field)) == NULL)
  146. return (NULL);
  147. driver = db_start_driver(fi->driver);
  148. if (driver == NULL)
  149. return (NULL);
  150. db_init_handle(&handle);
  151. db_set_handle(&handle, fi->database, NULL);
  152. if (db_open_database(driver, &handle) != DB_OK)
  153. return (NULL);
  154. db_init_string(&table_name);
  155. db_set_string(&table_name, fi->table);
  156. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  157. return (NULL);
  158. ncols = db_get_table_number_of_columns(table);
  159. sprintf(buf, " ");
  160. for (col = 0; col < ncols; col++) {
  161. if (col == 0)
  162. sprintf(buf, "%s(%s)",
  163. db_get_column_name(db_get_table_column(table, col)),
  164. db_sqltype_name(db_get_column_sqltype
  165. (db_get_table_column(table, col))));
  166. else
  167. sprintf(buf, "%s,%s(%s)", buf,
  168. db_get_column_name(db_get_table_column(table, col)),
  169. db_sqltype_name(db_get_column_sqltype
  170. (db_get_table_column(table, col))));
  171. }
  172. G_debug(3, "%s", buf);
  173. db_close_database(driver);
  174. db_shutdown_driver(driver);
  175. return G_store(G_chop(buf));
  176. }