describe.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*!
  2. \file db/drivers/describe.c
  3. \brief Low level OGR SQL driver
  4. (C) 2004-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Radim Blazek
  8. \author Some updates by Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/gis.h>
  11. #include <grass/datetime.h>
  12. #include <grass/dbmi.h>
  13. #include <grass/glocale.h>
  14. #include <ogr_api.h>
  15. #include "globals.h"
  16. #include "proto.h"
  17. /*!
  18. \brief Describe table using driver
  19. \param table_name table name (as dbString)
  20. \param table[out] pointer to dbTable
  21. \return DB_OK on success
  22. \return DB_FAILED on failure
  23. */
  24. int db__driver_describe_table(dbString * table_name, dbTable ** table)
  25. {
  26. int i, nlayers;
  27. OGRLayerH hLayer = NULL;
  28. OGRFeatureDefnH hFeatureDefn;
  29. /* get number of OGR layers in the datasource */
  30. nlayers = OGR_DS_GetLayerCount(hDs);
  31. /* find OGR layer */
  32. for (i = 0; i < nlayers; i++) {
  33. hLayer = OGR_DS_GetLayer(hDs, i);
  34. hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
  35. if (G_strcasecmp
  36. ((char *)OGR_FD_GetName(hFeatureDefn),
  37. db_get_string(table_name)) == 0) {
  38. break;
  39. }
  40. hLayer = NULL;
  41. }
  42. if (hLayer == NULL) {
  43. db_d_append_error(_("OGR layer <%s> does not exist\n"),
  44. db_get_string(table_name));
  45. db_d_report_error();
  46. return DB_FAILED;
  47. }
  48. G_debug(3, "->>");
  49. if (describe_table(hLayer, table, NULL) == DB_FAILED) {
  50. db_d_append_error(_("Unable to describe table\n"));
  51. db_d_report_error();
  52. return DB_FAILED;
  53. }
  54. return DB_OK;
  55. }
  56. /*!
  57. \brief Describe table
  58. If c is not NULL cur->cols and cur->ncols is also set
  59. cursor may be null
  60. \param hLayer OGR layer
  61. \param[put] table pointer to dbTable
  62. \param c pointer to cursor
  63. */
  64. int describe_table(OGRLayerH hLayer, dbTable **table, cursor *c)
  65. {
  66. int i, ncols, kcols, col;
  67. dbColumn *column;
  68. OGRFeatureDefnH hFeatureDefn;
  69. OGRFieldDefnH hFieldDefn;
  70. const char *fieldName, *fidcol;
  71. int ogrType;
  72. int *cols;
  73. G_debug(1, "describe_table()");
  74. hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
  75. ncols = OGR_FD_GetFieldCount(hFeatureDefn);
  76. G_debug(2, " ncols = %d (without fid column)", ncols);
  77. /* for some formats fid column is not defined, e.g. ESRI Shapefile */
  78. fidcol = OGR_L_GetFIDColumn(hLayer);
  79. G_debug(2, " fidcol = %s", fidcol);
  80. /* identify known columns */
  81. cols = (int *) G_malloc(ncols * sizeof(int));
  82. kcols = 0;
  83. for(i = 0; i < ncols; i++) {
  84. hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
  85. ogrType = OGR_Fld_GetType(hFieldDefn);
  86. fieldName = OGR_Fld_GetNameRef(hFieldDefn);
  87. if (ogrType != OFTInteger && ogrType != OFTReal &&
  88. ogrType != OFTString && ogrType != OFTDate &&
  89. ogrType != OFTTime && ogrType != OFTDateTime ) {
  90. G_warning(_("OGR driver: column '%s', OGR type %d is not supported"),
  91. fieldName, ogrType);
  92. cols[i] = 0;
  93. }
  94. else {
  95. cols[i] = 1;
  96. kcols++;
  97. }
  98. }
  99. if (*fidcol)
  100. kcols++;
  101. G_debug(2, " kcols = %d (including fid column)", kcols);
  102. /* allocate dbTable */
  103. if (!(*table = db_alloc_table(kcols))) {
  104. return DB_FAILED;
  105. }
  106. /* set the table name */
  107. /* TODO */
  108. db_set_table_name(*table, "");
  109. /* set the table description */
  110. db_set_table_description(*table, "");
  111. /* TODO */
  112. /*
  113. db_set_table_delete_priv_granted (*table);
  114. db_set_table_insert_priv_granted (*table);
  115. db_set_table_delete_priv_not_granted (*table);
  116. db_set_table_insert_priv_not_granted (*table);
  117. */
  118. if (*fidcol) {
  119. column = db_get_table_column(*table, 0);
  120. db_set_column_host_type(column, OFTInteger);
  121. db_set_column_sqltype(column, DB_SQL_TYPE_INTEGER);
  122. db_set_column_name(column, fidcol);
  123. db_set_column_length(column, 11); /* ??? */
  124. db_set_column_precision(column, 0);
  125. col = 1;
  126. }
  127. else {
  128. col = 0;
  129. }
  130. for (i = 0; i < ncols; i++, col++) {
  131. int sqlType;
  132. int size, precision, scale;
  133. hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
  134. ogrType = OGR_Fld_GetType(hFieldDefn);
  135. fieldName = OGR_Fld_GetNameRef(hFieldDefn);
  136. if (!(cols[i])) {
  137. continue; /* unknown type */
  138. }
  139. switch (ogrType) {
  140. case OFTInteger:
  141. sqlType = DB_SQL_TYPE_INTEGER;
  142. size = OGR_Fld_GetWidth(hFieldDefn); /* OK ? */
  143. precision = 0;
  144. break;
  145. case OFTReal:
  146. sqlType = DB_SQL_TYPE_DOUBLE_PRECISION;
  147. size = OGR_Fld_GetWidth(hFieldDefn); /* OK ? */
  148. precision = OGR_Fld_GetPrecision(hFieldDefn);
  149. break;
  150. case OFTString:
  151. case OFTDate:
  152. case OFTTime:
  153. case OFTDateTime:
  154. sqlType = DB_SQL_TYPE_CHARACTER;
  155. size = OGR_Fld_GetWidth(hFieldDefn);
  156. if (size == 0) {
  157. G_warning(_("column '%s', type 'string': unknown width -> stored as varchar(250) "
  158. "some data may be lost"), fieldName);
  159. size = 250;
  160. }
  161. precision = 0;
  162. break;
  163. default:
  164. G_warning(_("Unknown type"));
  165. break;
  166. }
  167. G_debug(3, " %d: field %d : ogrType = %d, name = %s, size=%d precision=%d",
  168. i, col, ogrType, fieldName, size, precision);
  169. column = db_get_table_column(*table, col);
  170. db_set_column_host_type(column, ogrType);
  171. db_set_column_sqltype(column, sqlType);
  172. db_set_column_name(column, fieldName);
  173. db_set_column_length(column, size);
  174. db_set_column_precision(column, precision);
  175. /* TODO */
  176. scale = 0;
  177. /*
  178. db_set_column_scale (column, scale);
  179. */
  180. /* TODO */
  181. db_set_column_null_allowed(column);
  182. db_set_column_has_undefined_default_value(column);
  183. db_unset_column_use_default_value(column);
  184. /* TODO */
  185. /*
  186. db_set_column_select_priv_granted (column);
  187. db_set_column_update_priv_granted (column);
  188. db_set_column_update_priv_not_granted (column);
  189. */
  190. }
  191. if (c) {
  192. c->cols = cols;
  193. c->ncols = ncols;
  194. }
  195. else {
  196. G_free(cols);
  197. }
  198. return DB_OK;
  199. }