v.db.renamecol.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.db.renamecol
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: interface to db.execute to drop a column from the
  8. # attribute table connected to a given vector map
  9. # - Based on v.db.dropcol
  10. # - with special trick for SQLite and DBF (here the new col is
  11. # added/values copied/old col deleted)
  12. # COPYRIGHT: (C) 2007 by the GRASS Development Team
  13. #
  14. # This program is free software under the GNU General Public
  15. # License (>=v2). Read the file COPYING that comes with GRASS
  16. # for details.
  17. #
  18. # TODO: MySQL untested
  19. #############################################################################
  20. #%Module
  21. #% description: Renames a column in the attribute table connected to a given vector map.
  22. #% keywords: vector, database, attribute table
  23. #%End
  24. #%option
  25. #% key: map
  26. #% type: string
  27. #% gisprompt: old,vector,vector
  28. #% key_desc : name
  29. #% description: Vector map for which to rename attribute column
  30. #% required : yes
  31. #%end
  32. #%option
  33. #% key: layer
  34. #% type: integer
  35. #% description: Layer where to rename column
  36. #% answer: 1
  37. #% required : no
  38. #%end
  39. #%option
  40. #% key: column
  41. #% type: string
  42. #% description: Old and new name of the column (old,new)
  43. #% required : yes
  44. #% multiple: yes
  45. #% key_desc: oldcol,newcol
  46. #%end
  47. import sys
  48. import os
  49. import grass
  50. def error():
  51. def main():
  52. map = options['map']
  53. layer = options['layer']
  54. column = options['column']
  55. mapset = grass.gisenv()['MAPSET']
  56. if not grass.find_file(map, element = 'vector', mapset = mapset):
  57. grass.fatal("Vector map <%s> not found in current mapset" % map)
  58. s = grass.read_command('v.db.connect', flags = 'g', map = map, layer = layer);
  59. if not s:
  60. grass.fatal("An error occured while running v.db.connect")
  61. f = s.split()
  62. table = f[1]
  63. keycol = f[2]
  64. database = f[3]
  65. driver = f[4]
  66. if not table:
  67. grass.fatal("There is no table connected to the input vector map. Cannot rename any column")
  68. cols = column.split(',')
  69. oldcol = cols[0]
  70. newcol = cols[1]
  71. if $driver == "dbf":
  72. if len(newcol) > 10:
  73. grass.fatal("Column name <%s> too long. The DBF driver supports column names not longer than 10 characters" % newcol)
  74. if oldcol == keycol:
  75. grass.fatal("Cannot rename column <%s> as it is needed to keep table <%s> connected to the input vector map" % (oldcol, table))
  76. # describe old col
  77. oldcoltype = None
  78. s = grass.read_command('db.describe', flags = 'c', table = table)
  79. for l in s.splitlines():
  80. if not l.startswith('Column '):
  81. continue
  82. f = l.split(':')
  83. if f[1].lstrip() != oldcol:
  84. continue
  85. oldcoltype = f[2]
  86. oldcollength = f[3]
  87. # old col there?
  88. if not oldcol:
  89. grass.fatal("Column <%s> not found in table <%s>" % (coldcol, table))
  90. # some tricks
  91. if driver in ['sqlite', 'dbf']:
  92. if oldcoltype.upper() == "CHARACTER":
  93. colspec = "%s varchar(%s)" % (newcol, oldcollength)
  94. else:
  95. colspec = "%s %s" % (newcol, oldcoltype)
  96. grass.run_command('v.db.addcol', map = map, layer = layer, column = colspec)
  97. sql = "UPDATE %s SET %s=%s" % (table, newcol, oldcol)
  98. grass.write_command('db.execute', database = database, driver = driver, stdin = sql)
  99. grass.run_command('v.db.dropcol', map = map, layer = layer, column = oldcol)
  100. else:
  101. sql = "ALTER TABLE %s RENAME %s TO %s" % (table, oldcol, newcol)
  102. grass.write_command('db.execute', database = database, driver = driver, stdin = sql)
  103. # write cmd history:
  104. grass.run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
  105. if __name__ == "__main__":
  106. options, flags = grass.parser()
  107. main()