delete_tab.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*!
  2. * \file db/dbmi_client/delete_tab.c
  3. *
  4. * \brief DBMI Library (client) - delete table
  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 <grass/glocale.h>
  16. #include "macros.h"
  17. /*!
  18. \brief Delete table
  19. \param drv driver name
  20. \param dbname database name
  21. \param tblname table name
  22. \return DB_OK on success
  23. \return DB_FAILED on failure
  24. */
  25. int db_delete_table(const char *drvname, const char *dbname, const char *tblname)
  26. {
  27. dbDriver *driver;
  28. dbHandle handle;
  29. dbString sql;
  30. G_debug(3, "db_delete_table(): driver = %s, db = %s, table = %s\n",
  31. drvname, dbname, tblname);
  32. db_init_handle(&handle);
  33. db_init_string(&sql);
  34. /* Open driver and database */
  35. driver = db_start_driver(drvname);
  36. if (driver == NULL) {
  37. G_warning(_("Unable to open driver <%s>"), drvname);
  38. return DB_FAILED;
  39. }
  40. db_set_handle(&handle, dbname, NULL);
  41. if (db_open_database(driver, &handle) != DB_OK) {
  42. G_warning(_("Unable to open database <%s> by driver <%s>"),
  43. dbname, drvname);
  44. db_shutdown_driver(driver);
  45. return DB_FAILED;
  46. }
  47. /* Delete table */
  48. /* TODO test if the tables exist */
  49. db_set_string(&sql, "drop table ");
  50. db_append_string(&sql, tblname);
  51. G_debug(3, db_get_string(&sql));
  52. if (db_execute_immediate(driver, &sql) != DB_OK) {
  53. G_warning(_("Unable to drop table: '%s'"),
  54. db_get_string(&sql));
  55. db_close_database(driver);
  56. db_shutdown_driver(driver);
  57. return DB_FAILED;
  58. }
  59. db_close_database(driver);
  60. db_shutdown_driver(driver);
  61. return DB_OK;
  62. }