v.db.dropcolumn.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.db.dropcolumn
  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.addcolumn
  10. # - with special trick for SQLite
  11. # COPYRIGHT: (C) 2007 by the GRASS Development Team
  12. #
  13. # This program is free software under the GNU General Public
  14. # License (>=v2). Read the file COPYING that comes with GRASS
  15. # for details.
  16. #
  17. #############################################################################
  18. #%module
  19. #% description: Drops a column from the attribute table connected to a given vector map.
  20. #% keywords: vector
  21. #% keywords: database
  22. #% keywords: attribute table
  23. #%end
  24. #%option G_OPT_V_MAP
  25. #% key: map
  26. #%end
  27. #%option G_OPT_V_FIELD
  28. #%end
  29. #%option G_OPT_DB_COLUMN
  30. #% description: Name of attribute column to drop
  31. #% required: yes
  32. #%end
  33. import sys
  34. import os
  35. import string
  36. import grass.script as grass
  37. def main():
  38. map = options['map']
  39. layer = options['layer']
  40. column = options['column']
  41. mapset = grass.gisenv()['MAPSET']
  42. # does map exist in CURRENT mapset?
  43. if not grass.find_file(map, element = 'vector', mapset = mapset):
  44. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  45. f = grass.vector_layer_db(map, layer)
  46. table = f['table']
  47. keycol = f['key']
  48. database = f['database']
  49. driver = f['driver']
  50. if not table:
  51. grass.fatal(_("There is no table connected to the input vector map. "
  52. "Unable to delete any column. Exiting."))
  53. if column == keycol:
  54. grass.fatal(_("Unable to delete <%s> column as it is needed to keep table <%s> "
  55. "connected to the input vector map <%s>") % \
  56. (column, table, map))
  57. if not grass.vector_columns(map, layer).has_key(column):
  58. grass.fatal(_("Column <%s> not found in table <%s>") % (column, table))
  59. if driver == "sqlite":
  60. # echo "Using special trick for SQLite"
  61. # http://www.sqlite.org/faq.html#q13
  62. colnames = []
  63. coltypes = []
  64. for f in grass.db_describe(table)['cols']:
  65. if f[0] == column:
  66. continue
  67. colnames.append(f[0])
  68. coltypes.append("%s %s" % (f[0], f[1]))
  69. colnames = ", ".join(colnames)
  70. coltypes = ", ".join(coltypes)
  71. cmds = [
  72. "BEGIN TRANSACTION",
  73. "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
  74. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  75. "DROP TABLE ${table}",
  76. "CREATE TABLE ${table}(${coldef})",
  77. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  78. "CREATE UNIQUE INDEX ${table}_cat ON ${table} (${keycol} )",
  79. "DROP TABLE ${table}_backup",
  80. "COMMIT"
  81. ]
  82. tmpl = string.Template(';\n'.join(cmds))
  83. sql = tmpl.substitute(table = table, coldef = coltypes, colnames = colnames, keycol = keycol)
  84. else:
  85. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  86. if grass.write_command('db.execute', input = '-', database = database, driver = driver,
  87. stdin = sql) != 0:
  88. grass.fatal(_("Deleting column failed"))
  89. # write cmd history:
  90. grass.vector_history(map)
  91. if __name__ == "__main__":
  92. options, flags = grass.parser()
  93. main()