[GRASS-SVN] r33594 - in grass/trunk/scripts: db.dropcol v.db.dropcol v.db.join v.db.renamecol

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 29 11:33:08 EDT 2008


Author: glynn
Date: 2008-09-29 11:33:08 -0400 (Mon, 29 Sep 2008)
New Revision: 33594

Added:
   grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py
Modified:
   grass/trunk/scripts/db.dropcol/db.dropcol.py
   grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py
   grass/trunk/scripts/v.db.join/v.db.join.py
Log:
Convert v.db.renamecol to Python
Fix bugs in parsing "db.describe -c" output


Modified: grass/trunk/scripts/db.dropcol/db.dropcol.py
===================================================================
--- grass/trunk/scripts/db.dropcol/db.dropcol.py	2008-09-29 14:21:41 UTC (rev 33593)
+++ grass/trunk/scripts/db.dropcol/db.dropcol.py	2008-09-29 15:33:08 UTC (rev 33594)
@@ -84,12 +84,13 @@
 	#echo "Using special trick for SQLite"
 	# http://www.sqlite.org/faq.html#q13
 	colnames = []
-	coldef = []
+	coltypes = []
 	s = grass.read_command('db.describe', flags = 'c', table = table)
 	for l in s.splitlines():
 	    if not l.startswith('Column '):
 		continue
 	    f = l.split(':')
+	    f[1] = f[1].lstrip()
 	    if f[1] == column:
 		continue
 	    colnames.append(f[1])

Modified: grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py
===================================================================
--- grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py	2008-09-29 14:21:41 UTC (rev 33593)
+++ grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py	2008-09-29 15:33:08 UTC (rev 33594)
@@ -86,12 +86,13 @@
 	#echo "Using special trick for SQLite"
 	# http://www.sqlite.org/faq.html#q13
 	colnames = []
-	coldef = []
+	coltypes = []
 	s = grass.read_command('db.describe', flags = 'c', table = table)
 	for l in s.splitlines():
 	    if not l.startswith('Column '):
 		continue
 	    f = l.split(':')
+	    f[1] = f[1].lstrip()
 	    if f[1] == column:
 		continue
 	    colnames.append(f[1])

Modified: grass/trunk/scripts/v.db.join/v.db.join.py
===================================================================
--- grass/trunk/scripts/v.db.join/v.db.join.py	2008-09-29 14:21:41 UTC (rev 33593)
+++ grass/trunk/scripts/v.db.join/v.db.join.py	2008-09-29 15:33:08 UTC (rev 33594)
@@ -98,12 +98,13 @@
     template = string.Template("UPDATE $table SET $colname=(%s);" % select)
 
     for col in cols:
-	colname = col[0]
-	if len(col) > 2:
-	    coltype = "%s(%s)" % col[1:3]
+	colname = col[1].lstrip()
+	if len(col) > 3:
+	    coltype = "%s(%s)" % (col[2], col[3])
 	else:
-	    coltype = "%s" % col[1]
+	    coltype = "%s" % col[2]
 	colspec = "%s %s" % (colname, coltype)
+
 	if grass.run_command('v.db.addcol', map = map, col = colspec) != 0:
 	    grass.fatal("Error creating column <%s>." % colname)
 
@@ -111,15 +112,11 @@
 				   otable = otable, ocolumn = ocolumn,
 				   colname = colname)
 
-	p = grass.feed_command('db.execute')
-	p.stdin.write(stmt)
-	p.stdin.close()
-	p.wait()
-	if p.returncode != 0:
+	if grass.write_command('db.execute', stdin = stmt) != 0:
 	    grass.fatal("Error filling column <%s>." % colname)
 
     # write cmd history:
-    grass.run_command('v.support', map = map, cmdhist = cmdline)
+    grass.run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
 
 if __name__ == "__main__":
     options, flags = grass.parser()


Property changes on: grass/trunk/scripts/v.db.join/v.db.join.py
___________________________________________________________________
Name: svn:executable
   + *

Added: grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py
===================================================================
--- grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py	                        (rev 0)
+++ grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py	2008-09-29 15:33:08 UTC (rev 33594)
@@ -0,0 +1,126 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       v.db.renamecol
+# AUTHOR(S):    Markus Neteler
+#               Converted to Python by Glynn Clements
+# PURPOSE:      interface to db.execute to drop a column from the 
+#               attribute table connected to a given vector map
+#               - Based on v.db.dropcol
+#               - with special trick for SQLite and DBF (here the new col is 
+#                 added/values copied/old col deleted)
+# COPYRIGHT:    (C) 2007 by 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.
+#
+# TODO: MySQL untested
+#############################################################################
+
+
+#%Module
+#%  description: Renames a column in the attribute table connected to a given vector map.
+#%  keywords: vector, database, attribute table
+#%End
+#%option
+#% key: map
+#% type: string
+#% gisprompt: old,vector,vector
+#% key_desc : name
+#% description: Vector map for which to rename attribute column
+#% required : yes
+#%end
+#%option
+#% key: layer
+#% type: integer
+#% description: Layer where to rename column
+#% answer: 1
+#% required : no
+#%end
+#%option
+#% key: column
+#% type: string
+#% description: Old and new name of the column (old,new)
+#% required : yes
+#% multiple: yes
+#% key_desc: oldcol,newcol
+#%end
+
+import sys
+import os
+import grass
+
+def error():
+
+def main():
+    map = options['map']
+    layer = options['layer']
+    column = options['column']
+
+    mapset = grass.gisenv()['MAPSET']
+
+    if not grass.find_file(map, element = 'vector', mapset = mapset):
+	grass.fatal("Vector map <%s> not found in current mapset" % map)
+
+    s = grass.read_command('v.db.connect', flags = 'g', map = map, layer = layer);
+    if not s:
+	grass.fatal("An error occured while running v.db.connect")
+    f = s.split()
+    table = f[1]
+    keycol = f[2]
+    database = f[3]
+    driver = f[4]
+
+    if not table:
+	grass.fatal("There is no table connected to the input vector map. Cannot rename any column")
+
+    cols = column.split(',')
+    oldcol = cols[0]
+    newcol = cols[1]
+
+    if $driver == "dbf":
+	if len(newcol) > 10:
+	    grass.fatal("Column name <%s> too long. The DBF driver supports column names not longer than 10 characters" % newcol)
+
+    if oldcol == keycol:
+	grass.fatal("Cannot rename column <%s> as it is needed to keep table <%s> connected to the input vector map" % (oldcol, table))
+
+    # describe old col
+    oldcoltype = None
+    s = grass.read_command('db.describe', flags = 'c', table = table)
+    for l in s.splitlines():
+	if not l.startswith('Column '):
+	    continue
+	f = l.split(':')
+	if f[1].lstrip() != oldcol:
+	    continue
+	oldcoltype = f[2]
+	oldcollength = f[3]
+
+    # old col there?
+    if not oldcol:
+	grass.fatal("Column <%s> not found in table <%s>" % (coldcol, table))
+
+    # some tricks
+    if driver in ['sqlite', 'dbf']:
+	if oldcoltype.upper() == "CHARACTER":
+	    colspec = "%s varchar(%s)" % (newcol, oldcollength)
+	else:
+	    colspec = "%s %s" % (newcol, oldcoltype)
+
+	grass.run_command('v.db.addcol', map = map, layer = layer, column = colspec)
+	sql = "UPDATE %s SET %s=%s" % (table, newcol, oldcol)
+	grass.write_command('db.execute', database = database, driver = driver, stdin = sql)
+	grass.run_command('v.db.dropcol', map = map, layer = layer, column = oldcol)
+    else:
+	sql = "ALTER TABLE %s RENAME %s TO %s" % (table, oldcol, newcol)
+	grass.write_command('db.execute', database = database, driver = driver, stdin = sql)
+
+    # write cmd history:
+    grass.run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


Property changes on: grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the grass-commit mailing list