db.droptable.py 3.0 KB

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