Browse Source

v.db.addcolumn: inform user when column already exists

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@43037 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 15 years ago
parent
commit
8135e1d03f
1 changed files with 17 additions and 12 deletions
  1. 17 12
      scripts/v.db.addcolumn/v.db.addcolumn.py

+ 17 - 12
scripts/v.db.addcolumn/v.db.addcolumn.py

@@ -55,38 +55,43 @@ import os
 import grass.script as grass
 import grass.script as grass
 
 
 def main():
 def main():
-    map = options['map']
-    layer = options['layer']
+    map     = options['map']
+    layer   = options['layer']
     columns = options['columns']
     columns = options['columns']
     columns = [col.strip() for col in columns.split(',')]
     columns = [col.strip() for col in columns.split(',')]
-
+    
     # does map exist in CURRENT mapset?
     # does map exist in CURRENT mapset?
     mapset = grass.gisenv()['MAPSET']
     mapset = grass.gisenv()['MAPSET']
     exists = bool(grass.find_file(map, element = 'vector', mapset = mapset)['file'])
     exists = bool(grass.find_file(map, element = 'vector', mapset = mapset)['file'])
-
+    
     if not exists:
     if not exists:
 	grass.fatal(_("Vector map <%s> not found in current mapset") % map)
 	grass.fatal(_("Vector map <%s> not found in current mapset") % map)
-
+    
     try:
     try:
         f = grass.vector_db(map)[int(layer)]
         f = grass.vector_db(map)[int(layer)]
     except KeyError:
     except KeyError:
 	grass.fatal(_("There is no table connected to this map. Run v.db.connect or v.db.addtable first."))
 	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']
     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:
     for col in columns:
 	if not col:
 	if not col:
 	    grass.fatal(_("There is an empty column. Did you leave a trailing comma?"))
 	    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 = grass.feed_command('db.execute', input = '-', database = database, driver = driver)
 	p.stdin.write("ALTER TABLE %s ADD COLUMN %s" % (table, col))
 	p.stdin.write("ALTER TABLE %s ADD COLUMN %s" % (table, col))
+        grass.debug("ALTER TABLE %s ADD COLUMN %s" % (table, col))
 	p.stdin.close()
 	p.stdin.close()
 	if p.wait() != 0:
 	if p.wait() != 0:
 	    grass.fatal(_("Unable to add column <%s>.") % col)
 	    grass.fatal(_("Unable to add column <%s>.") % col)
-
+    
     # write cmd history:
     # write cmd history:
     grass.vector_history(map)
     grass.vector_history(map)