describe.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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(_("pg driver: PostGIS column '%s', type 'geometry' will not be converted"),
  81. fname);
  82. continue;
  83. }
  84. else {
  85. /* Warn, ignore and continue */
  86. G_warning(_("pg driver: column '%s', type %d is not supported"),
  87. fname, pgtype);
  88. continue;
  89. }
  90. }
  91. if (gpgtype == PG_TYPE_INT8)
  92. G_warning(_("column '%s' : type int8 (bigint) is stored as integer (4 bytes) "
  93. "some data may be damaged"), fname);
  94. if (gpgtype == PG_TYPE_VARCHAR && fsize < 0) {
  95. G_warning(_("column '%s' : type character varying is stored as varchar(250) "
  96. "some data may be lost"), fname);
  97. fsize = 250;
  98. }
  99. if (gpgtype == PG_TYPE_BOOL)
  100. G_warning(_("column '%s' : type bool (boolean) is stored as char(1), values: 0 (false), "
  101. "1 (true)"), fname);
  102. column = db_get_table_column(*table, kcols);
  103. db_set_column_name(column, fname);
  104. db_set_column_length(column, fsize);
  105. db_set_column_host_type(column, gpgtype);
  106. db_set_column_sqltype(column, sqltype);
  107. /* TODO */
  108. precision = 0;
  109. scale = 0;
  110. /*
  111. db_set_column_precision (column, precision);
  112. db_set_column_scale (column, scale);
  113. */
  114. /* TODO */
  115. db_set_column_null_allowed(column);
  116. db_set_column_has_undefined_default_value(column);
  117. db_unset_column_use_default_value(column);
  118. /* TODO */
  119. /*
  120. db_set_column_select_priv_granted (column);
  121. db_set_column_update_priv_granted (column);
  122. db_set_column_update_priv_not_granted (column);
  123. */
  124. if (c) {
  125. c->cols[kcols] = i;
  126. }
  127. kcols++;
  128. }
  129. return DB_OK;
  130. }
  131. int get_column_info(PGresult * res, int col, int *pgtype, int *gpgtype,
  132. int *sqltype, int *size)
  133. {
  134. *pgtype = (int)PQftype(res, col);
  135. *gpgtype = get_gpg_type(*pgtype);
  136. /* Convert internal type to PG_TYPE_* */
  137. /* TODO: we should load field names from pg_type table instead of using copy of #defines */
  138. switch (*gpgtype) {
  139. case PG_TYPE_BIT:
  140. case PG_TYPE_INT2:
  141. case PG_TYPE_INT4:
  142. case PG_TYPE_INT8:
  143. case PG_TYPE_SERIAL:
  144. case PG_TYPE_OID:
  145. *sqltype = DB_SQL_TYPE_INTEGER;
  146. *size = PQfsize(res, col);
  147. break;
  148. case PG_TYPE_CHAR:
  149. case PG_TYPE_BPCHAR:
  150. case PG_TYPE_VARCHAR:
  151. *sqltype = DB_SQL_TYPE_CHARACTER;
  152. *size = PQfmod(res, col) - 4; /* Looks strange but works, something better? */
  153. break;
  154. case PG_TYPE_TEXT:
  155. *sqltype = DB_SQL_TYPE_TEXT;
  156. break;
  157. case PG_TYPE_FLOAT4:
  158. case PG_TYPE_FLOAT8:
  159. case PG_TYPE_NUMERIC:
  160. *sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
  161. *size = PQfsize(res, col);
  162. break;
  163. /* I'm not sure if text length is correct for size */
  164. case PG_TYPE_DATE:
  165. *sqltype = DB_SQL_TYPE_DATE;
  166. *size = 10; /* YYYY-MM-DD */
  167. break;
  168. case PG_TYPE_TIME:
  169. *sqltype = DB_SQL_TYPE_TIME;
  170. *size = 8; /* HH:MM:SS */
  171. break;
  172. case PG_TYPE_TIMESTAMP:
  173. *sqltype = DB_SQL_TYPE_TIMESTAMP;
  174. *size = 22; /* YYYY-MM-DD HH:MM:SS+TZ */
  175. break;
  176. case PG_TYPE_BOOL:
  177. *sqltype = DB_SQL_TYPE_CHARACTER;
  178. *size = 1;
  179. break;
  180. default:
  181. *sqltype = DB_SQL_TYPE_UNKNOWN;
  182. *size = 0;
  183. }
  184. return 0;
  185. }
  186. /* for given internal postgres type returns GRASS Postgres type (one of PG_TYPE_*) */
  187. int get_gpg_type(int pgtype)
  188. {
  189. int i;
  190. for (i = 0; i < pg_ntypes; i++) {
  191. if (pg_types[i][0] == pgtype)
  192. return pg_types[i][1];
  193. }
  194. return PG_TYPE_UNKNOWN;
  195. }