v.db.renamecolumn.py 4.0 KB

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