db.droptable.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #% keywords: database
  18. #% keywords: 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 os
  40. import grass.script as grass
  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')
  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. nuldev = file(os.devnull, 'w')
  61. if not grass.db_table_exist(table):
  62. grass.fatal(_("Table <%s> not found in database <%s>") % \
  63. (table, database))
  64. # check if table is used somewhere (connected to vector map)
  65. used = []
  66. vects = grass.list_strings('vect')
  67. for vect in vects:
  68. for f in grass.vector_db(vect, stderr = nuldev).itervalues():
  69. if not f:
  70. continue
  71. if f['table'] == table:
  72. used.append(vect)
  73. break
  74. if len(used) > 0:
  75. grass.warning(_("Deleting table <%s> which is attached to following map(s):") % table)
  76. for vect in used:
  77. grass.warning("%s" % vect)
  78. if not force:
  79. grass.message(_("The table <%s> would be deleted.") % table)
  80. grass.message("")
  81. grass.message(_("You must use the force flag to actually remove it. Exiting."))
  82. sys.exit(0)
  83. p = grass.feed_command('db.execute', input = '-', database = database, driver = driver)
  84. p.stdin.write("DROP TABLE " + table)
  85. p.stdin.close()
  86. p.wait()
  87. if p.returncode != 0:
  88. grass.fatal(_("Cannot continue (problem deleting table)."))
  89. if __name__ == "__main__":
  90. options, flags = grass.parser()
  91. main()