[GRASS-SVN] r38098 - grass/trunk/scripts/v.db.renamecolumn

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jun 27 07:05:11 EDT 2009


Author: martinl
Date: 2009-06-27 07:05:11 -0400 (Sat, 27 Jun 2009)
New Revision: 38098

Added:
   grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.html
   grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.py
Removed:
   grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.html
   grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.py
Log:
v.db.renamecol renamed to v.db.renamecolumn (part 3)


Deleted: grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.html
===================================================================
--- grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.html	2009-06-27 11:03:25 UTC (rev 38097)
+++ grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.html	2009-06-27 11:05:11 UTC (rev 38098)
@@ -1,50 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<em>v.db.renamecolumn</em> renames a column in the attribute table connected
-to a given vector map. It automatically checks the connection for the specified
-layer.
-
-<h2>NOTES</h2>
-
-If the map table is connected through the DBF or SQLite drivers, the renaming
-is internally done by adding a new column with new name, transferring the contents
-of the old column to the new column and dropping the old column. This is needed
-as DBF or SQLite do not support "ALTER TABLE" command to rename columns. Due to
-this the renamed column is found as last column of the table, it's original position
-cannot be maintained.
-
-<p>
-The SQLite driver will exit with an error if the column rename involves only a change of 
-case, i.e., upper-to-lowercase, or lower-to-uppercase. The SQLite protocol considers "NAME"
-and "name" to be identical column names. In cases like these, the user should rename the original
-column to an intermediary name, then rename the intermediary to the final name.
-
-<p>
-
-
-<h2>EXAMPLES</h2>
-
-Renaming a column:<br>
-<div class="code"><pre>
-v.info -c myroads
-v.db.renamecolumn myroads column=label,roadtype
-v.info -c myroads
-</pre></div>
-
-<h2>SEE ALSO</h2>
-
-<em><a HREF="db.execute.html">db.execute</a></em>,
-<em><a HREF="v.db.addcol.html">v.db.addcol</a></em>,
-<em><a HREF="v.db.addtable.html">v.db.addtable</a></em>,
-<em><a HREF="v.db.connect.html">v.db.connect</a></em>,
-<em><a HREF="v.db.dropcolumn.html">v.db.dropcolumn</a></em>,
-<em><a HREF="v.db.droptable.html">v.db.droptable</a></em>,
-<em><a HREF="v.db.select.html">v.db.select</a></em>,
-<em><a HREF="v.db.update.html">v.db.update</a></em>
-
-
-<h2>AUTHOR</h2>
-
-Markus Neteler
-
-<p><i>Last changed: $Date$</i>

Deleted: grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.py
===================================================================
--- grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.py	2009-06-27 11:03:25 UTC (rev 38097)
+++ grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.py	2009-06-27 11:05:11 UTC (rev 38098)
@@ -1,118 +0,0 @@
-#!/usr/bin/env python
-
-############################################################################
-#
-# MODULE:       v.db.renamecolumn
-# 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.dropcolumn
-#               - 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.script as grass
-
-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)
-
-    f = grass.vector_layer_db(map, layer)
-
-    table = f['table']
-    keycol = f['key']
-    database = f['database']
-    driver = f['driver']
-
-    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
-    for f in grass.db_describe(table)['cols']:
-	if f[0] != oldcol:
-	    continue
-	oldcoltype = f[1]
-	oldcollength = f[2]
-
-    # 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.dropcolumn', 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.vector_history(map)
-
-if __name__ == "__main__":
-    options, flags = grass.parser()
-    main()

Copied: grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.html (from rev 38097, grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.html)
===================================================================
--- grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.html	                        (rev 0)
+++ grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.html	2009-06-27 11:05:11 UTC (rev 38098)
@@ -0,0 +1,50 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.db.renamecolumn</em> renames a column in the attribute table connected
+to a given vector map. It automatically checks the connection for the specified
+layer.
+
+<h2>NOTES</h2>
+
+If the map table is connected through the DBF or SQLite drivers, the renaming
+is internally done by adding a new column with new name, transferring the contents
+of the old column to the new column and dropping the old column. This is needed
+as DBF or SQLite do not support "ALTER TABLE" command to rename columns. Due to
+this the renamed column is found as last column of the table, it's original position
+cannot be maintained.
+
+<p>
+The SQLite driver will exit with an error if the column rename involves only a change of 
+case, i.e., upper-to-lowercase, or lower-to-uppercase. The SQLite protocol considers "NAME"
+and "name" to be identical column names. In cases like these, the user should rename the original
+column to an intermediary name, then rename the intermediary to the final name.
+
+<p>
+
+
+<h2>EXAMPLES</h2>
+
+Renaming a column:<br>
+<div class="code"><pre>
+v.info -c myroads
+v.db.renamecolumn myroads column=label,roadtype
+v.info -c myroads
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em><a HREF="db.execute.html">db.execute</a></em>,
+<em><a HREF="v.db.addcol.html">v.db.addcol</a></em>,
+<em><a HREF="v.db.addtable.html">v.db.addtable</a></em>,
+<em><a HREF="v.db.connect.html">v.db.connect</a></em>,
+<em><a HREF="v.db.dropcolumn.html">v.db.dropcolumn</a></em>,
+<em><a HREF="v.db.droptable.html">v.db.droptable</a></em>,
+<em><a HREF="v.db.select.html">v.db.select</a></em>,
+<em><a HREF="v.db.update.html">v.db.update</a></em>
+
+
+<h2>AUTHOR</h2>
+
+Markus Neteler
+
+<p><i>Last changed: $Date$</i>


Property changes on: grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:mergeinfo
   + 
Added: svn:eol-style
   + native

Copied: grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.py (from rev 38097, grass/trunk/scripts/v.db.renamecolumn/v.db.renamecol.py)
===================================================================
--- grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.py	                        (rev 0)
+++ grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.py	2009-06-27 11:05:11 UTC (rev 38098)
@@ -0,0 +1,118 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       v.db.renamecolumn
+# 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.dropcolumn
+#               - 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.script as grass
+
+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)
+
+    f = grass.vector_layer_db(map, layer)
+
+    table = f['table']
+    keycol = f['key']
+    database = f['database']
+    driver = f['driver']
+
+    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
+    for f in grass.db_describe(table)['cols']:
+	if f[0] != oldcol:
+	    continue
+	oldcoltype = f[1]
+	oldcollength = f[2]
+
+    # 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.dropcolumn', 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.vector_history(map)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


Property changes on: grass/trunk/scripts/v.db.renamecolumn/v.db.renamecolumn.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mergeinfo
   + 
Added: svn:eol-style
   + native



More information about the grass-commit mailing list