dbcolumns.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /*!
  20. \brief Fetches list of DB column names of vector map attribute table
  21. \param Map vector map
  22. \param field layer number
  23. \return list of column(s) names on success
  24. \return NULL on error
  25. */
  26. const char *Vect_get_column_names(const struct Map_info *Map, int field)
  27. {
  28. int num_dblinks, ncols, col;
  29. struct field_info *fi;
  30. dbDriver *driver = NULL;
  31. dbHandle handle;
  32. dbString table_name;
  33. dbTable *table;
  34. char buf[2000];
  35. num_dblinks = Vect_get_num_dblinks(Map);
  36. if (num_dblinks <= 0)
  37. return (NULL);
  38. G_debug(3,
  39. "Displaying column names for database connection of layer %d:",
  40. field);
  41. if ((fi = Vect_get_field(Map, field)) == NULL)
  42. return (NULL);
  43. driver = db_start_driver(fi->driver);
  44. if (driver == NULL)
  45. return (NULL);
  46. db_init_handle(&handle);
  47. db_set_handle(&handle, fi->database, NULL);
  48. if (db_open_database(driver, &handle) != DB_OK)
  49. return (NULL);
  50. db_init_string(&table_name);
  51. db_set_string(&table_name, fi->table);
  52. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  53. return (NULL);
  54. ncols = db_get_table_number_of_columns(table);
  55. sprintf(buf, " ");
  56. for (col = 0; col < ncols; col++) {
  57. if (col == 0)
  58. sprintf(buf, "%s",
  59. db_get_column_name(db_get_table_column(table, col)));
  60. else
  61. sprintf(buf, "%s,%s", buf,
  62. db_get_column_name(db_get_table_column(table, col)));
  63. }
  64. G_debug(3, "%s", buf);
  65. db_close_database(driver);
  66. db_shutdown_driver(driver);
  67. return G_store(G_chop(buf));
  68. }
  69. /*!
  70. \brief Fetches list of DB column types of vector map attribute table
  71. \param Map vector map
  72. \param field layer number
  73. \return list of column(s) types on success
  74. \return NULL on error
  75. */
  76. const char *Vect_get_column_types(const struct Map_info *Map, int field)
  77. {
  78. int num_dblinks, ncols, col;
  79. struct field_info *fi;
  80. dbDriver *driver = NULL;
  81. dbHandle handle;
  82. dbString table_name;
  83. dbTable *table;
  84. char buf[2000];
  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. sprintf(buf, " ");
  106. for (col = 0; col < ncols; col++) {
  107. if (col == 0)
  108. sprintf(buf, "%s",
  109. db_sqltype_name(db_get_column_sqltype
  110. (db_get_table_column(table, col))));
  111. else
  112. sprintf(buf, "%s,%s", buf,
  113. db_sqltype_name(db_get_column_sqltype
  114. (db_get_table_column(table, col))));
  115. }
  116. G_debug(3, "%s", buf);
  117. db_close_database(driver);
  118. db_shutdown_driver(driver);
  119. return G_store(G_chop(buf));
  120. }
  121. /*!
  122. \brief Fetches list of DB column names and types of vector map attribute table
  123. \param Map vector map
  124. \param field layer number
  125. \return list of column(s) types on success
  126. \return NULL on error
  127. */
  128. const char *Vect_get_column_names_types(const struct Map_info *Map, int field)
  129. {
  130. int num_dblinks, ncols, col;
  131. struct field_info *fi;
  132. dbDriver *driver = NULL;
  133. dbHandle handle;
  134. dbString table_name;
  135. dbTable *table;
  136. char buf[2000];
  137. num_dblinks = Vect_get_num_dblinks(Map);
  138. if (num_dblinks <= 0)
  139. return (NULL);
  140. G_debug(3,
  141. "Displaying column types for database connection of layer %d:",
  142. field);
  143. if ((fi = Vect_get_field(Map, field)) == NULL)
  144. return (NULL);
  145. driver = db_start_driver(fi->driver);
  146. if (driver == NULL)
  147. return (NULL);
  148. db_init_handle(&handle);
  149. db_set_handle(&handle, fi->database, NULL);
  150. if (db_open_database(driver, &handle) != DB_OK)
  151. return (NULL);
  152. db_init_string(&table_name);
  153. db_set_string(&table_name, fi->table);
  154. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  155. return (NULL);
  156. ncols = db_get_table_number_of_columns(table);
  157. sprintf(buf, " ");
  158. for (col = 0; col < ncols; col++) {
  159. if (col == 0)
  160. sprintf(buf, "%s(%s)",
  161. db_get_column_name(db_get_table_column(table, col)),
  162. db_sqltype_name(db_get_column_sqltype
  163. (db_get_table_column(table, col))));
  164. else
  165. sprintf(buf, "%s,%s(%s)", buf,
  166. db_get_column_name(db_get_table_column(table, col)),
  167. db_sqltype_name(db_get_column_sqltype
  168. (db_get_table_column(table, col))));
  169. }
  170. G_debug(3, "%s", buf);
  171. db_close_database(driver);
  172. db_shutdown_driver(driver);
  173. return G_store(G_chop(buf));
  174. }