v.db.renamecolumn.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.db.renamecolumn
  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.dropcolumn
  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. #% keyword: vector
  23. #% keyword: attribute table
  24. #% keyword: database
  25. #%end
  26. #%option G_OPT_V_MAP
  27. #%end
  28. #%option G_OPT_V_FIELD
  29. #%end
  30. #%option
  31. #% key: column
  32. #% type: string
  33. #% description: Old and new name of the column (old,new)
  34. #% required: yes
  35. #% multiple: no
  36. #% key_desc: oldcol,newcol
  37. #%end
  38. import sys
  39. import os
  40. import grass.script as grass
  41. def main():
  42. map = options['map']
  43. layer = options['layer']
  44. column = options['column']
  45. mapset = grass.gisenv()['MAPSET']
  46. if not grass.find_file(map, element = 'vector', mapset = mapset):
  47. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  48. f = grass.vector_layer_db(map, layer)
  49. table = f['table']
  50. keycol = f['key']
  51. database = f['database']
  52. driver = f['driver']
  53. if not table:
  54. grass.fatal(_("There is no table connected to the input vector map. Cannot rename any column"))
  55. cols = column.split(',')
  56. oldcol = cols[0]
  57. newcol = cols[1]
  58. if driver == "dbf":
  59. if len(newcol) > 10:
  60. grass.fatal(_("Column name <%s> too long. The DBF driver supports column names not longer than 10 characters") % newcol)
  61. if oldcol == keycol:
  62. grass.fatal(_("Cannot rename column <%s> as it is needed to keep table <%s> connected to the input vector map") % (oldcol, table))
  63. # describe old col
  64. oldcoltype = None
  65. for f in grass.db_describe(table)['cols']:
  66. if f[0] != oldcol:
  67. continue
  68. oldcoltype = f[1]
  69. oldcollength = f[2]
  70. # old col there?
  71. if not oldcoltype:
  72. grass.fatal(_("Column <%s> not found in table <%s>") % (oldcol, table))
  73. # some tricks
  74. if driver in ['sqlite', 'dbf']:
  75. if oldcoltype.upper() == "CHARACTER":
  76. colspec = "%s varchar(%s)" % (newcol, oldcollength)
  77. else:
  78. colspec = "%s %s" % (newcol, oldcoltype)
  79. grass.run_command('v.db.addcolumn', map = map, layer = layer, column = colspec)
  80. sql = "UPDATE %s SET %s=%s" % (table, newcol, oldcol)
  81. grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = sql)
  82. grass.run_command('v.db.dropcolumn', map = map, layer = layer, column = oldcol)
  83. elif driver == 'mysql':
  84. if oldcoltype.upper() == "CHARACTER":
  85. newcoltype = "varchar(%s)" % (oldcollength)
  86. else:
  87. newcoltype = oldcoltype
  88. sql = "ALTER TABLE %s CHANGE %s %s %s" % (table, oldcol, newcol, newcoltype)
  89. grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = sql)
  90. else:
  91. sql = "ALTER TABLE %s RENAME %s TO %s" % (table, oldcol, newcol)
  92. grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = sql)
  93. # write cmd history:
  94. grass.vector_history(map)
  95. if __name__ == "__main__":
  96. options, flags = grass.parser()
  97. main()