|
@@ -3,7 +3,7 @@
|
|
|
############################################################################
|
|
|
#
|
|
|
# MODULE: db.droptable
|
|
|
-# AUTHOR(S): Markus Neteler
|
|
|
+# AUTHOR(S): Markus Neteler
|
|
|
# Converted to Python by Glynn Clements
|
|
|
# PURPOSE: Interface to db.execute to drop an attribute table
|
|
|
# COPYRIGHT: (C) 2007, 2012 by Markus Neteler and the GRASS Development Team
|
|
@@ -43,9 +43,9 @@
|
|
|
#%end
|
|
|
|
|
|
import sys
|
|
|
-import os
|
|
|
import grass.script as grass
|
|
|
|
|
|
+
|
|
|
def main():
|
|
|
table = options['table']
|
|
|
force = flags['f']
|
|
@@ -53,7 +53,7 @@ def main():
|
|
|
if not options['driver'] or not options['database']:
|
|
|
# check if DB parameters are set, and if not set them.
|
|
|
grass.run_command('db.connect', flags = 'c')
|
|
|
-
|
|
|
+
|
|
|
kv = grass.db_connection()
|
|
|
if options['database']:
|
|
|
database = options['database']
|
|
@@ -64,45 +64,35 @@ def main():
|
|
|
else:
|
|
|
driver = kv['driver']
|
|
|
# schema needed for PG?
|
|
|
-
|
|
|
+
|
|
|
if force:
|
|
|
grass.message(_("Forcing ..."))
|
|
|
-
|
|
|
+
|
|
|
# check if table exists
|
|
|
- nuldev = file(os.devnull, 'w')
|
|
|
if not grass.db_table_exist(table):
|
|
|
grass.fatal(_("Table <%s> not found in database <%s>") % \
|
|
|
(table, database))
|
|
|
-
|
|
|
+
|
|
|
# check if table is used somewhere (connected to vector map)
|
|
|
- used = []
|
|
|
- vects = grass.list_strings('vect')
|
|
|
- for vect in vects:
|
|
|
- for f in grass.vector_db(vect, stderr = nuldev).itervalues():
|
|
|
- if not f:
|
|
|
- continue
|
|
|
- if f['table'] == table:
|
|
|
- used.append(vect)
|
|
|
- break
|
|
|
+ used = grass.db.db_table_in_vector(table)
|
|
|
if len(used) > 0:
|
|
|
grass.warning(_("Deleting table <%s> which is attached to following map(s):") % table)
|
|
|
for vect in used:
|
|
|
grass.warning("%s" % vect)
|
|
|
-
|
|
|
+
|
|
|
if not force:
|
|
|
grass.message(_("The table <%s> would be deleted.") % table)
|
|
|
grass.message("")
|
|
|
grass.message(_("You must use the force flag to actually remove it. Exiting."))
|
|
|
sys.exit(0)
|
|
|
-
|
|
|
+
|
|
|
p = grass.feed_command('db.execute', input = '-', database = database, driver = driver)
|
|
|
p.stdin.write("DROP TABLE " + table)
|
|
|
p.stdin.close()
|
|
|
p.wait()
|
|
|
if p.returncode != 0:
|
|
|
grass.fatal(_("Cannot continue (problem deleting table)."))
|
|
|
-
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
options, flags = grass.parser()
|
|
|
main()
|
|
|
-
|