|
@@ -5,16 +5,15 @@
|
|
|
# MODULE: db.droptable
|
|
|
# AUTHOR(S): Markus Neteler
|
|
|
# Converted to Python by Glynn Clements
|
|
|
-# PURPOSE: interface to db.execute to drop an attribute table
|
|
|
-# COPYRIGHT: (C) 2007 by Markus Neteler and the GRASS Development Team
|
|
|
+# PURPOSE: Interface to db.execute to drop an attribute table
|
|
|
+# COPYRIGHT: (C) 2007, 2012 by Markus Neteler and the GRASS Development Team
|
|
|
#
|
|
|
-# This program is free software under the GNU General Public
|
|
|
-# License (>=v2). Read the file COPYING that comes with GRASS
|
|
|
-# for details.
|
|
|
+# This program is free software under the GNU General
|
|
|
+# Public License (>=v2). Read the file COPYING that
|
|
|
+# comes with GRASS for details.
|
|
|
#
|
|
|
#############################################################################
|
|
|
|
|
|
-
|
|
|
#%module
|
|
|
#% description: Drops an attribute table.
|
|
|
#% keywords: database
|
|
@@ -26,8 +25,20 @@
|
|
|
#% description: Force removal (required for actual deletion of files)
|
|
|
#%end
|
|
|
|
|
|
+#%option G_OPT_DB_DRIVER
|
|
|
+#% label: Name of database driver
|
|
|
+#% description: If not given then default driver is used
|
|
|
+#% guisection: Connection
|
|
|
+#%end
|
|
|
+
|
|
|
+#%option G_OPT_DB_DATABASE
|
|
|
+#% label: Name of database
|
|
|
+#% description: If not given then default database is used
|
|
|
+#% guisection: Connection
|
|
|
+#%end
|
|
|
+
|
|
|
#%option G_OPT_DB_TABLE
|
|
|
-#% description: Table name to drop
|
|
|
+#% description: Name of table to drop
|
|
|
#% required: yes
|
|
|
#%end
|
|
|
|
|
@@ -39,22 +50,30 @@ def main():
|
|
|
table = options['table']
|
|
|
force = flags['f']
|
|
|
|
|
|
- # check if DB parameters are set, and if not set them.
|
|
|
- grass.run_command('db.connect', flags = 'c')
|
|
|
-
|
|
|
+ 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()
|
|
|
- database = kv['database']
|
|
|
- driver = kv['driver']
|
|
|
+ if options['database']:
|
|
|
+ database = options['database']
|
|
|
+ else:
|
|
|
+ database = kv['database']
|
|
|
+ if options['driver']:
|
|
|
+ driver = kv['driver']
|
|
|
+ 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, stdout = nuldev, stderr = nuldev):
|
|
|
- grass.fatal(_("Table <%s> not found in current mapset") % 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')
|
|
@@ -69,20 +88,20 @@ def main():
|
|
|
grass.warning(_("Deleting table <%s> which is attached to following map(s):") % table)
|
|
|
for vect in used:
|
|
|
grass.message(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()
|