describe.c 6.1 KB

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