describe.c 5.7 KB

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