[GRASS-SVN] r53610 - grass/trunk/scripts/db.droptable

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Oct 29 10:52:20 PDT 2012


Author: martinl
Date: 2012-10-29 10:52:19 -0700 (Mon, 29 Oct 2012)
New Revision: 53610

Modified:
   grass/trunk/scripts/db.droptable/db.droptable.html
   grass/trunk/scripts/db.droptable/db.droptable.py
Log:
db.droptable: driver/database options added (required by v.db.reconnect.all)


Modified: grass/trunk/scripts/db.droptable/db.droptable.html
===================================================================
--- grass/trunk/scripts/db.droptable/db.droptable.html	2012-10-29 17:33:41 UTC (rev 53609)
+++ grass/trunk/scripts/db.droptable/db.droptable.html	2012-10-29 17:52:19 UTC (rev 53610)
@@ -1,19 +1,24 @@
 <h2>DESCRIPTION</h2>
 
-<em>db.droptable</em> drops an attribute table.
-If the <b>-f</b> force flag is not given then nothing is removed, instead
-a preview of the action to be taken is printed.
+<em>db.droptable</em> drops an attribute table.  If the <b>-f</b>
+force flag is not given then nothing is removed, instead a preview of
+the action to be taken is printed.
 
 <h2>NOTES</h2>
 
-<em>db.droptable</em> is a front-end to <em>db.execute</em> to allow easier
-usage. To some extent it is verified if the table is connected to a vector map
-to avoid accidential table removal.
+<em>db.droptable</em> is a front-end
+to <em><a href="db.execute.html">db.execute</a></em> to allow easier
+usage. To some extent it is verified if the table is connected to a
+vector map to avoid accidential table removal.
 
-<h2>EXAMPLE</h2>
+<h2>EXAMPLES</h2>
 
-Removing an attribute table:
-<p><div class="code"><pre>
+<h3>Removing an attribute table from default database</h3>
+
+<div class="code"><pre>
+# show default database
+db.connect -p
+
 # show available tables
 db.tables -p
 
@@ -21,23 +26,40 @@
 db.droptable table=sometable
 
 # actually drop the table
-db.droptable -f sometable
+db.droptable -f table=sometable
 </pre></div>
 
+<h3>Removing an attribute table from given database</h3>
+
+<em>db.droptable</em> allows to define optionally <b>driver</b>
+and <b>database</b> options different from default connection settings
+(<tt>db.connect -p</tt>).
+
+<div class="code"><pre>
+# drop the table from SQLite database
+db.droptable -f table=sometable driver=sqlite database=/opt/sqlite.db
+</pre></div>
+
 <h2>SEE ALSO</h2>
 
 <em>
-<a href="db.describe.html">db.describe</a>,
-<a href="db.droptable.html">db.droptable</a>,
+<a href="db.dropdb.html">db.dropdb</a>,
+<a href="db.dropcolumn.html">db.dropcolumn</a>,
 <a href="db.execute.html">db.execute</a>,
 <a href="db.login.html">db.login</a>,
+<a href="db.connect.html">db.connect</a>,
 <a href="db.tables.html">db.tables</a>,
+<a href="db.describe.html">db.describe</a>,
 <a href="v.db.droptable.html">v.db.droptable</a>,
-<a href="sql.html">GRASS SQL interface</a>
 </em>
 
-<h2>AUTHOR</h2>
+<p>
+<a href="sql.html">GRASS SQL interface</a>
 
-Markus Neteler
+<h2>AUTHORS</h2>
 
-<p><i>Last changed: $Date$</i>
+Markus Neteler<br>
+Driver and database options added by Martin Landa, Czech Technical University in Prague, Czech Republic
+
+<p>
+<i>Last changed: $Date$</i>

Modified: grass/trunk/scripts/db.droptable/db.droptable.py
===================================================================
--- grass/trunk/scripts/db.droptable/db.droptable.py	2012-10-29 17:33:41 UTC (rev 53609)
+++ grass/trunk/scripts/db.droptable/db.droptable.py	2012-10-29 17:52:19 UTC (rev 53610)
@@ -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 @@
     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 @@
 	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()



More information about the grass-commit mailing list