dbcolumns.c 5.4 KB

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