db.droptable.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  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. def main():
  41. table = options['table']
  42. force = flags['f']
  43. if not options['driver'] or not options['database']:
  44. # check if DB parameters are set, and if not set them.
  45. grass.run_command('db.connect', flags='c', quiet=True)
  46. kv = grass.db_connection()
  47. if options['database']:
  48. database = options['database']
  49. else:
  50. database = kv['database']
  51. if options['driver']:
  52. driver = options['driver']
  53. else:
  54. driver = kv['driver']
  55. # schema needed for PG?
  56. if force:
  57. grass.message(_("Forcing ..."))
  58. # check if table exists
  59. if not grass.db_table_exist(table):
  60. grass.fatal(_("Table <%s> not found in database <%s>") %
  61. (table, database))
  62. # check if table is used somewhere (connected to vector map)
  63. used = grass.db.db_table_in_vector(table)
  64. if used:
  65. grass.warning(_("Deleting table <%s> which is attached to following map(s):") % table)
  66. for vect in used:
  67. grass.warning("%s" % vect)
  68. if not force:
  69. grass.message(_("The table <%s> would be deleted.") % table)
  70. grass.message("")
  71. grass.message(_("You must use the force flag to actually remove it. Exiting."))
  72. sys.exit(0)
  73. p = grass.feed_command('db.execute', input='-', database=database,
  74. driver=driver)
  75. p.stdin.write("DROP TABLE " + table)
  76. p.stdin.close()
  77. p.wait()
  78. if p.returncode != 0:
  79. grass.fatal(_("Cannot continue (problem deleting table)."))
  80. if __name__ == "__main__":
  81. options, flags = grass.parser()
  82. main()