[GRASS-SVN] r43037 - grass/trunk/scripts/v.db.addcolumn

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Aug 10 11:22:39 EDT 2010


Author: martinl
Date: 2010-08-10 15:22:39 +0000 (Tue, 10 Aug 2010)
New Revision: 43037

Modified:
   grass/trunk/scripts/v.db.addcolumn/v.db.addcolumn.py
Log:
v.db.addcolumn: inform user when column already exists


Modified: grass/trunk/scripts/v.db.addcolumn/v.db.addcolumn.py
===================================================================
--- grass/trunk/scripts/v.db.addcolumn/v.db.addcolumn.py	2010-08-10 12:08:04 UTC (rev 43036)
+++ grass/trunk/scripts/v.db.addcolumn/v.db.addcolumn.py	2010-08-10 15:22:39 UTC (rev 43037)
@@ -55,38 +55,43 @@
 import grass.script as grass
 
 def main():
-    map = options['map']
-    layer = options['layer']
+    map     = options['map']
+    layer   = options['layer']
     columns = options['columns']
     columns = [col.strip() for col in columns.split(',')]
-
+    
     # does map exist in CURRENT mapset?
     mapset = grass.gisenv()['MAPSET']
     exists = bool(grass.find_file(map, element = 'vector', mapset = mapset)['file'])
-
+    
     if not exists:
 	grass.fatal(_("Vector map <%s> not found in current mapset") % map)
-
+    
     try:
         f = grass.vector_db(map)[int(layer)]
     except KeyError:
 	grass.fatal(_("There is no table connected to this map. Run v.db.connect or v.db.addtable first."))
-    table = f['table']
+    
+    table    = f['table']
     database = f['database']
-    driver = f['driver']
-
-    colnum = len(columns)
-
+    driver   = f['driver']
+    column_existing = grass.vector_columns(map, int(layer)).keys()    
+    
     for col in columns:
 	if not col:
 	    grass.fatal(_("There is an empty column. Did you leave a trailing comma?"))
-
+        col_name = col.split(' ')[0].strip()
+        if col_name in column_existing:
+            grass.error(_("Column <%s> is already in the table. Skipping.") % col_name)
+            continue
+        grass.verbose(_("Adding column <%s> to the table") % col_name)
 	p = grass.feed_command('db.execute', input = '-', database = database, driver = driver)
 	p.stdin.write("ALTER TABLE %s ADD COLUMN %s" % (table, col))
+        grass.debug("ALTER TABLE %s ADD COLUMN %s" % (table, col))
 	p.stdin.close()
 	if p.wait() != 0:
 	    grass.fatal(_("Unable to add column <%s>.") % col)
-
+    
     # write cmd history:
     grass.vector_history(map)
 



More information about the grass-commit mailing list