describe.c 6.1 KB

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