describe.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**********************************************************
  2. * MODULE: mysql
  3. * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  4. * PURPOSE: MySQL database driver
  5. * COPYRIGHT: (C) 2001 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. **********************************************************/
  11. #include <grass/gis.h>
  12. #include <grass/dbmi.h>
  13. #include <grass/glocale.h>
  14. #include "globals.h"
  15. #include "proto.h"
  16. int db__driver_describe_table(dbString * table_name, dbTable ** table)
  17. {
  18. dbString sql;
  19. MYSQL_RES *res;
  20. db_init_string(&sql);
  21. db_set_string(&sql, "select * from ");
  22. db_append_string(&sql, db_get_string(table_name));
  23. db_append_string(&sql, " where 1 = 0");
  24. if (mysql_query(connection, db_get_string(&sql)) != 0) {
  25. db_d_append_error("%s\n%s\n%s",
  26. _("Unable to describe table:"),
  27. db_get_string(&sql),
  28. mysql_error(connection));
  29. db_d_report_error();
  30. return DB_FAILED;
  31. }
  32. res = mysql_store_result(connection);
  33. if (res == NULL) {
  34. db_d_append_error("%s\n%s",
  35. db_get_string(&sql),
  36. mysql_error(connection));
  37. db_d_report_error();
  38. return DB_FAILED;
  39. }
  40. if (describe_table(res, table, NULL) == DB_FAILED) {
  41. db_d_append_error(_("Unable to describe table"));
  42. db_d_report_error();
  43. mysql_free_result(res);
  44. return DB_FAILED;
  45. }
  46. mysql_free_result(res);
  47. db_set_table_name(*table, db_get_string(table_name));
  48. return DB_OK;
  49. }
  50. /* describe table, if c is not NULL cur->cols and cur->ncols is also set */
  51. int describe_table(MYSQL_RES * res, dbTable ** table, cursor * c)
  52. {
  53. int i, ncols, kcols;
  54. char *name;
  55. int sqltype, length;
  56. dbColumn *column;
  57. MYSQL_FIELD *fields;
  58. G_debug(3, "describe_table()");
  59. ncols = mysql_num_fields(res);
  60. fields = mysql_fetch_fields(res);
  61. /* Count columns of known type */
  62. kcols = 0;
  63. for (i = 0; i < ncols; i++) {
  64. field_info(&(fields[i]), &sqltype, &length);
  65. if (sqltype == DB_SQL_TYPE_UNKNOWN)
  66. continue;
  67. kcols++; /* known types */
  68. }
  69. G_debug(3, "kcols = %d", kcols);
  70. if (!(*table = db_alloc_table(kcols))) {
  71. return DB_FAILED;
  72. }
  73. if (c) {
  74. c->ncols = kcols;
  75. c->cols = (int *)G_malloc(kcols * sizeof(int));
  76. }
  77. db_set_table_name(*table, "");
  78. db_set_table_description(*table, "");
  79. /* Currently not used in GRASS */
  80. /*
  81. db_set_table_delete_priv_granted (*table);
  82. db_set_table_insert_priv_granted (*table);
  83. db_set_table_delete_priv_not_granted (*table);
  84. db_set_table_insert_priv_not_granted (*table);
  85. */
  86. kcols = 0;
  87. for (i = 0; i < ncols; i++) {
  88. name = fields[i].name;
  89. field_info(&(fields[i]), &sqltype, &length);
  90. G_debug(3, "col: %s, kcols %d, sqltype %d", name, kcols, sqltype);
  91. G_debug(3, "flags = %d", fields[i].flags);
  92. if (sqltype == DB_SQL_TYPE_UNKNOWN) {
  93. /* Print warning and continue */
  94. G_warning(_("MySQL driver: column '%s', type %d "
  95. "is not supported"), name, fields[i].type);
  96. continue;
  97. }
  98. if (fields[i].type == MYSQL_TYPE_LONGLONG)
  99. G_warning(_("column '%s' : type BIGINT is stored as "
  100. "integer (4 bytes) some data may be damaged"), name);
  101. column = db_get_table_column(*table, kcols);
  102. db_set_column_name(column, name);
  103. db_set_column_length(column, length);
  104. db_set_column_host_type(column, (int)fields[i].type);
  105. db_set_column_sqltype(column, sqltype);
  106. db_set_column_precision(column, (int)fields[i].decimals);
  107. db_set_column_scale(column, 0);
  108. if (!(fields[i].flags & NOT_NULL_FLAG)) {
  109. db_set_column_null_allowed(column);
  110. }
  111. db_set_column_has_undefined_default_value(column);
  112. db_unset_column_use_default_value(column);
  113. /* Currently not used in GRASS */
  114. /*
  115. db_set_column_select_priv_granted (column);
  116. db_set_column_update_priv_granted (column);
  117. db_set_column_update_priv_not_granted (column);
  118. */
  119. if (c) {
  120. c->cols[kcols] = i;
  121. }
  122. kcols++;
  123. }
  124. return DB_OK;
  125. }
  126. /* Get sqltype for field */
  127. void field_info(MYSQL_FIELD * field, int *sqltype, int *length)
  128. {
  129. *length = field->length;
  130. switch (field->type) {
  131. case MYSQL_TYPE_TINY:
  132. *sqltype = DB_SQL_TYPE_SMALLINT;
  133. break;
  134. case MYSQL_TYPE_SHORT:
  135. case MYSQL_TYPE_LONG:
  136. case MYSQL_TYPE_INT24:
  137. case MYSQL_TYPE_LONGLONG:
  138. *sqltype = DB_SQL_TYPE_INTEGER;
  139. break;
  140. case MYSQL_TYPE_DECIMAL:
  141. #ifdef MYSQL_TYPE_NEWDECIMAL
  142. case MYSQL_TYPE_NEWDECIMAL:
  143. #endif
  144. *sqltype = DB_SQL_TYPE_DECIMAL;
  145. break;
  146. case MYSQL_TYPE_FLOAT:
  147. *sqltype = DB_SQL_TYPE_REAL;
  148. break;
  149. case MYSQL_TYPE_DOUBLE:
  150. *sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
  151. break;
  152. case MYSQL_TYPE_TIMESTAMP:
  153. *sqltype = DB_SQL_TYPE_TIMESTAMP;
  154. break;
  155. case MYSQL_TYPE_DATE:
  156. *sqltype = DB_SQL_TYPE_DATE;
  157. break;
  158. case MYSQL_TYPE_TIME:
  159. *sqltype = DB_SQL_TYPE_TIME;
  160. break;
  161. case MYSQL_TYPE_DATETIME:
  162. /* *sqltype = DB_SQL_TYPE_DATETIME; */
  163. /* *sqltype |= DB_DATETIME_MASK; */
  164. *sqltype = DB_SQL_TYPE_TIMESTAMP;
  165. break;
  166. case MYSQL_TYPE_YEAR:
  167. *sqltype = DB_SQL_TYPE_INTEGER;
  168. break;
  169. case MYSQL_TYPE_STRING:
  170. case MYSQL_TYPE_VAR_STRING:
  171. case MYSQL_TYPE_SET:
  172. case MYSQL_TYPE_ENUM:
  173. *sqltype = DB_SQL_TYPE_CHARACTER;
  174. break;
  175. case MYSQL_TYPE_BLOB:
  176. if (field->flags & BINARY_FLAG) {
  177. *sqltype = DB_SQL_TYPE_UNKNOWN;
  178. }
  179. else {
  180. *sqltype = DB_SQL_TYPE_TEXT;
  181. }
  182. break;
  183. case MYSQL_TYPE_GEOMETRY:
  184. case MYSQL_TYPE_NULL:
  185. *sqltype = DB_SQL_TYPE_UNKNOWN;
  186. break;
  187. default:
  188. *sqltype = DB_SQL_TYPE_UNKNOWN;
  189. }
  190. return;
  191. }