cursor.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*!
  2. \file db/drivers/cursor.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 <stdio.h>
  11. #include <grass/gis.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 Close cursor
  19. \param dbc pointer to dbCursor to be closed
  20. \return DB_OK on success
  21. \return DB_FAILED on failure
  22. */
  23. int db__driver_close_cursor(dbCursor * dbc)
  24. {
  25. cursor *c;
  26. G_debug(3, "db_driver_close_cursor()");
  27. /* get my cursor via the dbc token */
  28. c = (cursor *) db_find_token(db_get_cursor_token(dbc));
  29. if (c == NULL)
  30. return DB_FAILED;
  31. /* free_cursor(cursor) */
  32. free_cursor(c);
  33. G_debug(3, "Cursor closed");
  34. return DB_OK;
  35. }
  36. /*!
  37. \brief Allocate cursor
  38. \return pointer to cursor structure
  39. \return NULL on error
  40. */
  41. cursor *alloc_cursor()
  42. {
  43. cursor *c;
  44. /* allocate the cursor */
  45. c = (cursor *) db_malloc(sizeof(cursor));
  46. if (c == NULL) {
  47. db_d_append_error(_("Unable to allocate cursor"));
  48. return NULL;
  49. }
  50. G_zero(c, sizeof(cursor));
  51. /* tokenize it */
  52. c->token = db_new_token(c);
  53. if (c->token < 0) {
  54. db_d_append_error(_("Unable to add new token"));
  55. return NULL;
  56. }
  57. return c;
  58. }
  59. /*!
  60. \brief Free cursor structure (destroy OGR feature and release OGR layer)
  61. \param c pointer to cursor
  62. */
  63. void free_cursor(cursor * c)
  64. {
  65. if (c->hFeature)
  66. OGR_F_Destroy(c->hFeature);
  67. if (c->hLayer)
  68. OGR_DS_ReleaseResultSet(hDs, c->hLayer);
  69. G_free(c->cols);
  70. G_free(c);
  71. db_drop_token(c->token);
  72. }