d_fetch.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*!
  2. * \file db/dbmi_driver/d_fetch.c
  3. *
  4. * \brief DBMI Library (driver) - fetch data
  5. *
  6. * (C) 1999-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author Joel Jones (CERL/UIUC), Radim Blazek
  13. */
  14. #include <grass/dbmi.h>
  15. #include "macros.h"
  16. #include "dbstubs.h"
  17. static int valid_cursor(dbCursor * cursor, int position);
  18. /*!
  19. \brief Fetch data
  20. \return DB_OK on success
  21. \return DB_FAILED on failure
  22. */
  23. int db_d_fetch(void)
  24. {
  25. dbToken token;
  26. dbCursor *cursor;
  27. int stat;
  28. int more;
  29. int position;
  30. /* get the arg(s) */
  31. DB_RECV_TOKEN(&token);
  32. DB_RECV_INT(&position);
  33. cursor = (dbCursor *) db_find_token(token);
  34. if (!valid_cursor(cursor, position)) {
  35. DB_SEND_FAILURE();
  36. return DB_FAILED;
  37. }
  38. /* call the procedure */
  39. stat = db_driver_fetch(cursor, position, &more);
  40. /* send the return code */
  41. if (stat != DB_OK) {
  42. DB_SEND_FAILURE();
  43. return DB_OK;
  44. }
  45. DB_SEND_SUCCESS();
  46. /* results */
  47. DB_SEND_INT(more);
  48. if (more) {
  49. DB_SEND_TABLE_DATA(cursor->table);
  50. }
  51. return DB_OK;
  52. }
  53. static int valid_cursor(dbCursor * cursor, int position)
  54. {
  55. if (cursor == NULL)
  56. return 0;
  57. if (!db_test_cursor_type_fetch(cursor)) {
  58. db_error("not a fetchable cursor");
  59. return 0;
  60. }
  61. if (position != DB_NEXT && !db_test_cursor_mode_scroll(cursor)) {
  62. db_error("not a scrollable cursor");
  63. return 0;
  64. }
  65. return 1;
  66. }