describe.c 5.6 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. {
  26. append_error ( db_get_string(&sql) );
  27. append_error ( "\n" );
  28. append_error ( mysql_error(connection) );
  29. report_error();
  30. return DB_FAILED;
  31. }
  32. res = mysql_store_result ( connection );
  33. if ( res == NULL ) {
  34. append_error ( db_get_string(&sql) );
  35. append_error ( "\n" );
  36. append_error ( mysql_error(connection) );
  37. report_error();
  38. return DB_FAILED;
  39. }
  40. if ( describe_table( res, table, NULL) == DB_FAILED ) {
  41. append_error("Cannot describe table\n");
  42. 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 ) 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",
  90. 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. {
  110. db_set_column_null_allowed(column);
  111. }
  112. db_set_column_has_undefined_default_value(column);
  113. db_unset_column_use_default_value(column);
  114. /* Currently not used in GRASS */
  115. /*
  116. db_set_column_select_priv_granted (column);
  117. db_set_column_update_priv_granted (column);
  118. db_set_column_update_priv_not_granted (column);
  119. */
  120. if ( c ) {
  121. c->cols[kcols] = i;
  122. }
  123. kcols++;
  124. }
  125. return DB_OK;
  126. }
  127. /* Get sqltype for field */
  128. void field_info ( MYSQL_FIELD *field, int * sqltype, int * length )
  129. {
  130. *length = field->length;
  131. switch ( field->type )
  132. {
  133. case MYSQL_TYPE_TINY:
  134. *sqltype = DB_SQL_TYPE_SMALLINT;
  135. break;
  136. case MYSQL_TYPE_SHORT:
  137. case MYSQL_TYPE_LONG:
  138. case MYSQL_TYPE_INT24:
  139. case MYSQL_TYPE_LONGLONG:
  140. *sqltype = DB_SQL_TYPE_INTEGER;
  141. break;
  142. case MYSQL_TYPE_DECIMAL:
  143. *sqltype = DB_SQL_TYPE_DECIMAL;
  144. break;
  145. case MYSQL_TYPE_FLOAT:
  146. *sqltype = DB_SQL_TYPE_REAL;
  147. break;
  148. case MYSQL_TYPE_DOUBLE:
  149. *sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
  150. break;
  151. case MYSQL_TYPE_TIMESTAMP:
  152. *sqltype = DB_SQL_TYPE_TIMESTAMP;
  153. break;
  154. case MYSQL_TYPE_DATE:
  155. *sqltype = DB_SQL_TYPE_DATE;
  156. break;
  157. case MYSQL_TYPE_TIME:
  158. *sqltype = DB_SQL_TYPE_TIME;
  159. break;
  160. case MYSQL_TYPE_DATETIME:
  161. //*sqltype = DB_SQL_TYPE_DATETIME;
  162. //*sqltype |= DB_DATETIME_MASK;
  163. *sqltype = DB_SQL_TYPE_TIMESTAMP;
  164. break;
  165. case MYSQL_TYPE_YEAR:
  166. *sqltype = DB_SQL_TYPE_INTEGER;
  167. break;
  168. case MYSQL_TYPE_STRING:
  169. case MYSQL_TYPE_VAR_STRING:
  170. case MYSQL_TYPE_SET:
  171. case MYSQL_TYPE_ENUM:
  172. *sqltype = DB_SQL_TYPE_CHARACTER;
  173. break;
  174. case MYSQL_TYPE_BLOB:
  175. if ( field->flags & BINARY_FLAG )
  176. {
  177. *sqltype = DB_SQL_TYPE_UNKNOWN;
  178. }
  179. else
  180. {
  181. *sqltype = DB_SQL_TYPE_TEXT;
  182. }
  183. break;
  184. case MYSQL_TYPE_GEOMETRY:
  185. case MYSQL_TYPE_NULL:
  186. *sqltype = DB_SQL_TYPE_UNKNOWN;
  187. break;
  188. default:
  189. *sqltype = DB_SQL_TYPE_UNKNOWN;
  190. }
  191. return;
  192. }