describe.c 5.4 KB

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