db.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*!
  2. \file db/drivers/db.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 <stdlib.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/glocale.h>
  15. #include <ogr_api.h>
  16. #include "globals.h"
  17. #include "proto.h"
  18. /*!
  19. \brief Open database (OGR datasource)
  20. \param handle pointer to dbHandle (db name and schema)
  21. \return DB_OK on success
  22. \return DB_FAILED on failure
  23. */
  24. int db__driver_open_database(dbHandle * handle)
  25. {
  26. const char *name;
  27. dbConnection connection;
  28. db_get_connection(&connection);
  29. name = db_get_handle_dbname(handle);
  30. /* if name is empty use connection.databaseName */
  31. if (strlen(name) == 0)
  32. name = connection.databaseName;
  33. G_debug(3, "db_driver_open_database() name = '%s'", name);
  34. OGRRegisterAll();
  35. /* try read-write access */
  36. hDs = OGROpen(name, TRUE, NULL);
  37. if (hDs == NULL) {
  38. /* try read-only access */
  39. hDs = OGROpen(name, FALSE, NULL);
  40. if (hDs)
  41. G_important_message(_("Had to open data source read-only"));
  42. }
  43. if (hDs == NULL) {
  44. db_d_append_error(_("Unable to open OGR data source"));
  45. db_d_report_error();
  46. return DB_FAILED;
  47. }
  48. G_debug(3, "Datasource opened");
  49. return DB_OK;
  50. }
  51. /*!
  52. \brief Close open database
  53. \return DB_OK on success
  54. \return DB_FAILED on failure
  55. */
  56. int db__driver_close_database()
  57. {
  58. G_debug(3, "db_driver_close_database()");
  59. OGR_DS_Destroy(hDs);
  60. G_debug(3, "Database closed");
  61. return DB_OK;
  62. }