fetch.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "globals.h"
  18. #include "proto.h"
  19. int db__driver_fetch(dbCursor * cn, int position, int *more)
  20. {
  21. cursor *c;
  22. dbToken token;
  23. dbTable *table;
  24. dbColumn *column;
  25. dbValue *value;
  26. int col, ncols;
  27. int htype, sqltype, ctype;
  28. int dbfrow, dbfcol;
  29. /* get cursor token */
  30. token = db_get_cursor_token(cn);
  31. /* get the cursor by its token */
  32. if (!(c = (cursor *) db_find_token(token))) {
  33. db_error("cursor not found");
  34. return DB_FAILED;
  35. }
  36. /* fetch on position */
  37. switch (position) {
  38. case DB_NEXT:
  39. c->cur++;
  40. break;
  41. case DB_CURRENT:
  42. break;
  43. case DB_PREVIOUS:
  44. c->cur--;
  45. break;
  46. case DB_FIRST:
  47. c->cur = 0;
  48. break;
  49. case DB_LAST:
  50. c->cur = c->nrows - 1;
  51. break;
  52. };
  53. if ((c->cur >= c->nrows) || (c->cur < 0)) {
  54. *more = 0;
  55. return DB_OK;
  56. }
  57. *more = 1;
  58. /* get the data out of the descriptor into the table */
  59. table = db_get_cursor_table(cn);
  60. ncols = db_get_table_number_of_columns(table);
  61. dbfrow = c->set[c->cur];
  62. for (col = 1; col <= ncols; col++) {
  63. dbfcol = c->cols[col - 1];
  64. column = db_get_table_column(table, col - 1);
  65. value = db_get_column_value(column);
  66. db_free_string(&value->s);
  67. sqltype = db_get_column_sqltype(column);
  68. ctype = db_sqltype_to_Ctype(sqltype);
  69. htype = db_get_column_host_type(column);
  70. if (db.tables[c->table].rows[dbfrow].values[dbfcol].is_null) {
  71. db_set_value_null(value);
  72. }
  73. else {
  74. db_set_value_not_null(value);
  75. switch (ctype) {
  76. case DB_C_TYPE_STRING:
  77. db_set_string(&(value->s),
  78. db.tables[c->table].rows[dbfrow].values[dbfcol].
  79. c);
  80. break;
  81. case DB_C_TYPE_INT:
  82. value->i = db.tables[c->table].rows[dbfrow].values[dbfcol].i;
  83. break;
  84. case DB_C_TYPE_DOUBLE:
  85. value->d = db.tables[c->table].rows[dbfrow].values[dbfcol].d;
  86. break;
  87. }
  88. }
  89. }
  90. return DB_OK;
  91. }
  92. int db__driver_get_num_rows(dbCursor * cn)
  93. {
  94. cursor *c;
  95. dbToken token;
  96. /* get cursor token */
  97. token = db_get_cursor_token(cn);
  98. /* get the cursor by its token */
  99. if (!(c = (cursor *) db_find_token(token))) {
  100. db_error("cursor not found");
  101. return DB_FAILED;
  102. }
  103. return (c->nrows);
  104. }