describe.c 5.9 KB

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