column.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. \file lib/db/dbmi_client/column.c
  3. \brief DBMI Library (client) - column info
  4. (C) 1999-2008, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. \author Joel Jones (CERL/UIUC), Radim Blazek
  9. \author Update by Glynn Clement <glynn gclements.plus.com>
  10. \author Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <grass/gis.h>
  15. #include <grass/dbmi.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. \brief Get column sqltype
  19. See db_sqltype_name().
  20. Supported types:
  21. - DB_SQL_TYPE_UNKNOWN
  22. - DB_SQL_TYPE_CHARACTER
  23. - DB_SQL_TYPE_SMALLINT
  24. - DB_SQL_TYPE_INTEGER
  25. - DB_SQL_TYPE_REAL
  26. - DB_SQL_TYPE_DOUBLE_PRECISION
  27. - DB_SQL_TYPE_DECIMAL
  28. - DB_SQL_TYPE_NUMERIC
  29. - DB_SQL_TYPE_DATE
  30. - DB_SQL_TYPE_TIME
  31. - DB_SQL_TYPE_TIMESTAMP
  32. - DB_SQL_TYPE_INTERVAL
  33. - DB_SQL_TYPE_TEXT
  34. - DB_SQL_TYPE_SERIAL
  35. \param driver DB driver
  36. \param tab table name
  37. \param col column name
  38. \return column sqltype
  39. \return -1 on error
  40. */
  41. int db_column_sqltype(dbDriver * driver, const char *tab, const char *col)
  42. {
  43. dbTable *table;
  44. dbString table_name;
  45. dbColumn *column;
  46. int ncol, cl, type;
  47. type = -1;
  48. db_init_string(&table_name);
  49. db_set_string(&table_name, tab);
  50. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  51. return -1;
  52. db_free_string(&table_name);
  53. ncol = db_get_table_number_of_columns(table);
  54. for (cl = 0; cl < ncol; cl++) {
  55. column = db_get_table_column(table, cl);
  56. if (strcmp(db_get_column_name(column), col) == 0) {
  57. type = db_get_column_sqltype(column);
  58. break;
  59. }
  60. }
  61. db_free_table(table);
  62. return type;
  63. }
  64. /*!
  65. \brief Get column ctype
  66. See db_sqltype_to_Ctype().
  67. Supported types:
  68. - DB_C_TYPE_STRING
  69. - DB_C_TYPE_INT
  70. - DB_C_TYPE_DOUBLE
  71. - DB_C_TYPE_DATETIME
  72. \param driver DB driver
  73. \param tab table name
  74. \param col column name
  75. \return column Ctype
  76. \return -1 on error
  77. */
  78. int db_column_Ctype(dbDriver * driver, const char *tab, const char *col)
  79. {
  80. int type;
  81. if ((type = db_column_sqltype(driver, tab, col)) >= 0) {
  82. type = db_sqltype_to_Ctype(type);
  83. return type;
  84. }
  85. return -1;
  86. }
  87. /*!
  88. \brief Get column structure by table and column name.
  89. Column is set to new dbColumn structure or NULL if column was not found
  90. \param Driver DB driver
  91. \param tname table name
  92. \param cname column name
  93. \param[out] Column column structure to store within
  94. \return DB_OK on success
  95. \return DB_FAILED on failure
  96. */
  97. int db_get_column(dbDriver * Driver, const char *tname, const char *cname,
  98. dbColumn ** Column)
  99. {
  100. int i, ncols, ret;
  101. dbTable *Table;
  102. dbColumn *Col;
  103. dbString tabname;
  104. db_init_string(&tabname);
  105. db_set_string(&tabname, tname);
  106. if (db_describe_table(Driver, &tabname, &Table) != DB_OK) {
  107. G_warning(_("Unable to describe table <%s>"), tname);
  108. return DB_FAILED;
  109. }
  110. *Column = NULL;
  111. ret = DB_FAILED;
  112. ncols = db_get_table_number_of_columns(Table);
  113. G_debug(3, "ncol = %d", ncols);
  114. for (i = 0; i < ncols; i++) {
  115. Col = db_get_table_column(Table, i);
  116. if (G_strcasecmp(db_get_column_name(Col), cname) == 0) {
  117. *Column = db_copy_column(NULL, Col);
  118. ret = DB_OK;
  119. break;
  120. }
  121. }
  122. db_free_table(Table);
  123. return ret;
  124. }