-
-
Notifications
You must be signed in to change notification settings - Fork 291
/
db.droptable.py
executable file
·105 lines (87 loc) · 2.93 KB
/
db.droptable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
############################################################################
#
# 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, 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.
#
#############################################################################
#%module
#% description: Drops an attribute table.
#% keyword: database
#% keyword: attribute table
#%end
#%flag
#% key: f
#% 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: Name of table to drop
#% required: yes
#%end
import sys
import grass.script as grass
# i18N
import os
import gettext
gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
def main():
table = options['table']
force = flags['f']
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', quiet=True)
kv = grass.db_connection()
if options['database']:
database = options['database']
else:
database = kv['database']
if options['driver']:
driver = options['driver']
else:
driver = kv['driver']
# schema needed for PG?
if force:
grass.message(_("Forcing ..."))
# check if table exists
if not grass.db_table_exist(table):
grass.warning(_("Table <%s> not found in database <%s>") %
(table, database))
sys.exit(0)
# check if table is used somewhere (connected to vector map)
used = grass.db.db_table_in_vector(table)
if used:
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()