describe.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*****************************************************************************
  2. *
  3. * MODULE: OGR driver
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: DB driver for OGR sources
  8. *
  9. * COPYRIGHT: (C) 2004 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <grass/dbmi.h>
  17. #include <grass/datetime.h>
  18. #include <grass/gis.h>
  19. #include "ogr_api.h"
  20. #include "globals.h"
  21. #include "proto.h"
  22. #include <grass/glocale.h>
  23. int db__driver_describe_table(dbString * table_name, dbTable ** table)
  24. {
  25. int i, nlayers;
  26. OGRLayerH hLayer = NULL;
  27. OGRFeatureDefnH hFeatureDefn;
  28. /* Find data source */
  29. nlayers = OGR_DS_GetLayerCount(hDs);
  30. for (i = 0; i < nlayers; i++) {
  31. hLayer = OGR_DS_GetLayer(hDs, i);
  32. hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
  33. if (G_strcasecmp
  34. ((char *)OGR_FD_GetName(hFeatureDefn),
  35. db_get_string(table_name)) == 0) {
  36. break;
  37. }
  38. hLayer = NULL;
  39. }
  40. if (hLayer == NULL) {
  41. append_error("Table '%s' does not exist\n",
  42. db_get_string(table_name));
  43. report_error();
  44. return DB_FAILED;
  45. }
  46. G_debug(3, "->>");
  47. if (describe_table(hLayer, table, NULL) == DB_FAILED) {
  48. append_error("Cannot describe table\n");
  49. report_error();
  50. return DB_FAILED;
  51. }
  52. return DB_OK;
  53. }
  54. /* describe table, if c is not NULL cur->cols and cur->ncols is also set
  55. * cursor may be null
  56. */
  57. int describe_table(OGRLayerH hLayer, dbTable ** table, cursor * c)
  58. {
  59. int i, ncols, kcols;
  60. dbColumn *column;
  61. OGRFeatureDefnH hFeatureDefn;
  62. OGRFieldDefnH hFieldDefn;
  63. const char *fieldName;
  64. int ogrType;
  65. int *cols;
  66. G_debug(3, "describe_table()");
  67. hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
  68. ncols = OGR_FD_GetFieldCount(hFeatureDefn);
  69. G_debug(3, "ncols = %d", ncols);
  70. /* Identify known columns */
  71. cols = (int *)G_malloc(ncols * sizeof(int));
  72. kcols = 0;
  73. for (i = 0; i < ncols; i++) {
  74. hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
  75. ogrType = OGR_Fld_GetType(hFieldDefn);
  76. OGR_Fld_GetNameRef(hFieldDefn);
  77. fieldName = OGR_Fld_GetNameRef(hFieldDefn);
  78. if (ogrType != OFTInteger && ogrType != OFTReal &&
  79. ogrType != OFTString) {
  80. G_warning(_("OGR driver: column '%s', OGR type %d is not supported"),
  81. fieldName, ogrType);
  82. cols[i] = 0;
  83. }
  84. else {
  85. cols[i] = 1;
  86. kcols++;
  87. }
  88. }
  89. if (!(*table = db_alloc_table(kcols))) {
  90. return DB_FAILED;
  91. }
  92. /* set the table name */
  93. /* TODO */
  94. db_set_table_name(*table, "");
  95. /* set the table description */
  96. db_set_table_description(*table, "");
  97. /* TODO */
  98. /*
  99. db_set_table_delete_priv_granted (*table);
  100. db_set_table_insert_priv_granted (*table);
  101. db_set_table_delete_priv_not_granted (*table);
  102. db_set_table_insert_priv_not_granted (*table);
  103. */
  104. for (i = 0; i < ncols; i++) {
  105. int sqlType;
  106. int size, precision, scale;
  107. if (!(cols[i]))
  108. continue; /* unknown type */
  109. hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
  110. ogrType = OGR_Fld_GetType(hFieldDefn);
  111. fieldName = OGR_Fld_GetNameRef(hFieldDefn);
  112. G_debug(3, "field %d : ogrType = %d, name = %s", i, ogrType,
  113. fieldName);
  114. switch (ogrType) {
  115. case OFTInteger:
  116. sqlType = DB_SQL_TYPE_INTEGER;
  117. size = OGR_Fld_GetWidth(hFieldDefn); /* OK ? */
  118. precision = 0;
  119. break;
  120. case OFTReal:
  121. sqlType = DB_SQL_TYPE_DOUBLE_PRECISION;
  122. size = OGR_Fld_GetWidth(hFieldDefn); /* OK ? */
  123. precision = OGR_Fld_GetPrecision(hFieldDefn);
  124. break;
  125. case OFTString:
  126. sqlType = DB_SQL_TYPE_CHARACTER;
  127. size = OGR_Fld_GetWidth(hFieldDefn);
  128. if (size == 0) {
  129. G_warning(_("column '%s', type 'string': unknown width -> stored as varchar(250) "
  130. "some data may be lost"), fieldName);
  131. size = 250;
  132. }
  133. precision = 0;
  134. break;
  135. default:
  136. G_warning(_("Unknown type"));
  137. break;
  138. }
  139. column = db_get_table_column(*table, i);
  140. db_set_column_host_type(column, ogrType);
  141. db_set_column_sqltype(column, sqlType);
  142. db_set_column_name(column, fieldName);
  143. db_set_column_length(column, size);
  144. db_set_column_precision(column, precision);
  145. /* TODO */
  146. scale = 0;
  147. /*
  148. db_set_column_scale (column, scale);
  149. */
  150. /* TODO */
  151. db_set_column_null_allowed(column);
  152. db_set_column_has_undefined_default_value(column);
  153. db_unset_column_use_default_value(column);
  154. /* TODO */
  155. /*
  156. db_set_column_select_priv_granted (column);
  157. db_set_column_update_priv_granted (column);
  158. db_set_column_update_priv_not_granted (column);
  159. */
  160. }
  161. if (c) {
  162. c->cols = cols;
  163. c->ncols = ncols;
  164. }
  165. else {
  166. G_free(cols);
  167. }
  168. return DB_OK;
  169. }