db.droptable.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: db.droptable
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Interface to db.execute to drop an attribute table
  8. # COPYRIGHT: (C) 2007, 2012 by Markus Neteler and the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General
  11. # Public License (>=v2). Read the file COPYING that
  12. # comes with GRASS for details.
  13. #
  14. #############################################################################
  15. # %module
  16. # % description: Drops an attribute table.
  17. # % keyword: database
  18. # % keyword: attribute table
  19. # %end
  20. # %flag
  21. # % key: f
  22. # % description: Force removal (required for actual deletion of files)
  23. # %end
  24. # %option G_OPT_DB_DRIVER
  25. # % label: Name of database driver
  26. # % description: If not given then default driver is used
  27. # % guisection: Connection
  28. # %end
  29. # %option G_OPT_DB_DATABASE
  30. # % label: Name of database
  31. # % description: If not given then default database is used
  32. # % guisection: Connection
  33. # %end
  34. # %option G_OPT_DB_TABLE
  35. # % description: Name of table to drop
  36. # % required: yes
  37. # %end
  38. import sys
  39. import grass.script as grass
  40. from grass.script.utils import encode
  41. def main():
  42. table = options["table"]
  43. force = flags["f"]
  44. if not options["driver"] or not options["database"]:
  45. # check if DB parameters are set, and if not set them.
  46. grass.run_command("db.connect", flags="c", quiet=True)
  47. kv = grass.db_connection()
  48. if options["database"]:
  49. database = options["database"]
  50. else:
  51. database = kv["database"]
  52. if options["driver"]:
  53. driver = options["driver"]
  54. else:
  55. driver = kv["driver"]
  56. # schema needed for PG?
  57. if force:
  58. grass.message(_("Forcing ..."))
  59. # check if table exists
  60. if not grass.db_table_exist(table):
  61. grass.warning(_("Table <%s> not found in database <%s>") % (table, database))
  62. sys.exit(0)
  63. # check if table is used somewhere (connected to vector map)
  64. used = grass.db.db_table_in_vector(table)
  65. if used:
  66. grass.warning(
  67. _("Deleting table <%s> which is attached to following map(s):") % table
  68. )
  69. for vect in used:
  70. grass.warning("%s" % vect)
  71. if not force:
  72. grass.message(_("The table <%s> would be deleted.") % table)
  73. grass.message("")
  74. grass.message(_("You must use the force flag to actually remove it. Exiting."))
  75. sys.exit(0)
  76. p = grass.feed_command("db.execute", input="-", database=database, driver=driver)
  77. p.stdin.write(encode("DROP TABLE " + table))
  78. p.stdin.close()
  79. p.wait()
  80. if p.returncode != 0:
  81. grass.fatal(_("Cannot continue (problem deleting table)."))
  82. if __name__ == "__main__":
  83. options, flags = grass.parser()
  84. main()