describe.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <grass/dbmi.h>
  2. #include <grass/datetime.h>
  3. #include "globals.h"
  4. #include "proto.h"
  5. #include <grass/glocale.h>
  6. int db__driver_describe_table(dbString * table_name, dbTable ** table)
  7. {
  8. dbString sql;
  9. PGresult *res;
  10. db_init_string(&sql);
  11. db_set_string(&sql, "select * from ");
  12. db_append_string(&sql, db_get_string(table_name));
  13. db_append_string(&sql, " where 1 = 0");
  14. res = PQexec(pg_conn, db_get_string(&sql));
  15. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
  16. append_error(db_get_string(&sql));
  17. append_error("\n");
  18. append_error(PQerrorMessage(pg_conn));
  19. report_error();
  20. PQclear(res);
  21. return DB_FAILED;
  22. }
  23. if (describe_table(res, table, NULL) == DB_FAILED) {
  24. append_error("Cannot describe table\n");
  25. report_error();
  26. PQclear(res);
  27. return DB_FAILED;
  28. }
  29. PQclear(res);
  30. return DB_OK;
  31. }
  32. /* describe table, if c is not NULL cur->cols and cur->ncols is also set */
  33. int describe_table(PGresult * res, dbTable ** table, cursor * c)
  34. {
  35. int i, ncols, kcols;
  36. int pgtype, gpgtype;
  37. char *fname;
  38. int sqltype, fsize, precision, scale;
  39. dbColumn *column;
  40. G_debug(3, "describe_table()");
  41. ncols = PQnfields(res);
  42. /* Count columns of known type */
  43. kcols = 0;
  44. for (i = 0; i < ncols; i++) {
  45. get_column_info(res, i, &pgtype, &gpgtype, &sqltype, &fsize);
  46. if (sqltype == DB_SQL_TYPE_UNKNOWN)
  47. continue;
  48. kcols++; /* known types */
  49. }
  50. G_debug(3, "kcols = %d", kcols);
  51. if (!(*table = db_alloc_table(kcols))) {
  52. return DB_FAILED;
  53. }
  54. if (c) {
  55. c->ncols = kcols;
  56. c->cols = (int *)G_malloc(kcols * sizeof(int));
  57. }
  58. /* set the table name */
  59. /* TODO */
  60. db_set_table_name(*table, "");
  61. /* set the table description */
  62. db_set_table_description(*table, "");
  63. /* TODO */
  64. /*
  65. db_set_table_delete_priv_granted (*table);
  66. db_set_table_insert_priv_granted (*table);
  67. db_set_table_delete_priv_not_granted (*table);
  68. db_set_table_insert_priv_not_granted (*table);
  69. */
  70. kcols = 0;
  71. for (i = 0; i < ncols; i++) {
  72. fname = PQfname(res, i);
  73. get_column_info(res, i, &pgtype, &gpgtype, &sqltype, &fsize);
  74. G_debug(3,
  75. "col: %s, kcols %d, pgtype : %d, gpgtype : %d, sqltype %d, fsize : %d",
  76. fname, kcols, pgtype, gpgtype, sqltype, fsize);
  77. /* PG types defined in globals.h (and pg_type.h) */
  78. if (sqltype == DB_SQL_TYPE_UNKNOWN) {
  79. if (gpgtype == PG_TYPE_POSTGIS_GEOM) {
  80. G_warning(_("PostgreSQL driver: PostGIS column '%s', type 'geometry' "
  81. "will not be converted"),
  82. fname);
  83. continue;
  84. }
  85. else {
  86. /* Warn, ignore and continue */
  87. G_warning(_("PostgreSQL driver: column '%s', type %d is not supported"),
  88. fname, pgtype);
  89. continue;
  90. }
  91. }
  92. if (gpgtype == PG_TYPE_INT8)
  93. G_warning(_("column '%s' : type int8 (bigint) is stored as integer (4 bytes) "
  94. "some data may be damaged"), fname);
  95. if (gpgtype == PG_TYPE_VARCHAR && fsize < 0) {
  96. G_warning(_("column '%s' : type character varying is stored as varchar(250) "
  97. "some data may be lost"), fname);
  98. fsize = 250;
  99. }
  100. if (gpgtype == PG_TYPE_BOOL)
  101. G_warning(_("column '%s' : type bool (boolean) is stored as char(1), values: 0 (false), "
  102. "1 (true)"), fname);
  103. column = db_get_table_column(*table, kcols);
  104. db_set_column_name(column, fname);
  105. db_set_column_length(column, fsize);
  106. db_set_column_host_type(column, gpgtype);
  107. db_set_column_sqltype(column, sqltype);
  108. /* TODO */
  109. precision = 0;
  110. scale = 0;
  111. /*
  112. db_set_column_precision (column, precision);
  113. db_set_column_scale (column, scale);
  114. */
  115. /* TODO */
  116. db_set_column_null_allowed(column);
  117. db_set_column_has_undefined_default_value(column);
  118. db_unset_column_use_default_value(column);
  119. /* TODO */
  120. /*
  121. db_set_column_select_priv_granted (column);
  122. db_set_column_update_priv_granted (column);
  123. db_set_column_update_priv_not_granted (column);
  124. */
  125. if (c) {
  126. c->cols[kcols] = i;
  127. }
  128. kcols++;
  129. }
  130. return DB_OK;
  131. }
  132. int get_column_info(PGresult * res, int col, int *pgtype, int *gpgtype,
  133. int *sqltype, int *size)
  134. {
  135. *pgtype = (int)PQftype(res, col);
  136. *gpgtype = get_gpg_type(*pgtype);
  137. /* Convert internal type to PG_TYPE_* */
  138. /* TODO: we should load field names from pg_type table
  139. instead of using copy of #defines */
  140. switch (*gpgtype) {
  141. case PG_TYPE_BIT:
  142. case PG_TYPE_INT2:
  143. case PG_TYPE_INT4:
  144. case PG_TYPE_INT8:
  145. case PG_TYPE_SERIAL:
  146. case PG_TYPE_OID:
  147. *sqltype = DB_SQL_TYPE_INTEGER;
  148. *size = PQfsize(res, col);
  149. break;
  150. case PG_TYPE_CHAR:
  151. case PG_TYPE_BPCHAR:
  152. case PG_TYPE_VARCHAR:
  153. *sqltype = DB_SQL_TYPE_CHARACTER;
  154. *size = PQfmod(res, col) - 4; /* Looks strange but works, something better? */
  155. break;
  156. case PG_TYPE_TEXT:
  157. *sqltype = DB_SQL_TYPE_TEXT;
  158. break;
  159. case PG_TYPE_FLOAT4:
  160. case PG_TYPE_FLOAT8:
  161. case PG_TYPE_NUMERIC:
  162. *sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
  163. *size = PQfsize(res, col);
  164. break;
  165. /* I'm not sure if text length is correct for size */
  166. case PG_TYPE_DATE:
  167. *sqltype = DB_SQL_TYPE_DATE;
  168. *size = 10; /* YYYY-MM-DD */
  169. break;
  170. case PG_TYPE_TIME:
  171. *sqltype = DB_SQL_TYPE_TIME;
  172. *size = 8; /* HH:MM:SS */
  173. break;
  174. case PG_TYPE_TIMESTAMP:
  175. *sqltype = DB_SQL_TYPE_TIMESTAMP;
  176. *size = 22; /* YYYY-MM-DD HH:MM:SS+TZ */
  177. break;
  178. case PG_TYPE_BOOL:
  179. *sqltype = DB_SQL_TYPE_CHARACTER;
  180. *size = 1;
  181. break;
  182. default:
  183. *sqltype = DB_SQL_TYPE_UNKNOWN;
  184. *size = 0;
  185. }
  186. return 0;
  187. }
  188. /* for given internal postgres type returns GRASS Postgres type (one of PG_TYPE_*) */
  189. int get_gpg_type(int pgtype)
  190. {
  191. int i;
  192. for (i = 0; i < pg_ntypes; i++) {
  193. if (pg_types[i][0] == pgtype)
  194. return pg_types[i][1];
  195. }
  196. return PG_TYPE_UNKNOWN;
  197. }