describe.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*****************************************************************************
  2. *
  3. * MODULE: DBF driver
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: Simple driver for reading and writing dbf files
  8. *
  9. * COPYRIGHT: (C) 2000 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/shapefil.h>
  19. #include <grass/glocale.h>
  20. #include "globals.h"
  21. #include "proto.h"
  22. int db__driver_describe_table(dbString * table_name, dbTable ** table)
  23. {
  24. int tab;
  25. char *name;
  26. name = db_get_string(table_name);
  27. tab = find_table(name);
  28. if (tab == -1) {
  29. db_d_append_error(_("Table '%s' doesn't exist"),
  30. db_get_string(table_name));
  31. db_d_report_error();
  32. return DB_FAILED;
  33. }
  34. describe_table(tab, NULL, 0, table);
  35. return DB_OK;
  36. }
  37. /* scols is array of indexes selected columns or null,
  38. * if nscols == 0 => describe all columns */
  39. int describe_table(int tab, int *scols, int nscols, dbTable ** table)
  40. {
  41. int i, col, ncols, dbtype, precision, scale;
  42. dbColumn *column;
  43. COLUMN *dcol;
  44. load_table_head(tab);
  45. ncols = db.tables[tab].ncols;
  46. if (nscols > 0)
  47. ncols = nscols;
  48. if (!(*table = db_alloc_table(ncols))) {
  49. return DB_FAILED;
  50. }
  51. for (i = 0; i < ncols; i++) {
  52. if (nscols > 0)
  53. col = scols[i];
  54. else
  55. col = i;
  56. dcol = &(db.tables[tab].cols[col]);
  57. column = db_get_table_column(*table, i);
  58. db_set_column_name(column, dcol->name);
  59. db_set_column_length(column, dcol->width);
  60. db_set_column_host_type(column, dcol->type);
  61. switch (dcol->type) {
  62. case DBF_INT:
  63. dbtype = DB_SQL_TYPE_INTEGER;
  64. precision = dcol->width - 1; /* one char for sign */
  65. scale = 0;
  66. break;
  67. case DBF_DOUBLE:
  68. dbtype = DB_SQL_TYPE_DOUBLE_PRECISION;
  69. precision = dcol->width - 2; /* one char for sign second for decimal point */
  70. scale = dcol->decimals;
  71. break;
  72. case DBF_CHAR:
  73. dbtype = DB_SQL_TYPE_CHARACTER;
  74. precision = 0;
  75. scale = 0;
  76. break;
  77. default:
  78. dbtype = DB_SQL_TYPE_UNKNOWN;
  79. break;
  80. }
  81. db_set_column_sqltype(column, dbtype);
  82. /* precision is number of digits */
  83. db_set_column_precision(column, precision);
  84. /* scale is number of digits to the right of decimal point */
  85. db_set_column_scale(column, scale);
  86. db_set_column_null_allowed(column);
  87. db_set_column_has_undefined_default_value(column);
  88. db_unset_column_use_default_value(column);
  89. db_set_column_select_priv_granted(column);
  90. if (db.tables[tab].write)
  91. db_set_column_update_priv_granted(column);
  92. else
  93. db_set_column_update_priv_not_granted(column);
  94. }
  95. /* set the table name */
  96. db_set_table_name(*table, db.tables[tab].name);
  97. /* set the table description */
  98. db_set_table_description(*table, "");
  99. if (db.tables[tab].write) {
  100. db_set_table_delete_priv_granted(*table);
  101. db_set_table_insert_priv_granted(*table);
  102. }
  103. else {
  104. db_set_table_delete_priv_not_granted(*table);
  105. db_set_table_insert_priv_not_granted(*table);
  106. }
  107. return DB_OK;
  108. }