[GRASS-SVN] r48337 - grass/trunk/scripts/v.db.dropcolumn

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Sep 17 15:29:48 EDT 2011


Author: lucadelu
Date: 2011-09-17 12:29:48 -0700 (Sat, 17 Sep 2011)
New Revision: 48337

Modified:
   grass/trunk/scripts/v.db.dropcolumn/v.db.dropcolumn.py
Log:
implement remove of multi column, ticket 853

Modified: grass/trunk/scripts/v.db.dropcolumn/v.db.dropcolumn.py
===================================================================
--- grass/trunk/scripts/v.db.dropcolumn/v.db.dropcolumn.py	2011-09-17 18:49:16 UTC (rev 48336)
+++ grass/trunk/scripts/v.db.dropcolumn/v.db.dropcolumn.py	2011-09-17 19:29:48 UTC (rev 48337)
@@ -32,8 +32,7 @@
 #%option G_OPT_V_FIELD
 #%end
 
-#%option G_OPT_DB_COLUMN
-#% description: Name of attribute column to drop
+#%option G_OPT_DB_COLUMNS
 #% required: yes
 #%end
 
@@ -45,7 +44,7 @@
 def main():
     map    = options['map']
     layer  = options['layer']
-    column = options['column']
+    columns = options['columns'].split(',')
     
     mapset = grass.gisenv()['MAPSET']
     
@@ -64,48 +63,50 @@
 	grass.fatal(_("There is no table connected to the input vector map. "
                       "Unable to delete any column. Exiting."))
     
-    if column == keycol:
+    if keycol in columns:
 	grass.fatal(_("Unable to delete <%s> column as it is needed to keep table <%s> "
                       "connected to the input vector map <%s>") % \
-                        (column, table, map))
+                        (keycol, table, map))
     
-    if not grass.vector_columns(map, layer).has_key(column):
-	grass.fatal(_("Column <%s> not found in table <%s>") % (column, table))
-    
-    if driver == "sqlite":
-	# echo "Using special trick for SQLite"
-	# http://www.sqlite.org/faq.html#q13
-	colnames = []
-	coltypes = []
-	for f in grass.db_describe(table)['cols']:
-	    if f[0] == column:
-		continue
-	    colnames.append(f[0])
-	    coltypes.append("%s %s" % (f[0], f[1]))
+    for column in columns:
+        if not grass.vector_columns(map, layer).has_key(column):
+            grass.warning(_("Column <%s> not found in table <%s>. Skipped") % (column, table))
+            continue
+        
+        if driver == "sqlite":
+            # echo "Using special trick for SQLite"
+            # http://www.sqlite.org/faq.html#q13
+            colnames = []
+            coltypes = []
+            for f in grass.db_describe(table)['cols']:
+                if f[0] == column:
+                    continue
+                colnames.append(f[0])
+                coltypes.append("%s %s" % (f[0], f[1]))
 
-	colnames = ", ".join(colnames)
-	coltypes = ", ".join(coltypes)
+            colnames = ", ".join(colnames)
+            coltypes = ", ".join(coltypes)
 
-	cmds = [
-	    "BEGIN TRANSACTION",
-	    "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
-	    "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
-	    "DROP TABLE ${table}",
-	    "CREATE TABLE ${table}(${coldef})",
-	    "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
-	    "CREATE UNIQUE INDEX ${table}_cat ON ${table} (${keycol} )",
-	    "DROP TABLE ${table}_backup",
-	    "COMMIT"
-	    ]
-	tmpl = string.Template(';\n'.join(cmds))
-	sql = tmpl.substitute(table = table, coldef = coltypes, colnames = colnames, keycol = keycol)
-    else:
-	sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
+            cmds = [
+                "BEGIN TRANSACTION",
+                "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
+                "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
+                "DROP TABLE ${table}",
+                "CREATE TABLE ${table}(${coldef})",
+                "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
+                "CREATE UNIQUE INDEX ${table}_cat ON ${table} (${keycol} )",
+                "DROP TABLE ${table}_backup",
+                "COMMIT"
+                ]
+            tmpl = string.Template(';\n'.join(cmds))
+            sql = tmpl.substitute(table = table, coldef = coltypes, colnames = colnames, keycol = keycol)
+        else:
+            sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
+        
+        if grass.write_command('db.execute', input = '-', database = database, driver = driver,
+                              stdin = sql) != 0:
+            grass.fatal(_("Deleting column failed"))
     
-    if grass.write_command('db.execute', input = '-', database = database, driver = driver,
-			   stdin = sql) != 0:
-	grass.fatal(_("Deleting column failed"))
-    
     # write cmd history:
     grass.vector_history(map)
     



More information about the grass-commit mailing list